Skip to main content

Ultra-fast query string and url-encoded form-data parsers

Project description

Fast Query Parsers

Starlite logo

This library includes ultra-fast Rust based query string and urlencoded parsers. These parsers are used by Starlite, but are developed separately - and can of course be used separately.

Discord Matrix

Installation

pip install fast-query-parsers

Usage

The library exposes two function parse_query_string and parse_url_encoded_dict.

parse_query_string

This function is used to parse a query string into a list of key/value tuples.

from fast_query_parsers import parse_query_string

result = parse_query_string(b"value=1&value=2&type=dollar&country=US", "&")
# [("value", "1"), ("value", "2"), ("type", "dollar"), ("country", "US")]

The first argument to this function is a byte string that includes the query string to be parsed, the second argument is the separator used.

Benchmarks

Query string parsing is more than x5 times faster than the standard library:

stdlib parse_qsl parsing query string: Mean +- std dev: 2.86 us +- 0.03 us
.....................
parse_query_string parsing query string: Mean +- std dev: 916 ns +- 13 ns
.....................
stdlib parse_qsl parsing urlencoded query string: Mean +- std dev: 8.30 us +- 0.10 us
.....................
parse_query_string urlencoded query string: Mean +- std dev: 1.50 us +- 0.03 us

parse_url_encoded_dict

This function is used to parse a url-encoded form data dictionary and parse it into the python equivalent of JSON types.

from urllib.parse import urlencode

from fast_query_parsers import parse_url_encoded_dict

result = parse_url_encoded_dict(
    urlencode(
        [
            ("value", "10"),
            ("value", "12"),
            ("veggies", '["tomato", "potato", "aubergine"]'),
            ("nested", '{"some_key": "some_value"}'),
            ("calories", "122.53"),
            ("healthy", "true"),
            ("polluting", "false"),
            ("json", "null"),
        ]
    ).encode()
)

# result == {
#     "value": [10, 12],
#     "veggies": ["tomato", "potato", "aubergine"],
#     "nested": {"some_key": "some_value"},
#     "calories": 122.53,
#     "healthy": True,
#     "polluting": False,
#     "json": None,
# }

This function handles type conversions correctly - unlike the standard library function parse_qs. Additionally, it does not nest all values inside lists.

Benchmarks

Url Encoded parsing is more than x2 times faster than the standard library, without accounting for parsing of values:

stdlib parse_qs parsing url-encoded values into dict: Mean +- std dev: 8.99 us +- 0.09 us
.....................
parse_url_encoded_dict parse url-encoded values into dict: Mean +- std dev: 3.77 us +- 0.08 us

To actually mimick the parsing done by parse_url_encoded_dict we will need a utility along these lines:

from collections import defaultdict
from contextlib import suppress
from json import loads, JSONDecodeError
from typing import Any, DefaultDict, Dict, List
from urllib.parse import parse_qsl


def parse_url_encoded_form_data(encoded_data: bytes) -> Dict[str, Any]:
    """Parse an url encoded form data into dict of parsed values"""
    decoded_dict: DefaultDict[str, List[Any]] = defaultdict(list)
    for k, v in parse_qsl(encoded_data.decode(), keep_blank_values=True):
        with suppress(JSONDecodeError):
            v = loads(v) if isinstance(v, str) else v
        decoded_dict[k].append(v)
    return {k: v if len(v) > 1 else v[0] for k, v in decoded_dict.items()}

With the above, the benchmarks looks like so:

python parse_url_encoded_form_data parsing url-encoded values into dict: Mean +- std dev: 19.7 us +- 0.1 us
.....................
parse_url_encoded_dict parsing url-encoded values into dict: Mean +- std dev: 3.69 us +- 0.03 us

Contributing

All contributions are of course welcome!

Repository Setup

  1. Run cargo install to setup the rust dependencies and poetry install to setup the python dependencies.
  2. Install the pre-commit hooks with pre-commit install (requires pre-commit).

Building

Run poetry run maturin develop --release --strip to install a release wheel (without debugging info). This wheel can be used in tests and benchmarks.

Benchmarking

There are basic benchmarks using pyperf in place. To run these execute poetry run python benchrmarks.py.

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

fast_query_parsers-0.2.0.tar.gz (22.9 kB view details)

Uploaded Source

Built Distributions

fast_query_parsers-0.2.0-cp38-abi3-win_amd64.whl (193.4 kB view details)

Uploaded CPython 3.8+ Windows x86-64

fast_query_parsers-0.2.0-cp38-abi3-win32.whl (190.0 kB view details)

Uploaded CPython 3.8+ Windows x86

