NOTE: This page is still a work in progress... We need to go through our docstrings and make them sphinx-compliant, and figure out how to improve formatting with the sphinx-bootstrap-theme plugin. Pull requests would be very welcome.
A module that brings in equivalents of the new and modified Python 3 builtins into Py2. Has no effect on Py3.
See the docs here (docs/what-else.rst) for more information.
alias of ifilter
alias of imap
alias of izip
Return the same as repr(). In Python 3.x, the repr() result will contain printable characters unescaped, while the ascii() result will have such characters backslash-escaped.
unichr(i) -> Unicode character
Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
Return the hexadecimal representation of an integer or long integer.
raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading.
Return the next item from the iterator. If default is given and the iterator is exhausted, it is returned instead of raising StopIteration.
Return the octal representation of an integer or long integer.
Open file and return a stream. Raise IOError upon failure.
file is either a text or byte string giving the name (and the path if the file isn’t in the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is given, it is closed when the returned I/O object is closed, unless closefd is set to False.)
mode is an optional string that specifies the mode in which the file is opened. It defaults to ‘r’ which means open for reading in text mode. Other common values are ‘w’ for writing (truncating the file if it already exists), and ‘a’ for appending (which on some Unix systems, means that all writes append to the end of the file regardless of the current seek position). In text mode, if encoding is not specified the encoding used is platform dependent. (For reading and writing raw bytes use binary mode and leave encoding unspecified.) The available modes are:
Character | Meaning |
‘r’ | open for reading (default) |
‘w’ | open for writing, truncating the file first |
‘a’ | open for writing, appending to the end of the file if it exists |
‘b’ | binary mode |
‘t’ | text mode (default) |
‘+’ | open a disk file for updating (reading and writing) |
‘U’ | universal newline mode (for backwards compatibility; unneeded for new code) |
The default mode is ‘rt’ (open for reading text). For binary random access, the mode ‘w+b’ opens and truncates the file to 0 bytes, while ‘r+b’ opens the file without truncation.
Python distinguishes between files opened in binary and text modes, even when the underlying operating system doesn’t. Files opened in binary mode (appending ‘b’ to the mode argument) return contents as bytes objects without any decoding. In text mode (the default, or when ‘t’ is appended to the mode argument), the contents of the file are returned as strings, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given.
buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size of a fixed-size chunk buffer. When no buffering argument is given, the default buffering policy works as follows:
encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent, but any encoding supported by Python can be passed. See the codecs module for the list of supported encodings.
errors is an optional string that specifies how encoding errors are to be handled—this argument should not be used in binary mode. Pass ‘strict’ to raise a ValueError exception if there is an encoding error (the default of None has the same effect), or pass ‘ignore’ to ignore errors. (Note that ignoring encoding errors can lead to data loss.) See the documentation for codecs.register for a list of the permitted encoding error strings.
newline controls how universal newlines works (it only applies to text mode). It can be None, ‘’, ‘n’, ‘r’, and ‘rn’. It works as follows:
If closefd is False, the underlying file descriptor will be kept open when the file is closed. This does not work when a file name is given and must be True in that case.
open() returns a file object whose type depends on the mode, and through which the standard file operations such as reading and writing are performed. When open() is used to open a file in a text mode (‘w’, ‘r’, ‘wt’, ‘rt’, etc.), it returns a TextIOWrapper. When used to open a file in a binary mode, the returned class varies: in read binary mode, it returns a BufferedReader; in write binary and append binary modes, it returns a BufferedWriter, and in read/write mode, it returns a BufferedRandom.
It is also possible to use a string or bytearray as a file for both reading and writing. For strings StringIO can be used like a file opened in a text mode, and for bytes a BytesIO can be used like a file opened in a binary mode.
With two arguments, equivalent to x**y. With three arguments, equivalent to (x**y) % z, but may be more efficient (e.g. for ints).
See Python 3 documentation: uses Banker’s Rounding.
Delegates to the __round__ method if for some reason this exists.
If not, rounds a number to a given precision in decimal digits (default 0 digits). This returns an int when called with one argument, otherwise the same type as the number. ndigits may be negative.
See the test_round method in future/tests/test_builtins.py for examples.
Like builtin super(), but capable of magic.
This acts just like the builtin super() function, but if called without any arguments it attempts to infer them at runtime.
alias of newbytes
alias of newdict
alias of newint
alias of newlist
alias of newobject
alias of newrange
alias of newstr
This module contains backports the data types that were significantly changed in the transition from Python 2 to Python 3.
It is used as follows:
from __future__ import division, absolute_import, print_function
from builtins import bytes, dict, int, range, str
to bring in the new semantics for these functions from Python 3. And then, for example:
b = bytes(b'ABCD')
assert list(b) == [65, 66, 67, 68]
assert repr(b) == "b'ABCD'"
assert [65, 66] in b
# These raise TypeErrors:
# b + u'EFGH'
# b.split(u'B')
# bytes(b',').join([u'Fred', u'Bill'])
s = str(u'ABCD')
# These raise TypeErrors:
# s.join([b'Fred', b'Bill'])
# s.startswith(b'A')
# b'B' in s
# s.find(b'A')
# s.replace(u'A', b'a')
# This raises an AttributeError:
# s.decode('utf-8')
assert repr(s) == 'ABCD' # consistent repr with Py3 (no u prefix)
for i in range(10**11)[:10]:
pass
and:
class VerboseList(list):
def append(self, item):
print('Adding an item')
super().append(item) # new simpler super() function
range is a custom class that backports the slicing behaviour from Python 3 (based on the xrange module by Dan Crosta). See the newrange module docstring for more details.
super() is based on Ryan Kelly’s magicsuper module. See the newsuper module docstring for more details.
Python 3 modifies the behaviour of round() to use “Banker’s Rounding”. See http://stackoverflow.com/a/10825998. See the newround module docstring for more details.
A backport of the Python 3 bytes object to Py2
Returns a newstr (i.e. unicode subclass)
Decode B using the codec registered for encoding. Default encoding is ‘utf-8’. errors may be given to set a different error handling scheme. Default is ‘strict’ meaning that encoding errors raise a UnicodeDecodeError. Other possible values are ‘ignore’ and ‘replace’ as well as any other name registered with codecs.register_error that is able to handle UnicodeDecodeErrors.
Returns index of sub in bytes. Raises ValueError if byte is not in bytes and TypeError if can’t be converted bytes or its length is not 1.
Return a copy of b with all ASCII characters converted to lowercase.
Return a translation table (a bytes object of length 256) suitable for use in the bytes or bytearray translate method where each byte in frm is mapped to the byte at the same position in to. The bytes objects frm and to must be of the same length.
Like S.rfind() but raise ValueError when the substring is not found.
Strip trailing bytes contained in the argument. If the argument is omitted, strip trailing ASCII whitespace.
Return a list of the lines in B, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.
Strip leading and trailing bytes contained in the argument. If the argument is omitted, strip trailing ASCII whitespace.
Return a copy of b with all ASCII characters converted to uppercase.
A backport of the Python 3 dict object to Py2
A backport of the Python 3 int object to Py2
Return the integer represented by the given array of bytes.
The mybytes argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.
The byteorder argument determines the byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value.
The signed keyword-only argument indicates whether two’s complement is used to represent the integer.
Return an array of bytes representing an integer.
The integer is represented using length bytes. An OverflowError is raised if the integer is not representable with the given number of bytes.
The byteorder argument determines the byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value.
The signed keyword-only argument determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.
A backport of the Python 3 list object to Py2
Pure-Python backport of Python 3’s range object. See the CPython documentation for details:
Return the number of ocurrences of integer value in the sequence this range represents.
Return the 0-based position of integer value in the sequence this range represents.
A backport of the Python 3 str object to Py2
Returns bytes
Encode S using the codec registered for encoding. Default encoding is ‘utf-8’. errors may be given to set a different error handling scheme. Default is ‘strict’ meaning that encoding errors raise a UnicodeEncodeError. Other possible values are ‘ignore’, ‘replace’ and ‘xmlcharrefreplace’ as well as any other name registered with codecs.register_error that can handle UnicodeEncodeErrors.
Like newstr.find() but raise ValueError when the substring is not found.
Return a translation table usable for str.translate().
If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters to Unicode ordinals, strings or None. Character keys will be then converted to ordinals. If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y. If there is a third argument, it must be a string, whose characters will be mapped to None in the result.
Return a list of the lines in S, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.
Return a copy of the string S, where all characters have been mapped through the given translation table, which must be a mapping of Unicode ordinals to Unicode ordinals, strings, or None. Unmapped characters are left untouched. Characters mapped to None are deleted.
Python 3 reorganized the standard library (PEP 3108). This module exposes several standard library modules to Python 2 under their new Python 3 names.
It is designed to be used as follows:
from future import standard_library
standard_library.install_aliases()
And then these normal Py3 imports work on both Py3 and Py2:
import builtins
import configparser
import copyreg
import queue
import reprlib
import socketserver
import winreg # on Windows only
import test.support
import html, html.parser, html.entites
import http, http.client, http.server
import http.cookies, http.cookiejar
import urllib.parse, urllib.request, urllib.response, urllib.error, urllib.robotparser
import xmlrpc.client, xmlrpc.server
import _thread
import _dummy_thread
import _markupbase
from itertools import filterfalse, zip_longest
from sys import intern
from collections import UserDict, UserList, UserString
from collections import OrderedDict, Counter # even on Py2.6
from subprocess import getoutput, getstatusoutput
from subprocess import check_output # even on Py2.6
(The renamed modules and functions are still available under their old names on Python 2.)
This is a cleaner alternative to this idiom (see http://docs.pythonsprints.com/python3_porting/py-porting.html):
try:
import queue
except ImportError:
import Queue as queue
We don’t currently support these modules, but would like to:
import dbm
import dbm.dumb
import dbm.gnu
import collections.abc # on Py33
import tkinter
import pickle # should (optionally) bring in cPickle on Python 2
A class for import hooks mapping Py3 module names etc. to the Py2 equivalents.
Currently this function is unneeded, as we are not attempting to provide import hooks for modules with ambiguous names: email, urllib, pickle.
Returns True if the import hooks are installed, False if not.
Deprecated. Use remove_hooks() instead. This will be removed by future v1.0.
Deprecated. Use install_hooks() instead. This will be removed by future v1.0.
A context-manager that prevents standard library modules like configparser from being imported from the local python-future source folder on Py3.
(The presence of a configparser folder would otherwise prevent setuptools from running on Py3.)
>>> HTTPConnection = from_import('http.client', 'HTTPConnection')
>>> HTTPServer = from_import('http.server', 'HTTPServer')
>>> urlopen, urlparse = from_import('urllib.request', 'urlopen', 'urlparse')
Equivalent to this on Py3:
>>> from module_name import symbol_names[0], symbol_names[1], ...
and this on Py2:
>>> from future.moves.module_name import symbol_names[0], ...
or:
>>> from future.backports.module_name import symbol_names[0], ...
except that it also handles dotted module names such as http.client.
Acts as a context manager. Saves the state of sys.modules and restores it after the ‘with’ block.
Use like this:
>>> from future import standard_library
>>> with standard_library.hooks():
... import http.client
>>> import requests
For this to work, http.client will be scrubbed from sys.modules after the ‘with’ block. That way the modules imported in the ‘with’ block will continue to be accessible in the current namespace but not from any imported modules (like requests).
Pass a (potentially dotted) module name of a Python 3 standard library module. This function imports the module compatibly on Py2 and Py3 and returns the top-level module.
>>> http = import_('http.client')
>>> http = import_('http.server')
>>> urllib = import_('urllib.request')
>>> conn = http.client.HTTPConnection(...)
>>> response = urllib.request.urlopen('http://mywebsite.com')
>>> # etc.
>>> package_name = import_(module_name)
On Py3, equivalent to this:
>>> import module_name
On Py2, equivalent to this if backport=False:
>>> from future.moves import module_name
or to this if backport=True:
>>> from future.backports import module_name
except that it also handles dotted module names such as http.client The effect then is like this:
>>> from future.backports import module
>>> from future.backports.module import submodule
>>> module.submodule = submodule
Note that this would be a SyntaxError in Python:
>>> from future.backports import http.client
Monkey-patches the standard library in Py2.6/7 to provide aliases for better Py3 compatibility.
This function installs the future.standard_library import hook into sys.meta_path.
Tries to infer whether the module m is from the Python 2 standard library. This may not be reliable on all systems.
This function removes the import hook from sys.meta_path.
Add any previously scrubbed modules back to the sys.modules cache, but only if it’s safe to do so.
Removes any Python 2 standard library modules from sys.modules that would interfere with Py3-style imports using import hooks. Examples are modules with the same names (like urllib or email).
(Note that currently import hooks are disabled for modules like these with ambiguous names anyway ...)
Acts as a context manager. Use like this:
>>> from future import standard_library
>>> standard_library.install_hooks()
>>> import http.client
>>> # ...
>>> with standard_library.suspend_hooks():
>>> import requests # incompatible with ``future``'s standard library hooks
If the hooks were disabled before the context, they are not installed when the context is left.
A selection of cross-compatible functions for Python 2 and 3.
This exports useful functions for 2/3 compatible code that are not builtins on Python 3:
bind_method: binds functions to classes
native_str_to_bytes and bytes_to_native_str
native_str: always equal to the native platform string object (because this may be shadowed by imports from future.builtins)
lists: lrange(), lmap(), lzip(), lfilter()
iterable method compatibility: iteritems, iterkeys, itervalues
- Uses the original method if available, otherwise uses items, keys, values.
types:
- text_type: unicode in Python 2, str in Python 3
- binary_type: str in Python 2, bythes in Python 3
- string_types: basestring in Python 2, str in Python 3
- bchr(c):
Take an integer and make a 1-character byte string
- bord(c)
Take the result of indexing on a byte string and make an integer
- tobytes(s)
Take a text string, a byte string, or a sequence of characters taken from a byte string, and make a byte string.
This module also defines a simple decorator called python_2_unicode_compatible (from django.utils.encoding) which defines __unicode__ and __str__ methods consistently under Python 3 and 2. To support Python 3 and 2 with a single code base, simply define a __str__ method returning unicode text and apply the python_2_unicode_compatible decorator to the class like this:
>>> from future.utils import python_2_unicode_compatible
>>> @python_2_unicode_compatible
... class MyClass(object):
... def __str__(self):
... return u'Unicode string: \u5b54\u5b50'
>>> a = MyClass()
Then, after this import:
>>> from future.builtins import str
the following is True on both Python 3 and 2:
>>> str(a) == a.encode('utf-8').decode('utf-8')
True
and, on a Unicode-enabled terminal with the right fonts, these both print the Chinese characters for Confucius:
print(a)
print(str(a))
On Python 3, this decorator is a no-op.
Some of the functions in this module come from the following sources:
- Jinja2 (BSD licensed: see https://github.com/mitsuhiko/jinja2/blob/master/LICENSE)
- Pandas compatibility module pandas.compat
- six.py by Benjamin Peterson
- Django
A decorator to turn a function or method call that returns text, i.e. unicode, into one that returns a native platform str.
Use it as a decorator like this:
from __future__ import unicode_literals
class MyClass(object):
@as_native_str(encoding='ascii')
def __repr__(self):
return next(self._iter).upper()
Bind a method to class, python 2 and python 3 compatible.
None
From jinja2/_compat.py. License: BSD.
Use as a decorator like this:
@implements_iterator
class UppercasingIterator(object):
def __init__(self, iterable):
self._iter = iter(iterable)
def __iter__(self):
return self
def __next__(self):
return next(self._iter).upper()
Python 2.7 has both new-style and old-style classes. Old-style classes can be pesky in some circumstances, such as when using inheritance. Use this function to test for whether a class is new-style. (Python 3 only has new-style classes.)
>>> isinstance(obj, bytes)
>>> from future.builtins import bytes
A function equivalent to the str.isidentifier method on Py3
Deprecated. Tests whether an object is a Py3 int or either a Py2 int or long.
Instead of using this function, you can use:
>>> from future.builtins import int
>>> isinstance(obj, int)
The following idiom is equivalent:
>>> from numbers import Integral
>>> isinstance(obj, Integral)
Equivalent to the result of isinstance(obj, newbytes) were __instancecheck__ not overridden on the newbytes subclass. In other words, it is REALLY a newbytes instance, not a Py2 native str object?
>>> isinstance(obj, str)
>>> from future.builtins import str
Use this only if compatibility with Python versions before 2.7 is required. Otherwise, prefer viewitems().
Use this only if compatibility with Python versions before 2.7 is required. Otherwise, prefer viewkeys().
Use this only if compatibility with Python versions before 2.7 is required. Otherwise, prefer viewvalues().
filter(function or None, sequence) -> list, tuple, or string
Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list.
map(function, sequence[, sequence, ...]) -> list
Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).
range(stop) -> list of integers range(start, stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers. range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0. When step is given, it specifies the increment (or decrement). For example, range(4) returns [0, 1, 2, 3]. The end point is omitted! These are exactly the valid indices for a list of 4 elements.
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the i-th element from each of the argument sequences. The returned list is truncated in length to the length of the shortest argument sequence.
On Py3, this is a no-op: native(obj) -> obj
On Py2, returns the corresponding native Py2 types that are superclasses for backported objects from Py3:
>>> from builtins import str, bytes, int
>>> native(str(u'ABC'))
u'ABC'
>>> type(native(str(u'ABC')))
unicode
>>> native(bytes(b'ABC'))
b'ABC'
>>> type(native(bytes(b'ABC')))
bytes
>>> native(int(10**20))
100000000000000000000L
>>> type(native(int(10**20)))
long
Existing native types on Py2 will be returned unchanged:
>>> type(native(u'ABC'))
unicode
alias of str
alias of str
On Py3, returns an encoded string. On Py2, returns a newbytes type, ignoring the encoding argument.
DEPRECATED: import old_div from past.utils instead.
Equivalent to a / b on Python 2 without from __future__ import division.
TODO: generalize this to other objects (like arrays etc.)
A decorator that defines __unicode__ and __str__ methods under Python 2. Under Python 3 it does nothing.
To support Python 2 and 3 with a single code base, define a __str__ method returning unicode text and apply this decorator to the class.
The implementation comes from django.utils.encoding.
Raise exception with existing traceback. If traceback is not passed, uses sys.exc_info() to get traceback.
Use this to create a Py2 native string when “from __future__ import unicode_literals” is in effect.
Encodes to latin-1 (where the first 256 chars are the same as ASCII.)
Function for iterating over dictionary items with the same set-like behaviour on Py2.7 as on Py3.
Passes kwargs to method.
Function for iterating over dictionary keys with the same set-like behaviour on Py2.7 as on Py3.
Passes kwargs to method.
Function for iterating over dictionary values with the same set-like behaviour on Py2.7 as on Py3.
Passes kwargs to method.
Function from jinja2/_compat.py. License: BSD.
Use it like this:
class BaseForm(object):
pass
class FormType(type):
pass
class Form(with_metaclass(FormType, BaseForm)):
pass
This requires a bit of explanation: the basic idea is to make a dummy metaclass for one level of class instantiation that replaces itself with the actual metaclass. Because of internal type checks we also need to make sure that we downgrade the custom metaclass for one level to something closer to type (that’s why __call__ and __init__ comes back from type etc.).
This has the advantage over six.with_metaclass of not introducing dummy classes into the final MRO.
A resurrection of some old functions from Python 2 for use in Python 3. These should be used sparingly, to help with porting efforts, since code using them is no longer standard Python 3 code.
This module provides the following:
Forward-ports of types from Python 2 for use with Python 3: