Skip to content

Commit ba8fb65

Browse files
committed
adding STRUCT_MAPPING constant
1 parent b21fa54 commit ba8fb65

3 files changed

Lines changed: 32 additions & 59 deletions

File tree

wolframclient/deserializers/wxf/wxfparser.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,27 +141,27 @@ def token_for_string(self, token):
141141

142142
def token_for_integer8(self, token):
143143
self.context.add_part()
144-
token.data = constants.StructInt8LE.unpack(self.reader.read(1))[0]
144+
token.data = constants.STRUCT_MAPPING.Integer8.unpack(self.reader.read(1))[0]
145145
return token
146146

147147
def token_for_integer16(self, token):
148148
self.context.add_part()
149-
token.data = constants.StructInt16LE.unpack(self.reader.read(2))[0]
149+
token.data = constants.STRUCT_MAPPING.Integer16.unpack(self.reader.read(2))[0]
150150
return token
151151

152152
def token_for_integer32(self, token):
153153
self.context.add_part()
154-
token.data = constants.StructInt32LE.unpack(self.reader.read(4))[0]
154+
token.data = constants.STRUCT_MAPPING.Integer32.unpack(self.reader.read(4))[0]
155155
return token
156156

157157
def token_for_integer64(self, token):
158158
self.context.add_part()
159-
token.data = constants.StructInt64LE.unpack(self.reader.read(8))[0]
159+
token.data = constants.STRUCT_MAPPING.Integer64.unpack(self.reader.read(8))[0]
160160
return token
161161

162162
def token_for_real64(self, token):
163163
self.context.add_part()
164-
token.data = constants.StructDouble.unpack(self.reader.read(8))[0]
164+
token.data = constants.STRUCT_MAPPING.Real64.unpack(self.reader.read(8))[0]
165165
return token
166166

167167
def token_for_function(self, token):

wolframclient/serializers/wxfencoder/constants.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,17 @@ def _bytes(value):
9191
)
9292
)
9393

94-
StructInt8LE = struct.Struct(b"<b")
95-
StructUInt8LE = struct.Struct(b"<B")
96-
StructInt16LE = struct.Struct(b"<h")
97-
StructUInt16LE = struct.Struct(b"<H")
98-
StructInt32LE = struct.Struct(b"<i")
99-
StructUInt32LE = struct.Struct(b"<I")
100-
StructInt64LE = struct.Struct(b"<q")
101-
StructUInt64LE = struct.Struct(b"<Q")
102-
StructFloat = struct.Struct(b"<f")
103-
StructDouble = struct.Struct(b"<d")
94+
STRUCT_MAPPING = Settings(
95+
Integer8=struct.Struct(b"<b"),
96+
UnsignedInteger8=struct.Struct(b"<B"),
97+
Integer16=struct.Struct(b"<h"),
98+
UnsignedInteger16=struct.Struct(b"<H"),
99+
Integer32=struct.Struct(b"<i"),
100+
UnsignedInteger32=struct.Struct(b"<I"),
101+
Integer64=struct.Struct(b"<q"),
102+
UnsignedInteger64=struct.Struct(b"<Q"),
103+
Real32=struct.Struct(b"<f"),
104+
Real64=struct.Struct(b"<d"),
105+
ComplexReal32=struct.Struct(b"<f"),
106+
ComplexReal64=struct.Struct(b"<d"),
107+
)

wolframclient/serializers/wxfencoder/utils.py

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,9 @@
33
from wolframclient.exception import WolframLanguageException
44
from 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
)
1910
from wolframclient.utils import six
2011
from 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

7672
if six.JYTHON:
7773

@@ -97,17 +93,17 @@ def integer_to_bytes(value, int_size):
9793

9894
if 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

106102
else:
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

148144
if 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

190173
else:
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

Comments
 (0)