Allows decorating and caching expensive data consuming tasks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class CachedSingleton(object):
"""Provides caching singleton storage for data access decoration.
Usage:
class CachedSingletonClass(CachedSingleton):
def _getitem(self, name):
# implement data getting routine, such as db access
CachedSingletonClass().attribute1 # returns value as if _getitem('attribute1') was called
CachedSingletonClass().attribute2 # returns value as if _getitem('attribute2') was called
CachedSingletonClass().__doc__ # returns real docstring
"""
__instance = None
def __new__(classtype, *args, **kwargs):
if classtype != type(classtype.__instance):
classtype.__instance = object.__new__(classtype, *args, **kwargs)
classtype.__instance.cache = {}
return classtype.__instance
# to be implemented by contract in the descendant classes
def _getitem(self, name):
return None
def __getattribute__(self, name):
try:
return object.__getattribute__(self, name)
except:
if not self.cache.has_key(name):
self.cache[name] = self._getitem(name)
return self.cache[name]
|
singleton copyright http://code.activestate.com/recipes/52558/#c8
__getattribute__ "undecoration" trick copyright http://www.ibm.com/developerworks/linux/library/l-python-elegance-2.html
I think, that this is the shorter and faster implementation:
Rather than returning None, I'd have CachedSingleton._getitem
Alexander, your solution does not solve 2 problems: 1) caching, as you call the _getitem every time 2) accessing existing attributes of the object (like __doc__) instead of calling _getitem for them
David, thanks, raise is much more pythonic in this case