File: property.py

package info (click to toggle)
kaa-base 0.6.0%2Bsvn4596-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 2,348 kB
  • ctags: 3,068
  • sloc: python: 11,094; ansic: 1,862; makefile: 74
file content (79 lines) | stat: -rw-r--r-- 1,710 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python
#
# This tests the property decorator in kaa.utils, which is designed to be
# forward-compatible with Python 2.6 and Python 3.0.  Demonstrates propety
# getters, setters, and deleters, with inheritance.
#
# See:
#   http://bugs.python.org/issue1416
#   http://permalink.gmane.org/gmane.comp.python.general/551183
#   http://bugs.python.org/issue1620

from kaa.utils import property

class A(object):
    def __init__(self):
        self._readonly = 42
        self._writable = 0

    @property
    def readonly(self):
        "readonly docstring"
        return self._readonly

    @property
    def writable(self):
        return self._writable

    @writable.setter
    def writable(self, value):
        self._writable = value


class B(A):
    @A.readonly.getter
    def readonly(self):
        "subclass readonly docstring"
        return super(B, self).readonly * 2

    @readonly.setter
    def readonly(self, value):
        # Ok, not exactly readonly anymore :)
        self._readonly = value

    @A.writable.deleter
    def writable(self):
        self._writable = None


a = A()
assert(a.readonly == 42)
try:
    a.readonly = 0
except AttributeError:
    pass
else:
    raise AttributeError('a.readonly was writable')

a.writable = a.readonly / 2
assert(a.writable == 21)

try:
    del a.writable
except AttributeError:
    pass
else:
    raise AttributeError('del a.writable did not raise exception as expected')

assert(A.readonly.__doc__ == 'readonly docstring')

b = B()
assert(b.readonly == 42*2)
b.readonly = 20
assert(b.readonly == 20*2)
del b.writable
assert(b.writable is None)

assert(B.readonly.__doc__ == 'subclass readonly docstring')
print 'All property tests ok.'