mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
cast.from_env(): Add function
cast.from_env() takes an environment variable name instead of the string as the first argument and does the obvious thing. It also takes a default and saves the conversion in case the environment variable doesn't exist. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
8b6504f01b
commit
1a267dd2e5
1 changed files with 9 additions and 1 deletions
|
|
@ -1,6 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import pytimeparse
|
||||
import pytimeparse, os
|
||||
from datetime import datetime, timedelta
|
||||
from collections import OrderedDict
|
||||
|
||||
|
|
@ -97,6 +97,14 @@ def from_str(s: str, target_type=None, default_type=None, throw=True, log_level=
|
|||
slog(log_level, msg)
|
||||
return None
|
||||
|
||||
def from_env(key: str, default=None, target_type=None, default_type=None, throw=True, log_level=WARNING, caller=None): # export
|
||||
val = os.getenv(key)
|
||||
if val is None:
|
||||
return default
|
||||
if target_type is None and default is not None:
|
||||
target_type = type(default)
|
||||
return from_str(val, target_type=target_type, default_type=default_type, throw=throw, log_level=log_level, caller=caller)
|
||||
|
||||
# deprecated name
|
||||
def cast_str(s: str, target_type=None, default_type=None, throw=True, log_level=WARNING, caller=None):
|
||||
return from_str(s, target_type=target_type, default_type=None, throw=True, log_level=WARNING, caller=None)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue