Skip to content

Commit

Permalink
Infer default values for the uuid and single options of asdict() (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
jesper-friis authored Jun 25, 2024
1 parent 879ee73 commit fea68ad
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
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()

0 comments on commit fea68ad

Please sign in to comment.