← Back to team overview

registry team mailing list archive

[Merge] lp:~duke-eml/ipython/ipython-saveappend into lp:ipython

 

Marmaduke Woodman has proposed merging lp:~duke-eml/ipython/ipython-saveappend into lp:ipython.

Requested reviews:
  Registry Administrators (registry)

For more details, see:
https://code.launchpad.net/~duke-eml/ipython/ipython-saveappend/+merge/46268

I have made a small change to allow the magic_save function to append to an existing file rather than overwrite it. 

In using IPython, I often would like to build up a file gradually from the command line history, and the default behavior of overwriting existing files makes this difficult, hence the modification. 

I haven't written any tests because I don't know how to put one in the testing framework, but in principle, the test would involve reading an existing file, running magic_save, then checking the file to see that the intended lines were appended correctly. 
-- 
https://code.launchpad.net/~duke-eml/ipython/ipython-saveappend/+merge/46268
Your team Registry Administrators is requested to review the proposed merge of lp:~duke-eml/ipython/ipython-saveappend into lp:ipython.
=== modified file 'IPython/core/magic.py'
--- IPython/core/magic.py	2010-12-15 20:17:01 +0000
+++ IPython/core/magic.py	2011-01-14 14:50:28 +0000
@@ -2002,19 +2002,23 @@
         filename you specify.
 
         It adds a '.py' extension to the file if you don't do so yourself, and
-        it asks for confirmation before overwriting existing files."""
+        it asks for confirmation before overwriting or appending to 
+        existing files."""
 
         opts,args = self.parse_options(parameter_s,'r',mode='list')
         fname,ranges = args[0], args[1:]
+        fmode = 'w'
         if not fname.endswith('.py'):
             fname += '.py'
         if os.path.isfile(fname):
-            ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
-            if ans.lower() not in ['y','yes']:
+            ans = raw_input('File `%s` exists. Overwrite (y/a/[N])? ' % fname)
+            if ans.lower() not in ['y','a','yes']:
                 print 'Operation cancelled.'
                 return
+            elif ans.lower() in ['a']:
+                fmode = 'a'
         cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
-        f = file(fname,'w')
+        f = file(fname,fmode)
         f.write(cmds)
         f.close()
         print 'The following commands were written to file `%s`:' % fname