Skip to main content

Python 2 to the power of 3. A lightweight porting helper library.

Project description

Eight is a Python module that provides a minimalist compatibility layer between Python 3 and 2. Eight lets you write code for Python 3.3+ while providing limited compatibility with Python 2.7 with no code changes. Eight is inspired by six, nine, and python-future, but provides better internationalization (i18n) support, is more lightweight, easier to use, and unambiguously biased toward Python 3 code: if you remove eight from your code, it will continue to function exactly as it did with eight on Python 3.

To write code for Python 3 that is portable to Python 2, you may also want to read Armin Ronacher’s excellent Python 3 porting guide, as well as the official porting guide.

Writing from eight import * in your code is a no-op in Python 3. In Python 2, it binds a bunch of Python 3 names to their Python 2 equivalents. Also, if you need to import a module or module member that was renamed in Python 3, writing from eight import <module> will do the right thing (equivalent to import <module> on Python 3 and import <old_name> as <module> on Python 2). Finally, eight can optionally wrap your standard streams and environment variable I/O to use text, not bytes (see below).

Installation

pip install eight

Synopsis

from eight import *
from eight import queue
from eight.collections import UserList, deque

If you use print, division, non-ASCII literals, or relative imports, you should also add this future import at the top of each source file:

from __future__ import (print_function, division, unicode_literals, absolute_import)

Wrapping stdio

Eight provides wrappers for sys.stdin, sys.stdout, and sys.stderr to make them (and methods that use them) behave like they do on Python 3. Specifically, in Python 3 these streams accept text data, and their .buffer attributes refer to the underlying streams that accept bytes. Eight uses the io module to do the same for you, but subclasses the TextIOWrapper class for sys.stdout and sys.stderr to coerce non-unicode input to unicode on Python 2 (otherwise, because of the Python 2 semantics, things like exception printing cease to work).

To enable stdio wrapping, use the following:

import eight
eight.wrap_stdio()

To revert the effects of this on any of the streams, use the detach method, e.g. sys.stdin = sys.stdin.detach() (but remember to condition this on eight.USING_PYTHON2). See the io module documentation for more information.

Decoding command-line arguments

Eight provides a utility function to decode the contents of sys.argv on Python 2 (as Python 3 does). It uses sys.stdin.encoding as the encoding to do so:

import eight
eight.decode_command_line_args()

The call to decode_command_line_args() replaces sys.argv with its decoded contents and returns the new contents. On Python 3, the call is a no-op (it returns sys.argv and leaves it intact).

Wrapping environment variable getters and setters

Eight provides utility wrappers to help bring Python 2 environment variable access and assignment in line with Python 3: encode the input to os.putenv (which is used for statements like os.environ[x] = y) and decode the output of os.getenv (used for x = os.environ[y]). Use wrap_os_environ_io() to monkey-patch these wrappers into the os module:

import eight
eight.wrap_os_environ_io()

On Python 3, the call is a no-op.

Selecting from the buffet

You can see what from eight import * will do by running IPython and typing import eight, then eight.<TAB>. Here is a full list of what’s available:

  • ascii

  • bytes

  • chr

  • filter

  • hex

  • input

  • int

  • map

  • oct

  • open

  • range

  • round

  • str

  • super

  • zip

You can import these symbols by listing them explicitly. If for any reason you see an issue with importing them all (which is recommended), you can of course import a subset.

In addition to names imported by from eight import *, the following modules are available and should be imported by name using from eight import <name> when needed:

  • queue (old name: Queue)

  • builtins (old name: __builtin__)

  • copyreg (old name: copy_reg)

  • configparser (old name: ConfigParser)

  • reprlib (old name: repr)

  • winreg (old name: _winreg)

  • _thread (old name: thread)

  • _dummy_thread (old name: dummy_thread)

The following modules have attributes which resided elsewhere in Python 2: TODO

Acknowledgments

Python-future for doing a bunch of heavy lifting on backports of Python 3 features.

License

Licensed under the terms of the Apache License, Version 2.0.

https://img.shields.io/travis/kislyuk/eight.svg https://img.shields.io/coveralls/kislyuk/eight.svg https://img.shields.io/pypi/v/eight.svg https://img.shields.io/pypi/dm/eight.svg https://img.shields.io/pypi/l/eight.svg https://readthedocs.org/projects/eight/badge/?version=latest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

eight-0.4.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

eight-0.4.1-py2.py3-none-any.whl (9.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file eight-0.4.1.tar.gz.

File metadata

  • Download URL: eight-0.4.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for eight-0.4.1.tar.gz
Algorithm Hash digest
SHA256 8f43362b7f4a1113e4f45bb05625d2655bdf91dbdb411545efb990b47c0c6889
MD5 5d8dfdc4bc7e7cc1996856ee0e79b070
BLAKE2b-256 9831c852711b7274886a22132d4f9b1d40c4a98f442bfa5090244f20ebc84374

See more details on using hashes here.

File details

Details for the file eight-0.4.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for eight-0.4.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9759dc2b48f8b128bb1f2488405acdd292646d3bddf744e22ad0683f03fd63a7
MD5 731cc8abf3b29cb6516ffaeb0b7c5509
BLAKE2b-256 57510785a3212aa8dc8a27f161ca8d88b190a86f7267bf3c13e085cc14a2e9d4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page