[Tutor] advice on creating and working with a complex data structure

Serdar Tumgoren zstumgoren at gmail.com
Mon Jul 20 18:26:20 CEST 2009


Hi everyone,
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,
filing_type, filing_num
          ['Mike Brown','Rep','C0035820','FL','15','tossup','F3','8183'],
          ['Mike Brown','Rep','C0035820','FL','15','tossup','F3','8149'],
          ['Susan Miller','Dem','C0013802','FL','15','tossup','F3','2180'],
        )

I was leaning toward creating a nested data structure like this:

races={'FL-15':
	     {'status':'tossup',
              'candidates':
                 {'Susan Miller':
                    {'party':'Dem',
                     'cmtes':
                       {'C0013802':
                         {'2180':'F3'},
                     }, # end cmtes
                   }, #end Susan Miller
                  'Mike Brown':
                    {'party':'Rep',
                     'cmtes':
                       {'C0035820':
                         {'8183':'F3',
                          '2810':'F3'},
                        'C0015823':
                         {'3900':'F3X'},
                       }, #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:

FL-15:
  Dem
    Susan Miller
      C0035820
        2180
  Rep
    Mike Brown
      C0035820
          8149
          8183

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.

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.

Regards,
Serdar


More information about the Tutor mailing list