caosadvancedtools.models.data_model module#
- class caosadvancedtools.models.data_model.DataModel(*args, enums: list[Record] | None = None)#
Bases:
dictA managed data model.
When constructing a data model the LinkAhead representation can easily be created using the classes RecordType and Propery, storing them in a Container and inserting it in LinkAhead. However, this has one drawback: You cannot simply change someting and update the container. The container will insist on having valid ids for all contained Entities.
This class allows you to define your model as easily but also provides you with a method (sync_data_model) that will sync with the data model in an existing LinkAhead instance.
This is possible because entities, defined in this model, are identified with entities in LinkAhead using names. I.e. a RecordType “Experiment” in this model will update an existing RecordType with name “Experiment” in LinkAhead. Thus, be carefull not to change existing Entities that were created for a different purpose (e.g. someone else’s experiment).
DataModel inherits from dict. The keys are always the names of the entities. Thus you cannot have unnamed or ambiguously named entities in your model.
Additionally the DataModel takes an
enumsarray parameter. This contains LinkAhead records which should serve as enum values. At present this only is relevant for synchronization with the LinkAhead server: These enum records will be synchronized just like the record types uponsync_data_model.Example:#
# Create a DataModel with a RecordType and a Property, not assuming any # relation between the two. dm = DataModel([db.RecordType(name=”myRecordType”),
db.Property(name=”myProperty”)])
# Sync the DataModel with the server, so that the server state is consistent # with this DataModel’s content. dm.sync_data_model() # Now the DataModel’s IDs are the same as on the server.
- collect_entities() list[Entity]#
Collect all entities and return as a flat list.
This includes explicitly defined RecordTypes and Properties, as well as RecordTypes mentioned as Properties.
- Returns:
out
- Return type:
list[db.Entity]
- static entities_without(entities, names)#
Return a new list with all entities which do not have certain names.
- Parameters:
entities (iterable) – A iterable with entities.
names (iterable of str) – Only entities which do not have one of these names will end up in the returned iterable.
- Returns:
A list with entities.
- Return type:
list
- get_deep(name: str, visited_props: dict | None = None, visited_parents: set | None = None)#
Attempt to resolve references for the given
name.The returned entity has all the properties it inherits from its ancestry and all properties have the correct descriptions and datatypes. This methods only uses data which is available in this DataModel, which acts kind of like a cache pool.
Note that this may change this data model (subsequent “get” like calls may also return deeper content.)
- static get_existing_entities(entities)#
Return a list with those entities of the supplied iterable that exist in the LinkAhead instance.
- Parameters:
entities (iterable) – The entities to be retrieved. This object will not be modified.
- Raises:
TransactionError – If the retrieval fails.
- sync_data_model(noquestion: bool = False, verbose: bool = True)#
Synchronize this DataModel with a LinkAhead instance.
Updates existing entities from the LinkAhead instance and inserts non-existing entities into the instance. Note: This allows to easily overwrite changes that were made to an existing data model. Use this function with care and double check its effect.
- Raises:
TransactionError – If one of the involved transactions fails.
- sync_ids_by_name(valid_entities)#
Add IDs from valid_entities to the entities in this DataModel.
“By name” means that the valid IDs (from the valid_entities) are assigned to the entities, their properties in this DataModel by their names, also parents are replaced by equally named entities in valid_entities. These changes happen in place to this DataModel!
- Parameters:
valid_entities (list of Entity) – A list (e.g. a Container) of valid entities.
- Return type:
None