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:

  • 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

Basic parsing:

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

Parsing a series so that it has a consistent type:

parser = TypeParser()
parser.parse_series(["1", "2", "3"])        # [1, 2, 3]
parser.parse_series(["5", "6.7", "8."])     # [5., 6.7, 8.]
parser.parse_series(["true", "false", ""])  # [True, False, None]
parser.parse_series(["1", "2.3", "abc"])    # ["1", "2.3", "abc"]

Parsing a table so that each column is of a consistent type:

parser = TypeParser()
table = parser.parse_table([
	["1", "5",   "true",  "1"],
	["2", "6.7", "false", "2.3"],
	["3", "8.0", "",      "abc"],
]):
assert table == [
	[1, 5.,  True,  "1"],
	[2, 6.7, False, "2.3"],
	[3, 8.,  None,  "abc"],
]

Issues

Found a bug? Please file an issue, or, better yet, submit a pull request.

Development

Clone the repository with git clone https://github.com/yushiyangk/parsetypes.git.

The source for the package is src/, with tests in tests/.

Virtual environment

Create the venv using python -m venv ..

To activate the venv, on Linux run source Scripts/activate, and on Windows run Scripts/Activate.ps1 or Scripts/activate.bat.

Later, to deactivate the venv, run deactivate.

Dependencies

Run pip install -r requirements.dev.txt.

Install

To install the package locally (in the venv) for development, run pip install -e ..

Tasks

For unit tests, run pytest.

To run unit tests across all supported Python versions, run tox p -m testall. This is slower than just pytest. Note that only Python versions that are installed locally will be run.

To run the full set of tasks before package publication, run tox p -m prepare. Alternatively, see below for manually running individual steps in this process.

Unit tests

Run pytest or coverage run -m pytest.

For coverage report, first run coverage run -m pytest, then either coverage report -m to print to stdout or coverage html to generate an HTML report in htmlcov/. Alternatively, run tox r -m test to do both steps automatically (slower).

Documentation

Run tox r -m docs.

The documentation is generated in docs/html/, using template files in docs/template/.

Packaging

Before packaging, check the package metadata by running pyroma . or tox r -m metadata.

To generate sdist and wheel packages, delete dist/ and parsetypes.egg-info/ if they exist, then run python -m build. Run twine check dist/* to check that the packages were generated properly. Alternatively, run tox r -m package to do these steps automatically.

Config files

  • MANIFEST.in Additional files to include in published sdist package
  • pyproject.toml Package metadata, as well as configs for test and build tools
  • requirements.dev.txt Package dependencies for development, in pip format
  • requirements.publish.txt Package dependencies for publishing, in pip format
  • tox.ini Config file for tox

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.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.1.tar.gz (107.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.1-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: parsetypes-0.3.1.tar.gz
  • Upload date:
  • Size: 107.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for parsetypes-0.3.1.tar.gz
Algorithm Hash digest
SHA256 d2a4f3ad172e46d3b91a7295095ec69c6f6446dd9aa79586a6c96321bee92342
MD5 cc7dc701a0a578df382360ef943abb76
BLAKE2b-256 3458557f4740785b68f66b62518bc6d5635e7d021372bcce8185b3e9027ed572

See more details on using hashes here.

File details

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

File metadata

  • Download URL: parsetypes-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for parsetypes-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 57880b82cff77dc52e0222a6b22a978686cf07c56cabc3a16fa76c7681867e96
MD5 130d9b72c0a0f30ed3c814eeeb14f655
BLAKE2b-256 554edb41ef41c779d1ed66a7e27a8c7cb9ec977f58d298254e948022f232e756

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