pip install --upgrade git+https://github.com/ablaternae/python-dotenv-typecast
from dotenv_typecast import read_dotenv
env = read_dotenv()
my_string = env.str("ENV_STR")
This plugin extends the dotenv
library with methods read_dotenv()
, DotEnv.cast()
, DotEnv.getenv()
, DotEnv.__getattr__()
.
It works almost like environs
but without marshmallow
's 350kb and valitadion, type cast only.
import dotenv_typecast
you can import all functions from either dotenv
or dotenv_typecast
:
import dotenv_typecast
from dotenv import dotenv_values, load_dotenv, read_dotenv
equivalent
from dotenv_typecast import dotenv_values, load_dotenv, read_dotenv
the read_dotenv
signature is similar of other funcs, but returns instance of dotenv.main.DotEnv
def read_dotenv(
dotenv_path: Optional[StrPath] = None,
stream: Optional[IO[str]] = None,
verbose: bool = False,
override: bool = False,
interpolate: bool = True,
encoding: Optional[str] = "utf-8",
) -> DotEnv:
example:
env = read_dotenv(".env", override=True)
data_dir = env.path("DATA_DIR", default="my_data_dir")
- added method
DotEvn.env(key, default)
(synDotEvn.getenv()
) for getting data from environment variables, like aos.getenv(key, default)
, not from private attribute like originDotEvn.get()
- all type-cast processing goes via its new method
- so using the parameter
override
seems more logical - version growed to stable, plugin is ready to production
runs via DotEnv.__getattr__
def typecast_method(env_name: str, default_value = None, *args, **kwargs) -> Any
The following are all type-casting methods of DotEnv
:
env.str
env.bool
env.int
env.float
env.decimal
env.list
(accepts optionalsubcast
anddelimiter
keyword arguments)env.json
(casts to aJSON object
)env.datetime
(uses fromisoformat() method)env.date
(uses fromisoformat() method)env.time
(uses fromisoformat() method)env.timedelta
(assumes value is an int\float in seconds)env.timestamp
(assumes value is an int\float in seconds)env.url
(recommended to specify the scheme)env.uuid
(assumes value is string of 36 char or 32 without dash; casts to aUUID object
)env.path
(casts to apathlib.Path
)
env.dict
(not implemented yet; accepts optionalsubcast_keys
,subcast_values
anddelimiter
keyword arguments)env.enum
(not implemented yet; casts to any given enum type specified intype
keyword argument, accepts optionalignore_case
keyword argument)env.log_level
(will not be implemented)