Skip to main content

testcell prevents your testing cells from affecting the global namespace

Project description

testcell

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 variables. 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 line is optionally wrapped with a display statement if needed.

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.

Install

pip install testcell

How to use

just import it with import testcell and then use the %%testcell cell magic.

%%testcell
a = "'a' is not polluting global scope"
a
"'a' is not polluting global scope"
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.

%%testcell verbose
a = "'a' is not polluting global scope"
a
### BEGIN
def _test_cell_():
    #| echo: false
    a = "'a' is not polluting global scope"
    display( # %%testcell
    a
    ) # %%testcell
try:
    _test_cell_()
finally:
    del _test_cell_
### 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:

%%testcell dryrun
a = "'a' is not polluting global scope"
a
### BEGIN
def _test_cell_():
    #| echo: false
    a = "'a' is not polluting global scope"
    display( # %%testcell
    a
    ) # %%testcell
try:
    _test_cell_()
finally:
    del _test_cell_
### END

On top of wrapping your cell’s code, it will optinally add a display(...) call on your last cell’s row, trying to emulate what a real cell usually does. If you explicitly add a semicolon ; in the end, this output will be suppressed.

%%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_
### END

There are more cases where display(...) is avoided: * display: if this is already present on the last line. * print: even in this case no print is preserved and no other display call is added. * # comment...#: these are preserved

%%testcell verbose
a = "'a' is not polluting global scope"
print(a)
### BEGIN
def _test_cell_():
    #| echo: false
    a = "'a' is not polluting global scope"
    print(a)
try:
    _test_cell_()
finally:
    del _test_cell_
### END
'a' is not polluting global scope

Run in isolation

%%testcelln is a shourtcut for %%testcell noglobals and executes the cell in complete isolation from global scope. This is very useful when you want to be sure that global variables or namespace should be part of the cell.

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.

aaa = 'global variable'
%%testcell
'aaa' in globals()
True
%%testcell noglobals
'aaa' in globals()
False
%%testcelln
'aaa' in globals()
False
%%testcelln
globals().keys()
dict_keys(['__builtins__', '_test_cell_'])

Inside the cell, from the global scope, only these two items are available: + __builtins__ : built in python’s functions. + _test_cell_ : %%testcell wrapped function executed (we can’t avoid this).

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.

aaa
'global variable'
%%testcell 
# WARNING: this will alter the state of global variable:
globals().update({'aaa' : 'modified global variable'});
aaa
'modified global variable'
del aaa

TODO:

  • Install as a plugin to enable it by default like other cell’s magic.
  • Eval possibility to run code on a new kernel to reduce even more possible side effects.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

testcell-0.0.3.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

testcell-0.0.3-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file testcell-0.0.3.tar.gz.

File metadata

  • Download URL: testcell-0.0.3.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for testcell-0.0.3.tar.gz
Algorithm Hash digest
SHA256 6a52cc119cffb780c773f63a0e8d4f25b7a31c4654af656930eb3f1dc4bff434
MD5 6fe50b5a825445545e7bc0eedde5b18d
BLAKE2b-256 c08ef647c03b31d7b330c1dc6a32c94272b725a5d51eb43da9031529b8ecb2e5

See more details on using hashes here.

File details

Details for the file testcell-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: testcell-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for testcell-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 78e00e1ec164aca49f3bc535fe611e8986b19ce2941b6fc8189b3d6f086e5e0f
MD5 2bed15cfede6e95396cf86fdc2b8ead1
BLAKE2b-256 ab012a5b3672af01ee07ae02241bfe8c1240653127b743ed7feb369f1d55df41

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page