News wrote:
> Hi Everyone,
>
>
> The attached code creates client connections to websphere queue managers
> and then processes an inquiry against them.
>
> The program functions when it gets options from the command line.
>
> It also works when pulling the options from a file.
>
> My issue is that it only processes the first line of the file.
>
> Does anyone know why this might be the case?
>
> The use_file() function should be returning each file line.
>
> Any help you can provide on this would be greatly appreciated.
>
> Thanks
>
>
(snip)
>
> #
> # Read a file into a list
Hint : use docstrings for this kind of comments.
> def use_file():
> myfile = open(options.filename)
1/ options.filename should be passed as an argument. Globals are
definitively evil and should be avoided by all means.
2/ open() may raise an error. You may (or not) want to handle this error
here.
> while True:
> line=myfile.readline()
> print line
Why this print statement ? debug ?
> if len(line) == 0:
> myfile.close()
> break
> else:
> return line
This of course will read only the first line. The return statements
terminates execution of the function. And BTW:
> myfile.close()
... This will never execute.
(snip)
> if options.filename is not None:
> line = use_file()
> (options,args) = parser.parse_args(line.split())
Using it that way wouldn't work anyway... and may have unexpected
results, since you're overwriting the existing options object.
> check_params()
> else:
> check_params()
Since you call check_params() anyway, it would simpler to write it:
if options.filename is not None:
do_someting_with_options_here()
check_params()
Now for your question:
if options.filename is not None:
try:
f = open(options.filename)
except IOError, e:
# stdout is for normal program outputs only,
# error messages should go to stderr
print >> sys.stderr, \
"failed to open options file %s : %s" \
% (options.filename, e)
sys.exit(1)
for line in f:
# pass the existing options object in,
# so it's updated with the new args instead of
# being overwritten
options, args = parser.parse_args(line.split(), options=options)
f.close()
del f
check_params()
BTW, check_params() should also takes the options object as argument.
Globals are evil. Definitively. Also, and if I may suggest,
check_options() would be a better name IMVHO.
And while we're at it, you're once again in a bad case of reinventing
the SquareWheel(tm) with the usage() function - optparse takes care of this:
http://docs.python.org/lib/optparse-generating-help.html
(snip)
if options.quiet is False:
You'd better not identity test against True or False. There are lot of
expressions that evaluate to True or False in a boolen context, without
actually *being* neither the True object nor the False object.
-> if not options.quiet:
(snip)
HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
--
http://mail.python.org/mailman/listinfo/python-list