Skip to main content

Super-fast and clean conversions to numbers.

Project description

https://img.shields.io/pypi/v/fastnumbers.svg https://img.shields.io/pypi/pyversions/fastnumbers.svg https://img.shields.io/pypi/l/fastnumbers.svg 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

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? Please see the following Jupyter notebooks for timing information on various Python versions.

Installation

Use pip!

$ pip install fastnumbers

How to Run Tests

Please note that fastnumbers is NOT set-up to support python setup.py test.

The recommended way to run tests is with tox. Suppose you want to run tests for Python 3.6 - you can run tests by simply executing the following:

$ tox -e py36

tox will create virtual a virtual environment for your tests and install all the needed testing requirements for you.

If you want to run testing on all of Python 2.7, 3.4, 3.5, 3.6, and 3.7 you can simply execute

$ tox

If you do not wish to use tox, you can install the testing dependencies and run the tests manually using pytest - fastnumbers contains a Pipfile for use with pipenv that makes it easy for you to install the testing dependencies:

$ pipenv install --skip-lock --dev
$ pipenv install --skip-lock -e .
$ pipenv run pytest

Author

Seth M. Morton

History

Please visit the changelog.

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.4.tar.gz (370.9 kB view details)

Uploaded Source

Built Distributions

fastnumbers-2.0.4-cp36-cp36m-win_amd64.whl (20.8 kB view details)

Uploaded CPython 3.6m Windows x86-64

fastnumbers-2.0.4-cp36-cp36m-win32.whl (19.5 kB view details)

Uploaded CPython 3.6m Windows x86

fastnumbers-2.0.4-cp36-cp36m-manylinux1_x86_64.whl (58.6 kB view details)

Uploaded CPython 3.6m

fastnumbers-2.0.4-cp36-cp36m-manylinux1_i686.whl (55.6 kB view details)

Uploaded CPython 3.6m

fastnumbers-2.0.4-cp36-cp36m-macosx_10_6_intel.whl (35.8 kB view details)

Uploaded CPython 3.6m macOS 10.6+ intel

fastnumbers-2.0.4-cp35-cp35m-win_amd64.whl (20.7 kB view details)

Uploaded CPython 3.5m Windows x86-64

fastnumbers-2.0.4-cp35-cp35m-win32.whl (19.4 kB view details)

Uploaded CPython 3.5m Windows x86

fastnumbers-2.0.4-cp35-cp35m-manylinux1_x86_64.whl (57.7 kB view details)

Uploaded CPython 3.5m

fastnumbers-2.0.4-cp35-cp35m-manylinux1_i686.whl (52.5 kB view details)

Uploaded CPython 3.5m

fastnumbers-2.0.4-cp35-cp35m-macosx_10_6_intel.whl (35.0 kB view details)

Uploaded CPython 3.5m macOS 10.6+ intel

fastnumbers-2.0.4-cp34-cp34m-win_amd64.whl (17.9 kB view details)

Uploaded CPython 3.4m Windows x86-64

fastnumbers-2.0.4-cp34-cp34m-win32.whl (17.7 kB view details)

Uploaded CPython 3.4m Windows x86

fastnumbers-2.0.4-cp34-cp34m-manylinux1_x86_64.whl (57.2 kB view details)

Uploaded CPython 3.4m

fastnumbers-2.0.4-cp34-cp34m-manylinux1_i686.whl (52.2 kB view details)

Uploaded CPython 3.4m

fastnumbers-2.0.4-cp34-cp34m-macosx_10_6_intel.whl (35.0 kB view details)

Uploaded CPython 3.4m macOS 10.6+ intel

fastnumbers-2.0.4-cp27-cp27mu-manylinux1_x86_64.whl (56.4 kB view details)

Uploaded CPython 2.7mu

fastnumbers-2.0.4-cp27-cp27mu-manylinux1_i686.whl (53.7 kB view details)

Uploaded CPython 2.7mu

fastnumbers-2.0.4-cp27-cp27m-win_amd64.whl (18.2 kB view details)

Uploaded CPython 2.7m Windows x86-64

fastnumbers-2.0.4-cp27-cp27m-win32.whl (18.2 kB view details)

