I am currently developing a python program, let us call it "generic.py",
and I am testing out the functions therein by testing them out
interactively in the python interpreter by invoking python and doing
import generic
Once I hit an error, I need to revise my file and reload the module using
reload(generic)
The difference in syntax between invoking import and reload is really
costing me time and patience.
Therefore, I would like to ask:
1. Is there a method of auto-reloading a module while developing it and
testing it with the interactive python interpreter?
2. Is there a better way of developing a program?
Thank you.
Chandra
Here is a very simple way to improve what you do, which won't require
you to change the way you work or to learn a new paradigm:
Instead of testing your functions interactively, put your testing code
in a file, e.g. 'program_tests.py'. Your can then type
python program_tests.py
at the shell interactive prompt. To perform the tests again, just
re-execute that file. If your tests are divided into
different units, you can put these in functions:
def test_frobz():
#testing code for frobzation of klops
def test_frizz():
#testing code for frizzment of frobzied klops
# etc..
So if you want to keep doing interactive tests, you can import
program_tests and call whichever testing functions you want. You may
even have arguments to those functions to test them with different
parameters.
I know some people will point at more 'pro' ways of testing but this has
the merit of being very straightforward. Then when you move on to more
sophisticated techniques, I think you will understand better the
motivations behind them.
--
Arnaud
Still there is 'reload' builting function you can call on any module, by
you must understand that it won't affect any other object that the
module itself. New objects created from that module will take effects,
but all objects created before won't.
JM
Reload is sufficiently flakey that it has been removed in 3.x. The
problem is that it genearally only *partially* replaces the module, so
that some code uses the old version and some the new. Guido tried to
rewrite it but gave up and removed it. The most sensible way to
completely remove a module is to shutdown and restart the interpreter.
> The difference in syntax between invoking import and reload is really
> costing me time and patience.
>
> Therefore, I would like to ask:
>
> 1. Is there a method of auto-reloading a module while developing it and
> testing it with the interactive python interpreter?
>
> 2. Is there a better way of developing a program?
This is what I now do.
Edit
# xxx/module.py
<incrementally written module code>
def _test():
<incrementally added tests>
if __name__ == '__main__': _test()
with IDLE and hit F5 to run and test. IDLE runs the file in a *fresh*
subinterpreter as the main module and then, whether or not an exception
is raised, switches to interactive mode, so one can interactively test
any objects created (before any exception). The switch to interactive
mode after running a file can also be done with a command line switch
when using CPython directly.
Terry Jan Reedy
It took me some time to cotton on to exactly what you were saying, but
once I grasped it and tried it out, I found it very effective and
time-saving.
Thank you very much Arnaud.
--
Chandra
Oh, I don't know. I like to think I'm fairly "pro" when it comes to
TDD, and this is exactly what I do - a unit test module run from the
shell.
--
Cheers,
Simon B.