Skip to content

Add single-use anonymous namespaces #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<space-name>` is blank, the cell is executed in a single-use anonymous
namespace, which cannot be accessed later.

### Remove a space

```python
Expand Down
2 changes: 1 addition & 1 deletion src/jupyter_spaces/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/jupyter_spaces/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 8 additions & 4 deletions src/jupyter_spaces/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
6 changes: 6 additions & 0 deletions tests/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={})
Expand Down