[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Is automatic reload of a module available in Python?

R (Chandra) Chandrasekhar

2/17/2010 2:28:00 PM

Dear Folks,

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
5 Answers

Arnaud Delobelle

2/17/2010 2:53:00 PM

0

"R (Chandra) Chandrasekhar" <chyavana@gmail.com> writes:

> Dear Folks,
>
> 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

Jean-Michel Pichavant

2/17/2010 3:14:00 PM

0

R (Chandra) Chandrasekhar wrote:
> Dear Folks,
>
> 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
You will always find people explaining to you how it can be possible if
you really know precisely how python object model works. If you actually
do, you can possibly read their technics.
But my short answer would be "No, there is no reliable way to reload a
module".

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

Terry Reedy

2/17/2010 8:22:00 PM

0

On 2/17/2010 9:27 AM, R (Chandra) Chandrasekhar wrote:
> Dear Folks,
>
> 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)

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


R (Chandra) Chandrasekhar

2/18/2010 4:38:00 PM

0

Arnaud Delobelle wrote:
> 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.

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

Simon Brunning

2/18/2010 4:55:00 PM

0

2010/2/17 Arnaud Delobelle <arnodel@googlemail.com>:
> 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.

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.