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

Uploaded Source

Built Distributions

fast_query_parsers-0.1.0-cp38-abi3-win_amd64.whl (192.0 kB view details)

Uploaded CPython 3.8+ Windows x86-64

fast_query_parsers-0.1.0-cp38-abi3-win32.whl (189.0 kB view details)

Uploaded CPython 3.8+ Windows x86

fast_query_parsers-0.1.0-cp38-abi3-musllinux_1_2_x86_64.whl (439.7 kB view details)

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

fast_query_parsers-0.1.0-cp38-abi3-musllinux_1_2_i686.whl (464.5 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ i686

fast_query_parsers-0.1.0-cp38-abi3-musllinux_1_2_armv7l.whl (516.9 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARMv7l

fast_query_parsers-0.1.0-cp38-abi3-musllinux_1_2_aarch64.whl (433.9 kB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (268.1 kB view details)

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

fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (346.3 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ s390x

fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (352.7 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64le

fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (366.8 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64

fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (253.1 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARMv7l

fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (252.8 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (294.3 kB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.5+ i686

fast_query_parsers-0.1.0-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (486.2 kB 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.1.0-cp38-abi3-macosx_10_7_x86_64.whl (246.2 kB view details)

Uploaded CPython 3.8+ macOS 10.7+ x86-64

File details

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

File metadata

  • Download URL: fast_query_parsers-0.1.0.tar.gz
  • Upload date:
  • Size: 21.4 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.1.0.tar.gz
Algorithm Hash digest
SHA256 c611ad80e3e7ac09255c611f0e785c0e832ce7e74fac7597252f43aaf1a3290b
MD5 81da9abdd0d7a48161de05009c039dd7
BLAKE2b-256 081fd77ab975033af0d3a1bc7714f524f03d86e707ab3ce24a55e61db2eb2ed7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 02e1c9345466b83c4eadcad55a52ec7aef7c5df410a3d0fd01372ed0119aa041
MD5 c690486ae92bac64eb261c0de60b1ee0
BLAKE2b-256 b90dc6ee08f3f5d3fbd8cdc2540e77c36fa2a5b4b58b4ffb1d6eb2c67e77a124

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 b935bcf2f9d42cc49cd138d79237de6d1c16d152e9ec0cab44207f1024742bd1
MD5 05cee0c7fc40baa074c1b31a62c4ed08
BLAKE2b-256 59e7b5c5359e6c5292fac64b19b562bdfe44f51a1d57f4ba7280b8fd39720ef9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 dba36401bad33fee511ccc724ec93da45aca9da19cf3518214d9d379ea004230
MD5 8de5711aadd677559ebe40a1f987f99c
BLAKE2b-256 37d2020acc1214cb7cb4233e36dba45765078e96028a51428dfef6dfa7be7819

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 9bb176306ad20488c7d6ed1c0a601b33c692a3a5f5161bb021c70907671e7761
MD5 8e36adfe4a8f7e11fca8e8c74033d6c2
BLAKE2b-256 68da43fadcee5d159ec47df8f3987ebc7df36fed5ce82965904e7056e5673101

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 dc774b2e39161b767937f5ecf5199f3f1ae9fa4ebb15cb858a05c8176ed92049
MD5 7a2c2e110ee42ce10d49530cd9d2f48c
BLAKE2b-256 cda4e57346838ebbc252b0229d4d2d1ac5c484f87c6096a82ba79185b976af46

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 ba168d29ce2b3c5fa6bf56880aa176a8e4302c5df8b208eba873b69db64540a3
MD5 01ce9feead1d9bac47351e9f2267ebd4
BLAKE2b-256 13c0864e26f653c854b9be811b0a8d94a9c8d3780d385d5f2696cfd8fbf9210d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0eaad3c0bc84395fc7b2bf4571f4ddf355d390acc21817f0f925658dabacfff7
MD5 d6765f4cdf685857e07a46e7cd3ee244
BLAKE2b-256 575dc7c32c2c18399ea471a912e67a385f6c98b177a0b71e2cc48075d5106837

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 a6a06f7f93088a5c5e34769ecfff80702005f824bec9cfbc125d1a0a1879d74c
MD5 9050c73d7cfffdb9144d65bfb0aa1407
BLAKE2b-256 bd4040213dbe6eed6915c718c4eb7851dea21b33bcd80b77486d1a27099d2ab6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 c3452449b2ae62d0c1f3f87d44af1edbbbceef895f9151cb8f06cfd8cb0b26e9
MD5 1b8b6872e359ab175b81714803ba4df4
BLAKE2b-256 c4513e53a3b11d1b1f3ce31627ff46a40ca6b016a34af5488fd3259e77c8047f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
Algorithm Hash digest
SHA256 52471d9be336c786f65b2dc0aee3ed0b85ae0cd9641ed3996f7efe7282be6e38
MD5 ca69ed9fa77dee465e69446884c82ec4
BLAKE2b-256 d5a965b630e8fd0d426ea6d30211e231904c4d81c96f0f423dd6029d2df064e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 aff36b60f1ab232fcd2e0a64786f48c23f9b94893d7eb2b3860d439633fb6495
MD5 3b4cc29c0952a09e0fabd36a47a408f6
BLAKE2b-256 1477aab710ff334abbae79309ad0ebadd8d1c9b1cbecbaae14c08f85e4e40237

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f7052ca1658542d1aca6ed9831389bcd23dbda3bb4fef691a418c77bfe476bbf
MD5 134105a27a96a3cc3cc34288d5fd2162
BLAKE2b-256 bc46bed7f4c8f792806131c080290006be0b067fa4eda1cb31450efb340083da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 53dcfcd9f266d8d34390f97e65cdaf413b37cc3ee4734b35c0b06031ed74d886
MD5 c6ed25fe594649a9a404d4bfea6bcf0f
BLAKE2b-256 ce64d32bb9a97cb23ba99314826124d417e71bb9688779282c6433aa57241990

See more details on using hashes here.

File details

Details for the file fast_query_parsers-0.1.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.1.0-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 3926f628ccb212cbe0b44fe79cc455538280a156c9d6c585613d06ea36970a93
MD5 14123669e30bd1a181f718f0b91a7409
BLAKE2b-256 4aaf796fae597c1f1cc82dab9364052d5d2c73359bee2af85f77779db81772a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fast_query_parsers-0.1.0-cp38-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 c68bcf28b280eb3ad87dc4ad53482f3b767170b3553e9412dad4afd536b95af6
MD5 11f4a1f0380c0c6c83e36d9471a13506
BLAKE2b-256 63abc749dbc839cb69419d223d3c09be2eba3b706cbd610b43e7c56c3e44a8ea

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