Name | fastconfig JSON |
Version |
0.2.0
JSON |
| download |
home_page | https://github.com/kyoto7250 |
Summary | A lightweight way to find the project root and load config |
upload_time | 2023-05-19 12:42:45 |
maintainer | |
docs_url | None |
author | kyoto7250 |
requires_python | >=3.8,<4.0 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 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_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
```bash
pip install fastconfig
```
## Usage
- `example.json`
```json
{
"setting_path": "setting_path",
"section": {
"numeric": 42
}
}
```
- `other.toml`
```toml
setting_path = "setting_path"
[section]
numeric = 24
```
```python
# 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.
Raw data
{
"_id": null,
"home_page": "https://github.com/kyoto7250",
"name": "fastconfig",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "kyoto7250",
"author_email": "kyoto7250@aol.com",
"download_url": "https://files.pythonhosted.org/packages/2f/92/b7658069b89524d74df3832b798e67ba27261026de63c67c74cffe310f04/fastconfig-0.2.0.tar.gz",
"platform": null,
"description": "# fastconfig\nA lightweight way to find the project root and load config in python3.\ncurrent support is [`3.9`, `3.10`, `3.11`].\n\nThis library provides two functionalities:\n\n* A function to search for files while traversing up to the project root.\n - `fastconfig.find_project_root`\n - `fastconfig.is_project_root`\n - `fastconfig.search`\n* A function to directly build a class from a configuration file.\n - `fastconfig.config.FastConfig`\n * `build`\n * `to_dict`\n\n\n## Install\n\n```bash\npip install fastconfig\n```\n\n## Usage\n\n- `example.json`\n```json\n{\n \"setting_path\": \"setting_path\",\n \"section\": {\n \"numeric\": 42\n }\n}\n```\n\n- `other.toml`\n```toml\nsetting_path = \"setting_path\"\n[section]\nnumeric = 24\n```\n\n```python\n# main.py\nimport fastconfig\nfrom fastconfig.config import FastConfig, fc_field\nfrom fastconfig.exception import FastConfigError\nfrom dataclasses import field, dataclass\n\n\n@dataclass\nclass Config(FastConfig):\n result: int = fc_field(key=\"section.numeric\", default=-1)\n # If metadata does not exist, it is searched by variable name\n setting_path: str = fc_field(default=\"default\")\n # Type checking is done based on the type of dataclass. Type checking is recursive.\n dic: dict[str, int] = fc_field(key=\"section\", default_factory=dict)\n\n\nif path := fastconfig.search(\"example.json\"):\n try:\n # build instance\n config = Config.build(path)\n assert config == Config(\n result=42, setting_path=\"setting_path\", dic={\"numeric\": 42}\n )\n except FastConfigError:\n raise RuntimeError\nelse:\n config = Config()\n\nif other_path := fastconfig.search(\"other.toml\"):\n # can update config\n config = Config.build(other_path, config)\n assert config == Config(result=24, setting_path=\"setting_path\", dic={\"numeric\": 24})\n```\n\n## Motivation\n\nIn 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).\n\n## Contribution\nIf you have suggestions for features or improvements to the code, please feel free to create an issue first.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A lightweight way to find the project root and load config",
"version": "0.2.0",
"project_urls": {
"Homepage": "https://github.com/kyoto7250",
"Repository": "https://github.com/kyoto7250/fastconfig"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "54888b07cdb2ef8f99c60944b3d9dbe7f6a8d224af2089a3ec81ae94c469c9ac",
"md5": "20958c1f292fab80195b53494323d8fe",
"sha256": "f37515044a6dcb14a6033d1bcc75d90678dbe879ecb0220c20d32593229f6643"
},
"downloads": -1,
"filename": "fastconfig-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "20958c1f292fab80195b53494323d8fe",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 8619,
"upload_time": "2023-05-19T12:42:42",
"upload_time_iso_8601": "2023-05-19T12:42:42.800731Z",
"url": "https://files.pythonhosted.org/packages/54/88/8b07cdb2ef8f99c60944b3d9dbe7f6a8d224af2089a3ec81ae94c469c9ac/fastconfig-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2f92b7658069b89524d74df3832b798e67ba27261026de63c67c74cffe310f04",
"md5": "121f1d98d7328727f65d8f5ed1e33dea",
"sha256": "d2ac6c982fa3b1a554208b711d59d22da35e605d0de689d9857ced8ee3cbd6f4"
},
"downloads": -1,
"filename": "fastconfig-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "121f1d98d7328727f65d8f5ed1e33dea",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 7510,
"upload_time": "2023-05-19T12:42:45",
"upload_time_iso_8601": "2023-05-19T12:42:45.080906Z",
"url": "https://files.pythonhosted.org/packages/2f/92/b7658069b89524d74df3832b798e67ba27261026de63c67c74cffe310f04/fastconfig-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-19 12:42:45",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kyoto7250",
"github_project": "fastconfig",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fastconfig"
}