1
\$\begingroup\$

How could I reduce this schedule checker? There are too many conditions.

This code is supposed to make sure we're not booking events for employees that are already booked in a specified timeframe.

for i in range(len(employeesChosen)):
    info = get_employee_info(employeesChosen[i])
    event_done = False
    if employeesChosen[i] not in currentEmployees and check_employee_availability(service,employeesChosen[i],currentStart,currentEnd,currentStart,calendarEnd):
        event_done = True
    else:
        for event in events:
            if employeesChosen[i] == event['summary']:

                if str2datetime(currentStart) >= str2datetime(event['start']['dateTime'].split('+')[0]) and str2datetime(currentEnd) <= str2datetime(event['end']['dateTime'].split('+')[0]):
                    event_done = False
                    break

                elif str2datetime(currentStart) <= str2datetime(event['start']['dateTime'].split('+')[0]) and str2datetime(currentEnd) <= str2datetime(event['end']['dateTime'].split('+')[0]):
                    event_done = False
                    break

                elif str2datetime(currentStart) >= str2datetime(event['start']['dateTime'].split('+')[0]) and str2datetime(currentEnd) >= str2datetime(event['end']['dateTime'].split('+')[0]):
                    event_done = False
                    break

                elif str2datetime(currentStart) <= str2datetime(event['start']['dateTime'].split('+')[0]) and str2datetime(currentEnd) >= str2datetime(event['end']['dateTime'].split('+')[0]):
                    event_done = False
                    break

                elif str2datetime(currentStart) < str2datetime(event['start']['dateTime'].split('+')[0]):  #rdv avant un qui est deja set
                    event_info = {'location': get_company_location(event['description']),'datetime': event['end']['dateTime'].split('+')[0]}
                    start_first_event = {'location': get_company_location(event['description']),'datetime': event['start']['dateTime'].split('+')[0]}
                    event_fabricant_info = {'location': get_company_location(event_fabricant),'datetime': currentStart}
                    end_second_event = {'location': get_company_location(event_fabricant),'datetime': currentEnd}
                    if check_event_possibility_if_before(event_info, event_fabricant_info, end_second_event, start_first_event, info[0]):
                        event_done = True
                    else:
                        event_done = False


                elif str2datetime(currentStart) > str2datetime(event['end']['dateTime'].split('+')[0]) or str2datetime(currentEnd): #rdv apres un qui est deja set
                    event_info={'location': get_company_location(event['description']), 'datetime': event['end']['dateTime'].split('+')[0]}
                    start_first_event ={'location': get_company_location(event['description']), 'datetime': event['start']['dateTime'].split('+')[0]}
                    event_fabricant_info = {'location': get_company_location(event_fabricant), 'datetime': currentStart}
                    end_second_event = {'location': get_company_location(event_fabricant), 'datetime': currentEnd}
                    if check_event_possibility(event_info, event_fabricant_info, end_second_event,start_first_event, info[0]):
                        event_done=True
                    else:
                        event_done=False

                else: event_done = False
\$\endgroup\$
3
\$\begingroup\$

Specific suggestions

  1. You can pull out variables for the several repeated calls (such as str2datetime(currentStart)) to massively simplify the code.

  2. The idiomatic way to write

    for counter in range(len(foos)):
        foo = foos[counter]
    

    is

    for foo in foos:
    

Tool support

  1. Use a formatter like Black to format the code to be more idiomatic.
  2. Use a linter like flake8 to recommend further changes like using snake_case variable names.
\$\endgroup\$
1
\$\begingroup\$

After introducing the variables start_datetime and end_datetime as @l0b0 suggested, your code becomes much clearer and more readable. It seems like your first 4 if/elif statements will always be True so the later statements will never be reached.

start_datetime = str2datetime(currentStart)
end_datetime = str2datetime(currentEnd)
event_start = str2datetime(event['start']['dateTime'].split('+')[0])
event_end = str2datetime(event['end']['dateTime'].split('+')[0])

if start_datetime >= event_start and end_datetime  <= event_end:
                                event_done = False
                                break

elif start_datetime  <= event_start  and end_datetime  <= event_end:
                                event_done = False
                                break

elif start_datetime  >= event_start and end_datetime  >= event_end :
                                event_done = False
                                break

elif start_datetime  <= event_start and end_datetime >= event_end:
                                event_done = False
                                break

I suppose that's not what you actually meant to do so you should change that first. If this is the behaviour you want; you can change this entire part to

event_done = False

and delete all conditions after this.

Something else, try to make your code PEP8 compliant. These are guidelines for how to format your code like making your lines max 79 characters long.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.