##// END OF EJS Templates
Improve macro test, modernise magic_macro code.
Thomas Kluyver -
Show More
@@ -2030,17 +2030,16 b' Currently the magic system has the following functions:\\n"""'
2030 In [60]: exec In[44:48]+In[49]"""
2030 In [60]: exec In[44:48]+In[49]"""
2031
2031
2032 opts,args = self.parse_options(parameter_s,'r',mode='list')
2032 opts,args = self.parse_options(parameter_s,'r',mode='list')
2033 if not args:
2033 if not args: # List existing macros
2034 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
2034 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
2035 macs.sort()
2035 isinstance(v, Macro))
2036 return macs
2037 if len(args) == 1:
2036 if len(args) == 1:
2038 raise UsageError(
2037 raise UsageError(
2039 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2038 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2040 name,ranges = args[0], args[1:]
2039 name,ranges = args[0], args[1:]
2041
2040
2042 #print 'rng',ranges # dbg
2041 #print 'rng',ranges # dbg
2043 lines = self.extract_input_slices(ranges,opts.has_key('r'))
2042 lines = self.extract_input_slices(ranges,'r' in opts)
2044 macro = Macro("\n".join(lines))
2043 macro = Macro("\n".join(lines))
2045 self.shell.define_macro(name, macro)
2044 self.shell.define_macro(name, macro)
2046 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2045 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
@@ -2076,7 +2075,7 b' Currently the magic system has the following functions:\\n"""'
2076 if ans.lower() not in ['y','yes']:
2075 if ans.lower() not in ['y','yes']:
2077 print 'Operation cancelled.'
2076 print 'Operation cancelled.'
2078 return
2077 return
2079 cmds = '\n'.join(self.extract_input_slices(ranges,opts.has_key('r')))
2078 cmds = '\n'.join(self.extract_input_slices(ranges, 'r' in opts))
2080 with open(fname,'w') as f:
2079 with open(fname,'w') as f:
2081 f.write(cmds)
2080 f.write(cmds)
2082 print 'The following commands were written to file `%s`:' % fname
2081 print 'The following commands were written to file `%s`:' % fname
@@ -183,8 +183,8 b' def test_macro():'
183 ip.magic("macro test 1-3")
183 ip.magic("macro test 1-3")
184 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
184 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
185
185
186 # List macros. This goes to stdout, so just check it doesn't crash.
186 # List macros.
187 ip.magic("macro")
187 assert "test" in ip.magic("macro")
188
188
189
189
190 # XXX failing for now, until we get clearcmd out of quarantine. But we should
190 # XXX failing for now, until we get clearcmd out of quarantine. But we should
General Comments 0
You need to be logged in to leave comments. Login now