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

Function _method_magic_marker

IPython/core/magic.py:178–209  ·  view source on GitHub ↗

Decorator factory for methods in Magics subclasses.

(magic_kind)

Source from the content-addressed store, hash-verified

176# and make a single one with convoluted logic.
177
178def _method_magic_marker(magic_kind):
179 """Decorator factory for methods in Magics subclasses.
180 """
181
182 validate_type(magic_kind)
183
184 # This is a closure to capture the magic_kind. We could also use a class,
185 # but it's overkill for just that one bit of state.
186 def magic_deco(arg):
187 call = lambda f, *a, **k: f(*a, **k)
188
189 if callable(arg):
190 # "Naked" decorator call (just @foo, no args)
191 func = arg
192 name = func.__name__
193 retval = decorator(call, func)
194 record_magic(magics, magic_kind, name, name)
195 elif isinstance(arg, str):
196 # Decorator called with arguments (@foo('bar'))
197 name = arg
198 def mark(func, *a, **kw):
199 record_magic(magics, magic_kind, name, func.__name__)
200 return decorator(call, func)
201 retval = mark
202 else:
203 raise TypeError("Decorator can only be called with "
204 "string or function")
205 return retval
206
207 # Ensure the resulting decorator has a usable docstring
208 magic_deco.__doc__ = _docstring_template.format('method', magic_kind)
209 return magic_deco
210
211
212def _function_magic_marker(magic_kind):

Callers 1

magic.pyFile · 0.85

Calls 2

validate_typeFunction · 0.85
formatMethod · 0.45

Tested by

no test coverage detected