Improved code compilation pattern, closes #472

This commit is contained in:
Simon Willison 2022-08-26 22:20:09 -07:00
commit a46a5e3a9e
3 changed files with 24 additions and 8 deletions

View file

@ -1359,12 +1359,9 @@ The following example adds a new ``score`` column, then updates it to list a ran
random.seed(10)
def convert(value):
global 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:
sqlite-utils convert recipes

View file

@ -432,12 +432,11 @@ def progressbar(*args, **kwargs):
def _compile_code(code, imports, variable="value"):
locals = {}
globals = {"r": recipes, "recipes": recipes}
# If user defined a convert() function, return that
try:
exec(code, globals, locals)
return locals["convert"]
exec(code, globals)
return globals["convert"]
except (AttributeError, SyntaxError, NameError, KeyError, TypeError):
pass
@ -464,8 +463,8 @@ def _compile_code(code, imports, variable="value"):
for import_ in imports:
globals[import_.split(".")[0]] = __import__(import_)
exec(code_o, globals, locals)
return locals["fn"]
exec(code_o, globals)
return globals["fn"]
def chunks(sequence: Iterable, size: int) -> Iterable[Iterable]:

View file

@ -606,3 +606,23 @@ def test_convert_hyphen_workaround(fresh_db_and_path):
assert list(db["names"].rows) == [
{"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"},
]