-
Notifications
You must be signed in to change notification settings - Fork 33
/
utilities.py
81 lines (66 loc) · 2.29 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
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
77
78
79
80
81
import time
import datetime
import json
import random
import string
import hashlib
from pathlib import Path
def random_string(length):
"""return a random string."""
alphabet = string.ascii_lowercase+string.ascii_uppercase
return "".join([random.choice(alphabet) for i in range(0, length)])
def human_timestamp(now=None):
"""Return a human readable timestamp."""
if now is None:
now = time.time()
local_time = time.localtime(now)
return time.strftime("%Z - %A %Y/%B/%d, %H:%M:%S", local_time)
def json_serial(obj):
"""Serialize the datetime."""
if isinstance(obj, datetime.datetime):
timestamp = obj.timestamp()
return human_timestamp(timestamp)
return str(obj)
def read_json_file(json_path, default=None):
"""
Return the given json file as dictionary.
@param {string} `json_path`
@return {dictionary}
"""
json_path = Path(json_path)
if not json_path.is_file():
if default is None:
raise ValueError(f"You try to read {json_path}. "
f"The file does not exist and you "
f"furnished no default.")
return default
with open(json_path, 'r') as json_data:
try:
answer = json.load(json_data)
except json.decoder.JSONDecodeError as err:
print("JSONDecodeError:", err)
message = f"Json error in {json_path}:\n {err}"
raise ValueError(message) from err
return answer
def json_to_str(json_dict, pretty=False, default=None):
"""Return a string representation of the given json."""
if pretty:
return json.dumps(json_dict,
sort_keys=True,
indent=4,
default=json_serial)
return json.dumps(json_dict, default=default)
def write_json_file(json_dict,
filename,
pretty=False,
default=None):
"""Write the dictionary in the given file."""
my_str = json_to_str(json_dict, pretty=pretty, default=default)
with open(filename, 'w') as json_file:
json_file.write(my_str)
def text_hash(text):
"""Return a hash of a text."""
m = hashlib.sha256()
b_text = text.encode("utf8")
m.update(b_text)
return m.hexdigest()