skip to navigation
skip to content

fastnumbers 2.0.2

Super-fast and clean conversions to numbers.

Super-fast and clean conversions to numbers.

fastnumbers is a module with the following three objectives:

  1. Provide drop-in replacements for the Python built-in int and float 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 int and float 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.

Examples

The below examples showcase the fast_float function, which is a fast conversion function with error-handling. Please see the API Documentation for other functions that are available from fastnumbers.

>>> from fastnumbers import fast_float, float as fnfloat
>>> # 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_DETAIL
Traceback (most recent call last):
  ...
ValueError: invalid literal for float(): bad input
>>> # 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 circle
7.0

NOTE: If you need locale-dependent conversions, supply the fastnumbers function of your choice to locale.atof.

import locale
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
print(atof('468,5', func=fast_float))  # Prints 468.5

Timing

Just how much faster is fastnumbers than a pure python implementation? Please see the following Jupyter notebooks for timing information on various Python versions.

Author

Seth M. Morton

History

These are the last three entries of the changelog. See the package documentation for the complete changelog.

11-11-2017 v. 2.0.2

  • Updated docstrings.
  • Improved timing documentation.
  • Added testing for Python 3.7.

04-30-2017 v. 2.0.1

  • Fixed bug in decimal digit limit on GCC.

04-30-2017 v. 2.0.0

  • Dropped support for Python 2.6.
  • Added support for Python 3.6 underscores.
  • Added drop-in replacements for the built-in int() and float() functions.
  • Incorporated unit tests from Python’s testing library to ensure that any input that Python can handle will also be handled the same way by fastnumbers.
  • Added Appveyor testing to ensure no surprises on Windows.
  • Revamped documentation.
  • Refactored internal mechanism for assessing overflow to be faster in the most common cases.
 
File Type Py Version Uploaded on Size
fastnumbers-2.0.2-cp27-cp27m-macosx_10_11_x86_64.whl (md5) Python Wheel cp27 2017-11-12 23KB
fastnumbers-2.0.2-cp27-cp27m-win32.whl (md5) Python Wheel cp27 2017-11-12 22KB
fastnumbers-2.0.2-cp27-cp27m-win_amd64.whl (md5) Python Wheel cp27 2017-11-12 22KB
fastnumbers-2.0.2-cp33-cp33m-macosx_10_9_x86_64.whl (md5) Python Wheel cp33 2017-11-12 22KB
fastnumbers-2.0.2-cp33-cp33m-win32.whl (md5) Python Wheel cp33 2017-11-12 22KB
fastnumbers-2.0.2-cp33-cp33m-win_amd64.whl (md5) Python Wheel cp33 2017-11-12 22KB
fastnumbers-2.0.2-cp34-cp34m-macosx_10_11_x86_64.whl (md5) Python Wheel cp34 2017-11-12 22KB
fastnumbers-2.0.2-cp34-cp34m-win32.whl (md5) Python Wheel cp34 2017-11-12 22KB
fastnumbers-2.0.2-cp34-cp34m-win_amd64.whl (md5) Python Wheel cp34 2017-11-12 22KB
fastnumbers-2.0.2-cp35-cp35m-macosx_10_11_x86_64.whl (md5) Python Wheel cp35 2017-11-12 22KB
fastnumbers-2.0.2-cp35-cp35m-win32.whl (md5) Python Wheel cp35 2017-11-12 24KB
fastnumbers-2.0.2-cp35-cp35m-win_amd64.whl (md5) Python Wheel cp35 2017-11-12 25KB
fastnumbers-2.0.2-cp36-cp36m-macosx_10_11_x86_64.whl (md5) Python Wheel cp36 2017-11-12 22KB
fastnumbers-2.0.2-cp36-cp36m-win32.whl (md5) Python Wheel cp36 2017-11-12 24KB
fastnumbers-2.0.2-cp36-cp36m-win_amd64.whl (md5) Python Wheel cp36 2017-11-12 25KB
fastnumbers-2.0.2.tar.gz (md5) Source 2017-11-12 340KB
fastnumbers-2.0.2.win-amd64-py2.7.exe (md5) MS Windows installer 2.7 2017-11-12 245KB
fastnumbers-2.0.2.win-amd64-py3.3.exe (md5) MS Windows installer 3.3 2017-11-12 243KB
fastnumbers-2.0.2.win-amd64-py3.4.exe (md5) MS Windows installer 3.4 2017-11-12 243KB
fastnumbers-2.0.2.win-amd64-py3.5.exe (md5) MS Windows installer 3.5 2017-11-12 602KB
fastnumbers-2.0.2.win-amd64-py3.6.exe (md5) MS Windows installer 3.6 2017-11-12 603KB
fastnumbers-2.0.2.win32-py2.7.exe (md5) MS Windows installer 2.7 2017-11-12 218KB
fastnumbers-2.0.2.win32-py3.3.exe (md5) MS Windows installer 3.3 2017-11-12 212KB
fastnumbers-2.0.2.win32-py3.4.exe (md5) MS Windows installer 3.4 2017-11-12 212KB
fastnumbers-2.0.2.win32-py3.5.exe (md5) MS Windows installer 3.5 2017-11-12 475KB
fastnumbers-2.0.2.win32-py3.6.exe (md5) MS Windows installer 3.6 2017-11-12 475KB