From f8e10df00eae209fb0a1ea03384d9153f673a3ec Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 21 Sep 2020 23:39:10 -0700 Subject: [PATCH] Keyword only arguments for transform() Also renamed columns= to types= Closes #165 --- docs/python-api.rst | 4 ++-- sqlite_utils/db.py | 12 +++++++----- tests/test_transform.py | 4 ++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/docs/python-api.rst b/docs/python-api.rst index 026e2e0..ba903a3 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -926,12 +926,12 @@ The ``table.transform()`` method can do all of these things, by implementing a m The ``.transform()`` method takes a number of parameters, all of which are optional. -To alter the type of a column, use the first argument: +To alter the type of a column, use the ``types=`` argument: .. code-block:: python # Convert the 'age' column to an integer, and 'weight' to a float - table.transform({"age": int, "weight": float}) + table.transform(types={"age": int, "weight": float}) The ``rename=`` parameter can rename columns: diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index d8ad05b..df95c3e 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -718,7 +718,8 @@ class Table(Queryable): def transform( self, - columns=None, + *, + types=None, rename=None, drop=None, pk=DEFAULT, @@ -728,7 +729,7 @@ class Table(Queryable): ): assert self.exists(), "Cannot transform a table that doesn't exist yet" sqls = self.transform_sql( - columns=columns, + types=types, rename=rename, drop=None, pk=pk, @@ -754,7 +755,8 @@ class Table(Queryable): def transform_sql( self, - columns=None, + *, + types=None, rename=None, drop=None, pk=DEFAULT, @@ -763,7 +765,7 @@ class Table(Queryable): drop_foreign_keys=None, tmp_suffix=None, ): - columns = columns or {} + types = types or {} rename = rename or {} drop = drop or set() new_table_name = "{}_new_{}".format( @@ -773,7 +775,7 @@ class Table(Queryable): new_column_pairs = [] copy_from_to = {column: column for column, _ in current_column_pairs} for name, type_ in current_column_pairs: - type_ = columns.get(name) or type_ + type_ = types.get(name) or type_ if name in drop: del [copy_from_to[name]] continue diff --git a/tests/test_transform.py b/tests/test_transform.py index c49efe1..f1bb894 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -18,7 +18,7 @@ import pytest ), # Change column type ( - {"columns": {"age": int}}, + {"types": {"age": int}}, [ "CREATE TABLE [dogs_new_suffix] (\n [id] INTEGER PRIMARY KEY,\n [name] TEXT,\n [age] INTEGER\n);", "INSERT INTO [dogs_new_suffix] ([id], [name], [age]) SELECT [id], [name], [age] FROM [dogs]", @@ -48,7 +48,7 @@ import pytest ), # Convert type AND rename column ( - {"columns": {"age": int}, "rename": {"age": "dog_age"}}, + {"types": {"age": int}, "rename": {"age": "dog_age"}}, [ "CREATE TABLE [dogs_new_suffix] (\n [id] INTEGER PRIMARY KEY,\n [name] TEXT,\n [dog_age] INTEGER\n);", "INSERT INTO [dogs_new_suffix] ([id], [name], [dog_age]) SELECT [id], [name], [age] FROM [dogs]",