[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Pyrhon2.5 to 2.4 conversion

tarekamr@gmail.com

2/28/2010 1:41:00 AM

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/_...)
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
6 Answers

Dennis Lee Bieber

2/28/2010 1:58:00 AM

0

On Sat, 27 Feb 2010 17:41:09 -0800 (PST), "tarekamr@gmail.com"
<tarekamr@gmail.com> declaimed the following in
gmane.comp.python.general:

>
> 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.

--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/

Chris Rebert

2/28/2010 1:59:00 AM

0

On Sat, Feb 27, 2010 at 5:41 PM, tarekamr@gmail.com <tarekamr@gmail.com> wrote:
> 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/_...)
> 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-e...

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
--
http://blog.re...

Steven D'Aprano

2/28/2010 2:03:00 AM

0

On Sat, 27 Feb 2010 17:41:09 -0800, tarekamr@gmail.com wrote:

> 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/mast...
__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.



--
Steven

MRAB

2/28/2010 2:12:00 AM

0

tarekamr@gmail.com wrote:
> 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/_...)
> 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))

Dennis Lee Bieber

2/28/2010 4:08:00 AM

0

On Sat, 27 Feb 2010 17:58:04 -0800, Dennis Lee Bieber
<wlfraed@ix.netcom.com> declaimed the following in
gmane.comp.python.general:

> 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...
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/

tarekamr@gmail.com

3/9/2010 8:31:00 PM

0

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

On Feb 28, 4:12 am, MRAB <pyt...@mrabarnett.plus.com> wrote:
> tarek...@gmail.com wrote:
> > 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 inhttp://github.com/simplegeo/python-oauth2/blob/master/oauth2/_...)
> > 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))