Ultra-fast query string and url-encoded form-data parsers
Project description
Fast Query Parsers
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.
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
- Run
cargo install
to setup the rust dependencies andpoetry install
to setup the python dependencies. - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file fast_query_parsers-0.4.0.tar.gz
.
File metadata
- Download URL: fast_query_parsers-0.4.0.tar.gz
- Upload date:
- Size: 24.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdeb39582a06f77c02e9a3d7a61f9ea7025c0c18b0e42d2e78a4d3cf1d13d862 |
|
MD5 | 50dcad84287cfc9c2d57cd7daf6804c0 |
|
BLAKE2b-256 | 51aa838662fb57827fefbdaf2ee63fc534bbadaeb6b98639a9a02abc94729f05 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-win_amd64.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 503.6 kB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebbdcb6d3e3936a959c73a96765a6abf02a8d214eb2dd1024bad1c83d0b7ba41 |
|
MD5 | 4a54b6d937cc03b91f55cdf7f0589bab |
|
BLAKE2b-256 | 923be8ae505fcff4734fcdf65d62c41e46db6497c3cefee3580da96ab725b600 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-win32.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-win32.whl
- Upload date:
- Size: 459.0 kB
- Tags: CPython 3.8+, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bda8b91ca9f95bf95346d93a5ea23678880b8721674713ef0bff6dad29eab480 |
|
MD5 | c20e2ad8e70339f2705050e63cc435d1 |
|
BLAKE2b-256 | b2b36f716db537f7a8ce0927e1c1ae90b9a99bfa75ab71b4ae9f7cd7c0719bfe |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-musllinux_1_2_x86_64.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 772.8 kB
- Tags: CPython 3.8+, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4f6d57b06623c840200415ed30aba14bc2de0c81189bb6609f802651b771e6a |
|
MD5 | 630edc37a054b303fe5ea0733a11f7fc |
|
BLAKE2b-256 | a7ab52616122c1efff6ae31bd6e3e000631cf86f227f2fc0393dd794a3462737 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-musllinux_1_2_i686.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-musllinux_1_2_i686.whl
- Upload date:
- Size: 762.4 kB
- Tags: CPython 3.8+, musllinux: musl 1.2+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d71a51b983c186ce7ed4b1af0292b5a937d485d6a330f02c52d95ccc81c2e1b7 |
|
MD5 | a57d83f52da0a4ee02f2f5cda09b96fd |
|
BLAKE2b-256 | ddf17da3a5e09b40620dd13d270c05bab2c77d46c1c94ba94e995f89ac8ff7a2 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-musllinux_1_2_armv7l.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-musllinux_1_2_armv7l.whl
- Upload date:
- Size: 773.8 kB
- Tags: CPython 3.8+, musllinux: musl 1.2+ ARMv7l
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2837df1c76fd7ae03ffe05ee05c75f639b80aec56617ba285deddaebb2a26691 |
|
MD5 | a43c5b7b158fcdfd8e64d4439658fa0d |
|
BLAKE2b-256 | 161498d61d7a4bc8f917eac144176073a2bb07f89cc8dcc85214035624a2c6b0 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-musllinux_1_2_aarch64.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-musllinux_1_2_aarch64.whl
- Upload date:
- Size: 727.0 kB
- Tags: CPython 3.8+, musllinux: musl 1.2+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d80f7fbb1f2036dc8d40360e6e2c297f69e00914aba7743c189595349f0a1e0a |
|
MD5 | f20513f444822a80374fbaea7be4d92b |
|
BLAKE2b-256 | 95abd8482536997048f7cc235b19a9532bd39999fda4aa7dd7ee63ed499fea56 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 601.7 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70f7abd5e82573875858d78149c13c0a4639d2e4f0b837f5cc8a21a87d6a1ac1 |
|
MD5 | c299e7274e2e35c315b6b19c891593de |
|
BLAKE2b-256 | 4ae4db733328acc40e185a3d5c2706ebccb650cc702d3b1642e0be2ddf902069 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
- Upload date:
- Size: 693.9 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ s390x
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67527991471aa2647e075643dbd71292fffcc6e4b51a588ce1892777bad40c72 |
|
MD5 | 78831b2a0d9acdbc237cb9be48c07588 |
|
BLAKE2b-256 | 72c82c2b3ab91381b376d213fede4921b7263edcd4bff7ac1fa341ac83010995 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
- Upload date:
- Size: 765.3 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ppc64le
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7ee77c1049c468e063bd8292b07aa3cc18ed1f1b3a0d771961ee977c7a721eb |
|
MD5 | 82fee7591cde99dee54273b85465cfb9 |
|
BLAKE2b-256 | ee4db95e068199fcdddba5155f47a0ce0ed15571a3c2b34be931c1b4c94b7822 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
- Upload date:
- Size: 801.4 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ppc64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59ababfa12fc08c442f6b06b09f8c0c40b642a9abba680d1da052fe86900c31a |
|
MD5 | 80d02027eed2cf9655e9774f47c737cf |
|
BLAKE2b-256 | 144184a7bf4bc1a2f2dbd1a06875f5f7e445bc2b15932fbc6e0118aaa389d7a9 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
- Upload date:
- Size: 510.5 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ARMv7l
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | acabff2ed93a893753bad5acc78c8c6151e27386648b69bcd3c694de10408b93 |
|
MD5 | e1f7769d5e300e9989053885df7eeae6 |
|
BLAKE2b-256 | 5a058e30693face0d1bc0be5ad98cf536b7ba6f426a5e55c3b1f7d86c8237882 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 545.9 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1aba46436734f31bd00fde8da6337d41203cad82f7701ad08b341b45fd79237 |
|
MD5 | f761c20a4e13039f392447d9b34944b0 |
|
BLAKE2b-256 | 4a57f47958f0005d57bd3158c44b1e24792d5bbded44e4a6c7025e61dece6e17 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
- Upload date:
- Size: 605.4 kB
- Tags: CPython 3.8+, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 505b9d3d18065f1a1dc8ab0d59b39ff2756f98d016513aec42a41733998a30b9 |
|
MD5 | 32fc6e0b9b106311cb7a07db79292586 |
|
BLAKE2b-256 | fad14a8ad669b0d44aa058851d2345f6340e7c3cf882c4b585e1b924eb9547f1 |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.8+, macOS 10.9+ universal2 (ARM64, x86-64), macOS 10.9+ x86-64, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd39fee9817f5b9ccd42c7893b778fdddb7401d8a57fb0219c83935d749e35d6 |
|
MD5 | b983942d733374d776cdd83ba9b19049 |
|
BLAKE2b-256 | 2eb4c64e34abbb779227bd49a20fece3beb4aac18fbed9e3fc330495c89cb4ce |
File details
Details for the file fast_query_parsers-0.4.0-cp38-abi3-macosx_10_7_x86_64.whl
.
File metadata
- Download URL: fast_query_parsers-0.4.0-cp38-abi3-macosx_10_7_x86_64.whl
- Upload date:
- Size: 566.7 kB
- Tags: CPython 3.8+, macOS 10.7+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d5b9a6cbba217bc3311c270030dcaacc9ce081e2343703cab3dbdf21f09fcab |
|
MD5 | 12ad4db723781cad6949880d358584b6 |
|
BLAKE2b-256 | 5dd0e4472d29a1f9e8132c2b31173d51889336cb433b1bde35242a035dc7b272 |