From c71184fb444a477dd90143bb07f58a20d87e5a71 Mon Sep 17 00:00:00 2001 From: edoardob90 Date: Sat, 15 Apr 2023 12:08:14 +0200 Subject: [PATCH 1/2] Fix bug of getting test module name from VS Code --- magic_example.ipynb | 11 +++++++++-- tutorial/tests/testsuite.py | 16 +++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/magic_example.ipynb b/magic_example.ipynb index ef577ec8..46bbd797 100644 --- a/magic_example.ipynb +++ b/magic_example.ipynb @@ -26,7 +26,7 @@ " print(\"running\")\n", " return x * 2\n", "\n", - "# result, len([1,2,3,4])" + "len([1,2,3,4])" ] }, { @@ -37,7 +37,7 @@ }, "outputs": [], "source": [ - "%%ipytest\n", + "%%ipytest magic_example\n", "\n", "def solution_power3 (x: int) -> int:\n", " print(\"running\")\n", @@ -47,6 +47,13 @@ " print(\"running\")\n", " return x ** 4" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/tutorial/tests/testsuite.py b/tutorial/tests/testsuite.py index 31422ece..1369623e 100644 --- a/tutorial/tests/testsuite.py +++ b/tutorial/tests/testsuite.py @@ -2,7 +2,7 @@ import pathlib import re from contextlib import redirect_stdout -from typing import Callable +from typing import Callable, Dict import ipynbname import pytest @@ -22,16 +22,14 @@ def _name_from_ipynbname() -> str | None: return None -def _name_from_globals() -> str | None: - module_path = globals().get('__vsc_ipynb_file__') - if module_path: - return pathlib.Path(module_path).stem - return None +def _name_from_globals(globals_dict: Dict) -> str | None: + module_path = globals_dict.get('__vsc_ipynb_file__') if globals_dict else None + return pathlib.Path(module_path).stem if module_path else None -def get_module_name(line: str) -> str: +def get_module_name(line: str, globals_dict: Dict = None) -> str: """Fetch the test module name""" - module_name = _name_from_line(line) or _name_from_ipynbname() or _name_from_globals() + module_name = _name_from_line(line) or _name_from_ipynbname() or _name_from_globals(globals_dict) if not module_name: raise RuntimeError("Test module is undefined. Did you provide an argument to %%ipytest?") @@ -66,7 +64,7 @@ class TestMagic(Magics): def ipytest(self, line: str, cell: str): """The `%%ipytest` cell magic""" # Get the module containing the test(s) - module_name = get_module_name(line) + module_name = get_module_name(line, self.shell.user_global_ns) # Check that the test module file exists module_file = pathlib.Path(f"tutorial/tests/test_{module_name}.py") From cc5cdfebfa30f2c4e8a56a8607e0188066ca5cbe Mon Sep 17 00:00:00 2001 From: Simone Baffelli Date: Tue, 18 Apr 2023 08:31:53 +0200 Subject: [PATCH 2/2] Removed invalid call to "input" in slides. --- slides/input_output_slides.ipynb | 62 +++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/slides/input_output_slides.ipynb b/slides/input_output_slides.ipynb index 670cb577..1090945b 100644 --- a/slides/input_output_slides.ipynb +++ b/slides/input_output_slides.ipynb @@ -87,6 +87,30 @@ "print(f\"x is {x}\")" ] }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Likewise, we can put any *expression* in `{}`:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "print(f\"x+1={x+1}\")" + ] + }, { "cell_type": "markdown", "metadata": { @@ -102,18 +126,19 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, - "outputs": [], "source": [ + "\n", + "```python\n", "print(\"Enter your name\")\n", "name = input()\n", - "print(f\"Hello, {name}\")" + "print(f\"Hello, {name}\")\n", + "```" ] }, { @@ -184,8 +209,8 @@ "outputs": [], "source": [ "#With `/` we can combine paths:\n", - "\n", - "data_dir = (pl.Path(\"./tutorial/tests\") / pl.Path(\"./data\"))\n", + "import pathlib as pl\n", + "data_dir = (pl.Path(\"../tutorial/tests\") / pl.Path(\"./data\"))\n", "\n", "#With the pattern `\"*.csv\"` we look for all files that end in \".csv\"\n", "\n", @@ -227,7 +252,7 @@ }, "outputs": [], "source": [ - "input_file = open(pl.Path(\"./tutorial/tests/data/hello.txt\"))\n" + "input_file = open(pl.Path(\"../tutorial/tests/data/hello.txt\").resolve())\n" ] }, { @@ -270,7 +295,7 @@ }, "outputs": [], "source": [ - "input_file = open(pl.Path(\"./tutorial/tests/data/hello.txt\"))\n", + "input_file = open(pl.Path(\"../tutorial/tests/data/hello.txt\"))\n", "print(input_file.readlines())\n", "input_file.close()\n" ] @@ -309,7 +334,7 @@ "outputs": [], "source": [ "# `w` opens a file for writing\n", - "output_file = open(pl.Path(\"./tutorial/tests/data/me.txt\"), \"w\")\n" + "output_file = open(pl.Path(\"../tutorial/tests/data/me.txt\"), \"w\")\n" ] }, { @@ -360,7 +385,7 @@ }, "outputs": [], "source": [ - "input_file = open(pl.Path(\"./tutorial/tests/data/me.txt\"))\n", + "input_file = open(pl.Path(\"../tutorial/tests/data/me.txt\"))\n", "print(input_file.readlines())\n", "input_file.close()\n" ] @@ -431,9 +456,9 @@ "outputs": [], "source": [ "import pathlib as pl\n", - "with open(pl.Path(\"./tutorial/tests/data/hello.txt\")) as input_file:\n", + "with open(pl.Path(\"../tutorial/tests/data/hello.txt\")) as input_file:\n", " #This is the scope of the context manager.\n", - " #As long as we stay inside of this, we can acess `input_file`\n", + " #As long as we stay inside of this, we can access `input_file`\n", " text = input_file.readlines()" ] }, @@ -472,7 +497,10 @@ }, "outputs": [], "source": [ - "print(input_file.readlines())" + "try:\n", + " print(input_file.readlines())\n", + "except ValueError as e:\n", + " print(e)" ] } ], @@ -483,7 +511,15 @@ "name": "python3" }, "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", "version": "3.11.3" }, "orig_nbformat": 4,