#!/bin/python3 import unittest class RangeSet: def __init__(self, rangespec): self.ranges = self.parse_spec(rangespec) def __contains__(self, num): return any(num in r for r in self.ranges) def parse_spec(self, spec: str) -> list[range]: result = [] parts = spec.split(',') parts = map(str.strip, parts) for part in parts: if '-' in part: start, end = part.split('-') result.append(range(int(start), int(end)+1)) else: n = int(part) result.append(range(n, n+1)) return result # TODO: simplification of inner ranges # TODO: Set operations: union, intersection, difference? # TODO: Comparison to classic range objects (only to check that it represents the same numbers) class TestRangeSetBasic(unittest.TestCase): def test_number_sequence(self): rs = RangeSet('1,3,4,5') for x in [1,3,4,5]: self.assertIn(x, rs) self.assertNotIn(2, rs) if __name__ == '__main__': print('This is a library. Runing the testsuite instead.') unittest.main()