This code demonstrates how to serialize and deserialize an ArrayXf object to/from a file.
This code demonstrates how to serialize and deserialize an ArrayXf object to/from a file. The code creates a 2D array of size 4x2, fills it with float values and writes it to a file. It then reads the data back and prints the content of the array.
1import iolink
2
3import tempfile
4import numpy as np
5
6from array import array
7
8
13 tmpFilePath = "{}/serialization_array_python.txt".format(tempfile.gettempdir())
14
15 shape = iolink.VectorXu64(4, 2)
16 arr = iolink.ArrayXf(shape)
17
18
19 arr.set_at((0, 0), 0.0)
20 arr.set_at((1, 0), 1.0)
21 arr.set_at((2, 0), 2.0)
22 arr.set_at((3, 0), 3.0)
23 arr.set_at((0, 1), 10.0)
24 arr.set_at((1, 1), 11.0)
25 arr.set_at((2, 1), 12.0)
26 arr.set_at((3, 1), 13.0)
27
28
29 dst = iolink.StreamAccessFactory.open_file_write(tmpFilePath)
30
31
32 iolink.Serialization.encode_arrayxf(arr, dst)
33
34
35 dst.flush()
36
37
38 src = iolink.StreamAccessFactory.open_file_read(tmpFilePath)
39
40
41 arrayRead = iolink.Serialization.decode_arrayxf(src)
42
43
44 print("Content of unserialized array", str(arrayRead))
45
Definition serialize_array.py:1