0

So i have numerous big functions, and since it is impossible to combine them all in one code, each one i contained it into a function. But i need to put all of them in one place. I did that, but the code runs somehow slow, and i want to make it faster. So i thought it might be because of the way i am organizing them. One way to make it faster: is when one of those functions returns a result, i want it to stop, and not check other functions if they are going to return something or not. This is the code:

def find_document(reference_number, date, document):
   print('''Please Wait !''')
   result = one_date_c(reference_number, date, document)
   if result:
     return result
   result = c_messy_year(reference_number, date, document)
   if result:
     return  result
   result = c_du_au_format(reference_number, date, document)
   if result:
     return result
   result = n_du_au_format(reference_number, date, document)
   if result:
     return result
   result = combined_dates_start_with_N(reference_number,date,document)
   if result:
     return result
   result = combined_dates_start_with_C(reference_number,date,document)
   if result:
     return result
  • 1
    Looking at the names of the functions, some of your functions might be improved by making them more generic, like combined_dates_start_with can accept the letter that it checks as an argument perhaps? It's a bit speculation but wanted to point out still. – Monata Jul 12 '23 at 09:35

2 Answers2

1
def find_document(reference_number, date, document):
   print('''Please Wait !''')
   if result:= one_date_c(reference_number, date, document):
     return result
   elif result:= c_messy_year(reference_number, date, document):
     return result
   elif result := c_du_au_format(reference_number, date, document)
     return result
   elif result:= n_du_au_format(reference_number, date, document):
     return result
   elif result:= combined_dates_start_with_N(reference_number,date,document):
     return result
   elif result:= combined_dates_start_with_C(reference_number,date,document):
     return result

The two changes that I could thought of were to use the assignment operator also known as walrus operator, and changing the other if statements to elif statements. This way I think it's more clear that it's supposed to return once.

The other idea that I had is to put the functions into a list and looping over it and breaking if the expected result is returned by any of them.

func_list = [one_date_c,c_messy_year,c_du_au_format,n_du_au_format,combined_dates_start_with_N,combined_dates_start_with_C]
for func in func_list:
  if result := func(reference_number, date, document):
    return result
Monata
  • 308
  • 1
  • 9
1

You can use short-circuiting with any() to achieve this:

def find_document(reference_number, date, document):
  print('''Please Wait !''')
  fns = (one_date_c, c_messy_year, c_du_au_format, n_du_au_format, combined_dates_start_with_N,
         combined_dates_start_with_C)
  any(result := fn(reference_number, date, document) for fn in fns)
  return result

If the result is already a boolean, it is even simpler:

def find_document(reference_number, date, document):
  print('''Please Wait !''')
  fns = (one_date_c, c_messy_year, c_du_au_format, n_du_au_format, combined_dates_start_with_N,
         combined_dates_start_with_C)
  return any(fn(reference_number, date, document) for fn in fns)