在Python中创建“runner”脚本的最佳方法是什么?(Best way to create a “runner” script in Python?)

我在一个目录中有一堆Python模块,都是派生类。 我需要一个“runner”脚本,为每个模块实例化其中的类(实际的类名可以通过模块文件名构建),并在每个模块上调用“go”方法。

我不知道有多少个模块,但我可以通过类似“bot _ *。py”的方式列出所有这些模块,

我认为这是关于“元编程”的东西,但怎么可能是最好的(最优雅的)方法呢?

I have a bunch of Python modules in a directory, all being a derivate class. I need a "runner" script that, for each module, instantiate the class that is inside it (the actual class name can be built by the module file name) and than call the "go" method on each of them.

I don't know how many modules are there, but I can list all of them globbing the directory via something like "bot_*.py"

I think this is something about "meta programming", but how could be the best (most elegant) way to do it?

最满意答案

def run_all(path): import glob, os print "Exploring %s" % path for filename in glob.glob(path + "/*.py"): # modulename = "bot_paperino" modulename = os.path.splitext(os.path.split(filename)[-1])[0] # classname = "Paperino" classname = modulename.split("bot_")[-1].capitalize() # package = "path.bot_paperino" package = filename.replace("\\", "/").replace("/", ".")[:-3] mod = __import__(package) if classname in mod.__dict__[modulename].__dict__.keys(): obj = mod.__dict__[modulename].__dict__[classname]() if hasattr(obj, "go"): obj.go() if __name__ == "__main__": import sys # Run on each directory passed on command line for path in sys.argv[1:]: run_all(sys.argv[1])

您需要在每个要运行的路径中都有一个__init__.py 。 根据您的意愿更改“bot_”。 在Windows和Linux上运行。

def run_all(path): import glob, os print "Exploring %s" % path for filename in glob.glob(path + "/*.py"): # modulename = "bot_paperino" modulename = os.path.splitext(os.path.split(filename)[-1])[0] # classname = "Paperino" classname = modulename.split("bot_")[-1].capitalize() # package = "path.bot_paperino" package = filename.replace("\\", "/").replace("/", ".")[:-3] mod = __import__(package) if classname in mod.__dict__[modulename].__dict__.keys(): obj = mod.__dict__[modulename].__dict__[classname]() if hasattr(obj, "go"): obj.go() if __name__ == "__main__": import sys # Run on each directory passed on command line for path in sys.argv[1:]: run_all(sys.argv[1])

You need a __init__.py in each path you want to "run". Change "bot_" at your will. Run on windows and linux.

在Python中创建“runner”脚本的最佳方法是什么?(Best way to create a “runner” script in Python?)

我在一个目录中有一堆Python模块,都是派生类。 我需要一个“runner”脚本,为每个模块实例化其中的类(实际的类名可以通过模块文件名构建),并在每个模块上调用“go”方法。

我不知道有多少个模块,但我可以通过类似“bot _ *。py”的方式列出所有这些模块,

我认为这是关于“元编程”的东西,但怎么可能是最好的(最优雅的)方法呢?

I have a bunch of Python modules in a directory, all being a derivate class. I need a "runner" script that, for each module, instantiate the class that is inside it (the actual class name can be built by the module file name) and than call the "go" method on each of them.

I don't know how many modules are there, but I can list all of them globbing the directory via something like "bot_*.py"

I think this is something about "meta programming", but how could be the best (most elegant) way to do it?

最满意答案

def run_all(path): import glob, os print "Exploring %s" % path for filename in glob.glob(path + "/*.py"): # modulename = "bot_paperino" modulename = os.path.splitext(os.path.split(filename)[-1])[0] # classname = "Paperino" classname = modulename.split("bot_")[-1].capitalize() # package = "path.bot_paperino" package = filename.replace("\\", "/").replace("/", ".")[:-3] mod = __import__(package) if classname in mod.__dict__[modulename].__dict__.keys(): obj = mod.__dict__[modulename].__dict__[classname]() if hasattr(obj, "go"): obj.go() if __name__ == "__main__": import sys # Run on each directory passed on command line for path in sys.argv[1:]: run_all(sys.argv[1])

您需要在每个要运行的路径中都有一个__init__.py 。 根据您的意愿更改“bot_”。 在Windows和Linux上运行。

def run_all(path): import glob, os print "Exploring %s" % path for filename in glob.glob(path + "/*.py"): # modulename = "bot_paperino" modulename = os.path.splitext(os.path.split(filename)[-1])[0] # classname = "Paperino" classname = modulename.split("bot_")[-1].capitalize() # package = "path.bot_paperino" package = filename.replace("\\", "/").replace("/", ".")[:-3] mod = __import__(package) if classname in mod.__dict__[modulename].__dict__.keys(): obj = mod.__dict__[modulename].__dict__[classname]() if hasattr(obj, "go"): obj.go() if __name__ == "__main__": import sys # Run on each directory passed on command line for path in sys.argv[1:]: run_all(sys.argv[1])

You need a __init__.py in each path you want to "run". Change "bot_" at your will. Run on windows and linux.