Skip to content

Commit

Permalink
Removed unused attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
HeMan committed Mar 7, 2020
1 parent 1d2d109 commit 2849dd2
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions binmap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ class BinmapDataclass(ABC):
Dataclass that does the converting to and from binary data
"""

_byteorder = ""
_formatstring = ""
_binarydata: dataclasses.InitVar[bytes] = b""

Expand All @@ -160,11 +159,10 @@ def __init_subclass__(cls, byteorder: str = ">"):
Subclass initiator. This makes the inheriting class a dataclass.
:param str byteorder: byteorder for binary data
"""
cls._byteorder = byteorder
dataclasses.dataclass(cls)
type_hints = get_type_hints(cls)

cls._formatstring = ""
cls._formatstring = byteorder

for field_ in dataclasses.fields(cls):
_base, _type = datatypemapping[type_hints[field_.name]]
Expand Down Expand Up @@ -193,12 +191,8 @@ def __bytes__(self):
"""
return struct.pack(
# TODO: use datclass.fields
self._byteorder + self._formatstring,
*(
v
for k, v in self.__dict__.items()
if k not in ["_byteorder", "_formatstring", "_binarydata"]
),
self._formatstring,
*(v for k, v in self.__dict__.items() if k not in ["_formatstring"]),
)

def __post_init__(self, _binarydata: bytes):
Expand All @@ -207,7 +201,7 @@ def __post_init__(self, _binarydata: bytes):
:param bytes _binarydata: Binary string that will be unpacked.
"""
if _binarydata != b"":
self._unpacker(_binarydata)
self.frombytes(_binarydata)
# Kludgy hack to keep order
for f in dataclasses.fields(self):
if "padding" in f.metadata:
Expand All @@ -219,7 +213,7 @@ def __post_init__(self, _binarydata: bytes):
del self.__dict__[f.name]
self.__dict__.update({f.name: val})

def _unpacker(self, value: bytes):
def frombytes(self, value: bytes):
"""
Unpacks value to each field
:param bytes value: binary string to unpack
Expand All @@ -231,18 +225,10 @@ def _unpacker(self, value: bytes):
for f in dataclasses.fields(self)
if not (type_hints[f.name] is types.pad)
]
args = struct.unpack(self._byteorder + self._formatstring, value)
args = struct.unpack(self._formatstring, value)
for arg, name in zip(args, datafields):
if "constant" in datafieldsmap[name].metadata:
if arg != datafieldsmap[name].default:
raise ValueError("Constant doesn't match binary data")

setattr(self, name, arg)

def frombytes(self, value: bytes):
"""
Public method to convert from byte string
:param bytes value: binary string to unpack
"""
self._unpacker(value)
self._binarydata = value

0 comments on commit 2849dd2

Please sign in to comment.