当前位置: 首页>>代码示例>>Python>>正文


Python InteractiveShellEmbed.run_cell方法代码示例

本文整理汇总了Python中IPython.frontend.terminal.embed.InteractiveShellEmbed.run_cell方法的典型用法代码示例。如果您正苦于以下问题:Python InteractiveShellEmbed.run_cell方法的具体用法?Python InteractiveShellEmbed.run_cell怎么用?Python InteractiveShellEmbed.run_cell使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IPython.frontend.terminal.embed.InteractiveShellEmbed的用法示例。


在下文中一共展示了InteractiveShellEmbed.run_cell方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from IPython.frontend.terminal.embed import InteractiveShellEmbed [as 别名]
# 或者: from IPython.frontend.terminal.embed.InteractiveShellEmbed import run_cell [as 别名]
class IterableIPShell:
  def __init__(self,argv=None,user_ns=None,user_global_ns=None,
               cin=None, cout=None,cerr=None, input_func=None):

    if argv is None:
      argv=[]

    # This is to get rid of the blockage that occurs during
    # IPython.Shell.InteractiveShell.user_setup()
    #IPython.iplib.raw_input = lambda x: None

    # not used?
    #self.term = IPython.genutils.IOTerm(cin=cin, cout=cout, cerr=cerr)

    from IPython.config.loader import Config
    cfg = Config()
    cfg.InteractiveShell.colors = "Linux"

    os.environ['TERM'] = 'dumb'
    excepthook = sys.excepthook
    self.IP = InteractiveShellEmbed(config=cfg, user_ns=user_ns, user_global_ns=user_global_ns)

    self.IP.system = lambda cmd: self.shell(self.IP.var_expand(cmd),
                                            header='IPython system call: ',
                                            verbose=self.IP.rc.system_verbose)
    if cin:
      IPython.utils.io.stdin = IPython.utils.io.IOStream(cin)
    if cout:
      IPython.utils.io.stdout = IPython.utils.io.IOStream(cout)
    if cerr:
      IPython.utils.io.stderr = IPython.utils.io.IOStream(cerr)
    if input_func:
      IPython.frontend.terminal.interactiveshell.raw_input_original = input_func
    sys.excepthook = excepthook
    self.iter_more = False
    self.history_level = 0
    self.complete_sep =  re.compile('[\s\{\}\[\]\(\)]')

  def execute(self):
    self.history_level = 0
    orig_stdout = sys.stdout
    line = None
    try:
      if self.IP.autoindent:
        if self.iter_more:
          self.IP.rl_do_indent = True
        else:
          self.IP.rl_do_indent = False
      line = self.IP.raw_input(self.IP.hooks.generate_prompt(self.iter_more))
      if self.IP.autoindent:
        self.IP.rl_do_indent = False
    except KeyboardInterrupt:
      self.IP.write('\nKeyboardInterrupt\n')
      self.IP.input_splitter.reset()
      self.iter_more = False
    except:
      self.IP.showtraceback()
    else:
      sys.stdout = self.cout
      self.IP.input_splitter.push(line)
      self.iter_more = self.IP.input_splitter.push_accepts_more()
      self.prompt = self.IP.hooks.generate_prompt(self.iter_more)
      if not self.iter_more:
        source_raw = self.IP.input_splitter.source_raw_reset()[1]
        self.IP.run_cell(source_raw)
    sys.stdout = orig_stdout

  def historyBack(self):
    self.history_level -= 1
    return self._getHistory()

  def historyForward(self):
    self.history_level += 1
    return self._getHistory()

  def _getHistory(self):
    try:
      rv = self.IP.user_ns['In'][self.history_level].strip('\n')
    except IndexError:
      self.history_level = 0
      rv = ''
    return rv

  def updateNamespace(self, ns_dict):
    self.IP.user_ns.update(ns_dict)

  def complete(self, line):
    split_line = self.complete_sep.split(line)
    possibilities = self.IP.complete(split_line[-1])[1]
    if possibilities:
      common_prefix = reduce(self._commonPrefix, possibilities)
      completed = line[:-len(split_line[-1])]+common_prefix
    else:
      completed = line
    return completed, possibilities

  def _commonPrefix(self, str1, str2):
    for i in range(len(str1)):
      if not str2.startswith(str1[:i+1]):
        return str1[:i]
#.........这里部分代码省略.........
开发者ID:bradenmacdonald,项目名称:astrodendro,代码行数:103,代码来源:ipython_view.py


注:本文中的IPython.frontend.terminal.embed.InteractiveShellEmbed.run_cell方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。