Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option append (-a) to %save #2131

Merged
merged 6 commits into from Jul 16, 2012
Merged

Add option append (-a) to %save #2131

merged 6 commits into from Jul 16, 2012

Conversation

doda-zz
Copy link

@doda-zz doda-zz commented Jul 13, 2012

inspired by: http://www.reddit.com/r/Python/comments/wc0wp/ipython_how_to_save_code_entered_in_the_shell_to/

:)

I'm not sure if simply writing a \n if it isn't there is the best way to handle the case of multiple save -a after one another, but i'd think it's than to try and change find_user_code.

Add newline after every write to make sure successive appends work.
@@ -61,6 +61,9 @@ def save(self, parameter_s=''):
-f: force overwrite. If file exists, %save will prompt for overwrite
unless -f is given.

-a: append to file. Open file in append mode and end every write
on a newline.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why we shouldn't end on a newline always anyway?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think there's ever a reason not to end a file with a newline, doda/ipython@4a6d842 would implement that and simplify the code as a result

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes sense - let's bring that commit into this PR.

@takluyver
Copy link
Member

This only writes the #coding: utf-8 header if -a isn't used. That makes sense, but is it worth also writing it if -a is specified and the file doesn't already exist?

Check whether the file didn't exist beforehand or if we're
not appending (in which case the file gets written anew anyways.
@doda-zz
Copy link
Author

doda-zz commented Jul 14, 2012

i've added 2 commits, the magic comment now always gets written if the file didn't exist beforehand, no matter -a or not and files are always ended with newlines, which makes sense given it's in the C standard

@@ -61,6 +61,8 @@ def save(self, parameter_s=''):
-f: force overwrite. If file exists, %save will prompt for overwrite
unless -f is given.

-a: open file in append mode.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this description could be a bit clearer - how about "append to the file instead of overwriting it."

@takluyver
Copy link
Member

Other than the docstring (and maybe a test), I think this looks fine.

@doda-zz
Copy link
Author

doda-zz commented Jul 14, 2012

grepping through the source didn't lead me to any tests for %save so i've written a test that covers both %save as well as %save -a. if there's anything else let me know! :)

@takluyver
Copy link
Member

Great, thanks. I'm running the tests now.

@takluyver
Copy link
Member

For some reason it's refusing to post the results automatically, but it's somehow causing a failure in the history tests:

======================================================================
FAIL: IPython.core.tests.test_history.test_history
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/thomas/.ipy_pr_tests/venv-python3.2/lib/python3.2/site-packages/ipython-0.14.dev-py3.2.egg/IPython/core/tests/test_history.py", line 98, in test_history
    "# coding: utf-8\n" + "\n".join(hist))
AssertionError: "# coding: utf-8\na=1\ndef f():\n    test = 1\n    return test\nb='\u20ac\xc6\xbe\xf7\xdf'\n" != "# coding: utf-8\na=1\ndef f():\n    test = 1\n    return test\nb='\u20ac\xc6\xbe\xf7\xdf'"
  # coding: utf-8
  a=1
  def f():
      test = 1
      return test
- b='\u20ac\xc6\xbe\xf7\xdf'
?          -
+ b='\u20ac\xc6\xbe\xf7\xdf'
    """Fail immediately, with the given message."""
>>  raise self.failureException('"# coding: utf-8\\na=1\\ndef f():\\n    test = 1\\n    return test\\nb=\'\u20ac\xc6\xbe\xf7\xdf\'\\n" != "# coding: utf-8\\na=1\\ndef f():\\n    test = 1\\n    return test\\nb=\'\u20ac\xc6\xbe\xf7\xdf\'"\n  # coding: utf-8\n  a=1\n  def f():\n      test = 1\n      return test\n- b=\'\u20ac\xc6\xbe\xf7\xdf\'\n?          -\n+ b=\'\u20ac\xc6\xbe\xf7\xdf\'')

-------------------- >> begin captured stdout << ---------------------
def f():
    test = 1
    return test
b='\u20ac\xc6\xbe\xf7\xdf'
The following commands were written to file `/tmp/tmpb3fnwk/test4.py`:
a=1
def f():
    test = 1
    return test
b='\u20ac\xc6\xbe\xf7\xdf'
The following commands were written to file `/tmp/tmpb3fnwk/test.py`:
a=1
def f():
    test = 1
    return test
b='\u20ac\xc6\xbe\xf7\xdf'

--------------------- >> end captured stdout << ----------------------

That's in Python 3.2, but it looks like it's failing in all versions of Python.

@takluyver
Copy link
Member

Oh, I think it's just because %save is now adding a newline at the end of the file, which the test doesn't expect. Could you just change the test?

@doda-zz
Copy link
Author

doda-zz commented Jul 14, 2012

here you go!

@takluyver
Copy link
Member

Test results for commit d5e7d79 merged into master
Platform: linux2

  • python2.7: OK (libraries not available: oct2py rpy2)
  • python3.1: OK (libraries not available: cython matplotlib numpy oct2py pymongo qt rpy2 wx wx.aui zmq)
  • python3.2: Failed, log at https://gist.github.com/3113637 (libraries not available: cython oct2py pymongo rpy2 wx wx.aui)

Not available for testing: python2.6

@takluyver
Copy link
Member

The timeout error is probably unrelated. This all looks OK now, but I'll give it a day or so in case anyone else wants to look over it.

@doda-zz
Copy link
Author

doda-zz commented Jul 14, 2012

sure thanks for the quick replies!

takluyver added a commit that referenced this pull request Jul 16, 2012
Add option append (-a) to %save
@takluyver takluyver merged commit 7efca4e into ipython:master Jul 16, 2012
@takluyver
Copy link
Member

Merged. Thanks for the contribution!

mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
Add option append (-a) to %save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants