Skip to content

Commit

Permalink
update test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiyuan Chen <[email protected]>
  • Loading branch information
ZhiyuanChen committed Sep 3, 2024
1 parent 7bd45fe commit 7c52f5a
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 107 deletions.
72 changes: 36 additions & 36 deletions chanfig/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ class Config(NestedDict):
Examples:
>>> c = Config(**{"f.n": "chang"})
>>> c.i.d = 1013
>>> c.i.d = 1016
>>> c.i.d
1013
1016
>>> c.d.i
Config(<class 'chanfig.config.Config'>, )
>>> c.freeze().dict()
{'f': {'n': 'chang'}, 'i': {'d': 1013}, 'd': {'i': {}}}
>>> c.d.i = 1013
{'f': {'n': 'chang'}, 'i': {'d': 1016}, 'd': {'i': {}}}
>>> c.d.i = 1016
Traceback (most recent call last):
ValueError: Attempting to alter a frozen config. Run config.defrost() to defrost first.
>>> c.d.e
Expand All @@ -94,7 +94,7 @@ class Config(NestedDict):
>>> with c.unlocked():
... del c.d
>>> c.dict()
{'f': {'n': 'chang'}, 'i': {'d': 1013}}
{'f': {'n': 'chang'}, 'i': {'d': 1016}}
"""

parser = None # ConfigParser, Python 3.7 does not support forward reference
Expand Down Expand Up @@ -319,17 +319,17 @@ def freeze(self, recursive: bool = True) -> Self:
+ `lock`
Examples:
>>> c = Config(**{'i.d': 1013})
>>> c = Config(**{'i.d': 1016})
>>> c.getattr('frozen')
False
>>> c.freeze(recursive=False).dict()
{'i': {'d': 1013}}
{'i': {'d': 1016}}
>>> c.getattr('frozen')
True
>>> c.i.getattr('frozen')
False
>>> c.lock().dict() # alias
{'i': {'d': 1013}}
{'i': {'d': 1016}}
>>> c.i.getattr('frozen')
True
"""
Expand Down Expand Up @@ -359,12 +359,12 @@ def locked(self):
Examples:
>>> c = Config()
>>> with c.locked():
... c['i.d'] = 1013
... c['i.d'] = 1016
Traceback (most recent call last):
ValueError: Attempting to alter a frozen config. Run config.defrost() to defrost first.
>>> c.i.d = 1013
>>> c.i.d = 1016
>>> c.dict()
{'i': {'d': 1013}}
{'i': {'d': 1016}}
"""

was_frozen = self.getattr("frozen", False)
Expand All @@ -387,21 +387,21 @@ def defrost(self, recursive: bool = True) -> Self:
+ `unlock`
Examples:
>>> c = Config(**{'i.d': 1013})
>>> c = Config(**{'i.d': 1016})
>>> c.getattr('frozen')
False
>>> c.freeze().dict()
{'i': {'d': 1013}}
{'i': {'d': 1016}}
>>> c.getattr('frozen')
True
>>> c.defrost(recursive=False).dict()
{'i': {'d': 1013}}
{'i': {'d': 1016}}
>>> c.getattr('frozen')
False
>>> c.i.getattr('frozen')
True
>>> c.unlock().dict() # alias
{'i': {'d': 1013}}
{'i': {'d': 1016}}
>>> c.i.getattr('frozen')
False
"""
Expand Down Expand Up @@ -433,9 +433,9 @@ def unlocked(self):
>>> c.freeze().dict()
{}
>>> with c.unlocked():
... c['i.d'] = 1013
... c['i.d'] = 1016
>>> c.defrost().dict()
{'i': {'d': 1013}}
{'i': {'d': 1016}}
"""

was_frozen = self.getattr("frozen", False)
Expand Down Expand Up @@ -465,13 +465,13 @@ def get(self, name: Any, default: Any = None, fallback: bool | None = None) -> A
KeyError: If `Config` does not contain `name` and `default`/`default_factory` is not specified.
Examples:
>>> d = Config(**{"i.d": 1013})
>>> d = Config(**{"i.d": 1016})
>>> d.get('i.d')
1013
1016
>>> d['i.d']
1013
1016
>>> d.i.d
1013
1016
>>> d.get('f', 2)
2
>>> d.f
Expand All @@ -480,7 +480,7 @@ def get(self, name: Any, default: Any = None, fallback: bool | None = None) -> A
>>> d.freeze()
Config(<class 'chanfig.config.Config'>,
('i'): Config(<class 'chanfig.config.Config'>,
('d'): 1013
('d'): 1016
)
)
>>> d.f
Expand Down Expand Up @@ -518,19 +518,19 @@ def set(
Examples:
>>> c = Config()
>>> c['i.d'] = 1013
>>> c['i.d'] = 1016
>>> c.i.d
1013
1016
>>> c.freeze().dict()
{'i': {'d': 1013}}
>>> c['i.d'] = 1013
{'i': {'d': 1016}}
>>> c['i.d'] = 1016
Traceback (most recent call last):
ValueError: Attempting to alter a frozen config. Run config.defrost() to defrost first.
>>> c.defrost().dict()
{'i': {'d': 1013}}
>>> c['i.d'] = 1013
{'i': {'d': 1016}}
>>> c['i.d'] = 1016
>>> c.i.d
1013
1016
"""

