forked from SocratesClub/datascience
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisting26-4.py
More file actions
executable file
·51 lines (41 loc) · 972 Bytes
/
Copy pathlisting26-4.py
File metadata and controls
executable file
·51 lines (41 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
print('Content-type: text/html\n')
import cgitb; cgitb.enable()
import psycopg2
conn = psycopg2.connect('user=foo password=bar dbname=baz')
curs = conn.cursor()
print("""
<html>
<head>
<title>The FooBar Bulletin Board</title>
</head>
<body>
<h1>The FooBar Bulletin Board</h1>
""")
curs.execute('SELECT * FROM messages')
rows = curs.dictfetchall()
toplevel = []
children = {}
for row in rows:
parent_id = row['reply_to']
if parent_id is None:
toplevel.append(row)
else:
children.setdefault(parent_id, []).append(row)
def format(row):
print(row['subject'])
try: kids = children[row['id']]
except KeyError: pass
else:
print('<blockquote>')
for kid in kids:
format(kid)
print('</blockquote>')
print('<p>')
for row in toplevel:
format(row)
print("""
</p>
</body>
</html>
""")