Mercurial > python-hglib
diff hglib/util.py @ 77:4282391dd693
util: add propertycache decorator
author | Idan Kamara <idankk86@gmail.com> |
---|---|
date | Sat, 15 Oct 2011 21:47:15 +0200 |
parents | a5dd7b5d0be1 |
children | f98d6e234cd9 |
line wrap: on
line diff
--- a/hglib/util.py Sat Oct 15 20:09:59 2011 +0200 +++ b/hglib/util.py Sat Oct 15 21:47:15 2011 +0200 @@ -131,6 +131,30 @@ """ Returns True if the return code was 0, False otherwise """ return self.ret == 0 +class propertycache(object): + """ + Decorator that remembers the return value of a function call. + + >>> class obj(object): + ... def func(self): + ... print 'func' + ... return [] + ... func = propertycache(func) + >>> o = obj() + >>> o.func + func + [] + >>> o.func + [] + """ + def __init__(self, func): + self.func = func + self.name = func.__name__ + def __get__(self, obj, type=None): + result = self.func(obj) + setattr(obj, self.name, result) + return result + close_fds = os.name == 'posix' startupinfo = None