Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Super-fast and clean conversions to numbers.

Project Description

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.
Release History

Release History

This version
History Node

2.0.2

History Node

2.0.1

History Node

2.0.0

History Node

1.0.0

History Node

0.7.4

History Node

0.7.3

History Node

0.7.2

History Node

0.7.1

History Node

0.7.0

History Node

0.6.2

History Node

0.6.1

History Node

0.6.0

History Node

0.5.2

History Node

0.5.1

History Node

0.5.0

History Node

0.4.0

History Node

0.3.0

History Node

0.2.0

History Node

0.1.4

History Node

0.1.3

History Node

0.1.2

History Node

0.1.1

History Node

0.1.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
fastnumbers-2.0.2-cp27-cp27m-macosx_10_11_x86_64.whl (23.6 kB) Copy SHA256 Checksum SHA256 cp27 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp27-cp27m-win32.whl (23.5 kB) Copy SHA256 Checksum SHA256 cp27 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp27-cp27m-win_amd64.whl (23.5 kB) Copy SHA256 Checksum SHA256 cp27 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp33-cp33m-macosx_10_9_x86_64.whl (23.1 kB) Copy SHA256 Checksum SHA256 cp33 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp33-cp33m-win32.whl (23.0 kB) Copy SHA256 Checksum SHA256 cp33 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp33-cp33m-win_amd64.whl (23.1 kB) Copy SHA256 Checksum SHA256 cp33 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp34-cp34m-macosx_10_11_x86_64.whl (23.0 kB) Copy SHA256 Checksum SHA256 cp34 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp34-cp34m-win32.whl (23.0 kB) Copy SHA256 Checksum SHA256 cp34 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp34-cp34m-win_amd64.whl (23.1 kB) Copy SHA256 Checksum SHA256 cp34 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp35-cp35m-macosx_10_11_x86_64.whl (23.1 kB) Copy SHA256 Checksum SHA256 cp35 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp35-cp35m-win32.whl (24.6 kB) Copy SHA256 Checksum SHA256 cp35 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp35-cp35m-win_amd64.whl (25.9 kB) Copy SHA256 Checksum SHA256 cp35 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp36-cp36m-macosx_10_11_x86_64.whl (23.4 kB) Copy SHA256 Checksum SHA256 cp36 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp36-cp36m-win32.whl (24.8 kB) Copy SHA256 Checksum SHA256 cp36 Wheel Nov 12, 2017
fastnumbers-2.0.2-cp36-cp36m-win_amd64.whl (26.1 kB) Copy SHA256 Checksum SHA256 cp36 Wheel Nov 12, 2017
fastnumbers-2.0.2.tar.gz (348.3 kB) Copy SHA256 Checksum SHA256 Source Nov 12, 2017
fastnumbers-2.0.2.win32-py2.7.exe (223.3 kB) Copy SHA256 Checksum SHA256 2.7 Windows Installer Nov 12, 2017
fastnumbers-2.0.2.win32-py3.3.exe (217.7 kB) Copy SHA256 Checksum SHA256 3.3 Windows Installer Nov 12, 2017
fastnumbers-2.0.2.win32-py3.4.exe (217.7 kB) Copy SHA256 Checksum SHA256 3.4 Windows Installer Nov 12, 2017
fastnumbers-2.0.2.win32-py3.5.exe (486.6 kB) Copy SHA256 Checksum SHA256 3.5 Windows Installer Nov 12, 2017
fastnumbers-2.0.2.win32-py3.6.exe (486.7 kB) Copy SHA256 Checksum SHA256 3.6 Windows Installer Nov 12, 2017
fastnumbers-2.0.2.win-amd64-py2.7.exe (250.9 kB) Copy SHA256 Checksum SHA256 2.7 Windows Installer Nov 12, 2017
fastnumbers-2.0.2.win-amd64-py3.3.exe (249.0 kB) Copy SHA256 Checksum SHA256 3.3 Windows Installer Nov 12, 2017
fastnumbers-2.0.2.win-amd64-py3.4.exe (249.0 kB) Copy SHA256 Checksum SHA256 3.4 Windows Installer Nov 12, 2017
fastnumbers-2.0.2.win-amd64-py3.5.exe (617.4 kB) Copy SHA256 Checksum SHA256 3.5 Windows Installer Nov 12, 2017
fastnumbers-2.0.2.win-amd64-py3.6.exe (617.5 kB) Copy SHA256 Checksum SHA256 3.6 Windows Installer Nov 12, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting