~mrp/time_computer

8443ba1b600ceddef817e5538eb2bc5e07a6ea15 — Mark Penner 3 years ago 976c430
refactoring
3 files changed, 26 insertions(+), 30 deletions(-)

D readme.md
A readme.rst
M tc.py
D readme.md => readme.md +0 -5
@@ 1,5 0,0 @@
Time Computer
=============
This is just a little Python script I wrote to figure the hours I work. The date, job, and
pairs of start time and end time should be entered in a text file. This script takes that
text file and figures hours worked each day, week, job, etc.

A readme.rst => readme.rst +4 -0
@@ 0,0 1,4 @@
Time Computer
==============

This is just a little Python script I wrote to figure the hours I work. The date, job, and pairs of start time and end time should be entered in a text file. This script takes that text file and figures hours worked each day, week, job, etc.

M tc.py => tc.py +22 -25
@@ 11,24 11,25 @@ import datetime


def parse_line(line):
    """ parse a line in the input file """
    """
    parse a line (as a string) from the input file
    return the date, job name, and hours in a dictionary
    """
    items = line.split()
    year = int(items[0][0:4])
    month = int(items[0][4:6])
    day = int(items[0][6:8])
    name = items[1]
    del items[0:2]

    times = []
    for i in range(2, len(items)):
        timehm = items[i].split(":")
        time = float(timehm[0]) + (float(timehm[1]) / 60)
    for hhmm in items:
        hh_mm = hhmm.split(":")
        time = float(hh_mm[0]) + (float(hh_mm[1]) / 60)
        times.append(time)

    hours = 0
    i = 1
    for i in range(1, len(times), 2):
        hours += times[i] - times[i - 1]

    return {"date": datetime.date(year, month, day), "job": name, "hours": hours}




@@ 93,7 94,10 @@ def job_hours(data):


def total_hours(data):
    """ figures total hours worked and returns total hours as a float """
    """
    calculate total hours worked
    return total hours as a float
    """
    total = 0.0
    for item in data:
        total += item["hours"]


@@ 105,27 109,20 @@ def time_computer(data, standard_week):
    take dictionary of data from each line
    calculate and print all the results
    """
    weekly = weekly_hours(data)
    output = {
        "daily": daily_hours(data),
        "weekly": weekly,
        "reg_overtime": reg_overtime_hours(weekly, standard_week),
        "job": job_hours(data),
        "total": total_hours(data),
    }
    print("\nhours by day")
    for day in output["daily"].items():
        print(str(day[0]) + ":", round(day[1], 2))
    for day, hours in daily_hours(data).items():
        print(str(day) + ":", round(hours, 2))
    print("\nhours by week")
    for week in output["weekly"].items():
        print(str(week[0]) + ":", round(week[1], 2))
    weekly = weekly_hours(data)
    for week, hours in weekly.items():
        print(str(week) + ":", round(hours, 2))
    print("\nhours by job")
    for job in output["job"].items():
        print(job[0] + ":", round(job[1], 2))
    for job_name, hours in job_hours(data).items():
        print(job_name + ":", round(hours, 2))
    print("")
    for item in output["reg_overtime"].items():
        print(item[0] + ":", round(item[1], 2))
    print("\ntotal hours:", round(output["total"], 2))
    for name, hours in reg_overtime_hours(weekly, standard_week).items():
        print(name + ":", round(hours, 2))
    print("\ntotal hours:", round(total_hours(data), 2))


if __name__ == "__main__":