## ~mrp/time_computer

093b00be947a455cb7801955ed05a25cee58f33f — Mark Penner 3 years ago
```add daily_hours function to calculate hours worked each day
```
```2 files changed, 24 insertions(+), 0 deletions(-)

M tc.py
M test_tc.py
```
`M tc.py => tc.py +15 -0`
```@@ 32,7 32,22 @@ def parse_line(line):
return {"date": datetime.date(year, month, day), "job": name, "hours": hours}

+def daily_hours(data):
+    """
+    calculate how many hours worked each day
+    return a dictionary with dates as keys and hours as values
+    """
+    output = {}
+    for day in data:
+        if day["date"] in output:
+            output[day["date"]] += day["hours"]
+        else:
+            output[day["date"]] = day["hours"]
+    return output
+
+
def total_hours(data):
+    """ figures total hours worked and returns total hours as a float """
total = 0.0
for item in data:
total += item["hours"]

```
`M test_tc.py => test_tc.py +9 -0`
```@@ 20,6 20,15 @@ def test_parse_line():
}

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

```