Python class function being called from another class function returning None
OK I have two classes:
PMBMessage and PPConsume
PPConsume instanciated PMBMessage and then calls a function from PMBMessage to get a string returned. Here is the code from 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
I commented where the error occurs, here is the PMBMessage class:
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 and here is what outputs when these functions run - the error is from it returning None instead of the string that you see in the output it actually gets, but between one class to another it becomes 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
Please advise, it is much appreciated.
The recursive call needs to return its return value again, i.e.
...
print "HEY WE WENT THIS WAY"
return self.get_header_attribute(attr)
Otherwise the return value is simply dropped, and after the recursive call has returned, control reaches the end of the function and None
is implicitly returned.
精彩评论