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

Added option single to Instance.asdict() allowing to saving in multi-entity format #653

Merged
merged 8 commits into from
Oct 30, 2023
17 changes: 14 additions & 3 deletions bindings/python/dlite-entity-python.i
Original file line number Diff line number Diff line change
Expand Up @@ -669,14 +669,25 @@ def get_instance(id: str, metaid: str = None, check_storages: bool = True) -> "I
iterfun(self),
)

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:
Expand All @@ -700,7 +711,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,
Expand Down