[Tutor] advice on creating and working with a complex data structure
zstumgoren at gmail.com
Mon Jul 20 18:26:20 CEST 2009
I'm working with a database of campaign finance filings, and I'm
trying to create a data structure that captures the fact that:
- there can be multiple races
- races can have multiple candidates
- candidates can have multiple campaign committees
- campaign committees can file multiple reports
I'm trying to pull these records from a database and output them to an
HTML file in sorted order.
Here's an example of the data I'll be pulling:
records_from_db = (
# name, party, cmte_id, state, district, risk_rating,
I was leaning toward creating a nested data structure like this:
}, # end cmtes
}, #end Susan Miller
}, #end cmtes
}, #end Mike Brown
}, #end Candidates
}, #End FL-15
} #end races dict
Once I've created the data object, I'd like to group the output by
race, party, candidate, committee and filing, so that when I output in
my template, it appears like this:
I've been looking at Cookbook examples that rely on the setdefault
method to add key:value pairs to a dictionary, but I wasn't sure how
to implement that approach for a data structure with so many layers of
Based on all of the above, I had a few questions:
* Is the best approach to create some type of a Race class that can
store all these varying data points for each race?
* Should I be using some type of recursive function to handle all the
levels of nesting?
* Given the above data structure, what sorting approach is most appropriate?
And of course, please let me know if there's a simpler approach I'm
overlooking that would meet my requirements.
As always, the advice is appreciated.
More information about the Tutor