gpt4 book ai didi

python - 'classobj' 对象不可订阅

转载 作者:太空宇宙 更新时间:2023-11-04 01:39:31 24 4
gpt4 key购买 nike

从另一个类方法内部使用 self.methodname() 调用类方法时出现以下错误。

TypeError: 'classobj' object is not subscriptable

可能是什么问题?除此方法外,其他方法运行良好。代码细节是

class loadbalancer:
def __init__(self):
#somecode
def upload_config(self,loadbalancer_doc)
#some code
def create_loadbalancer(self,loadbalancer_doc)
self.upload_config(loadbalancer_doc)#trace back shows here i get this error

这里是 upload_config 方法:

def upload_config(self,loadbalancer_doc):
no_of_ports=len(loadbalancer_doc['frontend_ports'])
loadbalancer_config=''
for i in range(0, no_of_ports):
servers=''
for server_uuid in loadbalancer_doc['backend_servers']:
ip='192.168.1.1'
server_id=self.vms_collection.find_one({'_id':server_uuid}, {'id':1})['id']
servers=servers+'\tserver '+server_id+' '+ip+':'+loadbalancer_doc['backend_ports'][i]\
+' check port '+loadbalancer_doc['check_port']+' inter '+loadbalancer_doc['check_interval']+'\n'
loadbalancer_config=loadbalancer_config+'listen '+loadbalancer_doc['_id']+\
str(i)+'\n\tbind '+loadbalancer_doc['frontend_ip']+':'+loadbalancer_doc['frontend_ports'][i]\
+'\n\toption '+loadbalancer_doc['check_protocol']+' '+loadbalancer_doc['check_protocol_param']+'\n'+servers+'\n'

with open(LOCAL_DIR+loadbalancer_doc['_id'], 'w') as f:
f.write(loadbalancer_config)
try:
filenames=self.loadbalancer_collection.find({'destroyed_time':0})
except Exception,err:
os.remove(LOCAL_DIR+loadbalancer['_id'])
raise(err)
if not(filenames):
os.remove(LOCAL_DIR+loadbalancer['_id'])
raise Exception('no cursor returned')
configfiles=''
for file in filenames:
configfiles=configfiles+' -f '+REMOTE_CONFIG_FILE+file['_id']

try:
self._put_file(LOCAL_DIR+loadbalancer_doc['_id'],REMOTE_CONFIG_FILE+loadbalancer_doc['_id'])
except Exception,err:
os.remove(LOCAL_DIR+loadbalancer['_id'])
raise Exception('copying config file to remote server failed'+str(err))

try:
self._exec_command('haproxy'+\
' -f /etc/haproxy/haproxy.cfg'+configfiles+\
' -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)')
except Exception,err:
os.remove(LOCAL_DIR+loadbalancer['_id'])
#command_op=commands.getstatusoutput('ssh -i '+SSH_KEYFILE+' '+SSH_LOGIN+' rm '+REMOTE_CONFIG_FILE+loadbalancer_doc['_id'])
raise Exception('haproxy restart failed, err: '+str(err))

最佳答案

你可能有这样的东西:

balancer = loadbalancer
balancer.create_loadbalancer(...)

与 C++ 不同,创建对象时需要括号,即使 __init__ 不带任何参数:

balancer = loadbalancer()
balancer.create_loadbalancer(...)

在前一种情况下,balancer 是类对象,而不是类的对象。

编辑:

这些行可能会导致问题:

    except Exception,err:
os.remove(LOCAL_DIR+loadbalancer['_id'])
raise(err)
if not(filenames):
os.remove(LOCAL_DIR+loadbalancer['_id'])
raise Exception('no cursor returned')

你想用 loadbalancer['_id'] 做什么?

关于python - 'classobj' 对象不可订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6563448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com