# 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/13/61/7864448f9c72626065aa35d3f6e372a316d4658665c5ec342497fd902f83/betterconf-4.1.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.1.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": "cf6d141528767a03dca4c16d2ec0383ae3179bfc5f07800c7bbd0b1084c34cd8",
"md5": "e84bad2b18cab206ed8ef8808c559898",
"sha256": "acf21ac76d21032e40317e6d5f979af6dabc19742626819525289ae2890c9a9b"
},
"downloads": -1,
"filename": "betterconf-4.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e84bad2b18cab206ed8ef8808c559898",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.11",
"size": 10937,
"upload_time": "2024-11-18T13:39:54",
"upload_time_iso_8601": "2024-11-18T13:39:54.115123Z",
"url": "https://files.pythonhosted.org/packages/cf/6d/141528767a03dca4c16d2ec0383ae3179bfc5f07800c7bbd0b1084c34cd8/betterconf-4.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "13617864448f9c72626065aa35d3f6e372a316d4658665c5ec342497fd902f83",
"md5": "f8b049bc21cb7312eed4a45e03374840",
"sha256": "210d42917c0a393ec00bb195a6976bbcccee020b82b50a80810ffb07f4872fcd"
},
"downloads": -1,
"filename": "betterconf-4.1.0.tar.gz",
"has_sig": false,
"md5_digest": "f8b049bc21cb7312eed4a45e03374840",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.11",
"size": 9444,
"upload_time": "2024-11-18T13:39:55",
"upload_time_iso_8601": "2024-11-18T13:39:55.805257Z",
"url": "https://files.pythonhosted.org/packages/13/61/7864448f9c72626065aa35d3f6e372a316d4658665c5ec342497fd902f83/betterconf-4.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-18 13:39:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "prostomarkeloff",
"github_project": "betterconf",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "betterconf"
}