Timesheet program I wrote for Chris this summer.

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:
            num = raw_input("Type your number and press enter: ")
            return people[num]
        except KeyboardInterrupt:
            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:
            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
            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)
        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 = [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)
        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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s