Making data objectsΒΆ

To make a new object type like Neuron or PyOpenWorm.worm.Worm, for the most part, you just need to make a Python class.

Say, for example, that I want to record some information about drug reactions in C. elegans. I make Drug and Experiment classes to describe C. elegans reactions:

>>> from PyOpenWorm.dataObject import (DataObject,
...                                    DatatypeProperty,
...                                    ObjectProperty,
...                                    Alias)
>>> from PyOpenWorm.worm import Worm
>>> from PyOpenWorm.evidence import Evidence
>>> from PyOpenWorm.document import Document
>>> from PyOpenWorm.context import Context
>>> from PyOpenWorm.mapper import Mapper
>>> from PyOpenWorm import connect, ModuleRecorder

>>> class Drug(DataObject):
...     name = DatatypeProperty()
...     drug_name = Alias(name)
...     def identifier_augment(self):
...         return self.make_identifier_direct(
...     def defined_augment(self):
...         return

>>> class Experiment(DataObject):
...     drug = ObjectProperty(value_type=Drug)
...     subject = ObjectProperty(value_type=Worm)
...     route_of_entry = DatatypeProperty()
...     reaction = DatatypeProperty()

# Do some accounting stuff to register the classes. Usually happens behind
# the scenes.
>>> m = Mapper(('PyOpenWorm.dataObject.DataObject',))
>>> ModuleRecorder.add_listener(m)
>>> m.process_classes(Drug, Experiment)

So, we have created I can then make a Drug object for moon rocks and describe an experiment by Aperture Labs:

>>> ctx = Context(ident='', mapper=m)
>>> d = ctx(Drug)(name='moon rocks')
>>> e = ctx(Experiment)(key='experiment001')
>>> w = ctx(Worm)('C. elegans')
>>> e.subject(w)

>>> e.drug(d)

>>> e.route_of_entry('ingestion')

>>> e.reaction('no reaction')

>>> ev = Evidence(key='labresults', reference=Document(author="Aperture Labs"))
>>> ev.supports(ctx)

and save those statements:


For simple objects, this is all we have to do.

You can also add properties to an object after it has been created by calling either ObjectProperty or DatatypeProperty on the class:

>>> d = ctx(Drug)(name='moon rocks')
>>> Drug.DatatypeProperty('granularity', owner=d)

>>> d.granularity('ground up')

>>> do = Drug()

Properties added in this fashion will not propagate to any other objects:

>>> do.granularity
Traceback (most recent call last):
AttributeError: 'Drug' object has no attribute 'granularity'

They will, however, be saved along with the object they are attached to.