-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.py
More file actions
executable file
·76 lines (58 loc) · 1.79 KB
/
utility.py
File metadata and controls
executable file
·76 lines (58 loc) · 1.79 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
"""
* Copyright (c) 2022, William Minidis <william.minidis@protonmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
Provides utility function for database and ip address.
"""
from typing import IO, Union
from io import TextIOBase
from typing import Dict, Any, Iterable
from ipaddress import ip_address, IPv4Address, IPv6Address
from json import load, loads, JSONDecodeError
from scipy.special import comb
def is_ipv4(string: str) -> bool:
"""
Validates a string for IPv4.
"""
try:
result = ip_address(string)
except ValueError:
return False
return isinstance(result, IPv4Address)
def is_ipv6(string: str) -> bool:
"""
Validates a string for IPv6.
"""
try:
result = ip_address(string)
except ValueError:
return False
return isinstance(result, IPv6Address)
def load_json(stream) -> Union[dict, Exception]:
try:
if isinstance(stream, TextIOBase):
json_data = load(stream)
elif isinstance(stream, str):
json_data = loads(stream)
else:
raise TypeError(f"Unknown type to load json from: {stream}")
except (JSONDecodeError, TypeError) as err:
return err
return json_data
def extract_keys(dct: Dict[Any, Any], keys: Iterable[Any]) -> Dict[Any, Any]:
"""
Try to extract specific keys for a dictionary, defaulting value to None if key does not exist.
"""
return {key: try_get_key(key, dct) for key in keys}
def try_get_key(key: Any, dct: Dict[Any, Any]) -> Any:
"""
Tires to get a key from a dictionary, returns None if it does not exist.
"""
if key in dct:
return dct[key]
return None
def str_join(*strings, sep="") -> str:
"""
Joins multiple strings with optional separator.
"""
return sep.join(strings)