Split economist code into own file

This commit is contained in:
Edward Betts 2023-11-05 12:48:24 +00:00
parent aeeeea2041
commit f7022c992d
2 changed files with 34 additions and 31 deletions

View file

@ -23,7 +23,7 @@ from dateutil.relativedelta import FR, relativedelta
from agenda import thespacedevs
from . import birthday, calendar, fx, gwr, markets, sun, waste_schedule
from . import birthday, calendar, economist, fx, gwr, markets, sun, waste_schedule
from .types import Event
warnings.simplefilter(action="ignore", category=FutureWarning)
@ -129,35 +129,6 @@ def get_next_bank_holiday(input_date: date) -> list[Event]:
return hols
def next_economist(input_date: date) -> date:
"""Next date that the Economist is published."""
# Define the publication day (Thursday) and the day of the week of the input date
publication_day = 3 # Thursday (0 - Monday, 1 - Tuesday, ..., 6 - Sunday)
current_day_of_week = input_date.weekday()
current_week_number = input_date.isocalendar().week
# Define the list of weeks when The Economist is not published
non_publication_weeks = [26, 56]
# Check if the input date is a publication day (Thursday)
if (
current_day_of_week == publication_day
and current_week_number not in non_publication_weeks
):
return input_date
# Calculate the date for the next Thursday after the input date
days_until_next_thursday = (publication_day - current_day_of_week + 7) % 7
next_thursday_date = input_date + timedelta(days=days_until_next_thursday)
# Check if the next Thursday falls in a non-publication week
while next_thursday_date.isocalendar().week in non_publication_weeks:
# If it does, add 7 days to find the next Thursday
next_thursday_date += timedelta(days=7)
return next_thursday_date
def uk_financial_year_end(input_date: date) -> date:
"""Next date of the end of the UK financial year, April 5th."""
# Determine the year of the input date
@ -406,7 +377,7 @@ def get_data(now: datetime) -> typing.Mapping[str, str | object]:
reply = {
"now": now,
"gbpusd": fx.get_gbpusd(config),
"next_economist": next_economist(today),
"next_economist": economist.next_pub_date(today),
"bank_holiday": get_next_bank_holiday(today),
"us_holiday": get_us_holidays(today),
"next_us_presidential_election": next_us_presidential_election,

32
agenda/economist.py Normal file
View file

@ -0,0 +1,32 @@
"""Next publication date of the Economist."""
from datetime import date, timedelta
def next_pub_date(input_date: date) -> date:
"""Next date that the Economist is published."""
# Define the publication day (Thursday) and the day of the week of the input date
publication_day = 3 # Thursday (0 - Monday, 1 - Tuesday, ..., 6 - Sunday)
current_day_of_week = input_date.weekday()
current_week_number = input_date.isocalendar().week
# Define the list of weeks when The Economist is not published
non_publication_weeks = [26, 56]
# Check if the input date is a publication day (Thursday)
if (
current_day_of_week == publication_day
and current_week_number not in non_publication_weeks
):
return input_date
# Calculate the date for the next Thursday after the input date
days_until_next_thursday = (publication_day - current_day_of_week + 7) % 7
next_thursday_date = input_date + timedelta(days=days_until_next_thursday)
# Check if the next Thursday falls in a non-publication week
while next_thursday_date.isocalendar().week in non_publication_weeks:
# If it does, add 7 days to find the next Thursday
next_thursday_date += timedelta(days=7)
return next_thursday_date