473,320 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

help with comparison

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
4 1358
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Al Newton | last post by:
I want to use STL's sort algorithm to sort a string vector. Some of the strings are fairly long (300 to 400 chars) and the vector isn't small (5,000 to 10,000 elements). Naturally, sorting time...
5
by: Dr. Ann Huxtable | last post by:
Hello All, I am reading a CSV (comma seperated value) file into a 2D array. I want to be able to sort multiple columns (ala Excel), so I know for starters, I cant be using the array, I need...
8
by: Jean Jacques | last post by:
Hi , How to check the values of a input fields in a java script when the there are something like this : field1 field2 field3 field4
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
5
by: bruce.james.lee | last post by:
hi i have a problem with integer subtraction in C. printf("%d", c < (a - b)); a is got from a #define and is 0x80000000 and b is got from input and is also 0x80000000. c is ffffffff (-1)....
3
by: baumann | last post by:
hi all, i could not understand the "unnecessary" pointer comparison. /* 207 * min()/max() macros that also do 208 * strict type-checking.. See the 209 * "unnecessary" pointer comparison....
4
by: Darryn Ross | last post by:
Hi, i am trying to run a simple SQL statement from my C# app to return a number of records, however when i do my record count is always zero, but i know there are transactions in there... My...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
1
by: Learner | last post by:
Hello Friends, I need help converting the below C# code snippet to vb.net. case "CreationDate": comparison = new Comparison<MembershipUserWrapper>( delegate(MembershipUserWrapper lhs,...
14
by: pgfdbug | last post by:
First I am learning this as I go so please forgive my ignorance, all is self taught. I was given this as program to run an LED signboard for my fire station. The program is supposed to transmit the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.