Skip to main content

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.

There is a very usable thing in our fancy-web-world, called .envs. Betterconf, since 4.5.0 automatically supports them out-of-the-box! See it:

from betterconf import betterconf, DotenvProvider

# here betterconf gets values from `.env` file, you can change name or the path passing it as the first
# argument to provider.
# also, auto_load lets you not to write `provider.load_into_env` (loading variables to os.environ)
# or `provider.load_into_provider()` (loading them into the provider's inner storage).
@betterconf(provider=DotenvProvider(auto_load=True))
class Config:
    val1: int
    val2: str
    name: str

cfg = Config()
print(cfg.val1, cfg.val2, cfg.name)

Your .env then looks like this:

name="John Wicked"
val1=12
val2="testing value"

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.

Release files for betterconf 4.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for betterconf 4.5.0
File Size Uploaded
betterconf-4.5.0.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for betterconf 4.5.0
File Interpreter ABI Platform
betterconf-4.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.3 kB

Release files / betterconf-4.5.0.tar.gz

Download URL betterconf-4.5.0.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
aebc050d24cf8fdf837b204411c57afcbc837158a7fa21713d31b240b2a1f1d3
BLAKE2b-256 checksum
How to use checksums
e53071b6a8d16547db3003265dc03a7a677660bc1c244c46b1a8640e53f3bd87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.6 Linux/6.10.13-3-MANJARO

Release files / betterconf-4.5.0-py3-none-any.whl

Download URL betterconf-4.5.0-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee6ad8ae4c49a7f977555dcb435eb258eb044e10a2f43fdd77bb67b8ff682118
BLAKE2b-256 checksum
How to use checksums
183c86921e4e407f25413819e04471606c32377f47887af7873e0613f5036946
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.6 Linux/6.10.13-3-MANJARO

Release history Release notifications | RSS feed

This release

4.5.0 This release

2 release files

4.2.0

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.8.0

2 release files

2.7.0

2 release files

2.6.1

2 release files

2.5.1

2 release files

2.5

2 release files

2.1.1

2 release files

2.1

2 release files

2.0

2 release files

1.8

2 release files

1.5

2 release files

1.2

2 release files

1.1

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page