def clean(self):
        """
        Clean ranges list, combining adjacent ranges
        """

        ranges = sorted(_swapAllPairs(self.ranges,
                                      that=self._infinity,
                                      ifIs=None))

        mergedRanges = [(float('-inf'), float('-inf'))]


        for low, high in ranges:
            previousLow, previousHigh = mergedRanges[-1]

            if previousHigh < low - 1:
                mergedRanges.append((low, high))
                continue

            mergedRanges[-1] = (min(previousLow, low),
                                max(previousHigh, high))

        self.ranges = _swapAllPairs(mergedRanges[1:],
                                    that=None,
                                    ifIs=self._infinity)

        Clean ranges list, combining adjacent ranges
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/mail/imap4.py