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
12+ from wolframclient .utils .functional import last
2113
2214if six .JYTHON :
2315 import jarray
@@ -71,21 +63,26 @@ def integer_size(value):
7163 raise ValueError ("Value %i is not a machine-sized integer." % value )
7264
7365
74- _packing = {1 : StructInt8LE , 2 : StructInt16LE , 4 : StructInt32LE , 8 : StructInt64LE }
66+ _packing = {
67+ 1 : STRUCT_MAPPING .Integer8 ,
68+ 2 : STRUCT_MAPPING .Integer16 ,
69+ 4 : STRUCT_MAPPING .Integer32 ,
70+ 8 : STRUCT_MAPPING .Integer64 ,
71+ }
7572
7673if six .JYTHON :
7774
7875 def integer_to_bytes (value , int_size ):
7976 buffer = jarray .zeros (8 , "c" )
80- _packing . get ( int_size ) .pack_into (buffer , 0 , value )
77+ _packing [ int_size ] .pack_into (buffer , 0 , value )
8178 return buffer [:int_size ].tostring ()
8279
8380
8481elif six .PY2 :
8582
8683 def integer_to_bytes (value , int_size ):
8784 buffer = bytearray (8 )
88- _packing . get ( int_size ) .pack_into (buffer , 0 , value )
85+ _packing [ int_size ] .pack_into (buffer , 0 , value )
8986 return buffer [:int_size ]
9087
9188
@@ -97,17 +94,17 @@ def integer_to_bytes(value, int_size):
9794
9895if six .JYTHON :
9996
100- def float_to_bytes (value ):
97+ def float_to_bytes (value , pack_into = STRUCT_MAPPING . Real64 . pack_into ):
10198 buffer = jarray .zeros (8 , "c" )
102- StructDouble . pack_into (buffer , 0 , value )
99+ pack_into (buffer , 0 , value )
103100 return buffer .tostring ()
104101
105102
106103else :
107104
108- def float_to_bytes (value ):
105+ def float_to_bytes (value , pack_into = STRUCT_MAPPING . Real64 . pack_into ):
109106 buffer = bytearray (8 )
110- StructDouble . pack_into (buffer , 0 , value )
107+ pack_into (buffer , 0 , value )
111108 return buffer
112109
113110
@@ -146,20 +143,7 @@ def array_to_list(data, dimensions, wl_type):
146143
147144
148145if 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- )
146+ unpack_mapping = Settings ((k , last (v .format )) for k , v in STRUCT_MAPPING .items ())
163147
164148 def _to_complex (array , max_depth , curr_depth ):
165149 # recursivelly traverse the array until the last (real) dimension is reached
@@ -188,20 +172,6 @@ def _array_to_list(data, shape, array_type):
188172
189173
190174else :
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- )
205175
206176 def _array_to_list (data , shape , array_type ):
207177 value , _ = _build_array_from_bytes (data , 0 , array_type , shape , 0 )
@@ -216,7 +186,7 @@ def _build_array_from_bytes(data, offset, array_type, dimensions, current_dim):
216186 )
217187 new_array .append (new_elem )
218188 else :
219- struct = unpack_mapping [array_type ]
189+ struct = STRUCT_MAPPING [array_type ]
220190 # complex values, need two reals for each.
221191 if array_type == "ComplexReal32" or array_type == "ComplexReal64" :
222192 for i in range (dimensions [- 1 ]):
0 commit comments