reportlab/lib/abag.py
author aaron_watters
Fri, 15 Dec 2000 14:24:09 +0000
changeset 548 223699ff68fc
parent 517 dffd258916bc
child 561 58942687b2e1
permissions -rw-r--r--
added a __repr__ for debug purposes
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
548
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/lib/abag.py,v 1.2 2000/12/15 14:24:09 aaron_watters Exp $
517
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
     5
class ABag:
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
     6
	"""
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
     7
	A trivial BAG class for holding attributes
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
     8
	"""
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
     9
	def __init__(self,**attr):
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    10
		for k,v in attr.items():
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    11
			setattr(self,k,v)
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    12
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    13
	def clone(self,**attr):
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    14
		n = apply(ABag,(),self.__dict__)
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    15
		if attr != {}: apply(ABag.__init__,(n,),attr)
dffd258916bc Initial version split from paraparser.py
rgbecker
parents:
diff changeset
    16
		return n
548
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    17
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    18
	def __repr__(self):
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    19
		import string
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    20
		n = self.__class__.__name__
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    21
		L = [n+"("]
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    22
		keys = self.__dict__.keys()
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    23
		for k in keys:
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    24
			v = getattr(self, k)
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    25
			rk = repr(k)
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    26
			rv = repr(v)
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    27
			rk = "  "+string.replace(rk, "\n", "\n  ")
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    28
			rv = "    "+string.replace(rv, "\n", "\n    ")
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    29
			L.append(rk)
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    30
			L.append(rv)
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    31
		L.append(") #"+n)
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    32
		return string.join(L, "\n")
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    33
		
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    34
if __name__=="__main__":
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    35
	AB = ABag(a=1, c="hello")
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    36
	CD = AB.clone()
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    37
	print AB
223699ff68fc added a __repr__ for debug purposes
aaron_watters
parents: 517
diff changeset
    38
	print CD