<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import ast <EOL> import re <EOL> from IPython . core . error import UsageError <EOL> from IPython . core . magic import Magics <EOL> from IPython . core import magic_arguments <EOL> from ipython_genutils . text import dedent <EOL> NO_LAST_RESULT = "<STR_LIT>" <EOL> def exec_args ( f ) : <EOL> """<STR_LIT>""" <EOL> args = [ <EOL> magic_arguments . argument ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT>" , <EOL> const = True , dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" , <EOL> ) , <EOL> magic_arguments . argument ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT>" , <EOL> const = False , dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" , <EOL> ) , <EOL> magic_arguments . argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , <EOL> help = "<STR_LIT>" , <EOL> ) , <EOL> magic_arguments . argument ( '<STR_LIT>' , action = "<STR_LIT>" , <EOL> const = True , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" , <EOL> ) , <EOL> magic_arguments . argument ( '<STR_LIT>' , action = "<STR_LIT>" , <EOL> const = True , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" , <EOL> ) , <EOL> magic_arguments . argument ( '<STR_LIT>' , action = "<STR_LIT>" , <EOL> const = False , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" , <EOL> ) , <EOL> ] <EOL> for a in args : <EOL> f = a ( f ) <EOL> return f <EOL> def output_args ( f ) : <EOL> """<STR_LIT>""" <EOL> args = [ <EOL> magic_arguments . argument ( '<STR_LIT>' , action = "<STR_LIT>" , dest = '<STR_LIT>' , <EOL> const = '<STR_LIT>' , <EOL> help = "<STR_LIT>" <EOL> ) , <EOL> magic_arguments . argument ( '<STR_LIT>' , action = "<STR_LIT>" , dest = '<STR_LIT>' , <EOL> const = '<STR_LIT>' , <EOL> help = "<STR_LIT>" <EOL> ) , <EOL> magic_arguments . argument ( '<STR_LIT>' , dest = '<STR_LIT>' , type = str , <EOL> choices = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:type>' ] , default = '<STR_LIT:type>' , <EOL> help = """<STR_LIT>""" <EOL> ) , <EOL> magic_arguments . argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , type = str , <EOL> help = """<STR_LIT>""" <EOL> ) , <EOL> ] <EOL> for a in args : <EOL> f = a ( f ) <EOL> return f <EOL> class ParallelMagics ( Magics ) : <EOL> """<STR_LIT>""" <EOL> magics = None <EOL> registered = True <EOL> suffix = '<STR_LIT>' <EOL> _autopx = False <EOL> view = None <EOL> last_result = None <EOL> verbose = False <EOL> def __init__ ( self , shell , view , suffix = '<STR_LIT>' ) : <EOL> self . view = view <EOL> self . suffix = suffix <EOL> self . magics = dict ( cell = { } , line = { } ) <EOL> line_magics = self . magics [ '<STR_LIT>' ] <EOL> px = '<STR_LIT>' + suffix <EOL> if not suffix : <EOL> line_magics [ '<STR_LIT:result>' ] = self . result <EOL> line_magics [ '<STR_LIT>' + suffix ] = self . result <EOL> line_magics [ px ] = self . px <EOL> line_magics [ '<STR_LIT>' + suffix ] = self . pxconfig <EOL> line_magics [ '<STR_LIT>' + px ] = self . autopx <EOL> self . magics [ '<STR_LIT>' ] [ px ] = self . cell_px <EOL> super ( ParallelMagics , self ) . __init__ ( shell = shell ) <EOL> def _eval_target_str ( self , ts ) : <EOL> if '<STR_LIT::>' in ts : <EOL> targets = eval ( "<STR_LIT>" % ts ) <EOL> elif '<STR_LIT:all>' in ts : <EOL> targets = '<STR_LIT:all>' <EOL> else : <EOL> targets = eval ( ts ) <EOL> return targets <EOL> @ magic_arguments . magic_arguments ( ) <EOL> @ exec_args <EOL> def pxconfig ( self , line ) : <EOL> """<STR_LIT>""" <EOL> args = magic_arguments . parse_argstring ( self . pxconfig , line ) <EOL> if args . targets : <EOL> self . view . targets = self . _eval_target_str ( args . targets ) <EOL> if args . block is not None : <EOL> self . view . block = args . block <EOL> if args . set_verbose is not None : <EOL> self . verbose = args . set_verbose <EOL> @ magic_arguments . magic_arguments ( ) <EOL> @ output_args <EOL> def result ( self , line = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> args = magic_arguments . parse_argstring ( self . result , line ) <EOL> if self . last_result is None : <EOL> raise UsageError ( NO_LAST_RESULT ) <EOL> self . last_result . get ( ) <EOL> self . last_result . display_outputs ( groupby = args . groupby ) <EOL> def px ( self , line = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return self . parallel_execute ( line ) <EOL> def parallel_execute ( self , cell , block = None , groupby = '<STR_LIT:type>' , save_name = None ) : <EOL> """<STR_LIT>""" <EOL> block = self . view . block if block is None else block <EOL> base = "<STR_LIT>" if block else "<STR_LIT>" <EOL> targets = self . view . targets <EOL> if isinstance ( targets , list ) and len ( targets ) > <NUM_LIT:10> : <EOL> str_targets = str ( targets [ : <NUM_LIT:4> ] ) [ : - <NUM_LIT:1> ] + '<STR_LIT>' + str ( targets [ - <NUM_LIT:4> : ] ) [ <NUM_LIT:1> : ] <EOL> else : <EOL> str_targets = str ( targets ) <EOL> if self . verbose : <EOL> print ( base + "<STR_LIT>" % str_targets ) <EOL> result = self . view . execute ( cell , silent = False , block = False ) <EOL> self . last_result = result <EOL> if save_name : <EOL> self . shell . user_ns [ save_name ] = result <EOL> if block : <EOL> result . get ( ) <EOL> result . display_outputs ( groupby ) <EOL> else : <EOL> return result <EOL> @ magic_arguments . magic_arguments ( ) <EOL> @ exec_args <EOL> @ output_args <EOL> def cell_px ( self , line = '<STR_LIT>' , cell = None ) : <EOL> """<STR_LIT>""" <EOL> args = magic_arguments . parse_argstring ( self . cell_px , line ) <EOL> if args . targets : <EOL> save_targets = self . view . targets <EOL> self . view . targets = self . _eval_target_str ( args . targets ) <EOL> block = False if args . local else args . block <EOL> try : <EOL> ar = self . parallel_execute ( cell , block = block , <EOL> groupby = args . groupby , <EOL> save_name = args . save_name , <EOL> ) <EOL> finally : <EOL> if args . targets : <EOL> self . view . targets = save_targets <EOL> block = self . view . block if args . block is None else args . block <EOL> if args . local : <EOL> self . shell . run_cell ( cell ) <EOL> if block : <EOL> ar . get ( ) <EOL> ar . display_outputs ( args . groupby ) <EOL> if not block : <EOL> return ar <EOL> def autopx ( self , line = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if self . _autopx : <EOL> self . _disable_autopx ( ) <EOL> else : <EOL> self . _enable_autopx ( ) <EOL> def _enable_autopx ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _original_run_cell = self . shell . run_cell <EOL> self . shell . run_cell = self . pxrun_cell <EOL> self . _autopx = True <EOL> print ( "<STR_LIT>" ) <EOL> def _disable_autopx ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _autopx : <EOL> self . shell . run_cell = self . _original_run_cell <EOL> self . _autopx = False <EOL> print ( "<STR_LIT>" ) <EOL> def pxrun_cell ( self , raw_cell , store_history = False , silent = False ) : <EOL> """<STR_LIT>""" <EOL> if ( not raw_cell ) or raw_cell . isspace ( ) : <EOL> return <EOL> ipself = self . shell <EOL> with ipself . builtin_trap : <EOL> cell = ipself . prefilter_manager . prefilter_lines ( raw_cell ) <EOL> if store_history : <EOL> ipself . history_manager . store_inputs ( ipself . execution_count , <EOL> cell , raw_cell ) <EOL> cell_name = ipself . compile . cache ( cell , ipself . execution_count ) <EOL> try : <EOL> ast . parse ( cell , filename = cell_name ) <EOL> except ( OverflowError , SyntaxError , ValueError , TypeError , <EOL> MemoryError ) : <EOL> ipself . showsyntaxerror ( ) <EOL> ipself . execution_count += <NUM_LIT:1> <EOL> return None <EOL> except NameError : <EOL> pass <EOL> if store_history : <EOL> ipself . history_manager . store_output ( ipself . execution_count ) <EOL> ipself . execution_count += <NUM_LIT:1> <EOL> if re . search ( r'<STR_LIT>' , cell ) : <EOL> self . _disable_autopx ( ) <EOL> return False <EOL> else : <EOL> try : <EOL> result = self . view . execute ( cell , silent = False , block = False ) <EOL> except : <EOL> ipself . showtraceback ( ) <EOL> return True <EOL> else : <EOL> if self . view . block : <EOL> try : <EOL> result . get ( ) <EOL> except : <EOL> self . shell . showtraceback ( ) <EOL> return True <EOL> else : <EOL> with ipself . builtin_trap : <EOL> result . display_outputs ( ) <EOL> return False <EOL> __doc__ = __doc__ . format ( <EOL> AUTOPX_DOC = dedent ( ParallelMagics . autopx . __doc__ ) , <EOL> PX_DOC = dedent ( ParallelMagics . px . __doc__ ) , <EOL> RESULT_DOC = dedent ( ParallelMagics . result . __doc__ ) , <EOL> CONFIG_DOC = dedent ( ParallelMagics . pxconfig . __doc__ ) , <EOL> ) </s>
