Skip to content

Commit

Permalink
Adding printing of attributes (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus committed Feb 13, 2020
1 parent 4627a9e commit 7ebc1e1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion GooseHDF5/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
warnings.filterwarnings("ignore")


__version__ = '0.4.0'
__version__ = '0.5.0'


def abspath(path):
Expand Down
48 changes: 43 additions & 5 deletions GooseHDF5/cli/G5list.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
-f, --fold=ARG Fold paths.
-d, --max-depth=ARG Maximum depth to display.
-r, --root=ARG Start a certain point in the path-tree. [default: /]
--info Print information: shape, dtype.
-i, --info Print information: shape, dtype.
-l, --long As above but will all attributes.
-h, --help Show help.
--version Show version.
Expand Down Expand Up @@ -46,11 +47,18 @@ def print_info(source, paths):
Print the paths to all datasets (one per line), including type information.
'''

def has_attributes(lst):
for i in lst:
if i != '-' and i != '0':
return True
return False

out = {
'path': [],
'size': [],
'shape': [],
'dtype': []}
'dtype': [],
'attrs': []}

for path in paths:
if path in source:
Expand All @@ -59,11 +67,13 @@ def print_info(source, paths):
out['size'] += [str(data.size)]
out['shape'] += [str(data.shape)]
out['dtype'] += [str(data.dtype)]
out['attrs'] += [str(len(data.attrs))]
else:
out['path'] += [path]
out['size'] += ['-']
out['shape'] += ['-']
out['dtype'] += ['-']
out['attrs'] += ['-']

width = {}
for key in out:
Expand All @@ -73,16 +83,42 @@ def print_info(source, paths):
fmt = '{0:%ds} {1:%ds} {2:%ds} {3:%ds}' % \
(width['path'], width['size'], width['shape'], width['dtype'])

print(fmt.format('path', 'size', 'shape', 'dtype'))
if has_attributes(out['attrs']):
fmt += ' {4:%ds}' % width['attrs']

print(fmt.format('path', 'size', 'shape', 'dtype', 'attrs'))
print(fmt.format(
'=' * width['path'],
'=' * width['size'],
'=' * width['shape'],
'=' * width['dtype']))
'=' * width['dtype'],
'=' * width['attrs']))

for i in range(len(out['path'])):
print(fmt.format(out['path'][i], out['size'][i], out['shape'][i], out['dtype'][i]))
print(fmt.format(
out['path'][i],
out['size'][i],
out['shape'][i],
out['dtype'][i],
out['attrs'][i]))


def print_attribute(source, paths):

for path in paths:
if path in source:

data = source[path]

print('"{0:s}"'.format(path))
print('size = {0:s}, shape = {1:s}, dtype = {2:s}'.format(
str(data.size), str(data.shape), str(data.dtype)))

for key in data.attrs:
print(key + ':')
print(data.attrs[key])

print('')

def main():
r'''
Expand All @@ -103,5 +139,7 @@ def main():

if args['--info']:
print_info(source, paths)
elif args['--long']:
print_attribute(source, paths)
else:
print_plain(source, paths)
8 changes: 4 additions & 4 deletions GooseHDF5/cli/G5print.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<dataset> Path to the dataset.
Options:
-r, --regex Evaluate dataset name as a regular expression.
--info Print information: shape, dtype.
-h, --help Show help.
--version Show version.
-r, --regex Evaluate dataset name as a regular expression.
-i, --info Print information: shape, dtype.
-h, --help Show help.
--version Show version.
(c - MIT) T.W.J. de Geus | tom@geus.me | www.geus.me | github.com/tdegeus/GooseHDF5
'''
Expand Down

0 comments on commit 7ebc1e1

Please sign in to comment.