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. If not_empty isn’t specified, then the original value v 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 to None)

  • not_empty – The value to return if v is not empty (defaults to the original value v)

  • kwargs – Any additional kwargs to pass to empty()

Key zero

if zero=True, then v is empty if it’s int 0 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 if not_empty isn’t specified.

Return K is_empty

The value specified as is_empty is returned if v is empty

Return T not_empty

The value specified as not_empty is returned if v is not empty (and not_empty was specified)