Porting code to Python 3
http://slid.es/edwardliaw/python-six
https://github.com/edliaw/iepug_six
Edward Liaw
2014-04-15
What we'll talk about today
Differences between Python 3.4 and Python 2.7.x
A bit more about Unicode
Using Six to write code that supports both
Available tools to convert to Python 3
What's changed?
Unicode / bytes
Print function
Lazy default with iterators (e.g. zip, dict.items, map)
Library rearrangement (e.g. urllib)
Integer / float division
Exceptions
Examples
# Python 2.7.6
>>> print u'\u0040'
@
>>> print '\u0040'
\u0040
>>> print xrange(10)
xrange(10)
>>> print range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Python 3.4.0
>>> print('\u0040')
@
>>> print(b'\u0040')
b'\\u0040'
>>> print(range(10))
range(0, 10)
>>> print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
A bit about Unicode
bytestrings are strings of 8-bit characters: the glyph they map to is determined by encoding.
unicode strings are strings of code points/glyphs: the raw bytes are abstracted away.
encoding takes an encoded string of glyphs and returns a bytestring.
decoding takes a bytestring and returns the glyphs respective to the encoding that they are in.
codecs provide methods to encode or decode bytestrings.
Differences in Unicode Implementation
Python 2:
bytestring default
print uses local encoding
implicit encoding/decoding
Six wraps differences
Provides alternate functions that are equivalent in the opposing six implementation.
Generally don't have to code in it: You can code in a Python 3-esque way and use scripts to backport Python 2 compatibility.
Tools
futurize script
Resources
https://docs.python.org/3/howto/pyporting.html
Python Six
By Edward Liaw
Python Six
Writing code for Python 3.x and Python 2.7.x
- 981