0

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

                                         ^                                             
4
  • Can you post the whole stacktrace and python version? It parses correctly here. Commented Aug 2, 2010 at 6:41
  • Doesn't give me a syntax error. What version of python are you running? What version fo python are they running? Commented Aug 2, 2010 at 6:42
  • Works fine for me in both 3.1 and 2.6 Commented Aug 2, 2010 at 6:43
  • 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 Commented Aug 2, 2010 at 6:44

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.