Skip to content

Commit

Permalink
Merge pull request #393 from hkchekc/file_pprint
Browse files Browse the repository at this point in the history
Add pprint function for file

LGTM
  • Loading branch information
achilleas-k authored May 9, 2019
2 parents ef2637c + b9a7a47 commit d849bda
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
55 changes: 35 additions & 20 deletions nixio/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,42 +362,57 @@ def find_sources(self, filtr=lambda _: True, limit=None):
limit = maxint
return finders._find_sources(self, filtr, limit)

def pprint(self, indent=2, max_length=120, extra=True):
print(self)
def pprint(self, indent=2, max_length=120, extra=True, start_depth=0):
"""
Pretty Printing the Data and MetaData Tree of the whole File
:param indent: The length of one indentation space
:type indent: int
:param max_length: Maximum length of each line of output
:type max_length: int
:param extra: True to print extra information of Entities
:type extra: bool
:param start_depth: Starting depth of indentation
:type start_depth: int
"""
print("{}{}".format(" " * indent*start_depth, self))
for grp in self.groups:
self._pp(grp, max_length, indent, False)
self._pp(grp, max_length, indent*(start_depth + 1), False)
for da in grp.data_arrays:
self._pp(da, max_length, indent * 2, extra, True)
self._pp(da, max_length, indent*(start_depth + 2)
, extra, True)
for dim in da.dimensions:
self._pp(dim, max_length, indent * 3, False)
self._pp(dim, max_length, indent*(start_depth + 3), False)
for df in grp.data_frames:
self._pp(df, max_length, indent * 2, extra, True)
self._pp(df, max_length,
indent*(start_depth + 2), extra, True)
for tag in grp.tags:
self._pp(tag, max_length, indent * 2, extra, True)
self._pp(tag, max_length,
indent*(start_depth + 2), extra, True)
for fe in tag.features:
self._pp(fe, max_length, indent * 3, False)
self._pp(fe, max_length, indent*(start_depth + 3), False)
for mt in grp.multi_tags:
self._pp(mt, max_length, indent * 2, extra, True)
self._pp(mt, max_length, indent*(start_depth + 2), extra, True)
for fe in mt.features:
self._pp(fe, max_length, indent * 3, False)
self._pp(fe, max_length, indent*(start_depth + 3), False)
for da in self.data_arrays:
self._pp(da, max_length, indent, extra)
self._pp(da, max_length, indent*(start_depth + 1), extra)
for dim in da.dimensions:
self._pp(dim, max_length, indent * 2, False)
self._pp(dim, max_length, indent*(start_depth + 2), False)
for df in self.data_frames:
self._pp(df, max_length, indent, extra)
self._pp(df, max_length, indent*(start_depth + 1), extra)
for tag in self.tags:
self._pp(tag, max_length, indent, extra)
self._pp(tag, max_length, indent*(start_depth + 1), extra)
for fe in tag.features:
self._pp(fe, max_length, indent * 2, False)
self._pp(fe, max_length, indent*(start_depth + 2), False)
for mt in self.multi_tags:
self._pp(mt, max_length, indent, extra)
self._pp(mt, max_length, indent*(start_depth + 1), extra)
for fe in mt.features:
self._pp(fe, max_length, indent * 2, False)
self._pp(fe, max_length, indent*(start_depth + 2), False)

@staticmethod
def _pp(obj, ml, indent, ex, grp=False):
spaces = " " * (indent)
spaces = " "*(indent)
if grp:
prefix = "*"
else:
Expand All @@ -421,15 +436,15 @@ def _pp(obj, ml, indent, ex, grp=False):
else:
p = "{}{}{}".format(spaces, prefix, obj)
if len(p) > ml - 4:
split_len = int(ml / 2)
split_len = int(ml/2)
str1 = p[0:split_len]
str2 = p[-split_len:]
print("{} ... {}".format(str1, str2))
else:
print(p)
if ex:
if len(n) > ml - 4:
split_len = int(ml / 2)
split_len = int(ml/2)
nstr1 = n[0:split_len]
nstr2 = n[-split_len:]
print("{} ... {}".format(nstr1, nstr2))
Expand Down
25 changes: 24 additions & 1 deletion nixio/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,30 @@ def validate(self):
print("No errors found: The file is a valid NIX file")
return errors

# TODO: if same file, set_attr("entity_id", id_)
def pprint(self, indent=2, max_length=120, extra=True, max_depth=3):
"""
Pretty Printing the Data and MetaData Tree of the whole File
:param indent: The length of one indentation space
:type indent: int
:param max_length: Maximum length of each line of output
:type max_length: int
:param extra: True to print extra information of Entities
:type extra: bool
:param max_depth: Maximum recursion being printed in MetaData tree
:type max_depth: int
"""
print("File: name = {}".format(self._h5group.group.file.filename))
if self.blocks:
for blk in self.blocks:
blk.pprint(indent=indent,
max_length=max_length, extra=extra, start_depth=1)
if self.sections:
for sec in self.sections:
sec.pprint(indent=indent, max_depth=max_depth,
max_length=max_length, current_depth=1)

# TODO: if same file, set_attr("entity_id", id_)

def copy_section(self, obj, children=True, keep_id=True, name=""):
"""
Expand Down
2 changes: 1 addition & 1 deletion nixio/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def pprint(self, indent=2, max_depth=1, max_length=80, current_depth=0):
"""

spaces = " " * (current_depth * indent)
sec_str = "{} {} [{}]".format(spaces, self.name, self.type)
sec_str = "{}{} [{}]".format(spaces, self.name, self.type)
print(sec_str)
for p in self.props:
p.pprint(current_depth=current_depth, indent=indent,
Expand Down

0 comments on commit d849bda

Please sign in to comment.