mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-27 04:14:30 +02:00
Refactored to use itertools
Found an itertools mechanism that works for iterating through the current and next row in the cursor.
This commit is contained in:
parent
ea17c63cc5
commit
3f2e711a4b
2 changed files with 5 additions and 32 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import click
|
import click
|
||||||
import sqlite_utils
|
import sqlite_utils
|
||||||
from sqlite_utils.utils import iter_pairs
|
import itertools
|
||||||
import json as json_std
|
import json as json_std
|
||||||
import sys
|
import sys
|
||||||
import csv as csv_std
|
import csv as csv_std
|
||||||
|
|
@ -140,11 +140,12 @@ def json(path, sql, nl, arrays):
|
||||||
# We have to iterate two-at-a-time so we can know if we
|
# We have to iterate two-at-a-time so we can know if we
|
||||||
# should output a trailing comma or if we have reached
|
# should output a trailing comma or if we have reached
|
||||||
# the last row.
|
# the last row.
|
||||||
row = None
|
current_iter, next_iter = itertools.tee(cursor, 2)
|
||||||
|
next(next_iter, None)
|
||||||
first = True
|
first = True
|
||||||
headers = [c[0] for c in cursor.description]
|
headers = [c[0] for c in cursor.description]
|
||||||
for row, next_row, is_last in iter_pairs(cursor):
|
for row, next_row in itertools.zip_longest(current_iter, next_iter):
|
||||||
# We now reliably have row and next_row
|
is_last = next_row is None
|
||||||
data = row
|
data = row
|
||||||
if not arrays:
|
if not arrays:
|
||||||
data = dict(zip(headers, row))
|
data = dict(zip(headers, row))
|
||||||
|
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
def iter_pairs(iterator):
|
|
||||||
# Yields (item, next_item, is_last) pairs
|
|
||||||
# Last row is (item, None, True)
|
|
||||||
first = True
|
|
||||||
next_item = None
|
|
||||||
while True:
|
|
||||||
next_done = False
|
|
||||||
if first:
|
|
||||||
item, done = next_with_done(iterator)
|
|
||||||
if done:
|
|
||||||
break
|
|
||||||
next_item, next_done = next_with_done(iterator)
|
|
||||||
first = False
|
|
||||||
else:
|
|
||||||
item = next_item
|
|
||||||
next_item, next_done = next_with_done(iterator)
|
|
||||||
|
|
||||||
yield item, next_item, next_done
|
|
||||||
|
|
||||||
if next_done:
|
|
||||||
break
|
|
||||||
|
|
||||||
|
|
||||||
def next_with_done(iterator):
|
|
||||||
try:
|
|
||||||
return next(iterator), False
|
|
||||||
except StopIteration:
|
|
||||||
return None, True
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue