1 # -*- coding: utf-8 -*-
3 ## Part of the pyprimes.py package.
5 ## Copyright © 2014 Steven D'Aprano.
6 ## See the file __init__.py for the licence terms for this software.
9 """Python 2 and 3 compatibility layer for the pyprimes package.
11 This module is considered a private implementation detail and is subject
12 to change without notice.
15 from __future__
import division
19 import builtins
# Python 3.x.
21 # We're probably running Python 2.x.
22 import __builtin__
as builtins
27 except AttributeError:
28 # No next() builtin, so we're probably running Python 2.4 or 2.5.
29 def next(iterator
, *args
):
32 raise TypeError("next expected at most 2 arguments, got %d" % n
)
34 return iterator
.next()
43 range = builtins
.xrange
44 except AttributeError:
45 # No xrange built-in, so we're probably running Python3
46 # where range is already a lazy iterator.
47 assert type(builtins
.range(3)) is not list
48 range = builtins
.range
52 from itertools
import ifilter
as filter, izip
as zip
54 # Python 3, where filter and zip are already lazy.
55 assert type(builtins
.filter(None, [1, 2])) is not list
56 assert type(builtins
.zip("ab", [1, 2])) is not list
57 filter = builtins
.filter
63 except AttributeError:
66 for element
in iterable
:
73 from itertools
import compress
75 # Must be Python 2.x, so we need to roll our own.
76 def compress(data
, selectors
):
77 """compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"""
78 return (d
for d
, s
in zip(data
, selectors
) if s
)
82 reduce = builtins
.reduce
83 except AttributeError:
84 from functools
import reduce