help with comparison

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tekion

    help with comparison

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

    import optparse
    p = optparse.Option Parser(descript ion="script to do stuff",
    prog="myscript. py", ....)
    p.add_option("-c" "--compress", help="0 is noncompress")
    function1(optio ns.compress)

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

    when I call the script "myscript.p y 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.
  • John Machin

    #2
    Re: help with comparison

    On Nov 20, 2:21 pm, tekion <tek...@gmail.c omwrote:
    Hi,
    Could some one take a look at the below code snipet which keep
    failing:
    >
    import optparse
    p = optparse.Option Parser(descript ion="script to do stuff",
    prog="myscript. py", ....)
    p.add_option("-c" "--compress", help="0 is noncompress")
    function1(optio ns.compress)
    >
    here's what the what function definition looks like:
    function1(zipfi le) :
    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.p y 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.

    Comment

    • George Sakkis

      #3
      Re: help with comparison

      On Nov 19, 10:21 pm, tekion <tek...@gmail.c omwrote:
      Hi,
      Could some one take a look at the below code snipet which keep
      failing:
      >
      import optparse
      p = optparse.Option Parser(descript ion="script to do stuff",
      prog="myscript. py", ....)
      p.add_option("-c" "--compress", help="0 is noncompress")
      function1(optio ns.compress)
      >
      here's what the what function definition looks like:
      function1(zipfi le) :
      if (zipfile == 1):
         do stuff here with for compress file
      else
         do stuff here
      >
      when I call the script "myscript.p y 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_t rue" to
      p.add_option(). The test then is reduced to "if zipfile" and the
      program is to be called by "myscript.p y -c". Read the docs [1] for
      more details.

      HTH,
      George

      [1] http://docs.python.org/library/optpa...option-actions

      Comment

      • Tim Roberts

        #4
        Re: help with comparison

        tekion <tekion@gmail.c omwrote:
        >
        >Could some one take a look at the below code snipet which keep
        >failing:
        >
        >import optparse
        >p = optparse.Option Parser(descript ion="script to do stuff",
        >prog="myscript .py", ....)
        >p.add_option ("-c" "--compress", help="0 is noncompress")
        >function1(opti ons.compress)
        >
        >here's what the what function definition looks like:
        >function1(zipf ile) :
        >if (zipfile == 1):
        do stuff here with for compress file
        >else
        do stuff here
        >
        >when I call the script "myscript.p y 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.p y 1", you are not providing a value for the
        "-c" parameter. The "1" goes into "args", and options.compres s will ALWAYS
        be None. You would need to say "myscript.p y -c 1"

        2. Options are strings. It's simple character manipulation. So,
        options.compres s will NEVER be 1. If you did "myscript.p y -c 1", then
        options.compres s 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.compres s )

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

        However, that will read "0" as true.
        --
        Tim Roberts, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        • tekion

          #5
          Re: help with comparison

          On Nov 19, 11:36 pm, George Sakkis <george.sak...@ gmail.comwrote:
          On Nov 19, 10:21 pm,tekion<tek.. .@gmail.comwrot e:
          >
          >
          >
          Hi,
          Could some one take a look at the below code snipet which keep
          failing:
          >
          import optparse
          p = optparse.Option Parser(descript ion="script to do stuff",
          prog="myscript. py", ....)
          p.add_option("-c" "--compress", help="0 is noncompress")
          function1(optio ns.compress)
          >
          here's what the what function definition looks like:
          function1(zipfi le) :
          if (zipfile == 1):
          do stuff here with for compress file
          else
          do stuff here
          >
          when I call the script "myscript.p y 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_t rue" to
          p.add_option(). The test then is reduced to "if zipfile" and the
          program is to be called by "myscript.p y -c". Read the docs [1] for
          more details.
          >
          HTH,
          George
          >
          [1]http://docs.python.org/library/optparse.html#s tandard-option-actions
          Thanks. This fixed it.

          Comment

          Working...