Efficiently perform string to number type conversion with error handling.
Project description
Efficiently perform string to number type conversion with error handling.
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; numeric types can also be converted to other numeric types.
Additionally, the user has control over what happens in the event that the input string cannot be converted to a number:
the input can be returned as-is (this is the default behavior)
the input can be passed to a user-given key function then returned
a ValueError can be raised (like the built-in float or int)
a default value can be returned
Examples
fastnumbers contains functions that are fast C implementations similar to the following Pure Python function:
def fast_float(input, default=None, raise_on_invalid=False, key=None, inf=None, nan=None):
import math
try:
x = float(input)
except ValueError:
if raise_on_invalid:
raise
elif key is not None:
return key(input)
return default if default is not None else input
else:
if inf is not None and math.isinf(x):
return inf
elif nan is not None and math.isnan(x):
return nan
else:
return x
In addition to fast_float, there are also fast_real, fast_int, fast_forceint, isreal, isfloat, isint, and isintlike - please see the API Documentation for full details.
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
>>> # 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? 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 below, but vary based on your system:
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.
History
These are the last three entries of the changelog. See the package documentation for the complete changelog.
04-23-2016 v. 1.0.0
“coerce” in fast_real now applies to any input, not just numeric; the default is now True instead of False.
Now all ASCII whitespace characters are stripped by fastnumbers
Typechecking is now more forgiving
fastnumbers now checks for errors when converting between numeric types
Fixed bug where very small numbers are not converted properly
Testing now includes Python 2.6.
Removed safe_* functions (which were deprecated since version 0.3.0)
Fixed unicode handling on Windows.
Fixed Python2.6 on Windows.
03-19-2016 v. 0.7.4
Added the “coerce” option to fast_real.
03-08-2016 v. 0.7.3
Newline is now considered to be whitespace (for consistency with the builtin float and int).
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
Built Distributions
File details
Details for the file fastnumbers-1.0.0.zip
.
File metadata
- Download URL: fastnumbers-1.0.0.zip
- Upload date:
- Size: 64.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72206a51f9d021d92c9ded6297943454ea408149d1a4f5c1e731bb62fe86849e |
|
MD5 | d285c4b24a85b56b73bb47177319f705 |
|
BLAKE2b-256 | 670c4ae62574d12260c28942211c0db27dfcb87557161dab2d662c33c43d4b31 |
File details
Details for the file fastnumbers-1.0.0.tar.gz
.
File metadata
- Download URL: fastnumbers-1.0.0.tar.gz
- Upload date:
- Size: 48.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec0759a41e33a7bb23acaebbe86babce819cb130d550bdbd713ef7d918a07173 |
|
MD5 | 9657192bc85447178acc29efd3efeafb |
|
BLAKE2b-256 | ee5f3dd68114a4316799eab6af8175a6f0c4e8405f384fe62c66800ef2c782b5 |
File details
Details for the file fastnumbers-1.0.0.win-amd64-py3.5.exe
.
File metadata
- Download URL: fastnumbers-1.0.0.win-amd64-py3.5.exe
- Upload date:
- Size: 171.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae36253ecf7c4e05dc22c837898fa667405442d341f43bf365bd954e501d3cad |
|
MD5 | 9a620f231d4e6e4e999fe1d129e8720b |
|
BLAKE2b-256 | 2e1dbe559db23a3e1f57afbab406c1ea5b2da3f41a974298e6d6118bc44cb7da |
File details
Details for the file fastnumbers-1.0.0.win-amd64-py3.4.exe
.
File metadata
- Download URL: fastnumbers-1.0.0.win-amd64-py3.4.exe
- Upload date:
- Size: 254.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 559f1195e5cc89d74b0567f58ce6e3ebad64a5ef1eff8fd1dd47b940a12ee600 |
|
MD5 | 074debf018876ecc3eae9d1524099018 |
|
BLAKE2b-256 | 76ea5eb863e21cc16da4493c4a6bcef433124172731f3430b7ec79496f45e62b |
File details
Details for the file fastnumbers-1.0.0.win-amd64-py3.3.exe
.
File metadata
- Download URL: fastnumbers-1.0.0.win-amd64-py3.3.exe
- Upload date:
- Size: 254.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7918e11f7663a707a734a879805197bae954163ebfb4f34767ff1f135d41a2c |
|
MD5 | 17d7e2355da5852005b67a28538527fb |
|
BLAKE2b-256 | b0d5036729e4b9c34ac30ea8b095049c490bfea53358c9933c16e1d56145db4e |
File details
Details for the file fastnumbers-1.0.0.win-amd64-py2.7.exe
.
File metadata
- Download URL: fastnumbers-1.0.0.win-amd64-py2.7.exe
- Upload date:
- Size: 255.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01b928da6b18d3f96dcd890d5fb4ea35f36c05ccd952cd808618a5a373483867 |
|
MD5 | e05627f3debebd820859691c8d6f4e6e |
|
BLAKE2b-256 | bb6de9c812cbbc857f394415544fc31ce2c6bd2a68f9e10c602b8c61a22ce8c2 |
File details
Details for the file fastnumbers-1.0.0.win-amd64-py2.6.exe
.
File metadata
- Download URL: fastnumbers-1.0.0.win-amd64-py2.6.exe
- Upload date:
- Size: 256.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3da718dc544b93da5aa84b23e089fce5135a6757d72a9bbde137070f3c5ca13f |
|
MD5 | 997687aa8a1b81a0fd7be5e0209b10cf |
|
BLAKE2b-256 | f1d84a1f82ba85d88e56c2c1915dea2b24a59ca3fa6da214b82a8add943a82f8 |
File details
Details for the file fastnumbers-1.0.0.win32-py3.5.exe
.
File metadata
- Download URL: fastnumbers-1.0.0.win32-py3.5.exe
- Upload date:
- Size: 164.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c187855112c5e6448a520331355b23f650feed9795aa31c28a7d78d1b45786d3 |
|
MD5 | ff1268757b355b95178839115924ce45 |
|
BLAKE2b-256 | c72f894ff76406f11adcc862cde1c64019869d739222715a5c0d41bc98294b2f |
File details
Details for the file fastnumbers-1.0.0.win32-py3.4.exe
.
File metadata
- Download URL: fastnumbers-1.0.0.win32-py3.4.exe
- Upload date:
- Size: 224.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06ec90375ab4c8f3a1af7b4c0b0cdfbea5870fb8c8c58270fab2ecfad9323b2f |
|
MD5 | 3305380a279bb4296a12dcf9e15812d3 |
|
BLAKE2b-256 | 823eca5332c88be7d615488861ac5dfc6d284ed6ed794058ef2223a2f01a58c0 |
File details
Details for the file fastnumbers-1.0.0.win32-py3.3.exe
.
File metadata
- Download URL: fastnumbers-1.0.0.win32-py3.3.exe
- Upload date:
- Size: 224.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed86b5c1c6cde5b223e488d256f786fe1c0f869f61c3e1584582292a95eb48ce |
|
MD5 | 7cfd2f0e60a548c0ad0d37709302244d |
|
BLAKE2b-256 | 42b338693681be3607042f2b9d9d3f25c1a2461ab66e92a864bf3d402872fd4f |
File details
Details for the file fastnumbers-1.0.0.win32-py2.7.exe
.
File metadata
- Download URL: fastnumbers-1.0.0.win32-py2.7.exe
- Upload date:
- Size: 229.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25e828e49413926f82c05c3731c7a898f02cc4e3cd5dbb7cc3d2b5c1a24ab93d |
|
MD5 | c8f5422900486a108105ad12e137c875 |
|
BLAKE2b-256 | f45bbb76832671cb3f7bf4d86f19f702da2cd845035d9908b3e51211336e697f |
File details
Details for the file fastnumbers-1.0.0.win32-py2.6.exe
.
File metadata
- Download URL: fastnumbers-1.0.0.win32-py2.6.exe
- Upload date:
- Size: 229.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f0fd89e3e801b550625f54537ff3c8718640824848dff4dbba281ea61a23e75 |
|
MD5 | 875c81eaaec0b79742cb3a893ff73adb |
|
BLAKE2b-256 | 06245e606818c0741a572ab8cf8eef8f0ebe9053c89a5e67adefe5887067a464 |
File details
Details for the file fastnumbers-1.0.0-cp35-cp35m-win_amd64.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp35-cp35m-win_amd64.whl
- Upload date:
- Size: 30.5 kB
- Tags: CPython 3.5m, Windows x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 709855029e26700b67b71102b3fde3a385943c03d7435c677d44ed741ac130d1 |
|
MD5 | eb526f094c079fec9a06bd8f00b09eef |
|
BLAKE2b-256 | 5c5830acd378fb2eb6294ae6ccf6a5fb4cdf58b249df145f1b022c6003477744 |
File details
Details for the file fastnumbers-1.0.0-cp35-cp35m-win32.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp35-cp35m-win32.whl
- Upload date:
- Size: 30.9 kB
- Tags: CPython 3.5m, Windows x86
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d655195bafdc69d5bd502eee21b5c29027e986a99a1673ef5dc9dc780eea14d |
|
MD5 | 5d62d6ed25acecf2909eacffd368833e |
|
BLAKE2b-256 | 494e3243a1b277c3a87e3758c1d953606c6f0cbd13a72813ade8eb249f984094 |
File details
Details for the file fastnumbers-1.0.0-cp35-cp35m-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp35-cp35m-macosx_10_9_x86_64.whl
- Upload date:
- Size: 26.0 kB
- Tags: CPython 3.5m, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f39e4c071b54ca9ed95b3b4f9716a05f8b6c529f89b00b10bc0ea9721dcbad9 |
|
MD5 | 0487ce9ca6efd1c4c5b8875f0837005e |
|
BLAKE2b-256 | 73a85c779c81ff170ceeca1f6ec4c2fe2ab3a1eb4192f220951c7d5d14d2193f |
File details
Details for the file fastnumbers-1.0.0-cp34-cp34m-win_amd64.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp34-cp34m-win_amd64.whl
- Upload date:
- Size: 27.8 kB
- Tags: CPython 3.4m, Windows x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f4e92670b7396ac2f27954e7645d0c3e4edca477368fb3a115c56a20aacc658 |
|
MD5 | dab7507eb5e8ffa49025cfab11c5a789 |
|
BLAKE2b-256 | 3e52a42c177e627b2d2bee349b05533f41ede853225a643772e8203b3ed5591c |
File details
Details for the file fastnumbers-1.0.0-cp34-cp34m-win32.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp34-cp34m-win32.whl
- Upload date:
- Size: 29.2 kB
- Tags: CPython 3.4m, Windows x86
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d1bb0da929b24ab4fbb7548884f18ea27b2dc291e1470e17affba091061318d |
|
MD5 | 30a1b2c13324adf925d1e02e950dead0 |
|
BLAKE2b-256 | 87317ef7a5cc8b2d6a96f1f001ad0e17a7ea55e15f8cc5ff34ddea1908ef8276 |
File details
Details for the file fastnumbers-1.0.0-cp34-cp34m-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp34-cp34m-macosx_10_9_x86_64.whl
- Upload date:
- Size: 25.9 kB
- Tags: CPython 3.4m, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 782b8b4b71d595a4ce3c2cafcf2fda429ab129c6cc6865d3f8d05be3a8df9714 |
|
MD5 | 8181466653756166740d8bf171a31743 |
|
BLAKE2b-256 | a48366b79fd73410c6985c2189968080b3c6cd21889be9c84c3fce364260ccd8 |
File details
Details for the file fastnumbers-1.0.0-cp33-cp33m-win_amd64.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp33-cp33m-win_amd64.whl
- Upload date:
- Size: 27.8 kB
- Tags: CPython 3.3m, Windows x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8503adc8553abb0323d531182f582b3cfa43e1068527521b498fd57cae614384 |
|
MD5 | 80ab4b12c660125d392e06bd1c1d84b2 |
|
BLAKE2b-256 | 80c2ebdbb8bbedd997ff1aa9741da76c93b7ca355780876fde24ff0ef8719111 |
File details
Details for the file fastnumbers-1.0.0-cp33-cp33m-win32.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp33-cp33m-win32.whl
- Upload date:
- Size: 29.2 kB
- Tags: CPython 3.3m, Windows x86
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac510153329a0cd978a8a605ce2d656016fc33cc284427f3bb95584dd1517ebe |
|
MD5 | cec660e4a6e302560180fc066fe96fd0 |
|
BLAKE2b-256 | d5bacb8d5da0d214324740edad6b2e1b59d4eaea8d049dd904fb4beaa1fede6c |
File details
Details for the file fastnumbers-1.0.0-cp33-cp33m-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp33-cp33m-macosx_10_9_x86_64.whl
- Upload date:
- Size: 25.9 kB
- Tags: CPython 3.3m, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8378fab69373ed04140fce8b5025c12d90e16a9d132a0a6db635f4d3d0ebdd42 |
|
MD5 | 83ba306d7cf48d71b7f06eee16b3cb46 |
|
BLAKE2b-256 | bdefa67dae6cbd793884d3f8a24478a477b6460df32da6c5c5d8a724ce0d6ff6 |
File details
Details for the file fastnumbers-1.0.0-cp27-cp27mu-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp27-cp27mu-macosx_10_9_x86_64.whl
- Upload date:
- Size: 25.9 kB
- Tags: CPython 2.7mu, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bb286d9e9fc576ed8072e9b8e7a453f389baa9fca7fbc2a41fd19191ac99e30 |
|
MD5 | f3c0187c2c98f1713131bd482491acc5 |
|
BLAKE2b-256 | a9f8426b05bb003a4983c3fb6f0cbcfb3c59b7a370f57decce3b35f9990fdfdb |
File details
Details for the file fastnumbers-1.0.0-cp27-cp27m-win_amd64.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp27-cp27m-win_amd64.whl
- Upload date:
- Size: 27.8 kB
- Tags: CPython 2.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78f90ab38c2353ead5e20c93f7e6e4aa04175e4a06c20f9a142491f48969fb11 |
|
MD5 | 04be0252d426b4930d7ac44123d202b5 |
|
BLAKE2b-256 | cdbd969bdd50d33697ef53d50590e83b4f4e5a9f0f04f8018677950e3abbe57a |
File details
Details for the file fastnumbers-1.0.0-cp27-cp27m-win32.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp27-cp27m-win32.whl
- Upload date:
- Size: 29.3 kB
- Tags: CPython 2.7m, Windows x86
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ced789f3aba94c2e51376af665daaf747b8e0f5bc74b79b7a4e8f45fb5446de |
|
MD5 | 570e0d585f3bbaf5c963ec399c450878 |
|
BLAKE2b-256 | 843f0eea47e759d26783851f7e88568a385f92f461a2634c897e1a3d99355fa9 |
File details
Details for the file fastnumbers-1.0.0-cp26-cp26mu-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp26-cp26mu-macosx_10_9_x86_64.whl
- Upload date:
- Size: 25.9 kB
- Tags: CPython 2.6mu, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c0105a6c99dc082363b63f798cdcd1aad66a2b2df0b24f1932bf0a53e823f68 |
|
MD5 | d8b9684904994546d8b91b0016bbf83c |
|
BLAKE2b-256 | eb5a80155cd66f9e17d5df6bfb5e8fe7eb95ad28ad715e5115b72939c2704822 |
File details
Details for the file fastnumbers-1.0.0-cp26-cp26m-win_amd64.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp26-cp26m-win_amd64.whl
- Upload date:
- Size: 28.1 kB
- Tags: CPython 2.6m, Windows x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8472796ee96e5e7b740a2b943e002825d1a4ebb7c71008e0442d2c31fda7820c |
|
MD5 | 36d026a485119d937a002c0d26b63081 |
|
BLAKE2b-256 | 1bd7572e9bbf9e3984ad931cea05962dfd6d12f80f0501e83ec1a144f30316d7 |
File details
Details for the file fastnumbers-1.0.0-cp26-cp26m-win32.whl
.
File metadata
- Download URL: fastnumbers-1.0.0-cp26-cp26m-win32.whl
- Upload date:
- Size: 29.6 kB
- Tags: CPython 2.6m, Windows x86
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fbb70e040e4ff133e239dba208e9f7bd7c1cc7ba626396afe3da1b5fa9fdc11 |
|
MD5 | 88227d801ba5bef98cb670508d90e3d7 |
|
BLAKE2b-256 | de413ffe91cad2ad11a4a7cd2e8582c7e74790df671b10d73f4c0a90c94601fb |