forked from jonodrew/mentor-match
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
202 lines (177 loc) · 6.35 KB
/
helpers.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
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
import csv
import functools
import math
import operator
import os
import pathlib
import random
import string
import flask
import matching.rules.rule as rl
def grades() -> list[str]:
return [
"AA",
"AO",
"EO",
"HEO",
"SEO",
"Grade 7",
"Grade 6",
"SCS1",
"SCS2",
"SCS3",
"SCS4",
]
def valid_file(filename: str) -> bool:
return "." in filename and filename.rsplit(".", 1)[1].lower() == "csv"
def mentors_and_mentees_present(filenames: list[str]) -> bool:
"""
This function picks off the string after the last forward slash in each filename and then checks that they are
'mentors.csv' and 'mentees.csv'
:param filenames:
:return:
"""
return set(
map(lambda filename: filename.rsplit("/", 1)[-1].lower(), filenames)
) == {
"mentors.csv",
"mentees.csv",
}
def valid_files(filenames: list[str]) -> bool:
return mentors_and_mentees_present(filenames) and all(map(valid_file, filenames))
def random_string():
return "".join(random.choice(string.ascii_lowercase) for _ in range(10))
@functools.lru_cache
def known_file(path_to_file, role_type: str, quantity=50):
padding_size = int(math.log10(quantity)) + 1
pathlib.Path(path_to_file).mkdir(parents=True, exist_ok=True)
data_path = path_to_file / f"{role_type}s.csv"
with open(data_path, "w", newline="") as test_data:
data = known_data(role_type)
file_writer: csv.DictWriter[str] = csv.DictWriter(test_data, list(data.keys()))
rows = []
for i in range(quantity):
data["last name"] = str(i).zfill(padding_size)
data["email address"] = f"{role_type}.{str(i).zfill(padding_size)}@gov.uk"
rows.append(data.copy())
file_writer.writeheader()
file_writer.writerows(rows)
def known_data(role_type: str):
data = {
"first name": role_type,
"last name": "",
"email address": "",
"both mentor and mentee": "no",
"job title": "Some role",
"grade": "EO" if role_type == "mentor" else "AA",
"organisation": f"Department of {role_type.capitalize()}s",
"biography": "Test biography",
"profession": "Policy",
}
if role_type == "mentor":
data["characteristics"] = "bisexual, transgender"
elif role_type == "mentee":
data["match with similar identity"] = "yes"
data["identity to match"] = "bisexual"
else:
raise ValueError
return data
def random_data(role_type: str):
data = {
"first name": role_type,
"last name": "",
"email address": "",
"both mentor and mentee": random.choice(["yes", "no"]),
"job title": "Some role",
"grade": grades()[random.randint(2, len(grades()) - 1)]
if role_type == "mentor"
else grades()[random.randint(0, len(grades()) - 2)],
"organisation": (
"Department of"
f" {random.choice(['Fun', 'Truth', 'Joy', 'Love', 'Virtue', 'Peace'])}"
),
"profession": random.choice(["Policy", "DDaT", "Operations", "HR", "Security"]),
}
if role_type == "mentor":
characteristics = random.choice(
[
"",
", ".join(
random.sample(
[
"Asexual or aromantic",
"Gay",
"Lesbian",
"Bisexual or pansexual",
"Transgender",
"Non-binary",
],
random.randint(1, 2),
)
),
]
)
data["characteristics"] = characteristics
elif role_type == "mentee":
data["match with similar identity"] = random.choice(["yes", "no"])
data["identity to match"] = random.choice(
[
"",
"Asexual or aromantic",
"Gay",
"Lesbian",
"Bisexual or pansexual",
"Transgender",
"Non-binary",
]
)
else:
raise ValueError
return data
def rows_of_random_data(role_type: str, quantity: int = 50):
rows = []
padding_size = int(math.log10(quantity)) + 1
for i in range(quantity):
data = random_data(role_type)
data["last name"] = str(i).zfill(padding_size)
data["email address"] = f"{role_type}.{str(i).zfill(padding_size)}@gov.uk"
data["biography"] = (
f'My name is {data["first name"]} {data["last name"]}. I am a'
f' {data["grade"]}. I am in the {data["organisation"]}, in the'
f' {data["profession"]} profession. My characteristics is/are'
f' {data.get("characteristics", data.get("identity to match"))}. '
)
rows.append(data.copy())
return rows
def random_file(role_type: str, quantity: int = 50):
data_path = f"{role_type}s.csv"
with open(data_path, "w", newline="") as test_data:
rows = rows_of_random_data(role_type, quantity)
file_writer: csv.DictWriter[str] = csv.DictWriter(
test_data, list(rows[0].keys())
)
file_writer.writeheader()
file_writer.writerows(rows)
def base_rules() -> list[rl.Rule]:
return [
rl.Disqualify(
lambda match: match.mentee.organisation == match.mentor.organisation
),
rl.Disqualify(rl.Grade(target_diff=2, logical_operator=operator.gt).evaluate),
rl.Disqualify(rl.Grade(target_diff=0, logical_operator=operator.le).evaluate),
rl.Disqualify(lambda match: match.mentee in match.mentor.mentees),
rl.Grade(2, operator.eq, {True: 12, False: 0}),
rl.Grade(1, operator.eq, {True: 9, False: 0}),
rl.Generic(
{True: 10, False: 0},
lambda match: match.mentee.target_profession
== match.mentor.current_profession,
),
rl.Generic(
{True: 6, False: 0},
lambda match: match.mentee.characteristic in match.mentor.characteristics
and match.mentee.characteristic != "",
),
]
def get_data_folder_path(app_instance: flask.Flask, folder_name: str) -> pathlib.Path:
return pathlib.Path(os.path.join(app_instance.config["UPLOAD_FOLDER"], folder_name))