2011-06-08 40 views
0

好,我有兩個類調用。下面是從PPConsume代碼:我評論Python類的功能正在從另一個類的函數返回無

def handle(self, ch, delivery_tag, body): 
    print "Got: " + body 
    pmbmessage = PMBMessage(body) 
    msg_format_id = pmbmessage.get_header_attribute("msg_format_id") 
    print "back from the function" 
    print msg_format_id 
    if self.check_message(msg_format_id) == True: 
     os.system('./send_job.sh ' + '\'' + body + '\'') 
     print "success" 
    else: 
     print "fail" 
def check_message(self, msg_format): 
      #my error happens here 
    if msg_format[0:3] != self.cfg.family: 
     return False 
    if msg_format[6:12] != self.cfg.index: 
     return False 
    return True 

發生錯誤,這裏是PMBMessage類:

def get_header_attribute(self, attr): 
    print "========================" 
    print self.header 
    print "========================" 
    if self.header != {}: 
     if attr in self.header: 
      print str(self.header[attr]) 
      return str(self.header[attr]) 
     else: 
      return False 
    else: 
     self.parse_header() 
     print "HEY WE WENT THIS WAY" 
     self.get_header_attribute(attr) 

OK,這裏是什麼輸出時,這些功能運行 - 錯誤是從它返回None而不是您在輸出中看到的字符串,但它在一個類與另一個類之間變爲None:

Got:PMB01.00.0000THISISTHEFROMSYSTEM_THISISTHETOSYSTEM__STUFF________1111111111PP_IHI0000010100____messageuniqueid__messageuniqueid04/20/2011 12:0000:00:0000JOPP_IHI0000010100____messageuniqueid__messageuniqueid________________________________________/home/vcard/positivepay/meta/outbound/ppmbseops_7_vpa1_vpa1_clchk_20110527|simplecheck|path_register|2 
======================== 
{} 
======================== 
HEY WE WENT THIS WAY 
======================== 
{u'to_system': 'THISISTHETOSYSTEM___', u'priority': '1111111111', u'original_unique_id': 'messageuniqueid__messageuniqueid', u'family': 'STUFF________', u'created': '04/20/2011 12:0000:00:0000', u'msg_format_id': 'PP_IHI0000010100____', u'hop_count': 'JO', u'msg_unique_id': 'messageuniqueid__messageuniqueid', u'message_data': '/home/stuff/stuff/stuff/outbound/filename|simple|path_register|2', u'padding': '________________________________________', u'original_msg_format_id': 'PP_IHI0000010100____', u'version': '01.00.0000', u'from_system': 'THISISTHEFROMSYSTEM_', u'message_type': 'PMB'} 
======================== 
PP_IHI0000010100____ 
back from the function 
None 
Traceback (most recent call last): 
    File "consume_message.py", line 4, in ? 
    consume.consume() 
    File "/home/khouser/Listener/PPCheckConsume.py", line 18, in consume 
    channel.wait() 
    File "/usr/lib/python2.4/site-packages/amqplib/client_0_8/abstract_channel.py", line 82, in wait 
    return amqp_method(self, args, content) 
    File "/usr/lib/python2.4/site-packages/amqplib/client_0_8/channel.py", line 1978, in _basic_deliver 
    func(msg) 
    File "/home/khouser/Listener/PPConsume.py", line 21, in handle_pyamqplib_delivery 
    self.handle(msg.delivery_info["channel"], msg.delivery_info["delivery_tag"], msg.body) 
    File "/home/khouser/Listener/PPConsume.py", line 29, in handle 
    if self.check_message(msg_format_id) == True: 
    File "/home/khouser/Listener/PPConsume.py", line 36, in check_message 
    if msg_format[0:3] != self.cfg.family: 
TypeError: unsubscriptable object 

請注意,非常感謝。

+0

你能打印msg_format_id嗎?它輸出什麼? – Blender 2011-06-08 01:19:40

+0

另外,在Python中檢查「if something == True」是多餘的。只要做'如果有事'。 – Blender 2011-06-08 01:20:02

回答

3

遞歸調用需要再次返回它的返回值,即

... 
print "HEY WE WENT THIS WAY" 
return self.get_header_attribute(attr) 

否則,返回值是簡單地下降,遞歸調用返回之後,控制到達函數的結尾和None是含蓄回。

+0

*燈泡*非常感謝! – KacieHouser 2011-06-08 03:27:09

相關問題