summaryrefslogtreecommitdiff
path: root/alot/utils/collections.py
blob: acde56ce87653f81830d186ff39613bb1af4c2f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# This file is released under the GNU GPL, version 3 or a later revision.
# For further details see the COPYING file

from collections.abc import Set

# for backward compatibility with Python <3.7
from collections import OrderedDict


class OrderedSet(Set):
    """
    Ordered collection of distinct hashable objects.
    Taken from https://stackoverflow.com/a/10006674
    """

    def __init__(self, iterable=()):
        self.d = OrderedDict.fromkeys(iterable)

    def __len__(self):
        return len(self.d)

    def __contains__(self, element):
        return element in self.d

    def __iter__(self):
        return iter(self.d)

    def __repr__(self):
        return str(list(self))