Package: rootconfig
rootconfigrootconfiglibrary provides a convenient interface to parse, manage,
and export essential parameters in a Python project.
This measure of managing parameters can be used in projects like machine learning,
where various sets of hyper-parameters are experimented on a same code base.The core ofrootconfigis an abstractPythondataclasscalledRootConfig, which can be inherited to construct your own project configuration class.
The usage ofRootConfigclass is the same to any Pythondataclass.
You can declare attributes with type annotation.Basic UsageTo use theRootConfigclass, inherit it decorate it withdataclass.fromdataclassesimportdataclass,fieldfromfractionsimportFractionfrompathlibimportPathfromtypingimportLiteralfromrootconfigimportRootConfig@dataclassclassConfig(RootConfig):optimizer:Literal['AdamW','SGD']margin:Fractionlearning_rate:float=1e-4flags:list[bool]=field(default_factory=lambda:[True])You may directly create an instance.config=Config('SGD',learning_rate=float('nan'),margin=Fraction('4/3'),flags=[False])You may parse command-line arguments. All arguments are keyword arguments.config=Config.parse_args()# defaults to sys.argv[1:]# ORconfig=Config.parse_args([# '--learning-rate', '1e-4',  # default values can be safely omitted.'--optimizer','AdamW',# `Literal` arguments can be parsed.'--margin','3/4',# `Fraction` can be instantiated from a string. e.g. Fraction('3/4')'--flags','True','False'# A sequence of `bool` is supported by its Python literal.])# ORparser=...# You have your own Python's `ArgumentParser` instance.config=Config.parse_args(parser=parser)# Use your own parser.We offer first-class support to Python'sFraction,Decimal,complex,Path, andbool.listtype can be safely parsed either by providing multiple values.You may import from JSON files.config=Config.from_json(Path('/path/to/file'))...and you may export to a JSON file.config.to_json(Path('/path/to/file'))Non-serializable types likeFraction,Decimal,complex, andPathcan be safely imported and exported with special JSONObjectstructure.nan,inf, and-infare also supported.Type SupportsRootConfigautomatically check variable types when being instantiated.
If there are unsupported types or values do not match with their types,
anTypeErrorwould be raised.To fully support JSON import/export and Python'sArgumentParser,dataclassfields may only have the following types.String-convertable singleton types:intFractionDecimalfloatcomplexstrPathSingleton types that do not have one-to-one mapping with strings:bool: to supportboolin Python'sArgumentParser,
we explictely asked you to supply "True" or "False".
So far, we haven't considerArgumentParseractionstore_trueorstore_false.OthersLiteralFrom Python'stypingmodule,
supplied literals must be hashable.
Type arguments for "Literal" must beNoneor a literal value (int,bool, orstr).All literals must be in the same type.listlisttype can only accept singleton types aformentioned.Literaltype cannot be accepted.Supporting new types may cause some trouble to JSON serialization orArgumentParser.
For instance, it is very hard to parse an dictionary in command-line,
so, there is no support todict.
We may add support to some other types in the future,
but a treadoff to some features may be introduced.