pydent.models.Sample

class pydent.models.Sample(name=None, project=None, description=None, sample_type=None, sample_type_id=None, properties=None, field_values=None)[source]

Bases: pydent.models.field_value_mixins.FieldValueInterface, pydent.base.ModelBase

A Sample model.

Parameters
  • name

  • project

  • description

  • sample_type_id

  • properties

__init__(name=None, project=None, description=None, sample_type=None, sample_type_id=None, properties=None, field_values=None)[source]
Parameters
  • name

  • project

  • description

  • sample_type_id

  • properties

Methods

__init__([name, project, description, …])

param name

add_data(data)

Initializes fake attributes that correspond to data.

anonymize()

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

append_to_many(name, model)

Appends a model to the many relationship.

available_items([object_type_name, …])

connect_to_session(session)

Connect model to a session.

copy()

Return a copy of this sample.

create()

Creates this sample in the Aquarium instance if the sample has all fields required by the sample type.

create_interface()

rtype

Union[QueryInterface, BrowserInterface]

dump([only, include, ignore, …])

Dump (serialize) the Aquarium model instance to JSON.

field_value(name)

Returns the FieldValue associated with the sample by its name.

field_value_array(name)

find(session, model_id)

Finds a model instance by its model_id.

find_callback(model_name, model_id)

Finds a model using the model interface and model_id.

get_deserialized(name)

Get deserialized data by name.

get_field_type(name, role)

Returns a pydent.models.FieldType by its name and role from the instance’s metaclass.

get_field_types([name, role])

Returns a list of pydent.models.FieldType by their name and role from the instance’s metaclass.

get_field_value(name[, role])

Returns a pydent.models.FieldValue by its name and role.

get_field_value_array(name[, role])

Returns a list of pydent.models.FieldValue by their name and role.

get_metatype()

Returns the instance’s metatype.

get_relationships()

get_routing()

Returns the routing dictionary for this instance.

get_server_model_name()

get_tableized_name()

interface(session)

Creates a model interface from this class and a session.

is_deserialized(name)

Check if a given key has already been deserialized.

load(*args, **kwargs)

Deserialize the data to a model.

load_from(data[, owner])

Create a new model instance from loaded attributes.

new_field_value(name[, role, values_dict])

Instantiates a new pydent.models.FieldValue from a name and role.

new_field_value_from_field_type(field_type)

Instantiates a new pydent.models.FieldValue from a.

no_getter(*_)

Callback that always returns None.

one(session, query, **kwargs)

rtype

ModelBase

one_callback(model_name, *args, **kwargs)

rtype

ModelBase

print()

refresh()

Refresh this model from data from the server.

reload(data)

Reload model attributes from new data.

reset_field(name)

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

safe_get_field_type(fv)

Safely returns the field value’s pydent.models.FieldType from the model.

save()

Saves the sample, either by creating a new sample (if id=None) or updating the existing sample on the server.

set_field_value(name, role, values)

Sets a field value name and role using a dictionary, as in the following:

set_field_value_array(name, role, values_array)

Sets an array of field values by name and role using an array of dictionaries, as in the following:

update()

Updates the sample on the server.

update_field_values(value_dict[, role])

update_properties(prop_dict)

Update the FieldValues properties for this sample.

where(session, params)

Finds a list of models by some parameters.

where_callback(model_name, *args, **kwargs)

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

Attributes

DEFAULT_COPY_KEEP_UNANONYMOUS

GLOBAL_KEY

METATYPE

PRIMARY_KEY

SERVER_MODEL_NAME

counter

Sample.field_values

fields

id

identifier

Return the identifier used by Aquarium in autocompletes.

Sample.items

properties

rid

rtype

int

Sample.sample_type

session

The connected session instance.

_check_for_session()

Raises error if model is not connected to a session.

:raises NoSessionError

classmethod _flatten_deserialized_data(models, memo)

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.

_model_schema

alias of pydent.marshaller.base.SampleSchema

property _primary_key

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

_rid_dict()

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

add_data(data)

Initializes fake attributes that correspond to data.

anonymize()

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

Metatypes cannot be annonymized.

Returns

self

append_to_many(name, model)

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)

Connect model to a session.

Parameters

session (SessionABC) – the AqSession

Returns

None

Raises
copy()[source]

Return a copy of this sample.

create()[source]

Creates this sample in the Aquarium instance if the sample has all fields required by the sample type. Uses is_savable to check for missing fields.

Changed in version 0.1.5a17: Raises AquariumModelError if sample is missing required properties

Return type

List[Sample]

Returns

the list of saved samples

Raises

AquariumModelError – if required FieldValues are missing.

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

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

field_value(name)[source]

Returns the FieldValue associated with the sample by its name. If the there is more than one FieldValue with the same name (as in field_value arrays), it will return the first FieldValue. See the.

Sample.field_value_array() method.

Parameters

name (str) – name of the field value

Returns

the field value

Return type

FieldValue

classmethod find(session, model_id)

Finds a model instance by its model_id.

Return type

ModelBase

find_callback(model_name, model_id)

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

get_field_type(name, role)

Returns a pydent.models.FieldType by its name and role from the instance’s metaclass.

Parameters
  • name (basestring) – its name

  • role (basestring) – its role

Returns

metatypes field type or None if not found

Return type

FieldType | None

get_field_types(name=None, role=None)

Returns a list of pydent.models.FieldType by their name and role from the instance’s metaclass.

Parameters
  • name (basestring) – its name

  • role (basestring) – its role

Returns

list of field types

Return type

list

get_field_value(name, role=None)

Returns a pydent.models.FieldValue by its name and role.

Parameters
  • name (basestring) – its name

  • role (basestring) – its role

Returns

the field value or None if not found

Return type

FieldValue | None

get_field_value_array(name, role=None)

Returns a list of pydent.models.FieldValue by their name and role.

Parameters
  • name (basestring) – its name

  • role (basestring) – its role

Returns

list of field values

Return type

list

get_metatype()

Returns the instance’s metatype.

The metatype is either pydent.models.SampleType or pydent.models.OperationType). The metatype class name is stored as METATYPE in the class definition.

Returns

the metatype

Return type

ModelBase

get_routing()

Returns the routing dictionary for this instance.

Returns

routing dictionary

Return type

dict

property identifier

Return the identifier used by Aquarium in autocompletes.

classmethod interface(session)

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.

is_savable(do_raise=True)[source]
Checks whether this sample has fields required by the sample type.

If so, the sample can be saved or updated.

New in version 0.1.5a17.

Parameters

do_raise (bool) – raise an exception when required field values are missing

Return type

Tuple[bool, List[str]]

Returns

a boolean whether there are errors, and list of error messages

Raises

AquariumModelError – if do_raise is True and required fields are missing

classmethod load(*args, **kwargs)

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)

Create a new model instance from loaded attributes.

‘obj’ should have a o

Return type

Union[List[ModelBase], ModelBase]

merge()[source]

Merge sample by name. If a sample with the same name and sample_type_id is found on the server, update that model and save the updated data to the server. Else, create a new sample on the server.

Parameters

sample – sample to merge.

Returns

True if merged, False otherwise

model_schema

alias of pydent.marshaller.base.SampleSchema

new_field_value(name, role=None, values_dict=None)

Instantiates a new pydent.models.FieldValue from a name and role. The should be a field type with the name and role in the metatype. Optionally, a values dictionary may be provided with the following format:

values_dict = {
    "value": None,
    "sample": mysample,
    "item": myitem,
    "object_type": None
}
Parameters
  • name (basestring) – its name

  • role (basestring) – its role

  • values_dict (dict) – values to set the new field value

Returns

the new field value

Return type

FieldValue

new_field_value_from_field_type(field_type, values_dict=None)

Instantiates a new pydent.models.FieldValue from a.

pydent.models.FieldType instance. Optionally, a values dictionary may be provided with the following format:

values_dict = {
    "value": None,
    "sample": mysample,
    "item": myitem,
    "object_type": None
}
Parameters
  • field_type (FieldType) – field type to instantiate the field value

  • values_dict (dict) – values to set the new field value

Returns

the new field value

Return type

FieldValue

no_getter(*_)

Callback that always returns None.

refresh()

Refresh this model from data from the server.

Returns

self

Return type

self

reload(data)

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

safe_get_field_type(fv)

Safely returns the field value’s pydent.models.FieldType from the model.

If the field value has no reference to the field type, its metatype (e.g. pydent.models.SampleType or pydent.models.OperationType) is used to recover the field type.

save()[source]

Saves the sample, either by creating a new sample (if id=None) or updating the existing sample on the server.

Changed in version 0.1.5a17: Raises AquariumModelError if sample is missing required properties

Returns

property session

The connected session instance.

set_field_value(name, role, values)

Sets a field value name and role using a dictionary, as in the following:

values = {
    "value": None,
    "sample": mysample,
    "item": myitem,
    "object_type": None
}
set_field_value_array(name, role, values_array)

Sets an array of field values by name and role using an array of dictionaries, as in the following:

values_array = [{
    "value": None,
    "sample": mysample,
    "item": myitem,
    "object_type": None
}]
update()[source]

Updates the sample on the server.

Changed in version 0.1.5a17: Raises AquariumModelError if sample is missing required properties

Return type

Sample

Returns

update_properties(prop_dict)[source]

Update the FieldValues properties for this sample.

Parameters

prop_dict – values to update

Returns

self

Return type

Sample

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)

Finds a list of models by some parameters.

Return type

Optional[List[ModelBase]]

where_callback(model_name, *args, **kwargs)

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

Used to find models in model relationships.

Return type

Optional[List[ModelBase]]