reportlab/lib/abag.py
author rgbecker
Tue, 19 Dec 2006 14:50:51 +0000
changeset 2721 f52c73a6fa96
parent 2332 2a7ab4405e18
permissions -rw-r--r--
abag.py: improve speed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 1683
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
517
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 1683
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/abag.py
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 1683
diff changeset
     4
__version__=''' $Id$ '''
561
58942687b2e1 Docstring modified.
dinu_gherman
parents: 548
diff changeset
     5
517
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
     6
class ABag:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
     7
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
     8
    'Attribute Bag' - a trivial BAG class for holding attributes.
566
36bbae746b58 Changed comment to be slightly more meaningful
rgbecker
parents: 561
diff changeset
     9
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    10
    You may initialize with keyword arguments.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    11
    a = ABag(k0=v0,....,kx=vx,....) ==> getattr(a,'kx')==vx
566
36bbae746b58 Changed comment to be slightly more meaningful
rgbecker
parents: 561
diff changeset
    12
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    13
    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
    14
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    15
    def __init__(self,**attr):
2721
f52c73a6fa96 abag.py: improve speed
rgbecker
parents: 2332
diff changeset
    16
        self.__dict__.update(attr)
517
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    17
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    18
    def clone(self,**attr):
2721
f52c73a6fa96 abag.py: improve speed
rgbecker
parents: 2332
diff changeset
    19
        n = ABag(**self.__dict__)
f52c73a6fa96 abag.py: improve speed
rgbecker
parents: 2332
diff changeset
    20
        if attr: n.__dict__.update(attr)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    21
        return n
548
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    22
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    23
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    24
        import string
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    25
        n = self.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    26
        L = [n+"("]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    27
        keys = self.__dict__.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    28
        for k in keys:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    29
            v = getattr(self, k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    30
            rk = repr(k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    31
            rv = repr(v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    32
            rk = "  "+string.replace(rk, "\n", "\n  ")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    33
            rv = "    "+string.replace(rv, "\n", "\n    ")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    34
            L.append(rk)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    35
            L.append(rv)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    36
        L.append(") #"+n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    37
        return string.join(L, "\n")
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
    38
548
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    39
if __name__=="__main__":
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    40
    AB = ABag(a=1, c="hello")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    41
    CD = AB.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    42
    print AB
2721
f52c73a6fa96 abag.py: improve speed
rgbecker
parents: 2332
diff changeset
    43
    print CD