Skip to content

Commit 30900c4

Browse files
committed
actually commit more_itertools replacement
1 parent 8ca822f commit 30900c4

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/dbt_score/more_itertools.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Vendored utility functions from https://github.com/more-itertools/more-itertools."""
2+
3+
4+
def first_true(iterable, default=None, pred=None):
5+
"""Returns the first true value in the iterable.
6+
7+
If no true value is found, returns *default*
8+
9+
If *pred* is not None, returns the first item for which
10+
``pred(item) == True`` .
11+
12+
>>> first_true(range(10))
13+
1
14+
>>> first_true(range(10), pred=lambda x: x > 5)
15+
6
16+
>>> first_true(range(10), default='missing', pred=lambda x: x > 9)
17+
'missing'
18+
19+
"""
20+
return next(filter(pred, iterable), default)

tests/test_more_itertools.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Tests for vendored more_itertools functions."""
2+
from dbt_score.more_itertools import first_true
3+
4+
5+
class TestFirstTrue:
6+
"""Tests for `first_true`."""
7+
8+
def test_something_true(self):
9+
"""Test with no keyword arguments."""
10+
assert first_true(range(10), 1)
11+
12+
def test_nothing_true(self):
13+
"""Test default return value."""
14+
assert first_true([0, 0, 0]) is None
15+
16+
def test_default(self):
17+
"""Test with a default keyword."""
18+
assert first_true([0, 0, 0], default="!") == "!"
19+
20+
def test_pred(self):
21+
"""Test with a custom predicate."""
22+
assert first_true([2, 4, 6], pred=lambda x: x % 3 == 0) == 6

0 commit comments

Comments
 (0)