Skip to main content

No project description provided

Project description

PyPI - Python Version

rootconfig

rootconfig library 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 of rootconfig is an abstract Python dataclass called RootConfig, which can be inherited to construct your own project configuration class. The usage of RootConfig class is the same to any Python dataclass. You can declare attributes with type annotation.

Usage

To use the RootConfig class, inherit it decorate it with dataclass.

from dataclasses import dataclass, field
from fractions import Fraction
from pathlib import Path
from typing import Literal

from rootconfig import RootConfig


@dataclass
class Config(RootConfig):
    optimizer: Literal['AdamW', 'SGD']
    margin: Fraction
    learning_rate: float = 1e-4
    flags: 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:]
# OR
config= 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')
    '--debug', 'True', 'False'  # A sequence of `bool` is supported by its Python literal.
])

We offer first-class support to Python's Fraction, Decimal, complex, Path, and bool. list type 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 like Fraction, Decimal, complex, and Path can be safely imported and exported with special JSON Object structure. nan, inf, and -inf are also supported.

Type Supports

RootConfig automatically check variable types when being instantiated. If there are unsupported types or values do not match with their types, an TypeError would be raised.

To fully support JSON import/export and Python's ArgumentParser, dataclass fields may only have the following types.

  • String-convertable singleton types:
    • int
    • Fraction
    • Decimal
    • float
    • complex
    • str
    • Path
  • Singleton types that do not have one-to-one mapping with strings:
    • bool: to support bool in Python's ArgumentParser, we explictely asked you to supply "True" or "False". So far, we haven't consider ArgumentParser action store_true or store_false.
  • Others
    • Literal
      • From Python's typing module, supplied literals must be hashable. Type arguments for "Literal" must be None or a literal value (int, bool, or str).
      • All literals must be in the same type.
    • list

Supporting new types may cause some trouble to JSON serialization or ArgumentParser. For instance, it is very hard to parse an dictionary in command-line, so, there is no support to dict. We may add support to some other types in the future, but a treadoff to some features may be introduced.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rootconfig-0.1.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rootconfig-0.1.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file rootconfig-0.1.1.tar.gz.

File metadata

  • Download URL: rootconfig-0.1.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for rootconfig-0.1.1.tar.gz
Algorithm Hash digest
SHA256 37518002e69667f03018947e9237cfd65638e699ecd1429edc83cccc259c44bb
MD5 191f55560b66a515f2da9a53f5b4953d
BLAKE2b-256 b05eb94d4c580ced782e86efcd774bff79f754ab99d42746780fea29b408affb

See more details on using hashes here.

File details

Details for the file rootconfig-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: rootconfig-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for rootconfig-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da2cbdf7bfe14023fd0871c85dd97868e1b7f8684d6dddd07cca1951f50dad18
MD5 ffe547cd60478c6fdffec72b3c5f8254
BLAKE2b-256 7ab437ee414badf89a2debc5d5bdc2912ac6a76507e5d7b9e2c4a6b17d21c57b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page