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 Mar 20, 2018
1 parent 0b66357 commit ee93129
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions kaitaistruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
#
__version__ = '0.8'

from RecursionSafe import RecursionSafe

recSafe=RecursionSafe()

@recSafe.wrap
def repr_generator_for_all_props(self):
"""Generator to use in own __repr__ functions."""
return (
"".join(( str(k), "=", 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 @@ -26,6 +38,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
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@
],
keywords='kaitai,struct,construct,ksy,declarative,data structure,data format,file format,packet format,binary,parser,parsing,unpack,development',
py_modules=["kaitaistruct"],
requires=["RecursionSafe"],
dependency_links=["git+https://github.com/KOLANICH/RecursionSafe.py.git"],
)

0 comments on commit ee93129

Please sign in to comment.