From 18d8fa6b7cabbe475af509b5d3379a225565bd23 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Thu, 16 May 2024 15:08:03 +0100 Subject: [PATCH] Split out rio_carnival_events function --- agenda/carnival.py | 33 +++++++++++++++++++++++++++++++++ agenda/data.py | 30 ++---------------------------- 2 files changed, 35 insertions(+), 28 deletions(-) create mode 100644 agenda/carnival.py diff --git a/agenda/carnival.py b/agenda/carnival.py new file mode 100644 index 0000000..41a995b --- /dev/null +++ b/agenda/carnival.py @@ -0,0 +1,33 @@ +"""Calcuate the date for carnival.""" + +from datetime import date, timedelta + +from dateutil.easter import easter + +from .types import Event + + +def rio_carnival_events(start_date: date, end_date: date) -> list[Event]: + """List of events for Rio Carnival for each year between start_date and end_date.""" + events = [] + for year in range(start_date.year, end_date.year + 1): + easter_date = easter(year) + carnival_start = easter_date - timedelta(days=51) + carnival_end = easter_date - timedelta(days=46) + + # Only include the carnival if it falls within the specified date range + if ( + start_date <= carnival_start <= end_date + or start_date <= carnival_end <= end_date + ): + events.append( + Event( + name="carnival", + title="Rio Carnival", + date=carnival_start, + end_date=carnival_end, + url="https://en.wikipedia.org/wiki/Rio_Carnival", + ) + ) + + return events diff --git a/agenda/data.py b/agenda/data.py index 21a3d16..9699dac 100644 --- a/agenda/data.py +++ b/agenda/data.py @@ -14,12 +14,12 @@ import isodate # type: ignore import lxml import pytz import yaml -from dateutil.easter import easter from . import ( accommodation, birthday, calendar, + carnival, conference, domains, economist, @@ -66,32 +66,6 @@ def midnight(d: date) -> datetime: return datetime.combine(d, datetime.min.time()) -def rio_carnival_events(start_date: date, end_date: date) -> list[Event]: - """List of events for Rio Carnival for each year between start_date and end_date.""" - events = [] - for year in range(start_date.year, end_date.year + 1): - easter_date = easter(year) - carnival_start = easter_date - timedelta(days=51) - carnival_end = easter_date - timedelta(days=46) - - # Only include the carnival if it falls within the specified date range - if ( - start_date <= carnival_start <= end_date - or start_date <= carnival_end <= end_date - ): - events.append( - Event( - name="carnival", - title="Rio Carnival", - date=carnival_start, - end_date=carnival_end, - url="https://en.wikipedia.org/wiki/Rio_Carnival", - ) - ) - - return events - - def dates_from_rrule( rrule: str, start: date, end: date ) -> typing.Sequence[datetime | date]: @@ -458,7 +432,7 @@ async def get_data( events += economist.publication_dates(last_week, next_year) events += meetup.get_events(my_data) events += hn.whoishiring(last_year, next_year) - events += rio_carnival_events(last_year, next_year) + events += carnival.rio_carnival_events(last_year, next_year) # hide markets that happen while away optional = [