##// END OF EJS Templates
Simplifying code in several places.
Thomas Kluyver -
Show More
@@ -453,14 +453,9 b" def magic_history(self, parameter_s = ''):"
453 print('%s:%s' % (str(in_num).ljust(width), line_sep[multiline]),
453 print('%s:%s' % (str(in_num).ljust(width), line_sep[multiline]),
454 file=outfile, end='')
454 file=outfile, end='')
455 if pyprompts:
455 if pyprompts:
456 inline = ">>> " + inline
456 print(">>> ", end="", file=outfile)
457 if multiline:
457 if multiline:
458 lines = inline.splitlines()
458 inline = "\n... ".join(inline.splitlines()) + "\n..."
459 print('\n... '.join(lines), file=outfile)
460 print('... ', file=outfile)
461 else:
462 print(inline, file=outfile)
463 else:
464 print(inline, file=outfile)
459 print(inline, file=outfile)
465 if print_outputs and output:
460 if print_outputs and output:
466 print(repr(output), file=outfile)
461 print(repr(output), file=outfile)
@@ -57,7 +57,7 b' import IPython.utils.io'
57 from IPython.utils.path import get_py_filename
57 from IPython.utils.path import get_py_filename
58 from IPython.utils.process import arg_split, abbrev_cwd
58 from IPython.utils.process import arg_split, abbrev_cwd
59 from IPython.utils.terminal import set_term_title
59 from IPython.utils.terminal import set_term_title
60 from IPython.utils.text import LSString, SList, StringTypes, format_screen
60 from IPython.utils.text import LSString, SList, format_screen
61 from IPython.utils.timing import clock, clock2
61 from IPython.utils.timing import clock, clock2
62 from IPython.utils.warn import warn, error
62 from IPython.utils.warn import warn, error
63 from IPython.utils.ipstruct import Struct
63 from IPython.utils.ipstruct import Struct
@@ -2284,9 +2284,10 b' Currently the magic system has the following functions:\\n"""'
2284
2284
2285 # by default this is done with temp files, except when the given
2285 # by default this is done with temp files, except when the given
2286 # arg is a filename
2286 # arg is a filename
2287 use_temp = 1
2287 use_temp = True
2288
2288
2289 if re.match(r'\d',args):
2289 data = ''
2290 if args[0].isdigit():
2290 # Mode where user specifies ranges of lines, like in %macro.
2291 # Mode where user specifies ranges of lines, like in %macro.
2291 # This means that you can't edit files whose names begin with
2292 # This means that you can't edit files whose names begin with
2292 # numbers this way. Tough.
2293 # numbers this way. Tough.
@@ -2294,8 +2295,7 b' Currently the magic system has the following functions:\\n"""'
2294 data = '\n'.join(self.extract_input_slices(ranges,opts_r))
2295 data = '\n'.join(self.extract_input_slices(ranges,opts_r))
2295 elif args.endswith('.py'):
2296 elif args.endswith('.py'):
2296 filename = make_filename(args)
2297 filename = make_filename(args)
2297 data = ''
2298 use_temp = False
2298 use_temp = 0
2299 elif args:
2299 elif args:
2300 try:
2300 try:
2301 # Load the parameter given as a variable. If not a string,
2301 # Load the parameter given as a variable. If not a string,
@@ -2303,7 +2303,7 b' Currently the magic system has the following functions:\\n"""'
2303
2303
2304 #print '*** args',args,'type',type(args) # dbg
2304 #print '*** args',args,'type',type(args) # dbg
2305 data = eval(args,self.shell.user_ns)
2305 data = eval(args, self.shell.user_ns)
2306 if not type(data) in StringTypes:
2306 if not isinstance(data, basestring):
2307 raise DataIsObject
2307 raise DataIsObject
2308
2308
2309 except (NameError,SyntaxError):
2309 except (NameError,SyntaxError):
@@ -2313,11 +2313,9 b' Currently the magic system has the following functions:\\n"""'
2313 warn("Argument given (%s) can't be found as a variable "
2313 warn("Argument given (%s) can't be found as a variable "
2314 "or as a filename." % args)
2314 "or as a filename." % args)
2315 return
2315 return
2316 use_temp = False
2316
2317
2317 data = ''
2318 use_temp = 0
2319 except DataIsObject:
2318 except DataIsObject:
2320
2321 # macros have a special edit function
2319 # macros have a special edit function
2322 if isinstance(data,Macro):
2320 if isinstance(data, Macro):
2323 self._edit_macro(args,data)
2321 self._edit_macro(args,data)
@@ -2358,9 +2356,7 b' Currently the magic system has the following functions:\\n"""'
2358 warn('The file `%s` where `%s` was defined cannot '
2356 warn('The file `%s` where `%s` was defined cannot '
2359 'be read.' % (filename,data))
2357 'be read.' % (filename,data))
2360 return
2358 return
2361 use_temp = 0
2359 use_temp = False
2362 else:
2363 data = ''
2364
2360
2365 if use_temp:
2361 if use_temp:
2366 filename = self.shell.mktempfile(data)
2362 filename = self.shell.mktempfile(data)
@@ -2383,7 +2379,7 b' Currently the magic system has the following functions:\\n"""'
2383 if args.strip() == 'pasted_block':
2379 if args.strip() == 'pasted_block':
2384 self.shell.user_ns['pasted_block'] = file_read(filename)
2380 self.shell.user_ns['pasted_block'] = file_read(filename)
2385
2381
2386 if opts.has_key('x'): # -x prevents actual execution
2382 if 'x' in opts: # -x prevents actual execution
2387 print
2383 print
2388 else:
2384 else:
2389 print 'done. Executing edited code...'
2385 print 'done. Executing edited code...'
@@ -19,7 +19,6 b' import __main__'
19 import os
19 import os
20 import re
20 import re
21 import shutil
21 import shutil
22 import types
23
22
24 from IPython.external.path import path
23 from IPython.external.path import path
25
24
@@ -30,8 +29,6 b' from IPython.utils.data import flatten'
30 # Code
29 # Code
31 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
32
31
33 StringTypes = types.StringTypes
34
35
32
36 def unquote_ends(istr):
33 def unquote_ends(istr):
37 """Remove a single pair of quotes from the endpoints of a string."""
34 """Remove a single pair of quotes from the endpoints of a string."""
@@ -325,7 +322,7 b' def qw(words,flat=0,sep=None,maxsplit=-1):'
325 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q']
322 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q']
326 """
323 """
327
324
328 if type(words) in StringTypes:
325 if isinstance(words, basestring):
329 return [word.strip() for word in words.split(sep,maxsplit)
326 return [word.strip() for word in words.split(sep,maxsplit)
330 if word and not word.isspace() ]
327 if word and not word.isspace() ]
331 if flat:
328 if flat:
@@ -345,7 +342,7 b' def qw_lol(indata):'
345 We need this to make sure the modules_some keys *always* end up as a
342 We need this to make sure the modules_some keys *always* end up as a
346 list of lists."""
343 list of lists."""
347
344
348 if type(indata) in StringTypes:
345 if isinstance(indata, basestring):
349 return [qw(indata)]
346 return [qw(indata)]
350 else:
347 else:
351 return qw(indata)
348 return qw(indata)
@@ -18,7 +18,6 b' from __future__ import print_function'
18 # Stdlib
18 # Stdlib
19 import inspect
19 import inspect
20 import os
20 import os
21 import re
22
21
23 # Our own
22 # Our own
24 from IPython.core.interactiveshell import (
23 from IPython.core.interactiveshell import (
@@ -31,7 +30,6 b' from IPython.core.macro import Macro'
31 from IPython.core.payloadpage import install_payload_page
30 from IPython.core.payloadpage import install_payload_page
32 from IPython.utils import io
31 from IPython.utils import io
33 from IPython.utils.path import get_py_filename
32 from IPython.utils.path import get_py_filename
34 from IPython.utils.text import StringTypes
35 from IPython.utils.traitlets import Instance, Type, Dict
33 from IPython.utils.traitlets import Instance, Type, Dict
36 from IPython.utils.warn import warn
34 from IPython.utils.warn import warn
37 from IPython.zmq.session import extract_header
35 from IPython.zmq.session import extract_header
@@ -433,9 +431,10 b' class ZMQInteractiveShell(InteractiveShell):'
433
431
434 # by default this is done with temp files, except when the given
432 # by default this is done with temp files, except when the given
435 # arg is a filename
433 # arg is a filename
436 use_temp = 1
434 use_temp = True
437
435
438 if re.match(r'\d',args):
436 data = ''
437 if args[0].isdigit():
439 # Mode where user specifies ranges of lines, like in %macro.
438 # Mode where user specifies ranges of lines, like in %macro.
440 # This means that you can't edit files whose names begin with
439 # This means that you can't edit files whose names begin with
441 # numbers this way. Tough.
440 # numbers this way. Tough.
@@ -443,8 +442,7 b' class ZMQInteractiveShell(InteractiveShell):'
443 data = ''.join(self.extract_input_slices(ranges,opts_r))
442 data = ''.join(self.extract_input_slices(ranges,opts_r))
444 elif args.endswith('.py'):
443 elif args.endswith('.py'):
445 filename = make_filename(args)
444 filename = make_filename(args)
446 data = ''
445 use_temp = False
447 use_temp = 0
448 elif args:
446 elif args:
449 try:
447 try:
450 # Load the parameter given as a variable. If not a string,
448 # Load the parameter given as a variable. If not a string,
@@ -452,7 +450,7 b' class ZMQInteractiveShell(InteractiveShell):'
452
450
453 #print '*** args',args,'type',type(args) # dbg
451 #print '*** args',args,'type',type(args) # dbg
454 data = eval(args,self.shell.user_ns)
452 data = eval(args, self.shell.user_ns)
455 if not type(data) in StringTypes:
453 if not isinstance(data, basestring):
456 raise DataIsObject
454 raise DataIsObject
457
455
458 except (NameError,SyntaxError):
456 except (NameError,SyntaxError):
@@ -462,11 +460,9 b' class ZMQInteractiveShell(InteractiveShell):'
462 warn("Argument given (%s) can't be found as a variable "
460 warn("Argument given (%s) can't be found as a variable "
463 "or as a filename." % args)
461 "or as a filename." % args)
464 return
462 return
463 use_temp = False
465
464
466 data = ''
467 use_temp = 0
468 except DataIsObject:
465 except DataIsObject:
469
470 # macros have a special edit function
466 # macros have a special edit function
471 if isinstance(data,Macro):
467 if isinstance(data, Macro):
472 self._edit_macro(args,data)
468 self._edit_macro(args,data)
@@ -507,9 +503,7 b' class ZMQInteractiveShell(InteractiveShell):'
507 warn('The file `%s` where `%s` was defined cannot '
503 warn('The file `%s` where `%s` was defined cannot '
508 'be read.' % (filename,data))
504 'be read.' % (filename,data))
509 return
505 return
510 use_temp = 0
506 use_temp = False
511 else:
512 data = ''
513
507
514 if use_temp:
508 if use_temp:
515 filename = self.shell.mktempfile(data)
509 filename = self.shell.mktempfile(data)
General Comments 0
You need to be logged in to leave comments. Login now