Skip to content

Commit

Permalink
Don't solicit the sequence as a parameter. Instead, expect the caller…
Browse files Browse the repository at this point in the history
… to supply a sequence.
  • Loading branch information
jaraco committed Sep 4, 2018
1 parent 5b417e0 commit dd3bdc8
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions jaraco/itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,26 +981,25 @@ def list_or_single(iterable):
'a'
"""
warnings.warn("Use maybe_single", DeprecationWarning)
return maybe_single(iterable, sequence=list)
return maybe_single(list(iterable))


def maybe_single(iterable, sequence=tuple):
def maybe_single(sequence):
"""
Given an iterable, return the items as a sequence.
If the iterable contains exactly one item, return
that item. Correlary function to always_iterable.
Given a sequence, if it contains exactly one item,
return that item, otherwise return the sequence.
Correlary function to always_iterable.
>>> maybe_single(iter('abcd'))
>>> maybe_single(tuple('abcd'))
('a', 'b', 'c', 'd')
>>> maybe_single(['a'])
'a'
"""
result = sequence(iterable)
try:
result, = result
single, = sequence
except ValueError:
pass
return result
return sequence
return single


def self_product(iterable):
Expand Down

0 comments on commit dd3bdc8

Please sign in to comment.