Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / astroid / decorators.py @ 745

History | View | Annotate | Download (2.44 KB)

1
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
3
#
4
# This file is part of astroid.
5
#
6
# astroid is free software: you can redistribute it and/or modify it
7
# under the terms of the GNU Lesser General Public License as published by the
8
# Free Software Foundation, either version 2.1 of the License, or (at your
9
# option) any later version.
10
#
11
# astroid is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14
# for more details.
15
#
16
# You should have received a copy of the GNU Lesser General Public License along
17
# with astroid. If not, see <http://www.gnu.org/licenses/>.
18
#
19
# The code in this file was originally part of logilab-common, licensed under
20
# the same license.
21

    
22
""" A few useful function/method decorators."""
23

    
24
import wrapt
25

    
26

    
27
@wrapt.decorator
28
def cached(func, instance, args, kwargs):
29
    """Simple decorator to cache result of method calls without args."""
30
    cache = getattr(instance, '__cache', None)
31
    if cache is None:
32
        instance.__cache = cache = {}
33
    try:
34
        return cache[func]
35
    except KeyError:
36
        cache[func] = result = func(*args, **kwargs)
37
        return result
38

    
39

    
40
class cachedproperty(object):
41
    """ Provides a cached property equivalent to the stacking of
42
    @cached and @property, but more efficient.
43

44
    After first usage, the <property_name> becomes part of the object's
45
    __dict__. Doing:
46

47
      del obj.<property_name> empties the cache.
48

49
    Idea taken from the pyramid_ framework and the mercurial_ project.
50

51
    .. _pyramid: http://pypi.python.org/pypi/pyramid
52
    .. _mercurial: http://pypi.python.org/pypi/Mercurial
53
    """
54
    __slots__ = ('wrapped',)
55

    
56
    def __init__(self, wrapped):
57
        try:
58
            wrapped.__name__
59
        except AttributeError:
60
            raise TypeError('%s must have a __name__ attribute' %
61
                            wrapped)
62
        self.wrapped = wrapped
63

    
64
    @property
65
    def __doc__(self):
66
        doc = getattr(self.wrapped, '__doc__', None)
67
        return ('<wrapped by the cachedproperty decorator>%s'
68
                % ('\n%s' % doc if doc else ''))
69

    
70
    def __get__(self, inst, objtype=None):
71
        if inst is None:
72
            return self
73
        val = self.wrapped(inst)
74
        setattr(inst, self.wrapped.__name__, val)
75
        return val