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

2009-07-20 Thread Lucas P Melo

Serdar Tumgoren wrote:

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.
You could just create some classes with methods to handle the insertion 
and deletion of those data.

You could also keep a 'dict' to get objects given a string (or int).

I hope it helps.

Regards,
Lucas
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2009-07-20 Thread Alan Gauld


Serdar Tumgoren zstumgo...@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/

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2009-07-20 Thread Serdar Tumgoren
 Have you considered using classes? This looks like a fairly natural fit for
 race, candidate, committee and maybe report casses

Aha, okay, the multiple classes approach makes sense.  But would these
be nested classes, perhaps inheriting attributes from their parent
classes? E.g.:

class Race

class Candidate

class Committee

class Filing
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2009-07-20 Thread bob gailer

Serdar Tumgoren wrote:

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.
I suggest using the ORDER BY SQL Clause rather than trying to sort it in 
Python.


[snip]
--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor