~ubuntu-branches/ubuntu/quantal/genometools/quantal-backports

« back to all changes in this revision

Viewing changes to gtpython/gt/props.py

  • Committer: Package Import Robot
  • Author(s): Sascha Steinbiss
  • Date: 2012-07-09 14:10:23 UTC
  • Revision ID: package-import@ubuntu.com-20120709141023-juuu4spm6chqsf9o
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
 
 
5
class cachedproperty(object):
 
6
 
 
7
    '''
 
8
    >>> class C(object):
 
9
    ...    def __init__(self, x):
 
10
    ...        self._x = x
 
11
    ...    def get_x(self):
 
12
    ...        print "getting x"
 
13
    ...        return self._x
 
14
    ...
 
15
    ...    def set_x(self, newx):
 
16
    ...        print "setting x with %s" % newx
 
17
    ...        self._x = newx
 
18
    ...    def del_x(self):
 
19
    ...        self._x = "i am deleted"
 
20
    ...
 
21
    ...    x = cachedproperty(get_x, set_x, del_x)
 
22
    ...    other_x = cachedproperty(get_x, set_x)
 
23
 
 
24
    >>> c = C(5)
 
25
    >>> c.x
 
26
    getting x
 
27
    5
 
28
 
 
29
    # cached.
 
30
    >>> c.x
 
31
    5
 
32
 
 
33
    >>> c.x, c.y = 6, 7
 
34
    setting x with 6
 
35
 
 
36
    # uncached.
 
37
    >>> c.x
 
38
    getting x
 
39
    6
 
40
 
 
41
    >>> c.x, c.y
 
42
    (6, 7)
 
43
 
 
44
    >>> c.y = 35
 
45
    >>> c.x, c.y
 
46
    (6, 35)
 
47
 
 
48
    # ok with multiple instances.
 
49
    >>> d = C(4)
 
50
    >>> d.x
 
51
    getting x
 
52
    4
 
53
 
 
54
    >>> c.x
 
55
    6
 
56
    >>> c.other_x = 7
 
57
    setting x with 7
 
58
 
 
59
    >>> c.get_x()
 
60
    getting x
 
61
    7
 
62
    >>> del c.x
 
63
    >>> c.x
 
64
    getting x
 
65
    \'i am deleted\'
 
66
 
 
67
    >>> c.set_x(22)
 
68
    setting x with 22
 
69
 
 
70
    # but the property cant konw about it...
 
71
    >>> c.x
 
72
    \'i am deleted\'
 
73
 
 
74
 
 
75
    '''
 
76
 
 
77
    __slots__ = ("fget", "fset", "fdel", "n")
 
78
 
 
79
    def __init__(self, fget=None, fset=None, fdel=None):
 
80
        self.fget = fget
 
81
        self.fset = fset
 
82
        self.fdel = fdel
 
83
        self.n = "__" + fget.__name__
 
84
 
 
85
    def __get__(self, o, otype=None):
 
86
        if o is None:
 
87
            return None
 
88
        if self.n in o.__dict__:
 
89
            return (o.__dict__)[self.n]
 
90
        result = (o.__dict__)[self.n] = self.fget(o)
 
91
        return result
 
92
 
 
93
    def __set__(self, o, value):
 
94
        if self.fset is None:
 
95
            raise AttributeError, "unsettable %s (with %s)" % (self.n,
 
96
                    value)
 
97
        else:
 
98
            if o == 22:
 
99
                print self.n, o, value
 
100
            if self.n in o.__dict__:
 
101
                del (o.__dict__)[self.n]
 
102
            self.fset(o, value)
 
103
 
 
104
    def __delete__(self, o):
 
105
        if self.fdel is None:
 
106
            raise AttributeError, "undeletable %s (with %s)" % (self.n,
 
107
                    value)
 
108
        else:
 
109
            if self.n in o.__dict__:
 
110
                del (o.__dict__)[self.n]
 
111
            self.fdel(o)
 
112
 
 
113
 
 
114
if __name__ == "__main__":
 
115
    import doctest
 
116
    doctest.testmod()