Created
April 22, 2022 18:35
-
-
Save lykinsbd/b1d4c38e33c323e882b5ffbf8502cf6b to your computer and use it in GitHub Desktop.
If Elif vs Match Case in Python 3.10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def convert_timeframe_match_case(timeframe: str) -> tuple[date, date]: | |
"""Convert a provided relative timeframe, i.e. 'Last Week' into a from_date and to_date datetime `Date` object. | |
Args: | |
timeframe (str): A string for a relative timeframe from among the below: | |
["Yesterday", "This Week", "Last Week", "This Month", "Last Month", "Year to Date"] | |
Returns: | |
tuple[Date, Date]: from_date and to_date for a given timeframe | |
""" | |
acceptable_timeframes = ["Yesterday", "This Week", "Last Week", "This Month", "Last Month", "Year to Date"] | |
if timeframe not in acceptable_timeframes: | |
raise ValueError(f"Timeframe of {timeframe} not in list of acceptable Timeframes: {acceptable_timeframes}") | |
# Match against specific timeframes | |
from_date, to_date = (None, None) | |
match timeframe.split(): | |
case ["Year"]: | |
from_date = dateparser.parse("January 1").date() | |
case [("This" | "Last") as which, ("Month" | "Week") as week_or_month]: | |
if week_or_month == "Month": | |
from_date = dateparser.parse(timeframe).replace(day=1).date() | |
else: | |
from_date = (datetime.now() + relativedelta(weekday=MO(-1 if which == "This" else -2))).date() | |
if which == "Last": | |
to_date = (from_date + relativedelta(**{f"{week_or_month.lower()}s": +1, "seconds": -1})).date() | |
# If not caught above set for all other scenarios | |
if not from_date: | |
from_date = dateparser.parse(timeframe).date() | |
if not to_date: | |
to_date = datetime.now().date() | |
return from_date, to_date | |
def convert_timeframe_if_elif(timeframe: str) -> tuple[date, date]: | |
"""Convert a provided relative timeframe, i.e. 'Last Week' into a from_date and to_date datetime `Date` object. | |
Args: | |
timeframe (str): A string for a relative timeframe from among the below: | |
["Yesterday", "This Week", "Last Week", "This Month", "Last Month", "Year to Date"] | |
Returns: | |
tuple[Date, Date]: from_date and to_date for a given timeframe | |
""" | |
acceptable_timeframes = ["Yesterday", "This Week", "Last Week", "This Month", "Last Month", "Year to Date"] | |
if timeframe not in acceptable_timeframes: | |
raise ValueError(f"Timeframe of {timeframe} not in list of acceptable Timeframes: {acceptable_timeframes}") | |
if timeframe == "Year to Date": | |
from_date = dateparser.parse("January 1").date() | |
elif timeframe in ("This Month", "Last Month"): | |
from_date = dateparser.parse(timeframe).replace(day=1).date() | |
elif timeframe == "This Week": | |
from_date = (datetime.now() + relativedelta(weekday=MO(-1))).date() | |
elif timeframe == "Last Week": | |
from_date = (datetime.now() + relativedelta(weekday=MO(-2))).date() | |
else: | |
from_date = dateparser.parse(timeframe).date() | |
if timeframe == "Last Month": | |
to_date = (from_date + relativedelta(months=+1, seconds=-1)).date() | |
elif timeframe == "Last Week": | |
to_date = (from_date + relativedelta(weeks=+1, seconds=-1)).date() | |
else: | |
to_date = datetime.now().date() | |
return from_date, to_date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment