This repository was archived by the owner on Aug 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.py
More file actions
58 lines (50 loc) · 1.95 KB
/
read.py
File metadata and controls
58 lines (50 loc) · 1.95 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import json
class Read:
""" Store infomation such as name and score into the database """
def __init__(self, name, score):
if not isinstance(name, str):
raise TypeError ("name must be a string")
self.name = name
if not isinstance(score, int):
raise ValueError("score must be integer")
self.score = score
def __repr__(self) -> str:
return f"Player(name='{self.name}', score={self.score})"
def insert(self):
""" Insert name and their score to database """
new_data = {
self.name:{
'score':self.score}
}
try:
with open('database.json','r') as data_file:
data = json.load(data_file)
except FileNotFoundError:
with open('database.json','w') as data_file:
json.dump(new_data, data_file, indent=4)
else:
data.update(new_data)
with open('database.json','w') as data_file:
json.dump(data, data_file, indent=4)
def get_top(self):
""" Return list contain player and score sorted by their score """
try:
with open('database.json','r') as data_file:
data = json.load(data_file)
except FileNotFoundError:
print("No data File Found")
else:
# get name and their score into name:score
res = dict()
for i in data:
res[i] = data[i]['score']
# sort dict by value Decsending Order
sorted_score = dict()
sorted_name = sorted(res,key=res.get,reverse=True)
for name in sorted_name:
sorted_score[name] = res[name]
# convert dict to list to get player and their score
name_score = sorted_score.items()
# highest score list
highest_score = list(name_score)[:15]
return highest_score