Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

A more enjoyable environment variable getter and setter - for Python.

Project description

envjoy Build Status PyPI version

A more enjoyable environment variable getter and setter - for Python.

Introduction

Environment variable getting and setting has a bit of an itch it. This is a not-so-whiny library that makes it more easy to get and set environment variables, and optionally smarter data type interpretation.

Install

Install using pip:

$ pip install envjoy

Use

Very basic example:

from __future__ import print_function # Optional: Python 2 support for `env.print`

from envjoy import env

# non-casted access - never throws annoying errors

env.FOO
env.FOO = 1

del env.FOO

env.FOO = 1

# casted access - never throws annoying errors

del env['FOO']

print('---')

print(env['FOO']) # => None

env['FOO']= 1 # set value without complaints (casted to string)

print(env['FOO']) # => "1"
print(env['FOO']) # => 1

print('---')

env['FOO'] = None

print(env['FOO']) # => ''
print(env['FOO', bool]) # => False
print(env['FOO', int]) # => 0
print(env['FOO', float]) # => 0.0
print(env['FOO', str]) # => ''
print(env['FOO', tuple]) # => ()
print(env['FOO', list]) # => []
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = True

print(env['FOO']) # => 'True'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => 1
print(env['FOO', float]) # => 1.0
print(env['FOO', str]) # => 'true'
print(env['FOO', tuple]) # => (True)
print(env['FOO', list]) # => [True]
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = 'true' # => 'true'

print(env['FOO', bool]) # => True
print(env['FOO', int]) # => 1
print(env['FOO', float]) # => 1.0
print(env['FOO', str]) # => 'true'
print(env['FOO', tuple]) # => (True)
print(env['FOO', list]) # => [True]
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = 0

print(env['FOO']) # => '0'
print(env['FOO', bool]) # => False
print(env['FOO', int]) # => 0
print(env['FOO', float]) # => 0.0
print(env['FOO', str]) # => '0'
print(env['FOO', tuple]) # => (0)
print(env['FOO', list]) # => [0]
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = '0'

print(env['FOO']) # => '0'
print(env['FOO', bool]) # => False
print(env['FOO', int]) # => 0
print(env['FOO', float]) # => 0.0
print(env['FOO', str]) # => '0'
print(env['FOO', tuple]) # => (0)
print(env['FOO', list]) # => [0]
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = 1

print(env['FOO']) # => '1'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => 1
print(env['FOO', float]) # => 1.0
print(env['FOO', str]) # => '1'
print(env['FOO', tuple]) # => (1)
print(env['FOO', list]) # => [1]
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = '1'

print(env['FOO']) # => '1'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => 1
print(env['FOO', float]) # => 1.0
print(env['FOO', str]) # => '1'
print(env['FOO', tuple]) # => (1)
print(env['FOO', list]) # => [1]
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = -1

print(env['FOO']) # => '-1'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => -1
print(env['FOO', float]) # => -1.0
print(env['FOO', str]) # => '-1'
print(env['FOO', tuple]) # => (-1)
print(env['FOO', list]) # => [1]
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = '-1'

print(env['FOO']) # => '-1'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => -1
print(env['FOO', float]) # => -1.0
print(env['FOO', str]) # => '-1'
print(env['FOO', tuple]) # => (-1)
print(env['FOO', list]) # => [1]
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = 12.34

print(env['FOO']) # => '12.34'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => 12
print(env['FOO', float]) # => 12.34
print(env['FOO', str]) # => '12.34'
print(env['FOO', tuple]) # => (12.34)
print(env['FOO', list]) # => [12.34]
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = '12.34'

print(env['FOO']) # => '12.34'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => 12
print(env['FOO', float]) # => 12.34
print(env['FOO', str]) # => '12.34'
print(env['FOO', tuple]) # => (12.34)
print(env['FOO', list]) # => [12.34]
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = -12.34

print(env['FOO']) # => '-12.34'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => -12
print(env['FOO', float]) # => -12.34
print(env['FOO', str]) # => '-12.34'
print(env['FOO', tuple]) # => (-12.34)
print(env['FOO', list]) # => [-12.34]
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = '-12.34'

