Easier ByteField representation

This commit is contained in:
Henryk Plötz 2019-06-29 11:28:07 +02:00
parent 48aa86598e
commit 77f1762ece
2 changed files with 17 additions and 2 deletions

View file

@ -70,11 +70,20 @@ class APDU(metaclass=FieldContainer):
[(name, getattr(self, name)) for name in self._bitmaps.keys()]
def __repr__(self):
reps = [
(
k,
self.FIELDS[k].represent(v) if k in self.FIELDS
else BITMAPS[self._bitmaps[k]][0].represent(v) if k in self._bitmaps
else "{!r}".format(v)
)
for (k, v) in self.items()
]
return "{}({})".format(
self.__class__.__name__,
", ".join(
"{}={!r}".format(k, v)
for (k, v) in self.items()
"{}={}".format(k, v)
for (k, v) in reps
)
)

View file

@ -12,6 +12,8 @@ class Endianness(Enum):
class Field:
REPR_FORMAT = "{!r}"
def __init__(self, required=True, ignore_parse_error=False, *args, **kwargs):
self.required = required
self.ignore_parse_error = ignore_parse_error
@ -29,6 +31,9 @@ class Field:
def serialize(self, data: Any) -> bytes:
raise NotImplementedError
def represent(self, data: Any) -> str:
return self.REPR_FORMAT.format(data)
def __set__(self, instance, value):
instance._values[self] = value
@ -142,6 +147,7 @@ class StringField(Field):
class ByteField(IntField, FixedLengthField):
LENGTH = 1
REPR_FORMAT = "0x{:02X}"
class BCDField(FixedLengthField):