Kkit.fundict

 1class AmbiguityError(Exception):
 2    """
 3    Exception raised for ambiguity error.
 4    """
 5    pass
 6
 7class NoneDict(dict):
 8    """
 9    A dict that returns None for missing keys.
10
11    Example:
12
13    ```python
14    from Kkit import fundict
15    a = fundict.NoneDict({"a":1, "b":2})
16    a["c"] # None
17    ```
18    """
19    def __init__(self, *args, **kwargs):
20        super(NoneDict, self).__init__(*args, **kwargs)
21        for key, value in self.items():
22            if isinstance(value, dict):
23                self[key] = NoneDict(value)
24
25    def __getitem__(self, key):
26        return super(NoneDict, self).get(key, None)
27
28
29class AbbrDict(dict):
30    """
31    A dict that returns the value of the key that starts with the input key.
32
33    Example:
34
35    ```python
36    from Kkit import fundict
37    a = fundict.AbbrDict({"abxx":1, "acxx":2, "adxx":{"bcxx": 10, "bdxx":20}})
38    a["a"]        # raise AmbiguityError
39    a["ab"]       # 1
40    a["ac"]       # 2
41    a["ad"]["bc"] # 10
42    a["ad"]["bd"] # 20
43    a["c"]        # raise KeyError
44    ```
45    """
46    def __init__(self, *args, **kwargs):
47        super(AbbrDict, self).__init__(*args, **kwargs)
48        for key, value in self.items():
49            if isinstance(value, dict):
50                self[key] = AbbrDict(value)
51        self.__abbrdict_keys = list(super(AbbrDict, self).keys())
52        for i in self.__abbrdict_keys:
53            if isinstance(i, str)==False:
54                raise TypeError("all keys of AbbrDict must be str")
55
56    def __getitem__(self, key):
57        keys = list(super(AbbrDict, self).keys())
58        maybe = list(filter(lambda string: string.startswith(key), keys))
59        if len(maybe)==0:
60            raise KeyError("can't find key %s"%key)
61        elif len(maybe)==1:
62            # res = AbbrDict_content(maybe[0], key, super(AbbrDict, self).get(maybe[0], None))
63            return super(AbbrDict, self).get(maybe[0], None)
64        else:
65            error_str = "get ambiguity with %s: "%key+" ".join(maybe)
66            raise AmbiguityError(error_str)
class AmbiguityError(builtins.Exception):
2class AmbiguityError(Exception):
3    """
4    Exception raised for ambiguity error.
5    """
6    pass

Exception raised for ambiguity error.

class NoneDict(builtins.dict):
 8class NoneDict(dict):
 9    """
10    A dict that returns None for missing keys.
11
12    Example:
13
14    ```python
15    from Kkit import fundict
16    a = fundict.NoneDict({"a":1, "b":2})
17    a["c"] # None
18    ```
19    """
20    def __init__(self, *args, **kwargs):
21        super(NoneDict, self).__init__(*args, **kwargs)
22        for key, value in self.items():
23            if isinstance(value, dict):
24                self[key] = NoneDict(value)
25
26    def __getitem__(self, key):
27        return super(NoneDict, self).get(key, None)

A dict that returns None for missing keys.

Example:

from Kkit import fundict
a = fundict.NoneDict({"a":1, "b":2})
a["c"] # None
class AbbrDict(builtins.dict):
30class AbbrDict(dict):
31    """
32    A dict that returns the value of the key that starts with the input key.
33
34    Example:
35
36    ```python
37    from Kkit import fundict
38    a = fundict.AbbrDict({"abxx":1, "acxx":2, "adxx":{"bcxx": 10, "bdxx":20}})
39    a["a"]        # raise AmbiguityError
40    a["ab"]       # 1
41    a["ac"]       # 2
42    a["ad"]["bc"] # 10
43    a["ad"]["bd"] # 20
44    a["c"]        # raise KeyError
45    ```
46    """
47    def __init__(self, *args, **kwargs):
48        super(AbbrDict, self).__init__(*args, **kwargs)
49        for key, value in self.items():
50            if isinstance(value, dict):
51                self[key] = AbbrDict(value)
52        self.__abbrdict_keys = list(super(AbbrDict, self).keys())
53        for i in self.__abbrdict_keys:
54            if isinstance(i, str)==False:
55                raise TypeError("all keys of AbbrDict must be str")
56
57    def __getitem__(self, key):
58        keys = list(super(AbbrDict, self).keys())
59        maybe = list(filter(lambda string: string.startswith(key), keys))
60        if len(maybe)==0:
61            raise KeyError("can't find key %s"%key)
62        elif len(maybe)==1:
63            # res = AbbrDict_content(maybe[0], key, super(AbbrDict, self).get(maybe[0], None))
64            return super(AbbrDict, self).get(maybe[0], None)
65        else:
66            error_str = "get ambiguity with %s: "%key+" ".join(maybe)
67            raise AmbiguityError(error_str)

A dict that returns the value of the key that starts with the input key.

Example:

from Kkit import fundict
a = fundict.AbbrDict({"abxx":1, "acxx":2, "adxx":{"bcxx": 10, "bdxx":20}})
a["a"]        # raise AmbiguityError
a["ab"]       # 1
a["ac"]       # 2
a["ad"]["bc"] # 10
a["ad"]["bd"] # 20
a["c"]        # raise KeyError