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.

Provide a set of convenience functions that wrap 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 function with error-handling.
Please see the
API Documentation
for other functions that are available from fastnumbers.

>>>fromfastnumbersimportfast_float,floatasfnfloat>>># 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_DETAILTraceback(mostrecentcalllast):...ValueError:invalidliteralforfloat():badinput>>># 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 circle7.0

NOTE: If you need locale-dependent conversions, supply the fastnumbers
function of your choice to locale.atof.

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.