Base (pydent.base)

Aquarium model baseclass

This module contains the base classes for Trident models. Trident models load from JSON and dump to JSON. This is accomplished by adding the @add_schema decorator to classes inherited by the Base class. Using @add_schema dynamically creates a model schema that handles dumping and loading.

Features of Trident models:

load - models can be loaded from JSON data. Hierarchical JSON is loaded intelligently.

Sample.load({"name": "MyPrimer", "sample_type": {"name": "Primer", ...} })
# => <Sample(name="MyPrimer", sample_type=<SampleType(name="Primer")>)>
dump - models can be dumped to JSON. Dependent models and relationships can be

dumped as well.

s.dump(include=("sample_type"))

relationships - models relationships are stored

s = Sample.load(
    {"name": "MyPrimer",
    "sample_type_id": 1}
)

primer_type = s.sample_type
class pydent.base.ModelBase(**data)[source]

Bases: pydent.marshaller.base.SchemaModel

Base class for Aquarium models. Subclass of.

pydent.marshaller.MarshallerBase

  • creates instances from JSON using load

  • contains a reference to the pydent.session.aqsession.AqSession instance that loaded this model

The model initializer.

Parameters

data (dict) – data to add to the model

_check_for_session()[source]

Raises error if model is not connected to a session.

:raises NoSessionError

classmethod _flatten_deserialized_data(models, memo)[source]

Flattens all of the relationships found in the models, returning a rid: model dictionary.

Return type

dict

_get_data()

Return the model’s data.

_get_deserialized_data()

Return the deserialized model data.

property _primary_key

Returns the primary key (e.g. ‘id’) or the rid if id does not exist or is None.

_rid_dict()[source]

Dictionary of all models attached to this model keyed by their rid.

add_data(data)

Initializes fake attributes that correspond to data.

anonymize()[source]

Resets the primary key of the model and assigns a new rid.

Metatypes cannot be annonymized.

Returns

self

append_to_many(name, model)[source]

Appends a model to the many relationship.

Parameters
  • name (str) – name of the relationship or attribute

  • model (ModelBase) – model to append

Returns

None

Return type

None

connect_to_session(session)[source]

Connect model to a session.

Parameters

session (SessionABC) – the AqSession

Returns

None

Raises
copy(keep=None)[source]

Provides a deepcopy of the model, but annonymizes the primary and global keys unless class is a metatype (e.g. OperationType, SampleType, FieldType) or class name is found in list of ‘keep’.

By default, inventory classes such as Sample, Item, and Collection are ‘kept’.

This specific usecase is that when copying whole plans, that the integrity of the inventory used in the operations is maintained. These are the items that refer to physical inventory in the laboratory and are referred to by their rids, and so it is important to that any of these inventory are always maintain their rids. Meaning in the lab, their is only ONE instance of the inventory. In Trident and Aquarium, there is only ONE instance of inventory that is referred to by its rid.

Similarly, any metatype model must also maintain their rid.

Parameters

keep (Optional[bool]) – list of model classes (as a list of strings) to keep un-anonymous

Return type

ModelBase

Returns

copied model

dump(only=None, include=None, ignore=None, include_model_type=False, include_uri=False)[source]

Dump (serialize) the Aquarium model instance to JSON.

Parameters
  • only (Union[str, List[str], Tuple[str], Dict[str, Any], None]) – dump only the provided keys

  • include (Union[str, List[str], Tuple[str], Dict[str, Any], None]) – include the provided nested dump keys

  • ignore (Union[str, List[str], Tuple[str], Dict[str, Any], None]) – ignore the provided keys

  • include_model_type (bool) – if True, include the model class type for each entry using the __model__ key

  • include_uri (bool) – if True, include a URI using the session URL and the tableized model class name using the __uri__ key. If a session is not attached, look for the url namespace in the ModelBase.DEFAULT_NAMESPACE class attribute, as in http://aquarium.org/samples/10.

Return type

dict

Returns

serialized model instance

classmethod find(session, model_id)[source]

Finds a model instance by its model_id.

Return type

ModelBase

find_callback(model_name, model_id)[source]

Finds a model using the model interface and model_id.

Used to find models in model relationships.

Return type

ModelBase

get_deserialized(name)

Get deserialized data by name.

Deprecated since version 0.1.5a7: method will be removed in 0.2. Use self._get_deserialized_data[name] instead.

Parameters

name – name of attribute

Returns

classmethod interface(session)[source]

Creates a model interface from this class and a session.

This method can be overridden in model definitions for special cases.

Return type

Union[QueryInterface, BrowserInterface]

is_deserialized(name)

Check if a given key has already been deserialized.

New in version 0.1.5a7: Method added

Parameters

name (str) – name of the attribute

Return type

bool

Returns

True if key has been deserialized.

classmethod load(*args, **kwargs)[source]

Deserialize the data to a model.

Parameters

data (dict) – data to deserialize

Returns

the deserialized model

Return type

SchemaModel

classmethod load_from(data, owner=None)[source]

Create a new model instance from loaded attributes.

‘obj’ should have a o

Return type

Union[List[ModelBase], ModelBase]

no_getter(*_)[source]

Callback that always returns None.

refresh()[source]

Refresh this model from data from the server.

Returns

self

Return type

self

reload(data)[source]

Reload model attributes from new data.

Parameters

data (dict) – data to update model instance

Returns

model instance

Return type

ModelBase

reset_field(name)

Reset the field descriptor to its placeholder value, returning the behavior.

New in version 0.1.5a7: Method added to reset deserialized attributes

Parameters

name (str) – name of the attribute

Returns

None

property session

The connected session instance.

property uri

Return a URI for this model using the attached session url and tableized model name. For example, http://aquarium.org/samples/. If no session is attached, use the ModelBase.DEFAULT_NAMESPACE key for the url.

Return type

str

Returns

the instance URI

classmethod where(session, params)[source]

Finds a list of models by some parameters.

Return type

Optional[List[ModelBase]]

where_callback(model_name, *args, **kwargs)[source]

Finds models using a model interface and a set of parameters.

Used to find models in model relationships.

Return type

Optional[List[ModelBase]]