Skip to content

Commit

Permalink
Implement ONDict.asdict (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
okomestudio authored Apr 30, 2020
1 parent 5622d69 commit f678413
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/resconfig/ondict.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,24 @@ def allkeys(self, as_str=False):
for key in self.__allkeys(("__ROOT__",), {"__ROOT__": self}):
yield ".".join(key) if as_str else key

def __asdict(self, d):
if not isinstance(d, MutableMapping):
return d
result = {}
for k in d.keys():
result[k] = self.__asdict(d[k])
return result

def asdict(self) -> dict:
"""Get a built-in dict representation of itself.
All the nested mappings are converted into :class:`dict`.
Returns:
Built-in :class:`dict` object.
"""
return self.__asdict(self)

def merge(self, *args, **kwargs):
"""Merge from dict and/or iterable.
Expand Down
12 changes: 12 additions & 0 deletions tests/resconfig/test_ondict.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ def test_allkeys(self, d):
assert list(d.allkeys()) == [("foo", "bar", "baz"), ("foo", "qux")]
assert list(d.allkeys(as_str=True)) == ["foo.bar.baz", "foo.qux"]

def test_asdict(self):
d = ONDict({"a.b.c": 0})
assert type(d) == ONDict
assert type(d["a"]) == ONDict
assert type(d["a.b"]) == ONDict
assert type(d["a.b.c"]) == int
d = d.asdict()
assert type(d) == dict
assert type(d["a"]) == dict
assert type(d["a"]["b"]) == dict
assert type(d["a"]["b"]["c"]) == int

@pytest.mark.parametrize(
"args, kwargs, expected",
[
Expand Down

0 comments on commit f678413

Please sign in to comment.