Skip to main content

Environment variable parser library for python

Project description

envio

Environment variable parser library for python.

Mostly created as a reference for how basic package creation works. Many similar projects exist; the goal of this one to make the most basic implementation possible and also establish the minimum amount of boilerplate to create a package for submission.

Installation

run pip install envio

Usage

Basic usage

import envio

# Fetch the envirnment variable MYARG and use value 'yay' if not found
MYARG = envio.get_var('MYARG', default='yay')

# If a default is not specified, then an exception will be raised if the
# environment variable does not exist. This is how we can specify required
# variables.
MYARG = envio.get_var('MYARG') # MYARG is required

Environment variables can also be coerced into multiple different types

# coerce required variable MYINT into an integer
MYINT = envio.get_var('MYINT', var_type=int)

MYFLOAT = envio.get_var('MYFLOAT', var_type=float)

# Acceptable inputs for boolean values include:
# case insensitive versions of 'true' and 'false'
# case insensitive words 'on' and 'off'
# '1' and '0'
# case insensitive letters 't' and 'f'
MYBOOL = envio.get_var('MYBOOL', var_type=bool)

# Dictionaries are also supported but require the environment variable to be
# a valid json string. Below is an example of how to specify a simple dict
# through a json string.
MYDICT = envio.get_var('MYDICT', var_type='json', default='{"hello": "world"}')

Variables can also be coerced into lists by using the many=True argument.

# e.g MYLIST <-- '1,hello, 7, 12.0'
# the following will parse as
# MYLIST = ['1', 'hello', '7', '12.0']
MYLIST = envio.get_var('MYLIST', many=True)

# list parsing can be combined with type coercion. This will enforce that
# every member of the list be parsed as 'var_type'.
MYINTS = envio.get_var('MYINTS', var_type=int, many=True)

# The delimmiter used for parsing lists defaults to a comma, but it can be
# changed with an optional 'delimmiter' param.
MYINTS = envio.get_var('MYINTS', var_type=int, many=True, delimmiter='||', default='1||2||3')

# using 'var_type'=json also allows the parsing of integers, lists of integers
# and the like. It allows mixed types in lists and should be used carefully.
# the example below will produce [1, 2, '3']
MYLIST = envio.get_var('MYLIST', var_type='json', default='[1, 2, "3"]')

Default values can only be STRING values. This is done so that even the default value we choose has to go through the same parser as if we had gotten it as an environment variable. This avoids a class of errors where an incompatible default can be chosen. e.g. expecting a integer but defaulting to a boolean.

# this is the correct way to specify a default for a list of integers
MYINTS = envio.get_var('MYINTS', var_type=int, many=True, default='1,2,3')

Development

You'll want to clone the package somewhere and have either pipenv or virtualenv available on your environment.

A Pipfile is included in this project. It comes mostly with packages used to run twine - the utility used to submit packages to pypi. It can also be used to install this project's distributions locally.

All commands assume the working directory is the repo root.

# install a virtual env and the pipfile requirements.
pipenv install
# activate the virtualenv in the shell. All commands should be run after this
# activation
pipenv shell

# builds can be generated like so:
python setup.py sdist bdist_wheel

# builds can then be submitted to pypi using twine like so:

# upload to test pypi
twine upload --repository-url  https://test.pypi.org/legacy/ dist/*

# upload to the actual pypi servers.
twine upload --repository-url  https://upload.pypi.org/legacy/ dist/*

# tests can be run using setup.py as well
python setup.py test

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

envio-1.0.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

envio-1.0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file envio-1.0.1.tar.gz.

File metadata

  • Download URL: envio-1.0.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.4

File hashes

Hashes for envio-1.0.1.tar.gz
Algorithm Hash digest
SHA256 51f2ebfdb3d8af69af0fc89d83d936caf4152c5b1773aec6c9620e810cd5c807
MD5 e1678ed35796ba7a628ce3215f903ef7
BLAKE2b-256 65f4631c3555a77155c73ccc9d6646abcd944119b358f60ac56692a0c8691a7d

See more details on using hashes here.

File details

Details for the file envio-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: envio-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.4

File hashes

Hashes for envio-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3310517553f4d208cb17bdb9b0435e964c603dc0351e77ff35ee165da0a8dfef
MD5 9d5e6afb41d9c197b2cea354ed0fa3ab
BLAKE2b-256 3c9f5105b1565c4bf6d63d8b6b555cffcec2ab1e957463e8aefe91c6c02e28ee

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