Skip to content

Commit 8d2b9fb

Browse files
committed
taxes: explanatory material, recursive implementation of split_by_bracket
1 parent 1cb1d77 commit 8d2b9fb

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

2013-04/intro_taxes/README.rst

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
==================
3+
The Tax Man Cometh
4+
==================
5+
6+
---------------------------
7+
Or: The Other Inevitability
8+
---------------------------
9+
10+
11+
:Author: Daniel J. Rocco, Ph.D.
12+
13+
14+
Tax season gave me an excuse to look at the ``csv`` module to show how one
15+
could read in CSV data, manipulate it, and write out a CSV file using Python.
16+
17+
Topics covered:
18+
19+
* reading in a CSV file
20+
* creating a lightweight model of a tax bracket using ``namedtuple``
21+
* using ``map`` to call a function on each element of a list
22+
* massaging the string data from a CSV into a more useful numeric format with
23+
Python's ``Decimal`` class
24+
* problem analysis: breaking down the "compute tax" problem into a set of
25+
prerequisites and a list of smaller steps
26+
* review of some of Python's list mechanisms including comprehensions and the
27+
``sum`` function
28+
* writing out a CSV file
29+
30+
31+
Organization
32+
============
33+
34+
``01_csv2.py``
35+
read in a CSV file and print out its rows
36+
37+
``01b_csv2.py``
38+
read in a CSV file and convert its rows to a list
39+
40+
``02_semantically_enhanced.py``
41+
create a lightweight model of a tax bracket using ``namedtuple``; use the
42+
``Bracket._make`` function to convert the rows of the CSV file to Bracket
43+
objects
44+
45+
``brackets.py``
46+
further process the input data by converting strings to ``Decimal`` objects
47+
more suited for mathematical calculations
48+
49+
``03_split_by_bracket.txt``
50+
sketches out the design of our tax calculator, which includes the
51+
prerequisite information needed to perform the calculation and the steps
52+
involved in the calculation itself
53+
54+
``split_by_bracket.py``
55+
implementation of 03 step 1, including some sample calculations
56+
57+
``tax.py``
58+
implementation of the tax calculator, including some sample calculations
59+
60+
``tax_table.py``
61+
uses ``tax.py`` to write out a tax table
62+
63+
``simple_brackets.csv``
64+
the example brackets, which present a simplified progressive tax model.
65+
Changing the values here (e.g. using tax schedule data available on the
66+
web) will update the brackets used by the examples above
67+
68+
69+
Running the Examples
70+
--------------------
71+
72+
I've added support code to the example Python programs above to allow them to
73+
be run standalone. All you will need is a working Python installation; for
74+
example::
75+
76+
$ python tax.py
77+
single $5000: 500.0
78+
single $11000: 1200.0
79+
single $24000: 4200.0
80+
81+
married $7000: 700.0
82+
married $15000: 1500.0
83+
married $19000: 2300.0
84+
married $31000: 4800.0
85+
86+
87+
Recursive implementation of ``split_by_bracket``
88+
------------------------------------------------
89+
90+
Due to popular demand I have added a recursive implementation of
91+
``split_by_bracket`` in ``split_by_bracket_recursive.py`` along with some
92+
example runs.
93+
94+
95+
Resources
96+
=========
97+
98+
* `The Python Tutorial <http://docs.python.org/2/tutorial/>`_
99+
* `The csv module <http://docs.python.org/2/library/csv.html>`_
100+
* `The collections module, home of namedtuple <http://docs.python.org/2/library/collections.html>`_
101+
* `Python's Decimal <http://docs.python.org/2/library/decimal.html>`_
102+
* `Tax Bracket Article <http://en.wikipedia.org/wiki/Tax_bracket>`_; source of the simple brackets used in the demo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from brackets import brackets
2+
3+
4+
def split_by_bracket(earnings, filing_status, _i=0, _already_taxed=0):
5+
bracket = brackets[_i]
6+
bracket_amount = getattr(bracket, filing_status)
7+
8+
if not bracket_amount or earnings <= bracket_amount:
9+
return [(earnings - _already_taxed, bracket.tax_rate)]
10+
else:
11+
return [(bracket_amount - _already_taxed, bracket.tax_rate)] + \
12+
split_by_bracket(earnings, filing_status,
13+
_i=_i + 1, _already_taxed=bracket_amount)
14+
15+
16+
if __name__ == '__main__':
17+
for amount in (5000, 11000, 24000):
18+
print 'single ${}:'.format(amount), \
19+
split_by_bracket(amount, 'single')
20+
21+
print
22+
23+
for amount in (7000, 15000, 19000, 31000):
24+
print 'married ${}:'.format(amount), \
25+
split_by_bracket(amount, 'married_joint')

0 commit comments

Comments
 (0)