| 1 | # This program is free software; you can redistribute it and/or modify it under |
|---|
| 2 | # the terms of the GNU General Public License as published by the Free Software |
|---|
| 3 | # Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 4 | # version. |
|---|
| 5 | # |
|---|
| 6 | # This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 7 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 8 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 9 | # |
|---|
| 10 | # You should have received a copy of the GNU General Public License along with |
|---|
| 11 | # this program; if not, write to the Free Software Foundation, Inc., |
|---|
| 12 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 13 | """this module contains some function/method decorators |
|---|
| 14 | |
|---|
| 15 | :author: Logilab |
|---|
| 16 | :copyright: 2006-2008 LOGILAB S.A. (Paris, FRANCE) |
|---|
| 17 | :contact: http://www.logilab.fr/ -- mailto:python-projects@logilab.org |
|---|
| 18 | """ |
|---|
| 19 | __docformat__ = "restructuredtext en" |
|---|
| 20 | |
|---|
| 21 | # XXX rewrite so we can use the decorator syntax when keyarg has to be specified |
|---|
| 22 | |
|---|
| 23 | def cached(callableobj, keyarg=None): |
|---|
| 24 | """simple decorator to cache result of method call""" |
|---|
| 25 | #print callableobj, keyarg, callableobj.func_code.co_argcount |
|---|
| 26 | if callableobj.func_code.co_argcount == 1 or keyarg == 0: |
|---|
| 27 | |
|---|
| 28 | def cache_wrapper1(self, *args): |
|---|
| 29 | cache = '_%s_cache_' % callableobj.__name__ |
|---|
| 30 | #print 'cache1?', cache |
|---|
| 31 | try: |
|---|
| 32 | return self.__dict__[cache] |
|---|
| 33 | except KeyError: |
|---|
| 34 | #print 'miss' |
|---|
| 35 | value = callableobj(self, *args) |
|---|
| 36 | setattr(self, cache, value) |
|---|
| 37 | return value |
|---|
| 38 | return cache_wrapper1 |
|---|
| 39 | |
|---|
| 40 | elif keyarg: |
|---|
| 41 | |
|---|
| 42 | def cache_wrapper2(self, *args, **kwargs): |
|---|
| 43 | cache = '_%s_cache_' % callableobj.__name__ |
|---|
| 44 | key = args[keyarg-1] |
|---|
| 45 | #print 'cache2?', cache, self, key |
|---|
| 46 | try: |
|---|
| 47 | _cache = self.__dict__[cache] |
|---|
| 48 | except KeyError: |
|---|
| 49 | #print 'init' |
|---|
| 50 | _cache = {} |
|---|
| 51 | setattr(self, cache, _cache) |
|---|
| 52 | try: |
|---|
| 53 | return _cache[key] |
|---|
| 54 | except KeyError: |
|---|
| 55 | #print 'miss', self, cache, key |
|---|
| 56 | _cache[key] = callableobj(self, *args, **kwargs) |
|---|
| 57 | return _cache[key] |
|---|
| 58 | return cache_wrapper2 |
|---|
| 59 | |
|---|
| 60 | def cache_wrapper3(self, *args): |
|---|
| 61 | cache = '_%s_cache_' % callableobj.__name__ |
|---|
| 62 | #print 'cache3?', cache, self, args |
|---|
| 63 | try: |
|---|
| 64 | _cache = self.__dict__[cache] |
|---|
| 65 | except KeyError: |
|---|
| 66 | #print 'init' |
|---|
| 67 | _cache = {} |
|---|
| 68 | setattr(self, cache, _cache) |
|---|
| 69 | try: |
|---|
| 70 | return _cache[args] |
|---|
| 71 | except KeyError: |
|---|
| 72 | #print 'miss' |
|---|
| 73 | _cache[args] = callableobj(self, *args) |
|---|
| 74 | return _cache[args] |
|---|
| 75 | return cache_wrapper3 |
|---|
| 76 | |
|---|
| 77 | def clear_cache(obj, funcname): |
|---|
| 78 | """function to clear a cache handled by the cached decorator""" |
|---|
| 79 | try: |
|---|
| 80 | del obj.__dict__['_%s_cache_' % funcname] |
|---|
| 81 | except KeyError: |
|---|
| 82 | pass |
|---|
| 83 | |
|---|
| 84 | def copy_cache(obj, funcname, cacheobj): |
|---|
| 85 | """copy cache for <funcname> from cacheobj to obj""" |
|---|
| 86 | cache = '_%s_cache_' % funcname |
|---|
| 87 | try: |
|---|
| 88 | setattr(obj, cache, cacheobj.__dict__[cache]) |
|---|
| 89 | except KeyError: |
|---|
| 90 | pass |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | class wproperty(object): |
|---|
| 94 | """simple descriptor expecting to take a modifier function as first argument |
|---|
| 95 | and looking for a _<function name> to retreive the attribute |
|---|
| 96 | """ |
|---|
| 97 | def __init__(self, setfunc): |
|---|
| 98 | self.setfunc = setfunc |
|---|
| 99 | self.attrname = '_%s' % setfunc.__name__ |
|---|
| 100 | |
|---|
| 101 | def __set__(self, obj, value): |
|---|
| 102 | self.setfunc(obj, value) |
|---|
| 103 | |
|---|
| 104 | def __get__(self, obj, cls): |
|---|
| 105 | assert obj is not None |
|---|
| 106 | return getattr(obj, self.attrname) |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | class classproperty(object): |
|---|
| 110 | def __init__(self, get): |
|---|
| 111 | self.get = get |
|---|
| 112 | def __get__(self, inst, cls): |
|---|
| 113 | return self.get(cls) |
|---|
| 114 | |
|---|
| 115 | from time import clock |
|---|
| 116 | def timed(f): |
|---|
| 117 | def wrap(*args, **kwargs): |
|---|
| 118 | t = clock() |
|---|
| 119 | #for i in range(100): |
|---|
| 120 | res = f(*args, **kwargs) |
|---|
| 121 | print '%s time: %.9f' % (f.__name__, clock() - t) |
|---|
| 122 | return res |
|---|
| 123 | return wrap |
|---|