|
1
|
|
|
""" |
|
2
|
|
|
Support classes to help with querying and processing web data. |
|
3
|
|
|
""" |
|
4
|
|
|
from __future__ import annotations |
|
5
|
|
|
|
|
6
|
|
|
import re |
|
7
|
|
|
from datetime import date, datetime |
|
8
|
|
|
from typing import Any, Callable, FrozenSet, Iterable, Optional, Set, Type, TypeVar, Union |
|
9
|
|
|
|
|
10
|
|
|
from pocketutils.tools.base_tools import BaseTools |
|
|
|
|
|
|
11
|
|
|
from pocketutils.tools.string_tools import StringTools |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
from mandos.model.apis.pubchem_support._nav_model import FilterFn |
|
14
|
|
|
|
|
15
|
|
|
T = TypeVar("T") |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
empty_frozenset = frozenset([]) |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class Filter: |
|
|
|
|
|
|
21
|
|
|
@classmethod |
|
22
|
|
|
def has_key(cls, key: str) -> FilterFn: |
|
|
|
|
|
|
23
|
|
|
return FilterFn(lambda content: key in content) |
|
24
|
|
|
|
|
25
|
|
|
@classmethod |
|
26
|
|
|
def key_does_not_equal(cls, key: str, disallowed_value: Any) -> FilterFn: |
|
|
|
|
|
|
27
|
|
|
return FilterFn(lambda content: content.get(key) != disallowed_value) |
|
28
|
|
|
|
|
29
|
|
|
@classmethod |
|
30
|
|
|
def key_equals(cls, key: str, allowed_value: Any) -> FilterFn: |
|
|
|
|
|
|
31
|
|
|
return FilterFn(lambda content: content.get(key) == allowed_value) |
|
32
|
|
|
|
|
33
|
|
|
@classmethod |
|
34
|
|
|
def key_is_not_in(cls, key: str, disallowed_values: Set[Any]) -> FilterFn: |
|
|
|
|
|
|
35
|
|
|
return FilterFn(lambda content: content.get(key) not in disallowed_values) |
|
36
|
|
|
|
|
37
|
|
|
@classmethod |
|
38
|
|
|
def key_is_in(cls, key: str, allowed_values: Set[Any]) -> FilterFn: |
|
|
|
|
|
|
39
|
|
|
return FilterFn(lambda content: content.get(key) in allowed_values) |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
class Flatmap: |
|
|
|
|
|
|
43
|
|
|
@classmethod |
|
44
|
|
|
def construct(cls, tp: Type[T]) -> Callable[[Iterable[Any]], T]: |
|
|
|
|
|
|
45
|
|
|
""" |
|
46
|
|
|
Function that constructs an instance whose attributes are from the passed list. |
|
47
|
|
|
""" |
|
48
|
|
|
|
|
49
|
|
|
def construct(things: Iterable[str]) -> Optional[str]: |
|
50
|
|
|
return tp(*things) |
|
51
|
|
|
|
|
52
|
|
|
return construct |
|
53
|
|
|
|
|
54
|
|
|
@classmethod |
|
55
|
|
|
def join_nonnulls(cls, sep: str = "; ") -> Callable[[Iterable[str]], Optional[str]]: |
|
|
|
|
|
|
56
|
|
|
def opt_join(things: Iterable[str]) -> Optional[str]: |
|
57
|
|
|
x = [s.strip() for s in things] |
|
|
|
|
|
|
58
|
|
|
return None if len(x) == 0 else sep.join(x) |
|
59
|
|
|
|
|
60
|
|
|
return opt_join |
|
61
|
|
|
|
|
62
|
|
|
@classmethod |
|
63
|
|
|
def request_only(cls) -> Callable[[Iterable[str]], Optional[str]]: |
|
|
|
|
|
|
64
|
|
|
def only_nonreq(things: Iterable[str]) -> Optional[str]: |
|
65
|
|
|
# TODO: Did I mean to excludeNone here? |
|
|
|
|
|
|
66
|
|
|
things = [s.strip() for s in things if s is not None] |
|
67
|
|
|
if len(things) > 1: |
|
|
|
|
|
|
68
|
|
|
raise ValueError(f"{len(things)} items in {things}") |
|
69
|
|
|
elif len(things) == 0: |
|
70
|
|
|
return None |
|
71
|
|
|
else: |
|
72
|
|
|
return things[0] |
|
73
|
|
|
|
|
74
|
|
|
return only_nonreq |
|
75
|
|
|
|
|
76
|
|
|
@classmethod |
|
77
|
|
|
def require_only(cls) -> Callable[[Iterable[str]], Optional[str]]: |
|
|
|
|
|
|
78
|
|
|
return BaseTools.only |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
class Mapx: |
|
|
|
|
|
|
82
|
|
|
@classmethod |
|
83
|
|
|
def roman_to_arabic( |
|
|
|
|
|
|
84
|
|
|
cls, min_val: Optional[int] = None, max_val: Optional[int] = None |
|
|
|
|
|
|
85
|
|
|
) -> Callable[[str], int]: |
|
86
|
|
|
def roman_to_arabic(s: str) -> int: |
|
|
|
|
|
|
87
|
|
|
return StringTools.roman_to_arabic(s.strip(), min_val=min_val, max_val=max_val) |
|
88
|
|
|
|
|
89
|
|
|
return roman_to_arabic |
|
90
|
|
|
|
|
91
|
|
|
@classmethod |
|
92
|
|
|
def split_and_flatten_nonnulls( |
|
|
|
|
|
|
93
|
|
|
cls, sep: str, skip_nulls: bool = False |
|
|
|
|
|
|
94
|
|
|
) -> Callable[[Iterable[Union[str, int, float, None]]], Set[str]]: |
|
95
|
|
|
def split_flat(things: Iterable[str]) -> Set[str]: |
|
96
|
|
|
results = set() |
|
97
|
|
|
for thing in things: |
|
98
|
|
|
if thing is not None and thing != float("NaN") or not skip_nulls: |
|
99
|
|
|
# let it fail if skip_nulls is False |
|
100
|
|
|
for bit in str(thing).split(sep): |
|
101
|
|
|
results.add(bit.strip()) |
|
102
|
|
|
return results |
|
103
|
|
|
|
|
104
|
|
|
return split_flat |
|
105
|
|
|
|
|
106
|
|
|
@classmethod |
|
107
|
|
|
def extract_group_1( |
|
|
|
|
|
|
108
|
|
|
cls, pattern: Union[str, re.Pattern] |
|
|
|
|
|
|
109
|
|
|
) -> Callable[[Optional[Any]], Optional[str]]: |
|
110
|
|
|
pattern = pattern if isinstance(pattern, re.Pattern) else re.compile(pattern) |
|
111
|
|
|
|
|
112
|
|
|
def _match(thing: Optional[str]) -> Optional[str]: |
|
113
|
|
|
if thing is None: |
|
114
|
|
|
return None |
|
115
|
|
|
match = pattern.fullmatch(str(thing)) |
|
116
|
|
|
if match is None: |
|
117
|
|
|
return None |
|
118
|
|
|
return match.group(1) |
|
119
|
|
|
|
|
120
|
|
|
return _match |
|
121
|
|
|
|
|
122
|
|
|
@classmethod |
|
123
|
|
|
def lowercase_unless_acronym(cls) -> Callable[[str], str]: |
|
|
|
|
|
|
124
|
|
|
def lowercase_unless_acronym(s: str) -> str: |
|
|
|
|
|
|
125
|
|
|
s = s.strip() |
|
126
|
|
|
return s if s.isupper() else s.lower() |
|
127
|
|
|
|
|
128
|
|
|
return lowercase_unless_acronym |
|
129
|
|
|
|
|
130
|
|
|
@classmethod |
|
131
|
|
|
def n_bar_items( |
|
|
|
|
|
|
132
|
|
|
cls, sep: str = "||", null_is_zero: bool = False |
|
|
|
|
|
|
133
|
|
|
) -> Callable[[Optional[str]], int]: |
|
134
|
|
|
def n_bar_items(value: str) -> int: |
|
135
|
|
|
if null_is_zero and value is None: |
|
136
|
|
|
return 0 |
|
137
|
|
|
return len(value.split(sep)) |
|
138
|
|
|
|
|
139
|
|
|
return n_bar_items |
|
140
|
|
|
|
|
141
|
|
|
@classmethod |
|
142
|
|
|
def not_null(cls) -> Callable[[Any], bool]: |
|
|
|
|
|
|
143
|
|
|
return lambda value: value is not None |
|
144
|
|
|
|
|
145
|
|
|
@classmethod |
|
146
|
|
|
def identity(cls) -> Callable[[T], T]: |
|
|
|
|
|
|
147
|
|
|
return lambda value: value |
|
148
|
|
|
|
|
149
|
|
|
@classmethod |
|
150
|
|
|
def int_date(cls, nullable: bool = False) -> Callable[[Optional[str]], Optional[date]]: |
|
|
|
|
|
|
151
|
|
|
def int_date(s: Optional[str]) -> Optional[date]: |
|
|
|
|
|
|
152
|
|
|
if s is None and nullable: |
|
153
|
|
|
return None |
|
154
|
|
|
return datetime.strptime(str(s).strip(), "%Y%m%d").date() |
|
155
|
|
|
|
|
156
|
|
|
return int_date |
|
157
|
|
|
|
|
158
|
|
|
@classmethod |
|
159
|
|
|
def get_str(cls, nullable: bool = False) -> Callable[[str], Optional[str]]: |
|
|
|
|
|
|
160
|
|
|
def get_str(value): |
|
161
|
|
|
if nullable and value is None: |
|
|
|
|
|
|
162
|
|
|
return None |
|
163
|
|
|
elif not isinstance(value, (int, float, str)): |
|
164
|
|
|
raise TypeError(f"{value} is a {type(value)}, not int-like") |
|
165
|
|
|
return str(value) |
|
166
|
|
|
|
|
167
|
|
|
return get_str |
|
168
|
|
|
|
|
169
|
|
|
@classmethod |
|
170
|
|
|
def get_float(cls, nullable: bool = False) -> Callable[[str], Optional[str]]: |
|
|
|
|
|
|
171
|
|
|
def get_float(value): |
|
172
|
|
|
if nullable and value is None: |
|
|
|
|
|
|
173
|
|
|
return None |
|
174
|
|
|
elif not isinstance(value, (int, float, str)): |
|
175
|
|
|
raise TypeError(f"{value} is a {type(value)}, not int-like") |
|
176
|
|
|
return float(value) |
|
177
|
|
|
|
|
178
|
|
|
return get_float |
|
179
|
|
|
|
|
180
|
|
|
@classmethod |
|
181
|
|
|
def get_int(cls, nullable: bool = False) -> Callable[[str], Optional[int]]: |
|
|
|
|
|
|
182
|
|
|
def get_int(value): |
|
183
|
|
|
if nullable and value is None: |
|
|
|
|
|
|
184
|
|
|
return None |
|
185
|
|
|
elif not isinstance(value, (int, float, str)): |
|
186
|
|
|
raise TypeError(f"{value} is a {type(value)}, not int-like") |
|
187
|
|
|
return int(value) |
|
188
|
|
|
|
|
189
|
|
|
return get_int |
|
190
|
|
|
|
|
191
|
|
|
@classmethod |
|
192
|
|
|
def req_is(cls, type_, nullable: bool = False, then_convert=None) -> Callable[[str], str]: |
|
|
|
|
|
|
193
|
|
|
def req_is(value): |
|
194
|
|
|
if nullable and value is None: |
|
195
|
|
|
pass |
|
196
|
|
|
elif not isinstance(value, type_): |
|
197
|
|
|
raise TypeError(f"{value} is a {type(value)}, not {type_}") |
|
198
|
|
|
return value if then_convert is None else then_convert(value) |
|
199
|
|
|
|
|
200
|
|
|
req_is.__name__ = f"req_is_{type_}" + ("_or_null" if nullable else "") |
|
201
|
|
|
return req_is |
|
202
|
|
|
|
|
203
|
|
|
@classmethod |
|
204
|
|
|
def str_to( |
|
|
|
|
|
|
205
|
|
|
cls, type_: Callable[[str], T], nullable: bool = False, flex_type: bool = False |
|
|
|
|
|
|
206
|
|
|
) -> Callable[[str], str]: |
|
207
|
|
|
def str_to(value: Optional[str]) -> Optional[T]: |
|
208
|
|
|
if value is None and nullable: |
|
|
|
|
|
|
209
|
|
|
return None |
|
210
|
|
|
elif value is None: |
|
211
|
|
|
raise ValueError(f"Value for type {type_} is None") |
|
212
|
|
|
if type_ is not None and not flex_type and not isinstance(value, str): |
|
213
|
|
|
raise TypeError(f"{value} is a {type(value)}, not str") |
|
214
|
|
|
return type_(str(value).strip()) |
|
215
|
|
|
|
|
216
|
|
|
str_to.__name__ = f"req_is_{type_}" + ("_or_null" if nullable else "") |
|
217
|
|
|
return str_to |
|
218
|
|
|
|
|
219
|
|
|
@classmethod |
|
220
|
|
|
def split_to( |
|
|
|
|
|
|
221
|
|
|
cls, type_, sep: str = "||", nullable: bool = False |
|
|
|
|
|
|
222
|
|
|
) -> Callable[[Optional[str]], FrozenSet[int]]: |
|
223
|
|
|
def split_to(value: str) -> FrozenSet[int]: |
|
224
|
|
|
return frozenset([type_(x) for x in cls.split(sep, nullable=nullable)(value)]) |
|
225
|
|
|
|
|
226
|
|
|
return split_to |
|
227
|
|
|
|
|
228
|
|
|
@classmethod |
|
229
|
|
|
def split(cls, sep: str, nullable: bool = False) -> Callable[[Optional[str]], FrozenSet[str]]: |
|
|
|
|
|
|
230
|
|
|
def split(value: str) -> FrozenSet[str]: |
|
231
|
|
|
if value is None and nullable: |
|
|
|
|
|
|
232
|
|
|
return empty_frozenset |
|
233
|
|
|
elif value is None: |
|
234
|
|
|
raise ValueError(f"Value is None") |
|
|
|
|
|
|
235
|
|
|
return frozenset([s.strip() for s in str(value).split(sep)]) |
|
236
|
|
|
|
|
237
|
|
|
return split |
|
238
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
__all__ = ["Filter", "Mapx", "Flatmap"] |
|
241
|
|
|
|