Super-fast and clean conversions to numbers.
Project description
Super-fast and clean conversions to numbers.
Source Code: https://github.com/SethMMorton/fastnumbers
Downloads: https://pypi.python.org/pypi/fastnumbers
Documentation: http://fastnumbers.readthedocs.io/
fastnumbers is a module with the following three objectives:
Provide drop-in replacements for the Python built-in int and float that on average is up to 2x faster. These functions should be identically to the Python built-ins except for a few specific corner-cases as mentioned in the API documentation.
Provide a set of convenience functions that wraps the above int and float replacements and provides easy, concise, powerful, fast and flexible error handling.
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 functions 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? Below are the timing results for the fast_float and float function on Python 2.7; 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.07e14")', float_try))
print("re:", timeit('float_re("56.07e14")', float_re))
print("fast", timeit('fast_float("56.07e14")', 'from fastnumbers import fast_float'))
print()
print('Built-in float compared to fastnumbers.float:')
print("Built-in:", timeit('float("56.07e14")'))
print("fastnumbers:", timeit('float("56.07e14")', 'from fastnumbers import float'))
print()
The results will be similar to below, but vary based on your system:
Invalid input: try: 2.09141492844 re: 0.724852085114 fast 0.181249141693 Valid input: try: 0.365114927292 re: 1.42145609856 fast 0.228940963745 Built-in float compared to fastnumbers.float: Built-in: 0.234441041946 fastnumbers: 0.228511810303
As you can see, in all cases fastnumbers beats the pure python implementations (although not always significant).
History
These are the last three entries of the changelog. See the package documentation for the complete changelog.
04-30-2017 v. 2.0.0
Dropped support for Python 2.6.
Added support for Python 3.6 underscores.
Added drop-in replacements for the built-in int() and float() functions.
Incorporated unit tests from Python’s testing library to ensure that any input that Python can handle will also be handled the same way by fastnumbers.
Added Appveyor testing to ensure no surprises on Windows.
Revamped documentation.
Refactored internal mechanism for assessing overflow to be faster in the most common cases.
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.
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 Distribution
Built Distributions
File details
Details for the file fastnumbers-2.0.0.tar.gz
.
File metadata
- Download URL: fastnumbers-2.0.0.tar.gz
- Upload date:
- Size: 299.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96d28e7a693e3b373cddb234e4e157083159666b63eb9208b100cbeaab032553 |
|
MD5 | 563673b563a82cea16e320eeebb8f7a0 |
|
BLAKE2b-256 | cbe7875d2f9108be4d1b6a035d711c41c7aec0b0ca1ef8b6cf04da86f87de551 |
File details
Details for the file fastnumbers-2.0.0.win-amd64-py3.6.exe
.
File metadata
- Download URL: fastnumbers-2.0.0.win-amd64-py3.6.exe
- Upload date:
- Size: 620.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 508706ebad9dea88d12cbe8c4221865e5d4944140abdc521ace52b29e93dd740 |
|
MD5 | af572ed8cc76158a83b2e4655139cd7c |
|
BLAKE2b-256 | f69ff710637a4264304dd26429b994bf5fe6f2d6d686a7b273499ee9ea59c67c |
File details
Details for the file fastnumbers-2.0.0.win-amd64-py3.5.exe
.
File metadata
- Download URL: fastnumbers-2.0.0.win-amd64-py3.5.exe
- Upload date:
- Size: 620.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7299e96db20b4694fa1d5e80c3e2a20f812380cb43d201276fc0aa94f4f85b5e |
|
MD5 | 04a5710e09ed01517656fac6ce1a90e9 |
|
BLAKE2b-256 | 4103ce1448b834938a833ed44b75bc756af845c418e8fe1d304fee69ccfabb45 |
File details
Details for the file fastnumbers-2.0.0.win-amd64-py3.4.exe
.
File metadata
- Download URL: fastnumbers-2.0.0.win-amd64-py3.4.exe
- Upload date:
- Size: 251.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49207297320d71b7a811750748bb14f3c719399d4985adebc23dbff765832059 |
|
MD5 | 0c40b3320cb31d3a2fe17241a4020ee4 |
|
BLAKE2b-256 | 9a6189d84b26297573023dd4de4edc59934bd4ec541da7ead34c2f8dbe1cb616 |
File details
Details for the file fastnumbers-2.0.0.win-amd64-py3.3.exe
.
File metadata
- Download URL: fastnumbers-2.0.0.win-amd64-py3.3.exe
- Upload date:
- Size: 251.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | de77d8f65d96706e5bb4e92334ce7f3e40c23e5d9563427987ee71feb80dfd20 |
|
MD5 | 07409378fa0d1bd6a499b42465ee1e98 |
|
BLAKE2b-256 | 76d7b09c73739c3a2db41d0f18a47f0bdeddc60d6036e8997697cb687d52617d |
File details
Details for the file fastnumbers-2.0.0.win-amd64-py2.7.exe
.
File metadata
- Download URL: fastnumbers-2.0.0.win-amd64-py2.7.exe
- Upload date:
- Size: 253.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7935d6f0945e3e8d1f84f9987a8c380758ee7f9c94d08532c838f313d99e3272 |
|
MD5 | 73e605f843adcf27b84fe4851cf9ffdd |
|
BLAKE2b-256 | 0e8dcac36d7706e49d8869bbf51d5aa75d158911c058df6d29989debea154a2b |
File details
Details for the file fastnumbers-2.0.0.win32-py3.6.exe
.
File metadata
- Download URL: fastnumbers-2.0.0.win32-py3.6.exe
- Upload date:
- Size: 489.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | caacd47693f3af126ff4224be8f59b6f599215997cc661c55e2f2c700c0140f9 |
|
MD5 | 02e6f4dc6548b39bad8536950a2004f2 |
|
BLAKE2b-256 | 802e968191cdda4e9f461a390f89572aff6915a6832f55f15e3c23bd674d41bf |
File details
Details for the file fastnumbers-2.0.0.win32-py3.5.exe
.
File metadata
- Download URL: fastnumbers-2.0.0.win32-py3.5.exe
- Upload date:
- Size: 489.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50cbc1c6bc01ff392cc45a749dc0daee416074bd89b479a18a468a2422313905 |
|
MD5 | 7dbd410a7493ce35804ec962bd780b55 |
|
BLAKE2b-256 | e8cc6b59634ed71e77ba3815d3658f731b89e2e208d1aa79e43352637fdadff4 |
File details
Details for the file fastnumbers-2.0.0.win32-py3.4.exe
.
File metadata
- Download URL: fastnumbers-2.0.0.win32-py3.4.exe
- Upload date:
- Size: 220.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57d78fe087c272ddca7c3a975e751102cafd8421951b54be4f75b30bae7a2c40 |
|
MD5 | 55e44143a83fc6445c699c1a60532622 |
|
BLAKE2b-256 | 5f1de25c8f3b33ac23c1fdbb64a3d47dad6f1ddd1226667becf63f1b924959c5 |
File details
Details for the file fastnumbers-2.0.0.win32-py3.3.exe
.
File metadata
- Download URL: fastnumbers-2.0.0.win32-py3.3.exe
- Upload date:
- Size: 220.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb6b0ea9449c271a3ea68541fa120df305e4fc07f42f3d09c6ef45bcc541b5e4 |
|
MD5 | 3de5d1b3709a9628ab86602c5d87ee4a |
|
BLAKE2b-256 | d2f415e1474dd4d066c48b99bf3aeb9a54fd1d5fabafd623a8c8521e5ee40a5e |
File details
Details for the file fastnumbers-2.0.0.win32-py2.7.exe
.
File metadata
- Download URL: fastnumbers-2.0.0.win32-py2.7.exe
- Upload date:
- Size: 226.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4684c37b4e67ac46f382f43fadd1e370bd4030fdc77b47f39c25b93b9d9bfdff |
|
MD5 | 4292456295248bc7daa4021d003e3842 |
|
BLAKE2b-256 | 900cfecf706955801aa84e2c9d854fbcf0008971f46ef3b3abc18a2feec4f812 |
File details
Details for the file fastnumbers-2.0.0-cp36-cp36m-win_amd64.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp36-cp36m-win_amd64.whl
- Upload date:
- Size: 27.3 kB
- Tags: CPython 3.6m, Windows x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f23aedb12dcd1fdbd4ed3b5ad39a4cc8d98f936002ea45f165703e16132da8cf |
|
MD5 | cafc7f1ec1a71c16246d419d8ad5bfc5 |
|
BLAKE2b-256 | edd3baa622946eb1afd36c5f70f3bfb91bb47749e3bc3067b4ef4045e3a2f9f9 |
File details
Details for the file fastnumbers-2.0.0-cp36-cp36m-win32.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp36-cp36m-win32.whl
- Upload date:
- Size: 26.0 kB
- Tags: CPython 3.6m, Windows x86
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54718862cb11228b0b8769c345e464f2febde553fe291fd5c5dfdf7a6e9ecc9d |
|
MD5 | fae7e8ce8a87fc09db5fe2656c24880e |
|
BLAKE2b-256 | 0c659c8a72d7f60aeec6a11b92486a56bc5f55ad8b7fb323dca5f149ee5b872e |
File details
Details for the file fastnumbers-2.0.0-cp36-cp36m-macosx_10_11_x86_64.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp36-cp36m-macosx_10_11_x86_64.whl
- Upload date:
- Size: 24.7 kB
- Tags: CPython 3.6m, macOS 10.11+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aada48c038a2e71f5b142beebb7544b372359dba76cdccc81f4090eb5d11a214 |
|
MD5 | c1ae9b06ecba0e20d876316454f9dcf1 |
|
BLAKE2b-256 | feb95df281efaea059c4b0c3011a22002a6d1e2c031adb0b1493a169132ace67 |
File details
Details for the file fastnumbers-2.0.0-cp35-cp35m-win_amd64.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp35-cp35m-win_amd64.whl
- Upload date:
- Size: 27.1 kB
- Tags: CPython 3.5m, Windows x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c1a6977dba520463ac55a165da2569c663ca54909a0459f0a2864dabdcb1e4c |
|
MD5 | 476616e34e2869b42c672fd13b6a62b5 |
|
BLAKE2b-256 | f50d0717d663b96fbc50c4e36ef6d4f659e3795be93010cc3738a1685d289469 |
File details
Details for the file fastnumbers-2.0.0-cp35-cp35m-win32.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp35-cp35m-win32.whl
- Upload date:
- Size: 25.9 kB
- Tags: CPython 3.5m, Windows x86
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f7983262add909516ada9b2362b31f13736dc48b6e8ac9a3943f21fd7f4278e |
|
MD5 | cfa4a069624e8e4d31584957c30ff4d3 |
|
BLAKE2b-256 | d02f38d66f518aa46d54f8213afeb8d2bc263a299c4687004063bdc642a7db65 |
File details
Details for the file fastnumbers-2.0.0-cp35-cp35m-macosx_10_11_x86_64.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp35-cp35m-macosx_10_11_x86_64.whl
- Upload date:
- Size: 24.4 kB
- Tags: CPython 3.5m, macOS 10.11+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42a3d82f0d35599cfdf3e01e819fb4304acfd2d71002e49043452a779c7c8548 |
|
MD5 | a740451e28fc2c499f89ad1821c7ab1d |
|
BLAKE2b-256 | a344b35c33b9a2a5b188b8d0774e8ced73fa71884b184306d9f8a8c29fcd3a6e |
File details
Details for the file fastnumbers-2.0.0-cp34-cp34m-win_amd64.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp34-cp34m-win_amd64.whl
- Upload date:
- Size: 24.4 kB
- Tags: CPython 3.4m, Windows x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 714625b13f4c9d82b4df57137e53b2219d3b88845747b9ed6bedc00db394ccdc |
|
MD5 | 1b15c16251dd5ba478b075f9c680b115 |
|
BLAKE2b-256 | d68e8712e5df07b6d4977f684e76c210cd47094c62b9cd4c1ab8d745f58d986d |
File details
Details for the file fastnumbers-2.0.0-cp34-cp34m-win32.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp34-cp34m-win32.whl
- Upload date:
- Size: 24.2 kB
- Tags: CPython 3.4m, Windows x86
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca34a95634e3c9f47e465a8aa911ebb7d0c3c03c6dc4b6b0314cab51b241d10d |
|
MD5 | ce91169fe85bb271a7b3b726728916af |
|
BLAKE2b-256 | 68d4638085b489ddd0e346cb546c68c9e91fa4b4b24105bd7f4215bbecf76de8 |
File details
Details for the file fastnumbers-2.0.0-cp34-cp34m-macosx_10_11_x86_64.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp34-cp34m-macosx_10_11_x86_64.whl
- Upload date:
- Size: 24.3 kB
- Tags: CPython 3.4m, macOS 10.11+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | acb3cd96fbb282bb231dd9006227317a438890d33d1a60d72f1fd61b8df17f00 |
|
MD5 | 304ba0d3ebfbb7357bef2baa2a50f370 |
|
BLAKE2b-256 | 93830b5b0cbdf3e5b24fd93b2edb2c572379c171a407c625e1193734e31cc627 |
File details
Details for the file fastnumbers-2.0.0-cp33-cp33m-win_amd64.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp33-cp33m-win_amd64.whl
- Upload date:
- Size: 24.4 kB
- Tags: CPython 3.3m, Windows x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 935253da881da226fb70d498dad7b666dfc094018bd67fcd10b8a0f80aa17b2b |
|
MD5 | 503dcea8df5a6573497542d0a57b3fdd |
|
BLAKE2b-256 | f98b2a56626435b80311dea263aec5a60f19eb56cf3a897a0810d1ba113899e5 |
File details
Details for the file fastnumbers-2.0.0-cp33-cp33m-win32.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp33-cp33m-win32.whl
- Upload date:
- Size: 24.2 kB
- Tags: CPython 3.3m, Windows x86
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4419320644ca692dd473aa102a47fd83a53ee1b2f831bed1d0896981d2aec59b |
|
MD5 | 2b92f624f237a4aefbf8f4dec5fc8237 |
|
BLAKE2b-256 | bc57f5e5328598f6cddd717be09fc3059f0ef7bb733ac3e2950764c66088a8f0 |
File details
Details for the file fastnumbers-2.0.0-cp33-cp33m-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp33-cp33m-macosx_10_9_x86_64.whl
- Upload date:
- Size: 24.3 kB
- Tags: CPython 3.3m, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05bfe90fa1767b5beb74373008c00dd779a181faff94ca4f6e0c42adacc9ed4c |
|
MD5 | 09b9d4b298d928a2eaf157bbd88b8b12 |
|
BLAKE2b-256 | e4e13e2082a5e12d35d5f25b4dc14bb44e2c81d0a625601166636312f9849dac |
File details
Details for the file fastnumbers-2.0.0-cp27-cp27m-win_amd64.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp27-cp27m-win_amd64.whl
- Upload date:
- Size: 24.7 kB
- Tags: CPython 2.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0bfbe508e2b7e8d60b71207c895d443781ffe3c3a634b4deeb87d652d215c4b |
|
MD5 | bbdcca82cf57993f6da25b00496f3a52 |
|
BLAKE2b-256 | bbcf506b8aa9cc333af1b4902abce0df2aebd89ee62944edce29130a386f8e09 |
File details
Details for the file fastnumbers-2.0.0-cp27-cp27m-win32.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp27-cp27m-win32.whl
- Upload date:
- Size: 24.7 kB
- Tags: CPython 2.7m, Windows x86
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fcc37d253298adc3123c4d687ab78d1f4d95a61eb25e4f35f153b45b8dc3a44 |
|
MD5 | 81b4ea996c9355cc5062614088aff96f |
|
BLAKE2b-256 | db977d287c3e55b62e2c6ad41750cdd12dece002c9c24c6046a657f265a866fc |
File details
Details for the file fastnumbers-2.0.0-cp27-cp27m-macosx_10_11_x86_64.whl
.
File metadata
- Download URL: fastnumbers-2.0.0-cp27-cp27m-macosx_10_11_x86_64.whl
- Upload date:
- Size: 24.9 kB
- Tags: CPython 2.7m, macOS 10.11+ x86-64
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ca48f4a1cff4f4c4209eb18d7c0f51e88776a6011e5af491705542c68cd3c54 |
|
MD5 | 0fccd30a89ff0113bd272f076bc7b055 |
|
BLAKE2b-256 | e83ed03d7fb8bf1ecc34332b802d9eeba2f033af4b063c185a08d7452bbd864a |