Created
February 5, 2013 19:08
-
-
Save willtownes/4716794 to your computer and use it in GitHub Desktop.
Checklist for making python 2.7 scripts run in python 2.4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''I recently had to deploy a test script I had written in python 2.7 to a server where only python 2.4 was allowed. In the process of making my code backwards compatible, I made a brief checklist of things to watch out for: | |
* any and all functions can still be used but you have to manually code them | |
* with-as blocks must be replaced with try-finally | |
* try-except-finally must be converted to try-except-else | |
* string formatting: "".format() must be replaced with "%s"%(args) approach. | |
* Replace all instances of "except Exception as err:" with "except Exception, e:" | |
* may need to manually convert some unicode strings into str, especially when loading json | |
* Some modules have different names, eg: "email.MIMEText" versus "email.mime.text.MIMEText" | |
* The datetime module does not have strptime() in 2.4. However, you can import it with a different name and then make a subclass with the desired behavior by calling strptime() from the time module instead:''' | |
from datetime import datetime as datetime_base | |
if not hasattr(datetime_base,'strptime'): | |
import time | |
class datetime(datetime_base): | |
@classmethod | |
def strptime(self,date_string,format): | |
return datetime_base(*(time.strptime(date_string, format)[0:6])) | |
else: datetime = datetime_base |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment