Skip to content

Commit

Permalink
refactor Grep
Browse files Browse the repository at this point in the history
  • Loading branch information
tandav committed Nov 30, 2023
1 parent 2753d7d commit 2bdd140
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 8 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
.PHONY: test
test:
pytest

.PHONY: doctest
doctest:
python -m doctest docs/reference.md

.PHONY: coverage
Expand Down
4 changes: 2 additions & 2 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ Same as `FilterKeys` but for `v` in `(k, v)` pairs

# case-insensitive
>>> ['hello foo', 'world', 'awesome FOo'] | Grep('foo', i=True) | Pipe(list)
['hello foo', 'awesome foo']
['hello foo', 'awesome FOo']
>>> ['hello foo', 'world', 'awesome FOo'] | Grep('Foo', i=True) | Pipe(list)
['hello foo', 'awesome foo']
['hello foo', 'awesome FOo']

# invert match
>>> ['hello foo', 'world', 'awesome FOo'] | Grep('foo', v=True) | Pipe(list)
Expand Down
2 changes: 1 addition & 1 deletion pipe21.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ValueBy (B): __ror__ = lambda self, it: it | Map(lambda x: (x, self.f
class Append (B): __ror__ = lambda self, it: it | Map(lambda x: (*x, self.f(x)))
class Keys (B): __ror__ = lambda self, it: it | Map(lambda kv: kv[0])
class Values (B): __ror__ = lambda self, it: it | Map(lambda kv: kv[1])
class Grep (B): __ror__ = lambda self, it: it | MapSwitch([(lambda x: self.kw.setdefault('i', False), str.lower)]) | (FilterFalse if self.kw.get('v', False) else Filter)(lambda x: re.search(self.f.lower() if self.kw['i'] else self.f, x))
class Grep (B): __ror__ = lambda self, it: it | (FilterFalse if self.kw.get('v', False) else Filter)(re.compile(self.f, flags=re.I if self.kw.get('i', False) else 0).search)
class IterLines (B): __ror__ = lambda self, p: p | Pipe(open) | Pipe(lambda t: t | Map(str.strip) if self.kw.get('strip', True) else t)
class Count (B): __ror__ = lambda self, it: sum(1 for _ in it)
class Slice (B): __ror__ = lambda self, it: itertools.islice(it, self.f, *self.args)
Expand Down
4 changes: 2 additions & 2 deletions tests/pipe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ def test_grep_v(it, grep, expected):
@pytest.mark.parametrize(
('it', 'grep', 'v', 'i', 'expected'), [
(['hello foo', 'world', 'awesome FOo'], 'foo', False, False, ['hello foo']),
(['hello foo', 'world', 'awesome FOo'], 'foo', False, True, ['hello foo', 'awesome foo']),
(['hello foo', 'world', 'awesome FOo'], 'Foo', False, True, ['hello foo', 'awesome foo']),
(['hello foo', 'world', 'awesome FOo'], 'foo', False, True, ['hello foo', 'awesome FOo']),
(['hello foo', 'world', 'awesome FOo'], 'Foo', False, True, ['hello foo', 'awesome FOo']),
(['hello foo', 'world', 'awesome FOo'], 'foo', True, False, ['world', 'awesome FOo']),
(['hello foo', 'world', 'awesome FOo'], 'foo', True, True, ['world']),
(['hello foo', 'world', 'awesome FOo'], 'Foo', True, True, ['world']),
Expand Down

0 comments on commit 2bdd140

Please sign in to comment.