Skip to main content

Declare and load configuration from environment variables

Project description

Declare and load configuration from environment variables.

Install

pip install config

Examples

Declare and load scalar values

from config import Config, parse_int, parse_float, parse_str, parse_bool

cfg = Config()

# declare variables with the appropriate parser
cfg.declare('my_int_variable', parse_int())
cfg.declare('my_float_variable', parse_float())
cfg.declare('my_str_variable', parse_str())
cfg.declare('my_bool_variable', parse_bool())

# load the values

# will load the value of MY_INT_VARIABLE as an int
int_result = cfg.get('my_int_variable')
# will load the value of MY_FLOAT_VARIABLE as a float
float_result  = cfg.get('my_float_variable')
# will load the value of MY_STR_VARIABLE as a str
str_result = cfg.get('my_str_variable')

Declare and load list values

from config import Config, parse_int_list

cfg = Config()

# declare variables with the appropriate parser
cfg.declare('my_int_list_variable', parse_int_list())

# load the values

# will load the value of MY_INT_LIST_VARIABLE as a list of ints.
# By default it assumes the elements to be comma separated
int_list_result = cfg.get('my_int_list_variable')

Declare and load nested values

from config import Config, parse_str

cfg = Config()
cfg.declare(
    'dict',
    {
       'value1': parse_str(),
       'dict2': {
           'value2': parse_str(),
       },
    },
)

# this will load values from two environment variables DICT_VALUE_1 and DICT_DICT2_VALUE2 and return them in the
# same structure as declared above
dict_result = cfg.get('dict')

Add validation

from config import Config, parse_str, parse_str_list
from validators import email

# config expects validators to raise an Error on failure.
# Since the validators package returns Failures instead of raising, we create a small adapter.
def email_validator(value):
    result = email(value)
    if isinstance(result, ValidationFailure):
        raise ValueError('"{}" is not a valid email address'.format(value))

cfg = Config()

cfg.declare('valid_email', parse_str(validator=email_validator))
# this also works with lists. The validator function is applied to each value separately
cfg.declare('valid_list_of_emails, parse_str_list(validator=email_validator))

valid_email = cfg.get('valid_email')
valid_list_of_emails = cfg.get('valid_list_of_emails')

Reloading configuration at runtime

from config import Config, parse_str, reload

cfg = Config()
cfg.declare('some_value', parse_str())
value = cfg.get('some_value')

# Values are actually loaded during declare().
# Changes to the environment at runtime are not picked up automatically.
# Relaoding has to be triggered explicitly.

cfg.reload()

new_value = cfg.get('some_value')

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

env_config-1.4.0.tar.gz (8.5 kB view details)

Uploaded Source

File details

Details for the file env_config-1.4.0.tar.gz.

File metadata

  • Download URL: env_config-1.4.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for env_config-1.4.0.tar.gz
Algorithm Hash digest
SHA256 019cc39a207d4a7dad9f8ae306449f7ff5866845508a9caefa328639e8375556
MD5 8295e8161731e93c9632ed2e4cf2847b
BLAKE2b-256 0ff65a838150c3935c2e7f7a957565ea47ad871a0b3fa0ac7347bd3d51a33c02

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