Consolidate result data

This commit is contained in:
Christian Wolf 2022-11-15 10:48:50 +01:00
parent a4e9020ebd
commit eb7ffc7f78
2 changed files with 131 additions and 3 deletions

View File

@ -30,7 +30,8 @@ def test_extractPersonFromRow():
'lastName': 'Mustermann', 'lastName': 'Mustermann',
'name': 'Max Mustermann', 'name': 'Max Mustermann',
'club': 'TSC Entenhausen', 'club': 'TSC Entenhausen',
'id': None 'id': None,
'group': None
} }
assert person.__dict__ == expected assert person.__dict__ == expected
@ -40,8 +41,63 @@ def test_extractCompetitionFromRow():
expected = { expected = {
'dance': 'Rumba', 'dance': 'Rumba',
'class_': 'Adv.', 'class_': 'Adv.',
'group': 'Kin',
'place': '2', 'place': '2',
'placeTo': '2', 'placeTo': '2',
'id': 2 'id': 2
} }
assert person.__dict__ == expected assert person.__dict__ == expected
def test_combineRowsByPerson():
rows = [
worker.ResultRow('Max', 'Mustermann', 'TSC Entenhausen', '2', 'Kin', 'Adv.', 'Cha Cha', '-', '-'),
worker.ResultRow('Max', 'Mustermann', 'TSC Entenhausen', '2', 'Kin', 'Adv.', 'Rumba', '2', '2'),
worker.ResultRow('Max', 'Mustermann', 'TSC Entenhausen', '2', 'Kin', 'Beg.', 'Jive', '1', '1'),
worker.ResultRow('Maxime', 'Musterfrau', '1. SC Entenhausen', '1', 'Kin', 'Adv.', 'Rumba', '1', '1')
]
dataWorker = worker.DataWorker()
result = dataWorker.combineRowsByPerson(rows)
expected = {
worker.ResultPerson('Max', 'Mustermann', 'TSC Entenhausen'): [
worker.CompetitionResult('Rumba', 'Kin', 'Adv.', '2', '2', '2'),
worker.CompetitionResult('Jive', 'Kin', 'Beg.', '1', '1', '2')
],
worker.ResultPerson('Maxime', 'Musterfrau', '1. SC Entenhausen'): [
worker.CompetitionResult('Rumba', 'Kin', 'Adv.', '1', '1', '1')
]
}
assert result == expected
def test_checkUniqueIds_True():
person1 = worker.ResultPerson('Max', 'Mustermann', 'TSC Entenhausen')
person2 = worker.ResultPerson('Maxime', 'Musterfrau', '1. SC Entenhausen')
data = {
person1: [
worker.CompetitionResult('Rumba', 'Kin', 'Adv.', '2', '2', 2),
worker.CompetitionResult('Jive', 'Kin', 'Beg.', '1', '1', 2)
],
person2: [
worker.CompetitionResult('Rumba', 'Kin', 'Adv.', '1', '1', 1)
]
}
dataWorker = worker.DataWorker()
assert dataWorker.checkUniqueIds(data) == True
assert person1.id == 2
assert person2.id == 1
def test_checkUniqueIds_True():
person1 = worker.ResultPerson('Max', 'Mustermann', 'TSC Entenhausen')
person2 = worker.ResultPerson('Maxime', 'Musterfrau', '1. SC Entenhausen')
data = {
person1: [
worker.CompetitionResult('Rumba', 'Kin', 'Adv.', '2', '2', 2),
worker.CompetitionResult('Jive', 'Kin', 'Beg.', '1', '1', 3)
],
person2: [
worker.CompetitionResult('Rumba', 'Kin', 'Adv.', '1', '1', 1)
]
}
dataWorker = worker.DataWorker()
assert dataWorker.checkUniqueIds(data) == False
assert person1.id == None
assert person2.id == 1

View File

@ -14,12 +14,13 @@ class ResultRow:
self.placeTo = placeTo self.placeTo = placeTo
class ResultPerson: class ResultPerson:
def __init__(self, firstName, lastName, club, id = None): def __init__(self, firstName, lastName, club, id = None, group = None):
self.firstName = firstName self.firstName = firstName
self.lastName = lastName self.lastName = lastName
self.name = f'{firstName} {lastName}' self.name = f'{firstName} {lastName}'
self.club = club self.club = club
self.id = id self.id = id
self.group = group
@staticmethod @staticmethod
def extractFromResultRow(row: ResultRow): def extractFromResultRow(row: ResultRow):
@ -29,9 +30,31 @@ class ResultPerson:
club=row.club club=row.club
) )
def __eq__(self, o):
if not isinstance(o, ResultPerson):
return False
return (
self.firstName == o.firstName and
self.lastName == o.lastName and
self.club == o.club and
self.id == o.id
)
def __repr__(self):
if self.id is None:
return f'{self.name} ({self.club})'
else:
return f'{self.name} ({self.club}) [{self.id}]'
def __hash__(self):
text = str(self)
return text.__hash__()
class CompetitionResult: class CompetitionResult:
def __init__(self, dance, class_, place, placeTo, id): def __init__(self, dance, group, class_, place, placeTo, id):
self.dance = dance self.dance = dance
self.group = group
self.class_ = class_ self.class_ = class_
self.place = place self.place = place
self.placeTo = placeTo self.placeTo = placeTo
@ -41,11 +64,31 @@ class CompetitionResult:
def extractFromResultRow(row: ResultRow): def extractFromResultRow(row: ResultRow):
return CompetitionResult( return CompetitionResult(
dance=row.dance, dance=row.dance,
group=row.group,
class_=row.class_, class_=row.class_,
place=row.place, placeTo=row.placeTo, place=row.place, placeTo=row.placeTo,
id=row.id id=row.id
) )
def __repr__(self):
if self.place == self.placeTo:
result = f'{self.place}.'
else:
result = f'{self.place}.-{self.placeTo}.'
return f'Result[{self.id}]({self.group} {self.class_} {self.dance} as {result})'
def __eq__(self, o):
if not isinstance(o, CompetitionResult):
return False
return (
self.dance == o.dance and
self.class_ == o.class_ and
self.group == o.group and
self.place == o.place and self.placeTo == o.placeTo and
self.id == o.id
)
class CSVExtractor: class CSVExtractor:
def __init__(self): def __init__(self):
self.l = logging.getLogger('solo_turnier.worker') self.l = logging.getLogger('solo_turnier.worker')
@ -68,3 +111,32 @@ class CSVExtractor:
__processRow(row) __processRow(row)
return ret return ret
class DataWorker:
def __init__(self):
self.l = logging.getLogger('solo_turnier.worker')
def combineRowsByPerson(self, rows: list[ResultRow]) -> dict[ResultPerson, list[CompetitionResult]]:
ret = {}
for row in rows:
result = CompetitionResult.extractFromResultRow(row)
if result.place == '-' or result.placeTo == '-':
continue
person = ResultPerson.extractFromResultRow(row)
if person not in ret:
ret[person] = []
ret[person].append(result)
return ret
def checkUniqueIds(self, data: dict[ResultPerson, list[CompetitionResult]]) -> bool:
unique = True
for person in data:
ids = set([c.id for c in data[person]])
if len(ids) == 1:
person.id = list(ids)[0]
else:
unique = False
return unique