Query String Multimap Parser

Parse repeated query parameters into a multimap, rebuild a stable query string, and read typed single-value flags safely.

PythonData Integrity

Code

pythonsnippet
61 lines
from collections import defaultdict
from collections.abc import Iterable
from urllib.parse import parse_qsl, urlencode

Params = dict[str, list[str]]


def parse_multimap(query: str) -> Params:
    grouped: dict[str, list[str]] = defaultdict(list)
    for key, value in parse_qsl(query, keep_blank_values=True):
        grouped[key].append(value)
    return dict(grouped)


def build_multimap(params: dict[str, Iterable[str]], *, sort_keys: bool = True) -> str:
    pairs: list[tuple[str, str]] = []
    keys = sorted(params) if sort_keys else list(params)
    for key in keys:
        for value in params[key]:
            pairs.append((key, value))
    return urlencode(pairs)


def get_one(params: Params, key: str, default: str | None = None) -> str | None:
    values = params.get(key)
    if not values:
        return default
    if len(values) > 1:
        raise ValueError(f"{key} must not repeat")
    return values[0]


def get_bool(params: Params, key: str, default: bool = False) -> bool:
    value = get_one(params, key)
    if value is None:
        return default
    normalized = value.strip().lower()
    if normalized in {"1", "true", "yes", "on"}:
        return True
    if normalized in {"0", "false", "no", "off"}:
        return False
    raise ValueError(f"{key} is not a boolean")


def main() -> None:
    query = "tag=python&tag=go&page=2&debug=1&empty="
    params = parse_multimap(query)
    print(params)
    print("tags:", params["tag"])
    print("page:", get_one(params, "page"))
    print("debug:", get_bool(params, "debug"))
    print("empty:", repr(get_one(params, "empty")))
    print(build_multimap(params))
    try:
        get_one({"tag": ["a", "b"]}, "tag")
    except ValueError as error:
        print("rejected:", error)


if __name__ == "__main__":
    main()

What the code does

Query String Multimap Parser comes down to the functions parse_multimap, build_multimap, get_one and get_bool.

Repetition is confined to for key, value in parse_qsl(query, keep_blank_values=True) — the rest of the file is straight-line. From urllib it pulls defaultdict, Iterable, parse_qsl, urlencode — the imports name exactly which library surface the snippet touches. Invalid states are rejected up front: it throws ValueError instead of continuing with bad data.

What to watch for

  • urllib must be installed first — the snippet imports it directly.
  • The guard len(values) > 1 short-circuits the flow with an early raise when its condition holds.

Using it

The primary signature is def parse_multimap(query: str) -> Params

The demo output is labeled (tags:, page:, debug:), so every printed line maps back to its originating operation.

There are 4 calls inside main — parse_multimap, get_one, get_bool and build_multimap — executed top to bottom.

Key statements

def get_one(params: Params, key: str, default: str | None = None) -> str | None:
    values = params.get(key)
    if not values:
        return default
    if len(values) > 1:
        raise ValueError(f"{key} must not repeat")
    return values[0]
def build_multimap(params: dict[str, Iterable[str]], *, sort_keys: bool = True) -> str:
    pairs: list[tuple[str, str]] = []
    keys = sorted(params) if sort_keys else list(params)
    for key in keys:
        for value in params[key]:
            pairs.append((key, value))
    return urlencode(pairs)
def parse_multimap(query: str) -> Params:
    grouped: dict[str, list[str]] = defaultdict(list)
    for key, value in parse_qsl(query, keep_blank_values=True):
        grouped[key].append(value)
    return dict(grouped)

Interactive Tools for This Topic

FAQ

What happens if the preconditions do not hold?
Validation comes first — `ValueError` marks the failure at `len(values) > 1`, before any partial work happens.
Do I need to install anything before running this?
Yes: `urllib` must be present in the project or the import fails at load time.