Skip to main content

Config injector for Python

Project description

Python Config Injector

PyPI - Python Version PyPI - Package Version PyPI - License Documentation Status Build Coverage Status Code style: black Checked with mypy Imports: isort security: bandit

What is this

It is a simple library to inject non-sensitive configurations into class variables. Basically, it's like BaseSettings in pydantic library but for constants in json, yaml, toml or ini formats. conjector can work with different Python types (like tuple, datetime, dataclass and so on) and recursively cast config values to them.

More information about the library and all features you can find in the official documentation.

When to use

  • If you deal with constants in your code, like error messages, default values for something, numeric coefficients, and so on.
  • If you hate global variables, and you like non-python files to store static information.
  • If you want to have an easy way to manage different constants depending on environments (like test, dev, prod).
  • If you like type hints and clean code.

How to install

To install this library just enter:

pip install conjector

By default, conjector work only with the builtin json and ini deserializers. To work with yaml or toml (if you are using python <= 3.10):

pip install conjector[yaml]
# or
pip install conjector[toml]
# or faster version of json
pip install conjector[json]

How to use

For injecting values you need only the decorator properties under a target class. By default, the library will search a config file application.yml in the same directory where your file with the used decorator is located, like below:

project_root
|---services
|   |   email_message_service.py
|   |   application.yml
|.....

Example:

services/application.yml:

default_text_style:
  size: 14
  weight: bold
  font: "Times New Roman"
  color:
    - 128
    - 128
    - 128
language_greetings:
  - language: english
    text: hello
  - language: german
    text: hallo
  - language: french
    text: bonjour
wellcome_message: "{greeting}! Thank you for registration, {username}!"
mailing_frequency:
  days: 5
  hours: 12

services/email_message_service.py:

from typing import TypedDict
from dataclasses import dataclass
from datetime import timedelta
from conjector import properties


@dataclass
class TextStyle:
    size: int
    weight: str
    font: str
    color: tuple[int, int, int] | str


class GreetingDict(TypedDict):
    language: str
    text: str


@properties
class EmailMessageService:
    default_text_style: TextStyle
    language_greetings: list[GreetingDict]
    wellcome_message: str
    mailing_frequency: timedelta | None

    # And using these class variables in some methods...

And that's how will look an equivalent of the code above but with "hard-coded" constants, without config files and @properties decorator:

class EmailMessageService:
    default_text_style = TextStyle(
        size=14, weight="bold", font="Times New Roman", color=(128, 128, 128)
    )
    language_greetings = [
      GreetingDict(language="english", text="hello"),
      GreetingDict(language="german", text="hallo"),
      GreetingDict(language="french", text="bonjour"),
    ]
    wellcome_message = "{greeting}! Thank you for registration, {username}!"
    mailing_frequency = timedelta(days=5, hours=12)
    
    # And using these class variables in some methods...

All config values will be inserted and cast according to the type annotations once during the application or script start.

Different environments

Using this library it's easy to manage different environments and corresponding config files. It could be done like so:

import os
from conjector import properties


@properties(filename=os.getenv("CONFIG_FILENAME", "application.yml"))
class SomeEnvDependingService:
    env_depend_var: str

In this case, you can set CONFIG_FILENAME=application-dev.yml in env variables, and conjector will use that file.

About contributing

You will make conjector better if you open issues or create pull requests with improvements.

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

conjector-1.7.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

conjector-1.7.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file conjector-1.7.0.tar.gz.

File metadata

  • Download URL: conjector-1.7.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for conjector-1.7.0.tar.gz
Algorithm Hash digest
SHA256 725cc3e4fa96ff9c93cf4aac5e0a545174c383c440dfc08067fb95a76d8ef101
MD5 41b086d782aa71d7195f5bbf47a8632b
BLAKE2b-256 2f7a419551dc334d6b15da833281695dc30c2746843fe437e8875b38cbc9a2f6

See more details on using hashes here.

File details

Details for the file conjector-1.7.0-py3-none-any.whl.

File metadata

  • Download URL: conjector-1.7.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for conjector-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aaaf7f7aec2faa8b8629a5ad7a559cd3d45396454e08de88e0e29bfaffd05ffd
MD5 d7f0dda258ca9dff09d6ce1e7c55e234
BLAKE2b-256 60c2122b8edf47c34710580ce7ab04eaf61d1428978606dc886051911cead0a4

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