| 1 |
""" |
| 2 |
MultiContainerInterface |
| 3 |
======================== |
| 4 |
|
| 5 |
This is a guide to creating custom API classes with the ``MultiContainerInterface`` class. |
| 6 |
|
| 7 |
""" |
| 8 |
|
| 9 |
############################################################################### |
| 10 |
# Introduction |
| 11 |
# ------------ |
| 12 |
# The :py:class:`~hdmf.container.MultiContainerInterface` class provides an easy |
| 13 |
# and convenient way to create standard methods for a container class that contains |
| 14 |
# a collection of containers of a specified type. For |
| 15 |
# example, let's say you want to define a class ``MyContainerHolder`` that |
| 16 |
# contains a collection of ``MyContainer`` objects. By having ``MyContainerHolder`` |
| 17 |
# extend :py:class:`~hdmf.container.MultiContainerInterface` |
| 18 |
# and specifying certain configuration settings |
| 19 |
# in the class, your ``MyContainerHolder`` class would be generated with: |
| 20 |
# |
| 21 |
# 1. an attribute for a labelled dictionary that holds ``MyContainer`` objects |
| 22 |
# 2. an ``__init__`` method to initialize ``MyContainerHolder`` with a collection of |
| 23 |
# ``MyContainer`` objects |
| 24 |
# 3. a method to add ``MyContainer`` objects to the dictionary |
| 25 |
# 4. access of items from the dictionary using ``__getitem__`` (square bracket notation) |
| 26 |
# 5. a method to get ``MyContainer`` objects from the dictionary (optional) |
| 27 |
# 6. a method to create ``MyContainer`` objects and add them to the dictionary (optional) |
| 28 |
# |
| 29 |
|
| 30 |
############################################################################### |
| 31 |
# Specifying the class configuration |
| 32 |
# ---------------------------------- |
| 33 |
# To specify the class configuration for a |
| 34 |
# :py:class:`~hdmf.container.MultiContainerInterface` subclass, define the variable |
| 35 |
# ``__clsconf__`` in the new class. ``__clsconf__`` should be set to a dictionary |
| 36 |
# with three required keys, ``'attr'``, ``'type'``, and ``'add'``. |
| 37 |
# |
| 38 |
# The ``'attr'`` key should map to a string value that is the name of the attribute that |
| 39 |
# will be created to hold the collection of container objects. |
| 40 |
# |
| 41 |
# The ``'type'`` key should map to a type or a tuple of types that says what objects |
| 42 |
# are allowed in this collection. |
| 43 |
# |
| 44 |
# The ``'add'`` key should map to a string value that is the name of the |
| 45 |
# method to be generated that allows users to add a container to the collection. |
| 46 |
|
| 47 |
# sphinx_gallery_thumbnail_path = 'figures/gallery_thumbnail_multicontainerinterface.png' |
| 48 |
from hdmf.container import Container, MultiContainerInterface |
| 49 |
|
| 50 |
|
| 51 |
class ContainerHolder(MultiContainerInterface): |
| 52 |
|
| 53 |
__clsconf__ = { |
| 54 |
'attr': 'containers', |
| 55 |
'type': Container, |
| 56 |
'add': 'add_container', |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
############################################################################### |
| 61 |
# The above code will generate: |
| 62 |
# |
| 63 |
# 1. the attribute ``containers`` as a :py:class:`~hdmf.utils.LabelledDict` that |
| 64 |
# holds ``Container`` objects |
| 65 |
# 2. the ``__init__`` method which accepts a collection of ``Container`` objects |
| 66 |
# 3. the ``add_container`` method that allows users to add ``Container`` objects |
| 67 |
# to the ``containers`` dictionary. |
| 68 |
# |
| 69 |
# Here is an example of instantiating the new ``ContainerHolder`` class and |
| 70 |
# using the generated add method. |
| 71 |
|
| 72 |
obj1 = Container('obj1') |
| 73 |
obj2 = Container('obj2') |
| 74 |
holder1 = ContainerHolder() |
| 75 |
holder1.add_container(obj1) |
| 76 |
holder1.add_container(obj2) |
| 77 |
holder1.containers # this is a LabelledDict where the keys are the name of the container |
| 78 |
# i.e., {'obj1': obj1, 'obj2': obj2} |
| 79 |
|
| 80 |
############################################################################### |
| 81 |
# Constructor options |
| 82 |
# ---------------------------------- |
| 83 |
# The constructor accepts a dict/list/tuple of ``Container`` objects, a single |
| 84 |
# ``Container`` object, or None. If a dict is passed, only the dict values are used. |
| 85 |
# You can specify the argument as a keyword argument with the attribute name as |
| 86 |
# the keyword argument key. |
| 87 |
|
| 88 |
holder2 = ContainerHolder(obj1) |
| 89 |
holder3 = ContainerHolder([obj1, obj2]) |
| 90 |
holder4 = ContainerHolder({'unused_key1': obj1, 'unused_key2': obj2}) |
| 91 |
holder5 = ContainerHolder(containers=obj1) |
| 92 |
|
| 93 |
############################################################################### |
| 94 |
# By default, the new class has the 'name' attribute set to the name of the class, |
| 95 |
# but a user-specified name can be provided in the constructor. |
| 96 |
|
| 97 |
named_holder = ContainerHolder(name='My Holder') |
| 98 |
|
| 99 |
############################################################################### |
| 100 |
# Adding containers to the collection |
| 101 |
# ----------------------------------- |
| 102 |
# Similar to the constructor, the generated add method accepts a dict/list/tuple |
| 103 |
# of ``Container`` objects or a single ``Container`` object. If a dict is passed, |
| 104 |
# only the dict values are used. |
| 105 |
|
| 106 |
holder6 = ContainerHolder() |
| 107 |
holder6.add_container(obj1) |
| 108 |
|
| 109 |
holder7 = ContainerHolder() |
| 110 |
holder7.add_container([obj1, obj2]) |
| 111 |
|
| 112 |
holder8 = ContainerHolder() |
| 113 |
holder8.add_container({'unused_key1': obj1, 'unused_key2': obj2}) |
| 114 |
|
| 115 |
holder9 = ContainerHolder() |
| 116 |
holder9.add_container(containers=obj1) |
| 117 |
|
| 118 |
############################################################################### |
| 119 |
# Getting items from the collection |
| 120 |
# ----------------------------------- |
| 121 |
# You can access a container in the collection by using the name of the |
| 122 |
# container within square brackets. As a convenience, if there is |
| 123 |
# only one item in the collection, you can use None within square brackets. |
| 124 |
|
| 125 |
holder10 = ContainerHolder(obj1) |
| 126 |
holder10['obj1'] |
| 127 |
holder10[None] |
| 128 |
|
| 129 |
############################################################################### |
| 130 |
# Getting items from the collection using a custom getter |
| 131 |
# -------------------------------------------------------- |
| 132 |
# You can use the ``'get'`` key in ``__clsconf__`` to generate a getter method as |
| 133 |
# an alternative to using the square bracket notation for accessing items from |
| 134 |
# the collection. Like the square bracket notation, if there is only one item |
| 135 |
# in the collection, you can omit the name or pass None to the getter method. |
| 136 |
# |
| 137 |
# The ``'get'`` key should map to a string value that is the name of the getter |
| 138 |
# method to be generated. The ``'get'`` key in ``__clsconf__`` is optional. |
| 139 |
|
| 140 |
|
| 141 |
class ContainerHolderWithGet(MultiContainerInterface): |
| 142 |
|
| 143 |
__clsconf__ = { |
| 144 |
'attr': 'containers', |
| 145 |
'type': Container, |
| 146 |
'add': 'add_container', |
| 147 |
'get': 'get_container', |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
holder11 = ContainerHolderWithGet(obj1) |
| 152 |
holder11.get_container('obj1') |
| 153 |
holder11.get_container() |
| 154 |
|
| 155 |
############################################################################### |
| 156 |
# Creating and adding items to the collection using a custom create method |
| 157 |
# ------------------------------------------------------------------------ |
| 158 |
# You can use the ``'create'`` key in ``__clsconf__`` to generate a create method |
| 159 |
# as a convenience method so that users do not need to initialize the |
| 160 |
# ``Container`` object and then add it to the collection. Those two steps are |
| 161 |
# combined into one line. The arguments to the custom create method are the |
| 162 |
# same as the arguments to the Container's ``__init__`` method, but the |
| 163 |
# ``__init__`` method `must` be defined using :py:func:`~hdmf.utils.docval`. |
| 164 |
# The created object will be returned by the create method. |
| 165 |
# |
| 166 |
# The ``'create'`` key should map to a string value that is the name of the create |
| 167 |
# method to be generated. The ``'create'`` key in ``__clsconf__`` is optional. |
| 168 |
|
| 169 |
|
| 170 |
class ContainerHolderWithCreate(MultiContainerInterface): |
| 171 |
|
| 172 |
__clsconf__ = { |
| 173 |
'attr': 'containers', |
| 174 |
'type': Container, |
| 175 |
'add': 'add_container', |
| 176 |
'create': 'create_container', |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
holder12 = ContainerHolderWithCreate() |
| 181 |
holder12.create_container('obj1') |
| 182 |
|
| 183 |
############################################################################### |
| 184 |
# Specifying multiple types allowed in the collection |
| 185 |
# ------------------------------------------------------------------------ |
| 186 |
# The ``'type'`` key in ``__clsconf__`` allows specifying a single type or a |
| 187 |
# list/tuple of types. |
| 188 |
# |
| 189 |
# You cannot specify the ``'create'`` key in ``__clsconf__`` |
| 190 |
# when multiple types are allowed in the collection because it cannot be |
| 191 |
# determined which type to initialize. |
| 192 |
|
| 193 |
from hdmf.container import Data |
| 194 |
|
| 195 |
|
| 196 |
class ContainerHolderWithMultipleTypes(MultiContainerInterface): |
| 197 |
|
| 198 |
__clsconf__ = { |
| 199 |
'attr': 'things', |
| 200 |
'type': (Container, Data), |
| 201 |
'add': 'add_thing', |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
############################################################################### |
| 206 |
# Specifying multiple collections |
| 207 |
# ------------------------------------------------------------------------ |
| 208 |
# You can support multiple collections in your |
| 209 |
# :py:class:`~hdmf.container.MultiContainerInterface` |
| 210 |
# subclass by setting the ``__clsconf__`` variable to a list of dicts instead |
| 211 |
# of a single dict. |
| 212 |
# |
| 213 |
# When specifying multiple collections, square bracket notation access of items |
| 214 |
# (i.e., calling ``__getitem__``) is not supported, because it is not clear |
| 215 |
# which collection to get the desired item from. |
| 216 |
|
| 217 |
from hdmf.container import Data |
| 218 |
|
| 219 |
|
| 220 |
class MultiCollectionHolder(MultiContainerInterface): |
| 221 |
|
| 222 |
__clsconf__ = [ |
| 223 |
{ |
| 224 |
'attr': 'containers', |
| 225 |
'type': Container, |
| 226 |
'add': 'add_container', |
| 227 |
}, |
| 228 |
{ |
| 229 |
'attr': 'data', |
| 230 |
'type': Data, |
| 231 |
'add': 'add_data', |
| 232 |
}, |
| 233 |
] |
| 234 |
|
| 235 |
|
| 236 |
############################################################################### |
| 237 |
# Managing container parents |
| 238 |
# ------------------------------------------------------------------------ |
| 239 |
# If the parent of the container being added is not already set, then the parent |
| 240 |
# will be set to the containing object. |
| 241 |
|
| 242 |
obj3 = Container('obj3') |
| 243 |
holder13 = ContainerHolder(obj3) |
| 244 |
obj3.parent # this is holder13 |
| 245 |
|
| 246 |
############################################################################### |
| 247 |
# :py:class:`~hdmf.utils.LabelledDict` objects support removal of an item using |
| 248 |
# the del operator or the :py:meth:`~hdmf.utils.LabelledDict.pop` |
| 249 |
# method. If the parent of the container being removed is the containing object, |
| 250 |
# then its parent will be reset to None. |
| 251 |
|
| 252 |
del holder13.containers['obj3'] |
| 253 |
obj3.parent # this is back to None |
| 254 |
|
| 255 |
############################################################################### |
| 256 |
# Using a custom constructor |
| 257 |
# ------------------------------------------------------------------------ |
| 258 |
# You can override the automatically generated constructor for your |
| 259 |
# :py:class:`~hdmf.container.MultiContainerInterface` subclass. |
| 260 |
|
| 261 |
|
| 262 |
class ContainerHolderWithCustomInit(MultiContainerInterface): |
| 263 |
|
| 264 |
__clsconf__ = { |
| 265 |
'attr': 'containers', |
| 266 |
'type': Container, |
| 267 |
'add': 'add_container', |
| 268 |
} |
| 269 |
|
| 270 |
def __init__(self, name, my_containers): |
| 271 |
super().__init__(name=name) |
| 272 |
self.containers = my_containers |
| 273 |
self.add_container(Container('extra_container')) |
| 274 |
|
| 275 |
|
| 276 |
holder14 = ContainerHolderWithCustomInit('my_name', [obj1, obj2]) |
| 277 |
holder14.containers # contains the 'extra_container' container |