From 687739acdc23f3868a47c998e0569aab81398295 Mon Sep 17 00:00:00 2001 From: tim Date: Thu, 5 Nov 2020 12:44:33 +0200 Subject: [PATCH] Update to accept either high or low amounts, and add some formatting. --- taxcalc.py | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/taxcalc.py b/taxcalc.py index 183b288..c9ad47e 100755 --- a/taxcalc.py +++ b/taxcalc.py @@ -3,49 +3,53 @@ import locale import math +import pprint import re import sys +''' + Rates from https://www.sars.gov.za/Tax-Rates/Income-Tax/Pages/Rates%20of%20Tax%20for%20Individuals.aspx + low is the lowest value for that bracket; rate is the tax percentage applied to that bracket +''' tax_rates = [ dict( rate = 18, - low = 1, + high = 205_900, ), dict( rate = 26, - low = 205_901, + high = 321_600, ), dict( rate = 31, - low = 321_601, + high = 445_100, ), dict( rate = 36, - low = 445_101, + high = 584_200, ), dict( rate = 39, - low = 584_201, + high = 744_800, ), dict( rate = 41, - low = 744_801, + high = 1_577_300, ), dict( rate = 45, - low = 1_577_301, ), ] for r in tax_rates: idx = tax_rates.index(r) - if idx < len(tax_rates) - 1: - high = tax_rates[idx+1].get('low') - r['high'] = high - 1 - else: - r['high'] = False - -def error(e): - if e: - print(e); - print("{0}: Please enter the annual or monthly amount before tax".format(sys.argv[0])) - sys.exit() + if not r.get('high'): + if idx < len(tax_rates) - 1: + high = tax_rates[idx+1].get('low') + r['high'] = high - 1 + if not r.get('low'): + if idx > 0: + low = tax_rates[idx-1].get('high') + r['low'] = low + 1 + else: + r['low'] = 1 +#pprint.pprint(tax_rates) locale.setlocale( locale.LC_MONETARY, 'en_ZA.UTF-8' ) -locale._override_localeconv = {'mon_thousands_sep': ' ', 'mon_decimal_point': ','} +locale._override_localeconv = {'mon_thousands_sep': ' ', 'mon_decimal_point': '.'} currency = locale.localeconv() if len(sys.argv) < 2: @@ -70,7 +74,7 @@ if amount > 100_000: is_annual = True else: is_annual = False - print("Notice: value {} determined to be per month, dividing tax rates by 12".format(amount)) + print("Notice: value {} determined to be per month, dividing tax rates by 12".format(locale.currency(amount, grouping=True))) total_tax = 0 for r in tax_rates: @@ -91,3 +95,10 @@ for r in tax_rates: break print("\nTotal tax on {} is {}".format(locale.currency(amount, grouping=True), locale.currency(total_tax, grouping=True))) + +def error(e): + if e: + print(e); + print("{0}: Please enter the annual or monthly amount before tax".format(sys.argv[0])) + sys.exit() +