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_emptyisn’t specified, then the original valuevwill 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
vis empty (defaults toNone)not_empty – The value to return if
vis 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 int0or 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
vis returned ifnot_emptyisn’t specified.- Return K is_empty
The value specified as
is_emptyis returned ifvis empty- Return T not_empty
The value specified as
not_emptyis returned ifvis not empty (and not_empty was specified)