~mrp/time_computer

57509ecdc990fed92c99c94a91a5f8c00e2f2b71 — Mark Penner 2 years ago ae0e55e
add functionality for figuring mileage
3 files changed, 29 insertions(+), 9 deletions(-)

M tc.py
M test-file
M test_tc.py
M tc.py => tc.py +16 -6
@@ 20,7 20,7 @@ settings = {
def parse_line(line):
    """
    parse a line (as a string) from the input file
    return the date, job name, and hours in a dictionary
    return the date, job name, hours, and miles in a dictionary
    """
    items = line.split()
    year = int(items[0][0:4])


@@ 30,14 30,24 @@ def parse_line(line):
    del items[0:2]

    times = []
    for hhmm in items:
    miles = 0
    for i, hhmm in enumerate(items):
        # miles can be put at the end of a line using syntax "mi 59.5" for 59.5 miles
        if hhmm == "mi":
            miles = float(items[i + 1])
            break
        hh_mm = hhmm.split(":")
        time = float(hh_mm[0]) + (float(hh_mm[1]) / 60)
        time = int(hh_mm[0]) + (int(hh_mm[1]) / 60)
        times.append(time)
    hours = 0
    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}
    return {
        "date": datetime.date(year, month, day),
        "job": name,
        "hours": hours,
        "miles": miles,
    }


def daily_hours(data):


@@ 149,7 159,7 @@ def time_computer(data, settings):
    for name, hours in reg_ot.items():
        print(name + ":", round(hours, 2))
    print("\ntotal hours:", round(total_hours(data), 2))
    print("")
    print("total mileage:", round(sum([x["miles"] for x in data]), 2), "\n")
    if settings["rate"] > 0:
        for name, amount in gross_pay(
            reg_ot["regular"], reg_ot["overtime"], settings["rate"], settings["ot_mult"]


@@ 159,7 169,7 @@ def time_computer(data, settings):

if __name__ == "__main__":
    print("time computer")
    print("version 0.1.1")
    print("version 0.1.2")
    print("        ____")
    print("       /    \\")
    print("      /  |   \\")

M test-file => test-file +3 -3
@@ 1,12 1,12 @@
20181126 job1 06:47 11:59 12:52 17:03
20181127 job2 06:47 11:55 12:32 17:24
20181127 job2 06:47 11:55 12:32 17:24 mi 54.3
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
20181204 job2 07:08 11:57 12:32 17:30 mi 22.5
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
20181213 job2 12:32 16:58 mi 22.1

M test_tc.py => test_tc.py +10 -0
@@ 18,6 18,16 @@ def test_parse_line():
        "date": datetime.date(2021, 3, 31),
        "job": "jobname",
        "hours": 9.383333333333333,
        "miles": 0,
    }


def test_parse_line_with_mileage():
    assert tc.parse_line("20210331 jobname 06:47 11:59 12:52 17:03 mi 59.5") == {
        "date": datetime.date(2021, 3, 31),
        "job": "jobname",
        "hours": 9.383333333333333,
        "miles": 59.5,
    }