Skip to content

Commit 468756d

Browse files
authored
Merge pull request #4 from njzjz/json
add a JSON encoder to encode `Argument`
2 parents 559efc5 + d0b6eab commit 468756d

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

dargs/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .dargs import Argument, Variant
1+
from .dargs import Argument, Variant, ArgumentEncoder
22

3-
__all__ = ["Argument", "Variant"]
3+
__all__ = ["Argument", "Variant", "ArgumentEncoder"]

dargs/dargs.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from copy import deepcopy
2424
from enum import Enum
2525
import fnmatch, re
26+
import json
2627

2728

2829
INDENT = " " # doc is indented by four spaces
@@ -632,4 +633,51 @@ def trim_by_pattern(argdict: dict, pattern: str,
632633
f"following reserved names: {', '.join(conflict)}")
633634
unrequired = list(filter(rem.match, argdict.keys()))
634635
for key in unrequired:
635-
argdict.pop(key)
636+
argdict.pop(key)
637+
638+
639+
class ArgumentEncoder(json.JSONEncoder):
640+
"""Extended JSON Encoder to encode Argument object:
641+
642+
Examples
643+
--------
644+
>>> json.dumps(some_arg, cls=ArgumentEncoder)
645+
"""
646+
def default(self, obj) -> Dict[str, Union[str, bool, List]]:
647+
"""Generate a dict containing argument information, making it ready to be encoded
648+
to JSON string.
649+
650+
Note
651+
----
652+
All object in the dict should be JSON serializable.
653+
654+
Returns
655+
-------
656+
dict: Dict
657+
a dict containing argument information
658+
"""
659+
if isinstance(obj, Argument):
660+
return {
661+
"object": "Argument",
662+
"name": obj.name,
663+
"type": obj.dtype,
664+
"optional": obj.optional,
665+
"alias": obj.alias,
666+
"doc": obj.doc,
667+
"repeat": obj.repeat,
668+
"sub_fields": obj.sub_fields,
669+
"sub_variants": obj.sub_variants,
670+
}
671+
elif isinstance(obj, Variant):
672+
return {
673+
"object": "Variant",
674+
"flag_name": obj.flag_name,
675+
"optional": obj.optional,
676+
"default_tag": obj.default_tag,
677+
"choice_dict": obj.choice_dict,
678+
"choice_alias": obj.choice_alias,
679+
"doc": obj.doc,
680+
}
681+
elif isinstance(obj, type):
682+
return obj.__name__
683+
return json.JSONEncoder.default(self, obj)

tests/test_docgen.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from context import dargs
22
import unittest
3-
from dargs import Argument, Variant
3+
import json
4+
from dargs import Argument, Variant, ArgumentEncoder
45

56

67
class TestDocgen(unittest.TestCase):
@@ -16,6 +17,7 @@ def test_sub_fields(self):
1617
], doc="sub doc." * 5)
1718
], doc="Base doc. " * 10)
1819
docstr = ca.gen_doc()
20+
jsonstr = json.dumps(ca, cls=ArgumentEncoder)
1921
# print("\n\n"+docstr)
2022

2123
def test_sub_repeat(self):
@@ -29,6 +31,7 @@ def test_sub_repeat(self):
2931
], doc="sub doc." * 5)
3032
], doc="Base doc. " * 10, repeat=True)
3133
docstr = ca.gen_doc()
34+
jsonstr = json.dumps(ca, cls=ArgumentEncoder)
3235
# print("\n\n"+docstr)
3336

3437
def test_sub_variants(self):
@@ -66,6 +69,7 @@ def test_sub_variants(self):
6669
], optional=True, default_tag="type1", doc="another vnt")
6770
])
6871
docstr = ca.gen_doc(make_anchor=True)
72+
jsonstr = json.dumps(ca, cls=ArgumentEncoder)
6973
# print("\n\n"+docstr)
7074

7175
def test_multi_variants(self):
@@ -110,6 +114,7 @@ def test_multi_variants(self):
110114
])
111115
])
112116
docstr = ca.gen_doc()
117+
jsonstr = json.dumps(ca, cls=ArgumentEncoder)
113118
# print("\n\n"+docstr)
114119

115120
def test_dpmd(self):

0 commit comments

Comments
 (0)