biodome
Project description
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!
How it works
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file biodome-2017.6.5-py2.py3-none-any.whl
.
File metadata
- Download URL: biodome-2017.6.5-py2.py3-none-any.whl
- Upload date:
- Size: 15.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcb6522807c042e7f8faf266db75c67ece6586e8959cd3cc274ea2750d03672c |
|
MD5 | 1a7cf8a724a883aa26951744c7e8d305 |
|
BLAKE2b-256 | aee5c1bd9ea62076271c1ab60c0b4b5723ef2bd62339a26514cf997aadbe5896 |