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.1.tar.gz (25.2 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.8+ Windows x86-64

fast_query_parsers-1.0.1-cp38-abi3-win32.whl (630.1 kB view details)

Uploaded CPython 3.8+ Windows x86

fast_query_parsers-1.0.1-cp38-abi3-musllinux_1_2_x86_64.whl (958.1 kB view details)

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

fast_query_parsers-1.0.1-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.1-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.1-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.1-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.1-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.1-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (922.2 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64le

fast_query_parsers-1.0.1-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (958.6 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64

fast_query_parsers-1.0.1-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.1-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.1-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (819.1 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.5+ i686

fast_query_parsers-1.0.1-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.1-cp38-abi3-macosx_10_7_x86_64.whl (738.1 kB view details)

Uploaded CPython 3.8+ macOS 10.7+ x86-64

File details

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

File metadata

  • Download URL: fast_query_parsers-1.0.1.tar.gz
  • Upload date:
  • Size: 25.2 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.1.tar.gz
Algorithm Hash digest
SHA256 242615e036aa41b32362ddc50b4ac6b067571fd346f437702f1c3109b94c091f
MD5 f299f4636cfceda4808effbf1aeb7718
BLAKE2b-256 b3ea0372386c67423135bfe2d788e181e0734e8e24ca98d4e62a8f18bef16ff8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 263d73e4a7ffa1a821ea384bc05db3f5c39e69d2d177c3fac69453a48833ad6e
MD5 ed5793066b19990becd426f8b06a49b3
BLAKE2b-256 e9604e76280f5c2c867fc09b7c21e1598fe75ece980274614be2997a25cab837

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 dd4d0f7d857767bf21704cf59112b7a1effba0af4c9ebb22fbc1746588d99fe0
MD5 729dcad33b01f8c7d3c8712a0e253cac
BLAKE2b-256 4a9ead166a28091213268a713e7a30426d5bd4fd265092941613cd988e150a38

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 34ceee1e310c6386862ff488e0beb653cd9f0d7cd90d80ea52ff61d21e02fa74
MD5 5a2bd01654e7e44e7654d9554e062ec5
BLAKE2b-256 487abb074a9305fad0804eaf6ec5a802803106714d0727c148f23cc2dfc47c9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 9e910a5d9fc559b3478f281cd67cf867d2e91a745507545f2293f7bb68fe83ea
MD5 6319e4682796f9dec13b566501cde2e3
BLAKE2b-256 b2d9dd33f9de6bab02470becd19ecd6e2098d7ffa7773158f8fbd55187cb31e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 be1bc0c5befde296201c80f2c6f2982761ecf97fa722a54cff98a21dcb265d00
MD5 4caaef7eabb6a398daf442cd8911f560
BLAKE2b-256 2b332a3cd8a31784ece1bed11f31f98d99129825b27f088f23b42d1a733def4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 3943ff70648c3ca5e749fa6860a034bfb561a17f1fba975ce366b5c686ac6182
MD5 b4965fc08e0c61d81035d25674bd7333
BLAKE2b-256 d8318728033838e6e5d8b7a7f82c0ba18af34fb1d688cee29db88401126f261b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e16eece38db95064aa6f2f12168481bb0152d7dc6785088ebd624ac386cf47d8
MD5 6baf8882d234328a73827025cec33248
BLAKE2b-256 592065e69b203144d34e0fc71f2040a2ed9acf83a7c306194402772849511308

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 0a1f708ca40b99ff864c8352f809dea6c11223eb4dd35e2bbf3f8013d01b7d83
MD5 f36d4e1008f80621e40635549273975f
BLAKE2b-256 635b507e4d1884a6c11ee0f8803e2e94cb9a1d678353b89eeaa4bf225a6de394

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 935b7d14611012441dfd636e4c30ad36e32eb379d1163006752f9db98ef8b048
MD5 813b2bb5b229a4389ed1dbc89bc0cc44
BLAKE2b-256 5940a2d59a94ddb5e5f7db90433ffcd81f87d30f4326ac7aa7c314ddf9fdc25d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
Algorithm Hash digest
SHA256 295cf6518d1d97a2c64a1aacd71c9b6a98c51eb09ed8ad562904d3a68dbbd7ae
MD5 c4c40203e572e1dcbec879cdc1d81b44
BLAKE2b-256 bbc8aac5a8db696543c6dff98bb4c41889cd11145ae4b005e44dff5269869afc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 0b0a746fe1f4aead906362c169113ac55c481429382d2a896190a49e6d279071
MD5 11e183e73f5ce3a182f444e05887c94f
BLAKE2b-256 4a4b54a2a51be455ef9999468d3de716d8cdb8c1cfda8122d9c06d0a14305152

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8d460b92821ecb55fceb9ba47c4bf8e794eb2b398bf54d5deab0854c36006ee9
MD5 c0d96cbcb8cfe674e18bbe8b0c84bd3a
BLAKE2b-256 e43fdb1ec7d2bd837be6d1ae49390b7f40141576e109689d90ebc3c3b68ad0f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 f7830d8a6c4023edf6119101b3f55ce35103993d0b80b50a7b815134582931b4
MD5 e2f9055f9083a0eb99fb383ebc756812
BLAKE2b-256 b72775ee247cae090ed96778b85060f1d32f8158f7c00b12df261795d71585d0

See more details on using hashes here.

File details

Details for the file fast_query_parsers-1.0.1-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.1-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 60363e41464e4a43589bbfa09dcb4364dfd934ffdf5bac002d90ee95ff8ace22
MD5 48741f7d823b392c36f5038c2fda2093
BLAKE2b-256 51b7278cbd773763fa99e6dce9375b6e83dd3ba86117070eb6444ee9f6ca709a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.1-cp38-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 962af428aa50b848ae1f84ede487fa802bea1e2f2a32d5838c7faa5ae5c5acf7
MD5 77f80baecdbd752a9e249633870f8d0f
BLAKE2b-256 bbb87966985d2a6f5082c09fb3144c1fe8a62b1b73fb783475a4a541f7cf1ad9

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