Permalink
Browse files

Simplifying code in several places.

  • Loading branch information...
takluyver committed Feb 5, 2011
1 parent f030c17 commit 79555a1f9d57efaa4db062325251904ab8315feb
Showing with 27 additions and 45 deletions.
  1. +3 −8 IPython/core/history.py
  2. +12 −16 IPython/core/magic.py
  3. +2 −5 IPython/utils/text.py
  4. +10 −16 IPython/zmq/zmqshell.py
View
@@ -442,15 +442,10 @@ def magic_history(self, parameter_s = ''):
print('%s:%s' % (str(in_num).ljust(width), line_sep[multiline]),
file=outfile, end='')
if pyprompts:
- inline = ">>> " + inline
+ print(">>> ", end="", file=outfile)
if multiline:
- lines = inline.splitlines()
- print('\n... '.join(lines), file=outfile)
- print('... ', file=outfile)
- else:
- print(inline, file=outfile)
- else:
- print(inline, file=outfile)
+ inline = "\n... ".join(inline.splitlines()) + "\n..."
+ print(inline, file=outfile)
if print_outputs and output:
print(repr(output), file=outfile)
View
@@ -57,7 +57,7 @@
from IPython.utils.path import get_py_filename
from IPython.utils.process import arg_split, abbrev_cwd
from IPython.utils.terminal import set_term_title
-from IPython.utils.text import LSString, SList, StringTypes, format_screen
+from IPython.utils.text import LSString, SList, format_screen
from IPython.utils.timing import clock, clock2
from IPython.utils.warn import warn, error
from IPython.utils.ipstruct import Struct
@@ -2218,26 +2218,26 @@ class DataIsObject(Exception): pass
# by default this is done with temp files, except when the given
# arg is a filename
- use_temp = 1
+ use_temp = True
- if re.match(r'\d',args):
+ data = ''
+ if args[0].isdigit():
# Mode where user specifies ranges of lines, like in %macro.
# This means that you can't edit files whose names begin with
# numbers this way. Tough.
ranges = args.split()
data = '\n'.join(self.extract_input_slices(ranges,opts_r))
elif args.endswith('.py'):
filename = make_filename(args)
- data = ''
- use_temp = 0
+ use_temp = False
elif args:
try:
# Load the parameter given as a variable. If not a string,
# process it as an object instead (below)
#print '*** args',args,'type',type(args) # dbg
- data = eval(args,self.shell.user_ns)
- if not type(data) in StringTypes:
+ data = eval(args, self.shell.user_ns)
+ if not isinstance(data, basestring):
raise DataIsObject
except (NameError,SyntaxError):
@@ -2247,13 +2247,11 @@ class DataIsObject(Exception): pass
warn("Argument given (%s) can't be found as a variable "
"or as a filename." % args)
return
-
- data = ''
- use_temp = 0
+ use_temp = False
+
except DataIsObject:
-
# macros have a special edit function
- if isinstance(data,Macro):
+ if isinstance(data, Macro):
self._edit_macro(args,data)
return
@@ -2292,9 +2290,7 @@ class DataIsObject(Exception): pass
warn('The file `%s` where `%s` was defined cannot '
'be read.' % (filename,data))
return
- use_temp = 0
- else:
- data = ''
+ use_temp = False
if use_temp:
filename = self.shell.mktempfile(data)
@@ -2317,7 +2313,7 @@ class DataIsObject(Exception): pass
if args.strip() == 'pasted_block':
self.shell.user_ns['pasted_block'] = file_read(filename)
- if opts.has_key('x'): # -x prevents actual execution
+ if 'x' in opts: # -x prevents actual execution
print
else:
print 'done. Executing edited code...'
View
@@ -19,7 +19,6 @@
import os
import re
import shutil
-import types
from IPython.external.path import path
@@ -30,8 +29,6 @@
# Code
#-----------------------------------------------------------------------------
-StringTypes = types.StringTypes
-
def unquote_ends(istr):
"""Remove a single pair of quotes from the endpoints of a string."""
@@ -325,7 +322,7 @@ def qw(words,flat=0,sep=None,maxsplit=-1):
['a', 'b', '1', '2', 'm', 'n', 'p', 'q']
"""
- if type(words) in StringTypes:
+ if isinstance(words, basestring):
return [word.strip() for word in words.split(sep,maxsplit)
if word and not word.isspace() ]
if flat:
@@ -345,7 +342,7 @@ def qw_lol(indata):
We need this to make sure the modules_some keys *always* end up as a
list of lists."""
- if type(indata) in StringTypes:
+ if isinstance(indata, basestring):
return [qw(indata)]
else:
return qw(indata)
View
@@ -18,7 +18,6 @@
# Stdlib
import inspect
import os
-import re
# Our own
from IPython.core.interactiveshell import (
@@ -31,7 +30,6 @@
from IPython.core.payloadpage import install_payload_page
from IPython.utils import io
from IPython.utils.path import get_py_filename
-from IPython.utils.text import StringTypes
from IPython.utils.traitlets import Instance, Type, Dict
from IPython.utils.warn import warn
from IPython.zmq.session import extract_header
@@ -433,26 +431,26 @@ class DataIsObject(Exception): pass
# by default this is done with temp files, except when the given
# arg is a filename
- use_temp = 1
+ use_temp = True
- if re.match(r'\d',args):
+ data = ''
+ if args[0].isdigit():
# Mode where user specifies ranges of lines, like in %macro.
# This means that you can't edit files whose names begin with
# numbers this way. Tough.
ranges = args.split()
data = ''.join(self.extract_input_slices(ranges,opts_r))
elif args.endswith('.py'):
filename = make_filename(args)
- data = ''
- use_temp = 0
+ use_temp = False
elif args:
try:
# Load the parameter given as a variable. If not a string,
# process it as an object instead (below)
#print '*** args',args,'type',type(args) # dbg
- data = eval(args,self.shell.user_ns)
- if not type(data) in StringTypes:
+ data = eval(args, self.shell.user_ns)
+ if not isinstance(data, basestring):
raise DataIsObject
except (NameError,SyntaxError):
@@ -462,13 +460,11 @@ class DataIsObject(Exception): pass
warn("Argument given (%s) can't be found as a variable "
"or as a filename." % args)
return
-
- data = ''
- use_temp = 0
+ use_temp = False
+
except DataIsObject:
-
# macros have a special edit function
- if isinstance(data,Macro):
+ if isinstance(data, Macro):
self._edit_macro(args,data)
return
@@ -507,9 +503,7 @@ class DataIsObject(Exception): pass
warn('The file `%s` where `%s` was defined cannot '
'be read.' % (filename,data))
return
- use_temp = 0
- else:
- data = ''
+ use_temp = False
if use_temp:
filename = self.shell.mktempfile(data)

0 comments on commit 79555a1

Please sign in to comment.