Enables piping iterables to functions with the pipe character
Project description
Pipedi
A simple Python package that enables piping iterables to functions with the pipe | character like in a shell.
with open('input.txt', 'r') as input_file:
res = list(input_file \
| strip('\n') \
| tail(10) \
| rev() \
| grep('x', invert=True) \
| cut(delimiter=';', fields=[1, 0, 3]) \
| sort(delimiter=';', fields=[2, 1]) \
| tac()
)
print(res)
Install
pip install pipedi
Usage
It provides two decorators that enable a function to be used in a pipe.
from pipedi import piped, pipable
Use the pipable decorator for simple functions that edit items in the stream (i.e. don't remove or add items). These functions must take an items as the first parameter. Rest of the parameters can be anything.
@pipable
def wrap(item, pre, post):
return pre + item + post
Use the piped decorator for functions that edit the stream (e.g. filter, add, or enumerate items). These functions must take an iterable as the first parameter. The rest can be anything.
@piped
def grep(iterable, regex):
for line in iterable:
if re.search(regex, line) is not None:
yield line
@piped
def duplicate(iterable):
for i, line in enumerate(iterable):
yield line
yield line
@piped
def enum(iterable):
for i, line in enumerate(iterable):
yield (i, line)
When used in a pipe, the first parameters (item or iterable) are omitted. Rest of the arguments are provided to the function.
['apple', 'banana', 'cocos'] | grep('^.[aeiouy]') | wrap('[', ']')
See more examples in the tests directory and the src/pipedi/util directory.
The result of a pipe is a normal iterable, so you can use existing tools on it.
For example to duplicate a stream, you can use itertools.dup.
from itertools import dup
with open('input.txt', 'r') as input_file:
lines = input_file | strip('\n')
input1, input2 = dup(lines)
lines_with_x = input1 | grep('x')
lines_without_x = input2 | grep('x', invert=True)
Building
# Clone this repository
$ git clone git@github.com:akupar/pipedi.git
$ cd pipedi
# Create virtual environment
.../pipedi$ python -m venv venv
# Activate virtual environment
.../pipedi$ . venv/bin/activate
# Install requirements
(venv) .../pipedi$ pip install -r requirements.txt
# Install the pipedi package in editable mode
(venv) .../pipedi$ pip install -e .
# Run tests
(venv) .../pipedi$ python -m pytest
# Preview readme
(venv) .../pipedi$ grip -b README.md
# Build package
(venv) .../pipedi$ python -m build
``´
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pipedi-0.0.2.tar.gz.
File metadata
- Download URL: pipedi-0.0.2.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d4078a52b4a3373dd4ea0d8f68655cf3194d90dfb84e0287c64aec4805fc227
|
|
| MD5 |
09d944252a692ab07094294a47ff71fb
|
|
| BLAKE2b-256 |
64186ba2f0c4d33ee1aaf1ed265bd7364f952aebcacb0d2e326d913c6c55e2bc
|
File details
Details for the file pipedi-0.0.2-py3-none-any.whl.
File metadata
- Download URL: pipedi-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b4f07eebf2df3c31fd490ec6f40edc62db19afca956ad6c66eecc8fd85c5165
|
|
| MD5 |
b6a919bd6bf3563624cb4d2335632891
|
|
| BLAKE2b-256 |
64fea322cdd68e2d0aae145c81f2c2da4313843edb01007c38137c91c414ccb7
|