import struct from decimal import Decimal def pwp_number(fmt, value_type): size = struct.calcsize(fmt) def decorator(cls): def __init__(self, value): self.value = value_type(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("