1 | from typing import Dict, TypeVar, Optional, Type, Generic, Any, Set, Collection,\
|
---|
2 | List, Iterable
|
---|
3 |
|
---|
4 | K=TypeVar('K')
|
---|
5 | V=TypeVar('V')
|
---|
6 |
|
---|
7 | class Keys(Generic[K,V], Set[K]):
|
---|
8 | '''
|
---|
9 | Keys is a Set, containing the keys of the given dict.
|
---|
10 | Actions on Keys are reflected directly into the dict.
|
---|
11 |
|
---|
12 | An alternative to the python dict.keys() function.
|
---|
13 | The dict.keys() function returns a dict_keys object
|
---|
14 | that really is a second class citizen because it offers a very
|
---|
15 | small subset of the functionality of set. Eg no "pop" method.
|
---|
16 |
|
---|
17 | See also collections ABC Set
|
---|
18 | '''
|
---|
19 | def __init__(self, d:Dict[K,V]):
|
---|
20 | self._d:Dict[K,V]=d
|
---|
21 |
|
---|
22 | def pop(self, key:K, default:Any=None)->Any:
|
---|
23 | '''
|
---|
24 | Remove given key from the dict
|
---|
25 | @param key the key to pop
|
---|
26 | @param default the value to return if key is not present.
|
---|
27 | @return the value for the given key.
|
---|
28 | If the key is not in dict, returns the default value.
|
---|
29 |
|
---|
30 | override is bit poor, because original is not properly typed...
|
---|
31 | '''
|
---|
32 | return self._d.pop(key, default)
|
---|
33 |
|
---|
34 | def __contains__(self, key:Any):
|
---|
35 | return key in self._d
|
---|
36 |
|
---|
37 | def __len__(self):
|
---|
38 | return len(self._d)
|
---|
39 |
|
---|
40 | def __delitem__(self, key:K):
|
---|
41 | '''
|
---|
42 | remove key from dict. May thrkw key error. See also pop
|
---|
43 | '''
|
---|
44 | del self._d[key]
|
---|
45 |
|
---|
46 | def __getitem__(self, key:K)->V:
|
---|
47 | '''
|
---|
48 | raises keyerror if item not there. See also get
|
---|
49 | '''
|
---|
50 | return self._d[key]
|
---|
51 |
|
---|
52 | def get(self, key:K, default:Optional[Type]=None)->Any:
|
---|
53 | '''
|
---|
54 | @param key the key to look up
|
---|
55 | @param default the default value for return if key not present, defaults to None
|
---|
56 | @return value for given key, or default if key is not in dict
|
---|
57 | '''
|
---|
58 | return self._d.get(key, default)
|
---|
59 |
|
---|
60 |
|
---|
61 | def __hash__(self):
|
---|
62 | return hash(frozenset(self._d.keys()))
|
---|
63 |
|
---|
64 | def __repr__(self):
|
---|
65 | return repr(set(self._d))
|
---|
66 |
|
---|
67 | def __iter__(self):
|
---|
68 | return iter(self._d.keys())
|
---|
69 |
|
---|
70 | def __eq__(self, other):
|
---|
71 | return self._d.keys().__eq__(other)
|
---|
72 |
|
---|
73 | def add(self, key:K):
|
---|
74 | raise NotImplemented # NotSupported would be better
|
---|
75 |
|
---|
76 | def difference_update(self, *items: Iterable[Any])->None:
|
---|
77 | for lst in items:
|
---|
78 | for key in lst:
|
---|
79 | self._d.pop(key)
|
---|
80 |
|
---|
81 | # TODO many more may be implemented later
|
---|
82 |
|
---|
83 | def remove(self, key:K):
|
---|
84 | del self._d[key]
|
---|
85 |
|
---|
86 | |
---|