From 8bd6270b0c8648e466db04e09adea6258ef800c8 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Tue, 26 Sep 2023 18:38:24 +0200 Subject: [PATCH] Added option `single` to Instance.asdict() allowing to saving in multi-entity format. --- bindings/python/dlite-entity-python.i | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/bindings/python/dlite-entity-python.i b/bindings/python/dlite-entity-python.i index 0ede8394e..20ee3c0e0 100644 --- a/bindings/python/dlite-entity-python.i +++ b/bindings/python/dlite-entity-python.i @@ -639,14 +639,24 @@ def get_instance(id: str, metaid: str = None, check_storages: bool = True) -> "I inst[k] = v return inst - def asdict(self, soft7=True, uuid=True): + def asdict(self, soft7=True, uuid=True, single=True): """Returns a dict representation of self. Arguments: soft7: Whether to structure metadata as SOFT7. uuid: Whether to include UUID in the dict. + single: Whether to return in single-entity format. + If None, single-entity format is used for metadata and + multi-entity format for data instances. """ - d = OrderedDict() + dct = d = OrderedDict() + if single is None: + single = self.is_meta + + if not single: + d = {} + dct[self.uuid] = d + if uuid: d['uuid'] = self.uuid if self.uri: @@ -670,7 +680,7 @@ def get_instance(id: str, metaid: str = None, check_storages: bool = True) -> "I if self.has_property('relations') and ( self.is_meta or self.meta.has_property('relations')): d['relations'] = self['relations'].tolist() - return d + return dct def asjson(self, indent=0, single=None, urikey=False, with_uuid=False, with_meta=False,