From 07c3ebc7e04f375d523f756f4aaad7f63d92f79a Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Sat, 3 Dec 2022 05:58:37 +0100 Subject: [PATCH] =?UTF-8?q?P=C5=99id=C3=A1na=20knihovna=20na=20parsov?= =?UTF-8?q?=C3=A1n=C3=AD=20rozsah=C5=AF=20(WIP)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Použije se na automatické vyrábění problémů. Inspirováno dialogy k tisku na rozsahy stran. Pardon, psal jsem to někde separátně, tak jsem to nakódil anglicky… --- rangeset.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 rangeset.py diff --git a/rangeset.py b/rangeset.py new file mode 100755 index 00000000..dd5952f1 --- /dev/null +++ b/rangeset.py @@ -0,0 +1,34 @@ +#!/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()