Your SlideShare is downloading. ×
  • Like
2 × 3 = 6
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

2 × 3 = 6

  • 193 views
Published

A brief overview of six, a module of “utilities for wrapping between Python 2 and 3,” and cross-version compatibility as a whole.

A brief overview of six, a module of “utilities for wrapping between Python 2 and 3,” and cross-version compatibility as a whole.

Published in Technology , Design
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
193
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. 2×3=6
  • 2. six • Utilities for wrapping between Python 2 and 3 • Multiplication is more powerful • “Five” has already been snatched away by the Zope Five project http://pythonhosted.org/six/
  • 3. Use Case: Reflection
  • 4. Python 2 >>> text = 'Lorem ipsum'! >>> isinstance(text, str)! True
  • 5. Python 2 >>> text = u'Lorem ipsum'! >>> isinstance(text, str)! False
  • 6. Python 2 >>> text = u'Lorem ipsum'! >>> isinstance(text, str)! False! >>> isinstance(text, unicode)! True
  • 7. Python 2 >>> text1 = 'Lorem ipsum'! >>> text2 = u'Lorem ipsum'! >>> isinstance(text1, (str, unicode))! True! >>> isinstance(text2, (str, unicode))! True
  • 8. Python 2 >>> text1 = 'Lorem ipsum'! >>> text2 = u'Lorem ipsum'! >>> isinstance(text1, basestring)! True! >>> isinstance(text2, basestring)! True
  • 9. Python 3 >>> text = 'Lorem ipsum' >>> isinstance(text, str)! True # "Unicode"!
  • 10. Python 3 >>> text = u'Lorem ipsum' ! # Python 3.3+! ! >>> isinstance(text, unicode)! Traceback (most recent call last):! File "<stdin>", line 2, in <module>! NameError: name 'unicode' is not defined
  • 11. Python 3 >>> text = 'Lorem ipsum'! >>> isinstance(text, basestring)! Traceback (most recent call last):! File "<stdin>", line 2, in <module>! NameError: name 'basestring' is not defined
  • 12. six >>> import six! >>> text = 'Lorem ipsum'! >>> isinstance(text, six.string_types)! True! >>> text = u'Lorem ipsum' # Python 3.3+! >>> isinstance(text, six.string_types)! True
  • 13. six Python 2 Python 3 class_types type, types.ClassType type integer_types long, int int string_types basestring str text_type unicode str binary_type str, bytes (2.7) bytes MAXSIZE sys.maxsize (2.6+) sys.maxsize
  • 14. Use Case: Syntax Compatibility
  • 15. Python 2 >>> lv = {}! >>> exec 'x = 3, y = 4' in {}, lv! >>> with open('foo.txt', 'w') as f:! ... print >>f lv['x'], lv['y']! ...! >>>
  • 16. Python 3 >>> lv = {}! >>> exec('x = 3, y = 4', {}, lv)! >>> with open('foo.txt', 'w') as f:! ... print(lv['x'], lv['y'], file=f)! ...! >>>
  • 17. six >>> from six import exec_, print_! >>> lv = {}! >>> exec_('x = 3, y = 4', {}, lv)! >>> with open('foo.txt', 'w') as f:! ... print_(lv['x'], lv['y'], file=f)! ...! >>>
  • 18. Python 2 class MetaFoo(type):! pass! ! class Foo(object):! __metaclass__ = MetaFoo
  • 19. Python 3 class MetaFoo(type):! pass! ! class Foo(object, metaclass=MetaFoo):! pass
  • 20. six from six import add_metaclass! ! class MetaFoo(type):! pass! ! @add_metaclass(MetaFoo)! class Foo(object):! pass
  • 21. six from six import with_metaclass! ! class MetaFoo(type):! pass! ! class Foo(with_metaclass(object, MetaFoo)):! pass
  • 22. six Python 2 Python 3 exec_ exec (statement) exec (function) print_ print (statement) print (function) reraise exception re-raising (can contain previous tracebacks) with_metaclass metaclassing (creates an intermediate class) add_metaclass metaclassing (no intermediate class)
  • 23. Use Case: Texts
  • 24. Python 2 a_byte_string = 'Lorem ipsum'! still_bytestr = b'Lorem ipsum' a_unicode_obj = u'Lorem ipsum' # 2.7!
  • 25. Python 3 a_unicode_obj = 'Lorem ipsum'! still_unicode = u'Lorem ipsum' a_byte_string = b'Lorem ipsum' # 3.3+ !
  • 26. six from six import b, u! a_byte_string = b('Lorem ipsum')! a_unicode_obj = u('Lorem ipsum')
  • 27. six a_unicode_obj = u('Lorem ipsum') Be careful with this on Python 2!
  • 28. six Python 2 Python 3 b str, bytes (2.7) bytes u unicode (allows escaping) str unichr unichr chr int2byte chr bytes([integer]) byte2int ord(bytestr[0]) bytestr[0]
  • 29. six Python 2 Python 3 indexbytes(buf, i) ord(buf[i]) buf[i] iterbytes(buf) an iterator for a bytes instance buf StringIO StringIO.StringIO io.StringIO BytesIO StringIO.StringIO io.BytesIO
  • 30. Use Case: Name Changes
  • 31. Python 2 from itertools import izip! ! u = raw_input()! ! for i, v in izip(xrange(len(u)), u):! print i, v
  • 32. Python 3 u = input()! ! for i, v in zip(range(len(u)), u):! print i, v
  • 33. six from six.moves import input, zip, range! ! u = input()! ! for i, v in zip(range(len(u)), u):! print i, v
  • 34. Caveats from six.moves import ConfigParser! ! # This will fail on Python 3!! parser = ConfigParser.SafeConfigParser()
  • 35. Read the Doc (Too many to list) http://pythonhosted.org/six/#module-six.moves
  • 36. Advanced Usage • Customised renames • Object model compatibility • six.PY2 and six.PY3 (when all others fail)
  • 37. In the Meantime • pies • Supports 2.6+ only (six supports 2.4+) • Lightweight • python-future • Based on six • Let you write in pure Python 3
  • 38. Related Reading http://pythonhosted.org/six/ http://python3porting.com/differences.html http://docs.python.org/3/library/ http://docs.python.org/2/library/ http://python-future.org https://github.com/timothycrosley/pies