Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为三种不同脚本编写Python的最佳实践_Python_Python 2.7 - Fatal编程技术网

为三种不同脚本编写Python的最佳实践

为三种不同脚本编写Python的最佳实践,python,python-2.7,Python,Python 2.7,我有一个项目,在我开始之前。我想采纳专家的建议 以下是项目要求: 我必须写三个不同的脚本,比如脚本1,脚本2,脚本 3.对于三个不同的脚本,很少有变量值是相同的。所以我计划再增加一个脚本,说普通脚本,所以所有的普通脚本 变量值在公共脚本中声明 脚本1可能需要脚本2和脚本3不需要的不同变量值-脚本2和脚本3也是如此 3. 下面是我的问题 我是否应该只在公共脚本中包含公共变量值,并在脚本本身中声明非公共变量脚本1特定 变量在脚本1中声明,而不是在公共脚本和 脚本2和脚本3也是如此 或者我应该在公共脚

我有一个项目,在我开始之前。我想采纳专家的建议

以下是项目要求:

我必须写三个不同的脚本,比如脚本1,脚本2,脚本 3.对于三个不同的脚本,很少有变量值是相同的。所以我计划再增加一个脚本,说普通脚本,所以所有的普通脚本 变量值在公共脚本中声明

脚本1可能需要脚本2和脚本3不需要的不同变量值-脚本2和脚本3也是如此 3. 下面是我的问题

我是否应该只在公共脚本中包含公共变量值,并在脚本本身中声明非公共变量脚本1特定 变量在脚本1中声明,而不是在公共脚本和 脚本2和脚本3也是如此

或者我应该在公共脚本文件中声明脚本1、脚本2和脚本3的所有输入和变量,然后调用 脚本1、2和3的变量值

哪个选项是最好的,或者我需要遵循的任何其他标准

另一个要求是,我必须要求用户运行所有脚本,即脚本1,然后是脚本2,脚本3,或者他想要运行的脚本 只有特定脚本,即他/她可以选择脚本1, 脚本2,脚本3。实现这些目标的最佳实践是什么 种类

现在我仍然只使用函数,编写单独的脚本并作为模块导入。对于这些项目,我第一次计划使用 python类


我需要你的提示如何在这个项目中使用类,我现在不知道的类。但是我开始阅读文档。

我建议的方法是声明一个类。 类是一种在函数之间共享变量并以适当顺序运行这些函数的干净方法。您可以这样构建您的类:

class MyClass(object):
  def __init__(self):
    # in the constructor, initialize your shared variables
    self.mySharedVar_1 = 0
    self.mySharedVar_2 = []
    # and so on

  def oldScript1(self,script1InputParam_1, script1InputParam_2):
    # here you implement what was in script1 with
    # variables that are specific to script1
    myScript1Var_1 = 0
    # example operation using a shared variable and a script1 variable
    myScript1Var_2 = myScript1Var_1 + self.mySharedVar_1

    return myScript1Var_2

  def oldScript2(self,script2InputParam_1):
    # same as for script1

  def oldScript3(self,script3InputParam_1):
    # again, same as for script1

  def run(self):
    # here you run the methods in the appropriate order, using the outputs of one script as the input to another
    outputScript1 = self.oldScript1(1,2)
    outputScript2 = self.oldScript2(outputScript1)
    # and so on for script3
然后在主脚本中,只需编写以下内容:

import MyClass

myInstance = MyClass() # you can specify constructor values if necessary
myInstance.run()

这个问题类似于面向对象编程:可以有一个定义某些属性的基类和继承基类并重新定义属性的子类。这取决于你的选择

但是,您所做的是一种配置文件

我认为最佳实践是在一个公共脚本中定义所有公共变量。这样,您就可以在一个地方记录它们。这是更好的维护

其他脚本只能重新定义它们需要的内容

普通的.py

脚本1.py

对于项目需求问题:变量值是什么意思?如果您有一些全局变量,例如,一个文件路径或一个常数,这3个脚本都是相同的,并且您对类不是很熟悉,那么我建议创建一个配置文件,该文件将提供给每个脚本。 然后,您可以在各自的脚本中更改这些值: conf.py文件:

配置文件:全局值 全局\u 1=某些\u字符串 全局_2=215某个数

script1.py也适用于script2.py和script3.py

from.conf导入* new_variable=全局_2+542使用全局值的新操作

对于问题3:最好、更好的方法是提供选择脚本1、脚本2和脚本3的选项。例如,您声明了一个包含以下选项的函数: 我们想通过命令行传递两个文件名,我们还想给出一个选项来检查脚本的用法。脚本的用法如下−

用法:test.py-i-o

下面是test.py的脚本−

#!/usr/bin/python
import sys, getopt
def main(argv):
    inputfile = ''
    outputfile = ''
    try:
        opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
    except getopt.GetoptError:
        print 'test.py -i <inputfile> -o <outputfile>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'test.py -i <inputfile> -o <outputfile>'
            sys.exit()
        elif opt in ("-i", "--ifile"):
            inputfile = arg
        elif opt in ("-o", "--ofile"):
            outputfile = arg
    print 'Input file is "', inputfile
    print 'Output file is "', outputfile

if __name__ == "__main__":
    main(sys.argv[1:])
关于问题4:
当然,类是用python编程的好方法,但它不是回答常见变量问题的最简单方法。

在“common”之前的“.”是什么意思?相对导入。阅读文件:
from .common import *

VAR2 = "another thing2"
#!/usr/bin/python
import sys, getopt
def main(argv):
    inputfile = ''
    outputfile = ''
    try:
        opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
    except getopt.GetoptError:
        print 'test.py -i <inputfile> -o <outputfile>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'test.py -i <inputfile> -o <outputfile>'
            sys.exit()
        elif opt in ("-i", "--ifile"):
            inputfile = arg
        elif opt in ("-o", "--ofile"):
            outputfile = arg
    print 'Input file is "', inputfile
    print 'Output file is "', outputfile

if __name__ == "__main__":
    main(sys.argv[1:])