def clear(self): """Destroys this object and its content.""" appengine_config.log_appstats_event( '%s.destroy' % self.__class__.__name__, {}) _instance = self._instances().get(self.__class__) if _instance: del self._instances()[self.__class__]
def clear_instance(cls): """Destroys the instance of this cls.""" appengine_config.log_appstats_event( '%s.destroy' % cls.__name__, {}) _instance = cls._instances().get(cls) if _instance: del cls._instances()[cls]
def instance(cls, *args, **kwargs): """Creates new or returns existing instance of the object.""" # pylint: disable-msg=protected-access _instance = cls._instances().get(cls) if not _instance: try: _instance = cls(*args, **kwargs) except: logging.exception( 'Failed to instantiate %s: %s, %s', cls, args, kwargs) raise appengine_config.log_appstats_event('%s.create' % cls.__name__, {}) _instance._init_args = (args, kwargs) cls._instances()[cls] = _instance else: _before = _instance._init_args _now = (args, kwargs) if _now != _before: raise AssertionError( 'Singleton initiated with %s already exists. ' 'Failed to re-initialized it with %s.' % (_before, _now)) return _instance
def __init__(self, namespace): """Override this method and properly instantiate self.cache.""" self.namespace = namespace self.cache = None appengine_config.log_appstats_event( '%s.connect' % self.__class__.__name__, {'namespace': namespace})