Skip to content

Commit

Permalink
Attempted to add user_expressions to do_execute
Browse files Browse the repository at this point in the history
Includes overhaul of stata.run_single error handling
  • Loading branch information
hugetim committed Oct 12, 2024
1 parent 12fc23f commit d960704
Show file tree
Hide file tree
Showing 11 changed files with 829 additions and 427 deletions.
897 changes: 598 additions & 299 deletions nbs/02_stata.ipynb

Large diffs are not rendered by default.

30 changes: 14 additions & 16 deletions nbs/03_stata_more.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@
" if condition:\n",
" self.varname = sfi.SFIToolkit.getTempName()\n",
" cmd = f\"quietly gen {self.varname} = cond({condition},1,0)\"\n",
" run_single(cmd, show_exc_warning=False)\n",
" run_single(cmd)\n",
"\n",
" def clear(self):\n",
" \"\"\"Remove temporary select_var from Stata dataset\"\"\"\n",
Expand Down Expand Up @@ -780,7 +780,7 @@
" def __enter__(self):\n",
" import sfi\n",
" self.idx_var = sfi.SFIToolkit.getTempName()\n",
" run_single(f\"gen {self.idx_var} = _n\", show_exc_warning=False)\n",
" run_single(f\"gen {self.idx_var} = _n\")\n",
" return self.idx_var\n",
" \n",
" def __exit__(self, exc_type, exc_value, exc_tb):\n",
Expand All @@ -799,7 +799,7 @@
"text": [
"var1 __000001\n",
"var1\n",
"Elapsed time: 0.0009 seconds\n"
"Elapsed time: 0.0004 seconds\n"
]
}
],
Expand Down Expand Up @@ -993,7 +993,7 @@
" run_direct_cleaned(_program_define_code, quietly=True)\n",
" run_direct(_program_name, quietly=False, inline=True, echo=False)\n",
" finally:\n",
" run_single(f\"capture program drop {_program_name}\", show_exc_warning=False)"
" run_single(f\"capture program drop {_program_name}\")"
]
},
{
Expand Down Expand Up @@ -1339,8 +1339,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Elapsed time: 0.1576 seconds\n",
"Elapsed time: 0.0796 seconds\n"
"Elapsed time: 0.2008 seconds\n",
"Elapsed time: 0.0915 seconds\n"
]
}
],
Expand All @@ -1363,7 +1363,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Elapsed time: 0.2360 seconds\n"
"Elapsed time: 0.3095 seconds\n"
]
}
],
Expand Down Expand Up @@ -1414,7 +1414,7 @@
" program drop {_program_name}\n",
" \"\"\").strip()\n",
" except Exception as e:\n",
" run_single(f\"capture program drop {_program_name}\", show_exc_warning=True)\n",
" run_sfi(f\"capture program drop {_program_name}\")\n",
" raise(e)\n",
" return [c.strip() for c in var_code.split() if c] if var_code else None"
]
Expand Down Expand Up @@ -1637,8 +1637,7 @@
"#| export\n",
"def local_names():\n",
" run_single(\"\"\"\\\n",
" mata : st_local(\"temp_nbstata_all_locals\", invtokens(st_dir(\"local\", \"macro\", \"*\")'))\"\"\",\n",
" show_exc_warning=False)\n",
" mata : st_local(\"temp_nbstata_all_locals\", invtokens(st_dir(\"local\", \"macro\", \"*\")'))\"\"\")\n",
" out = get_local('temp_nbstata_all_locals')\n",
" set_local('temp_nbstata_all_locals', \"\")\n",
" return out.split()"
Expand Down Expand Up @@ -1672,8 +1671,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Elapsed time: 0.0082 seconds\n",
"Elapsed time: 0.1817 seconds\n"
"Elapsed time: 0.0006 seconds\n",
"Elapsed time: 0.2444 seconds\n"
]
}
],
Expand Down Expand Up @@ -1820,7 +1819,8 @@
" try:\n",
" run_single(f\"local `output': display {input_str}\")\n",
" except SyntaxError as e:\n",
" raise SyntaxError(f\"Invalid Stata '[%fmt] [=]exp' display expression: {input_str}\")\n",
" combined_message = f\"{str(e)}\\nInvalid Stata '[%fmt] [=]exp' display expression: {input_str}\"\n",
" raise SyntaxError(combined_message)\n",
" return get_local(get_local(\"output\"))"
]
},
Expand Down Expand Up @@ -1858,9 +1858,7 @@
"output_type": "stream",
"text": [
"=%y\n",
"%y invalid name\n",
"%9.2f = value\n",
"value not found\n"
"%9.2f = value\n"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion nbs/05_noecho.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
"def _restore_locals_and_clear_sreturn():\n",
" for lname, value in _after_local_dict().items():\n",
" set_local(lname, value)\n",
" run_single(\"sreturn clear\", show_exc_warning=False)"
" run_single(\"sreturn clear\")"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions nbs/07_browse.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@
" program drop {_program_name}\n",
" \"\"\").strip()\n",
" except Exception as e:\n",
" run_single(f\"capture program drop {_program_name}\", show_exc_warning=True)\n",
" run_sfi(f\"capture program drop {_program_name}\")\n",
" raise(e)\n",
" return output.replace(\"\\n> \", \"\") #[c.strip() for c in var_code.split() if c] if var_code else None"
]
Expand All @@ -264,7 +264,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"(U.S. life expectancy, 1900-1940)\n",
"(US life expectancy, 19001940)\n",
"(1 real change made, 1 to missing)\n"
]
}
Expand Down Expand Up @@ -1369,7 +1369,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ec4cb64b80ca428daf26e9ed609c4dd1",
"model_id": "c8c7bee37b5a47f7b526676d2cb4c610",
"version_major": 2,
"version_minor": 0
},
Expand Down
162 changes: 129 additions & 33 deletions nbs/14_kernel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"#| export\n",
"from nbstata.config import Config\n",
"from nbstata.misc_utils import print_red\n",
"from nbstata.stata_more import user_expression\n",
"from nbstata.inspect import get_inspect\n",
"from nbstata.stata_session import StataSession\n",
"from nbstata.completions import CompletionsManager\n",
Expand Down Expand Up @@ -126,6 +127,25 @@
" self.stata_ready = True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def _stata_error_reply(ename, evalue, execution_count=None):\n",
" reply_content = {\n",
" 'status': \"error\",\n",
" \"traceback\": [],\n",
" \"ename\": ename,\n",
" \"evalue\": evalue,\n",
" }\n",
" if execution_count is not None:\n",
" reply_content['execution_count'] = execution_count\n",
" return reply_content"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -170,13 +190,11 @@
"def _handle_stata_import_error(err, silent, execution_count):\n",
" if not silent:\n",
" print_red(f\"ModuleNotFoundError: {_missing_stata_message}\")\n",
" return {\n",
" \"traceback\": [],\n",
" \"ename\": \"ModuleNotFoundError\",\n",
" \"evalue\": _missing_stata_message,\n",
" 'status': \"error\",\n",
" 'execution_count': execution_count,\n",
" }"
" return _stata_error_reply(\n",
" ename = \"ModuleNotFoundError\", \n",
" evalue = _missing_stata_message, \n",
" execution_count = execution_count,\n",
" )"
]
},
{
Expand All @@ -187,25 +205,21 @@
"source": [
"#| export\n",
"def _handle_stata_init_error(err, silent, execution_count):\n",
" reply_content = {\n",
" \"traceback\": [],\n",
" \"ename\": \"Stata init error\",\n",
" \"evalue\": str(err),\n",
" }\n",
" reply_content = _stata_error_reply(\n",
" ename = \"Stata init error\", \n",
" evalue = str(err), \n",
" execution_count = execution_count,\n",
" )\n",
" if not silent:\n",
" print_red(reply_content['evalue'])\n",
" reply_content.update({\n",
" 'status': \"error\",\n",
" 'execution_count': execution_count,\n",
" })\n",
" return reply_content"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Stata code cell execution (and error display)"
"## Stata code execution (and error display)"
]
},
{
Expand Down Expand Up @@ -366,26 +380,108 @@
"outputs": [],
"source": [
"#| export\n",
"def _handle_stata_error(err, silent, execution_count):\n",
" reply_content = {\n",
" \"traceback\": [],\n",
" \"ename\": \"Stata error\",\n",
" \"evalue\": str(err),\n",
" }\n",
"def _handle_stata_error(err, silent=False, execution_count=None):\n",
" reply_content = _stata_error_reply(\n",
" ename = \"Stata error\", \n",
" evalue = str(err), \n",
" execution_count = execution_count,\n",
" )\n",
" if not silent:\n",
" print_stata_error(reply_content['evalue'])\n",
"# self.send_response(\n",
"# self.iopub_socket,\n",
"# \"error\",\n",
"# reply_content,\n",
"# )\n",
" reply_content.update({\n",
" 'status': \"error\",\n",
" 'execution_count': execution_count,\n",
" })\n",
" return reply_content"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def _format_user_obj(user_expr_output):\n",
" return dict(\n",
" status='ok',\n",
" data={'text/plain': user_expr_output},\n",
" metadata={},\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def user_expressions(expressions):\n",
" results = {}\n",
" for key, expr in expressions.items():\n",
" try:\n",
" value = _format_user_obj(user_expression(expr))\n",
" except Exception as err:\n",
" value = _stata_error_reply(\n",
" ename = \"Stata user expression error\",\n",
" evalue = str(err)\n",
" )\n",
" print_red(value['evalue'])\n",
" results[key] = value\n",
" return results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'1': {'status': 'ok', 'data': {'text/plain': '6'}, 'metadata': {}}}"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#| eval: False\n",
"user_expressions({\"1\": \"5 + 1\"})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[31msam not found\n",
"\n",
"Invalid Stata '[%fmt] [=]exp' display expression: sam\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"{'1': {'status': 'error',\n",
" 'traceback': [],\n",
" 'ename': 'Stata user expression error',\n",
" 'evalue': \"sam not found\\n\\nInvalid Stata '[%fmt] [=]exp' display expression: sam\"}}"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#| eval: False\n",
"user_expressions({\"1\": \"sam\"})"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -428,7 +524,7 @@
" 'status': \"ok\",\n",
" 'execution_count': self.execution_count,\n",
" 'payload': [],\n",
" 'user_expressions': {},\n",
" 'user_expressions': user_expressions(user_expressions or {}),\n",
" }"
]
},
Expand Down
6 changes: 4 additions & 2 deletions nbstata/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,14 @@
'nbstata/kernel.py'),
'nbstata.kernel.PyStataKernel.post_do_hook': ( 'kernel.html#pystatakernel.post_do_hook',
'nbstata/kernel.py'),
'nbstata.kernel._format_user_obj': ('kernel.html#_format_user_obj', 'nbstata/kernel.py'),
'nbstata.kernel._handle_stata_error': ('kernel.html#_handle_stata_error', 'nbstata/kernel.py'),
'nbstata.kernel._handle_stata_import_error': ( 'kernel.html#_handle_stata_import_error',
'nbstata/kernel.py'),
'nbstata.kernel._handle_stata_init_error': ('kernel.html#_handle_stata_init_error', 'nbstata/kernel.py'),
'nbstata.kernel.print_stata_error': ('kernel.html#print_stata_error', 'nbstata/kernel.py')},
'nbstata.kernel._stata_error_reply': ('kernel.html#_stata_error_reply', 'nbstata/kernel.py'),
'nbstata.kernel.print_stata_error': ('kernel.html#print_stata_error', 'nbstata/kernel.py'),
'nbstata.kernel.user_expressions': ('kernel.html#user_expressions', 'nbstata/kernel.py')},
'nbstata.magics': { 'nbstata.magics.Frame': ('magics.html#frame', 'nbstata/magics.py'),
'nbstata.magics.Frame.__enter__': ('magics.html#frame.__enter__', 'nbstata/magics.py'),
'nbstata.magics.Frame.__exit__': ('magics.html#frame.__exit__', 'nbstata/magics.py'),
Expand Down Expand Up @@ -217,7 +220,6 @@
'nbstata.stata.macro_expand': ('stata.html#macro_expand', 'nbstata/stata.py'),
'nbstata.stata.obs_count': ('stata.html#obs_count', 'nbstata/stata.py'),
'nbstata.stata.pwd': ('stata.html#pwd', 'nbstata/stata.py'),
'nbstata.stata.resolve_macro': ('stata.html#resolve_macro', 'nbstata/stata.py'),
'nbstata.stata.run_direct': ('stata.html#run_direct', 'nbstata/stata.py'),
'nbstata.stata.run_single': ('stata.html#run_single', 'nbstata/stata.py'),
'nbstata.stata.set_local': ('stata.html#set_local', 'nbstata/stata.py'),
Expand Down
2 changes: 1 addition & 1 deletion nbstata/browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _parse_browse_magic_syntax(code):
program drop {_program_name}
""").strip()
except Exception as e:
run_single(f"capture program drop {_program_name}", show_exc_warning=True)
run_sfi(f"capture program drop {_program_name}")
raise(e)
return output.replace("\n> ", "") #[c.strip() for c in var_code.split() if c] if var_code else None

Expand Down
Loading

0 comments on commit d960704

Please sign in to comment.