- Timestamp:
- 05/29/13 06:15:38 (3 years ago)
- Location:
- branches/decorate
- Files:
-
- 4 edited
- 1 copied
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/cache.py
r665 r667 57 57 58 58 keymap = kwd.get('keymap', None) 59 if keymap is None: keymap = hashmap(flat=True) #XXX: stringmap ?59 if keymap is None: keymap = hashmap(flat=True) 60 60 cache = archive_dict() 61 61 … … 78 78 79 79 def wrapper(*args, **kwds): 80 #XXX: add try/except... any failure has result = f(*args, **kwds)81 80 _args, _kwds = rounded_args(*args, **kwds) 82 81 key = keymap(*_args, **_kwds) … … 106 105 cache.archive = obj 107 106 107 def __get_cache(): 108 """Get the cache""" 109 return cache 110 108 111 def clear(keepstats=False): 109 112 """Clear the cache and statistics""" … … 122 125 wrapper.archive = archive 123 126 wrapper.archived = cache.archived 124 #wrapper._cache = None #XXX127 wrapper.__cache__ = __get_cache 125 128 #wrapper._queue = None #XXX 126 129 return update_wrapper(wrapper, user_function) … … 130 133 131 134 def inf_cache(*arg, **kwd): 132 ''' Infinitely-growing (INF) cache decorator.135 '''infinitely-growing (INF) cache decorator. 133 136 134 137 This decorator memoizes a function's return value each time it is called. … … 162 165 163 166 keymap = kwd.get('keymap', None) 164 if keymap is None: keymap = hashmap(flat=True) #XXX: stringmap ?167 if keymap is None: keymap = hashmap(flat=True) 165 168 cache = kwd.get('cache', None) 166 169 if cache is None: cache = archive_dict() … … 186 189 187 190 def wrapper(*args, **kwds): 188 #XXX: add try/except... any failure has result = f(*args, **kwds)189 191 _args, _kwds = rounded_args(*args, **kwds) 190 192 key = keymap(*_args, **_kwds) … … 212 214 cache.archive = obj 213 215 216 def __get_cache(): 217 """Get the cache""" 218 return cache 219 214 220 def clear(keepstats=False): 215 221 """Clear the cache and statistics""" … … 229 235 wrapper.archive = archive 230 236 wrapper.archived = cache.archived 231 #wrapper._cache = cache #XXX237 wrapper.__cache__ = __get_cache 232 238 #wrapper._queue = None #XXX 233 239 return update_wrapper(wrapper, user_function) … … 237 243 238 244 def lfu_cache(maxsize=100, cache=None, keymap=None, tol=None, deep=False): 239 ''' Least-frequenty-used (LFU) cache decorator.245 '''least-frequenty-used (LFU) cache decorator. 240 246 241 247 This decorator memoizes a function's return value each time it is called. … … 277 283 return inf_cache(cache=cache, keymap=keymap, tol=tol, deep=deep) 278 284 279 if keymap is None: keymap = hashmap(flat=True) #XXX: stringmap ?285 if keymap is None: keymap = hashmap(flat=True) 280 286 if cache is None: cache = archive_dict() 281 287 elif type(cache) is dict: cache = archive_dict(cache) … … 299 305 300 306 def wrapper(*args, **kwds): 301 #XXX: add try/except... any failure has result = f(*args, **kwds)302 307 _args, _kwds = rounded_args(*args, **kwds) 303 308 key = keymap(*_args, **_kwds) … … 340 345 cache.archive = obj 341 346 347 def __get_cache(): 348 """Get the cache""" 349 return cache 350 342 351 def clear(keepstats=False): 343 352 """Clear the cache and statistics""" … … 358 367 wrapper.archive = archive 359 368 wrapper.archived = cache.archived 360 #wrapper._cache = cache #XXX369 wrapper.__cache__ = __get_cache 361 370 #wrapper._queue = use_count #XXX 362 371 return update_wrapper(wrapper, user_function) … … 366 375 367 376 def lru_cache(maxsize=100, cache=None, keymap=None, tol=None, deep=False): 368 ''' Least-recently-used (LRU) cache decorator.377 '''least-recently-used (LRU) cache decorator. 369 378 370 379 This decorator memoizes a function's return value each time it is called. … … 407 416 maxqueue = maxsize * 10 #XXX: user settable? confirm this works as expected 408 417 409 if keymap is None: keymap = hashmap(flat=True) #XXX: stringmap ?418 if keymap is None: keymap = hashmap(flat=True) 410 419 if cache is None: cache = archive_dict() 411 420 elif type(cache) is dict: cache = archive_dict(cache) … … 435 444 436 445 def wrapper(*args, **kwds): 437 #XXX: add try/except... any failure has result = f(*args, **kwds)438 446 _args, _kwds = rounded_args(*args, **kwds) 439 447 key = keymap(*_args, **_kwds) … … 495 503 cache.archive = obj 496 504 505 def __get_cache(): 506 """Get the cache""" 507 return cache 508 497 509 def clear(keepstats=False): 498 510 """Clear the cache and statistics""" … … 514 526 wrapper.archive = archive 515 527 wrapper.archived = cache.archived 516 #wrapper._cache = cache #XXX528 wrapper.__cache__ = __get_cache 517 529 #wrapper._queue = queue #XXX 518 530 return update_wrapper(wrapper, user_function) … … 522 534 523 535 def mru_cache(maxsize=100, cache=None, keymap=None, tol=None, deep=False): 524 ''' Most-recently-used (MRU) cache decorator.536 '''most-recently-used (MRU) cache decorator. 525 537 526 538 This decorator memoizes a function's return value each time it is called. … … 562 574 return inf_cache(cache=cache, keymap=keymap, tol=tol, deep=deep) 563 575 564 if keymap is None: keymap = hashmap(flat=True) #XXX: stringmap ?576 if keymap is None: keymap = hashmap(flat=True) 565 577 if cache is None: cache = archive_dict() 566 578 elif type(cache) is dict: cache = archive_dict(cache) … … 588 600 589 601 def wrapper(*args, **kwds): 590 #XXX: add try/except... any failure has result = f(*args, **kwds)591 602 _args, _kwds = rounded_args(*args, **kwds) 592 603 key = keymap(*_args, **_kwds) … … 627 638 cache.archive = obj 628 639 640 def __get_cache(): 641 """Get the cache""" 642 return cache 643 629 644 def clear(keepstats=False): 630 645 """Clear the cache and statistics""" … … 645 660 wrapper.archive = archive 646 661 wrapper.archived = cache.archived 647 #wrapper._cache = cache #XXX662 wrapper.__cache__ = __get_cache 648 663 #wrapper._queue = queue #XXX 649 664 return update_wrapper(wrapper, user_function) … … 693 708 return inf_cache(cache=cache, keymap=keymap, tol=tol, deep=deep) 694 709 695 if keymap is None: keymap = hashmap(flat=True) #XXX: stringmap ?710 if keymap is None: keymap = hashmap(flat=True) 696 711 if cache is None: cache = archive_dict() 697 712 elif type(cache) is dict: cache = archive_dict(cache) … … 714 729 715 730 def wrapper(*args, **kwds): 716 #XXX: add try/except... any failure has result = f(*args, **kwds)717 731 _args, _kwds = rounded_args(*args, **kwds) 718 732 key = keymap(*_args, **_kwds) … … 748 762 cache.archive = obj 749 763 764 def __get_cache(): 765 """Get the cache""" 766 return cache 767 750 768 def clear(keepstats=False): 751 769 """Clear the cache and statistics""" … … 765 783 wrapper.archive = archive 766 784 wrapper.archived = cache.archived 767 #wrapper._cache = None #XXX785 wrapper.__cache__ = __get_cache 768 786 #wrapper._queue = None #XXX 769 787 return update_wrapper(wrapper, user_function) -
branches/decorate/memoize.py
r666 r667 6 6 from archives import archive_dict 7 7 from keymaps import stringmap 8 from cache import _CacheInfo 8 from cache import _CacheInfo, inf_cache 9 9 10 10 __all__ = ['memoize', 'memoized'] … … 13 13 #XXX: memoized fails when decorating a class method ??? 14 14 15 #XXX: essentially this is: cache.inf_cache(keymap=stringmap(flat=False)) 16 def memoized(cache=None, keymap=None, tol=None, deep=False): 17 """Decorator that memoizes a function's return value each time it is called. 18 If called later with the same arguments, the memoized value is returned, and 19 not re-evaluated. This may lead to memory issues, as cache is not cleared. 20 This decorator takes an integer tolerance 'tol', equal to the number of 21 decimal places to which it will round off floats. 22 23 cache = storage hashmap (default is {}) 24 keymap = cache key encoder (default is keymaps.stringmap(flat=False)) 25 tol = integer tolerance for rounding (default is None) 26 deep = boolean for rounding depth (default is False, i.e. 'shallow') 27 """ 28 maxsize = None 29 30 if keymap is None: keymap = stringmap(flat=False) #XXX: hashmap(flat=True) 31 if cache is None: cache = archive_dict() 32 elif type(cache) is dict: cache = archive_dict(cache) 33 # does archive make sense with database, file, ?... (requires more thought) 34 35 if deep: rounded = deep_round 36 else: rounded = simple_round 37 #else: rounded = shallow_round #FIXME: slow 38 39 @rounded(tol) 40 def rounded_args(*args, **kwds): 41 return (args, kwds) 42 43 def decorating_function(user_function): 44 _len = len # localize the global len() function 45 stats = [0, 0, 0] # make statistics updateable non-locally 46 HIT, MISS, LOAD = 0, 1, 2 # names for the stats fields 47 _len = len # localize the global len() function 48 #lock = RLock() # linkedlist updates aren't threadsafe 49 50 def wrapper(*args, **kwds): 51 try: 52 _args, _kwds = rounded_args(*args, **kwds) 53 key = keymap(*_args, **_kwds) 54 55 try: 56 # get cache entry 57 result = cache[key] 58 stats[HIT] += 1 59 except KeyError: 60 # if not in cache, look in archive 61 if cache.archived(): 62 cache.load(key) 63 try: 64 result = cache[key] 65 stats[LOAD] += 1 66 except KeyError: 67 # if not found, then compute 68 result = user_function(*args, **kwds) 69 cache[key] = result 70 stats[MISS] += 1 71 #XXX: NO cache purge... 72 except: #TypeError 73 result = user_function(*args, **kwds) 74 return result 75 76 def archive(obj): 77 """Replace the cache archive""" 78 cache.archive = obj 79 80 def __get_cache(): 81 """Get the cache""" 82 return cache 83 84 def clear(keepstats=False): 85 """Clear the cache and statistics""" 86 cache.clear() 87 if not keepstats: stats[:] = [0, 0, 0] 88 89 def info(): 90 """Report cache statistics""" 91 return _CacheInfo(stats[HIT], stats[MISS], stats[LOAD], maxsize, len(cache)) 92 93 # interface 94 wrapper.__wrapped__ = user_function 95 wrapper.info = info 96 wrapper.clear = clear 97 wrapper.load = cache.load 98 wrapper.dump = cache.dump 99 wrapper.archive = archive 100 wrapper.archived = cache.archived 101 wrapper.__cache__ = __get_cache 102 return update_wrapper(wrapper, user_function) 103 104 return decorating_function 105 15 # backward compatibility 16 memoized = inf_cache 106 17 107 18 class memoize(object): 108 """Decorator that memoizes a function's return value each time it is called. 19 """This is safecache.inf_cache implemented as a class-based decorator. 20 21 Decorator that memoizes a function's return value each time it is called. 109 22 If called later with the same arguments, the memoized value is returned, and 110 23 not re-evaluated. This may lead to memory issues, as cache is not cleared. … … 113 26 def __init__(self, cache=None, keymap=None, tol=None, deep=False): 114 27 self.__maxsize = None 115 if keymap is None: keymap = stringmap(flat=False) #XXX: hashmap(True) ?28 if keymap is None: keymap = stringmap(flat=False) 116 29 if cache is None: cache = archive_dict() 117 30 elif type(cache) is dict: cache = archive_dict(cache) … … 128 41 return 129 42 130 def __call__(self, user_function): #XXX:i.e. 'decorating_function'43 def __call__(self, user_function): # i.e. 'decorating_function' 131 44 stats = [0, 0, 0] # make statistics updateable non-locally 132 45 HIT, MISS, LOAD = 0, 1, 2 # names for the stats fields … … 136 49 _args, _kwds = self.__rounded_args(*args, **kwds) 137 50 key = self.__keymap(*_args, **_kwds) 138 139 try:140 # get cache entry141 result = self.__cache[key]142 stats[HIT] += 1143 except KeyError:144 # if not in cache, look in archive145 if self.__cache.archived():146 self.__cache.load(key)147 try:148 result = self.__cache[key]149 stats[LOAD] += 1150 except KeyError:151 # if not found, then compute152 result = user_function(*args, **kwds)153 self.__cache[key] = result154 stats[MISS] += 1155 #XXX: NO cache purge...156 51 except: #TypeError 157 52 result = user_function(*args, **kwds) 53 return result 54 55 try: 56 # get cache entry 57 result = self.__cache[key] 58 stats[HIT] += 1 59 except KeyError: 60 # if not in cache, look in archive 61 if self.__cache.archived(): 62 self.__cache.load(key) 63 try: 64 result = self.__cache[key] 65 stats[LOAD] += 1 66 except KeyError: 67 # if not found, then compute 68 result = user_function(*args, **kwds) 69 self.__cache[key] = result 70 stats[MISS] += 1 158 71 return result 159 72 -
branches/decorate/safecache.py
r665 r667 14 14 from rounding import deep_round, simple_round 15 15 from archives import archive_dict 16 from keymaps import hashmap 16 from keymaps import stringmap 17 from cache import Counter 17 18 18 19 _CacheInfo = namedtuple("CacheInfo", ['hit','miss','load','maxsize','size']) 19 20 20 class Counter(dict):21 'Mapping where default values are zero'22 def __missing__(self, key):23 return 024 25 21 #XXX: what about caches that expire due to time, calls, etc... 26 #XXX: check the impact of not serializing by default, and hashmap by default22 #XXX: check the impact of not serializing by default, and stringmap by default 27 23 28 24 def no_cache(*arg, **kwd): 29 ''' empty (NO) cache decorator.25 ''''safe' version of the empty (NO) cache decorator. 30 26 31 27 Unlike other cache decorators, this decorator does not cache. It is a … … 35 31 rounding on inputs will be 'shallow' or 'deep'. 36 32 37 keymap = cache key encoder (default is keymaps. hashmap(flat=True))33 keymap = cache key encoder (default is keymaps.stringmap(flat=False)) 38 34 tol = integer tolerance for rounding (default is None) 39 35 deep = boolean for rounding depth (default is False, i.e. 'shallow') … … 41 37 If *keymap* is given, it will replace the hashing algorithm for generating 42 38 cache keys. Several hashing algorithms are available in 'keymaps'. The 43 default keymap requires arguments to the cached function to be hashable. 39 default keymap does not require arguments to the cached function to be 40 hashable. If a hashing error occurs, the cached function will be evaluated. 44 41 45 42 If the keymap retains type information, then arguments of different types … … 57 54 58 55 keymap = kwd.get('keymap', None) 59 if keymap is None: keymap = hashmap(flat=True) #XXX: stringmap ?56 if keymap is None: keymap = stringmap(flat=False) 60 57 cache = archive_dict() 61 58 … … 78 75 79 76 def wrapper(*args, **kwds): 80 #XXX: add try/except... any failure has result = f(*args, **kwds) 81 _args, _kwds = rounded_args(*args, **kwds) 82 key = keymap(*_args, **_kwds) 77 try: 78 _args, _kwds = rounded_args(*args, **kwds) 79 key = keymap(*_args, **_kwds) 80 except: #TypeError 81 result = user_function(*args, **kwds) 82 return result 83 83 84 84 # look in archive … … 106 106 cache.archive = obj 107 107 108 def __get_cache(): 109 """Get the cache""" 110 return cache 111 108 112 def clear(keepstats=False): 109 113 """Clear the cache and statistics""" … … 122 126 wrapper.archive = archive 123 127 wrapper.archived = cache.archived 124 #wrapper._cache = None #XXX128 wrapper.__cache__ = __get_cache 125 129 #wrapper._queue = None #XXX 126 130 return update_wrapper(wrapper, user_function) … … 130 134 131 135 def inf_cache(*arg, **kwd): 132 ''' Infinitely-growing (INF) cache decorator.136 ''''safe' version of the infinitely-growing (INF) cache decorator. 133 137 134 138 This decorator memoizes a function's return value each time it is called. … … 141 145 142 146 cache = storage hashmap (default is {}) 143 keymap = cache key encoder (default is keymaps. hashmap(flat=True))147 keymap = cache key encoder (default is keymaps.stringmap(flat=False)) 144 148 tol = integer tolerance for rounding (default is None) 145 149 deep = boolean for rounding depth (default is False, i.e. 'shallow') … … 147 151 If *keymap* is given, it will replace the hashing algorithm for generating 148 152 cache keys. Several hashing algorithms are available in 'keymaps'. The 149 default keymap requires arguments to the cached function to be hashable. 153 default keymap does not require arguments to the cached function to be 154 hashable. If a hashing error occurs, the cached function will be evaluated. 150 155 151 156 If the keymap retains type information, then arguments of different types … … 162 167 163 168 keymap = kwd.get('keymap', None) 164 if keymap is None: keymap = hashmap(flat=True) #XXX: stringmap ?169 if keymap is None: keymap = stringmap(flat=False) 165 170 cache = kwd.get('cache', None) 166 171 if cache is None: cache = archive_dict() … … 186 191 187 192 def wrapper(*args, **kwds): 188 #XXX: add try/except... any failure has result = f(*args, **kwds) 189 _args, _kwds = rounded_args(*args, **kwds) 190 key = keymap(*_args, **_kwds) 193 try: 194 _args, _kwds = rounded_args(*args, **kwds) 195 key = keymap(*_args, **_kwds) 196 except: #TypeError 197 result = user_function(*args, **kwds) 198 return result 191 199 192 200 try: … … 212 220 cache.archive = obj 213 221 222 def __get_cache(): 223 """Get the cache""" 224 return cache 225 214 226 def clear(keepstats=False): 215 227 """Clear the cache and statistics""" … … 229 241 wrapper.archive = archive 230 242 wrapper.archived = cache.archived 231 #wrapper._cache = cache #XXX243 wrapper.__cache__ = __get_cache 232 244 #wrapper._queue = None #XXX 233 245 return update_wrapper(wrapper, user_function) … … 237 249 238 250 def lfu_cache(maxsize=100, cache=None, keymap=None, tol=None, deep=False): 239 ''' Least-frequenty-used (LFU) cache decorator.251 ''''safe' version of the least-frequenty-used (LFU) cache decorator. 240 252 241 253 This decorator memoizes a function's return value each time it is called. … … 250 262 maxsize = maximum cache size 251 263 cache = storage hashmap (default is {}) 252 keymap = cache key encoder (default is keymaps. hashmap(flat=True))264 keymap = cache key encoder (default is keymaps.stringmap(flat=False)) 253 265 tol = integer tolerance for rounding (default is None) 254 266 deep = boolean for rounding depth (default is False, i.e. 'shallow') … … 258 270 If *keymap* is given, it will replace the hashing algorithm for generating 259 271 cache keys. Several hashing algorithms are available in 'keymaps'. The 260 default keymap requires arguments to the cached function to be hashable. 272 default keymap does not require arguments to the cached function to be 273 hashable. If a hashing error occurs, the cached function will be evaluated. 261 274 262 275 If the keymap retains type information, then arguments of different types … … 277 290 return inf_cache(cache=cache, keymap=keymap, tol=tol, deep=deep) 278 291 279 if keymap is None: keymap = hashmap(flat=True) #XXX: stringmap ?292 if keymap is None: keymap = stringmap(flat=False) 280 293 if cache is None: cache = archive_dict() 281 294 elif type(cache) is dict: cache = archive_dict(cache) … … 299 312 300 313 def wrapper(*args, **kwds): 301 #XXX: add try/except... any failure has result = f(*args, **kwds) 302 _args, _kwds = rounded_args(*args, **kwds) 303 key = keymap(*_args, **_kwds) 314 try: 315 _args, _kwds = rounded_args(*args, **kwds) 316 key = keymap(*_args, **_kwds) 317 except: #TypeError 318 result = user_function(*args, **kwds) 319 return result 304 320 305 321 try: … … 340 356 cache.archive = obj 341 357 358 def __get_cache(): 359 """Get the cache""" 360 return cache 361 342 362 def clear(keepstats=False): 343 363 """Clear the cache and statistics""" … … 358 378 wrapper.archive = archive 359 379 wrapper.archived = cache.archived 360 #wrapper._cache = cache #XXX380 wrapper.__cache__ = __get_cache 361 381 #wrapper._queue = use_count #XXX 362 382 return update_wrapper(wrapper, user_function) … … 366 386 367 387 def lru_cache(maxsize=100, cache=None, keymap=None, tol=None, deep=False): 368 ''' Least-recently-used (LRU) cache decorator.388 ''''safe' version of the least-recently-used (LRU) cache decorator. 369 389 370 390 This decorator memoizes a function's return value each time it is called. … … 379 399 maxsize = maximum cache size 380 400 cache = storage hashmap (default is {}) 381 keymap = cache key encoder (default is keymaps. hashmap(flat=True))401 keymap = cache key encoder (default is keymaps.stringmap(flat=False)) 382 402 tol = integer tolerance for rounding (default is None) 383 403 deep = boolean for rounding depth (default is False, i.e. 'shallow') … … 387 407 If *keymap* is given, it will replace the hashing algorithm for generating 388 408 cache keys. Several hashing algorithms are available in 'keymaps'. The 389 default keymap requires arguments to the cached function to be hashable. 409 default keymap does not require arguments to the cached function to be 410 hashable. If a hashing error occurs, the cached function will be evaluated. 390 411 391 412 If the keymap retains type information, then arguments of different types … … 407 428 maxqueue = maxsize * 10 #XXX: user settable? confirm this works as expected 408 429 409 if keymap is None: keymap = hashmap(flat=True) #XXX: stringmap ?430 if keymap is None: keymap = stringmap(flat=False) 410 431 if cache is None: cache = archive_dict() 411 432 elif type(cache) is dict: cache = archive_dict(cache) … … 435 456 436 457 def wrapper(*args, **kwds): 437 #XXX: add try/except... any failure has result = f(*args, **kwds) 438 _args, _kwds = rounded_args(*args, **kwds) 439 key = keymap(*_args, **_kwds) 458 try: 459 _args, _kwds = rounded_args(*args, **kwds) 460 key = keymap(*_args, **_kwds) 461 except: #TypeError 462 result = user_function(*args, **kwds) 463 return result 440 464 441 465 try: … … 495 519 cache.archive = obj 496 520 521 def __get_cache(): 522 """Get the cache""" 523 return cache 524 497 525 def clear(keepstats=False): 498 526 """Clear the cache and statistics""" … … 514 542 wrapper.archive = archive 515 543 wrapper.archived = cache.archived 516 #wrapper._cache = cache #XXX544 wrapper.__cache__ = __get_cache 517 545 #wrapper._queue = queue #XXX 518 546 return update_wrapper(wrapper, user_function) … … 522 550 523 551 def mru_cache(maxsize=100, cache=None, keymap=None, tol=None, deep=False): 524 ''' Most-recently-used (MRU) cache decorator.552 ''''safe' version of the most-recently-used (MRU) cache decorator. 525 553 526 554 This decorator memoizes a function's return value each time it is called. … … 535 563 maxsize = maximum cache size 536 564 cache = storage hashmap (default is {}) 537 keymap = cache key encoder (default is keymaps. hashmap(flat=True))565 keymap = cache key encoder (default is keymaps.stringmap(flat=False)) 538 566 tol = integer tolerance for rounding (default is None) 539 567 deep = boolean for rounding depth (default is False, i.e. 'shallow') … … 543 571 If *keymap* is given, it will replace the hashing algorithm for generating 544 572 cache keys. Several hashing algorithms are available in 'keymaps'. The 545 default keymap requires arguments to the cached function to be hashable. 573 default keymap does not require arguments to the cached function to be 574 hashable. If a hashing error occurs, the cached function will be evaluated. 546 575 547 576 If the keymap retains type information, then arguments of different types … … 562 591 return inf_cache(cache=cache, keymap=keymap, tol=tol, deep=deep) 563 592 564 if keymap is None: keymap = hashmap(flat=True) #XXX: stringmap ?593 if keymap is None: keymap = stringmap(flat=False) 565 594 if cache is None: cache = archive_dict() 566 595 elif type(cache) is dict: cache = archive_dict(cache) … … 588 617 589 618 def wrapper(*args, **kwds): 590 #XXX: add try/except... any failure has result = f(*args, **kwds) 591 _args, _kwds = rounded_args(*args, **kwds) 592 key = keymap(*_args, **_kwds) 619 try: 620 _args, _kwds = rounded_args(*args, **kwds) 621 key = keymap(*_args, **_kwds) 622 except: #TypeError 623 result = user_function(*args, **kwds) 624 return result 593 625 594 626 try: … … 627 659 cache.archive = obj 628 660 661 def __get_cache(): 662 """Get the cache""" 663 return cache 664 629 665 def clear(keepstats=False): 630 666 """Clear the cache and statistics""" … … 645 681 wrapper.archive = archive 646 682 wrapper.archived = cache.archived 647 #wrapper._cache = cache #XXX683 wrapper.__cache__ = __get_cache 648 684 #wrapper._queue = queue #XXX 649 685 return update_wrapper(wrapper, user_function) … … 653 689 654 690 def rr_cache(maxsize=100, cache=None, keymap=None, tol=None, deep=False): 655 ''' random-replacement (RR) cache decorator.691 ''''safe' version of the random-replacement (RR) cache decorator. 656 692 657 693 This decorator memoizes a function's return value each time it is called. … … 666 702 maxsize = maximum cache size 667 703 cache = storage hashmap (default is {}) 668 keymap = cache key encoder (default is keymaps. hashmap(flat=True))704 keymap = cache key encoder (default is keymaps.stringmap(flat=False)) 669 705 tol = integer tolerance for rounding (default is None) 670 706 deep = boolean for rounding depth (default is False, i.e. 'shallow') … … 674 710 If *keymap* is given, it will replace the hashing algorithm for generating 675 711 cache keys. Several hashing algorithms are available in 'keymaps'. The 676 default keymap requires arguments to the cached function to be hashable. 712 default keymap does not require arguments to the cached function to be 713 hashable. If a hashing error occurs, the cached function will be evaluated. 677 714 678 715 If the keymap retains type information, then arguments of different types … … 693 730 return inf_cache(cache=cache, keymap=keymap, tol=tol, deep=deep) 694 731 695 if keymap is None: keymap = hashmap(flat=True) #XXX: stringmap ?732 if keymap is None: keymap = stringmap(flat=False) 696 733 if cache is None: cache = archive_dict() 697 734 elif type(cache) is dict: cache = archive_dict(cache) … … 714 751 715 752 def wrapper(*args, **kwds): 716 #XXX: add try/except... any failure has result = f(*args, **kwds) 717 _args, _kwds = rounded_args(*args, **kwds) 718 key = keymap(*_args, **_kwds) 753 try: 754 _args, _kwds = rounded_args(*args, **kwds) 755 key = keymap(*_args, **_kwds) 756 except: #TypeError 757 result = user_function(*args, **kwds) 758 return result 719 759 720 760 try: … … 748 788 cache.archive = obj 749 789 790 def __get_cache(): 791 """Get the cache""" 792 return cache 793 750 794 def clear(keepstats=False): 751 795 """Clear the cache and statistics""" … … 765 809 wrapper.archive = archive 766 810 wrapper.archived = cache.archived 767 #wrapper._cache = None #XXX811 wrapper.__cache__ = __get_cache 768 812 #wrapper._queue = None #XXX 769 813 return update_wrapper(wrapper, user_function) -
branches/decorate/surrogate.py
r665 r667 20 20 21 21 22 from memoize import memoized 22 from safecache import inf_cache as memoized 23 #from cache import inf_cache as memoized 23 24 from archives import archive_dict, file_archive 24 from keymaps import picklemap, hashmap25 from keymaps import picklemap, stringmap, hashmap 25 26 dumps = picklemap(flat=False) 27 encode = stringmap(flat=False) 28 hashed = hashmap(flat=False) 26 29 cache = archive_dict(archive=file_archive('surrogate.pkl')) 27 30 #@memoized(keymap=dumps, tol=0, deep=True) # slower, but more robust 28 #@memoized( tol=0, deep=True)31 #@memoized(keymap=encode, tol=0, deep=True) 29 32 #@memoized(cache=cache, keymap=dumps) # slower, but more robust 30 @memoized(cache=cache) 33 #@memoized(cache=cache, keymap=hashed) # fails to cache (unhashable) 34 @memoized(cache=cache, keymap=encode) 31 35 def marc_surr(x): 32 36 """calculate perforation area using a tanh-based model surrogate -
branches/decorate/test_cache.py
r666 r667 21 21 """ 22 22 23 from memoize importmemoized24 #from memoize import memoize 23 #from safecache import inf_cache as memoized 24 from cache import inf_cache as memoized 25 25 from timer import timed 26 26 from keymaps import picklemap -
branches/decorate/test_cached.py
r666 r667 1 from memoize import memoized 1 #from safecache import inf_cache as memoized 2 from cache import inf_cache as memoized 2 3 from archives import file_archive 3 4 from timer import timed -
branches/decorate/test_timed_monitor.py
r666 r667 179 179 try: model.dump() 180 180 except: pass 181 try: print model.info() 182 except: pass 181 183 182 184 # EOF
Note: See TracChangeset
for help on using the changeset viewer.