Skip to content

Commit

Permalink
Version bump. Moved CacheProperties to utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
PacketPerception committed Jun 14, 2016
1 parent 2140590 commit c63f0dd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion juggling/__init__.py
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__)
33 changes: 2 additions & 31 deletions juggling/pattern/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# Python2
from UserList import UserList # noqa

from .utils import CacheProperties


__all__ = ['Pattern']

Expand Down Expand Up @@ -108,37 +110,6 @@ def generate_state(pattern, starting_throw=0):
return state


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)


class Pattern(CacheProperties, UserList):
"""
The base representation of any juggling pattern, regardless of notation. A :class:`Pattern` looks
Expand Down
29 changes: 29 additions & 0 deletions juggling/pattern/utils.py
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)

0 comments on commit c63f0dd

Please sign in to comment.