- Timestamp:
- 04/06/13 13:23:59 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/memoize.py
r658 r659 114 114 115 115 #FIXME: the below need expiration of cache due to time, calls, etc... 116 # and r/w to file or other caching mechanisms117 116 #FIXME: memoize*_round fails when decorating a class method 117 #FIXME: need to ensure that all open's are closed appropriately 118 118 119 119 # Want: … … 161 161 162 162 163 '''164 163 class file_dict(archive_dict): 165 164 """dictionary-style interface to a file""" 165 def __get_archive(self, archive): 166 try: 167 f = open(archive, 'rb') 168 import dill as pickle 169 cache = pickle.load(f) 170 f.close() 171 except: 172 cache = {} 173 return cache 166 174 def __init__(self, filename=None, serialized=True): # False 167 175 import os 168 176 """filename = full filepath""" 169 177 if filename is None: 170 if serialized: filename = 'memo.pkl' 171 else: filename = 'memo.py' 178 if serialized: filename = 'memo.pkl' #FIXME: need better default 179 else: filename = 'memo.py' #FIXME: need better default 172 180 self._filename = filename 181 self._serialized = serialized 173 182 if not os.path.exists(filename): 174 183 if serialized: self.dump(filename) 175 184 else: open(filename, 'wb').write('memo = {}') 176 185 return 186 def __setitem__(self, key, value): 187 memo = self.__asdict__() 188 memo[key] = value 189 self.__save__(memo) 190 return 191 def __getitem__(self, key): 192 memo = self.__asdict__() 193 return memo[key] 194 def __asdict__(self): 195 if self._serialized: 196 memo = self.__get_archive(self._filename) 197 else: 198 import os 199 file = os.path.basename(self._filename) 200 root = os.path.realpath(self._filename).rstrip(file)[:-1] 201 curdir = os.path.realpath(os.curdir) 202 file = file.rstrip('.py') or file.rstrip('.pyc') \ 203 or file.rstrip('.pyo') or file.rstrip('.pyd') 204 os.chdir(root) 205 exec 'from %s import memo' % file #FIXME: unsafe 206 os.chdir(curdir) 207 return memo 208 def has_key(self, key): 209 return key in self.__asdict__() 210 def get(self, key, value=None): 211 memo = self.__asdict__() 212 return memo.get(key, value) 213 def __save__(self, memo=None): 214 if memo == None: return 215 if self._serialized: 216 f = open(self._filename, 'wb') 217 import dill as pickle 218 pickle.dump(memo, f) 219 else: 220 open(self._filename, 'wb').write('memo = %s' % memo) 221 return 222 def pop(self, key, *value): 223 memo = self.__asdict__() 224 res = memo.pop(key, *value) 225 self.__save__(memo) 226 return res 227 def clear(self): 228 self.__save__({}) 229 return 230 def update(self, adict, **kwds): 231 memo = self.__asdict__() 232 memo.update(adict, **kwds) 233 self.__save__(memo) 234 return 235 def itervalues(self): 236 return self.__asdict__().itervalues() 237 def values(self): 238 return list(self.itervalues()) 239 def iteritems(self): 240 return self.__asdict__().iteritems() 241 def items(self): 242 return list(self.iteritems()) 243 def keys(self): 244 return list(self.__iter__()) 245 def __repr__(self): 246 return "memo(%s)" % self.__asdict__() 247 def __iter__(self): 248 return self.__asdict__().iterkeys() 249 iterkeys = __iter__ 177 250 pass 178 '''179 251 180 252 … … 319 391 Can memoize a *method* on an object. 320 392 """ 321 def __init__(self, func): #memo=None, serializer=str): 393 def __init__(self, memo=None, serializer=str, tol=None, deep=False): 394 # self.func = func 395 if memo is None: memo = archive_dict() 396 elif type(memo) is dict: memo = archive_dict(memo) 397 self.memo = memo 398 self.__serializer = serializer 399 400 if deep: rounded = deep_round 401 else: rounded = simple_round 402 403 @rounded(tol) 404 def rounded_args(*args, **kwds): 405 return (args, kwds) 406 self.__rounded_args = rounded_args 407 return 408 409 def __call__(self, func): 322 410 self.func = func 323 #if memo is None: memo = dict() 324 self.memo = {} #memo 325 import cPickle as pickle 326 self.serializer = pickle.dumps #serializer 327 328 def __call__(self, *args, **kwds): 329 try: 330 argstr = self.serializer((args, kwds)) 331 if self.memo.has_key(argstr): 332 return self.memo[argstr] 333 res = self.func(*args, **kwds) 334 self.memo[argstr] = res 335 return res 336 except: #TypeError 337 return self.func(*args, **kwds) 411 import functools 412 @functools.wraps(func) 413 def dec(*args, **kwds): 414 try: 415 _args, _kwds = self.__rounded_args(*args, **kwds) 416 argstr = self.__serializer((_args, _kwds)) 417 if self.memo.has_key(argstr): 418 return self.memo[argstr] 419 res = self.func(*args, **kwds) 420 self.memo[argstr] = res 421 return res 422 except: #TypeError 423 return self.func(*args, **kwds) 424 return dec 338 425 339 426 def __repr__(self):
Note: See TracChangeset
for help on using the changeset viewer.