Skip to content
Snippets Groups Projects
Unverified Commit a3c42592 authored by Piotr Maślanka's avatar Piotr Maślanka Committed by GitHub
Browse files

v2.2.17 - added coding.sequences.choose (#38)

* v2.2.17 - added coding.sequences.choose

* link to GitHub at primary page of docs

* tests for python 3.5 + code reformat

* moved requirements to requirements.txt instead of .travis.yml
parent 7352aa3b
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,6 @@ python: ...@@ -9,7 +9,6 @@ python:
cache: pip cache: pip
install: install:
- pip install -r requirements.txt - pip install -r requirements.txt
- pip install pyyaml toml
- pip install --force-reinstall "coverage>=4.0,<4.4" codeclimate-test-reporter # for codeclimate-test-reporter - pip install --force-reinstall "coverage>=4.0,<4.4" codeclimate-test-reporter # for codeclimate-test-reporter
script: script:
- bash tests/test_posix/test_hang_until_sig.sh - bash tests/test_posix/test_hang_until_sig.sh
......
# v2.2.17 # v2.2.17
* _TBA_ * added [choose](satella/coding/sequences/choose.py)
# v2.2.16 # v2.2.16
......
choose
======
To return the single element that returns true on given callable, use the following function:
.. autofunction:: satella.coding.sequences.choose
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
Welcome to satella's documentation! Welcome to satella's documentation!
=================================== ===================================
Visit the project's page at GitHub_!
.. _GitHub: https://github.com/piotrmaslanka/satella
.. toctree:: .. toctree::
:maxdepth: 2 :maxdepth: 2
:caption: Contents :caption: Contents
...@@ -11,6 +15,7 @@ Welcome to satella's documentation! ...@@ -11,6 +15,7 @@ Welcome to satella's documentation!
coding/functions coding/functions
coding/structures coding/structures
coding/concurrent coding/concurrent
coding/sequences
instrumentation/traceback instrumentation/traceback
instrumentation/metrics instrumentation/metrics
exception_handling exception_handling
......
# coding=UTF-8 # coding=UTF-8
__version__ = '2.2.17a1' __version__ = '2.2.17'
from .choose import choose
__all__ = ['choose']
import typing as tp
T = tp.TypeVar('T')
__all__ = ['choose']
def choose(filter_fun: tp.Callable[[T], bool], iterable: tp.Iterable[T]) -> T:
"""
Return a single value that exists in given iterable
:param filter_fun: function that returns bool on the single value
:param iterable: iterable to examine
:return: single element in the iterable that matches given input
:raises ValueError: on multiple elements matching, or none at all
"""
elem_candidate = None
found = False
for elem in iterable:
if filter_fun(elem):
if found:
raise ValueError(
'Multiple values (%s, %s) seen' % (repr(elem_candidate), repr(elem)))
elem_candidate = elem
found = True
if not found:
raise ValueError('No elements matching given filter seen')
return elem_candidate
import logging
import typing as tp
import unittest
from satella.coding.sequences import choose
logger = logging.getLogger(__name__)
class TestSequences(unittest.TestCase):
def test_choose(self):
self.assertEqual(choose(lambda x: x == 2, [1, 2, 3, 4, 5]), 2)
self.assertRaises(ValueError, lambda: choose(lambda x: x % 2 == 0, [1, 2, 3, 4, 5]))
self.assertRaises(ValueError, lambda: choose(lambda x: x == 0, [1, 2, 3, 4, 5]))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment