/Lib/idlelib/Delegator.py

http://unladen-swallow.googlecode.com/ · Python · 33 lines · 25 code · 7 blank · 1 comment · 3 complexity · bb32fe515997cbc8d00e36c5d1f23803 MD5 · raw file

  1. class Delegator:
  2. # The cache is only used to be able to change delegates!
  3. def __init__(self, delegate=None):
  4. self.delegate = delegate
  5. self.__cache = {}
  6. def __getattr__(self, name):
  7. attr = getattr(self.delegate, name) # May raise AttributeError
  8. setattr(self, name, attr)
  9. self.__cache[name] = attr
  10. return attr
  11. def resetcache(self):
  12. for key in self.__cache.keys():
  13. try:
  14. delattr(self, key)
  15. except AttributeError:
  16. pass
  17. self.__cache.clear()
  18. def cachereport(self):
  19. keys = self.__cache.keys()
  20. keys.sort()
  21. print keys
  22. def setdelegate(self, delegate):
  23. self.resetcache()
  24. self.delegate = delegate
  25. def getdelegate(self):
  26. return self.delegate