Skip to main content

This module helps to cast environment variables to desired types. It may be very useful for 12factor app usage.

Project description

envcast

Build and publish PyPI version codecov Code style: black Imports: isort

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:

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envcast-1.2.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

envcast-1.2.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file envcast-1.2.0.tar.gz.

File metadata

  • Download URL: envcast-1.2.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.12 Linux/5.13.0-1021-azure

File hashes

Hashes for envcast-1.2.0.tar.gz
Algorithm Hash digest
SHA256 bbd4342af70fab3c3c652e52ce3dd5055d563338352f8816b14ef52d601b8e0a
MD5 777e7dea7d6157a746cb056b197b695a
BLAKE2b-256 05ea6863501080fa5ce154df1cfd3891d393d2a8b9d858c3ba900dacaabeef4d

See more details on using hashes here.

File details

Details for the file envcast-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: envcast-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.12 Linux/5.13.0-1021-azure

File hashes

Hashes for envcast-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db52c9448da0fc4f88967911763a471e55708373793d4fcce1124609a9f124bd
MD5 f4cec51a9bce091208370a5c6eb33673
BLAKE2b-256 e804ba34a045c2428fce111dec92105a31d2cea3ffa34ab4f19b565bf0bdaca9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page