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 should be small (generally < 64k)
  • 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 Group objects, 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, 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().

__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, in which case the total number of points must be unchanged.
dtype
Data type of the attribute. Overrides data.dtype if both are given.
modify(name, value)

Change the value of an attribute while preserving its type.

Differs from __setitem__ in that if the attribute already exists, its type is preserved. This can be very 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

Table Of Contents

Previous topic

Datasets

Next topic

Dimension Scales

This Page