Samuel-Roach / ergast-py

A comprehensive Python wrapper for the Ergast API
GNU General Public License v3.0
45 stars 9 forks source link

[ValueError] Helper function getting pitstop time: construct_pitstop_duration #10

Closed ibrahimroshdy closed 1 year ago

ibrahimroshdy commented 1 year ago

Hi, @Samuel-Roach

https://github.com/Samuel-Roach/ergast-py/blob/ea4efd92d864e7a1a586a26271263eeaedc2c307/ergast_py/helpers.py#L76

Results in ValueError when attempting to get data from race where pitstop time was larger than expected. (In case of crashes, RED FLAGS, that lasted for a long time are considered as a pitstop in ERGAST database.


Replicate Error

Pulling data from Silverstone Round 10, 2022 Zhou Guanyu Crash

# Try to pull the pitstop data from the Silverstone, round_no: 10, season: 2022
import ergast_py

e = ergast_py.Ergast()
e.season(2022).round(10).get_pit_stops()
valueerror

you will be left with '50:37.323' as a pitstop duration, which is not expected, but valid in such case. Below is the current helper function where it uses strptime for seconds and milliseconds only.

    def construct_pitstop_duration(self, time: str) -> datetime.time:
        """
        Construct a datetime.time (pit stop duration) from a time string
        Looking for the format of ``%S.%f``
        """
        if time != "":
            return datetime.datetime.strptime(f"{time}", "%S.%f").time()
        return None

Quick Fix

A quick fix would be using try expect for value error


    def construct_pitstop_duration(self, time: str) -> datetime.time:
        """
        Construct a datetime.time (pit stop duration) from a time string

        Looking for the format of ``%S.%f``
        """
        if time != "":
            try:
                return datetime.datetime.strptime(f"{time}", "%S.%f").time()
            except ValueError as VE:
                return datetime.datetime.strptime(f"{time}", "%M:%S.%f").time()
        return None
ibrahimroshdy commented 1 year ago

Hey @Samuel-Roach , I found the same thing happens with as well. https://github.com/Samuel-Roach/ergast-py/blob/ea4efd92d864e7a1a586a26271263eeaedc2c307/ergast_py/helpers.py#L11

Replicating error

When retrieving older data. For example years 1956 where the time object is not retrieved because it is missing in the Ergast DB.

    e = ergast_py.Ergast()
    constructorresults = e.season(1956).limit(sys.maxsize).get_results()

Results in


    raise ValueError("time data %r does not match format %r" %
ValueError: time data '1956-01-22 ' does not match format '%Y-%m-%d %H:%M:%SZ'

Quick fix

    def construct_datetime_str(self, date: str, time: str) -> datetime.datetime:
        """
        Construct a datetime.datetime from the date and time strings.

        Looking for the format of ``%Y-%m-%d %H:%M:%SZ``
        """
        try:
            new_datetime = datetime.datetime.strptime(f"{date} {time}", "%Y-%m-%d %H:%M:%SZ")
        except ValueError as VE:
            new_datetime = datetime.datetime.strptime(f"{date}", "%Y-%m-%d")
        new_datetime = new_datetime.replace(tzinfo=datetime.timezone.utc)
        return new_datetime
Samuel-Roach commented 1 year ago

Raised a PR for the initial issue. The second comment should already by addressed by https://github.com/Samuel-Roach/ergast-py/pull/5