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

2 × 3 = 6

2 × 3 = 6

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

Tzu-ping Chung

January 17, 2014
Tweet

More Decks by Tzu-ping Chung

Other Decks in Programming

Transcript

  1. 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/
  2. >>> text = u'Lorem ipsum'! >>> isinstance(text, str)! False! >>>

    isinstance(text, unicode)! True Python 2
  3. >>> text1 = 'Lorem ipsum'! >>> text2 = u'Lorem ipsum'!

    >>> isinstance(text1, (str, unicode))! True! >>> isinstance(text2, (str, unicode))! True Python 2
  4. >>> text1 = 'Lorem ipsum'! >>> text2 = u'Lorem ipsum'!

    >>> isinstance(text1, basestring)! True! >>> isinstance(text2, basestring)! True Python 2
  5. >>> 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 Python 3
  6. >>> text = 'Lorem ipsum'! >>> isinstance(text, basestring)! Traceback (most

    recent call last):! File "<stdin>", line 2, in <module>! NameError: name 'basestring' is not defined Python 3
  7. >>> import six! >>> text = 'Lorem ipsum'! >>> isinstance(text,

    six.string_types)! True! >>> text = u'Lorem ipsum' # Python 3.3+! >>> isinstance(text, six.string_types)! True six
  8. 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
  9. >>> lv = {}! >>> exec 'x = 3, y

    = 4' in {}, lv! >>> with open('foo.txt', 'w') as f:! ... print >>f lv['x'], lv['y']! ...! >>> Python 2
  10. Python 3 >>> lv = {}! >>> exec('x = 3,

    y = 4', {}, lv)! >>> with open('foo.txt', 'w') as f:! ... print(lv['x'], lv['y'], file=f)! ...! >>>
  11. 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)! ...! >>>
  12. from six import with_metaclass! ! class MetaFoo(type):! pass! ! class

    Foo(with_metaclass(object, MetaFoo)):! pass six
  13. 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)
  14. 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]
  15. 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
  16. from itertools import izip! ! u = raw_input()! ! for

    i, v in izip(xrange(len(u)), u):! print i, v Python 2
  17. from six.moves import input, zip, range! ! u = input()!

    ! for i, v in zip(range(len(u)), u):! print i, v six
  18. from six.moves import ConfigParser! ! # This will fail on

    Python 3!! parser = ConfigParser.SafeConfigParser() Caveats
  19. 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