solo-auswertung/src/solo_turnier/group.py

108 lines
2.8 KiB
Python
Raw Normal View History

import re
2023-11-19 16:07:20 +00:00
class Group:
def __init__(self, text: str):
self.name = text
2023-11-19 16:07:20 +00:00
def __repr__(self):
return f"{self.name}"
2023-11-19 16:07:20 +00:00
def getContainedGroups(self):
return (self,)
2023-11-19 16:07:20 +00:00
class CombinedGroup:
def __init__(self, grpA: Group, grpB: Group):
self.clsA = grpA
self.clsB = grpB
2023-11-19 16:07:20 +00:00
def __repr__(self):
2023-11-19 16:07:20 +00:00
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)
2023-11-19 16:07:20 +00:00
Group_t = Group | CombinedGroup
2023-11-19 16:07:20 +00:00
class GroupParser:
2023-11-19 16:07:20 +00:00
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 = {
2023-11-19 16:07:20 +00:00
"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,
}
2023-11-20 10:53:58 +00:00
def parseGroup(self, cls: str) -> Group_t:
2023-11-19 16:07:20 +00:00
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]
2023-11-19 16:07:20 +00:00
2023-11-20 10:53:58 +00:00
def isPureGroup(self, cls: str) -> bool:
parsedGroup = self.parseGroup(cls)
return isinstance(parsedGroup, Group)
2023-11-19 16:07:20 +00:00
def getGroups(self) -> list[Group]:
2023-11-19 16:07:20 +00:00
return [
GroupParser.KIN,
GroupParser.JUN,
GroupParser.JUG,
GroupParser.HGR,
GroupParser.MAS1,
GroupParser.MAS2,
GroupParser.MAS3,
GroupParser.MAS4,
GroupParser.MAS5,
]
2023-11-19 16:07:20 +00:00
def getGroupsAsSortedList(self, groups) -> list[Group]:
return [x for x in self.getGroups() if x in groups]