[英]Execute chain of functions in Python
I am doing data pre-processing using Python and Pandas.我正在使用 Python 和 Pandas 进行数据预处理。 For that reason, I wrote several functions for cleaning data, which should be executed one after another in sequence.为此,我写了几个清理数据的函数,应该依次执行。
I don't want to manually execute all functions every time I open Jupyter.我不想每次打开 Jupyter 都手动执行所有功能。 So I am searching for something similar to a trigger, where I will include all function names and by initializing it, all functions will be executed.所以我正在寻找类似于触发器的东西,我将在其中包含所有函数名称并通过初始化它来执行所有函数。
One way is to define a Python function which will call other functions inside body;一种方法是定义一个 Python 函数,它会调用 body 内部的其他函数; but not sure how efficient is that.但不确定它的效率如何。
I looked for sklearn.Pipeline()
and pandas.pipe()
functions, but none of those seemed right tool for me.我寻找sklearn.Pipeline()
和pandas.pipe()
函数,但这些对我来说似乎都不是正确的工具。
Any recommendations?有什么建议吗?
Write a driver function - lets call it start()
and call all your data cleaning functions one after the other in this function.编写一个驱动程序函数 - 让我们调用它start()
并在此函数中一个接一个地调用所有数据清理函数。
def start():
data_cleaning_1()
data_cleaning_2()
data_cleaning_3()
.
.
Call the start()
function.调用start()
函数。
It can be done by storing the reference to the functions on a list, but create a new function that call all those functions makes more sense.这可以通过将函数的引用存储在列表中来完成,但创建一个调用所有这些函数的新函数更有意义。
def fun1():
print("function 1")
def fun2():
print("function 2")
functions = [fun1,fun2]
for function in functions:
function()
or要么
def fun1():
print("function 1")
def fun2():
print("function 2")
def begin():
func1()
func2()
# any other function
begin()
If you want something more advanced, you could use the design pattern Chain of Responsibility reading material如果你想要更高级的东西,你可以使用责任链阅读材料的设计模式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.