import utils class tax_brackets: """A class to represent tax brackets and some operations on them.""" def __init__(self, brackets): self.brackets = brackets def compute_taxes_for_income(self, income): """Compute the tax bill for income given our brackets.""" taxes_due = 0 while income > 1: (threshold, rate) = self.get_bracket_for_income(income) taxes_due += (income - threshold) * rate income = threshold assert taxes_due >= 0, "Somehow computed negative tax bill?!" return taxes_due def get_bracket_for_income(self, income): """Return the bracket that the last dollar of income was in.""" assert income > 0, "Income should be >0 to be taxed" rate = 0.0 threshold = 0 for bracket in self.brackets: # Bracket is a tuple of (monetary_threshold, # tax_rate). So bracket[0] is a dollar amount and # bracket[1] is a tax rate. e.g. ( 250000, 0.20 ) # indicates that every dollar you earn over 250K is # taxed at 20%. if (income > bracket[0] and rate < bracket[1]): threshold = bracket[0] rate = bracket[1] return (threshold, rate) def get_bracket_above_income(self, income): """Return the next bracket above the one activated by income.""" assert income > 0, "Income should be >0 to be taxed" rate = 1.0 threshold = None # Walk the income tax brackets and look for the one just above # the one this year's income is activating. for bracket in self.brackets: if (bracket[0] > income and bracket[1] < rate): threshold = bracket[0] rate = bracket[1] # Note: this can return (None, 1.0) iff the income is already # in the top tax bracket. return (threshold, rate) def get_bracket_below_income(self, income): """Return the next bracket below the one activated by income.""" assert income > 0, "Income should be >0 to be taxed" bracket = self.get_bracket_for_income(income) income = bracket[0] return self.get_bracket_for_income(income) def get_effective_tax_rate_for_income(self, income): """Compute and return the effective tax rate for an income.""" if income <= 0: return 0 tax_bill = self.compute_taxes_for_income(income) return float(tax_bill) / income def adjust_with_multiplier(self, multiplier): """Every year the IRS adjusts the income tax brackets for inflation.""" for bracket in self.brackets: if bracket[0] != 1: bracket[0] *= multiplier def dump(self): """Print out the tax brackets we're using in here.""" for x in self.brackets: print "{:<20} -> {:<3}".format(utils.format_money(x[0]), utils.format_rate(x[1]))