-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
43 lines (31 loc) · 1.14 KB
/
stats.py
File metadata and controls
43 lines (31 loc) · 1.14 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
import datetime
import json
def create_stats_file() -> None:
data = {
"filesExtracted": 0,
"stringsExtracted": 0,
"lastTimeUsed": "Never"
}
json_object = json.dumps(obj=data, indent=4)
with open(file="stats.json", mode="w+") as f:
f.write(json_object)
def update_stats(num_of_strings: int) -> None:
# Read current stats
with open(file="stats.json", mode="r") as f:
json_object = json.load(f)
files_extracted = json_object["filesExtracted"]
strings_extracted = json_object["stringsExtracted"]
# Add 1 to the number of extracted files
# and parameter num_of_strings to the number of extracted strings
new_data = {
"filesExtracted": files_extracted + 1,
"stringsExtracted": strings_extracted + num_of_strings,
"lastTimeUsed": datetime.datetime.now().strftime("%Y-%m-%d")
}
json_object = json.dumps(obj=new_data, indent=4)
# Write stats back to JSON file
with open(file="stats.json", mode="w") as f:
f.write(json_object)
def read_stats() -> dict:
with open(file="stats.json", mode="r") as f:
return json.load(f)