Utilities¶
-
class
headerparser.
NormalizedDict
(data=None, normalizer=None, body=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.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.If not specified, the normalizer defaults to
operator.methodcaller('lower')
, soNormalizedDict
defaults to yet another case-insensitive dictionary. (Note that this default assumes that all non-normalized keys are strings, which is true whenHeaderParser
is populating the mapping but need not be true when you are using this class for your own purposes.)Parameters: - data (mapping) – a mapping or iterable of
(key, value)
pairs with which to initialize the instance - normalizer (callable) – A callable to apply to keys before looking them
up; defaults to
operator.methodcaller('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
= None¶ This is where
HeaderParser
stores the message body (if any) accompanying the header section represented by the mapping
-
normalized
()[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: NormalizedDict
- data (mapping) – a mapping or iterable of
-
headerparser.
BOOL
(s)[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: bool Raises: ValueError – if s
is not one of the values listed above
-
headerparser.
lower
(s)¶ Alias for
operator.methodcaller('lower')