22import io
33import re
44
5- from IPython .core .magic import register_cell_magic
6- from IPython .core .magic_arguments import argument , magic_arguments , parse_argstring
7-
8-
9- def traceback (mode ):
10- """Set the traceback mode.
11-
12- mode: string
13- """
14- with contextlib .redirect_stdout (io .StringIO ()):
15- get_ipython ().run_cell (f'%xmode { mode } ' )
16-
17-
18- traceback ('Minimal' )
19-
205
216def extract_function_name (text ):
227 """Find a function definition and return its name.
@@ -34,61 +19,75 @@ def extract_function_name(text):
3419 return None
3520
3621
37- @ register_cell_magic
38- def add_method_to ( args , cell ):
22+ # the functions that define cell magic commands are only defined
23+ # if we're running in Jupyter.
3924
40- # get the name of the function defined in this cell
41- func_name = extract_function_name (cell )
42- if func_name is None :
43- return f"This cell doesn't define any new functions."
25+ try :
26+ from IPython .core .magic import register_cell_magic
27+ from IPython .core .magic_arguments import argument , magic_arguments , parse_argstring
4428
45- # get the class we're adding it to
46- namespace = get_ipython ().user_ns
47- class_name = args
48- cls = namespace .get (class_name , None )
49- if cls is None :
50- return f"Class '{ class_name } ' not found."
29+ @register_cell_magic
30+ def add_method_to (args , cell ):
5131
52- # save the old version of the function if it was already defined
53- old_func = namespace . get ( func_name , None )
54- if old_func is not None :
55- del namespace [ func_name ]
32+ # get the name of the function defined in this cell
33+ func_name = extract_function_name ( cell )
34+ if func_name is None :
35+ return f"This cell doesn't define any new functions."
5636
57- # Execute the cell to define the function
58- get_ipython ().run_cell (cell )
37+ # get the class we're adding it to
38+ namespace = get_ipython ().user_ns
39+ class_name = args
40+ cls = namespace .get (class_name , None )
41+ if cls is None :
42+ return f"Class '{ class_name } ' not found."
5943
60- # get the newly defined function
61- new_func = namespace .get (func_name , None )
62- if new_func is None :
63- return f"This cell didn't define { func_name } ."
44+ # save the old version of the function if it was already defined
45+ old_func = namespace .get (func_name , None )
46+ if old_func is not None :
47+ del namespace [ func_name ]
6448
65- # add the function to the class and remove it from the namespace
66- setattr (cls , func_name , new_func )
67- del namespace [func_name ]
68-
69- # restore the old function to the namespace
70- if old_func is not None :
71- namespace [func_name ] = old_func
72-
73-
74- @register_cell_magic
75- def expect_error (line , cell ):
76- try :
49+ # Execute the cell to define the function
7750 get_ipython ().run_cell (cell )
78- except Exception as e :
79- get_ipython ().run_cell ('%tb' )
80-
8151
52+ # get the newly defined function
53+ new_func = namespace .get (func_name , None )
54+ if new_func is None :
55+ return f"This cell didn't define { func_name } ."
8256
83- @magic_arguments ()
84- @argument ('exception' , help = 'Type of exception to catch' )
85- @register_cell_magic
86- def expect (line , cell ):
87- args = parse_argstring (expect , line )
88- exception = eval (args .exception )
89- try :
90- get_ipython ().run_cell (cell )
91- except exception as e :
92- get_ipython ().run_cell ("%tb" )
93-
57+ # add the function to the class and remove it from the namespace
58+ setattr (cls , func_name , new_func )
59+ del namespace [func_name ]
9460
61+ # restore the old function to the namespace
62+ if old_func is not None :
63+ namespace [func_name ] = old_func
64+
65+ @register_cell_magic
66+ def expect_error (line , cell ):
67+ try :
68+ get_ipython ().run_cell (cell )
69+ except Exception as e :
70+ get_ipython ().run_cell ("%tb" )
71+
72+ @magic_arguments ()
73+ @argument ("exception" , help = "Type of exception to catch" )
74+ @register_cell_magic
75+ def expect (line , cell ):
76+ args = parse_argstring (expect , line )
77+ exception = eval (args .exception )
78+ try :
79+ get_ipython ().run_cell (cell )
80+ except exception as e :
81+ get_ipython ().run_cell ("%tb" )
82+
83+ def traceback (mode ):
84+ """Set the traceback mode.
85+
86+ mode: string
87+ """
88+ with contextlib .redirect_stdout (io .StringIO ()):
89+ get_ipython ().run_cell (f"%xmode { mode } " )
90+
91+ traceback ("Minimal" )
92+ except (ImportError , NameError ):
93+ print ("Warning: IPython is not available, cell magic not defined." )
0 commit comments