-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.py
141 lines (112 loc) · 4.07 KB
/
seed.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from connect_db import session
from models import Student, Tutor, Group, Subject, Mark
import faker
from datetime import datetime, timedelta
from random import randint
faker = faker.Faker()
NUM_STUDENTS = 50
NUM_GROUPS = 3
NUM_SUBJECTS = randint(5, 8)
NUM_TUTORS = randint(3, 5)
NUM_MARKS = randint(10, 19)
FAKE_DATA_RANGES = NUM_STUDENTS, NUM_GROUPS, NUM_SUBJECTS, NUM_TUTORS, NUM_MARKS
groups = ["Группа А", "Группа Б", "Группа В", "Группа Г", "Группа Д"]
subjects = [
"Математика",
"Физика",
"Химия Гачи-Мучи",
"Истории у костра",
"Иностранный язык",
"Искусство",
"Музыка",
"Оленеводство",
"Грибоварение",
]
def gen_fake_data(data_ranges: tuple) -> tuple:
def generate_subject_name() -> str:
while subjects:
subject = faker.random_element(elements=subjects)
subjects.remove(subject)
yield subject
def generate_group_name() -> str:
group = faker.random_element(elements=groups)
groups.remove(group)
return group
fake_students = []
fake_groups = []
fake_subjects = []
fake_tutors = []
fake_marks = []
for _ in range(data_ranges[0]):
fake_students.append(faker.name())
for _ in range(data_ranges[1]):
fake_groups.append(generate_group_name())
subects_gen = generate_subject_name()
for _ in range(data_ranges[2]):
fake_subjects.append(next(subects_gen, None))
for _ in range(data_ranges[3]):
fake_tutors.append(faker.name())
for _ in range(data_ranges[4]):
fake_marks.append(randint(0, 100))
return fake_students, fake_groups, fake_subjects, fake_tutors, fake_marks
def prepare_data(data: tuple) -> tuple:
def generate_random_date() -> datetime:
start_date = datetime(2000, 1, 1)
end_date = datetime(2023, 12, 31)
delta = end_date - start_date
random_days = randint(0, delta.days)
random_date = start_date + timedelta(days=random_days)
# random_date = random_date.strftime("%Y-%m-%d")
return random_date
students_list = []
for item in data[0]:
students_list.append((item, randint(1, NUM_GROUPS)))
groups_list = []
for item in data[1]:
groups_list.append((item,))
subjects_list = []
for item in data[2]:
subjects_list.append((item, randint(1, NUM_TUTORS)))
tutors_list = []
for item in data[3]:
tutors_list.append((item,))
marks_list = []
for item in data[4]:
marks_list.append(
(
item,
randint(1, NUM_STUDENTS),
randint(1, NUM_SUBJECTS),
generate_random_date(),
)
)
return students_list, groups_list, subjects_list, tutors_list, marks_list
def insert_data(data: tuple) -> None:
with session:
try:
for item in data[1]:
group = Group(name_uq=item[0])
session.add(group)
session.commit()
for item in data[0]:
student = Student(name_uq=item[0], group_id=item[1])
session.add(student)
session.commit()
for item in data[3]:
tutor = Tutor(name_uq=item[0])
session.add(tutor)
session.commit()
for item in data[2]:
subject = Subject(name_uq=item[0], tutor_id_fk=item[1])
session.add(subject)
session.commit()
for item in data[4]:
mark = Mark(mark_value=item[0], student_id_fk=item[1], subject_id_fk=item[2], today_date=item[3])
session.add(mark)
session.commit()
except Exception as error:
print(error)
if __name__ == "__main__":
generated_fake_data = gen_fake_data(FAKE_DATA_RANGES)
prepared_fake_data = prepare_data(generated_fake_data)
insert_data(prepared_fake_data)