Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer default values for the uuid and single options of asdict() #833

Merged
merged 8 commits into from
Jun 25, 2024
8 changes: 6 additions & 2 deletions bindings/python/dlite-entity-python.i
Original file line number Diff line number Diff line change
Expand Up @@ -692,12 +692,13 @@ def get_instance(
iterfun(self),
)

def asdict(self, soft7=True, uuid=True, single=True):
def asdict(self, soft7=True, uuid=None, single=None):
"""Returns a dict representation of self.

Arguments:
soft7: Whether to structure metadata as SOFT7.
uuid: Whether to include UUID in the dict.
uuid: Whether to include UUID in the dict. The default is true
if `single=True` and URI is None, otherwise it is false.
single: Whether to return in single-entity format.
If None, single-entity format is used for metadata and
multi-entity format for data instances.
Expand All @@ -706,6 +707,9 @@ def get_instance(
if single is None:
single = self.is_meta

if uuid is None:
uuid = single and self.uri

if not single:
d = {}
dct[self.uuid] = d
Expand Down
6 changes: 4 additions & 2 deletions bindings/python/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@
item2 = Item([3], properties={
"name": "b", "f": [float("-inf"), 0, float("inf")]
})
dims = infer_dimensions(meta=Item, values=item1.asdict()["properties"])
dims = infer_dimensions(
meta=Item, values=item1.asdict(single=True)["properties"]
)
assert dims == {"nf": 2}

Ref = dlite.get_instance("http://onto-ns.com/meta/0.1/Ref")
ref = Ref(dimensions={"nitems": 2, "nrefs": 1})
ref.item = item1
ref.items = item1, item2
ref.refs = [ref]
dims = infer_dimensions(meta=Ref, values=ref.asdict()["properties"])
dims = infer_dimensions(meta=Ref, values=ref.asdict(single=True)["properties"])
assert dims == {"nitems": 2, "nrefs": 1}
2 changes: 1 addition & 1 deletion storages/python/python-storage-plugins/bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def save(self, inst: dlite.Instance) -> None:

"""
self._data[inst.uuid] = inst.asdict(
soft7=dlite.asbool(self.options.soft7)
soft7=dlite.asbool(self.options.soft7), uuid=True, single=True
)

def queue(
Expand Down
2 changes: 1 addition & 1 deletion storages/python/python-storage-plugins/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def load(self, id):

def save(self, inst):
"""Stores `inst` in current storage."""
document = inst.asdict(uuid=True)
document = inst.asdict(uuid=True, single=True)
self.collection.insert_one(document)

def queue(self, pattern=None):
Expand Down
2 changes: 1 addition & 1 deletion storages/python/python-storage-plugins/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def to_bytes(cls, inst, soft7=True, with_uuid=False):
The bytes (or bytearray) object that the instance is saved to.
"""
return pyyaml.safe_dump(
inst.asdict(soft7=soft7, uuid=with_uuid),
inst.asdict(soft7=soft7, uuid=with_uuid, single=True),
default_flow_style=False,
sort_keys=False,
).encode()