16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 26 ■ PROJECT 7: YOUR OWN BULLETIN BOARD 491<br />

curs = conn.cursor()<br />

print """<br />

<br />

<br />

The FooBar Bulletin Board<br />

<br />

<br />

The FooBar Bulletin Board<br />

"""<br />

curs.execute('SELECT * FROM messages')<br />

rows = curs.dictfetchall()<br />

<strong>to</strong>plevel = []<br />

children = {}<br />

for row in rows:<br />

parent_id = row['reply_<strong>to</strong>']<br />

if parent_id is None:<br />

<strong>to</strong>plevel.append(row)<br />

else:<br />

children.setdefault(parent_id,[]).append(row)<br />

def format(row):<br />

print '%(subject)s' % row<br />

try: kids = children[row['id']]<br />

except KeyError: pass<br />

else:<br />

print ''<br />

for kid in kids:<br />

format(kid)<br />

print ''<br />

print ''<br />

for row in <strong>to</strong>plevel:<br />

format(row)<br />

print """<br />

<br />

<br />

Post message<br />

<br />

<br />

"""

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!