-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
68 lines (46 loc) · 1.44 KB
/
utils.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import gzip
import socket
import threading
from urllib.parse import unquote
import psutil
def get_local_ip() -> str:
"""
Gets the localhost IP address.
:return: the localhost IP address
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("www.google.com", 80))
ip, _ = s.getsockname()
s.close()
return ip
def compress_article(article_raw_bytes: bytes) -> bytes:
"""
Compresses an article object.
:param article_raw_bytes: the uncompressed article object
:return: the compressed article object
"""
return gzip.compress(article_raw_bytes)
def get_avg_cpu_percent() -> float:
"""
Gets the average CPU usage.
:return: the average CPU usage
"""
dict_key = "avg_cpu_percent"
def meta(retval: dict, iterations: int = 10) -> None:
"""
Calculates the average CPU usage in percentage.
:param retval: a dictionary containing the average CPU usage
:param iterations: number of measurements to be performed
"""
avg = 0
for _ in range(iterations):
avg += psutil.cpu_percent(interval=0.5)
retval[dict_key] = avg / iterations
retval = {}
profiling_thread = threading.Thread(target=meta, args=(retval,))
profiling_thread.start()
profiling_thread.join()
return retval[dict_key]
def is_url_encoded(path: str) -> bool:
unquoted = unquote(path)
return path != unquoted