reportlab/lib/abag.py
author andy_robinson
Wed, 17 Jul 2002 22:46:24 +0000
changeset 1677 1450177dd19e
parent 1559 2bb75ab0d0aa
child 1683 7fa753e4420a
permissions -rw-r--r--
Exterminated all tab characters and added a test to make sure they never come back.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
517
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
     1
#copyright ReportLab Inc. 2000
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
     2
#see license.txt for license details
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/lib/abag.py?cvsroot=reportlab
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/lib/abag.py,v 1.7 2002/07/17 22:46:23 andy_robinson Exp $
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
     5
__version__=''' $Id: abag.py,v 1.7 2002/07/17 22:46:23 andy_robinson Exp $ '''
561
58942687b2e1 Docstring modified.
dinu_gherman
parents: 548
diff changeset
     6
517
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
     7
class ABag:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
     8
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
     9
    'Attribute Bag' - a trivial BAG class for holding attributes.
566
36bbae746b58 Changed comment to be slightly more meaningful
rgbecker
parents: 561
diff changeset
    10
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    11
    You may initialize with keyword arguments.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    12
    a = ABag(k0=v0,....,kx=vx,....) ==> getattr(a,'kx')==vx
566
36bbae746b58 Changed comment to be slightly more meaningful
rgbecker
parents: 561
diff changeset
    13
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    14
    c = a.clone(ak0=av0,.....) copy with optional additional attributes.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    15
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    16
    def __init__(self,**attr):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    17
        for k,v in attr.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    18
            setattr(self,k,v)
517
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    19
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    20
    def clone(self,**attr):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    21
        n = apply(ABag,(),self.__dict__)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    22
        if attr != {}: apply(ABag.__init__,(n,),attr)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    23
        return n
548
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    24
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    25
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    26
        import string
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    27
        n = self.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    28
        L = [n+"("]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    29
        keys = self.__dict__.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    30
        for k in keys:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    31
            v = getattr(self, k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    32
            rk = repr(k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    33
            rv = repr(v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    34
            rk = "  "+string.replace(rk, "\n", "\n  ")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    35
            rv = "    "+string.replace(rv, "\n", "\n    ")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    36
            L.append(rk)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    37
            L.append(rv)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    38
        L.append(") #"+n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    39
        return string.join(L, "\n")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    40
        
548
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    41
if __name__=="__main__":
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    42
    AB = ABag(a=1, c="hello")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    43
    CD = AB.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    44
    print AB
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    45
    print CD