forked from qbreader/python-module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_api_utils.py
More file actions
213 lines (177 loc) · 7.36 KB
/
_api_utils.py
File metadata and controls
213 lines (177 loc) · 7.36 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""Useful functions used by both the asynchronous and synchronous API wrappers."""
from __future__ import annotations
import warnings
from enum import Enum, EnumType
from typing import Iterable, Optional, Union, Tuple
from qbreader.types import (
Difficulty,
Category,
Subcategory,
AlternateSubcategory,
UnnormalizedCategory,
UnnormalizedDifficulty,
UnnormalizedSubcategory,
UnnormalizedAlternateSubcategory,
)
def normalize_bool(boolean: Optional[Union[bool, str]]) -> str:
"""Normalize a boolean value to a string for HTTP requests."""
if isinstance(boolean, bool):
return str(boolean).lower()
elif isinstance(boolean, str):
if (boolean := boolean.lower()) in ("true", "false"):
return boolean
else:
raise ValueError(f"Invalid str value: {boolean}")
else:
raise TypeError(f"Invalid type: {type(boolean).__name__}, expected bool or str")
def normalize_enumlike(
unnormalized: Optional[Union[Enum, str, int, Iterable[Union[Enum, str, int]]]],
enum_type: EnumType,
) -> str:
"""Normalize a single or list of enum-like values into a comma separated string."""
def valid_enumlike(item: Union[Enum, str, int]) -> bool:
"""Check if an item is a valid enum-like value."""
return (
item in enum_type.__members__.values()
or str(item) in enum_type.__members__.values()
)
strs: list[str] = []
if unnormalized is None:
return ""
if isinstance(unnormalized, (str, int, enum_type)): # single item
if isinstance(unnormalized, bool): # python bools are ints
raise TypeError(
f"Invalid type: {type(unnormalized).__name__}, expected int, str, or "
+ f"{enum_type}."
)
if valid_enumlike(unnormalized): # type: ignore
# this is ok to ignore because it's counting strings as iterables
strs.append(str(unnormalized))
return ",".join(strs)
else:
warnings.warn(
f"Invalid value: {unnormalized} for {enum_type}.", UserWarning
)
return ""
if isinstance(unnormalized, Iterable): # iterable of str, int, or Difficulty
for item in unnormalized:
if isinstance(item, (str, int, enum_type)):
if valid_enumlike(item):
strs.append(str(item))
else:
warnings.warn(
f"Invalid value: {item} for {enum_type}.", UserWarning
)
else:
raise TypeError(
f"Invalid type: {type(item).__name__}, expected int, str, or "
+ f"{enum_type}."
)
strs = list(set(strs)) # remove duplicates
return ",".join(strs)
raise TypeError(
f"Invalid type: {type(unnormalized).__name__}, expected int, str, {enum_type}, "
+ "or Iterable of those."
)
def normalize_diff(unnormalized_diffs: UnnormalizedDifficulty):
"""Normalize a single or list of difficulty values to a comma separated string."""
return normalize_enumlike(unnormalized_diffs, Difficulty)
def normalize_cat(unnormalized_cats: UnnormalizedCategory):
"""Normalize a single or list of categories to a comma separated string."""
return normalize_enumlike(unnormalized_cats, Category)
def normalize_subcat(unnormalized_subcats: UnnormalizedCategory):
"""Normalize a single or list of subcategories to a comma separated string."""
return normalize_enumlike(unnormalized_subcats, Category)
def category_correspondence(
typed_alt_subcat: AlternateSubcategory,
) -> Tuple[Category, Subcategory]:
if typed_alt_subcat in [
AlternateSubcategory.ASTRONOMY,
AlternateSubcategory.COMPUTER_SCIENCE,
AlternateSubcategory.MATH,
AlternateSubcategory.EARTH_SCIENCE,
AlternateSubcategory.ENGINEERING,
AlternateSubcategory.MISC_SCIENCE,
]:
return (None, Subcategory.OTHER_SCIENCE)
if typed_alt_subcat in [
AlternateSubcategory.ARCHITECTURE,
AlternateSubcategory.DANCE,
AlternateSubcategory.FILM,
AlternateSubcategory.JAZZ,
AlternateSubcategory.OPERA,
AlternateSubcategory.PHOTOGRAPHY,
AlternateSubcategory.MISC_ARTS,
]:
return (None, Subcategory.OTHER_FINE_ARTS)
if typed_alt_subcat in [
AlternateSubcategory.ANTHROPOLOGY,
AlternateSubcategory.ECONOMICS,
AlternateSubcategory.LINGUISTICS,
AlternateSubcategory.PSYCHOLOGY,
AlternateSubcategory.SOCIOLOGY,
AlternateSubcategory.OTHER_SOCIAL_SCIENCE,
]:
return (None, Subcategory.SOCIAL_SCIENCE)
if typed_alt_subcat in [
AlternateSubcategory.DRAMA,
AlternateSubcategory.LONG_FICTION,
AlternateSubcategory.POETRY,
AlternateSubcategory.SHORT_FICTION,
AlternateSubcategory.MISC_LITERATURE,
]:
return (Category.LITERATURE, None)
def normalize_cats(
unnormalized_cats: UnnormalizedCategory,
unnormalized_subcats: UnnormalizedSubcategory,
unnormalized_alt_subcats: UnnormalizedAlternateSubcategory,
) -> Tuple[Category, Subcategory, AlternateSubcategory]:
"""
Normalize a single or list of categories, subcategories, and alternate_subcategories
to their corresponding comma-separated strings, taking into account categories and
subcategories that must be added for the alternate_subcategories to work.
"""
typed_alt_subcats: list[AlternateSubcategory] = []
if isinstance(unnormalized_alt_subcats, str):
typed_alt_subcats.append(AlternateSubcategory(unnormalized_alt_subcats))
elif isinstance(unnormalized_alt_subcats, Iterable):
for alt_subcat in unnormalized_alt_subcats:
typed_alt_subcats.append(AlternateSubcategory(alt_subcat))
to_be_pushed_cats: list[Category] = []
to_be_pushed_subcats: list[Subcategory] = []
for alt_subcat in typed_alt_subcats:
cat, subcat = category_correspondence(alt_subcat)
if cat:
to_be_pushed_cats.append(cat)
if subcat:
to_be_pushed_subcats.append(subcat)
final_cats = []
if unnormalized_cats is None:
final_cats = to_be_pushed_cats
elif isinstance(unnormalized_cats, str):
final_cats = [Category(unnormalized_cats), *to_be_pushed_cats]
elif isinstance(unnormalized_cats, Iterable):
for subcat in unnormalized_cats:
final_cats.append(Subcategory(subcat))
final_cats.append(*to_be_pushed_cats)
final_subcats = []
if unnormalized_subcats is None:
final_subcats = to_be_pushed_subcats
elif isinstance(unnormalized_subcats, str):
final_subcats = [Subcategory(unnormalized_subcats), *to_be_pushed_subcats]
elif isinstance(unnormalized_subcats, Iterable):
for subcat in unnormalized_subcats:
final_subcats.append(Subcategory(subcat))
final_subcats.append(*to_be_pushed_subcats)
return (
normalize_enumlike(final_cats, Category),
normalize_enumlike(final_subcats, Subcategory),
normalize_enumlike(typed_alt_subcats, AlternateSubcategory),
)
def prune_none(params: dict) -> dict:
"""Remove all None values from a dictionary."""
return {
key: value
for key, value in params.items()
if (value is not None and key is not None)
}