Python Forum
Use a block of code only one time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use a block of code only one time
#11
import getopt
import sys


# parse command line args
def test_tuple(input_value, t):
    possible_opts = ["desert", "ocean"]
    if input_value in possible_opts:
        if t[possible_opts.index(input_value)] > c_value:
            print("CRITICAL - Active: ", t[possible_opts.index(input_value)])
            sys.exit(2)
        elif t[possible_opts.index(input_value)] > w_value:
            print("WARNING - Active:", t[possible_opts.index(input_value)])
            sys.exit(1)
        else:
            print("OK - Active:", t[possible_opts.index(input_value)])
            sys.exit(0)
    else:
        print("Invalid input")


if __name__ == "__main__":

    t = (786, 200)

    try:
        opts, args = getopt.getopt(sys.argv[1:], "i:w:c:")
    except getopt.GetoptError:
        print("Usage: v2.py -i name -w 10 -c 20")
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-i':
            input_value = arg
        elif opt == '-w':
            w_value = int(arg)
        elif opt == '-c':
            c_value = int(arg)

    # validate args
    if len(sys.argv) == 1:
        print("# Usage:\t -w <warn> -c <crit>")
        sys.exit("No argument pass")
    else:
        test_tuple(input_value, t)
But I suggest you use a dictionary, like:
possible_opts = {
        "desert": 786,
        "ocean": 200
}
Reply
#12
Then you need to make sure idx is set differently for ocean and desert. From your original post it seems you are not setting idx at all. So you need to set that using a conditional based on input_value.

if input_vale == 'desert':
    idx = 0
else:
    idx = 1
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#13
Hi gontajones,

Now it's working. Could you please explain the condition possible_opts.index(input_value)] . I didn't understand the index.

Regards,
rlinux57
Reply
#14
Check this link Data Structures
>>> t = [0,1,2,3,4]
>>> possible_opts = ["a","b","c","d","e"]
>>> t
[0, 1, 2, 3, 4]
>>> possible_opts
['a', 'b', 'c', 'd', 'e']
>>> possible_opts.index("a")
0
>>> possible_opts.index("c")
2
>>> t[0]
0
>>> t[2]
2
>>> t[possible_opts.index("a")]
0
>>> t[possible_opts.index("c")]
2
>>>
Reply
#15
Hi gontajones,

Thank you for helping me.

Regards,
rlinux57
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python multiple try except block in my code -- can we shorten code mg24 10 5,892 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  "If Len(Word) == 0" Code Block in Pig Latin Program new_coder_231013 3 2,017 Jan-02-2022, 06:03 PM
Last Post: deanhystad
  Try,Except,Else to check that user has entered either y or n (Code block pasted) RandomNameGenerator 3 2,302 Jun-29-2021, 08:21 PM
Last Post: RandomNameGenerator
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,165 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Stumped by my own code (ratio & epoch-time calculation). MvGulik 2 2,089 Dec-30-2020, 12:04 AM
Last Post: MvGulik
  Code taking too much time to process ErPipex 11 4,819 Nov-16-2020, 09:42 AM
Last Post: DeaD_EyE
  What is the run time complexity of this code and please explain? samlee916 2 2,258 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  The count variable is giving me a hard time in this code D4isyy 2 1,929 Aug-09-2020, 10:32 PM
Last Post: bowlofred
  Having a hard time combining two parts of code. Coozeki 6 3,010 May-10-2020, 06:50 AM
Last Post: Coozeki
  Block of code, scope of variables and surprising exception arbiel 8 3,338 Apr-06-2020, 07:57 PM
Last Post: arbiel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020