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, s
(self, parameter_s='')
| 1467 | @skip_doctest |
| 1468 | @line_magic |
| 1469 | def macro(self, parameter_s=''): |
| 1470 | """Define a macro for future re-execution. It accepts ranges of history, |
| 1471 | filenames or string objects. |
| 1472 | |
| 1473 | Usage:: |
| 1474 | |
| 1475 | %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... |
| 1476 | |
| 1477 | Options: |
| 1478 | |
| 1479 | -r |
| 1480 | Use 'raw' input. By default, the 'processed' history is used, |
| 1481 | so that magics are loaded in their transformed version to valid |
| 1482 | Python. If this option is given, the raw input as typed at the |
| 1483 | command line is used instead. |
| 1484 | |
| 1485 | -q |
| 1486 | Quiet macro definition. By default, a tag line is printed |
| 1487 | to indicate the macro has been created, and then the contents of |
| 1488 | the macro are printed. If this option is given, then no printout |
| 1489 | is produced once the macro is created. |
| 1490 | |
| 1491 | This will define a global variable called `name` which is a string |
| 1492 | made of joining the slices and lines you specify (n1,n2,... numbers |
| 1493 | above) from your input history into a single string. This variable |
| 1494 | acts like an automatic function which re-executes those lines as if |
| 1495 | you had typed them. You just type 'name' at the prompt and the code |
| 1496 | executes. |
| 1497 | |
| 1498 | The syntax for indicating input ranges is described in %history. |
| 1499 | |
| 1500 | Note: as a 'hidden' feature, you can also use traditional python slice |
| 1501 | notation, where N:M means numbers N through M-1. |
| 1502 | |
| 1503 | For example, if your history contains (print using %hist -n ):: |
| 1504 | |
| 1505 | 44: x=1 |
| 1506 | 45: y=3 |
| 1507 | 46: z=x+y |
| 1508 | 47: print(x) |
| 1509 | 48: a=5 |
| 1510 | 49: print('x',x,'y',y) |
| 1511 | |
| 1512 | you can create a macro with lines 44 through 47 (included) and line 49 |
| 1513 | called my_macro with:: |
| 1514 | |
| 1515 | In [55]: %macro my_macro 44-47 49 |
| 1516 | |
| 1517 | Now, typing `my_macro` (without quotes) will re-execute all this code |
| 1518 | in one pass. |
| 1519 | |
| 1520 | You don't need to give the line-numbers in order, and any given line |
| 1521 | number can appear multiple times. You can assemble macros with any |
| 1522 | lines from your input history in any order. |
| 1523 | |
| 1524 | The macro is a simple object which holds its value in an attribute, |
| 1525 | but IPython's display system checks for macros and executes them as |
| 1526 | code instead of printing them when you type their name. |
nothing calls this directly
no test coverage detected