protobin 0.0.3__tar.gz → 0.0.4__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.
- {protobin-0.0.3 → protobin-0.0.4}/PKG-INFO +1 -1
- {protobin-0.0.3 → protobin-0.0.4}/protobin/fields.py +19 -15
- {protobin-0.0.3 → protobin-0.0.4}/pyproject.toml +1 -1
- {protobin-0.0.3 → protobin-0.0.4}/tests/tests.py +14 -0
- {protobin-0.0.3 → protobin-0.0.4}/.gitignore +0 -0
- {protobin-0.0.3 → protobin-0.0.4}/LICENSE.txt +0 -0
- {protobin-0.0.3 → protobin-0.0.4}/README.md +0 -0
- {protobin-0.0.3 → protobin-0.0.4}/protobin/__init__.py +0 -0
- {protobin-0.0.3 → protobin-0.0.4}/protobin/errors.py +0 -0
- {protobin-0.0.3 → protobin-0.0.4}/protobin/protocol.py +0 -0
- {protobin-0.0.3 → protobin-0.0.4}/tests/demo.json +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: protobin
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.4
|
|
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
|
|
@@ -75,8 +75,7 @@ class FieldBase:
|
|
|
75
75
|
def split(self, binary):
|
|
76
76
|
if self.bytes:
|
|
77
77
|
return binary[:self.bytes], binary[self.bytes:]
|
|
78
|
-
|
|
79
|
-
return binary[1:self.bytes + 1], binary[self.bytes + 1:]
|
|
78
|
+
return binary[1:binary[0] + 1], binary[binary[0] + 1:]
|
|
80
79
|
|
|
81
80
|
def to_dict(self):
|
|
82
81
|
return {
|
|
@@ -134,11 +133,18 @@ class FieldBits(FieldBase):
|
|
|
134
133
|
self.length = js.get('length', 0)
|
|
135
134
|
self.bytes = math.ceil(self.length / 8)
|
|
136
135
|
|
|
136
|
+
def decode(self, binary):
|
|
137
|
+
self.ensure_length(binary)
|
|
138
|
+
a, b, length = self.split(binary)
|
|
139
|
+
val = self.from_binary_bits(a, length)
|
|
140
|
+
return val, b
|
|
141
|
+
|
|
137
142
|
def split(self, binary):
|
|
138
|
-
if
|
|
139
|
-
self.
|
|
140
|
-
|
|
141
|
-
|
|
143
|
+
if self.length:
|
|
144
|
+
return binary[:self.bytes], binary[self.bytes:], self.length
|
|
145
|
+
length = binary[0]
|
|
146
|
+
bts = math.ceil(length / 8)
|
|
147
|
+
return binary[1:bts + 1], binary[bts + 1:], length
|
|
142
148
|
|
|
143
149
|
def to_binary(self, val: List[bool]):
|
|
144
150
|
if len(val) == 0:
|
|
@@ -150,18 +156,19 @@ class FieldBits(FieldBase):
|
|
|
150
156
|
bits = ''
|
|
151
157
|
for b in val:
|
|
152
158
|
bits += '1' if b else '0'
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
159
|
+
bts = int(len(bits) / 8)
|
|
160
|
+
if not self.length:
|
|
161
|
+
binary += len(val).to_bytes(1, 'big', signed=False)
|
|
162
|
+
binary += int(bits, 2).to_bytes(bts, 'big', signed=False)
|
|
156
163
|
return binary
|
|
157
164
|
|
|
158
|
-
def
|
|
165
|
+
def from_binary_bits(self, binary, length):
|
|
159
166
|
numero = int.from_bytes(binary, 'big', signed=False)
|
|
160
167
|
bits = bin(numero)[2:]
|
|
161
168
|
val = [False] * 8
|
|
162
169
|
for b in bits:
|
|
163
170
|
val.append(b == '1')
|
|
164
|
-
val = val[-
|
|
171
|
+
val = val[-length:]
|
|
165
172
|
return val
|
|
166
173
|
|
|
167
174
|
|
|
@@ -313,10 +320,7 @@ class FieldString(FieldBase):
|
|
|
313
320
|
return len(utf).to_bytes(1, 'big') + utf
|
|
314
321
|
|
|
315
322
|
def from_binary(self, binary):
|
|
316
|
-
|
|
317
|
-
self.bytes = int.from_bytes(binary[:1], 'big', signed=False)
|
|
318
|
-
binary = binary[1:]
|
|
319
|
-
return binary[:self.bytes].decode('utf')
|
|
323
|
+
return binary.decode('utf')
|
|
320
324
|
|
|
321
325
|
|
|
322
326
|
class FieldTime(FieldBase):
|
|
@@ -116,6 +116,7 @@ class BasicTest(unittest.TestCase):
|
|
|
116
116
|
{'key': 'test', 'bytes': 0, 'type': 'string'}
|
|
117
117
|
]}],
|
|
118
118
|
'bits': [{'key': 'test', 'type': 'bits'}],
|
|
119
|
+
'bits_fixed': [{'key': 'test', 'type': 'bits', 'length': 7}],
|
|
119
120
|
'bool': [{'key': 'test', 'bytes': 1, 'type': 'bool'}],
|
|
120
121
|
'char': [{'key': 'test', 'bytes': 1, 'type': 'char'}],
|
|
121
122
|
'date': [{'key': 'test', 'bytes': 1, 'type': 'date'}],
|
|
@@ -148,6 +149,15 @@ class BasicTest(unittest.TestCase):
|
|
|
148
149
|
print('recv', recv)
|
|
149
150
|
self.assertEqual(data, recv)
|
|
150
151
|
|
|
152
|
+
def test_bits_fixed(self):
|
|
153
|
+
data = {'test': [True, False, True, False, False, True, True]}
|
|
154
|
+
print('data', data)
|
|
155
|
+
binary = self.protocol.encode(data, 'bits_fixed')
|
|
156
|
+
print('binary', binary)
|
|
157
|
+
recv = self.protocol.decode(binary, 'bits_fixed')
|
|
158
|
+
print('recv', recv)
|
|
159
|
+
self.assertEqual(data, recv)
|
|
160
|
+
|
|
151
161
|
def test_bool(self):
|
|
152
162
|
data = {'test': False}
|
|
153
163
|
binary = self.protocol.encode(data, 'bool')
|
|
@@ -207,6 +217,10 @@ class BasicTest(unittest.TestCase):
|
|
|
207
217
|
binary = self.protocol.encode(data, 'string')
|
|
208
218
|
recv = self.protocol.decode(binary, 'string')
|
|
209
219
|
self.assertEqual(data, recv)
|
|
220
|
+
data = {'test': 'otro texto de prueba más largo'}
|
|
221
|
+
binary = self.protocol.encode(data, 'string')
|
|
222
|
+
recv = self.protocol.decode(binary, 'string')
|
|
223
|
+
self.assertEqual(data, recv)
|
|
210
224
|
|
|
211
225
|
def test_timestamp(self):
|
|
212
226
|
data = {'test': datetime.datetime(2024, 9, 13, 15, 23, 51, 265981)}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|