From 6d2b7054e22a11ee6f1ef4c32426bb2776447d79 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sun, 5 Sep 2021 18:49:34 +0200 Subject: [PATCH] [WEI] Optimizations in survey load Signed-off-by: Yohann D'ANELLO --- apps/wei/forms/surveys/wei2021.py | 37 ++++++++++++++++--------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/apps/wei/forms/surveys/wei2021.py b/apps/wei/forms/surveys/wei2021.py index 2ff8adcc..eaaba872 100644 --- a/apps/wei/forms/surveys/wei2021.py +++ b/apps/wei/forms/surveys/wei2021.py @@ -43,28 +43,29 @@ class WEISurveyForm2021(forms.Form): registration._force_save = True registration.save() - rng = Random(information.seed) - - words = None - # Update seed - rng.randint(0, information.step) - - buses = WEISurveyAlgorithm2021.get_buses() - scores = sum((list(WEIBusInformation2021(bus).scores.values()) for bus in buses), []) - average_score = sum(scores) / len(scores) - - preferred_words = {bus: [word for word in WORDS - if WEIBusInformation2021(bus).scores[word] >= average_score] - for bus in buses} - while words is None or len(set(words)) != len(words): - # Ensure that there is no the same word 2 times - words = [rng.choice(words) for _ignored2, words in preferred_words.items()] - rng.shuffle(words) - words = [(w, w) for w in words] if self.data: self.fields["word"].choices = [(w, w) for w in WORDS] if self.is_valid(): return + + rng = Random((information.step + 1) * information.seed) + + words = None + + buses = WEISurveyAlgorithm2021.get_buses() + informations = {bus: WEIBusInformation2021(bus) for bus in buses} + scores = sum((list(informations[bus].scores.values()) for bus in buses), []) + average_score = sum(scores) / len(scores) + + preferred_words = {bus: [word for word in WORDS + if informations[bus].scores[word] >= average_score] + for bus in buses} + while words is None or len(set(words)) != len(words): + print("toto") + # Ensure that there is no the same word 2 times + words = [rng.choice(words) for _ignored2, words in preferred_words.items()] + rng.shuffle(words) + words = [(w, w) for w in words] self.fields["word"].choices = words