From e884b7982f120dbe640b694c3973f659b174c696 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 28 Jul 2026 05:43:29 +0200 Subject: [PATCH] Advanced types --- src/adv_types.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ src/general.py | 5 +++-- src/pwp.py | 27 ++++++++++------------- src/units.py | 3 ++- 4 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 src/adv_types.py diff --git a/src/adv_types.py b/src/adv_types.py new file mode 100644 index 0000000..c729109 --- /dev/null +++ b/src/adv_types.py @@ -0,0 +1,57 @@ +import struct + +def pwp_number(fmt): + size = struct.calcsize(fmt) + + def decorator(cls): + def __init__(self, value): + self.value = value + + def to_com(self): + return struct.pack(fmt, self.value) + + @classmethod + def from_com(cls, data, offset=0): + value = struct.unpack_from(fmt, data, offset)[0] + return cls(value), offset + size + + cls.__init__ = __init__ + cls.to_com = to_com + cls.from_com = from_com + + return cls + + return decorator + +# Unsigned integers +@pwp_number(" bytes: origin = get_origin(typ) @@ -23,7 +21,7 @@ def encode_value(value, typ) -> bytes: return b"\x01" + encode_value(value, real_type) # list[T] - if origin is list: + elif origin is list: item_type = get_args(typ)[0] out = len(value).to_bytes(4, "little") @@ -62,10 +60,7 @@ def encode_value(value, typ) -> bytes: if typ is bool: return b"\x01" if value else b"\x00" - if typ is uint8: - return struct.pack("