Skip to content

Commit

Permalink
Merge pull request #1000 from dp247/add-new-common-functions
Browse files Browse the repository at this point in the history
Add three new functions to common.py
  • Loading branch information
robbrad authored Dec 17, 2024
2 parents 668822a + f721554 commit 8825750
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
26 changes: 26 additions & 0 deletions uk_bin_collection/tests/test_common_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 30 additions & 6 deletions uk_bin_collection/uk_bin_collection/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 8825750

Please sign in to comment.