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 Apr 12, 2022
1 parent f941406 commit bbad79a
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 @@ -23,6 +24,16 @@
# pylint: disable=invalid-name,missing-docstring,too-many-public-methods
# pylint: disable=useless-object-inheritance,super-with-arguments,consider-using-f-string

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 @@ -34,6 +45,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 bbad79a

Please sign in to comment.