Online Modification of Python Code
Robert Kern
rkern at ucsd.edu
Tue Sep 6 00:46:07 EDT 2005
Bengt Richter wrote:
> What about something on the pattern of (untested!)
>
> import algomodule # first time
>
> def algoreload():
> algosource = algomodule.__file__.replace('.pyc','py')
> algomodule.__dict__.clear()
> try:
> execfile(algosource, algomodule.__dict__)
> algomodule.__file__ = algosource
> return 'ok'
> except Exception, e:
> return '%s: %s -- couldn't execfile %r' %(e.__class__.__name__, e, algosource)
>
> while True:
> cmd = raw_input('cmd > ').strip()
> if cmd == 'reload': print algoreload()
> elif cmd == 'exit': raise SystemExit('ok, exiting ...')
> elif cmd == 'edit': print os.popen('notepad.exe '+algomodule.__file__.replace('.pyc','py')).read() or 'ok'
> else:
> cmd = cmd.split()
> args = map(float, cmd[1:])
> print getattr(algomodule, cmd[0], (lambda name, *ign: 'No such function: %r'%name).__get__(cmd[0], str))(*args)
>
> this would (theoretically ;-) let you type commands like
> sqrt 9
> and have alogomodule.sqrt called with float('9'), and then
> edit
> and edit the module source in notepad, and then
> sqrt 16
> and have the new function called, etc.
>
> The cmd module will let you set up something fancier than above, and obviously you don't have to run notepad ;-)
http://ipython.scipy.org
--
Robert Kern
rkern at ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
More information about the Python-list
mailing list