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.
from cool import F
assert 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.
from cool import FF
assert (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.
from functools import reduce
from cool import F
assert range(10) | F(reduce, lambda x, y: x + y) == 45
assert range(10) | F(reduce, lambda x, y: x + y, ..., 10) == 55
square = 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 >>.
Note: R inherits from functools.partial.
from pathlib import PurePath
from cool import R
# Redirect output to specified filepath
R(print, "hello") > PurePath("your-filepath")
# Append mode
R(print, "world") >> PurePath("your-filepath")
Redirect to opened file or other streams.
from io import StringIO
from cool import R
with open("filepath", "a+", encoding="utf8") as file:
R(print, "hello") >> file
out = StringIO("")
R(print, "hello") > out
out.seek(0, 0)
assert out.read() == "hello\n"
Maybe you also want to block the output, just like > /dev/null.
from cool import R
R(print, "hello") > None
# Or
R(print, "hello") >> None
Note that after the calculation is over, R will faithfully return the return value of your function. Try the following example.
from pathlib import PurePath
from cool import F, R
def func(num):
return range(num) | F(map, lambda x: print(x) or x) | F(sum)
print(R(func, 10) > PurePath("filepath"))
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.
import cool
cool.set_global(cool.F, cool.FF)
Maybe you also want to expose functools.reduce to the world, just like map/filter.
import functools
import cool
cool.set_global(cool.F, cool.FF, functools.reduce)
Metadata
Release files for cool 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cool-0.4.0.tar.gz | 7.9 kB | Without provenance | Details for cool-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cool-0.4.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for cool-0.4.0-py2.py3-none-any.whl |
Total release size: 16.0 kB
Release files / cool-0.4.0.tar.gz
| Download URL | cool-0.4.0.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4521ea6acd39dbf6d662d7e60fd22c0f3b969683ffcc9efd25538d57c33d18bc
|
|
BLAKE2b-256 checksum How to use checksums |
4b450c51ac35c4089149f34e44c166a3f1a52a5129402cb3b16b023b61b6b454
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.5 CPython/3.8.5 Linux/4.4.0-19041-Microsoft
|
Release files / cool-0.4.0-py2.py3-none-any.whl
| Download URL | cool-0.4.0-py2.py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
9a2afc89e7c05b27d4f9cba99e9353ef3f274e9afdd2494a3b70c7f7f647596b
|
|
BLAKE2b-256 checksum How to use checksums |
fbf952c018688e713bb7ede96ab46f54cd57169efb47b60bc5ff3086d57d0664
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.5 CPython/3.8.5 Linux/4.4.0-19041-Microsoft
|
Security for cool 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.