~mrp/time_computer

976c430037b1e86f7ce63d892a6a67c303b68d4b — Mark Penner 3 years ago 36b4edd
finish rewrite of tc.py

the rewrite is intended to make it easier to read and to extend with more
functionality later if desired.
2 files changed, 47 insertions(+), 145 deletions(-)

M tc.py
M test-file
M tc.py => tc.py +35 -133
@@ 100,9 100,37 @@ def total_hours(data):
    return total


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))
    print("\nhours by week")
    for week in output["weekly"].items():
        print(str(week[0]) + ":", round(week[1], 2))
    print("\nhours by job")
    for job in output["job"].items():
        print(job[0] + ":", round(job[1], 2))
    print("")
    for item in output["reg_overtime"].items():
        print(item[0] + ":", round(item[1], 2))
    print("\ntotal hours:", round(output["total"], 2))


if __name__ == "__main__":
    print("time computer")
    print("version 2020-04")
    print("version 0.1.0")
    print("        ____")
    print("       /    \\")
    print("      /  |   \\")


@@ 111,138 139,12 @@ if __name__ == "__main__":
    print("       \\____/")

    try:
        FName = sys.argv[1]
        file_name = sys.argv[1]
    except IndexError:
        FName = input("File Name: ")
    try:
        infile = open(FName, "r")
    except FileNotFoundError:
        print("file not found, exiting")
        quit()

    minutes = [[None, 0]]
    weeks = [[None, 0]]
    days = [[None, 0]]
    dates = []
    totalminutes = 0
    linenum = 1
    # data = []
    # for line in infile:
    # data.append(parse_line(line))

    # print("Total hours:", total_hours(data))
        file_name = input("File Name: ")

    try:

        while True:
            linebuffer = []
            try:
                year = int(infile.read(4))
            except ValueError:
                break
            month = int(infile.read(2))
            day = int(infile.read(3))
            d = datetime.date(year, month, day)

            # read job name (random length)
            name = ""
            while True:
                x = infile.read(1)
                if x == " ":
                    break
                name = name + x
            linebuffer.append(name)

            while True:
                x = int(infile.read(2))
                linebuffer.append(x)
                if infile.read(1) == "\n":
                    break

            # figure hours for one clock-in/out
            x = 0
            timeIndex = 1
            while True:
                try:
                    x += (
                        linebuffer[timeIndex + 2] * 60 + linebuffer[timeIndex + 3]
                    ) - (linebuffer[timeIndex] * 60 + linebuffer[timeIndex + 1])
                except IndexError:
                    break
                timeIndex += 4  # go to next in/out

            # add minutes to right job
            jobIndex = 0
            while (
                linebuffer[0] != minutes[jobIndex][0] and minutes[jobIndex][0]
            ):  # check if current job matches previous
                jobIndex += 1

            if linebuffer[0] != minutes[jobIndex][0]:  # if no match append jobname
                minutes.append([None, 0])
                minutes[jobIndex][0] = linebuffer[0]
            minutes[jobIndex][1] += x

            # add minutes to right week
            weekIndex = 0
            ic = d.isocalendar()
            while ic[1] != weeks[weekIndex][0] and weeks[weekIndex][0]:
                weekIndex += 1
            if ic[1] != weeks[weekIndex][0]:
                weeks.append([None, 0])
                weeks[weekIndex][0] = ic[1]
            weeks[weekIndex][1] += x

            # add minutes to right day
            dayIndex = 0
            while d != days[dayIndex][0] and days[dayIndex][0]:
                dayIndex += 1
            if d != days[dayIndex][0]:
                days.append([None, 0])
                days[dayIndex][0] = d
            days[dayIndex][1] += x

            totalminutes += x
            linenum += 1
    except:
        print("error on line", linenum)
    finally:
        infile.close()

    print("\nhours by day")
    x = 0
    while days[x][0]:
        print(str(days[x][0]) + ":", round(days[x][1] / 60, 2))
        x += 1
    with open(file_name) as infile:
        data = [parse_line(line) for line in infile]

    print("\nhours by job")
    x = 0
    while minutes[x][0]:
        print(minutes[x][0] + ":", round(minutes[x][1] / 60, 2))
        x += 1

    print("\nhours by week")
    stdweek = 40
    regular_time = 0
    overtime = 0
    x = 0
    while weeks[x][0]:
        if weeks[x][1] / 60 <= stdweek:
            print("week", str(weeks[x][0]) + ":", round(weeks[x][1] / 60, 2))
            regular_time += weeks[x][1] / 60
        else:
            print(
                "week",
                str(weeks[x][0]) + ":",
                stdweek,
                "overtime:",
                round(weeks[x][1] / 60 - stdweek, 2),
            )
            regular_time += stdweek
            overtime += weeks[x][1] / 60 - stdweek
        x += 1

    print("\nregular hours:", round(regular_time, 2))
    print("overtime:", round(overtime, 2))

    print("\ntotal hours:", round(totalminutes / 60, 2), "\n")
    standard_week = 40
    time_computer(data, standard_week)

M test-file => test-file +12 -12
@@ 1,12 1,12 @@
20181126 wat 06:47 11:59 12:52 17:03
20181127 watson 06:47 11:55 12:32 17:24
20181128 wat 06:48 12:02 12:35 17:23
20181129 wat 06:47 11:51 12:48 16:44
20181130 wat 06:46 11:51 12:44 17:15
20181203 wat 07:04 12:17
20181204 watson 07:08 11:57 12:32 17:30
20181205 byler 07:03 11:58 12:35 18:25
20181206 byler 09:17 11:57 12:46 20:40
20181207 byler 07:32 11:58 11:59 12:52 17:03 20:40
20181213 byler 07:32 11:58
20181213 watson 12:32 16:58
20181126 job1 06:47 11:59 12:52 17:03
20181127 job2 06:47 11:55 12:32 17:24
20181128 job1 06:48 12:02 12:35 17:23
20181129 job1 06:47 11:51 12:48 16:44
20181130 job1 06:46 11:51 12:44 17:15
20181203 job1 07:04 12:17
20181204 job2 07:08 11:57 12:32 17:30
20181205 job3 07:03 11:58 12:35 18:25
20181206 job3 09:17 11:57 12:46 20:40
20181207 job3 07:32 11:58 11:59 12:52 17:03 20:40
20181213 job3 07:32 11:58
20181213 job2 12:32 16:58