protobin 0.0.43__tar.gz → 0.0.46__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: protobin
3
- Version: 0.0.43
3
+ Version: 0.0.46
4
4
  Summary: Python library for encode and decode data in protobin format
5
5
  Project-URL: Homepage, https://github.com/drmelectronic/protobin-python
6
6
  Project-URL: Bug Tracker, https://github.com/drmelectronic/protobin-python/issues
@@ -121,7 +121,11 @@ class ArrayField(FieldBase):
121
121
  data = {}
122
122
  for f in self.fields:
123
123
  val, binary = f.decode(binary)
124
- data[f.key] = val
124
+ if f.keys:
125
+ for k, v in val.items():
126
+ data[k] = v
127
+ else:
128
+ data[f.key] = val
125
129
  lista.append(data)
126
130
  return lista, binary
127
131
 
@@ -155,11 +159,13 @@ class BitsField(FieldBase):
155
159
  return b'\x00'
156
160
  binary = b''
157
161
  if len(val) % 8:
158
- bits = '0' * (8 - (len(val) % 8))
162
+ final = '0' * (8 - (len(val) % 8))
159
163
  else:
160
- bits = ''
164
+ final = ''
165
+ bits = ''
161
166
  for b in val:
162
167
  bits += '1' if b else '0'
168
+ bits += final
163
169
  bts = int(len(bits) / 8)
164
170
  if not self.length:
165
171
  binary += len(val).to_bytes(1, 'big', signed=False)
@@ -169,10 +175,12 @@ class BitsField(FieldBase):
169
175
  def from_binary_bits(self, binary, length):
170
176
  numero = int.from_bytes(binary, 'big', signed=False)
171
177
  bits = bin(numero)[2:]
172
- val = [False] * 8
173
- for b in bits:
174
- val.append(b == '1')
175
- val = val[-length:]
178
+ max_length = (length // 8) * 8 + (8 if (length % 8) else 0)
179
+ if len(bits) < max_length:
180
+ bits = '0' * (max_length - len(bits)) + bits
181
+ val = []
182
+ for i in range(length):
183
+ val.append(bits[i] == '1')
176
184
  return val
177
185
 
178
186
 
@@ -214,7 +222,7 @@ class CharField(FieldBase):
214
222
 
215
223
  def to_binary(self, val):
216
224
  if val is None:
217
- return bytes([0])
225
+ val = ''
218
226
  encoded = val[:self.bytes].encode('utf')
219
227
  if self.bytes == 1 and len(encoded) > 1:
220
228
  raise ValueError(f'{self} of one byte is possible to encode a utf-8 character')
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "protobin"
7
- version = "0.0.43"
7
+ version = "0.0.46"
8
8
  authors = [
9
9
  { name="Daniel Robles", email="drm.electronic@gmail.com" },
10
10
  ]
@@ -177,6 +177,26 @@ class BasicTest(unittest.TestCase):
177
177
  binary = self.protocol.encode(data, 'bits')
178
178
  h, recv = self.protocol.decode(binary)
179
179
  self.assertEqual(data, recv)
180
+ data = {'test': [True, False]}
181
+ binary = self.protocol.encode(data, 'bits')
182
+ h, recv = self.protocol.decode(binary)
183
+ self.assertEqual(data, recv)
184
+
185
+ def test_bits_false(self):
186
+ data = {'test': [False, False]}
187
+ binary = self.protocol.encode(data, 'bits')
188
+ h, recv = self.protocol.decode(binary)
189
+ self.assertEqual(data, recv)
190
+
191
+ def test_bits_false2(self):
192
+ data = {'test': [False, True]}
193
+ binary = self.protocol.encode(data, 'bits')
194
+ h, recv = self.protocol.decode(binary)
195
+ self.assertEqual(data, recv)
196
+ data = {'test': [False, True, False, False, False, False, False, False, False, False, False, False, False, False]}
197
+ binary = self.protocol.encode(data, 'bits')
198
+ h, recv = self.protocol.decode(binary)
199
+ self.assertEqual(data, recv)
180
200
 
181
201
  def test_bits_fixed(self):
182
202
  data = {'test': [True, False, True, False, False, True, True]}
@@ -202,12 +222,16 @@ class BasicTest(unittest.TestCase):
202
222
  h, recv = self.protocol.decode(binary)
203
223
  self.assertEqual(data, recv)
204
224
 
205
- def test_char_utf(self):
206
- data = {'test': 'ñ'}
225
+ def test_char_none(self):
226
+ data = {'test': None}
207
227
  binary = self.protocol.encode(data, 'char')
208
- print('binary', binary)
209
228
  h, recv = self.protocol.decode(binary)
210
- self.assertEqual(data, recv)
229
+ self.assertEqual({'test': ' '}, recv)
230
+
231
+ def test_char_utf(self):
232
+ data = {'test': 'ñ'}
233
+ with self.assertRaises(ValueError):
234
+ self.protocol.encode(data, 'char')
211
235
 
212
236
  def test_char2(self):
213
237
  data = {'test': 'FA', 'prueba': 'DO'}
@@ -851,3 +875,46 @@ report:
851
875
  self.assertEqual(binary, rasp)
852
876
  h, recv = client.decode(binary)
853
877
  print(recv)
878
+
879
+ def test_flag_array(self):
880
+ codec = {
881
+ "formats":{
882
+ "flag_array": {
883
+ "header": "F",
884
+ "fields": {
885
+ "id": {"bytes": 2, "type": "unsigned"},
886
+ "lado,control,datear,activo": {"type": "flags"}
887
+ }
888
+ }
889
+ }
890
+ }
891
+ data = {'lado': False, 'activo': True, 'datear': True, 'control': True, 'id': 1}
892
+ client = Protocol(js=codec, server=None)
893
+ binary = client.encode(data, 'flag_array')
894
+ h, recv = client.decode(binary)
895
+ self.assertEqual(data, recv)
896
+ codec = {
897
+ "formats":{
898
+ "flag_array": {
899
+ "header": "F",
900
+ "fields": {
901
+ "items": {
902
+ "type": "array",
903
+ "array": {
904
+ "id": {"bytes": 2, "type": "unsigned"},
905
+ "lado,control,datear,activo": {"type": "flags"}
906
+ }
907
+ }
908
+ }
909
+ }
910
+ }
911
+ }
912
+ data = {'items': [
913
+ {'lado': False, 'activo': True, 'datear': True, 'control': True, 'id': 1},
914
+ {'lado': False, 'activo': True, 'datear': False, 'control': True, 'id': 2},
915
+ {'lado': False, 'activo': True, 'datear': False, 'control': False, 'id': 3}
916
+ ]}
917
+ client = Protocol(js=codec, server=None)
918
+ binary = client.encode(data, 'flag_array')
919
+ h, recv = client.decode(binary)
920
+ self.assertEqual(data, recv)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes