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("