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 import numpy as np
33 from .pickleable import Pickleable
34 from .observable import Observable
35
36 -class ObsAr(np.ndarray, Pickleable, Observable):
37 """
38 An ndarray which reports changes to its observers.
39
40 .. warning::
41
42 ObsAr tries to not ever give back an observable array itself. Thus,
43 if you want to preserve an ObsAr you need to work in memory. Let
44 `a` be an ObsAr and you want to add a random number `r` to it. You need to
45 make sure it stays an ObsAr by working in memory (see numpy for details):
46
47 .. code-block:: python
48
49 a[:] += r
50
51 The observers can add themselves with a callable, which
52 will be called every time this array changes. The callable
53 takes exactly one argument, which is this array itself.
54 """
55 __array_priority__ = -1
56 - def __new__(cls, input_array, *a, **kw):
57
58 if not isinstance(input_array, ObsAr):
59 try:
60
61 obj = np.atleast_1d(np.require(input_array, dtype=np.float_, requirements=['W', 'C'])).view(cls)
62 except ValueError:
63
64 obj = np.atleast_1d(np.require(input_array, requirements=['W', 'C'])).view(cls)
65 else: obj = input_array
66 super(ObsAr, obj).__init__(*a, **kw)
67 return obj
68
70
71 if obj is None: return
72 self.observers = getattr(obj, 'observers', None)
73 self._update_on = getattr(obj, '_update_on', None)
74
76
77
78 return out_arr.view(np.ndarray)
79
83
84 @property
86 """
87 Return the ObsAr underlying array as a standard ndarray.
88 """
89 return self.view(np.ndarray)
90
92 """
93 Make a copy. This means, we delete all observers and return a copy of this
94 array. It will still be an ObsAr!
95 """
96 from .lists_and_dicts import ObserverList
97 memo = {}
98 memo[id(self)] = self
99 memo[id(self.observers)] = ObserverList()
100 return self.__deepcopy__(memo)
101
108
112
116
120
122 return self.__getitem__(slice(start, stop))
123
125 return self.__setitem__(slice(start, stop), val)
126
128 r = np.ndarray.__ilshift__(self, *args, **kwargs)
129 self.notify_observers()
130 return r
131
133 r = np.ndarray.__irshift__(self, *args, **kwargs)
134 self.notify_observers()
135 return r
136
137
139 r = np.ndarray.__ixor__(self, *args, **kwargs)
140 self.notify_observers()
141 return r
142
143
145 r = np.ndarray.__ipow__(self, *args, **kwargs)
146 self.notify_observers()
147 return r
148
149
151 r = np.ndarray.__ifloordiv__(self, *args, **kwargs)
152 self.notify_observers()
153 return r
154
155
157 r = np.ndarray.__isub__(self, *args, **kwargs)
158 self.notify_observers()
159 return r
160
161
162 - def __ior__(self, *args, **kwargs):
163 r = np.ndarray.__ior__(self, *args, **kwargs)
164 self.notify_observers()
165 return r
166
167
169 r = np.ndarray.__itruediv__(self, *args, **kwargs)
170 self.notify_observers()
171 return r
172
173
175 r = np.ndarray.__idiv__(self, *args, **kwargs)
176 self.notify_observers()
177 return r
178
179
181 r = np.ndarray.__iand__(self, *args, **kwargs)
182 self.notify_observers()
183 return r
184
185
187 r = np.ndarray.__imod__(self, *args, **kwargs)
188 self.notify_observers()
189 return r
190
191
193 r = np.ndarray.__iadd__(self, *args, **kwargs)
194 self.notify_observers()
195 return r
196
197
199 r = np.ndarray.__imul__(self, *args, **kwargs)
200 self.notify_observers()
201 return r
202