Skip to content

Commit

Permalink
moved CacheInfo to klepto.tools; better naming for generated CacheInf…
Browse files Browse the repository at this point in the history
…o class;

byref in dill.dumps to enable klepto.keymap.picklemap to generate unique key;
moved klepto version to 0.1a1 and require dill-0.2b2;
updated test_cache and test_cache_info to run silently and check with asserts


git-svn-id: svn+ssh://svn.mystic.cacr.caltech.edu/pathos/klepto@553 8bfda07e-5b16-0410-ab1d-fd04ec2748df
  • Loading branch information
mmckerns committed Dec 1, 2013
1 parent d11afa8 commit bdee997
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 96 deletions.
21 changes: 7 additions & 14 deletions klepto/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
"""
a selection of caching decorators
"""
from __future__ import absolute_import

try:
from collections import namedtuple
except ImportError:
from ._namedtuple import namedtuple
from collections import deque
from random import choice #XXX: biased?
from heapq import nsmallest
Expand All @@ -24,12 +18,11 @@
from klepto.rounding import deep_round, simple_round
from klepto.archives import archive_dict
from klepto.keymaps import hashmap
from klepto.tools import CacheInfo

__all__ = ['no_cache','inf_cache','lfu_cache',\
'lru_cache','mru_cache','rr_cache']

_CacheInfo = namedtuple("CacheInfo", ['hit','miss','load','maxsize','size'])

class Counter(dict):
'Mapping where default values are zero'
def __missing__(self, key):
Expand Down Expand Up @@ -127,7 +120,7 @@ def clear(keepstats=False):

def info():
"""Report cache statistics"""
return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))
return CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))

# interface
wrapper.__wrapped__ = user_function
Expand Down Expand Up @@ -237,7 +230,7 @@ def clear(keepstats=False):

def info():
"""Report cache statistics"""
return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))
return CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))

# interface
wrapper.__wrapped__ = user_function
Expand Down Expand Up @@ -369,7 +362,7 @@ def clear(keepstats=False):

def info():
"""Report cache statistics"""
return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))
return CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))

# interface
wrapper.__wrapped__ = user_function
Expand Down Expand Up @@ -528,7 +521,7 @@ def clear(keepstats=False):

def info():
"""Report cache statistics"""
return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))
return CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))

# interface
wrapper.__wrapped__ = user_function
Expand Down Expand Up @@ -662,7 +655,7 @@ def clear(keepstats=False):

def info():
"""Report cache statistics"""
return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))
return CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))

# interface
wrapper.__wrapped__ = user_function
Expand Down Expand Up @@ -785,7 +778,7 @@ def clear(keepstats=False):

def info():
"""Report cache statistics"""
return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))
return CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))

# interface
wrapper.__wrapped__ = user_function
Expand Down
2 changes: 1 addition & 1 deletion klepto/archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def __save__(self, memo=None):
try:
f = open(self._filename, 'wb')
import dill as pickle #FIXME: dill import is slow
pickle.dump(memo, f)
pickle.dump(memo, f) #XXX: byref=True ?
f.close()
except:
pass #XXX: warning? fail?
Expand Down
4 changes: 2 additions & 2 deletions klepto/keymaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ class picklemap(keymap):
# self.typed = False #XXX: is always typed, so set typed=False
def encode(self, *args, **kwds):
"""use a flattened scheme for generating a key"""
return pickle.dumps(keymap.encode(self, *args, **kwds))
return pickle.dumps(keymap.encode(self, *args, **kwds), byref=True)
def encrypt(self, *args, **kwds):
"""use a non-flat scheme for generating a key"""
return pickle.dumps(keymap.encrypt(self, *args, **kwds))
return pickle.dumps(keymap.encrypt(self, *args, **kwds), byref=True)


# EOF
21 changes: 7 additions & 14 deletions klepto/safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
If a hashing error occurs, the cached function will be evaluated.
"""
from __future__ import absolute_import

try:
from collections import namedtuple
except ImportError:
from ._namedtuple import namedtuple
from collections import deque
from random import choice #XXX: biased?
from heapq import nsmallest
Expand All @@ -26,12 +20,11 @@
from klepto.rounding import deep_round, simple_round
from klepto.archives import archive_dict
from klepto.keymaps import stringmap
from klepto.tools import CacheInfo

__all__ = ['no_cache','inf_cache','lfu_cache',\
'lru_cache','mru_cache','rr_cache']

_CacheInfo = namedtuple("CacheInfo", ['hit','miss','load','maxsize','size'])

class Counter(dict):
'Mapping where default values are zero'
def __missing__(self, key):
Expand Down Expand Up @@ -134,7 +127,7 @@ def clear(keepstats=False):

def info():
"""Report cache statistics"""
return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))
return CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))

# interface
wrapper.__wrapped__ = user_function
Expand Down Expand Up @@ -249,7 +242,7 @@ def clear(keepstats=False):

def info():
"""Report cache statistics"""
return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))
return CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))

# interface
wrapper.__wrapped__ = user_function
Expand Down Expand Up @@ -386,7 +379,7 @@ def clear(keepstats=False):

def info():
"""Report cache statistics"""
return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))
return CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))

# interface
wrapper.__wrapped__ = user_function
Expand Down Expand Up @@ -550,7 +543,7 @@ def clear(keepstats=False):

def info():
"""Report cache statistics"""
return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))
return CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))

# interface
wrapper.__wrapped__ = user_function
Expand Down Expand Up @@ -689,7 +682,7 @@ def clear(keepstats=False):

def info():
"""Report cache statistics"""
return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))
return CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))

# interface
wrapper.__wrapped__ = user_function
Expand Down Expand Up @@ -817,7 +810,7 @@ def clear(keepstats=False):

def info():
"""Report cache statistics"""
return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))
return CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache))

# interface
wrapper.__wrapped__ = user_function
Expand Down
10 changes: 10 additions & 0 deletions klepto/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
"""

from __future__ import absolute_import

try:
from collections import namedtuple
except ImportError:
from ._namedtuple import namedtuple
CacheInfo = namedtuple("CacheInfo", ['hit','miss','load','maxsize','size'])

__all__ = ['isiterable']

def isiterable(x):
"""check if an object is iterable"""
#try:
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import os

# set version numbers
stable_version = '0.0a'
target_version = '0.1a'
stable_version = '0.0a1'
target_version = '0.1a1'
is_release = False

# check if easy_install is available
Expand Down Expand Up @@ -155,7 +155,7 @@
Klepto requires::
- python2, version >= 2.5 *or* python3, version >= 3.1
- dill, version >= 0.2a.dev
- dill, version >= 0.2b2.dev
Optional requirements::
Expand Down Expand Up @@ -243,7 +243,7 @@ def write_info_py(filename='klepto/info.py'):
""" % (target_version, long_description)

# add dependencies
dill_version = '>=0.2a.dev'
dill_version = '>=0.2b2.dev'
import sys
if has_setuptools:
setup_code += """
Expand Down
Loading

0 comments on commit bdee997

Please sign in to comment.