Skip to main content

Parse serialised data to recover their original underlying types

Project description

parsetypes

This Python package provides tools for parsing serialised data to recover their original underlying types.

Overview

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 inf as either a float or a normal string
  • give exact Decimal values instead of floats
  • detect inline lists

Install

pip install parsetypes

Basic examples

Import parser:

from parsetypes import TypeParser

Parse a single value:

parser = TypeParser()
parser.parse("1.2")   # 1.2
parser.parse("true")  # True
parser.parse("")      # None

Parse 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().

Issues

Found a bug? Please report an issue, or, better yet, contribute a bugfix.

Changelog

This project follows PEP 440 and Semantic Versioning (SemVer). In addition to the guarantees specified by SemVer, for versions before 1.0, this project guarantees backwards compatibility of the API for patch version updates (0.y.z).

The recommended version specifier is parsetypes ~= x.y for version 1.0 and later, and parsetypes ~= 0.y.z for versions prior to 1.0.

0.3.2

  • Improved documentation

0.3.1

  • Added the arguments allow_negative and allow_sign (both True by default) to parser.parse_int(), for parity with parser.is_int() which already had these arguments

0.3

  • Made the previously public but undocumented instance variables of TypeParser that corresponded to the constructor arguments private instead
  • Added public properties to TypeParser for accessing or modifying the same settings in a controlled manner

0.2.6

  • Added Nullable to automatic imports via from parsetypes import * (previously only TypeParser and reduce_types were imported)

0.2.5

  • Fixed documentation

0.2.4

  • Added parser.convert()

0.2.1, 0.2.2, 0.2.3

  • Fixed documentation

0.2

  • Added support for Python version 3.9; previously only 3.10 and 3.11 were supported

0.1.1

  • Updated documentation

0.1

  • Initial version

Project details


Download files

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

Source Distribution

parsetypes-0.3.2.tar.gz (106.3 kB view hashes)

Uploaded Source

Built Distribution

parsetypes-0.3.2-py3-none-any.whl (14.3 kB view hashes)

Uploaded Python 3

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