Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Minor fixes to work on Python 3:
Browse files Browse the repository at this point in the history
* The iteration in CachedFunction.clear_cache() would raise an error since
  dict.keys() no longer returns a list--instead it results in modifying a
  dict while iterating over it.  I changed this to self.cache.clear() (which
  also necessitated adding a clear() method to FileCache).

* Very minor test fix and incidental PEP8 cleanup.
  • Loading branch information
embray committed Nov 28, 2017
1 parent 92f95ce commit 49a4e2f
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions src/sage/misc/cachefunc.pyx
Expand Up @@ -502,7 +502,7 @@ cdef frozenset special_method_names = frozenset(['__abs__', '__add__',
def _cached_function_unpickle(module, name, cache=None):
"""
Unpickle the cache function ``name`` defined in ``module``.
This function loads ``name`` from ``module`` (it does not restore the code
of the actual function when it was pickled.) The cache is restored from
``cache`` if present.
Expand Down Expand Up @@ -1265,9 +1265,7 @@ cdef class CachedFunction(object):
sage: g.cache
{}
"""
cdef cache = self.cache
for key in cache.keys():
del cache[key]
self.cache.clear()

def precompute(self, arglist, num_processes=1):
"""
Expand Down Expand Up @@ -2564,7 +2562,7 @@ cdef class GloballyCachedMethodCaller(CachedMethodCaller):
sage: class MyParent(Parent):
....: pass
sage: class MyElement:
sage: class MyElement(object):
....: def __init__(self, x):
....: self.x = x
....: def parent(self):
Expand All @@ -2574,7 +2572,7 @@ cdef class GloballyCachedMethodCaller(CachedMethodCaller):
....: return self.x^2
sage: a = MyElement(2)
sage: a.f.get_key()
(<__main__.MyElement instance at 0x...>, ((), ()))
(<__main__.MyElement object at 0x...>, ((), ()))
sage: a.f.get_key()[0] is a
True
"""
Expand Down Expand Up @@ -2693,7 +2691,7 @@ cdef class CachedMethod(object):
"""
EXAMPLES::
sage: class Foo:
sage: class Foo(object):
....: def __init__(self, x):
....: self._x = x
....: @cached_method
Expand Down Expand Up @@ -2732,15 +2730,15 @@ cdef class CachedMethod(object):
The cached method ``f0`` accepts no arguments, which allows
for an improved way of caching: By an attribute of the cached
method itsel. This cache is *only* available in that way, i.e.,
method itself. This cache is *only* available in that way, i.e.,
it is not additionally stored as an attribute of ``a``::
sage: type(a.f0)
<type 'sage.misc.cachefunc.CachedMethodCallerNoArgs'>
sage: a.f0.cache
4
sage: sorted(dir(a))
['__doc__', '__init__', '__module__', '_cache__f', '_x', 'f', 'f0']
sage: sorted(n for n in dir(a) if not n.startswith('__'))
['_cache__f', '_x', 'f', 'f0']
The cached method has its name and module set::
Expand Down Expand Up @@ -3538,6 +3536,33 @@ class FileCache(object):
K.append(load(f))
return K

def clear(self):
"""
Clear all key, value pairs from self and unlink the associated files
from the file cache.
EXAMPLES::
sage: from sage.misc.cachefunc import FileCache
sage: dir = tmp_dir()
sage: FC1 = FileCache(dir, memory_cache=False, prefix='foo')
sage: FC2 = FileCache(dir, memory_cache=False, prefix='foo')
sage: k1 = ((), (('a', 1),))
sage: t1 = randint(0, 1000)
sage: k2 = ((), (('b', 1),))
sage: t2 = randint(0, 1000)
sage: FC1[k1] = t1
sage: FC2[k2] = t2
sage: FC2.clear()
sage: k1 in FC1
False
sage: k2 in FC1
False
"""

for k in list(self):
del self[k]

def _filename(self, key):
"""
Compute the filename associated with a certain key.
Expand Down Expand Up @@ -3615,7 +3640,7 @@ class FileCache(object):

f = self._filename(key) + '.sobj'
try:
k,v = load(f)
k, v = load(f)
except IOError:
raise KeyError(key)
if k != key:
Expand Down

0 comments on commit 49a4e2f

Please sign in to comment.