Skip to main content

cast-from-env

Get and cast environment variables

Installation

python setup.py install

Usage

Call from_env() with the name of an environment variable, and either a default value to return if the environment variable is not set, or a type to cast the environment variable's value to if it is set. If a default value is passed and the variable is set, its value will be cast to the type of the default value.

Actually, any callable can be passed as the second parameter, and will be used to cast the value if it's not None. So if you pass e.g. json.loads a JSON env var value will be cast to a Python list or dict.

Also, if the type of the default value – or the type passed in instead – is bool, from_env() will return True if the environment variable's string value is any of '1', 'true', 't', 'yes', 'y', and 'on', and False otherwise.

Examples

from cast_from_env import from_env
from json import loads

from_env('STRING_VAR', 'default value')  # Value or 'default value' if not set
from_env('INTEGER_VAR', 100)  # Value cast to int, or 100 if not set
from_env('INTEGER_VAR', int)  # Value cast to int, or None if not set
from_env('FLOAT_VAR', 1.0)  # Value cast to float, or 1.0 if not set
from_env('TRUE_VAR', bool)  # True if value is '1', 'true', 'yes', or 'on'
from_env('TRUE_VAR', True)  # True if value is '1', 'true', 'yes', 'on', or not set
from_env('TEST_JSON', loads)  # JSON string is cast to Python list or dict

Metadata

Release files for cast-from-env 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cast-from-env 0.0.8
File Size Uploaded Provenance Actions
cast_from_env-0.0.8.tar.gz 2.5 kB Without provenance Details for cast_from_env-0.0.8.tar.gz

Release files / cast_from_env-0.0.8.tar.gz

Download URL cast_from_env-0.0.8.tar.gz
Size 2.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8f5ee9c09e79f442db86969b9f6ca1266759b76849a004c8f522592017cc1609
BLAKE2b-256 checksum
How to use checksums
4221b113ff62cfe4fdd6850ffa827589686f146965324c8f8598a7fc171adfbd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.16

Security for cast-from-env 0.0.8

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page