zoukankan      html  css  js  c++  java
  • Pipe——Python 的中缀语法库

    赖勇浩(http://laiyonghao.com)

    注:本文基本上是翻译这篇文章(http://dev-tricks.net/pipe-infix-syntax-for-python)。

    通过 Pipe 模块,就能够使用 Python 用上中缀语法。

    首先来看一下传统前缀语法的代码:

    sum(select(where(take_while(fib(), lambda x: x < 1000000) lambda x: x % 2), lambda x: x * x))

    很难读?再来看看中缀语法代码:

    fib() | take_while(lambda x: x < 1000000) \
          | where(lambda x: x % 2) \
          | select(lambda x: x * x) \
          | sum()

    好读多了吧?

    虽然 Pipe 基类的代码很少,但很强大,能够让你很容易写出 pipeable 函数哦。而且这个模块本身就带了超过 30 个已经写好的函数,比如 ‘where’, ‘group_by’, ‘sort’, ‘take_while’ …

    如果想一下 Pipe,需要先安装,在命令行执行:

    pip install -U pipe

    然后等着安装完成就行了。现在可以打开一个交互式 Shell,来试一下:

    Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48) 
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from pipe import *
    >>> [1, 2, 3, 4, 5] | add
    15
    >>> [5, 4, 3, 2, 1] | sort
    [1, 2, 3, 4, 5]

    很简单吧?如果有什么问题,可以随时 help(pipe) 一下,就可以看到完备的帮助了。

    接下来我们展示一下组合两个或更多的 pipeable 函数:

    >>> [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) | add
    34

    因为 Pipe 是惰性求值的,所以我们完成可以弄一个无穷生成器而不用担心内存用完,比如:

    >>> def fib():
    ...    a, b = 0, 1
    ...    while True:
    ...        yield a
    ...        a, b = b, a + b

    现在让我们用 fib() 函数来完成一个 http://projecteuler.net 的第 2 题:

    Find the sum of all the even-valued terms in Fibonacci which do not exceed four million.

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

    怎么样?可读性强吧?漂亮不?

    最后,我们来学习一下如何利用 @Pipe decorator 创建一个新的 pipeable 函数:

    假定要创建一个函数 yield 它的输入的前 x 个元素

    假定要创建一个函数能够用以 (1, 2, 3, 4, 5) | take(2) 语句来获取前两个元素

    那么最初的实现可能是这样的:

    def take(iterable, qte):
        for item in iterable:
            if qte > 0:
                qte -= 1
                yield item
            else:
                return

    现在,你只要把 @Pipe 盖在这个函数上头,这货就是 pipeable 函数了!

    ====================

    鸣谢:

    感谢 @yinhm 在 Twitter 上分享《Pipe: Infix syntax for Python》一文,让我知道还有这等神器。

    感谢 @kyhpudding 在 Twitter 上分享他的 solo 模块,一个比 pipe 更奇幻的模块,希望我能理解清楚,能够跟大家介绍之。

  • 相关阅读:
    Jenkins搭建
    Hexo搭建静态博客站点
    FactoryBean简介以及Mybatis-Spring应用
    ArrayList源码分析
    BCZM : 1.8
    BCZM : 1.7
    BCZM : 1.6
    BCZM : 1.5
    BCZM : 1.4
    BCZM : 1.3
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154352.html
Copyright © 2011-2022 走看看