Skip to content

Commit 9bdfa61

Browse files
committed
feat(info): add new arg set to only show those members that are set
1 parent b702c0c commit 9bdfa61

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

neuroml/nml/generatedssupersuper.py

+34-14
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ def info(self, show_contents=False, return_format="string"):
344344
By default, this will only show the members, and not their contents.
345345
To see contents that have been set, use `show_contents=True`. This will
346346
not show empty/unset contents. To see all contents, set
347-
`show_contents=all`.
347+
`show_contents="all"`. To only show members that have values set, use
348+
`show_contents="set"`.
348349
349350
Note that not all members will have ids (since not all NeuroML2
350351
ComponentTypes have ids). For members that do not have ids, the object
@@ -361,10 +362,10 @@ def info(self, show_contents=False, return_format="string"):
361362
If "dict" or "list" is provided, the information is returned as a
362363
dict/list instead of being printed. Note that if `show_contents` is
363364
`False`, only a list of members is available and will be returned
364-
even if "dict" is supplied. If `show_contents` is `True` or "all"
365-
but "list" is provided, only the list of members will be returned.
366-
If something other than "string", "list", or "dict" is provided,
367-
the string representation is returned and printed.
365+
even if "dict" is supplied. If `show_contents` is `True`, "all", or
366+
"set" but "list" is provided, only the list of members will be
367+
returned. If something other than "string", "list", or "dict" is
368+
provided, the string representation is returned and printed.
368369
:type return_format: str
369370
:returns: info string, or list of members or dict with members as keys
370371
and member values as values
@@ -389,7 +390,7 @@ def info(self, show_contents=False, return_format="string"):
389390
info_str += "Valid members for {} are:\n".format(class_name)
390391
all_members = self._get_members()
391392
for member in all_members:
392-
info_str += "* {} (class: {}, {})\n".format(
393+
member_str = "* {} (class: {}, {})\n".format(
393394
member.get_name(),
394395
member.get_data_type(),
395396
"Optional" if member.get_optional() else "Required",
@@ -410,11 +411,13 @@ def info(self, show_contents=False, return_format="string"):
410411
# will be empty, []
411412
# if it's a scalar, it will be set to None or to a non
412413
# container value
414+
contents_str = ""
413415
if contents is None or (
414416
isinstance(contents, list) and len(contents) == 0
415417
):
416418
if show_contents == "all":
417-
info_str += "\t* Contents: {}\n\n".format(contents)
419+
contents_str = "\t* Contents: {}\n\n".format(contents)
420+
# has contents
418421
else:
419422
contents_id = None
420423
# if list, iterate to get ids
@@ -431,12 +434,27 @@ def info(self, show_contents=False, return_format="string"):
431434
contents_id = f"'{contents.id}'"
432435
else:
433436
contents_id = contents
434-
info_str += "\t* Contents ('ids'/<objects>): {}\n\n".format(
437+
contents_str = "\t* Contents ('ids'/<objects>): {}\n\n".format(
435438
contents_id
436439
)
437-
info_ret[member.get_name()]["members"] = getattr(
438-
self, member.get_name(), None
439-
)
440+
441+
member_val = getattr(self, member.get_name(), None)
442+
443+
# if all, show everything
444+
if show_contents == "all":
445+
info_str += member_str + contents_str
446+
info_ret[member.get_name()]["members"] = member_val
447+
448+
# if set, only show members where contents exist
449+
elif show_contents == "set":
450+
if len(contents_str) > 0:
451+
info_str += member_str + contents_str
452+
if member_val is not None:
453+
info_ret[member.get_name()]["members"] = member_val
454+
else:
455+
info_str += member_str + contents_str
456+
info_ret[member.get_name()]["members"] = member_val
457+
440458
else:
441459
info_ret.append(member.get_name())
442460

@@ -447,9 +465,11 @@ def info(self, show_contents=False, return_format="string"):
447465
return info_ret
448466
elif return_format == "dict":
449467
return info_ret
450-
451-
print(info_str)
452-
return info_str
468+
elif return_format == "string":
469+
return info_str
470+
else:
471+
print(info_str)
472+
return info_str
453473

454474
def validate(self, recursive=False):
455475
"""Validate the component.

0 commit comments

Comments
 (0)