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