# testcell
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
**TL;DR**: `%%testcell` prevents your testing cells from affecting the
global namespace.
The Python cell magic `%%testcell` executes a cell without *polluting*
the notebook’s global namespace. This is useful whenever you want to
test your code without having any of the local variables escape that
cell.
What’s happening under the hood is that your cell code, before being
executed, is wrapped in a temporary function that will be deleted after
execution. To give you the feeling of *seamless integration* the last
statement is optionally returned like it happens in a normal cell.
**WARNING:** this don’t protect you from *the side effects of your code*
like deleting a file or mutating the state of a global variable.
<div>
[](https://colab.research.google.com/github/artste/testcell/blob/main/demo/testcell_demo.ipynb)
</div>
<div>
[](https://www.kaggle.com/artste/introducing-testcell)
</div>
## Install
``` sh
pip install testcell
```
## How to use
just import it with `import testcell` and then use the `%%testcell` cell
magic.
``` python
%%testcell
a = "'a' is not polluting global scope"
a
```
"'a' is not polluting global scope"
``` python
assert 'a' not in locals()
```
What is happening under the hood is that `%%testcell` wraps your cell’s
code with a function, execute it and then deletes it. Adding the
`verbose` keywork will print which code will be executed.
NOTE: The actual cell code is enclosed within `BEGIN` and `END` comment
blocks for improved readability.
``` python
%%testcell verbose
a = "'a' is not polluting global scope"
a
```
### BEGIN
def _test_cell_():
#| echo: false
a = "'a' is not polluting global scope"
return a # %%testcell
try:
_ = _test_cell_()
finally:
del _test_cell_
_ # This will be added to global scope
### END
"'a' is not polluting global scope"
If you’re just interested in seeing what will be executed, but actually
not executing it, you ca use `dryrun` option:
``` python
%%testcell dryrun
a = "'a' is not polluting global scope"
a
```
### BEGIN
def _test_cell_():
#| echo: false
a = "'a' is not polluting global scope"
return a # %%testcell
try:
_ = _test_cell_()
finally:
del _test_cell_
if _ is not None: display(_)
### END
If you add a semicolon `;` at the end of your last statement no `return`
statement is added and nothing is displayed like a normal jupyter cell.
``` python
%%testcell verbose
a = "'a' is not polluting global scope"
a;
```
### BEGIN
def _test_cell_():
#| echo: false
a = "'a' is not polluting global scope"
a;
try:
_ = _test_cell_()
finally:
del _test_cell_
_ # This will be added to global scope
### END
`testcell` works seamlessly with existing `print` or `display`statements
on last line:
``` python
%%testcell verbose
a = "'a' is not polluting global scope"
print(a)
```
### BEGIN
def _test_cell_():
#| echo: false
a = "'a' is not polluting global scope"
return print(a) # %%testcell
try:
_ = _test_cell_()
finally:
del _test_cell_
_ # This will be added to global scope
### END
'a' is not polluting global scope
Moreover, thanks to `ast`, it properly deals with complex situations
like comments on the last line and multi lines statements
``` python
%%testcell verbose
a = "'a' is not polluting global scope"
(a,
True)
# this is a comment on last line
```
### BEGIN
def _test_cell_():
#| echo: false
a = "'a' is not polluting global scope"
return (a,
True) # %%testcell
try:
_ = _test_cell_()
finally:
del _test_cell_
_ # This will be added to global scope
### END
("'a' is not polluting global scope", True)
### Run in isolation
`%%testcelln` is a shortcut for `%%testcell noglobals` and executes the
cell in complete isolation from the global scope. This is very useful
when you want to ensure that global variables or namespaces are not
accessible within the cell.
``` python
aaa = 'global variable'
```
``` python
%%testcell
'aaa' in globals()
```
True
``` python
%%testcell noglobals
'aaa' in globals()
```
False
``` python
%%testcelln
'aaa' in globals()
```
False
``` python
%%testcelln
globals().keys()
```
dict_keys(['__builtins__'])
With `%%testcelln` inside the cell, you’ll be able to access only to
`__builtins__` (aka: standard python’s functions). **It behaves like a
notebook-in-notebook**.
``` 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
not protect you.
``` python
aaa
```
'global variable'
``` python
%%testcell
# WARNING: this will alter the state of global variable:
globals().update({'aaa' : 'modified global variable'});
```
``` python
aaa
```
'modified global variable'
``` python
del aaa
```
## Links:
- PROJECT PAGE: <https://github.com/artste/testcell>
- DOCUMENTATION: <https://artste.github.io/testcell>
- PYPI: <https://pypi.org/project/testcell>
- COLAB DEMO:
[testcell_demo.ipynb](https://colab.research.google.com/github/artste/testcell/blob/main/demo/testcell_demo.ipynb)
- KAGGLE SAMPLE NOTEBOOK:
<https://www.kaggle.com/artste/introducing-testcell>
## Todo:
- Install as a plugin to enable it by default like other cell’s magic.
Raw data
{
"_id": null,
"home_page": "https://github.com/artste/testcell",
"name": "testcell",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "nbdev jupyter notebook python",
"author": "Stefano Giomo",
"author_email": "artste@users.noreply.github.com",
"download_url": "https://files.pythonhosted.org/packages/7b/1c/e10bf7a72fed99d311332c23c927af13075598841fae8cf42f70069e183e/testcell-0.0.6.tar.gz",
"platform": null,
"description": "# testcell\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\n**TL;DR**: `%%testcell` prevents your testing cells from affecting the\nglobal namespace.\n\nThe Python cell magic `%%testcell` executes a cell without *polluting*\nthe notebook\u2019s global namespace. This is useful whenever you want to\ntest your code without having any of the local variables escape that\ncell.\n\nWhat\u2019s happening under the hood is that your cell code, before being\nexecuted, is wrapped in a temporary function that will be deleted after\nexecution. To give you the feeling of *seamless integration* the last\nstatement is optionally returned like it happens in a normal cell.\n\n**WARNING:** this don\u2019t protect you from *the side effects of your code*\nlike deleting a file or mutating the state of a global variable.\n\n<div>\n\n[](https://colab.research.google.com/github/artste/testcell/blob/main/demo/testcell_demo.ipynb)\n\n</div>\n\n<div>\n\n[](https://www.kaggle.com/artste/introducing-testcell)\n\n</div>\n\n## Install\n\n``` sh\npip install testcell\n```\n\n## How to use\n\njust import it with `import testcell` and then use the `%%testcell` cell\nmagic.\n\n``` python\n%%testcell\na = \"'a' is not polluting global scope\"\na\n```\n\n \"'a' is not polluting global scope\"\n\n``` python\nassert 'a' not in locals()\n```\n\nWhat is happening under the hood is that `%%testcell` wraps your cell\u2019s\ncode with a function, execute it and then deletes it. Adding the\n`verbose` keywork will print which code will be executed.\n\nNOTE: The actual cell code is enclosed within `BEGIN` and `END` comment\nblocks for improved readability.\n\n``` python\n%%testcell verbose\na = \"'a' is not polluting global scope\"\na\n```\n\n\n ### BEGIN\n def _test_cell_():\n #| echo: false\n a = \"'a' is not polluting global scope\"\n return a # %%testcell\n try:\n _ = _test_cell_()\n finally:\n del _test_cell_\n _ # This will be added to global scope\n ### END\n\n \"'a' is not polluting global scope\"\n\nIf you\u2019re just interested in seeing what will be executed, but actually\nnot executing it, you ca use `dryrun` option:\n\n``` python\n%%testcell dryrun\na = \"'a' is not polluting global scope\"\na\n```\n\n\n ### BEGIN\n def _test_cell_():\n #| echo: false\n a = \"'a' is not polluting global scope\"\n return a # %%testcell\n try:\n _ = _test_cell_()\n finally:\n del _test_cell_\n if _ is not None: display(_)\n ### END\n\nIf you add a semicolon `;` at the end of your last statement no `return`\nstatement is added and nothing is displayed like a normal jupyter cell.\n\n``` python\n%%testcell verbose\na = \"'a' is not polluting global scope\"\na;\n```\n\n\n ### BEGIN\n def _test_cell_():\n #| echo: false\n a = \"'a' is not polluting global scope\"\n a;\n try:\n _ = _test_cell_()\n finally:\n del _test_cell_\n _ # This will be added to global scope\n ### END\n\n`testcell` works seamlessly with existing `print` or `display`statements\non last line:\n\n``` python\n%%testcell verbose\na = \"'a' is not polluting global scope\"\nprint(a)\n```\n\n\n ### BEGIN\n def _test_cell_():\n #| echo: false\n a = \"'a' is not polluting global scope\"\n return print(a) # %%testcell\n try:\n _ = _test_cell_()\n finally:\n del _test_cell_\n _ # This will be added to global scope\n ### END\n 'a' is not polluting global scope\n\nMoreover, thanks to `ast`, it properly deals with complex situations\nlike comments on the last line and multi lines statements\n\n``` python\n%%testcell verbose\na = \"'a' is not polluting global scope\"\n(a,\n True)\n# this is a comment on last line\n```\n\n\n ### BEGIN\n def _test_cell_():\n #| echo: false\n a = \"'a' is not polluting global scope\"\n return (a,\n True) # %%testcell\n try:\n _ = _test_cell_()\n finally:\n del _test_cell_\n _ # This will be added to global scope\n ### END\n\n (\"'a' is not polluting global scope\", True)\n\n### Run in isolation\n\n`%%testcelln` is a shortcut for `%%testcell noglobals` and executes the\ncell in complete isolation from the global scope. This is very useful\nwhen you want to ensure that global variables or namespaces are not\naccessible within the cell.\n\n``` python\naaa = 'global variable'\n```\n\n``` python\n%%testcell\n'aaa' in globals()\n```\n\n True\n\n``` python\n%%testcell noglobals\n'aaa' in globals()\n```\n\n False\n\n``` python\n%%testcelln\n'aaa' in globals()\n```\n\n False\n\n``` python\n%%testcelln\nglobals().keys()\n```\n\n dict_keys(['__builtins__'])\n\nWith `%%testcelln` inside the cell, you\u2019ll be able to access only to\n`__builtins__` (aka: standard python\u2019s functions). **It behaves like a\nnotebook-in-notebook**.\n\n``` python\n%%testcell\ndef my_function(x):\n print(aaa) #\u00a0global variable\n return x\n\ntry:\n my_function(123)\nexcept Exception as e:\n print(e)\n```\n\n global variable\n\n``` python\n%%testcelln\ndef my_function(x):\n print(aaa) #\u00a0global variable\n return x\n\ntry:\n my_function(123)\nexcept Exception as e:\n print(e)\n```\n\n name 'aaa' is not defined\n\nAs you can see from this last example, `%%testcelln` helps you to\nidentify that `my_function` refers global variable `aaa`.\n\n**IMPORTANT**: this is *just wrapping your cell* and so it\u2019s still\nrunning on your main kernel. If you modify variables that has been\ncreated outside of this cell (aka: if you have side effects) this will\nnot protect you.\n\n``` python\naaa\n```\n\n 'global variable'\n\n``` python\n%%testcell \n# WARNING: this will alter the state of global variable:\nglobals().update({'aaa' : 'modified global variable'});\n```\n\n``` python\naaa\n```\n\n 'modified global variable'\n\n``` python\ndel aaa\n```\n\n## Links:\n\n- PROJECT PAGE: <https://github.com/artste/testcell>\n- DOCUMENTATION: <https://artste.github.io/testcell>\n- PYPI: <https://pypi.org/project/testcell>\n- COLAB DEMO:\n [testcell_demo.ipynb](https://colab.research.google.com/github/artste/testcell/blob/main/demo/testcell_demo.ipynb)\n- KAGGLE SAMPLE NOTEBOOK:\n <https://www.kaggle.com/artste/introducing-testcell>\n\n## Todo:\n\n- Install as a plugin to enable it by default like other cell\u2019s magic.\n",
"bugtrack_url": null,
"license": "Apache Software License 2.0",
"summary": "testcell prevents your testing cells from affecting the global namespace",
"version": "0.0.6",
"project_urls": {
"Homepage": "https://github.com/artste/testcell"
},
"split_keywords": [
"nbdev",
"jupyter",
"notebook",
"python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b0b9f44c3b5ded18a1355f5bf7240726252188910cfdf11ec28de79035ab735f",
"md5": "7f9983ca4a194181b34ea9be068a1dd4",
"sha256": "1aa8804dc887ad3e506853142994f81f15c8cb6ad11e3d610c597fdb9e48f8ea"
},
"downloads": -1,
"filename": "testcell-0.0.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7f9983ca4a194181b34ea9be068a1dd4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 10278,
"upload_time": "2023-05-29T08:01:56",
"upload_time_iso_8601": "2023-05-29T08:01:56.313298Z",
"url": "https://files.pythonhosted.org/packages/b0/b9/f44c3b5ded18a1355f5bf7240726252188910cfdf11ec28de79035ab735f/testcell-0.0.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7b1ce10bf7a72fed99d311332c23c927af13075598841fae8cf42f70069e183e",
"md5": "69209ac77cdd5a79a2678cc4fae1982d",
"sha256": "d96b5e70e22cff7bafc3950461a5110e95cc6716737bdde27e88d59fd6936bbd"
},
"downloads": -1,
"filename": "testcell-0.0.6.tar.gz",
"has_sig": false,
"md5_digest": "69209ac77cdd5a79a2678cc4fae1982d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 12396,
"upload_time": "2023-05-29T08:01:58",
"upload_time_iso_8601": "2023-05-29T08:01:58.065608Z",
"url": "https://files.pythonhosted.org/packages/7b/1c/e10bf7a72fed99d311332c23c927af13075598841fae8cf42f70069e183e/testcell-0.0.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-29 08:01:58",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "artste",
"github_project": "testcell",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "testcell"
}