Skip to main content

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

Release files for parsetypes 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for parsetypes 0.3.3
File Size Uploaded
parsetypes-0.3.3.tar.gz 106.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for parsetypes 0.3.3
File Interpreter ABI Platform
parsetypes-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 120.7 kB

Release files / parsetypes-0.3.3.tar.gz

Download URL parsetypes-0.3.3.tar.gz
Size 106.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7b58b11c97831bb7fce37ae3edd72e0c970575dea452ec6199b548aa33d24770
BLAKE2b-256 checksum
How to use checksums
a96e97dd6dd5846c273235b4a589ed360628a3331301791feee238a6d5172b7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.13.0

Release files / parsetypes-0.3.3-py3-none-any.whl

Download URL parsetypes-0.3.3-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6dcd010a6490aa0ba3fa65ba90a643d1fc68f29e63f41c612a4c303c7e911862
BLAKE2b-256 checksum
How to use checksums
f732b10aed442742cc3a69c9b2c0df70b56598bc7b9f58176cd998d6d5da2f69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.13.0

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page