Skip to content

Commit

Permalink
Merge pull request #268 from erikrose/python-3.x
Browse files Browse the repository at this point in the history
Drop Python 2.7 support
  • Loading branch information
bbayles committed Feb 3, 2019
2 parents bab3265 + 90158cb commit 37b0af1
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 154 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ dist: "xenial"
language: "python"

python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "3.7"
- "pypy2.7-6.0"
- "pypy3.5-6.0"

install:
Expand Down
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ Python iterables.
| | `make_decorator <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.make_decorator>`_, |
| | `SequenceView <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.SequenceView>`_, |
| | `consume <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.consume>`_, |
| | `accumulate <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.accumulate>`_, |
| | `tabulate <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.tabulate>`_, |
| | `repeatfunc <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.repeatfunc>`_ |
+------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Expand Down
1 change: 0 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,5 @@ Others
**Itertools recipes**

.. autofunction:: consume
.. autofunction:: accumulate(iterable, func=operator.add)
.. autofunction:: tabulate
.. autofunction:: repeatfunc
18 changes: 9 additions & 9 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
master_doc = 'index'

# General information about the project.
project = u'more-itertools'
copyright = u'2012, Erik Rose'
project = 'more-itertools'
copyright = '2012, Erik Rose'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '5.0.0'
version = '6.0.0'
# The full version, including alpha/beta/rc tags.
release = version

Expand Down Expand Up @@ -190,8 +190,8 @@
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
('index', 'more-itertools.tex', u'more-itertools Documentation',
u'Erik Rose', 'manual'),
('index', 'more-itertools.tex', 'more-itertools Documentation',
'Erik Rose', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -220,8 +220,8 @@
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'more-itertools', u'more-itertools Documentation',
[u'Erik Rose'], 1)
('index', 'more-itertools', 'more-itertools Documentation',
['Erik Rose'], 1)
]

# If true, show URL addresses after external links.
Expand All @@ -234,8 +234,8 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'more-itertools', u'more-itertools Documentation',
u'Erik Rose', 'more-itertools', 'One line description of project.',
('index', 'more-itertools', 'more-itertools Documentation',
'Erik Rose', 'more-itertools', 'One line description of project.',
'Miscellaneous'),
]

Expand Down
15 changes: 14 additions & 1 deletion docs/versions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@ Version History

.. automodule:: more_itertools

5.1.0
6.0.0
-----

* Major chances:
* Python 2.7 is no longer supported. The 5.0.0 release will be the last
version targeting Python 2.7.
* All future releases will target the active versions of Python 3.
As of 2019, those are Python 3.4 and above.
* The ``six`` library is no longer a dependency.
* The :func:`accumulate` function is no longer part of this library. You
may import a better version from the standard ``itertools`` module.

* Changes to existing itertools:
* The order of the parameters in :func:`grouper` have changed to match
the latest recipe in the itertools documentation. Use of the old order
will be supported in this release, but emit a ``DeprecationWarning``.
The legacy behavior will be dropped in a future release. (thanks to jaraco)
* :func:`distinct_permutations` was improved (thanks to jferard - see also `permutations with unique values <https://stackoverflow.com/questions/6284396/permutations-with-unique-values>`_ at StackOverflow.)
* An unused parameter was removed from :func:`substrings`. (thanks to pylang)

* Other changes:
*

5.0.0
-----
Expand Down
77 changes: 23 additions & 54 deletions more_itertools/more.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

from collections import Counter, defaultdict, deque
from functools import partial, wraps
from heapq import merge
Expand All @@ -14,17 +12,12 @@
repeat,
starmap,
takewhile,
tee
tee,
zip_longest,
)
from operator import itemgetter, lt, gt, sub
from sys import maxsize, version_info
try:
from collections.abc import Sequence
except ImportError:
from collections import Sequence

from six import binary_type, string_types, text_type
from six.moves import filter, map, range, zip, zip_longest
from collections.abc import Sequence

from .recipes import consume, flatten, take

Expand Down Expand Up @@ -169,7 +162,7 @@ def last(iterable, default=_marker):
return default


class peekable(object):
class peekable:
"""Wrap an iterator to allow lookahead and prepending elements.
Call :meth:`peek` on the result to get the value that will be returned
Expand Down Expand Up @@ -241,10 +234,6 @@ def __bool__(self):
return False
return True

def __nonzero__(self):
# For Python 2 compatibility
return self.__bool__()

def peek(self, default=_marker):
"""Return the item that will be next returned from ``next()``.
Expand Down Expand Up @@ -298,8 +287,6 @@ def __next__(self):

return next(self._it)

next = __next__ # For Python 2 compatibility

def _get_slice(self, index):
# Normalize the slice's arguments
step = 1 if (index.step is None) else index.step
Expand Down Expand Up @@ -339,14 +326,11 @@ def __getitem__(self, index):
return self._cache[index]


def _collate(*iterables, **kwargs):
def _collate(*iterables, key=lambda a: a, reverse=False):
"""Helper for ``collate()``, called when the user is using the ``reverse``
or ``key`` keyword arguments on Python versions below 3.5.
"""
key = kwargs.pop('key', lambda a: a)
reverse = kwargs.pop('reverse', False)

