https://github.com/va1/smart-getenv
Wrapper for os.getenv that achieves return values of a specified type
https://github.com/va1/smart-getenv
env environment-variables python python3
Last synced: 4 months ago
JSON representation
Wrapper for os.getenv that achieves return values of a specified type
- Host: GitHub
- URL: https://github.com/va1/smart-getenv
- Owner: Va1
- License: apache-2.0
- Created: 2015-06-21T19:35:03.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2019-03-03T22:32:16.000Z (over 7 years ago)
- Last Synced: 2024-04-25T14:43:54.840Z (about 2 years ago)
- Topics: env, environment-variables, python, python3
- Language: Python
- Size: 10.7 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
Python smart getenv
===================
Since environment variables in os.environ are strings, it often appears inconvenient to store and retrieve other
data types such as bool or list. The package provides a single function that wraps os.getenv and allows
you to specify the desired variable type.
Tested and supported types: str, int, float, list. tuple, dict.
`PyPi page`_
Usage
-----
Install:
.. code:: bash
$ pip install smart-getenv
Prepare the variables:
.. code:: bash
$ export BOOLEAN=true
$ export LIST=a,b,c
$ export TRICKY_LIST=d:e:f
$ export DICT="{'foo':'bar'}"
Get them:
.. code:: python
>>> from smart_getenv import getenv
>>>
>>> getenv('BOOLEAN', type=str)
'true'
>>> getenv('BOOLEAN', type=bool)
True
>>> getenv('LIST', type=list)
['a', 'b', 'c']
>>> getenv('LIST', type=tuple)
('a', 'b', 'c')
>>> getenv('TRICKY_LIST', type=list, separator=':')
['d', 'e', 'f']
>>> getenv('DICT', type=dict)
{'foo': 'bar'}
>>> getenv('LOST', default='default value anyone?')
'default value anyone?'
Run tests:
.. code:: bash
$ python tests.py
.. _PyPi page: https://pypi.python.org/pypi/smart-getenv