Python code for 2.5 and 2.4?

J

Joseph Turian

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
 
M

Mike Driscoll

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-funcs.html 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
 
R

Robert Kern

Joseph said:
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
 
A

Arnaud Delobelle

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
 
M

Mike Driscoll

Joseph,

Seehttp://docs.python.org/lib/built-in-funcs.htmlwhich 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
 
J

Joseph Turian

Joseph said:
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!
 
P

Paul Rubin

Arnaud Delobelle said:
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)
 
A

Arnaud Delobelle

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

Paul Rubin

Arnaud Delobelle said:
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.
 

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
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top