Skip to main content

Utility functions to make it easier to work with os.environ

Project description

https://travis-ci.org/yunojuno/python-env-utils.svg?branch=master https://badge.fury.io/py/env_utils.svg https://codecov.io/gh/yunojuno/python-env-utils/branch/master/graph/badge.svg

env utils

This library extends the standard library’s getenv function, allowing you to coerce the return value into a type.

And that’s it.

It’s been released as a library because every project we have includes the same requirements - read in environment variables, coerce them into the correct type.

The problem is that environment variables are always stored as strings, but Python will evaluate any string (even “”) as True if cast to a boolean. This is almost never the desired behaviour. If you set an environment variable to “”, “0” or “False”, you want it to be False.

>>> os.environ['foo'] = "0"
>>> val = os.getenv('foo')
>>> val
"0"
>>> bool(val)
True

env_utils.get_env will coerce the value into the type you require. The package contains basic helper functions that coerce booleans, integers, decimals, floats, dates, lists and dictionaries.

# FOO=0
>>> env_utils.get_env('FOO')
"0"
>>> env_utils.get_bool('FOO')
False
>>> env_utils.get_int('FOO')
0

# FOO=foo,bar
>>> env_utils.get_list('FOO', separator=',')
['foo', 'bar']

# FOO='{"foo": true}'
>>> env_utils.get_dict('FOO')
{'foo': True}

# FOO=2016-11-23
>>> env_utils.get_date('FOO')
datetime.date(2016, 11, 23)

You can supply any function you like to coerce the value, e.g.:

>>> import os
>>> os.getenv('FOO_NAME')
'bob'
>>> class Foo(object):
...     def __init__(self, name):
...         self.name = name
>>> coerce = lambda x: Foo(x)
>>> import env_utils
>>> foo = env_utils.get_env('FOO_NAME', coerce=coerce)
>>> foo.name
>>> 'bob'

Installation

The library is available at pypi as ‘env_utils’, and can installed using pip:

$ pip install env_utils

Tests

The tests can be run using tox:

$ tox

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_utils-1.2.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distributions

env_utils-1.2.0.macosx-10.12-intel.tar.gz (4.7 kB view details)

Uploaded Source

env_utils-1.2.0-py2-none-any.whl (5.4 kB view details)

Uploaded Python 2

File details

Details for the file env_utils-1.2.0.tar.gz.

File metadata

  • Download URL: env_utils-1.2.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for env_utils-1.2.0.tar.gz
Algorithm Hash digest
SHA256 151295120d45f7c16b4a9c4a7fa4937d9956dfb8829223d62c6d1a908b637a14
MD5 178d44f97a157df081fc6f65b26e2779
BLAKE2b-256 7b2ace19d7bdad70c42a0573fb11566eb166c3f00b52bb034bcaa043885b3c70

See more details on using hashes here.

File details

Details for the file env_utils-1.2.0.macosx-10.12-intel.tar.gz.

File metadata

File hashes

Hashes for env_utils-1.2.0.macosx-10.12-intel.tar.gz
Algorithm Hash digest
SHA256 7bed35a634a204abf47b5802f95064a7a6453157439d14c81491f68a7e3d6af8
MD5 9753fc29053482733ed5e2eaab283346
BLAKE2b-256 4e99fde9629b249b1b1a5805fa49afa1c33629515b493bc90d6fd0c9f19a9c79

See more details on using hashes here.

File details

Details for the file env_utils-1.2.0-py2-none-any.whl.

File metadata

File hashes

Hashes for env_utils-1.2.0-py2-none-any.whl
Algorithm Hash digest
SHA256 e551159721bcadc6a74ed763ef3e1afbc4482c48ddfae49177d2a29b2cd6c021
MD5 7f7fb9bc9d8b634a304ad5bcad70ed84
BLAKE2b-256 93b41b662c43f273f134057e17e3c8270db905cddbf3f6da51d813bd4ece89c7

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