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

Method recall

IPython/core/magics/history.py:222–277  ·  view source on GitHub ↗

r"""Repeat a command, or get command to input line for editing. %recall and %rep are equivalent. - %recall (no arguments): Place a string version of last computation result (stored in the special '_' variable) to the next input prompt. Allows you to create

(self, arg)

Source from the content-addressed store, hash-verified

220
221 @line_magic
222 def recall(self, arg):
223 r"""Repeat a command, or get command to input line for editing.
224
225 %recall and %rep are equivalent.
226
227 - %recall (no arguments):
228
229 Place a string version of last computation result (stored in the
230 special '_' variable) to the next input prompt. Allows you to create
231 elaborate command lines without using copy-paste::
232
233 In[1]: l = ["hei", "vaan"]
234 In[2]: "".join(l)
235 Out[2]: heivaan
236 In[3]: %recall
237 In[4]: heivaan_ <== cursor blinking
238
239 %recall 45
240
241 Place history line 45 on the next input prompt. Use %hist to find
242 out the number.
243
244 %recall 1-4
245
246 Combine the specified lines into one cell, and place it on the next
247 input prompt. See %history for the slice syntax.
248
249 %recall foo+bar
250
251 If foo+bar can be evaluated in the user namespace, the result is
252 placed at the next input prompt. Otherwise, the history is searched
253 for lines which contain that substring, and the most recent one is
254 placed at the next input prompt.
255 """
256 if not arg: # Last output
257 self.shell.set_next_input(str(self.shell.user_ns["_"]))
258 return
259 # Get history range
260 histlines = self.shell.history_manager.get_range_by_str(arg)
261 cmd = "\n".join(x[2] for x in histlines)
262 if cmd:
263 self.shell.set_next_input(cmd.rstrip())
264 return
265
266 try: # Variable in user namespace
267 cmd = str(eval(arg, self.shell.user_ns))
268 except Exception: # Search for term in history
269 histlines = self.shell.history_manager.search("*"+arg+"*")
270 for h in reversed([x[2] for x in histlines]):
271 if 'recall' in h or 'rep' in h:
272 continue
273 self.shell.set_next_input(h.rstrip())
274 return
275 else:
276 self.shell.set_next_input(cmd.rstrip())
277 print("Couldn't evaluate or find in history:", arg)
278
279 @line_magic

Callers

nothing calls this directly

Calls 3

set_next_inputMethod · 0.80
get_range_by_strMethod · 0.45
searchMethod · 0.45

Tested by

no test coverage detected