def test_issue_212(self): """ Issue 212 Fails with: AttributeError: 'SharedConfig' object has no attribute 'lapl' """ def closer(x): def w(): try: if x: x.close() except IOError: pass return w orig_name = self.mktemp() new_name = self.mktemp() f = File(orig_name, 'w') self.addCleanup(closer(f)) f.create_group('a') f.close() g = File(new_name, 'w') self.addCleanup(closer(g)) g['link'] = ExternalLink(orig_name, '/') # note root group g.close() h = File(new_name, 'r') self.addCleanup(closer(h)) self.assertIsInstance(h['link']['a'], Group)
class TestVisit(TestCase): """ Feature: The .visit and .visititems methods allow iterative access to group and subgroup members """ def setUp(self): self.f = File(self.mktemp(), "w") self.groups = ["grp1", "grp1/sg1", "grp1/sg2", "grp2", "grp2/sg1", "grp2/sg1/ssg1"] for x in self.groups: self.f.create_group(x) def tearDown(self): self.f.close() def test_visit(self): """ All subgroups are visited """ l = [] self.f.visit(l.append) self.assertSameElements(l, self.groups) def test_visititems(self): """ All subgroups and contents are visited """ l = [] comp = [(x, self.f[x]) for x in self.groups] self.f.visititems(lambda x, y: l.append((x, y))) self.assertSameElements(comp, l) def test_bailout(self): """ Returning a non-None value immediately aborts iteration """ x = self.f.visit(lambda x: x) self.assertEqual(x, self.groups[0]) x = self.f.visititems(lambda x, y: (x, y)) self.assertEqual(x, (self.groups[0], self.f[self.groups[0]]))
def test_write_block(self): """ Test that writing to a user block does not destroy the file """ name = self.mktemp() f = File(name, 'w', userblock_size=512) f.create_group("Foobar") f.close() pyfile = open(name, 'r+b') try: pyfile.write(b'X'*512) finally: pyfile.close() f = h5py.File(name, 'r') try: self.assert_("Foobar" in f) finally: f.close() pyfile = open(name, 'rb') try: self.assertEqual(pyfile.read(512), b'X'*512) finally: pyfile.close()
def test_backing(self): """ Core driver saves to file when backing store used """ fname = self.mktemp() fid = File(fname, 'w', driver='core', backing_store=True) fid.create_group('foo') fid.close() fid = File(fname, 'r') self.assert_('foo' in fid) fid.close()
def test_backing(self): """ Core driver saves to file when backing store used """ fname = self.mktemp() fid = File(fname, "w", driver="core", backing_store=True) fid.create_group("foo") fid.close() fid = File(fname, "r") self.assert_("foo" in fid) fid.close()
def test_create(self): """ Mode 'w' opens file in overwrite mode """ fname = self.mktemp() fid = File(fname, 'w') self.assertTrue(fid) fid.create_group('foo') fid.close() fid = File(fname, 'w') self.assertNotIn('foo', fid) fid.close()
def test_readwrite(self): """ Mode 'r+' opens existing file in readwrite mode """ fname = self.mktemp() fid = File(fname, "w") fid.create_group("foo") fid.close() fid = File(fname, "r+") self.assert_("foo" in fid) fid.create_group("bar") self.assert_("bar" in fid) fid.close()
def test_readonly(self): """ Mode 'r' opens file in readonly mode """ fname = self.mktemp() fid = File(fname, 'w') fid.close() self.assert_(not fid) fid = File(fname, 'r') self.assert_(fid) with self.assertRaises(ValueError): fid.create_group('foo') fid.close()
def test_readwrite(self): """ Mode 'r+' opens existing file in readwrite mode """ fname = self.mktemp() fid = File(fname, 'w') fid.create_group('foo') fid.close() fid = File(fname, 'r+') self.assert_('foo' in fid) fid.create_group('bar') self.assert_('bar' in fid) fid.close()
def test_readonly(self): """ Core driver can be used to open existing files """ fname = self.mktemp() fid = File(fname, "w") fid.create_group("foo") fid.close() fid = File(fname, "r", driver="core") self.assert_(fid) self.assert_("foo" in fid) with self.assertRaises(ValueError): fid.create_group("bar") fid.close()
def test_readonly(self): """ Core driver can be used to open existing files """ fname = self.mktemp() fid = File(fname, 'w') fid.create_group('foo') fid.close() fid = File(fname, 'r', driver='core') self.assert_(fid) self.assert_('foo' in fid) with self.assertRaises(ValueError): fid.create_group('bar') fid.close()
class TestExternalLinks(TestCase): """ Feature: Create and manage external links """ def setUp(self): self.f = File(self.mktemp(), "w") self.ename = self.mktemp() self.ef = File(self.ename, "w") self.ef.create_group("external") self.ef.close() def tearDown(self): if self.f: self.f.close() if self.ef: self.ef.close() def test_epath(self): """ External link paths attributes """ el = ExternalLink("foo.hdf5", "/foo") self.assertEqual(el.filename, "foo.hdf5") self.assertEqual(el.path, "/foo") def test_erepr(self): """ External link repr """ el = ExternalLink("foo.hdf5", "/foo") self.assertIsInstance(repr(el), basestring) def test_create(self): """ Creating external links """ self.f["ext"] = ExternalLink(self.ename, "/external") grp = self.f["ext"] self.ef = grp.file self.assertNotEqual(self.ef, self.f) self.assertEqual(grp.name, "/external") def test_exc(self): """ KeyError raised when attempting to open broken link """ self.f["ext"] = ExternalLink(self.ename, "/missing") with self.assertRaises(KeyError): self.f["ext"] # I would prefer IOError but there's no way to fix this as the exception # class is determined by HDF5. def test_exc_missingfile(self): """ KeyError raised when attempting to open missing file """ self.f["ext"] = ExternalLink("mongoose.hdf5", "/foo") with self.assertRaises(KeyError): self.f["ext"]
class BaseMapping(BaseGroup): """ Base class for mapping tests """ def setUp(self): self.f = File(self.mktemp(), 'w') self.groups = ('a','b','c','d') for x in self.groups: self.f.create_group(x) def tearDown(self): if self.f: self.f.close()
class BaseMapping(BaseGroup): """ Base class for mapping tests """ def setUp(self): self.f = File(self.mktemp(), "w") self.groups = ("a", "b", "c", "d") for x in self.groups: self.f.create_group(x) def tearDown(self): if self.f: self.f.close()
class BaseMapping(BaseGroup): """ Base class for mapping tests """ def setUp(self): self.f = File(self.mktemp(), 'w') self.groups = ('a', 'b', 'c', 'd') for x in self.groups: self.f.create_group(x) self.f['x'] = h5py.SoftLink('/mongoose') self.groups = self.groups + ('x', ) def tearDown(self): if self.f: self.f.close()
class BaseMapping(BaseGroup): """ Base class for mapping tests """ def setUp(self): self.f = File(self.mktemp(), 'w') self.groups = ('a','b','c','d') for x in self.groups: self.f.create_group(x) self.f['x'] = h5py.SoftLink('/mongoose') self.groups = self.groups + ('x',) def tearDown(self): if self.f: self.f.close()
class BaseMapping(BaseGroup): """ Base class for mapping tests """ def setUp(self): self.f = File(self.mktemp(), "w") self.groups = ("a", "b", "c", "d") for x in self.groups: self.f.create_group(x) self.f["x"] = h5py.SoftLink("/mongoose") self.groups = self.groups + ("x",) def tearDown(self): if self.f: self.f.close()
def test_readonly_delete_exception(self): """ Deleting object in readonly file raises KeyError """ # Note: it is impossible to restore the old behavior (ValueError) # without breaking the above test (non-existing objects) fname = self.mktemp() hfile = File(fname, 'w') try: hfile.create_group('foo') finally: hfile.close() hfile = File(fname, 'r') try: with self.assertRaises(KeyError): del hfile['foo'] finally: hfile.close()
def test_append(self): """ Mode 'a' opens file in append/readwrite mode, creating if necessary """ fname = self.mktemp() fid = File(fname, 'a') try: self.assert_(fid) fid.create_group('foo') self.assert_('foo' in fid) finally: fid.close() fid = File(fname, 'a') try: self.assert_('foo' in fid) fid.create_group('bar') self.assert_('bar' in fid) finally: fid.close()
def test_mode(self): """ Retrieved File objects have a meaningful mode attribute """ hfile = File(self.mktemp(),'w') try: grp = hfile.create_group('foo') self.assertEqual(grp.file.mode, hfile.mode) finally: hfile.close()
def test_readonly_delete_exception(self): """ Deleting object in readonly file raises KeyError """ # Note: it is impossible to restore the old behavior (ValueError) # without breaking the above test (non-existing objects) fname = self.mktemp() hfile = File(fname,'w') try: hfile.create_group('foo') finally: hfile.close() hfile = File(fname, 'r') try: with self.assertRaises(KeyError): del hfile['foo'] finally: hfile.close()
def test_mode(self): """ Retrieved File objects have a meaningful mode attribute """ hfile = File(self.mktemp(), 'w') try: grp = hfile.create_group('foo') self.assertEqual(grp.file.mode, hfile.mode) finally: hfile.close()
def test_close(self): """ All retrieved File objects are closed at the same time """ fname = self.mktemp() hfile = File(fname, 'w') grp = hfile.create_group('foo') hfile2 = grp.file hfile3 = hfile['/'].file hfile2.close() self.assertFalse(hfile) self.assertFalse(hfile2) self.assertFalse(hfile3)
class TestVisit(TestCase): """ Feature: The .visit and .visititems methods allow iterative access to group and subgroup members """ def setUp(self): self.f = File(self.mktemp(), 'w') self.groups = [ 'grp1', 'grp1/sg1', 'grp1/sg2', 'grp2', 'grp2/sg1', 'grp2/sg1/ssg1' ] for x in self.groups: self.f.create_group(x) def tearDown(self): self.f.close() def test_visit(self): """ All subgroups are visited """ l = [] self.f.visit(l.append) self.assertSameElements(l, self.groups) def test_visititems(self): """ All subgroups and contents are visited """ l = [] comp = [(x, self.f[x]) for x in self.groups] self.f.visititems(lambda x, y: l.append((x,y))) self.assertSameElements(comp, l) def test_bailout(self): """ Returning a non-None value immediately aborts iteration """ x = self.f.visit(lambda x: x) self.assertEqual(x, self.groups[0]) x = self.f.visititems(lambda x, y: (x,y)) self.assertEqual(x, (self.groups[0], self.f[self.groups[0]]))
class TestCopy(TestCase): def setUp(self): self.f1 = File(self.mktemp(), "w") self.f2 = File(self.mktemp(), "w") def tearDown(self): if self.f1: self.f1.close() if self.f2: self.f2.close() def test_copy_path_to_path(self): foo = self.f1.create_group("foo") foo["bar"] = [1, 2, 3] self.f1.copy("foo", "baz") baz = self.f1["baz"] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz["bar"], np.array([1, 2, 3])) def test_copy_path_to_group(self): foo = self.f1.create_group("foo") foo["bar"] = [1, 2, 3] baz = self.f1.create_group("baz") self.f1.copy("foo", baz) baz = self.f1["baz"] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz["foo/bar"], np.array([1, 2, 3])) self.f1.copy("foo", self.f2["/"]) self.assertIsInstance(self.f2["/foo"], Group) self.assertArrayEqual(self.f2["foo/bar"], np.array([1, 2, 3])) def test_copy_group_to_path(self): foo = self.f1.create_group("foo") foo["bar"] = [1, 2, 3] self.f1.copy(foo, "baz") baz = self.f1["baz"] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz["bar"], np.array([1, 2, 3])) self.f2.copy(foo, "foo") self.assertIsInstance(self.f2["/foo"], Group) self.assertArrayEqual(self.f2["foo/bar"], np.array([1, 2, 3])) def test_copy_group_to_group(self): foo = self.f1.create_group("foo") foo["bar"] = [1, 2, 3] baz = self.f1.create_group("baz") self.f1.copy(foo, baz) baz = self.f1["baz"] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz["foo/bar"], np.array([1, 2, 3])) self.f1.copy(foo, self.f2["/"]) self.assertIsInstance(self.f2["/foo"], Group) self.assertArrayEqual(self.f2["foo/bar"], np.array([1, 2, 3])) def test_copy_dataset(self): self.f1["foo"] = [1, 2, 3] foo = self.f1["foo"] self.f1.copy(foo, "bar") self.assertArrayEqual(self.f1["bar"], np.array([1, 2, 3])) self.f1.copy("foo", "baz") self.assertArrayEqual(self.f1["baz"], np.array([1, 2, 3])) self.f1.copy("foo", self.f2) self.assertArrayEqual(self.f2["foo"], np.array([1, 2, 3])) self.f2.copy(self.f1["foo"], self.f2, "bar") self.assertArrayEqual(self.f2["bar"], np.array([1, 2, 3]))
class TestCopy(TestCase): def setUp(self): self.f1 = File(self.mktemp(), 'w') self.f2 = File(self.mktemp(), 'w') def tearDown(self): if self.f1: self.f1.close() if self.f2: self.f2.close() @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_path_to_path(self): foo = self.f1.create_group('foo') foo['bar'] = [1, 2, 3] self.f1.copy('foo', 'baz') baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz['bar'], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_path_to_group(self): foo = self.f1.create_group('foo') foo['bar'] = [1, 2, 3] baz = self.f1.create_group('baz') self.f1.copy('foo', baz) baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz['foo/bar'], np.array([1, 2, 3])) self.f1.copy('foo', self.f2['/']) self.assertIsInstance(self.f2['/foo'], Group) self.assertArrayEqual(self.f2['foo/bar'], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_group_to_path(self): foo = self.f1.create_group('foo') foo['bar'] = [1, 2, 3] self.f1.copy(foo, 'baz') baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz['bar'], np.array([1, 2, 3])) self.f2.copy(foo, 'foo') self.assertIsInstance(self.f2['/foo'], Group) self.assertArrayEqual(self.f2['foo/bar'], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_group_to_group(self): foo = self.f1.create_group('foo') foo['bar'] = [1, 2, 3] baz = self.f1.create_group('baz') self.f1.copy(foo, baz) baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz['foo/bar'], np.array([1, 2, 3])) self.f1.copy(foo, self.f2['/']) self.assertIsInstance(self.f2['/foo'], Group) self.assertArrayEqual(self.f2['foo/bar'], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_dataset(self): self.f1['foo'] = [1, 2, 3] foo = self.f1['foo'] self.f1.copy(foo, 'bar') self.assertArrayEqual(self.f1['bar'], np.array([1, 2, 3])) self.f1.copy('foo', 'baz') self.assertArrayEqual(self.f1['baz'], np.array([1, 2, 3])) self.f1.copy('foo', self.f2) self.assertArrayEqual(self.f2['foo'], np.array([1, 2, 3])) self.f2.copy(self.f1['foo'], self.f2, 'bar') self.assertArrayEqual(self.f2['bar'], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_shallow(self): foo = self.f1.create_group('foo') bar = foo.create_group('bar') foo['qux'] = [1, 2, 3] bar['quux'] = [4, 5, 6] self.f1.copy(foo, 'baz', shallow=True) baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertIsInstance(baz['bar'], Group) self.assertEqual(len(baz['bar']), 0) self.assertArrayEqual(baz['qux'], np.array([1, 2, 3])) self.f2.copy(foo, 'foo', shallow=True) self.assertIsInstance(self.f2['/foo'], Group) self.assertIsInstance(self.f2['foo/bar'], Group) self.assertEqual(len(self.f2['foo/bar']), 0) self.assertArrayEqual(self.f2['foo/qux'], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_without_attributes(self): self.f1['foo'] = [1, 2, 3] foo = self.f1['foo'] foo.attrs['bar'] = [4, 5, 6] self.f1.copy(foo, 'baz', without_attrs=True) self.assertArrayEqual(self.f1['baz'], np.array([1, 2, 3])) self.assert_('bar' not in self.f1['baz'].attrs) self.f2.copy(foo, 'baz', without_attrs=True) self.assertArrayEqual(self.f2['baz'], np.array([1, 2, 3])) self.assert_('bar' not in self.f2['baz'].attrs) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_soft_links(self): self.f1['bar'] = [1, 2, 3] foo = self.f1.create_group('foo') foo['baz'] = SoftLink('/bar') self.f1.copy(foo, 'qux', expand_soft=True) self.f2.copy(foo, 'foo', expand_soft=True) del self.f1['bar'] self.assertIsInstance(self.f1['qux'], Group) self.assertArrayEqual(self.f1['qux/baz'], np.array([1, 2, 3])) self.assertIsInstance(self.f2['/foo'], Group) self.assertArrayEqual(self.f2['foo/baz'], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_external_links(self): filename = self.f1.filename self.f1['foo'] = [1, 2, 3] self.f2['bar'] = ExternalLink(filename, 'foo') self.f1.close() self.f1 = None self.assertArrayEqual(self.f2['bar'], np.array([1, 2, 3])) self.f2.copy('bar', 'baz', expand_external=True) os.unlink(filename) self.assertArrayEqual(self.f2['baz'], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_refs(self): self.f1['foo'] = [1, 2, 3] self.f1['bar'] = [4, 5, 6] foo = self.f1['foo'] bar = self.f1['bar'] foo.attrs['bar'] = bar.ref self.f1.copy(foo, 'baz', expand_refs=True) self.assertArrayEqual(self.f1['baz'], np.array([1, 2, 3])) baz_bar = self.f1['baz'].attrs['bar'] self.assertArrayEqual(self.f1[baz_bar], np.array([4, 5, 6])) # The reference points to a copy of bar, not to bar itself. self.assertNotEqual(self.f1[baz_bar].name, bar.name) self.f1.copy('foo', self.f2, 'baz', expand_refs=True) self.assertArrayEqual(self.f2['baz'], np.array([1, 2, 3])) baz_bar = self.f2['baz'].attrs['bar'] self.assertArrayEqual(self.f2[baz_bar], np.array([4, 5, 6])) self.f1.copy('/', self.f2, 'root', expand_refs=True) self.assertArrayEqual(self.f2['root/foo'], np.array([1, 2, 3])) self.assertArrayEqual(self.f2['root/bar'], np.array([4, 5, 6])) foo_bar = self.f2['root/foo'].attrs['bar'] self.assertArrayEqual(self.f2[foo_bar], np.array([4, 5, 6])) # There's only one copy of bar, which the reference points to. self.assertEqual(self.f2[foo_bar], self.f2['root/bar'])
def test_closed_file(self): """ Trying to modify closed file raises ValueError """ fid = File(self.mktemp(), 'w') fid.close() with self.assertRaises(ValueError): fid.create_group('foo')
class TestAdditionalMappingFuncs(BaseMapping): """ Feature: Other dict methods (pop, pop_item, clear, update, setdefault) are available. """ def setUp(self): self.f = File(self.mktemp(), 'w') for x in ('/test/a','/test/b','/test/c','/test/d'): self.f.create_group(x) self.group = self.f['test'] def tearDown(self): if self.f: self.f.close() def test_pop_item(self): """.pop_item exists and removes item""" key, val = self.group.popitem() self.assertNotIn(key, self.group) def test_pop(self): """.pop exists and removes specified item""" self.group.pop('a') self.assertNotIn('a', self.group) def test_pop_default(self): """.pop falls back to default""" # e shouldn't exist as a group value = self.group.pop('e', None) self.assertEqual(value, None) def test_pop_raises(self): """.pop raises KeyError for non-existence""" # e shouldn't exist as a group with self.assertRaises(KeyError): key = self.group.pop('e') def test_clear(self): """.clear removes groups""" self.group.clear() self.assertEqual(len(self.group), 0) def test_update_dict(self): """.update works with dict""" new_items = {'e': np.array([42])} self.group.update(new_items) self.assertIn('e', self.group) def test_update_iter(self): """.update works with list""" new_items = [ ('e', np.array([42])), ('f', np.array([42])) ] self.group.update(new_items) self.assertIn('e', self.group) def test_update_kwargs(self): """.update works with kwargs""" new_items = {'e': np.array([42])} self.group.update(**new_items) self.assertIn('e', self.group) def test_setdefault(self): """.setdefault gets group if it exists""" value = self.group.setdefault('a') self.assertEqual(value, self.group.get('a')) def test_setdefault_with_default(self): """.setdefault gets default if group doesn't exist""" # e shouldn't exist as a group # 42 used as groups should be strings value = self.group.setdefault('e', np.array([42])) self.assertEqual(value, 42) def test_setdefault_no_default(self): """ .setdefault gets None if group doesn't exist, but as None isn't defined as data for a dataset, this should raise a TypeError. """ # e shouldn't exist as a group with self.assertRaises(TypeError): self.group.setdefault('e')
class TestCopy(TestCase): def setUp(self): self.f1 = File(self.mktemp(), 'w') self.f2 = File(self.mktemp(), 'w') def tearDown(self): if self.f1: self.f1.close() if self.f2: self.f2.close() @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_path_to_path(self): foo = self.f1.create_group('foo') foo['bar'] = [1,2,3] self.f1.copy('foo', 'baz') baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz['bar'], np.array([1,2,3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_path_to_group(self): foo = self.f1.create_group('foo') foo['bar'] = [1,2,3] baz = self.f1.create_group('baz') self.f1.copy('foo', baz) baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz['foo/bar'], np.array([1,2,3])) self.f1.copy('foo', self.f2['/']) self.assertIsInstance(self.f2['/foo'], Group) self.assertArrayEqual(self.f2['foo/bar'], np.array([1,2,3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_group_to_path(self): foo = self.f1.create_group('foo') foo['bar'] = [1,2,3] self.f1.copy(foo, 'baz') baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz['bar'], np.array([1,2,3])) self.f2.copy(foo, 'foo') self.assertIsInstance(self.f2['/foo'], Group) self.assertArrayEqual(self.f2['foo/bar'], np.array([1,2,3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_group_to_group(self): foo = self.f1.create_group('foo') foo['bar'] = [1,2,3] baz = self.f1.create_group('baz') self.f1.copy(foo, baz) baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz['foo/bar'], np.array([1,2,3])) self.f1.copy(foo, self.f2['/']) self.assertIsInstance(self.f2['/foo'], Group) self.assertArrayEqual(self.f2['foo/bar'], np.array([1,2,3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_dataset(self): self.f1['foo'] = [1,2,3] foo = self.f1['foo'] self.f1.copy(foo, 'bar') self.assertArrayEqual(self.f1['bar'], np.array([1,2,3])) self.f1.copy('foo', 'baz') self.assertArrayEqual(self.f1['baz'], np.array([1,2,3])) self.f1.copy('foo', self.f2) self.assertArrayEqual(self.f2['foo'], np.array([1,2,3])) self.f2.copy(self.f1['foo'], self.f2, 'bar') self.assertArrayEqual(self.f2['bar'], np.array([1,2,3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_shallow(self): foo = self.f1.create_group('foo') bar = foo.create_group('bar') foo['qux'] = [1,2,3] bar['quux'] = [4,5,6] self.f1.copy(foo, 'baz', shallow=True) baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertIsInstance(baz['bar'], Group) self.assertEqual(len(baz['bar']), 0) self.assertArrayEqual(baz['qux'], np.array([1,2,3])) self.f2.copy(foo, 'foo', shallow=True) self.assertIsInstance(self.f2['/foo'], Group) self.assertIsInstance(self.f2['foo/bar'], Group) self.assertEqual(len(self.f2['foo/bar']), 0) self.assertArrayEqual(self.f2['foo/qux'], np.array([1,2,3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_without_attributes(self): self.f1['foo'] = [1,2,3] foo = self.f1['foo'] foo.attrs['bar'] = [4,5,6] self.f1.copy(foo, 'baz', without_attrs=True) self.assertArrayEqual(self.f1['baz'], np.array([1,2,3])) self.assert_('bar' not in self.f1['baz'].attrs) self.f2.copy(foo, 'baz', without_attrs=True) self.assertArrayEqual(self.f2['baz'], np.array([1,2,3])) self.assert_('bar' not in self.f2['baz'].attrs) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_soft_links(self): self.f1['bar'] = [1,2,3] foo = self.f1.create_group('foo') foo['baz'] = SoftLink('/bar') self.f1.copy(foo, 'qux', expand_soft=True) self.f2.copy(foo, 'foo', expand_soft=True) del self.f1['bar'] self.assertIsInstance(self.f1['qux'], Group) self.assertArrayEqual(self.f1['qux/baz'], np.array([1,2,3])) self.assertIsInstance(self.f2['/foo'], Group) self.assertArrayEqual(self.f2['foo/baz'], np.array([1,2,3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_external_links(self): filename = self.f1.filename self.f1['foo'] = [1,2,3] self.f2['bar'] = ExternalLink(filename, 'foo') self.f1.close() self.f1 = None self.assertArrayEqual(self.f2['bar'], np.array([1,2,3])) self.f2.copy('bar', 'baz', expand_external=True) os.unlink(filename) self.assertArrayEqual(self.f2['baz'], np.array([1,2,3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_refs(self): self.f1['foo'] = [1,2,3] self.f1['bar'] = [4,5,6] foo = self.f1['foo'] bar = self.f1['bar'] foo.attrs['bar'] = bar.ref self.f1.copy(foo, 'baz', expand_refs=True) self.assertArrayEqual(self.f1['baz'], np.array([1,2,3])) baz_bar = self.f1['baz'].attrs['bar'] self.assertArrayEqual(self.f1[baz_bar], np.array([4,5,6])) # The reference points to a copy of bar, not to bar itself. self.assertNotEqual(self.f1[baz_bar].name, bar.name) self.f1.copy('foo', self.f2, 'baz', expand_refs=True) self.assertArrayEqual(self.f2['baz'], np.array([1,2,3])) baz_bar = self.f2['baz'].attrs['bar'] self.assertArrayEqual(self.f2[baz_bar], np.array([4,5,6])) self.f1.copy('/', self.f2, 'root', expand_refs=True) self.assertArrayEqual(self.f2['root/foo'], np.array([1,2,3])) self.assertArrayEqual(self.f2['root/bar'], np.array([4,5,6])) foo_bar = self.f2['root/foo'].attrs['bar'] self.assertArrayEqual(self.f2[foo_bar], np.array([4,5,6])) # There's only one copy of bar, which the reference points to. self.assertEqual(self.f2[foo_bar], self.f2['root/bar'])
class TestExternalLinks(TestCase): """ Feature: Create and manage external links """ def setUp(self): self.f = File(self.mktemp(), 'w') self.ename = self.mktemp() self.ef = File(self.ename, 'w') self.ef.create_group('external') self.ef.close() def tearDown(self): if self.f: self.f.close() if self.ef: self.ef.close() def test_epath(self): """ External link paths attributes """ el = ExternalLink('foo.hdf5', '/foo') self.assertEqual(el.filename, 'foo.hdf5') self.assertEqual(el.path, '/foo') def test_erepr(self): """ External link repr """ el = ExternalLink('foo.hdf5','/foo') self.assertIsInstance(repr(el), six.string_types) def test_create(self): """ Creating external links """ self.f['ext'] = ExternalLink(self.ename, '/external') grp = self.f['ext'] self.ef = grp.file self.assertNotEqual(self.ef, self.f) self.assertEqual(grp.name, '/external') def test_exc(self): """ KeyError raised when attempting to open broken link """ self.f['ext'] = ExternalLink(self.ename, '/missing') with self.assertRaises(KeyError): self.f['ext'] # I would prefer IOError but there's no way to fix this as the exception # class is determined by HDF5. def test_exc_missingfile(self): """ KeyError raised when attempting to open missing file """ self.f['ext'] = ExternalLink('mongoose.hdf5','/foo') with self.assertRaises(KeyError): self.f['ext'] def test_close_file(self): """ Files opened by accessing external links can be closed Issue 189. """ self.f['ext'] = ExternalLink(self.ename, '/') grp = self.f['ext'] f2 = grp.file f2.close() self.assertFalse(f2) def test_unicode_encode(self): """ Check that external links encode unicode filenames properly Testing issue #732 """ ext_filename = os.path.join(mkdtemp(), "α.hdf5") with File(ext_filename, "w") as ext_file: ext_file.create_group('external') self.f['ext'] = ExternalLink(ext_filename, '/external') def test_unicode_decode(self): """ Check that external links decode unicode filenames properly Testing issue #732 """ ext_filename = os.path.join(mkdtemp(), "α.hdf5") with File(ext_filename, "w") as ext_file: ext_file.create_group('external') ext_file["external"].attrs["ext_attr"] = "test" self.f['ext'] = ExternalLink(ext_filename, '/external') self.assertEqual(self.f["ext"].attrs["ext_attr"], "test")
class TestCopy(TestCase): def setUp(self): self.f1 = File(self.mktemp(), 'w') self.f2 = File(self.mktemp(), 'w') def tearDown(self): if self.f1: self.f1.close() if self.f2: self.f2.close() @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_path_to_path(self): foo = self.f1.create_group('foo') foo['bar'] = [1,2,3] self.f1.copy('foo', 'baz') baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz['bar'], np.array([1,2,3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_path_to_group(self): foo = self.f1.create_group('foo') foo['bar'] = [1,2,3] baz = self.f1.create_group('baz') self.f1.copy('foo', baz) baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz['foo/bar'], np.array([1,2,3])) self.f1.copy('foo', self.f2['/']) self.assertIsInstance(self.f2['/foo'], Group) self.assertArrayEqual(self.f2['foo/bar'], np.array([1,2,3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_group_to_path(self): foo = self.f1.create_group('foo') foo['bar'] = [1,2,3] self.f1.copy(foo, 'baz') baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz['bar'], np.array([1,2,3])) self.f2.copy(foo, 'foo') self.assertIsInstance(self.f2['/foo'], Group) self.assertArrayEqual(self.f2['foo/bar'], np.array([1,2,3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_group_to_group(self): foo = self.f1.create_group('foo') foo['bar'] = [1,2,3] baz = self.f1.create_group('baz') self.f1.copy(foo, baz) baz = self.f1['baz'] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz['foo/bar'], np.array([1,2,3])) self.f1.copy(foo, self.f2['/']) self.assertIsInstance(self.f2['/foo'], Group) self.assertArrayEqual(self.f2['foo/bar'], np.array([1,2,3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1,8,9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_dataset(self): self.f1['foo'] = [1,2,3] foo = self.f1['foo'] self.f1.copy(foo, 'bar') self.assertArrayEqual(self.f1['bar'], np.array([1,2,3])) self.f1.copy('foo', 'baz') self.assertArrayEqual(self.f1['baz'], np.array([1,2,3])) self.f1.copy('foo', self.f2) self.assertArrayEqual(self.f2['foo'], np.array([1,2,3])) self.f2.copy(self.f1['foo'], self.f2, 'bar') self.assertArrayEqual(self.f2['bar'], np.array([1,2,3]))
class TestExternalLinks(TestCase): """ Feature: Create and manage external links """ def setUp(self): self.f = File(self.mktemp(), 'w') self.ename = self.mktemp() self.ef = File(self.ename, 'w') self.ef.create_group('external') self.ef.close() def tearDown(self): if self.f: self.f.close() if self.ef: self.ef.close() def test_epath(self): """ External link paths attributes """ el = ExternalLink('foo.hdf5', '/foo') self.assertEqual(el.filename, 'foo.hdf5') self.assertEqual(el.path, '/foo') def test_erepr(self): """ External link repr """ el = ExternalLink('foo.hdf5','/foo') self.assertIsInstance(repr(el), six.string_types) def test_create(self): """ Creating external links """ self.f['ext'] = ExternalLink(self.ename, '/external') grp = self.f['ext'] self.ef = grp.file self.assertNotEqual(self.ef, self.f) self.assertEqual(grp.name, '/external') def test_exc(self): """ KeyError raised when attempting to open broken link """ self.f['ext'] = ExternalLink(self.ename, '/missing') with self.assertRaises(KeyError): self.f['ext'] # I would prefer IOError but there's no way to fix this as the exception # class is determined by HDF5. def test_exc_missingfile(self): """ KeyError raised when attempting to open missing file """ self.f['ext'] = ExternalLink('mongoose.hdf5','/foo') with self.assertRaises(KeyError): self.f['ext'] def test_close_file(self): """ Files opened by accessing external links can be closed Issue 189. """ self.f['ext'] = ExternalLink(self.ename, '/') grp = self.f['ext'] f2 = grp.file f2.close() self.assertFalse(f2)
class TestExternalLinks(TestCase): """ Feature: Create and manage external links """ def setUp(self): self.f = File(self.mktemp(), 'w') self.ename = self.mktemp() self.ef = File(self.ename, 'w') self.ef.create_group('external') self.ef.close() def tearDown(self): if self.f: self.f.close() if self.ef: self.ef.close() def test_epath(self): """ External link paths attributes """ el = ExternalLink('foo.hdf5', '/foo') self.assertEqual(el.filename, 'foo.hdf5') self.assertEqual(el.path, '/foo') def test_erepr(self): """ External link repr """ el = ExternalLink('foo.hdf5','/foo') self.assertIsInstance(repr(el), basestring) def test_create(self): """ Creating external links """ self.f['ext'] = ExternalLink(self.ename, '/external') grp = self.f['ext'] self.ef = grp.file self.assertNotEqual(self.ef, self.f) self.assertEqual(grp.name, '/external') def test_exc(self): """ KeyError raised when attempting to open broken link """ self.f['ext'] = ExternalLink(self.ename, '/missing') with self.assertRaises(KeyError): self.f['ext'] # I would prefer IOError but there's no way to fix this as the exception # class is determined by HDF5. def test_exc_missingfile(self): """ KeyError raised when attempting to open missing file """ self.f['ext'] = ExternalLink('mongoose.hdf5','/foo') with self.assertRaises(KeyError): self.f['ext'] def test_close_file(self): """ Files opened by accessing external links can be closed Issue 189. """ self.f['ext'] = ExternalLink(self.ename, '/') grp = self.f['ext'] f2 = grp.file f2.close() self.assertFalse(f2)
class TestCopy(TestCase): def setUp(self): self.f1 = File(self.mktemp(), "w") self.f2 = File(self.mktemp(), "w") def tearDown(self): if self.f1: self.f1.close() if self.f2: self.f2.close() @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_path_to_path(self): foo = self.f1.create_group("foo") foo["bar"] = [1, 2, 3] self.f1.copy("foo", "baz") baz = self.f1["baz"] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz["bar"], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_path_to_group(self): foo = self.f1.create_group("foo") foo["bar"] = [1, 2, 3] baz = self.f1.create_group("baz") self.f1.copy("foo", baz) baz = self.f1["baz"] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz["foo/bar"], np.array([1, 2, 3])) self.f1.copy("foo", self.f2["/"]) self.assertIsInstance(self.f2["/foo"], Group) self.assertArrayEqual(self.f2["foo/bar"], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_group_to_path(self): foo = self.f1.create_group("foo") foo["bar"] = [1, 2, 3] self.f1.copy(foo, "baz") baz = self.f1["baz"] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz["bar"], np.array([1, 2, 3])) self.f2.copy(foo, "foo") self.assertIsInstance(self.f2["/foo"], Group) self.assertArrayEqual(self.f2["foo/bar"], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_group_to_group(self): foo = self.f1.create_group("foo") foo["bar"] = [1, 2, 3] baz = self.f1.create_group("baz") self.f1.copy(foo, baz) baz = self.f1["baz"] self.assertIsInstance(baz, Group) self.assertArrayEqual(baz["foo/bar"], np.array([1, 2, 3])) self.f1.copy(foo, self.f2["/"]) self.assertIsInstance(self.f2["/foo"], Group) self.assertArrayEqual(self.f2["foo/bar"], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_dataset(self): self.f1["foo"] = [1, 2, 3] foo = self.f1["foo"] self.f1.copy(foo, "bar") self.assertArrayEqual(self.f1["bar"], np.array([1, 2, 3])) self.f1.copy("foo", "baz") self.assertArrayEqual(self.f1["baz"], np.array([1, 2, 3])) self.f1.copy("foo", self.f2) self.assertArrayEqual(self.f2["foo"], np.array([1, 2, 3])) self.f2.copy(self.f1["foo"], self.f2, "bar") self.assertArrayEqual(self.f2["bar"], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_shallow(self): foo = self.f1.create_group("foo") bar = foo.create_group("bar") foo["qux"] = [1, 2, 3] bar["quux"] = [4, 5, 6] self.f1.copy(foo, "baz", shallow=True) baz = self.f1["baz"] self.assertIsInstance(baz, Group) self.assertIsInstance(baz["bar"], Group) self.assertEqual(len(baz["bar"]), 0) self.assertArrayEqual(baz["qux"], np.array([1, 2, 3])) self.f2.copy(foo, "foo", shallow=True) self.assertIsInstance(self.f2["/foo"], Group) self.assertIsInstance(self.f2["foo/bar"], Group) self.assertEqual(len(self.f2["foo/bar"]), 0) self.assertArrayEqual(self.f2["foo/qux"], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_without_attributes(self): self.f1["foo"] = [1, 2, 3] foo = self.f1["foo"] foo.attrs["bar"] = [4, 5, 6] self.f1.copy(foo, "baz", without_attrs=True) self.assertArrayEqual(self.f1["baz"], np.array([1, 2, 3])) self.assert_("bar" not in self.f1["baz"].attrs) self.f2.copy(foo, "baz", without_attrs=True) self.assertArrayEqual(self.f2["baz"], np.array([1, 2, 3])) self.assert_("bar" not in self.f2["baz"].attrs) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_soft_links(self): self.f1["bar"] = [1, 2, 3] foo = self.f1.create_group("foo") foo["baz"] = SoftLink("/bar") self.f1.copy(foo, "qux", expand_soft=True) self.f2.copy(foo, "foo", expand_soft=True) del self.f1["bar"] self.assertIsInstance(self.f1["qux"], Group) self.assertArrayEqual(self.f1["qux/baz"], np.array([1, 2, 3])) self.assertIsInstance(self.f2["/foo"], Group) self.assertArrayEqual(self.f2["foo/baz"], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_external_links(self): filename = self.f1.filename self.f1["foo"] = [1, 2, 3] self.f2["bar"] = ExternalLink(filename, "foo") self.f1.close() self.f1 = None self.assertArrayEqual(self.f2["bar"], np.array([1, 2, 3])) self.f2.copy("bar", "baz", expand_external=True) os.unlink(filename) self.assertArrayEqual(self.f2["baz"], np.array([1, 2, 3])) @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9), "Bug in HDF5<1.8.8 prevents copying open dataset") def test_copy_refs(self): self.f1["foo"] = [1, 2, 3] self.f1["bar"] = [4, 5, 6] foo = self.f1["foo"] bar = self.f1["bar"] foo.attrs["bar"] = bar.ref self.f1.copy(foo, "baz", expand_refs=True) self.assertArrayEqual(self.f1["baz"], np.array([1, 2, 3])) baz_bar = self.f1["baz"].attrs["bar"] self.assertArrayEqual(self.f1[baz_bar], np.array([4, 5, 6])) # The reference points to a copy of bar, not to bar itself. self.assertNotEqual(self.f1[baz_bar].name, bar.name) self.f1.copy("foo", self.f2, "baz", expand_refs=True) self.assertArrayEqual(self.f2["baz"], np.array([1, 2, 3])) baz_bar = self.f2["baz"].attrs["bar"] self.assertArrayEqual(self.f2[baz_bar], np.array([4, 5, 6])) self.f1.copy("/", self.f2, "root", expand_refs=True) self.assertArrayEqual(self.f2["root/foo"], np.array([1, 2, 3])) self.assertArrayEqual(self.f2["root/bar"], np.array([4, 5, 6])) foo_bar = self.f2["root/foo"].attrs["bar"] self.assertArrayEqual(self.f2[foo_bar], np.array([4, 5, 6])) # There's only one copy of bar, which the reference points to. self.assertEqual(self.f2[foo_bar], self.f2["root/bar"])
class TestExternalLinks(TestCase): """ Feature: Create and manage external links """ def setUp(self): self.f = File(self.mktemp(), 'w') self.ename = self.mktemp() self.ef = File(self.ename, 'w') self.ef.create_group('external') self.ef.close() def tearDown(self): if self.f: self.f.close() if self.ef: self.ef.close() def test_epath(self): """ External link paths attributes """ el = ExternalLink('foo.hdf5', '/foo') self.assertEqual(el.filename, 'foo.hdf5') self.assertEqual(el.path, '/foo') def test_erepr(self): """ External link repr """ el = ExternalLink('foo.hdf5', '/foo') self.assertIsInstance(repr(el), six.string_types) def test_create(self): """ Creating external links """ self.f['ext'] = ExternalLink(self.ename, '/external') grp = self.f['ext'] self.ef = grp.file self.assertNotEqual(self.ef, self.f) self.assertEqual(grp.name, '/external') def test_exc(self): """ KeyError raised when attempting to open broken link """ self.f['ext'] = ExternalLink(self.ename, '/missing') with self.assertRaises(KeyError): self.f['ext'] # I would prefer IOError but there's no way to fix this as the exception # class is determined by HDF5. def test_exc_missingfile(self): """ KeyError raised when attempting to open missing file """ self.f['ext'] = ExternalLink('mongoose.hdf5', '/foo') with self.assertRaises(KeyError): self.f['ext'] def test_close_file(self): """ Files opened by accessing external links can be closed Issue 189. """ self.f['ext'] = ExternalLink(self.ename, '/') grp = self.f['ext'] f2 = grp.file f2.close() self.assertFalse(f2) @ut.skipIf(NO_FS_UNICODE, "No unicode filename support") def test_unicode_encode(self): """ Check that external links encode unicode filenames properly Testing issue #732 """ ext_filename = os.path.join(mkdtemp(), u"α.hdf5") with File(ext_filename, "w") as ext_file: ext_file.create_group('external') self.f['ext'] = ExternalLink(ext_filename, '/external') @ut.skipIf(NO_FS_UNICODE, "No unicode filename support") def test_unicode_decode(self): """ Check that external links decode unicode filenames properly Testing issue #732 """ ext_filename = os.path.join(mkdtemp(), u"α.hdf5") with File(ext_filename, "w") as ext_file: ext_file.create_group('external') ext_file["external"].attrs["ext_attr"] = "code" self.f['ext'] = ExternalLink(ext_filename, '/external') self.assertEqual(self.f["ext"].attrs["ext_attr"], "code") def test_unicode_hdf5_path(self): """ Check that external links handle unicode hdf5 paths properly Testing issue #333 """ ext_filename = os.path.join(mkdtemp(), "external.hdf5") with File(ext_filename, "w") as ext_file: ext_file.create_group(u'α') ext_file[u"α"].attrs["ext_attr"] = "code" self.f['ext'] = ExternalLink(ext_filename, u'/α') self.assertEqual(self.f["ext"].attrs["ext_attr"], "code")
class TestAdditionalMappingFuncs(BaseMapping): """ Feature: Other dict methods (pop, pop_item, clear, update, setdefault) are available. """ def setUp(self): self.f = File(self.mktemp(), 'w') for x in ('/test/a', '/test/b', '/test/c', '/test/d'): self.f.create_group(x) self.group = self.f['test'] def tearDown(self): if self.f: self.f.close() def test_pop_item(self): """.pop_item exists and removes item""" key, val = self.group.popitem() self.assertNotIn(key, self.group) def test_pop(self): """.pop exists and removes specified item""" self.group.pop('a') self.assertNotIn('a', self.group) def test_pop_default(self): """.pop falls back to default""" # e shouldn't exist as a group value = self.group.pop('e', None) self.assertEqual(value, None) def test_pop_raises(self): """.pop raises KeyError for non-existence""" # e shouldn't exist as a group with self.assertRaises(KeyError): key = self.group.pop('e') def test_clear(self): """.clear removes groups""" self.group.clear() self.assertEqual(len(self.group), 0) def test_update_dict(self): """.update works with dict""" new_items = {'e': np.array([42])} self.group.update(new_items) self.assertIn('e', self.group) def test_update_iter(self): """.update works with list""" new_items = [('e', np.array([42])), ('f', np.array([42]))] self.group.update(new_items) self.assertIn('e', self.group) def test_update_kwargs(self): """.update works with kwargs""" new_items = {'e': np.array([42])} self.group.update(**new_items) self.assertIn('e', self.group) def test_setdefault(self): """.setdefault gets group if it exists""" value = self.group.setdefault('a') self.assertEqual(value, self.group.get('a')) def test_setdefault_with_default(self): """.setdefault gets default if group doesn't exist""" # e shouldn't exist as a group # 42 used as groups should be strings value = self.group.setdefault('e', np.array([42])) self.assertEqual(value, 42) def test_setdefault_no_default(self): """ .setdefault gets None if group doesn't exist, but as None isn't defined as data for a dataset, this should raise a TypeError. """ # e shouldn't exist as a group with self.assertRaises(TypeError): self.group.setdefault('e')