8.2.34 | Linux | www-data (uid:33)
👁 View: _itertools.py
📁 /snap/core24/1643/usr/lib/python3.12/importlib/resources/_itertools.py
# from more_itertools 9.0 def only(iterable, default=None, too_long=None): """If *iterable* has only one item, return it. If it has zero items, return *default*. If it has more than one item, raise the exception given by *too_long*, which is ``ValueError`` by default. >>> only([], default='missing') 'missing' >>> only([1]) 1 >>> only([1, 2]) # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... ValueError: Expected exactly one item in iterable, but got 1, 2, and perhaps more.' >>> only([1, 2], too_long=TypeError) # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... TypeError Note that :func:`only` attempts to advance *iterable* twice to ensure there is only one item. See :func:`spy` or :func:`peekable` to check iterable contents less destructively. """ it = iter(iterable) first_value = next(it, default) try: second_value = next(it) except StopIteration: pass else: msg = ( 'Expected exactly one item in iterable, but got {!r}, {!r}, ' 'and perhaps more.'.format(first_value, second_value) ) raise too_long or ValueError(msg) return first_value
← Back
💻 Terminal
📤 Upload
📂
Click or Drag & Drop
📁 /snap/core24/1643/usr/lib/python3.12/importlib/resources
NameSizePermModifiedActions
📁 __pycache__/-07552026-04-10 04:18🗑 Delete
__init__.py506B06442026-03-03 12:15👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
_adapters.py4.4K06442026-03-03 12:15👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
_common.py5.3K06442026-03-03 12:15👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
_itertools.py1.2K06442026-03-03 12:15👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
_legacy.py3.4K06442026-03-03 12:15👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
abc.py5.1K06442026-03-03 12:15👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
readers.py4.2K06442026-03-03 12:15👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del
simple.py2.5K06442026-03-03 12:15👁 View ✏️ Edit 📝 Rename 📦 Move 🗑 Del