本文整理汇总了Python中IPython.terminal.embed.InteractiveShellEmbed.clear_instance方法的典型用法代码示例。如果您正苦于以下问题:Python InteractiveShellEmbed.clear_instance方法的具体用法?Python InteractiveShellEmbed.clear_instance怎么用?Python InteractiveShellEmbed.clear_instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IPython.terminal.embed.InteractiveShellEmbed
的用法示例。
在下文中一共展示了InteractiveShellEmbed.clear_instance方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wrapper
# 需要导入模块: from IPython.terminal.embed import InteractiveShellEmbed [as 别名]
# 或者: from IPython.terminal.embed.InteractiveShellEmbed import clear_instance [as 别名]
def wrapper(namespace=namespace, banner=''):
config = load_default_config()
# Always use .instace() to ensure _instance propagation to all parents
# this is needed for <TAB> completion works well for new imports
# and clear the instance to always have the fresh env
# on repeated breaks like with inspect_response()
InteractiveShellEmbed.clear_instance()
shell = InteractiveShellEmbed.instance(
banner1=banner, user_ns=namespace, config=config)
shell()
示例2: _ipython
# 需要导入模块: from IPython.terminal.embed import InteractiveShellEmbed [as 别名]
# 或者: from IPython.terminal.embed.InteractiveShellEmbed import clear_instance [as 别名]
def _ipython(local, banner):
from IPython.terminal.embed import InteractiveShellEmbed
from IPython.terminal.ipapp import load_default_config
InteractiveShellEmbed.clear_instance()
shell = InteractiveShellEmbed.instance(
banner1=banner,
user_ns=local,
config=load_default_config()
)
shell()
示例3: console
# 需要导入模块: from IPython.terminal.embed import InteractiveShellEmbed [as 别名]
# 或者: from IPython.terminal.embed.InteractiveShellEmbed import clear_instance [as 别名]
def console():
"""Starts an interactive IPython console"""
import sys
import builtins
from IPython.terminal.embed import (load_default_config,
InteractiveShell, InteractiveShellEmbed)
header = 'Django debug console'
config = load_default_config()
config.InteractiveShellEmbed = config.TerminalInteractiveShell
# save ps1/ps2 if defined
ps1 = None
ps2 = None
try:
ps1 = sys.ps1
ps2 = sys.ps2
except AttributeError:
pass
# save previous instance
saved_shell_instance = InteractiveShell._instance
if saved_shell_instance is not None:
cls = type(saved_shell_instance)
cls.clear_instance()
# Starts shell
retvalue = None
def exit(value=None):
nonlocal retvalue
retvalue = value
try:
builtins.exit = exit
shell = InteractiveShellEmbed.instance(config=config)
shell(header=header, stack_depth=2, compile_flags=None)
InteractiveShellEmbed.clear_instance()
finally:
pass
# restore previous instance
if saved_shell_instance is not None:
cls = type(saved_shell_instance)
cls.clear_instance()
for subclass in cls._walk_mro():
subclass._instance = saved_shell_instance
if ps1 is not None:
sys.ps1 = ps1
sys.ps2 = ps2
return retvalue
示例4: interact
# 需要导入模块: from IPython.terminal.embed import InteractiveShellEmbed [as 别名]
# 或者: from IPython.terminal.embed.InteractiveShellEmbed import clear_instance [as 别名]
def interact(self, **kwargs):
from IPython.terminal.embed import InteractiveShellEmbed, load_default_config
import sys
config = kwargs.get('config')
header = kwargs.pop('header', u'')
compile_flags = kwargs.pop('compile_flags', None)
if config is None:
config = load_default_config()
config.InteractiveShellEmbed = config.TerminalInteractiveShell
kwargs['config'] = config
frame = sys._getframe(1)
shell = InteractiveShellEmbed.instance(
_init_location_id='%s:%s' % (
frame.f_code.co_filename, frame.f_lineno), **kwargs)
shell(header=header, stack_depth=2, compile_flags=compile_flags,
_call_location_id='%s:%s' % (frame.f_code.co_filename, frame.f_lineno))
InteractiveShellEmbed.clear_instance()
示例5: embed2
# 需要导入模块: from IPython.terminal.embed import InteractiveShellEmbed [as 别名]
# 或者: from IPython.terminal.embed.InteractiveShellEmbed import clear_instance [as 别名]
def embed2(**kwargs):
"""
Modified from IPython.terminal.embed.embed so I can mess with stack_depth
"""
config = kwargs.get('config')
header = kwargs.pop('header', u'')
stack_depth = kwargs.pop('stack_depth', 2)
compile_flags = kwargs.pop('compile_flags', None)
import IPython
from IPython.core.interactiveshell import InteractiveShell
from IPython.terminal.embed import InteractiveShellEmbed
if config is None:
config = IPython.terminal.ipapp.load_default_config()
config.InteractiveShellEmbed = config.TerminalInteractiveShell
kwargs['config'] = config
#save ps1/ps2 if defined
ps1 = None
ps2 = None
try:
ps1 = sys.ps1
ps2 = sys.ps2
except AttributeError:
pass
#save previous instance
saved_shell_instance = InteractiveShell._instance
if saved_shell_instance is not None:
cls = type(saved_shell_instance)
cls.clear_instance()
shell = InteractiveShellEmbed.instance(**kwargs)
shell(header=header, stack_depth=stack_depth, compile_flags=compile_flags)
InteractiveShellEmbed.clear_instance()
#restore previous instance
if saved_shell_instance is not None:
cls = type(saved_shell_instance)
cls.clear_instance()
for subclass in cls._walk_mro():
subclass._instance = saved_shell_instance
if ps1 is not None:
sys.ps1 = ps1
sys.ps2 = ps2
示例6: IPS
# 需要导入模块: from IPython.terminal.embed import InteractiveShellEmbed [as 别名]
# 或者: from IPython.terminal.embed.InteractiveShellEmbed import clear_instance [as 别名]
def IPS(copy_namespaces=True, overwrite_globals=False):
"""Starts IPython embedded shell. This is similar to IPython.embed() but with some
additional features:
1. Print a list of the calling frames before entering the prompt
2. (optionally) copy local name space to global one to prevent certain IPython bug.
3. while doing so optinally overwrite names in the global namespace
"""
# let the user know, where this shell is 'waking up'
# construct frame list
# this will be printed in the header
frame_info_list = []
frame_list = []
frame = inspect.currentframe()
while frame is not None:
frame_list.append(frame)
info = inspect.getframeinfo(frame)
frame_info_list.append(info)
frame = frame.f_back
frame_info_list.reverse()
frame_list.reverse()
frame_info_str_list = [format_frameinfo(fi) for fi in frame_info_list]
custom_header1 = "----- frame list -----\n\n"
frame_info_str = "\n--\n".join(frame_info_str_list[:-1])
custom_header2 = "\n----- end of frame list -----\n"
custom_header = "{0}{1}{2}".format(custom_header1, frame_info_str, custom_header2)
# prevent IPython shell to be launched in IP-Notebook
test_str = str(frame_info_list[0]) + str(frame_info_list[1])
if 'IPython' in test_str and 'zmq' in test_str:
print("\n- Not entering IPython embedded shell -\n")
return
# copied (and modified) from IPython/terminal/embed.py
config = load_default_config()
config.InteractiveShellEmbed = config.TerminalInteractiveShell
# these two lines prevent problems related to the initialization
# of ultratb.FormattedTB below
InteractiveShellEmbed.clear_instance()
InteractiveShellEmbed._instance = None
shell = InteractiveShellEmbed.instance()
# achieve that custom macros are loade in interactive shell
shell.magic('load_ext storemagic')
if config.StoreMagics.autorestore:
shell.magic('store -r')
ar_keys = [k.split("/")[-1] for k in shell.db.keys() if k.startswith("autorestore/")]
else:
ar_keys = []
# adapt the namespaces to prevent missing names inside the shell
# see: https://github.com/ipython/ipython/issues/62
# https://github.com/ipython/ipython/issues/10695
if copy_namespaces and len(frame_list) >= 2:
# callers_frame to IPS()
# note that frame_list and frame_info_list were reversed above
f1 = frame_list[-2]
lns = f1.f_locals
gns = f1.f_globals
l_keys = set(lns)
g_keys = set(gns)
u_keys = shell.user_ns.keys()
# those keys which are in local ns but not in global
safe_keys = l_keys - g_keys
unsafe_keys = l_keys.intersection(g_keys)
assert safe_keys.union(unsafe_keys) == l_keys
gns.update({k:lns[k] for k in safe_keys})
if unsafe_keys and not overwrite_globals:
custom_header += "following local keys have " \
"not been copied:\n{}\n".format(unsafe_keys)
if unsafe_keys and overwrite_globals:
gns.update({k:lns[k] for k in unsafe_keys})
custom_header += "following global keys have " \
"been overwritten:\n{}\n".format(unsafe_keys)
# now update the gns with stuff from the user_ns (if it will not overwrite anything)
# this could be implemented cleaner
for k in ar_keys:
if k not in gns:
gns[k] = shell.user_ns[k]
else:
print("omitting key from user_namespace:", k)
dummy_module = DummyMod()
dummy_module.__dict__ = gns
#.........这里部分代码省略.........