Attributes¶
Groups and datasets can have small bits of named information attached to them. This is the official way to store metadata in HDF5. Each of these objects has a small proxy object (AttributeManager) attached to it as <obj>.attrs. Attributes have the following properties:
- They may be created from any scalar or NumPy array
- Each attribute must be small (recommended < 64k for HDF5 1.6)
- There is no partial I/O (i.e. slicing); the entire attribute must be read.
They support the same dictionary API as groups.
Reference¶
- class h5py.AttributeManager(parent)¶
Allows dictionary-style access to an HDF5 object’s attributes.
These are created exclusively by the library and are available as a Python attribute at <object>.attrs
Like the members of groups, attributes provide a minimal dictionary- style interface. Anything which can be reasonably converted to a Numpy array or Numpy scalar can be stored.
Attributes are automatically created on assignment with the syntax <obj>.attrs[name] = value, with the HDF5 type automatically deduced from the value. Existing attributes are overwritten.
To modify an existing attribute while preserving its type, use the method modify(). To specify an attribute of a particular type and shape (or to create an empty attribute), use create().
- __getitem__(name)¶
- Read the value of an attribute.
- __setitem__(name, value)¶
Set a new attribute, overwriting any existing attribute.
The type and shape of the attribute are determined from the data. To use a specific type or shape, or to preserve the type of an attribute, use the methods create() and modify().
Broadcasting isn’t supported for attributes.
- __delitem__(name)¶
- Delete an attribute (which must already exist).
- create(name, data, shape=None, dtype=None)¶
Create a new attribute, overwriting any existing attribute.
- name
- Name of the new attribute (required)
- data
- An array to initialize the attribute (required)
- shape
- Shape of the attribute. Overrides data.shape if both are given. The total number of points must be unchanged.
- dtype
- Data type of the attribute. Overrides data.dtype if both are given. Must be conversion-compatible with data.dtype.
- modify(name, value)¶
Change the value of an attribute while preserving its type.
Differs from __setitem__ in that the type of an existing attribute is preserved. Useful for interacting with externally generated files.
If the attribute doesn’t exist, it will be automatically created.
Inherited dictionary interface
- _DictCompat.keys()¶
- Get a list containing member names
- _DictCompat.values()¶
- Get a list containing member objects
- _DictCompat.items()¶
- Get a list of tuples containing (name, object) pairs
- _DictCompat.iterkeys()¶
- Get an iterator over member names
- _DictCompat.itervalues()¶
- Get an iterator over member objects
- _DictCompat.iteritems()¶
- Get an iterator over (name, object) pairs
- _DictCompat.get(name, default=None)¶
- Retrieve the member, or return default if it doesn’t exist