icalendar.caselessdict module#

class icalendar.caselessdict.CaselessDict(*args, **kwargs)[source]#

Bases: OrderedDict

A case-insensitive dictionary that uses strings as keys.

All keys are stored in uppercase internally, but values retain their original case. Keys can be provided as str or bytes. They are converted to Unicode via to_unicode(), then uppercased before storage.

Parameters:

Example

Create a new CaselessDict and normalize existing keys to uppercase.

>>> from icalendar.caselessdict import CaselessDict
>>> d = CaselessDict(summary="Meeting")
>>> d["SUMMARY"]
'Meeting'
>>> "summary" in d
True
canonical_order = None#
copy()[source]#

Return a shallow copy of the dictionary.

Return type:

Self

Returns:

A new instance of the same type with the same contents.

get(key, default=None)[source]#

Return the key, optionally with a default value.

Parameters:
  • key (Any) – The key to look up, case-insensitively.

  • default (Any) – The value to return if the key is not found.

Return type:

Any

Returns:

The value for the key, if present, else the value specified by default.

has_key(key)[source]#

Check whether a key exists, case-insensitively.

This is a legacy method. Use key in dict instead.

Parameters:

key (Any) – The key to check, case-insensitively.

Return type:

bool

Returns:

True if the key exists, else False.

pop(key, default=None)[source]#

Remove and return the value for key, or default if not found.

Parameters:
  • key (Any) – The key to remove, case-insensitively.

  • default (Any) – The value to return if the key is not found.

Return type:

Any

Returns:

The removed value, or the value of default.

popitem()[source]#

Remove and return the last inserted (key, value) pair.

Return type:

tuple[Any, Any]

Returns:

A (key, value) tuple.

Raises:

KeyError – If the dictionary is empty.

setdefault(key, value=None)[source]#

Create the (key, value) pair, optionally with a value.

Once set, to change default value use update().

Parameters:
  • key (Any) – The key to look up or create, case-insensitively.

  • value (Any) – The default value to set, if given, else None.

Return type:

Any

Returns:

The value for the key.

sorted_items()[source]#

Sort items according to the canonical order for this class.

Items whose keys are listed in canonical_order appear first in that order. Remaining items appear alphabetically by key.

Return type:

list[tuple[Any, Any]]

Returns:

A sorted list of (key, value) tuples.

sorted_keys()[source]#

Sort keys according to the canonical order for this class.

Keys listed in canonical_order appear first in that order. Remaining keys appear alphabetically at the end.

Return type:

list[str]

Returns:

A sorted list of keys.

update(*args, **kwargs)[source]#

Update the dictionary with (key, value) pairs, normalizing keys to uppercase.

Multiple keys that differ only in case will overwrite each other. Only the last value is retained.

Parameters:
  • *args (Any) – Mappings or iterables of (key, value) pairs.

  • **kwargs (Any) – Additional (key, value) pairs.

Return type:

None

icalendar.caselessdict.canonsort_items(dict1, canonical_order=None)[source]#

Sort items from a mapping according to a canonical key order.

Parameters:
  • dict1 (Mapping[TypeVar(KT), TypeVar(VT)]) – The mapping whose items to sort.

  • canonical_order (Iterable[TypeVar(KT)] | None) – The preferred order for leading keys. If None, all keys are sorted alphabetically.

Return type:

list[tuple[TypeVar(KT), TypeVar(VT)]]

Returns:

A list of (key, value) tuples sorted by canonical order.

Example

>>> from icalendar.caselessdict import canonsort_items
>>> canonsort_items({"C": 3, "A": 1, "B": 2}, ["B", "C"])
[('B', 2), ('C', 3), ('A', 1)]
icalendar.caselessdict.canonsort_keys(keys, canonical_order=None)[source]#

Sort leading keys according to a canonical order.

Keys specified in canonical_order appear first in that order. Remaining keys appear alphabetically at the end.

Parameters:
  • keys (Iterable[TypeVar(KT)]) – The keys to sort.

  • canonical_order (Iterable[TypeVar(KT)] | None) – The preferred order for leading keys. Keys not in this sequence are sorted alphabetically after the canonical ones. If None, all keys are sorted alphabetically.

Return type:

list[TypeVar(KT)]

Returns:

A new list of keys sorted by canonical order first, then alphabetically.

Example

>>> from icalendar.caselessdict import canonsort_keys
>>> canonsort_keys(["C", "A", "B"], ["B", "C"])
['B', 'C', 'A']