print(env['FOO']) # => '-12.34'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => -12
print(env['FOO', float]) # => -12.34
print(env['FOO', str]) # => '-12.34'
print(env['FOO', tuple]) # => (-12.34)
print(env['FOO', list]) # => [-12.34]
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = 'foo bar baz 1 2 3'

print(env['FOO']) # => 'foo bar baz 1 2 3'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => 123
print(env['FOO', float]) # => 123.0
print(env['FOO', str]) # => 'foo bar baz 1 2 3'
print(env['FOO', tuple]) # => ('foo bar baz 1 2 3')
print(env['FOO', list]) # => ['foo bar baz 1 2 3']
print(env['FOO', dict]) # => {}

print('---')

env['FOO'] = 'foo,bar,baz,1,2,3'

print(env['FOO']) # => 'foo,bar,baz,1,2,3'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => 123
print(env['FOO', float]) # => 123.0
print(env['FOO', str]) # => 'foo,bar,baz,1,2,3'
print(env['FOO', tuple]) # => ('foo', 'bar', 'baz')
print(env['FOO', list]) # => ['foo', 'bar', 'baz']
print(env['FOO', dict]) # => {0: 'foo', 1: 'bar', 2: 'baz'}

print('---')

env['FOO'] = ('foo', 'bar', 'baz', 1, 2, 3)

print(env['FOO']) # => '(foo,bar,baz,1,2,3)'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => 123
print(env['FOO', float]) # => 123.0
print(env['FOO', str]) # => '(foo,bar,baz,1,2,3)'
print(env['FOO', tuple]) # => ('foo', 'bar', 'baz')
print(env['FOO', list]) # => ['foo', 'bar', 'baz', 1, 2, 3]
print(env['FOO', dict]) # => {} # TODO:  {0: 'foo', 1: 'bar', 2: 'baz', 3: 1, 4: 2, 5: 3}

print('---')

env['FOO'] = ['foo', 'bar', 'baz', 1, 2, 3]

print(env['FOO']) # => '[foo,bar,baz,1,2,3]'
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => 123
print(env['FOO', float]) # => 123.0
print(env['FOO', str]) # => '[foo, bar, baz, 1, 2, 3]'
print(env['FOO', tuple]) # => ('foo', 'bar', 'baz', 1, 2, 3)
print(env['FOO', list]) # => ['foo', 'bar', 'baz', 1, 2, 3]
print(env['FOO', dict]) # => {} # TODO:  {0: 'foo', 1: 'bar', 2: 'baz', 3: 1, 4: 2, 5: 3}

print('---')

env['FOO'] = {'foo': 1, 'bar': 2, 'baz': 3}

print(env['FOO']) # => '{foo:1,bar:2,baz:3}' # REVIEW: handle nested json
print(env['FOO', bool]) # => True
print(env['FOO', int]) # => 123
print(env['FOO', float]) # => 123.0
print(env['FOO', str]) # => '{foo: 1, bar: 2, baz: 3}'
print(env['FOO', tuple]) # => ({0: 'foo', 1: 'bar', 2: 'baz', 3: 1, 4: 2, 5: 3})
print(env['FOO', list]) # => [{0: 'foo', 1: 'bar', 2: 'baz', 3: 1, 4: 2, 5: 3}]
print(env['FOO', dict]) # => {'foo': 1, 'bar': 2, 'baz': 3}

# etc.

print('---')

env.inspect()

print('---')

env.print()

print('---')

Test

Clone down source code:

$ make install

Run colorful tests, with only native environment (dependency sandboxing up to you):

$ make test

Run less colorful tests, with multi-environment (using tox):

$ make test-tox

About

This project was mainly initiated - in lack of solid existing alternatives - to be used at our work at Markable.ai to have common code conventions between various programming environments where Python (research, CV, AI) is heavily used.

License

Released under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for envjoy, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size envjoy-0.1.0-py3-none-any.whl (18.2 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size envjoy-0.1.0.tar.gz (18.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page