pelper-python助手函数
pelper的Python项目详细描述
pelper–python helper函数可以简化测量、忽略、缓存, python 2.7、3.4和3.5的管道、功能帮助程序等。
pelper包含有用的助手函数、装饰器、上下文管理器 -所有能让你的python生活简单一点的事情。 pelper没有依赖项, 覆盖率100%, 而且有很好的记录。
示例
管道数据通过UNIX类/类药剂管:
>>>frompelperimportpipe>>>pipe("some datat, some data",set,(sorted,{"reverse":True}))['t','s','o','m','e','d','a',',',' ']
pelper提供p-函数,即第一个参数是数据的函数。 有pmap(与map类似,但与pipel一起工作):
>>>frompelperimportpipe,pmap>>>pipe(...range(5),...(pmap,lambdax:x*x),...list)[0,1,4,9,16]
…还有过滤器:
>>>frompelperimportpipe,pfilter>>>pipe(...range(5),...(pfilter,lambdax:x>2),...list)[3,4]
从iterables中获取n元素(如果不能使用方括号,则很有用 符号,例如,如果您使用管道)
>>>frompelperimporttake>>>take("hello world",5)['h','e','l','l','o']
从iterables中获取第n个元素(如果您不能使用正方形 括号符号,例如,如果您使用管道)
>>>frompelperimportnth>>>nth(range(5),2)2
展平任意嵌套的列表:
>>>frompelperimportflatten>>>flatten([1,[2,2,[3,3]]])[1,2,2,3,3]
测量功能的持续时间:
>>>frompelperimportprint_duration>>>@print_duration()...deff(n):pass
测量上下文的持续时间:
>>>frompelperimportprint_duration>>>withprint_duration():...range(4)
忽略例外:
>>>frompelperimportignored>>>withignored(OSError):...raiseOSError()# this is ignored
缓存已计算的函数结果:
>>>frompelperimportcache>>>@cache>>>deffib(n):...return1ifn<2elsefib(n-1)+fib(n-2)>>>f(500)# this would run for quite a wile without the cache decorator
更容易打印和格式化:
>>>frompelperimportprintf>>>printf("Hello {name}, I'm {something}",name="Alan",something="world")HelloAlan,I'm world
安装
pelper只是一个文件,没有依赖项。 您只需将pelper.py放入您的项目并使用它。
或者通过运行:
pip install pelper
或者通过运行以下命令从源代码安装:
pip install .
开发
使用virtualenv处理pelper。 通过:
pip install -e requirements-dev.txt
测试
pelper使用doctest、py.test和tox进行测试。 它还有覆盖范围。
您可以对所有受支持的python版本运行测试,并构建和测试 文档:
tox
仅对指定版本的python运行测试:
tox -e py27,py34,py35
或者只对当前版本的python运行测试:
py.test
文件
通过以下途径生成文档:
cd docs sphinx html