Skip to main content

No project description provided

Project description

Cool

Make Python code cooler.

Install

pip install cool

Or fetch from github

pip install git+https://github.com/abersheeran/cool@setup.py

Usage

Pipe

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

from cool import F

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

Or you need to pass multiple parameters through the pipeline:

from cool import FF


def get_data():
    return 1, 2


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

Use alias like follow code, you can use map/filter/reduce more conveniently:

from functools import reduce
from cool import F

Filter = F(F, filter)
Map = F(F, map)
Reduce = F(F, reduce)

range(100) | Filter(lambda x: x % 2) | Map(lambda x: x * x) | Reduce(lambda x, y: x + y)

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 cool import R

# Redirect output to specified filepath
R(print, "hello") > "your-filepath"
# Append mode
R(print, "world") >> "your-filepath"

Maybe you want to redirect to other streams

from io import StringIO
from cool import R


out = StringIO("")
R(print, "hello") > out
out.read() == "hello"

Note that after the calculation is over, R will faithfully return the return value of your function. Try the following example.

from cool import F, R


def func():
    return range(10) | F(map, lambda x: print(x) or x) | F(sum)


print(R(func) > "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

pipe.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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

cool-0.1.0-py2.py3-none-any.whl (7.4 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page