from datetime import datetime
from dateutil.relativedelta import relativedelta

start_date = "2022-08-30"


def get_date_range(start_date):
    data_list = []
    now = datetime.now()
    sample = "%Y-%m-%d"
    first_date = datetime.strptime(start_date, sample)
    last_date = first_date + relativedelta(months=+3)
    if last_date < now:
        data_list.append([first_date.strftime(sample),
                          last_date.strftime(sample)])
    else:
        data_list.append([first_date.strftime(sample),
                         now.strftime(sample)])
        return data_list

    i = 0
    while (1):
        first_date = datetime.strptime(
            data_list[i][1], sample) + relativedelta(days=+1)
        last_date = first_date + relativedelta(months=+3)
        if last_date < now:
            data_list.append([first_date.strftime(sample),
                              last_date.strftime(sample)])
            i += 1
        else:
            data_list.append([first_date.strftime(sample),
                              now.strftime(sample)])
            break
    return data_list


data_list = get_date_range(start_date)
print(data_list)

 Public
Share a link to this review

10.53% issue ratio

R1 Missing type hints

Type hints help humans and linters (like mypy) to understand what to expect "in" and "out" for a function. Not only it serves as a documentation for others (and you after some time, when the code is wiped from your "brain cache"), but also allows using automated tools to find type errors.

R14 Not using "early quit"

"Early quit" is a common pattern which reduces branching and indents in python code. Instead of writing if a: <1000 lines of code> else: <return / raise exception>, revert the condition: if not a: <return / raise exception>. This way the "else" clause is not needed, and program logic is straightforward: if something is wrong, quit or raise, otherwise go on.

Mixing "returning" and "not returning" is a bad idea. Better separate those.

Suggested change:
# this is a corner case
if last_date >= now:
    data_list.append([
        first_date.strftime(sample),
        now.strftime(sample)
    ])
    return data_list
    
# here goes a default case
data_list.append([
    first_date.strftime(sample),
    last_date.strftime(sample)
])
O16 While True

Developers always write code without bugs. So there's no chance of writing a code that will never exit infinite loop, right? However, if it happens anyway and your program ends up in an infinite loop, it won't just break and raise an exception. It will get stuck, become unresponsive, and maybe will eat all memory if you allocate something inside the loop. Very dangerous, but easy to avoid - use for loops with iteration limit: for _ in range(10_000): <your code> else: raise LoopError('Ran code 10k times and didn't break from loop, something went terribly wrong')

Since you're iterating over data_list anyway, you don't need i

Suggested change:
for date_ in data_list:
    first_date = datetime.strptime(data_[1], sample) + relativedelta(days=+1)
L9 Bad design

Seems like things could be organized in a better way.

Calling strftime and then strptime on the result is redundant. Better store as python datetime and convert to string right before returning.


Create new review request