This Python package provides tools for parsing serialised data to recover their original underlying types.
The TypeParser class provides configurable type inference and parsing. This can be initialised with different settings to, for example:
- allow
None(null values) or not - treat
infas either a float or a normal string - give exact Decimal values instead of floats
- detect inline lists
pip install parsetypes
Import parser:
from parsetypes import TypeParserParse a single value:
parser = TypeParser()
parser.parse("1.2") # 1.2
parser.parse("true") # True
parser.parse("") # NoneParse a series so that it has a consistent type:
parser = TypeParser()
parser.parse_series(["0", "1", "2"]) # [0, 1, 2]
parser.parse_series(["0", "1.2", ""]) # [0.0, 1.2, None]
parser.parse_series(["false", "true", ""]) # [False, True, None]
parser.parse_series(["false", "true", "2"]) # [0, 1, 2]
parser.parse_series(["1", "2.3", "abc"]) # ["1", "2.3", "abc"]Parse a table so that each column is of a consistent type:
parser = TypeParser()
table = parser.parse_table([
["0", "3", "false", "false", "7"],
["1", "4.5", "true", "true", "8.9"],
["2", "", "", "6", "abc"],
]):
assert table == [
[0, 3.0, False, 0, "7"],
[1, 4.5, True, 1, "8.9"],
[2, None, None, 6, "abc"],
]The main contribution of this module lies in the infer_series() and infer_table() functions, which are also called by parse_series() and parse_table().
Found a bug? Please report an issue, or, better yet, contribute a bugfix.