Skip to content

Commit bbad79a

Browse files
committed
Added __repr__ and a helper function
1 parent f941406 commit bbad79a

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

@@ -23,6 +24,16 @@
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+
29+
@recursive_repr()
30+
def repr_generator_for_all_props(self):
31+
"""Generator to use in own __repr__ functions."""
32+
return (
33+
"".join(( str(k), "=", reprer.repr(getattr(self, k)) ))
34+
for k in dir(self)
35+
if k[0] != "_" and not hasattr(KaitaiStruct, k) and not isinstance(getattr(self, k), type)
36+
)
2637

2738
class KaitaiStruct(object):
2839
def __init__(self, stream):
@@ -34,6 +45,16 @@ def __enter__(self):
3445
def __exit__(self, *args, **kwargs):
3546
self.close()
3647

48+
def __repr__(self):
49+
return "".join(
50+
(
51+
self.__class__.__name__,
52+
"(",
53+
", ".join( repr_generator_for_all_props(self) ),
54+
")"
55+
)
56+
)
57+
3758
def close(self):
3859
self._io.close()
3960

0 commit comments

Comments
 (0)