Skip to main content

Lightweight and opinionated config library for your Python services.

Project description

minicfg

📑 minicfg is a lightweight, minimalistic and easy-to-use configuration package for your Python services.

Features

  • Lightweight: minicfg is a small package with no dependencies.
  • Easy to use: minicfg provides a simple API to define and populate configurations.
  • Type casting: minicfg supports type casting for the fields.
  • File attachment: minicfg supports attaching a file to a field.
  • Prefixing: minicfg supports prefixing the fields with a custom name prefix.
  • Nested configurations: minicfg supports nested configurations.
  • Custom providers: minicfg supports custom providers to populate the configuration from different sources.

Installation

Just install minicfg using your favorite package manager, for example:

pip install minicfg

Usage

from minicfg import Minicfg, Field, minicfg_prefix
from minicfg.caster import IntCaster

@minicfg_prefix("MYSERVICE")
class Env(Minicfg):
    @minicfg_prefix("BOT")
    class TelegramBot(Minicfg):
        # attach_file_field=True will read the value from the file provided in MYSERVICE_BOT_TOKEN_FILE env var
        # if no file is provided, it will read the value from MYSERVICE_BOT_TOKEN env var.
        TOKEN = Field(attach_file_field=True)
    
    class API1(Minicfg):  # <-- API1 class name will be used as a prefix for the fields inside it
        API_TOKEN = Field()  # API_TOKEN will be read from MYSERVICE_API1_API_TOKEN env var

    @minicfg_prefix("MONGO")
    class Mongo(Minicfg):
        HOST = Field()
        PORT = Field(caster=IntCaster())  # PORT will be casted to an integer type
        

# Populate the configuration from the environment variables:
env = Env.populated()

print(f"Telegram bot token: {env.TelegramBot.TOKEN}")
print(f"API1 token: {env.API1.API_TOKEN}")
print(f"Mongo settings: {env.Mongo.HOST}:{env.Mongo.PORT}")

"""
Try running the script with the following environment variables:
MYSERVICE_BOT_TOKEN=token MYSERVICE_API1_API_TOKEN=token123 MYSERVICE_MONGO_HOST=localhost MYSERVICE_MONGO_PORT=5432
And you should see the following output:

>>> Bot token: token
>>> API1 token: token123
>>> Mongo settings: localhost:5432
"""

More examples are available here.

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

minicfg-2.0.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

minicfg-2.0.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file minicfg-2.0.1.tar.gz.

File metadata

  • Download URL: minicfg-2.0.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.20.0.post1 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for minicfg-2.0.1.tar.gz
Algorithm Hash digest
SHA256 4d9b4d8888b3b0e27a01bf2ffa202b30d6695edbe862d9d2b0903c232b8050d5
MD5 72e44c6179274f0af55bf150daffcb2e
BLAKE2b-256 1ee9630407ee93b40b1a71b4fadbc8b5fed0d917e2c922469a99977b7c7c4fa2

See more details on using hashes here.

File details

Details for the file minicfg-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: minicfg-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.20.0.post1 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for minicfg-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e0928e81b4396fd84958c5289441033fa4da6dbc24b729a1cb897aaf58e641c7
MD5 ce96d7bb917b02e5e84080659d9f3234
BLAKE2b-256 f96b90e4e1934e7e99bd8d304949ed173eda8fbda8f30dcde3392a2efdbefe3e

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