-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
55 lines (46 loc) · 1.55 KB
/
database.py
File metadata and controls
55 lines (46 loc) · 1.55 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
import json
class Database():
def __init__(self):
# load all data
with open('students.data') as json_file:
self.data = json.load(json_file)
def get(self, target, keyword):
for user in self.data:
if user[target] == keyword:
return user
return None
def save(self):
with open('students.data', mode='w') as f:
f.write(json.dumps(self.data, indent=2))
def addNew(self, studentID,username, password, fullname, lastname):
new_user= {
"studentID" : str(studentID),
"fullname": fullname,
"lastname": lastname,
"username": username,
"password": password,
"subjects" : [],
}
self.data.append(new_user)
self.save()
def update(self, target, keyword, update_target, new_data):
for user in self.data:
if user[target] == keyword:
user[update_target] = new_data
self.save()
return True
return False
def remove(self,studentID):
for user in self.data:
if user['studentID'] == studentID:
self.data.remove(user)
self.save()
return True
return False
def removeAll(self):
removes = []
for user in self.data:
removes.append(user)
for target in removes:
self.data.remove(target)
self.save()