解放 Python 的表达力

2019-10-14 01:57:46 +08:00
 thautwarm

这个是给 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

5373 次点击
所在节点    Python
24 条回复
thautwarm
2019-10-14 11:03:09 +08:00
@laike9m 所以 moshmosh 是个 pragma oriented 的语法扩展。。。
thautwarm
2019-10-14 11:09:02 +08:00
@janxin 😝
pythonee
2019-10-14 11:57:00 +08:00
看了楼主的 V 站和 github,这是我在 v 站捕获的另一位大神呀
thautwarm
2019-10-14 23:58:52 +08:00
支持了 IPython console, 我觉得这个项目已经无敌了, 远远超过已有的东西(

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/608977

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX