Create abstraction classes for constants

This commit is contained in:
Christian Wolf 2022-11-28 19:01:13 +01:00
parent 9e59ca79d2
commit d00639d64a
5 changed files with 199 additions and 0 deletions

View File

@ -1,4 +1,7 @@
from . import competition_class
from . import group
from . import cli from . import cli
from . import reader from . import reader
from . import participant from . import participant

View File

@ -0,0 +1,47 @@
import re
class CompetitionClass:
def __init__(self, text: str):
self.name = text
def __repr__(self):
return self.name
class CombinedCompetitionClass:
def __init__(self, clsA: CompetitionClass, clsB: CompetitionClass):
self.clsA = clsA
self.clsB = clsB
def __repr__(self):
return f'{self.clsA}/{self.clsB}'
class CompetitionClassParser:
def __init__(self):
self.NEWC = CompetitionClass('Newc.')
self.BEG = CompetitionClass('Beg.')
self.ADV = CompetitionClass('Adv.')
self.mapNames = {
'Newc': self.NEWC,
'Newc.': self.NEWC,
'Newcomer': self.NEWC,
'Beg': self.BEG,
'Beg.': self.BEG,
'Beginner': self.BEG,
'Adv': self.ADV,
'Adv.': self.ADV,
'Advanced': self.ADV,
}
def parseClass(self, cls: str) -> CompetitionClass|CombinedCompetitionClass:
match = re.compile('^(\\w+\\.?)/(\\w+\\.?)$').match(cls)
if match is not None:
clsA = self.mapNames[match.group(1)]
clsB = self.mapNames[match.group(2)]
return CombinedCompetitionClass(clsA, clsB)
else:
return self.mapNames[cls]
def isPureClass(self, cls: str) -> bool:
parsedClass = self.parseClass(cls)
return isinstance(parsedClass, CompetitionClass)

47
src/solo_turnier/group.py Normal file
View File

@ -0,0 +1,47 @@
import re
class Group:
def __init__(self, text: str):
self.name = text
def __repr__(self):
return self.name
class CombinedGroup:
def __init__(self, grpA: Group, grpB: Group):
self.clsA = grpA
self.clsB = grpB
def __repr__(self):
return f'{self.clsA}/{self.clsB}'
class GroupParser:
def __init__(self):
self.KIN = Group('Kin.')
self.JUN = Group('Jun.')
self.JUG = Group('Jug.')
self.mapNames = {
'Kin': self.KIN,
'Kin.': self.KIN,
'Kinder': self.KIN,
'Jun': self.JUN,
'Jun.': self.JUN,
'Junioren': self.JUN,
'Jug': self.JUG,
'Jug.': self.JUG,
'Jugend': self.JUG,
}
def parseClass(self, cls: str) -> Group|CombinedGroup:
match = re.compile('^(\\w+\\.?)/(\\w+\\.?)$').match(cls)
if match is not None:
grpA = self.mapNames[match.group(1)]
grpB = self.mapNames[match.group(2)]
return CombinedGroup(grpA, grpB)
else:
return self.mapNames[cls]
def isPureClass(self, cls: str) -> bool:
parsedClass = self.parseClass(cls)
return isinstance(parsedClass, Group)

View File

@ -0,0 +1,51 @@
import solo_turnier.competition_class
import pytest
@pytest.fixture(params=range(9))
def fix_pureClass(request):
cases = (
('Newc', 'Newc.'),
('Newc.', 'Newc.'),
('Newcomer', 'Newc.'),
('Beg', 'Beg.'),
('Beg.', 'Beg.'),
('Beginner', 'Beg.'),
('Adv', 'Adv.'),
('Adv.', 'Adv.'),
('Advanced', 'Adv.'),
)
return cases[request.param]
def test_pureClassParsing(fix_pureClass):
className = fix_pureClass[0]
expected = fix_pureClass[1]
parser = solo_turnier.competition_class.CompetitionClassParser()
ret = parser.parseClass(className)
assert isinstance(ret, solo_turnier.competition_class.CompetitionClass)
assert str(ret) == expected
assert parser.isPureClass(className)
@pytest.fixture(params=range(4))
def fix_combinedClass(request):
cases = (
('Newc/Beg', 'Newc./Beg.'),
('Newc./Beg', 'Newc./Beg.'),
('Beginner/Adv', 'Beg./Adv.'),
('Beg/Adv', 'Beg./Adv.'),
)
return cases[request.param]
def test_combinedClassParsing(fix_combinedClass):
className = fix_combinedClass[0]
expected = fix_combinedClass[1]
parser = solo_turnier.competition_class.CompetitionClassParser()
ret = parser.parseClass(className)
assert isinstance(ret, solo_turnier.competition_class.CombinedCompetitionClass)
assert str(ret) == expected
assert not parser.isPureClass(className)

View File

@ -0,0 +1,51 @@
import solo_turnier.group
import pytest
@pytest.fixture(params=range(9))
def fix_pureClass(request):
cases = (
('Kin', 'Kin.'),
('Kin.', 'Kin.'),
('Kinder', 'Kin.'),
('Jun', 'Jun.'),
('Jun.', 'Jun.'),
('Junioren', 'Jun.'),
('Jug', 'Jug.'),
('Jug.', 'Jug.'),
('Jugend', 'Jug.'),
)
return cases[request.param]
def test_pureClassParsing(fix_pureClass):
className = fix_pureClass[0]
expected = fix_pureClass[1]
parser = solo_turnier.group.GroupParser()
ret = parser.parseClass(className)
assert isinstance(ret, solo_turnier.group.Group)
assert str(ret) == expected
assert parser.isPureClass(className)
@pytest.fixture(params=range(4))
def fix_combinedClass(request):
cases = (
('Kin/Jun', 'Kin./Jun.'),
('Kin./Jun', 'Kin./Jun.'),
('Junioren/Jug', 'Jun./Jug.'),
('Jun/Jug', 'Jun./Jug.'),
)
return cases[request.param]
def test_combinedClassParsing(fix_combinedClass):
className = fix_combinedClass[0]
expected = fix_combinedClass[1]
parser = solo_turnier.group.GroupParser()
ret = parser.parseClass(className)
assert isinstance(ret, solo_turnier.group.CombinedGroup)
assert str(ret) == expected
assert not parser.isPureClass(className)