Outsource the table shaping for use with flask

This commit is contained in:
Christian Wolf 2023-11-22 18:52:25 +01:00
parent c690ce755a
commit f944aabc38
10 changed files with 113 additions and 16 deletions

View File

@ -31,4 +31,7 @@ class BatchWorker:
worker.filterOutFinalists(combinedData, removeFilteredParicipants) worker.filterOutFinalists(combinedData, removeFilteredParicipants)
return combinedData outputShaper = solo_turnier.workers.OutputShaper.OutputShaper()
shapedData = outputShaper.shapeResults(combinedData)
return shapedData

View File

@ -86,17 +86,21 @@ class ConsoleOutputter(AbstractOutputter):
return ret return ret
def _outputGroup( def _outputGroup(
self, group: solo_turnier.group.Group, groupResults: types.TotalGroupResult self,
group: solo_turnier.group.Group,
groupResults: solo_turnier.types.GroupTableData,
): ):
print(f"Einzeltanzwettbewerb der Gruppe {group}") if group is not None:
print(f"Einzeltanzwettbewerb der Gruppe {group}")
else:
print("Einzeltanzwettbewerbe ohne eindeutige Gruppenzuordnung")
tableData = [["Tanz"] + groupResults.dances] tableData = [["Tanz"] + groupResults.dances]
participants = list(groupResults.results.keys()) participants = list(groupResults.resultsInGroup.keys())
participants.sort(key=lambda x: (x.id, x.name)) participants.sort(key=lambda x: (x.id, x.name))
for participant in participants: for participant in participants:
results = groupResults.results[participant] results = groupResults.resultsInGroup[participant]
results = self._reshapeRow(results, groupResults.dances)
self.l.log(5, "Results of %s: %s", participant, results) self.l.log(5, "Results of %s: %s", participant, results)
@ -128,14 +132,14 @@ class ConsoleOutputter(AbstractOutputter):
self.l.log(5, "table data: %s", pprint.pformat(tableData)) self.l.log(5, "table data: %s", pprint.pformat(tableData))
print(tabulate(tableData, headers="firstrow", tablefmt="fancy_grid")) print(tabulate(tableData, headers="firstrow", tablefmt="fancy_grid"))
def output(self, data: types.State4): def output(self, data: types.Stage5):
for idx, group in enumerate(data.results): for idx, group in enumerate(data.resultsPerGroup):
if idx > 0: if idx > 0:
print() print()
self.l.debug("Output for group %s", group) self.l.debug("Output for group %s", group)
self._outputGroup(group, data.results[group]) self._outputGroup(group, data.resultsPerGroup[group])
# self.groups = self.worker.collectPersonsInGroups(data) # self.groups = self.worker.collectPersonsInGroups(data)
# self.dances = self.worker.getAllDancesInCompetitions(data) # self.dances = self.worker.getAllDancesInCompetitions(data)

View File

@ -13,7 +13,11 @@
{% for group in data.groups %} {% for group in data.groups %}
{% block groupBlk scoped %} {% block groupBlk scoped %}
<div class="section"> <div class="section">
{% if group is none %}
<h1>Auswertung ohne eindeutige Gruppe</h1>
{% else %}
<h1>Auswertung Gruppe {{ group.name }}</h1> <h1>Auswertung Gruppe {{ group.name }}</h1>
{% endif %}
<table class="tab-summary"> <table class="tab-summary">
<tr> <tr>
<th>Teilnehmer</th> <th>Teilnehmer</th>
@ -39,9 +43,10 @@
{% if not participant.finalist %} {% if not participant.finalist %}
Kein/e Finalist/in Kein/e Finalist/in
{% endif %} {% endif %}
{{ res.getNativePlace() }} ({{ res.nativeClass }}) <br /> {{ res.getNativePlace() }}
({{ res.nativeClass }}) <br />
<span class="competition-place"> <span class="competition-place">
{{ res.getPlace() }} in {{ res.competitionClass }} {{ res.place }} in {{ res.competitionClass }}
</span> </span>
{% endif %} {% endif %}
</td> </td>

View File

@ -13,5 +13,6 @@ from .htmlCompetitionTotalResults import HtmlCompetitionTotalResults
from .singleParticipantResult import SingleParticipantResult from .singleParticipantResult import SingleParticipantResult
from .totalGroupResult import TotalGroupResult from .totalGroupResult import TotalGroupResult
from .participant import Participant from .participant import Participant
from .tableData import *
from .stages import * from .stages import *

