Skip to content

kyoto7250/fastconfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fastconfig - Type-hint based config reader in python3!

A lightweight way to find the project root and load config in python3. current support is 3.9, 3.10, 3.11.

This library provides two functionalities:

  • A function to search for files while traversing up to the project root.
    • fastconfig.find_project_root
    • fastconfig.is_project_root
    • fastconfig.search
  • A function to directly build a class from a configuration file.
    • fastconfig.config.FastConfig
      • build
      • to_dict

Install

pip install fastconfig

Usage

  • example.json
{
    "setting_path": "setting_path",
    "section": {
        "numeric": 42
    }
}
  • other.toml
setting_path = "setting_path"
[section]
numeric = 24
# main.py
from fastconfig import FastConfig, fc_field, FastConfigError, search
from dataclasses import dataclass


@dataclass
class Config(FastConfig):
    result: int = fc_field(key="section.numeric", default=-1)
    # If metadata does not exist, it is searched by variable name.
    # If metadata does not exist and the variable not found in the target file, raise FastConfigError (MissingRequiredElementError)
    setting_path: str = fc_field(default="default")
    # Type-checking is done based on the type of dataclass. Type-checking is recursive.
    dic: dict[str, int] = fc_field(key="section", default_factory=dict)


if path := search("example.json"):
    try:
        # build instance
        config = Config.build(path)
        assert config == Config(
            result=42, setting_path="setting_path", dic={"numeric": 42}
        )
    except FastConfigError:
        raise RuntimeError
else:
    config = Config()

if other_path := search("other.toml"):
    # can update config
    config = Config.build(other_path, config)
    assert config == Config(result=24, setting_path="setting_path", dic={"numeric": 24})

Motivation

In many projects, it is common to write configuration files, read them in code, and build Config classes. I created this library to enable these functions to be implemented by simply defining a class and specifying a file name (such as pyproject.toml).

Contribution

If you have suggestions for features or improvements to the code, please feel free to create an issue first.

About

A lightweight way to find the project root and load a config

Resources

License

Stars

Watchers

Forks

Packages

No packages published