Skip to content
This repository has been archived by the owner on Jan 31, 2021. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abersheeran committed Jan 12, 2021
1 parent a4a87c1 commit 4051dc5
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Pipe.py

A non-intrusive Python pipeline.

There are only pipeline. If you want more functional tools, you should look for another library.

## Install

```
pip install only-pipe
```

Or fetch from github

```
pip install git+https://github.com/abersheeran/only-pipe
```

## Usage

Use pipeline to pass data as a positional parameter to the next function:

```python
from pipe import F

range(10) | F(filter, lambda x: x % 2) | F(sum) == 25
```

Or you need to pass multiple parameters through the pipeline:

```python
from pipe import FF


def get_data():
return 1, 2


get_data() | FF(lambda x, y: x + y) == 3
```

## Set Global

Maybe you don't want to use `from pipe import F` in every file of the entire project, you can use the following code to set it as a global function, just like `min`/`max`/`sum`.

```python
import pipe

pipe.set_global(pipe.F, pipe.FF)
```

Maybe you also want to expose `functools.reduce` to the world, just like `map`/`filter`.

```python
import pipe
import functools

pipe.set_global(pipe.F, pipe.FF, functools.reduce)
```

## More

No more ~ This library has less than ten lines of valid code, but it is very effective.

If you like it, please give a Star. 😀

This repository is released under the MIT. Do what you want!
33 changes: 33 additions & 0 deletions pipe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from functools import partial


class F(partial):
"""
Python Pipe. e.g.`range(10) | F(filter, lambda x: x % 2) | F(sum)`
WRANING: There will be a small performance loss when building a
pipeline. Please do not use it in performance-sensitive locations.
"""

def __ror__(self, other):
return self(other)


class FF(partial):
"""
Python Pipe. e.g.`("f", 10) | FF(lambda letter, num: letter * num)`
WRANING: There will be a small performance loss when building a
pipeline. Please do not use it in performance-sensitive locations.
"""

def __ror__(self, other):
if not isinstance(other, tuple):
raise TypeError("FF can only accept tuple as parameters")
return self(*other)


def set_global(*args):
import builtins

args | F(map, lambda arg: setattr(builtins, arg.__name__, arg)) | F(list)
31 changes: 31 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/python3
import os
from setuptools import setup


with open("./README.md", "r", encoding="utf8") as file:
long_description = file.read()


setup_kwargs = {
"name": "only-pipe",
"version": "1.0.0",
"description": "A non-intrusive Python pipeline.",
"long_description": long_description,
"long_description_content_type": "text/markdown",
"author": "abersheeran",
"author_email": "[email protected]",
"url": "https://github.com/abersheeran/only-pipe",
"license": "MIT",
"classifiers": [
# Trove classifiers
# Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
],
"py_modules": ["pipe"],
}

setup(**setup_kwargs)
11 changes: 11 additions & 0 deletions test_pipe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pipe
import functools

pipe.set_global(pipe.F, pipe.FF, functools.reduce)

assert range(10) | F(filter, lambda x: x % 2) | F(sum) == 25
assert (1, 2, 3) | F(sum) == 6

assert (1, 2) | FF(lambda x, y: x + y) == 3

assert range(10) | F(reduce, lambda x, y: x + y) == 45

0 comments on commit 4051dc5

Please sign in to comment.