min_or_max = partial(max if reverse else min, key=itemgetter(0))
peekables = [peekable(it) for it in iterables]
peekables = [p for p in peekables if p] # Kill empties.
Expand Down Expand Up @@ -384,9 +368,7 @@ def collate(*iterables, **kwargs):
If the elements of the passed-in iterables are out of order, you might get
unexpected results.
On Python 2.7, this function delegates to :func:`heapq.merge` if neither
of the keyword arguments are specified. On Python 3.5+, this function
is an alias for :func:`heapq.merge`.
On Python 3.5+, this function is an alias for :func:`heapq.merge`.
"""
if not kwargs:
Expand Down Expand Up @@ -475,8 +457,7 @@ def with_iter(context_manager):
"""
with context_manager as iterable:
for item in iterable:
yield item
yield from iterable


def one(iterable, too_short=None, too_long=None):
Expand Down Expand Up @@ -727,7 +708,7 @@ def substrings(iterable):
yield seq[i:i + n]


class bucket(object):
class bucket:
"""Wrap *iterable* and return an object that buckets it iterable into
child iterables based on a *key* function.
Expand Down Expand Up @@ -912,7 +893,7 @@ def collapse(iterable, base_type=None, levels=None):
def walk(node, level):
if (
((levels is not None) and (level > levels)) or
isinstance(node, string_types) or
isinstance(node, str) or
((base_type is not None) and isinstance(node, base_type))
):
yield node
Expand All @@ -928,8 +909,7 @@ def walk(node, level):
for x in walk(child, level + 1):
yield x

for x in walk(iterable, 0):
yield x
yield from walk(iterable, 0)


def side_effect(func, iterable, chunk_size=None, before=None, after=None):
Expand Down Expand Up @@ -987,8 +967,7 @@ def side_effect(func, iterable, chunk_size=None, before=None, after=None):
else:
for chunk in chunked(iterable, chunk_size):
func(chunk)
for item in chunk:
yield item
yield from chunk
finally:
if after is not None:
after()
Expand Down Expand Up @@ -1138,8 +1117,7 @@ def padded(iterable, fillvalue=None, n=None, next_multiple=False):
"""
it = iter(iterable)
if n is None:
for item in chain(it, repeat(fillvalue)):
yield item
yield from chain(it, repeat(fillvalue))
elif n < 1:
raise ValueError('n must be at least 1')
else:
Expand Down Expand Up @@ -1216,7 +1194,7 @@ def stagger(iterable, offsets=(-1, 0, 1), longest=False, fillvalue=None):
)


def zip_offset(*iterables, **kwargs):
def zip_offset(*iterables, offsets, longest=False, fillvalue=None):
"""``zip`` the input *iterables* together, but offset the `i`-th iterable
by the `i`-th item in *offsets*.
Expand All @@ -1237,10 +1215,6 @@ def zip_offset(*iterables, **kwargs):
sequence. Specify *fillvalue* to use some other value.
"""
offsets = kwargs['offsets']
longest = kwargs.get('longest', False)
fillvalue = kwargs.get('fillvalue', None)

if len(iterables) != len(offsets):
raise ValueError("Number of iterables and offsets didn't match")

Expand Down Expand Up @@ -1384,7 +1358,7 @@ def divide(n, iterable):
return ret


def always_iterable(obj, base_type=(text_type, binary_type)):
def always_iterable(obj, base_type=(str, bytes)):
"""If *obj* is iterable, return an iterator over its items::
>>> obj = (1, 2, 3)
Expand Down Expand Up @@ -1673,13 +1647,13 @@ def rstrip(iterable, pred):
"""
cache = []
cache_append = cache.append
cache_clear = cache.clear
for x in iterable:
if pred(x):
cache_append(x)
else:
for y in cache:
yield y
del cache[:]
yield from cache
cache_clear()
yield x


Expand Down Expand Up @@ -1768,8 +1742,7 @@ def islice_extended(iterable, *args):
cache.append(item)
else:
# When both start and stop are positive we have the normal case
for item in islice(it, start, stop, step):
yield item
yield from islice(it, start, stop, step)
else:
start = -1 if (start is None) else start

Expand Down Expand Up @@ -1814,8 +1787,7 @@ def islice_extended(iterable, *args):

cache = list(islice(it, n))

for item in cache[i::step]:
yield item
yield from cache[i::step]


def always_reversible(iterable):
Expand Down Expand Up @@ -1883,7 +1855,7 @@ def difference(iterable, func=sub):
This is the opposite of :func:`accumulate`'s default behavior:
>>> from more_itertools import accumulate
>>> from itertools import accumulate
>>> iterable = [0, 1, 2, 3, 4]
>>> list(accumulate(iterable))
[0, 1, 3, 6, 10]
Expand Down Expand Up @@ -1955,7 +1927,7 @@ def __repr__(self):
return '{}({})'.format(self.__class__.__name__, repr(self._target))


class seekable(object):
class seekable:
"""Wrap an iterator to allow for seeking backward and forward. This
progressively caches the items in the source iterable so they can be
re-visited.
Expand Down Expand Up @@ -2029,8 +2001,6 @@ def __next__(self):
self._cache.append(item)
return item

next = __next__

def elements(self):
return SequenceView(self._cache)

Expand All @@ -2041,7 +2011,7 @@ def seek(self, index):
consume(self, remainder)


class run_length(object):
class run_length:
"""
:func:`run_length.encode` compresses an iterable with run-length encoding.
It yields groups of repeated items with the count of how many times they
Expand Down Expand Up @@ -2326,8 +2296,7 @@ def replace(iterable, pred, substitutes, count=None, window_size=1):
if pred(*w):
if (count is None) or (n < count):
n += 1
for s in substitutes:
yield s
yield from substitutes
consume(windows, window_size - 1)
continue

Expand Down

0 comments on commit 37b0af1

Please sign in to comment.