[Python-au] Calling two Python Scripts from another script PartII
Gary Reynolds
garyr at dentistry.usyd.edu.au
Sun Nov 23 01:18:02 CET 2003
Duane Hennessy wrote:
> Another way to run python scripts from another script is if the
> scripts you want to run are just a consecutive list of commands
> without a 'main' function or anything then you can more easily execute
> them like the following example...
Realistically guys, I don't like what you are preaching here...
Yes, python (like most languages) has an exec command, but you really
should not use it when calling more python code. Python already has
methods for obtaining functionality from other /modules/ which should be
used instead.
Rather than having straight line code in each of your files, split them
up based on what they do in terms of functionality. You will end up
with code that is intrinsically documented by way of meaningful function
calls, rather than the user needing to open secondary modules to see
what they actually do.
For example:
file = open("search_engine.py", "r")
exec(file.read())
file.close()
Or:
from search_engine import *
se = SearchEngine("http://www.mysite.com.au/")
se.spider()
se.index()
se.commit()
The second example is reasonably obvious to the reader what is going to
happen... but I wouldn't have a clue what my first example does?
Also, when you need to re-run that part of the script, you need to
reload it into memory which will be slower... much better to import
modules rather than scripts...
Where you can, split common bits of functionality into modules and
import them, but make sure there is no straight line code in them - only
classes or functions.
My $0.222 (inc. GST)
Gary
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://starship.python.net/pipermail/python-au/attachments/20031123/23d77740/attachment.htm
More information about the python-au
mailing list