classGyroscopeDataFile(object): # the file path to read # a dictionary of angular velocities # This dictionary will store mappings between the timestamp # and the angular velocity at that instant. def__init__(self, filepath): self.filepath = filepath self.omega = {}
# read the file and populate the Omega dictionary. defparse(self): with self._getfile_object() as fp: # We validate that the first line of the csv file matches our # expectation. If not, the csv file was probably not compatible # and will error out over the next few lines. firstline = fp.readline().strip() ifnot firstline == "gyro": raise Exception("The first line isn't valid")
# The strip function removed any additional whitespace # (tabs, spaces, newline characters, among others) # that might be stored in the file. for line in fp.readlines(): line = line.strip() parts = line.split(",") # convert strings into numetric type timestamp = int(parts[3]) ox = float(parts[0]) oy = float(parts[1]) oz = float(parts[2]) ''' if timestamp < 100000000: timestamp = timestamp * 10 if timestamp < 100000000: timestamp = timestamp * 10 ''' print("%s: %s, %s, %s" % (timestamp, ox, oy, oz)) self.omega[timestamp] = (ox, oy, oz) return
# return a sorted list of timestamps from small to large num defget_timestamps(self): return sorted(self.omega.keys())