Skip to content
/ envcast Public

Package for environment variables parsing with type casting

License

Notifications You must be signed in to change notification settings

xfenix/envcast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

86 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.