Show More
@@ -1,29 +1,29 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | A backwards compatibility layer for IPython.Shell. |
|
4 | A backwards compatibility layer for IPython.Shell. | |
5 |
|
5 | |||
6 | Previously, IPython had an IPython.Shell module. IPython.Shell has been moved |
|
6 | Previously, IPython had an IPython.Shell module. IPython.Shell has been moved | |
7 | to IPython.core.shell and is being refactored. This new module is provided |
|
7 | to IPython.core.shell and is being refactored. This new module is provided | |
8 | for backwards compatability. We strongly encourage everyone to start using |
|
8 | for backwards compatability. We strongly encourage everyone to start using | |
9 | the new code in IPython.core.shell. |
|
9 | the new code in IPython.core.shell. | |
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Copyright (C) 2008-2009 The IPython Development Team |
|
13 | # Copyright (C) 2008-2009 The IPython Development Team | |
14 | # |
|
14 | # | |
15 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | # Distributed under the terms of the BSD License. The full license is in | |
16 | # the file COPYING, distributed as part of this software. |
|
16 | # the file COPYING, distributed as part of this software. | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | from warnings import warn |
|
19 | from warnings import warn | |
20 |
|
20 | |||
21 | msg = """ |
|
21 | msg = """ | |
22 | This module (IPython.Shell) has been moved to a new location |
|
22 | This module (IPython.Shell) has been moved to a new location | |
23 | (IPython.core.shell) and is being refactored. Please update your code |
|
23 | (IPython.core.shell) and is being refactored. Please update your code | |
24 | to use the new IPython.core.shell module""" |
|
24 | to use the new IPython.core.shell module""" | |
25 |
|
25 | |||
26 | warn(msg, category=DeprecationWarning, stacklevel=1) |
|
26 | warn(msg, category=DeprecationWarning, stacklevel=1) | |
27 |
|
27 | |||
28 |
from IPython.core.shell import |
|
28 | from IPython.core.shell import start, IPShell, IPShellEmbed | |
29 |
|
29 |
@@ -1,131 +1,131 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 |
|
2 | |||
3 | """An example of how to embed an IPython shell into a running program. |
|
3 | """An example of how to embed an IPython shell into a running program. | |
4 |
|
4 | |||
5 | Please see the documentation in the IPython.Shell module for more details. |
|
5 | Please see the documentation in the IPython.Shell module for more details. | |
6 |
|
6 | |||
7 | The accompanying file example-embed-short.py has quick code fragments for |
|
7 | The accompanying file example-embed-short.py has quick code fragments for | |
8 | embedding which you can cut and paste in your code once you understand how |
|
8 | embedding which you can cut and paste in your code once you understand how | |
9 | things work. |
|
9 | things work. | |
10 |
|
10 | |||
11 | The code in this file is deliberately extra-verbose, meant for learning.""" |
|
11 | The code in this file is deliberately extra-verbose, meant for learning.""" | |
12 |
|
12 | |||
13 | # The basics to get you going: |
|
13 | # The basics to get you going: | |
14 |
|
14 | |||
15 | # IPython sets the __IPYTHON__ variable so you can know if you have nested |
|
15 | # IPython sets the __IPYTHON__ variable so you can know if you have nested | |
16 | # copies running. |
|
16 | # copies running. | |
17 |
|
17 | |||
18 | # Try running this code both at the command line and from inside IPython (with |
|
18 | # Try running this code both at the command line and from inside IPython (with | |
19 | # %run example-embed.py) |
|
19 | # %run example-embed.py) | |
20 | try: |
|
20 | try: | |
21 | __IPYTHON__ |
|
21 | __IPYTHON__ | |
22 | except NameError: |
|
22 | except NameError: | |
23 | nested = 0 |
|
23 | nested = 0 | |
24 | args = [''] |
|
24 | args = [''] | |
25 | else: |
|
25 | else: | |
26 | print "Running nested copies of IPython." |
|
26 | print "Running nested copies of IPython." | |
27 | print "The prompts for the nested copy have been modified" |
|
27 | print "The prompts for the nested copy have been modified" | |
28 | nested = 1 |
|
28 | nested = 1 | |
29 | # what the embedded instance will see as sys.argv: |
|
29 | # what the embedded instance will see as sys.argv: | |
30 | args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ', |
|
30 | args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ', | |
31 | '-po','Out<\\#>: ','-nosep'] |
|
31 | '-po','Out<\\#>: ','-nosep'] | |
32 |
|
32 | |||
33 | # First import the embeddable shell class |
|
33 | # First import the embeddable shell class | |
34 |
from IPython. |
|
34 | from IPython.core.shell import IPShellEmbed | |
35 |
|
35 | |||
36 | # Now create an instance of the embeddable shell. The first argument is a |
|
36 | # Now create an instance of the embeddable shell. The first argument is a | |
37 | # string with options exactly as you would type them if you were starting |
|
37 | # string with options exactly as you would type them if you were starting | |
38 | # IPython at the system command line. Any parameters you want to define for |
|
38 | # IPython at the system command line. Any parameters you want to define for | |
39 | # configuration can thus be specified here. |
|
39 | # configuration can thus be specified here. | |
40 | ipshell = IPShellEmbed(args, |
|
40 | ipshell = IPShellEmbed(args, | |
41 | banner = 'Dropping into IPython', |
|
41 | banner = 'Dropping into IPython', | |
42 | exit_msg = 'Leaving Interpreter, back to program.') |
|
42 | exit_msg = 'Leaving Interpreter, back to program.') | |
43 |
|
43 | |||
44 | # Make a second instance, you can have as many as you want. |
|
44 | # Make a second instance, you can have as many as you want. | |
45 | if nested: |
|
45 | if nested: | |
46 | args[1] = 'In2<\\#>' |
|
46 | args[1] = 'In2<\\#>' | |
47 | else: |
|
47 | else: | |
48 | args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ', |
|
48 | args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ', | |
49 | '-po','Out<\\#>: ','-nosep'] |
|
49 | '-po','Out<\\#>: ','-nosep'] | |
50 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') |
|
50 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') | |
51 |
|
51 | |||
52 | print '\nHello. This is printed from the main controller program.\n' |
|
52 | print '\nHello. This is printed from the main controller program.\n' | |
53 |
|
53 | |||
54 | # You can then call ipshell() anywhere you need it (with an optional |
|
54 | # You can then call ipshell() anywhere you need it (with an optional | |
55 | # message): |
|
55 | # message): | |
56 | ipshell('***Called from top level. ' |
|
56 | ipshell('***Called from top level. ' | |
57 | 'Hit Ctrl-D to exit interpreter and continue program.\n' |
|
57 | 'Hit Ctrl-D to exit interpreter and continue program.\n' | |
58 | 'Note that if you use %kill_embedded, you can fully deactivate\n' |
|
58 | 'Note that if you use %kill_embedded, you can fully deactivate\n' | |
59 | 'This embedded instance so it will never turn on again') |
|
59 | 'This embedded instance so it will never turn on again') | |
60 |
|
60 | |||
61 | print '\nBack in caller program, moving along...\n' |
|
61 | print '\nBack in caller program, moving along...\n' | |
62 |
|
62 | |||
63 | #--------------------------------------------------------------------------- |
|
63 | #--------------------------------------------------------------------------- | |
64 | # More details: |
|
64 | # More details: | |
65 |
|
65 | |||
66 | # IPShellEmbed instances don't print the standard system banner and |
|
66 | # IPShellEmbed instances don't print the standard system banner and | |
67 | # messages. The IPython banner (which actually may contain initialization |
|
67 | # messages. The IPython banner (which actually may contain initialization | |
68 | # messages) is available as <instance>.IP.BANNER in case you want it. |
|
68 | # messages) is available as <instance>.IP.BANNER in case you want it. | |
69 |
|
69 | |||
70 | # IPShellEmbed instances print the following information everytime they |
|
70 | # IPShellEmbed instances print the following information everytime they | |
71 | # start: |
|
71 | # start: | |
72 |
|
72 | |||
73 | # - A global startup banner. |
|
73 | # - A global startup banner. | |
74 |
|
74 | |||
75 | # - A call-specific header string, which you can use to indicate where in the |
|
75 | # - A call-specific header string, which you can use to indicate where in the | |
76 | # execution flow the shell is starting. |
|
76 | # execution flow the shell is starting. | |
77 |
|
77 | |||
78 | # They also print an exit message every time they exit. |
|
78 | # They also print an exit message every time they exit. | |
79 |
|
79 | |||
80 | # Both the startup banner and the exit message default to None, and can be set |
|
80 | # Both the startup banner and the exit message default to None, and can be set | |
81 | # either at the instance constructor or at any other time with the |
|
81 | # either at the instance constructor or at any other time with the | |
82 | # set_banner() and set_exit_msg() methods. |
|
82 | # set_banner() and set_exit_msg() methods. | |
83 |
|
83 | |||
84 | # The shell instance can be also put in 'dummy' mode globally or on a per-call |
|
84 | # The shell instance can be also put in 'dummy' mode globally or on a per-call | |
85 | # basis. This gives you fine control for debugging without having to change |
|
85 | # basis. This gives you fine control for debugging without having to change | |
86 | # code all over the place. |
|
86 | # code all over the place. | |
87 |
|
87 | |||
88 | # The code below illustrates all this. |
|
88 | # The code below illustrates all this. | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | # This is how the global banner and exit_msg can be reset at any point |
|
91 | # This is how the global banner and exit_msg can be reset at any point | |
92 | ipshell.set_banner('Entering interpreter - New Banner') |
|
92 | ipshell.set_banner('Entering interpreter - New Banner') | |
93 | ipshell.set_exit_msg('Leaving interpreter - New exit_msg') |
|
93 | ipshell.set_exit_msg('Leaving interpreter - New exit_msg') | |
94 |
|
94 | |||
95 | def foo(m): |
|
95 | def foo(m): | |
96 | s = 'spam' |
|
96 | s = 'spam' | |
97 | ipshell('***In foo(). Try @whos, or print s or m:') |
|
97 | ipshell('***In foo(). Try @whos, or print s or m:') | |
98 | print 'foo says m = ',m |
|
98 | print 'foo says m = ',m | |
99 |
|
99 | |||
100 | def bar(n): |
|
100 | def bar(n): | |
101 | s = 'eggs' |
|
101 | s = 'eggs' | |
102 | ipshell('***In bar(). Try @whos, or print s or n:') |
|
102 | ipshell('***In bar(). Try @whos, or print s or n:') | |
103 | print 'bar says n = ',n |
|
103 | print 'bar says n = ',n | |
104 |
|
104 | |||
105 | # Some calls to the above functions which will trigger IPython: |
|
105 | # Some calls to the above functions which will trigger IPython: | |
106 | print 'Main program calling foo("eggs")\n' |
|
106 | print 'Main program calling foo("eggs")\n' | |
107 | foo('eggs') |
|
107 | foo('eggs') | |
108 |
|
108 | |||
109 | # The shell can be put in 'dummy' mode where calls to it silently return. This |
|
109 | # The shell can be put in 'dummy' mode where calls to it silently return. This | |
110 | # allows you, for example, to globally turn off debugging for a program with a |
|
110 | # allows you, for example, to globally turn off debugging for a program with a | |
111 | # single call. |
|
111 | # single call. | |
112 | ipshell.set_dummy_mode(1) |
|
112 | ipshell.set_dummy_mode(1) | |
113 | print '\nTrying to call IPython which is now "dummy":' |
|
113 | print '\nTrying to call IPython which is now "dummy":' | |
114 | ipshell() |
|
114 | ipshell() | |
115 | print 'Nothing happened...' |
|
115 | print 'Nothing happened...' | |
116 | # The global 'dummy' mode can still be overridden for a single call |
|
116 | # The global 'dummy' mode can still be overridden for a single call | |
117 | print '\nOverriding dummy mode manually:' |
|
117 | print '\nOverriding dummy mode manually:' | |
118 | ipshell(dummy=0) |
|
118 | ipshell(dummy=0) | |
119 |
|
119 | |||
120 | # Reactivate the IPython shell |
|
120 | # Reactivate the IPython shell | |
121 | ipshell.set_dummy_mode(0) |
|
121 | ipshell.set_dummy_mode(0) | |
122 |
|
122 | |||
123 | print 'You can even have multiple embedded instances:' |
|
123 | print 'You can even have multiple embedded instances:' | |
124 | ipshell2() |
|
124 | ipshell2() | |
125 |
|
125 | |||
126 | print '\nMain program calling bar("spam")\n' |
|
126 | print '\nMain program calling bar("spam")\n' | |
127 | bar('spam') |
|
127 | bar('spam') | |
128 |
|
128 | |||
129 | print 'Main program finished. Bye!' |
|
129 | print 'Main program finished. Bye!' | |
130 |
|
130 | |||
131 | #********************** End of file <example-embed.py> *********************** |
|
131 | #********************** End of file <example-embed.py> *********************** |
General Comments 0
You need to be logged in to leave comments.
Login now