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.
NormalizedDict
takes 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,NormalizedDict
behaves like a normalMutableMapping
class.If a normalizer is not specified upon instantiation, a default will be used that converts strings to lowercase and leaves everything else unchanged, so
NormalizedDict
defaults to yet another case-insensitive dictionary.Two
NormalizedDict
instances compare equal iff their normalizers, bodies, andnormalized_dict()
return values are equal. When comparing aNormalizedDict
to any other type of mapping, the other mapping is first converted to aNormalizedDict
using 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 thebody
attribute
- body: str | None
This is where
HeaderParser
stores 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
bool
values. 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 abool
results in aValueError
.- Parameters:
s (string) – a boolean-like string to convert to a
bool
- Return type:
- Raises:
ValueError – if
s
is not one of the values listed above
- headerparser.lower(s: Any) Any [source]
New in version 0.2.0.
Convert
s
to lowercase by calling itslower()
method if it has one; otherwise, returns
unchanged
- 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