-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.py
More file actions
35 lines (23 loc) · 938 Bytes
/
utils.py
File metadata and controls
35 lines (23 loc) · 938 Bytes
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
import importlib.util
import re
def has_module(module_name: str) -> bool:
"""Check if module is available."""
return importlib.util.find_spec(module_name) is not None
def extract_digits(value: int | str) -> int:
value_str = str(value).strip("\"'")
digit_match = re.search(r"\d+", value_str)
if not digit_match:
raise ValueError(f"Input '{value}' does not contain any digits")
extracted_digits = digit_match.group()
if not extracted_digits.isdigit():
raise ValueError(f"Extracted value '{extracted_digits}' is not a valid digit string")
return int(extracted_digits)
def normalize_chain_id(
in_value: int | str | list[int | str] | None,
) -> int | list[int] | None:
"""Normalize str values integers."""
if in_value is None:
return None
if isinstance(in_value, list):
return [extract_digits(c) for c in in_value]
return extract_digits(in_value)