Skip to content

Commit

Permalink
refactor: change usage of LppData.size to len(LppData)
Browse files Browse the repository at this point in the history
  • Loading branch information
smlng committed Apr 10, 2021
1 parent 1def1ed commit 172bd91
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
9 changes: 4 additions & 5 deletions cayennelpp/lpp_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def __bytes__(self):
buf = hdr_buf + dat_buf
return bytes(buf)

def __len__(self):
"""Return the length of the LppData byte string representation."""
return self.type.size + 2

def __str__(self):
"""Return a pretty string representation of the LppData object."""
return 'LppData(channel = {}, type = {}, value = {})'.format(
Expand All @@ -50,8 +54,3 @@ def from_bytes(class_object, buf):
raise BufferError("Buffer too small!")
value = lpp_type.decode(buf[2:(2 + size)])
return class_object(chn, type_, value)

@property
def size(self):
"""Return the length of the LppData byte string representation."""
return self.type.size + 2
6 changes: 3 additions & 3 deletions cayennelpp/lpp_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ def from_bytes(cls, buf):
while i < len(buf):
lppdata = LppData.from_bytes(buf[i:])
data.append(lppdata)
i = i + lppdata.size
i = i + len(lppdata)
return cls(data)

def __add_data_item(self, item):
"""Helper function to add an LppData item to this LppFrame."""
if not isinstance(item, LppData):
raise TypeError()
if self.maxsize > 0:
if self.size + item.size > self.maxsize:
if self.size + len(item) > self.maxsize:
raise BufferError()
self._data.append(item)

Expand Down Expand Up @@ -93,7 +93,7 @@ def size(self):
"""Return the length of the LppFrame byte string representation."""
size = 0
for d in self._data:
size += d.size
size += len(d)
return size

def get_by_type(self, type_):
Expand Down
2 changes: 1 addition & 1 deletion cayennelpp/tests/test_lpp_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_gps_from_bytes_invalid_size():


def test_lpp_data_size():
assert LppData(0, 0, 0).size == 3
assert len(LppData(0, 0, 0)) == 3


def test_lpp_data_str():
Expand Down

0 comments on commit 172bd91

Please sign in to comment.