Movatterモバイル変換


[0]ホーム

URL:


Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker DeckSpeaker Deck
Speaker Deck

Porting Django apps to Python 3

Avatar for Jacob Kaplan-Moss Jacob Kaplan-Moss
July 04, 2013

Porting Django apps to Python 3

Presented at DjangoCon AU 2013.

Avatar for Jacob Kaplan-Moss

Jacob Kaplan-Moss

July 04, 2013
Tweet

More Decks by Jacob Kaplan-Moss

See All by Jacob Kaplan-Moss

Other Decks in Technology

See All in Technology

Featured

See All Featured

Transcript

  1. Porting Django apps to Python 3 Jacob Kaplan-Moss[email protected]

  2. “Django 1.5 is… the first release with Python 3 support!

    ... Everything’s in place for you to start porting your apps to Python 3.” — http://django.me/1.5 “As of Django 1.6, Python 3 support is considered stable and you can safely use it in production. — http://django.me/faq
  3. Do I want to use Python 3?

  4. ˑYes!

  5. Python 3: now with 30% fewer warts!

  6. ˑ Unicode no longer sucks!

  7. Can I use Python 3?

  8. ✓ SQLite ✓ PostgreSQL ✘ MySQL* ? Oracle

  9. ✓ modwsgi ✓ uWSGI ✓ gunicorn: sync ? gunicorn: async

  10. ✓ South ✓ Celery ✓ Raven (Sentry) ✓ django-extensions ✘

    django-compressor ✘ django-social-auth ✘ django-tagging ✘ django-debug-toolbar ✘ django-registration ✘ Haystack
  11. ✓ South ✓ Celery ✓ Raven (Sentry) ✓ django-extensions ✓

    django-pipeline ✓ django-allauth ✓ django-taggit ✘ django-debug-toolbar ✘ django-registration ✘ Haystack
  12. Should I use Python 3?

  13. Options 1. Python 3 only. 2. Translated source (2to3). 3.

    Single codebase.
  14. Python 3 only?

  15. 2to3

  16. ˑ Single source wins!

  17. HOWTO

  18. 1. Choose an approach

  19. 2. Evaluate dependencies

  20. 3. Get the test suite running

  21. 4. Fix unicode handling

  22. 5. Iterate on test failures

  23. Case study: a new website

  24. 1. Choose an approach Single source: Python 3.3 only.

  25. 2. Evaluate dependencies • Light CMS capabilities. • Moderately complex

    user / permissions / authentication system. • Heavy integration with social networks. • Moderate traffic with extreme “spikes.”
  26. 3. Get the test suite running ✓ django-discover-runner

  27. 4. Fix unicode handling django.utils.encoding

  28. ˑIt works! ... but costs ~20% more.

  29. Case study: django-sitetree Ported by Jeff Triplett (@webology) https://github.com/idlesign/django-sitetree/commit/c7475

  30. 1. Choose an approach Shared source: Python 2.6+, 3.3; Django

    1.4+
  31. 2. Evaluate dependencies None (whew).

  32. 3. Get the test suite running Tox: http://tox.readthedocs.org/

  33. Tox [tox] envlist  -­‐  py27-­‐django14,  py33-­‐django15 [py27-­‐django14] basepython  =  python2.7

    deps  =  Django==1.4 [py33-­‐django15] basepython  =  python33 deps  =  Django==1.5
  34. Syntax changes print  "foo"   print("foo") except  Exception,  ex:  

    except  Exception  as  ex: raise  Exception,  "msg"        raise  Exception("message”) class  C:                                    class  C(metaclass=M)      __metaclass__  =  M More: http://docs.python.org/3.0/whatsnew/3.0.html 2 3
  35. 4. Fix unicode handling django.utils.encoding

  36. Models and unicode class  M(models.Model):   class  M(models.Model):    

       def  __unicode__(self):            def  __str__(self):                return  self.name                  return  self.name @python_2_unicode_compat class  M(models.Model):        def  __str__(self):                return  self.name 2 3
  37. 5. Iterate on test failures Six: http://pythonhosted.org/six/ (also django.utils.six)

  38. Six class  C:              

                         class  C(metaclass=M)      __metaclass__  =  M class  C(six.with_metaclass(M)): 2 3
  39. Six isinstance(s,  str)   isinstance(s,  bytes) isinstance(s,  unicode)   isinstance(s,

     str) isinstance(s,  six.binary_type) isinstance(s,  six.text_type) 2 3
  40. Six if  six.PY3: ...

  41. More info: django.me/py3

  42. ˑ We’ve done our job…

  43. ˑ We’ve done our job… now it’s your turn!

  44. Thanks! Jacob Kaplan-Moss[email protected]


[8]ページ先頭

©2009-2025 Movatter.jp