MCPcopy Create free account
hub / github.com/bslatkin/effectivepython / SortedDict

Class SortedDict

example_code/item_025_example_17.py:35–55  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

33from typing import Iterator, MutableMapping
34
35class SortedDict(MutableMapping[str, int]):
36 def __init__(self) -> None:
37 self.data: Dict[str, int] = {}
38
39 def __getitem__(self, key: str) -> int:
40 return self.data[key]
41
42 def __setitem__(self, key: str, value: int) -> None:
43 self.data[key] = value
44
45 def __delitem__(self, key: str) -> None:
46 del self.data[key]
47
48 def __iter__(self) -> Iterator[str]:
49 keys = list(self.data.keys())
50 keys.sort()
51 for key in keys:
52 yield key
53
54 def __len__(self) -> int:
55 return len(self.data)
56
57
58votes = {

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected