Skip to main content

A simple library for easy handling of .env files and environment variables configurations

Project description

Magic Config

A simple library for easy handling of .env files and environment variables configurations.

This library is a class for working with configurations. The class is implemented as a singleton, which allows you to always have exactly one instance of the same data registry everywhere.

The configuration data itself is taken from the .env file or from environment variables.

Installation

pip install magic-config

Upgrade

pip install --upgrade magic-config

Pypi url: https://pypi.org/project/magic-config/

Example:

DEBUG=1 myapp.py

My app script:

from magic_config import Config

# You can access to variables as property of class object
if Config.DEBUG:
  ...

# You can access to variables in lower case (and camel case, and other case)
if Config.debug:
  ...

# You can access to variables as key of dict object
if Config["debug"]:
  ...

Configure custom variables

# You can add variables to the object

# as dict
MagicConfig({
    "Number": 456,
    "Boolean": True
})

# as named arguments
MagicConfig(
    Number=456,
    Boolean=True
)

Prepared autogenerators for DB URIs

For example in .env file you can write only this data:

MONGO_HOST="127.0.0.1"
MONGO_USER="user"
MONGO_PWD="*****"
MONGO_DB="test"
MONGO_PORT=27017

and in code you can call the MONGO_URL

from magic_config import Config

Config.MONGO_URL

# output:
# mongodb://user:passwd@127.0.0.1:27017/test?authSource=admin&tls=false

Set type casting for environment variables

For example if you create in source root magic.config file and write:

DEBUG="bool"
DEBUG_STEP="bool"
DEBUG_USER_ID="int"

then you run

DEBUG=1 DEBUG_STEP=1 DEBUG_USER_ID=1 DEBUG_USER_ID=10011888 python server.py
Config.DEBUG # True
Config.DEBUG_STEP # True
Config.DEBUG_USER_ID # int(10011888)

Custom file path

Instead default .env file in source root you can configure path to .env file:

# for example

env_file = os.path.realpath(os.path.dirname(os.path.realpath(__file__)) + "/../my.env")
MagicConfig(env_file=env_file)

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

magic-config-0.1.13.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

magic_config-0.1.13-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file magic-config-0.1.13.tar.gz.

File metadata

  • Download URL: magic-config-0.1.13.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for magic-config-0.1.13.tar.gz
Algorithm Hash digest
SHA256 769ca72e04042a1c8cd83415984c12ff9dce67f43864e44d798771e44f604975
MD5 87356c7512650f2ea676522c1f3209e1
BLAKE2b-256 5a98d40f77687853b4e9e5df73e414ecbf42605de3674e906c738b434fb56952

See more details on using hashes here.

File details

Details for the file magic_config-0.1.13-py3-none-any.whl.

File metadata

File hashes

Hashes for magic_config-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 bf40a44d462227d1ce732a9a58a34fb64dfa6465ad016bcc35ab559d97cb0ad2
MD5 2e8e0c25c3d0ef379c1fa62f98c466ba
BLAKE2b-256 a7e7c4a79da007d9f6e307f6784721ef0bd1e8cc424a27642823a963a1094161

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