improve /best formatting, account for unknown responses

master
Tomáš Mládek 2019-10-23 11:16:13 +02:00
parent f1ebd8d4c6
commit 7762ffd80a
1 changed files with 23 additions and 15 deletions

View File

@ -4,7 +4,6 @@ import re
from dataclasses import dataclass
from datetime import datetime, timedelta
from enum import Enum
from operator import itemgetter
from typing import List, Dict
import telegram
@ -153,33 +152,42 @@ class DudleBot:
@staticmethod
def _best_of_plan(plan):
days = [plan.start + timedelta(days=i) for i in range(plan.duration.days)]
results = [0] * plan.duration.days
results = [(0, 0)] * plan.duration.days
for entry in plan.entries.values():
for idx, response in enumerate(entry.responses):
if response == PlanResponse.YES:
results[idx] += 1
# elif response == PlanResponse.UNKNOWN:
# results[idx] += 0.01
results[idx] = (results[idx][0] + 1, results[idx][1])
elif response == PlanResponse.UNKNOWN:
results[idx] = (results[idx][0], results[idx][1] + 1)
sorted_days = list(zip(days, results))
sorted_days.sort(key=lambda dwr: dwr[1][1], reverse=True) # First sort by unknowns
sorted_days.sort(key=lambda dwr: dwr[1][0], reverse=True) # Then sort by positive answers
sorted_days = sorted(zip(days, results), key=itemgetter(1), reverse=True)
best_days = []
last_result, different_results = None, 0
for day, result in sorted_days:
if result == 0 or (len(best_days) >= 3 and different_results > 1):
for idx, day_with_result in enumerate(sorted_days):
day, result = day_with_result
yes, unknown = result
if yes == 0 or (idx > 3 and different_results > 1):
break
best_days.append((day, result))
best_days.append(day_with_result)
if result != last_result:
different_results += 1
last_result = result
response = f"Best {len(best_days)} result{'s' if len(best_days) > 1 else ''}:\n\n"
for idx, dayresult in enumerate(best_days):
day, result = dayresult
response += f"{idx + 1}. {day.strftime('%A')} ({result})\n"
return response
if len(best_days) > 0:
response = f"Best {len(best_days)} result{'s' if len(best_days) > 1 else ''}:\n\n"
for idx, day_with_result in enumerate(best_days):
day, result = day_with_result
yes, unknown = result
result_fmt = str(yes) + (f" + {unknown}?" if unknown > 0 else "")
response += f"{idx + 1}. {day.strftime('%A %-d/%-m')} ({result_fmt})\n"
return response
else:
return "There has been no resolution."
def start(self):
self.logger.info("Starting DudleBot...")