Skip to content

Commit

Permalink
[Section] Add pprint method
Browse files Browse the repository at this point in the history
Closes #309
  • Loading branch information
mpsonntag committed Nov 22, 2018
1 parent bcf5357 commit 3b8d475
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions odml/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,30 @@ def create_property(self, name, value=None, dtype=None, oid=None):
prop.parent = self

return prop

def pprint(self, indent=2, max_depth=1, max_length=80, current_depth=0):
"""
Pretty print method to visualize Section-Property trees.
:param indent: number of leading spaces for every child Section or Property.
:param max_length: maximum number of characters printed in one line.
:param current_depth: number of hierarchical levels printed from the
starting Section.
"""
spaces = " " * (current_depth * indent)
sec_str = "{} {} [{}]".format(spaces, self.name, self.type)
print(sec_str)
for p in self.props:
p.pprint(current_depth=current_depth, indent=indent,
max_length=max_length)
if max_depth == -1 or current_depth < max_depth:
for s in self.sections:
s.pprint(current_depth=current_depth+1, max_depth=max_depth,
indent=indent, max_length=max_length)
elif max_depth == current_depth:
child_sec_indent = spaces + " " * indent
more_indent = spaces + " " * (current_depth + 2 * indent)
for s in self.sections:
print("{} {} [{}]\n{}[...]".format(child_sec_indent,
s.name, s.type,
more_indent))

0 comments on commit 3b8d475

Please # to comment.