mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-08-01 15:04:11 +02:00
Tests and docs for new analyze_column() parameters, refs #544
This commit is contained in:
parent
9e9d63b989
commit
9f23e68cf0
3 changed files with 58 additions and 8 deletions
|
|
@ -1115,7 +1115,26 @@ You can inspect the database to see the results like this::
|
|||
Analyzing a column
|
||||
==================
|
||||
|
||||
The ``table.analyze_column(column, common_limit=10, value_truncate=None)`` method is used by the :ref:`analyze-tables <cli_analyze_tables>` CLI command. It returns a ``ColumnDetails`` named tuple with the following fields:
|
||||
The ``table.analyze_column(column)`` method is used by the :ref:`analyze-tables <cli_analyze_tables>` CLI command.
|
||||
|
||||
It takes the following arguments and options:
|
||||
|
||||
``column`` - required
|
||||
The name of the column to analyze
|
||||
|
||||
``common_limit``
|
||||
The number of most common values to return. Defaults to 10.
|
||||
|
||||
``value_truncate``
|
||||
If set to an integer, values longer than this will be truncated to this length. Defaults to None.
|
||||
|
||||
``most_common``
|
||||
If set to False, the ``most_common`` field of the returned ``ColumnDetails`` will be set to None. Defaults to True.
|
||||
|
||||
``least_common``
|
||||
If set to False, the ``least_common`` field of the returned ``ColumnDetails`` will be set to None. Defaults to True.
|
||||
|
||||
And returns a ``ColumnDetails`` named tuple with the following fields:
|
||||
|
||||
``table``
|
||||
The name of the table
|
||||
|
|
@ -1141,10 +1160,6 @@ The ``table.analyze_column(column, common_limit=10, value_truncate=None)`` metho
|
|||
``least_common``
|
||||
The ``N`` least common values as a list of ``(value, count)`` tuples`, or ``None`` if the table is entirely distinct or if the number of distinct values is less than N (since they will already have been returned in ``most_common``)
|
||||
|
||||
``N`` defaults to 10, or you can pass a custom ``N`` using the ``common_limit`` parameter.
|
||||
|
||||
You can use the ``value_truncate`` parameter to truncate values in the ``most_common`` and ``least_common`` lists to a specified number of characters.
|
||||
|
||||
.. _python_api_add_column:
|
||||
|
||||
Adding columns
|
||||
|
|
|
|||
|
|
@ -3436,6 +3436,8 @@ class Table(Queryable):
|
|||
:param common_limit: Show this many column values
|
||||
:param value_truncate: Truncate display of common values to this many characters
|
||||
:param total_rows: Optimization - pass the total number of rows in the table to save running a fresh ``count(*)`` query
|
||||
:param most_common: If ``True``, calculate the most common values
|
||||
:param least_common: If ``True``, calculate the least common values
|
||||
"""
|
||||
db = self.db
|
||||
table = self.name
|
||||
|
|
|
|||
|
|
@ -25,10 +25,11 @@ def db_to_analyze(fresh_db):
|
|||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"column,expected",
|
||||
"column,extra_kwargs,expected",
|
||||
[
|
||||
(
|
||||
"id",
|
||||
{},
|
||||
ColumnDetails(
|
||||
table="stuff",
|
||||
column="id",
|
||||
|
|
@ -42,6 +43,7 @@ def db_to_analyze(fresh_db):
|
|||
),
|
||||
(
|
||||
"owner",
|
||||
{},
|
||||
ColumnDetails(
|
||||
table="stuff",
|
||||
column="owner",
|
||||
|
|
@ -55,6 +57,7 @@ def db_to_analyze(fresh_db):
|
|||
),
|
||||
(
|
||||
"size",
|
||||
{},
|
||||
ColumnDetails(
|
||||
table="stuff",
|
||||
column="size",
|
||||
|
|
@ -66,11 +69,41 @@ def db_to_analyze(fresh_db):
|
|||
least_common=None,
|
||||
),
|
||||
),
|
||||
(
|
||||
"owner",
|
||||
{"most_common": False},
|
||||
ColumnDetails(
|
||||
table="stuff",
|
||||
column="owner",
|
||||
total_rows=8,
|
||||
num_null=0,
|
||||
num_blank=0,
|
||||
num_distinct=4,
|
||||
most_common=None,
|
||||
least_common=[("Anne", 1), ("Terry...", 2)],
|
||||
),
|
||||
),
|
||||
(
|
||||
"owner",
|
||||
{"least_common": False},
|
||||
ColumnDetails(
|
||||
table="stuff",
|
||||
column="owner",
|
||||
total_rows=8,
|
||||
num_null=0,
|
||||
num_blank=0,
|
||||
num_distinct=4,
|
||||
most_common=[("Joan", 3), ("Kumar", 2)],
|
||||
least_common=None,
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_analyze_column(db_to_analyze, column, expected):
|
||||
def test_analyze_column(db_to_analyze, column, extra_kwargs, expected):
|
||||
assert (
|
||||
db_to_analyze["stuff"].analyze_column(column, common_limit=2, value_truncate=5)
|
||||
db_to_analyze["stuff"].analyze_column(
|
||||
column, common_limit=2, value_truncate=5, **extra_kwargs
|
||||
)
|
||||
== expected
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue