Skip to content

Commit 12b4ee7

Browse files
committed
Added __repr__ and a helper function
1 parent 8391731 commit 12b4ee7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

kaitaistruct.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import struct
44
from io import open, BytesIO, SEEK_CUR, SEEK_END # noqa
5+
from reprlib import Repr, recursive_repr
56

67
PY2 = sys.version_info[0] == 2
78

@@ -23,6 +24,17 @@
2324
# pylint: disable=invalid-name,missing-docstring,too-many-public-methods
2425
# pylint: disable=useless-object-inheritance,super-with-arguments,consider-using-f-string
2526

27+
reprer = Repr()
28+
reprer.maxother = sys.maxsize * 2 + 1
29+
30+
@recursive_repr()
31+
def repr_generator_for_all_props(self):
32+
"""Generator to use in own __repr__ functions."""
33+
return (
34+
"".join(( str(k), "=", reprer.repr(getattr(self, k)) ))
35+
for k in dir(self)
36+
if k[0] != "_" and not hasattr(KaitaiStruct, k) and not isinstance(getattr(self, k), type)
37+
)
2638

2739
class KaitaiStruct(object):
2840
def __init__(self, stream):
@@ -34,6 +46,16 @@ def __enter__(self):
3446
def __exit__(self, *args, **kwargs):
3547
self.close()
3648

49+
def __repr__(self):
50+
return "".join(
51+
(
52+
self.__class__.__name__,
53+
"(",
54+
", ".join( repr_generator_for_all_props(self) ),
55+
")"
56+
)
57+
)
58+
3759
def close(self):
3860
self._io.close()
3961

0 commit comments

Comments
 (0)