## ~mrp/time_computer

ae0e55e44221ca243e08d8b411133797c1ce9be7 — Mark Penner 2 years ago
```add function to figure gross pay and effective hourly rate
```
```2 files changed, 72 insertions(+), 25 deletions(-)

M tc.py
M test_tc.py
```
`M tc.py => tc.py +37 -6`
```@@ 1,13 1,20 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
-#  Time Computer version: 2021-03
+#  Time Computer version: 0.1.1
#
#  mrpenner

import sys
import datetime
+from decimal import *
+
+settings = {
+    "standard_week": Decimal(40),
+    "rate": Decimal(0),
+    "ot_mult": Decimal(1.5),
+}

def parse_line(line):

@@ 69,6 76,8 @@ def reg_overtime_hours(weekly, standard_week):
and standard_week an integer of hours when overtime begins
return a dictionary like {"regular": 40, "overtime": 4.5}
"""
+    # only needed until everything is converted to Decimals
+    standard_week = float(standard_week)
output = {"regular": 0, "overtime": 0}
for week in weekly:
if weekly[week] <= standard_week:

@@ 104,7 113,23 @@ def total_hours(data):

-def time_computer(data, standard_week):
+def gross_pay(reg_hrs, ot_hrs, rate, ot_mult):
+    """
+    figure total gross pay and effective hourly rate
+    """
+    output = {}
+    reg_hrs = Decimal(reg_hrs)
+    ot_hrs = Decimal(ot_hrs)
+    output["gross pay"] = Decimal(
+        (reg_hrs * rate) + (ot_hrs * rate * ot_mult)
+    ).quantize(Decimal(".01"), rounding=ROUND_HALF_UP)
+    output["effective rate"] = Decimal(
+        output["gross pay"] / (reg_hrs + ot_hrs)
+    ).quantize(Decimal(".01"), rounding=ROUND_HALF_UP)
+    return output
+
+
+def time_computer(data, settings):
"""
take dictionary of data from each line
calculate and print all the results

@@ 120,14 145,21 @@ def time_computer(data, standard_week):
for job_name, hours in job_hours(data).items():
print(job_name + ":", round(hours, 2))
print("")
-    for name, hours in reg_overtime_hours(weekly, standard_week).items():
+    reg_ot = reg_overtime_hours(weekly, settings["standard_week"])
+    for name, hours in reg_ot.items():
print(name + ":", round(hours, 2))
print("\ntotal hours:", round(total_hours(data), 2))
+    print("")
+    if settings["rate"] > 0:
+        for name, amount in gross_pay(
+            reg_ot["regular"], reg_ot["overtime"], settings["rate"], settings["ot_mult"]
+        ).items():
+            print(name + ": \$", amount)

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

@@ 143,5 175,4 @@ if __name__ == "__main__":
with open(file_name) as infile:
data = [parse_line(line) for line in infile]

-    standard_week = 40
-    time_computer(data, standard_week)
+    time_computer(data, settings)

```
`M test_tc.py => test_tc.py +35 -19`
```@@ 8,6 8,7 @@ mrpenner
"""

import datetime
+from decimal import *

tc = __import__("tc")

@@ 21,21 22,27 @@ 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}
+    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_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}
+    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_reg_overtime_hours():

@@ 46,13 53,16 @@ def test_reg_overtime_hours():

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}
+    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():

@@ 65,3 75,9 @@ def test_total_hours():
)
== 16.380000000000003
)
+
+
+def test_gross_pay():
+    assert tc.gross_pay(
+        Decimal("40"), Decimal("5.5"), Decimal("12.5"), Decimal("1.5")
+    ) == {"gross pay": Decimal("603.13"), "effective rate": Decimal("13.26")}

```