text
stringlengths 0
828
|
---|
4968,"def partition(src, key=None):
|
""""""No relation to :meth:`str.partition`, ``partition`` is like
|
:func:`bucketize`, but for added convenience returns a tuple of
|
``(truthy_values, falsy_values)``.
|
>>> nonempty, empty = partition(['', '', 'hi', '', 'bye'])
|
>>> nonempty
|
['hi', 'bye']
|
*key* defaults to :class:`bool`, but can be carefully overridden to
|
use any function that returns either ``True`` or ``False``.
|
>>> import string
|
>>> is_digit = lambda x: x in string.digits
|
>>> decimal_digits, hexletters = partition(string.hexdigits, is_digit)
|
>>> ''.join(decimal_digits), ''.join(hexletters)
|
('0123456789', 'abcdefABCDEF')
|
""""""
|
bucketized = bucketize(src, key)
|
return bucketized.get(True, []), bucketized.get(False, [])"
|
4969,"def unique_iter(src, key=None):
|
""""""Yield unique elements from the iterable, *src*, based on *key*,
|
in the order in which they first appeared in *src*.
|
>>> repetitious = [1, 2, 3] * 10
|
>>> list(unique_iter(repetitious))
|
[1, 2, 3]
|
By default, *key* is the object itself, but *key* can either be a
|
callable or, for convenience, a string name of the attribute on
|
which to uniqueify objects, falling back on identity when the
|
attribute is not present.
|
>>> pleasantries = ['hi', 'hello', 'ok', 'bye', 'yes']
|
>>> list(unique_iter(pleasantries, key=lambda x: len(x)))
|
['hi', 'hello', 'bye']
|
""""""
|
if not is_iterable(src):
|
raise TypeError('expected an iterable, not %r' % type(src))
|
if key is None:
|
key_func = lambda x: x
|
elif callable(key):
|
key_func = key
|
elif isinstance(key, basestring):
|
key_func = lambda x: getattr(x, key, x)
|
else:
|
raise TypeError('""key"" expected a string or callable, not %r' % key)
|
seen = set()
|
for i in src:
|
k = key_func(i)
|
if k not in seen:
|
seen.add(k)
|
yield i
|
return"
|
4970,"def one(src, default=None, key=None):
|
""""""Along the same lines as builtins, :func:`all` and :func:`any`, and
|
similar to :func:`first`, ``one()`` returns the single object in
|
the given iterable *src* that evaluates to ``True``, as determined
|
by callable *key*. If unset, *key* defaults to :class:`bool`. If
|
no such objects are found, *default* is returned. If *default* is
|
not passed, ``None`` is returned.
|
If *src* has more than one object that evaluates to ``True``, or
|
if there is no object that fulfills such condition, return
|
*default*. It's like an `XOR`_ over an iterable.
|
>>> one((True, False, False))
|
True
|
>>> one((True, False, True))
|
>>> one((0, 0, 'a'))
|
'a'
|
>>> one((0, False, None))
|
>>> one((True, True), default=False)
|
False
|
>>> bool(one(('', 1)))
|
True
|
>>> one((10, 20, 30, 42), key=lambda i: i > 40)
|
42
|
See `Martín Gaitán's original repo`_ for further use cases.
|
.. _Martín Gaitán's original repo: https://github.com/mgaitan/one
|
.. _XOR: https://en.wikipedia.org/wiki/Exclusive_or
|
""""""
|
ones = list(itertools.islice(filter(key, src), 2))
|
return ones[0] if len(ones) == 1 else default"
|
4971,"def same(iterable, ref=_UNSET):
|
""""""``same()`` returns ``True`` when all values in *iterable* are
|
equal to one another, or optionally a reference value,
|
*ref*. Similar to :func:`all` and :func:`any` in that it evaluates
|
an iterable and returns a :class:`bool`. ``same()`` returns
|
``True`` for empty iterables.
|
>>> same([])
|
True
|
>>> same([1])
|
True
|
>>> same(['a', 'a', 'a'])
|
True
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.