View File

@ -19,5 +19,5 @@ class Participant(Person):
return f"Part({self.id} {self.name},F)" return f"Part({self.id} {self.name},F)"
return f"Part({self.id} {self.name})" return f"Part({self.id} {self.name})"
def __ge__(self, other): def __gt__(self, other):
return self.id >= other.id return self.id > other.id

View File

@ -24,3 +24,6 @@ class SingleParticipantResult:
asFinalist = " as finalist" if self.finalist else "" asFinalist = " as finalist" if self.finalist else ""
return f"SR[{self.place} in {self.dance} {self.competitionClass} ({self.nativePlace} {self.nativeClass}){asFinalist}]" return f"SR[{self.place} in {self.dance} {self.competitionClass} ({self.nativePlace} {self.nativeClass}){asFinalist}]"
def getNativePlace(self) -> str:
return str(self.nativePlace)

View File

@ -2,12 +2,23 @@ import solo_turnier
from .totalGroupResult import TotalGroupResult from .totalGroupResult import TotalGroupResult
from .htmlCompetitionTotalResults import HtmlCompetitionTotalResults from .htmlCompetitionTotalResults import HtmlCompetitionTotalResults
from .singleParticipantResult import SingleParticipantResult
from .participant import Participant
from .tableData import GroupTableData
NullableGroup = solo_turnier.group.Group | None
class Stage5:
def __init__(
self,
resultsPerGroup: dict[NullableGroup, GroupTableData],
):
self.resultsPerGroup = resultsPerGroup
class State4: class State4:
def __init__( def __init__(self, resultPerGroup: dict[NullableGroup, TotalGroupResult]):
self, resultPerGroup: dict[solo_turnier.group.Group | None, TotalGroupResult]
):
parser = solo_turnier.group.GroupParser() parser = solo_turnier.group.GroupParser()
self.groups = parser.getGroupsAsSortedList(resultPerGroup.keys()) self.groups = parser.getGroupsAsSortedList(resultPerGroup.keys())
self.results = resultPerGroup self.results = resultPerGroup

View File

@ -0,0 +1,11 @@
import solo_turnier
from .participant import Participant
from .singleParticipantResult import SingleParticipantResult
SortedResultList = dict[Participant, list[SingleParticipantResult | None]]
class GroupTableData:
def __init__(self, dances: list[str], results: SortedResultList):
self.dances = dances
self.resultsInGroup = results

View File

@ -0,0 +1,58 @@
import solo_turnier
import logging
class OutputShaper:
def __init__(self):
self.l = logging.getLogger("solo_turnier.worker.OutputShaper")
def shapeResults(
self, results: solo_turnier.types.State4
) -> solo_turnier.types.Stage5:
ret = {}
for group in results.results:
ret[group] = self._handleGroup(results.results[group])
return solo_turnier.types.Stage5(ret)
def _handleGroup(
self,
totalGroupResult: solo_turnier.types.TotalGroupResult,
) -> solo_turnier.types.GroupTableData:
sortedResultList = {}
for participant in totalGroupResult.results:
sortedResultList[participant] = [None for x in totalGroupResult.dances]
for result in totalGroupResult.results[participant]:
if result.dance not in totalGroupResult.dances:
self.l.error(
"Result in unknown dance found in table. This is a bug. (%s)",
result,
)
continue
idx = totalGroupResult.dances.index(result.dance)
sortedResultList[participant][idx] = result
return solo_turnier.types.GroupTableData(
totalGroupResult.dances, sortedResultList
)
def _reshapeRow(
self,
results: list[solo_turnier.types.SingleParticipantResult],
dances: list[str],
) -> list[solo_turnier.types.SingleParticipantResult]:
ret = [None for x in dances]
for result in results:
if result.dance not in dances:
self.l.error(
"Result in unknown dance found in table. This is a bug. (%s)",
result,
)
continue
idx = dances.index(result.dance)
ret[idx] = result
return ret

View File

@ -1,3 +1,4 @@
from . import ResultExtractor from . import ResultExtractor
from . import DataWorker from . import DataWorker
from . import Worker from . import Worker
from . import OutputShaper