Skip to content

Commit a28d591

Browse files
committed
working implementation of python array
1 parent 2e8c37e commit a28d591

3 files changed

Lines changed: 23 additions & 17 deletions

File tree

wolframclient/language/array.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import absolute_import, print_function, unicode_literals
22

33
from wolframclient.serializers.wxfencoder import constants
4-
from wolframclient.utils import six
4+
from wolframclient.utils.encoding import concatenate_bytes
55

66
try:
77
from collections.abc import Sequence
@@ -14,14 +14,11 @@ def __init__(self, array, type, shape=None):
1414

1515
self.array = array
1616
self.shape = shape or (len(array),)
17-
18-
if isinstance(type, six.string_types):
19-
self.type = constants.ARRAY_TYPES[type]
20-
else:
21-
self.type = type
17+
self.type = type
18+
self.struct = constants.STRUCT_MAPPING[type]
2219

2320
def tobytes(self):
24-
raise NotImplementedError("aaa")
21+
return concatenate_bytes(self.struct.pack(el) for el in self.array)
2522

2623
def __getitem__(self, k):
2724
return self.array[k]

wolframclient/serializers/encoders/builtin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import math
44

5+
from wolframclient.language.array import PythonArray
56
from wolframclient.language.expression import WLFunction, WLInputExpression, WLSymbol
67
from wolframclient.serializers.serializable import WLSerializable
78
from wolframclient.serializers.utils import safe_len
@@ -173,3 +174,8 @@ def encode_serializable(serializer, o):
173174
@encoder.dispatch(Association)
174175
def encode_association(serializer, o):
175176
return _to_key_value(serializer.serialize_association, serializer, o)
177+
178+
179+
@encoder.dispatch(PythonArray)
180+
def encode_array(serializer, o):
181+
return serializer.serialize_numeric_array(o.tobytes(), o.shape, o.type)

wolframclient/tests/serializers/wxf_pythonarray.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22

33
from __future__ import absolute_import, print_function, unicode_literals
44

5-
from functools import partial
6-
5+
from wolframclient.language.array import PythonArray
76
from wolframclient.serializers import export
87
from wolframclient.utils.api import numpy
9-
from wolframclient.utils.pythonarray import PythonArray
108
from wolframclient.utils.tests import TestCase as BaseTestCase
119

1210

1311
class TestCase(BaseTestCase):
14-
15-
export = partial(export, target_format="wl")
16-
1712
def test_python_array(self):
1813

19-
for array, numpy_type, wl_type in (([1, 2, 3], numpy.uint64, "Integer64"),):
20-
21-
print(self.export(numpy.array(array, numpy_type)))
22-
print(self.export(PythonArray(array, wl_type)))
14+
for array, numpy_type, wl_type in (
15+
([1, 2, 3], numpy.int8, "Integer8"),
16+
([1, 2, 3], numpy.int32, "Integer32"),
17+
([1, 2, 3], numpy.int64, "Integer64"),
18+
([1.2, 2.3, 3], numpy.float32, "Real32"),
19+
([1.2, 2.3, 3], numpy.float64, "Real64"),
20+
):
21+
22+
self.assertEqual(
23+
export(numpy.array(array, numpy_type), target_format="wxf"),
24+
export(PythonArray(array, wl_type), target_format="wxf"),
25+
)

0 commit comments

Comments
 (0)