diff --git a/chanfig/config.py b/chanfig/config.py index 1770ad69..94a5d72f 100755 --- a/chanfig/config.py +++ b/chanfig/config.py @@ -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(, ) >>> 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 @@ -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 @@ -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 """ @@ -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) @@ -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 """ @@ -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) @@ -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 @@ -480,7 +480,7 @@ def get(self, name: Any, default: Any = None, fallback: bool | None = None) -> A >>> d.freeze() Config(, ('i'): Config(, - ('d'): 1013 + ('d'): 1016 ) ) >>> d.f @@ -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) @@ -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') @@ -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) diff --git a/chanfig/flat_dict.py b/chanfig/flat_dict.py index ffcc9d72..023fdc4a 100644 --- a/chanfig/flat_dict.py +++ b/chanfig/flat_dict.py @@ -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 @@ -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') @@ -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' @@ -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 """ diff --git a/chanfig/nested_dict.py b/chanfig/nested_dict.py index 40364075..68216666 100644 --- a/chanfig/nested_dict.py +++ b/chanfig/nested_dict.py @@ -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 @@ -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(, ) >>> 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' @@ -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' @@ -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') @@ -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') @@ -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 diff --git a/chanfig/parser.py b/chanfig/parser.py index 44bbe17a..7217ebd3 100644 --- a/chanfig/parser.py +++ b/chanfig/parser.py @@ -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() diff --git a/chanfig/variable.py b/chanfig/variable.py index 9b5a038d..e7c9f0ef 100644 --- a/chanfig/variable.py +++ b/chanfig/variable.py @@ -147,7 +147,7 @@ def dtype(self) -> type: Data type of the object wrapped in `Variable`. Examples: - >>> id = Variable(1013) + >>> id = Variable(1016) >>> type(id) >>> id.dtype @@ -238,11 +238,11 @@ def to(self, cls: Callable) -> Any: # pylint: disable=C0103 cls: The type to convert to. Examples: - >>> id = Variable(1013) + >>> id = Variable(1016) >>> id.to(float) - 1013.0 + 1016.0 >>> id.to(str) - '1013.0' + '1016.0' """ self.value = cls(self.value) @@ -253,9 +253,9 @@ def int(self) -> int: Convert the object wrapped in `Variable` to python `int`. Examples: - >>> id = Variable(1013.0) + >>> id = Variable(1016.0) >>> id.int() - 1013 + 1016 """ return self.to(int) @@ -265,9 +265,9 @@ def float(self) -> float: Convert the object wrapped in `Variable` to python `float`. Examples: - >>> id = Variable(1013) + >>> id = Variable(1016) >>> id.float() - 1013.0 + 1016.0 """ return self.to(float) @@ -277,9 +277,9 @@ def str(self) -> str: Convert the object wrapped in `Variable` to python `float`. Examples: - >>> id = Variable(1013) + >>> id = Variable(1016) >>> id.str() - '1013' + '1016' """ return self.to(str) @@ -289,7 +289,7 @@ def wrap(self) -> None: Wrap the type of `Variable`. Examples: - >>> id = Variable(1013) + >>> id = Variable(1016) >>> id.unwrap() >>> isinstance(id, int) False @@ -305,7 +305,7 @@ def unwrap(self) -> None: Unwrap the type of `Variable`. Examples: - >>> id = Variable(1013) + >>> id = Variable(1016) >>> id.unwrap() >>> isinstance(id, int) False @@ -319,7 +319,7 @@ def unwrapped(self): Context manager which temporarily unwrap the `Variable`. Examples: - >>> id = Variable(1013) + >>> id = Variable(1016) >>> isinstance(id, int) True >>> with id.unwrapped(): diff --git a/demo/config.json b/demo/config.json index eb730901..a17e959f 100644 --- a/demo/config.json +++ b/demo/config.json @@ -1,6 +1,6 @@ { "name": "CHANfiG", - "seed": 1013, + "seed": 1016, "activation": "GELU", "optim": { "lr": 0.001 @@ -21,5 +21,5 @@ }, "dropout": 0.1 }, - "id": "CHANfiG_1013" + "id": "CHANfiG_1016" } \ No newline at end of file diff --git a/demo/config.py b/demo/config.py index d78dc44f..a4f52bd1 100644 --- a/demo/config.py +++ b/demo/config.py @@ -29,7 +29,7 @@ class DataloaderConfig(Config): class TestConfig(Config): name: str = "CHANfiG" - seed: int = 1013 + seed: int = 1016 activation: str = "GELU" dataloader: DataloaderConfig = DataloaderConfig() diff --git a/demo/config.yaml b/demo/config.yaml index c51b56cd..0a7b5674 100644 --- a/demo/config.yaml +++ b/demo/config.yaml @@ -3,7 +3,7 @@ dataloader: batch_size: 64 num_workers: 4 pin_memory: true -id: CHANfiG_1013 +id: CHANfiG_1016 model: decoder: dropout: 0.1 @@ -15,4 +15,4 @@ model: name: CHANfiG optim: lr: 0.001 -seed: 1013 +seed: 1016 diff --git a/tests/test_config.py b/tests/test_config.py index ad777227..0161eaf7 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -42,7 +42,7 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) num_classes = Variable(10) self.name = "CHANfiG" - self.seed = Variable(1013, help="random seed") + self.seed = Variable(1016, help="random seed") data_factory = partial(DataConfig, name="CIFAR10") self.datasets = Config(default_factory=data_factory) self.datas = Config(default_factory=data_factory) @@ -71,7 +71,7 @@ def test_post(self): config = TestConfig() config.boot() assert config.name == "chanfig" - assert config.id == "chanfig_1013" + assert config.id == "chanfig_1016" assert config.datasets.a.name == "cifar10" def test_parse(self): @@ -126,7 +126,7 @@ def test_variable(self): def test_fstring(self): config = TestConfig() - assert f"seed{config.seed}" == "seed1013" + assert f"seed{config.seed}" == "seed1016" def test_load(self): config = TestConfig() diff --git a/tests/test_configclass.py b/tests/test_configclass.py index edc92d42..5dbc21c5 100644 --- a/tests/test_configclass.py +++ b/tests/test_configclass.py @@ -22,7 +22,7 @@ class AncestorConfig: __test__ = False name: str = "Chang" - seed: int = Variable(1013, help="random seed") + seed: int = Variable(1016, help="random seed") @configclass diff --git a/tests/test_flat_dict.py b/tests/test_flat_dict.py index 4576ed65..8bdb8799 100644 --- a/tests/test_flat_dict.py +++ b/tests/test_flat_dict.py @@ -106,9 +106,9 @@ def test_construct_namespace(self): parser = ArgumentParser() parser.add_argument("--name", type=str) parser.add_argument("--seed", type=int) - d = FlatDict(parser.parse_args(["--name", "chang", "--seed", "1013"])) + d = FlatDict(parser.parse_args(["--name", "chang", "--seed", "1016"])) assert d.name == "chang" - assert d.seed == 1013 + assert d.seed == 1016 def test_conflicts(self): d = FlatDict(keys=0, values=1, items=2) diff --git a/tests/test_nested_dict.py b/tests/test_nested_dict.py index 8a19fc9e..6f977162 100644 --- a/tests/test_nested_dict.py +++ b/tests/test_nested_dict.py @@ -21,15 +21,15 @@ class Test: - dict = NestedDict({"i.d": 1013, "f.n": "chang"}) + dict = NestedDict({"i.d": 1016, "f.n": "chang"}) def test_dict(self): - assert self.dict == NestedDict({"i.d": 1013, "f.n": "chang"}) - assert self.dict == NestedDict(**{"i.d": 1013, "f.n": "chang"}) + assert self.dict == NestedDict({"i.d": 1016, "f.n": "chang"}) + assert self.dict == NestedDict(**{"i.d": 1016, "f.n": "chang"}) def test_list(self): - assert self.dict == NestedDict([("i.d", 1013), ("f.n", "chang")]) - assert self.dict == NestedDict(*[("i.d", 1013), ("f.n", "chang")]) + assert self.dict == NestedDict([("i.d", 1016), ("f.n", "chang")]) + assert self.dict == NestedDict(*[("i.d", 1016), ("f.n", "chang")]) def test_contains(self): assert "f" in self.dict @@ -43,7 +43,7 @@ def test_sub_dict(self): assert self.dict["n.l"] == "liu" def test_interpolate(self): - d = NestedDict({"i.d": 1013, "i.i.d": "${i.d}"}) + d = NestedDict({"i.d": 1016, "i.i.d": "${i.d}"}) d.interpolate() assert d.i.d is d.i.i.d @@ -94,9 +94,9 @@ def test_fallback(self): assert d.get("n.b.d", fallback=True) == 0.5 def test_to_dict(self): - d = NestedDict({"i.d": 1013, "f.n": "chang"}) - assert d.dict() == {"i": {"d": 1013}, "f": {"n": "chang"}} - assert d.dict(flatten=True) == {"i.d": 1013, "f.n": "chang"} + d = NestedDict({"i.d": 1016, "f.n": "chang"}) + assert d.dict() == {"i": {"d": 1016}, "f": {"n": "chang"}} + assert d.dict(flatten=True) == {"i.d": 1016, "f.n": "chang"} class ConfigDict(NestedDict):