Using Functools.lru_cache On Functions With Constant But Non-hashable Objects
Is it possible to use functools.lru_cache for caching a partial function created by functools.partial? My problem is a function that takes hashable parameters and contant, non-hash
Solution 1:
As the array is constant you can use a wrapper around the actual lru cached function and simply pass the key value to it:
from functools import lru_cache, partial
import numpy as np
def lru_wrapper(array=None):
@lru_cache(maxsize=None)
def foo(key):
return '%s:' % key, array
return foo
arr = np.array([1, 2, 3])
func = lru_wrapper(array=arr)
for x in [0, 0, 1, 2, 2, 1, 2, 0]:
print (func(x))
print (func.cache_info())
Outputs:
('0:', array([1, 2, 3]))
('0:', array([1, 2, 3]))
('1:', array([1, 2, 3]))
('2:', array([1, 2, 3]))
('2:', array([1, 2, 3]))
('1:', array([1, 2, 3]))
('2:', array([1, 2, 3]))
('0:', array([1, 2, 3]))
CacheInfo(hits=5, misses=3, maxsize=None, currsize=3)
Solution 2:
Here is an example of how to use lru_cache
with functools.partial
:
from functools import lru_cache, partial
import numpy as np
def foo(key, array):
return '%s:' % key, array
arr = np.array([1, 2, 3])
pfoo = partial(foo, array=arr)
func = lru_cache(maxsize=None)(pfoo)
for x in [0, 0, 1, 2, 2, 1, 2, 0]:
print(func(x))
print(func.cache_info())
Output:
('0:', array([1, 2, 3]))
('0:', array([1, 2, 3]))
('1:', array([1, 2, 3]))
('2:', array([1, 2, 3]))
('2:', array([1, 2, 3]))
('1:', array([1, 2, 3]))
('2:', array([1, 2, 3]))
('0:', array([1, 2, 3]))
CacheInfo(hits=5, misses=3, maxsize=None, currsize=3)
This is more concise than solution of @AshwiniChaudhary, and also uses the functools.partial
following the OP's requirement.
Post a Comment for "Using Functools.lru_cache On Functions With Constant But Non-hashable Objects"