Skip to content

Commit 357d910

Browse files
fix: fix repeat dict for nb (#86)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Documentation** - Updated the Jupyter Notebook to improve the clarity of the JSON structure by renaming keys and adding new entries for better organization of repeatable items. - **Enhancements** - Improved flexibility of the `ArgumentData` class to handle various data structures more effectively. - Refined error handling in the `print_html` method for better robustness against unknown data types. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Jinzhe Zeng <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c165c59 commit 357d910

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

dargs/notebook.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,24 @@ class ArgumentData:
147147
The data to be displayed.
148148
arg : Union[dargs.Argument, dargs.Variant]
149149
The Argument that describes the data.
150+
repeat : bool, optional
151+
The argument is repeat
150152
"""
151153

152-
def __init__(self, data: dict, arg: Argument | Variant):
154+
def __init__(self, data: dict, arg: Argument | Variant, repeat: bool = False):
153155
self.data = data
154156
self.arg = arg
157+
self.repeat = repeat
155158
self.subdata = []
156159
self._init_subdata()
157160

158161
def _init_subdata(self):
159162
"""Initialize sub ArgumentData."""
160-
if isinstance(self.data, dict) and isinstance(self.arg, Argument):
163+
if (
164+
isinstance(self.data, dict)
165+
and isinstance(self.arg, Argument)
166+
and not (self.arg.repeat and not self.repeat)
167+
):
161168
sub_fields = self.arg.sub_fields.copy()
162169
# extend subfiles with sub_variants
163170
for vv in self.arg.sub_variants.values():
@@ -178,9 +185,18 @@ def _init_subdata(self):
178185
isinstance(self.data, list)
179186
and isinstance(self.arg, Argument)
180187
and self.arg.repeat
188+
and not self.repeat
181189
):
182190
for dd in self.data:
183-
self.subdata.append(ArgumentData(dd, self.arg))
191+
self.subdata.append(ArgumentData(dd, self.arg, repeat=True))
192+
elif (
193+
isinstance(self.data, dict)
194+
and isinstance(self.arg, Argument)
195+
and self.arg.repeat
196+
and not self.repeat
197+
):
198+
for dd in self.data.values():
199+
self.subdata.append(ArgumentData(dd, self.arg, repeat=True))
184200

185201
def print_html(self, _level=0, _last_one=True):
186202
"""Print the data with Argument in HTML format.
@@ -203,7 +219,7 @@ def print_html(self, _level=0, _last_one=True):
203219
if _level > 0 and not (
204220
isinstance(self.data, dict)
205221
and isinstance(self.arg, Argument)
206-
and self.arg.repeat
222+
and self.repeat
207223
):
208224
if isinstance(self.arg, (Argument, Variant)):
209225
buff.append(r"""<span class="dargs-key">""")

docs/nb.ipynb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
" 3\n",
3030
" ],\n",
3131
" \"test_variant\": \"test_variant_argument\",\n",
32-
" \"test_repeat\": [\n",
32+
" \"test_repeat_list\": [\n",
3333
" {\"test_repeat_item\": false},\n",
3434
" {\"test_repeat_item\": true}\n",
3535
" ],\n",
36+
" \"test_repeat_dict\": {\n",
37+
" \"test1\": {\"test_repeat_item\": false},\n",
38+
" \"test2\": {\"test_repeat_item\": true}\n",
39+
" },\n",
3640
" \"_comment\": \"This is an example data\"\n",
3741
"}\n",
3842
"\"\"\"\n",

0 commit comments

Comments
 (0)