| 1 |
Building API Classes |
| 2 |
==================== |
| 3 |
|
| 4 |
After you have written an extension, you will need a Pythonic way to interact with the data model. To do this, |
| 5 |
you will need to write some classes that represent the data you defined in your specification extensions. |
| 6 |
|
| 7 |
The :py:mod:`hdmf.container` module defines two base classes that represent the primitive structures supported by |
| 8 |
the schema. :py:class:`~hdmf.container.Data` represents datasets and :py:class:`~hdmf.container.Container` |
| 9 |
represents groups. See the classes in the `:py:mod:hdmf.common` package for examples. |
| 10 |
|
| 11 |
The register_class function/decorator |
| 12 |
------------------------------------- |
| 13 |
|
| 14 |
When defining a class that represents a *data_type* (i.e. anything that has a *data_type_def*) |
| 15 |
from your extension, you can tell HDMF which *data_type* it represents using the function |
| 16 |
:py:func:`~hdmf.common.register_class`. This class can be called on its own, or used as a class decorator. The |
| 17 |
first argument should be the *data_type* and the second argument should be the *namespace* name. |
| 18 |
|
| 19 |
The following example demonstrates how to register a class as the Python class representation of the |
| 20 |
*data_type* "MyContainer" from the *namespace* "my_ns". The namespace must be loaded prior to the below code using |
| 21 |
the :py:func:`~hdmf.common.load_namespaces` function. |
| 22 |
|
| 23 |
.. code-block:: python |
| 24 |
|
| 25 |
from hdmf.common import register_class |
| 26 |
from hdmf.container import Container |
| 27 |
|
| 28 |
class MyContainer(Container): |
| 29 |
... |
| 30 |
|
| 31 |
register_class(data_type='MyContainer', namespace='my_ns', container_cls=MyContainer) |
| 32 |
|
| 33 |
|
| 34 |
Alternatively, you can use :py:func:`~hdmf.common.register_class` as a decorator. |
| 35 |
|
| 36 |
.. code-block:: python |
| 37 |
|
| 38 |
from hdmf.common import register_class |
| 39 |
from hdmf.container import Container |
| 40 |
|
| 41 |
@type_map.register_class('MyContainer', 'my_ns') |
| 42 |
class MyContainer(Container): |
| 43 |
... |
| 44 |
|
| 45 |
:py:func:`~hdmf.common.register_class` is used with :py:class:`~hdmf.container.Data` the same way it is used with |
| 46 |
:py:class:`~hdmf.container.Container`. |