inline comparison

S

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
 
P

Peter Hansen

sam said:
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
 
T

Tim Roberts

sam said:
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?
 
S

sam

Tim said:
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):
.....
 
D

Diez B. Roggisch

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
 
S

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
 
S

Steve Holden

sam said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,142
Members
47,775
Latest member
MadgeMatti

Latest Threads

Top