~mrp/time_computer

51636518d6a82096c5bcfe9d45b4bc3f183a9214 — Mark Penner 3 years ago 093b00b
add weekly_hours function to calculate weekly hours
2 files changed, 24 insertions(+), 0 deletions(-)

M tc.py
M test_tc.py
M tc.py => tc.py +15 -0
@@ 46,6 46,21 @@ def daily_hours(data):
    return output


def weekly_hours(data):
    """
    calculate how many hours worked each week
    return a dictionary with ISO week numbers as keys and hours as values
    """
    output = {}
    for day in data:
        week = day["date"].isocalendar()[1]
        if week in output:
            output[week] += day["hours"]
        else:
            output[week] = day["hours"]
    return output


def total_hours(data):
    """ figures total hours worked and returns total hours as a float """
    total = 0.0

M test_tc.py => test_tc.py +9 -0
@@ 29,6 29,15 @@ def test_daily_hours():
    ) == {datetime.date(2021, 3, 31): 16.380000000000003}


def test_weekly_hours():
    assert tc.weekly_hours(
        [
            {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 9.38},
            {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 7},
        ]
    ) == {13: 16.380000000000003}


def test_total_hours():
    assert (
        tc.total_hours(