1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """
20 Generic data structures.
21 """
24 """
25 Dictionary form which key can be assessed as attribute.
26 """
28 """
29 Consult self dictionary for attribute. It's a shorthand.
30 """
31 return self[name]
33 """
34 Save to self dictionary first, then self object table.
35
36 @note: This method is overriden as a stupid-preventer. It makes
37 attribute setting consistent with attribute getting.
38 """
39 if name in self:
40 self[name] = value
41 else:
42 super(AttributeDict, self).__setattr__(name, value)
43
45 """
46 Dictionary with default values.
47
48 @cvar defdict: dictionary for default values.
49 @type defdict: dict
50 """
51 defdict = {}
53 """
54 Assign default values to self after initiated.
55 """
56 super(DefaultDict, self).__init__(*args, **kw)
57 for key in self.defdict:
58 self[key] = self.defdict[key]
59
61 """
62 Dictionary in which key/value can only be assigned once.
63 """
65 """
66 Check for duplicated assignment.
67 """
68 if key in self:
69 raise IndexError, "Cannot reset value for key=%s to override %s."%(
70 str(key), str(self[key]))
71 super(SingleAssignDict, self).__setitem__(key, item)
72
74 """
75 Registry class for the name of types.
76 """
78 self[tobj.__name__] = tobj
79 return tobj
80
82 """
83 Timer dictionary with increase method.
84 """
86 self.vtype = kw.pop('vtype', float)
87 super(Timer, self).__init__(*args, **kw)
89 self[key] = self.get(key, self.vtype(0)) + self.vtype(delta)
90
93 """
94 Set up pointers. All attributes which are pointers have to be
95 initialized here.
96 """
97 pass
99 """
100 Release pointer.
101 """
102 for key in self._pointers_:
103 setattr(self, key, None)
104 @property
106 """
107 Determine if all the pointers are fully bound.
108 """
109 for key in self._pointers_:
110 if getattr(self, key, None)==None:
111 return False
112 return True
113 @property
115 """
116 Determine if all the pointers are fully unbound.
117 """
118 for key in self._pointers_:
119 if getattr(self, key, None)!=None:
120 return False
121 return True
123 """
124 Meta class to make classes with ctypes pointers or containers with ctypes
125 pointers. The type will feather classes with bind/unbind methods along
126 with is_bound/is_unbound properties. The names of pointer variables have
127 to be listed in _pointers_ class list variable.
128
129 The bind/unbind methods are designed to be applied to pointers used by the
130 instance. is_bound/is_unbound properties can test for if pointers are
131 fully bound or fully unbound to the instance, respectively. You have to
132 override the bind method and initiate pointers in it rather than in other
133 method. You can leave it alone if you don't need it. Be sure to enter
134 correct entries into the _pointers_ class variable.
135
136 @cvar _pointers_: a list containing names of variables for ctypes pointers,
137 ctypes structures, or containers that hold ctypes pointers. The list
138 would be used in binding/unbinding process. Subclassing does not
139 override the content of this list. The names defined in the
140 superclasses will be prepended in front of anything in the list defined
141 in the subclass.
142 @ctype _pointers_: list
143 """
144 - def __new__(cls, name, bases, namespace):
145
146 pointers = []
147 for base in bases:
148 pointers.extend(getattr(base, '_pointers_', []))
149 pointers.extend(namespace.get('_pointers_', []))
150 namespace['_pointers_'] = pointers
151
152
153 for key in ('bind', 'unbind', 'is_bound', 'is_unbound'):
154 if namespace.get(key, None) != None:
155 continue
156 nokeyinbases = True
157 for base in bases:
158 if getattr(base, key, None) != None:
159 nokeyinbases = False
160 break
161 if nokeyinbases:
162 namespace[key] = globals()[key]
163
164 return super(TypeWithBinder, cls).__new__(cls, name, bases, namespace)
165