Usage of objects in HTML parser

This commit is contained in:
Christian Wolf 2022-11-15 14:38:59 +01:00
parent 4275466cca
commit 358f985978
2 changed files with 19 additions and 10 deletions

View File

@ -3,6 +3,17 @@ from bs4 import BeautifulSoup
import logging import logging
import re import re
class HtmlParticipant:
def __init__(self, name, place, finalist):
self.name = name
self.place = place
self.finalist = finalist
class HtmlImport:
def __init__(self, title: str, participants: dict[int, HtmlParticipant]):
self.title = title
self.participants = participants
class HtmlParser: class HtmlParser:
def __init__(self): def __init__(self):
@ -26,11 +37,7 @@ class HtmlParser:
name = match.group(1) name = match.group(1)
number = match.group(2) number = match.group(2)
participant = { participant = HtmlParticipant(name, place, finalist)
'name': name,
'place': place,
'finalist': finalist
}
participants[number] = participant participants[number] = participant
for row in rows: for row in rows:
@ -55,10 +62,7 @@ class HtmlParser:
title = soup.find('div', class_='eventhead').table.tr.td.contents[0] title = soup.find('div', class_='eventhead').table.tr.td.contents[0]
ret = { ret = HtmlImport(title, participants)
'participants': participants,
'title': title
}
return ret return ret
def guessDataFromHtmlTitle(self, title): def guessDataFromHtmlTitle(self, title):

View File

@ -25,7 +25,12 @@ def test_extractDataFromHtml(dataProviderHtmlParser):
parser = solo_turnier.html_parser.HtmlParser() parser = solo_turnier.html_parser.HtmlParser()
actualResult = parser.parseString(htmlString) actualResult = parser.parseString(htmlString)
assert actualResult == expected participants = {}
for i in actualResult.participants:
participants[i] = actualResult.participants[i].__dict__
assert actualResult.title == expected['title']
assert participants == expected['participants']
@pytest.fixture(params=range(5)) @pytest.fixture(params=range(5))
def fixture_guessDataFromTitle(request): def fixture_guessDataFromTitle(request):