dataimport.py
branchstable
changeset 7160 923013173031
parent 7159 3bcccd3ab6b6
child 7170 32b5d9d43a7e
equal deleted inserted replaced
7159:3bcccd3ab6b6 7160:923013173031
   143     >>> data = lazytable(ucsvreader(open(filename)))
   143     >>> data = lazytable(ucsvreader(open(filename)))
   144     """
   144     """
   145     header = reader.next()
   145     header = reader.next()
   146     for row in reader:
   146     for row in reader:
   147         yield dict(zip(header, row))
   147         yield dict(zip(header, row))
       
   148 
       
   149 def lazydbtable(cu, table, headers):
       
   150     """return an iterator on rows of a sql table. On each row, fetch columns
       
   151     defined in headers and return values as a dictionary.
       
   152 
       
   153     >>> data = lazydbtable(cu, 'experimentation', ('id', 'nickname', 'gps'))
       
   154     """
       
   155     cu.execute('SELECT %s FROM %s' % (','.join(headers), table,))
       
   156     while True:
       
   157         row = cu.fetchone()
       
   158         if row is None:
       
   159             break
       
   160         yield dict(zip(headers, row))
   148 
   161 
   149 def mk_entity(row, map):
   162 def mk_entity(row, map):
   150     """Return a dict made from sanitized mapped values.
   163     """Return a dict made from sanitized mapped values.
   151 
   164 
   152     ValueError can be raised on unexpected values found in checkers
   165     ValueError can be raised on unexpected values found in checkers