reportlab/lib/abag.py
author rgbecker
Fri, 19 Jan 2001 11:45:16 +0000
changeset 566 36bbae746b58
parent 561 58942687b2e1
child 1440 243d35446390
permissions -rw-r--r--
Changed comment to be slightly more meaningful
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
566
36bbae746b58 Changed comment to be slightly more meaningful
rgbecker
parents: 561
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/lib/abag.py,v 1.4 2001/01/19 11:45:16 rgbecker Exp $
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:
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
     7
	"""
561
58942687b2e1 Docstring modified.
dinu_gherman
parents: 548
diff changeset
     8
	A trivial BAG class for holding attributes.
566
36bbae746b58 Changed comment to be slightly more meaningful
rgbecker
parents: 561
diff changeset
     9
36bbae746b58 Changed comment to be slightly more meaningful
rgbecker
parents: 561
diff changeset
    10
	a = ABag(k0=v0,....,kx=vx,....) ==> getattr(a,'kx')==vx
36bbae746b58 Changed comment to be slightly more meaningful
rgbecker
parents: 561
diff changeset
    11
36bbae746b58 Changed comment to be slightly more meaningful
rgbecker
parents: 561
diff changeset
    12
	c = a.clone(ak0=av0,.....) copy with optional additional attributes.
517
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    13
	"""
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    14
	def __init__(self,**attr):
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    15
		for k,v in attr.items():
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    16
			setattr(self,k,v)
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    17
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    18
	def clone(self,**attr):
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    19
		n = apply(ABag,(),self.__dict__)
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    20
		if attr != {}: apply(ABag.__init__,(n,),attr)
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    21
		return n
548
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    22
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    23
	def __repr__(self):
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    24
		import string
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    25
		n = self.__class__.__name__
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    26
		L = [n+"("]
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    27
		keys = self.__dict__.keys()
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    28
		for k in keys:
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    29
			v = getattr(self, k)
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    30
			rk = repr(k)
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    31
			rv = repr(v)
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    32
			rk = "  "+string.replace(rk, "\n", "\n  ")
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    33
			rv = "    "+string.replace(rv, "\n", "\n    ")
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    34
			L.append(rk)
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    35
			L.append(rv)
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    36
		L.append(") #"+n)
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    37
		return string.join(L, "\n")
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    38
		
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    39
if __name__=="__main__":
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    40
	AB = ABag(a=1, c="hello")
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    41
	CD = AB.clone()
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    42
	print AB
566
36bbae746b58 Changed comment to be slightly more meaningful
rgbecker
parents: 561
diff changeset
    43
	print CD