import json
import logging
import time
+from collections import Counter, defaultdict
from dataclasses import dataclass
from datetime import datetime
def status_report(self):
# Calculate winner and format result message
- winner = "It's a tie!"
if self.votes:
- max_votes = max(self.votes.values())
- winners = [option for option, votes in self.votes.items() if votes == max_votes]
+ option_counts = {}
+ for voter, option in self.votes.items():
+ if option not in option_counts:
+ option_counts[option] = 0
+ option_counts[option] += 1
+
+ winners = {}
+ max_count = max(option_counts.values())
+ for option, count in option_counts.items():
+ if count == max_count:
+ winners[option] = [voter for voter, chosen_option in self.votes.items() if
+ chosen_option == option]
else:
- winners = []
+ winners = {}
- if len(winners) == 1:
- winner = f"**Winner:** {winners[0]}"
- elif len(winners) > 1:
- winner = f"**Winners:** {', '.join(winners)}"
+ if winners:
+ w = "Winner" if len(winners) == 1 else "Winners"
+ winner = f"**{w}:** " + ", ".join([f"{o} ({', '.join(v)})" for (o, v) in winners.items()])
+ else:
+ winner = "No winner..."
status_message = f"**Poll status:**\n"
status_message += self.option_listing(with_votes=True)