icalendar.caselessdict module#
- class icalendar.caselessdict.CaselessDict(*args, **kwargs)[source]#
Bases:
OrderedDictA 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
strorbytes. They are converted to Unicode viato_unicode(), then uppercased before storage.- Parameters:
*args (
Any) – Positional arguments passed toOrderedDict.**kwargs (
Any) – Keyword arguments passed toOrderedDict.
Example
Create a new
CaselessDictand 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 adefaultvalue.- 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 dictinstead.- Parameters:
key (
Any) – The key to check, case-insensitively.- Return type:
bool- Returns:
Trueif the key exists, elseFalse.
- pop(key, default=None)[source]#
Remove and return the value for
key, ordefaultif 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, elseNone.
- 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_orderappear 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_orderappear 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. IfNone, 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_orderappear 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. IfNone, 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']