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 Litestar, 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

encoded = 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 = parse_url_encoded_dict(encoded, parse_numbers=True)

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

Note: the second argument passed to parse_url_encoded_dict dictates whether numbers should be parsed. If True, the value will be parsed into an int or float as appropriate, otherwise it will be kept as a string. By default the value of this arg is True.

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 mimic 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-1.0.2.tar.gz (25.3 kB view details)

Uploaded Source

Built Distributions

fast_query_parsers-1.0.2-cp38-abi3-win_amd64.whl (670.9 kB view details)

Uploaded CPython 3.8+ Windows x86-64

fast_query_parsers-1.0.2-cp38-abi3-win32.whl (630.2 kB view details)

Uploaded CPython 3.8+ Windows x86

fast_query_parsers-1.0.2-cp38-abi3-musllinux_1_2_x86_64.whl (957.9 kB view details)

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

fast_query_parsers-1.0.2-cp38-abi3-musllinux_1_2_i686.whl (959.6 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ i686

fast_query_parsers-1.0.2-cp38-abi3-musllinux_1_2_armv7l.whl (957.0 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARMv7l

fast_query_parsers-1.0.2-cp38-abi3-musllinux_1_2_aarch64.whl (901.8 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (786.8 kB view details)

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

fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (890.0 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ s390x

fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (922.1 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64le

fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (958.3 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64

fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (692.6 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARMv7l

fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (724.1 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (819.2 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.5+ i686

fast_query_parsers-1.0.2-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.4 MB 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-1.0.2-cp38-abi3-macosx_10_7_x86_64.whl (738.0 kB view details)

Uploaded CPython 3.8+ macOS 10.7+ x86-64

File details

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

File metadata

  • Download URL: fast_query_parsers-1.0.2.tar.gz
  • Upload date:
  • Size: 25.3 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-1.0.2.tar.gz
Algorithm Hash digest
SHA256 c3f3f66e2d139a003ebb061963a72d3becf24e3a207d07c7b7c454446a5ebbfb
MD5 247850a55aa55cfd198bf65b052fbfa8
BLAKE2b-256 e6a2cec98bf13680249ffb9d93c52706cd6e585a0d705624501ea23e636e6788

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 89dd3a07f4b84660891fb6c0279cb9aa9be1f1c976b41c15f852bf3b7a950e20
MD5 d78f6e2e8b9a775598d333f345774e23
BLAKE2b-256 e9d4bca1b3c6582f23e0de9580485e1d3ab9df9ea5ea1be792110bc50f263b00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 2b36e7b2727ec645dc4ae3b929fe348b50c6b38626b5bc4da5d93166edf87ccb
MD5 4869c6e08e71fa38d3b91d2ee9de8919
BLAKE2b-256 d09a9141632a6dd606ded74c87096355c57b2139e320335f549299ef95905f00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 53b6e4f72cedb2c2291e20a6744891e47a0080aae0caa62ac906853045c74ddd
MD5 700b3ae5a1a1d7336b8b0ad934efe8b4
BLAKE2b-256 e41903088511a1f3a62a7209b5da7d811a7965439bac05bfcf132536b35fb39e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 70adbcd9f2df2fa7efd3eb2a6901bdc788524a07b08253e5b4e5ef1cb33455d2
MD5 1af7eb2e2ba650d2b70d130c0c66166b
BLAKE2b-256 8b8f44287c466543bc8bd3ee8c0bc34cbd2bca603179add95191135e9b27fed2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 d9b4b644d32fc696ed6e658f5240f016f05902d899067119e1e041729ca816ad
MD5 8222300f363407a9171e0e50a432d62b
BLAKE2b-256 71db7691e3eff3961a0480912a6ad84aa65bee7ad480f9d1836397726c3139d6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 a21f4c1de1936814bc0b1a6a3a39ccb0a737c9a6a92de99e65d0c0d05f191182
MD5 c3c00e8d18178ceb15a0d2fe5fc430a7
BLAKE2b-256 b816acc15e7f89ea30ed3f6b294fe3295f89f10c14e8cee8f2c5803b40230223

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c511b436f9102797e8935889f52364207b57b2ea4a72d2b5dfcd88f448e50560
MD5 480bd6e4eda1c8ca3da809645856eccb
BLAKE2b-256 582b41149f413fce8c75721b3300d3f687f6ee1faae4eb4d9610f50e6229a167

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 2afa643626ed121a3d62082a22ca61a4e307020c429f9a823e404caaa416c9e7
MD5 a0ec9407af8304f00c87150c49548065
BLAKE2b-256 2f623311078af8f65fc3a16e28e1985760241303ecaf1e774fcd79145e36330a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 ceec861ed4b83cda9360873d31e23bb6226d257dfb2daef445aa537f429c99ca
MD5 7f18df67694cf0d1e88e78d7976867ff
BLAKE2b-256 556994b6c11da60e84ecb94a68f0abcfee979ee926e784d53bdb302ab7456c0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
Algorithm Hash digest
SHA256 51ffc6e14b9c6d05197ac13859bdca7d6f2586a1cc8689f4fc2cc20d480cd839
MD5 d88bd6091de6df538dd37d2439edd79e
BLAKE2b-256 1b002b2ea2933cb44c1f7bd7ef8d449d05fe950d4a7e5f161d9cee0e3fa21e52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 9c8dff92f8453ee4ceab37be20fd1e9cf1330996a4dd9d393303526fc3682f9d
MD5 d0ca18b5a6df462802072aa7603e83dd
BLAKE2b-256 383c04bb38bbeb8da09e751a89b95a668db526eea9dc3cc1c93ddc1fb3876d77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9fb9c79126d16a26e9bdfefbe548b026a82f04bc6dac93663cf3b07b1ae5e08b
MD5 8c14caded490b03ad9cf2be736ee365b
BLAKE2b-256 d09192dd784c183811c38427abafafec7483172f573087dacce1e77475ec57a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 f14c490e1dc43e1471e9b29e102d5bc64ac4285aafb055fd15291f13c74bc535
MD5 24ab4a67214fa563bdfe8f4b581ad85d
BLAKE2b-256 532dae924b4b33275245174f4688a6a6ae5fc10d6ccf3ccbf9516e5fdc01b6df

See more details on using hashes here.

File details

Details for the file fast_query_parsers-1.0.2-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-1.0.2-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 634e17da63f9ba2caa57f9886deeba5b2bdb8098a0d4d0ee0a99cf93d8ab327a
MD5 c0bd0adf12c21610e654f77950e06353
BLAKE2b-256 c3d97937dae4b7f3c5b714f7172c3566e3d7af35767d3ca64e33ec6abb24d5c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.2-cp38-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 203680ea5b0f249666ac5e6ca483e4afbae1b4d74dbddc89f217e57d615a824b
MD5 6a1904c831db2ee50928450804218a8b
BLAKE2b-256 4dcd3b3fd9a85c1861f84a22573093fef6a3cf9312b4443d83fa75b0f690a535

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