fast_query_parsers-0.2.0-cp38-abi3-musllinux_1_2_x86_64.whl (440.2 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ x86-64

fast_query_parsers-0.2.0-cp38-abi3-musllinux_1_2_i686.whl (464.6 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ i686

fast_query_parsers-0.2.0-cp38-abi3-musllinux_1_2_armv7l.whl (517.3 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARMv7l

fast_query_parsers-0.2.0-cp38-abi3-musllinux_1_2_aarch64.whl (434.2 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (268.6 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ x86-64

fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (348.5 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ s390x

fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (354.8 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64le

fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (368.0 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64

fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (253.5 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARMv7l

fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (253.2 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (294.5 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.5+ i686

fast_query_parsers-0.2.0-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (487.0 kB view details)

Uploaded CPython 3.8+ macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

fast_query_parsers-0.2.0-cp38-abi3-macosx_10_7_x86_64.whl (246.6 kB view details)

Uploaded CPython 3.8+ macOS 10.7+ x86-64

File details

Details for the file fast_query_parsers-0.2.0.tar.gz.

File metadata

  • Download URL: fast_query_parsers-0.2.0.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for fast_query_parsers-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3a0105dc3f5c39befe89aaeb5ac0e71e02f9854f75fd09b4b13775085be44ab5
MD5 1349f8775585d284af4e70faf530af86
BLAKE2b-256 3c1bd48e1b4fe574238f77d015f77ab30f3201c2446712ea41ca0a4240e93bae

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 1a0d8b3435a6ab51ac8eb3a96eab8fc715b09e7e196ec74af48ac4b126f3d4bc
MD5 8234f03855a80a0795bf083896ee1c2f
BLAKE2b-256 140c6364f08f77134d5e081b431431c7b42bd43eae4d0092405b01e8f220946e

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-win32.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 a5d262709e2f08a5f953070160eabe7ca3c909985026b00ee3c7d45d0bf9b9d6
MD5 53b9b545c84adc916de287153d66df09
BLAKE2b-256 7aba05c48fe4ae0b76a624ce6f10c09d55151e9689d185eb2e898f0b2b9ce3d4

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2916e7fe50b91c9f0576af7c6b06677e6634a7017967fe3d2e9030a86a650afa
MD5 e357ce8fb766640314f0a38fed31e501
BLAKE2b-256 68fda46800f9b0db266eacf8c6ff8e3823395186ff697557c23ad2041a2f38a1

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 325f267013388b902f9d0a95a84a128c3aeb2f1b252494e7f516dfd433c7e5a8
MD5 99041be8c56a456ba17da923e34e0820
BLAKE2b-256 d8b2d274629075a8f95f771612f81e39c5eb2db38a9f4b27e3ecea67c4d8de42

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 8e55c12af6235d04cd1ed0ddfbcf959bd1265b0bf6a8fa5b1481f47feb44bed7
MD5 f4af2c30b0c1000ca0b139e583383153
BLAKE2b-256 ca2c56813eb14688f53e69526d4ddd4c46175008453eebb488da38440aceb273

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 51335ffed2d835e5f679f8f01c593e8477b21a6e6bc90a0caa9e1eb66016e533
MD5 f302bbfe755d79f95ea3e604c4659f27
BLAKE2b-256 f5d6ae27fa05bf86a53f2a39f3928b79012919ba7c618caf5d60d903fc75e121

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3ffaf0cde56652c2b40ec9292b4abd79c2a76a0f095ffb22dbead6129f47209e
MD5 02982863fdef3dadd63bd9ceaea5dcc7
BLAKE2b-256 18d7fecfbe46bf247162eb3a347d8369204e393aeb62732be8cb7dd259cf930f

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 e9d86c9e09f1eebb6fa161792f6565b8b4821211ef58f054f3183c210ac01a21
MD5 ee91f147e57ed92a17dd9a9e5fa437f2
BLAKE2b-256 0e3830e7dd1f5799b20c7ca46f37710ba7974ce708dae8b84142f57ad74f83a8

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 9764839c444c025aaad114373e9b1e6eafd16e32321663a870811358f9d65f18
MD5 d4fbb8378b4e38456c1b5f1822c86ad9
BLAKE2b-256 a6279d6af078a9adf6f6a31ad9a348bf37ead224d176d5e633a54deaa0fad221

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
Algorithm Hash digest
SHA256 03520ba888acfd8a26a1a4cfa60c499e29db5d4d2fdcd1a8f393c786ad493d13
MD5 b866dc17b0f3c06c1ead837b71eac390
BLAKE2b-256 16fad4c57c6db42875fe1148544fec386abcf4bb9532f857611334e4c8ae87b3

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 fdec68138ed327f1b893e0a78156a8b2fcfc0753cc26ea28da53d578801ef86f
MD5 de2db80cbaba5fed5ce68042e0d80bd0
BLAKE2b-256 8c49d5d6a2fd3c420dda48bb38de979b3d67d99f836aa59cb486106797f778e5

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b1556b7ab6614525d5697e9f46bd56200de505e6423ee99df38f88163df7053d
MD5 e78f54ab71b805b97bab44d63353bec4
BLAKE2b-256 f53b1e5d57d43651466ff9ac21489462a9aa67123690cd3b8341bb722204592c

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 862705292b003d3c1f0d0219be874b586e71a3e9bc462ad24493c7de3d7ca53b
MD5 5de7467e4f18d1d4bc8a613732f25939
BLAKE2b-256 f94367cc5168d5b8da4aa876eabdefc48c80517b9068f4e2b8ee26ef01f9b191

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 301154360a39b1dc347e9a9e0cb3578c8877f63bb96367d54e8012b81a4007b3
MD5 f7da84f18c7d1dc8a9dcf99693c98c54
BLAKE2b-256 a3d1ac841c3afac7659b1da4a23e4f2064bac327c4b3b449e89da12c845d4574

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.2.0-cp38-abi3-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for fast_query_parsers-0.2.0-cp38-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 4f7219e1dc7480307a32fae000470c2b8979849a34395d7787898da23e7d6a7e
MD5 d89013a91982d25764f039d04da8e6ef
BLAKE2b-256 a478f3d344f94f37b04d9ab5b9b779c568130db21f8d8e3b415e668f73631961

See more details on using hashes here.

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