-
Notifications
You must be signed in to change notification settings - Fork 0
/
citizen.py
112 lines (98 loc) · 3.31 KB
/
citizen.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from tinydb import TinyDB, Query
class Citizen:
def __init__(self, name, password):
self.name = name
self.password = password
self.score = 0
self.num_of_ratings = 0
self.education = 0
def toDict(self):
return {
'name':self.name,
'password':self.password,
'score':self.score,
'num_of_ratings':self.num_of_ratings,
'education':self.education}
def getLevel(self):
if self.score > 90:
return 1
if self.score > 70:
return 2
if self.score > 55:
return 3
if self.score > 45:
return 4
if self.score > 30:
return 5
if self.score > 10:
return 6
return 7
class CitizenManager:
def __init__(self, filename):
self.db_filename = filename
def constructCitizen(self, name, password, score, num_of_ratings, education):
citizen = Citizen(name, password)
citizen.score = score
citizen.num_of_ratings = num_of_ratings
citizen.education = education
return citizen
def persist(self, citizen):
db = TinyDB(self.db_filename)
query = Query()
if not db.contains(query.name == citizen.name):
db.insert(citizen.toDict())
db.close()
return True
db.close()
return False
def update(self, citizen):
db = TinyDB(self.db_filename)
query = Query()
if citizen.name != None:
if citizen.score > 100:
citizen.score = 100
if citizen.score < 0:
citizen.score = 0
# Updated if exists, inserted if not
db.upsert(citizen.toDict(), query.name == citizen.name)
db.close()
return True
db.close()
return False
def getAll(self):
db = TinyDB(self.db_filename)
db_result = db.all()
result = []
for person in db_result:
if not 'admin' in person['name']:
result.append(self.constructCitizen(person['name'],
person['password'],
person['score'],
person['num_of_ratings'],
person['education']))
db.close()
return result
def getByName(self, name):
db = TinyDB(self.db_filename)
query = Query()
db_result = db.search(query.name == name)
if len(db_result) == 1:
person = db_result[0]
citizen = self.constructCitizen(person['name'],
person['password'],
person['score'],
person['num_of_ratings'],
person['education'])
db.close()
return citizen
db.close()
return None
def removeByName(self, name):
db = TinyDB(self.db_filename)
query = Query()
db.remove(query.name == name)
db.close()
def clearDb(self):
db = TinyDB(self.db_filename)
db.purge()
db.close()