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

Commit

Permalink
Renamed key_normalizer to create_key for cached_function to be consis…
Browse files Browse the repository at this point in the history
…tent with UniqueFactory
  • Loading branch information
saraedum committed Mar 20, 2014
1 parent b74edaa commit faf771b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/sage/misc/cachefunc.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cdef class CachedFunction(object):
cdef argfix_init(self)
cpdef get_cache(self)
cpdef clear_cache(self)
cdef object _key_normalizer
cdef object _create_key

cdef class CachedMethod

Expand Down
42 changes: 21 additions & 21 deletions src/sage/misc/cachefunc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ AUTHORS:
- Tom Boothby (added DiskCachedFunction).
- Simon King (improved performance, more doctests, cython version,
CachedMethodCallerNoArgs, weak cached function, cached special methods).
- Julian Rueth (2014-03-19): added ``key_normalizer`` parameter
- Julian Rueth (2014-03-19): added ``create_key`` parameter
EXAMPLES:
Expand Down Expand Up @@ -477,7 +477,7 @@ cdef class CachedFunction(object):
- ``f`` -- a function
- ``name`` (optional string) -- name that the cached version
of ``f`` should be provided with.
- ``key_normalizer`` -- a function or ``None`` (default: ``None``), the
- ``create_key`` -- a function or ``None`` (default: ``None``), the
parameters to ``f`` are normalized with this function when computing a
key for the cache
Expand Down Expand Up @@ -524,17 +524,17 @@ cdef class CachedFunction(object):
sage: mul(5,2)
'foo'
The parameter ``key_normalizer`` can be used to ignore parameters for
The parameter ``create_key`` can be used to ignore parameters for
caching. In this example we ignore the parameter ``algorithm``::
sage: @cached_function(key_normalizer = (lambda x, y, algorithm: (x,y)))
sage: @cached_function(create_key = (lambda x, y, algorithm: (x,y)))
....: def mul(x, y, algorithm="default"):
....: return x*y
sage: mul(1,1,algorithm="default") is mul(1,1,algorithm="algorithm") is mul(1,1) is mul(1,1,'default')
True
"""
def __init__(self, f, classmethod=False, name=None, key_normalizer=None):
def __init__(self, f, classmethod=False, name=None, create_key=None):
"""
Create a cached version of a function, which only recomputes
values it hasn't already computed. A custom name can be
Expand Down Expand Up @@ -571,10 +571,10 @@ cdef class CachedFunction(object):
"""
self.is_classmethod = classmethod
self._common_init(f, None, name=name, key_normalizer=key_normalizer)
self._common_init(f, None, name=name, create_key=create_key)
self.cache = {}

def _common_init(self, f, argument_fixer, name=None, key_normalizer=None):
def _common_init(self, f, argument_fixer, name=None, create_key=None):
"""
Perform initialization common to CachedFunction and CachedMethodCaller.
Expand All @@ -598,14 +598,14 @@ cdef class CachedFunction(object):
self.__module__ = f.__module__
except AttributeError:
self.__module__ = f.__objclass__.__module__
self._key_normalizer = key_normalizer
self._create_key = create_key
if argument_fixer is not None: # it is None unless the argument fixer
# was known previously. See #15038.
self._argument_fixer = argument_fixer
if self._key_normalizer is None:
if self._create_key is None:
self._fix_to_pos = argument_fixer.fix_to_pos
else:
self._fix_to_pos = self._fix_to_pos_and_normalize_key
self._fix_to_pos = self._fix_to_pos_and_create_key

cdef argfix_init(self):
"""
Expand All @@ -624,31 +624,31 @@ cdef class CachedFunction(object):
"""
A = ArgumentFixer(self.f,classmethod=self.is_classmethod)
self._argument_fixer = A
if self._key_normalizer:
self._fix_to_pos = self._fix_to_pos_and_normalize_key
if self._create_key:
self._fix_to_pos = self._fix_to_pos_and_create_key
else:
self._fix_to_pos = A.fix_to_pos

def _fix_to_pos_and_normalize_key(self, *args, **kwargs):
def _fix_to_pos_and_create_key(self, *args, **kwargs):
r"""
Normalize parameters to obtain a key for the cache.
For performance reasons, this method is only called if a ``key_normalizer`` has been passed in
For performance reasons, this method is only called if a ``create_key`` has been passed in
the constructor.
TESTS::
sage: @cached_function(key_normalizer = (lambda x, y, algorithm: (x,y)))
sage: @cached_function(create_key = (lambda x, y, algorithm: (x,y)))
....: def mul(x, y, algorithm="default"):
....: return x*y
sage: mul(2,3) # this initializes _argument_fixer
6
sage: mul._fix_to_pos_and_normalize_key(1,1,"default")
sage: mul._fix_to_pos_and_create_key(1,1,"default")
(1, 1)
"""
args, kwargs = self._argument_fixer.fix_to_pos(*args, **kwargs)
return self._key_normalizer(*args, **dict(kwargs))
return self._create_key(*args, **dict(kwargs))

def __reduce__(self):
"""
Expand Down Expand Up @@ -1001,17 +1001,17 @@ cdef class WeakCachedFunction(CachedFunction):
sage: a = f()
doing a computation
The parameter ``key_normalizer`` can be used to ignore parameters for
The parameter ``create_key`` can be used to ignore parameters for
caching. In this example we ignore the parameter ``algorithm``::
sage: @weak_cached_function(key_normalizer = (lambda x, algorithm: x))
sage: @weak_cached_function(create_key = (lambda x, algorithm: x))
....: def mod_ring(x, algorithm="default"):
....: return IntegerModRing(x)
sage: mod_ring(1,algorithm="default") is mod_ring(1,algorithm="algorithm") is mod_ring(1) is mod_ring(1,'default')
True
"""
def __init__(self, f, classmethod=False, name=None, key_normalizer=None):
def __init__(self, f, classmethod=False, name=None, create_key=None):
"""
The inputs to the function must be hashable.
The outputs to the function must be weakly referenceable.
Expand All @@ -1038,7 +1038,7 @@ cdef class WeakCachedFunction(CachedFunction):
'<WeakValueDictionary at 0x...>'
"""
self._common_init(f, None, name=name, key_normalizer=key_normalizer)
self._common_init(f, None, name=name, create_key=create_key)
self.cache = WeakValueDictionary()

def __call__(self, *args, **kwds):
Expand Down

0 comments on commit faf771b

Please sign in to comment.