Skip to content

Commit 07f54ee

Browse files
When --interact is given, use IPython.embed() if IPython is available. This allows tab completion & color highlighting, which enables much faster exploration & debugging.
1 parent c0b1d76 commit 07f54ee

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

switch_model/solve.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,27 @@ def debug(type, value, tb):
184184

185185
if instance.options.interact:
186186
m = instance # present the solved model as 'm' for convenience
187-
banner = (
188-
"\n"
189-
"=======================================================================\n"
190-
"Entering interactive Python shell.\n"
191-
"Abstract model is in 'model' variable; \n"
192-
"Solved instance is in 'instance' and 'm' variables.\n"
193-
"Type ctrl-d or exit() to exit shell.\n"
194-
"=======================================================================\n"
195-
)
196-
import code
197-
code.interact(banner=banner, local=dict(list(globals().items()) + list(locals().items())))
187+
hr = os.linesep + "="*60 + os.linesep
188+
banner_msg = os.linesep.join([
189+
"Entering interactive Python shell.",
190+
"Abstract model is in 'model' variable;",
191+
"Solved instance is in 'instance' and 'm' variables.",
192+
"Type ctrl-d or exit() to exit shell.",
193+
])
194+
try:
195+
import IPython
196+
banner_msg += os.linesep + "Use tab to auto-complete"
197+
banner = hr + banner_msg + hr
198+
IPython.embed(
199+
banner1=banner,
200+
exit_msg="Leaving interactive interpretter, returning to program.",
201+
colors=instance.options.interact_color)
202+
except ImportError:
203+
import code
204+
banner = hr + banner_msg + hr
205+
code.interact(
206+
banner=banner,
207+
local=dict(list(globals().items()) + list(locals().items())))
198208

199209

200210
def reload_prior_solution_from_pickle(instance, outdir):
@@ -549,6 +559,14 @@ def define_arguments(argparser):
549559
'--interact', default=False, action='store_true',
550560
help='Enter interactive shell after solving the instance to enable '
551561
'inspection of the solved model.')
562+
try:
563+
import IPython
564+
argparser.add_argument(
565+
'--interact-color', dest="interact_color", default="NoColor",
566+
help='Use this color scheme with the interactive shell.'
567+
'Options: NoColor, LightBG, Linux')
568+
except ImportError:
569+
pass
552570

553571

554572
def add_module_args(parser):

0 commit comments

Comments
 (0)