diff --git a/README.md b/README.md index 041e6d7..8461991 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,9 @@ execution equivalent to not using such keyword. Mutable objects in the user namespace can be altered (e.g. appending an item to a list). +If `` is blank, the cell is executed in a single-use anonymous +namespace, which cannot be accessed later. + ### Remove a space ```python diff --git a/src/jupyter_spaces/__init__.py b/src/jupyter_spaces/__init__.py index 5d6dbfb..7583c27 100644 --- a/src/jupyter_spaces/__init__.py +++ b/src/jupyter_spaces/__init__.py @@ -1,5 +1,5 @@ """Jupyter Spaces is an IPython extension for creating parallel namespaces -availabe within the user namespace. It is designed to be used via IPython +available within the user namespace. It is designed to be used via IPython magics in Jupyter notebooks. Copyright (c) 2018 Davide Sarra. diff --git a/src/jupyter_spaces/magics.py b/src/jupyter_spaces/magics.py index b358e9e..bf80e22 100644 --- a/src/jupyter_spaces/magics.py +++ b/src/jupyter_spaces/magics.py @@ -16,6 +16,8 @@ def space(self, line, cell): line (str): Content following `%%space` magic call, expected to match the space name. If the provided space name has already been used and not been removed, the same space object is used. + Unless the name is blank, in which case a unique anonymous + namespace is used. cell (str): Content following the first line. Examples: diff --git a/src/jupyter_spaces/space.py b/src/jupyter_spaces/space.py index 9453c18..73671b3 100644 --- a/src/jupyter_spaces/space.py +++ b/src/jupyter_spaces/space.py @@ -23,7 +23,7 @@ def register(self): def get_space(self, name, outer_space): """Get existing Space if a Space has the same name, otherwise create - and get new Space. + and get new Space. Blank name creates a single-use anonymous Space. Args: name (str): Name of the Space. @@ -32,9 +32,13 @@ def get_space(self, name, outer_space): Returns: Space: Space. """ - if name not in self._register: - self._register[name] = Space(name=name, outer_space=outer_space) - return self._register[name] + space = self._register.get(name) + if space is not None: + return space + space = Space(name=name, outer_space=outer_space) + if name: + self._register[name] = space + return space def remove_space(self, name): """Remove Space from register. diff --git a/tests/test_magics.py b/tests/test_magics.py index 9efa8b0..846c8c5 100644 --- a/tests/test_magics.py +++ b/tests/test_magics.py @@ -178,3 +178,8 @@ def test_none_does_not_produce_any_stdout(ip, capsys): def test_statement_does_not_produce_any_stdout(ip, capsys): ip.run_cell_magic(magic_name="space", line="tomato", cell="x = 1") assert capsys.readouterr().out == "" + + +def test_anonymous_spaces(ip, capsys): + ip.run_cell_magic(magic_name="space", line="", cell="x = 1") + ip.run_cell_magic(magic_name="space", line="", cell="assert 'x' not in globals()") diff --git a/tests/test_space.py b/tests/test_space.py index 8bbe609..5141cae 100644 --- a/tests/test_space.py +++ b/tests/test_space.py @@ -134,6 +134,12 @@ def test_get_existing_space(self): space2 = space_register.get_space(name="tomato", outer_space={}) assert space1 is space2 + def test_get_anonymous_space(self): + space_register = SpaceRegister() + space1 = space_register.get_space(name="", outer_space={}) + space2 = space_register.get_space(name="", outer_space={}) + assert space1 is not space2 + def test_remove_existing_space(self): space_register = SpaceRegister() space_register.get_space(name="tomato", outer_space={})