Skip to main content

Litestar Logo - Light Litestar Logo - Dark

Project Status
CI/CD Publish CI
Package PyPI - Version PyPI - Support Python Versions PyPI - Downloads
Community Reddit Discord Matrix Medium Twitter Blog
Meta Litestar Project License - MIT Litestar Sponsors linting - Ruff code style - Black

Fast Query Parsers

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.

[!IMPORTANT]
Starlite has been renamed to Litestar

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.

Release files for fast-query-parsers 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fast-query-parsers 1.0.3
File Size Uploaded
fast_query_parsers-1.0.3.tar.gz 25.3 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for fast-query-parsers 1.0.3
File
fast_query_parsers-1.0.3-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details
fast_query_parsers-1.0.3-cp38-abi3-win32.whl CPython 3.8 abi3 Windows x86-32 Details
fast_query_parsers-1.0.3-cp38-abi3-musllinux_1_2_x86_64.whl CPython 3.8 abi3 Linux musl 1.2+ x86-64 Details
fast_query_parsers-1.0.3-cp38-abi3-musllinux_1_2_i686.whl CPython 3.8 abi3 Linux musl 1.2+ x86-32 Details
fast_query_parsers-1.0.3-cp38-abi3-musllinux_1_2_armv7l.whl CPython 3.8 abi3 Linux musl 1.2+ ARMv7l Details
fast_query_parsers-1.0.3-cp38-abi3-musllinux_1_2_aarch64.whl CPython 3.8 abi3 Linux musl 1.2+ ARM64 Details
fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.8 abi3 Linux glibc 2.17+ x86-64 Details
fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl CPython 3.8 abi3 Linux glibc 2.17+ IBM System/390x Details
fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl CPython 3.8 abi3 Linux glibc 2.17+ PowerPC 64-le Details
fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl CPython 3.8 abi3 Linux glibc 2.17+ PowerPC 64-be Details
fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl CPython 3.8 abi3 Linux glibc 2.17+ ARMv7l Details
fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.8 abi3 Linux glibc 2.17+ ARM64 Details
fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl CPython 3.8 abi3 Linux glibc 2.5+ x86-32 Details
fast_query_parsers-1.0.3-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl CPython 3.8 abi3 macOS 10.9+ x86-64, macOS 10.9+ universal2 (ARM64, x86-64), macOS 11.0+ ARM64 Details
fast_query_parsers-1.0.3-cp38-abi3-macosx_10_7_x86_64.whl CPython 3.8 abi3 macOS 10.7+ x86-64 Details

Total release size: 13.5 MB

Release files / fast_query_parsers-1.0.3.tar.gz

