def day_in_history(day: datetime, count_history: CountHistory): days_in_history = { date_to_dmy(day_count.date) for day_count in count_history.daily_counts } lookedup_day = date_to_dmy(day) return lookedup_day in days_in_history
def extract_day_count(day: datetime, count_history: CountHistory) -> int: if not day_in_history(day, count_history): raise ValueError('Day {} is not in count history'.format( date_to_dmy(day))) str_day_to_count = { date_to_dmy(day_count.date): day_count.count for day_count in count_history.daily_counts } return str_day_to_count[date_to_dmy(day)]
def _compute_day_expression(day: date, publish_date: date) -> str: if day == publish_date: return 'Aujourd\'hui' if publish_date - timedelta(days=1) == day: return 'Hier' date_str = date_to_dmy(day) return f'Le {date_str}'
def extract_relevant_facts(day: datetime, count_history: CountHistory) -> list: if not day_in_history(day, count_history): raise ValueError('Day {} is not in count history'.format( date_to_dmy(day))) relevant_facts: List[RelevantFact] = [] if day_is_absolute_maximum(day, count_history): relevant_facts.append(RelevantFact.new_record(day)) else: it_is, rank = day_is_absolute_top_k(day, count_history, k=10) if it_is: relevant_facts.append(RelevantFact.top_k(day, rank)) else: if day_is_last_day_of_month(day): month_count, month_rank, previous_month_datetime = extract_month_stats( day, count_history) relevant_facts.append( RelevantFact.month_rank(day, month_count, month_rank, previous_month_datetime)) elif not day_is_first_day_of_year(day) and day_is_yearly_maximum( day, count_history): relevant_facts.append(RelevantFact.new_yearly_record(day)) elif not day_is_first_day_of_month(day) and day_is_monthly_record( day, count_history): relevant_facts.append(RelevantFact.new_monthly_record(day)) else: it_is, count_so_far, ( other_month_year, other_month_month), other_count = is_best_month_to_be( day, count_history) if it_is: previous_record_month = datetime(year=other_month_year, month=other_month_month, day=1) relevant_facts.append( RelevantFact.best_month_to_be(day, count_so_far, previous_record_month, other_count)) else: total_count = extract_total_count(count_history) relevant_facts.append( RelevantFact.total_count(day, total_count)) return relevant_facts
def extract_day_incipit(day: datetime): if day_is_today(day): return 'Aujourd\'hui' if day_is_yesterday(day): return 'Hier' return 'Le {}'.format(date_to_dmy(day))
SEBASTOPOL = 'SEBASTOPOL' def lambda_handler(event, context): tweet = get_tweet() logging.info(tweet) if event.get('test'): logging.info('Posting to slack') post_text_to_slack(tweet) return tweet logging.info('Posting tweet') post_tweet(tweet) if __name__ == '__main__': answer = pad_answer(fetch_data(ECO_COUNTER_URL)) today = datetime.now() for i in range(len(answer)): count_history = CountHistory.from_url_answer( answer[:(-i or len(answer))]) day_of_publication = today - timedelta(days=i) day_to_test = today - timedelta(days=i + 1) try: print(date_to_dmy(day_of_publication)) print(prepare_message_for_std_out(day_to_test, count_history)) except Exception: print(date_to_dmy(day_of_publication)) print(traceback.format_exc()) print() print()