Put CSV parsing into separate file

This commit is contained in:
Christian Wolf 2022-11-28 20:11:50 +01:00
parent d00639d64a
commit 5d141113aa
2 changed files with 84 additions and 59 deletions

View File

@ -6,7 +6,26 @@ import logging
import re import re
from pprint import pformat 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): def __init__(self, fileName: str):
self.fileName = fileName self.fileName = fileName
@ -29,3 +48,67 @@ class AllResultReader:
l = logging.getLogger('solo_turnier.reader.all_results') l = logging.getLogger('solo_turnier.reader.all_results')
l.log(5, 'Imported results from allresults.csv file: %s', (ret)) l.log(5, 'Imported results from allresults.csv file: %s', (ret))
return 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

View File

@ -2,24 +2,6 @@ import logging
from solo_turnier import html_parser 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: class HtmlPerson:
def __init__(self, name, id, group): def __init__(self, name, id, group):
self.name = name self.name = name
@ -115,47 +97,7 @@ class CompetitionResult:
self.id == o.id 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: class PreviewWorker:
def __init__(self): def __init__(self):