From fea68ada0ae1de5484fe122f52be3890a9b32c02 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Tue, 25 Jun 2024 20:27:10 +0200 Subject: [PATCH] Infer default values for the `uuid` and `single` options of asdict() (#833) * Changed default values for the `uuid` and `single` options of asdict() Updated affected tests. * List installed python packages to make it easy to see what is installed --- bindings/python/dlite-entity-python.i | 8 ++++++-- bindings/python/tests/test_utils.py | 6 ++++-- storages/python/python-storage-plugins/bson.py | 2 +- storages/python/python-storage-plugins/mongodb.py | 2 +- storages/python/python-storage-plugins/yaml.py | 2 +- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/bindings/python/dlite-entity-python.i b/bindings/python/dlite-entity-python.i index 9379876d6..7b6bb35b9 100644 --- a/bindings/python/dlite-entity-python.i +++ b/bindings/python/dlite-entity-python.i @@ -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. @@ -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 diff --git a/bindings/python/tests/test_utils.py b/bindings/python/tests/test_utils.py index fd5d1ccff..12affe4bd 100644 --- a/bindings/python/tests/test_utils.py +++ b/bindings/python/tests/test_utils.py @@ -178,7 +178,9 @@ 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") @@ -186,5 +188,5 @@ 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} diff --git a/storages/python/python-storage-plugins/bson.py b/storages/python/python-storage-plugins/bson.py index 129202f77..7e64bae5e 100644 --- a/storages/python/python-storage-plugins/bson.py +++ b/storages/python/python-storage-plugins/bson.py @@ -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( diff --git a/storages/python/python-storage-plugins/mongodb.py b/storages/python/python-storage-plugins/mongodb.py index 22fb8b6a8..d09b7a6f2 100644 --- a/storages/python/python-storage-plugins/mongodb.py +++ b/storages/python/python-storage-plugins/mongodb.py @@ -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): diff --git a/storages/python/python-storage-plugins/yaml.py b/storages/python/python-storage-plugins/yaml.py index 1d3415bc7..047935ac7 100644 --- a/storages/python/python-storage-plugins/yaml.py +++ b/storages/python/python-storage-plugins/yaml.py @@ -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()