diff --git a/kaitaistruct.py b/kaitaistruct.py index 733cbd9..4ccb9b7 100644 --- a/kaitaistruct.py +++ b/kaitaistruct.py @@ -2,6 +2,7 @@ import sys import struct from io import open, BytesIO, SEEK_CUR, SEEK_END # noqa +from reprlib import Repr, recursive_repr PY2 = sys.version_info[0] == 2 @@ -14,6 +15,16 @@ # __version__ = '0.9' +reprer = Repr() + +@recursive_repr() +def repr_generator_for_all_props(self): + """Generator to use in own __repr__ functions.""" + return ( + "".join(( str(k), "=", reprer.repr(getattr(self, k)) )) + for k in dir(self) + if k[0] != "_" and not hasattr(KaitaiStruct, k) and not isinstance(getattr(self, k), type) + ) class KaitaiStruct(object): def __init__(self, stream): @@ -25,6 +36,16 @@ def __enter__(self): def __exit__(self, *args, **kwargs): self.close() + def __repr__(self): + return "".join( + ( + self.__class__.__name__, + "(", + ", ".join( repr_generator_for_all_props(self) ), + ")" + ) + ) + def close(self): self._io.close()