From 5d141113aab39834c9e53fb9dc2ced0e9f456bb0 Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Mon, 28 Nov 2022 20:11:50 +0100 Subject: [PATCH] Put CSV parsing into separate file --- src/solo_turnier/reader.py | 85 +++++++++++++++++++++++++++++++++++++- src/solo_turnier/worker.py | 58 -------------------------- 2 files changed, 84 insertions(+), 59 deletions(-) diff --git a/src/solo_turnier/reader.py b/src/solo_turnier/reader.py index ca246d2..84d8f0c 100644 --- a/src/solo_turnier/reader.py +++ b/src/solo_turnier/reader.py @@ -6,7 +6,26 @@ import logging import re from pprint import pformat -class AllResultReader: +class ResultRow: + def __init__(self, firstName, lastName, club, id, group, class_, dance, place, placeTo, competitionGroup, competitionClass): + self.firstName = firstName + self.lastName = lastName + self.name = f'{firstName} {lastName}' + self.club = club + self.id = id + self.group = group + self.class_ = class_ + self.dance = dance + self.place = place + self.placeTo = placeTo + self.competitionGroup = competitionGroup + self.competitionClass = competitionClass + + def __str__(self): + return f'{self.name} ({self.id}, {self.club}) is in {self.group} {self.class_} and danced the {self.dance} in {self.competitionGroup} {self.competitionClass} getting place {self.place}-{self.placeTo}' + + +class CSVResultReader: def __init__(self, fileName: str): self.fileName = fileName @@ -29,3 +48,67 @@ class AllResultReader: l = logging.getLogger('solo_turnier.reader.all_results') l.log(5, 'Imported results from allresults.csv file: %s', (ret)) return ret + + def extractResult(self, entries = None) -> list[ResultRow]: + if entries is None: + entries = self.readFile() + + def __processRow(row): + result = ResultRow( + competitionGroup=self.__mapGroup(row[2]), + competitionClass=self.__mapClass(row[3]), + dance=row[4], + id=row[5], + firstName=row[6], lastName=row[7], + club=row[10], + place=row[12], placeTo=row[13], + group=self.__mapGroup(row[15]), class_=self.__mapClass(row[16]) + ) + self.l.log(5, 'Found row in CSV: %s', result) + return result + + ret = map(__processRow, imported['data']) + + return ret + +class CSVExtractor: + def __init__(self): + self.l = logging.getLogger('solo_turnier.worker') + self.__groupMaps = { + 'Kinder': 'Kin.', + 'Junioren': 'Jun.', + 'Jugend': 'Jug.' + } + self.__classMaps = { + 'Newcomer': 'Newc.', + 'Beginner': 'Beg.', + 'Advanced': 'Adv.' + } + + def __mapGroup(self, group): + return self.__groupMaps.get(group, group) + + def __mapClass(self, class_): + return self.__classMaps.get(class_, class_) + + def mapCSVImport(self, imported) -> list[ResultRow]: + ret = [] + + def __processRow(row): + result = ResultRow( + competitionGroup=self.__mapGroup(row[2]), + competitionClass=self.__mapClass(row[3]), + dance=row[4], + id=row[5], + firstName=row[6], lastName=row[7], + club=row[10], + place=row[12], placeTo=row[13], + group=self.__mapGroup(row[15]), class_=self.__mapClass(row[16]) + ) + ret.append(result) + self.l.log(5, 'Found row in CSV: %s', result) + + for row in imported['data']: + __processRow(row) + + return ret diff --git a/src/solo_turnier/worker.py b/src/solo_turnier/worker.py index c1e9db7..2e8b51f 100644 --- a/src/solo_turnier/worker.py +++ b/src/solo_turnier/worker.py @@ -2,24 +2,6 @@ import logging from solo_turnier import html_parser -class ResultRow: - def __init__(self, firstName, lastName, club, id, group, class_, dance, place, placeTo, competitionGroup, competitionClass): - self.firstName = firstName - self.lastName = lastName - self.name = f'{firstName} {lastName}' - self.club = club - self.id = id - self.group = group - self.class_ = class_ - self.dance = dance - self.place = place - self.placeTo = placeTo - self.competitionGroup = competitionGroup - self.competitionClass = competitionClass - - def __str__(self): - return f'{self.name} ({self.id}, {self.club}) is in {self.group} {self.class_} and danced the {self.dance} in {self.competitionGroup} {self.competitionClass} getting place {self.place}-{self.placeTo}' - class HtmlPerson: def __init__(self, name, id, group): self.name = name @@ -115,47 +97,7 @@ class CompetitionResult: self.id == o.id ) -class CSVExtractor: - def __init__(self): - self.l = logging.getLogger('solo_turnier.worker') - self.__groupMaps = { - 'Kinder': 'Kin.', - 'Junioren': 'Jun.', - 'Jugend': 'Jug.' - } - self.__classMaps = { - 'Newcomer': 'Newc.', - 'Beginner': 'Beg.', - 'Advanced': 'Adv.' - } - def __mapGroup(self, group): - return self.__groupMaps.get(group, group) - - def __mapClass(self, class_): - return self.__classMaps.get(class_, class_) - - def mapCSVImport(self, imported) -> list[ResultRow]: - ret = [] - - def __processRow(row): - result = ResultRow( - competitionGroup=self.__mapGroup(row[2]), - competitionClass=self.__mapClass(row[3]), - dance=row[4], - id=row[5], - firstName=row[6], lastName=row[7], - club=row[10], - place=row[12], placeTo=row[13], - group=self.__mapGroup(row[15]), class_=self.__mapClass(row[16]) - ) - ret.append(result) - self.l.debug('Found row in CSV: %s', result) - - for row in imported['data']: - __processRow(row) - - return ret class PreviewWorker: def __init__(self):