Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more compatibility helpers, minor code improvements
- Loading branch information
Showing
6 changed files
with
143 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,58 @@ | ||
"""Python 2/3 compatibility helpers.""" | ||
# -*- coding: utf-8 -*- | ||
|
||
u""" | ||
Python 2/3 compatibility helpers. | ||
.. py:attribute:: viewkeys | ||
viewkeys(D) → a set-like object providing a view on D's keys. | ||
.. py:attribute:: viewvalues | ||
viewvalues(D) → an object providing a view on D's values. | ||
.. py:attribute:: viewitems | ||
viewitems(D) → a set-like object providing a view on D's items. | ||
.. py:attribute:: iterkeys | ||
iterkeys(D) → an iterator over the keys of D. | ||
.. py:attribute:: itervalues | ||
itervalues(D) → an iterator over the values of D. | ||
.. py:attribute:: iteritems | ||
iteritems(D) → an iterator over the (key, value) items of D. | ||
.. py:attribute:: izip | ||
Alias for :func:`zip` on Python 3 / :func:`itertools.izip` on Python 2. | ||
""" | ||
|
||
from operator import methodcaller | ||
from sys import version_info | ||
|
||
PY2 = version_info[0] == 2 | ||
if PY2: # pragma: no cover | ||
assert version_info[1] > 6, 'Python >= 2.7 required' | ||
|
||
if PY2: # pragma: no cover | ||
iteritems = lambda x: x.iteritems() | ||
viewitems = lambda x: x.viewitems() | ||
assert version_info[1] > 6, 'Python >= 2.7 required' | ||
viewkeys = methodcaller('viewkeys') | ||
viewvalues = methodcaller('viewvalues') | ||
viewitems = methodcaller('viewitems') | ||
iterkeys = methodcaller('iterkeys') | ||
itervalues = methodcaller('itervalues') | ||
iteritems = methodcaller('iteritems') | ||
from itertools import izip | ||
else: # pragma: no cover | ||
iteritems = lambda x: iter(x.items()) | ||
viewitems = lambda x: x.items() | ||
|
||
iteritems.__doc__ = 'Python 2/3 compatible iteritems' | ||
viewitems.__doc__ = 'Python 2/3 compatible viewitems' | ||
viewkeys = methodcaller('keys') | ||
viewvalues = methodcaller('values') | ||
viewitems = methodcaller('items') | ||
_compose = lambda f, g: lambda x: f(g(x)) | ||
iterkeys = _compose(iter, viewkeys) | ||
itervalues = _compose(iter, viewvalues) | ||
iteritems = _compose(iter, viewitems) | ||
izip = zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters