def test_attr_dictcompat(self): # Test dictionary interface f = h5py.File(res.get_name(), 'w') values = {'a': 42, 'b': "hello", 'c': -32} self.assertRaises(KeyError, f.attrs.__getitem__, 'foo') self.assert_(not 'foo' in f.attrs) for key, val in values.iteritems(): f.attrs[key] = val self.assert_(key in f.attrs) self.assertEqual(len(values), len(f.attrs)) def assert_cmp_equal(what): c1 = getattr(f.attrs, what) c2 = getattr(values, what) self.assertEqual(set(c1()), set(c2()), what) for w in [ 'keys', 'values', 'items', 'iterkeys', 'itervalues', 'iteritems' ]: assert_cmp_equal(w) self.assertEqual(f.attrs.get('a', 99), 42) self.assertEqual(f.attrs.get('foo', 99), 99) del f.attrs['a'] self.assert_(not 'a' in f.attrs)
def test_attr_dictcompat(self): # Test dictionary interface f = h5py.File(res.get_name(), 'w') values = {'a': 42, 'b': "hello", 'c': -32} self.assertRaises(KeyError, f.attrs.__getitem__, 'foo') self.assert_(not 'foo' in f.attrs) for key, val in values.iteritems(): f.attrs[key] = val self.assert_(key in f.attrs) self.assertEqual(len(values), len(f.attrs)) def assert_cmp_equal(what): c1 = getattr(f.attrs, what) c2 = getattr(values, what) self.assertEqual(set(c1()), set(c2()), what) for w in ['keys', 'values', 'items', 'iterkeys', 'itervalues', 'iteritems']: assert_cmp_equal(w) self.assertEqual(f.attrs.get('a', 99), 42) self.assertEqual(f.attrs.get('foo', 99), 99) del f.attrs['a'] self.assert_(not 'a' in f.attrs)
def test_enum(self): # Test high-level enumerated type vals = {'a': 1, 'b': 2, 'c': 42} f = h5py.File(res.get_name(), 'w') for idx, basetype in enumerate(np.dtype(x) for x in (common.INTS + common.UINTS)): msg = "dset %s, type %s" % (idx, basetype) dt = h5py.special_dtype(enum=(basetype, vals)) self.assertEqual(h5py.check_dtype(enum=dt), vals, msg) self.assert_(h5py.check_dtype(enum=np.dtype('i')) is None, msg) # Test dataset creation refarr = np.zeros((4,4), dtype=dt) ds = f.create_dataset(str(idx), (4,4), dtype=dt) self.assert_(np.all(ds[...] == refarr), msg) # Test conversion to/from plain integer ds[0,0] = np.array(64, dtype=dt) self.assertEqual(ds[0,0], 64, msg)
def test_enum(self): # Test high-level enumerated type vals = {'a': 1, 'b': 2, 'c': 42} f = h5py.File(res.get_name(), 'w') for idx, basetype in enumerate( np.dtype(x) for x in (common.INTS + common.UINTS)): msg = "dset %s, type %s" % (idx, basetype) dt = h5py.special_dtype(enum=(basetype, vals)) self.assertEqual(h5py.check_dtype(enum=dt), vals, msg) self.assert_(h5py.check_dtype(enum=np.dtype('i')) is None, msg) # Test dataset creation refarr = np.zeros((4, 4), dtype=dt) ds = f.create_dataset(str(idx), (4, 4), dtype=dt) self.assert_(np.all(ds[...] == refarr), msg) # Test conversion to/from plain integer ds[0, 0] = np.array(64, dtype=dt) self.assertEqual(ds[0, 0], 64, msg)
def setUp(self): self.f = h5py.File(res.get_name(), "w")
def setUp(self): self.f = h5py.File(res.get_name(), 'w')