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

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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.8+ Windows x86-64

fast_query_parsers-1.0.0-cp38-abi3-win32.whl (630.0 kB view details)

Uploaded CPython 3.8+ Windows x86

fast_query_parsers-1.0.0-cp38-abi3-musllinux_1_2_x86_64.whl (957.7 kB view details)

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

fast_query_parsers-1.0.0-cp38-abi3-musllinux_1_2_i686.whl (959.4 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ i686

fast_query_parsers-1.0.0-cp38-abi3-musllinux_1_2_armv7l.whl (956.9 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARMv7l

fast_query_parsers-1.0.0-cp38-abi3-musllinux_1_2_aarch64.whl (902.0 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

fast_query_parsers-1.0.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (786.7 kB view details)

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

fast_query_parsers-1.0.0-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.0-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.0-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (958.5 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64

fast_query_parsers-1.0.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (692.7 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARMv7l

fast_query_parsers-1.0.0-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.0-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.0-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.0-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.0.tar.gz.

File metadata

  • Download URL: fast_query_parsers-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 76f32c69c13a0a690111e9e3d8b6f63898aa45d604950e91d5c6a6b2abad6aea
MD5 0863e49c4acef034cb83382d0c998448
BLAKE2b-256 8ff61c9fca424004fec9dfbcbdc466b2336291d00e264afbe788ae9f38e31369

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 af3d959da3fdb2a3df1ba22d1db9da21fda3c6537ab3cb6d26bc871586ae0002
MD5 fd607fb0e7db02ba0b63f3bc0ca7b87f
BLAKE2b-256 3afd1bcf26be15fdfbeaa9c514b21f419fe7926b7def228ca957065927afa135

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 6a95f32c4286c8cc56e6d1ef2fd68ae0bf45d4cb142c8119a7de7e0c5e0ce0de
MD5 6eb2e5736bc973454cf6c20beee761e2
BLAKE2b-256 d1f7c2470b0b8aab4514b0628a0e7c4a348b5551d93f3f1d06e5829854c25392

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3f76e8be49354be7dbbe6040efea87f21f16effe03e903a369f78fa0f956b306
MD5 dfdc01bd02b34151fcb5183c647a71b9
BLAKE2b-256 b51a2bb8aaecbccb54d2274f84370ac401aa7fcca44b97d178d5e12b9cecad25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 f96b29d6b91cf629c4a7cd20b89068e345effdf589ec4573ef669f2e156214df
MD5 35d737a4b17d01ca6d7de3adc71cba87
BLAKE2b-256 2ff0104aa0a04761c660d58470b3161b1bdff4597cf95f0d66005e7748cb32ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 4c321c87895e286e08a836139a78d035dab3ac4ce77f9906dbb2f46c5675f326
MD5 3e954924ff9f8ac532faf74d43292fc0
BLAKE2b-256 1caefb86bd49adb791f0e8e495146466caa26bd2174ebab6d3a582f1277e88b5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 4b8a15f3fa03638dbf997f2071a84f3ffe2877742712eff3435591adf41aab1f
MD5 88f2e096537b7bef9d348f1f6e33c879
BLAKE2b-256 70a262e467c77b33ab326be0de2531173681350e198ca3dc3f7f53e624b62ecd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 44f2694643f9ca7868a07943d70758ffff6f56ce921e3875169d419ed97fa519
MD5 0095257e9d9a0a2f3f80d65c4335e5d1
BLAKE2b-256 51adb13920c861a9c543f2a6778090012189af610a077ad5dfa4ba3da76a779c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 f3816e00cff6c08ca7962359afa6f9b0504bd2b716cda74db0f36b0a9d7e0f06
MD5 932ae3e7312a1cf901a6aff600ce918c
BLAKE2b-256 f91d578a4a22da3e12506974a2e76b8ca28128f491395d5af4344b92dcc8cce1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 66a1aef9314c2d60dd366f32fefca05124ee6c3cc2565d0afce72cad76f74128
MD5 a087e505497604409e2286e5abfff72d
BLAKE2b-256 ec450f3ea51db4aba1cd3e2607fe49ce44aad02948cde2e1e59efe7e00fa3f72

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
Algorithm Hash digest
SHA256 6761c582d112f3501bd468ab1788d6227d62f918d3bd0f598cf03dd39bf45420
MD5 39c858d082f1335e55e0bb1d8feb29eb
BLAKE2b-256 d88ab7d2427adb6fc03d193c598a5141fefea9216abc17c4d0a258b7041db179

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 4a6c95552c6e3f33292c2f957e9583b460f959936b117e8fdf72f84303aa4591
MD5 4abea4bb1384e16bddc9fc8557c08e1e
BLAKE2b-256 cd17251a874f3e28f57a8ebd557f0eaaf6826933c2e4b20f574d750e87aeee94

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 75d188ee816ac8547d2d86672917a350393326acecdf965d3c1d02bf7fcb4e32
MD5 baf912c305f303801c4e102bdbfa7d43
BLAKE2b-256 5dedcb2e3f02ce2d8460c384690253060f24a6facdf6c33b9e4497d5a21eb2ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 cfc0df87c48b0f22f97c8e3c1a12bed33e4dba15009feefff66ec13c154fac37
MD5 e639d3bc43c3882f75d9907ee32fe5a8
BLAKE2b-256 1ea7811eed1c002bfc0c680c4116e6e4b5d4646eeebcbd99fe9e841a01303534

See more details on using hashes here.

File details

Details for the file fast_query_parsers-1.0.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-1.0.0-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 612a58d5af661472093cf91624d322298b40dd76cc18fef86f26a39a65a57d97
MD5 07dba42c472146114ad0af4996a2f612
BLAKE2b-256 5bd33f96d98838002567dbee2d004b33fe2e08f09ff222b7bf2bd38c24fd9734

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-1.0.0-cp38-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 0e79684c696ca4db6b8a674961d6dc401f7c26472fdd11c8030c02cfc7cd8e50
MD5 2062803afaaf5235694194d2d9d8be60
BLAKE2b-256 f95ec75840f233f9e5ce0a7f5a1345642a2c161cca332b5377f5bd59911cc8f5

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