在Python中调用多个函数的优雅方法 [英] Elegant way to invoke multiple functions in Python
问题描述
我正在使用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)
因此,每个函数的输出(返回语句变量)都用作后续函数的输入.所有函数都将数据帧作为输出返回.因此observation
,survey_data
,cleaned_hash_file
,data_file
,hash_file
,cols
都是每个函数中使用的所有数据帧.
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?
推荐答案
尝试遍历函数.假定当前迭代的输入与先前迭代的输出具有相同的顺序:
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)
这篇关于在Python中调用多个函数的优雅方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!