Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The March To 3

The March To 3

The How's and Why's of porting to Python 3.

daniellindsley

April 08, 2013
Tweet

More Decks by daniellindsley

Other Decks in Technology

Transcript

  1. The March
    To3

    View full-size slide

  2. Who?
    • Daniel Lindsley
    • Pythonista since 2003
    • Djangonaut since 2006
    • Author of Tastypie / Haystack / itty / others

    View full-size slide

  3. Your language
    is...

    View full-size slide

  4. Your language
    is...
    DEAD

    View full-size slide

  5. Your language
    is...
    DEAD
    ...for small values of “dead”.

    View full-size slide

  6. is dead;
    Long live !

    View full-size slide

  7. Why port to Python 3?

    View full-size slide

  8. Active Development
    Python 2.7.x will only get security updates.
    No Python 2.8 ever.

    View full-size slide

  9. As Fast As 2.7
    With 3.3 & it’ll only get better.

    View full-size slide

  10. Cleaned Up Syntax
    We’ll get to some of these in a bit.

    View full-size slide

  11. Better String Handling
    Unicode everywhere.

    View full-size slide

  12. Changes from 2 to 3
    •Removals
    • Backticks, classic classes
    • <>, apply, reduce, dict.has_key
    • Others

    View full-size slide

  13. Changes from 2 to 3
    •Changed
    • print is now print()
    • next() is now __next__()
    • Better raise syntax
    • Metaclasses

    View full-size slide

  14. Changes from 2 to 3
    •Changed (cont.)
    • Faster Decimal implementation
    • Reorganized & spruced up stdlib
    • Careful with I/O!

    View full-size slide

  15. Changes from 2 to 3
    •New!
    • Built-in OrderedDict
    • String formatting
    • dict / set comprehensions
    • Generator delegation via yield from

    View full-size slide

  16. Changes from 2 to 3
    •New! (cont.)
    • Built-in virtualenv support!
    • unittest2 by default!
    • World domination with importlib

    View full-size slide

  17. “What versions should
    I support?”

    View full-size slide

  18. “What versions should
    I support?”
    Simple answer: Python 2.6+ & Python 3.3+

    View full-size slide

  19. “So how do I port?”

    View full-size slide

  20. Decide between...
    • A One-time port
    • A Combined 2 & 3 codebase
    ...or...

    View full-size slide

  21. The One-time port

    View full-size slide

  22. The One-time port
    • Ensure you have good test coverage
    • Use 2to3 to convert the codebase
    • Run tests & fix until it passes

    View full-size slide

  23. The Combined
    2 & 3 codebase

    View full-size slide

  24. The Combined
    2 & 3 codebase
    • First, you need a venv:
    •virtualenv -p python3 env3
    •. env3/bin/activate

    View full-size slide

  25. The Combined
    2 & 3 codebase
    • Ensure you have good test coverage
    • Require six as a dependency
    • Use six to patch over the differences
    • Run tests in Py3 & fix until it passes...
    • ...then run tests in Py2 & fix until it passes!

    View full-size slide

  26. Using six in practice

    View full-size slide

  27. More information
    • http://pyvideo.org/video/1730/python-33-
    trust-me-its-better-than-27
    • http://pyvideo.org/video/1787/porting-
    django-apps-to-python-3
    • http://pyvideo.org/video/1704/why-you-
    should-use-python-3-for-text-processing
    • http://pythonhosted.org/six/

    View full-size slide

  28. More information
    • http://www.dabeaz.com/python3io/
    MasteringIO.pdf

    View full-size slide

  29. Thanks!
    @daniellindsley
    https://github.com/toastdriven

    View full-size slide