Skip to main content
https://travis-ci.org/SethMMorton/fastnumbers.svg?branch=master

Convert strings to numbers quickly.

This module is a Python C extension that will convert strings to numbers much faster than can be done using pure Python. Additionally, if the string cannot be converted, instead of a ValueError the return value can be either the input as-is or a default value.

To achieve this, the module makes some assumptions about the input type (input is int (or long), float, or str (or unicode)), and otherwise a TypeError is raised.

NOTE: The old safe_real, safe_float, safe_int, and safe_forceint functions are deprecated as of fastnumbers version >= 0.3.0; fast_real, fast_float, fast_int, and fast_forceint have each been reimplemented to fall back on the “safe” algorithm if overflow or loss of precision is detected and so the separate “safe” functions are no longer needed.

Examples

fastnumbers is essentially a fast C implementation of the following Pure Python function:

def fast_float(input, raise_on_invalid=False, default=None):
    try:
        return float(input)
    except ValueError:
        if raise_on_invalid:
            raise
        return default if default is not None else input

Some example usage:

>>> from fastnumbers import fast_float
>>> # 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
>>> # 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
>>> # 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? Below are the timing results for the *_float functions; please see the Timing Documentation for details into all timing results.

from timeit import timeit
float_try = '''\
def float_try(input):
    """Typical approach to this problem."""
    try:
        return float(input)
    except ValueError:
        return input
'''

float_re = '''\
import re
float_match = re.compile(r'[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$').match
def float_re(input):
    """Alternate approach to this problem."""
    try:
        if float_match(input):
            return float(input)
        else:
            return input
    except TypeError:
        return float(input)
'''

print('Invalid input:')
print("Try:", timeit('float_try("invalid")', float_try))
print("re:", timeit('float_re("invalid")', float_re))
print("fast", timeit('fast_float("invalid")', 'from fastnumbers import fast_float'))
print()
print('Valid input:')
print("try:", timeit('float_try("56.07")', float_try))
print("re:", timeit('float_re("56.07")', float_re))
print("fast", timeit('fast_float("56.07")', 'from fastnumbers import fast_float'))

The results will be similar to the below, by vary on the system you are on:

Invalid input:
Try: 2.27156710625
re: 0.570491075516
fast 0.173984050751

Valid input:
try: 0.378665924072
re: 1.08740401268
fast 0.204708099365

As you can see, in all cases fastnumbers beats the pure python implementations.

Full Suite of Functions

In addition to fast_float mentioned above, there are also

  • fast_real

  • fast_int

  • fast_forceint

  • isreal

  • isfloat

  • isint

  • isintlike

Please see the API Documentation for full details.

Author

Seth M. Morton

History

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

05-12-2015 v. 0.5.0

  • Made ‘default’ first optional argument instead of ‘raise_on_invalid’ for conversion functions.

  • Added ‘num_only’ option for checker functions.

05-03-2015 v. 0.4.0

  • Added support for conversion of single Unicode characters that represent numbers and digits.

04-23-2015 v. 0.3.0

  • Updated the fast_* functions to check if an overflow loss of precision has occurred, and if so fall back on the more accurate number conversion method.

  • In response to the above change, the safe_* functions are now deprecated, and internally now use the same code as the fast_* functions.

  • Updated all unit testing to use the hypothesis module, which results in better test coverage.

Release files for fastnumbers 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distributions (sdists)

Source distribution for fastnumbers 0.5.0
File Size Uploaded
fastnumbers-0.5.0.zip 59.7 kB Details
fastnumbers-0.5.0.tar.gz 43.9 kB Details

Release files / fastnumbers-0.5.0.zip

Download URL fastnumbers-0.5.0.zip
Size 59.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ce511b69a7af38b09a80daa87a87e8bd7cdea5ef1d7535a320c06e52ecffcc9e
BLAKE2b-256 checksum
How to use checksums
e11bb2792e0fe9337ae843787f05749b930dd6053e3c3512ff1f1b06bc448c2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / fastnumbers-0.5.0.tar.gz

Download URL fastnumbers-0.5.0.tar.gz
Size 43.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0023c3e885bebb28024d23621e98d26955a5d8b2df9f7408d186e28b5ecccfb3
BLAKE2b-256 checksum
How to use checksums
df560862312d2d4dabbad8b18e94ccf801ef790649fe5a01ce4c85db033603e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

5.2.0

64 release files

5.1.1

67 release files

5.1.0

65 release files

5.0.1

54 release files

5.0.0

54 release files

3.1.0

31 release files

2.2.1

28 release files

2.2.0

28 release files

2.1.1

28 release files

2.0.4

23 release files

2.0.3

23 release files

2.0.2

26 release files

2.0.0

26 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.2

2 release files

0.5.1

2 release files

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page