From f96aac3a0d89b40859d77a4f6f1c268f83197a18 2013-06-16 10:54:20 From: Jeff Knisley Date: 2013-06-16 10:54:20 Subject: [PATCH] Add -q option (suppress print upon creation) to %macro Macros are very, very useful and "Matlab" like (as well as other similar math computing environs). Often I (or my students) use a macro to load long complex code from a url -- e.g., large data sets, simulated data, preprocessing of data, special plotting commands, grading routines... Currently, this requires defining the macro at the end of the notebook so when the "print upon creation" occurs it doesn't overwhelm the notebook (except at the end). The -q option suppresses the print contents upon creation. Example with a Matplotlib example: In[1]: %macro tmp http://matplotlib.org/mpl_examples/api/date_demo.py Macro `tmp` created. To execute, type its name (without quotes). === Macro contents: === """ Show how to make date plots in matplotlib using date tick locators and formatters. See major_minor_demo1.py for more information on controlling major and minor ticks ... In[2]: %macro -q tmp2 http://matplotlib.org/mpl_examples/api/date_demo.py (nothing) Perhaps, though, the first line should print -- e.g., Macro `tmp` created. To execute, type its name (without quotes). In the docstraing, I also fixed a typo (an "as" that should be an "at") and clarified how to produce an example output. --- diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index ccc1a2b..14470ce 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -1031,8 +1031,13 @@ python-profiler package from non-free.""") -r: use 'raw' input. By default, the 'processed' history is used, so that magics are loaded in their transformed version to valid - Python. If this option is given, the raw input as typed as the + Python. If this option is given, the raw input as typed at the command line is used instead. + + -q: quiet macro definition. By default, a tag line is printed + to indicate the macro has been created, and then the contents of + the macro are printed. If this option is given, then no printout + is produced once the macro is created. This will define a global variable called `name` which is a string made of joining the slices and lines you specify (n1,n2,... numbers @@ -1046,7 +1051,7 @@ python-profiler package from non-free.""") Note: as a 'hidden' feature, you can also use traditional python slice notation, where N:M means numbers N through M-1. - For example, if your history contains (%hist prints it):: + For example, if your history contains (print using %hist -n ):: 44: x=1 45: y=3 @@ -1076,7 +1081,7 @@ python-profiler package from non-free.""") print macro_name """ - opts,args = self.parse_options(parameter_s,'r',mode='list') + opts,args = self.parse_options(parameter_s,'rq',mode='list') if not args: # List existing macros return sorted(k for k,v in self.shell.user_ns.iteritems() if\ isinstance(v, Macro)) @@ -1093,9 +1098,10 @@ python-profiler package from non-free.""") return macro = Macro(lines) self.shell.define_macro(name, macro) - print 'Macro `%s` created. To execute, type its name (without quotes).' % name - print '=== Macro contents: ===' - print macro, + if not ( 'q' in opts) : + print 'Macro `%s` created. To execute, type its name (without quotes).' % name + print '=== Macro contents: ===' + print macro, @magic_arguments.magic_arguments() @magic_arguments.argument('output', type=str, default='', nargs='?',