Skip to main content

Environment to python dictionary parser util

Project description

PyPI version Downloads Downloads Downloads

Environment to python dictionary parser util

pip install env2dict

About

This small package provides an ability of easy setting/overriding Python variables from environment. It is expected that u will use it to override your configuration data without changing configuration files itself, what is especially useful for containers-oriented applications.

Syntax

To use it, u need to define environment variables matches the pattern: PrefixBodyOperationSuffix where:

  • Prefix is any word to determine target variables; for instance, DD prefix means to use only variables starts with DD; can be empty, what means to select all available variables (not recommended)
  • Body is the name of the target configuration parameter
  • OperationSuffix is the one of next available suffixes (by default, but u can change your defaults):
    • _NUMBER to convert variable value to integer (environment variables are always strings)
    • _FLOAT to convert variable value to float
    • _FLAG to convert variable value to optional[boolean] at that values:
      • 1/yes/Yes/True/true equal true
      • 0/no/No/False/false equal false
      • None/null/NULL equal null
    • _LIST means to parse variable value to string list
    • _LIST_APPEND means to parse variable value to string list and append to existing list instead of override
    • _JSON means to parse variable value as json string
    • no suffix means that no conversion will be performed, so variable value will stay a string

Moreover, u can put nested dicts values using __ separator (or other on your choice) in the environment variable name.

Note also that u can combine these suffixes to perform more complicated transformations.

Examples

  • env variable DD_S_COUNT_NUMBER=10 (with prefix DD_ conversation) will be converted to S_COUNT=10 Python object
  • DD_S_COUNT=10 🠚 S_COUNT="10"
  • DD_USE_THIS_FLAG=yes 🠚 USE_THIS=True
  • DD_USE_THIS_FLAG=true 🠚 USE_THIS=True
  • DD_USE_THIS_FLAG=no 🠚 USE_THIS=False
  • DD_ALLOWED_HOSTS_LIST_APPEND=127.0.0.1;dev.ocr.com;dev.web.com will append a list ['127.0.0.1', 'dev.ocr.com', 'dev.web.com'] to ALLOWED_HOSTS variable
  • DD_READ_ME_JSON={\"a\": 1, \"b\": [1, 2]} will be translated to READ_ME={'a': 1, 'b': [1, 2]}
  • DD_SOME_DICT__KEY1__KEY2=postgres will create a dictionary SOME_DICT={'KEY1': {'KEY2': 'postgres'}} if it doesn't exist and will add a field in existing dictionary by aforementioned route
  • DD_A__B_LIST_APPEND_JSON=[1, 2, 3, [1, 2, \"3\"]] will append [1, 2, 3, [1, 2, "3"]] to the end of value V from A={"B": V}

How to use

from env2dict import parse_vars

new_vars = parse_vars(
    prefix='DD_',
    initial_vars=None,
    source=None
)

More info

Please take a look at:

  • parse_vars function docstring
  • tests directory of this package repo.

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

env2dict-0.0.4.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

env2dict-0.0.4-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file env2dict-0.0.4.tar.gz.

File metadata

  • Download URL: env2dict-0.0.4.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.19

File hashes

Hashes for env2dict-0.0.4.tar.gz
Algorithm Hash digest
SHA256 941e98f9614d3221f052b423d1021f2d863c44daf362e7b35964059df16b78bb
MD5 e6056bf37bc2e367b0861832c4ee7935
BLAKE2b-256 2e237eb37dacb7ba8ea02b84f9cfc0c11a037708e349f3235cb2f67f01aec83f

See more details on using hashes here.

File details

Details for the file env2dict-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: env2dict-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.19

File hashes

Hashes for env2dict-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a317ff196c52706a548cac7ad73afdb5390529b10623a3e3621503f4f9f2687d
MD5 11f8e46947c417c2d9a91413ff9db96f
BLAKE2b-256 4732cc0a2398f07b49fca8efeb1f32131b0ba35d6085c97e26b8e14eaed2fd87

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