https://github.com/abersheeran/cool
Make Python code cooler. Less is more.
https://github.com/abersheeran/cool
pipeline python redirect
Last synced: 5 months ago
JSON representation
Make Python code cooler. Less is more.
- Host: GitHub
- URL: https://github.com/abersheeran/cool
- Owner: abersheeran
- License: apache-2.0
- Created: 2021-01-31T07:37:31.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-03-25T09:17:32.000Z (over 2 years ago)
- Last Synced: 2025-05-04T23:39:05.884Z (5 months ago)
- Topics: pipeline, python, redirect
- Language: Python
- Homepage:
- Size: 51.8 KB
- Stars: 139
- Watchers: 3
- Forks: 12
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Cool.py
Make Python code cooler. 100% coverage. Use and enjoy this code!
## Install
```
pip install cool
```Or fetch from github
```
pip install git+https://github.com/abersheeran/cool@setup.py
```## Usage
### Pipe
*Note: as fast as you didn't use F!*
Use pipeline to pass data as a positional parameter to the next function.
```python
from cool import Fassert range(10) | F(filter, lambda x: x % 2) | F(sum) == 25
```Or you need to pass multiple parameters through the pipeline. Note that `FF` can only accept one parameter, and it must be an iterable object.
```python
from cool import FFassert (1, 2) | FF(lambda x, y: x + y) == 3
```You can use `...` as a placeholder. This is useful when you need to pass non-continuous parameters to create a partial function.
```python
from functools import reduce
from cool import Fassert range(10) | F(reduce, lambda x, y: x + y) == 45
assert range(10) | F(reduce, lambda x, y: x + y, ..., 10) == 55square = F(pow, ..., 2)
assert range(10) | F(map, square) | F(sum) == 285
```The `range(10) | F(reduce, lambda x, y: x + y, ..., 10)` is equivalent to `reduce(lambda x, y: x + y, range(10), 10)`.
### Redirect
Just like the redirection symbol in `Shell`, you can redirect the output to a specified file or `TextIO` object through `>` or `>>`.
```python
from pathlib import PurePath
from cool import R# Redirect output to specified filepath
R(lambda : print("hello")) > PurePath("your-filepath")# Append mode
R(lambda : print("hello")) >> PurePath("your-filepath")
```Redirect to opened file or other streams.
```python
from io import StringIO
from cool import Rwith open("filepath", "a+", encoding="utf8") as file:
R(lambda : print("hello")) >> fileout = StringIO("")
R(lambda : print("hello")) > out
out.seek(0, 0)
assert out.read() == "hello\n"
```Maybe you also want to block the output, just like `> /dev/null`.
```python
from cool import RR(lambda : print("hello")) > None
# Or
R(lambda : print("hello")) >> None
```Note that after the calculation is over, `R` will faithfully return the return value of your function. Try the following example.
```python
from pathlib import PurePath
from cool import F, Rdef func(num):
return range(num) | F(map, lambda x: print(x) or x) | F(sum)result = R(lambda : func(10)) > PurePath("filepath")
assert result == 45
```### Set Global
Maybe you don't want to use `from cool 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 coolcool.set_global(cool.F, cool.FF)
```Maybe you also want to expose `functools.reduce` to the world, just like `map`/`filter`.
```python
import functools
import coolcool.set_global(cool.F, cool.FF, functools.reduce)
```