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

Method macro

IPython/core/magics/execution.py:1351–1431  ·  view source on GitHub ↗

Define a macro for future re-execution. It accepts ranges of history, filenames or string objects. Usage:\\ %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... Options: -r: use 'raw' input. By default, the 'processed' history is used, so that

(self, parameter_s='')

Source from the content-addressed store, hash-verified

1349 @skip_doctest
1350 @line_magic
1351 def macro(self, parameter_s=''):
1352 """Define a macro for future re-execution. It accepts ranges of history,
1353 filenames or string objects.
1354
1355 Usage:\\
1356 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1357
1358 Options:
1359
1360 -r: use 'raw' input. By default, the 'processed' history is used,
1361 so that magics are loaded in their transformed version to valid
1362 Python. If this option is given, the raw input as typed at the
1363 command line is used instead.
1364
1365 -q: quiet macro definition. By default, a tag line is printed
1366 to indicate the macro has been created, and then the contents of
1367 the macro are printed. If this option is given, then no printout
1368 is produced once the macro is created.
1369
1370 This will define a global variable called `name` which is a string
1371 made of joining the slices and lines you specify (n1,n2,... numbers
1372 above) from your input history into a single string. This variable
1373 acts like an automatic function which re-executes those lines as if
1374 you had typed them. You just type 'name' at the prompt and the code
1375 executes.
1376
1377 The syntax for indicating input ranges is described in %history.
1378
1379 Note: as a 'hidden' feature, you can also use traditional python slice
1380 notation, where N:M means numbers N through M-1.
1381
1382 For example, if your history contains (print using %hist -n )::
1383
1384 44: x=1
1385 45: y=3
1386 46: z=x+y
1387 47: print x
1388 48: a=5
1389 49: print 'x',x,'y',y
1390
1391 you can create a macro with lines 44 through 47 (included) and line 49
1392 called my_macro with::
1393
1394 In [55]: %macro my_macro 44-47 49
1395
1396 Now, typing `my_macro` (without quotes) will re-execute all this code
1397 in one pass.
1398
1399 You don't need to give the line-numbers in order, and any given line
1400 number can appear multiple times. You can assemble macros with any
1401 lines from your input history in any order.
1402
1403 The macro is a simple object which holds its value in an attribute,
1404 but IPython's display system checks for macros and executes them as
1405 code instead of printing them when you type their name.
1406
1407 You can view a macro's contents by explicitly printing it with::
1408

Callers

nothing calls this directly

Calls 5

UsageErrorClass · 0.90
MacroClass · 0.90
parse_optionsMethod · 0.80
find_user_codeMethod · 0.80
define_macroMethod · 0.80

Tested by

no test coverage detected