From ae0e55e44221ca243e08d8b411133797c1ce9be7 Mon Sep 17 00:00:00 2001 From: Mark Penner Date: Thu, 2 Dec 2021 22:20:47 -0600 Subject: [PATCH] add function to figure gross pay and effective hourly rate --- tc.py | 43 +++++++++++++++++++++++++++++++++++++------ test_tc.py | 54 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 72 insertions(+), 25 deletions(-) diff --git a/tc.py b/tc.py index 05b181e..714931a 100644 --- a/tc.py +++ b/tc.py @@ -1,13 +1,20 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # -# Time Computer version: 2021-03 +# Time Computer version: 0.1.1 # # MIT License # 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): return total -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) diff --git a/test_tc.py b/test_tc.py index 9b309ea..3e9ba29 100644 --- a/test_tc.py +++ b/test_tc.py @@ -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")} -- 2.43.4