Skip to main content

lunaconf is a Python library to support easy-to-understand configuration for evaluations

Project description

lunaconf

lunaconf is a Python library to support easy-to-understand configuration for evaluations

Installation

pip install lunaconf

Usage

It provides the following interfaces:

  • lunaconf.LunaConf: A base class that every configuration class should inherit from. It is a subclass of pydantic.BaseModel and thus Pydantic features can be used.

    import typing, lunaconf, pydantic
    
    class Config(lunaconf.LunaConf):
        require: int
        opt_int: int = 10
        opt_str: str = "default"
        opt_list: list[int] = pydantic.Field(default_factory=lambda: [1, 2, 3])
    
        @classmethod
        def __lunaconf_default__(cls) -> typing.Self:
            return cls(require=42)
    

    Here __lunaconf_default__ is a class method that should be overloaded if the class has required fields. It should return a default instance of the class to set default values for the fields.

  • lunaconf.lunaconf_cli: Construct a configuration from the CLI.

    # example.py
    import lunaconf
    
    config = lunaconf.lunaconf_cli(Config)
    

    Some CLI arguments and their corresponding generated configurations are as follows.

    $ python3 example.py
    # Config(require=42, opt_int=10, opt_str='default', opt_list=[1, 2, 3])
    
    $ python3 example.py -J config.json    # suppose config.json contains {"require": 100}
    # Config(require=100, opt_int=10, opt_str='default', opt_list=[1, 2, 3])
    
    $ python3 example.py opt_int=233
    # Config(require=42, opt_int=233, opt_str='default', opt_list=[1, 2, 3])
    
    $ python3 example.py opt_list.0=4
    # Config(require=42, opt_int=10, opt_str='default', opt_list=[4, 2, 3])
    

    The . in the CLI modifications can be used by nested fields and list indices:

    # config
    class Inner(lunaconf.LunaConf):
        a: int = 1
    class Outer(lunaconf.LunaConf):
        inner: typing.Optional[Inner] = None
        lst: list[int] = lunaconf.Field(default_factory=lambda: [1, 2, 3])
    
    $ python3 example.py
    # Outer(inner=None, lst=[1, 2, 3])
    $ python3 example.py inner.a=10 lst.1=20
    # Outer(inner=Inner(a=10), lst=[1, 20, 3])
    

    Available command-line options:

    • command positional arguments: specify the modifications to the configuration in the form of key1.key2=value1; key3.key4=value2 etc. The . can be used to access nested fields and list indices.
    • -j <json_str> / -J <json_file>: specify the JSON to overload the configuration.
    • -t <toml_str> / -T <toml_file>: specify the TOML to overload the configuration.
    • -d <str> / -D <file>: detect the format of the string/file and parse it accordingly. It will first try to parse it as JSON, if it fails, it will try to parse it as TOML. If both fail, an error will be raised.
    • -C <file>: the extra configuration file. This file contains command line arguments (one group per line) that will be parsed interleaved with the other command line arguments. Lines starting with # are treated as comments and ignored.
    • -a: whether or not output all fields with -p / -P flags, and also affect the application of post_action_with_all or post_action_without_all callables passed to lunaconf_cli.
    • -p: print the final configuration in JSON and exit.
    • -P: print the final configuration in TOML and exit.

Special Values

The following special values can be used in the command line arguments to represent certain Python values, and are output in some cases for unsupported values in JSON/TOML:

  • Input <null> leads to None; TOML will output <null> for None.
  • Input <del> to delete element in an array, or reset the field to its default.
  • Input <inf>, <-inf>, <nan> lead to float('inf'), float('-inf'), float('nan') respectively; JSON will output <inf>, <-inf>, <nan> for these values.

Strings inside the angle brackets are case-insensitive.

Examples

For more examples, please refer to the unit tests in the tests folder.

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

lunaconf-0.4.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

lunaconf-0.4.2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file lunaconf-0.4.2.tar.gz.

File metadata

  • Download URL: lunaconf-0.4.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.18

File hashes

Hashes for lunaconf-0.4.2.tar.gz
Algorithm Hash digest
SHA256 dce50155d6a3df9c94e674168b31c2b2a7d577ca32ba42acf2a892be5d0f217c
MD5 66266f3ce50fb3737a9be2fda37f28fd
BLAKE2b-256 5722e86b5d0c335ffd6a9eacfaeb488a7e899a1b297d2f5dbd12dd3d30c72328

See more details on using hashes here.

File details

Details for the file lunaconf-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: lunaconf-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.18

File hashes

Hashes for lunaconf-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8f8f9c13899db545639b650fb451adb7d2ca9336696368c30a9d4c14350a28e7
MD5 e6baa1c1d4459405ecdcb41f4e38cf44
BLAKE2b-256 b69a6d03f6e58fd41ec85ce95ba064ab80f9d2018d3bc0955b3577af563ddded

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