Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hashlib / md5. Compatibility with python 2.4

python 2.6 reports that the md5 module is obsolete and hashlib should be used. If I change import md5 to import hashlib I will solve for python 2.5 and python 2.6, but not for python 2.4, which has no hashlib module (leading to a ImportError, which I can catch).

Now, to fix it, I could do a try/catch, and define a getMd5() function so that a proper one gets defined according to the result of the try block. Is this solution ok?

How would you solve this issue in a more general case, like, for example: you have two different libraries with the same objective but different interface, and you want to use one, but fall back and use the other if the first one is not found.

like image 728
Stefano Borini Avatar asked Sep 14 '09 20:09

Stefano Borini


1 Answers

In general the following construct is just fine:

try:
    import module
except ImportError: 
    # Do something else.

In your particular case, perhaps:

try: 
   from hashlib import md5
except ImportError:
   from md5 import md5
like image 101
Kenan Banks Avatar answered Oct 05 '22 23:10

Kenan Banks