-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mplanchard/memoize
Version 1.1.0 Candidate
- Loading branch information
Showing
28 changed files
with
1,443 additions
and
236 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,8 @@ | ||
pydecor\.\_memoization module | ||
============================= | ||
|
||
.. automodule:: pydecor._memoization | ||
:members: | ||
:private-members: | ||
:undoc-members: | ||
:show-inheritance: |
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,8 @@ | ||
pydecor\.decorators\.\_utility module | ||
===================================== | ||
|
||
.. automodule:: pydecor.decorators._utility | ||
:members: | ||
:private-members: | ||
:undoc-members: | ||
:show-inheritance: |
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,8 @@ | ||
pydecor\.decorators\.generic module | ||
=================================== | ||
|
||
.. automodule:: pydecor.decorators.generic | ||
:members: | ||
:private-members: | ||
:undoc-members: | ||
:show-inheritance: |
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,8 @@ | ||
pydecor\.decorators\.ready\_to\_wear module | ||
=========================================== | ||
|
||
.. automodule:: pydecor.decorators.ready_to_wear | ||
:members: | ||
:private-members: | ||
:undoc-members: | ||
:show-inheritance: |
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
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,14 +1,7 @@ | ||
# -*- coding: UTF-8 -*- | ||
""" | ||
Main package | ||
""" | ||
|
||
from ._version import __version__, __version_info__ | ||
from .decorators import ( | ||
after, | ||
before, | ||
construct_decorator, | ||
decorate, | ||
instead, | ||
intercept, | ||
log_call, | ||
) | ||
from .decorators import * |
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 @@ | ||
# -*- coding: UTF-8 -*- | ||
""" | ||
Required functionality for the memoization function | ||
""" | ||
|
||
__all__ = ( | ||
'convert_to_hashable', | ||
'hashable', | ||
) | ||
|
||
import dill as pickle | ||
|
||
|
||
def convert_to_hashable(args, kwargs): | ||
"""Return args and kwargs as a hashable tuple""" | ||
return hashable(args), hashable(kwargs) | ||
|
||
|
||
def hashable(item): | ||
"""Get return a hashable version of an item | ||
If the item is natively hashable, return the item itself. If | ||
it is not, return it dumped to a pickle string. | ||
""" | ||
try: | ||
hash(item) | ||
except TypeError: | ||
item = pickle.dumps(item) | ||
return item |
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,116 @@ | ||
# -*- coding: UTF-8 -*- | ||
""" | ||
Caches for memoization | ||
""" | ||
|
||
from __future__ import absolute_import, unicode_literals | ||
|
||
|
||
__all__ = ( | ||
'LRUCache', | ||
'FIFOCache', | ||
'TimedCache', | ||
) | ||
|
||
|
||
from collections import OrderedDict | ||
from time import time | ||
|
||
|
||
class LRUCache(OrderedDict): | ||
"""Self-pruning cache using an LRU strategy | ||
If instantiated with a ``max_size`` other than ``0``, will | ||
automatically prune the least-recently-used (LRU) key/value | ||
pair when inserting an item after reaching the specified size. | ||
An item is considered to be "used" when it is inserted or | ||
accessed, at which point its position in recently used | ||
queue is updated to the most recent. | ||
Supports all standard dictionary methods. | ||
:param int max_size: maximum number of entries to save | ||
before pruning | ||
""" | ||
|
||
def __init__(self, max_size=0, *args, **kwargs): | ||
super(LRUCache, self).__init__(*args, **kwargs) | ||
self._max_size = max_size | ||
|
||
def __getitem__(self, key, **kwargs): | ||
value = OrderedDict.__getitem__(self, key) | ||
del self[key] | ||
OrderedDict.__setitem__(self, key, value, **kwargs) | ||
return value | ||
|
||
def __setitem__(self, key, value, **kwargs): | ||
if key in self: | ||
del self[key] | ||
OrderedDict.__setitem__(self, key, value, **kwargs) | ||
if self._max_size and len(self) > self._max_size: | ||
self.popitem(last=False) | ||
|
||
|
||
class FIFOCache(OrderedDict): | ||
"""Self-pruning cache using a FIFO strategy | ||
If instantiated with a ``max_size`` other than ``0``, will | ||
automatically prune the least-recently-inserted key/value | ||
pair when inserting an item after reaching the specified | ||
size. | ||
Supports all standard dictionary methods. | ||
:param int max_size: maximum number of entries to save | ||
before pruning | ||
""" | ||
|
||
def __init__(self, max_size=0, *args, **kwargs): | ||
super(FIFOCache, self).__init__(*args, **kwargs) | ||
self._max_size = max_size | ||
|
||
def __setitem__(self, key, value, **kwargs): | ||
OrderedDict.__setitem__(self, key, value) | ||
if self._max_size and len(self) > self._max_size: | ||
self.popitem(last=False) | ||
|
||
|
||
class TimedCache(dict): | ||
"""Self-pruning cache whose entries can be set to expire | ||
If instantiated with a ``max_age`` other than ``0``, will | ||
consider entries older than the specified age to be invalid, | ||
removing them from the cache upon an attempt to access them | ||
and returning as though they do not exist. | ||
Supports all standard dictionary methods. | ||
:param int max_age: age in seconds beyond which entries | ||
should be considered invalid. The default is 0, which | ||
means that entries should be stored forever. | ||
""" | ||
|
||
def __init__(self, max_age=0, *args, **kwargs): | ||
super(TimedCache, self).__init__(*args, **kwargs) | ||
self._max_age = max_age | ||
|
||
def __getitem__(self, key): | ||
value, last_time = dict.__getitem__(self, key) | ||
now = time() | ||
if self._max_age and now - last_time > self._max_age: | ||
del self[key] | ||
raise KeyError(key) | ||
else: | ||
return value | ||
|
||
def __setitem__(self, key, value): | ||
now = time() | ||
dict.__setitem__(self, key, (value, now)) | ||
|
||
def __contains__(self, key): | ||
try: | ||
self.__getitem__(key) | ||
except KeyError: | ||
return False | ||
return True |
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,3 +1,4 @@ | ||
# -*- coding: UTF-8 -*- | ||
""" | ||
This rather skinny module just contains constants for use elsewhere. | ||
""" | ||
|
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,20 @@ | ||
# -*- coding: UTF-8 -*- | ||
""" | ||
Public interface for decorators sub-package | ||
""" | ||
|
||
__all__ = ( | ||
'after', | ||
'before', | ||
'construct_decorator', | ||
'decorate', | ||
'instead', | ||
'Decorated', | ||
'DecoratorType', | ||
'intercept', | ||
'log_call', | ||
'memoize', | ||
) | ||
|
||
from .generic import * | ||
from .ready_to_wear import * |
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
Oops, something went wrong.