用于生成器、生成器函数和基于生成器的协程的工具

gentools的Python项目详细描述


https://img.shields.io/pypi/v/gentools.svg?style=flat-squarehttps://img.shields.io/pypi/l/gentools.svg?style=flat-squarehttps://img.shields.io/pypi/pyversions/gentools.svg?style=flat-squarehttps://img.shields.io/travis/ariebovenberg/gentools.svg?style=flat-squarehttps://img.shields.io/codecov/c/github/ariebovenberg/gentools.svg?style=flat-squarehttps://img.shields.io/readthedocs/gentools.svg?style=flat-squarehttps://img.shields.io/codeclimate/maintainability/ariebovenberg/gentools.svg?style=flat-square

用于生成器、生成器函数和基于生成器的协同路由的工具。

主要功能:

  • 创建可重复使用的生成器
  • 合成生成器
  • 使用return构建与python 2/3兼容的生成器。

安装

pip install gentools

示例

  • 使生成器函数可重用:
>>>@reusable...defcountdown(value,step):...whilevalue>0:...yieldvalue...value-=step>>>from_3=countdown(3,step=1)>>>list(from_3)[3,2,1]>>>list(from_3)[3,2,1]>>>isinstance(from_3,countdown)# generator func is wrapped in a classTrue>>>from_3.step# attribute access to arguments1>>>from_3.replace(value=5)# create new instance with replaced fieldscountdown(value=5,step=1)# descriptive repr()
  • 映射生成器的yieldsendreturn值:
>>>@map_return('final value: {}'.format)...@map_send(int)...@map_yield('the current max is: {}'.format)...defmy_max(value):...whilevalue<100:...newvalue=yieldvalue...ifnewvalue>value:...value=newvalue...returnvalue>>>gen=my_max(5)>>>next(gen)'the current max is: 5'>>>gen.send(11.3)'the current max is: 11'>>>gen.send(104)StopIteration('final value: 104')
  • 通过另一个生成器中继生成器的屈服/发送交互:
>>>deftry_until_positive(outvalue):...value=yieldoutvalue...whilevalue<0:...value=yield'not positive, try again'...returnvalue>>>@relay(try_until_positive)...defmy_max(value):...whilevalue<100:...newvalue=yieldvalue...ifnewvalue>value:...value=newvalue...returnvalue>>>gen=my_max(5)>>>next(gen)5>>>gen.send(-4)'not positive, try again'>>>gen.send(-1)'not positive, try again'>>>gen.send(8)8>>>gen.send(104)StopIteration(104)
  • 使用return使python 2/3兼容生成器。
>>>@py2_compatible...defmy_max(value):...whilevalue<100:...newvalue=yieldvalue...ifnewvalue>value:...value=newvalue...return_(value)

发布历史记录

发展

1.1.0(2018-03-02)

  • 增加了对Python2.7、3.3的支持
  • 新的py2_compatibledecorator与py2/3兼容return_
  • 新的stopiter_valuehelper
  • 文档的改进

1.0.2(2018-02-08)

  • 向文档中添加更多示例

1.0.1(2018-01-27)

  • 更新开发人员状态分类器

1.0.0(2018-01-27)

  • 在公共api中包含compose

0.4.0(2018-01-24)

  • pipe重命名为relay

0.3.1(2018-01-23)

  • 修复可重用生成器中的复制问题.replace()

0.3.0(2018-01-22)

  • 使可重用生成器作为方法可调用

0.2.2(2018-01-21)

  • 自述文件的更新

0.2.0(2018-01-21)

  • 重新组织模块,改进文档,重命名函数。

0.1.0(2018-01-17)

  • 初始版本

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Apache Flink外部Jar   创建和强制转换对象数组时发生java错误   Java,添加数组   具有相同包结构和类的java JAR   java Jenkins未能构建Maven项目   java为什么一个forloop比另一个更快,尽管它们做的“一样”?   servlets在将“/”站点迁移到Java EE包时处理contextpath引用   无法解析java MavReplugin:2.21或其某个依赖项   泛型如何编写比较器来泛化Java中的两种类型的对象?   java Android Emulator未在netbeans上加载   多线程Java使用线程对数组中的数字求和:在同步块中使用新变量作为锁:差异   java如何在JSP/servlet中设置<input>标记的值?