-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Version bump. Moved CacheProperties to utils module
- Loading branch information
1 parent
2140590
commit c63f0dd
Showing
3 changed files
with
32 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import logging | ||
|
||
__version__ = '0.1.3' | ||
__version__ = '0.1.4' | ||
|
||
logger = logging.getLogger(__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class CacheProperties(object): | ||
""" Will automatically cache property attributes of itself. Can clear with self._clear_cache() """ | ||
_cache_exclude = [] | ||
|
||
def __init__(self, *args, **kwargs): # noqa | ||
self._cache = {} | ||
super(CacheProperties, self).__init__() | ||
|
||
def _clear_cache(self): | ||
try: | ||
self._cache.clear() | ||
except AttributeError: | ||
pass # means _cache hasn't been set yet | ||
|
||
def __setattr__(self, key, value): | ||
if key != '_cache' and key in self._cache: | ||
del self._cache[key] | ||
super(CacheProperties, self).__setattr__(key, value) | ||
|
||
def __getattribute__(self, name, *args, **kwargs): | ||
exclude_attrs = super(CacheProperties, self).__getattribute__('_cache_exclude') | ||
if (not name.startswith('__') and | ||
name not in ['_cache', '_cached_attrs', '_clear_cache'] + exclude_attrs and | ||
isinstance(getattr(self.__class__, name, None), property)): | ||
cache = super(CacheProperties, self).__getattribute__('_cache') | ||
if name not in cache: | ||
cache[name] = super(CacheProperties, self).__getattribute__(name) | ||
return cache[name] | ||
return super(CacheProperties, self).__getattribute__(name, *args, **kwargs) |