Skip to content

Commit

Permalink
edit examples for pipe with args
Browse files Browse the repository at this point in the history
  • Loading branch information
tandav committed May 14, 2023
1 parent 2beeee3 commit 5102660
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
29 changes: 26 additions & 3 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,34 @@ Put a value into a function as 1st argument
>>> range(5) | Pipe(list)
[0, 1, 2, 3, 4]

>>> 'FF' | Pipe(int, 16)
>>> 2 | Pipe(pow, 8)
256

>>> 'FF' | Pipe(int, base=16)
255

>>> 2 | Pipe(pow, exp=8)
256
>>> b'\x02\x00' | Pipe(int.from_bytes, byteorder='big')
512

>>> 'ab' | Pipe(enumerate, start=0) | Pipe(list)
[(0, 'a'), (1, 'b')]

>>> import math
>>> 5.01 | Pipe(math.isclose, 5, abs_tol=0.01)
True

>>> import random
>>> random.seed(44)
>>> [0, 1, 2] | Pipe(random.choices, [0.8, 0.15, 0.05], k=20)
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0]

>>> import itertools
>>> [0, 1, 2] | Pipe(itertools.zip_longest, 'ab', fillvalue=None) | Pipe(list)
[(0, 'a'), (1, 'b'), (2, None)]

>>> import operator
>>> [0, 1, 2] | Pipe(itertools.accumulate, operator.add, initial=100) | Pipe(list)
[100, 100, 101, 103]

```

Expand Down
13 changes: 11 additions & 2 deletions tests/pipe_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import functools
import itertools
import math
import operator
import random

Expand All @@ -25,8 +27,15 @@ def test_pipe(it):


def test_pipe_args_kwargs():
assert 'FF' | Pipe(int, 16) == 255
assert 2 | Pipe(pow, exp=8) == 256
assert 2 | Pipe(pow, 8) == 256
assert 'FF' | Pipe(int, base=16) == 255
assert b'\x02\x00' | Pipe(int.from_bytes, byteorder='big') == 512
assert 'ab' | Pipe(enumerate, start=0) | Pipe(list) == [(0, 'a'), (1, 'b')]
assert 5.01 | Pipe(math.isclose, 5, abs_tol=0.01) is True
random.seed(44)
assert [0, 1, 2] | Pipe(random.choices, [0.8, 0.15, 0.05], k=20) == [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0]
assert [0, 1, 2] | Pipe(itertools.zip_longest, 'ab', fillvalue=None) | Pipe(list) == [(0, 'a'), (1, 'b'), (2, None)]
assert [0, 1, 2] | Pipe(itertools.accumulate, operator.add, initial=100) | Pipe(list) == [100, 100, 101, 103]


@given(st.lists(st.integers() | st.characters() | st.floats() | st.booleans() | st.binary()))
Expand Down

0 comments on commit 5102660

Please sign in to comment.