Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

Super-fast and clean conversions to numbers.

Project description

https://img.shields.io/travis/SethMMorton/fastnumbers/master.svg?label=travis-ci https://ci.appveyor.com/api/projects/status/5ahtcvmt3aoui3mw/branch/master?svg=true https://api.codacy.com/project/badge/Grade/7221f3d2be3147e9a975d604f1770cfb

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.

Installation

Use pip!

$ pip install fastnumbers

How to Run Tests

Please note that fastnumbers is NOT set-up to support python setup.py test.

The recommended way to run tests is with tox. Suppose you want to run tests for Python 3.6 - you can run tests by simply executing the following:

$ tox -e py36

tox will create virtual a virtual environment for your tests and install all the needed testing requirements for you.

If you want to run testing on all of Python 2.7, 3.4, 3.5, 3.6, and 3.7 you can simply execute

$ tox

If you do not wish to use tox, you can install the testing dependencies and run the tests manually using pytest - fastnumbers contains a Pipfile for use with pipenv that makes it easy for you to install the testing dependencies:

$ pipenv install --skip-lock --dev
$ pipenv install --skip-lock -e .
$ pipenv run pytest

Author

Seth M. Morton

History

Please visit the changelog.

Release history Release notifications

This version
History Node

2.0.4

History Node

2.0.3

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 the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
fastnumbers-2.0.4-cp27-cp27m-macosx_10_6_intel.whl (36.2 kB) Copy SHA256 hash SHA256 Wheel cp27 May 18, 2018
fastnumbers-2.0.4-cp27-cp27m-manylinux1_i686.whl (53.6 kB) Copy SHA256 hash SHA256 Wheel cp27 May 18, 2018
fastnumbers-2.0.4-cp27-cp27m-manylinux1_x86_64.whl (56.2 kB) Copy SHA256 hash SHA256 Wheel cp27 May 18, 2018
fastnumbers-2.0.4-cp27-cp27mu-manylinux1_i686.whl (53.7 kB) Copy SHA256 hash SHA256 Wheel cp27 May 18, 2018
fastnumbers-2.0.4-cp27-cp27mu-manylinux1_x86_64.whl (56.4 kB) Copy SHA256 hash SHA256 Wheel cp27 May 18, 2018
fastnumbers-2.0.4-cp27-cp27m-win32.whl (18.2 kB) Copy SHA256 hash SHA256 Wheel cp27 May 18, 2018
fastnumbers-2.0.4-cp27-cp27m-win_amd64.whl (18.2 kB) Copy SHA256 hash SHA256 Wheel cp27 May 18, 2018
fastnumbers-2.0.4-cp34-cp34m-macosx_10_6_intel.whl (35.0 kB) Copy SHA256 hash SHA256 Wheel cp34 May 18, 2018
fastnumbers-2.0.4-cp34-cp34m-manylinux1_i686.whl (52.2 kB) Copy SHA256 hash SHA256 Wheel cp34 May 18, 2018
fastnumbers-2.0.4-cp34-cp34m-manylinux1_x86_64.whl (57.2 kB) Copy SHA256 hash SHA256 Wheel cp34 May 18, 2018
fastnumbers-2.0.4-cp34-cp34m-win32.whl (17.7 kB) Copy SHA256 hash SHA256 Wheel cp34 May 18, 2018
fastnumbers-2.0.4-cp34-cp34m-win_amd64.whl (17.9 kB) Copy SHA256 hash SHA256 Wheel cp34 May 18, 2018
fastnumbers-2.0.4-cp35-cp35m-macosx_10_6_intel.whl (35.0 kB) Copy SHA256 hash SHA256 Wheel cp35 May 18, 2018
fastnumbers-2.0.4-cp35-cp35m-manylinux1_i686.whl (52.5 kB) Copy SHA256 hash SHA256 Wheel cp35 May 18, 2018
fastnumbers-2.0.4-cp35-cp35m-manylinux1_x86_64.whl (57.7 kB) Copy SHA256 hash SHA256 Wheel cp35 May 18, 2018
fastnumbers-2.0.4-cp35-cp35m-win32.whl (19.4 kB) Copy SHA256 hash SHA256 Wheel cp35 May 18, 2018
fastnumbers-2.0.4-cp35-cp35m-win_amd64.whl (20.7 kB) Copy SHA256 hash SHA256 Wheel cp35 May 18, 2018
fastnumbers-2.0.4-cp36-cp36m-macosx_10_6_intel.whl (35.8 kB) Copy SHA256 hash SHA256 Wheel cp36 May 18, 2018
fastnumbers-2.0.4-cp36-cp36m-manylinux1_i686.whl (55.6 kB) Copy SHA256 hash SHA256 Wheel cp36 May 18, 2018
fastnumbers-2.0.4-cp36-cp36m-manylinux1_x86_64.whl (58.6 kB) Copy SHA256 hash SHA256 Wheel cp36 May 18, 2018
fastnumbers-2.0.4-cp36-cp36m-win32.whl (19.5 kB) Copy SHA256 hash SHA256 Wheel cp36 May 18, 2018
fastnumbers-2.0.4-cp36-cp36m-win_amd64.whl (20.8 kB) Copy SHA256 hash SHA256 Wheel cp36 May 18, 2018
fastnumbers-2.0.4.tar.gz (370.9 kB) Copy SHA256 hash SHA256 Source None May 18, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page