Skip to content

Commit

Permalink
Added __repr__ and a helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
KOLANICH committed Dec 15, 2020
1 parent 9bc5ab4 commit 79f5669
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions kaitaistruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand All @@ -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()

Expand Down

0 comments on commit 79f5669

Please sign in to comment.