mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-28 21:04:13 +02:00
Improved code compilation pattern, closes #472
This commit is contained in:
parent
23ef1d6c20
commit
a46a5e3a9e
3 changed files with 24 additions and 8 deletions
|
|
@ -1359,12 +1359,9 @@ The following example adds a new ``score`` column, then updates it to list a ran
|
||||||
random.seed(10)
|
random.seed(10)
|
||||||
|
|
||||||
def convert(value):
|
def convert(value):
|
||||||
global random
|
|
||||||
return random.random()
|
return random.random()
|
||||||
'
|
'
|
||||||
|
|
||||||
Note the ``global random`` line here. Due to the way the tool compiles Python code, this is necessary to ensure the ``random`` module is available within the ``convert()`` function. If you were to omit this you would see a ``NameError: name 'random' is not defined`` error.
|
|
||||||
|
|
||||||
.. _cli_convert_recipes:
|
.. _cli_convert_recipes:
|
||||||
|
|
||||||
sqlite-utils convert recipes
|
sqlite-utils convert recipes
|
||||||
|
|
|
||||||
|
|
@ -432,12 +432,11 @@ def progressbar(*args, **kwargs):
|
||||||
|
|
||||||
|
|
||||||
def _compile_code(code, imports, variable="value"):
|
def _compile_code(code, imports, variable="value"):
|
||||||
locals = {}
|
|
||||||
globals = {"r": recipes, "recipes": recipes}
|
globals = {"r": recipes, "recipes": recipes}
|
||||||
# If user defined a convert() function, return that
|
# If user defined a convert() function, return that
|
||||||
try:
|
try:
|
||||||
exec(code, globals, locals)
|
exec(code, globals)
|
||||||
return locals["convert"]
|
return globals["convert"]
|
||||||
except (AttributeError, SyntaxError, NameError, KeyError, TypeError):
|
except (AttributeError, SyntaxError, NameError, KeyError, TypeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -464,8 +463,8 @@ def _compile_code(code, imports, variable="value"):
|
||||||
|
|
||||||
for import_ in imports:
|
for import_ in imports:
|
||||||
globals[import_.split(".")[0]] = __import__(import_)
|
globals[import_.split(".")[0]] = __import__(import_)
|
||||||
exec(code_o, globals, locals)
|
exec(code_o, globals)
|
||||||
return locals["fn"]
|
return globals["fn"]
|
||||||
|
|
||||||
|
|
||||||
def chunks(sequence: Iterable, size: int) -> Iterable[Iterable]:
|
def chunks(sequence: Iterable, size: int) -> Iterable[Iterable]:
|
||||||
|
|
|
||||||
|
|
@ -606,3 +606,23 @@ def test_convert_hyphen_workaround(fresh_db_and_path):
|
||||||
assert list(db["names"].rows) == [
|
assert list(db["names"].rows) == [
|
||||||
{"id": 1, "name": "-"},
|
{"id": 1, "name": "-"},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_convert_initialization_pattern(fresh_db_and_path):
|
||||||
|
db, db_path = fresh_db_and_path
|
||||||
|
db["names"].insert_all([{"id": 1, "name": "Cleo"}], pk="id")
|
||||||
|
result = CliRunner().invoke(
|
||||||
|
cli.cli,
|
||||||
|
[
|
||||||
|
"convert",
|
||||||
|
db_path,
|
||||||
|
"names",
|
||||||
|
"name",
|
||||||
|
"-",
|
||||||
|
],
|
||||||
|
input="import random\nrandom.seed(1)\ndef convert(value): return random.randint(0, 100)",
|
||||||
|
)
|
||||||
|
assert 0 == result.exit_code, result.output
|
||||||
|
assert list(db["names"].rows) == [
|
||||||
|
{"id": 1, "name": "17"},
|
||||||
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue