views:

92

answers:

5

I've written a small python script to create a file and calculate times. I've tested it on Fedora 10, and Ubuntu 8.x and it worked well. the python versions were 2.5.x.

I tried to run it on my production server (an old red hat based linux server), the version of python is 2.2.3. the script does not work and raises a syntax error in the class definition.

the script defines a class with methods to create files on disk an measure the time, to estimate disk write speed. it starts like this:

class TestDiskSpeed():
    def __init__(self, rounds=1, speedMode=SPEED_MODE_MEGABYTE):

the class definition is pointed as the error by python 2.2.3.

what are the major changes in python since 2.2.3 that could possibly crash my application? I'm using these modules: os, sys, time, stat, gc.

Update:

by removing the () from class definition python accepted the class. but it raises another error on this line:

size = long(size) if size != None else self._size

I'm a PHP developer, just entered python programming (maybe a month), and am very used to the ternary operation which is done in PHP like this:

$var = (condition) ? $valueIfTrue : $valueIfFalse;

I searched and found it is done in python like the one I used for my size variable. though it seems python 2.2.3 does not like it.

I was going to insert all the code in here (I'm going to release the script as LGPL anyway), but the code is more than 150 lines (still in development).

+8  A: 

I wrote a script a while ago to help answer this exact question: pyqver.

This script attempts to identify the minimum version of Python that is required to execute a particular source file.

When developing Python scripts for distribution, it is desirable to identify which minimum version of the Python interpreter is required. pyqver attempts to answer this question using a simplistic analysis of the output of the Python compiler.

Use the -v option to list the reasons why your script requires a particular minimum version. This is very helpful in deciding what to change to make your script work with a specific version of Python.

As always, feel free to fork and contribute!

Greg Hewgill
Holy crap, Greg. That's useful. I wish I could +5.
Jed Smith
thanks Greg. it is a very useful script. I've started learning python with 2.5, the same version your script is reporting as the minimum version required to run mine. :)
farzad
Awesome! Wish I had found this earlier :)
pojo
+1  A: 

That's weird, because it compiles if you remove the () in the class definition. However, the documentation says that empty parens there is ok.

Since you're using lots of python 2.5 features, it's going to be hard work to find them all. I would recommend reading the "What's new in Python" for every version between 2.2 and 2.5, then coming up with a list of features that you might be able to search for with your, such as:

  • Comprehensions and generators
  • Ternary expression
  • Decorators
  • New-style classes

Luckily, most of the new features come with a new language keyword (or a new way of using a keyword, in the case of the ternary x if p else y), so it shouldn't be hard to grep for them.

jleedev
good point, I removed the () from the class definition and python passed the line. but again I had another error on the "ternary operation" line.
farzad
+1  A: 

This page links to all the Python version docs - look for the "What's New in Python X.X" doc that Andrew Kuchling has been diligently writing for each release.

Off-hand, I would guess the problem is the empty ()'s in your class definition, but I don't see this syntax change listed in the 2.3 "What's new" doc.

I've tried to maintain backwards compatibility for my pyparsing module back to 2.3.2, but that was when list comprehensions were added, and I just can't make myself go back before that. No chance of upgrading your server?

Paul McGuire
unfortunately no. the OS and all software on it are old, and we can not even update a single thing. thanks for the link though.
farzad
+1  A: 

My money is on new-style classes, which the () on your class would imply.

Jed Smith
I thought you had to explicitly name inheritance from `(object)` to get new-style classes.
Paul McGuire
I did too, but you never know.
Jed Smith
A: 

The ternary operator is quite new. There are a few alternatives but I'd write it like this:

if size is None:
    size = self._size
else:
    size = long(size)
gnibbler