empty_if¶
-
privex.helpers.common.
empty_if
(v: V, is_empty: K = None, not_empty: T = <class 'privex.helpers.types.UseOrigVar'>, **kwargs) → Union[T, K, V][source]¶ Syntactic sugar for
x if empty(y) else z
. Ifnot_empty
isn’t specified, then the original valuev
will be returned if it’s not empty.Example 1:
>>> def some_func(name=None): ... name = empty_if(name, 'John Doe') ... return name >>> some_func("") John Doe >>> some_func("Dave") Dave
Example 2:
>>> empty_if(None, 'is empty', 'is not empty') is empty >>> empty_if(12345, 'is empty', 'is not empty') is not empty
- Parameters
v (Any) – The value to test for emptiness
is_empty – The value to return if
v
is empty (defaults toNone
)not_empty – The value to return if
v
is not empty (defaults to the original valuev
)kwargs – Any additional kwargs to pass to
empty()
- Key zero
if
zero=True
, then v is empty if it’s int0
or str'0'
- Key itr
if
itr=True
, then v is empty if it’s[]
,{}
, or is an iterable and has 0 length- Return V orig_var
The original value
v
is returned ifnot_empty
isn’t specified.- Return K is_empty
The value specified as
is_empty
is returned ifv
is empty- Return T not_empty
The value specified as
not_empty
is returned ifv
is not empty (and not_empty was specified)