[TriLUG] making a python module
Joseph Mack NA3T
jmack at wm7d.net
Sat Feb 9 15:50:55 EST 2008
On Sat, 9 Feb 2008, Michael Hrivnak wrote:
Thanks for the reply
> Quick point: what you are calling "main()" looks to just be the global
OK. I haven't got the lingo yet.
> When importing from a file, things in the global namespace
> get executed.
so if module volume has function volume_sphere() as well as
the global namespace code that I originally used to test
volume_sphere() when I wrote volume_sphere(), then when I
import volume_sphere() from module volume, the global
namespace code will be imported too and will be run? I would
have expected I'd just import volume_sphere() and the global
namespace code in module volume would be ignored.
> I'm not sure what you mean by wanting people to test your
> function without writing calling code. Maybe this would
in other languages I've used in the past (probably one or
both of C++, Java, but it's been a while) you write and test
your functions one at a time like this.
#code to test volume_sphere()
After testing, you throw this code into your library
directory and call volume_sphere(). main() in file
volume.mylanguage will be ignored, since you didn't start
execution there. However any time you want go back and test
volume_sphere(), you can execute the file volume.mylanguage
using main() in the file, and see that volume_sphere()
passes it's original tests. You don't have to write any
external test code for the file - you leave the test code
from when you wrote the function.
> def volume_sphere(r):
> return volume_sphere
> tester at hisorhermachine:~$ python -i volume_sphere.py
> tester at hisorhermachine:~$
I see. I'm not used to running code from the command line.
You're running volume_sphere() from its module without a
main(). I'm writing files to import volume_sphere() from
volume.py. I'll give your method a whirl.
> The tester still has to call the function, but nothing
> more. Or, you could simply write a small program that
> takes input from the user and calls volume_sphere().
I'm writing a small file that imports and runs
volume_sphere() and it's working fine. I just have to
delete/comment out the original global namespace code that I
used to first test volume_sphere(). I'd like to leave it in
there to run tests later if ever I need too.
Joseph Mack NA3T EME(B,D), FM05lw North Carolina
jmack (at) wm7d (dot) net - azimuthal equidistant map
generator at http://www.wm7d.net/azproj.shtml
Homepage http://www.austintek.com/ It's GNU/Linux!
More information about the TriLUG