Skip to content

Commit 0b315d3

Browse files
MischaU8simonw
andauthored
progressbar for inserts/upserts of other file formats
* progressbar for inserts/upserts of other file formats, closes #485 * Pin to Python 3.10.6 for the moment as workaround for mypy error Co-authored-by: Simon Willison <swillison@gmail.com>
1 parent d9b9e07 commit 0b315d3

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

.github/workflows/publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ jobs:
99
runs-on: ${{ matrix.os }}
1010
strategy:
1111
matrix:
12-
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
12+
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10.6"]
1313
os: [ubuntu-latest, windows-latest, macos-latest]
1414
steps:
1515
- uses: actions/checkout@v2
1616
- name: Set up Python ${{ matrix.python-version }}
17-
uses: actions/setup-python@v2
17+
uses: actions/setup-python@v4
1818
with:
1919
python-version: ${{ matrix.python-version }}
2020
- uses: actions/cache@v2

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ jobs:
1010
runs-on: ${{ matrix.os }}
1111
strategy:
1212
matrix:
13-
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
13+
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10.6"]
1414
numpy: [0, 1]
1515
os: [ubuntu-latest, macos-latest, windows-latest]
1616
steps:
1717
- uses: actions/checkout@v2
1818
- name: Set up Python ${{ matrix.python-version }}
19-
uses: actions/setup-python@v2
19+
uses: actions/setup-python@v4
2020
with:
2121
python-version: ${{ matrix.python-version }}
2222
- uses: actions/cache@v2

sqlite_utils/cli.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -953,14 +953,16 @@ def insert_upsert_implementation(
953953
decoded = io.TextIOWrapper(file, encoding=encoding)
954954

955955
tracker = None
956-
if csv or tsv:
957-
if sniff:
958-
# Read first 2048 bytes and use that to detect
959-
first_bytes = sniff_buffer.peek(2048)
960-
dialect = csv_std.Sniffer().sniff(first_bytes.decode(encoding, "ignore"))
961-
else:
962-
dialect = "excel-tab" if tsv else "excel"
963-
with file_progress(decoded, silent=silent) as decoded:
956+
with file_progress(decoded, silent=silent) as decoded:
957+
if csv or tsv:
958+
if sniff:
959+
# Read first 2048 bytes and use that to detect
960+
first_bytes = sniff_buffer.peek(2048)
961+
dialect = csv_std.Sniffer().sniff(
962+
first_bytes.decode(encoding, "ignore")
963+
)
964+
else:
965+
dialect = "excel-tab" if tsv else "excel"
964966
csv_reader_args = {"dialect": dialect}
965967
if delimiter:
966968
csv_reader_args["delimiter"] = delimiter
@@ -977,24 +979,24 @@ def insert_upsert_implementation(
977979
if detect_types:
978980
tracker = TypeTracker()
979981
docs = tracker.wrap(docs)
980-
elif lines:
981-
docs = ({"line": line.strip()} for line in decoded)
982-
elif text:
983-
docs = ({"text": decoded.read()},)
984-
else:
985-
try:
986-
if nl:
987-
docs = (json.loads(line) for line in decoded if line.strip())
988-
else:
989-
docs = json.load(decoded)
990-
if isinstance(docs, dict):
991-
docs = [docs]
992-
except json.decoder.JSONDecodeError:
993-
raise click.ClickException(
994-
"Invalid JSON - use --csv for CSV or --tsv for TSV files"
995-
)
996-
if flatten:
997-
docs = (dict(_flatten(doc)) for doc in docs)
982+
elif lines:
983+
docs = ({"line": line.strip()} for line in decoded)
984+
elif text:
985+
docs = ({"text": decoded.read()},)
986+
else:
987+
try:
988+
if nl:
989+
docs = (json.loads(line) for line in decoded if line.strip())
990+
else:
991+
docs = json.load(decoded)
992+
if isinstance(docs, dict):
993+
docs = [docs]
994+
except json.decoder.JSONDecodeError:
995+
raise click.ClickException(
996+
"Invalid JSON - use --csv for CSV or --tsv for TSV files"
997+
)
998+
if flatten:
999+
docs = (dict(_flatten(doc)) for doc in docs)
9981000

9991001
if convert:
10001002
variable = "row"

sqlite_utils/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ def __iter__(self):
155155
self._update(len(line))
156156
yield line
157157

158+
def read(self, size=-1):
159+
data = self._wrapped.read(size)
160+
self._update(len(data))
161+
return data
162+
158163

159164
@contextlib.contextmanager
160165
def file_progress(file, silent=False, **kwargs):

0 commit comments

Comments
 (0)