File: KeyCaseInsensitiveDict.py

package info (click to toggle)
sparql-wrapper-python 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,228 kB
  • sloc: python: 14,201; makefile: 30
file content (46 lines) | stat: -rw-r--r-- 1,377 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-

"""
A simple implementation of a key case-insensitive dictionary.
..
  Developers involved:
  * Ivan Herman <http://www.ivan-herman.net>
  * Sergio Fernández <http://www.wikier.org>
  * Carlos Tejo Alonso <http://www.dayures.net>
  * Alexey Zakhlestin <https://indeyets.ru/>
  Organizations involved:
  * `World Wide Web Consortium <http://www.w3.org>`_
  * `Foundation CTIC <http://www.fundacionctic.org/>`_
  :license: `W3C® Software notice and license <http://www.w3.org/Consortium/Legal/copyright-software>`_
"""

from typing import Dict, Mapping, TypeVar

_V = TypeVar("_V")

class KeyCaseInsensitiveDict(Dict[str, _V]):
    """
    A simple implementation of a key case-insensitive dictionary
    """

    def __init__(self, d: Mapping[str, _V]={}) -> None:
        """
        :param dict d: The source dictionary.
        """
        for k, v in d.items():
            self[k] = v

    def __setitem__(self, key: str, value: _V) -> None:
        if hasattr(key, "lower"):
            key = key.lower()
        dict.__setitem__(self, key, value)

    def __getitem__(self, key: str) -> _V:
        if hasattr(key, "lower"):
            key = key.lower()
        return dict.__getitem__(self, key)

    def __delitem__(self, key: str) -> None:
        if hasattr(key, "lower"):
            key = key.lower()
        dict.__delitem__(self, key)