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

Moving to Python 3, slowly

zroger
February 21, 2016

Moving to Python 3, slowly

zroger

February 21, 2016
Tweet

Other Decks in Programming

Transcript

  1. PYTHON 3 THE NEW HOTNESS ▸ Python 3.0 - December

    3, 2008 ▸ Python 3.1 - June 27, 2009 ▸ Python 3.2 - February 20, 2011 ▸ Python 3.3 - September 29, 2012 ▸ Python 3.4 - March 16, 2014 ▸ Python 3.5 - September 13, 2015 https://flic.kr/p/cDTHfu
  2. ▸ Applications for E-commerce, retail stores, customer experience, supply chain…

    ▸ 60 engineers ▸ 300 deploys per month ▸ 1 repository
  3. ‣ (Computing) Change or cause to change from using one

    system to another. mi·grate verb |ˈmīˌgrāt|
  4. When people are pushing you to just go straight there,

    sometimes you need to say, “Wait, I need to check the map and find the quickest route.” The preparatory refactoring does that for me. Jessica Kerr PREPARATORY REFACTORING
  5. Refactoring is a disciplined technique for restructuring an existing body

    of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Martin Fowler PREPARATORY REFACTORING
  6. for each desired change, make the change easy (warning: this

    may be hard), then make the easy change Kent Beck PREPARATORY REFACTORING
  7. PREPARATORY REFACTORING THE PLAN 1. Make the change easy (this

    may be hard)
 Refactor for Python 3 compatibility 2. Make the easy change
 Use Python 3 https://flic.kr/p/otHsfA
  8. TOX Tox Workflow 1. Creates a virtualenv 2. Installs dependencies

    3. Builds and installs the application 4. Runs specified commands https://flic.kr/p/8wR7Pm
  9. TOX tox.ini without setup.py [tox] envlist = py27,py35 skipsdist =

    True [testenv] deps = -r{toxinidir}/dev-requirements.txt setenv = PYTHONPATH = {toxinidir} commands = python manage.py test
  10. DEPENDENCIES INCOMPATIBLE DEPENDENCIES ▸ Ignore it ▸ Remove it ▸

    Replace it ▸ Fix it! https://flic.kr/p/pw5UrS
  11. TEXT FUTURE STATEMENTS from __future__ import absolute_import from __future__ import

    division from __future__ import print_function from __future__ import unicode_literals
  12. SIX iteritems example # py2
 for k, v in d.iteritems():


    pass # py3
 for k, v in d.items():
 pass # six
 import six
 for k, v in six.iteritems(d):
 pass
  13. SIX metaclass example # py2
 class MyClass(object):
 __metaclass__ = MyMeta

    # py3
 class MyClass(object, metaclass=Meta):
 pass # six
 @six.add_metaclass(Meta)
 class MyClass(object):
 pass
  14. SIX moved modules example # py2
 import HTMLParser as html_parser

    # py3
 import html.parser as html_parser # six
 from six.moves import html_parser
  15. 2TO3 example-2to3.py d = { 'foo': 'bar', 'baz': 'qux', }

    for k, v in d.iteritems(): print '%s => %s' % (k, v)
  16. 2TO3 example-2to3.py $> 2to3 example-2to3.py RefactoringTool: Refactored example-2to3.py --- example-2to3.py

    (original) +++ example-2to3.py (refactored) @@ -3,6 +3,6 @@ 'baz': 'qux', } -for k, v in d.iteritems(): - print '%s => %s' % (k, v) +for k, v in d.items(): + print('%s => %s' % (k, v))
  17. 2TO3 example-2to3.py from __future__ import print_function import six d =

    { 'foo': 'bar', 'baz': 'qux', } for k, v in six.iteritems(d): print('%s => %s' % (k, v))
  18. 2TO3 example-2to3.py $> 2to3 example-2to3.py RefactoringTool: No changes to example-2to3.py

    RefactoringTool: Files that need to be modified: RefactoringTool: example-2to3.py
  19. This library is a very thin wrapper around lib2to3 to

    utilize it to make Python 2 code more modern with the intention of eventually porting it over to Python 3. python-modernize
  20. PYTHON-MODERNIZE $ python-modernize example-2to3.py RefactoringTool: Refactored example-2to3.py --- example-2to3.py (original)

    +++ example-2to3.py (refactored) @@ -1,8 +1,11 @@ +from __future__ import absolute_import +from __future__ import print_function +import six d = { 'foo': 'bar', 'baz': 'qux', } -for k, v in d.iteritems(): - print '%s => %s' % (k, v) +for k, v in six.iteritems(d): + print('%s => %s' % (k, v))
  21. TEXT IN CONCLUSION 1. Practice preparatory refactoring. 2. Preserve current

    behaviors, using Tox to test against both Python 2 & 3. 3. Evaluate your dependencies for compatibility. 4. Use __future__ and six to write forward-compatible code. 5. Use python-modernize to automate your refactoring. https://flic.kr/p/i1JK8