简体   繁体   English

无法构造python类

[英]Unable to construct python class

I have the following functions, that are working within the boundaries on the Python script 我有以下函数,这些函数在Python脚本的边界内运行

The loop_vcenters function will return a dictinary with cluster: host subs_per_socket function will return the number of sockets for esx loop_vcenters函数将返回带有cluster: host subs_per_socket的字典cluster: host subs_per_socket函数将返回esx的套接字数

def loop_vcenters(vcenters):
    #------------------------- connect to vcenter ----------------------------------------
    si = SmartConnect(host = vcenters,user = 'username',pwd = 'password' ,sslContext=context)
    # disconnect from vcenter one done
    atexit.register(Disconnect, si)
    #get the object content from vcenter
    content = si.RetrieveContent()
    #list clusters
    cluster_host_dic_list=[]
    cluster_name = ""
    for cluster_obj in get_obj(content, vim.ComputeResource):
        cluster=cluster_obj.name
        hosts=[]
        for host in cluster_obj.host:
            hosts.append(host.name)
        #create dictinary ,key=cluster value=esx
        cluster_dic={cluster:hosts}
        cluster_host_dic_list.append(cluster_dic)
    return cluster_host_dic_list

def subs_per_socket(host):
    shost = content.searchIndex.FindByDnsName(dnsName=host, vmSearch=False)
    socket_count = shost.summary.hardware.numCpuPkgs
    sub_per_socket = socket_count/2
    return sub_per_socket

I want to put both functions into a class, but I cannot figure out how 我想将两个函数都放在一个类中,但是我不知道如何

class loop_vcenters:    
    def hosts_dic(self,name):
        si = SmartConnect(host = vcenters,user = 'username',pwd = 'password' ,sslContext=context)
        atexit.register(Disconnect, si)
        content = si.RetrieveContent()
        cluster_host_dic_list=[]
        cluster_name = ""
        for cluster_obj in get_obj(content, vim.ComputeResource):
            cluster=cluster_obj.name
            hosts=[]
            for host in cluster_obj.host:
                hosts.append(host.name)
            cluster_dic={cluster:hosts}
            cluster_host_dic_list.append(cluster_dic)
      return cluster_host_dic_list
  1. I am unable to get the host dictionary like the loop_vcenters function returned. 我无法获得返回loop_vcenters函数的主机字典。
d = loop_vcenters('vcenters')
Traceback (most recent call last):
File "", line 1, in 
  File "", line 5, in __init__
NameError: global name 'vcenters' is not defined
  1. How can I add the subs_per_socket(host) function to the class? 如何将subs_per_socket(host)函数添加到类中?
d = loop_vcenters('vcenters')

You are calling the class loop_vcenters with an argument when no init is defined. 当未定义init时,您正在使用参数调用类loop_vcenters。

File "", line 5, in __init__ __init__中的文件“”,第5行

NameError: global name 'vcenters' is not defined NameError:全局名称'vcenters'未定义

If you want to pass the argument to host_dic, you should be calling 如果要将参数传递给host_dic,则应调用

d = loop_vcenters.host_dic('vcenters')

which will return cluster_host_dic_list to the variable d. 这将把cluster_host_dic_list返回到变量d。

To add subs_per_socket(host), just define it under the class just as you did the other function. 要添加subs_per_socket(host),只需在类下定义它即可,就像您执行其他功能一样。

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

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