[WEI] Display status bar with tqdm

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
Yohann D'ANELLO 2021-09-16 20:46:34 +02:00
parent e89383e3f4
commit 1ef25924a0
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 39 additions and 7 deletions

View File

@ -171,7 +171,7 @@ class WEISurveyAlgorithm2021(WEISurveyAlgorithm):
def get_bus_information_class(cls): def get_bus_information_class(cls):
return WEIBusInformation2021 return WEIBusInformation2021
def run_algorithm(self): def run_algorithm(self, display_tqdm=False):
""" """
Gale-Shapley algorithm implementation. Gale-Shapley algorithm implementation.
We modify it to allow buses to have multiple "weddings". We modify it to allow buses to have multiple "weddings".
@ -196,11 +196,26 @@ class WEISurveyAlgorithm2021(WEISurveyAlgorithm):
free_seats = bus.size - WEIMembership.objects.filter(bus=bus, registration__first_year=False).count() free_seats = bus.size - WEIMembership.objects.filter(bus=bus, registration__first_year=False).count()
quotas[bus] = 4 + int(non_men_total / registrations.count() * free_seats) quotas[bus] = 4 + int(non_men_total / registrations.count() * free_seats)
# Repartition for non men people first tqdm_obj = None
self.make_repartition(non_men, quotas) if display_tqdm:
self.make_repartition(men) from tqdm import tqdm
tqdm_obj = tqdm(total=len(non_men), desc="Non-hommes")
def make_repartition(self, surveys, quotas=None): # Repartition for non men people first
self.make_repartition(non_men, quotas, tqdm_obj=tqdm_obj)
if display_tqdm:
tqdm_obj.close()
from tqdm import tqdm
tqdm_obj = tqdm(total=len(men), desc="Hommes")
self.make_repartition(men, tqdm_obj=tqdm_obj)
if display_tqdm:
tqdm_obj.close()
def make_repartition(self, surveys, quotas=None, tqdm_obj=None):
free_surveys = surveys.copy() # Remaining surveys free_surveys = surveys.copy() # Remaining surveys
while free_surveys: # Some students are not affected while free_surveys: # Some students are not affected
survey = free_surveys[0] survey = free_surveys[0]
@ -235,6 +250,11 @@ class WEISurveyAlgorithm2021(WEISurveyAlgorithm):
free_surveys.append(least_preferred_survey) free_surveys.append(least_preferred_survey)
survey.select_bus(bus) survey.select_bus(bus)
survey.save() survey.save()
free_surveys.remove(survey)
break break
else: else:
raise ValueError(f"User {survey.registration.user} has no free seat") raise ValueError(f"User {survey.registration.user} has no free seat")
if tqdm_obj is not None:
tqdm_obj.n = len(surveys) - len(free_surveys)
tqdm_obj.refresh()

View File

@ -24,7 +24,14 @@ class Command(BaseCommand):
sid = transaction.savepoint() sid = transaction.savepoint()
algorithm = CurrentSurvey.get_algorithm_class()() algorithm = CurrentSurvey.get_algorithm_class()()
algorithm.run_algorithm()
try:
from tqdm import tqdm
display_tqdm = True
except ImportError:
display_tqdm = False
algorithm.run_algorithm(display_tqdm=display_tqdm)
output = options['output'] output = options['output']
registrations = algorithm.get_registrations() registrations = algorithm.get_registrations()
@ -34,8 +41,13 @@ class Command(BaseCommand):
for bus, members in per_bus.items(): for bus, members in per_bus.items():
output.write(bus.name + "\n") output.write(bus.name + "\n")
output.write("=" * len(bus.name) + "\n") output.write("=" * len(bus.name) + "\n")
order = -1
for r in members: for r in members:
output.write(r.user.username + "\n") survey = CurrentSurvey(r)
for order, (b, _score) in enumerate(survey.ordered_buses()):
if b == bus:
break
output.write(f"{r.user.username} ({order + 1})\n")
output.write("\n") output.write("\n")
if not options['doit']: if not options['doit']: