-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
support_types.py
895 lines (700 loc) · 24.3 KB
/
support_types.py
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
"""
The MIT License (MIT)
Copyright (c) [2015-2017] [Andrew Annex]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
The MIT License (MIT)
Copyright (c) 2013 Philipp Rasch
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from ctypes import c_char_p, c_bool, c_int, c_double,\
c_char, c_void_p, sizeof, \
Array, create_string_buffer, cast, Structure, \
string_at
import numpy
import six
from numpy import ctypeslib as numpc
# Collection of supporting functions for wrapper functions
__author__ = 'AndrewAnnex'
errorformat = """
================================================================================
Toolkit version: {tkvsn}
{short} --
{explain}
{long}
{traceback}
================================================================================\
"""
class SpiceyError(Exception):
"""
SpiceyError wraps CSPICE errors.
:type value: str
"""
def __init__(self, value, found=None):
self.value = value
self.found = found
def __str__(self):
return self.value
def toDoubleVector(x):
return DoubleArray.from_param(param=x)
def toDoubleMatrix(x):
return DoubleMatrix.from_param(param=x)
def toIntVector(x):
return IntArray.from_param(param=x)
def toIntMatrix(x):
return IntMatrix.from_param(param=x)
def toBoolVector(x):
return BoolArray.from_param(param=x)
def toPythonString(inString):
if six.PY2:
if isinstance(inString, c_char_p):
return toPythonString(inString.value)
return string_at(inString).rstrip()
elif six.PY3:
if isinstance(inString, c_char_p):
return toPythonString(inString.value)
return bytes.decode(string_at(inString), errors="ignore").rstrip()
def emptyCharArray(xLen=None, yLen=None):
if not yLen:
yLen = 1
if not xLen:
xLen = 1
if isinstance(xLen, c_int):
xLen = xLen.value
if isinstance(yLen, c_int):
yLen = yLen.value
return ((c_char * xLen) * yLen)()
def emptyDoubleMatrix(x=3, y=3):
if isinstance(x, c_int):
x = x.value
if isinstance(y, c_int):
y = y.value
return ((c_double * x) * y)()
def emptyDoubleVector(n):
if isinstance(n, c_int):
n = n.value
assert(isinstance(n, int))
return (c_double * n)()
def emptyIntMatrix(x=3, y=3):
if isinstance(x, c_int):
x = x.value
if isinstance(y, c_int):
y = y.value
return ((c_int * x) * y)()
def emptyIntVector(n):
if isinstance(n, c_int):
n = n.value
assert (isinstance(n, int))
return (c_int * n)()
def cVectorToPython(x):
"""
Convert the c vector data into the correct python data type
(numpy arrays or strings)
:param x:
:return:
"""
if isinstance(x[0], bool):
return numpy.frombuffer(x, dtype=numpy.bool).copy()
elif isinstance(x[0], int):
return numpy.frombuffer(x, dtype=numpy.int32).copy()
elif isinstance(x[0], float):
return numpy.frombuffer(x, dtype=numpy.float64).copy()
elif isinstance(x[0].value, bytes):
return [toPythonString(y) for y in x]
def cIntVectorToBoolPython(x):
return numpc.as_array(x).astype(bool)
def cMatrixToNumpy(x):
"""
Convert a ctypes 2d array (or matrix) into a numpy array for python use
:param x: thing to convert
:return: numpy.ndarray
"""
return numpc.as_array(x)
def stringToCharP(inobject, inlen=None):
"""
:param inobject: input string, int for getting null string of length of int
:param inlen: optional parameter, length of a given string can be specified
:return:
"""
if inlen and isinstance(inobject, str):
return create_string_buffer(inobject.encode(encoding='UTF-8'), inlen)
if isinstance(inobject, bytes):
return inobject
if isinstance(inobject, c_int):
return stringToCharP(" " * inobject.value)
if isinstance(inobject, int):
return stringToCharP(" " * inobject)
return c_char_p(inobject.encode(encoding='UTF-8'))
def listToCharArray(inList, xLen=None, yLen=None):
assert (isinstance(inList, list))
if not yLen:
yLen = len(inList)
if not xLen:
xLen = max(len(s) for s in inList) + 1
if isinstance(xLen, c_int):
xLen = xLen.value
if isinstance(yLen, c_int):
yLen = yLen.value
return ((c_char * xLen) * yLen)(*[stringToCharP(l, inlen=xLen) for l in inList])
def listToCharArrayPtr(inList, xLen=None, yLen=None):
assert (isinstance(inList, list))
if not yLen:
yLen = len(inList)
if not xLen:
xLen = max(len(s) for s in inList) + 1
if isinstance(xLen, c_int):
xLen = xLen.value
if isinstance(yLen, c_int):
yLen = yLen.value
return cast(((c_char * xLen) * yLen)(*[stringToCharP(l, inlen=xLen) for l in inList]), c_char_p)
class DoubleArrayType:
# Class type that will handle all double vectors,
# inspiration from python cookbook 3rd edition
def from_param(self, param):
typename = type(param).__name__
if hasattr(self, 'from_' + typename):
return getattr(self, 'from_' + typename)(param)
elif isinstance(param, Array):
return param
else:
raise TypeError("Can't convert %s" % typename)
# Cast from lists/tuples
def from_list(self, param):
val = ((c_double) * len(param))(*param)
return val
# Cast from Tuple
def from_tuple(self, param):
val = ((c_double) * len(param))(*param)
return val
# Cast from a numpy array,
def from_ndarray(self, param):
# return param.data_as(POINTER(c_double))
# the above older method does not work with
# functions which take vectors of known size
return numpc.as_ctypes(param)
# Cast from array.array objects
def from_array(self, param):
if param.typecode != 'd':
raise TypeError('must be an array of doubles')
return self.from_list(param)
class DoubleMatrixType:
# Class type that will handle all double matricies,
# inspiration from python cookbook 3rd edition
def from_param(self, param):
typename = type(param).__name__
if hasattr(self, 'from_' + typename):
return getattr(self, 'from_' + typename)(param)
elif isinstance(param, Array):
return param
else:
raise TypeError("Can't convert %s" % typename)
# Cast from lists/tuples
def from_list(self, param):
val = ((c_double * len(param[0])) * len(param))(*[DoubleArray.from_param(x) for x in param])
return val
# Cast from Tuple
def from_tuple(self, param):
val = ((c_double * len(param[0])) * len(param))(*[DoubleArray.from_param(x) for x in param])
return val
# Cast from a numpy array
def from_ndarray(self, param):
return numpc.as_ctypes(param)
# Cast from a numpy matrix
def from_matrix(self, param):
return numpc.as_ctypes(param)
class IntArrayType:
# Class type that will handle all int vectors,
# inspiration from python cookbook 3rd edition
def from_param(self, param):
typename = type(param).__name__
if hasattr(self, 'from_' + typename):
return getattr(self, 'from_' + typename)(param)
elif isinstance(param, Array):
return param
else:
raise TypeError("Can't convert %s" % typename)
# Cast from lists/tuples
def from_list(self, param):
val = ((c_int) * len(param))(*param)
return val
# Cast from Tuple
def from_tuple(self, param):
val = ((c_int) * len(param))(*param)
return val
# Cast from a numpy array
def from_ndarray(self, param):
# cspice always uses a int size half as big as the float, ie int32 if a float64 system default
return numpc.as_ctypes(param.astype(numpy.int32))
# Cast from array.array objects
def from_array(self, param):
if param.typecode != 'i':
raise TypeError('must be an array of ints')
return self.from_list(param)
class IntMatrixType:
# Class type that will handle all int matricies,
# inspiration from python cookbook 3rd edition
def from_param(self, param):
typename = type(param).__name__
if hasattr(self, 'from_' + typename):
return getattr(self, 'from_' + typename)(param)
elif isinstance(param, Array):
return param
else:
raise TypeError("Can't convert %s" % typename)
# Cast from lists/tuples
def from_list(self, param):
val = ((c_int * len(param[0])) * len(param))(*[IntArray.from_param(x) for x in param])
return val
# Cast from Tuple
def from_tuple(self, param):
val = ((c_int * len(param[0])) * len(param))(*[IntArray.from_param(x) for x in param])
return val
# Cast from a numpy array
def from_ndarray(self, param):
# cspice always uses a int size half as big as the float, ie int32 if a float64 system default
return numpc.as_ctypes(param.astype(numpy.int32))
# Cast from a numpy matrix
def from_matrix(self, param):
# cspice always uses a int size half as big as the float, ie int32 if a float64 system default
return numpc.as_ctypes(param.astype(numpy.int32))
class BoolArrayType:
# Class type that will handle all int vectors,
# inspiration from python cookbook 3rd edition
def from_param(self, param):
typename = type(param).__name__
if hasattr(self, 'from_' + typename):
return getattr(self, 'from_' + typename)(param)
elif isinstance(param, Array):
return param
else:
raise TypeError("Can't convert %s" % typename)
# Cast from lists/tuples
def from_list(self, param):
val = ((c_bool) * len(param))(*param)
return val
# Cast from Tuple
def from_tuple(self, param):
val = ((c_bool) * len(param))(*param)
return val
# Cast from a numpy array
def from_ndarray(self, param):
# return param.data_as(POINTER(c_int))
# not sure if long is same as int, it should be..
# return numpy.ctypeslib.as_ctypes(param)
return self.from_param(param.tolist())
DoubleArray = DoubleArrayType()
IntArray = IntArrayType()
IntMatrix = IntMatrixType()
BoolArray = BoolArrayType()
DoubleMatrix = DoubleMatrixType()
class Plane(Structure):
_fields_ = [
('_normal', c_double * 3),
('_constant', c_double)
]
@property
def normal(self):
return cVectorToPython(self._normal)
@property
def constant(self):
return self._constant
def __str__(self):
return '<SpicePlane: normal=%s; constant=%s>' % (', '.join([str(x) for x in self._normal]), self._constant)
class Ellipse(Structure):
_fields_ = [
('_center', c_double * 3),
('_semi_major', c_double * 3),
('_semi_minor', c_double * 3)
]
@property
def center(self):
return cVectorToPython(self._center)
@property
def semi_major(self):
return cVectorToPython(self._semi_major)
@property
def semi_minor(self):
return cVectorToPython(self._semi_minor)
def __str__(self):
return '<SpiceEllipse: center = %s, semi_major = %s, semi_minor = %s>' % \
(self.center, self.semi_major, self.semi_minor)
class DataType(object):
SPICE_CHR = 0
SPICE_DP = 1
SPICE_INT = 2
SPICE_TIME = 3
SPICE_BOOL = 4
CHR = 0
DP = 1
INT = 2
TIME = 3
BOOL = 4
def __init__(self):
pass
class SpiceDSKDescr(Structure):
_fields_ = [
('_surfce', c_int),
('_center', c_int),
('_dclass', c_int),
('_dtype', c_int),
('_frmcde', c_int),
('_corsys', c_int),
('_corpar', c_double * 10),
('_co1min', c_double),
('_co1max', c_double),
('_co2min', c_double),
('_co2max', c_double),
('_co3min', c_double),
('_co3max', c_double),
('_start', c_double),
('_stop', c_double),
]
@property
def surfce(self):
return self._surfce
@property
def center(self):
return self._center
@property
def dclass(self):
return self._dclass
@property
def dtype(self):
return self._dtype
@property
def frmcde(self):
return self._frmcde
@property
def corsys(self):
return self._corsys
@property
def corpar(self):
return cVectorToPython(self._corpar)
@property
def co1min(self):
return self._co1min
@property
def co1max(self):
return self._co1max
@property
def co2min(self):
return self._co2min
@property
def co2max(self):
return self._co2max
@property
def co3min(self):
return self._co3min
@property
def co3max(self):
return self._co3max
@property
def start(self):
return self._start
@property
def stop(self):
return self._stop
class SpiceDLADescr(Structure):
_fields_ = [
('_bwdptr', c_int),
('_fwdptr', c_int),
('_ibase', c_int),
('_isize', c_int),
('_dbase', c_int),
('_dsize', c_int),
('_cbase', c_int),
('_csize', c_int)
]
@property
def bwdptr(self):
return self._bwdptr
@property
def fwdptr(self):
return self._fwdptr
@property
def ibase(self):
return self._ibase
@property
def isize(self):
return self._isize
@property
def dbase(self):
return self._dbase
@property
def dsize(self):
return self._dsize
@property
def cbase(self):
return self._cbase
@property
def csize(self):
return self._csize
class SpiceEKDataType(c_int):
_SPICE_CHR = c_int(0)
_SPICE_DP = c_int(1)
_SPICE_INT = c_int(2)
_SPICE_TIME = c_int(3)
_SPICE_BOOL = c_int(4)
_fields_ = [
('SPICE_CHR', _SPICE_CHR),
('SPICE_DP', _SPICE_DP),
('SPICE_INT', _SPICE_INT),
('SPICE_TIME', _SPICE_TIME),
('SPICE_BOOL', _SPICE_BOOL),
]
SPICE_CHR = _SPICE_CHR.value
SPICE_DP = _SPICE_DP.value
SPICE_INT = _SPICE_INT.value
SPICE_TIME = _SPICE_TIME.value
SPICE_BOOL = _SPICE_BOOL.value
def emptySpiceEKDataTypeVector(n):
if isinstance(n, c_int):
n = n.value
assert(isinstance(n, int))
return (SpiceEKDataType * n)()
class SpiceEKExprClass(c_int):
_SPICE_EK_EXP_COL = c_int(0)
_SPICE_EK_EXP_FUNC = c_int(1)
_SPICE_EK_EXP_EXPR = c_int(2)
_fields_ = [
('SPICE_EK_EXP_COL', _SPICE_EK_EXP_COL),
('SPICE_EK_EXP_FUNC', _SPICE_EK_EXP_FUNC),
('SPICE_EK_EXP_EXPR', _SPICE_EK_EXP_EXPR)
]
SPICE_EK_EXP_COL = _SPICE_EK_EXP_COL.value
SPICE_EK_EXP_FUNC = _SPICE_EK_EXP_FUNC.value
SPICE_EK_EXP_EXPR = _SPICE_EK_EXP_EXPR.value
class SpiceSPK18Subtype(c_int):
_S18TP0 = c_int(0)
_S18TP1 = c_int(1)
S18TP0 = _S18TP0.value
S18TP1 = _S18TP1.value
_fields_ = [
('S18TP0', _S18TP0),
('S18TP1', _S18TP1)
]
def emptySpiceEKExprClassVector(n):
if isinstance(n, c_int):
n = n.value
assert(isinstance(n, int))
return (SpiceEKExprClass * n)()
class SpiceEKAttDsc(Structure):
_fields_ = [
('_cclass', c_int),
('_dtype', SpiceEKDataType),
('_strlen', c_int),
('_size', c_int),
('_indexd', c_bool),
('_nullok', c_bool)
]
@property
def cclass(self):
return self._cclass
@property
def dtype(self):
return self._dtype.value
@property
def strlen(self):
return self._strlen
@property
def size(self):
return self._size
@property
def indexd(self):
return self._indexd
@property
def nullok(self):
return self._nullok
def __str__(self):
return '<SpiceEKAttDsc cclass = %s, dtype = %s, strlen = %s, size = %s, indexd = %s, nullok = %s >' % \
(self.cclass, self.dtype, self.strlen, self.size, self.indexd, self.nullok)
class SpiceEKSegSum(Structure):
_fields_ = [
('_tabnam', c_char * 65),
('_nrows', c_int),
('_ncols', c_int),
('_cnames', (c_char * 100) * 33),
('_cdescrs', SpiceEKAttDsc * 100)
]
@property
def tabnam(self):
return toPythonString(self._tabnam)
@property
def nrows(self):
return self._nrows
@property
def ncols(self):
return self._ncols
@property
def cnames(self):
return cVectorToPython(self._cnames)[0:self.ncols]
@property
def cdescrs(self):
return self._cdescrs[0:self.ncols]
def __str__(self):
return '<SpiceEKSegSum tabnam = %s, nrows = %s, ncols = %s, cnames = %s, cdescrs = %s >' % (self.tabnam, self.nrows, self.ncols, self.cnames, self.cdescrs)
# SpiceCell implementation below is inpart from github.com/DaRasch/spiceminer/
# and modified as needed for this author, maybe we should work together?
### helper classes/functions ###
BITSIZE = {'char': sizeof(c_char), 'int': sizeof(c_int), 'double': sizeof(c_double), 'bool': sizeof(c_int), 'time': sizeof(c_int)}
def _char_getter(data_p, index, length):
return toPythonString((c_char * length).from_address(data_p + index * length * BITSIZE['char']))
def _double_getter(data_p, index, length):
return c_double.from_address(data_p + index * BITSIZE['double']).value
def _int_getter(data_p, index, length):
return c_int.from_address(data_p + index * BITSIZE['int']).value
def SPICEDOUBLE_CELL(size):
return SpiceCell.double(size)
def SPICEINT_CELL(size):
return SpiceCell.integer(size)
def SPICECHAR_CELL(size, length):
return SpiceCell.character(size, length)
def SPICEBOOL_CELL(size):
return SpiceCell.bool(size)
def SPICETIME_CELL(size):
return SpiceCell.time(size)
class SpiceCell(Structure):
#Most written by DaRasch, see included MIT license at file header
DATATYPES_ENUM = {'char': 0, 'double': 1, 'int': 2, 'time': 3, 'bool': 4}
DATATYPES_GET = [_char_getter, _double_getter] + [_int_getter] * 3
baseSize = 6
minCharLen = 6
CTRLBLOCK = 6
_fields_ = [
('dtype', c_int),
('length', c_int),
('size', c_int),
('card', c_int),
('isSet', c_int),
('adjust', c_int),
('init', c_int),
('base', c_void_p),
('data', c_void_p)
]
def __init__(self, dtype=None, length=None, size=None, card=None, isSet=None, base=None, data=None):
super(SpiceCell, self).__init__()
self.dtype = dtype
self.length = length
self.size = size
self.card = card
self.isSet = isSet
self.adjust = 0 # Always False, because not implemented
self.init = 0 # Always False, because this is the constructor
self.base = base # void pointer
self.data = data
def __str__(self):
return '<SpiceCell dtype = %s, length = %s, size = %s, card = %s,' \
' isSet = %s, adjust = %s, init = %s, base = %s, data = %s>' % \
(self.dtype, self.length, self.size, self.card, self.isSet,
self.adjust, self.init, self.base, self.data)
def is_int(self):
return self.dtype == 2
def is_double(self):
return self.dtype == 1
def is_char(self):
return self.dtype == 0
def is_time(self):
return self.dtype == 3
def is_bool(self):
return self.dtype == 4
def is_set(self):
return self.isSet == 1
@classmethod
def character(cls, size, length):
base = (c_char * ((cls.CTRLBLOCK + size) * length))()
data = (c_char * (size * length)).from_buffer(
base, cls.CTRLBLOCK * BITSIZE['char'] * length)
instance = cls(cls.DATATYPES_ENUM['char'], length, size, 0, 1,
cast(base, c_void_p),
cast(data, c_void_p))
return instance
@classmethod
def integer(cls, size):
base = (c_int * (cls.CTRLBLOCK + size))()
data = (c_int * size).from_buffer(
base, cls.CTRLBLOCK * BITSIZE['int'])
instance = cls(cls.DATATYPES_ENUM['int'], 0, size, 0, 1,
cast(base, c_void_p),
cast(data, c_void_p))
return instance
@classmethod
def double(cls, size):
base = (c_double * (cls.CTRLBLOCK + size))()
data = (c_double * size).from_buffer(
base, cls.CTRLBLOCK * BITSIZE['double'])
instance = cls(cls.DATATYPES_ENUM['double'], 0, size, 0, 1,
cast(base, c_void_p),
cast(data, c_void_p))
return instance
@classmethod
def bool(cls, size):
base = (c_int * (cls.CTRLBLOCK + size))()
data = (c_int * size).from_buffer(
base, cls.CTRLBLOCK * BITSIZE['bool'])
instance = cls(cls.DATATYPES_ENUM['bool'], 0, size, 0, 1,
cast(base, c_void_p),
cast(data, c_void_p))
return instance
@classmethod
def time(cls, size):
base = (c_int * (cls.CTRLBLOCK + size))()
data = (c_int * size).from_buffer(
base, cls.CTRLBLOCK * BITSIZE['time'])
instance = cls(cls.DATATYPES_ENUM['time'], 0, size, 0, 1,
cast(base, c_void_p),
cast(data, c_void_p))
return instance
def __len__(self):
return self.card
def __iter__(self):
getter = SpiceCell.DATATYPES_GET[self.dtype]
length, card, data = self.length, self.card, self.data
for i in six.moves.range(card):
yield (getter(data, i, length))
def __contains__(self, key):
return key in self.__iter__()
def __getitem__(self, key):
getter = SpiceCell.DATATYPES_GET[self.dtype]
if isinstance(key, slice):
#TODO Typechecking
if self.card == 0:
return []
else:
start, stop, step = key.indices(self.card)
return [getter(self.data, i, self.length) for i in six.moves.range(start, stop, step)]
elif key in six.moves.range(-self.card, self.card):
index = key if key >= 0 else self.card - abs(key)
return getter(self.data, index, self.length)
elif not isinstance(key, int):
raise TypeError('SpiceCell indices must be integers, not {}'.format(type(key)))
else:
raise IndexError('SpiceCell index out of range')
def reset(self):
self.card = 0
self.init = 0