Permalink
Browse files

Improve macro test, modernise magic_macro code.

  • Loading branch information...
takluyver committed Feb 9, 2011
1 parent e72b879 commit 13e5c56b152f4cb31fb30f3bf7b58a4d248135ab
Showing with 7 additions and 8 deletions.
  1. +5 −6 IPython/core/magic.py
  2. +2 −2 IPython/core/tests/test_magic.py
View
@@ -1964,17 +1964,16 @@ def magic_macro(self,parameter_s = ''):
In [60]: exec In[44:48]+In[49]"""
opts,args = self.parse_options(parameter_s,'r',mode='list')
- if not args:
- macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
- macs.sort()
- return macs
+ if not args: # List existing macros
+ return sorted(k for k,v in self.shell.user_ns.iteritems() if\
+ isinstance(v, Macro))
if len(args) == 1:
raise UsageError(
"%macro insufficient args; usage '%macro name n1-n2 n3-4...")
name,ranges = args[0], args[1:]
#print 'rng',ranges # dbg
- lines = self.extract_input_slices(ranges,opts.has_key('r'))
+ lines = self.extract_input_slices(ranges,'r' in opts)
macro = Macro("\n".join(lines))
self.shell.define_macro(name, macro)
print 'Macro `%s` created. To execute, type its name (without quotes).' % name
@@ -2010,7 +2009,7 @@ def magic_save(self,parameter_s = ''):
if ans.lower() not in ['y','yes']:
print 'Operation cancelled.'
return
- cmds = '\n'.join(self.extract_input_slices(ranges,opts.has_key('r')))
+ cmds = '\n'.join(self.extract_input_slices(ranges, 'r' in opts))
with open(fname,'w') as f:
f.write(cmds)
print 'The following commands were written to file `%s`:' % fname
@@ -183,8 +183,8 @@ def test_macro():
ip.magic("macro test 1-3")
nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
- # List macros. This goes to stdout, so just check it doesn't crash.
- ip.magic("macro")
+ # List macros.
+ assert "test" in ip.magic("macro")
# XXX failing for now, until we get clearcmd out of quarantine. But we should

0 comments on commit 13e5c56

Please sign in to comment.