Decorator storing input in list to call every `n` calls?
-
13-06-2021 - |
質問
How do I write a decorator that stores input for that function in a list
and only calls the function after n
is stored?
解決
Calls func
every 100,000 calls, use with @counter
before you function:
def counter(func):
def wrapper(val, *args, **kwargs):
if val:
wrapper.count = wrapper.count + 1
if wrapper.count % 100000 == 0: # insert 100,000 at a time, should be speedier thanks to `.executemany()`
to_logfile(str(wrapper.count) + '\n', 'counter2.log')
return func(wrapper.valarr, *args, **kwargs)
else:
if len(wrapper.valarr)==1000000:
wrapper.vallarr = []
wrapper.valarr.append([val])
return
wrapper.count = 0
wrapper.valarr = []
return wrapper
(feel free to suggest improvements)
他のヒント
I am going to presume that
- your function accepts any number of positional arguments
f(a); f(b)
is the same asf(a, b)
- it returns nothing (any result occurs as a side-effect)
.
import functools
class Batcher(object):
def __init__(self, n=100):
self.n = n
def __call__(self, fn):
@functools.wraps(fn)
def _fn(*args):
_fn.cache.extend(args)
_fn.calls += 1
if _fn.calls == _fn.n:
fn(*_fn.cache)
_fn.cache = []
_fn.calls = 0
_fn.n = self.n
_fn.cache = []
_fn.calls = 0
return _fn
then testing it,
@Batcher(20)
def mysum(*args):
print sum(args)
for i in range(1,25):
mysum(i)
prints
210 # <- result of sum(1..20)
Here's a class implementation, as others have submitted function implentations:
class IntervalCache(object):
def __init__(self, f):
self._f = f
self._cache = []
self._n = 0
self._interval = 1000
def __call__(self, *args, **kwargs):
if self._n == self._interval:
result = [self.f(*c_args, **c_kwargs) for c_args, c_kwargs in self._cache]
self._n = 0
self._cache = []
return result + [self.f(*args, **kwargs)]
else:
self._cache.append((*args, **kwargs))
self._n += 1
@property
def getCache(self):
return self._cache
def resetCache(self):
self._cache = []
self._n = 0
def getInterval(self):
return self._interval
def setInterval(self, value):
self._interval = value
interval = property(getInterval, setInterval)
Usage:
#wrapping a function
@IntervalCache
def foo(*args, **kwargs):
print args, kwargs
return True
#setting the caching interval
foo.interval = 10
#calling foo a bunch of times
for i in range(20):
print foo(i, i+1, bar=i*2)
#retrieving the contents of the cache
print foo.getCache()
#resetting the contents of the cache
foo.resetCache()
所属していません StackOverflow