| IOLink Python 1.11.0

Loading...
Searching...
No Matches
serialize_array.py

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.

1 import iolink
2
3 import tempfile
4 import numpy as np
5
6 from array import array
7
8
12 def serialize_array():
13 tmpFilePath = "{}/serialization_array_python.txt".format(tempfile.gettempdir())
14
15 shape = iolink.VectorXu64(4, 2)
16 arr = iolink.ArrayXf(shape)
17
18 # fill the array with some values, cell by cell
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 # open a streamAccess to serialize the array to a file
29 dst = iolink.StreamAccessFactory.open_file_write(tmpFilePath)
30
31 # array is serialized to the file
32 iolink.Serialization.encode_arrayxf(arr, dst)
33
34 # flush the data to the file
35 dst.flush()
36
37 # open another streamAccess to read the serialized array from the file
38 src = iolink.StreamAccessFactory.open_file_read(tmpFilePath)
39
40 # array is deserialized from the file
41 arrayRead = iolink.Serialization.decode_arrayxf(src)
42
43 # print the content of the array
44 print("Content of unserialized array", str(arrayRead))
45
46 serialize_array()
serialize_array
Definition serialize_array.py:1