Utilities
- class headerparser.NormalizedDict(data: None | Mapping | Iterable[tuple[Any, Any]] = None, normalizer: Callable[[Any], Any] | None = None, body: str | None = None)[source]
A generalization of a case-insensitive dictionary.
NormalizedDicttakes a callable (the “normalizer”) that is applied to any key passed to its__getitem__,__setitem__, or__delitem__method, and the result of the call is then used for the actual lookup. When iterating over aNormalizedDict, each key is returned as the “pre-normalized” form passed to__setitem__the last time the key was set (but seenormalized()below). Aside from this,NormalizedDictbehaves like a normalMutableMappingclass.If a normalizer is not specified upon instantiation, a default will be used that converts strings to lowercase and leaves everything else unchanged, so
NormalizedDictdefaults to yet another case-insensitive dictionary.Two
NormalizedDictinstances compare equal iff their normalizers, bodies, andnormalized_dict()return values are equal. When comparing aNormalizedDictto any other type of mapping, the other mapping is first converted to aNormalizedDictusing the same normalizer.- Parameters:
data (mapping) – a mapping or iterable of
(key, value)pairs with which to initialize the instancenormalizer (callable) – A callable to apply to keys before looking them up; defaults to
lower. The callable MUST be idempotent (i.e.,normalizer(x)must equalnormalizer(normalizer(x))for all inputs) or else bad things will happen to your dictionary.body (string or
None) – initial value for thebodyattribute
- body: str | None
This is where
HeaderParserstores the message body (if any) accompanying the header section represented by the mapping
- copy() NormalizedDict[source]
Create a shallow copy of the mapping
- normalized() NormalizedDict[source]
Return a copy of the instance such that iterating over it will return normalized keys instead of the keys passed to
__setitem__>>> normdict = NormalizedDict() >>> normdict['Foo'] = 23 >>> normdict['bar'] = 42 >>> sorted(normdict) ['Foo', 'bar'] >>> sorted(normdict.normalized()) ['bar', 'foo']
- Return type:
- headerparser.BOOL(s: str) bool[source]
Convert boolean-like strings to
boolvalues. The strings'yes','y','on','true', and'1'are converted toTrue, and the strings'no','n','off','false', and'0'are converted toFalse. The conversion is case-insensitive and ignores leading & trailing whitespace. Any value that cannot be converted to aboolresults in aValueError.- Parameters:
s (string) – a boolean-like string to convert to a
bool- Return type:
- Raises:
ValueError – if
sis not one of the values listed above
- headerparser.lower(s: Any) Any[source]
New in version 0.2.0.
Convert
sto lowercase by calling itslower()method if it has one; otherwise, returnsunchanged
- headerparser.unfold(s: str) str[source]
New in version 0.2.0.
Remove folding whitespace from a string by converting line breaks (and any whitespace adjacent to line breaks) to a single space and removing leading & trailing whitespace.
>>> unfold('This is a \n folded string.\n') 'This is a folded string.'
- Parameters:
s (string) – a string to unfold
- Return type:
string