Skip to content

Commit

Permalink
Added utioity function get_referred_instances()
Browse files Browse the repository at this point in the history
  • Loading branch information
jesper-friis committed Oct 16, 2023
1 parent 274ca49 commit ea7149e
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion bindings/python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,42 @@ def infer_dimensions(meta, values, strict=True):
missing_dims = dimnames.difference(dims.keys())
raise CannotInferDimensionError(
f"insufficient number of properties provided to infer dimensions: "
f"{missing_dims}"
f"{missing_dims} for metadata: {meta.uri}"
)

return dims


def get_referred_instances(inst, include_meta=False):
"""Return a set with all instances that are directly or indirectly
referred to by `inst`.
If `include_meta` is true, also return metadata.
"""
references = set()
_get_referred_instances(inst, include_meta, references)
return references


def _get_referred_instances(inst, include_meta, references):
"""Recursive help function for get_referred_instances()."""
if inst is None or inst in references:
return
references.add(inst)
if isinstance(inst, dlite.Collection):
for i in inst.get_instances():
_get_referred_instances(i, include_meta, references)
else:
for prop in inst.meta.properties["properties"]:
if prop.type == "ref":
if prop.ndims:
for i in inst[prop.name]:
_get_referred_instances(
i, include_meta, references
)
else:
_get_referred_instances(
inst[prop.name], include_meta, references
)
if include_meta:
_get_referred_instances(inst.meta, include_meta, references)

0 comments on commit ea7149e

Please sign in to comment.