Files
pre-commit/pre_commit/util.py
2014-03-13 23:48:17 -07:00

37 lines
819 B
Python

import functools
import os
class cached_property(object):
"""Like @property, but caches the value."""
def __init__(self, func):
self.__name__ = func.__name__
self.__module__ = func.__module__
self.__doc__ = func.__doc__
self._func = func
def __get__(self, obj, cls):
if obj is None:
return self
value = self._func(obj)
obj.__dict__[self.__name__] = value
return value
def memoize_by_cwd(func):
"""Memoize a function call based on os.getcwd()."""
cache = {}
@functools.wraps(func)
def wrapper(*args):
cwd = os.getcwd()
key = (cwd,) + args
try:
return cache[key]
except KeyError:
ret = cache[key] = func(*args)
return ret
return wrapper