envcast
Package for environment variables parsing with type casting. Why do you need it? Because you can't just grab environment variables as is, you need to cast them to desired types for your application (for example like bool variable: how to cast strings False, "", 0 to bool without boilerplaite?).
This packages just cast needed environment variables to desired types with syntax very familiar to os.getenv users.
Plus this package has good test coverage and quality codebase.
Written in modern python 3.7+ with full support of:
- https://www.python.org/dev/peps/pep-0526/
- https://www.python.org/dev/peps/pep-0484/
- https://www.python.org/dev/peps/pep-0008/
- https://www.python.org/dev/peps/pep-0257/
- https://www.python.org/dev/peps/pep-0518/
- https://www.python.org/dev/peps/pep-0585/
TL;DR
It behaves very similar to os.getenv:
import envcast
# result will be casted to str
envcast.env('SOME_ENV_VAR', 'defaultvalue', type_cast=str)
# result will be casted to bool (if it like 1 or on or true/True -> if will be python True)
# BUT, if there is no value, default value is None will be casted to bool, so it will be False
envcast.env('SOME_BOOL_ENV_VAR', type_cast=bool)
Usage, API, more details and examples
Signature of env and dotenv absolutely similar and looks like this:
# var_name is desired variable name
# default_value going through type casting, so it must be in desired type
# type_cast — desired variable type casting function
# list_type_cast applies if type_cast is list, tuple
envcast.env(var_name: str, default_value: typing.Any = None,
type_cast: type = str, list_type_cast: type = str)
From environment variables
For casting good old plain env variables you will need do following:
import envcast
this_will_be_bool: bool = envcast.env('SOME_ENV_VARIABLE', 'false', type_cast=bool))
or_this_is_string_by_default: str = envcast.env('OTHER_ENV_VAR')
this_is_int: int = envcast.env('MORE_ENV', type_cast=int)
From .env file
If your are using .env file, you can do it too:
import envcast
envcast.set_dotenv_path('.')
# Can be any of the following :
# envcast.set_dotenv_path('~/some/.env')
# envcast.set_dotenv_path('/tmp/.env')
# envcast.set_dotenv_path('/tmp/')
this_will_be_bool: bool = envcast.dotenv('SOME_ENV_VARIABLE', 'false', type_cast=bool))
or_this_is_string_by_default: str = envcast.dotenv('OTHER_ENV_VAR')
this_is_int: int = envcast.dotenv('MORE_ENV', type_cast=int)
Dont worry, file will be readed and parsed only once.
Exceptions
- envcast.exceptions.IncorrectDotenvPath
- envcast.exceptions.NotSettedDotenvPath
- envcast.exceptions.BrokenDotenvStructure
Supported casting types
You can pass to type_cast or list_type_cast any desired type casting callables.
It may be any builtin type, of Decimal, Path, or any other callable.
Listing values
If you want to parse and cast environment variable with list of values:
MY_FANCY_VAR=True, On, Ok, False, Disabled, 0
You will need expression like this:
envcast.env('MY_FANCY_VAR', type_cast=bool, list_type_cast=list)
If you cares about what exactly can be separator for list values: it can be ,, (space) or |.
Changelog
You can check https://github.com/xfenix/envcast/releases/ release page.
Metadata
Release files for envcast 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| envcast-1.2.0.tar.gz | 7.5 kB | Without provenance | Details for envcast-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| envcast-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for envcast-1.2.0-py3-none-any.whl |
Total release size: 15.3 kB
Release files / envcast-1.2.0.tar.gz
| Download URL | envcast-1.2.0.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bbd4342af70fab3c3c652e52ce3dd5055d563338352f8816b14ef52d601b8e0a
|
|
BLAKE2b-256 checksum How to use checksums |
05ea6863501080fa5ce154df1cfd3891d393d2a8b9d858c3ba900dacaabeef4d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.9.12 Linux/5.13.0-1021-azure
|
Release files / envcast-1.2.0-py3-none-any.whl
| Download URL | envcast-1.2.0-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
db52c9448da0fc4f88967911763a471e55708373793d4fcce1124609a9f124bd
|
|
BLAKE2b-256 checksum How to use checksums |
e804ba34a045c2428fce111dec92105a31d2cea3ffa34ab4f19b565bf0bdaca9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.9.12 Linux/5.13.0-1021-azure
|
Security for envcast 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.