Lai Yonghao (http://laiyonghao.com)
Note: This article is basically a translation of this article.ArticleHttp://dev-tricks.net/pipe-infix-syntax-for-python ).
Through the pipe module, you can use the infix syntax in Python.
First, let's take a look at the traditional prefix syntaxCode:
Sum (select (where (take_while (FIB (), Lambda X: x <1000000) Lambda X: X % 2), Lambda X: x * X ))
Hard to read? Let's take a look at the fix syntax code:
FIB () | take_while (lambda X: x <1000000) \ | where (lambda X: X % 2) \ | select (lambda X: x * X) \ | sum ()
I have read too much, right?
Although the pipe base class has few codes, it is very powerful and can easily write the pipeable function. In addition, this module contains more than 30 functions that have been written, such as 'where', 'Group _ by', 'sort ', 'Take _ while '...
If you want to install pipe first, run the following command on the command line:
PIP install-U pipe
Then wait until the installation is complete. Now you can open an interactive shell and give it a try:
Python 2.6.6 (r266: 84292, Dec 26 2010, 22:31:48) [GCC 4.4.5] On linux2type "help", "Copyright ", "Credits" or "License" for more information. >>> from pipe import * >>> [1, 2, 3, 4, 5] | add15 >>> [5, 4, 3, 2, 1] | sort [1, 2, 3, 4, 5]
Is it easy? If you have any questions, you can help (PIPE) at any time to see the complete help.
Next we will show you how to combine two or more pipeable functions:
>>> [1, 2, 3, 4, 5] | where (lambda X: X % 2) | Concat '1, 3, 5' >>> [1, 2, 3, 4, 5] | where (lambda X: X % 2) | tail (2) | Concat '3, 5' >>> [1, 2, 3, 4, 5] | where (lambda X: X % 2) | tail (2) | select (lambda X: x * x) | Concat '9, 25' >>> [1, 2, 3, 4, 5] | where (lambda X: X % 2) | tail (2) | select (lambda X: x * x) | add34
Because pipe is evaluated by inertia, we can create an infinite generator without worrying about memory usage. For example:
>>> Def fib ():... a, B = 0, 1... while true :... yield... a, B = B, A + B
Now let's use the FIB () function to complete 2nd of A http://projecteuler.net:
Find the sum of all the even-valued terms in maid.
>>> Euler2 = fib () | where (lambda X: X % 2 = 0) | take_while (lambda X: x <4000000) | add >>> assert euler2 = 4613732
How is it? Is it readable? Pretty?
Finally, let's learn how to use @ pipe decorator to create a new pipeable function:
Assume that you want to create a function, yield, which has the first X elements of input.
Assume that you want to create a function that can be used in (1, 2, 3, 4, 5) | take (2) statements to obtain the first two elements.
The initial implementation may be as follows:
Def take (iterable, QTE): For item in iterable: If QTE> 0: QTE-= 1 yield item else: Return
Now, you just need to put @ pipe in the header of this function. This product is the pipeable function!
================================
Thanks:
Thanks to @ yinhm for sharing the article pipe: infix syntax for python on Twitter, so that I can see that there are also such artifacts.
Thanks to @ kyhpudding for sharing his solo module on Twitter, a more fantastic module than pipe. I hope I can understand it clearly and introduce it to you.