Uploaded CPython 2.7m Windows x86

fastnumbers-2.0.4-cp27-cp27m-manylinux1_x86_64.whl (56.2 kB view details)

Uploaded CPython 2.7m

fastnumbers-2.0.4-cp27-cp27m-manylinux1_i686.whl (53.6 kB view details)

Uploaded CPython 2.7m

fastnumbers-2.0.4-cp27-cp27m-macosx_10_6_intel.whl (36.2 kB view details)

Uploaded CPython 2.7m macOS 10.6+ intel

File details

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

File metadata

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

File hashes

Hashes for fastnumbers-2.0.4.tar.gz
Algorithm Hash digest
SHA256 53ecfcfd250868b52091f61ffe06962c429bef253e8989674230f1d0e6161470
MD5 9f425c6e420eaa1c3f392a0628bc9f84
BLAKE2b-256 acaf9dcc9aded81249d6d8ccbbfd7c14dd24be6e09d5b6738bebd408610992a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 05c6d9366f4bfe4140ef025d8e12ad02fb5f1ed6d92958bec842e231e76fc401
MD5 49c78e9d662bbe609c0cc2c1cf1220a9
BLAKE2b-256 bddb2779fd78c804b71150e8008d6a4394eaa82cfd7c29434208d6950c45d49e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 171b2f0394411930ea42ecb618ee9974ad84faae599c4a33956180ec5db35267
MD5 68df2333c3d0fee17641f26ef88da30b
BLAKE2b-256 2307b511b2850c9ee2f3accce745cd6c587c9d38d979c1580f2646d1ddad2331

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1eda845d0ec51ded665837299373600153b9bb85e51bd4c1c273b5c02404e6b0
MD5 410088c2a107033c7bedfc42192ffdd8
BLAKE2b-256 5be52868b83cbcf0ccb88f9b6676fc0ee12321e252adf2658e0ab54ef2201312

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 ad08f9f492c3e08202980e40ed06c8cc8d2f585b68f876111488fabaca41b1c4
MD5 85f0d35ba1043121e2977c55de0a4ccd
BLAKE2b-256 a98ab0cf106f834516f6335c7127d1c85577bb6e29b5ee07ec1d7b4fa7d96d17

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp36-cp36m-macosx_10_6_intel.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp36-cp36m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 14d0b863baf96e1253e64e0627e73dc299fbafa9bb0ee27c72d4ba4ea7ff0917
MD5 7b120aa1f4817e5dfa2e226a60ed9547
BLAKE2b-256 10a8eec2c5e05c5e63370275820b42d47f13ff581eed7250a308942f9479def6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 ac8d5b687cabf696213434668345f82978aff579e22d28a043bc3ea976357da6
MD5 de687c94247b13733bfedfb483483217
BLAKE2b-256 83203e3984b235521d91a32bdef0d8d60d1f9820f8de43c2238286e4ed4467e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 be6c1b24dee42ea99ae06269f41ad1b351cb849fc93974363b05c5235b5484a2
MD5 ac64e91d864adaaf669db268d40462f0
BLAKE2b-256 91bacda435336c4ba9dbe198cbd3173ff6b22a54f343fd1841b4c22f0d6953eb

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a87f5a56275ef912e608c7ee6914ed35ce9e97a1d230fa3071208b9332949a70
MD5 08cbdf2947bd16c91c743da0224fd81c
BLAKE2b-256 1ecfcdd6547fff3e96303573ab1fa6c98e5dce2f76c2e389d7855dc4f222a912

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 e0ad90d5465adbbb63ea0607b89fa38e6e75c6e9eeee7e0cf99108746f620433
MD5 b3836f8ffaf6733d7415edd0d9332dfd
BLAKE2b-256 39f6f4a75b861f39eac73e8e9b1dddd4593ec607a53056a95e0c01f7621fb476

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp35-cp35m-macosx_10_6_intel.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp35-cp35m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 32d89097c6d1cfd1aec554fabf5165bf26b51a1df4a1fc5bc6970ce95f1b0db7
MD5 ff0df514bd2e5032a01596fc7dbca81a
BLAKE2b-256 45c2ee589d08497d9246ec76bd67b398138c4c3dcccf090ffb3ec4306f68af80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 c06375a417a2c6bee29f0fb53aff0a59d892b83470952b334d694bc999688f53
MD5 6366cd3742a5fe6b353763be9ab0d5a3
BLAKE2b-256 6f3af8e5d9fb172f7500d0d1548945f6958361c27050b4139bfc62a629bafad5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 c8fdeb168596eeb69251755c54cbf71e2677dac1996936549aac085d44fe6255
MD5 a9389afd5ec4b80e0ebf57e5635ec0d2
BLAKE2b-256 9254c535832d22d6ef6d0fd7dc51cc99dae17bbbc80c279cbbdbb49f979a513b

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 072d152c4579624bafcb38dd370e247d7c15a4fd26f64f0d6161ba09a1d5cd24
MD5 4bfad5e646da7d27c56b87fcf72c0a44
BLAKE2b-256 20fb5101711b8158855078c8759cc0fb1d496a50c2d6bdb11acb84fbe0422fa3

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp34-cp34m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 3f862c466aade24dcca5f035d9204a63049977a7402fb89bbcc6350f9a5a8f16
MD5 1d6e9ae59ec9c2cf600691395a84e7fc
BLAKE2b-256 983d6611b9fa81b76a4e73849a3bf6c52ceb6bb6b629616a601b73b365553b01

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp34-cp34m-macosx_10_6_intel.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp34-cp34m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 8d00855ab41cfbfab1e57bfd35c028b8a450087373461ea3d34d489ede68fbbe
MD5 20a2aacfb457839845e0e6e40d5704f9
BLAKE2b-256 33be65697da3ec591098bc6bd5ff824b63a8e2bbf923aafc48a1016a7775483c

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 977993034a717f8b503ac8610e4516b1ce9db2cbece5d7af3477d4847fbceebc
MD5 497dc3c8db05c0af8a9389d836a214d5
BLAKE2b-256 4e505b2f311b35d4b02e50d5a6a8c78b7d50e992840363bb3177523e828b0639

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp27-cp27mu-manylinux1_i686.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 7f125cf7187e3e3bb9bbe05614bdc0504b1b92bd917cd530224d1a9eea73a60a
MD5 ca2964eda6c7242889e8f52e4100eea9
BLAKE2b-256 d216be09b10daf95fa214eb2256a737ac0920f6c6d83dcb7fc2e5ceab588acf0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 dde74a997ef418d042b0a76e4e73ab252c93135d876a5b1d0e168e45f60066d3
MD5 f418579521a3b41cd0c9df6aa3ca4aa3
BLAKE2b-256 89c5d6a256ae7c878cf51350894efb5f53c106bca1c34b9e569b5425def11ffe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 66cc601aa7b2f41d3d8afc06874bbbd8a6e52b866ece0d410b69846c8cf57794
MD5 9a8eb5ff16b47f4cee736d39089d89d4
BLAKE2b-256 cb62b291ed4841f7bd298a23283a03f48be3ed2f08cebf39738c465cafbb78c0

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 65be8d828712308a4dad0ca3714281c77d6ec9114e6fba12af80ea239fe94694
MD5 eed2840074c8b08070cda3ea82f4b839
BLAKE2b-256 b6e33983b322928a5789a942742517cbd67cc77a641c04825902fe712f7a242a

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp27-cp27m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 e5dd795122d608bf63b4c818e0b910c97029669fe2c9ec9e52d92c7812deca7d
MD5 0e1d6904a3804c7ee3429d53e0b98d48
BLAKE2b-256 e254c371a86c1cdbd30d23adc1500474d850f5ed9a8830ee77f835a6a1c49371

See more details on using hashes here.

File details

Details for the file fastnumbers-2.0.4-cp27-cp27m-macosx_10_6_intel.whl.

File metadata

File hashes

Hashes for fastnumbers-2.0.4-cp27-cp27m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 f9e406ee124222549b9fe046fd548b4d773e8378dd10c2301ffebc76780eb444
MD5 e5e404620d44518b96ed60cc59645af0
BLAKE2b-256 514ad54ba604d3efc7fea408f10d0898b015b3050e7011657aeae8c5aab8cb9a

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