Skip to content

Commit

Permalink
delete GrepV, use v=True argument
Browse files Browse the repository at this point in the history
  • Loading branch information
tandav committed Nov 29, 2023
1 parent 40039db commit fc7a69f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
16 changes: 6 additions & 10 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,16 @@ Same as `FilterKeys` but for `v` in `(k, v)` pairs
>>> ['hello foo', 'world', 'awesome FOo'] | Grep('Foo', i=True) | Pipe(list)
['hello foo', 'awesome foo']

```

## GrepV

```py
>>> ['hello foo', 'world', 'awesome FOo'] | GrepV('foo') | Pipe(list)
# invert match
>>> ['hello foo', 'world', 'awesome FOo'] | Grep('foo', v=True) | Pipe(list)
['world', 'awesome FOo']
>>> ['foo1', 'foo2', '3foo', 'bar1'] | GrepV('^foo.*') | Pipe(list)
>>> ['foo1', 'foo2', '3foo', 'bar1'] | Grep('^foo.*', v=True) | Pipe(list)
['3foo', 'bar1']

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

```
Expand Down
3 changes: 1 addition & 2 deletions pipe21.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +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)]) | Filter(lambda x: re.search(self.f.lower() if self.kw['i'] else self.f, x))
class GrepV (B): __ror__ = lambda self, it: it | MapSwitch([(lambda x: self.kw.setdefault('i', False), str.lower)]) | FilterFalse(lambda x: re.search(self.f.lower() if self.kw['i'] else self.f, x))
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 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
20 changes: 10 additions & 10 deletions tests/pipe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,21 @@ def test_grep(it, grep, expected):
],
)
def test_grep_v(it, grep, expected):
assert it | GrepV(grep) | Pipe(list) == expected
assert it | Grep(grep, v=True) | Pipe(list) == expected


@pytest.mark.parametrize(
('it', 'op', 'grep', 'i', 'expected'), [
(['hello foo', 'world', 'awesome FOo'], Grep, 'foo', False, ['hello foo']),
(['hello foo', 'world', 'awesome FOo'], Grep, 'foo', True, ['hello foo', 'awesome foo']),
(['hello foo', 'world', 'awesome FOo'], Grep, 'Foo', True, ['hello foo', 'awesome foo']),
(['hello foo', 'world', 'awesome FOo'], GrepV, 'foo', False, ['world', 'awesome FOo']),
(['hello foo', 'world', 'awesome FOo'], GrepV, 'foo', True, ['world']),
(['hello foo', 'world', 'awesome FOo'], GrepV, 'Foo', True, ['world']),
('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', True, False, ['world', 'awesome FOo']),
(['hello foo', 'world', 'awesome FOo'], 'foo', True, True, ['world']),
(['hello foo', 'world', 'awesome FOo'], 'Foo', True, True, ['world']),
],
)
def test_grep_i(it, op, grep, i, expected):
assert it | op(grep, i=i) | Pipe(list) == expected
def test_grep_i(it, v, grep, i, expected):
assert it | Grep(grep, v=v, i=i) | Pipe(list) == expected


def test_iter_lines(tmp_path):
Expand Down

0 comments on commit fc7a69f

Please sign in to comment.