<s> from metakernel import Magic , option <EOL> import importlib <EOL> import logging <EOL> class KernelMagic ( Magic ) : <EOL> kernels = { } <EOL> kernel_name = None <EOL> @ option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , default = "<STR_LIT:default>" , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> def line_kernel ( self , module_name , class_name , kernel_name = "<STR_LIT:default>" ) : <EOL> """<STR_LIT>""" <EOL> self . kernel_name = kernel_name <EOL> module = importlib . import_module ( module_name ) <EOL> class_ = getattr ( module , class_name ) <EOL> class_ . subkernel ( self . kernel ) <EOL> self . kernels [ kernel_name ] = class_ ( ) <EOL> self . retval = self . kernels [ kernel_name ] <EOL> @ option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , default = None , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> def cell_kx ( self , kernel_name = None ) : <EOL> """<STR_LIT>""" <EOL> if kernel_name is None : <EOL> kernel_name = self . kernel_name <EOL> self . retval = self . kernels [ kernel_name ] . do_execute_direct ( self . code ) <EOL> self . evaluate = False <EOL> @ option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , default = None , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> def line_kx ( self , code , kernel_name = None ) : <EOL> """<STR_LIT>""" <EOL> if kernel_name is None : <EOL> kernel_name = self . kernel_name <EOL> code = str ( code ) <EOL> self . retval = self . kernels [ kernel_name ] . do_execute_direct ( code ) <EOL> def post_process ( self , retval ) : <EOL> return self . retval <EOL> def register_magics ( kernel ) : <EOL> kernel . register_magics ( KernelMagic ) <EOL> def register_ipython_magics ( ) : <EOL> from metakernel import IPythonKernel <EOL> from IPython . core . magic import register_line_magic , register_cell_magic <EOL> kernel = IPythonKernel ( ) <EOL> magic = KernelMagic ( kernel ) <EOL> @ register_line_magic <EOL> def kernel ( line ) : <EOL> """<STR_LIT>""" <EOL> if line . strip ( ) . count ( "<STR_LIT:U+0020>" ) == <NUM_LIT:1> : <EOL> kernel_name = "<STR_LIT:default>" <EOL> module_name , class_name = [ item . strip ( ) for item in line . strip ( ) . split ( "<STR_LIT:U+0020>" , <NUM_LIT:1> ) ] <EOL> else : <EOL> module_name , class_name , kernel_name = [ item . strip ( ) for item in line . strip ( ) . split ( "<STR_LIT:U+0020>" , <NUM_LIT:2> ) ] <EOL> magic . line_kernel ( module_name , class_name , kernel_name ) <EOL> @ register_cell_magic <EOL> def kx ( line , cell ) : <EOL> """<STR_LIT>""" <EOL> if line . strip ( ) : <EOL> module_name = line . strip ( ) <EOL> else : <EOL> module_name = "<STR_LIT:default>" <EOL> magic . code = cell <EOL> magic . cell_kx ( module_name ) </s>
