Name | pipe-operator JSON |
Version |
1.1.0
JSON |
| download |
home_page | None |
Summary | Elixir's pipe operator in Python |
upload_time | 2024-11-23 21:59:15 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.9 |
license | Copyright 2018 Robin Hilliard Copyright 2024 Jordan Kowal Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
pipe
operator
elixir
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# ✨ Pipe Operator ✨







- [✨ Pipe Operator ✨](#-pipe-operator-)
- [⚡ Quick start](#-quick-start)
- [📕 Overview](#-overview)
- [🐍 Pythonic implementation](#-pythonic-implementation)
- [Available classes](#available-classes)
- [Limitations](#limitations)
- [🍹 Elixir-like implementation](#-elixir-like-implementation)
- [Overview](#overview)
- [Operations and shortcuts](#operations-and-shortcuts)
- [How it works](#how-it-works)
- [Linters and type-checkers issues](#linters-and-type-checkers-issues)
- [Performances](#performances)
- [🔗 Useful links](#-useful-links)
`pipe_operator` allows you to use an elixir pipe-like syntax in python.
This module provides 2 vastly different implementations, each with its own pros and cons.
## ⚡ Quick start
As simple as `pip install pipe_operator`.
Then either import the 🐍 **pythonic classes** or the 🍹 **elixir functions**
```python
# Pythonic classes
from pipe_operator import Pipe, PipeArgs, PipeEnd, PipeStart, Tap, Then, ThreadPipe, ThreadWait
# Elixir functions
from pipe_operator import elixir_pipe, tap, then
```
## 📕 Overview
You can use the 🐍 **pythonic** implementation, which is **entirely compatible with linters and type-checkers**,
but a bit more verbose than the original pipe operator:
```python
from pipe_operator import Pipe, PipeArgs, PipeEnd, PipeStart, Tap, Then, ThreadPipe, ThreadWait
result = (
PipeStart("3") # starts the pipe
>> Pipe(int) # function with 1-arg
>> Pipe(my_func, 2000, z=10) # function with multiple args
>> Tap(print) # side effect
>> Then(lambda x: x + 1) # lambda
>> Pipe(MyClass) # class
>> Pipe(MyClass.my_classmethod) # classmethod
>> Tap(MyClass.my_method) # side effect that can update the original object
>> Pipe(MyClass.my_other_method) # method
>> Then[int, int](lambda x: x * 2) # explicitly-typed lambda
>> PipeArgs(my_other_func, 4, 5, 6) # special case for functions with no positional/keyword parameters
>> ThreadPipe("t1", do_something) # thread
>> ThreadWait(["t1"]) # wait for thread(s)
>> PipeEnd() # extract the value
)
```
Or the 🍹 **elixir-like** implementation, whose syntax greatly resembles the original pipe operator,
but has major issues with linters and type-checkers.
```python
from pipe_operator import elixir_pipe, tap, then
@elixir_pipe
def workflow(value):
results = (
value # raw value
>> BasicClass # class call
>> _.value # property (shortcut)
>> BasicClass() # class call
>> _.get_value_plus_arg(10) # method call
>> 10 + _ - 5 # binary operation (shortcut)
>> {_, 1, 2, 3} # object creation (shortcut)
>> [x for x in _ if x > 4] # comprehension (shortcut)
>> (lambda x: x[0]) # lambda (shortcut)
>> my_func(_) # function call
>> tap(my_func) # side effect
>> my_other_func(2, 3) # function call with extra args
>> then(lambda a: a + 1) # then
>> f"value is {_}" # formatted string (shortcut)
)
return results
workflow(3)
```
## 🐍 Pythonic implementation
### Available classes
In the 🐍 **pythonic implementation**, we expose the following classes:
| Class | Description | Examples |
| ------------ | --------------------------------------------------------------------- | ----------------------------------------- |
| `PipeStart` | The start of the pipe | `PipeStart("3")` |
| `Pipe` | Used to call almost any functions or classes, or methods | `Pipe(int)`, `Pipe(my_func, 2000, z=10)` |
| `PipeArgs` | Same as `Pipe` but for function with no positional/keyword parameters | `PipeArgs(func, 1, 2)` |
| `Then` | Same as `Pipe`, but for 1-arg lambda functions | `Then(lambda x: x.attribute)` |
| `Tap` | Used to trigger a side effect (meaning it returns the original value) | `Tap(print)`, `Tap(lambda x: x.method())` |
| `ThreadPipe` | Used to call a function in a thread | `ThreadPipe("t1", do_something)()` |
| `ThreadWait` | Used to wait for multiple (or all)threads to finish | `ThreadWait()`, `ThreadWait(["id1"])` |
| `PipeEnd` | The end of the pipe, to extract the raw final result | `PipeEnd()` |
### Limitations
**property:** Properties cannot be called directly. You must resort to the use of `Then(lambda x: x.my_property)`.
This will work just fine and ensure type-safety throughout the pipe.
**functions without positional/keyword parameters:** While they are technically supported by the `Pipe` class,
your type-checker will not handle them properly, because the `Pipe` class expect the function to have
at least 1 positional/keyword parameter (ie the first one, passed down the pipe). To bypass this limitation,
you should use `PipeArgs` instead.
**pyright:** `pyright` seems to have trouble dealing with our `>>` in some specific cases. As such,
we advise you set `reportOperatorIssue = "none"` in your `pyright` config.
## 🍹 Elixir-like implementation
### Overview
In the 🍹 **elixir-like implementation**, we expose 3 functions:
- `elixir_pipe`: a decorator that enables the use of "pipe" in our function
- `tap`: a function to trigger a side-effect and return the original value
- `then`: (optional) the proper way to pass lambdas into the pipe
The `elixir_pipe` decorator can take arguments allowing you to customize
```python
# Those are the default args
@elixir_pipe(placeholder="_", lambda_var="_pipe_x", operator=">>", debug=False)
def my_function()
...
```
- `placeholder`: The expected variable used in shortcut like `_.property`
- `lambda_var`: The variable named used internally when we generate lambda function. You'll likely never change this
- `operator`: The operator used in the pipe
- `debug`: If true, will print the output after each pipe operation
### Operations and shortcuts
Initially, all operations can be supported through the base operations,
with `lambdas` allowing you to perform any other operations. To make lambda usage cleaner,
you can write them into `then` calls as well.
| Operation | Input | Output |
| ------------------------- | ------------------------ | ---------------------- |
| function calls | `a >> b(...)` | `b(a, ...)` |
| class calls | `a >> B(...)` | `B(a, ...)` |
| calls without parenthesis | `a >> b` | `b(a)` |
| lambda calls | `a >> (lambda x: x + 4)` | `(lambda x: x + 4)(a)` |
However, we've also added shortcuts, based on the `placeholder` argument, allowing you
to skip the lambda declaration and directly perform the following operations:
| Operation | Input | Output |
| --------------------------- | -------------------------------- | ------------------------------------------ |
| method calls | `a >> _.method(...)` | `a.method(...)` |
| property calls | `a >> _.property` | `a.property` |
| binary operators | `a >> _ + 3` | `(lambda Z: Z + 3)(a)` |
| f-strings | `a >> f"{_}"` | `(lambda Z: f"{Z}")(a)` |
| list/set/... creations | `a >> [_, 1, 2]` | `(lambda Z: [Z, 1, 2])(a)` |
| list/set/... comprehensions | `a >> [x + _ for x in range(_)]` | `(lambda Z: [x + Z for x in range(Z)])(a)` |
### How it works
Here's quick rundown of how it works. Feel free to inspect the source code or the tests.
Once you've decorated your function and run the code:
- We pull the AST from the original function
- We remove our own decorator, to avoid recursion and impacting other functions
- We then rewrite the AST, following a specific set of rules (as shown in the table below)
- And finally we execute the re-written AST
Eventually, `a >> b(...) >> c(...)` becomes `c(b(a, ...), ...)`.
### Linters and type-checkers issues
Sadly, this implementation comes short when dealing with linters (like `ruff` or `flake8`)
and type-checkers (like `mypy` or `pyright`). Because these are static code analyzers, they inspect
the original code, and not your AST-modified version. To bypass the errors, you'll need to disable
the following:
- `mypy`: Either ignore `operator,call-arg,call-overload,name-defined`, or ignore just `name-defined` and use the `@no_type_check` decorator
- `pyright`: Set `reportOperatorIssue`, `reportCallIssue`, `reportUndefinedVariable` to `none`
- `ruff`: Disable the `F821` error
- `flake8`: Disable the `F821` error
### Performances
In terms of performances, this implementation should add very little overhead.
The decorator and AST rewrite are run **only once at compile time**, and while it does
generate a few extra lambda functions, it also removes the need for intermediate
variables.
## 🔗 Useful links
- [Want to contribute?](CONTRIBUTING.md)
- [See what's new!](CHANGELOG.md)
- Originally forked from [robinhilliard/pipes](https://github.com/robinhilliard/pipes)
Raw data
{
"_id": null,
"home_page": null,
"name": "pipe-operator",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": "Jordan Kowal <kowaljordan@gmail.com>",
"keywords": "pipe, operator, elixir",
"author": null,
"author_email": "Jordan Kowal <kowaljordan@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/30/ed/7dd8ed5044cd79a51b996fa7acaccfae0da940d23a042f066df314bb1dcc/pipe_operator-1.1.0.tar.gz",
"platform": null,
"description": "# \u2728 Pipe Operator \u2728\n\n\n\n\n\n\n\n\n\n- [\u2728 Pipe Operator \u2728](#-pipe-operator-)\n - [\u26a1 Quick start](#-quick-start)\n - [\ud83d\udcd5 Overview](#-overview)\n - [\ud83d\udc0d Pythonic implementation](#-pythonic-implementation)\n - [Available classes](#available-classes)\n - [Limitations](#limitations)\n - [\ud83c\udf79 Elixir-like implementation](#-elixir-like-implementation)\n - [Overview](#overview)\n - [Operations and shortcuts](#operations-and-shortcuts)\n - [How it works](#how-it-works)\n - [Linters and type-checkers issues](#linters-and-type-checkers-issues)\n - [Performances](#performances)\n - [\ud83d\udd17 Useful links](#-useful-links)\n\n`pipe_operator` allows you to use an elixir pipe-like syntax in python.\nThis module provides 2 vastly different implementations, each with its own pros and cons.\n\n## \u26a1 Quick start\n\nAs simple as `pip install pipe_operator`.\nThen either import the \ud83d\udc0d **pythonic classes** or the \ud83c\udf79 **elixir functions**\n\n```python\n# Pythonic classes\nfrom pipe_operator import Pipe, PipeArgs, PipeEnd, PipeStart, Tap, Then, ThreadPipe, ThreadWait\n# Elixir functions\nfrom pipe_operator import elixir_pipe, tap, then\n```\n\n## \ud83d\udcd5 Overview\n\nYou can use the \ud83d\udc0d **pythonic** implementation, which is **entirely compatible with linters and type-checkers**,\nbut a bit more verbose than the original pipe operator:\n\n```python\nfrom pipe_operator import Pipe, PipeArgs, PipeEnd, PipeStart, Tap, Then, ThreadPipe, ThreadWait\n\nresult = (\n PipeStart(\"3\") # starts the pipe\n >> Pipe(int) # function with 1-arg\n >> Pipe(my_func, 2000, z=10) # function with multiple args\n >> Tap(print) # side effect\n >> Then(lambda x: x + 1) # lambda\n >> Pipe(MyClass) # class\n >> Pipe(MyClass.my_classmethod) # classmethod\n >> Tap(MyClass.my_method) # side effect that can update the original object\n >> Pipe(MyClass.my_other_method) # method\n >> Then[int, int](lambda x: x * 2) # explicitly-typed lambda\n >> PipeArgs(my_other_func, 4, 5, 6) # special case for functions with no positional/keyword parameters\n >> ThreadPipe(\"t1\", do_something) # thread\n >> ThreadWait([\"t1\"]) # wait for thread(s)\n >> PipeEnd() # extract the value\n)\n```\n\nOr the \ud83c\udf79 **elixir-like** implementation, whose syntax greatly resembles the original pipe operator,\nbut has major issues with linters and type-checkers.\n\n```python\nfrom pipe_operator import elixir_pipe, tap, then\n\n@elixir_pipe\ndef workflow(value):\n results = (\n value # raw value\n >> BasicClass # class call\n >> _.value # property (shortcut)\n >> BasicClass() # class call\n >> _.get_value_plus_arg(10) # method call\n >> 10 + _ - 5 # binary operation (shortcut)\n >> {_, 1, 2, 3} # object creation (shortcut)\n >> [x for x in _ if x > 4] # comprehension (shortcut)\n >> (lambda x: x[0]) # lambda (shortcut)\n >> my_func(_) # function call\n >> tap(my_func) # side effect\n >> my_other_func(2, 3) # function call with extra args\n >> then(lambda a: a + 1) # then\n >> f\"value is {_}\" # formatted string (shortcut)\n )\n return results\n\nworkflow(3)\n```\n\n## \ud83d\udc0d Pythonic implementation\n\n### Available classes\n\nIn the \ud83d\udc0d **pythonic implementation**, we expose the following classes:\n\n| Class | Description | Examples |\n| ------------ | --------------------------------------------------------------------- | ----------------------------------------- |\n| `PipeStart` | The start of the pipe | `PipeStart(\"3\")` |\n| `Pipe` | Used to call almost any functions or classes, or methods | `Pipe(int)`, `Pipe(my_func, 2000, z=10)` |\n| `PipeArgs` | Same as `Pipe` but for function with no positional/keyword parameters | `PipeArgs(func, 1, 2)` |\n| `Then` | Same as `Pipe`, but for 1-arg lambda functions | `Then(lambda x: x.attribute)` |\n| `Tap` | Used to trigger a side effect (meaning it returns the original value) | `Tap(print)`, `Tap(lambda x: x.method())` |\n| `ThreadPipe` | Used to call a function in a thread | `ThreadPipe(\"t1\", do_something)()` |\n| `ThreadWait` | Used to wait for multiple (or all)threads to finish | `ThreadWait()`, `ThreadWait([\"id1\"])` |\n| `PipeEnd` | The end of the pipe, to extract the raw final result | `PipeEnd()` |\n\n### Limitations\n\n**property:** Properties cannot be called directly. You must resort to the use of `Then(lambda x: x.my_property)`.\nThis will work just fine and ensure type-safety throughout the pipe.\n\n**functions without positional/keyword parameters:** While they are technically supported by the `Pipe` class,\nyour type-checker will not handle them properly, because the `Pipe` class expect the function to have\nat least 1 positional/keyword parameter (ie the first one, passed down the pipe). To bypass this limitation,\nyou should use `PipeArgs` instead.\n\n**pyright:** `pyright` seems to have trouble dealing with our `>>` in some specific cases. As such,\nwe advise you set `reportOperatorIssue = \"none\"` in your `pyright` config.\n\n## \ud83c\udf79 Elixir-like implementation\n\n### Overview\n\nIn the \ud83c\udf79 **elixir-like implementation**, we expose 3 functions:\n\n- `elixir_pipe`: a decorator that enables the use of \"pipe\" in our function\n- `tap`: a function to trigger a side-effect and return the original value\n- `then`: (optional) the proper way to pass lambdas into the pipe\n\nThe `elixir_pipe` decorator can take arguments allowing you to customize\n\n```python\n# Those are the default args\n@elixir_pipe(placeholder=\"_\", lambda_var=\"_pipe_x\", operator=\">>\", debug=False)\ndef my_function()\n ...\n```\n\n- `placeholder`: The expected variable used in shortcut like `_.property`\n- `lambda_var`: The variable named used internally when we generate lambda function. You'll likely never change this\n- `operator`: The operator used in the pipe\n- `debug`: If true, will print the output after each pipe operation\n\n### Operations and shortcuts\n\nInitially, all operations can be supported through the base operations,\nwith `lambdas` allowing you to perform any other operations. To make lambda usage cleaner,\nyou can write them into `then` calls as well.\n\n| Operation | Input | Output |\n| ------------------------- | ------------------------ | ---------------------- |\n| function calls | `a >> b(...)` | `b(a, ...)` |\n| class calls | `a >> B(...)` | `B(a, ...)` |\n| calls without parenthesis | `a >> b` | `b(a)` |\n| lambda calls | `a >> (lambda x: x + 4)` | `(lambda x: x + 4)(a)` |\n\nHowever, we've also added shortcuts, based on the `placeholder` argument, allowing you\nto skip the lambda declaration and directly perform the following operations:\n\n| Operation | Input | Output |\n| --------------------------- | -------------------------------- | ------------------------------------------ |\n| method calls | `a >> _.method(...)` | `a.method(...)` |\n| property calls | `a >> _.property` | `a.property` |\n| binary operators | `a >> _ + 3` | `(lambda Z: Z + 3)(a)` |\n| f-strings | `a >> f\"{_}\"` | `(lambda Z: f\"{Z}\")(a)` |\n| list/set/... creations | `a >> [_, 1, 2]` | `(lambda Z: [Z, 1, 2])(a)` |\n| list/set/... comprehensions | `a >> [x + _ for x in range(_)]` | `(lambda Z: [x + Z for x in range(Z)])(a)` |\n\n### How it works\n\nHere's quick rundown of how it works. Feel free to inspect the source code or the tests.\nOnce you've decorated your function and run the code:\n\n- We pull the AST from the original function\n- We remove our own decorator, to avoid recursion and impacting other functions\n- We then rewrite the AST, following a specific set of rules (as shown in the table below)\n- And finally we execute the re-written AST\n\nEventually, `a >> b(...) >> c(...)` becomes `c(b(a, ...), ...)`.\n\n### Linters and type-checkers issues\n\nSadly, this implementation comes short when dealing with linters (like `ruff` or `flake8`)\nand type-checkers (like `mypy` or `pyright`). Because these are static code analyzers, they inspect\nthe original code, and not your AST-modified version. To bypass the errors, you'll need to disable\nthe following:\n\n- `mypy`: Either ignore `operator,call-arg,call-overload,name-defined`, or ignore just `name-defined` and use the `@no_type_check` decorator\n- `pyright`: Set `reportOperatorIssue`, `reportCallIssue`, `reportUndefinedVariable` to `none`\n- `ruff`: Disable the `F821` error\n- `flake8`: Disable the `F821` error\n\n### Performances\n\nIn terms of performances, this implementation should add very little overhead.\nThe decorator and AST rewrite are run **only once at compile time**, and while it does\ngenerate a few extra lambda functions, it also removes the need for intermediate\nvariables.\n\n## \ud83d\udd17 Useful links\n\n- [Want to contribute?](CONTRIBUTING.md)\n- [See what's new!](CHANGELOG.md)\n- Originally forked from [robinhilliard/pipes](https://github.com/robinhilliard/pipes)\n",
"bugtrack_url": null,
"license": "Copyright 2018 Robin Hilliard Copyright 2024 Jordan Kowal Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
"summary": "Elixir's pipe operator in Python",
"version": "1.1.0",
"project_urls": {
"Changelog": "https://github.com/Jordan-Kowal/pipe-operator/blob/main/CHANGELOG.md",
"Issues": "https://github.com/Jordan-Kowal/pipe-operator/issues",
"Release notes": "https://github.com/Jordan-Kowal/pipe-operator/releases",
"Source": "https://github.com/Jordan-Kowal/pipe-operator"
},
"split_keywords": [
"pipe",
" operator",
" elixir"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "779eede7bb83cd58d0be2a401934d041740a41a1a66585d3af5aaee268b50e6a",
"md5": "599998ef505932143f6151c8d6fb2018",
"sha256": "af58e3c54d4dd1cfa7db3a5c976ed9a6807ce1f016fb7f78f4953a5bb3bff488"
},
"downloads": -1,
"filename": "pipe_operator-1.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "599998ef505932143f6151c8d6fb2018",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 29524,
"upload_time": "2024-11-23T21:59:13",
"upload_time_iso_8601": "2024-11-23T21:59:13.510455Z",
"url": "https://files.pythonhosted.org/packages/77/9e/ede7bb83cd58d0be2a401934d041740a41a1a66585d3af5aaee268b50e6a/pipe_operator-1.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "30ed7dd8ed5044cd79a51b996fa7acaccfae0da940d23a042f066df314bb1dcc",
"md5": "fe397ca298a2e1e2f59fdb39cbf1ff56",
"sha256": "77ef9b8f63f9a3a0ef3a50e5960eff6cd5871fe1ec5a4eace129b93e9cd767ae"
},
"downloads": -1,
"filename": "pipe_operator-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "fe397ca298a2e1e2f59fdb39cbf1ff56",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 26152,
"upload_time": "2024-11-23T21:59:15",
"upload_time_iso_8601": "2024-11-23T21:59:15.055523Z",
"url": "https://files.pythonhosted.org/packages/30/ed/7dd8ed5044cd79a51b996fa7acaccfae0da940d23a042f066df314bb1dcc/pipe_operator-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-23 21:59:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Jordan-Kowal",
"github_project": "pipe-operator",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pipe-operator"
}