/usr/lib/python3/dist-packages/h5py/tests/old/test_attrs.py is in python3-h5py 2.6.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | # This file is part of h5py, a Python interface to the HDF5 library.
#
# http://www.h5py.org
#
# Copyright 2008-2013 Andrew Collette and contributors
#
# License: Standard 3-clause BSD; see "license.txt" for full license terms
# and contributor agreement.
"""
Attributes testing module
Covers all operations which access the .attrs property, with the
exception of data read/write and type conversion. Those operations
are tested by module test_attrs_data.
"""
from __future__ import absolute_import
import six
import numpy as np
import collections
from .common import TestCase, ut
from h5py.highlevel import File
from h5py import h5a, h5t
from h5py.highlevel import AttributeManager
class BaseAttrs(TestCase):
def setUp(self):
self.f = File(self.mktemp(), 'w')
def tearDown(self):
if self.f:
self.f.close()
class TestAccess(BaseAttrs):
"""
Feature: Attribute creation/retrieval via special methods
"""
def test_create(self):
""" Attribute creation by direct assignment """
self.f.attrs['a'] = 4.0
self.assertEqual(list(self.f.attrs.keys()), ['a'])
self.assertEqual(self.f.attrs['a'], 4.0)
def test_overwrite(self):
""" Attributes are silently overwritten """
self.f.attrs['a'] = 4.0
self.f.attrs['a'] = 5.0
self.assertEqual(self.f.attrs['a'], 5.0)
def test_rank(self):
""" Attribute rank is preserved """
self.f.attrs['a'] = (4.0, 5.0)
self.assertEqual(self.f.attrs['a'].shape, (2,))
self.assertArrayEqual(self.f.attrs['a'], np.array((4.0,5.0)))
def test_single(self):
""" Attributes of shape (1,) don't become scalars """
self.f.attrs['a'] = np.ones((1,))
out = self.f.attrs['a']
self.assertEqual(out.shape, (1,))
self.assertEqual(out[()], 1)
def test_access_exc(self):
""" Attempt to access missing item raises KeyError """
with self.assertRaises(KeyError):
self.f.attrs['a']
class TestDelete(BaseAttrs):
"""
Feature: Deletion of attributes using __delitem__
"""
def test_delete(self):
""" Deletion via "del" """
self.f.attrs['a'] = 4.0
self.assertIn('a', self.f.attrs)
del self.f.attrs['a']
self.assertNotIn('a', self.f.attrs)
def test_delete_exc(self):
""" Attempt to delete missing item raises KeyError """
with self.assertRaises(KeyError):
del self.f.attrs['a']
class TestUnicode(BaseAttrs):
"""
Feature: Attributes can be accessed via Unicode or byte strings
"""
def test_ascii(self):
""" Access via pure-ASCII byte string """
self.f.attrs[b"ascii"] = 42
out = self.f.attrs[b"ascii"]
self.assertEqual(out, 42)
def test_raw(self):
""" Access via non-ASCII byte string """
name = b"non-ascii\xfe"
self.f.attrs[name] = 42
out = self.f.attrs[name]
self.assertEqual(out, 42)
def test_unicode(self):
""" Access via Unicode string with non-ascii characters """
name = six.u("Omega") + six.unichr(0x03A9)
self.f.attrs[name] = 42
out = self.f.attrs[name]
self.assertEqual(out, 42)
class TestCreate(BaseAttrs):
"""
Options for explicit attribute creation
"""
def test_named(self):
""" Attributes created from named types link to the source type object
"""
self.f['type'] = np.dtype('u8')
self.f.attrs.create('x', 42, dtype=self.f['type'])
self.assertEqual(self.f.attrs['x'], 42)
aid = h5a.open(self.f.id, b'x')
htype = aid.get_type()
htype2 = self.f['type'].id
self.assertEqual(htype, htype2)
self.assertTrue(htype.committed())
class TestMutableMapping(BaseAttrs):
'''Tests if the registration of AttributeManager as a MutableMapping
behaves as expected
'''
def test_resolution(self):
assert issubclass(AttributeManager, collections.MutableMapping)
assert isinstance(self.f.attrs, collections.MutableMapping)
def test_validity(self):
'''
Test that the required functions are implemented.
'''
AttributeManager.__getitem__
AttributeManager.__setitem__
AttributeManager.__delitem__
AttributeManager.__iter__
AttributeManager.__len__
class TestVlen(BaseAttrs):
def test_vlen(self):
a = np.array([np.arange(3), np.arange(4)],
dtype=h5t.special_dtype(vlen=int))
self.f.attrs['a'] = a
self.assertArrayEqual(self.f.attrs['a'][0], a[0])
|