Skip to content

Commit 38e38f3

Browse files
committed
Added __repr__ and a helper function
1 parent c56bf6b commit 38e38f3

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

kaitaistruct.py

+21
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

@@ -20,6 +21,16 @@
2021
# runtime is compatible with the generated code.
2122
API_VERSION = (0, 10)
2223

24+
reprer = Repr()
25+
26+
@recursive_repr()
27+
def repr_generator_for_all_props(self):
28+
"""Generator to use in own __repr__ functions."""
29+
return (
30+
"".join(( str(k), "=", reprer.repr(getattr(self, k)) ))
31+
for k in dir(self)
32+
if k[0] != "_" and not hasattr(KaitaiStruct, k) and not isinstance(getattr(self, k), type)
33+
)
2334

2435
class KaitaiStruct(object):
2536
def __init__(self, stream):
@@ -31,6 +42,16 @@ def __enter__(self):
3142
def __exit__(self, *args, **kwargs):
3243
self.close()
3344

45+
def __repr__(self):
46+
return "".join(
47+
(
48+
self.__class__.__name__,
49+
"(",
50+
", ".join( repr_generator_for_all_props(self) ),
51+
")"
52+
)
53+
)
54+
3455
def close(self):
3556
self._io.close()
3657

0 commit comments

Comments
 (0)