-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_.py
More file actions
440 lines (350 loc) · 18.5 KB
/
array_.py
File metadata and controls
440 lines (350 loc) · 18.5 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
from __future__ import annotations
import math
import numbers
from collections.abc import Sized
from bitstring.exceptions import CreationError
from typing import Union, List, Iterable, Any, Optional, BinaryIO, overload, TextIO
from bitstring.bits import Bits, BitsType
from bitstring.bitarray_ import BitArray
from bitstring.dtypes import Dtype, dtype_register
from bitstring import utils
from bitstring.bitstring_options import Options, Colour
import copy
import array
import operator
import io
import sys
ElementType = Union[float, str, int, bytes, bool, Bits]
options = Options()
class Array:
"""Return an Array whose elements are initialised according to the fmt string.
The dtype string can be typecode as used in the struct module or any fixed-length bitstring
format.
a = Array('>H', [1, 15, 105])
b = Array('int5', [-9, 0, 4])
The Array data is stored compactly as a BitArray object and the Array behaves very like
a list of items of the given format. Both the Array data and fmt properties can be freely
modified after creation. If the data length is not a multiple of the fmt length then the
Array will have 'trailing_bits' which will prevent some methods from appending to the
Array.
Methods:
append() -- Append a single item to the end of the Array.
byteswap() -- Change byte endianness of all items.
count() -- Count the number of occurences of a value.
extend() -- Append new items to the end of the Array from an iterable.
fromfile() -- Append items read from a file object.
insert() -- Insert an item at a given position.
pop() -- Remove and return an item.
pp() -- Pretty print the Array.
reverse() -- Reverse the order of all items.
tobytes() -- Return Array data as bytes object, padding with zero bits at the end if needed.
tofile() -- Write Array data to a file, padding with zero bits at the end if needed.
tolist() -- Return Array items as a list.
Special methods:
Also available are the operators [], ==, !=, +, *, <<, >>, &, |, ^,
plus the mutating operators [], +=, *=, <<=, >>=, &=, |=, ^=.
Properties:
data -- The BitArray binary data of the Array. Can be freely modified.
dtype -- The format string or typecode. Can be freely modified.
itemsize -- The length *in bits* of a single item. Read only.
trailing_bits -- If the data length is not a multiple of the fmt length, this BitArray
gives the leftovers at the end of the data.
"""
def __init__(self, dtype: Union[str, Dtype], initializer: Optional[Union[int, Array, array.array, Iterable, Bits, bytes, bytearray, memoryview, BinaryIO]]=None, trailing_bits: Optional[BitsType]=None) -> None:
self.data = BitArray()
if isinstance(dtype, Dtype) and dtype.scale == 'auto':
if isinstance(initializer, (int, Bits, bytes, bytearray, memoryview, BinaryIO)):
raise TypeError("An Array with an 'auto' scale factor can only be created from an iterable of values.")
auto_scale = self._calculate_auto_scale(initializer, dtype.name, dtype.length)
dtype = Dtype(dtype.name, dtype.length, scale=auto_scale)
try:
self._set_dtype(dtype)
except ValueError as e:
raise CreationError(e)
if isinstance(initializer, numbers.Integral):
self.data = BitArray(initializer * self._dtype.bitlength)
elif isinstance(initializer, (Bits, bytes, bytearray, memoryview)):
self.data += initializer
elif isinstance(initializer, io.BufferedReader):
self.fromfile(initializer)
elif initializer is not None:
self.extend(initializer)
if trailing_bits is not None:
self.data += BitArray._create_from_bitstype(trailing_bits)
_largest_values = None
def _create_element(self, value: ElementType) -> Bits:
"""Create Bits from value according to the token_name and token_length"""
pass
def __len__(self) -> int:
return len(self.data) // self._dtype.length
@overload
def __getitem__(self, key: slice) -> Array:
...
@overload
def __getitem__(self, key: int) -> ElementType:
...
def __getitem__(self, key: Union[slice, int]) -> Union[Array, ElementType]:
if isinstance(key, slice):
start, stop, step = key.indices(len(self))
if step != 1:
d = BitArray()
for s in range(start * self._dtype.length, stop * self._dtype.length, step * self._dtype.length):
d.append(self.data[s:s + self._dtype.length])
a = self.__class__(self._dtype)
a.data = d
return a
else:
a = self.__class__(self._dtype)
a.data = self.data[start * self._dtype.length:stop * self._dtype.length]
return a
else:
if key < 0:
key += len(self)
if key < 0 or key >= len(self):
raise IndexError(f'Index {key} out of range for Array of length {len(self)}.')
return self._dtype.read_fn(self.data, start=self._dtype.length * key)
@overload
def __setitem__(self, key: slice, value: Iterable[ElementType]) -> None:
...
@overload
def __setitem__(self, key: int, value: ElementType) -> None:
...
def __setitem__(self, key: Union[slice, int], value: Union[Iterable[ElementType], ElementType]) -> None:
if isinstance(key, slice):
start, stop, step = key.indices(len(self))
if not isinstance(value, Iterable):
raise TypeError('Can only assign an iterable to a slice.')
if step == 1:
new_data = BitArray()
for x in value:
new_data += self._create_element(x)
self.data[start * self._dtype.length:stop * self._dtype.length] = new_data
return
items_in_slice = len(range(start, stop, step))
if not isinstance(value, Sized):
value = list(value)
if len(value) == items_in_slice:
for s, v in zip(range(start, stop, step), value):
self.data.overwrite(self._create_element(v), s * self._dtype.length)
else:
raise ValueError(f"Can't assign {len(value)} values to an extended slice of length {items_in_slice}.")
else:
if key < 0:
key += len(self)
if key < 0 or key >= len(self):
raise IndexError(f'Index {key} out of range for Array of length {len(self)}.')
start = self._dtype.length * key
self.data.overwrite(self._create_element(value), start)
return
def __delitem__(self, key: Union[slice, int]) -> None:
if isinstance(key, slice):
start, stop, step = key.indices(len(self))
if step == 1:
self.data.__delitem__(slice(start * self._dtype.length, stop * self._dtype.length))
return
r = reversed(range(start, stop, step)) if step > 0 else range(start, stop, step)
for s in r:
self.data.__delitem__(slice(s * self._dtype.length, (s + 1) * self._dtype.length))
else:
if key < 0:
key += len(self)
if key < 0 or key >= len(self):
raise IndexError
start = self._dtype.length * key
del self.data[start:start + self._dtype.length]
def __repr__(self) -> str:
list_str = f'{self.tolist()}'
trailing_bit_length = len(self.data) % self._dtype.length
final_str = '' if trailing_bit_length == 0 else ', trailing_bits=' + repr(self.data[-trailing_bit_length:])
return f"Array('{self._dtype}', {list_str}{final_str})"
def astype(self, dtype: Union[str, Dtype]) -> Array:
"""Return Array with elements of new dtype, initialised from current Array."""
pass
def insert(self, i: int, x: ElementType) -> None:
"""Insert a new element into the Array at position i.
"""
pass
def pop(self, i: int=-1) -> ElementType:
"""Return and remove an element of the Array.
Default is to return and remove the final element.
"""
pass
def byteswap(self) -> None:
"""Change the endianness in-place of all items in the Array.
If the Array format is not a whole number of bytes a ValueError will be raised.
"""
pass
def count(self, value: ElementType) -> int:
"""Return count of Array items that equal value.
value -- The quantity to compare each Array element to. Type should be appropriate for the Array format.
For floating point types using a value of float('nan') will count the number of elements that are NaN.
"""
pass
def tobytes(self) -> bytes:
"""Return the Array data as a bytes object, padding with zero bits if needed.
Up to seven zero bits will be added at the end to byte align.
"""
pass
def tofile(self, f: BinaryIO) -> None:
"""Write the Array data to a file object, padding with zero bits if needed.
Up to seven zero bits will be added at the end to byte align.
"""
pass
def pp(self, fmt: Optional[str]=None, width: int=120, show_offset: bool=True, stream: TextIO=sys.stdout) -> None:
"""Pretty-print the Array contents.
fmt -- Data format string. Defaults to current Array dtype.
width -- Max width of printed lines in characters. Defaults to 120. A single group will always
be printed per line even if it exceeds the max width.
show_offset -- If True shows the element offset in the first column of each line.
stream -- A TextIO object with a write() method. Defaults to sys.stdout.
"""
pass
def equals(self, other: Any) -> bool:
"""Return True if format and all Array items are equal."""
pass
def __iter__(self) -> Iterable[ElementType]:
start = 0
for _ in range(len(self)):
yield self._dtype.read_fn(self.data, start=start)
start += self._dtype.length
def __copy__(self) -> Array:
a_copy = self.__class__(self._dtype)
a_copy.data = copy.copy(self.data)
return a_copy
def _apply_op_to_all_elements(self, op, value: Union[int, float, None], is_comparison: bool=False) -> Array:
"""Apply op with value to each element of the Array and return a new Array"""
pass
def _apply_op_to_all_elements_inplace(self, op, value: Union[int, float]) -> Array:
"""Apply op with value to each element of the Array in place."""
pass
def _apply_bitwise_op_to_all_elements(self, op, value: BitsType) -> Array:
"""Apply op with value to each element of the Array as an unsigned integer and return a new Array"""
pass
def _apply_bitwise_op_to_all_elements_inplace(self, op, value: BitsType) -> Array:
"""Apply op with value to each element of the Array as an unsigned integer in place."""
pass
@classmethod
def _promotetype(cls, type1: Dtype, type2: Dtype) -> Dtype:
"""When combining types which one wins?
1. We only deal with types representing floats or integers.
2. One of the two types gets returned. We never create a new one.
3. Floating point types always win against integer types.
4. Signed integer types always win against unsigned integer types.
5. Longer types win against shorter types.
6. In a tie the first type wins against the second type.
"""
pass
def __add__(self, other: Union[int, float, Array]) -> Array:
"""Add int or float to all elements."""
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.add, other)
return self._apply_op_to_all_elements(operator.add, other)
def __iadd__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.add, other)
return self._apply_op_to_all_elements_inplace(operator.add, other)
def __isub__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.sub, other)
return self._apply_op_to_all_elements_inplace(operator.sub, other)
def __sub__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.sub, other)
return self._apply_op_to_all_elements(operator.sub, other)
def __mul__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.mul, other)
return self._apply_op_to_all_elements(operator.mul, other)
def __imul__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.mul, other)
return self._apply_op_to_all_elements_inplace(operator.mul, other)
def __floordiv__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.floordiv, other)
return self._apply_op_to_all_elements(operator.floordiv, other)
def __ifloordiv__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.floordiv, other)
return self._apply_op_to_all_elements_inplace(operator.floordiv, other)
def __truediv__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.truediv, other)
return self._apply_op_to_all_elements(operator.truediv, other)
def __itruediv__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.truediv, other)
return self._apply_op_to_all_elements_inplace(operator.truediv, other)
def __rshift__(self, other: Union[int, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.rshift, other)
return self._apply_op_to_all_elements(operator.rshift, other)
def __lshift__(self, other: Union[int, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.lshift, other)
return self._apply_op_to_all_elements(operator.lshift, other)
def __irshift__(self, other: Union[int, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.rshift, other)
return self._apply_op_to_all_elements_inplace(operator.rshift, other)
def __ilshift__(self, other: Union[int, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.lshift, other)
return self._apply_op_to_all_elements_inplace(operator.lshift, other)
def __mod__(self, other: Union[int, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.mod, other)
return self._apply_op_to_all_elements(operator.mod, other)
def __imod__(self, other: Union[int, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.mod, other)
return self._apply_op_to_all_elements_inplace(operator.mod, other)
def __and__(self, other: BitsType) -> Array:
return self._apply_bitwise_op_to_all_elements(operator.iand, other)
def __iand__(self, other: BitsType) -> Array:
return self._apply_bitwise_op_to_all_elements_inplace(operator.iand, other)
def __or__(self, other: BitsType) -> Array:
return self._apply_bitwise_op_to_all_elements(operator.ior, other)
def __ior__(self, other: BitsType) -> Array:
return self._apply_bitwise_op_to_all_elements_inplace(operator.ior, other)
def __xor__(self, other: BitsType) -> Array:
return self._apply_bitwise_op_to_all_elements(operator.ixor, other)
def __ixor__(self, other: BitsType) -> Array:
return self._apply_bitwise_op_to_all_elements_inplace(operator.ixor, other)
def __rmul__(self, other: Union[int, float]) -> Array:
return self._apply_op_to_all_elements(operator.mul, other)
def __radd__(self, other: Union[int, float]) -> Array:
return self._apply_op_to_all_elements(operator.add, other)
def __rsub__(self, other: Union[int, float]) -> Array:
neg = self._apply_op_to_all_elements(operator.neg, None)
return neg._apply_op_to_all_elements(operator.add, other)
def __rand__(self, other: BitsType) -> Array:
return self._apply_bitwise_op_to_all_elements(operator.iand, other)
def __ror__(self, other: BitsType) -> Array:
return self._apply_bitwise_op_to_all_elements(operator.ior, other)
def __rxor__(self, other: BitsType) -> Array:
return self._apply_bitwise_op_to_all_elements(operator.ixor, other)
def __lt__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.lt, other, is_comparison=True)
return self._apply_op_to_all_elements(operator.lt, other, is_comparison=True)
def __gt__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.gt, other, is_comparison=True)
return self._apply_op_to_all_elements(operator.gt, other, is_comparison=True)
def __ge__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.ge, other, is_comparison=True)
return self._apply_op_to_all_elements(operator.ge, other, is_comparison=True)
def __le__(self, other: Union[int, float, Array]) -> Array:
if isinstance(other, Array):
return self._apply_op_between_arrays(operator.le, other, is_comparison=True)
return self._apply_op_to_all_elements(operator.le, other, is_comparison=True)
def __eq__(self, other: Any) -> Array:
return self._eq_ne(operator.eq, other)
def __ne__(self, other: Any) -> Array:
return self._eq_ne(operator.ne, other)
def __neg__(self):
return self._apply_op_to_all_elements(operator.neg, None)
def __abs__(self):
return self._apply_op_to_all_elements(operator.abs, None)