超快速和干净的数字转换。

fastnumbers的Python项目详细描述


https://img.shields.io/pypi/v/fastnumbers.svghttps://img.shields.io/pypi/pyversions/fastnumbers.svghttps://img.shields.io/pypi/l/fastnumbers.svghttps://img.shields.io/travis/SethMMorton/fastnumbers/master.svg?label=travis-cihttps://ci.appveyor.com/api/projects/status/5ahtcvmt3aoui3mw/branch/master?svg=truehttps://codecov.io/gh/SethMMorton/fastnumbers/branch/master/graph/badge.svghttps://api.codacy.com/project/badge/Grade/7221f3d2be3147e9a975d604f1770cfb

超快速和干净的数字转换。

fastnumbers是具有以下三个目标的模块:

  1. 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.
  2. Provide a set of convenience functions that wrap the above ^{tt2}$ and ^{tt3}$ replacements and provides easy, concise, powerful, fast and flexible error handling.
  3. 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怎么这么快?

cpython竭尽全力确保字符串输入转换为 数字正确(您可以通过检查 integer conversions 为了 float conversions, 但这种额外的努力只需要非常大的 整数或具有多个数字或大指数的浮点数。对于整数,如果 结果可以放入一个clong,然后是一个简单的<;10行c代码的算法 已经足够了。对于浮动,如果数字不需要高精度或不需要 有一个大的指数(如“-123.45e6”),那么一个短的朴素算法也是 可能的。

这些简单的算法速度很快,但是性能的提高来自于 不安全的费用(不防止溢出或舍入错误)。 fastnumbers使用启发式来确定输入是否可以安全地转换 使用更快的朴素算法。这些启发式方法非常保守。- 如果有任何机会,天真的结果不会给完全相同的结果 结果,作为内置函数,它将依赖于cpython的转换 功能。因此,fastnumbers至少和cpython的一样快 内置的floatint函数,通常速度要快得多 因为大多数真实世界的数字都通过了启发式。

安装

使用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

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

推荐PyPI第三方库


热门话题
java Springboot数据JPA findByDate()   java是否可以有多个顶级树节点?   javahibernatehql。子查询中的多个   使用Twilio验证java Keyclope电话号码   java重写对象的toString()表示返回意外的符号   java Android最多每15分钟调用一个方法,否则使用保存的数据   在java swing中突出显示jeditorpane中的一些单词   java将时间戳转换为UTC时区   由于main中存在ArrayIndexOutOfBoundsException,导致java编译错误   java如何通过requestscope获取对象内部对象的值?   java访问安卓代码内的网站并检索生成的图像   java这种日期格式的模式是什么?   java解析包含超链接的xml字符串