test_attribute_create.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # This file is part of h5py, a Python interface to the HDF5 library.
  2. #
  3. # http://www.h5py.org
  4. #
  5. # Copyright 2008-2013 Andrew Collette and contributors
  6. #
  7. # License: Standard 3-clause BSD; see "license.txt" for full license terms
  8. # and contributor agreement.
  9. """
  10. Tests the h5py.AttributeManager.create() method.
  11. """
  12. import numpy as np
  13. from .. import h5t, h5a
  14. from .common import TestCase, make_name
  15. class TestArray(TestCase):
  16. """
  17. Check that top-level array types can be created and read.
  18. """
  19. def test_int(self):
  20. # See issue 498
  21. name = make_name()
  22. dt = np.dtype('(3,)i')
  23. data = np.arange(3, dtype='i')
  24. self.f.attrs.create(name, data=data, dtype=dt)
  25. aid = h5a.open(self.f.id, name.encode('utf-8'))
  26. htype = aid.get_type()
  27. self.assertEqual(htype.get_class(), h5t.ARRAY)
  28. out = self.f.attrs[name]
  29. self.assertArrayEqual(out, data)
  30. def test_string_dtype(self):
  31. # See issue 498 discussion
  32. self.f.attrs.create(make_name(), data=42, dtype='i8')
  33. def test_str(self):
  34. # See issue 1057
  35. name = make_name()
  36. self.f.attrs.create(name, chr(0x03A9))
  37. out = self.f.attrs[name]
  38. self.assertEqual(out, chr(0x03A9))
  39. self.assertIsInstance(out, str)
  40. def test_tuple_of_unicode(self):
  41. # Test that a tuple of unicode strings can be set as an attribute. It will
  42. # be converted to a numpy array of vlen unicode type:
  43. name = make_name()
  44. data = ('a', 'b')
  45. self.f.attrs.create(name, data=data)
  46. result = self.f.attrs[name]
  47. self.assertTrue(all(result == data))
  48. self.assertEqual(result.dtype, np.dtype('O'))
  49. def test_unicode_np_array(self):
  50. # However, a numpy array of type U being passed in will not be
  51. # automatically converted, and should raise an error as it does
  52. # not map to a h5py dtype
  53. data = np.array(['a', 'b'], dtype='U1')
  54. with self.assertRaises(TypeError):
  55. self.f.attrs.create('x', data=data)
  56. def test_shape_scalar(self):
  57. name = make_name()
  58. self.f.attrs.create(name, data=42, shape=1)
  59. result = self.f.attrs[name]
  60. self.assertEqual(result.shape, (1,))
  61. def test_shape_array(self):
  62. name = make_name()
  63. self.f.attrs.create(name, data=np.arange(3), shape=3)
  64. result = self.f.attrs[name]
  65. self.assertEqual(result.shape, (3,))
  66. def test_dtype(self):
  67. dt = np.dtype('(3,)i')
  68. array = np.arange(3, dtype='i')
  69. self.f.attrs.create(make_name(), data=array, dtype=dt)
  70. # Array dtype shape is incompatible with data shape
  71. array = np.arange(4, dtype='i')
  72. with self.assertRaises(ValueError):
  73. self.f.attrs.create('x', data=array, dtype=dt)
  74. # Shape of new attribute conflicts with shape of data
  75. dt = np.dtype('()i')
  76. with self.assertRaises(ValueError):
  77. self.f.attrs.create('x', data=array, shape=(5,), dtype=dt)
  78. def test_key_type(self):
  79. with self.assertRaises(TypeError):
  80. self.f.attrs.create(1, data=('a', 'b'))