从另一个返回非

2024-05-14 17:13:16 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我有两门课:

PMB消息和PPConsume

PPConsume实例化PMBMessage,然后从PMBMessage调用函数以获取返回的字符串。以下是来自PPConsume的代码:

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类:

^{pr2}$

好的,下面是这些函数运行时的输出结果-错误是它返回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

请告知,不胜感激。在


Tags: inpyselfidformatmessagehomeline
1条回答
网友
1楼 · 发布于 2024-05-14 17:13:16

递归调用需要再次返回其返回值,即

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

否则,只需删除返回值,在递归调用返回后,控制将到达函数的末尾,并隐式返回None。在

相关问题 更多 >

    热门问题