abag.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #Copyright ReportLab Europe Ltd. 2000-2017
  2. #see license.txt for license details
  3. #history https://hg.reportlab.com/hg-public/reportlab/log/tip/src/reportlab/lib/abag.py
  4. __version__='3.3.0'
  5. __doc__='''Data structure to hold a collection of attributes, used by styles.'''
  6. class ABag:
  7. """
  8. 'Attribute Bag' - a trivial BAG class for holding attributes.
  9. This predates modern Python. Doing this again, we'd use a subclass
  10. of dict.
  11. You may initialize with keyword arguments.
  12. a = ABag(k0=v0,....,kx=vx,....) ==> getattr(a,'kx')==vx
  13. c = a.clone(ak0=av0,.....) copy with optional additional attributes.
  14. """
  15. def __init__(self,**attr):
  16. self.__dict__.update(attr)
  17. def clone(self,**attr):
  18. n = self.__class__(**self.__dict__)
  19. if attr: n.__dict__.update(attr)
  20. return n
  21. def __repr__(self):
  22. D = self.__dict__
  23. K = list(D.keys())
  24. K.sort()
  25. return '%s(%s)' % (self.__class__.__name__,', '.join(['%s=%r' % (k,D[k]) for k in K]))
  26. if __name__=="__main__":
  27. AB = ABag(a=1, c="hello")
  28. CD = AB.clone()
  29. print(AB)
  30. print(CD)