超快速和干净的数字转换。
fastnumbers的Python项目详细描述
超快速和干净的数字转换。
- Source Code: https://github.com/SethMMorton/fastnumbers
- Downloads: https://pypi.org/project/fastnumbers/
- Documentation: http://fastnumbers.readthedocs.io/
fastnumbers是具有以下三个目标的模块:
- Provide drop-in replacements for the Python built-in ^{tt2}$ and ^{tt3}$ that on average are up to 2x faster. These functions should behave identically to the Python built-ins except for a few specific corner-cases as mentioned in the API documentation.
- Provide a set of convenience functions that wrap the above ^{tt2}$ and ^{tt3}$ replacements and provides easy, concise, powerful, fast and flexible error handling.
- Provide a set of functions that can be used to rapidly identify if an input could be converted to int or float.
notice:2020年1月1日之后的第一个主要版本将放弃对python 2.7的支持。
示例
下面的示例展示了fast_float函数,它是 具有错误处理功能的快速转换函数。 请看 API Documentation 对于fastnumbers提供的其他函数。
>>>fromfastnumbersimportfast_float,floatasfnfloat>>># Convert string to a float>>>fast_float('56.07')56.07>>># Unconvertable string returned as-is by default>>>fast_float('bad input')'bad input'>>># Unconvertable strings can trigger a default value>>>fast_float('bad input',default=0)0>>># 'default' is also the first optional positional arg>>>fast_float('bad input',0)0>>># Integers are converted to floats>>>fast_float(54)54.0>>># One can ask inf or nan to be substituted with another value>>>fast_float('nan')nan>>>fast_float('nan',nan=0.0)0.0>>>fast_float(float('nan'),nan=0.0)0.0>>>fast_float('56.07',nan=0.0)56.07>>># The default built-in float behavior can be triggered with>>># "raise_on_invalid" set to True.>>>fast_float('bad input',raise_on_invalid=True)#doctest: +IGNORE_EXCEPTION_DETAILTraceback(mostrecentcalllast):...ValueError:invalidliteralforfloat():badinput>>># A key function can be used to return an alternate value for invalid input>>>fast_float('bad input',key=len)9>>>fast_float(54,key=len)54.0>>># Single unicode characters can be converted.>>>fast_float(u'\u2164')# Roman numeral 5 (V)5.0>>>fast_float(u'\u2466')# 7 enclosed in a circle7.0
注意:如果需要依赖于区域设置的转换,请提供fastnumbers 您选择的函数locale.atof。
importlocalelocale.setlocale(locale.LC_ALL,'de_DE.UTF-8')print(atof('468,5',func=fast_float))# Prints 468.5
计时
fastnumbers比纯python实现快多少? 有关各种python版本的计时信息,请参阅以下jupyter笔记本。
fastnumbers怎么这么快?
cpython竭尽全力确保字符串输入转换为 数字正确(您可以通过检查 integer conversions 为了 float conversions, 但这种额外的努力只需要非常大的 整数或具有多个数字或大指数的浮点数。对于整数,如果 结果可以放入一个clong,然后是一个简单的<;10行c代码的算法 已经足够了。对于浮动,如果数字不需要高精度或不需要 有一个大的指数(如“-123.45e6”),那么一个短的朴素算法也是 可能的。
这些简单的算法速度很快,但是性能的提高来自于 不安全的费用(不防止溢出或舍入错误)。 fastnumbers使用启发式来确定输入是否可以安全地转换 使用更快的朴素算法。这些启发式方法非常保守。- 如果有任何机会,天真的结果不会给完全相同的结果 结果,作为内置函数,它将依赖于cpython的转换 功能。因此,fastnumbers至少和cpython的一样快 内置的float和int函数,通常速度要快得多 因为大多数真实世界的数字都通过了启发式。
安装
使用pip!
$ pip install fastnumbers
如何运行测试
请注意,fastnumbers未设置为支持python setup.py test。
建议使用tox运行测试。 假设您想为Python3.6运行测试—只需执行 以下内容:
$ tox -e py36
tox将为您的测试创建虚拟a虚拟环境,并安装所有 你需要的测试要求。
如果您想在所有Python2.7、3.4、3.5、3.6和3.7上运行测试,您可以简单地 执行
$ tox
如果不想使用tox,可以使用 dev-requirements.txt文件,然后使用 pytest。
$ pip install -r dev/requirements.txt $ pytest
历史记录
请访问changelog。