src/reportlab/lib/abag.py
changeset 2964 32352db0d71e
parent 2721 f52c73a6fa96
child 3029 eded59f94021
equal deleted inserted replaced
2963:c414c0ab69e7 2964:32352db0d71e
       
     1 #Copyright ReportLab Europe Ltd. 2000-2004
       
     2 #see license.txt for license details
       
     3 #history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/abag.py
       
     4 __version__=''' $Id$ '''
       
     5 
       
     6 class ABag:
       
     7     """
       
     8     'Attribute Bag' - a trivial BAG class for holding attributes.
       
     9 
       
    10     You may initialize with keyword arguments.
       
    11     a = ABag(k0=v0,....,kx=vx,....) ==> getattr(a,'kx')==vx
       
    12 
       
    13     c = a.clone(ak0=av0,.....) copy with optional additional attributes.
       
    14     """
       
    15     def __init__(self,**attr):
       
    16         self.__dict__.update(attr)
       
    17 
       
    18     def clone(self,**attr):
       
    19         n = ABag(**self.__dict__)
       
    20         if attr: n.__dict__.update(attr)
       
    21         return n
       
    22 
       
    23     def __repr__(self):
       
    24         import string
       
    25         n = self.__class__.__name__
       
    26         L = [n+"("]
       
    27         keys = self.__dict__.keys()
       
    28         for k in keys:
       
    29             v = getattr(self, k)
       
    30             rk = repr(k)
       
    31             rv = repr(v)
       
    32             rk = "  "+string.replace(rk, "\n", "\n  ")
       
    33             rv = "    "+string.replace(rv, "\n", "\n    ")
       
    34             L.append(rk)
       
    35             L.append(rv)
       
    36         L.append(") #"+n)
       
    37         return string.join(L, "\n")
       
    38 
       
    39 if __name__=="__main__":
       
    40     AB = ABag(a=1, c="hello")
       
    41     CD = AB.clone()
       
    42     print AB
       
    43     print CD