Hacker News new | ask | show | jobs
by dpkp 2592 days ago
have you written your own 2/3 compatibility layer? I can't imagine writing anything large without six...

fwiw, six can easily be vendored into a project to avoid the technical external dependency. that is how we manage it for kafka-python.

2 comments

No compatibility layer. Its really not bad.

there are a few modules that are simply at different locations but have the same API

  if PY3:
    from http import client as httplib
  else:
    import httplib
constants

  PY3 = sys.version_info >= (3, 0)
  PY2 = sys.version_info < (3, 0)
  
  PY26 = sys.version_info >= (2, 6) and sys.version_info < (2, 7)
is string

  isinstance(<maybe_string>, basestring if PY2 else str)
using different classes

  # Python 2.6 doesn't properly UTF-8 encode syslog messages, so it needs
  # to be performed in a custom formatter.
  formatter_class = UnicodeLoggingFormatter if PY26 else logging.Formatter
You don't need all of six in most cases. You just need a couple of different functions.

As the maintainer of WebOb/Pyramid/Waitress we have a compat module that contains all of the changes/renames/functions to help with the Python2/3 compatibility and all tests run across both platforms.

Are the functions borrowed heavily from six? Yes, but we don't need to vendor all of six.