Skip to content

Commit 7c38b83

Browse files
committed
Added __repr__ and a helper function
1 parent 0b66357 commit 7c38b83

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

kaitaistruct.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
#
1616
__version__ = '0.8'
1717

18+
from RecursionSafe import RecursionSafe
19+
20+
recSafe=RecursionSafe()
21+
22+
@recSafe.wrap
23+
def repr_generator_for_all_props(self):
24+
"""Generator to use in own __repr__ functions."""
25+
return (
26+
"".join(( str(k), "=", repr(v) ))
27+
for k, v in self.__dict__.items()
28+
if k[0] != "_"
29+
)
1830

1931
class KaitaiStruct(object):
2032
def __init__(self, stream):
@@ -26,6 +38,16 @@ def __enter__(self):
2638
def __exit__(self, *args, **kwargs):
2739
self.close()
2840

41+
def __repr__(self):
42+
return "".join(
43+
(
44+
self.__class__.__name__,
45+
"(",
46+
", ".join( repr_generator_for_all_props(self) ),
47+
")"
48+
)
49+
)
50+
2951
def close(self):
3052
self._io.close()
3153

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@
4444
],
4545
keywords='kaitai,struct,construct,ksy,declarative,data structure,data format,file format,packet format,binary,parser,parsing,unpack,development',
4646
py_modules=["kaitaistruct"],
47+
requires=["RecursionSafe"],
48+
dependency_links=["git+https://github.com/KOLANICH/RecursionSafe.py.git"],
4749
)

0 commit comments

Comments
 (0)