By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

help with comparison

P: n/a
Hi,
Could some one take a look at the below code snipet which keep
failing:

import optparse
p = optparse.OptionParser(description="script to do stuff",
prog="myscript.py", ....)
p.add_option("-c" "--compress", help="0 is noncompress")
function1(options.compress)

here's what the what function definition looks like:
function1(zipfile) :
if (zipfile == 1):
do stuff here with for compress file
else
do stuff here

when I call the script "myscript.py 1", the above test keeps falling
to the else clause. I am thinking the object zipfile is not the same
as "1". Any thoughts as how I should test if the argument being pass
in and parse by optparse is 1 or "0"? Thanks.
Nov 20 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Nov 20, 2:21*pm, tekion <tek...@gmail.comwrote:
Hi,
Could some one take a look at the below code snipet which keep
failing:

import optparse
p = optparse.OptionParser(description="script to do stuff",
prog="myscript.py", ....)
p.add_option("-c" "--compress", help="0 is noncompress")
function1(options.compress)

here's what the what function definition looks like:
function1(zipfile) :
Do some elementary debugging; insert here:
print type(zipfile), repr(zipfile)
print type(1), repr(1)
then work out for yourself what you should do next.
if (zipfile == 1):
* *do stuff here with for compress file
else
* *do stuff here

when I call the script "myscript.py 1", the above test keeps falling
to the else clause. *I am thinking the object zipfile is not the same
as "1". Any thoughts as how I should test if the argument being pass
in and parse by optparse is 1 or "0"? *Thanks.
Nov 20 '08 #2

P: n/a
On Nov 19, 10:21*pm, tekion <tek...@gmail.comwrote:
Hi,
Could some one take a look at the below code snipet which keep
failing:

import optparse
p = optparse.OptionParser(description="script to do stuff",
prog="myscript.py", ....)
p.add_option("-c" "--compress", help="0 is noncompress")
function1(options.compress)

here's what the what function definition looks like:
function1(zipfile) :
if (zipfile == 1):
* *do stuff here with for compress file
else
* *do stuff here

when I call the script "myscript.py 1", the above test keeps falling
to the else clause. *I am thinking the object zipfile is not the same
as "1". Any thoughts as how I should test if the argument being pass
in and parse by optparse is 1 or "0"? *Thanks.
1 (without quotes) is not the same as "1" (with quotes); the first is
an integer, the second a string. optparse returns strings by default,
so the easiest change would be to make the check 'if zipfile == "1"'.

Even better, since it's a boolean option, pass action="store_true" to
p.add_option(). The test then is reduced to "if zipfile" and the
program is to be called by "myscript.py -c". Read the docs [1] for
more details.

HTH,
George

[1] http://docs.python.org/library/optpa...option-actions
Nov 20 '08 #3

P: n/a
tekion <te****@gmail.comwrote:
>
Could some one take a look at the below code snipet which keep
failing:

import optparse
p = optparse.OptionParser(description="script to do stuff",
prog="myscript.py", ....)
p.add_option("-c" "--compress", help="0 is noncompress")
function1(options.compress)

here's what the what function definition looks like:
function1(zipfile) :
if (zipfile == 1):
do stuff here with for compress file
else
do stuff here

when I call the script "myscript.py 1", the above test keeps falling
to the else clause. I am thinking the object zipfile is not the same
as "1". Any thoughts as how I should test if the argument being pass
in and parse by optparse is 1 or "0"? Thanks.
There are many problems with this. This is NOT your real code. You're not
showing us the call to parse_args, so we can't see where the "options"
variable comes from, and the declaration of function1 is wrong. When
asking a question like this, you should always include runnable code, to
make it easier for us to reproduce the problem.

However, I will assume that you have this:
options, args = p.parse_args()

Did you even TRY printing out "options" and "args" to make sure that they
contained what you expected? If you had run even one or two experiments,
two things would become clear.

1. When you call "myscript.py 1", you are not providing a value for the
"-c" parameter. The "1" goes into "args", and options.compress will ALWAYS
be None. You would need to say "myscript.py -c 1"

2. Options are strings. It's simple character manipulation. So,
options.compress will NEVER be 1. If you did "myscript.py -c 1", then
options.compress will be "1". That's a string, not a number.

If what you really want is "present" vs "not present", then use this:

function1( options.compress )

def function1( zipfile=None ):
if zipfile:
# compress
else:
# do not compress

However, that will read "0" as true.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Nov 20 '08 #4

P: n/a
On Nov 19, 11:36 pm, George Sakkis <george.sak...@gmail.comwrote:
On Nov 19, 10:21 pm,tekion<tek...@gmail.comwrote:
Hi,
Could some one take a look at the below code snipet which keep
failing:
import optparse
p = optparse.OptionParser(description="script to do stuff",
prog="myscript.py", ....)
p.add_option("-c" "--compress", help="0 is noncompress")
function1(options.compress)
here's what the what function definition looks like:
function1(zipfile) :
if (zipfile == 1):
do stuff here with for compress file
else
do stuff here
when I call the script "myscript.py 1", the above test keeps falling
to the else clause. I am thinking the object zipfile is not the same
as "1". Any thoughts as how I should test if the argument being pass
in and parse by optparse is 1 or "0"? Thanks.

1 (without quotes) is not the same as "1" (with quotes); the first is
an integer, the second a string. optparse returns strings by default,
so the easiest change would be to make the check 'if zipfile == "1"'.

Even better, since it's a boolean option, pass action="store_true" to
p.add_option(). The test then is reduced to "if zipfile" and the
program is to be called by "myscript.py -c". Read the docs [1] for
more details.

HTH,
George

[1]http://docs.python.org/library/optparse.html#standard-option-actions
Thanks. This fixed it.
Nov 20 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.