Last active
August 29, 2015 14:05
-
-
Save milesrichardson/c015ac71aaadb2112044 to your computer and use it in GitHub Desktop.
Generate logs according to the format specified by Stan Eisenstat at Yale, for CS 223 and CS 323.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
class Log: | |
def __init__(self, hours_estimate=0, file_path=None): | |
self.hours_estimate = hours_estimate | |
self.items = [] | |
self.discussion = "Don't forget a 100+ word discussion!" | |
if file_path is not None: | |
self.parse(file_path) | |
@property | |
def total(self): | |
times = [i[2] for i in self.items] | |
hours = 0 | |
minutes = 0 | |
for time in times: | |
i_hours, i_minutes = time.split(':') | |
hours += int(i_hours) | |
minutes += int(i_minutes) | |
whole_hours = math.floor(minutes/60) + hours | |
extra_minutes = minutes % 60 | |
return '%02d:%02d' % (int(whole_hours), extra_minutes) | |
def new_item(self, date='01/01', start_time='00:00', time_spent='1:20', | |
work_completed='something'): | |
item = (date, start_time, time_spent, work_completed) | |
self.items.append(item) | |
def render_items(self): | |
rtn = '' | |
for item in self.items: | |
assert type(item) is tuple | |
rtn += " ".join(item) | |
rtn +="\n " | |
return rtn | |
def render(self): | |
template = """ ESTIMATE of time to complete assignment: {{est}} hours | |
Start Time | |
Date Time Spent Work completed | |
---- ----- ---- -------------- | |
{{items}} | |
----- | |
{{total}} TOTAL time spent | |
Discussion: {{discussion}} | |
""" | |
template = template.replace('{{est}}', str(self.hours_estimate)) | |
template = template.replace('{{items}}', self.render_items()) | |
template = template.replace('{{total}}', self.total) | |
template = template.replace('{{discussion}}', self.discussion) | |
return template | |
def parse(self, file_path): | |
with open(file_path, 'r') as fp: | |
for line in fp.readlines(): | |
if 'ESTIMATE' in line: | |
self.hours_estimate = line.split(': ')[1].split(' ')[0] | |
if line[0] == ' ' and '/' in line and ':' in line: | |
self.new_item(*line.split(' ')) | |
in_discussion = False | |
if 'Discussion' in line: | |
in_discussion = True | |
self.discussion += line | |
def prompt_for_item(log): | |
new_item = raw_input('Add an item? [y/n]') | |
if new_item == 'n': | |
return log | |
date = raw_input("Date?") | |
start_time = raw_input("Start time?") | |
spent_time = raw_input("Time spent?") | |
work_completed = raw_input("Work completed?") | |
log.new_item(date, start_time, spent_time, work_completed) | |
return log | |
def prompt_for_new_log(): | |
hours_est = raw_input('Hours estimate? Enter a number.') | |
log = Log(hours_estimate=hours_est) | |
log = prompt_for_item(log) | |
return log | |
def prompt_for_existing_log(): | |
log = Log(file_path=file_name) | |
log = prompt_for_item(log) | |
return log | |
if __name__ == '__main__': | |
file_name = raw_input('Enter path of existing file, [Enter] for new log.') | |
if file_name == '': | |
log = prompt_for_new_log() | |
else: | |
log = prompt_for_existing_log() | |
with open('time.log', 'w') as fp: | |
fp.write(log.render()) | |
print 'Wrote to time.log:\n' | |
print log.render() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment