3 ms·
In this context it's worth noting that writing a memoizing decorator in Python is ridiculously easy: class memoized(object): def __init__(self, func):
by yuvadam 16y ago
In this context it's worth noting that writing a memoizing decorator in Python is ridiculously easy:
class memoized(object):
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
try:
return self.cache[args]
except KeyError:
value = self.func(*args)
self.cache[args] = value
return value
- ubasu 16y agoSee previous discussion on how this is different from simple memoization: http://news.ycombinator.com/item?id=1477207 http://news.ycombinator.com/item?id=1477207
- pgbovine 16y ago(IncPy author here ...) yup agreed, but the programmer needs to figure out: 1.) when it's safe to memoize 2.) when it's worthwhile to memoize also, your memoization decorator doesn't save data to disk. if you wrote a persistent memoizer, then you would need to also track all dependencies for the data you memoized, so that you can know when it's safe to invalidate on-disk cache entries. IncPy takes care of all of this automatically ;)
- newtonapple 16y agoI have a question. How does IncPy keep track of data dependency coming from IO layer e.g. sockets. Say I want to write a simple web crawler. The function crawls a website, spits out the HTML in raw string, sleeps for 5 seconds and then crawls the site again. Will IncPy not memoize the function at all, knows that the data coming from the network layer has not changed and return the memoized string immediately, and/or smart enough to detect when input (the website) has changed and expires the cache automatically?
- ippisl 16y agoincPy need checks the functions it caches to be pure and determinstic. determinstic function is a function that when given the same input , always returns the same output. incPy tests for a few options of non-determinism , and will allow in the future to define non determinstic sources in a config file.