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
andallow_sign
(bothTrue
by default) toparser.parse_int()
, for parity withparser.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 viafrom parsetypes import *
(previously onlyTypeParser
andreduce_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file parsetypes-0.3.2.tar.gz
.
File metadata
- Download URL: parsetypes-0.3.2.tar.gz
- Upload date:
- Size: 106.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82362496cf55db1e9a9ce5921fad2c553304375f2bd5f17964c29ff98d5e8b07 |
|
MD5 | aa9eb380d233492e53aed187fa9b6b50 |
|
BLAKE2b-256 | d55605e218b3b91a46d7bd0d504b50667b66a70f4df3f78ca9be2225dd8a37cf |
File details
Details for the file parsetypes-0.3.2-py3-none-any.whl
.
File metadata
- Download URL: parsetypes-0.3.2-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97f55acd9553136c464a846d636af05dce5ab96a44b11325b0b686e347298e4c |
|
MD5 | 2c701afbe4483b1809f7bbefef99f375 |
|
BLAKE2b-256 | 379df1c0dc0b26c90191f1e8016283ffec1ef3761b9035beb73213db23700e85 |