Skip to content

Commit

Permalink
add MethodCaller #31
Browse files Browse the repository at this point in the history
  • Loading branch information
tandav committed May 24, 2023
1 parent 907d649 commit 80fda2b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,23 @@ namespace()

```

## MethodCaller

```py
>>> class K:
... def hello(self):
... return 'hello'
... def increment(self, i, add=1):
... return i + add
>>> k = K()
>>> k | MethodCaller('hello')
'hello'
>>> k | MethodCaller('increment', 1)
2
>>> k | MethodCaller('increment', 1, add=2)
3

```

## Unique

Expand Down
1 change: 1 addition & 0 deletions pipe21.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class MapDelItem (B): __ror__ = lambda self, it: it | Map(lambda kv: kv | DelI
class MapGetAttr (B): __ror__ = lambda self, it: it | Map(lambda kv: kv | GetAttr(self.f))
class MapSetAttr (B): __ror__ = lambda self, it: it | Map(lambda kv: kv | SetAttr(self.f, self.args[0]))
class MapDelAttr (B): __ror__ = lambda self, it: it | Map(lambda kv: kv | DelAttr(self.f))
class MethodCaller (B): __ror__ = lambda self, x: operator.methodcaller(self.f, *self.args, **self.kw)(x)


class Unique(B):
Expand Down
15 changes: 15 additions & 0 deletions tests/pipe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,21 @@ def test_descriptors():
assert [SimpleNamespace(a='b')] | MapDelAttr('a') | Pipe(list) == [SimpleNamespace()]


class K:
def hello(self):
return 'hello'

def increment(self, i, add=1):
return i + add


def test_methodcaller():
k = K()
assert k | MethodCaller('hello') == 'hello'
assert k | MethodCaller('increment', 1) == 2
assert k | MethodCaller('increment', 1, add=2) == 3


@pytest.mark.parametrize(
('seq', 'key', 'expected'), [
([0, 1, 1, 2], None, [0, 1, 2]),
Expand Down

0 comments on commit 80fda2b

Please sign in to comment.