Skip to content

Commit 3b8d475

Browse files
committed
[Section] Add pprint method
Closes #309
1 parent bcf5357 commit 3b8d475

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

odml/section.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,3 +611,30 @@ def create_property(self, name, value=None, dtype=None, oid=None):
611611
prop.parent = self
612612

613613
return prop
614+
615+
def pprint(self, indent=2, max_depth=1, max_length=80, current_depth=0):
616+
"""
617+
Pretty print method to visualize Section-Property trees.
618+
619+
:param indent: number of leading spaces for every child Section or Property.
620+
:param max_length: maximum number of characters printed in one line.
621+
:param current_depth: number of hierarchical levels printed from the
622+
starting Section.
623+
"""
624+
spaces = " " * (current_depth * indent)
625+
sec_str = "{} {} [{}]".format(spaces, self.name, self.type)
626+
print(sec_str)
627+
for p in self.props:
628+
p.pprint(current_depth=current_depth, indent=indent,
629+
max_length=max_length)
630+
if max_depth == -1 or current_depth < max_depth:
631+
for s in self.sections:
632+
s.pprint(current_depth=current_depth+1, max_depth=max_depth,
633+
indent=indent, max_length=max_length)
634+
elif max_depth == current_depth:
635+
child_sec_indent = spaces + " " * indent
636+
more_indent = spaces + " " * (current_depth + 2 * indent)
637+
for s in self.sections:
638+
print("{} {} [{}]\n{}[...]".format(child_sec_indent,
639+
s.name, s.type,
640+
more_indent))

0 commit comments

Comments
 (0)