Utilities to handle OS environment variables
Project description
os-env-injection
Utilities to handle OS environment variables
Why this library?
You have a function which requires several arguments that typically depend on the system on when the function is running
def f(url: str, user: str, password: str, subdomain: str):
...
You would like to have the possibility to have its values to be read from the OS env. If you simply do
import os
def f(
url: str = os.environ["URL"],
user: str = os.environ["USER"],
password: str = os.environ["PASSWORD"],
subdomain: str = os.environ["SUBDOMAIN"],
):
...
then you will encounter a problem whenever the OS env variables are not set, because the defaults are evaluated at import time. This means in practice, that you will be forced to use them, instead of passing the values directly.
A workaround is to write
import os
def f(
url: str = os.environ.get("URL", None),
user: str = os.environ.get("USER", None),
password: str = os.environ.get("PASSWORD", None),
subdomain: str = os.environ.get("SUBDOMAIN", None),
):
...
In this way, there are still a couple of drawbacks:
- it is necessary to write code to check the values of each variable to raise an exception if values are missing (
None
) - the default values will be evaluated when the function is first imported.
In case you are setting the OS env dynamically (e.g. by executing a shell script withexport
s) you could end up in troubles.
The nice solution - Quickstart
First, install the library
pip install os-env-injection
From the previous example, say that all variables are required except for subdomain
which can stay None
.
One can use this library to write:
from os_env_injection import inject_os_env, Injection
from typing import Optional
@inject_os_env(
injections=[
Injection(var_name="url", os_env_key="OS_ENV_URL"),
Injection(var_name="user", os_env_key="OS_ENV_USER"),
Injection(var_name="password", os_env_key="OS_ENV_PASSWORD"),
Injection(var_name="subdomain", os_env_key="OS_ENV_SUBDOMAIN", is_required=False),
]
)
def f(url: str, user: str, password: str, subdomain: Optional[str]):
...
What will happen?
- If you explicitly pass a value for a parameter when you call
f
, it will be used - If no value is passed, then it will try to read it from the OS environment variable specified in
os_env_key
. - If no value is found in the OS environment, then it will raise an exception if
is_required
isTrue
(default). It will not raise an exception and set the value toNone
otherwise.
Hint: Injection(var_name="url")
is the same as Injection(var_name="url", os_env_key="OS_ENV_URL", is_required=True)
.
Setup development environment (for contributors only)
-
Create a virtual environment and activate it
python -m venv venv source venv/bin/activate
-
Install the developer dependencies you will need
pip install -U pip wheel setuptools pip install -e .[dev]
-
Set black as pre-commit package (will automatically apply black before committing)
pre-commit install
-
To run the tests
pytest
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 Distribution
Built Distribution
Hashes for os-env-injection-1.0.0rc0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b970d271cfdc7699a32815606fababbe2a3d1acf1f0b51b412bd44555518565 |
|
MD5 | 062be7db398dc1629ecb877a450e9a94 |
|
BLAKE2b-256 | f34d1288303279e21f22440537cfe908497b43bcf66f6f9ee2e00ebfa83bcdb2 |
Hashes for os_env_injection-1.0.0rc0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e4c1dd99e493187399327b999df8bdbc417267ebb29084a835b358ae444d6c7 |
|
MD5 | 7d119126463da75ac7b7c2ffc9bd2e35 |
|
BLAKE2b-256 | de2a65817c861e7e44d2242683b5922ccb5b14fee83270c5f8fbc19c2dcbdce4 |