2011-07-03 86 views
1

從另一個類方法內部使用self.methodname()調用類方法時出現以下錯誤。'classobj'對象不是可訂閱的

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)) 
+2

聽起來好像你是在某個地方傳遞一個類而不是一個對象,因爲類通常不實現'__getitem__'方法。但是如果沒有你擴展你的問題,那實在是在猜測。 – phant0m

+1

請在'loadbalancer'的實例上實際調用'create_loadbalancer'的代碼。 – MAK

+0

我沒有看到在那裏調用'create_loadbalancer'。請發佈異常追溯。 –

回答

3

你可能有這樣的事情:

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']辦?

+0

os.remove(LOCAL_DIR + loadbalancer ['_ id']) 應該刪除名稱由字符串生成的文件(LOCAL_DIR + loadbalancer ['_ id']) – shreyas

+0

我知道該行的其餘部分是幹什麼的,但是你是什麼期望通過嘗試從類對象('loadbalancer')中獲得'['_id']'?在其他地方,你使用'loadbalancer_doc ['_ id']',這很好,因爲'loadbalancer_doc'是一個'dict'。 –

+0

確定它應該是loadbalancer_doc,而不是;非常感謝您的幫助......是的問題解決了 – shreyas

相關問題