class CachedAttribute(object): '''Computes attribute value and caches it in instance. Example: class MyClass(object): def myMethod(self): # ... myMethod = CachedAttribute(myMethod) Use "del inst.myMethod" to clear cache.''' def __init__(self, method, name=None): self.method = method self.name = name or method.__name__ def __get__(self, inst, cls): if inst is None: return self result = self.method(inst) setattr(inst, self.name, result) return result class CachedClassAttribute(object): '''Computes attribute value and caches it in class. Example: class MyClass(object): def myMethod(cls): # ... myMethod = CachedClassAttribute(myMethod) Use "del MyClass.myMethod" to clear cache.''' def __init__(self, method, name=None): self.method = method self.name = name or method.__name__ def __get__(self, inst, cls): result = self.method(cls) setattr(cls, self.name, result) return result class ReadAliasAttribute(object): '''If not explcitly assigned this attribute is an alias for other. Example: class Document(object): title='?' shortTitle=ReadAliasAttribute('title')''' def __init__(self, name): self.name = name def __get__(self, inst, cls): if inst is None: return self return getattr(inst, self.name) class AliasAttribute(ReadAliasAttribute): '''This attribute is an alias for other. Example: class Document(object): newAttrName=somevalue deprecatedAttrName=AliasAttribute('newAttrName')''' def __set__(self, inst, value): setattr(inst, self.name, value) def __delete__(self, inst): delattr(inst, self.name)