Timesheet program I wrote for Chris this summer.
By Collin Anderson
from datetime import datetime
import os
def load_people():
lines = [x.strip() for x in file('people.txt') if x.strip()]
data = {}
for line in lines:
print line
parts = line.split(' ')
data[parts[0]] = ' '.join(parts[1:])
return data
people = load_people()
cur_year = datetime.today().year
def get_name():
while 1:
try:
num = raw_input("Type your number and press enter: ")
return people[num]
except KeyboardInterrupt:
raise
except:
print "Invalid number"
def newday():
return datetime.now().strftime("%m/%d %a")
def dateofline(line):
return datetime(cur_year, int(line[0:2]), int(line[3:5])).date().day
def input_time():
while 1:
try:
time = raw_input("Enter the time that you finished working (ie 4:32): ")
hour, minute = (int(x) for x in x.split(':'))
if hour < 7:
hour += 12
time = datetime.now()
time.hour, time.minute, = hour, minute
return time
except:
print "Invalid time."
def time():
return datetime.now().strftime("%H:%M")
def punchin():
print "Punched In"
return 'tI ' + time()
def punchout():
print "Punched Out"
return 'tO ' + time()
def parsetime(string):
return int(string[2:4]), int(string[5:7])
def record_time(name):
filename = "%s.txt" % name
if not os.path.exists(filename):
f = file(filename, 'w')
f.write('%sn' % name)
f.write(newday())
return f.write(punchin())
f = file(filename, 'a')
line = [x.strip() for x in file(filename) if x.strip()][-1]
if datetime.today().day != dateofline(line):
times = line.split('t')[1:]
if len(times) % 2:
print "You did not punch out last time."
time = input_time()
new_time = 'O ' + time.strftime("%H:%M")
f.write('t' + new_time)
times.append(new_time)
times = [parsetime(x) for x in times]
punched_in = -1
total_hours = 0
total_minutes = 0
for x in times:
total_hours += punched_in * x[0]
total_minutes += punched_in * x[1]
punched_in *= -1
total_time = total_hours + total_minutes/60.
f.write('tTotal: %0.2f' % total_time)
f.write('n')
f.write(newday())
return f.write(punchin())
raise NotImplemented
times = line.split('t')[1:]
if times[-1][0] == 'I':
return f.write(punchout())
if times[-1][0] == 'O':
return f.write(punchin())
raise Exception("Invalid data: %s" % times)
while 1:
name = get_name()
print name
record_time(name)
Tags: chris, python, timesheet
This entry was posted on August 25, 2008 at 9:17 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.