~mrp/time_computer

35d18a64a2dfaef90c5ead991d7f79a3c21d04e3 — Mark Penner 3 years ago 89f85e9
add working parse_line function
2 files changed, 49 insertions(+), 29 deletions(-)

M tc.py
M test_tc.py
M tc.py => tc.py +40 -21
@@ 9,7 9,7 @@
import sys
import datetime

if __name__ == '__main__':
if __name__ == "__main__":
    print("time computer")
    print("version 2020-03")
    print("        ____")


@@ 18,7 18,7 @@ if __name__ == '__main__':
    print("     |   |    |")
    print("      \\   \\  /")
    print("       \\____/")
    

    try:
        FName = sys.argv[1]
    except IndexError:


@@ 28,7 28,7 @@ if __name__ == '__main__':
    except FileNotFoundError:
        print("file not found, exiting")
        quit()
    

    minutes = [[None, 0]]
    weeks = [[None, 0]]
    days = [[None, 0]]


@@ 45,7 45,7 @@ if __name__ == '__main__':
            month = int(infile.read(2))
            day = int(infile.read(3))
            d = datetime.date(year, month, day)
    

            # read job name (random length)
            name = ""
            while True:


@@ 54,37 54,37 @@ if __name__ == '__main__':
                    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]
                    )
                    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()


@@ 94,7 94,7 @@ if __name__ == '__main__':
                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]:


@@ 103,26 103,26 @@ if __name__ == '__main__':
                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
    

    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


@@ 143,14 143,33 @@ if __name__ == '__main__':
            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")

def parse(line):

def parse_line(line):
    """
    parse a line in the input file
    """
    return []
    items = line.split()

    year = int(items[0][0:4])
    month = int(items[0][4:6])
    day = int(items[0][6:8])
    name = items[1]

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

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

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

M test_tc.py => test_tc.py +9 -8
@@ 7,13 7,14 @@ MIT License
mrpenner
"""

import unittest
import datetime

tc = __import__("tc")

class Tests(unittest.TestCase):
    
    def test_parse(self):
        self.assertEqual(tc.parse([]), [], "parse a line in file")
        
if __name__ == '__main__':
    unittest.main()

def test_parse_line():
    assert tc.parse_line("20210331 jobname 08:00 12:00 13:00 17:00") == {
        "date": datetime.date(2021, 3, 31),
        "job": "jobname",
        "hours": 8,
    }