utils.py
 1  import os
 2  import pickle
 3  from pprint import pprint
 4  
 5  import numpy as np
 6  import pandas as pd
 7  
 8  from telemetry.influx import Influx
 9  from telemetry.models import Lap
10  
11  
12  def read_dataframe(file_path):
13      return pd.read_csv(file_path, compression="gzip", parse_dates=["_time"])
14  
15  
16  def save_dataframe(df, file_path):
17      df.to_csv(file_path, compression="gzip", index=False)
18  
19  
20  def process_dataframe(df):
21      df = df.sort_values(by="_time")
22      df = df.replace(np.nan, None)
23      return df
24  
25  
26  def get_session_df(session_id, measurement="fast_laps", bucket="fast_laps"):
27      dir_path = os.path.dirname(os.path.realpath(__file__))
28      file_path = f"{dir_path}/data/session_{session_id}_df.csv.gz"
29  
30      if os.path.exists(file_path):
31          session_df = read_dataframe(file_path)
32      else:
33          influx = Influx()
34          session_df = influx.session_df(session_id, measurement=measurement, bucket=bucket, start="-10y")
35          save_dataframe(session_df, file_path)
36  
37      return process_dataframe(session_df)
38  
39  
40  def get_lap_df(lap_id, measurement="fast_laps", bucket="fast_laps"):
41      dir_path = os.path.dirname(os.path.realpath(__file__))
42      file_path = f"{dir_path}/data/lap_{lap_id}_df.csv.gz"
43  
44      if os.path.exists(file_path):
45          lap_df = read_dataframe(file_path)
46      else:
47          influx = Influx()
48          lap = Lap.objects.get(id=lap_id)
49          laps = influx.telemetry_for_laps([lap], measurement=measurement, bucket=bucket)
50          lap_df = laps[0]
51          save_dataframe(lap_df, file_path)
52  
53      return process_dataframe(lap_df)
54  
55  
56  def read_responses(file_name, pickled=False):
57      dir_path = os.path.dirname(os.path.realpath(__file__))
58      file_path = f"{dir_path}/data/responses_{file_name}.txt"
59      if pickled:
60          with open(file_path, "rb") as f:
61              responses = pickle.load(
62                  f
63              )  # nosec FIXME #312 https://bandit.readthedocs.io/en/latest/blacklists/blacklist_calls.html#b301-pickle
64          return responses
65  
66      with open(file_path, "r") as f:
67          responses = eval(
68              f.read()
69          )  # nosec FIXME #313 https://bandit.readthedocs.io/en/latest/blacklists/blacklist_calls.html#b307-eval
70      return responses
71  
72  
73  def save_responses(responses, file_name, pickled=False):
74      dir_path = os.path.dirname(os.path.realpath(__file__))
75      file_path = f"{dir_path}/data/responses_{file_name}.txt"
76      if pickled:
77          with open(file_path, "wb") as f:
78              pickle.dump(responses, f)
79          return
80  
81      with open(file_path, "w") as f:
82          pprint(responses, stream=f, width=200)