Skip to main content

Syntax suger for python's functional programming as Unix pipes. - GoodManWEN/pipeit

Project description

pipeit

fury licence pyversions Publish Build

This is a super simple wrapper , let's use python functional programming like Unix pipe!

Inspired by abersheeran/only-pipe , czheo/syntax_sugar_python , pipetools

Install

pip install pipeit

Usage

  • Statements start with PIPE and end with END OR you can even ellipsis them.
  • There're only two objects(PIPE & END) and three types(Filter ,Map & Reduce) in namespace, so feel free to use from pipeit import *.
  • Convert filter into tuple or capital the first letter, e.g. map(lambda x:x + 1) => (map , lambda x:x + 1) or Map(lambda x:x + 1) , however DO NOT MIX USE THEM.
  • It'll be 10% ~ 20% faster using the original python functional way than using these wrappers.

v0.2.0 Update:

  • Simple code timing means

v0.3.0 Update:

  • Easier reading and writing operations

Example

Basic useage:

>>> from pipit import PIPE , END , Map , Filter , Reduce

>>> data = PIPE | range(10) | (map , lambda x:x + 1) | (map , str) | list | END
>>> data
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

# (map , lambda x:x + 1) equals to Map(lambda x:x + 1)
>>> func = lambda x: PIPE | range(x) | Map(lambda x:x + 1) | Map(str) | list | END
>>> func(10)
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

Or you may want a more easy use.

>>> range(10) | Filter(lambda x:x<5) | list
[0, 1, 2, 3, 4]

>>> for _ in range(3) | Map(str):
        print(repr(_))


'0'
'1'
'2'

Code timer updated in version 0.2.0, you can easily detect the execution time of code blocks or statements.

from pipeit import *
from functools import reduce

foo = list(range(100))
for _ in timeit(1e6): # which means loop for 1m times
    bar = foo | Filter(lambda x: x%3) | Map(lambda x: 10*x) | Reduce(lambda x, y: x+y) | int

with timeit(): # a handwritten for loop is required under context manager mode
    for _ in range(int(1e6)):
        bar = reduce(lambda x, y: x+y, map(lambda x: 10*x, filter(lambda x: x%3, foo)))

# output: 
# [line 5][approximate] time cost / loop: 9.8967234μs
# [line 8][exact] time cost: 7.0519098s 

Better IO functions are updated in version 0.3.0. If you hate typing encoding="utf-8" over and over again, believe me, you'll love them.

Use simple Read()/Write()/ReadB()/WriteB() functions instead of the default practice of with open(). You can specify the encoding format, but they are specified as utf-8 by default. Another advantage of doing this is that you no longer need to worry about accidentally emptying the file by not changing 'w' to 'r'.

from pipeit import *
import json

# 
Write("a.txt", "Hello World!")
WriteB("b.json", "[1, 2, 3]".encode("utf-8"))
assert Read("a.txt") == "Hello World!"
assert ReadB("b.txt") == b"[1, 2, 3]"

Similarly, you can use pipes to send data to them, or pipes to receive data from them.

# OR
"[1, 2, 3]".encode("utf-8") | WriteB("b.json")

# another typical scenario is to cache data to hard disk and read it back again
html_a = "abc" # requests.get("https://a.example.com").text
html_b = "123" # ...
htmls = {"html_a": html_a, "html_b": html_b}
json.dumps(htmls) | Write("htmls.json")
# 
htmls = Read("htmls.json") | json.loads
html_a, html_b = htmls.values()

>> and | operands are functionally identical while doing IO.

"Hello World!" >> Write("a.txt")
text = Read("a.txt") >> str
assert text == "Hello World!"

Project details


Download files

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

Source Distribution

pipeit-0.3.4.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

pipeit-0.3.4-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file pipeit-0.3.4.tar.gz.

File metadata

  • Download URL: pipeit-0.3.4.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.18

File hashes

Hashes for pipeit-0.3.4.tar.gz
Algorithm Hash digest
SHA256 28b1008f935788c1ad2e773450b0f3258cc7bac0caeda8926dd7538caba88437
MD5 9990dc42cc285d5a697fe01cf82e192a
BLAKE2b-256 720ecdeec986a4228e3ae27fb42e46b333d98f1d987bf22fdef4d8e623597c38

See more details on using hashes here.

File details

Details for the file pipeit-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: pipeit-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.18

File hashes

Hashes for pipeit-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 06be195c02f9abbe71e08a6ccba013cf275c74e739d09f4e9279257262535f55
MD5 0159eb747a05cc3d123c596e26fc3f66
BLAKE2b-256 dcbe7139cb3f8f17a4587ed8b701c2ab6ebb89d24599aa7aa5f6052012592293

See more details on using hashes here.

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