简体   繁体   English

编写Python脚本以使用控制台命令执行

[英]Writing a Python script to execute with a console command

Research is at the bottom, read before -1'ing... Thanks. 研究在底部,在-1之前阅读...谢谢。

I have to write a Python script that runs SQL queries. 我必须编写一个运行SQL查询的Python脚本。 I made a main class and called SQLQuery. 我创建了一个主类并调用了SQLQuery。 Each SQLQuery instance represents a query. 每个SQLQuery实例代表一个查询。 The script must be structured like this: 脚本的结构必须如下:

class SQLQuery(object):

     def __init___(self, string_myQuery)...

instance1 = SQLQuery(SQLQuery1)...

instance2 = SQLQuery(SQLQuery2)...

As a user requirement, the instances must be in the same file as the class (so I can't just make each instance a main and execute that file separately), and each instance must be executed with Linux console commands. 作为用户要求,实例必须与类位于同一文件中(因此我不能只将每个实例作为主要文件并单独执行该文件),并且必须使用Linux控制台命令执行每个实例。 I can execute the entire script with a simple python SQLQuery.py but I need to execute each instance separately. 我可以使用简单的python SQLQuery.py执行整个脚本,但我需要单独执行每个实例。 The queries will be executed every day, automatically, so I don't need a terminal UI tree. 查询将每天自动执行,因此我不需要终端UI树。 It should be executed with a command similar to this: 它应该使用类似于下面的命令执行:

python SQLQuery.py -inst1

will execute instance1. 将执行instance1。

python SQLQuery.py -inst2

will execute instance2. 将执行instance2。

I have researched how to execute Python scripts with Linux commands and most of the articles are about calling commands from the Python script. 我研究了如何使用Linux命令执行Python脚本,大多数文章都是关于从Python脚本调用命令。 However, I found this article from the Python documentation. 但是,我从Python文档中找到了这篇文章 It suggests adding -m, so: 它建议添加-m,所以:

python SQLQuery.py -m inst1

This would let me set my main with a console command, but it doesn't work since the instances aren't modules. 这将让我使用控制台命令设置我的主,但它不起作用,因为实例不是模块。 And since the instances must be in the same file as the class, I can't just import them as a module when I execute SQLQuery.py with a console command. 由于实例必须与类在同一个文件中,因此当我使用控制台命令执行SQLQuery.py时,我不能将它们作为模块导入。

Ignoring all the irrelevancies, it sounds like your problem is that you have a bunch of global objects named instance1 , instance2 , instance3 , etc., and you want to call some method on one of them based on a command-line parameter whose value will be similar to, but not identical to, the instance names. 忽略所有无关紧要,听起来你的问题是你有一堆名为instance1instance2instance3等的全局对象,并且你想根据一个命令行参数调用其中一个方法,其值为与实例名称相似但不相同。

That's probably not a good idea… but it's not that hard: 这可能不是一个好主意......但并不是那么难:

if __name__ == '__main__':
    inst = sys.argv[1] # will be 'inst1', 'inst13', etc.
    inst_number = inst[5:] # so '1', '13', etc.
    inst_name = 'instance' + inst_number
    instance = globals()[inst_name]
    instance.execute()

A much better way to do the same thing is to put the instance globals into a list or dict that you can index. 做同样事情的更好方法是将instance全局变量放入可以索引的列表或字典中。

For example, let's say instead of instance1 , instance2 , etc., you've got an instances dict, with instances['1'] , instances[2] , etc. Now instead of this: 例如,假设代替instance1instance2等,你有一个instances dict, instances['1']instances[2]等。现在代替这个:

inst_name = 'instance' + inst_number
instance = globals()[inst_name]
instance.execute()

… you just do this: ......你这样做:

instances[inst_number].execute()

Also, instead of coming up with a command-line parameter that has extra stuff in it that you have to parse and throw away, and has no more meaning for a human reader than for your code, why not just take a number? 此外,不是想出一个命令行参数,其中包含额外的东西,你必须解析并丢弃,并且对于人类读者而言没有比代码更多的意义,为什么不只是取一个数字呢?

python myscript.py 12

Or, alternatively, use argparse to create an argument that can be used in all of the obvious ways: 或者,使用argparse创建一个可以以所有显而易见的方式使用的参数:

python myscript.py --instance=12
python myscript.py --instance 12
python myscript.py -i12
python myscript.py -i 12

Either way, your code gets the string '12' , which it can then use to look up the function, as above. 无论哪种方式,您的代码都会获得字符串'12' ,然后可以使用它来查找函数,如上所述。

You have the wrong syntax for the -m option. 您的-m选项语法错误。 Suppose you have the following file named foo.py : 假设您有以下名为foo.py文件:

import sys
print 'First arg is: ', sys.argv[1]

Then you would call it like this: 然后你会这样称呼它:

$ python -m foo bar
First arg is:  bar

Note that the ".py" extension is omitted. 请注意,省略了“.py”扩展名。 You can then use the command line argument to decide which object to use or use the argparse or optparse module to handle the argument. 然后,您可以使用命令行参数来决定使用哪个对象,或使用argparseoptparse模块来处理参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM