Skip to content

Commit

Permalink
fix: fix repeat dict for nb (#86)
Browse files Browse the repository at this point in the history
<!-- 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>
  • Loading branch information
njzjz and pre-commit-ci[bot] authored Nov 21, 2024
1 parent c165c59 commit 357d910
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
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 @@ class ArgumentData:
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 @@ def _init_subdata(self):
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))

def print_html(self, _level=0, _last_one=True):
"""Print the data with Argument in HTML format.
Expand All @@ -203,7 +219,7 @@ def print_html(self, _level=0, _last_one=True):
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

0 comments on commit 357d910

Please sign in to comment.