diff --git a/requiremnts.txt b/requiremnts.txt new file mode 100644 index 0000000..a00a1bb --- /dev/null +++ b/requiremnts.txt @@ -0,0 +1 @@ +tabulate==0.9.0 diff --git a/src/solo_turnier/batch.py b/src/solo_turnier/batch.py index e211228..8b62f9c 100644 --- a/src/solo_turnier/batch.py +++ b/src/solo_turnier/batch.py @@ -2,6 +2,9 @@ import solo_turnier import logging import os +import pprint + +import tabulate class BatchWorker: def __init__( @@ -30,6 +33,11 @@ class BatchWorker: 'Slowfox': 14, 'Quickstep': 15 } + self.__groupFinalMap = { + 'Kinder': 'Kin.', + 'Junioren': 'Jun.', + 'Jugend': 'Jug.' + } def __extractDataFromFiles(self): allResultName = os.path.join(self.config.importPath(), 'allresults.csv') @@ -39,20 +47,20 @@ class BatchWorker: finals = [] # Do this in a loop - # finalName = os.path.join(self.config.importPath(), 'wert_er.txt') - # readerFinal = solo_turnier.reader.ERReader(finalName) - # result = readerFinal.readFile() - # finals.append(result) + finalName = os.path.join(self.config.importPath(), 'wert_er.txt') + readerFinal = solo_turnier.reader.ERReader(finalName) + result = readerFinal.readFile() + finals.append(result) return (allResults, finals) def __convertFinalToTuple(self, result): - return ( - result['gruppe'], - result['klasse'], - result['tanz'], - result['finalisten'] - ) + return { + 'group': result['gruppe'], + 'class': result['klasse'], + 'dance': result['tanz'], + 'finalists': result['finalisten'] + } def __extractAllData(self, r): return { @@ -84,27 +92,34 @@ class BatchWorker: return person def __extractPersons(self, allResults): - personRows = [self.__extractPersonData(r) for r in allResults['data']] - return list(set(personRows)) + def decorate(person,i): + return (person.firstName, person.lastName, person.club, i, person) - def __extractDanceResult(self, r): - data = self.__extractAllData(r) - try: - return { - 'dance': data['dance'], - 'class': data['personClass'], - 'place': int(data['place']), - 'placeTo': int(data['placeTo']), - 'id': data['id'] - } - except ValueError: - self.l.debug('Issue found with data %s.', data) - return None + personRows = [self.__extractPersonData(r) for r in allResults['data']] + persons = list(set(personRows)) + persons = [decorate(p,i) for i,p in enumerate(persons)] + persons.sort() + persons = [p[4] for p in persons] + return persons def __extractAllResults(self, allResults, persons: list[solo_turnier.participant.Person]): + def __extractDanceResult(r): + data = self.__extractAllData(r) + try: + return { + 'dance': data['dance'], + 'class': data['personClass'], + 'place': int(data['place']), + 'placeTo': int(data['placeTo']), + 'id': data['id'] + } + except ValueError: + self.l.debug('Issue found with data %s.', data) + return None + ret = {} for row in allResults['data']: - danceResult = self.__extractDanceResult(row) + danceResult = __extractDanceResult(row) if danceResult is not None: person = self.__extractPersonData(row) if person not in ret: @@ -126,6 +141,21 @@ class BatchWorker: competitions.sort() competitions = [r[2] for r in competitions] return competitions + + + def __extractGroups(self, allResults): + def __extractGroupEntries(r): + data = self.__extractAllData(r) + return data['personGroup'] + + def __decorate(r): + return (self.__groupMap[r], r) + + rows = [__extractGroupEntries(r) for r in allResults['data']] + groups = [__decorate(r) for r in list(set(rows))] + groups.sort() + groups = [r[1] for r in groups] + return groups def __extarctBasicData(self, allResults): def __extractRow(r): @@ -152,12 +182,116 @@ class BatchWorker: totalResults = self.__extractAllResults(allResults, persons) self.l.debug('Total results: %s', totalResults) - competitions = self.__extractCompetitions(allResults) - self.l.debug('Competitions: %s', competitions) + groups = self.__extractGroups(allResults) + self.l.debug('Found groups: %s', groups) + + groupRes = {} + for group in groups: + self.l.info('Processing group %s', group) + groupRes[group] = {} + + personsInGroup = [p for p in persons if p.group == group] + self.l.debug('Remaining persons: %s', personsInGroup) + groupRes[group]['participants'] = personsInGroup + + groupRes[group]['results'] = {} + for person in personsInGroup: + groupRes[group]['results'][person] = totalResults[person] + # resultsInGroup = totalResults[personsInGroup] + # self.l.debug('Results in group: %s', resultsInGroup) + + # Do this in a loop + finalists = [self.__convertFinalToTuple(r) for r in finals] + self.l.debug('Mapped to tuples: %s', finalists) + + self.l.info('Augmenting the list of participants') + for finalistList in finalists: + self.l.debug('Augmenting competition %s', finalistList) + longGroup = finalistList['group'] + grep = self.__groupFinalMap[longGroup] + + for person in groupRes[group]['results'].keys(): + self.l.debug('Checking %s for finalist state in competition', person) + for competition in groupRes[group]['results'][person]: + if competition['dance'] != finalistList['dance']: + continue + if competition['class'] != finalistList['class']: + continue + + competition['finalist'] = (competition['id'] in finalistList['finalists']) + self.l.debug('Organized results: %s', groupRes) + + dancesRaw = [competition['dance'] for group in groups for person in groupRes[group]['participants'] for competition in groupRes[group]['results'][person]] + dances = list(set(dancesRaw)) + dances = [(self.__danceMap[d], d) for d in dances] + dances.sort() + dances = [r[1] for r in dances] + self.l.debug('Found dances in competition: %s', dances) + + ret = {} + for group in groups: + ret[group] = {} + + for person in groupRes[group]['participants']: + ret[group][person] = {} + # Seed the data structure + for dance in dances: + ret[group][person][dance] = { + 'participated': False, + 'class': '', + 'finalist': False, + 'place': '', + 'id': 0 + } + + for competition in groupRes[group]['results'][person]: + def __getPlace(): + if competition['place'] == competition['placeTo']: + return str(competition['place']) + else: + return f"{competition['place']}-{competition['placeTo']}" + + dance = competition['dance'] + ret[group][person][dance]['participated'] = True + ret[group][person][dance]['class'] = competition['class'] + ret[group][person][dance]['finalist'] = competition.get('finalist', False) + ret[group][person][dance]['place'] = __getPlace() + ret[group][person][dance]['id'] = competition['id'] + + self.l.debug('Returned data: %s', pprint.pformat(ret)) + + for group in groups: + self.l.debug('Outputting table for group %s', group) + + print(f'Ergebnisse der Gruppe {group}') + print() + + row = ['Teilnehmer(in)'] + dances + table = [row] + + for person in ret[group]: + row = [str(person)] + for dance in dances: + if not ret[group][person][dance]['participated']: + row.append('') + continue + if not ret[group][person][dance]['finalist']: + row.append('x') + continue + row.append(f"{ret[group][person][dance]['place']} ({ret[group][person][dance]['class']}, {ret[group][person][dance]['id']})") + table.append(row) + + self.l.debug(table) + # print(tabulate.tabulate(table)) + print(tabulate.tabulate(table, headers='firstrow', tablefmt='fancy_grid')) + print() + + + # self.l.debug('Organized results: %s', pprint.pformat( groupRes['Jun.'])) + # competitions = self.__extractCompetitions(allResults) + # self.l.debug('Competitions: %s', competitions) # basicData = self.__extarctBasicData(allResults) # self.l.debug('Basic extracted data: %s', basicData) - # finalTuples = [self.__convertFinalToTuple(r) for r in finals] - # self.l.debug('Mapped to tuples: %s', finalTuples) diff --git a/src/solo_turnier/participant.py b/src/solo_turnier/participant.py index 46976f0..26c8b94 100644 --- a/src/solo_turnier/participant.py +++ b/src/solo_turnier/participant.py @@ -13,6 +13,9 @@ class Person: self.group = group def __eq__(self, o): + if not isinstance(o, Person): + False + return ( self.firstName == o.firstName and self.lastName == o.lastName and diff --git a/src/solo_turnier/reader.py b/src/solo_turnier/reader.py index 347700a..c214b77 100644 --- a/src/solo_turnier/reader.py +++ b/src/solo_turnier/reader.py @@ -49,12 +49,15 @@ class ERReader: if found == -1: raise Exception(f'Could not find the dance in the result file.') + pass # Extract the finalists finalists = [] for i in range(found + 1, len(restLines)): if restLines[i].startswith('TopTurnier'): break + + # self.l.debug('Parsing line "%s"', restLines[i].strip()) match = re.compile('[0-9]+').match(restLines[i].strip()) if match is None: