Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add visit and visititems methods to Group class #31

Merged
merged 3 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions pyfive/high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def visit(self, func):
return that value from the visit method.

"""
raise NotImplementedError
return self.visititems(lambda name, obj: func(name))

def visititems(self, func):
"""
Expand All @@ -105,7 +105,19 @@ def visititems(self, func):
return that value from the visit method.

"""
raise NotImplementedError
root_name_length = len(self.name)
if not self.name.endswith('/'):
root_name_length += 1
queue = deque(self.values())
while queue:
obj = queue.popleft()
name = obj.name[root_name_length:]
ret = func(name, obj)
if ret is not None:
return ret
if isinstance(obj, Group):
queue.extend(obj.values())
return None

@property
def attrs(self):
Expand Down Expand Up @@ -167,15 +179,10 @@ def __init__(self, filename):

def _get_object_by_address(self, obj_addr):
""" Return the object pointed to by a given address. """
# breadth first search of the file hierarchy for the given address
queue = deque([self])
while queue:
obj = queue.popleft()
if obj._dataobjects.offset == obj_addr:
return obj
if isinstance(obj, Group):
queue.extend(child for child in obj.values())
return None
if self._dataobjects.offset == obj_addr:
return self
return self.visititems(
lambda x, y: y if y._dataobjects.offset == obj_addr else None)

def close(self):
""" Close the file. """
Expand Down
44 changes: 44 additions & 0 deletions tests/test_visit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
""" Test visit and visititems methods. """
from __future__ import print_function

import os

import pyfive

DIRNAME = os.path.dirname(__file__)
GROUPS_HDF5_FILE = os.path.join(DIRNAME, 'groups.hdf5')


def test_visit_method():
with pyfive.File(GROUPS_HDF5_FILE) as hfile:

assert hfile.visit(lambda x: print(x)) is None

name = 'group2/subgroup1'
assert hfile.visit(lambda x: x if x == name else None) == name

name = '/group2/subgroup1' # starts with /, not found
assert hfile.visit(lambda x: x if x == name else None) is None

group2 = hfile['group2']

name = 'subgroup1'
assert group2.visit(lambda x: x if x == name else None) == name

name = 'group2/subgroup1' # rooted at group2
assert group2.visit(lambda x: x if x == name else None) is None


def test_visititems_method():
with pyfive.File(GROUPS_HDF5_FILE) as hfile:

assert hfile.visititems(lambda x, y: print(x, y.name)) is None

name = 'group2/subgroup1'
ret = hfile.visititems(lambda x, y: x if x == name else None)
assert ret == name

name = '/group2/subgroup1' # starts with /, not found
assert hfile.visititems(lambda x, y: x if x == name else None) is None
ret = hfile.visititems(lambda x, y: y if y.name == name else None)
assert ret.name == name