-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Utilities.py
32 lines (26 loc) · 1.2 KB
/
Utilities.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
from datetime import datetime
def safe_decode(data):
encodings = ['utf-8', 'utf-16', 'gbk', 'iso-8859-1', 'iso-8859-5', 'iso-8859-6', 'big5', 'shift_jis', 'euc-kr', 'euc-jp', 'windows-1252', 'windows-1251', 'windows-1256']
for encoding in encodings:
try:
return data.decode(encoding)
except UnicodeDecodeError:
pass
return data.decode('utf-8', 'replace') # Default to utf-8 with replacement
# Convert bytes to string recursively over a dictionary or list and decode bytes safely into a string
def handle_bytes(obj):
if isinstance(obj, bytes):
return safe_decode(obj)
elif isinstance(obj, list):
return [handle_bytes(item) for item in obj]
elif isinstance(obj, dict):
return {key: handle_bytes(value) for key, value in obj.items()}
else:
return obj
def ns_to_datetime(ns, formatting='%Y-%m-%d %H:%M:%S.%f'):
# Convert nanoseconds to seconds
seconds = ns / 1_000_000_000
# Create a datetime object
dt_object = datetime.fromtimestamp(seconds)
# Format the datetime object as a string
return dt_object.strftime(formatting)[:-3] # trimming microseconds to milliseconds