From ebf1eea5b7cfabf9c3a1ab40fbca21de118a0d4b Mon Sep 17 00:00:00 2001 From: David Park Date: Tue, 12 Nov 2024 22:35:56 +0000 Subject: [PATCH 1/4] feature: Add is_weekend(), is_working_day() and get_next_working_day() --- uk_bin_collection/uk_bin_collection/collect_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uk_bin_collection/uk_bin_collection/collect_data.py b/uk_bin_collection/uk_bin_collection/collect_data.py index 0a9e510e6d..1190a6eb3c 100755 --- a/uk_bin_collection/uk_bin_collection/collect_data.py +++ b/uk_bin_collection/uk_bin_collection/collect_data.py @@ -2,7 +2,7 @@ import importlib import os import sys -import logging +import logging, logging.config from uk_bin_collection.uk_bin_collection.get_bin_data import ( setup_logging, LOGGING_CONFIG, From d89a4efbc0d3988520dd4e4c7983a3c93feae21b Mon Sep 17 00:00:00 2001 From: David Park Date: Tue, 12 Nov 2024 22:37:33 +0000 Subject: [PATCH 2/4] Revert "feature: Add is_weekend(), is_working_day() and get_next_working_day()" This reverts commit ebf1eea5b7cfabf9c3a1ab40fbca21de118a0d4b. --- uk_bin_collection/uk_bin_collection/collect_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uk_bin_collection/uk_bin_collection/collect_data.py b/uk_bin_collection/uk_bin_collection/collect_data.py index 1190a6eb3c..0a9e510e6d 100755 --- a/uk_bin_collection/uk_bin_collection/collect_data.py +++ b/uk_bin_collection/uk_bin_collection/collect_data.py @@ -2,7 +2,7 @@ import importlib import os import sys -import logging, logging.config +import logging from uk_bin_collection.uk_bin_collection.get_bin_data import ( setup_logging, LOGGING_CONFIG, From e4fb52660dcd82aba9d61dc764526a3008b5789b Mon Sep 17 00:00:00 2001 From: David Park Date: Tue, 12 Nov 2024 22:38:40 +0000 Subject: [PATCH 3/4] feature: Add is_weekend(), is_working_day() and get_next_working_day() --- uk_bin_collection/uk_bin_collection/common.py | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/uk_bin_collection/uk_bin_collection/common.py b/uk_bin_collection/uk_bin_collection/common.py index 37c5092abc..024ae9655a 100644 --- a/uk_bin_collection/uk_bin_collection/common.py +++ b/uk_bin_collection/uk_bin_collection/common.py @@ -10,7 +10,6 @@ import requests from dateutil.parser import parse from selenium import webdriver -from selenium.common.exceptions import WebDriverException from selenium.webdriver.chrome.service import Service as ChromeService from urllib3.exceptions import MaxRetryError from webdriver_manager.chrome import ChromeDriverManager @@ -162,6 +161,31 @@ def is_holiday(date_to_check: datetime, region: Region = Region.ENG) -> bool: return False +def is_weekend(date_to_check: datetime) -> bool: + """ + Checks if a given date is a weekend + :param date_to_check: Date to check if it falls on a weekend + :return: Bool - true if a weekend day, false if not + """ + return True if date_to_check.date().weekday() >= 5 else False + + +def is_working_day(date_to_check: datetime, region: Region = Region.ENG) -> bool: + """ + Wraps is_holiday() and is_weekend() into one function + :param date_to_check: Date to check if holiday + :param region: The UK nation to check. Defaults to ENG. + :return: Bool - true if a working day (non-holiday, Mon-Fri). + """ + return False if is_holiday(date_to_check, region) or is_weekend(date_to_check) else True + + +def get_next_working_day(date: datetime, region: Region = Region.ENG) -> datetime: + while not is_working_day(date, region): + date += timedelta(days=1) + return date + + def get_weekday_dates_in_period(start: datetime, day_of_week: int, amount=8) -> list: """ Returns a list of dates of a given weekday from a start date for the given amount of weeks @@ -208,7 +232,7 @@ def get_next_occurrence_from_day_month(date: datetime) -> datetime: # Check if the target date has already occurred this year if (target_month < current_month) or ( - target_month == current_month and target_day < current_day + target_month == current_month and target_day < current_day ): date = pd.to_datetime(date) + pd.DateOffset(years=1) @@ -291,10 +315,10 @@ def contains_date(string, fuzzy=False) -> bool: def create_webdriver( - web_driver: str = None, - headless: bool = True, - user_agent: str = None, - session_name: str = None, + web_driver: str = None, + headless: bool = True, + user_agent: str = None, + session_name: str = None, ) -> webdriver.Chrome: """ Create and return a Chrome WebDriver configured for optional headless operation. From f721554e4a2c794375cf013168cd0e42ec2148ae Mon Sep 17 00:00:00 2001 From: David Park Date: Sat, 14 Dec 2024 00:16:47 +0000 Subject: [PATCH 4/4] feature: Add unit tests for new functions --- .../tests/test_common_functions.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/uk_bin_collection/tests/test_common_functions.py b/uk_bin_collection/tests/test_common_functions.py index bba11819a7..2e009e97e6 100644 --- a/uk_bin_collection/tests/test_common_functions.py +++ b/uk_bin_collection/tests/test_common_functions.py @@ -134,6 +134,32 @@ def test_is_holiday_different_region(mock_holidays_func): assert is_holiday(datetime(2023, 11, 30), Region.ENG) is False +def test_is_weekend_when_true(): + weekend_date = datetime(2024, 12, 7) + assert is_weekend(weekend_date) is True + + +def test_is_weekend_when_false(): + weekend_date = datetime(2024, 12, 6) + assert is_weekend(weekend_date) is False + + +def test_is_working_day_when_true(): + working_day_date = datetime(2024, 12, 6) + assert is_working_day(working_day_date) is True + + +def test_is_working_day_when_false(): + working_day_date = datetime(2024, 12, 7) + assert is_working_day(working_day_date) is False + + +def test_get_next_working_day(): + sample_date = datetime(2024, 12, 7) + next_working_day = get_next_working_day(sample_date) + assert next_working_day == datetime(2024, 12, 9) + + def test_remove_alpha_characters(): test_string = "12345abc12345" result = remove_alpha_characters(test_string)