0

Can anyone explain why I get the following syntax error when running the setup.py install:

Exception

SyntaxError: ('invalid syntax', ('build/bdist.linux-x86_64/egg/cardstream/payment.py', 46, 15, '            for key, value in parse_qs(query).items()\n'))

Code

 @classmethod
 def decode(this, query):

      """Decode a request/response from the given query string.
      """

      return {
          key: value if len(value) > 1 else value[0]
              for key, value in parse_qs(query).items()
      }

The line it refers to is the for. I'm familiar with Python, but not so much that I can figure out why it's moaning.

Edit For completeness, here is the exception when the script is run manually:

[...pythonsdk]$ python test/test_gateway.py
Traceback (most recent call last):
  File "test/test_gateway.py", line 4, in <module>
    from REMOVED.payment import Gateway
  File "/usr/lib/python2.6/site-packages/REMOVED-0.0.1-py2.6.egg/REMOVED/payment.py", line 46
    for key, value in parse_qs(query).items()
      ^
SyntaxError: invalid syntax
5
  • 2
    Old Python version without dictionary comprehensions? Commented Jun 2, 2016 at 5:26
  • Oh, maybe. Running on a box that probably hasn't been updated for some time. I'll check. Thanks. Commented Jun 2, 2016 at 5:29
  • That was it. Thank you @KlausD. Commented Jun 2, 2016 at 5:44
  • 3
    Yes, from those paths it looks like you have Python 2.6. Dictionary comprehensions were added in 2.7. Commented Jun 2, 2016 at 5:44
  • Unfortunately the live box only has 2.6, but I was able to confirm this was the case by installing 2.7.x on a windows box and running. Thanks for your help Commented Jun 2, 2016 at 5:48

2 Answers 2

1

Just convert the dict comprehension to the equivalent dict() constructor call:

return dict((
          (key, value if len(value) > 1 else value[0])
              for key, value in parse_qs(query).items()
      ))
Sign up to request clarification or add additional context in comments.

Comments

0

It doesn't like you splitting your dictionary comprehension on separate lines.

Just put it on a single line:

return {
    key: value if len(value) > 1 else value[0] for key, value in parse_qs(query).items()
}

or if you absolutely have to split use line continuation character \

return {
    key: value if len(value) > 1 else value[0] \
        for key, value in parse_qs(query).items()
}

1 Comment

This has been solved by using Python 2.7.x rather than the 2.6.6 version on the live box. But thanks for your time :)

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.