#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Define a new aggregator function. """ #end_pymotw_header import sqlite3 import collections db_filename = 'todo.db' class Mode(object): def __init__(self): self.counter = collections.Counter() def step(self, value): print 'step(%r)' % value self.counter[value] += 1 def finalize(self): result, count = self.counter.most_common(1)[0] print 'finalize() -> %r (%d times)' % (result, count) return result with sqlite3.connect(db_filename) as conn: conn.create_aggregate('mode', 1, Mode) cursor = conn.cursor() cursor.execute("select mode(deadline) from task where project = 'pymotw'") row = cursor.fetchone() print 'mode(deadline) is:', row[0]