Skip to content

Commit

Permalink
Implement batched from an idea of @YashAgarwal in #85.
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienPalard committed Dec 1, 2023
1 parent 05d6b49 commit a8f0226
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ See the `Constructing your own` paragraph below.

Alphabetical list of available pipes; when several names are listed for a given pipe, these are aliases.

## `batched`

Like Python 3.12 `itertool.batched`:

```python
>>> from pipe import batched
>>> list("ABCDEFG" | batched(3))
[('A', 'B', 'C'), ('D', 'E', 'F'), ('G',)]

```

## `chain`

Chain a sequence of iterables:
Expand Down
7 changes: 7 additions & 0 deletions pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ def transpose(iterable):
return list(zip(*iterable))


@Pipe
def batched(iterable, n):
iterator = iter(iterable)
while batch := tuple(itertools.islice(iterator, n)):
yield batch


chain = Pipe(itertools.chain.from_iterable)
chain_with = Pipe(itertools.chain)
islice = Pipe(itertools.islice)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ classifiers = [
"Topic :: Software Development",
"Topic :: Software Development :: Libraries :: Python Modules",
]
requires-python = ">= 3.7"
requires-python = ">= 3.8"

[project.urls]
repository = "https://github.com/JulienPalard/Pipe"
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exclude_lines =
if __name__ == .__main__.:

[tox]
envlist = py3{8,9,10,11,12}, flake8, mypy, black, pylint, coverage
envlist = py3{8,9,10,11,12,13}, flake8, mypy, black, pylint, coverage
isolated_build = True

[testenv]
Expand Down

0 comments on commit a8f0226

Please sign in to comment.