Skip to content

Instantly share code, notes, and snippets.

@janpipek
Last active August 29, 2015 14:06
Show Gist options
  • Save janpipek/d113821c958c8a3cac20 to your computer and use it in GitHub Desktop.
Save janpipek/d113821c958c8a3cac20 to your computer and use it in GitHub Desktop.
Quickly show HDF5 tree using pydons
#!/usr/bin/env python
import pydons
import sys
def _wrap_with(code):
def inner(text, bold=True):
c = code
if bold:
c = "1;%s" % c
return "\033[%sm%s\033[m" % (c, text) # '\033[m'
return inner
red = _wrap_with('31')
green = _wrap_with('32')
yellow = _wrap_with('33')
blue = _wrap_with('34')
magenta = _wrap_with('35')
cyan = _wrap_with('36')
white = _wrap_with('37')
if len(sys.argv) < 2:
print "Usage: hdftree <path> [<maximum indent level>]"
sys.exit(-1)
if len(sys.argv) >= 3:
max_indent = int(sys.argv[2])
else:
max_indent = 100
fb = pydons.FileBrowser(sys.argv[1])
def browse(object, indent=0):
if hasattr(object, "items"):
for key, item in object.items():
if type(item) == pydons.MatStruct:
if indent < max_indent:
print " " * indent + yellow(key)
browse(item, indent + 1)
else:
print " " * indent + yellow(key) + ": " + white("[%d items]" % len(item.items()))
else:
print (" " * indent + green(key) + ": " +
white(" x ".join((str(i) for i in item.shape)) +
", " + str(item.dtype)))
browse(fb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment