[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.python

Python code for 2.5 and 2.4?

Joseph Turian

2/25/2008 8:41:00 PM

I was given code that was written for python 2.5, and uses simple
functions like 'all' which are not present in 2.4

I want to make the code 2.4 compatible. What is the best way to do
this?
If I define function 'all', then won't I break 2.5 compatability?

Thanks,
Joseph
9 Answers

Mike Driscoll

2/25/2008 8:48:00 PM

0

On Feb 25, 2:40 pm, Joseph Turian <tur...@gmail.com> wrote:
> I was given code that was written for python 2.5, and uses simple
> functions like 'all' which are not present in 2.4
>
> I want to make the code 2.4 compatible. What is the best way to do
> this?
> If I define function 'all', then won't I break 2.5 compatability?
>
> Thanks,
> Joseph

See http://docs.python.org/lib/built-in-... which shows the
current built-ins for Python. It also shows an equivalent function for
all() that should work in 2.4.

You can do a check at the beginning of your file by importing the sys
module. Something like this:

<code>
# untested
import sys
version = sys.version.split(' ')[0]

if '2.5' not in version:
# use custom all() script

</code>

HTH

Mike

Robert Kern

2/25/2008 9:03:00 PM

0

Joseph Turian wrote:
> I was given code that was written for python 2.5, and uses simple
> functions like 'all' which are not present in 2.4
>
> I want to make the code 2.4 compatible. What is the best way to do
> this?

If it's a single file, put something like the following code near the top. If
you have multiple modules, put it into a separate module, say compatibility.py,
and change the other modules to import these functions from there.


import sys
if sys.version_info[:2] < (2,5):
def all(*args):
...
def any(*args):
...
else:
# Only bother with this else clause and the __all__ line if you are putting
# this in a separate file.
import __builtin__
all = __builtin__.all
any = __builtin__.any

__all__ = ['all', 'any']


> If I define function 'all', then won't I break 2.5 compatability?

No. Defining a function named the same thing as a builtin function will not
break anything. You just wouldn't be using the efficient implementation already
in Python 2.5. Using the if: else: suite above lets you have both at the expense
of some clunkiness.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Arnaud Delobelle

2/25/2008 10:03:00 PM

0

On Feb 25, 8:47 pm, Mike Driscoll <kyoso...@gmail.com> wrote:
>
> You can do a check at the beginning of your file by importing the sys
> module. Something like this:
>
> <code>
> # untested
> import sys
> version = sys.version.split(' ')[0]
>
> if '2.5' not in version:
>    # use custom all() script
>
> </code>

Or simply:

try:
all
except NameError:
def all(iterable):
for x in iterable:
if not x: return False
return True

--
Arnaud

Mike Driscoll

2/25/2008 10:08:00 PM

0

Joseph,

On 2/25/08, Joseph Turian <turian@gmail.com> wrote:
>
> > Seehttp://docs.python.org/lib/built-in-funcs... shows the
> > current built-ins for Python. It also shows an equivalent function for
> > all() that should work in 2.4.
>
> Yes, I see that.
>
> > You can do a check at the beginning of your file by importing the sys
> > module. Something like this:
> > <code>
> > # untested
> > import sys
> > version = sys.version.split(' ')[0]
> >
> > if '2.5' not in version:
> > # use custom all() script
> >
> > </code>
>
> Okay.
> I guess I was asking how do I nicely package the above into a file
> python25.py from which I can do:
> from python25 import all
> And python25 figures out the python version and returns either the
> hand-coded version or the library version.
> How can I write python25.py ?
>

Well one way to do this would be something like this:

<python25.py>

def all(iterable):
for element in iterable:
if not element:
return False
return True

</python25.py>

And combine that with the code that Robert gave you. I'm not sure of
the exact syntax, but I would think you could do an IF statement that
creates the custom definition and returns it if Python 2.4 or less is
installed and return the normal version for 2.5 if it is installed.

Mike

Joseph Turian

2/25/2008 10:46:00 PM

0

On 25 fév, 16:02, Robert Kern <robert.k...@gmail.com> wrote:
> Joseph Turian wrote:
> > I was given code that was written for python 2.5, and uses simple
> > functions like 'all' which are not present in 2.4
>
> > I want to make the code 2.4 compatible. What is the best way to do
> > this?
>
> If it's a single file, put something like the following code near the top. If
> you have multiple modules, put it into a separate module, say compatibility.py,
> and change the other modules to import these functions from there.
>
> import sys
> if sys.version_info[:2] < (2,5):
> def all(*args):
> ...
> def any(*args):
> ...
> else:
> # Only bother with this else clause and the __all__ line if you are putting
> # this in a separate file.
> import __builtin__
> all = __builtin__.all
> any = __builtin__.any
>
> __all__ = ['all', 'any']
>
> > If I define function 'all', then won't I break 2.5 compatability?
>
> No. Defining a function named the same thing as a builtin function will not
> break anything. You just wouldn't be using the efficient implementation already
> in Python 2.5. Using the if: else: suite above lets you have both at the expense
> of some clunkiness.
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless enigma
> that is made terrible by our own mad attempt to interpret it as though it had
> an underlying truth."
> -- Umberto Eco

This is what I was looking for. Thanks!

Paul Rubin

2/25/2008 11:18:00 PM

0

Arnaud Delobelle <arnodel@googlemail.com> writes:
> def all(iterable):
> for x in iterable:
> if not x: return False
> return True

from itertools import imap
def all(iterable):
return False not in imap(bool, iterable)

def any(iterable):
return True in imap(bool, iterable)

Arnaud Delobelle

2/25/2008 11:43:00 PM

0

On Feb 25, 11:17 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> Arnaud Delobelle <arno...@googlemail.com> writes:
> >     def all(iterable):
> >         for x in iterable:
> >             if not x: return False
> >         return True
>
> from itertools import imap
> def all(iterable):
>    return False not in imap(bool, iterable)

Ok. In that case, the following is probably faster (in fact for long
iterables it may be equivalent to the builtin all):

from itertools import ifilterfalse

def all(iterable):
for _ in ifilterfalse(None, iterable):
return False
return True


But my point was really about not checking for the version but just
checking for the existence of the name 'all'.

--
Arnaud

Paul Rubin

2/25/2008 11:47:00 PM

0

Arnaud Delobelle <arnodel@googlemail.com> writes:
> Ok. In that case, the following is probably faster (in fact for long
> iterables it may be equivalent to the builtin all):
>
> from itertools import ifilterfalse ...

Nice.

Robert Kern

2/26/2008 12:46:00 AM

0

Bhatt, Omkar wrote:
> PLEASE TAKE ME OFF THIS LIST!

You can unsubscribe yourself using the link at the bottom of every message:

http://mail.python.org/mailman/listinfo/p...

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco