1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
| # app.py
import struct as st
# D - data, I - instruciya.
(Dmatrix2D, Istop)=range(2)
def make_bin_string(string:str)->tuple:
return (*map(ord, string), 0)
def make_string_from_bin(inputt, pb):
strr=""
pb_=pb
lenn=0
byte=inputt[pb_]
while byte!=0:
strr+=chr(byte)
pb_+=1
byte=inputt[pb_]
lenn+=1
return strr, lenn + 1 # s nolom.
def dump_matrix2D(matrix2D_list)->tuple: # <rows> <cols> <kol-vo elem-ov flatten kak float32> <elems>
H=len(matrix2D_list)
W=len(matrix2D_list[0])
N=W * H # kol-vo elementov v matrice.
int_length=4
float_length=4
out_vector=[0] * (N * float_length) # 4 baita dlya float 32.
N_bin=st.pack('<i', N) # na mladshih adresah starshie baiti.
out_vector_=[0] * (int_length + 2)
out_vector_[0]=H
out_vector_[1]=W
out_vector_[2]=N_bin[0]
out_vector_[3]=N_bin[1]
out_vector_[4]=N_bin[2]
out_vector_[5]=N_bin[3]
k=0
for row in range(H):
for elem in range(W):
for inner_elem in range(float_length):
elem_bin=st.pack('<f', matrix2D_list[row][elem]) # kak float32
out_vector[k]=elem_bin[inner_elem]
k+=1
out_vector_.extend(out_vector)
return out_vector_
def get_next4bytes(inputt, pb):
f=inputt[pb]
pb+=1
sec=inputt[pb]
pb+=1
third=inputt[pb]
pb+=1
fourth=inputt[pb]
return st.unpack('<i', bytes((f, sec, third, fourth)))[0]
def get_matrix_float32(inputt, pb, rows, cols, elems_N):
float_size=4
pb_=pb
out_matrix=rows * [[0] * cols]
flatten_len=elems_N * float_size
flatten=[0] * elems_N
i=0
k=0
e=0
tmp_buff=[0] * float_size
while i < flatten_len + 1 :
if k==0 or k % 4!=0:
tmp_buff[k]=inputt[pb_]
k+=1
else:
val=st.unpack('<f', bytes(tmp_buff))[0]
tmp_buff[0]=inputt[pb_]
k=1
flatten[e]=val
e+=1
i+=1
pb_+=1
for row in range(rows):
for elem in range(cols):
out_matrix[row][elem]=flatten[row * cols + elem]
return out_matrix
# Serializator.
def ser_item(inputt):
out_vector=[]
for i in inputt:
if isinstance(i, int):
out_vector.append(i)
elif isinstance(i, list):
out_vector.extend(dump_matrix2D(i))
elif isinstance(i, str):
out_vector.extend(make_bin_string(i))
return out_vector
def ser_dumps(inputt:dict):
out_vector_=[]
for i in inputt.items():
out_vector=ser_item((Dmatrix2D, i[0], i[1]))
out_vector_.extend(out_vector)
out_vector_.append(Istop)
return out_vector_
def ser(dictt, fname):
with open(fname, 'wb') as f:
vector=ser_dumps(dictt)
f.write(bytes(vector))
# Deserializator.
def deser_machine(inputt):
pb=0
data={}
float_size=4
while True:
command=inputt[pb]
if command==Dmatrix2D:
pb+=1
name, lenn=make_string_from_bin(inputt, pb)
pb+=lenn
rows=inputt[pb]
pb+=1
cols=inputt[pb]
pb+=1
elems_N=get_next4bytes(inputt, pb)
pb+=4
matrix=get_matrix_float32(inputt, pb, rows, cols, elems_N)
pb+=(elems_N * float_size - 1)
data[name]=matrix
elif command==Istop:
return data
pb+=1
def deser(fname):
content=b''
with open(fname, 'rb') as f:
content=f.read()
return deser_machine(content)
if __name__ == '__main__':
# Mogut bit tolko pryamougolnie/kvadratnie 2D spiski.
test_matrix=[[10, 11]]
test_matrix_new=[[10, 20, 30], [10, 9, 40]]
ser({'test': test_matrix, 'test_matrix_new': test_matrix_new}, 'matr.bin')
outp=deser('matr.bin')
print('outp', outp)
"""
Out:
Создается файл matr.bin.
Читается как:
outp {'test': [[10.0, 11.0]], 'test_matrix_new': [[10.0, 9.0, 40.0], [10.0, 9.0, 40.0]]}
""" |