-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.py
More file actions
46 lines (36 loc) · 1.15 KB
/
interface.py
File metadata and controls
46 lines (36 loc) · 1.15 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
from pickle import dump, load
PATH = "groups.txt"
class Group:
def __init__(self, title: str):
self.title = title
self.terms: list[list[str]] = []
def new_term(self, term: str, definition: str):
self.terms.append([term, definition])
def sort_groups():
pass
def restore_groups(groups: list[Group]):
with open(PATH, 'wb') as file:
for group in groups:
dump(group, file)
def store_group(group: Group):
with open(PATH, 'ab') as file:
dump(group, file)
def retrieve_groups() -> list[Group]:
groups = []
with open(PATH, 'rb') as file:
while True:
try:
group = load(file)
groups.append(group)
except EOFError:
break
return groups
if __name__ == "__main__":
from random import randrange
groups = []
for group_num in range(50):
group = Group(f"Group {group_num} Title")
for term_num in range(randrange(3, 11)):
group.new_term(f"Group {group_num} - Term {term_num}", f"Group {group_num} - Definition {term_num}")
groups.append(group)
restore_groups(groups)