From 736dca6289fd087ba8d810456e4c7f9f49a584e5 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 12 Dec 2020 21:28:43 -0800 Subject: [PATCH] Much improved design for CLI output of analyze-tables --- sqlite_utils/cli.py | 41 +++++++++++++++++++++++++++++++++++- sqlite_utils/db.py | 2 +- tests/test_analyze_tables.py | 39 +++++++++++++++++++++++++++++----- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 02111a9..81454dc 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -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 = { diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 1974860..9f2653c 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -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 diff --git a/tests/test_analyze_tables.py b/tests/test_analyze_tables.py index 5aa8269..17ec966 100644 --- a/tests/test_analyze_tables.py +++ b/tests/test_analyze_tables.py @@ -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() )