solo-auswertung/src/solo_turnier/group.py

110 lines
2.9 KiB
Python

import re
class Group:
def __init__(self, text: str):
self.name = text
def __repr__(self):
return f"{self.name}"
def getContainedGroups(self):
return (self,)
class CombinedGroup:
def __init__(self, grpA: Group, grpB: Group):
self.clsA = grpA
self.clsB = grpB
def __repr__(self):
return f"{self.clsA}/{self.clsB}"
def __hash__(self):
return hash(("combinedGroup", self.clsA, self.clsB))
def __eq__(self, other):
return type(self) == type(other) and self.__hash__() == other.__hash__()
def getContainedGroups(self):
return (self.clsA, self.clsB)
Group_t = Group | CombinedGroup
class GroupParser:
KIN = Group("Kin.")
JUN = Group("Jun.")
JUG = Group("Jug.")
HGR = Group("Hgr.")
MAS1 = Group("Mas. I")
MAS2 = Group("Mas. II")
MAS3 = Group("Mas. III")
MAS4 = Group("Mas. IV")
MAS5 = Group("Mas. V")
def __init__(self):
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,
"Hgr": self.HGR,
"HGr": self.HGR,
"Hgr.": self.HGR,
"HGr.": self.HGR,
"Hauptgruppe": self.HGR,
"Mas. I": self.MAS1,
"Mas. II": self.MAS2,
"Mas. III": self.MAS3,
"Mas. IV": self.MAS4,
"Mas. V": self.MAS5,
"Mas I": self.MAS1,
"Mas II": self.MAS2,
"Mas III": self.MAS3,
"Mas IV": self.MAS4,
"Mas V": self.MAS5,
"Masters I": self.MAS1,
"Masters II": self.MAS2,
"Masters III": self.MAS3,
"Masters IV": self.MAS4,
"Masters V": self.MAS5,
}
def parseGroup(self, cls: str) -> Group_t:
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 isPureGroup(self, cls: str) -> bool:
parsedGroup = self.parseGroup(cls)
return isinstance(parsedGroup, Group)
def getGroups(self) -> list[Group]:
return [
GroupParser.KIN,
GroupParser.JUN,
GroupParser.JUG,
GroupParser.HGR,
GroupParser.MAS1,
GroupParser.MAS2,
GroupParser.MAS3,
GroupParser.MAS4,
GroupParser.MAS5,
]
def getGroupsAsSortedList(self, groups) -> list[Group]:
mainGroups = [x for x in self.getGroups() if x in groups]
additionalGroups = set(groups).difference(mainGroups)
return mainGroups + list(additionalGroups)