| 4 | import bisect |
| 5 | |
| 6 | class 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 | |
| 21 | if __name__ == '__main__': |
| 22 | items = SortedItems([5, 1, 3]) |