Skip to content

Commit

Permalink
DOC: extended example on usage with functions
Browse files Browse the repository at this point in the history
  • Loading branch information
artste committed May 13, 2023
1 parent 0189abd commit 1075d82
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,37 @@ available: + `__builtins__` : built in python’s functions. +
`_test_cell_` : `%%testcell` wrapped function executed (we can’t avoid
this).

``` python
%%testcell
def my_function(x):
print(aaa) # global variable
return x

try:
my_function(123)
except Exception as e:
print(e)
```

global variable

``` python
%%testcelln
def my_function(x):
print(aaa) # global variable
return x

try:
my_function(123)
except Exception as e:
print(e)
```

name 'aaa' is not defined

As you can see from this last example, `%%testcelln` helps you to
identify that `my_function` refers global variable `aaa`.

**IMPORTANT**: this is *just wrapping your cell* and so it’s still
running on your main kernel. If you modify variables that has been
created outside of this cell (aka: if you have side effects) this will
Expand Down
93 changes: 93 additions & 0 deletions nbs/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,99 @@
"+ `_test_cell_` : `%%testcell` wrapped function executed (we can't avoid this)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```python\n",
"%%testcell\n",
"def my_function(x):\n",
" print(aaa) # global variable\n",
" return x\n",
"\n",
"try:\n",
" my_function(123)\n",
"except Exception as e:\n",
" print(e)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"global variable\n"
]
}
],
"source": [
"%%testcell\n",
"#| echo: false\n",
"def my_function(x):\n",
" print(aaa) # global variable\n",
" return x\n",
"\n",
"try:\n",
" my_function(123)\n",
"except Exception as e:\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```python\n",
"%%testcelln\n",
"def my_function(x):\n",
" print(aaa) # global variable\n",
" return x\n",
"\n",
"try:\n",
" my_function(123)\n",
"except Exception as e:\n",
" print(e)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"name 'aaa' is not defined\n"
]
}
],
"source": [
"%%testcelln\n",
"#| echo: false\n",
"def my_function(x):\n",
" print(aaa) # global variable\n",
" return x\n",
"\n",
"try:\n",
" my_function(123)\n",
"except Exception as e:\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see from this last example, `%%testcelln` helps you to identify that `my_function` refers global variable `aaa`."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 1075d82

Please sign in to comment.