Quickly convert strings to number types.
Project description
Convert strings to numbers quickly.
Source Code: https://github.com/SethMMorton/fastnumbers
Downloads: https://pypi.python.org/pypi/fastnumbers
Documentation: http://pythonhosted.org//fastnumbers/
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 (although this behavior is customizable).
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.
If you don’t want to return the input as-is for invalid input, you can either set raise_on_invalid or default to some value:
>>> from fastnumbers import safe_float
>>> safe_float('bad input', raise_on_invalid=True) #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError: invalid literal for float(): bad input
>>> safe_float('bad input', default=0.0)
0.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.
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.
History
These are the last three entries of the changelog. See the package documentation for the complete changelog.
09-03-2014 v. 0.2.0
Added a ‘default’ option to the conversion functions.
08-12-2014 v. 0.1.4
Fixed bug where ‘.’ was incorrectly identified as a valid float/int and converted to 0. This bug only applied to the fast_* and is* functions.
The method to catch corner-cases like ‘.’, ‘+’, ‘e’, etc. has been reworked to be more general… case-by-case patches should no longer be needed.
08-12-2014 v. 0.1.3
Fixed bug where ‘e’ and ‘E’ were incorrectly identified as a valid float/int and converted to 0. This bug only applied to the fast_* and is* functions.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
File details
Details for the file fastnumbers-0.2.0.zip
.
File metadata
- Download URL: fastnumbers-0.2.0.zip
- Upload date:
- Size: 48.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d66657aac65aa196c1a328c4aa938d6c6197fa7eb62d2dee112c91c6b6f8d5b5 |
|
MD5 | e1afdcfba68cf9bab79c8fd4f90a1c88 |
|
BLAKE2b-256 | 8f5ab7255e0e82c56336682a5ac6394703b62b34f3d6c09ba4a49c327476670c |
File details
Details for the file fastnumbers-0.2.0.tar.gz
.
File metadata
- Download URL: fastnumbers-0.2.0.tar.gz
- Upload date:
- Size: 34.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e172754ec4519ec085f7245b5c7807861c3bfc2610caf7945c91d317319e675 |
|
MD5 | 618641b5c3056a12f44747c9babd365a |
|
BLAKE2b-256 | a41da2037bce49f3e36cabe8869ce0a26b81cb29408e097c2790114a1eb2b896 |