Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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
Oop 使用参数从pythonshell运行python脚本_Oop_Parameters_Python - Fatal编程技术网

Oop 使用参数从pythonshell运行python脚本

Oop 使用参数从pythonshell运行python脚本,oop,parameters,python,Oop,Parameters,Python,免责声明:我正试图用python编写我的第一个类,因此请提出建设性的批评 #!/usr/bin/python from optparse import OptionParser class Permutate: email_dict = {} email_provider = ( '@gmail.com', '@aol.com', '@yahoo.com' ) def __init__(self, fname

免责声明:我正试图用python编写我的第一个类,因此请提出建设性的批评

#!/usr/bin/python

from optparse import OptionParser

class Permutate:

    email_dict = {}
    email_provider = (
        '@gmail.com',
        '@aol.com',
        '@yahoo.com'
    )

    def __init__(self, fname, lname):
        '''
            vars:
            [fname] - first name of the permutation
            [lname] - last name of the permutation
            [email_combination] - first and last name
        '''
        self.fname = fname
        self.lname = lname
        self.email_combination = email_combination

    '''
        compute_first_last():
            email_combination = <string> + <string> + <string>
                computes the first name and last name combination plus and email provider.

                Loop through all combinations of first + last + email_provider while the value *is u

                A single combination looks like <fname>.<lname>@<email_provider>
#/usr/bin/python
从optpasse导入OptionParser
类置换:
电子邮件_dict={}
电子邮件提供商=(
“@gmail.com”,
@aol.com,,
“@yahoo.com”
)
定义初始化(self、fname、lname):
'''
变量:
[fname]-排列的第一个名称
[lname]-置换的姓氏
[电子邮件组合]-姓名和姓氏
'''
self.fname=fname
self.lname=lname
self.email\u组合=email\u组合
'''
先计算后计算():
电子邮件组合=++
计算名字和姓氏的组合以及电子邮件提供商。
当值*为u时,循环遍历first+last+email_provider的所有组合
一个单一的组合看起来像@

谢谢

对于接受Python中的命令行参数,
optparse
库非常方便

有关更多信息,请查看:

您可以接受与以下类似的命令行参数:

from optparse import OptionParser

def main():

  parser = OptionParser(
          usage='%prog -f firstname -l lastname',
          version='%prog 1.1')

  parser.add_option('-f', '--firstname', dest='firstname')

  parser.add_option('-l', '--lastname', dest='lastname')

  (opt, args) = parser.parse_args()
  print opt.firstname
  print opt.lastname

  # You can create the new Permutate object here
  # perm = Permutate(opt.firstname, opt.lastname)
  # and call its relevant functions


if __name__ == '__main__':
  main()
您将得到如下结果:

python myfile.py -f firstnamehere -l lastnamehere
firstnamehere
lastnamehere

注意:您还应该执行输入卫生,以确保用户正确调用脚本。

我相信本节和compute\u first\u last()方法:

不应在类permutate的范围内(当前为)。编辑:还有,我之前删除了这个部分,但忘记了重新包含,实际上应该是这样的:

if __name___ == '__main__':
    compute_first_last()
目前,解释器所做的只是解析一个包含类的文件。它缺少所需方法的执行,因为它包含在类中并且从未显式调用。此外,您正在调用compute_first_last方法,该方法不提供任何参数,尽管它需要四个参数。你可以从这里拿走!:-)

编辑:关于如何/做什么的解释:

编辑:再次检查代码后,我编辑了上面的内容

#!/usr/bin/python

from optparse import OptionParser

class Permutate:

    email_dict = {}
    email_provider = (
        '@gmail.com',
        '@aol.com',
        '@yahoo.com'
    )

    def __init__(self, fname, lname):
        '''
            vars:
            [fname] - first name of the permutation
            [lname] - last name of the permutation
            [email_combination] - first and last name
        '''
        self.fname = fname
        self.lname = lname
        self.email_combination = email_combination

'''
    compute_first_last():
        email_combination = <string> + <string> + <string>
            computes the first name and last name combination plus and email provider.

            Loop through all combinations of first + last + email_provider while the value *is u

            A single combination looks like <fname>.<lname>@<email_provider>
'''

def compute_first_last(self, fname, lname, email_combination):
    parser = OptionParser(usage='%prog -f fname -l lname', version='%prog 1.1')

    parser.add_option('-f', '--fname', dest='fname')
    parser.add_option('-l', '--lname', dest='lname')

    (opt, args) = parser.parse_args()

    perm = Permutate(opt.fname, opt.lname)

    email_combination = fname + "." + lname + email_provider

    while combination in email_combination:
        print combination

if __name__ == '__main__':
    compute_first_last() #this method call needs to be provided parameters
