-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
34 lines (28 loc) · 1.01 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from typing import List, Callable, Union
def listify(func: Callable[[any], any]) -> Callable[[List[any]], List[any]]:
"""Wrapper for making a function compatible with lists of objects.
Args:
func (Callable[[any], any]): the function to wrap
Returns:
Callable[[List[any]], List[any]]: the wrapped function that can handle lists of objects
"""
def wrapper(lst: Union[List[any], any]) -> Union[List[any], any]:
if isinstance(lst, list):
return [func(elem) for elem in lst]
else:
return func(lst)
wrapper.__name__ = func.__name__
return wrapper
def list_and_space_output_processor(x: any) -> str:
"""Output processor to handle -65 as space and lists of objects.
Args:
x (any): the object to process
Returns:
str: the processed display string
"""
if isinstance(x, list):
return ", ".join(map(list_and_space_output_processor, x))
elif x == -65:
return " "
else:
return str(x)