# HG changeset patch # User Idan Kamara # Date 1318708035 -7200 # Node ID 4282391dd69325b2a130ea11d739cc5da004e2af # Parent 37307caccf54792423831ebda673246636730cfe util: add propertycache decorator diff -r 37307caccf54 -r 4282391dd693 hglib/util.py --- 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