-
Notifications
You must be signed in to change notification settings - Fork 0
/
trackman_output.py
53 lines (44 loc) · 1.85 KB
/
trackman_output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import csv
from pathlib import Path
from trackman_html_constants import data_points
import os
class TrackmanOutput:
def __init__(self, date, report_id, club):
self.date = date
self.file_name = 'out/' + club + '.csv'
# stat headers - e.g. ball speed, club path, etc. Assume all the same for each club for now
self.shots = []
self.headers = ['Date', 'ReportId', 'ShotNum'] + data_points
self.club = club
self.report_id = report_id
def write(self):
file = Path(self.file_name)
if file.is_file():
self.shots = self.combine_existing_rows()
os.remove(self.file_name)
self.write_new_file()
def combine_existing_rows(self):
with open(self.file_name, 'r') as original_file:
reader = csv.DictReader(original_file, fieldnames=self.headers)
next(reader, None) # skip header for existing file
keys = {(self.report_id, self.date, shot['ShotNum']): shot for shot in self.shots}
combined_shots = []
for row in reader:
key = (row['ReportId'], row['Date'], row['ShotNum'])
if key in keys:
row.update(keys[key])
combined_shots.append(row)
del keys[key]
else:
combined_shots.append(row)
combined_shots.extend(keys.values())
return combined_shots
def write_new_file(self):
with open(self.file_name, 'w') as output_file:
writer = csv.DictWriter(output_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL,
fieldnames=self.headers)
writer.writeheader()
for shot in self.shots:
writer.writerow(shot)
def add_shot(self, stats):
self.shots.append(stats)