home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / DELEGATOR.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  866 b   |  35 lines

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