-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
64 lines (48 loc) · 1.89 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
import json
from datetime import datetime, timedelta
import os
from pathlib import Path
def add_to_json(new_data, filename='data.json'):
root = os.path.dirname(__file__)
with open(os.path.join(root, filename), 'r+') as file:
# First we load existing data into a dict.
file_data = json.load(file)
# Join new_data with file_data inside emp_details
file_data[f"{datetime.timestamp(datetime.now())}"] = new_data
# Sets file's current position at offset.
file.seek(0)
# convert back to json.
json.dump(file_data, file, indent=4)
def true_path(filename='data.json'):
root = os.path.dirname(__file__)
return os.path.join(root, filename)
def load_json(filename='data.json'):
root = os.path.dirname(__file__)
with open(os.path.join(root, filename), mode='r') as file:
return json.load(file)
def dump_json(data, filename='data.json'):
root = os.path.dirname(__file__)
with open(os.path.join(root, filename), mode='w') as file:
json.dump(data, file)
def date(ts):
return datetime.fromtimestamp(ts)
def touch_file(filename="data.json", default_content=None):
root = os.path.dirname(__file__)
path = os.path.join(root, filename)
if not os.path.exists(path):
print("Creating file for:", path)
if default_content is None:
default_content = {}
with open(path, "a") as file:
json.dump(default_content, file)
def emojiday(date):
"""From datetime"""
return ["☀️", "🌙", "🔥", "💧", "🌲", "⚜️", "⛰"][int(f"{date:%w}")]
def format_solve_time(solve_time):
solve_time = f"{timedelta(milliseconds=solve_time)}"
if len(solve_time) > 10:
solve_time = solve_time[:10]
*h, m, s = solve_time.split(":")
h = "" if h == ["0"] else ":".join(h) + "h"
m = "" if m == "00" else f"{int(m)}" + "m"
return f"{h} {m} {s}s"