Package memtools :: Module pattern
[hide private]

Source Code for Module memtools.pattern

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3  # 
 4  #       pattern.py 
 5  # 
 6  #       Copyright 2010 Pablo Alejandro Costesich <pcostesi@alu.itba.edu.ar> 
 7  # 
 8  #       Redistribution and use in source and binary forms, with or without 
 9  #       modification, are permitted provided that the following conditions are 
10  #       met: 
11  # 
12  #       * Redistributions of source code must retain the above copyright 
13  #         notice, this list of conditions and the following disclaimer. 
14  #       * Redistributions in binary form must reproduce the above 
15  #         copyright notice, this list of conditions and the following disclaimer 
16  #         in the documentation and/or other materials provided with the 
17  #         distribution. 
18  #       * Neither the name of the Dev Team nor the names of its 
19  #         contributors may be used to endorse or promote products derived from 
20  #         this software without specific prior written permission. 
21  # 
22  #       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
23  #       "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
24  #       LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
25  #       A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
26  #       OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
27  #       SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
28  #       LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
29  #       DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
30  #       THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
31  #       (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
32  #       OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
33  # 
34   
35   
36   
37  from functools import wraps 
38  from hashlib import md5 
39  import logging 
40   
41 -class Memorized(object):
42 """ This class wraps a normal callable and returns a memorized callable 43 with a "memo" storage. End users are not intended to know what happens 44 inside this class, neither they should know about it. 45 46 """ 47
48 - def __init__(self, f, memo, hashing_function=md5, debug=False):
49 self.__f = f 50 self.__memo = memo 51 logging.basicConfig(level=logging.WARNING) 52 self.log = logging.getLogger("Memorized Callable %s" % f.__name__) 53 if debug: 54 self.log.setLevel(logging.DEBUG) 55 self.hashing_function = hashing_function
56
57 - def __create_key(self, f, *args, **kwargs):
58 # TODO: Is there another way to create a key? 59 the_hash = self.hashing_function(f.__name__) 60 for arg in args: 61 the_hash.update(str(arg)) 62 the_hash.update("|") 63 for key, val in kwargs.iteritems(): 64 the_hash.update("%s:%s;".__mod__(key, val)) 65 return the_hash.hexdigest()
66
67 - def __call__(self, *args, **kwargs):
68 key = self.__create_key(self.__f, args, kwargs) 69 self.log.debug("Calling memorized value %s", key) 70 try: 71 return self.__memo[key] 72 except KeyError: 73 self.log.debug("No key %s found. Calculating value...", key) 74 val = self.__f(*args, **kwargs) 75 self.__memo[key] = val 76 return val
77 78
79 -class memorize(object):
80 """ 81 The memorize decorator takes a Memory (or Memory-compatible) object 82 and masks the Memorized decorator to keep the original signature 83 clean. 84 85 Use of this decorator is deprecated since Memory objects implement 86 the same functionality in their __call__ methods. 87 """ 88
89 - def __init__(self, memory, debug=False):
90 self._memory = memory 91 self.debug = debug
92
93 - def __call__(self, f):
94 memo = Memorized(f, self._memory, self.debug) 95 wraps(f)(memo) 96 return memo
97