return super().set(name, value, convert_mapping)
Expand All @@ -544,9 +544,9 @@ def delete(self, name: Any) -> None:
name:
Examples:
>>> d = Config(**{"i.d": 1013, "f.n": "chang"})
>>> d = Config(**{"i.d": 1016, "f.n": "chang"})
>>> d.i.d
1013
1016
>>> d.f.n
'chang'
>>> d.delete('i.d')
Expand Down Expand Up @@ -580,21 +580,21 @@ def pop(self, name: Any, default: Any = Null) -> Any:
Examples:
>>> c = Config()
>>> c['i.d'] = 1013
>>> c['i.d'] = 1016
>>> c.pop('i.d')
1013
1016
>>> c.pop('i.d', True)
True
>>> c.freeze().dict()
{'i': {}}
>>> c['i.d'] = 1013
>>> c['i.d'] = 1016
Traceback (most recent call last):
ValueError: Attempting to alter a frozen config. Run config.defrost() to defrost first.
>>> c.defrost().dict()
{'i': {}}
>>> c['i.d'] = 1013
>>> c['i.d'] = 1016
>>> c.pop('i.d')
1013
1016
"""

return super().pop(name, default)
28 changes: 14 additions & 14 deletions chanfig/flat_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ class FlatDict(dict, metaclass=Dict):
Examples:
>>> d = FlatDict()
>>> d.d = 1013
>>> d.d = 1016
>>> d['d']
1013
>>> d['i'] = 1013
1016
>>> d['i'] = 1016
>>> d.i
1013
1016
>>> d.a = Variable(1)
>>> d.b = d.a
>>> d.a, d.b
Expand Down Expand Up @@ -244,15 +244,15 @@ def get(self, name: Any, default: Any = None) -> Any:
TypeError: If `name` is not hashable.
Examples:
>>> d = FlatDict(d=1013)
>>> d = FlatDict(d=1016)
>>> d.get('d')
1013
1016
>>> d['d']
1013
1016
>>> d.d
1013
1016
>>> d.get('d', None)
1013
1016
>>> d.get('f', 2)
2
>>> d.get('f')
Expand Down Expand Up @@ -286,9 +286,9 @@ def set(self, name: Any, value: Any) -> None:
Examples:
>>> d = FlatDict()
>>> d.set('d', 1013)
>>> d.set('d', 1016)
>>> d.get('d')
1013
1016
>>> d['n'] = 'chang'
>>> d.n
'chang'
Expand Down Expand Up @@ -451,12 +451,12 @@ def setattr(self, name: str, value: Any) -> None:
>>> d.setattr('attr', 'value')
>>> d.getattr('attr')
'value'
>>> d.set('d', 1013)
>>> d.set('d', 1016)
>>> d.setattr('d', 1031) # RuntimeWarning: d already exists in FlatDict.
>>> d.get('d')
1013
1016
>>> d.d
1013
1016
>>> d.getattr('d')
1031
"""
Expand Down
44 changes: 22 additions & 22 deletions chanfig/nested_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,24 @@ class NestedDict(DefaultDict): # pylint: disable=E1136
('n'): 'chang'
)
)
>>> NestedDict({"i.d": [{'c': 1013}, {'k': 1031}]})
>>> NestedDict({"i.d": [{'c': 1016}, {'k': 1031}]})
NestedDict(
('i'): NestedDict(
('d'): [NestedDict(
('c'): 1013
('c'): 1016
), NestedDict(
('k'): 1031
)]
)
)
>>> d = NestedDict({"f.n": "chang"}, default_factory=NestedDict)
>>> d.i.d = 1013
>>> d.i.d = 1016
>>> d['i.d']
1013
1016
>>> d.i.d
1013
1016
>>> d.dict()
{'f': {'n': 'chang'}, 'i': {'d': 1013}}
{'f': {'n': 'chang'}, 'i': {'d': 1016}}
"""

convert_mapping = False
Expand Down Expand Up @@ -333,22 +333,22 @@ def get(self, name: Any, default: Any = None, fallback: bool | None = None) -> A
TypeError: If `name` is not hashable.
Examples:
>>> d = NestedDict({"i.d": 1013}, default_factory=NestedDict)
>>> d = NestedDict({"i.d": 1016}, default_factory=NestedDict)
>>> d.get('i.d')
1013
1016
>>> d['i.d']
1013
1016
>>> d.i.d
1013
1016
>>> d.get('i.d', None)
1013
1016
>>> d.get('f', 2)
2
>>> d.get('a.b', None)
>>> d.f
NestedDict(<class 'chanfig.nested_dict.NestedDict'>, )
>>> del d.f
>>> d = NestedDict({"i.d": 1013})
>>> d = NestedDict({"i.d": 1016})
>>> d.e
Traceback (most recent call last):
AttributeError: 'NestedDict' object has no attribute 'e'
Expand Down Expand Up @@ -407,11 +407,11 @@ def set( # pylint: disable=W0221
Examples:
>>> d = NestedDict(default_factory=NestedDict)
>>> d.set('i.d', 1013)
>>> d.set('i.d', 1016)
>>> d.get('i.d')
1013
1016
>>> d.dict()
{'i': {'d': 1013}}
{'i': {'d': 1016}}
>>> d['f.n'] = 'chang'
>>> d.f.n
'chang'
Expand Down Expand Up @@ -496,9 +496,9 @@ def delete(self, name: Any) -> None:
name:
Examples:
>>> d = NestedDict({"i.d": 1013, "f.n": "chang"})
>>> d = NestedDict({"i.d": 1016, "f.n": "chang"})
>>> d.i.d
1013
1016
>>> d.f.n
'chang'
>>> d.delete('i.d')
Expand Down Expand Up @@ -548,9 +548,9 @@ def pop(self, name: Any, default: Any = Null) -> Any:
value: If `NestedDict` does not contain `name`, return `default`.
Examples:
>>> d = NestedDict({"i.d": 1013, "f.n": "chang", "n.a.b.c": 1}, default_factory=NestedDict)
>>> d = NestedDict({"i.d": 1016, "f.n": "chang", "n.a.b.c": 1}, default_factory=NestedDict)
>>> d.pop('i.d')
1013
1016
>>> d.pop('i.d', True)
True
>>> d.pop('i.d')
Expand Down Expand Up @@ -596,12 +596,12 @@ def setdefault( # type: ignore[override] # pylint: disable=R0912,W0221
value: If `NestedDict` does not contain `name`, return `value`.
Examples:
>>> d = NestedDict({"i.d": 1013, "f.n": "chang", "n.a.b.c": 1})
>>> d = NestedDict({"i.d": 1016, "f.n": "chang", "n.a.b.c": 1})
>>> d.setdefault("d.i", 1031)
1031
>>> d.setdefault("i.d", "chang")
1013
>>> d.setdefault("f.n", 1013)
1016
>>> d.setdefault("f.n", 1016)
'chang'
>>> d.setdefault("n.a.b.d", 2)
2
Expand Down
4 changes: 2 additions & 2 deletions chanfig/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ def parse( # pylint: disable=R0912
Examples:
Note that all examples uses NestedDict instead of Config for avoiding circular import.
>>> p = ConfigParser()
>>> p.parse(['--i.d', '1013', '--f.n', 'chang']).dict()
{'i': {'d': 1013}, 'f': {'n': 'chang'}}
>>> p.parse(['--i.d', '1016', '--f.n', 'chang']).dict()
{'i': {'d': 1016}, 'f': {'n': 'chang'}}
Values in command line overrides values in `default_config` file.
>>> p = ConfigParser()
Expand Down
Loading

0 comments on commit 7c52f5a

Please sign in to comment.