33from wolframclient .exception import WolframLanguageException
44from wolframclient .serializers .wxfencoder .constants import (
55 ARRAY_TYPES ,
6+ STRUCT_MAPPING ,
67 VALID_PACKED_ARRAY_TYPES ,
78 WXF_CONSTANTS ,
8- StructDouble ,
9- StructFloat ,
10- StructInt8LE ,
11- StructInt16LE ,
12- StructInt32LE ,
13- StructInt64LE ,
14- StructUInt8LE ,
15- StructUInt16LE ,
16- StructUInt32LE ,
17- StructUInt64LE ,
189)
1910from wolframclient .utils import six
2011from wolframclient .utils .datastructures import Settings
@@ -71,7 +62,12 @@ def integer_size(value):
7162 raise ValueError ("Value %i is not a machine-sized integer." % value )
7263
7364
74- _packing = {1 : StructInt8LE , 2 : StructInt16LE , 4 : StructInt32LE , 8 : StructInt64LE }
65+ _packing = {
66+ 1 : STRUCT_MAPPING .Integer8 ,
67+ 2 : STRUCT_MAPPING .Integer16 ,
68+ 4 : STRUCT_MAPPING .Integer32 ,
69+ 8 : STRUCT_MAPPING .Integer64 ,
70+ }
7571
7672if six .JYTHON :
7773
@@ -97,17 +93,17 @@ def integer_to_bytes(value, int_size):
9793
9894if six .JYTHON :
9995
100- def float_to_bytes (value ):
96+ def float_to_bytes (value , pack_into = STRUCT_MAPPING . Real64 . pack_into ):
10197 buffer = jarray .zeros (8 , "c" )
102- StructDouble . pack_into (buffer , 0 , value )
98+ pack_into (buffer , 0 , value )
10399 return buffer .tostring ()
104100
105101
106102else :
107103
108- def float_to_bytes (value ):
104+ def float_to_bytes (value , pack_into = STRUCT_MAPPING . Real64 . pack_into ):
109105 buffer = bytearray (8 )
110- StructDouble . pack_into (buffer , 0 , value )
106+ pack_into (buffer , 0 , value )
111107 return buffer
112108
113109
@@ -146,20 +142,7 @@ def array_to_list(data, dimensions, wl_type):
146142
147143
148144if hasattr (memoryview , "cast" ):
149- unpack_mapping = Settings (
150- Integer8 = "b" ,
151- UnsignedInteger8 = "B" ,
152- Integer16 = "h" ,
153- UnsignedInteger16 = "H" ,
154- Integer32 = "i" ,
155- UnsignedInteger32 = "I" ,
156- Integer64 = "q" ,
157- UnsignedInteger64 = "Q" ,
158- Real32 = "f" ,
159- Real64 = "d" ,
160- ComplexReal32 = "f" ,
161- ComplexReal64 = "d" ,
162- )
145+ unpack_mapping = Settings ((k , v .format [1 :]) for k , v in STRUCT_MAPPING .items ())
163146
164147 def _to_complex (array , max_depth , curr_depth ):
165148 # recursivelly traverse the array until the last (real) dimension is reached
@@ -188,20 +171,6 @@ def _array_to_list(data, shape, array_type):
188171
189172
190173else :
191- unpack_mapping = Settings (
192- Integer8 = StructInt8LE ,
193- UnsignedInteger8 = StructUInt8LE ,
194- Integer16 = StructInt16LE ,
195- UnsignedInteger16 = StructUInt16LE ,
196- Integer32 = StructInt32LE ,
197- UnsignedInteger32 = StructUInt32LE ,
198- Integer64 = StructInt64LE ,
199- UnsignedInteger64 = StructUInt64LE ,
200- Real32 = StructFloat ,
201- Real64 = StructDouble ,
202- ComplexReal32 = StructFloat ,
203- ComplexReal64 = StructDouble ,
204- )
205174
206175 def _array_to_list (data , shape , array_type ):
207176 value , _ = _build_array_from_bytes (data , 0 , array_type , shape , 0 )
@@ -216,7 +185,7 @@ def _build_array_from_bytes(data, offset, array_type, dimensions, current_dim):
216185 )
217186 new_array .append (new_elem )
218187 else :
219- struct = unpack_mapping [array_type ]
188+ struct = STRUCT_MAPPING [array_type ]
220189 # complex values, need two reals for each.
221190 if array_type == "ComplexReal32" or array_type == "ComplexReal64" :
222191 for i in range (dimensions [- 1 ]):
0 commit comments