diff --git a/sat/tools/common/utils.py b/sat/tools/common/utils.py index a7e4054a..aa7a2dbe 100644 --- a/sat/tools/common/utils.py +++ b/sat/tools/common/utils.py @@ -44,3 +44,32 @@ def recursive_update(ori: dict, update: dict): ori[k] = v return ori +class OrderedSet(collections.abc.MutableSet): + """A mutable sequence which doesn't keep duplicates""" + # TODO: complete missing set methods + + def __init__(self, values=None): + self._dict = {} + if values is not None: + self.update(values) + + def __len__(self): + return len(self._dict) + + def __iter__(self): + return iter(self._dict.keys()) + + def __contains__(self, item): + return item in self._dict + + def add(self, item): + self._dict[item] = None + + def discard(self, item): + try: + del self._dict[item] + except KeyError: + pass + + def update(self, items): + self._dict.update({i: None for i in items})