Skip to main content

Configs in Python made smooth and simple

Project description

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:

poetry add betterconf

However, you can use pip:

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:

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.

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.

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 for details.

Made with :heart: by prostomarkeloff and our contributors.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

betterconf-4.1.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

betterconf-4.1.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file betterconf-4.1.0.tar.gz.

File metadata

  • Download URL: betterconf-4.1.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.9.12-3-MANJARO

File hashes

Hashes for betterconf-4.1.0.tar.gz
Algorithm Hash digest
SHA256 210d42917c0a393ec00bb195a6976bbcccee020b82b50a80810ffb07f4872fcd
MD5 f8b049bc21cb7312eed4a45e03374840
BLAKE2b-256 13617864448f9c72626065aa35d3f6e372a316d4658665c5ec342497fd902f83

See more details on using hashes here.

File details

Details for the file betterconf-4.1.0-py3-none-any.whl.

File metadata

  • Download URL: betterconf-4.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.9.12-3-MANJARO

File hashes

Hashes for betterconf-4.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 acf21ac76d21032e40317e6d5f979af6dabc19742626819525289ae2890c9a9b
MD5 e84bad2b18cab206ed8ef8808c559898
BLAKE2b-256 cf6d141528767a03dca4c16d2ec0383ae3179bfc5f07800c7bbd0b1084c34cd8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page