From 525b445c49bea1409dd8fc6d35b890e0a8d0121a Mon Sep 17 00:00:00 2001 From: Lennard Geese <lennard.geese@sva.de> Date: Wed, 16 Apr 2025 07:56:11 +0200 Subject: [PATCH] Add and update documentation --- DataAnalyser.py | 16 +++++++++------- DataImporter.py | 23 ++++++++++++++--------- main.py | 2 +- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/DataAnalyser.py b/DataAnalyser.py index ca7e140..a05ede2 100644 --- a/DataAnalyser.py +++ b/DataAnalyser.py @@ -1,11 +1,8 @@ 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: diff --git a/DataImporter.py b/DataImporter.py index b021da4..325ad35 100644 --- a/DataImporter.py +++ b/DataImporter.py @@ -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) diff --git a/main.py b/main.py index e9d8b82..a15573d 100644 --- a/main.py +++ b/main.py @@ -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) -- GitLab