function to validate dates from a list and return a tuple with findings

Question:

I need help to create a function that receives the 2 values below and returns a tuple with 2 lists inside as detailed below.

initial_date = date(2021, 11, 30)
today = date.today()
balance_dates = {
    1: date(2020, 5, 31), 2: date(2020, 6, 20), 3: date(2020, 6, 20),
    4: date(2020, 8, 30), 5: date(2020, 5, 31), 6: date(2020, 12, 31),
    7: date(2020, 5, 31), 8: date(2020, 11, 30), 9: date(2023, 2, 28),
    10: date(2024, 5, 31), 11: date(2023, 11, 30), 12: date(2023, 2, 28),

}

Function: check_missing_or_wrong_balances(initial_date, balance_date) (Returns a tuple with 2 lists)

Description of the tuple:

  1. (list 1) Check if balance_date has at least one date representing the very last day of each month from initial_date to the current date and if not, create/append the missing month (full date with the last day YYY-mm-dd) to a list and return it as the first value of the tuple.

  2. (list 2) if the date tested above isn’t the last day of the given month, create/append the id of that date in another list returned as the second value of the tuple. Additionally, add the ids of future dates (after the current date) and ids of duplicated dates, leaving only the first match found outside of this (e.g. if 3 exact dates were found, add the id of 2 occurences).

Asked By: Pabluez

||

Answers:

Here’s a possible implementation of the check_missing_or_wrong_balances function that satisfies the requirements described:

from datetime import datetime, timedelta

def check_missing_or_wrong_balances(initial_date, balance_date):
    # Convert the initial date to a datetime object
    initial_date = datetime.strptime(initial_date, '%Y-%m-%d')

    # Create a list of expected end-of-month dates from the initial date to now
    today = datetime.now()
    expected_dates = []
    while initial_date <= today:
        year = initial_date.year
        month = initial_date.month
        last_day = (datetime(year, month, 1) + timedelta(days=32)).replace(day=1) - timedelta(days=1)
        expected_dates.append(last_day)
        initial_date = last_day + timedelta(days=1)

    # Create two lists to store the results
    missing_dates = []
    wrong_dates = []

    # Loop through the balance dates and process each one
    processed_dates = set() # Set to keep track of already processed dates
    for id, date_str in balance_date.items():
        date = datetime.strptime(date_str, '%Y-%m-%d')
        if date in processed_dates:
            continue # Skip already processed dates
        processed_dates.add(date)
        if date > today:
            wrong_dates.append(id)
            continue
        if date not in expected_dates:
            missing_dates.append((date.year, date.month, date.day))
        else:
            expected_dates.remove(date)
        last_day = (date + timedelta(days=32)).replace(day=1) - timedelta(days=1)
        if date != last_day:
            wrong_dates.append(id)

    # Check if there are any remaining expected dates not found in balance_date
    for date in expected_dates:
        missing_dates.append((date.year, date.month, date.day))

    # Return the results as a tuple of two lists
    return (missing_dates, wrong_dates)

Here’s how you can use this function with the example data you provided:

initial_date = "1985-05-31"
balance_date = {
    '1': '2020-05-31', '2': '2020-06-20', '3': '2020-06-20',
    '4': '2020-08-30', '5': '2020-05-31', '6': '2020-12-31',
    '7': '2020-05-31', '8': '2020-11-30', '9': '2023-02-29',
    '10': '2024-05-31', '11': '2023-11-30', '12': '2023-02-28',
}

missing_dates, wrong_dates = check_missing_or_wrong_balances(initial_date, balance_date)
print("Missing dates:", missing_dates)
print("Wrong dates:", wrong_dates)
Answered By: M.Mevlevi
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.