diff --git a/src/data_types/frequency.py b/src/data_types/frequency.py index f84a177..695a6a9 100644 --- a/src/data_types/frequency.py +++ b/src/data_types/frequency.py @@ -2,7 +2,7 @@ from typing import List from datetime import timedelta import calendar -from utils import str_date, str_datetime, from_now, plural +from utils import str_date, str_datetime, from_now, plural, percent class Frequency: @@ -35,10 +35,10 @@ class Frequency: f"- **earliest message**: {str_datetime(self.dates[0])} ({from_now(self.dates[0])})", f"- **latest message**: {str_datetime(self.dates[-1])} ({from_now(self.dates[-1])})", f"- **messages/day**: {total_msg/delta.days:,.2f}", - f"- **busiest day of week**: {calendar.day_name[busiest_weekday]} (~{self.week[busiest_weekday]/n_weekdays:,.2f} msg, {round(100*self.week[busiest_weekday]/total_msg)}%)", + f"- **busiest day of week**: {calendar.day_name[busiest_weekday]} (~{self.week[busiest_weekday]/n_weekdays:,.2f} msg, {percent(self.week[busiest_weekday]/total_msg)})", f"- **busiest day ever**: {str_date(self.busiest_day)} ({from_now(self.busiest_day)}) ({self.busiest_day_count} msg)", f"- **messages/hour**: {total_msg*3600/delta.total_seconds():,.2f}", - f"- **busiest hour of day**: {busiest_hour:0>2}:00 (~{self.day[busiest_hour]/n_hours:,.2f} msg, {round(100*self.day[busiest_hour]/total_msg)}%)", + f"- **busiest hour of day**: {busiest_hour:0>2}:00 (~{self.day[busiest_hour]/n_hours:,.2f} msg, {percent(self.day[busiest_hour]/total_msg)})", f"- **busiest hour ever**: {str_datetime(self.busiest_hour)} ({from_now(self.busiest_hour)}) ({self.busiest_hour_count} msg)", f"- **longest break**: {plural(int(self.longest_break.total_seconds()/3600), 'hour')} ({plural(self.longest_break.days,'day')}) from {str_datetime(self.longest_break_start)} ({from_now(self.longest_break_start)})", ] diff --git a/src/data_types/presence.py b/src/data_types/presence.py index 52ba9e5..50ef2f4 100644 --- a/src/data_types/presence.py +++ b/src/data_types/presence.py @@ -2,7 +2,7 @@ from typing import List from collections import defaultdict -from utils import mention, channel_mention, plural +from utils import mention, channel_mention, plural, percent class Presence: @@ -23,7 +23,7 @@ class Presence: ret = [] if member_specific: ret += [ - f"- **messages**: {self.msg_count} ({100*self.msg_count/self.total_msg:.0f}% of server's)" + f"- **messages**: {self.msg_count} ({percent(self.msg_count/self.total_msg)} of server's)" ] if show_top_channel: top_channel = sorted(self.channel_usage)[-1] @@ -33,16 +33,16 @@ class Presence: key=lambda k: self.channel_usage[k] / self.channel_total[k], )[-1] ret += [ - f"- **most visited channel**: {channel_mention(top_channel)} ({self.channel_usage[top_channel]:,} msg, {100*self.channel_usage[top_channel]//channel_sum:.0f}%)", - f"- **mostly found in**: {channel_mention(found_in)} ({self.channel_usage[found_in]:,} msg, {100*self.channel_usage[found_in]//self.channel_total[found_in]:.0f}% of channel's)", + f"- **most visited channel**: {channel_mention(top_channel)} ({self.channel_usage[top_channel]:,} msg, {percent(self.channel_usage[top_channel]/channel_sum)})", + f"- **mostly found in**: {channel_mention(found_in)} ({self.channel_usage[found_in]:,} msg, {percent(self.channel_usage[found_in]/self.channel_total[found_in])} of channel's)", ] if member_specific: if len(self.mentions) > 0: top_mention = sorted(self.mentions)[-1] mention_sum = sum(self.mentions.values()) ret += [ - f"- **was mentioned**: {plural(mention_sum, 'time')} ({100*mention_sum/self.mention_count:.0f}% of server's)", - f"- **mostly mentioned by**: {mention(top_mention)} ({plural(self.mentions[top_mention], 'time')}, {100*self.mentions[top_mention]//mention_sum:.0f}%)", + f"- **was mentioned**: {plural(mention_sum, 'time')} ({percent(mention_sum/self.mention_count)} of server's)", + f"- **mostly mentioned by**: {mention(top_mention)} ({plural(self.mentions[top_mention], 'time')}, {percent(self.mentions[top_mention]/mention_sum)})", ] else: ret += ["- **was mentioned**: 0 times"] @@ -50,8 +50,8 @@ class Presence: top_mention = sorted(self.mention_others)[-1] mention_sum = sum(self.mention_others.values()) ret += [ - f"- **mentioned others**: {plural(mention_sum, 'time')} ({100*mention_sum/self.mention_count:.0f}% of server's)", - f"- **mostly mentioned**: {mention(top_mention)} ({plural(self.mention_others[top_mention], 'time')}, {100*self.mention_others[top_mention]//mention_sum:.0f}%)", + f"- **mentioned others**: {plural(mention_sum, 'time')} ({percent(mention_sum/self.mention_count)} of server's)", + f"- **mostly mentioned**: {mention(top_mention)} ({plural(self.mention_others[top_mention], 'time')}, {percent(self.mention_others[top_mention]/mention_sum)})", ] else: ret += ["- **was mentioned**: 0 times"] @@ -59,12 +59,12 @@ class Presence: # ({self.used_reaction_total/self.used_reaction_all_total}) ret += [ f"- **reactions**: {plural(self.used_reaction_total, 'time')}", - f"- **most used reaction**: {self.most_used_reaction} ({plural(self.most_used_reaction_count, 'time')}, {100*self.most_used_reaction_count/self.used_reaction_total:.0f}%)", + f"- **most used reaction**: {self.most_used_reaction} ({plural(self.most_used_reaction_count, 'time')}, {percent(self.most_used_reaction_count/self.used_reaction_total)})", ] if member_specific: ret[ -2 - ] += f" ({100*self.used_reaction_total/self.used_reaction_all_total:.0f}% of server's)" + ] += f" ({percent(self.used_reaction_total/self.used_reaction_all_total)} of server's)" else: ret += ["- **reactions**: 0 times"] return ret diff --git a/src/utils/utils.py b/src/utils/utils.py index 2ef96ec..ec1a2c5 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -96,6 +96,15 @@ def plural(count: int, word: str) -> str: return f"{count:,} {word}{'s' if count != 1 else ''}" +def percent(p: float) -> str: + if p < 0.01: + return f"{100*p:.2f}%" + elif p < 0.1: + return f"{100*p:.1f}%" + else: + return f"{100*p:.0f}%" + + # DATE FORMATTING