Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant way to invoke multiple functions in Python

I am doing a data cleaning using Python. I have got the below workflow to call all my functions

  if __name__ == "__main__":

       data_file, hash_file, cols = read_file()
       survey_data, cleaned_hash_file = format_files(data_file, hash_file, cols)
       survey_data, cleaned_hash_file = rename_columns(survey_data, cleaned_hash_file)
       survey_data, cleaned_hash_file = data_transformation_stage_1(survey_data, cleaned_hash_file)
       observation, survey_data, cleaned_hash_file = data_transformation_stage_2(survey_data, cleaned_hash_file)
       observation, survey_data, cleaned_hash_file = data_transformation_stage_3(observation, survey_data, cleaned_hash_file)
       observation, survey_data, cleaned_hash_file = observation_date_fill(observation, survey_data, cleaned_hash_file)
       write_file(observation, survey_data, cleaned_hash_file)

So, the output (return statement variables) from each function is used as an input to the subsequent functions. All the functions return dataframe as an output. So observation,survey_data,cleaned_hash_file,data_file,hash_file,cols are all dataframes used in each function.

Is there any other better and elegant way to write this?

like image 842
The Great Avatar asked Jul 20 '26 19:07

The Great


1 Answers

Try iterating through your functions. It assumes that input of the current iteration has the same order as the output of the previous iteration:

funcs = [read_file, format_files, rename_columns, data_transformation_stage_1, data_transformation_stage_2, data_transformation_stage_3, observation_date_fill, write_file]

output = []
for func in funcs:
    output = func(*output)
like image 175
Georgios Douzas Avatar answered Jul 23 '26 07:07

Georgios Douzas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!