-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from MSeal/kernelAware
Added a warning for gluing an object outside of a context
- Loading branch information
Showing
7 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
import sys | ||
|
||
from mock import MagicMock | ||
from ..utils import is_kernel | ||
|
||
|
||
def test_is_kernel_true(): | ||
class FakeIPyKernel(): | ||
kernel = True | ||
sys.modules['IPython'] = MagicMock() | ||
sys.modules['IPython'].get_ipython.return_value = FakeIPyKernel | ||
assert is_kernel() | ||
del sys.modules['IPython'] | ||
|
||
|
||
def test_not_kernel_in_ipython(): | ||
sys.modules['IPython'] = MagicMock() | ||
sys.modules['IPython'].get_ipython.return_value = {} | ||
assert not is_kernel() | ||
del sys.modules['IPython'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
utils.py | ||
Provides the utilities for scrapbook functions and operations. | ||
""" | ||
import sys | ||
import warnings | ||
from functools import wraps | ||
|
||
|
||
def is_kernel(): | ||
""" | ||
Returns True if execution context is inside a kernel | ||
""" | ||
# if IPython hasn't been imported, there's nothing to check | ||
if 'IPython' in sys.modules: | ||
from IPython import get_ipython | ||
ipy = get_ipython() | ||
if ipy is not None: | ||
return getattr(ipy, 'kernel', None) is not None | ||
return False | ||
|
||
|
||
def kernel_required(f): | ||
@wraps(f) | ||
def wrapper(*args, **kwds): | ||
if not is_kernel(): | ||
warnings.warn( | ||
"No kernel detected for '{fname}'.".format( | ||
fname=f.__name__)) | ||
return f(*args, **kwds) | ||
return wrapper |