-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_fake_data.py
More file actions
219 lines (183 loc) · 6.47 KB
/
Copy pathgenerate_fake_data.py
File metadata and controls
219 lines (183 loc) · 6.47 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import json
import random
import os
import re
from faker import Faker
################################################################################
# Some settings:
################################################################################
ADMIN_COUNT = 2
STUDENT_COUNT = 40
LECTURER_COUNT = 10
EXAM_REG_COUNT = 6
COURSE_COUNT = 10
ROLES = ["Student", "Admin", "Lecturer"]
FIELDS_OF_STUDY = [
"Computer Science",
"Chemistry",
"Biology",
"Physics",
"Religion",
"Sociology",
]
MODULE_PREFICES = [
"Topics of",
"Introduction to",
"Applied",
"Theorotical",
"Experimental",
]
COURSE_TYPES = ["Lecture", "Project Group", "Seminar"]
COUNTRIES = ["Germany", "United States", "Italy", "France", "United Kingdom", "Belgium", "Netherlands", "Spain", "Austria", "Switzerland", "Poland"]
fake = Faker("en-US")
fake.random.seed(654321)
################################################################################
basepath = os.path.join("defaults", "generated")
lecturer_ids = []
modules_by_field_of_study = {
field: [] for field in FIELDS_OF_STUDY
} # Dict with modules mapped to their field of study (to let generated data appear less random)
def generate_user(role: str):
assert role in ROLES
strip_username = lambda username: re.sub("^[a-zA-Z-.]", "", username)
profile = fake.simple_profile()
while (
len(profile["name"].split(" ")) != 2
and len(strip_username(profile["username"])) not in range(5,17)
): # Some names were like Mr. John Smith...
profile = fake.simple_profile()
username = strip_username(profile["username"])
return {
"governmentId": username + fake.pystr(),
"authUser": {
"username": username,
"password": username, # more convenient than fake.password(),
"role": role,
},
"user": {
"username": username,
"enrollmentIdSecret": "",
"isActive": True,
"role": role,
"address": {
"street": fake.street_name(),
"houseNumber": fake.building_number().lstrip("0"),
"zipCode": fake.postcode(),
"city": fake.city(),
"country": random.choice(COUNTRIES)
},
"firstName": profile["name"].split(" ")[0],
"lastName": profile["name"].split(" ")[1],
"email": profile["mail"],
"birthDate": profile["birthdate"].strftime("%Y-%m-%d"),
"phoneNumber": "+{:012d}".format(fake.pyint(0, int("9"*12))),
},
}
def generate_student():
student = generate_user("Student")
student["user"]["latestImmatriculation"] = ""
student["user"]["matriculationId"] = str(fake.pyint(1000000, 9999999))
return student
def generate_lecturer(all_lecturer_ids: list):
lecturer = generate_user("Lecturer")
lecturer["user"]["freeText"] = fake.paragraph()
lecturer["user"]["researchArea"] = fake.job()
all_lecturer_ids.append(lecturer["user"]["username"])
return lecturer
def generate_admin():
return generate_user("Admin")
def generate_exam_reg(all_modules: list):
field_of_study = random.choice(FIELDS_OF_STUDY)
my_modules = []
count = random.randint(2, 5) # Random number of modules for this exam reg
for _ in range(count):
# Choose existing or generate new module for this exam reg
if random.random() < 0.8 or not my_modules:
new_module = {
"id": "M."
+ str(fake.pyint(0, 9999)).zfill(4)
+ "."
+ str(fake.pyint(0, 99999)).zfill(5),
"name": random.choice(MODULE_PREFICES) + " " + field_of_study,
}
all_modules[field_of_study].append(new_module)
my_modules.append(new_module)
elif (
field_of_study in modules_by_field_of_study
and modules_by_field_of_study[field_of_study]
):
module_cand = random.choice(modules_by_field_of_study[field_of_study])
if module_cand and module_cand not in my_modules:
my_modules.append(module_cand)
return {
"name": random.choice(["Bachelor", "Master"])
+ " "
+ field_of_study
+ " v"
+ str(fake.pyint(1, 8)),
"active": True,
"modules": my_modules,
}
def generate_course():
lecturer = random.choice(lecturer_ids)
flatten = lambda list_to_flatten: [
item for sub_list in list_to_flatten for item in sub_list
]
all_module_ids = set(
map(
lambda module: module.get("id"), flatten(modules_by_field_of_study.values())
)
)
module_ids = random.sample(all_module_ids, random.randint(1, 4))
return {
"courseId": "",
"moduleIds": module_ids,
"courseName": fake.catch_phrase(),
"courseType": random.choice(COURSE_TYPES),
"startDate": "2020-12-08",
"endDate": "2020-12-08",
"ects": random.randint(3, 10),
"lecturerId": lecturer,
"maxParticipants": 10 * random.randint(1, 20),
"currentParticipants": 0,
"courseLanguage": random.choice(["German", "English"]),
"courseDescription": fake.paragraph(2),
}
def write_to_file(data, _dir, filename):
directory = os.path.join(os.path.dirname(__file__), basepath, _dir)
if not os.path.exists(directory):
os.makedirs(directory)
with open(os.path.join(directory, filename), "w+") as f:
f.write(data)
def json_dump_dict(data: dict):
return json.dumps(data, indent=4)
for i in range(ADMIN_COUNT):
write_to_file(
json_dump_dict(generate_student()), "admins", str(i).zfill(2) + ".json"
)
for i in range(STUDENT_COUNT):
write_to_file(
json_dump_dict(generate_student()), "students", str(i).zfill(2) + ".json"
)
for i in range(LECTURER_COUNT):
write_to_file(
json_dump_dict(generate_lecturer(lecturer_ids)),
"lecturers",
str(i).zfill(2) + ".json",
)
for i in range(EXAM_REG_COUNT):
write_to_file(
json_dump_dict(generate_exam_reg(modules_by_field_of_study)),
"examRegs",
str(i).zfill(2) + ".json",
)
for i in range(COURSE_COUNT):
write_to_file(
json_dump_dict(generate_course()), "courses", str(i).zfill(2) + ".json"
)
print("Done! 😎")
print(
"Generated: {} Admins, {} Students, {} Lecturers, {} Exam Regs and {} Courses".format(
ADMIN_COUNT, STUDENT_COUNT, LECTURER_COUNT, EXAM_REG_COUNT, COURSE_COUNT
)
)