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
#8
Hi,

Below is the code, Please look into it.

if __name__ == "__main__": 
    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)

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

# validate args
if len(sys.argv)==1:
    print("# Usage:\t -w <warn> -c <crit>")
    sys.exit("No argument pass") 
else:
    idx = 0
    test_tuple(input_value, t, idx)
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 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 16,098 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  "If Len(Word) == 0" Code Block in Pig Latin Program new_coder_231013 3 3,279 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 3,398 Jun-29-2021, 08:21 PM
Last Post: RandomNameGenerator
  Assistance with running a few lines of code at an EXACT time nethatar 5 4,967 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Stumped by my own code (ratio & epoch-time calculation). MvGulik 2 3,073 Dec-30-2020, 12:04 AM
Last Post: MvGulik
  Code taking too much time to process ErPipex 11 7,544 Nov-16-2020, 09:42 AM
Last Post: DeaD_EyE
  What is the run time complexity of this code and please explain? samlee916 2 3,055 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  The count variable is giving me a hard time in this code D4isyy 2 2,802 Aug-09-2020, 10:32 PM
Last Post: bowlofred
  Having a hard time combining two parts of code. Coozeki 6 4,566 May-10-2020, 06:50 AM
Last Post: Coozeki
  Block of code, scope of variables and surprising exception arbiel 8 5,014 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