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

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, the string is returned as-is instead of returning a ValueError.

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.

Examples

It is probably easiest to illustrate fastnumbers in use rather than describe it:

>>> from fastnumbers import safe_float
>>> def float_no_raise(input):
...     try:
...         return float(input)
...     except ValueError:
...         return input
...
>>> safe_float('56.07')
56.07
>>> float_no_raise('56.07') == safe_float('56.07')
True
>>> safe_float('bad input')
'bad input'
>>> float_no_raise('bad input') == safe_float('bad input')
True
>>> safe_float(54)
54.0
>>> float_no_raise(54) == safe_float(54)
True

If you really need speed, there are fast versions of the conversion functions:

>>> from fastnumbers import fast_float
>>> fast_float('56.07')
56.07
>>> safe_float('56.07') == fast_float('56.07')
True

The difference between safe_float and fast_float is that the fast version uses an extremely fast implementation of atof under the hood that does not do overflow or underflow checking, and also can lose precision around the 12th decimal place for extreme exponents; for the majority of cases, the results will be identical.

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.

import re
from timeit import timeit
float_match = re.compile(r'[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$').match
float_try = '''\
def float_try(input):
    """Typical approach to this problem."""
    try:
        return float(input)
    except ValueError:
        return input
'''

float_re = '''\
def float_re(input):
    """Alternate approach to this problem."""
    try:
        if float_match(x):
            return float(x)
        else:
            return x
    except TypeError:
        return float(x)
'''

print('Invalid input:')
print(timeit('float_try("invalid")', float_try))
print(timeit('float_re("invalid")', float_re))
print(timeit('safe_float("invalid"), 'from fastnumbers import safe_float'))
print(timeit('fast_float("invalid"), 'from fastnumbers import fast_float'))
print()
print('Valid input:')
print(timeit('float_try("56.07")', float_try))
print(timeit('float_re("56.07")', float_re))
print(timeit('safe_float("56.07"), 'from fastnumbers import safe_float'))
print(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:
2.28478188515
0.601616001129
0.543533372879
0.185416555405

Valid input:
0.774985694885
1.7571870327
0.584108567238
0.275424480438

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

Full Suite of Functions

In addition to safe_float and fast_float mentioned above, there are also

  • safe_real

  • safe_int

  • safe_forceint

  • fast_real

  • fast_int

  • fast_forceint

  • isreal

  • isfloat

  • isint

  • isintlike

Please see the API Documentation for full details.

Author

Seth M. Morton

History

08-12-2014 v. 0.1.2

  • Fixed bug where ‘+’ and ‘-’ were incorrectly identified as a valid float/int and converted to 0. This bug only applied to the fast_* and is* functions.

  • Fixed bug where ‘safe_forceint’ did not handle ‘nan’ correctly.

08-11-2014 v. 0.1.1

  • ‘fastnumbers’ now understands ‘inf’ and ‘nan’.

08-10-2014 v. 0.1.0

  • Initial release of ‘fastnumbers’.

Release files for fastnumbers 0.1.2

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.1.2
File Size Uploaded
fastnumbers-0.1.2.zip 46.5 kB Details
fastnumbers-0.1.2.tar.gz 32.7 kB Details

Release files / fastnumbers-0.1.2.zip

Download URL fastnumbers-0.1.2.zip
Size 46.5 kB
Tags Source
SHA-256 checksum
How to use checksums
39381b49dc48b6a23d1830c948d37db64b4e0c7b1f6e34fa0ca05e90d35fd951
BLAKE2b-256 checksum
How to use checksums
0961805efe200afd80b7811d710c594fde38a7d315bc67e0a8df7dcfe38b312b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / fastnumbers-0.1.2.tar.gz

Download URL fastnumbers-0.1.2.tar.gz
Size 32.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a72a182e731305ea661459c34532d1ebde8bcfae23ec227aa92a19b0325cd7eb
BLAKE2b-256 checksum
How to use checksums
86265cb79d7f628f2f6793a3a6a6f1fea6e31c05165e6698ff14a106ea2a9de2
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

0.5.0

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

This release

0.1.2 This release

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