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

Uploaded Source

Built Distributions

fast_query_parsers-0.3.0-cp38-abi3-win_amd64.whl (505.5 kB view details)

Uploaded CPython 3.8+ Windows x86-64

fast_query_parsers-0.3.0-cp38-abi3-win32.whl (458.3 kB view details)

Uploaded CPython 3.8+ Windows x86

fast_query_parsers-0.3.0-cp38-abi3-musllinux_1_2_x86_64.whl (770.7 kB view details)

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

fast_query_parsers-0.3.0-cp38-abi3-musllinux_1_2_i686.whl (758.2 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ i686

fast_query_parsers-0.3.0-cp38-abi3-musllinux_1_2_armv7l.whl (772.4 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARMv7l

fast_query_parsers-0.3.0-cp38-abi3-musllinux_1_2_aarch64.whl (726.6 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (598.8 kB view details)

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

fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (684.6 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ s390x

fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (764.2 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64le

fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (798.7 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64

fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (509.0 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARMv7l

fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (545.7 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (602.6 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.5+ i686

fast_query_parsers-0.3.0-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.1 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-0.3.0-cp38-abi3-macosx_10_7_x86_64.whl (541.7 kB view details)

Uploaded CPython 3.8+ macOS 10.7+ x86-64

File details

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

File metadata

  • Download URL: fast_query_parsers-0.3.0.tar.gz
  • Upload date:
  • Size: 23.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-0.3.0.tar.gz
Algorithm Hash digest
SHA256 df972c0b58d0bf51fa43b67d2604ab795984015d47552d02175ebcc685e4852b
MD5 6bb31ba484df790356581da1c7e3c992
BLAKE2b-256 1cc4767a5ee3ca35134dd4947a8619603058bc844862cfb49f0f58ae7f59209f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4e9dd270f085a2337fab407d230a252664f952561cb83acece343a5c6a7d8e6b
MD5 dff38b15e79d4ddb60d7f59c8e4090a7
BLAKE2b-256 f671a31a0094393c3e3111ed20d839016da202a802c0669782097415e5d88845

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 89ee582ab4b331f078eca2fbf51ce82396f749879fc30f20668b85a2896ea165
MD5 536ae39de3326fd1230238c9865b811c
BLAKE2b-256 7166423896b351e51cf998d778446b0a900df1c664a5799303a62c3ea77f17a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 47639fc331e668b1be405c4e24af70ad21fc01a6847d2730803346ab9586e788
MD5 cbdf6eefaba77c3113c22051f77e0e95
BLAKE2b-256 2cfd3de5091d01ff47511e4ec67f111659a933f5762e1a6ee5574e20b466e0cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 0395ef9cacf49318a0f3caac4fde8a01dd7b7315c14f8a7d084965dedd764868
MD5 882f8aca92819fe2fa640b450738af07
BLAKE2b-256 a7c47bdfa38ec1aaabeded6ec3e6e43e13037b72604c12db4fac3ec2e1106dbb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 e9b277e8281d7fe9b8e9450767cf1c1158ed729d81e03c1dfa2fe2118b22ffe0
MD5 4a401b28b1654feaa9439bdf2caa19a2
BLAKE2b-256 da587b1b982830eca1283fa134b5799e3398a498b501e8546d53357b669b0806

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 a659eb2acc0aa44f4cce7a79c4e0c76f14a26026fc13db8f094a29a38cf66248
MD5 bc13bedfc2ce81552163ccb0a24ec31c
BLAKE2b-256 0ab62603242180d17fd24bb2c000c0f69dec89e5ddcb616ba2ea5675aedab46f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 befa91ff707f77e9948759bafdfe2b743d4b587e3f7767f2b5887b1ffa37f41d
MD5 81c69bd608738bc9374bdbfd41338875
BLAKE2b-256 d55ed2e404b477e571742e01e8f8a4ba0923b817074bcc8bb2ab736fbe706876

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 97d13dcc6cad29c8ef7c6f4bb6240847b476e4eb0445ac7f59d49a11db1644e2
MD5 dd4a0a85c025ed82f1717b6057b5f83a
BLAKE2b-256 f4906df39babc790fdfffd713a937cce40a92418288678048ebdb8798fa75ad1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 189eed1007229c1e4a032076d503e92869eebd4a1ebd2498e6bb1b1b0c525708
MD5 2d7d3879b19ba5ad1c40aa081b93bb8d
BLAKE2b-256 c652969ec37a7b61650a67c7b8b7dc08837eea2a937546d43d2335a48d0e9f13

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
Algorithm Hash digest
SHA256 52ef0241bcadc93ef47f650070c5f31f616cd5185a3af95f8b365e1fb135d991
MD5 6e68c6ea38ebf9ac46898edad055cd5d
BLAKE2b-256 afb2ca00ae22dfdcb71905a6cd3127d9d4d91e4e7709937212e54cd23d95950b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 dd94f7715b6c1ec57b50e63c22298253648d864c9e78f90a258ebf2c07d13a38
MD5 2b4661850c1867e48a96142809009a3b
BLAKE2b-256 af695cb96c0b5eb436ad68a4164c5bd25b5bedddbb18b9365d7c5fa88dd3ded3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1becd3a0a06b14c57ee34976bf54d08fc910d4b410170ee5c28ac8d16ef23575
MD5 86ddbcb88c541fa7ed79ba2e7ebb0779
BLAKE2b-256 fd8911758156f8d0e2fe9287ba6baa3bc19efd6237d99b5b80adb2ab9588e1c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 00d3d519cfd020c7a3bd961c9568b6042f853f25e204b8a307ff5b569dee9c38
MD5 51d2b8a463d51586a196e66750dfbc33
BLAKE2b-256 165346c138b01ae7facae47e4057a83d5b03d95f476dac4192288f093a9dad92

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.3.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.3.0-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 f2ffa7f1298db8ff8e013733e32edb77509a468843f5641875386f1df9bdecb4
MD5 16dfa6500c8ac32ef6044b652cd7a1c6
BLAKE2b-256 35d156bc1096fcf9f490b341a65f9b58787b56ccf206b3cc747d28e3486d7e59

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.3.0-cp38-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 e01e9b794d6fad11207341a8e0a7b179f085c9874b3981a77f862a5e44cb241d
MD5 fda75d99ca5ac706c5d5624c3ee16f10
BLAKE2b-256 072344866f602a1211a4977152dcb3fd12054af11eb4e30cf97f32d13deb94ea

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