Python的中缀运算符
betwixt的Python项目详细描述
在Python中很容易生成命名的中缀二进制运算符。在
演示时间:
# the only useful function in the module >>> from betwixt import infix_operator # any function of 2 arguments would do >>> from fnmatch import fnmatch # make the binary function into an operator, delimited by `*` >>> matches = infix_operator('*', fnmatch) # use it >>> 'foo.txt' *matches* '*.txt' True # other delimiters can be used >>> matches = infix_operator('|', fnmatch) >>> 'foo.txt' |matches| '*.txt' True
也可以使用装饰器窗体:
^{pr2}$最后,betwixt expressive)别名infix_operator:
>>> from betwixt import betwixt >>> @betwixt('*') ... def joining(left, right): ... return left.join(right) >>> '_' *joining* ['a', 'b', 'c'] 'a_b_c' >>> split_at = betwixt('//', lambda lhs, rhs: lhs.split(rhs)) >>> 'a_b_c' //split_at// '_' ['a', 'b', 'c']
模块中提供了所有这些示例运算符和其他一些运算符 betwixt.examples。在
这个想法是从 http://code.activestate.com/recipes/384122-infix-operators/和 类似的C++黑客在Web上的代码我不能再找到了,但是没有 实际密码被盗。在
安装
pip install betwixt
发展
要运行所有测试运行:
tox
请注意,要合并来自所有tox环境的覆盖率数据,请执行以下操作:
Windows | ^{pr 6}$ |
---|---|
Other | ^{pr 7}$ |
版权和许可
版权所有(c)2016-2020 Riccardo Murri<;riccardo.murri@gmail.com>
这是免费软件,根据条款和条件提供 GNU LGPL的一部分—有关详细信息,请参阅文件许可证。在
变更日志
- PyPI的第一个版本。在
- 项目
标签: