Dear all,

For those that were at the last DevForum we briefly discussed supporting both Python 2 and 3 at the same time, and I promised to send an email around with links.

First of all, it will take quite some time before all of Invenio can support Python 3, so this is just an initial kick-off to start thinking about Python 3. If we start already now writing compatible code, we'll have a considerably easier job in the future. I encourage everyone to have a look through the links to see how easy it easy to write Python 2/3 compatible code - it's well worth the investment to upgrade your Python fu now :-) It's after all more than 5 years ago Python 3 was released :-) For all new external modules like Flask-Registry, we should strive to make them Python 3 compatible from the beginning.

Writing Python 2/3 compatible source code usually means writing code against Python 2.6, 2.7 and 3.3+. Forget about 2.5- and 3.0/3.1/3.2, since they don't include the necessary syntax changes and features to easily write compatible code. An easy way to get started, is to switch your editor to use Python 3 for syntax error detection, so you'll start seeing the most common errors (e.g. if you're using Sublime 3 that will already be the case).

Some of the most easy changes are:

Print as a function:
print "some string" - > print("some string")

Try/except:
try:
    raise Exception()
except Exception, e:
    pass

-->
try:
    raise Exception()
except Exception *as* e:
    pass

New style classes only:
class SomeName -> class SomeName(object)

Using the six library for (http://pythonhosted.org/six/) compatible code:
isinstance("text", basestring) -> isinstance("text", six.string_types)

I encourage you to look through these links for easy reading on Python 3:
http://python3porting.com/differences.html
http://python3porting.com/preparing.html
http://python3porting.com/noconv.html
http://docs.pythonsprints.com/python3_porting/py-porting.html

Other good sources of information is:

http://python3porting.com/
http://pythonhosted.org/six/
http://docs.python.org/dev/howto/pyporting.html
http://docs.python.org/3/whatsnew/index.html

The trickiest part of supporting both Python 2 and 3 is definitely the strings, since most of Invenio is dealing with UTF8 byte strings instead of unicode stringss. We'll have to see how we best deal with this.

I'm no Python 3 expert myself, so I hope learn from your experiences as well.

Best regards,
Lars

--
Lars Holm Nielsen
CERN, IT Department, Collaboration & Information Services
http://zenodo.org | Tel: +41 22 76 79182 | Cel: +41 76 672 8927

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to