Valentyn
Valentyn

Reputation: 612

How to call functions in a strict order?

I've written the following functions, which should call in strict order for the correct program flow:

  1. reading file config.json

  2. connecting to SQL Server

  3. finding all SQL files in a directory

  4. execution of all SQL files and saving the results to the CSV file

  5. sending CSV-file with the results in the mail

  6. writing CSV-file as Google Sheet document

  7. closing a SQL Server session

def read_config():
    ....
    return config

def connect_to_sql_server(config):
    ....
    return connection

def find_sql_files_without_traverse(config):
   ...
   return files_list

def exec_sql_and_save_to_csv(files_list, connection, config):
   ...
   return csv_file

def send_email(csv_file, config):
   ...
   return 

def save_to_gspread(csv_file):
   ...
   return

def close_sql_server_conn(connection):
   ....
   return 

I call the functions like this:

send_email(exec_sql_and_save_to_csv(find_sql_files_without_traverse(read_config()), connect_to_sql_server(read_config()), read_config()), read_config())
save_to_gspread(exec_sql_and_save_to_csv(find_sql_files_without_traverse(read_config()), connect_to_sql_server(read_config()), read_config()))
close_sql_server_conn(connect_to_sql_server(read_config()))

It seems to me that this is the wrong way to call a function.

I'd really grateful for your responses.

Upvotes: 1

Views: 76

Answers (3)

srknzl
srknzl

Reputation: 787

Every time you do read_config() in your code, this function is called again. Your code may work, but it will run more than once.

This is bad for

1.Speed: Your code will be slower because you call so many functions where not needed.

2.Readabilty: You code will be shorter in line number but it will be harder to understand. Short codes are not good all the time.

e.g We could write c for config in short but it would not be easily understandable. Much better to write like this.

This is completely understandable and written by Séraphin I am not going to change it, as it is completely correct.

config = read_config()

connection = connect_to_sql_server(config)

files_list = find_sql_files_without_traverse(config)

csv_file= exec_sql_and_save_to_csv(files_list, connection, config)

send_email(csv_file, config)

save_to_gspread(csv_file)

close_sql_server_conn(connection)

Upvotes: 1

brunns
brunns

Reputation: 2764

If it were me, I'd do:

from contextlib import closing

config = read_config()
with closing(connect_to_sql_server(config)) as conn:
   sql_files = find_sql_files_without_traverse(config)
   csv_file = exec_sql_and_save_to_csv(sql_files, conn, config)
   send_email(csv_file, config)
   save_to_gspread(csv_file)

Upvotes: 0

Séraphin
Séraphin

Reputation: 744

One way of doing that is simply to call your functions in the desired order and assign the result to variables to be used as input for the next functions:


config = read_config():

connection = connect_to_sql_server(config)

files_list = find_sql_files_without_traverse(config)

csv_file= exec_sql_and_save_to_csv(files_list, connection, config)

send_email(csv_file, config)

save_to_gspread(csv_file)

close_sql_server_conn(connection)

Note: I would recommend to check the different Programming Paradigms in Python and chose the one that bests fits your needs for future developments. Intrresting article on that subject

Upvotes: 3

Related Questions