eafig 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- eafig-0.1.0/.gitignore +11 -0
- eafig-0.1.0/LICENSE +21 -0
- eafig-0.1.0/PKG-INFO +35 -0
- eafig-0.1.0/README.md +3 -0
- eafig-0.1.0/examples/main.py +23 -0
- eafig-0.1.0/pyproject.toml +20 -0
- eafig-0.1.0/src/eafig/__init__.py +5 -0
- eafig-0.1.0/src/eafig/load.py +40 -0
- eafig-0.1.0/src/eafig/registry.py +98 -0
- eafig-0.1.0/src/eafig/save.py +32 -0
- eafig-0.1.0/src/eafig/state.py +4 -0
eafig-0.1.0/.gitignore
ADDED
eafig-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MugeTong
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
eafig-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: eafig
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A simple figure generation library
|
|
5
|
+
Project-URL: Homepage, https://github.com/MugeTong/eafig
|
|
6
|
+
Project-URL: Issues, https://github.com/MugeTong/eafig/issues
|
|
7
|
+
Author-email: MugeTong <here5320@gmail.com>
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2026 MugeTong
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Requires-Python: >=3.12
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# Eafig
|
|
34
|
+
|
|
35
|
+
Manage your hyperparameters more easily.
|
eafig-0.1.0/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from eafig import load_config, register_config, save_config
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@register_config
|
|
5
|
+
class MyConfig:
|
|
6
|
+
a: int
|
|
7
|
+
b: str
|
|
8
|
+
c: float = 1.0
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def main():
|
|
12
|
+
# Load config from a file
|
|
13
|
+
load_config()
|
|
14
|
+
|
|
15
|
+
# Create a config instance using the loaded config
|
|
16
|
+
config_instance = MyConfig(1, "asd")
|
|
17
|
+
print("Config instance:", config_instance)
|
|
18
|
+
|
|
19
|
+
# Save the current config to a file
|
|
20
|
+
save_config()
|
|
21
|
+
|
|
22
|
+
if __name__ == "__main__":
|
|
23
|
+
main()
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "eafig"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A simple figure generation library"
|
|
9
|
+
authors = [{ name = "MugeTong", email = "here5320@gmail.com" }]
|
|
10
|
+
license = {"file" = "LICENSE"}
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
requires-python = ">=3.12"
|
|
13
|
+
requires = [
|
|
14
|
+
"pyyaml",
|
|
15
|
+
"omegaconf"
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[project.urls]
|
|
19
|
+
Homepage = "https://github.com/MugeTong/eafig"
|
|
20
|
+
Issues = "https://github.com/MugeTong/eafig/issues"
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from omegaconf import OmegaConf
|
|
3
|
+
|
|
4
|
+
from .state import _LOADED_CONFIG, _LOAD_PATH
|
|
5
|
+
|
|
6
|
+
_initialized = False
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def load_config(
|
|
10
|
+
config_path: str | None = None,
|
|
11
|
+
cmd: bool = True,
|
|
12
|
+
) -> None:
|
|
13
|
+
"""Load configuration from a file and optionally from command line arguments.
|
|
14
|
+
|
|
15
|
+
Priorities (from highest to lowest):
|
|
16
|
+
1. Command line arguments
|
|
17
|
+
2. Configuration file
|
|
18
|
+
3. Given values for class instantiation
|
|
19
|
+
4. Default values in the decorator definition
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
global _initialized, _LOAD_PATH
|
|
23
|
+
if _initialized:
|
|
24
|
+
print("Configuration from file and command line already loaded. Skipping.")
|
|
25
|
+
return
|
|
26
|
+
_initialized = True
|
|
27
|
+
|
|
28
|
+
if config_path is None:
|
|
29
|
+
print(
|
|
30
|
+
f"No configuration file path provided. Using default path: '{_LOAD_PATH}'"
|
|
31
|
+
)
|
|
32
|
+
_LOAD_PATH = config_path or _LOAD_PATH
|
|
33
|
+
|
|
34
|
+
_LOADED_CONFIG.update(
|
|
35
|
+
OmegaConf.load(_LOAD_PATH) if os.path.exists(_LOAD_PATH) else {}
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
if cmd:
|
|
39
|
+
# Command line overrides have the highest priority
|
|
40
|
+
_LOADED_CONFIG.update(OmegaConf.from_cli())
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
from typing import (
|
|
2
|
+
Any,
|
|
3
|
+
Optional,
|
|
4
|
+
Type,
|
|
5
|
+
TypeVar,
|
|
6
|
+
cast,
|
|
7
|
+
get_type_hints,
|
|
8
|
+
dataclass_transform,
|
|
9
|
+
)
|
|
10
|
+
from dataclasses import MISSING, dataclass, fields, is_dataclass
|
|
11
|
+
|
|
12
|
+
from .state import _CURRENT_INSTANCES, _LOADED_CONFIG
|
|
13
|
+
|
|
14
|
+
T = TypeVar("T")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _convert_type(value: Any, target_type: Any) -> Any:
|
|
18
|
+
"""Best-effort conversion for loaded config values."""
|
|
19
|
+
if value is None:
|
|
20
|
+
return None
|
|
21
|
+
if isinstance(value, target_type):
|
|
22
|
+
return value
|
|
23
|
+
try:
|
|
24
|
+
return target_type(value)
|
|
25
|
+
except Exception as e:
|
|
26
|
+
raise TypeError(
|
|
27
|
+
f"Failed to convert {value} ({type(value)}) to {target_type}"
|
|
28
|
+
) from e
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass_transform()
|
|
32
|
+
def register_config(cls: Optional[Type[T]] = None, /, *, frozen: bool = False):
|
|
33
|
+
"""
|
|
34
|
+
A decorator to register a config class to the registry.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def wrap(cls: Type[T]) -> Type[T]:
|
|
38
|
+
if not is_dataclass(cls):
|
|
39
|
+
cls = dataclass(cls, frozen=frozen)
|
|
40
|
+
|
|
41
|
+
ori_init = cls.__init__
|
|
42
|
+
ori_cls = cast(type[Any], cls)
|
|
43
|
+
|
|
44
|
+
def new_init(self, *args, **kwargs):
|
|
45
|
+
field_list = list(fields(ori_cls))
|
|
46
|
+
if len(args) > len(field_list):
|
|
47
|
+
raise TypeError(
|
|
48
|
+
f"Too many positional arguments for '{ori_cls.__name__}'"
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# Parameters provided via class instantiation
|
|
52
|
+
provided = {field_list[i].name: arg for i, arg in enumerate(args)}
|
|
53
|
+
provided.update(kwargs)
|
|
54
|
+
if frozen and provided:
|
|
55
|
+
raise TypeError(
|
|
56
|
+
f"Cannot provide parameters to frozen configuration '{ori_cls.__name__}'"
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# Type hints for type conversion
|
|
60
|
+
hints = get_type_hints(ori_cls)
|
|
61
|
+
|
|
62
|
+
# Loaded configuration from file or command line
|
|
63
|
+
loaded_config = _LOADED_CONFIG.get(ori_cls.__name__, {})
|
|
64
|
+
|
|
65
|
+
# Priority: command > file > provided > default
|
|
66
|
+
init_kwargs = {}
|
|
67
|
+
for f in field_list:
|
|
68
|
+
if f.name in loaded_config:
|
|
69
|
+
value = loaded_config[f.name]
|
|
70
|
+
elif f.name in provided:
|
|
71
|
+
value = provided[f.name]
|
|
72
|
+
elif f.default is not MISSING:
|
|
73
|
+
value = f.default
|
|
74
|
+
elif f.default_factory is not MISSING:
|
|
75
|
+
value = f.default_factory()
|
|
76
|
+
else:
|
|
77
|
+
raise TypeError(
|
|
78
|
+
f"{ori_cls.__name__}.__init__() missing required field: '{f.name}'"
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
if f.name in hints:
|
|
82
|
+
value = _convert_type(value, hints[f.name])
|
|
83
|
+
init_kwargs[f.name] = value
|
|
84
|
+
|
|
85
|
+
ori_init(self, **init_kwargs)
|
|
86
|
+
if ori_cls.__name__ in _CURRENT_INSTANCES:
|
|
87
|
+
raise ValueError(
|
|
88
|
+
f"Registered configuration '{ori_cls.__name__}' already has an instance. Multiple instances are not allowed."
|
|
89
|
+
)
|
|
90
|
+
_CURRENT_INSTANCES[ori_cls.__name__] = self
|
|
91
|
+
|
|
92
|
+
ori_cls.__init__ = new_init
|
|
93
|
+
return ori_cls
|
|
94
|
+
|
|
95
|
+
if cls is None:
|
|
96
|
+
return wrap
|
|
97
|
+
else:
|
|
98
|
+
return wrap(cls)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from dataclasses import asdict
|
|
2
|
+
import os
|
|
3
|
+
from typing import Dict, Any
|
|
4
|
+
import yaml
|
|
5
|
+
from .state import _CURRENT_INSTANCES, _LOADED_CONFIG, _SAVE_PATH
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def parse_config() -> Dict[str, Any]:
|
|
9
|
+
"""
|
|
10
|
+
Parse all stored configuration.
|
|
11
|
+
"""
|
|
12
|
+
config = {}
|
|
13
|
+
for name, instance in _CURRENT_INSTANCES.items():
|
|
14
|
+
config[name] = asdict(instance)
|
|
15
|
+
|
|
16
|
+
for name, loaded in _LOADED_CONFIG.items():
|
|
17
|
+
if name not in config:
|
|
18
|
+
config[name] = loaded
|
|
19
|
+
return config
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def save_config(to: str | None = None) -> None:
|
|
23
|
+
"""
|
|
24
|
+
Save the current loaded configuration to a file.
|
|
25
|
+
"""
|
|
26
|
+
global _SAVE_PATH
|
|
27
|
+
_SAVE_PATH = to or _SAVE_PATH
|
|
28
|
+
config_to_save = parse_config()
|
|
29
|
+
|
|
30
|
+
os.makedirs(os.path.dirname(_SAVE_PATH), exist_ok=True)
|
|
31
|
+
with open(_SAVE_PATH, "w") as f:
|
|
32
|
+
yaml.dump(config_to_save, f)
|