Welcome to @environment’s documentation!¶
@environment¶
Decorator for convenient loading of environment variables. @environment allows you to declare dependencies on environment variables so that it’s clear what needs to be set. Also, any error handling is moved away making the code clearer.
- Free software: MIT license
- Documentation: https://atenvironment.readthedocs.io.
Getting started¶
Install @environment from pip:
pip install atenvironment
Using @environment is as simple as:
from atenvironment import environment
@environment('API_KEY', 'TOKEN')
def check(a, b, c, key, token):
# API_KEY is in key
# TOKEN is in token
Then call the function as:
check(a, b, c)
Environment variables are checked and provided to the function as trailing parameters in order of declaration. In case the token is not in environment an atenvironment.EnvironMiss
exception is raised.
You can also provide your own error handling function. In addition, some environment variables can be loaded directly into object variable in case instance property is to be initialized.
See the docummentation for more details.
[ ~ Dependencies scanned by PyUp.io ~ ]
Installation¶
Stable release¶
To install @environment, run this command in your terminal:
$ pip install atenvironment
This is the preferred method to install @environment, as it will always install the most recent stable release.
If you don’t have pip installed, this Python installation guide can guide you through the process.
From sources¶
The sources for @environment can be downloaded from the Github repo.
You can either clone the public repository:
$ git clone git://github.com/eghuro/atenvironment
Or download the tarball:
$ curl -OL https://github.com/eghuro/atenvironment/tarball/master
Once you have a copy of the source, you can install it with:
$ python setup.py install
atenvironment¶
atenvironment package¶
Module contents¶
Top-level package for @environment.
-
exception
atenvironment.
EnvironMiss
[source]¶ Bases:
KeyError
Error indicating key is not present in environment.
-
exception
atenvironment.
UnknownKeyword
[source]¶ Bases:
BaseException
Exception indicating unknown keyword was provided to @environment decorator in **kwargs.
-
atenvironment.
environment
(*value, **kwargs)[source]¶ @environment decorator.
- Arguments:
value – one or more environment tokens requested
onerror – optional function to be called if any of the environment tokens in value is not present in environment. Such function must take one parameter what is a string value of a missing environment token. If onerror is not set, error is logged and EnvironMiss exception is raised
in_self – optional list of variable names in case instance property is to be initialized. If present, must be of same length as value. None in this list means particular element will be passed into the function and not into any instance property.
default – optional list of default values in case environment token in value is not present in environment. If present, must be of same length as value. onerror will not be called in such case.
The decorator checks for presence of environment tokens and if successful reads their values to the function parameters of the decorated function after any called parameters provided.
Eg. if calling function(a, b, c) that is decorated with @environment(‘X’) the function must be defined as def function(a, b, c, x) and X from the environment is read as last parameter.
When combining decorators or using multiple environment tokens in one @environment(‘X’, ‘Y’, ‘Z’) the values are loaded from the left to the right, from the top to the bottom.
If a function parameter for @environment is missing, when such function is called a TypeError is raised by the interpreter.
Contributing¶
Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.
You can contribute in many ways:
Types of Contributions¶
Report Bugs¶
Report bugs at https://github.com/eghuro/atenvironment/issues.
If you are reporting a bug, please include:
- Your operating system name and version.
- Any details about your local setup that might be helpful in troubleshooting.
- Detailed steps to reproduce the bug.
Fix Bugs¶
Look through the GitHub issues for bugs. Anything tagged with “bug” and “help wanted” is open to whoever wants to implement it.
Implement Features¶
Look through the GitHub issues for features. Anything tagged with “enhancement” and “help wanted” is open to whoever wants to implement it.
Write Documentation¶
@environment could always use more documentation, whether as part of the official @environment docs, in docstrings, or even on the web in blog posts, articles, and such.
Submit Feedback¶
The best way to send feedback is to file an issue at https://github.com/eghuro/atenvironment/issues.
If you are proposing a feature:
- Explain in detail how it would work.
- Keep the scope as narrow as possible, to make it easier to implement.
- Remember that this is a volunteer-driven project, and that contributions are welcome :)
Get Started!¶
Ready to contribute? Here’s how to set up atenvironment for local development.
Fork the atenvironment repo on GitHub.
Clone your fork locally:
$ git clone git@github.com:your_name_here/atenvironment.git
Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:
$ mkvirtualenv atenvironment $ cd atenvironment/ $ python setup.py develop
Create a branch for local development:
$ git checkout -b name-of-your-bugfix-or-feature
Now you can make your changes locally.
When you’re done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:
$ flake8 atenvironment tests $ python setup.py test or py.test $ tox
To get flake8 and tox, just pip install them into your virtualenv.
Commit your changes and push your branch to GitHub:
$ git add . $ git commit -m "Your detailed description of your changes." $ git push origin name-of-your-bugfix-or-feature
Submit a pull request through the GitHub website.
Pull Request Guidelines¶
Before you submit a pull request, check that it meets these guidelines:
- The pull request should include tests.
- If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst.
- The pull request should work for Python 3.5 and 3.6, and for PyPy. Check https://travis-ci.org/eghuro/atenvironment/pull_requests and make sure that the tests pass for all supported Python versions.
Deploying¶
A reminder for the maintainers on how to deploy. Make sure all your changes are committed (including an entry in HISTORY.rst). Then run:
$ bumpversion patch # possible: major / minor / patch
$ git push
$ git push --tags
Travis will then deploy to PyPI if tests pass.
Credits¶
Development Lead¶
- Alexandr Mansurov <alex@eghuro.cz>
Contributors¶
None yet. Why not be the first?
History¶
0.2.1 (2019-03-31)¶
- Updated dependencies and supported Python versions.
0.2.0 (2018-06-24)¶
- on error: optional function to be called if any of the environment tokens in value is not present in environment
- in self: optional variable name in case instance property is to be initialized
- package slightly restructured
0.1.0 (2018-06-12)¶
- First release on PyPI.