By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,271 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,271 IT Pros & Developers. It's quick & easy.

inline comparison

P: n/a
sam
Hi,

I got the the following syntax error in comparison:
File "/usr/local/work/myparser.py", line 85
if ( (m=self.macro_parser.match (d)) != None ):
^
SyntaxError: invalid syntax

How can I get around wtih this? I don't want to break down this
comparison in two steps.

Thanks
Sam
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
sam wrote:
I got the the following syntax error in comparison:
File "/usr/local/work/myparser.py", line 85
if ( (m=self.macro_parser.match (d)) != None ):
^
SyntaxError: invalid syntax

How can I get around wtih this?
Break the comparison into two steps.
I don't want to break down this comparison in two steps.


Why not? Assignments are statements in Python, and
you can't have a statement inside an expression like
you are trying to do. C can do it, and many other
languages, but not all, and not Python. There are
alternatives involving creating your own class, but
why bother? Just split the code...

-Peter
Jul 18 '05 #2

P: n/a
sam <sa*****@authtec.com> wrote:
Hi,

I got the the following syntax error in comparison:
File "/usr/local/work/myparser.py", line 85
if ( (m=self.macro_parser.match (d)) != None ):
^
SyntaxError: invalid syntax

How can I get around wtih this? I don't want to break down this
comparison in two steps.


Sorry, you have to. Assignment statements are not expressions in Python.

m = self.macro_parser.match(d)
if m:
xxx

You know the outer parentheses are not necessary, right?
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #3

P: n/a
sam
Tim Roberts wrote:
sam <sa*****@authtec.com> wrote:

Hi,

I got the the following syntax error in comparison:
File "/usr/local/work/myparser.py", line 85
if ( (m=self.macro_parser.match (d)) != None ):
^
SyntaxError: invalid syntax

How can I get around wtih this? I don't want to break down this
comparison in two steps.

Sorry, you have to. Assignment statements are not expressions in Python.

m = self.macro_parser.match(d)
if m:
xxx

This is very bad to me, I will need to write some cumbersome syntax as
follow:
for d in self.data:
m = self.macro_parser.match (d)) != None ):
if (m == None):
m_nat = self.a_parser.match (d)
if (m_a == None):
m_rdr = self.b_parser.match (d)
if (m_b == None):
m_c = self.c_parser.match (d)
if (m_c == None):
m_d = self.d_parser.match (d)
if (m_d == None):
m_e = self.e_parser.match (d)
if (m_e == None):
.....

You know the outer parentheses are not necessary, right?


Jul 18 '05 #4

P: n/a
> This is very bad to me, I will need to write some cumbersome syntax as
follow:
for d in self.data:
m = self.macro_parser.match (d)) != None ):
if (m == None):
m_nat = self.a_parser.match (d)
if (m_a == None):
m_rdr = self.b_parser.match (d)
if (m_b == None):
m_c = self.c_parser.match (d)
if (m_c == None):
m_d = self.d_parser.match (d)
if (m_d == None):
m_e = self.e_parser.match (d)
if (m_e == None):
.....

This has been discussed a zillion times on c.l.py - and various solutions
have been suggested, amongst these is something like this:

class Matcher:
def __init__(self, rex):
self.rex = rex

def match(self, data):
self.mo = self.rex.match(data)
return self.mo

Then you can use code like this:

m = Matcher(...)

if m.match("some string"):
print m.mo.groups()

And beside that: the code you gave above does _not_ look better if you could
inline the assignment, you still have the same hierarchy of if-statements.

If you really need that sort of dependent matching, there are better ways to
accomplish that in python:

for m, name in [self.macro_parser, self.a_parser, self.b_parser, ...]:
mo = m.match(data)
if mo:
break

--
Regards,

Diez B. Roggisch
Jul 18 '05 #5

P: n/a
sam
Diez B. Roggisch wrote:

If you really need that sort of dependent matching, there are better ways to
accomplish that in python:

for m, name in [self.macro_parser, self.a_parser, self.b_parser, ...]:
mo = m.match(data)
if mo:
break
Hi, this method looks more elegant.
However I got the following error:
for m, name in [self.macro_parser]
^
SyntaxError: invalid syntax

What is the valid syntax with this usage?

Thanks
Sam

Jul 18 '05 #6

P: n/a
sam wrote:
Diez B. Roggisch wrote:

If you really need that sort of dependent matching, there are better
ways to
accomplish that in python:

for m, name in [self.macro_parser, self.a_parser, self.b_parser, ...]:
mo = m.match(data)
if mo:
break

Hi, this method looks more elegant.
However I got the following error:
for m, name in [self.macro_parser]
^
SyntaxError: invalid syntax

What is the valid syntax with this usage?

Thanks
Sam

You've missed the colon off after the "for" statement. I do it all the
time ...

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.