1 """Defines the base class for simple types."""
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 from pyffi.object_models.any_type import AnyType
57
59 """Base class from which all simple types are derived. Simple
60 types contain data which is not divided further into smaller pieces,
61 and that can represented efficiently by a (usually native) Python type,
62 typically ``int``, ``float``, or ``str``.
63
64 A brief example of usage:
65
66 >>> class Short(SimpleType):
67 ... def __init__(self):
68 ... # for fun, let default value be 3
69 ... self._value = 3
70 ... def set_value(self, value):
71 ... # check type
72 ... if not isinstance(value, int):
73 ... raise TypeError("Expected int but got %s."
74 ... % value.__class__.__name__)
75 ... # check range
76 ... if value < -0x8000 or value > 0x7fff:
77 ... raise ValueError("Value %i out of range." % value)
78 ... self._value = value
79 >>> test = Short()
80 >>> print(test)
81 3
82 >>> test.value = 255
83 >>> print(test)
84 255
85 >>> test.value = 100000 # doctest: +ELLIPSIS
86 Traceback (most recent call last):
87 ...
88 ValueError: ...
89 >>> test.value = "hello world" # doctest: +ELLIPSIS
90 Traceback (most recent call last):
91 ...
92 TypeError: ...
93
94 Also override :meth:`read` and :meth:`write` if you wish to read and write data
95 of this type, and :meth:`is_interchangeable` if you wish to declare data as
96 equivalent.
97 """
98
99 __metaclass__ = _MetaSimpleType
100
101
102
103 @property
105 """A property which wraps the actual data. This property
106 always calls :meth:`set_value` to assign the value, and ensures
107 that the value is valid (type, range, ...). Unless you know
108 what you are doing, always use the `value` property to change
109 the data.
110 """
111 return None
112
113 _value = None
114 """The data."""
115
117 """String representation. This implementation is simply a wrapper
118 around ``str`` on :attr:`_value`.
119
120 :return: String representation.
121 :rtype: ``str``
122 """
123 return self._value.__str__()
124
126 """Return the stored value.
127
128 :return: The stored value.
129 :rtype: Whatever is appropriate.
130 """
131 return self._value
132
134 """Set stored value. Override this method to enable validation
135 (type checking, range checking, and so on).
136
137 :param value: The value to store.
138 :type value: Whatever is appropriate.
139 """
140 self._value = value
141
142
143
145 """This checks for object identity of the value."""
146 return isinstance(other, SimpleType) and (self._value is other._value)
147
148
149
151 """Display string for the detail tree. This implementation is simply
152 a wrapper around C{self.:attr:`_value`.__str__()}.
153
154 :return: String representation.
155 :rtype: ``str``
156 """
157 return self._value.__str__()
158