Skip to main content

Super-fast and clean conversions to numbers.

Project description

https://img.shields.io/travis/SethMMorton/fastnumbers/master.svg?label=travis-ci https://ci.appveyor.com/api/projects/status/5ahtcvmt3aoui3mw/branch/master?svg=true https://codecov.io/gh/SethMMorton/fastnumbers/branch/master/graph/badge.svg https://api.codacy.com/project/badge/Grade/7221f3d2be3147e9a975d604f1770cfb https://img.shields.io/pypi/pyversions/fastnumbers.svg https://img.shields.io/pypi/format/fastnumbers.svg https://img.shields.io/pypi/l/fastnumbers.svg

Super-fast and clean conversions to numbers.

fastnumbers is a module with the following three objectives:

  1. Provide drop-in replacements for the Python built-in int and float that on average are up to 2x faster. These functions should behave identically to the Python built-ins except for a few specific corner-cases as mentioned in the API documentation.

  2. Provide a set of convenience functions that wrap the above int and float replacements and provides easy, concise, powerful, fast and flexible error handling.

  3. 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 function 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).

Author

Seth M. Morton

History

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

04-30-2017 v. 2.0.1

  • Fixed bug in decimal digit limit on GCC.

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastnumbers-2.0.1.tar.gz (299.5 kB view details)

Uploaded Source

Built Distributions

fastnumbers-2.0.1.win-amd64-py3.6.exe (620.5 kB view details)

Uploaded Source

fastnumbers-2.0.1.win-amd64-py3.5.exe (620.3 kB view details)

Uploaded Source

fastnumbers-2.0.1.win-amd64-py3.4.exe (252.0 kB view details)

Uploaded Source

fastnumbers-2.0.1.win-amd64-py3.3.exe (252.0 kB view details)

Uploaded Source

fastnumbers-2.0.1.win-amd64-py2.7.exe (253.9 kB view details)

Uploaded Source

fastnumbers-2.0.1.win32-py3.6.exe (489.7 kB view details)

Uploaded Source

fastnumbers-2.0.1.win32-py3.5.exe (489.6 kB view details)

Uploaded Source

fastnumbers-2.0.1.win32-py3.4.exe (220.6 kB view details)

Uploaded Source

fastnumbers-2.0.1.win32-py3.3.exe (220.6 kB view details)

Uploaded Source

fastnumbers-2.0.1.win32-py2.7.exe (226.2 kB view details)

Uploaded Source

fastnumbers-2.0.1-cp36-cp36m-win_amd64.whl (27.3 kB view details)

Uploaded CPython 3.6m Windows x86-64

fastnumbers-2.0.1-cp36-cp36m-win32.whl (26.1 kB view details)

Uploaded CPython 3.6m Windows x86

fastnumbers-2.0.1-cp36-cp36m-macosx_10_11_x86_64.whl (24.7 kB view details)

Uploaded CPython 3.6m macOS 10.11+ x86-64

fastnumbers-2.0.1-cp35-cp35m-win_amd64.whl (27.2 kB view details)

Uploaded CPython 3.5m Windows x86-64

fastnumbers-2.0.1-cp35-cp35m-win32.whl (25.9 kB view details)

Uploaded CPython 3.5m Windows x86

fastnumbers-2.0.1-cp35-cp35m-macosx_10_11_x86_64.whl (24.4 kB view details)

Uploaded CPython 3.5m macOS 10.11+ x86-64

fastnumbers-2.0.1-cp34-cp34m-win_amd64.whl (24.4 kB view details)

Uploaded CPython 3.4m Windows x86-64

fastnumbers-2.0.1-cp34-cp34m-win32.whl (24.3 kB view details)

Uploaded CPython 3.4m Windows x86

fastnumbers-2.0.1-cp34-cp34m-macosx_10_11_x86_64.whl (24.4 kB view details)

Uploaded CPython 3.4m macOS 10.11+ x86-64

fastnumbers-2.0.1-cp33-cp33m-win_amd64.whl (24.4 kB view details)

Uploaded CPython 3.3m Windows x86-64

fastnumbers-2.0.1-cp33-cp33m-win32.whl (24.3 kB view details)

Uploaded CPython 3.3m Windows x86

fastnumbers-2.0.1-cp33-cp33m-macosx_10_9_x86_64.whl (24.4 kB view details)

Uploaded CPython 3.3m macOS 10.9+ x86-64

fastnumbers-2.0.1-cp27-cp27m-win_amd64.whl (24.8 kB view details)

Uploaded CPython 2.7m Windows x86-64

fastnumbers-2.0.1-cp27-cp27m-win32.whl (24.8 kB view details)

Uploaded CPython 2.7m Windows x86

fastnumbers-2.0.1-cp27-cp27m-macosx_10_11_x86_64.whl (25.0 kB view details)

Uploaded CPython 2.7m macOS 10.11+ x86-64

File details

Details for the file fastnumbers-2.0.1.tar.gz.

File metadata

  • Download URL: fastnumbers-2.0.1.tar.gz
  • Upload date:
  • Size: 299.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for fastnumbers-2.0.1.tar.gz
Algorithm Hash digest
SHA256 ea67263d6360b9294c4bc5924089771d3f003bf6b6343383d700ea9659b506fb
MD5 d2cd43754491641fc35e006567a49f20
BLAKE2b-256 56fa0b903ef58704de00991bb76ce32cb5b74339d8e8828183eab066210154fc

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1.win-amd64-py3.6.exe.

File metadata

File hashes

Hashes for fastnumbers-2.0.1.win-amd64-py3.6.exe
Algorithm Hash digest
SHA256 a61889371c49f1cc8baa956941db22c2b832e06d4210299eb2d4906e656cea9b
MD5 e5b54b8a47bfb3fb81bb3ea6ac1dcf42
BLAKE2b-256 75acb76ae007e4b7953e1d908eb558584494f2271cf94d282b602d47d459461d

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1.win-amd64-py3.5.exe.

File metadata

File hashes

Hashes for fastnumbers-2.0.1.win-amd64-py3.5.exe
Algorithm Hash digest
SHA256 9e35201ba8fe29cba4147ae77ef0e7897df9bea5179f2d58c1b2670a14a093c5
MD5 b30997aaa64c92e1e9d6b5bbd0333433
BLAKE2b-256 902d154422b1877e8d98938c4257d913db14806a7191b3c934e2dcbd9452f4af

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1.win-amd64-py3.4.exe.

File metadata

File hashes

Hashes for fastnumbers-2.0.1.win-amd64-py3.4.exe
Algorithm Hash digest
SHA256 dfaae1f5f7fde472fd0314769d3cce5cfd63b617eff4635de2046f10f3274876
MD5 2e5f99b12ae5f662a8cba07cc0c3fc00
BLAKE2b-256 e2e7263f77d4ba78c106f1ca5ec739bf5349b0d3e8fafa83365a44baf6f033b4

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1.win-amd64-py3.3.exe.

File metadata

File hashes

Hashes for fastnumbers-2.0.1.win-amd64-py3.3.exe
Algorithm Hash digest
SHA256 38d8c148784f0d1d33391587f37075c117c01977cccd88f4e91fdb8424a084e6
MD5 322b2a8364b3f079172d69571a2ad8fc
BLAKE2b-256 94c82bb08bbffa28eb6698ac29ba2f99ce580cf5130dfa980a7c837302c15189

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1.win-amd64-py2.7.exe.

File metadata

File hashes

Hashes for fastnumbers-2.0.1.win-amd64-py2.7.exe
Algorithm Hash digest
SHA256 10d9a49e7ee01690fd57b4b40a18abf1771cf870c3cb081a2a385140502046a7
MD5 f220ac9025e3f7c69c98582d638db46d
BLAKE2b-256 a6383735bd73f720b8bd71a77afd163f934dc9261cdbe8404d44ba13cc33352f

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1.win32-py3.6.exe.

File metadata

File hashes

Hashes for fastnumbers-2.0.1.win32-py3.6.exe
Algorithm Hash digest
SHA256 a0797c0fc3c7e9a6f125f65e67b820a9bf8f6942b7a308602307df18f9599d87
MD5 d49afa3ec3c05cb91f1adc48df7ad246
BLAKE2b-256 1e221602022361f578524c864993b365ab4e56ab3142b47733da5f9bb12723ea

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1.win32-py3.5.exe.

File metadata

File hashes

Hashes for fastnumbers-2.0.1.win32-py3.5.exe
Algorithm Hash digest
SHA256 3f161f8ff96563fa4a88e3a6a5b0ded5d410e526073f7508344c527bd3416cf5
MD5 c105695271adddc48ad021d52ce606f9
BLAKE2b-256 8d04c6c726a92b59c520ee1fab4c74cac1165324a44d43d98df8b5d75e137a6a

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1.win32-py3.4.exe.

File metadata

File hashes

Hashes for fastnumbers-2.0.1.win32-py3.4.exe
Algorithm Hash digest
SHA256 b926dfd202c6e58ecbdcbe0187a3da14a8ff81e7abe1db3a780695af53671998
MD5 75f56c3d2fbeb74734f29781987e2b3f
BLAKE2b-256 ac1cd5dc360b0e96ccff3daf3b20c525c1a830a2150470edccb7a0a0f1947169

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1.win32-py3.3.exe.

File metadata

File hashes

Hashes for fastnumbers-2.0.1.win32-py3.3.exe
Algorithm Hash digest
SHA256 60734c54eb8855f7f0264f97c48f3b94cca5d76067de471676159f7d100584dd
MD5 cfb843b3eff4f32308fdea3186006b49
BLAKE2b-256 7b4e5036ba17e69add505fb5db625b63e101777cd02bf8c244d97c4eeee07663

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1.win32-py2.7.exe.

File metadata

File hashes

