Skip to content
Snippets Groups Projects
Commit 525b445c authored by Lennard Geese's avatar Lennard Geese
Browse files

Add and update documentation

parent 21fc550f
Branches
No related tags found
No related merge requests found
import re
import string
from typing import Iterator
import pandas as pandas
import json as json
from fastf1.core import Session, Lap, Laps, DataNotLoadedError
from bs4 import BeautifulSoup, PageElement, NavigableString, Tag
from bs4 import BeautifulSoup
from DataHandler import DataHandler
......@@ -14,8 +11,8 @@ class DataAnalyser(DataHandler):
"""
Analyses sessions by extrapolating existing or new data from them.
Any method of this class must be given a Session object or a list thereof. If the method does not require such an
object, it should not be part of this class.
Any public method of this class must be given a Session, Event or Lap object or a list thereof. If the method does not
require such an object, it should not be part of this class.
"""
......@@ -122,7 +119,12 @@ class DataAnalyser(DataHandler):
# ===== Weather =====
def getWeatherFromHtml(self, rawHtml):
def extractWeatherFromHtml(self, rawHtml):
"""
Extract an events weather conditions from its respective wikipedia page.
:param rawHtml: HTML content of the wikipedia page from which to extract.
:return: The weather conditions of the event as specified in the wikipedia page's infobox.
"""
parsedHtml = BeautifulSoup(rawHtml, features="html.parser")
tableRows = parsedHtml.find_all("tr") # Get all table rows on wiki page
for tableRow in tableRows:
......
......@@ -17,24 +17,33 @@ class DataImporter(DataHandler, ABC):
"""
Imports/loads sessions as specified by SessionIdentifiers and/or certain criteria such as rain.
Any method of this class must be given a SessionIdentifier, a time or a time period by which to select
Any public method of this class must be given an Event, Session, SessionIdentifier, time or time period by which to select
sessions. If the method does not require one of these, it should not be part of this class.
"""
def importWeatherFromWiki(self, event: Event | SessionIdentifier):
def fetchEventWeather(self, event: Event | SessionIdentifier):
"""
Fetch session weather from the wikipedia entry for the specified event.
:param event: The event or identifier for any session of the event to fetch weather for.
:return: Weather conditions as string
"""
if isinstance(event, SessionIdentifier):
event = self.importEvent(event.year, event.event)
wikiHtml = self.importWikiHtml(event)
analyser = DataAnalyser()
weather = analyser.getWeatherFromHtml(wikiHtml)
weather = analyser.extractWeatherFromHtml(wikiHtml)
return weather
def importWikiHtml(self, event: Event):
"""
Fetch the HTML contents of the wikipedia page for the event given.
:param event: Event whose wikipedia page to fetch.
:return: HTML content of the wikipedia page as a string.
"""
apiRootUrl: string = "https://en.wikipedia.org/wiki/"
grandPrixName: string = f"{event.year} {event["EventName"]}"
uri: string = re.sub(" ", "_", grandPrixName)
......@@ -43,11 +52,7 @@ class DataImporter(DataHandler, ABC):
if not response.ok:
raise ConnectionError(f"Could not fetch wikipedia article with URL {url}")
return response.text
# URL example to get 2024 Sao Paulo GP: https://en.wikipedia.org/w/api.php?action=query&titles=2024_S%C3%A3o_Paulo_Grand_Prix&prop=extracts&format=json&exintro=1
# URL example: https://en.wikipedia.org/w/api.php?action=query&titles=2024_S%C3%A3o_Paulo_Grand_Prix&prop=extracts&format=json&exintro=1
def importEvent(self, year: int, event: int | str):
return fastf1.get_event(year, event)
......
......@@ -37,7 +37,7 @@ class Main:
def testNewRainRaceFetching(self):
dataHandler: Main.DataHandlingPackage = Main.DataHandlingPackage()
sessionIdentifier: SessionIdentifier = SessionIdentifier(2024, "Brazil", "R")
weather = dataHandler.importer.importWeatherFromWiki(sessionIdentifier)
weather = dataHandler.importer.fetchEventWeather(sessionIdentifier)
print(weather)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment