Skip to content

Commit

Permalink
Add tests for attr/link order
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjala committed Apr 29, 2024
1 parent d245136 commit 8f9e20e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
5 changes: 3 additions & 2 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ attributes:
are iterated in ascending creation order (consistent with
``dict`` in Python 3.7+); otherwise in ascending alphanumeric
order. Global configuration value can be overridden for
particular file and its subgroups by specifying ``track_order`` argument to
:class:`h5py.File`. The default is ``False``.
particular container by specifying ``track_order`` argument to
:class:`h5py.File`, :meth:`h5py.Group.create_group`,
:meth:`h5py.Group.create_dataset`. The default is ``False``.


IPython
Expand Down
62 changes: 62 additions & 0 deletions test/hl/test_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,68 @@ def test_create(self):
# close file
f.close()

def test_attribute_alphanumeric_order(self):
filename = self.getFileName("attribute_order")

# disable track_order
f = h5py.File(filename, 'w', track_order=False)

# Attributes on a group should be iterated through in alphanumeric order
g1 = f.create_group('g1')

g1.attrs['Z'] = 0
g1.attrs['Y'] = 1
g1.attrs.create('W', 2)
g1.attrs['X'] = 3
g1.attrs['V'] = 4

expected_sequence = ['V', 'W', 'X', 'Y', 'Z']

for i, attr in enumerate(g1.attrs):
self.assertEqual(attr, expected_sequence[i])

# Attributes on a dataset should be iterated through in alphanumeric order
dset = g1.create_dataset('dset', data=[1, 2, 3])
dset.attrs['Z'] = 0
dset.attrs['Y'] = 1
dset.attrs.create('W', 2)
dset.attrs['X'] = 3
dset.attrs['V'] = 4

for i, attr in enumerate(dset.attrs):
self.assertEqual(attr, expected_sequence[i])

def test_attribute_creation_order(self):
filename = self.getFileName("attribute_order")

# enable track_order
f = h5py.File(filename, 'w', track_order=True)

# Attributes on a group should be iterated through in creation order
g1 = f.create_group('g1')

g1.attrs['Z'] = 0
g1.attrs['Y'] = 1
g1.attrs.create('W', 2)
g1.attrs['X'] = 3
g1.attrs['V'] = 4

expected_sequence = ['Z', 'Y', 'W', 'X', 'V']

for i, attr in enumerate(g1.attrs):
self.assertEqual(attr, expected_sequence[i])

# Attributes on a dataset should be iterated through in creation order
dset = g1.create_dataset('dset', data=[1, 2, 3])
dset.attrs['Z'] = 0
dset.attrs['Y'] = 1
dset.attrs.create('W', 2)
dset.attrs['X'] = 3
dset.attrs['V'] = 4

for i, attr in enumerate(dset.attrs):
self.assertEqual(attr, expected_sequence[i])


if __name__ == '__main__':
loglevel = logging.ERROR
Expand Down
28 changes: 27 additions & 1 deletion test/hl/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,39 @@ def test_close(self):
f.close()
self.assertFalse(f)

def test_link_alphanumeric_order(self):
filename = self.getFileName("alphanumeric_order")
# default order of links is alphanumeric
f = h5py.File(filename, 'w', track_order=False)

f.create_dataset('dset', data=[1, 2, 3])

f['A'] = h5py.SoftLink('/group')
f['C'] = h5py.SoftLink('/dset')
f['B'] = f['dset']

expected_sequence = ["A", "B", "C", "dset"]

for i, item in enumerate(f):
self.assertEqual(item, expected_sequence[i])

# test for links within a subgroup
g = f.create_group('group')
g['Z'] = h5py.SoftLink('/dset')
g['X'] = h5py.SoftLink('/group')
g['Y'] = f['dset']

expected_sequence = ["X", "Y", "Z"]

for i, item in enumerate(g):
self.assertEqual(item, expected_sequence[i])

def test_link_creation_order(self):
filename = self.getFileName("track_order")
f = h5py.File(filename, 'w', track_order=True)

f.create_dataset('dset', data=[1, 2, 3])

# create links in group g2
f['A'] = h5py.SoftLink('/group')
f['C'] = h5py.SoftLink('/dset')
f['B'] = f['dset']
Expand Down

0 comments on commit 8f9e20e

Please sign in to comment.