import statistics import hfpy_utils CHARTS = "charts/" FOLDER = "swimdata/" def get_swim_data(fn): """Given the name of a swimmer's file, extract all the required data, then return it to the caller as a tuple.""" swimmer, age, distance, stroke = fn.removesuffix(".txt").split("-") with open(FOLDER + fn) as df: data = df.readlines() times = data[0].strip().split(",") converts = [] scaled = [] for t in times: if ":" in t: minutes, rest = t.split(":") seconds, hundredths = rest.split(".") else: minutes = 0 seconds, hundredths = t.split(".") converts.append(int(minutes) * 60 * 100 + int(seconds) * 100 + int(hundredths)) average = statistics.mean(converts) mins_secs, hundredths = f"{(average / 100):.2f}".split(".") mins_secs = int(mins_secs) minutes = mins_secs // 60 seconds = mins_secs - minutes * 60 average_str = f"{minutes}:{seconds}.{hundredths}" return swimmer, age, distance, stroke, average, average_str, times, converts def produce_bar_chart(fn): """Given the name of a swimmer's file, produce a HTML/SVG-based bar chart, saving it to the CHARTS folder. Return the path to the bar chart file.""" swimmer, age, distance, stroke, _, average_str, times, converts = get_swim_data(fn) from_max = max(converts) times.reverse() converts.reverse() title = f"{swimmer} (Under {age}) {distance} {stroke}" header = f""" {title}

{title}

""" body = "" for n, t in enumerate(times): bar_width = hfpy_utils.convert2range(converts[n], 0, from_max, 0, 350) body = body + f""" {t}
""" footer = f"""

Average time: {average_str}

""" page = header + body + footer save_to = f"{CHARTS}{fn.removesuffix('.txt')}.html" with open(save_to, "w") as sf: print(page, file=sf) return save_to