Přidána knihovna na parsování rozsahů (WIP)
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…
This commit is contained in:
parent
904a81b38f
commit
07c3ebc7e0
1 changed files with 34 additions and 0 deletions
34
rangeset.py
Executable file
34
rangeset.py
Executable file
|
@ -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()
|
Loading…
Reference in a new issue