| 123456789101112131415161718192021222324252627282930313233343536 |
- class field_decoder:
- def __init__(self, target: object) -> None:
- self.__field = target
-
- def load(self, content: bytes) -> object:
- self.__field.set(self.__bytes_decode(content))
- return self
- def __bytes_decode(self, content: bytes) -> any:
- if self.__field.target_type is str:
- return self.__decode_str(content)
- if self.__field.target_type is bytes:
- return content
- if self.__field.target_type is int:
- return self.__decode_int(content)
-
- if self.__field.target_type is float:
- return self.__decode_float(content)
-
- if self.__field.target_type is bool:
- return self.__decode_bool(content)
- def __decode_bool(self, content: bytes) -> bool:
- return True if int.from_bytes(content) != 0 else False
- def __decode_float(self, content: bytes) -> float:
- return self.__decode_int(content) / pow(10, self.__field.precission)
- def __decode_str(self, content: bytes) -> str:
- return content.decode("UTF-8")
- def __decode_int(self, content: bytes) -> int:
- return int.from_bytes(content)
-
|