# Configs in Python made smooth and simple
Betterconf (**better config**) is a Python library for project configuration
management. It allows you define your config like a regular Python class.
Features:
* Easy to hack.
* Less boilerplate.
* Minimal code to do big things.
* Zero dependencies. Only vanilla Python >=3.11
Betterconf is heavily typed, so your editors and typecheckers will be happy with it.
It's not that huge as Pydantic, so takes like 5 minutes to dive into.
Betterconf is highly customizable, so you can do anything with it.
## Installation
I recommend you to use poetry:
```sh
poetry add betterconf
```
However, you can use pip:
```sh
pip install betterconf
```
## How to?
Betterconf is very intuitive, so you don't need special knowledge to use it. We'll cover the basics.
The most simple config will look like this:
```python
from betterconf import betterconf
@betterconf
class Config:
LOGIN: str
PASSWORD: str
cfg = Config()
print(config.LOGIN)
```
Let's dive into what it does. By default, betterconf uses `EnvironmentProvider`, getting values with `os.getenv`,
so you can run this example with `LOGIN=vasya PASSWORD=admin python config.py` and simply get your login.
But what if you need a different provider? Betterconf lets you set providers as for config itself and for each field respectively.
```python
import json
from betterconf import Alias
from betterconf import betterconf, field
from betterconf import JSONProvider, AbstractProvider
sample_json_config = json.dumps({"field": {"from": {"json": 123}}})
# our provider, that just gives the name of field back
class NameProvider(AbstractProvider):
def get(self, name: str) -> str:
return name
@betterconf(provider=NameProvider())
class Config:
# value will be got from NameProvider and will simply be `my_fancy_name`
my_fancy_name: str
# here we get value from JSONProvider; the default nested_access is '.'
field_from_json: Alias[int, "field::from::json"] = field(provider=JSONProvider(sample_json_config, nested_access="::"))
```
Betterconf casts primitive types itself, they include list, float, str, int. But if you need specific caster, say for complex object, you can write your own.
```python
from betterconf import betterconf
from betterconf import AbstractCaster, field
class DashesToDotsCaster(AbstractCaster):
def cast(self, val: str) -> str:
return val.replace("_", ".")
@betterconf
class Config:
simple_int: int
value: str = field(caster=DashesToDotsCaster())
print(Config(value="privet_mir", simple_int="55666").value)
```
Subconfigs and referencing one field in another declaration is also available. Check out examples folder.
## License
This project is licensed under MIT License.
See [LICENSE](LICENSE) for details.
Made with :heart: by [prostomarkeloff](https://github.com/prostomarkeloff) and our contributors.
Raw data
{
"_id": null,
"home_page": "https://github.com/prostomarkeloff/betterconf",
"name": "betterconf",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.11",
"maintainer_email": null,
"keywords": "configs, config, env, .env",
"author": "prostomarkeloff",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/e9/6f/b1febe73d003a7d31b300ece34d0c82bf973a0bc7924b2da1b4d6d8ebf88/betterconf-4.0.0.tar.gz",
"platform": null,
"description": "# Configs in Python made smooth and simple\n\nBetterconf (**better config**) is a Python library for project configuration\nmanagement. It allows you define your config like a regular Python class.\n\nFeatures:\n\n* Easy to hack.\n* Less boilerplate.\n* Minimal code to do big things.\n* Zero dependencies. Only vanilla Python >=3.11\n\nBetterconf is heavily typed, so your editors and typecheckers will be happy with it.\n\nIt's not that huge as Pydantic, so takes like 5 minutes to dive into.\n\nBetterconf is highly customizable, so you can do anything with it.\n\n## Installation\n\nI recommend you to use poetry:\n\n```sh\npoetry add betterconf\n```\n\nHowever, you can use pip:\n\n```sh\npip install betterconf\n```\n\n## How to?\n\nBetterconf is very intuitive, so you don't need special knowledge to use it. We'll cover the basics.\n\nThe most simple config will look like this:\n```python\nfrom betterconf import betterconf\n\n@betterconf\nclass Config:\n LOGIN: str\n PASSWORD: str\n\ncfg = Config()\nprint(config.LOGIN)\n```\n\nLet's dive into what it does. By default, betterconf uses `EnvironmentProvider`, getting values with `os.getenv`,\nso you can run this example with `LOGIN=vasya PASSWORD=admin python config.py` and simply get your login.\n\n\nBut what if you need a different provider? Betterconf lets you set providers as for config itself and for each field respectively.\n\n```python\nimport json\nfrom betterconf import Alias\nfrom betterconf import betterconf, field\nfrom betterconf import JSONProvider, AbstractProvider\n\nsample_json_config = json.dumps({\"field\": {\"from\": {\"json\": 123}}})\n\n# our provider, that just gives the name of field back\nclass NameProvider(AbstractProvider):\n def get(self, name: str) -> str:\n return name\n\n@betterconf(provider=NameProvider())\nclass Config:\n # value will be got from NameProvider and will simply be `my_fancy_name`\n my_fancy_name: str\n # here we get value from JSONProvider; the default nested_access is '.'\n field_from_json: Alias[int, \"field::from::json\"] = field(provider=JSONProvider(sample_json_config, nested_access=\"::\"))\n\n```\n\nBetterconf casts primitive types itself, they include list, float, str, int. But if you need specific caster, say for complex object, you can write your own.\n\n```python\nfrom betterconf import betterconf\nfrom betterconf import AbstractCaster, field\n\nclass DashesToDotsCaster(AbstractCaster):\n def cast(self, val: str) -> str:\n return val.replace(\"_\", \".\")\n\n@betterconf\nclass Config:\n simple_int: int\n value: str = field(caster=DashesToDotsCaster())\n\nprint(Config(value=\"privet_mir\", simple_int=\"55666\").value)\n```\n\nSubconfigs and referencing one field in another declaration is also available. Check out examples folder.\n\n## License\nThis project is licensed under MIT License.\n\nSee [LICENSE](LICENSE) for details.\n\n\nMade with :heart: by [prostomarkeloff](https://github.com/prostomarkeloff) and our contributors.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Configs in Python made smooth and simple",
"version": "4.0.0",
"project_urls": {
"Documentation": "https://github.com/prostomarkeloff/betterconf",
"Homepage": "https://github.com/prostomarkeloff/betterconf",
"Repository": "https://github.com/prostomarkeloff/betterconf"
},
"split_keywords": [
"configs",
" config",
" env",
" .env"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5a459759174a7c17ad5c5812c50fe7eab1bd2f46fa61ca17ea961db5a27e5871",
"md5": "f706624c884fabdc1f81ac35f9afcd39",
"sha256": "4c1a629ec7ed26559c2c6308842867b7fa2b14085ccdbbf128758a35f30f68ea"
},
"downloads": -1,
"filename": "betterconf-4.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f706624c884fabdc1f81ac35f9afcd39",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.11",
"size": 10704,
"upload_time": "2024-10-06T20:47:05",
"upload_time_iso_8601": "2024-10-06T20:47:05.079341Z",
"url": "https://files.pythonhosted.org/packages/5a/45/9759174a7c17ad5c5812c50fe7eab1bd2f46fa61ca17ea961db5a27e5871/betterconf-4.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e96fb1febe73d003a7d31b300ece34d0c82bf973a0bc7924b2da1b4d6d8ebf88",
"md5": "b7d28e48c6daf3664289e554b4350f2d",
"sha256": "07f40e55b185d5c43eff0819051421a8fa742ecf21be02947907d613754b8e4a"
},
"downloads": -1,
"filename": "betterconf-4.0.0.tar.gz",
"has_sig": false,
"md5_digest": "b7d28e48c6daf3664289e554b4350f2d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.11",
"size": 9272,
"upload_time": "2024-10-06T20:47:06",
"upload_time_iso_8601": "2024-10-06T20:47:06.816789Z",
"url": "https://files.pythonhosted.org/packages/e9/6f/b1febe73d003a7d31b300ece34d0c82bf973a0bc7924b2da1b4d6d8ebf88/betterconf-4.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-06 20:47:06",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "prostomarkeloff",
"github_project": "betterconf",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "betterconf"
}