-3

Trying to figure out the best way to write this in python. I understand why my code isn't working, just not sure on the proper way to fix it. If the VER has more then one, the test will fail b/c it isn't split into single vars.

I want to have a program print out the following based on flags: all, open (only if lvl matches), closed (only if lvl matches), NA (only if all lvls are NA)

This is what I have so far:

#!/usr/bin/python

import getopt, sys

flago=''  #show open tickets
flagl=''  #show out of lvl tickets
flagc=''  #show closed tickets
flaga=''  #show all
fname=''

options, remainder = getopt.gnu_getopt(sys.argv[1:], 'olca')

for opt, arg in options:
    if opt in ('-o'):
        flago = True
    elif opt in ('-l'):
        flagl = True
    elif opt == '-c':
        flagc = True
    elif opt == '-a':
        flaga = True
#    fname = remainder[0]

#only show tickets if lvl matches regardless of status
reqlvls = ("lvl1", "lvl2", "lvl3" )

#loop through a file with following variables
# number, status(open/closed), lvl#,  comments
file = open ('test.conf')
for line in file:
  fields = line.strip().split(":")
  NUM=fields[0]
  STAT=fields[1]
  VER=fields[2]
  COMM=fields[3]

  #print all
  if flaga:
      print NUM, STAT, VER, COMM
  #show open 
  elif flago:
      # VER is messing up if it is set to VER:[lvl1, lvl3]
      # Need to iterate check open and iterate over VER
      if STAT == "open" and VER in reqlvls:
          print NUM, STAT, VER, COMM
  #show NA
  elif flagl:
      if VER not in reqlvls:
          print NUM, STAT, VER, COMM
  #hide if not reqlvl
  elif flagc:
      if STAT  == "closed" and VER in reqlvls:
          print NUM, STAT, VER, COMM

here is the test file:$ cat test.conf:

10:open:lvl1:"should show w/ -o"
11:open:lvl5:"should not show w/ -o, NA b/c of lvl"
12:open:lvl3, lvl5:"should w/ -o, req lvl > na lvl"
13:open:lvl1, lvl3:"should w/ -o"
14:open:lvl4, lvl5:"should not w/ -o NA b/c of lvl"
20:closed:lvl2:"should show closed"
21:closed:lvl5:"should not show w/ -c, NA b/c of lvl"
22:closed:lvl3, lvl5:"should w/ -c, req lvl > na lvl"
23:closed:lvl1, lvl3:"should w/ -c"
24:closed:lvl4, lvl5:"should not w/ -c NA b/c of lvl"

here is output:$

./test.py -a
10 open lvl1 "should show w/ -o"
11 open lvl5 "should not show w/ -o, NA b/c of lvl"
12 open lvl3, lvl5 "should w/ -o, req lvl > na lvl"
13 open lvl1, lvl3 "should w/ -o"
14 open lvl4, lvl5 "should not w/ -o NA b/c of lvl"
20 closed lvl2 "should show closed"
21 closed lvl5 "should not show w/ -c, NA b/c of lvl"
22 closed lvl3, lvl5 "should w/ -c, req lvl > na lvl"
23 closed lvl1, lvl3 "should w/ -c"
24 closed lvl4, lvl5 "should not w/ -c NA b/c of lvl"

$ ./test.py -o #should show 10,12,13
10 open lvl1 "should show w/ -o"

$ ./test.py -c  #should show 20,22,23
20 closed lvl2 "should show closed"

$ ./test.py -l  #should show 11,14,21,24
11 open lvl5 "should not show w/ -o, NA b/c of lvl"
12 open lvl3, lvl5 "should w/ -o, req lvl > na lvl"
13 open lvl1, lvl3 "should w/ -o"
14 open lvl4, lvl5 "should not w/ -o NA b/c of lvl"
21 closed lvl5 "should not show w/ -c, NA b/c of lvl"
22 closed lvl3, lvl5 "should w/ -c, req lvl > na lvl"
23 closed lvl1, lvl3 "should w/ -c"
24 closed lvl4, lvl5 "should not w/ -c NA b/c of lvl"

I have tried using a function like this listed from :

def checkreq(VER):
   for s in  reqlvls:
     for item in VER:
       if item in s:
            print 'LVL: ', s
            return s

then changing:

  elif flago:
      if STAT == "open" and checkreq(VER):

But that doesn't work right either.

got the function from Check if list item contains items from another list

Community
  • 1
  • 1
user3699853
  • 93
  • 1
  • 5

1 Answers1

0

You could try the other way around: checking that any of the levels in reqlvls is in VER. Example:

>>> VER = "lvl2"
>>> sum([x in VER for x in reqlvls])
1
>>> VER = "lvl2, lvl5"
>>> sum([x in VER for x in reqlvls])
1
>>> VER = "lvl6, lvl7"
>>> sum([x in VER for x in reqlvls])
0

So your code will end up like:

elif flago:
      # VER is messing up if it is set to VER:[lvl1, lvl3]
      # Need to iterate check open and iterate over VER
      if STAT == "open" and sum([x in VER for x in reqlvls]):
          print NUM, STAT, VER, COMM
Jose Varez
  • 2,019
  • 1
  • 12
  • 9