Skip to main content

A simple configuration library for Python

Project description

Deconfig

Decorator + Config

This is just another configuration library for Python. Create configuration objects and initialize them with data from different sources. It is designed to be extensible, and provides lots of features that are commonly used when dealing with configurations.

Installation

pip install deconfig

Quick Start

import deconfig as de
import os

os.environ["FOO"] = "bar"

# Deconfig uses EnvAdapter by default,
# but you can change this behaviour as follows:
de.set_default_adapters(de.EnvAdapter())


@de.config()
class ExampleConfig:

    @de.transformer.string() # Cast value to string
    @de.optional()           # Field can be null
    @de.field(name="foo")    # use name "foo" in configuration files
    def get_foo(self):
        pass


config = ExampleConfig()
config.get_foo() # Returns "bar"

Features

Optional

By default, deconfig considers all fields as required. But you can mark a field as optional by using the @de.optional() decorator.

import deconfig as de

@de.config(adapters=[de.EnvAdapter()])
class ExampleConfig:
    @de.optional()
    @de.field(name="foo")
    def get_foo(self):
        pass

    @de.field(name="bar")
    def get_bar(self):
        pass

config = ExampleConfig()
config.get_foo() # Returns None
config.get_bar() # Raises ValueError

Transformers

Configuration files can be very limiting. But with transformers, you can transform the value to any type/format you want.

import deconfig as de

capitalize = lambda x: x.capitalize()

@de.config(adapters=[de.EnvAdapter()])
class ExampleConfig:
    @de.transformer.integer()
    @de.field(name="foo")
    def get_foo(self):
        return "1"

    @de.transformer.comma_separated_array_string(str)
    @de.field(name="bar")
    def get_bar(self):
        return "default,values"

    @de.transformer.cast_custom(capitalize)
    @de.field(name="baz")
    def get_baz(self):
        return "hello"


config = ExampleConfig()
config.get_foo() # Returns 1
config.get_bar() # Returns ["default", "values"]
config.get_baz() # Returns "Hello"

Validator callbacks

You can also add custom validation logic to your fields.

import deconfig as de

def is_even(value) -> None:
    if not value % 2:
        raise ValueError("Value must be even")

@de.config(adapters=[de.EnvAdapter()])
class ExampleConfig:
    @de.validate(is_even)
    @de.field(name="foo")
    def get_foo(self):
        return 2

    @de.validate(is_even)
    @de.field(name="bar")
    def get_bar(self):
        return 3

config = ExampleConfig()
config.get_foo() # Returns 2
config.get_bar() # Raises ValueError

Customizations

Adapters are used to load data from different sources. By default, deconfig ships with adapter for environment variables and ini files. You can also create your own adapter for your custom use case.

Here is a pseudo adapter that sets all values to method name:

from typing import *
import deconfig as de

T = TypeVar("T")

class FooAdapter(de.AdapterBase):

    def get_field(self, field_name: str, method: Callable[..., T], *method_args, **method_kwargs) -> Any:
        return method.__name__

Read More

There is a lot more of options and features that you can use with deconfig. Check out the documentation for more information.

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

deconfig-0.1.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

deconfig-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file deconfig-0.1.0.tar.gz.

File metadata

  • Download URL: deconfig-0.1.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.19

File hashes

Hashes for deconfig-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9006846672f0ac1535848ecb71ca840bbe6b3f2b0498ecc1cd18e0f97c60dcd8
MD5 5ad25af59be31daaab0cfbeabf99d4a0
BLAKE2b-256 b323439262a0c1ee896cc8ddba659185168a4a07d345af4ad15f55731e64cf8d

See more details on using hashes here.

File details

Details for the file deconfig-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: deconfig-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.19

File hashes

Hashes for deconfig-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad8a47ca83c8a303d4781c1db7ba479269fe6b3f480819aa25186ea71c08c497
MD5 d5936e9e18f198bbeae12fbc6afd7e91
BLAKE2b-256 b9d32ba96a818fe168e16a24842f59d7c2731f4cf003d9c1d097711b68b9dcae

See more details on using hashes here.

Supported by

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