Created
March 12, 2013 15:40
-
-
Save msiemens/5143963 to your computer and use it in GitHub Desktop.
A simple Python file caching decorator, caching the function's response till the given file has been changed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import inspect | |
from functools import wraps | |
_file_cache = {} | |
def cache_file(path): | |
def cache_is_fresh(name): | |
return _file_cache[name]['MTIME'] == os.path.getmtime(path) | |
def func_has_args(func): | |
args, varargs, keywords, defaults = inspect.getargspec(func) | |
count = len(list(set(args) - set(['self']))) # Substract 'self' from | |
# args and count args | |
if varargs: | |
count += 1 | |
if keywords: | |
count += 1 | |
return count > 0 | |
def decorator(f): | |
if func_has_args(f): | |
raise SyntaxWarning('cache_file doesn\'t support arguments') | |
name = f.__name__ | |
@wraps(f) | |
def return_cache(*args, **kwargs): | |
if name in _file_cache.keys() and cache_is_fresh(name): | |
return _file_cache[name]['CONTENTS'] | |
else: | |
# Store to cache | |
_file_cache[name] = { | |
'MTIME': os.path.getmtime(path), | |
'CONTENTS': f(*args, **kwargs) | |
} | |
return _file_cache[name]['CONTENTS'] | |
return return_cache | |
return decorator | |
# Example: | |
counter = 0 | |
@cache_file('/tmp/cached-file') | |
def my_func(): | |
global counter | |
counter += 1 | |
return counter |
To be fair, when I wrote this I didn't have that much Python experience (this snippet is more than 9 years old now!). It worked back then but don't cosider this production ready code 🙂
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"self" is not always the name of the first argument of methods (same applies for "cls" in classmethods). It is just a convention. I would rather check if the function is a method/classmethod.