fastconfig
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_rootfastconfig.is_project_rootfastconfig.search
- A function to directly build a class from a configuration file.
fastconfig.config.FastConfigbuildto_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
import fastconfig
from fastconfig.config import FastConfig, fc_field
from fastconfig.exception import FastConfigError
from dataclasses import field, 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
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 := fastconfig.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 := fastconfig.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.
Metadata
Release files for fastconfig 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastconfig-0.2.0.tar.gz | 7.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastconfig-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.1 kB
Release files / fastconfig-0.2.0.tar.gz
| Download URL | fastconfig-0.2.0.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d2ac6c982fa3b1a554208b711d59d22da35e605d0de689d9857ced8ee3cbd6f4
|
|
BLAKE2b-256 checksum How to use checksums |
2f92b7658069b89524d74df3832b798e67ba27261026de63c67c74cffe310f04
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.10 Darwin/22.4.0
|
Release files / fastconfig-0.2.0-py3-none-any.whl
| Download URL | fastconfig-0.2.0-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f37515044a6dcb14a6033d1bcc75d90678dbe879ecb0220c20d32593229f6643
|
|
BLAKE2b-256 checksum How to use checksums |
54888b07cdb2ef8f99c60944b3d9dbe7f6a8d224af2089a3ec81ae94c469c9ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.10 Darwin/22.4.0
|