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

fix: fix repeat dict for nb #86

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions dargs/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,24 @@
The data to be displayed.
arg : Union[dargs.Argument, dargs.Variant]
The Argument that describes the data.
repeat : bool, optional
The argument is repeat
"""

def __init__(self, data: dict, arg: Argument | Variant):
def __init__(self, data: dict, arg: Argument | Variant, repeat: bool = False):
self.data = data
self.arg = arg
self.repeat = repeat
self.subdata = []
self._init_subdata()

def _init_subdata(self):
"""Initialize sub ArgumentData."""
if isinstance(self.data, dict) and isinstance(self.arg, Argument):
if (
isinstance(self.data, dict)
and isinstance(self.arg, Argument)
and not (self.arg.repeat and not self.repeat)
):
sub_fields = self.arg.sub_fields.copy()
# extend subfiles with sub_variants
for vv in self.arg.sub_variants.values():
Expand All @@ -178,9 +185,18 @@
isinstance(self.data, list)
and isinstance(self.arg, Argument)
and self.arg.repeat
and not self.repeat
):
for dd in self.data:
self.subdata.append(ArgumentData(dd, self.arg))
self.subdata.append(ArgumentData(dd, self.arg, repeat=True))
elif (
isinstance(self.data, dict)
and isinstance(self.arg, Argument)
and self.arg.repeat
and not self.repeat
):
for dd in self.data.values():
self.subdata.append(ArgumentData(dd, self.arg, repeat=True))

Check warning on line 199 in dargs/notebook.py

View check run for this annotation

Codecov / codecov/patch

dargs/notebook.py#L198-L199

Added lines #L198 - L199 were not covered by tests

def print_html(self, _level=0, _last_one=True):
"""Print the data with Argument in HTML format.
Expand All @@ -203,7 +219,7 @@
if _level > 0 and not (
isinstance(self.data, dict)
and isinstance(self.arg, Argument)
and self.arg.repeat
and self.repeat
):
if isinstance(self.arg, (Argument, Variant)):
buff.append(r"""<span class="dargs-key">""")
Expand Down
6 changes: 5 additions & 1 deletion docs/nb.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@
" 3\n",
" ],\n",
" \"test_variant\": \"test_variant_argument\",\n",
" \"test_repeat\": [\n",
" \"test_repeat_list\": [\n",
" {\"test_repeat_item\": false},\n",
" {\"test_repeat_item\": true}\n",
" ],\n",
" \"test_repeat_dict\": {\n",
" \"test1\": {\"test_repeat_item\": false},\n",
" \"test2\": {\"test_repeat_item\": true}\n",
" },\n",
" \"_comment\": \"This is an example data\"\n",
"}\n",
"\"\"\"\n",
Expand Down