Python interactive in your program
By Maxime Biais, Monday 5 February 2007 at 09:28 :: Python :: #26 :: rss
I'm working on a program that take a very long time to load and I can't test it by run/test/(fail|stop). Fortunately in the Python standard library, I've found the interactive interpreter objects.
import readline import rlcompleter from code import interact def start_interactive(banner) foo = Foo() foo.very_long_starting_function() readline.parse_and_bind("tab: complete") interact(banner=banner, local=locals())
It's start an interactive interpreter with completion (thanks to readline and rlcompleter modules).
>>> print foo.# (<TAB> pressed) foo.__class__ foo.__doc__ foo.__module__ foo.__init__ foo.moo foo.goo >>>
Considering you're not developing the Foo class; you may use your standard editor (instead of coding in the interactive interpreter) and then use the reload() function to reload the module you modified. With the file bar.py:
def bar(): return "bar bar"
>>> import bar >>> bar.bar() "bar bar"
Modify the bar function: replace return "bar bar" by return "foo foo" and then, reload the bar module:
>>> reload(bar) <module 'bar' from '/home/max/work/blogcode/bar.py'> >>> bar.bar() "foo foo"



Comments
No comment.
Write your comment