Hashes for fastnumbers-2.0.1.win32-py2.7.exe
Algorithm Hash digest
SHA256 4468dd9228b2210a498a9febd1d38ecf0bcb361ebc992a3bd918890a10588161
MD5 93f17eeac684f2c3b7f716f84fd77154
BLAKE2b-256 ba7daa3527a641ba3a2a4688195ee8445bf21f2ae4c78192a7255295075bb292

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 877e0cba1929341ff4d609b98a57a6d6c7b8833b38ab45247d62db0989372506
MD5 52414e747ffd54e346e544813fee3301
BLAKE2b-256 5c47620377c32baf658b10f06360507cf7aac1b06a8d141f841e37a4fa2f711a

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 a9a562c7f5140669459fdb045a7f036b1e123ca3e58feab1a827a3c29c114aa9
MD5 4c265b62432e951f69134cbfaa663c93
BLAKE2b-256 b7c36f3d8248b9063ed03eddd30053a83835b2fe2b07d52369cba1d56dcfe8f7

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp36-cp36m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 e45178aa515c9209d26af3826447b879dd2c1c4b005c0f18addd3fed02363a2e
MD5 24d0d1176dfb97d5ff8d49cf61045669
BLAKE2b-256 abddb1ff919a6b5921815f47d3381bbecbbf13299aeed07c21f5e31df8bc327d

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 781566320b8ce67bf212f745b540ff094ca3957f4daf5af5055105dc6379d352
MD5 1b7d81b036d2a12f7d41ab9d1931854f
BLAKE2b-256 cd1987a0aab91ae383a6deb8d93e83b658b5e37efa7e94518daa4a4514c6b442

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 f973c995cacd6b7d57814d5dd9ad774c1bb9f2bff4a5b03d0c638f3a24611cfa
MD5 6e233ac0e2efe5d7309a7cb3930576e6
BLAKE2b-256 d3a821c6a19337d3c5f2e2b3e10a9cedfe5b2aa405f863015388a73eb7336631

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp35-cp35m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp35-cp35m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 5ad7cd4fcf417d66b992165d675f3abcda69c47606614a0970d684b5852766f8
MD5 101b7d2fbc9e03663e8ec23acfd7a454
BLAKE2b-256 6e8e172889caa9c5e21aea330dbef82cc0912a3212c1bd9004dbd0d57a022a0c

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp34-cp34m-win_amd64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 872d2638fe041397c00f788e38862e0019dfdd6c90c1835c7d246c3edfbeae16
MD5 070152959f1c88da43c2da9202711371
BLAKE2b-256 ad4c2badfe0e19c9f7737a86891351be7b66e874351bbf7cde12bda591daa249

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp34-cp34m-win32.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 c971f3d285f2d33219003fc1ce40a3ceac000b80e604193a106c61b586c5a230
MD5 6330be212afb3303e3c2a034815017d3
BLAKE2b-256 ea0460f9b798549047c48c645a64a7a0e9812471d31ab6726de8b1d62b499baa

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp34-cp34m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp34-cp34m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 6c4c7867d9a5b9e70139afba04e3b02134fd91554579f6792a9388b1d61d2b38
MD5 63675ee0dd40370aadd3af51d91de6ed
BLAKE2b-256 5e69931328a32f4eacc0868ddde5129000ba2387833d8f77a00e8456ef48012a

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp33-cp33m-win_amd64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp33-cp33m-win_amd64.whl
Algorithm Hash digest
SHA256 8e1d232dc8f2bed36104e04d2907280c43416b58dd746de4561e28f48125fea5
MD5 d44bc66a41073fb3ca43d209b590e67d
BLAKE2b-256 939f79087182e662bad14261001580a498ac6459b47a0bdcb5e542f95771c2af

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp33-cp33m-win32.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp33-cp33m-win32.whl
Algorithm Hash digest
SHA256 4435bcf7f6872ff2a39f8a79953aa0ed112958243a30ee53e9fe051729d3de05
MD5 73a1d9f39b3541eaef7dcbaff946035a
BLAKE2b-256 6e1a3eeff063340716c921b042a780c952de2044db49340806461f24fcbe9f57

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp33-cp33m-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp33-cp33m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f46500db2cb82f6fb3d667512ff301ca11a4b4665533f5f035415b1f1ff6f17a
MD5 93cd0c2f9e19df7410775137205dec0b
BLAKE2b-256 c83a3e27c5ae2e4dfbb96aa9e5d160fdd16acd2f2cced0317fc5d6a78aadccd5

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 4d587afc1b43601600e61aa5fa7a743fbb3c6c10412a471360a31ce6cb1f16fc
MD5 9adfb729983e0a49609ecb4f0b2772c3
BLAKE2b-256 1fd039c3ac8f1bddb5086e686f0997f1d951cabef2dacec68a204bb1414ab15d

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 e4da0173790fcc37b70eeb21f0a9ffa6377907a15f1bdfcdeb7eb66cdaa9bbfc
MD5 d03b51fea001a0a682eaeee0cad29b30
BLAKE2b-256 dbb6fb4f63d208913e55bfe45a03ce69a70dc0f165155892222a900530b931b3

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.1-cp27-cp27m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.1-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 36760736e05be8d1224aabd3c1bf7c80bedae9874ed14f69019329d48a6d6f75
MD5 7b9f32180e4e619687a1dfda74780687
BLAKE2b-256 e762b6f2d1404e2359bc5fa8caa273965bad18c4a774965946c670d53128cc41

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page