Pyrhon2.5 to 2.4 conversion

T

tarekamr

Hi,

I am currently using oauth2.py library, and it works fine on one of my
PC's (python2.5), but later on when I tried to use it with python2.4
the following line (line 332 in http://github.com/simplegeo/python-oauth2/blob/master/oauth2/__init__.py)
showed a syntax error

items = [(k, v if type(v) != ListType else sorted(v)) for k,v in
sorted(self.items()) if k != 'oauth_signature']

So it there a way to convert this line to a python2.4 compliant
syntax.

Thanks a lot,
Tarek
 
D

Dennis Lee Bieber

items = [(k, v if type(v) != ListType else sorted(v)) for k,v in
sorted(self.items()) if k != 'oauth_signature']

So it there a way to convert this line to a python2.4 compliant
syntax.
Without checking change notices, I'd guess you'd have to change the
generator expression "(k, v ... sorted(v))" into a list comprehension
"[k, v ... sorted(v)]"... Which /may/ result in an increase in memory
usage...

If that doesn't work, it could be the use of "sorted()"... for that
you might need to skip the existing list comprehension and use an
explicit loop with .append(), and use x.sort() on temporary copies of
the input data. (Or remove the sorted() completely; it doesn't appear to
be a requirement of that statement that data be in sorted order -- since
none of the comparisons are position relative, just inequality -- you
can sort the results later.
 
C

Chris Rebert

Hi,

I am currently using oauth2.py library, and it works fine on one of my
PC's (python2.5), but later on when I tried to use it with python2.4
the following line (line 332 in http://github.com/simplegeo/python-oauth2/blob/master/oauth2/__init__.py)
showed a syntax error

items = [(k, v if type(v) != ListType else sorted(v)) for k,v in
sorted(self.items()) if k != 'oauth_signature']

So it there a way to convert this line to a python2.4 compliant
syntax.

This part is your problem:
(k, v if type(v) != ListType else sorted(v))

Conditional expressions like that were added in v2.5:
http://docs.python.org/whatsnew/2.5.html#pep-308-conditional-expressions

Here's an untested substitute:

def sort_or_tuple(k, v):
if type(v) != ListType:
return k, v
else:
return sorted(v)

items = [sort_or_tuple(k,v) for k,v in sorted(self.items()) if k !=
'oauth_signature']

Of course, the more obvious solution is just to upgrade your Python;
it's well worth it!

Cheers,
Chris
 
S

Steven D'Aprano

Hi,

I am currently using oauth2.py library, and it works fine on one of my
PC's (python2.5), but later on when I tried to use it with python2.4 the
following line (line 332 in
http://github.com/simplegeo/python-oauth2/blob/master/oauth2/ __init__.py)
showed a syntax error

items = [(k, v if type(v) != ListType else sorted(v)) for k,v in
sorted(self.items()) if k != 'oauth_signature']

So it there a way to convert this line to a python2.4 compliant syntax.

You would be better off installing Python2.5 on the PC rather than trying
to backport the library to 2.4. There could be thousands of changes
needed to backport the library.
 
M

MRAB

Hi,

I am currently using oauth2.py library, and it works fine on one of my
PC's (python2.5), but later on when I tried to use it with python2.4
the following line (line 332 in http://github.com/simplegeo/python-oauth2/blob/master/oauth2/__init__.py)
showed a syntax error

items = [(k, v if type(v) != ListType else sorted(v)) for k,v in
sorted(self.items()) if k != 'oauth_signature']

So it there a way to convert this line to a python2.4 compliant
syntax.
I think the clearest is:

items = []
for k, v in sorted(self.items()):
if k != 'oauth_signature':
if type(v) == ListType:
v = sorted(v)
items.append((k, v))
 
D

Dennis Lee Bieber

Without checking change notices, I'd guess you'd have to change the
generator expression "(k, v ... sorted(v))" into a list comprehension
"[k, v ... sorted(v)]"... Which /may/ result in an increase in memory
usage...
Okay... 50 lashes with the wet noodle...

I jumped on what initially looked like a generator expression... not
a ternary conditional expression...
 
T

tarekamr

Thanks a lot everybody for your help, it worked now :)

I am currently using oauth2.py library, and it works fine on one of my
PC's (python2.5), but later on when I tried to use it with python2.4
the following line (line 332 inhttp://github.com/simplegeo/python-oauth2/blob/master/oauth2/__init__.py)
showed a syntax error
items = [(k, v if type(v) != ListType else sorted(v)) for k,v in
sorted(self.items()) if k != 'oauth_signature']
So it there a way to convert this line to a python2.4 compliant
syntax.

I think the clearest is:

items = []
for k, v in sorted(self.items()):
     if k != 'oauth_signature':
         if type(v) == ListType:
             v = sorted(v)
         items.append((k, v))
 

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,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top