dlite.utils
#
Module Contents#
Classes#
The feature you request requires installing an external package. |
|
Cannot infer instance dimensions. |
|
Invalid number of instance dimensions. |
|
Metadata is not found in the internal instance store. |
|
A dict subclass providing a simple store for dict-representations |
Functions#
|
A exception hook that allows exceptions to define an |
|
Returns a new DLite instance created from dict. |
|
Converts obj to dlite Metadata. |
Returns the datamodel for dataclasses in Python standard library. |
|
|
Return a dlite property from a name and a pydantic property dict. |
|
Create a new dlite metadata from a pydantic model. |
|
Return a new dlite instance from a pydantic instance pydinst. |
Returns the datamodel for dataclasses in Python standard library. |
|
Returns a dict with all the DLite builtin path variables. |
|
|
Infer the dimensions if we should create an instance of meta with |
|
Return a set with all instances that are directly or indirectly |
Attributes#
- dlite.utils.HAVE_DATACLASSES = False#
- dlite.utils.HAVE_PYDANTIC = False#
- class dlite.utils.MissingDependencyError#
Bases:
dlite.DLiteError
The feature you request requires installing an external package.
Install it with
pip install <package>
- exit_code = 44#
- class dlite.utils.CannotInferDimensionError#
Bases:
dlite.DLiteError
Cannot infer instance dimensions.
- class dlite.utils.InvalidNumberOfDimensionsError#
Bases:
dlite.DLiteError
Invalid number of instance dimensions.
- class dlite.utils.MetadataNotDefinedError#
Bases:
dlite.DLiteError
Metadata is not found in the internal instance store.
- dlite.utils.uncaught_exception_hook(exetype, value, trace)#
A exception hook that allows exceptions to define an exit_code attribute that will make Python exit with that code if the exception is uncaught.
- class dlite.utils.DictStore(*args)#
Bases:
dict
A dict subclass providing a simple store for dict-representations of instances (including metadata).
The internal layout follows the multi-instance representation, where each top-level key-value pair is an UUID mapped to a dict-representation of an instance. Metadata is represented in soft7 format.
- Parameters:
args – One or more to initialise the store with.
- add(d, id=None)#
Add dict-representation d of an instance to the store.
- Parameters:
d – A dict to add to the store.
id – Optional id of d. The id must be consistent with any ‘uuid’, ‘uri’ or ‘identity’ key. This option is useful in the case d doesn’t have an ‘uuid’, ‘uri’ or ‘identity’ key.
Notes
The layout of d may be any of the supported representations for data instances or metadata, including any combination of single/multi-instance and soft7 representations. The internal layout is always multi-instance soft7.
- dlite.utils.instance_from_dict(d, id=None, single=None, check_storages=True)#
Returns a new DLite instance created from dict.
- Parameters:
d (
dict
) – Dict to parse. It should be of the same form as returned by the Instance.asdict() method.id (
str
) –Identity of the returned instance.
If d is in single-entity form with no explicit ‘uuid’ or ‘uri’, its identity will be assigned by id. Otherwise id must be consistent with the ‘uuid’ and/or ‘uri’ fields of d.
If d is in multi-entity form, id is used to select the instance to return.
single (bool | None |
"auto"
) – Whether the dict is assumed to be in single-entity form If single is None or “auto”, the form is inferred.check_storages (
bool
) – Whether to check if the instance already exists in storages specified in dlite.storage_path.
- dlite.utils.to_metadata(obj)#
Converts obj to dlite Metadata.
- dlite.utils.get_dataclass_entity_schema()#
Returns the datamodel for dataclasses in Python standard library.
- dlite.utils.pydantic_to_property(name, propdict, dimensions=None, namespace='http://onto-ns.com/meta', version='0.1')#
Return a dlite property from a name and a pydantic property dict.
- Parameters:
name (str) – Name of the property to create.
propdict (dict) – Pydantic property dict.
dimensions (Optional[dict]) – If given, the dict will be updated with new dimensions from array properties.
namespace (str) – For a reference property use this as the namespace of the property to refer to.
version (str) – For a reference property use this as the version of the property to refer to.
- Returns:
New DLite property.
- dlite.utils.pydantic_to_metadata(model, uri=None, default_namespace='http://onto-ns.com/meta', default_version='0.1', metaid=dlite.ENTITY_SCHEMA)#
Create a new dlite metadata from a pydantic model.
- Parameters:
model – A pydantic model or an instance of one to create the new metadata from.
uri – URI of the created metadata. If not given, it is inferred from default_namespace, default_version and the title of the model schema.
default_namespace – Default namespace used if uri is None.
default_version – Default version used if uri is None.
metaid – Metadata for the created metadata. Defaults to dlite.ENTITY_SCHEMA.
- dlite.utils.pydantic_to_instance(meta, pydinst)#
Return a new dlite instance from a pydantic instance pydinst.
- dlite.utils.get_pydantic_entity_schema()#
Returns the datamodel for dataclasses in Python standard library.
- dlite.utils.get_package_paths()#
Returns a dict with all the DLite builtin path variables.
- dlite.utils.infer_dimensions(meta, values, strict=True)#
Infer the dimensions if we should create an instance of meta with the given values.
- Parameters:
meta – URI or metadata object.
values – Dict mapping property names to values. Not all property names needs to be mapped.
strict – Whether to require that all keys in values correspond to a property name in meta.
- Returns:
Dict mapping dimension names to dimension values.
- Raises:
InvalidNumberOfDimensionsError – Inconsistent number of dimensions.
CannotInferDimensionError – Cannot infer instance dimensions.
- dlite.utils.get_referred_instances(inst, include_meta=False)#
Return a set with all instances that are directly or indirectly referred to by inst.
This function follows instances referred to by collections and via properties of type ‘ref’. Transaction parents are not included, hence the returned set only includes instances in the current snapshot.
Cyclic references are handled correctly.
If include_meta is true, also return metadata.
Example
If you have a collection ‘coll’ with three instances ‘inst1’, ‘inst2’ and ‘inst3’ and ‘inst2’ has a ‘ref’ property, which is an array [inst4, inst5], then
get_referred_instances(coll)
would return the set {coll, inst1, inst2, inst3, inst4, inst5}.
The same set would be returned even if one of the instances would have a ‘ref’ property referring back to ‘coll’.