Re: [Pyparsing] tokens.has_key('abc') throws exception
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2011-07-02 22:46:06
|
No tokens is not a dict, it is a ParseResults, but I want it to look very much like a dict. Still, has_key() is a deprecated call on dict, and the preferred form is: if 'new_field_name' in tokens: ParseResults will support this method. In general, has_key() should be discarded now, in favor of "in". -- Paul -----Original Message----- From: cathal coffey [mailto:cof...@gm...] Sent: Saturday, July 02, 2011 5:19 PM To: pyp...@li... Subject: [Pyparsing] tokens.has_key('abc') throws exception Hey Guys, I am using setResultsName like this setResultsName('new_field_name') then inside my parse action I want to do something like the below. if tokens.has_key('new_field_name'): #Do something else: #Do something else However this causes an exception to be thrown. I guess tokens is not a dict even tho it appears to function like one. My not so elegant solution to avoid this problem is the below. try: new_field_name = tokens['new_field_name'] #Do something except: #Do something else I don't like this try -> except approach, does anyone have a more elegant solution? Kind regards, Cathal |