From d00639d64a84f78667d96de8445e774afd2bda20 Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Mon, 28 Nov 2022 19:01:13 +0100 Subject: [PATCH] Create abstraction classes for constants --- src/solo_turnier/__init__.py | 3 ++ src/solo_turnier/competition_class.py | 47 +++++++++++++++++ src/solo_turnier/group.py | 47 +++++++++++++++++ .../tests/test_competition_class.py | 51 +++++++++++++++++++ src/solo_turnier/tests/test_group.py | 51 +++++++++++++++++++ 5 files changed, 199 insertions(+) create mode 100644 src/solo_turnier/competition_class.py create mode 100644 src/solo_turnier/group.py create mode 100644 src/solo_turnier/tests/test_competition_class.py create mode 100644 src/solo_turnier/tests/test_group.py diff --git a/src/solo_turnier/__init__.py b/src/solo_turnier/__init__.py index 610279e..2f06776 100644 --- a/src/solo_turnier/__init__.py +++ b/src/solo_turnier/__init__.py @@ -1,4 +1,7 @@ +from . import competition_class +from . import group + from . import cli from . import reader from . import participant diff --git a/src/solo_turnier/competition_class.py b/src/solo_turnier/competition_class.py new file mode 100644 index 0000000..57089d4 --- /dev/null +++ b/src/solo_turnier/competition_class.py @@ -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) diff --git a/src/solo_turnier/group.py b/src/solo_turnier/group.py new file mode 100644 index 0000000..a9fb538 --- /dev/null +++ b/src/solo_turnier/group.py @@ -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) diff --git a/src/solo_turnier/tests/test_competition_class.py b/src/solo_turnier/tests/test_competition_class.py new file mode 100644 index 0000000..1e35687 --- /dev/null +++ b/src/solo_turnier/tests/test_competition_class.py @@ -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) diff --git a/src/solo_turnier/tests/test_group.py b/src/solo_turnier/tests/test_group.py new file mode 100644 index 0000000..09eb5a8 --- /dev/null +++ b/src/solo_turnier/tests/test_group.py @@ -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)