Skip to content
/ runch Public

Refined munch: attribute-style access for python dicts, with generic typing support. Also supports reading & managing YAML/TOML/JSON configs directly.

License

Notifications You must be signed in to change notification settings

XieJiSS/runch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Runch

Refined munch. Provides basic munch functionality (attribute-style access for python dicts) with additional generic typing support and runtime validation.

Also provides a config reader that reads config files into predefined runch models. Say goodbye to config["key"], config.get("key") and runtime errors caused by missing keys!

Installation

pip install runch

If you find any bugs, please submit an issue or a pull request at GitHub.

Usage

# This model definition can be auto-generated by runch, based on existing config files.

from runch import RunchModel, RunchConfigReader

class ExampleConfig(RunchModel):
    db_host: str
    db_port: int
    db_user: str
    db_password: str
    db_name: str

# Read config from file.                 ↓ square brackets
example_config_reader = RunchConfigReader[ExampleConfig](
#                                         ^^^^^^^^^^^^^ Config model class name
    config_name="example_config_file",  # without extension
    config_dir="example_config_dir",    # default is os.environ.get("RUNCH_CONFIG_DIR", "./etc")
    config_ext="yaml"                   # default is "yaml"
    config_encoding="utf-8"             # default is "utf-8"
)
example_config = example_config_reader.read()  # Or .read_lazy() for lazy loading

print(example_config.config.db_host)  # with awesome intellicode support & runtime validation!
$ touch example_config_dir/example_config_file.yaml
db_host: localhost
db_port: 5432
db_user: user
db_password: password
db_name: database

Supported File Formats

  • YAML
  • JSON
  • TOML
  • arbitrary file formats with custom reader, specified via the custom_config_loader param of RunchConfigReader.__init__(). The custom reader should be a callable that takes a str-type file content and returns a dictionary.

Model Definition Generator

$ python -m runch <config_path> [config_ext]

Manual:

Usage: python -m runch <config_path> [config_name [config_ext]]
    Generate a model definition from a config file.

    config_path: path to your config file.
    config_ext: content type of your config file. Default is `yaml`.

    Example:
        python -m runch path/to/my_config.foo
        python -m runch path/to/my_config.foo chat_config
        python -m runch path/to/my_config.foo chat_config yaml

About

Refined munch: attribute-style access for python dicts, with generic typing support. Also supports reading & managing YAML/TOML/JSON configs directly.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages