MCPcopy Create free account
hub / github.com/dabeaz/python-cookbook / SortedItems

Class SortedItems

src/8/implementing_custom_containers/example1.py:6–19  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import bisect
5
6class SortedItems(collections.Sequence):
7 def __init__(self, initial=None):
8 self._items = sorted(initial) if initial is not None else []
9
10 # Required sequence methods
11 def __getitem__(self, index):
12 return self._items[index]
13
14 def __len__(self):
15 return len(self._items)
16
17 # Method for adding an item in the right location
18 def add(self, item):
19 bisect.insort(self._items, item)
20
21if __name__ == '__main__':
22 items = SortedItems([5, 1, 3])

Callers 1

example1.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected