Skip to content

Instantly share code, notes, and snippets.

@jh0ker
Last active February 3, 2023 20:38
Show Gist options
  • Select an option

  • Save jh0ker/56f5b4fb7d015b1b9e4c74d4a91d4568 to your computer and use it in GitHub Desktop.

Select an option

Save jh0ker/56f5b4fb7d015b1b9e4c74d4a91d4568 to your computer and use it in GitHub Desktop.

Revisions

  1. jh0ker revised this gist Aug 5, 2020. 1 changed file with 24 additions and 9 deletions.
    33 changes: 24 additions & 9 deletions mwt.py
    Original file line number Diff line number Diff line change
    @@ -2,40 +2,55 @@
    # Source: http://code.activestate.com/recipes/325905-memoize-decorator-with-timeout/#c1

    import time
    from functools import wraps

    class MWT(object):
    class MWT:
    """Memoize With Timeout"""
    _caches = {}
    _timeouts = {}

    def __init__(self,timeout=2):
    def __init__(self, timeout=2):
    self.timeout = timeout

    def collect(self):
    """Clear cache of results which have timed out"""
    t = time.time()

    for func in self._caches:
    cache = {}

    for key in self._caches[func]:
    if (time.time() - self._caches[func][key][1]) < self._timeouts[func]:
    if (t - self._caches[func][key][1]) < self._timeouts[func]:
    cache[key] = self._caches[func][key]

    self._caches[func] = cache

    def __call__(self, f):
    self.cache = self._caches[f] = {}
    cache = self._caches[f] = {}
    self._timeouts[f] = self.timeout

    @wraps(f)
    def func(*args, **kwargs):
    kw = sorted(kwargs.items())
    key = (args, tuple(kw))
    t = time.time()

    try:
    v = self.cache[key]
    v = cache[key]
    print("cache")
    if (time.time() - v[1]) > self.timeout:

    if (t - v[1]) > self.timeout:
    raise KeyError

    except KeyError:
    print("new")
    v = self.cache[key] = f(*args,**kwargs),time.time()
    v = cache[key] = f(*args,**kwargs), t

    return v[0]
    func.func_name = f.__name__

    return func
    def clear_cache():
    self._caches[f].clear()

    func.clear_cache = clear_cache

    return func
  2. jh0ker created this gist Nov 8, 2016.
    41 changes: 41 additions & 0 deletions mwt.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/usr/bin/env python
    # Source: http://code.activestate.com/recipes/325905-memoize-decorator-with-timeout/#c1

    import time

    class MWT(object):
    """Memoize With Timeout"""
    _caches = {}
    _timeouts = {}

    def __init__(self,timeout=2):
    self.timeout = timeout

    def collect(self):
    """Clear cache of results which have timed out"""
    for func in self._caches:
    cache = {}
    for key in self._caches[func]:
    if (time.time() - self._caches[func][key][1]) < self._timeouts[func]:
    cache[key] = self._caches[func][key]
    self._caches[func] = cache

    def __call__(self, f):
    self.cache = self._caches[f] = {}
    self._timeouts[f] = self.timeout

    def func(*args, **kwargs):
    kw = sorted(kwargs.items())
    key = (args, tuple(kw))
    try:
    v = self.cache[key]
    print("cache")
    if (time.time() - v[1]) > self.timeout:
    raise KeyError
    except KeyError:
    print("new")
    v = self.cache[key] = f(*args,**kwargs),time.time()
    return v[0]
    func.func_name = f.__name__

    return func