[Tutor] advice on creating and working with a complex data structure
Alan Gauld
alan.gauld at btinternet.com
Mon Jul 20 19:48:14 CEST 2009
"Serdar Tumgoren" <zstumgoren at gmail.com> wrote
> 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
Have you considered using classes? This looks like a fairly
natural fit for race, candidate, committee and maybe report
casses
> I'm trying to pull these records from a database and output them to an
> HTML file in sorted order.
Each class could have a render method that retuirns
an HTML fragment as a string...
> 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:
You could provide support for a sort method that would
do the grouping for yopu...
> 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
> nesting.
One level at a time!
But with classes there would be less nesting and more relatinships
> * Is the best approach to create some type of a Race class that can
> store all these varying data points for each race?
I think you want a lot more than a Race class, see above.
> * Should I be using some type of recursive function to handle all the
> levels of nesting?
Probably not, a simple inter-bject relationship will make traversal
much easier
> And of course, please let me know if there's a simpler approach I'm
> overlooking that would meet my requirements.
I think creating more objects and getting each object to handle
fetching the stuff from its next level down would be simpler
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list