Ordered query parameters. Iterating gives the pairs; repr hides
the values of CurrentRedactedNames and of Redacted values.
Attributes#
| Attribute | Type | Description |
|---|---|---|
params | tuple[Param, ...] = tuple(params) |
Functions#
__init__#
__init__(params=()) -> None| Parameter | Type | Description |
|---|---|---|
params | Iterable[Param] = () |
def __init__(self, params: Iterable[Param] = ()) -> None:
self.params: tuple[Param, ...] = tuple(params)get_all#
get_all(key) -> list[str]| Parameter | Type | Description |
|---|---|---|
key | str |
Returns list[str]
def get_all(self, key: str) -> list[str]:
return [_unwrap(v) for k, v in self.params if k == key]get_first#
get_first(key) -> str | None| Parameter | Type | Description |
|---|---|---|
key | str |
Returns str | None
def get_first(self, key: str) -> str | None:
return next((_unwrap(v) for k, v in self.params if k == key), None)get_last#
get_last(key) -> str | None| Parameter | Type | Description |
|---|---|---|
key | str |
Returns str | None
def get_last(self, key: str) -> str | None:
return next((_unwrap(v) for k, v in reversed(self.params) if k == key), None)set#
set(key, value) -> UrlParamsReplace every pair named key with one, at the end.
| Parameter | Type | Description |
|---|---|---|
key | str | |
value | Coercible |
Returns UrlParams
def set(self, key: str, value: Coercible) -> UrlParams:
"""Replace every pair named ``key`` with one, at the end."""
return UrlParams((*(p for p in self.params if p[0] != key), (key, _coerce(value))))transform#
transform(f) -> UrlParams| Parameter | Type | Description |
|---|---|---|
f | Callable[[tuple[Param, ...]], Iterable[Param]] |
Returns UrlParams
def transform(self, f: Callable[[tuple[Param, ...]], Iterable[Param]]) -> UrlParams:
return UrlParams(f(self.params))set_all#
set_all(input) -> UrlParamsThe pairs of input, then the pairs of self whose names
input does not set.
| Parameter | Type | Description |
|---|---|---|
input | Input |
Returns UrlParams
def set_all(self, input: Input) -> UrlParams:
"""The pairs of ``input``, then the pairs of ``self`` whose names
``input`` does not set."""
params = from_input(input).params
keys = {k for k, _ in params}
return UrlParams((*params, *(p for p in self.params if p[0] not in keys)))append#
append(key, value) -> UrlParams| Parameter | Type | Description |
|---|---|---|
key | str | |
value | Coercible |
Returns UrlParams
def append(self, key: str, value: Coercible) -> UrlParams:
return UrlParams((*self.params, (key, _coerce(value))))append_all#
append_all(input) -> UrlParams| Parameter | Type | Description |
|---|---|---|
input | Input |
Returns UrlParams
def append_all(self, input: Input) -> UrlParams:
return UrlParams((*self.params, *from_input(input).params))remove#
remove(key) -> UrlParams| Parameter | Type | Description |
|---|---|---|
key | str |
Returns UrlParams
def remove(self, key: str) -> UrlParams:
return UrlParams(p for p in self.params if p[0] != key)to_record#
to_record() -> dict[str, str | list[str]]Each name to its value, or to its values when repeated.
Returns dict[str, str | list[str]]
def to_record(self) -> dict[str, str | list[str]]:
"""Each name to its value, or to its values when repeated."""
out: dict[str, str | list[str]] = {}
for key, raw in self.params:
value = _unwrap(raw)
current = out.get(key)
if current is None:
out[key] = value
elif isinstance(current, str):
out[key] = [current, value]
else:
current.append(value)
return outredact#
redact(key) -> list[tuple[str, Value]]The pairs, with plain values of the names matching key hidden
in Redacted.
| Parameter | Type | Description |
|---|---|---|
key | Pattern | Sequence[Pattern] |
Returns list[tuple[str, Value]]
def redact(self, key: Pattern | Sequence[Pattern]) -> list[tuple[str, Value]]:
"""The pairs, with plain values of the names matching ``key`` hidden
in ``Redacted``."""
patterns: Sequence[Pattern] = (key,) if isinstance(key, (str, re.Pattern)) else key
return [
(
name,
Redacted(value)
if isinstance(value, str) and is_redacted_name(name, patterns)
else value,
)
for name, value in self.params
]to_json#
to_json() -> dict[str, object]Returns dict[str, object]
def to_json(self) -> dict[str, object]:
return {
"_id": "UrlParams",
"params": {k: str(v) for k, v in self.redact(current_redacted_names())},
}