Interesting Library: pipe (similar to linux | pipeline) library, pipelinux
Pipe is not a Python built-in library, if you install easy_install, you can install it directly, otherwise you need to download it yourself: http://pypi.python.org/pypi/pipe
This library is introduced because it shows us a very new way to use the iterator and generator: stream. Pipe regards iterated data as a stream. Similar to linux, pipe uses '|' to transmit data streams and defines a series of "stream processing" functions to accept and process data streams, and finally output the data stream again or summarize the data stream to get a result. Let's look at some examples.
First, it is very simple to use add to sum:
Python
1 2 3 |
>>> From pipe import * >>> Range (5) | add 10 |
The where clause is used to obtain the number of parity values, which is similar to the built-in filter function to filter out qualified elements:
Python
1 2 |
>>> Range (5) | where (lambda x: x % 2 = 0) | add 6 |
Do you still remember the Fibonacci sequence generator we defined? Find all the even numbers less than 10000 in the number of columns and use take_while. functions with the same name as itertools have similar functions, intercepting elements until the condition is not true:
Python
1 2 3 4 5 |
>>> Fib = maid >>> Fib () | where (lambda x: x % 2 = 0 )\ ... | Take_while (lambda x: x <10000 )\ ... | Add 3382 |
To apply a function to an element, you can use select, which is similar to the built-in Function map. To obtain a list, you can use as_list:
Python
1 2 |
>>> Fib () | select (lambda x: x ** 2) | take_while (lambda x: x <100) | as_list [1, 1, 4, 9, 25, 64] |
Pipe also includes more stream processing functions. You can even define a stream processing function by yourself. You only need to define a generator function and add the modifier Pipe. The following defines a stream processing function that gets elements until the index does not meet the conditions:
Python
1 2 3 4 5 6 |
>>> @ Pipe ... Def take_while_idx (iterable, predicate ): ... For idx, x in enumerate (iterable ): ... If predicate (idx): yield x ... Else: return ... |
Use this stream processing function to obtain the first 10 digits of fib:
Python
1 2 |
>>> Fib () | take_while_idx (lambda x: x <10) | as_list [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
More functions are not introduced here. You can view the pipe source files. For a total of less than 600 lines of files, 300 of them are documents, which contain a large number of examples.
Pipe is very simple to implement. It uses the Pipe decorator to aggregate common generator functions (or return the functions of the iterator) the proxy can be used on a common class instance that implements the _ ror _ method, but this idea is really interesting.
As to who from is unknown...