import sys
import csv
from social.models import *
from django import db

updates = Update.objects.all().order_by('-created_time')
f = open('some-updates.csv', 'w')
writer = csv.writer(f, delimiter=';')

# process updates 50,000 at a time to help with memory consumption

counter = 0
for start in range(0, updates.count(), 50000):
    updates_splice = list(updates[0:50000])
    while updates_splice:
        upd = updates_splice[0]
        cand = upd.feed.candidatefeed.candidate
        args = ["%s %s" % (cand.person.first_name.encode('utf8'), cand.person.last_name.encode('utf8')),
                cand.party_code.encode('utf8'), cand.municipality.name.encode('utf8'),
                cand.person.gender, upd.feed.type, upd.created_time, upd.interest]
        writer.writerow(args)
        db.reset_queries()
        updates_splice = updates_splice[1:]
        counter += 1
        if counter % 1000 == 0:
            print "%d updates written" % counter


@@ -66,16 +66,33 @@
 ort db%0A%0A
+party_dict = %7B%7D%0A%0A
 updates 
@@ -139,16 +139,54 @@
 d_time')
+.filter(created_time__gt='2012-08-01')
 %0Af = ope
@@ -317,16 +317,59 @@
 sumption
+%0Aprint %22total updates %25d%22 %25 updates.count()
 %0A%0Acounte
@@ -880,35 +880,8 @@
 gs)%0A
-        db.reset_queries()%0A
     
@@ -953,78 +953,421 @@
     
-if counter %25 1000 == 0:%0A            print %22%25d updates written%22 %25 counter%0A
+pcd = party_dict.get(cand.party_code, %7B%7D)%0A        pc = pcd.get(upd.feed.type, 0)%0A        pc += 1%0A        pcd%5Bupd.feed.type%5D = pc%0A        party_dict%5Bcand.party_code%5D = pcd%0A        if counter %25 1000 == 0:%0A            print %22%25d updates written%22 %25 counter%0A            db.reset_queries()%0A%0Afor p in party_dict.keys():%0A    pcd = party_dict%5Bp%5D%0A    for f in pcd.keys():%0A        sys.stderr.write(%22%25s,%25s,%25d%5Cn%22 %25 (p, f, pcd%5Bf%5D))
 %0A

Update some-update-exporter.py script to produce aggregate stats