Skip to main content

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.

Metadata

Release files for conjector 1.8.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 conjector 1.8.0
File Size Uploaded
conjector-1.8.0.tar.gz 13.7 kB Details

Built distribution (wheel)

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

Total release size: 26.1 kB

Release files / conjector-1.8.0.tar.gz

Download URL conjector-1.8.0.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5dd3e2b382fadc4617a0ad45c09f7677cc84c10ce9732d7567e16a9f5dd900d0
BLAKE2b-256 checksum
How to use checksums
40434abe4d517e9256ae6a5283324258009fb479e16a06c15fb621c85d72a22e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / conjector-1.8.0-py3-none-any.whl

Download URL conjector-1.8.0-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
44b88dd79043cfd8aa79d2008d259d4054f68348329cc01e4104cf36cd7d83ed
BLAKE2b-256 checksum
How to use checksums
9d686537b169d9d59e09bfa7958f1819d650d6ae70e04691df74946137bf9a43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

1.8.0 This release

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.0

1 release file

1.4.1

1 release file

1.4.0

2 release files

1.3.2

2 release files

1.3.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