-
Notifications
You must be signed in to change notification settings - Fork 31
/
ipython-magic.py
51 lines (42 loc) · 1.42 KB
/
ipython-magic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
####################################
# This file was created by Bohrium.
# It allows you to run NumPy code (cells) as Bohrium, by using the magic command
# `%%bohrium` in your cells, e.g.:
#
# %%bohrium
# print(numpy)
# print(numpy.arange(10))
####################################
from IPython.core.magic import register_cell_magic
try:
import bohrium
have_bohrium = True
@bohrium.replace_numpy
def execute(__code):
exec(__code, globals(), locals())
__excludes = set(["__excludes", "__code", "np", "bohrium"])
try:
# Python 2.x
for key, value in locals().iteritems():
if key not in __excludes:
globals()[key] = value
except:
# Python 3.x
for key, value in locals().items():
if key not in __excludes:
globals()[key] = value
except ImportError:
warning_shown = False # Warning about missin bohrium has been shown
def execute(__code):
global warning_shown
if not warning_shown:
print("WARNING: Module bohrium could not be imported.\n"
" The magic command '%%bohrium' will have no effect.")
warning_shown = True
exec(__code, globals())
@register_cell_magic
def bohrium(line, cell):
# Code must end with \n
code = cell if cell.endswith("\n") else cell + "\n"
execute(code)
return