From 20104270000b398a095c9f3f17ca18112a844f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 27 Dec 2017 12:29:18 +0100 Subject: [PATCH] some simplifications --- .gitignore | 1 + gex/TinyFrame.py | 16 +++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 35d3f7d..48853df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Byte-compiled / optimized / DLL files __pycache__/ +__javascript__/ *.py[cod] *$py.class diff --git a/gex/TinyFrame.py b/gex/TinyFrame.py index f169fcc..04403ec 100644 --- a/gex/TinyFrame.py +++ b/gex/TinyFrame.py @@ -38,6 +38,8 @@ class TinyFrame: self.reset_parser() + self._CKSUM_BYTES = None # will be updated on first compose / accept + def reset_parser(self): # parser state: SOF, ID, LEN, TYPE, HCK, PLD, PCK self.ps = 'SOF' @@ -50,8 +52,7 @@ class TinyFrame: # received frame self.rf = TF_Msg() - @property - def _CKSUM_BYTES(self): + def _calc_cksum_bytes(self): if self.CKSUM_TYPE == 'none' or self.CKSUM_TYPE is None: return 0 elif self.CKSUM_TYPE == 'xor': @@ -84,10 +85,6 @@ class TinyFrame: else: raise Exception("Bad cksum type!") - @property - def _SOF_BYTES(self): - return 1 if self.USE_SOF_BYTE else 0 - def _gen_frame_id(self): """ Get a new frame ID @@ -131,9 +128,11 @@ class TinyFrame: frame_id can be an ID of an existing session, None for a new session. """ + if self._CKSUM_BYTES is None: + self._CKSUM_BYTES = self._calc_cksum_bytes() if pld is None: - pld = b'' + pld = bytearray() if id is None: id = self._gen_frame_id() @@ -165,6 +164,9 @@ class TinyFrame: def accept_byte(self, b): # TODO this seems ripe for rewrite to avoid repetitive code + if self._CKSUM_BYTES is None: + self._CKSUM_BYTES = self._calc_cksum_bytes() + if self.ps == 'SOF': if self.USE_SOF_BYTE: if b != self.SOF_BYTE: