A set which keeps the ordering of the inserted items.
| 3 | |
| 4 | |
| 5 | class OrderedSet: |
| 6 | """ |
| 7 | A set which keeps the ordering of the inserted items. |
| 8 | """ |
| 9 | |
| 10 | def __init__(self, iterable=None): |
| 11 | self.dict = dict.fromkeys(iterable or ()) |
| 12 | |
| 13 | def add(self, item): |
| 14 | self.dict[item] = None |
| 15 | |
| 16 | def remove(self, item): |
| 17 | del self.dict[item] |
| 18 | |
| 19 | def discard(self, item): |
| 20 | try: |
| 21 | self.remove(item) |
| 22 | except KeyError: |
| 23 | pass |
| 24 | |
| 25 | def __iter__(self): |
| 26 | return iter(self.dict) |
| 27 | |
| 28 | def __reversed__(self): |
| 29 | return reversed(self.dict) |
| 30 | |
| 31 | def __contains__(self, item): |
| 32 | return item in self.dict |
| 33 | |
| 34 | def __bool__(self): |
| 35 | return bool(self.dict) |
| 36 | |
| 37 | def __len__(self): |
| 38 | return len(self.dict) |
| 39 | |
| 40 | def __repr__(self): |
| 41 | data = repr(list(self.dict)) if self.dict else "" |
| 42 | return f"{self.__class__.__qualname__}({data})" |
| 43 | |
| 44 | |
| 45 | class MultiValueDictKeyError(KeyError): |
no outgoing calls