2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
5 # Gregory Starck, g.starck@gmail.com
6 # Hartmut Goebel, h.goebel@goebel-consult.de
8 #This file is part of Shinken.
10 #Shinken is free software: you can redistribute it and/or modify
11 #it under the terms of the GNU Affero General Public License as published by
12 #the Free Software Foundation, either version 3 of the License, or
13 #(at your option) any later version.
15 #Shinken is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU Affero General Public License for more details.
20 #You should have received a copy of the GNU Affero General Public License
21 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
24 class memoized(object):
25 """Decorator that caches a function's return value each time it is called.
26 If called later with the same arguments, the cached value is returned, and
29 def __init__(self
, func
):
32 def __call__(self
, *args
):
34 return self
.cache
[args
]
36 self
.cache
[args
] = value
= self
.func(*args
)
39 # uncachable -- for instance, passing a list as an argument.
40 # Better to not cache than to blow up entirely.
41 return self
.func(*args
)
43 """Return the function's docstring."""
44 return self
.func
.__doc
__
48 #def memoized(function, limit=None):
49 # if isinstance(function, int):
50 # def memoize_wrapper(f):
51 # return memoized(f, function)
53 # return memoize_wrapper
57 # def memoize_wrapper(*args, **kwargs):
58 # key = cPickle.dumps((args, kwargs))
60 # list.append(list.pop(list.index(key)))
62 # dict[key] = function(*args, **kwargs)
64 # if limit is not None and len(list) > limit:
65 # del dict[list.pop(0)]
69 # memoize_wrapper._memoize_dict = dict
70 # memoize_wrapper._memoize_list = list
71 # memoize_wrapper._memoize_limit = limit
72 # memoize_wrapper._memoize_origfunc = function
73 # memoize_wrapper.func_name = function.func_name
74 # return memoize_wrapper
80 # "Return the nth fibonacci number."
84 # val = fibonacci(n-1) + fibonacci(n-2)