Pipe

A Python library to use infix notation in Python

Download .zip Download .tar.gz View on GitHub

Introduction

Pipe is a Python module enablig a sh like infix syntax (using pipes). As an exemple, here is the solution for the 2nd Euler Project exercise :
"Find the sum of all the even-valued terms in Fibonacci which do not exceed four million." (Given fib a generator of fibonacci numbers)

euler2 = fib() | where(lambda x: x % 2 == 0)
               | take_while(lambda x: x < 4000000)
               | add

Installation

$ easy_install pipe

or

$ git clone https://github.com/JulienPalard/Pipe.git
$ cd Pipe
$ python setup.py install

Vocabulary

  • a Pipe is a 'pipeable' function, somthing that you can pipe to. In the code '[1, 2, 3] | add' add is a Pipe
  • a Pipe function: A standard function returning a Pipe so it can be used like a normal Pipe but called like in : [1, 2, 3] | concat("#")

Syntax

The basic syntax is to use a Pipe like in a shell :

>>> [1, 2, 3] | add
6

A Pipe can be a function call, for exemple the Pipe function 'where' :

>>> [1, 2, 3] | where(lambda x: x % 2 == 0) #doctest: +ELLIPSIS
<generator object <genexpr> at ...>

A Pipe as a function is nothing more than a function returning a specialized Pipe.

Constructing your own

You can construct your pipes using Pipe classe initialized with lambdas like :

stdout = Pipe(lambda x: sys.stdout.write(str(x)))
select = Pipe(lambda iterable, pred: (pred(x) for x in iterable))

Or using decorators :

@Pipe
def stdout(x):
    sys.stdout.write(str(x))

Existing Pipes

You should read the README file or the python documentation for the module.