Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 8 cleanup #306

Merged
merged 7 commits into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ python:
- "3.5"
- "3.6"
- "3.7"
- "pypy3.5-6.0"
- "3.8-dev"
- "pypy3.5"

install:
- "pip install ."
Expand Down
59 changes: 37 additions & 22 deletions more_itertools/more.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ def first(iterable, default=_marker):
# want to do something different with flow control when I raise the
# exception, and it's weird to explicitly catch StopIteration.
if default is _marker:
raise ValueError('first() was called on an empty iterable, and no '
'default value was provided.')
raise ValueError(
'first() was called on an empty iterable, and no '
'default value was provided.'
)
return default


Expand All @@ -170,8 +172,10 @@ def last(iterable, default=_marker):
return deque(iterable, maxlen=1)[0]
except IndexError: # If the iterable was empty
if default is _marker:
raise ValueError('last() was called on an empty iterable, and no '
'default value was provided.')
raise ValueError(
'last() was called on an empty iterable, and no '
'default value was provided.'
)
return default


Expand Down Expand Up @@ -233,6 +237,7 @@ class peekable:
[]

"""

def __init__(self, iterable):
self._it = iter(iterable)
self._cache = deque()
Expand Down Expand Up @@ -399,11 +404,13 @@ def consumer(func):
``t.send()`` could be used.

"""

@wraps(func)
def wrapper(*args, **kwargs):
gen = func(*args, **kwargs)
next(gen)
return gen

return wrapper


Expand Down Expand Up @@ -529,6 +536,7 @@ def distinct_permutations(iterable):
sequence.

"""

def make_new_permutations(pool, e):
"""Internal helper function.
The output permutations are built up by adding element *e* to the
Expand Down Expand Up @@ -696,7 +704,7 @@ def substrings(iterable):
# And the rest
for n in range(2, item_count + 1):
for i in range(item_count - n + 1):
yield seq[i:i + n]
yield seq[i : i + n]


def substrings_indexes(seq, reverse=False):
Expand Down Expand Up @@ -729,7 +737,7 @@ def substrings_indexes(seq, reverse=False):
if reverse:
r = reversed(r)
return (
(seq[i:i + L], i, i + L) for L in r for i in range(len(seq) - L + 1)
(seq[i : i + L], i, i + L) for L in r for i in range(len(seq) - L + 1)
)


Expand Down Expand Up @@ -766,6 +774,7 @@ class bucket:
[]

"""

def __init__(self, iterable, key, validator=None):
self._it = iter(iterable)
self._key = key
Expand Down Expand Up @@ -917,11 +926,12 @@ def collapse(iterable, base_type=None, levels=None):
['a', ['b'], 'c', ['d']]

"""

def walk(node, level):
if (
((levels is not None) and (level > levels)) or
isinstance(node, (str, bytes)) or
((base_type is not None) and isinstance(node, base_type))
((levels is not None) and (level > levels))
or isinstance(node, (str, bytes))
or ((base_type is not None) and isinstance(node, base_type))
):
yield node
return
Expand Down Expand Up @@ -1016,7 +1026,7 @@ def sliced(seq, n):
For non-sliceable iterables, see :func:`chunked`.

"""
return takewhile(bool, (seq[i: i + n] for i in count(0, n)))
return takewhile(bool, (seq[i : i + n] for i in count(0, n)))


def split_at(iterable, pred):
Expand All @@ -1041,8 +1051,8 @@ def split_at(iterable, pred):


def split_before(iterable, pred):
"""Yield lists of items from *iterable*, where each list starts with an
item where callable *pred* returns ``True``:
"""Yield lists of items from *iterable*, where each list ends just before
an item for which callable *pred* returns ``True``:

>>> list(split_before('OneTwo', lambda s: s.isupper()))
[['O', 'n', 'e'], ['T', 'w', 'o']]
Expand Down Expand Up @@ -1317,9 +1327,13 @@ def sort_together(iterables, key_list=(0,), reverse=False):
[(3, 2, 1), ('a', 'b', 'c')]

"""
return list(zip(*sorted(zip(*iterables),
key=itemgetter(*key_list),
reverse=reverse)))
return list(
zip(
*sorted(
zip(*iterables), key=itemgetter(*key_list), reverse=reverse
)
)
)


def unzip(iterable):
Expand Down Expand Up @@ -1365,6 +1379,7 @@ def getter(obj):
# which just stops the unzipped iterables
# at first length mismatch
raise StopIteration

return getter

return tuple(map(itemgetter(i), it) for i, it in enumerate(iterables))
Expand Down Expand Up @@ -1774,7 +1789,7 @@ def islice_extended(iterable, *args):
if step > 0:
start = 0 if (start is None) else start

if (start < 0):
if start < 0:
# Consume all but the last -start items
cache = deque(enumerate(it, 1), maxlen=-start)
len_iter = cache[-1][0] if cache else 0
Expand Down Expand Up @@ -1995,6 +2010,7 @@ class SequenceView(Sequence):
require (much) extra storage.

"""

def __init__(self, target):
if not isinstance(target, Sequence):
raise TypeError
Expand Down Expand Up @@ -2319,9 +2335,7 @@ def rlocate(iterable, pred=bool, window_size=None):
if window_size is None:
try:
len_iter = len(iterable)
return (
len_iter - i - 1 for i in locate(reversed(iterable), pred)
)
return (len_iter - i - 1 for i in locate(reversed(iterable), pred))
except TypeError:
pass

Expand Down Expand Up @@ -2439,7 +2453,8 @@ def set_partitions(iterable, k=None):
if k is not None:
if k < 1:
raise ValueError(
"Can't partition in a negative or zero number of groups")
"Can't partition in a negative or zero number of groups"
)
elif k > n:
return

Expand All @@ -2455,7 +2470,7 @@ def set_partitions_helper(L, k):
yield [[e], *p]
for p in set_partitions_helper(M, k):
for i in range(len(p)):
yield p[:i] + [[e] + p[i]] + p[i + 1:]
yield p[:i] + [[e] + p[i]] + p[i + 1 :]

if k is None:
for k in range(1, n + 1):
Expand Down Expand Up @@ -2585,7 +2600,7 @@ def distinct_combinations(iterable, r):
else:
pool = tuple(iterable)
for i, prefix in unique_everseen(enumerate(pool), key=itemgetter(1)):
for suffix in distinct_combinations(pool[i + 1:], r - 1):
for suffix in distinct_combinations(pool[i + 1 :], r - 1):
yield (prefix,) + suffix


Expand Down
3 changes: 1 addition & 2 deletions more_itertools/recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ def grouper(iterable, n, fillvalue=None):
"""
if isinstance(iterable, int):
warnings.warn(
"grouper expects iterable as first parameter",
DeprecationWarning,
"grouper expects iterable as first parameter", DeprecationWarning
)
n, iterable = iterable, n
args = [iter(iterable)] * n
Expand Down
Loading