make_iterable#
- make_iterable(obj: Any, str_allowed: bool = False, convert_none: bool = False) list [source]#
Convert the object to a list if this object is not iterable
Useful to replace this kind of code:
>>> if to_convert is not None and not isinstance(to_convert, (list, tuple)): >>> to_convert = [to_convert]
by:
>>> to_convert = make_iterable(to_convert)
or:
>>> if isinstance(my_items, (list, tuple)): >>> first_item = my_items[0] >>> else: >>> first_item = my_items
by:
>>> first_item = make_iterable(to_convert)[0]
- Parameters:
- Returns:
Object as an iterable
- Return type:
Examples
>>> make_interable((1, 2, 3)) (1, 2, 3) >>> make_interable([1, 2, 3]) [1, 2, 3] >>> make_interable({1, 2, 3}) {1, 2, 3} >>> make_interable(np.array([1, 2, 3])) np.array([1, 2, 3]) >>> make_interable("1, 2, 3", str_allowed=True) "1, 2, 3" >>> make_interable("1, 2, 3", str_allowed=False) ["1, 2, 3"] >>> make_interable(1) [1] >>> make_interable(AnyPath("1, 2, 3")) [AnyPath("1, 2, 3")] >>> make_interable(None) None >>> make_interable(None, convert_none=True) [None]