diff --git a/docs/config.rst b/docs/config.rst index 97d4db9..9eb188b 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -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 diff --git a/test/hl/test_attribute.py b/test/hl/test_attribute.py index a0388fe..c6fc5ee 100644 --- a/test/hl/test_attribute.py +++ b/test/hl/test_attribute.py @@ -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 diff --git a/test/hl/test_file.py b/test/hl/test_file.py index c76d915..0bceaee 100644 --- a/test/hl/test_file.py +++ b/test/hl/test_file.py @@ -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']