Save format changed to be executable code

Save to file now outputs executable code (i.e. without the >>> and ... and with
"# OUT: " prepended to all output lines). I never used this feature much but
someone asked for this behaviour.
This commit is contained in:
Bob Farrell
2009-02-23 19:38:44 +00:00
parent 4f8a616674
commit 6f48a2ca66
2 changed files with 18 additions and 1 deletions
+4
View File
@@ -19,6 +19,10 @@ encoding.
Bohdan Vlasyuk sent me a patch that fixes a problem with the above patch from
Mark if sys.__stdout__.encoding didn't exist.
Save to file now outputs executable code (i.e. without the >>> and ... and with
"# OUT: " prepended to all output lines). I never used this feature much but
someone asked for this behaviour.
v0.7.1
======
Added support for a history file, defaults to ~/.pythonhist and 100 lines but
+14 -1
View File
@@ -688,6 +688,19 @@ class Repl(object):
return self.stdout_hist + '\n'
def formatforfile(self, s):
"""Format the stdout buffer to something suitable for writing to disk,
i.e. without >>> and ... at input lines and with "# OUT: " prepended to
output lines."""
def process():
for line in s.split('\n'):
if line.startswith('>>>') or line.startswith('...'):
yield line[4:]
elif line.rstrip():
yield "# OUT: %s" % (line,)
return "\n".join(process())
def write2file(self):
"""Prompt for a filename and write the current contents of the stdout
buffer to disk."""
@@ -697,7 +710,7 @@ class Repl(object):
if fn.startswith('~'):
fn = os.path.expanduser(fn)
s = self.getstdout()
s = self.formatforfile(self.getstdout())
try:
f = open(fn, 'w')