-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
compat.py
71 lines (47 loc) · 1.87 KB
/
compat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
u"""
Python 2/3 compatibility helpers.
.. py:attribute:: PY2
True iff running on Python 2.
.. 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.
.. py:attribute:: izip_longest
Alias for :func:`itertools.zip_longest` on Python 2 on Python 3 /
:func:`itertools.izip_longest` on Python 2.
"""
from operator import methodcaller
from sys import version_info
_compose = lambda f, g: lambda x: f(g(x))
PY2 = version_info[0] == 2
if PY2: # pragma: no cover
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 ifilter, imap, izip, izip_longest
else: # pragma: no cover
viewkeys = methodcaller('keys')
viewvalues = methodcaller('values')
viewitems = methodcaller('items')
iterkeys = _compose(iter, viewkeys)
itervalues = _compose(iter, viewvalues)
iteritems = _compose(iter, viewitems)
ifilter = filter
imap = map
izip = zip
from itertools import zip_longest as izip_longest