Mercurial > python-hglib
comparison 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 |
comparison
equal
deleted
inserted
replaced
76:37307caccf54 | 77:4282391dd693 |
---|---|
129 | 129 |
130 def __nonzero__(self): | 130 def __nonzero__(self): |
131 """ Returns True if the return code was 0, False otherwise """ | 131 """ Returns True if the return code was 0, False otherwise """ |
132 return self.ret == 0 | 132 return self.ret == 0 |
133 | 133 |
134 class propertycache(object): | |
135 """ | |
136 Decorator that remembers the return value of a function call. | |
137 | |
138 >>> class obj(object): | |
139 ... def func(self): | |
140 ... print 'func' | |
141 ... return [] | |
142 ... func = propertycache(func) | |
143 >>> o = obj() | |
144 >>> o.func | |
145 func | |
146 [] | |
147 >>> o.func | |
148 [] | |
149 """ | |
150 def __init__(self, func): | |
151 self.func = func | |
152 self.name = func.__name__ | |
153 def __get__(self, obj, type=None): | |
154 result = self.func(obj) | |
155 setattr(obj, self.name, result) | |
156 return result | |
157 | |
134 close_fds = os.name == 'posix' | 158 close_fds = os.name == 'posix' |
135 | 159 |
136 startupinfo = None | 160 startupinfo = None |
137 if os.name == 'nt': | 161 if os.name == 'nt': |
138 startupinfo = subprocess.STARTUPINFO() | 162 startupinfo = subprocess.STARTUPINFO() |