mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-17 14:04:11 +02:00
.convert(skip_false) option, refs #527
This commit is contained in:
parent
e4ed372517
commit
455c35b512
2 changed files with 12 additions and 1 deletions
|
|
@ -2618,6 +2618,7 @@ class Table(Queryable):
|
||||||
where: Optional[str] = None,
|
where: Optional[str] = None,
|
||||||
where_args: Optional[Union[Iterable, dict]] = None,
|
where_args: Optional[Union[Iterable, dict]] = None,
|
||||||
show_progress: bool = False,
|
show_progress: bool = False,
|
||||||
|
skip_false: bool = True,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Apply conversion function ``fn`` to every value in the specified columns.
|
Apply conversion function ``fn`` to every value in the specified columns.
|
||||||
|
|
@ -2660,7 +2661,7 @@ class Table(Queryable):
|
||||||
|
|
||||||
def convert_value(v):
|
def convert_value(v):
|
||||||
bar.update(1)
|
bar.update(1)
|
||||||
if not v:
|
if skip_false and not v:
|
||||||
return v
|
return v
|
||||||
return jsonify_if_needed(fn(v))
|
return jsonify_if_needed(fn(v))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,16 @@ def test_convert_where(fresh_db, where, where_args):
|
||||||
assert list(table.rows) == [{"id": 1, "title": "One"}, {"id": 2, "title": "TWO"}]
|
assert list(table.rows) == [{"id": 1, "title": "One"}, {"id": 2, "title": "TWO"}]
|
||||||
|
|
||||||
|
|
||||||
|
def test_convert_skip_false(fresh_db):
|
||||||
|
table = fresh_db["table"]
|
||||||
|
table.insert_all([{"x": 0}, {"x": 1}])
|
||||||
|
assert table.get(1)["x"] == 0
|
||||||
|
assert table.get(2)["x"] == 1
|
||||||
|
table.convert("x", lambda x: x + 1, skip_false=False)
|
||||||
|
assert table.get(1)["x"] == 1
|
||||||
|
assert table.get(2)["x"] == 2
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"drop,expected",
|
"drop,expected",
|
||||||
(
|
(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue