Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace zip_choices with strict zip #2064

Merged
merged 1 commit into from
Jan 29, 2024

Conversation

Kakadus
Copy link
Collaborator

@Kakadus Kakadus commented Nov 6, 2023

Another commit extracted for #2051. The zip_choices filter zipped choices' counts and names. This asserts, that the no answer option is always the last option.

@Kakadus Kakadus changed the title replace zip_choices with strict zip replace zip_choices with strict zip Nov 6, 2023
@Kakadus Kakadus mentioned this pull request Nov 6, 2023
Comment on lines 51 to 58
counts = OrderedDict(
(value, [0, name, color, value]) for (name, color, value) in self.choices.as_name_color_value_tuples()
)
counts.pop(NO_ANSWER)
for answer_counter in answer_counters:
counts[answer_counter.answer] = answer_counter.count
self.counts = tuple(counts.values())
counts[answer_counter.answer][0] = answer_counter.count
self.zipped_choices = tuple(counts.values())
self.counts = tuple((count for count, _, _, _ in counts.values()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels overly complicated, maybe something like the following is better?

            answer_indices = {
                answer: i for i, answer in enumerate(self.choices.values) if answer != NO_ANSWER
            }
            counts = [0] * len(answer_indices)
            for counter in answer_counters:
                counts[answer_indices[counter.answer]] = counter.count
            self.counts = tuple(counts)
            self.zipped_choices = self.choices.as_name_color_value_tuples()

We should probably refactor the whole choices business to use dataclasses instead of named tuples and increase typing with these result classes here, but we don't need to do it now

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is, that self.choices.as_name_color_value_tuples() contains a NO_ANSWER-element as the last element. This needs to be removed from the result of as_name_color_value_tuples. Also, the counts need to be prepended

Comment on lines -8 to 9
{% with colors=question_result.choices|to_colors %}
{% with colors=question_result|to_colors %}
{% for value, color in distribution|zip:colors %}
Copy link
Collaborator Author

@Kakadus Kakadus Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to your other comment: before, distribution values (w/o NO_ANSWER) may have been zipped with the colors with gray for NO_ANSWER.
Now, I explicitly remove NO_ANSWER from the colors and zip them strictly. I don't think a test is necessary, because this is already covered and only does some narrowing on the zipable tuples

@Kakadus Kakadus requested a review from niklasmohrin November 13, 2023 21:25
@fidoriel fidoriel mentioned this pull request Nov 20, 2023
evap/results/tools.py Outdated Show resolved Hide resolved
evap/results/tools.py Outdated Show resolved Hide resolved
evap/results/tools.py Outdated Show resolved Hide resolved
Copy link
Member

@richardebeling richardebeling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good so far, but will have to recheck changes to the Result classes once #2091 is merged

evap/results/tools.py Outdated Show resolved Hide resolved
evap/results/tools.py Outdated Show resolved Hide resolved
evap/results/tools.py Outdated Show resolved Hide resolved
rewrite choices logic

cleanup

revert counters logic improvement

merge Choices with CHoicesBase

zip QuestionResults colors

reformat
@Kakadus Kakadus force-pushed the zip-choices-strict branch from c2cc3bd to ad9bb22 Compare January 22, 2024 19:55
@richardebeling richardebeling self-requested a review January 22, 2024 21:13
@niklasmohrin niklasmohrin changed the title replace zip_choices with strict zip Replace zip_choices with strict zip Jan 29, 2024
@niklasmohrin niklasmohrin merged commit aae1356 into e-valuation:main Jan 29, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants