{"html_url": "https://github.com/simonw/sqlite-utils/issues/371#issuecomment-1008246239", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/371", "id": 1008246239, "node_id": "IC_kwDOCGYnMM48GJ3f", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-01-09T07:41:24Z", "updated_at": "2022-01-09T07:41:24Z", "author_association": "OWNER", "body": "Might be a case of modifying this line: https://github.com/simonw/sqlite-utils/blob/e0c476bc380744680c8b7675c24fb0e9f5ec6dcd/sqlite_utils/cli.py#L828\r\n\r\nTo:\r\n```python\r\ndocs = (fn(doc) or doc for doc in docs)\r\n```", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1097128334, "label": "Support mutating row in `--convert` without returning it"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/371#issuecomment-1008246366", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/371", "id": 1008246366, "node_id": "IC_kwDOCGYnMM48GJ5e", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-01-09T07:42:14Z", "updated_at": "2022-01-09T07:42:14Z", "author_association": "OWNER", "body": "Also need to update relevant docs for that example.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1097128334, "label": "Support mutating row in `--convert` without returning it"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/371#issuecomment-1008347768", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/371", "id": 1008347768, "node_id": "IC_kwDOCGYnMM48Gip4", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-01-09T18:12:30Z", "updated_at": "2022-01-09T18:12:30Z", "author_association": "OWNER", "body": "Tried this test:\r\n\r\n```python\r\n result = CliRunner().invoke(\r\n cli.cli,\r\n [\r\n \"insert\",\r\n db_path,\r\n \"rows\",\r\n \"-\",\r\n \"--convert\",\r\n 'row[\"is_chicken\"] = True',\r\n ],\r\n input='{\"name\": \"Azi\"}',\r\n )\r\n```\r\nAnd got this error:\r\n\r\n> `E + where 1 = ', 2, 30, ' return row[\"is_chicken\"] = True\\n'))>.exit_code`\r\n\r\nThe code snippet compilation isn't currently compatible with this.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1097128334, "label": "Support mutating row in `--convert` without returning it"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/371#issuecomment-1008348032", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/371", "id": 1008348032, "node_id": "IC_kwDOCGYnMM48GiuA", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-01-09T18:14:02Z", "updated_at": "2022-01-09T18:14:02Z", "author_association": "OWNER", "body": "Here's the code in question: https://github.com/simonw/sqlite-utils/blob/b8c134059e89f0fa040b84fb7d0bda25b9a52759/sqlite_utils/utils.py#L288-L299", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1097128334, "label": "Support mutating row in `--convert` without returning it"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/371#issuecomment-1008354207", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/371", "id": 1008354207, "node_id": "IC_kwDOCGYnMM48GkOf", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-01-09T18:54:54Z", "updated_at": "2022-01-09T18:54:54Z", "author_association": "OWNER", "body": "This seems to work:\r\n```python\r\ndef _compile_code(code, imports, variable=\"value\"):\r\n locals = {}\r\n globals = {\"r\": recipes, \"recipes\": recipes}\r\n # If user defined a convert() function, return that\r\n try:\r\n exec(code, globals, locals)\r\n return locals[\"convert\"]\r\n except (AttributeError, SyntaxError, NameError, KeyError, TypeError):\r\n pass\r\n\r\n # Try compiling their code as a function instead\r\n body_variants = [code]\r\n # If single line and no 'return', try adding the return\r\n if \"\\n\" not in code and not code.strip().startswith(\"return \"):\r\n body_variants.insert(0, \"return {}\".format(code))\r\n\r\n for variant in body_variants:\r\n new_code = [\"def fn({}):\".format(variable)]\r\n for line in variant.split(\"\\n\"):\r\n new_code.append(\" {}\".format(line))\r\n try:\r\n code_o = compile(\"\\n\".join(new_code), \"\", \"exec\")\r\n break\r\n except SyntaxError:\r\n # Try another variant, e.g. for 'return row[\"column\"] = 1'\r\n continue\r\n\r\n for import_ in imports:\r\n globals[import_.split(\".\")[0]] = __import__(import_)\r\n exec(code_o, globals, locals)\r\n return locals[\"fn\"]\r\n```", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1097128334, "label": "Support mutating row in `--convert` without returning it"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/sqlite-utils/issues/371#issuecomment-1008364701", "issue_url": "https://api.github.com/repos/simonw/sqlite-utils/issues/371", "id": 1008364701, "node_id": "IC_kwDOCGYnMM48Gmyd", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-01-09T20:04:35Z", "updated_at": "2022-01-09T20:04:35Z", "author_association": "OWNER", "body": "The previous code for highlighting errors in syntax (which was already a bit confused thanks to the added `return`, see https://github.com/simonw/sqlite-utils/issues/355#issuecomment-991393684 - isn't compatible with this approach at all. I'm going to ditch it and just show a generic `Error: Could not compile code` message.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1097128334, "label": "Support mutating row in `--convert` without returning it"}, "performed_via_github_app": null}