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

时间:2017-06-02 13:44:02

标签: python python-2.7

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

以下是项目要求:

  

我必须写三个不同的脚本,比如脚本1,脚本2,脚本   3.三个不同的脚本中很少有变量值相同。所以我计划再加一个脚本说共同脚本所以常见   变量值以common-script声明。

     
      
  1. 脚本1可能需要不同的变量值,这些值不是脚本2和脚本3所必需的 - 类似于脚本2和脚本   3。
  2.   

以下是我的问题。

  
      
  1. 我是否只在common-script中包含公共变量值,并在其脚本本身中声明非公共变量(特定于脚本1)   变量在脚本1中声明,而不是在common-script和   类似于脚本2和脚本3)。

  2.   
  3. 或者我应该在common-script文件本身中声明脚本1,脚本2和脚本3的所有输入和变量然后调用   变量值到脚本1,2和3?

  4.   

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

  
      
  1. 另一个要求是我必须要求用户运行脚本1的所有脚本,然后是脚本2,脚本3或者他想要运行   只有特定的脚本,他/她获得选择脚本1的选项,   脚本2,脚本3.实现这些的最佳实践是什么   样的?

  2.   
  3. 现在我只使用了函数并编写了单独的脚本并导入为模块。对于这些项目我第一次计划使用   python classes。

  4.   

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

3 个答案:

答案 0 :(得分:2)

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

但是,你正在做的是一种配置文件。

我认为最佳做法是在通用脚本中定义所有常见变量。这样你就可以在一个地方记录它们。这样做更好。

其他脚本只能重新定义他们需要的东西。

<强> common.py

#: This is my var 1
VAR1 = "thing1"

#: This is my var 2
VAR2 = "thing2"

<强> script1.py

from .common import *

VAR2 = "another thing2"

答案 1 :(得分:0)

我建议的方法是宣布一个班级。 类是一种“干净”的方式,可以在函数之间共享变量并以适当的顺序运行这些函数。你可以像这样构建你的类:

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()

答案 2 :(得分:0)

对于项目需求问题:“变量值”是什么意思?如果您有一些“全局”变量(例如,文件路径或常量)对于3个脚本是相同的,并且您对类不太满意,我建议创建一个配置文件,该文件将被赋予每个脚本。 然后,您可以在相应的脚本中更改这些值: conf.py文件:

#config file: global values GLOBAL_1 = "some_string" GLOBAL_2 = 215 # some number

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

from .conf import * new_variable = GLOBAL_2 + 542 # new operation using global value

对于问题3:最好和更好的方法是提供选择脚本1,脚本2,脚本3的选项。例如,您声明一个选项的功能,如: 考虑我们想通过命令行传递两个文件名,我们还想提供一个选项来检查脚本的用法。脚本的用法如下 -

usage: test.py -i <inputfile> -o <outputfile>

以下是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中编程的好方法,但它不是回答常见变量问题的最简单方法。

相关问题