# HG changeset patch # User Sylvain Thénault # Date 1301612801 -7200 # Node ID 923013173031df03cefb9947e2e3ffacd4daf848 # Parent 3bcccd3ab6b6eaf9d25436419a9124b6311fe535 [dataimport] new 'lazydbtable' generator function to feed data from a database table diff -r 3bcccd3ab6b6 -r 923013173031 dataimport.py --- a/dataimport.py Fri Apr 01 01:06:37 2011 +0200 +++ b/dataimport.py Fri Apr 01 01:06:41 2011 +0200 @@ -146,6 +146,19 @@ for row in reader: yield dict(zip(header, row)) +def lazydbtable(cu, table, headers): + """return an iterator on rows of a sql table. On each row, fetch columns + defined in headers and return values as a dictionary. + + >>> data = lazydbtable(cu, 'experimentation', ('id', 'nickname', 'gps')) + """ + cu.execute('SELECT %s FROM %s' % (','.join(headers), table,)) + while True: + row = cu.fetchone() + if row is None: + break + yield dict(zip(headers, row)) + def mk_entity(row, map): """Return a dict made from sanitized mapped values.