您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

count_participations.py 1.0KB

123456789101112131415161718192021222324252627282930
  1. from collections import Counter
  2. from typing import Dict, List
  3. from million.model.message import Message
  4. from million.model.participant import Participant
  5. def count_participations(
  6. messages: List[Message],
  7. participants: List[Participant] | None = [],
  8. threshold: int | None = 0
  9. ) -> Dict[str, int]:
  10. """
  11. Count the number of messages sent by each participant,\n
  12. you can specify a threshold to return only people having reached that many counts
  13. """
  14. participations = dict.fromkeys([p.name for p in participants], 0)
  15. participations.update(Counter([m.sender_name for m in messages]))
  16. return {k: v for k,v in sorted(participations.items(), key=lambda x: -x[1]) if v >= threshold}
  17. def podium(
  18. messages: List[Message],
  19. top: int,
  20. participants: List[Participant] | None = [],
  21. ) -> Dict[str, int]:
  22. """
  23. Returns the N biggest counters
  24. """
  25. cp = count_participations(messages, participants)
  26. return {k: cp[k] for idx, k in enumerate(cp) if idx < top}