Skip to main content

biodome

Project description

https://img.shields.io/badge/stdlib--only-yes-green.svg https://travis-ci.org/cjrh/biodome.svg?branch=master https://coveralls.io/repos/github/cjrh/biodome/badge.svg?branch=master https://img.shields.io/pypi/pyversions/biodome.svg https://img.shields.io/github/tag/cjrh/biodome.svg https://img.shields.io/badge/install-pip%20install%20biodome-ff69b4.svg https://img.shields.io/pypi/v/biodome.svg https://img.shields.io/badge/calver-YYYY.MM.MINOR-22bfda.svg

biodome

Controlled environments

Reading environment variables with os.environ is pretty easy, but after a while one gets pretty tired of having to cast variables to the right types and handling fallback to defaults.

This library provides a clean way read environment variables and fall back to defaults in a sane way.

How you were doing it:

import os

try:
    TIMEOUT = int(os.environ.get('TIMEOUT', 10))
except ValueError:
    TIMEOUT = 10

Wordy, boilerplate, DRY violation, etc.

How you will be doing it:

import biodome

TIMEOUT = biodome.environ.get('TIMEOUT', 10)

That’s right, it becomes a single line. But there’s a magic trick here: how does biodome know that TIMEOUT should be set to an int? It knows because it looks at the type of the default arguments. This works for a bunch of different things:

# Lists
os.environ['IGNORE_KEYS'] = '[1, 2, 3]'
biodome.environ.get('TIMEOUT', []) == [1, 2, 3]

# Dicts
os.environ['SETTINGS'] = '{"a": 1, "b": 2}'
biodome.environ.get('SETTINGS', {}) == dict(a=1, b=2)

If you look carefully at the above, you can see that we set the data via the stdlib os.environ dictionary; that’s right, biodome.environ is a drop-in replacement for os.environ. You don’t even have to switch out your entire codebase, you can do it piece by piece.

And while we’re on the subject of setting env vars, with biodome you don’t have to cast them first, it does string casting internally automatically, unlike os.environ:

# Dicts
biodome.environ['SETTINGS'] = dict(b=2, a=1)  # No cast required
biodome.environ.get('SETTINGS', {}) == dict(a=1, b=2)

True and False

I don’t know about you, but I use bool settings a LOT in environment variables, so handling this properly is really important to me. When calling biodome.environ.get('SETTING', default=<value>), the default value can also be a bool, i.e., True or False. In this case, any of the following values, and their upper- or mixed-case equivalents will be recognized as True:

['1', 'y', 'yes', 'on', 'active', 'activated', 'enabled', 'true',
't', 'ok', 'yeah']

Anything not in this list will be considered as False. Do you have ideas for more things that should be considered as True? I take PRs!

Callables

For explictness it is often convenient to declare and load environment variables at the top of the module in which they’re used:

""" My new module """
import biodome

ENABLE_SETTING_XYZ = biodome.environ.get('ENABLE_SETTING_XYZ', True)

def blah():
    print(ENABLE_SETTING_XYZ)

You could call environ.get() inside the functions and methods where it is used, but then you would lose the convenience of documenting all the available environment variables at the top of the module. As a solution to this problem, biodome provides a way to produce a callable for a particular setting. An extra advantage of doing this is that it becomes quite easy to make use of changes in environment variables on the fly. Here’s the modified example:

""" My new module """
import biodome

ENABLE_SETTING_XYZ = biodome.environ.get_callable(
    # Same as before
    'ENABLE_SETTING_XYZ', True
    )

def blah():
    print(ENABLE_SETTING_XYZ())  # Now a callable!

How it works internally

The key theme here is that the type of the default value is used to determine how to cast the input value. This works for the following types:

  • int

  • float

  • str

  • list

  • dict

  • set (NOTE: only supported in Python 3+ due to ast.literal_eval())

  • tuple

For the containers, we use ast.literal_eval() which is much safer than using eval() because code is not evaluated. Safety first! (thanks to @nickdirienzo for the tip)

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

biodome-2018.8.3.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

biodome-2018.8.3-py2.py3-none-any.whl (17.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file biodome-2018.8.3.tar.gz.

File metadata

  • Download URL: biodome-2018.8.3.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.18.4

File hashes

Hashes for biodome-2018.8.3.tar.gz
Algorithm Hash digest
SHA256 b33523c48ab99ee4e7d46ac1baa659cd92416161bd5781aa41b4cad80e09f58e
MD5 1ad7f75f6aa6df351397682300678cd3
BLAKE2b-256 e928b0594f0524626dc8a05bdd416cde2fa2a5141b92c9799f4ad841d233d278

See more details on using hashes here.

File details

Details for the file biodome-2018.8.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for biodome-2018.8.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f65f32a088f74df32d43f8f2e8c5eadaa806c1a439408de89f8bbc7ca72cbc02
MD5 d12c0e36d8841aa1d443fc6dd93b9698
BLAKE2b-256 2f2e61a08dbd22d4230a407cf44f7a407ed5e932057d297d2e923a1e6aa7b7d2

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