Download URL fast_query_parsers-1.0.3.tar.gz
Size 25.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5200a9e02997ad51d4d76a60ea1b256a68a184b04359540eb6310a15013df68f
BLAKE2b-256 checksum
How to use checksums
dd203a00b889a196e8dc5bede2f168d4a14edc8b5bccc3978a9f497f0f863e79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-win_amd64.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-win_amd64.whl
Size 689.7 kB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
21ae5f3a209aee7d3b84bdcdb33dd79f39fc8cb608b3ae8cfcb78123758c1a16
BLAKE2b-256 checksum
How to use checksums
ae4b07fe4d7b5c458bdde9b0bfd8e8cb5762341af6c9727b43c2331c0cb0dbc3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-win32.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-win32.whl
Size 646.4 kB
Tags CPython 3.8 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
14b3fab7e9a6ac1c1efaf66c3fd2a3fd1e25ede03ed14118035e530433830a11
BLAKE2b-256 checksum
How to use checksums
0ae321bc18edc003b54a2069eb854b9f92cacb5acc99e03c609487a23a673755
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-musllinux_1_2_x86_64.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-musllinux_1_2_x86_64.whl
Size 967.7 kB
Tags CPython 3.8 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
a70d4d8852606f2dd5b798ab628b9d8dc6970ddfdd9e96f4543eb0cc89a74fb5
BLAKE2b-256 checksum
How to use checksums
ea58942327d3f2694b8f1a2fffaaaef1cc3147571852473a80070ebd6156a62e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-musllinux_1_2_i686.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-musllinux_1_2_i686.whl
Size 965.4 kB
Tags CPython 3.8 Linux musl 1.2+ x86-32 abi3
SHA-256 checksum
How to use checksums
7ca6be04f443a1b055e910ccad01b1d72212f269a530415df99a87c5f1e9c927
BLAKE2b-256 checksum
How to use checksums
6fa9132572b9f40c2635fdedb7a1cb6cedd9c880f8ffbbfdd6215ee493bb6936
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-musllinux_1_2_armv7l.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-musllinux_1_2_armv7l.whl
Size 963.0 kB
Tags CPython 3.8 Linux musl 1.2+ ARMv7l abi3
SHA-256 checksum
How to use checksums
3a6377eb0c5b172fbc77c3f96deaf1e51708b4b96d27ce173658bf11c1c00b20
BLAKE2b-256 checksum
How to use checksums
f8b8bf5e44588f6ebd81d0c53ba49c79999dc54cb0fe81ad6dde6fed2cd45b56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-musllinux_1_2_aarch64.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-musllinux_1_2_aarch64.whl
Size 911.0 kB
Tags CPython 3.8 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
5736d3c32d6ba23995fa569fe572feabcfcfc30ac9e4709e94cff6f2c456a3d1
BLAKE2b-256 checksum
How to use checksums
05d45eb8c9d400230b9a45a0ce47a443e9fe37b0902729f9440adef677af1f0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 828.6 kB
Tags CPython 3.8 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
55a30b7cee0a53cddf9016b86fdad87221980d5a02a6126c491bd309755e6de9
BLAKE2b-256 checksum
How to use checksums
7434950b6d799839c11e93566aef426b67f0a446c4906e45e592026fde894459
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Size 940.0 kB
Tags CPython 3.8 Linux glibc 2.17+ IBM System/390x abi3
SHA-256 checksum
How to use checksums
e947e7251769593da93832a10861f59565a46149fa117ebdf25377e7b2853936
BLAKE2b-256 checksum
How to use checksums
c39f4dfa29d74276fa07c40689bfaa3b21d057249314aeb20150f0f41373d16d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Size 969.5 kB
Tags CPython 3.8 Linux glibc 2.17+ PowerPC 64-le abi3
SHA-256 checksum
How to use checksums
6720505f2d2a764c76bcc4f3730a9dff69d9871740e46264f6605d73f9ce3794
BLAKE2b-256 checksum
How to use checksums
419b5a42ddd23b85357be6764e14daa607d9b16bc6a395aae2c1cc2077e0a11d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
Size 1.0 MB
Tags CPython 3.8 Linux glibc 2.17+ PowerPC 64-be abi3
SHA-256 checksum
How to use checksums
0bdcc0ddb4cc69d823c2c0dedd8f5affc71042db39908ad2ca06261bf388cac6
BLAKE2b-256 checksum
How to use checksums
f0357a9a0c50588033edd9efba48f21e251dfcf77eaec2aff470988f622fbd3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Size 729.9 kB
Tags CPython 3.8 Linux glibc 2.17+ ARMv7l abi3
SHA-256 checksum
How to use checksums
a6e3d816c572a6fad1ae9b93713b2db0d3db6e8f594e035ad52361d668dd94a8
BLAKE2b-256 checksum
How to use checksums
75068861197982909bec00b180527df1e0e9791715271bfb84c8be389b6bf077
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 764.0 kB
Tags CPython 3.8 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
66630ad423b5b1f5709f82a4d8482cd6aa2f3fa73d2c779ff1877f25dee08d55
BLAKE2b-256 checksum
How to use checksums
515bb10719598dbd14201271efd0b950c6a09efa0a3f6246fec3c192c6b7a8d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Size 863.1 kB
Tags CPython 3.8 Linux glibc 2.5+ x86-32 abi3
SHA-256 checksum
How to use checksums
9bc2b457caa38371df1a30cfdfc57bd9bfdf348367abdaf6f36533416a0b0e93
BLAKE2b-256 checksum
How to use checksums
81a8ee95263abc9806c81d77be8a3420d1f4dde467a10030dde8b0fa0e63f700
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Size 1.5 MB
Tags CPython 3.8 abi3 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
42f26875311d1b151c3406adfa39ec2db98df111a369d75f6fa243ec8462f147
BLAKE2b-256 checksum
How to use checksums
c521c8c160f61a740efc4577079eb5747a6b2cb8d1168a84a0bfda6044113768
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / fast_query_parsers-1.0.3-cp38-abi3-macosx_10_7_x86_64.whl

Download URL fast_query_parsers-1.0.3-cp38-abi3-macosx_10_7_x86_64.whl
Size 766.2 kB
Tags CPython 3.8 abi3 macOS 10.7+ x86-64
SHA-256 checksum
How to use checksums
afbf71c1b4398dacfb9d84755eb026f8e759f68a066f1f3cc19e471fc342e74f
BLAKE2b-256 checksum
How to use checksums
43184179ac7064b4216ca42f2ed6f74e71254454acf2ec25ce6bb3ffbfda4aa6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

This release

1.0.3 This release

16 release files

1.0.2

16 release files

1.0.1

16 release files

1.0.0

16 release files

0.2.0

16 release files

0.1.0

16 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page