#/usr/bin/python
从optpasse导入OptionParser
类置换:
电子邮件_dict={}
电子邮件提供商=(
“@gmail.com”,
@aol.com,,
“@yahoo.com”
)
定义初始化(self、fname、lname):
'''
变量:
[fname]-排列的第一个名称
[lname]-置换的姓氏
[电子邮件组合]-姓名和姓氏
'''
self.fname=fname
self.lname=lname
self.email\u组合=email\u组合
'''
先计算后计算():
电子邮件组合=++
计算名字和姓氏的组合以及电子邮件提供商。
当值*为u时,循环遍历first+last+email_provider的所有组合
一个单一的组合看起来像@
'''
def compute_first_last(自我、fname、lname、电子邮件组合):
parser=OptionParser(用法=“%prog-f fname-l lname”,版本=“%prog 1.1”)
parser.add_选项('-f','-fname',dest='fname'))
parser.add_选项('-l','-lname',dest='lname'))
(opt,args)=parser.parse_args()
perm=Permutate(opt.fname,opt.lname)
电子邮件\u组合=fname+“+lname+电子邮件\u提供者
当组合在电子邮件中时\u组合:
打印组合
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
compute_first_last()#此方法调用需要提供参数

对问题进行修改后,这是一段工作代码,将为您提供输出。不确定这是否正是你想要的,但它会起作用

#!/usr/bin/python

from optparse import OptionParser

class Permutate:

    email_dict = {}
    email_provider = (
        '@gmail.com',
        '@aol.com',
        '@yahoo.com'
    )

    def __init__(self, fname, lname):
        '''
            vars:
            [fname] - first name of the permutation
            [lname] - last name of the permutation
            [email_combination] - first and last name
        '''
        assert isinstance(fname, str) and isinstance(lname, str), "Only strings may be supplied as names"

        self.fname = fname
        self.lname = lname
        self.email_combination = fname + "." + lname

        for provider in Permutate.email_provider:
            print "%s%s" % (self.email_combination, provider)

def compute_first_last():
    '''
        compute_first_last():
            email_combination = <string> + <string> + <string>
                computes the first name and last name combination plus and email provider.

                Loop through all combinations of first + last + email_provider while the value *is u

                A single combination looks like <fname>.<lname>@<email_provider>
    '''
    parser = OptionParser(usage='%prog -f fname -l lname', version='%prog 1.1')

    parser.add_option('-f', '--fname', dest='fname')
    parser.add_option('-l', '--lname', dest='lname')

    (opt, args) = parser.parse_args()

    perm = Permutate(opt.fname, opt.lname)

if __name__ == '__main__':
    compute_first_last()
#/usr/bin/python
从optpasse导入OptionParser
类置换:
电子邮件_dict={}
电子邮件提供商=(
“@gmail.com”,
@aol.com,,
“@yahoo.com”
)
定义初始化(self、fname、lname):
'''
变量:
[fname]-排列的第一个名称
[lname]-置换的姓氏
[电子邮件组合]-姓名和姓氏
'''
断言isinstance(fname,str)和isinstance(lname,str),“只能提供字符串作为名称”
self.fname=fname
self.lname=lname
self.email_composition=fname+“+lname
对于Permutate.email\u提供程序中的提供程序:
打印“%s%s%”(self.email\u组合,提供程序)
def compute_first_last():
'''
先计算后计算():
电子邮件组合=++
计算名字和姓氏的组合以及电子邮件提供商。
当值*为u时,循环遍历first+last+email_provider的所有组合
一个单一的组合看起来像@
'''
parser=OptionParser(用法=“%prog-f fname-l lname”,版本=“%prog 1.1”)
parser.add_选项('-f','-fname',dest='fname'))
parser.add_选项('-l','-lname',dest='lname'))
(opt,args)=parser.parse_args()
perm=Permutate(opt.fname,opt.lname)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
首先计算最后一次()

嗯,这里发生了什么:
self.email\u composition=email\u composition
?注意上面的单词:建设性。这不是批评,而是一个真正的问题。构造函数中的语句永远不会解析。请注意通过回答解释我应该如何进行解析。如前所述,这是我的第一个python类。作为一个小建议,请在方法声明之后直接包含所有docstring,所以在它下面而不是上面。请看这里:你能多说几句话吗?非常感谢你的回答。所谓输入卫生,我的意思是确保参数是您所期望的,并且在提供时不会导致错误。例如,python testmeagain.py-f firstname_here-l lastname here有一个包含下划线的名字。取决于
#!/usr/bin/python

from optparse import OptionParser

class Permutate:

    email_dict = {}
    email_provider = (
        '@gmail.com',
        '@aol.com',
        '@yahoo.com'
    )

    def __init__(self, fname, lname):
        '''
            vars:
            [fname] - first name of the permutation
            [lname] - last name of the permutation
            [email_combination] - first and last name
        '''
        assert isinstance(fname, str) and isinstance(lname, str), "Only strings may be supplied as names"

        self.fname = fname
        self.lname = lname
        self.email_combination = fname + "." + lname

        for provider in Permutate.email_provider:
            print "%s%s" % (self.email_combination, provider)

def compute_first_last():
    '''
        compute_first_last():
            email_combination = <string> + <string> + <string>
                computes the first name and last name combination plus and email provider.

                Loop through all combinations of first + last + email_provider while the value *is u

                A single combination looks like <fname>.<lname>@<email_provider>
    '''
    parser = OptionParser(usage='%prog -f fname -l lname', version='%prog 1.1')

    parser.add_option('-f', '--fname', dest='fname')
    parser.add_option('-l', '--lname', dest='lname')

    (opt, args) = parser.parse_args()

    perm = Permutate(opt.fname, opt.lname)

if __name__ == '__main__':
    compute_first_last()