HeaderMapping

collections.abc.MutableMapping which supports duplicate, case-insentive keys.

New in version 0.4.0.

Classes:

HeaderMapping()

Provides a MutableMapping interface to a list of headers, such as those used in an email message.

class HeaderMapping[source]

Bases: MutableMapping[str, ~VT]

Provides a MutableMapping interface to a list of headers, such as those used in an email message.

MutableMapping interface, which assumes there is exactly one occurrence of the header per mapping. Some headers do in fact appear multiple times, and for those headers you must use the get_all() method to obtain all values for that key.

Methods:

__contains__(name)

Returns whether name is in the HeaderMapping.

__delitem__(name)

Delete all occurrences of a header, if present.

__getitem__(name)

Get a header value.

__iter__()

Returns an iterator over the keys in the HeaderMapping.

__len__()

Return the total number of keys, including duplicates.

__repr__()

Return a string representation of the HeaderMapping.

__setitem__(name, val)

Set the value of a header.

get(k[, default])

Get a header value.

get_all(k[, default])

Return a list of all the values for the named field.

items()

Get all the message’s header fields and values.

keys()

Return a list of all the message’s header field names.

values()

Return a list of all the message’s header values.

__contains__(name)[source]

Returns whether name is in the HeaderMapping.

Parameters

name (object)

Return type

bool

__delitem__(name)[source]

Delete all occurrences of a header, if present.

Does not raise an exception if the header is missing.

Parameters

name (str)

__getitem__(name)[source]

Get a header value.

Note

If the header appears multiple times, exactly which occurrence gets returned is undefined. Use the get_all() method to get all values matching a header field name.

Parameters

name (str)

Return type

~VT

__iter__()[source]

Returns an iterator over the keys in the HeaderMapping.

Return type

Iterator[str]

__len__()[source]

Return the total number of keys, including duplicates.

Return type

int

__repr__()[source]

Return a string representation of the HeaderMapping.

New in version 0.4.1.

Return type

str

__setitem__(name, val)[source]

Set the value of a header.

Parameters
get(k, default=None)[source]

Get a header value.

Like __getitem__(), but returns default instead of None when the field is missing.

Parameters
  • k (str)

  • default – Default None.

Overloads
get_all(k, default=None)[source]

Return a list of all the values for the named field.

These will be sorted in the order they appeared in the original message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.

If no such fields exist, default is returned.

Parameters
  • k (str)

  • default – Default None.

Overloads
items()[source]

Get all the message’s header fields and values.

These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.

Return type

List[Tuple[str, ~VT]]

keys()[source]

Return a list of all the message’s header field names.

These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.

Return type

List[str]

values()[source]

Return a list of all the message’s header values.

These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list.

Return type

List[~VT]