/
utils.py
37 lines (27 loc) · 1.02 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
35
36
37
from typing import Dict, List, Union
def convert_to_list(a) -> List:
"""Convert wraps element in list if element isn't a list already."""
if a is None:
return []
elif isinstance(a, list):
return a
else:
return [a]
def convert_values_to_list(data: Dict) -> Dict:
"""Convert each value to a list."""
return {k: convert_to_list(v) for k, v in data.items()}
def filter_exact_match(
allowed_name: Union[str, List[str]], search_result: List[Dict]
) -> List[Dict]:
"""
Filter search results on items with exact matching names.
# Arguments
allowed_name: An allowed name, or list of allowed names
search_result: A list of dictionaries. Each dictionary must contain the key "name".
"""
allowed_names_list = convert_to_list(allowed_name)
return [s for s in search_result if s["name"] in allowed_names_list]
def filter_empty_values(data: Dict) -> Dict:
return {
k: v for k, v in data.items() if not (v is None or v == [] or v == {})
}