#!/usr/bin/env python
"""Miscellaneous functions that don't seem to fit elsewhere
:py:func:`guess_formatter`
Convert string into primitive data type it probably encoded before `str`
was called on it
:py:func:`number`
Convert string into numerical data type, trying `bool`, `int`, `float`,
then `str`
"""
import numpy
[docs]def number(inp):
"""Parses numbers from strings, preferring int over float.
Parses `nan`, `Nan`, `None`, `none`, `inf`, and `-inf`
Parameters
----------
inp : str
string input
Returns
-------
float, numpy.nan, numpy.inf, or -numpy.inf, or str if no conversion found
Raises
------
ValueError
if `inp` cannot be converted to a number
"""
if inp in ("nan", "NaN", "na", "None", "none"):
return numpy.nan
elif inp in ("inf", "Inf"):
return numpy.inf
elif inp in ("-inf", "-Inf"):
return -numpy.inf
else:
try:
# note: in python bools are also ints!
# isinstance(True,int) == True
val = int(inp)
except ValueError:
val = float(inp)
return val