MCPcopy Create free account
hub / github.com/ipython/ipython / set_hook

Method set_hook

IPython/core/interactiveshell.py:1012–1056  ·  view source on GitHub ↗

set_hook(name,hook) -> sets an internal IPython hook. IPython exposes some of its internal API as user-modifiable hooks. By adding your function to one of these hooks, you can modify IPython's behavior to call at runtime your own routines.

(self,name,hook, priority=50, str_key=None, re_key=None,
                 _warn_deprecated=True)

Source from the content-addressed store, hash-verified

1010 self.set_hook('show_in_pager', page.as_hook(page.display_page), 90)
1011
1012 def set_hook(self,name,hook, priority=50, str_key=None, re_key=None,
1013 _warn_deprecated=True):
1014 """set_hook(name,hook) -> sets an internal IPython hook.
1015
1016 IPython exposes some of its internal API as user-modifiable hooks. By
1017 adding your function to one of these hooks, you can modify IPython's
1018 behavior to call at runtime your own routines."""
1019
1020 # At some point in the future, this should validate the hook before it
1021 # accepts it. Probably at least check that the hook takes the number
1022 # of args it's supposed to.
1023
1024 f = types.MethodType(hook,self)
1025
1026 # check if the hook is for strdispatcher first
1027 if str_key is not None:
1028 sdp = self.strdispatchers.get(name, StrDispatch())
1029 sdp.add_s(str_key, f, priority )
1030 self.strdispatchers[name] = sdp
1031 return
1032 if re_key is not None:
1033 sdp = self.strdispatchers.get(name, StrDispatch())
1034 sdp.add_re(re.compile(re_key), f, priority )
1035 self.strdispatchers[name] = sdp
1036 return
1037
1038 dp = getattr(self.hooks, name, None)
1039 if name not in IPython.core.hooks.__all__:
1040 print("Warning! Hook '%s' is not one of %s" % \
1041 (name, IPython.core.hooks.__all__ ))
1042
1043 if _warn_deprecated and (name in IPython.core.hooks.deprecated):
1044 alternative = IPython.core.hooks.deprecated[name]
1045 warn("Hook {} is deprecated. Use {} instead.".format(name, alternative), stacklevel=2)
1046
1047 if not dp:
1048 dp = IPython.core.hooks.CommandChainDispatcher()
1049
1050 try:
1051 dp.add(f,priority)
1052 except AttributeError:
1053 # it was not commandchain, plain old func - replace
1054 dp = f
1055
1056 setattr(self.hooks,name, dp)
1057
1058 #-------------------------------------------------------------------------
1059 # Things related to events

Callers 5

init_hooksMethod · 0.95
init_completerMethod · 0.95
quick_completerFunction · 0.80
install_editorFunction · 0.80
create_inputhook_qt4Function · 0.80

Calls 5

addMethod · 0.95
StrDispatchClass · 0.90
add_sMethod · 0.80
add_reMethod · 0.80
formatMethod · 0.45

Tested by

no test coverage detected