views:

237

answers:

3

Hi,

Trying to execute someone's code, getting a syntax error. No idea why :(

def GetParsers( self, systags ):
    childparsers = reduce( lambda a,b : a+b, [[]] + [ plugin.GetParsers( systags ) for plugin in self.plugins ] )
    parsers = [ p for plist in [ self.parsers[t] for t in systags if self.parsers.has_key(t) ] for p in plist ]
    return reduce( lambda a,b : ( a+[b] if not b in a else a ), [[]] + parsers + childparsers )

And the error is

File "base.py", line 100
    return reduce( lambda a,b : ( a+[b] if not b in a else a ), [[]] + parsers + childparsers )

Python Version

Python 2.2.3 (#1, May  1 2006, 12:33:49)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-54)] on linux2

                                         ^                                             
+5  A: 

Conditional expressions were added in 2.5 (source) - you have 2.2. So no conditional expressions for you, I fear. They just don't exist yet in that version. Definitively update (not only for this little change, there are literally thousands of them since '06) if you can.

delnan
+5  A: 

You need to upgrade your Python installation to at least 2.5. More Information

Shane Reustle
+1  A: 

Upgrading to a newer version of Python will be the best solution, but if for some reason you can't upgrade you could update the code to use the and-or trick.

So this:

>>> 'a' if 1 == 2 else 'b'
'b'

Becomes:

>>> (1 == 2) and 'a' or 'b'
'b'

There is a slight problem here in that if the value you're return for True itself evaluates to False this statement won't work as you want. You can work around this as follows:

>>> ((1 == 2) and ['a'] or ['b'])[0]
'b'

In this case because the value is a non-empty list it will never evaluate to False so the trick will always work.

Dave Webb