mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-25 18:34:32 +02:00
Much improved design for CLI output of analyze-tables
This commit is contained in:
parent
955daaaf05
commit
736dca6289
3 changed files with 75 additions and 7 deletions
|
|
@ -7,6 +7,7 @@ import hashlib
|
|||
import pathlib
|
||||
import sqlite_utils
|
||||
from sqlite_utils.db import AlterError
|
||||
import textwrap
|
||||
import itertools
|
||||
import json
|
||||
import os
|
||||
|
|
@ -1399,8 +1400,46 @@ def analyze_tables(
|
|||
db["_analyze_tables_"].insert(
|
||||
column_details._asdict(), pk=("table", "column"), replace=True
|
||||
)
|
||||
most_common_rendered = _render_common(
|
||||
"\n\n Most common:", column_details.most_common
|
||||
)
|
||||
least_common_rendered = _render_common(
|
||||
"\n\n Least common:", column_details.least_common
|
||||
)
|
||||
details = (
|
||||
(
|
||||
textwrap.dedent(
|
||||
"""
|
||||
{table}.{column}: ({i}/{total})
|
||||
|
||||
click.echo("{}/{}: {}".format(i + 1, len(todo), column_details))
|
||||
Total rows: {total_rows}
|
||||
Null rows: {num_null}
|
||||
Blank rows: {num_blank}
|
||||
|
||||
Distinct values: {num_distinct}{most_common_rendered}{least_common_rendered}
|
||||
"""
|
||||
)
|
||||
.strip()
|
||||
.format(
|
||||
i=i + 1,
|
||||
total=len(todo),
|
||||
most_common_rendered=most_common_rendered,
|
||||
least_common_rendered=least_common_rendered,
|
||||
**column_details._asdict()
|
||||
)
|
||||
)
|
||||
+ "\n"
|
||||
)
|
||||
click.echo(details)
|
||||
|
||||
|
||||
def _render_common(title, values):
|
||||
if values is None:
|
||||
return ""
|
||||
lines = [title]
|
||||
for value, count in values:
|
||||
lines.append(" {}: {}".format(value, count))
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
FILE_COLUMNS = {
|
||||
|
|
|
|||
|
|
@ -1944,7 +1944,7 @@ class Table(Queryable):
|
|||
return self
|
||||
|
||||
def analyze_column(
|
||||
self, column, common_limit=10, value_truncate=100, total_rows=None
|
||||
self, column, common_limit=10, value_truncate=80, total_rows=None
|
||||
):
|
||||
db = self.db
|
||||
table = self.name
|
||||
|
|
|
|||
|
|
@ -89,12 +89,41 @@ def test_analyze_table(db_to_analyze_path):
|
|||
result = CliRunner().invoke(cli.cli, ["analyze-tables", db_to_analyze_path])
|
||||
assert (
|
||||
result.output.strip()
|
||||
== textwrap.dedent(
|
||||
== (
|
||||
"""
|
||||
1/3: ColumnDetails(table='stuff', column='id', total_rows=8, num_null=0, num_blank=0, num_distinct=8, most_common=None, least_common=None)
|
||||
2/3: ColumnDetails(table='stuff', column='owner', total_rows=8, num_null=0, num_blank=0, num_distinct=4, most_common=[('Joan', 3), ('Terryterryterry', 2), ('Kumar', 2), ('Anne', 1)], least_common=None)
|
||||
3/3: ColumnDetails(table='stuff', column='size', total_rows=8, num_null=0, num_blank=0, num_distinct=2, most_common=[(5, 5), (4, 3)], least_common=None)
|
||||
"""
|
||||
stuff.id: (1/3)
|
||||
|
||||
Total rows: 8
|
||||
Null rows: 0
|
||||
Blank rows: 0
|
||||
|
||||
Distinct values: 8
|
||||
|
||||
stuff.owner: (2/3)
|
||||
|
||||
Total rows: 8
|
||||
Null rows: 0
|
||||
Blank rows: 0
|
||||
|
||||
Distinct values: 4
|
||||
|
||||
Most common:
|
||||
Joan: 3
|
||||
Terryterryterry: 2
|
||||
Kumar: 2
|
||||
Anne: 1
|
||||
|
||||
stuff.size: (3/3)
|
||||
|
||||
Total rows: 8
|
||||
Null rows: 0
|
||||
Blank rows: 0
|
||||
|
||||
Distinct values: 2
|
||||
|
||||
Most common:
|
||||
5: 5
|
||||
4: 3"""
|
||||
).strip()
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue