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.3

  • Added support for Python 3.13

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

parsetypes-0.3.3-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file parsetypes-0.3.3.tar.gz.

File metadata

  • Download URL: parsetypes-0.3.3.tar.gz
  • Upload date:
  • Size: 106.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for parsetypes-0.3.3.tar.gz
Algorithm Hash digest
SHA256 7b58b11c97831bb7fce37ae3edd72e0c970575dea452ec6199b548aa33d24770
MD5 c0f8126ee4b5fa926e6b9502e62ee9d5
BLAKE2b-256 a96e97dd6dd5846c273235b4a589ed360628a3331301791feee238a6d5172b7d

See more details on using hashes here.

File details

Details for the file parsetypes-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: parsetypes-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for parsetypes-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6dcd010a6490aa0ba3fa65ba90a643d1fc68f29e63f41c612a4c303c7e911862
MD5 9af973bc42796878afaa6a60191e94a4
BLAKE2b-256 f732b10aed442742cc3a69c9b2c0df70b56598bc7b9f58176cd998d6d5da2f69

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page