-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold.py
325 lines (254 loc) · 9.8 KB
/
old.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import json
from dataclasses import dataclass
from datetime import datetime
from random import shuffle, choice
from typing import List
import pandas as pd
# ===================== GODFATHER SECTION ==============================================================
@dataclass
class Godfather:
_mail: str
_name: str
_sex: str
_number: str
_level: str
_speciality: str
@property
def mail(self) -> str:
return self._mail
@property
def name(self) -> str:
return self._name
@property
def sex(self) -> str:
return self._sex
@property
def number(self) -> str:
return self._number
@property
def level(self) -> str:
return self._level
@property
def speciality(self) -> str:
return self._speciality
def object_description(self):
print("Godfather description:")
print(f"Mail: {self.mail}")
print(f"Name: {self.name}")
print(f"Sex: {self.sex}")
print(f"Number: {self.number}")
print(f"Level: {self.level}")
print(f"Speciality: {self.speciality}\n")
@classmethod
def factory(cls, mail, name, sex, number, level, speciality) -> "Godfather":
name = name.upper()
return cls(mail, name, sex, number, level, speciality)
df_godfather = pd.read_csv("Parrains.csv", sep=",")
GODFATHERS = []
for index, row in df_godfather.iterrows():
godfather = Godfather.factory(
mail=row["Adresse e-mail"],
name=row["Nom(s) et Prénom(s) "],
sex=row["Sexe"],
number=row["Numéro de téléphone "],
level=row["Niveau académique "],
speciality=row["Spécialité "],
)
# print(index)
# godfather.object_description()
GODFATHERS.append(godfather)
# ======================================================================================================
# ===================== GODSON SECTION =================================================================
@dataclass
class Godson:
_mail: str
_name: str
_sex: str
_number: str
_level: str
_specialities: list
@property
def mail(self) -> str:
return self._mail
@property
def name(self) -> str:
return self._name
@property
def sex(self) -> str:
return self._sex
@property
def number(self) -> str:
return self._number
@property
def level(self) -> str:
return self._level
@property
def specialities(self) -> list:
return self._specialities
def object_description(self):
print("Godson description:")
print(f"Mail: {self._mail}")
print(f"Name: {self._name}")
print(f"Sex: {self._sex}")
print(f"Number: {self._number}")
print(f"Level: {self._level}")
print(f"Specialities: {self._specialities}")
@classmethod
def factory(cls, mail, name, sex, number, level, speciality) -> "Godson":
name = name.upper()
return cls(mail, name, sex, number, level, speciality)
df_godson = pd.read_csv("Filleuls.csv", sep=",")
GODSONS = []
for index, row in df_godson.iterrows():
# print(index)
# Check specialities' compliance
# Convert each selected speciality to list of string
specialities = row["Future Spécialité "].split(",")
# print("PHASE 1")
# print(specialities)
# Remove all spaces
specialities = [speciality.strip() for speciality in specialities]
# print("PHASE 2")
# print(specialities)
# Remove all empty strings
specialities = [speciality for speciality in specialities if speciality != ""]
# print("PHASE 3")
# print(specialities)
# Take only valid specialities
specialities = [speciality for speciality in specialities if
speciality in ("Génie Logiciel", "Sécurité Informatique", "Réseau", "Data Science")]
# print("PHASE 4")
# print(specialities)
godson = Godson.factory(
mail=row["Adresse e-mail"],
name=row["Nom(s) et Prénom(s) "],
sex=row["Sexe"],
number=row["Numéro de téléphone "],
level=row["Niveau académique "],
speciality=specialities,
)
# godson.object_description()
GODSONS.append(godson)
# ======================================================================================================
# ===================== SPONSORSHIP SECTION ============================================================
print(f"Number of godfathers: {len(GODFATHERS)}")
print(f"Number of godsons: {len(GODSONS)}")
print(f"Godson by godfather: {len(GODSONS) // len(GODFATHERS)}\n")
NUMBER_OF_GODSONS_BY_GODFATHER = len(GODSONS) // len(GODFATHERS)
@dataclass
class Sponsorship:
_godfather: Godfather
_godsons: List[Godson]
@property
def godfather(self) -> Godfather:
return self._godfather
@property
def godsons(self) -> List[Godson]:
return self._godsons
def object_description(self):
print("Sponsorship description:")
print(f"Godfather: {self.godfather.name}")
print(f"Godsons: {[son.name for son in self.godsons]}")
print(f"Length of godsons: {len(self.godsons)}\n")
def toJSON(self) -> dict:
return {
"godfather": self.godfather.name,
"godsons": [son.name for son in self.godsons]
}
@classmethod
def factory(cls, mfather, mgodsons) -> "Sponsorship":
return cls(mfather, mgodsons)
SPONSORSHIPS = []
for father in GODFATHERS:
godsons = []
spec = father.speciality # Retrieve the father speciality
# Retrieve sons who have selected the father speciality
sons_speciality = [son for son in GODSONS if spec in son.specialities]
if sons_speciality:
# Retrieve sons who take only the father speciality
sons_spec_unique = [son for son in sons_speciality if len(son.specialities) == 1]
if sons_spec_unique:
# Shuffle the list
shuffle(sons_spec_unique)
while len(godsons) < NUMBER_OF_GODSONS_BY_GODFATHER and len(sons_spec_unique) > 0:
godsons.append(sons_spec_unique.pop())
sons_speciality.remove(godsons[-1])
GODSONS.remove(godsons[-1])
else:
# Shuffle the list
shuffle(sons_speciality)
while len(godsons) < NUMBER_OF_GODSONS_BY_GODFATHER and len(sons_speciality) > 0:
godsons.append(sons_speciality.pop())
GODSONS.remove(godsons[-1])
else:
# Retrieve sons who have no speciality selected
sons_no_spec = [son for son in GODSONS if len(son.specialities) == 0]
if sons_no_spec:
# Shuffle the list
shuffle(sons_no_spec)
while len(godsons) < NUMBER_OF_GODSONS_BY_GODFATHER and len(sons_no_spec) > 0:
godsons.append(sons_no_spec.pop())
GODSONS.remove(godsons[-1])
# Do this when all last conditions are not satisfied
else:
# Shuffle the list
shuffle(GODSONS)
while len(godsons) < NUMBER_OF_GODSONS_BY_GODFATHER and len(GODSONS) > 0:
godsons.append(GODSONS.pop())
# Create the sponsorship
sponsorship = Sponsorship.factory(father, godsons)
# sponsorship.object_description()
SPONSORSHIPS.append(sponsorship)
# Operation to check if all godsons have been assigned to a godfather and complete godfather who have less than 5
# godsons
if len(GODSONS) > 0:
# Retrieve the godfathers who have less than 5 godsons
fathers = [father for father in SPONSORSHIPS if len(father.godsons) < 5]
for father in fathers:
while len(father.godsons) < NUMBER_OF_GODSONS_BY_GODFATHER and len(GODSONS) > 0:
father.godsons.append(GODSONS.pop())
# father.object_description()
# Operation to do when we have godsons again after the previous operation
if len(GODSONS) > 0:
shuffle(SPONSORSHIPS)
while len(GODSONS) > 0:
sponsorship = choice(SPONSORSHIPS)
sponsorship.godsons.append(GODSONS.pop())
# sponsorship.object_description()
# Count assigned godsons
count = 0
for sponsorship in SPONSORSHIPS:
sponsorship.object_description()
count += len(sponsorship.godsons)
print(f"Number of godsons assigned: {count}\n")
# ======================================================================================================
# ===================== JSON GENERATION SECTION ========================================================
# Function who will be used to sort the list of sponsorship by godfather name
def extract_name_json(json_file):
try:
return json_file["godfather"]
except KeyError:
return "No name"
SPONSORSHIPS_JSON = [sponsorship.toJSON() for sponsorship in SPONSORSHIPS]
# Sort the list of sponsorship by godfather name
SPONSORSHIPS_JSON.sort(key=extract_name_json)
print(json.dumps(SPONSORSHIPS_JSON, indent=4, ensure_ascii=False))
# Generate the json file
with open(f"sponsorships_{datetime.now().timestamp()}.json", "w") as file:
json.dump(SPONSORSHIPS_JSON, file, indent=4, ensure_ascii=False)
# ==========================================================================================================
# ===================== TXT FILE GENERATION SECTION ========================================================
def extract_name(obj):
try:
return obj.godfather.name
except:
return "No name"
SPONSORSHIPS.sort(key=extract_name)
with open(f"sponsorships_{datetime.now().timestamp()}.txt", "w") as file:
for sponsoring in SPONSORSHIPS:
file.write(f"SPONSOR : {sponsoring.godfather.name} ({sponsoring.godfather.number})\n")
file.write("MENTEES :\n")
for godson in sponsoring.godsons:
file.write(f"{godson.name} ({godson.number})\n")
file.write("\n\n")
# ==========================================================================================================