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


Messages In This Thread
Use a block of code only one time - by rlinux57 - Sep-18-2018, 10:45 AM
RE: Use a block of code only one time - by rlinux57 - Sep-18-2018, 05:38 PM
RE: Use a block of code only one time - by rlinux57 - Sep-18-2018, 07:22 PM
RE: Use a block of code only one time - by rlinux57 - Sep-19-2018, 06:02 AM
RE: Use a block of code only one time - by rlinux57 - Sep-20-2018, 08:41 AM
RE: Use a block of code only one time - by gontajones - Sep-20-2018, 09:44 AM
RE: Use a block of code only one time - by rlinux57 - Sep-20-2018, 06:37 PM
RE: Use a block of code only one time - by rlinux57 - Sep-21-2018, 12:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python multiple try except block in my code -- can we shorten code mg24 10 6,347 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,123 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,363 Jun-29-2021, 08:21 PM
Last Post: RandomNameGenerator
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,337 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Stumped by my own code (ratio & epoch-time calculation). MvGulik 2 2,177 Dec-30-2020, 12:04 AM
Last Post: MvGulik
  Code taking too much time to process ErPipex 11 5,016 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,329 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  The count variable is giving me a hard time in this code D4isyy 2 1,992 Aug-09-2020, 10:32 PM
Last Post: bowlofred
  Having a hard time combining two parts of code. Coozeki 6 3,153 May-10-2020, 06:50 AM
Last Post: Coozeki
  Block of code, scope of variables and surprising exception arbiel 8 3,473 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