Programming Assignment 6 (Time and Fitbit)

In this assignment you will develop a class to encapsulate a "time" object and then use this class in an application that computes statistics on a single day's data in a Fitbit device.

Time.py

class Time():
  def __init__(self, hour, minute, ampm):
    self._hour = hour
    self._minute = minute
    self._ampm = ampm  # midnight will be AM!!; ampm = 'AM' or 'PM'

  # This function returns the time 1 minute after time denoted by self
  def next(self):
    next = Time(0,0,'')
    if self.hour == 11 and self.minute == 59:
      if self.ampm == 'AM':
        h,m,a = 12,0,'PM'
      else:
        h,m,a = 12,0,'AM'
    elif self.hour == 12 and self.minute == 59:
      h,m,a = 1,0,self.ampm
    elif self.minute == 59:
      h,m,a = self.hour+1,0,self.ampm
    else:
      h,m,a = self.hour,self.minute+1,self.ampm
    return Time(h,m,a)

  # This function returns the time 1 minute before time denoted by self
  def previous(self):
    pass

  # This function returns the time nminutes minutes after time denoted by self
  def add(self,nminutes):
    t = Time(self._hour,self._minute,self._ampm)
    for i in range(nminutes):
      t = t.next()  
    return t

  # This function returns the time nminutes before time denoted by self
  def sub(self,nminutes):
    pass

  # This function returns number of minutes that have elapsed since midnight until time denoted by self
  def minutes_since_midnight(self):
    pass

  # This function returns True if time denoted by self is AFTER time denoted by t
  def after(self,t):
    pass

  # This function returns True if time denoted by self is EQUAL TO time denoted by t
  def equals(self,t):
    return ((self._minute == t._minute) and
            (self._hour == t._hour) and
            (self._ampm == t._ampm))

  # This function returns True if time denoted by self is BEFORE time denoted by t
  def before(self,t):
    pass

  # This function returns the number of minutes between time denoted by self and time denoted by t
  def minutes_between(self,t):
    pass

  # This function returns a string representation of the time denoted by self
  def __str__(self):
    def two_spaces(n):
      if n<10:
        return '0'+str(n)
      else:
        return str(n)
    return two_spaces(self._hour)+":"+two_spaces(self._minute)+self._ampm

Fitbit.py

Write a Python program that reads the name of a data file as a command line parameter, opens that file and reads Fitbit data for a particular day stored in the file, and prints the total number of steps taken and the hourly rate at which these steps were taken. A sample input file is shown below and a sample run of the program is also shown with the corresponding output.

We shall assume that the input file is well-formed with exactly 3 pieces of data per line: start time, end time, and number of steps. However, some entries may be incorrect in that the end time is "before" start time. If this is the case, you should print the invalid data and discard the entry from your calculations.

Mac-mini:p6-fitbit raj$ more in.dat
# read from file data about one day 
# format: start_time:end_time:#steps
09.30AM:09.45AM:220
11.45AM:12.23PM:300
11.45AM:10.23AM:302
2.45PM:3.23PM:202
3.45PM:3.53PM:90
5.45PM:5.53PM:80
6.45PM:7.23PM:1000
10.45PM:10.53PM:102

Mac-mini:p6-fitbit raj$ python3 Fitbit.py in.dat 
INVALID data: 11.45AM:10.23AM:302

Total steps:  1994
Hourly steps rate:  781 

Mac-mini:p6-fitbit raj$
Functions in Fitbit.py are described below:
import sys
from Time import *

# This function takes as input a string s of the form "09.45AM"
# and returns a Time object for the time denoted by s
def generate_time(s):
  pass

# This function takes as input fname and returns a list of non-comment 
# lines from the file
def read_data(fname):
  pass

# This function takes a list of non-comment lines from input and 
# returns a list of triples (from,to,nsteps) corresponding to the
# non-comment lines
def extract_data_parts(data):
  pass

# This function takes a list of triples (from,to,nsteps) and
# removes and prints "bad" data - in which from-time is after to-time
def remove_bad_data(data):
  pass

# This function takes a list of triples (from,to,nsteps) and 
# returns total number of steps and total number of walking minutes
# in the data
def compute_stats(data):
  pass

def main():
  total_steps,total_minutes = compute_stats(remove_bad_data(extract_data_parts(read_data(sys.argv[1]))))
  print("\nTotal steps: ",total_steps)
  print("Hourly steps rate: ",int(total_steps/(total_minutes/60)),"\n")

main()

Time.py SUBMIT this.

Fitbit.py SUBMIT this.

Driver.py. This driver program is for you to test Time.py. Please do not change this file.