-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper_functions.py
45 lines (37 loc) · 1.25 KB
/
helper_functions.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
38
39
40
41
42
43
44
45
import re
from typing import Dict, List, Optional
def cast_to_float(s: str) -> float:
if isinstance(s, str):
s = s.strip()
return float(s)
def parse_float(s: str, context: str = "") -> float:
"""Parses a string into a float. Handles scientific notation."""
# Remove leading and trailing non-numeric characters
s = str(s)
numeric_re_sub = f"[^0-9eE\-\+\.]"
s_trimmed = re.sub(f"^{numeric_re_sub}+", "", s)
s_trimmed = re.sub(f"{numeric_re_sub}+$", "", s_trimmed)
try:
return float(s_trimmed)
except (ValueError, TypeError) as e:
raise ValueError(
f'Could not parse "{s}" from "{context}" as a float.'
) from e
def get_value_from_entry(
entry: Dict[str, str],
target: str,
) -> Optional[float]:
"""Searches an entry for a value matching the target and quantifiers.
Scales by any attached quantifiers and the base unit.
Args:
entry (Dict[str, str]): Entry to search
target (str): Target to search for. "energy" or "area"
Returns:
Optional[float]: Scaled value, or None if not found
"""
value = None
for k in entry:
if target in k.lower():
value = cast_to_float(entry[k])
break
return value