From 55f5c1c80adc35313642d7d0bcafd27a203f495e Mon Sep 17 00:00:00 2001 From: smlng Date: Mon, 20 Sep 2021 21:32:37 +0200 Subject: [PATCH 1/5] fix: add to_bytes() helper for MicroPython --- cayennelpp/lpp_data.py | 4 ++++ cayennelpp/lpp_frame.py | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cayennelpp/lpp_data.py b/cayennelpp/lpp_data.py index 1b2af47..2188f16 100644 --- a/cayennelpp/lpp_data.py +++ b/cayennelpp/lpp_data.py @@ -47,6 +47,10 @@ def __str__(self): return 'LppData(channel = {}, type = {}, value = {})'.format( self.channel, self.type.name, str(self.value)) + def to_bytes(self): + """Explicit wrapper for MicroPython support.""" + return self.__bytes__() + @classmethod def from_bytes(class_object, buf): """Parse a given byte string and return a LppData object.""" diff --git a/cayennelpp/lpp_frame.py b/cayennelpp/lpp_frame.py index 5f52297..9dc4420 100644 --- a/cayennelpp/lpp_frame.py +++ b/cayennelpp/lpp_frame.py @@ -46,7 +46,14 @@ def __bytes__(self): """Return this LppFrame object as a byte string.""" buf = bytearray() for d in self._data: - buf = buf + bytes(d) + buf += bytes(d) + return bytes(buf) + + def to_bytes(self): + """Return this LppFrame object as a byte string (for MicroPython).""" + buf = bytearray() + for d in self._data: + buf += d.to_bytes() return bytes(buf) @classmethod From e4d9dbdef09a9ab2caeea56d6dc9dd035f6218f9 Mon Sep 17 00:00:00 2001 From: smlng Date: Mon, 27 Sep 2021 21:17:31 +0200 Subject: [PATCH 2/5] docs: add note for LppFrame.to_bytes() in README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index e4f9a75..63a419a 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,10 @@ frame.add_humidity(6, 34.5) buffer = bytes(frame) ``` +**Note:** MicroPython does not support `bytes(frame)` utilising the internal +method `LppFrame.__bytes__(self)` (yet). +Hence, you need to use `LppFrame.to_bytes(self)` instead. + ***Decoding*** ```Python From 025100bde251eab2fbc343e84a68ad7595897a85 Mon Sep 17 00:00:00 2001 From: smlng Date: Mon, 27 Sep 2021 21:23:01 +0200 Subject: [PATCH 3/5] tests: extend for tests to cover to_bytes() --- cayennelpp/tests/test_lpp_data.py | 1 + cayennelpp/tests/test_lpp_frame.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/cayennelpp/tests/test_lpp_data.py b/cayennelpp/tests/test_lpp_data.py index 353b544..32fb496 100644 --- a/cayennelpp/tests/test_lpp_data.py +++ b/cayennelpp/tests/test_lpp_data.py @@ -21,6 +21,7 @@ def test_generic_from_bytes(): buff = bytes([0x00, 0x64, 0xff, 0xff, 0xff, 0xfb]) data = LppData.from_bytes(buff) assert buff == bytes(data) + assert buff == data.to_bytes() assert int(data.type) == 100 assert data.value == (4294967291,) diff --git a/cayennelpp/tests/test_lpp_frame.py b/cayennelpp/tests/test_lpp_frame.py index ae6396a..d126c23 100644 --- a/cayennelpp/tests/test_lpp_frame.py +++ b/cayennelpp/tests/test_lpp_frame.py @@ -82,22 +82,26 @@ def test_frame_from_bytes(): buf = bytes([0x03, 0x67, 0x01, 0x10, 0x05, 0x67, 0x00, 0xff]) frame = LppFrame.from_bytes(buf) assert buf == bytes(frame) + assert buf == frame.to_bytes() assert len(frame) == 2 # 01 67 FF D7 buf = bytes([0x01, 0x67, 0xFF, 0xD7]) frame = LppFrame.from_bytes(buf) assert buf == bytes(frame) + assert buf == frame.to_bytes() assert len(frame) == 1 # 06 71 04 D2 FB 2E 00 00 buf = bytes([0x06, 0x71, 0x04, 0xD2, 0xFB, 0x2E, 0x00, 0x00]) frame = LppFrame.from_bytes(buf) assert buf == bytes(frame) + assert buf == frame.to_bytes() assert len(frame) == 1 # 01 88 06 76 5f f2 96 0a 00 03 e8 buf = bytes([0x01, 0x88, 0x06, 0x76, 0x5f, 0xf2, 0x96, 0x0a, 0x00, 0x03, 0xe8]) frame = LppFrame.from_bytes(buf) assert buf == bytes(frame) + assert buf == frame.to_bytes() assert len(frame) == 1 From f20122a5c9f4900464b85a077f6a1fe86fac30e0 Mon Sep 17 00:00:00 2001 From: smlng Date: Tue, 12 Oct 2021 21:51:28 +0200 Subject: [PATCH 4/5] docs: remove trailing whitespace in README --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 63a419a..bf2352a 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,11 @@ and data resolution. ## Getting Started PyCayenneLPP does not have any external dependencies and only uses builtin -functions and types of Python 3. It is compatible with all the latest and -officially supported Python versions 3.6 and above, though even Python 3.4 +functions and types of Python 3. It is compatible with all the latest and +officially supported Python versions 3.6 and above, though even Python 3.4 will do. -Since PyCayenneLPP 1.2.0 MicroPython is officially supported, and published +Since PyCayenneLPP 1.2.0 MicroPython is officially supported, and published as a separate package under `micropython-pycayennelpp`. ### Python 3 Prerequisites @@ -112,8 +112,8 @@ frame.add_humidity(6, 34.5) buffer = bytes(frame) ``` -**Note:** MicroPython does not support `bytes(frame)` utilising the internal -method `LppFrame.__bytes__(self)` (yet). +**Note:** MicroPython does not support `bytes(frame)` utilising the internal +method `LppFrame.__bytes__(self)` (yet). Hence, you need to use `LppFrame.to_bytes(self)` instead. ***Decoding*** @@ -133,7 +133,7 @@ print(frame) ***JSON Encoding*** -The LppUtil class provides helper function for proper JSON encoding of +The LppUtil class provides helper function for proper JSON encoding of PyCayenneLpp types, i.e. LppFrame, LppData and LppType. ```python @@ -150,7 +150,7 @@ frame.add_humidity(6, 34.5) print(json.dumps(frame, default=LppUtil.json_encode, indent=2)) ``` -There are two wrapper functions to explicitly encode the LPP type as a +There are two wrapper functions to explicitly encode the LPP type as a number or string, number being default for `LppUtil.json_encode` (see above): ```python From 63deef123ad835240448e6a6ce5a286e9df3fd12 Mon Sep 17 00:00:00 2001 From: smlng Date: Tue, 12 Oct 2021 21:52:21 +0200 Subject: [PATCH 5/5] chore: bump version to 2.4.0 --- setup.py | 2 +- usetup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index cf33a29..5e88e56 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ def readme(): setup( name='pycayennelpp', - version='2.3.0', + version='2.4.0', python_requires='>=3.6', description='Encoder and Decoder for CayenneLLP', long_description=readme(), diff --git a/usetup.py b/usetup.py index 423c4fb..385766e 100644 --- a/usetup.py +++ b/usetup.py @@ -9,7 +9,7 @@ def readme(): setup( name='micropython-pycayennelpp', - version='2.3.0', + version='2.4.0', description='Encoder and Decoder for CayenneLLP', long_description=readme(), long_description_content_type='text/markdown',