new val_sum for refactoring

This commit is contained in:
klemek
2021-01-14 17:22:59 +01:00
parent c938369eba
commit e1c619dabb
3 changed files with 12 additions and 8 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
from typing import List
from collections import defaultdict
from utils import percent, top_key, plural, precise
from utils import percent, top_key, plural, precise, val_sum
class Composition:
@@ -47,7 +47,7 @@ class Composition:
ret += [
f"- **answers**: {self.answers:,} ({percent(self.answers/msg_count)})"
]
total_emotes = sum(self.emotes.values())
total_emotes = val_sum(self.emotes)
if total_emotes > 0:
top_emote = top_key(self.emotes)
ret += [
+5 -5
View File
@@ -2,7 +2,7 @@ from typing import List, Optional
from collections import defaultdict
from utils import mention, channel_mention, plural, percent, top_key
from utils import mention, channel_mention, plural, percent, top_key, val_sum
class Presence:
@@ -37,7 +37,7 @@ class Presence:
]
if show_top_channel:
top_channel = top_key(self.channel_usage)
channel_sum = sum(self.channel_usage.values())
channel_sum = val_sum(self.channel_usage)
found_in = sorted(
self.channel_usage,
key=lambda k: self.channel_usage[k] / self.channel_total[k],
@@ -52,14 +52,14 @@ class Presence:
if member_specific:
if len(self.mentions) > 0:
top_mention = top_key(self.mentions)
mention_sum = sum(self.mentions.values())
mention_sum = val_sum(self.mentions)
ret += [
f"- **was mentioned**: {plural(mention_sum, 'time')} ({percent(mention_sum/self.mention_count)} of {type})",
f"- **mostly mentioned by**: {mention(top_mention)} ({plural(self.mentions[top_mention], 'time')}, {percent(self.mentions[top_mention]/mention_sum)})",
]
if len(self.mention_others) > 0:
top_mention = top_key(self.mention_others)
mention_sum = sum(self.mention_others.values())
mention_sum = val_sum(self.mention_others)
if member_specific:
ret += [
f"- **mentioned others**: {plural(mention_sum, 'time')} ({percent(mention_sum/self.mention_count)} of {type})",
@@ -72,7 +72,7 @@ class Presence:
]
if len(self.reactions) > 0:
total_used = sum(self.reactions.values())
total_used = val_sum(self.reactions)
top_reaction = top_key(self.reactions)
ret += [
f"- **reactions**: {plural(total_used, 'time')}",
+5 -1
View File
@@ -1,4 +1,4 @@
from typing import List, Dict, Union, Optional
from typing import List, Dict, Union, Optional, Any
import os
import logging
import discord
@@ -91,6 +91,10 @@ def top_key(d: Dict[Union[str, int], int]) -> Union[str, int]:
return sorted(d, key=lambda k: d[k])[-1]
def val_sum(d: Dict[Any, int]) -> int:
return sum(d.value())
# MESSAGE FORMATTING