V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
thautwarm
V2EX  ›  Python

解放 Python 的表达力

  •  2
     
  •   thautwarm ·
    thautwarm · 2019-10-14 01:57:46 +08:00 · 5327 次点击
    这是一个创建于 1628 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这个是给 python 用来做语法和语义扩展的, https://github.com/thautwarm/moshmosh

    搞出了目前最快的 python pattern matching: https://github.com/thautwarm/moshmosh/blob/master/tests/match.py

    实现也蛮卫生,不会像 pampy 这种骗人项目一样又慢又脏… 学习成本也小(不需要背 api),然后我也是跟了 python idea 里想换 thread,考虑了前人的 concern 和十几年的 story 做的,基础是 12 年的 extensible pattern matching 和我自己的创新,应该这几年内不会有更好的了(纯 python 实现和设计的话)。

    还有一些其他有用的扩展,比如让你再不用写 lambda 关键字的 quick lambda 扩展和 pipeline 扩展:

    https://github.com/thautwarm/moshmosh/blob/master/tests/quick_lambdas.py

    这个 quick lambda 和 scala 的有区别,能写出功能更强更简炼的表述,不过可能引入一点额外复杂度。。我个人是觉得很好读,如果有想法也可以 issue 或者自己花几十行写一个新的 quick lambda 扩展。

    然后 pipeline 扩展占用了|运算符,如果你想用原来的|,可以用#-pipeline关掉扩展,扩展的范围约束是以行数的区间为粒度的。扩展可以反复开启和关闭。

    然后是一个 quasiquote 的扩展 template-python,其实模式匹配也是基于它实现的:https://github.com/thautwarm/moshmosh/blob/master/tests/template_python.py

    可能不如 macropy 好用,但表达力是完善的。

    然后局部重载二元运算符的扩展:https://github.com/thautwarm/moshmosh/blob/master/tests/scoped_operators.py

    不知道有没有用,主要是简单,我在 ppt 里是拿来讲怎么实现一个扩展的。

    然后如果你想自己写一个扩展,可以看看 pipeline 扩展,只有 30 行而且很多空行和 import,也没有复杂语句。

    https://github.com/thautwarm/moshmosh/blob/master/moshmosh/extensions/pipelines.py

    这个扩展框架只在 main 模块里没法直接用,可以参考下面 benchmark 怎么做的 workaround https://github.com/thautwarm/moshmosh/blob/master/benchmark.py

    最后,欢迎大家来听和这个项目相关的 pyconchina talk, 杭州找李欣宜, 成都找 thautwarm.

    cn.pycon.org/index.html

    24 条回复    2019-10-14 23:58:52 +08:00
    thautwarm
        1
    thautwarm  
    OP
       2019-10-14 02:01:40 +08:00
    自定义 pattern 用的__match__ protocol 和几年前 python idea 某 thread 上提出的原版本不一样,主要是为了优化性能又 keep 表达力。__match__方法第一个参数是解构应该得到的变量数目,第二个是解构对象; 返回 None 表示匹配失败,返回一个 tuple 表示匹配成功(注意这个 tuple 的长度必须是第一个参数值那么多)
    thautwarm
        2
    thautwarm  
    OP
       2019-10-14 02:02:34 +08:00
    多行 lambda 其实完全可以做,但是为了能用现有的 python formatter 我还是不建议做语法扩展。
    语义扩展就 ok 了。。
    thautwarm
        3
    thautwarm  
    OP
       2019-10-14 02:06:21 +08:00
    我那个 benchmark 里是只比 pampy 快 16 倍到 20 倍,主要没兴趣多写了。。。

    https://github.com/thautwarm/moshmosh/blob/master/benchmark.py
    laike9m
        4
    laike9m  
       2019-10-14 05:14:06 +08:00
    红姐 tql
    之前不知道李欣宜,搜了一下才知道是做 PLT 的妹子,感觉好难得

    最近又有人在讨论 lambda: https://discuss.python.org/t/are-better-lambdas-really-impossible/2446
    locoz
        5
    locoz  
       2019-10-14 08:35:10 +08:00 via Android
    红姐🐮🍺
    thautwarm
        6
    thautwarm  
    OP
       2019-10-14 08:39:40 +08:00 via Android
    @laike9m 他们没谈 quick lambda...
    thautwarm
        7
    thautwarm  
    OP
       2019-10-14 08:45:06 +08:00 via Android
    @laike9m 说起来我觉得 quick lambda 真的好,既不需要更新现有语法(可以用之前的格式化工具),然后也比这个 post 里提的都简洁。。
    memedahui
        8
    memedahui  
       2019-10-14 08:47:33 +08:00
    没有爆照,差评
    BacGa
        9
    BacGa  
       2019-10-14 09:22:35 +08:00   ❤️ 1
    我倒是要看看有多少人是因为头像进来的
    deep777blue
        10
    deep777blue  
       2019-10-14 09:26:20 +08:00
    我倒是要看看有多少人是因为头像进来的
    laike9m
        11
    laike9m  
       2019-10-14 09:32:05 +08:00
    @thautwarm 什么是 "quick lambda" 😳
    KDr2
        12
    KDr2  
       2019-10-14 09:38:41 +08:00
    我建议直接上 hylang :D
    thautwarm
        13
    thautwarm  
    OP
       2019-10-14 09:41:43 +08:00 via Android
    @laike9m 我有一个绝妙的图: http://imgur.com/gallery/Axy81c6
    thautwarm
        14
    thautwarm  
    OP
       2019-10-14 09:43:02 +08:00 via Android
    @KDr2 hy 除了太菜还行
    exhades
        15
    exhades  
       2019-10-14 09:45:02 +08:00 via Android
    红姐 tql
    laike9m
        16
    laike9m  
       2019-10-14 09:59:11 +08:00
    靠 _ 来区分语义总觉得可读性不太好啊😹
    thautwarm
        17
    thautwarm  
    OP
       2019-10-14 10:12:06 +08:00 via Android
    @laike9m
    https://stackoverflow.com/questions/7673545/usage-of-in-scala-lambda-functions

    这个是 martin odersky 为了 scala 的可读性和简洁性设计的。。自那以后的 jvm 语言都或多或少有这功能。。
    quick lambda 这个名字是 lihaoyi 为 macropy 里的类似功能取的名字:https://macropy3.readthedocs.io/en/latest/quick_lambda.html#quicklambda
    thautwarm
        18
    thautwarm  
    OP
       2019-10-14 10:19:42 +08:00 via Android
    @laike9m 也可以自定义关键字啊,
    # +quick-lambda(it)
    然后就可以类似 kotlin 了:
    map(it * 2, seq) -> map(lambda it: it*2, seq)
    laike9m
        19
    laike9m  
       2019-10-14 10:48:46 +08:00
    @thautwarm 主要问题是,如果 py 里原来不存在 _ 的使用,那倒问题不大。然而 _ 已经有很多用处了,就会比较 confusing
    janxin
        20
    janxin  
       2019-10-14 11:00:48 +08:00   ❤️ 1
    本来还有犹豫杭州去不去,看来要去了
    thautwarm
        21
    thautwarm  
    OP
       2019-10-14 11:03:09 +08:00 via Android
    @laike9m 所以 moshmosh 是个 pragma oriented 的语法扩展。。。
    thautwarm
        22
    thautwarm  
    OP
       2019-10-14 11:09:02 +08:00 via Android
    @janxin 😝
    pythonee
        23
    pythonee  
       2019-10-14 11:57:00 +08:00
    看了楼主的 V 站和 github,这是我在 v 站捕获的另一位大神呀
    thautwarm
        24
    thautwarm  
    OP
       2019-10-14 23:58:52 +08:00   ❤️ 1
    支持了 IPython console, 我觉得这个项目已经无敌了, 远远超过已有的东西(
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5936 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 02:35 · PVG 10:35 · LAX 19:35 · JFK 22:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.