Giter Site home page Giter Site logo

cool's Introduction

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/[email protected]

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

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.

from io import StringIO
from cool import R

with open("filepath", "a+", encoding="utf8") as file:
    R(lambda : print("hello")) >> file


out = 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.

from cool import R

R(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.

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)


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.

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)

cool's People

Contributors

abersheeran avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.