简体   繁体   English

Python:尽管明显存在但未看到功能

[英]Python: Function not seen despite clearly being there

Below code has a call to a method called lago .下面的代码调用了一个名为lago的方法。

#!/usr/bin/env python
#
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
#

import sys

class InstallTest():
    """Ru Ovirt System Tests"""


    def run_command_checking_exit_code(command):
        """ Runs a command"""
        print("Command is " + str(command))
        print(command.read())
        print(command.close())

    def lago():
        """run the conductor profiles required to install OLVM """
        Log.test_objective('TODO')
        run_command_checking_exit_code('ls -al')
        """
        yum_list = ["epel-release", "centos-release-qemu-ev", "python-devel", "libvirt", "libvirt-devel", "libguestfs-tools", "libguestfs-devel", "gcc", "libffi-devel", "openssl-devel", "qemu-kvm-ev"]
        for yum in yum_list

        ret, msg, tup = self.client.run('/qa/conductor/tests/' + OSSE_OLV_VERSION + '/installer/installerfactory.py -s ' + OSSE_OLV_ENGINE_HOST + ' -t OS_OL7U6_X86_64_PVHVM_30GB -c 10.1.0.10 -o ' + self.log_jobdir_cc +'/vm_install_ol7.6', timeout=1000000)
        if ret:
            self.tc_fail('Creation of OLV Engine VM failed')
        ret, msg, tup = self.client.run('/qa/conductor/tests/' + OSSE_OLV_VERSION + '/installer/installerfactory.py -s ' + OSSE_OLV_ENGINE_HOST +' -p ovirt-engine -c 10.1.0.10 -o ' + self.log_jobdir_cc + '/engine_deploy', timeout=1000000)
        if ret:
            self.tc_fail('Install of OLV Engine Host failed')
        self.tc_pass('OLV Engine Host installed')
        """

    def main():
        lago()

    main()

However, it is shown to not exist in the output但是,它显示在输出中不存在

Traceback (most recent call last):
  File "C:/Users/rafranci/Downloads/ovirt_st_setup.py", line 20, in <module>
    class InstallTest():
  File "C:/Users/rafranci/Downloads/ovirt_st_setup.py", line 65, in InstallTest
    main()
  File "C:/Users/rafranci/Downloads/ovirt_st_setup.py", line 63, in main
    lago()
NameError: name 'lago' is not defined

As far as I can see, there is no reason for this - ideas?据我所知,没有任何理由 - 想法?

You have to instantiate class to call its method.您必须实例化类才能调用其方法。

def main():
   InstallTest().lago()

or或者

def main():
   install_test = InstallTest()
   install_test.lago()

This will work only if you are adding self parameter in your class.仅当您在类中添加self参数时,这才有效。

def lago(self):
    """run the conductor profiles required to install OLVM """
    Log.test_objective('TODO')
    run_command_checking_exit_code('ls -al')

Also, I don't understand why you needed class for this.另外,我不明白您为什么需要为此上课。 If you don't have any reason you can just remove the class and then your previous code will work fine as suggested in the comments.如果您没有任何理由,您可以删除该类,然后您之前的代码将按照评论中的建议正常工作。

Check the changes in code:检查代码中的更改:

class InstallTest():
    """Ru Ovirt System Tests"""


    def run_command_checking_exit_code(command):
        """ Runs a command"""
        print("Command is " + str(command))
        print(command.read())
        print(command.close())

    def lago(self):
        """run the conductor profiles required to install OLVM """
        #Log.test_objective('TODO')
        #run_command_checking_exit_code('ls -al')
        """
        yum_list = ["epel-release", "centos-release-qemu-ev", "python-devel", "libvirt", "libvirt-devel", "libguestfs-tools", "libguestfs-devel", "gcc", "libffi-devel", "openssl-devel", "qemu-kvm-ev"]
        for yum in yum_list

        ret, msg, tup = self.client.run('/qa/conductor/tests/' + OSSE_OLV_VERSION + '/installer/installerfactory.py -s ' + OSSE_OLV_ENGINE_HOST + ' -t OS_OL7U6_X86_64_PVHVM_30GB -c 10.1.0.10 -o ' + self.log_jobdir_cc +'/vm_install_ol7.6', timeout=1000000)
        if ret:
            self.tc_fail('Creation of OLV Engine VM failed')
        ret, msg, tup = self.client.run('/qa/conductor/tests/' + OSSE_OLV_VERSION + '/installer/installerfactory.py -s ' + OSSE_OLV_ENGINE_HOST +' -p ovirt-engine -c 10.1.0.10 -o ' + self.log_jobdir_cc + '/engine_deploy', timeout=1000000)
        if ret:
            self.tc_fail('Install of OLV Engine Host failed')
        self.tc_pass('OLV Engine Host installed')
        """

    def main(self):
        self.lago()

    def __init__(self):
        self.main()
InstallTest()

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

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