dlite.utils#

Module Contents#

Classes#

MissingDependencyError

The feature you request requires installing an external package.

CannotInferDimensionError

Cannot infer instance dimensions.

InvalidNumberOfDimensionsError

Invalid number of instance dimensions.

MetadataNotDefinedError

Metadata is not found in the internal instance store.

DictStore

A dict subclass providing a simple store for dict-representations

Functions#

uncaught_exception_hook(exetype, value, trace)

A exception hook that allows exceptions to define an

instance_from_dict(d[, id, single, check_storages])

Returns a new DLite instance created from dict.

to_metadata(obj)

Converts obj to dlite Metadata.

get_dataclass_entity_schema()

Returns the datamodel for dataclasses in Python standard library.

pydantic_to_property(name, propdict[, dimensions, ...])

Return a dlite property from a name and a pydantic property dict.

pydantic_to_metadata(model[, uri, default_namespace, ...])

Create a new dlite metadata from a pydantic model.

pydantic_to_instance(meta, pydinst)

Return a new dlite instance from a pydantic instance pydinst.

get_pydantic_entity_schema()

Returns the datamodel for dataclasses in Python standard library.

get_package_paths()

Returns a dict with all the DLite builtin path variables.

infer_dimensions(meta, values[, strict])

Infer the dimensions if we should create an instance of meta with

get_referred_instances(inst[, include_meta])

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 to parse. It should be of the same form as returned by the Instance.asdict() method.

  • id – ID (URI or UUID) if instance to return.

  • single – Unsused. Provided for backward compabitility.

  • 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:
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’.