Skip to content

Commit

Permalink
Code style improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
denpamusic committed Oct 19, 2023
1 parent f3b5fbb commit f91e0d3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pyplumio/helpers/uid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

def unpack_uid(message: bytearray, offset: int = 0) -> str:
"""Decode and return a complete UID string."""
uid_length = message[offset]
length = message[offset]
offset += 1
uid = message[offset : uid_length + offset]
uid = message[offset : length + offset]

return _encode_base5(uid + _uid_crc(uid))

Expand Down
1 change: 1 addition & 0 deletions pyplumio/structures/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def _convert_to_datetime(seconds: int) -> datetime:
"""Convert timestamp to a datetime object."""

def _seconds_to_datetime_args(seconds: int) -> Generator[Any, None, None]:
"""Convert seconds to a kwarg for a datetime class."""
intervals: tuple[tuple[str, int, int], ...] = (
(ATTR_YEAR, 32140800, 2000), # 60sec * 60min * 24h * 31d * 12m
(ATTR_MONTH, 2678400, 1), # 60sec * 60min * 24h * 31d
Expand Down
8 changes: 6 additions & 2 deletions pyplumio/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import functools
import struct

# Data type unpackers.
unpack_float = struct.Struct("<f").unpack
unpack_char = struct.Struct("<b").unpack
unpack_short = struct.Struct("<h").unpack
Expand All @@ -13,8 +14,6 @@
unpack_double = struct.Struct("<d").unpack
unpack_int64 = struct.Struct("<q").unpack
unpack_uint64 = struct.Struct("<Q").unpack
pack_header = struct.Struct("<BH4B").pack_into
unpack_header = struct.Struct("<BH4B").unpack_from


def unpack_string(data: bytearray, offset: int = 0) -> str:
Expand All @@ -24,6 +23,11 @@ def unpack_string(data: bytearray, offset: int = 0) -> str:
return data[offset : offset + strlen + 1].decode()


# Frame header packer/unpacker.
pack_header = struct.Struct("<BH4B").pack_into
unpack_header = struct.Struct("<BH4B").unpack_from


def crc(data: bytes) -> int:
"""Return a checksum."""
return functools.reduce(lambda x, y: x ^ y, data)
Expand Down

0 comments on commit f91e0d3

Please sign in to comment.