## ~mrp/time_computer

727d4d1ff19d18939de81c83ea9fa8de26f66c76 — Mark Penner 3 years ago
```add job_hours function to calculate hours per job
```
```2 files changed, 24 insertions(+), 0 deletions(-)

M tc.py
M test_tc.py
```
`M tc.py => tc.py +14 -0`
```@@ 61,6 61,20 @@ def weekly_hours(data):
return output

+def job_hours(data):
+    """
+    calculate how many hours worked each job
+    return a dictionary with job names as keys and hours as values
+    """
+    output = {}
+    for day in data:
+        if day["job"] in output:
+            output[day["job"]] += day["hours"]
+        else:
+            output[day["job"]] = 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 +10 -0`
```@@ 38,6 38,16 @@ def test_weekly_hours():
) == {13: 16.380000000000003}

+def test_job_hours():
+    assert tc.job_hours(
+        [
+            {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 9.38},
+            {"date": datetime.date(2021, 3, 31), "job": "jobname", "hours": 7},
+            {"date": datetime.date(2021, 3, 31), "job": "jobname2", "hours": 9.38},
+        ]
+    ) == {"jobname": 16.380000000000003, "jobname2": 9.38}
+
+
def test_total_hours():
assert (
tc.total_hours(

```