Dataclass to be CLI and logger compatible
- Python 100%
| .forgejo/workflows | Update pipelines | |
| .vscode | first commit | |
| dataclasses_reverse_cli | Fix spaced strings parsing | |
| tests | Fix spaced strings parsing | |
| .gitignore | Add the abillity to ignore some attributes | |
| .python-version | first commit | |
| LICENSE.md | Add LICENSE and urls | |
| pyproject.toml | Add the abillity to ignore some attributes | |
| README.md | first commit | |
| uv.lock | Add the abillity to ignore some attributes | |
Dataclass to be CLI and logger compatible
This package does only two things:
- it takes a dataclass and converts it to a command line str.
- it takes a dataclass and converts it to a loggable dict for 3rd party logging like MLFlow.
Contrary to
asdictit also converts nested dataclasses by separting their attributes by prefix.
By inheriting from the class ReverseCli you can use the to_command_string method to get the command line str and the property loggable_dict to get the loggable dict.
Example
@dataclass(kw_only=True)
class Parameters(ReverseCli):
a: int
b: str
c: bool
d: list[int]
@dataclass(kw_only=True)
class Nested(ReverseCli):
a: int
b: Parameters
parameters = Nested(a=1, b=Parameters(a=1, b="2", c=True, d=[3, 4]))
print(parameters.to_command_string())
# --a 1 --b.a 1 --b.b 2 --b.c --b.d 3 --b.d 4
print(parameters.loggable_dict)
# {'a': 1, 'b.a': 1, 'b.b': '2', 'b.c': True, 'b.d': [3, 4]}
Installation
We use UV for installation and RUFF for formating and linting.