0

I've written a script to be able to run all processes contained within it, but I'd like to be able to run only the desired processes.

I think I'm stumbling into a different use for lists than I've needed in the past, but I'm not quite sure how to go about it.

I've got four functions: foo1, foo2, foo3, and foo4. I'd like to be able to selectively run any of these functions within the python script itself using a specific number users insert using the input() function. A sample of code can be seen below.

ListAllVSs = requests.get('https://' + LMIP + '/access/listvs', verify=False, auth=MyCreds)

ListVSOutput = ListAllVSs.content.splitlines()

for Line in ListVSOutput:
    if b"<Index>" in Line:
        IndexLines.append(Line)

NumbersList = [int(re.search(br"\d+", Integer).group()) for Integer in IndexLines]

for Number in NumbersList:
    VSIDAndValue = (
    ('vs', str(Number)),
    )
    requests.get('https://' + LMIP + '/access/delvs', params=VSIDAndValue, verify=False, auth=MyCreds)

So, this is isn't exactly tied to a single function.

For example, users should insert "1" to run foo1, "2" to run foo2, "3" to run foo3, "4" to run foo4, "5" to run foo1 and foo2, etc. To run all possible combinations of functions, there will be 15 total possible inputs and any number below 0 or above 15 should be deemed as invalid.

I'd like to use "0" to run all four processes, which I think is possible due to how Python numbers its list positions, but I'm alright with this being "15", if needed.

Thanks in advance!

  • 1
    Create a list of the functions, i.e. `funcs = [foo, bar, baz, etc...]` and then call the one you want to invoke via its index with `funcs[index](...args..)`. – martineau Apr 01 '19 at 18:22
  • yesss, I can comment now. If you need an option to run all the functions, maybe just make another item in the dictionary and have it be a function that runs all functions. – Jeffyx Apr 01 '19 at 18:40

2 Answers2

3

you could use bitwise operators to test different combinations of processes:

def foo1():
    print("foo1()")

def foo2():
    print("foo2()")

def foo3():
    print("foo3()")

def foo4():
    print("foo4()")

def input(n):
    if n & (1 << 0) != 0: # (1 << 0) == 1
        foo1()
    if n & (1 << 1) != 0: # (1 << 1) == 2
        foo2()
    if n & (1 << 2) != 0: # (1 << 2) == 4
        foo3()
    if n & (1 << 3) != 0: # (1 << 3) == 8
        foo4()

In this example all numbers from 1 to 15 will give some output. 1 will print "foo1()" 4 will print "foo3()" ... if you want to print "foo1()" and "foo3()" you should call input() with parameter 5 (1 + 4)

The key to understand this is to note that each function fooX is called with the following numbers in base 2:

n = 110 = 00012 -> foo1()
n = 210 = 00102 -> foo2()
n = 410 = 01002 -> foo3()
n = 810 = 10002 -> foo4()

and the sum of them is just a boolean or (|) operation applied bit to bit:

(1 + 4)10 = 00012 | 01002 = 01012 = 510

Then you compare if a given bit is set to 1 with a & (bitwise and) operator and call the corresponding fooX().

hpc
  • 41
  • 4
  • This seems to be on the right track, but I'm having a hard time reconciling how python will distinguish between 1 and 4 vs 2 and 3 if I insert parameter 5. If you could shed some light, I'd really appreciate it! – Bill DeCastro Apr 01 '19 at 20:02
  • @BillDeCastro note that foo1 is called with n = 1, foo2 with n = 2, foo3 with n = 4 and foo4 with n = 8. if you want to test different combinations of them, just sum their corresponding n. Example: if you want to try foo2 and foo3 just use as n 2+4 = 6 – hpc Apr 01 '19 at 20:27
1

Use a dictionary, sorry I can't comment on your question I'm too new.

dict_functions = { '1' : foo1, '2' : foo2, '3' : foo3,  '4' : foo4}

user_input = input("What Number foo do you want to run? ")

dict_functions[user_input]()

If you can't figure this out can you give me some sample code? Also give credit to this guy if you want to put a limit on your input Limiting user input to a range in Python

Jeffyx
  • 78
  • 2
  • 9
  • 3
    You might want string keys or integer inputs. – Austin Apr 01 '19 at 18:21
  • sorry I didn't read the part where 0 - 15 should be invalid, i'm sure answering that would be a duplicate https://stackoverflow.com/questions/19821273/limiting-user-input-to-a-range-in-python – Jeffyx Apr 01 '19 at 18:24
  • The sample code is added to the original post. Too long to post here, sadly. To summarize, what I'm trying to do with this particular block of code is to iterate through a list of XML strings to eradicate the numbers from "ListVSOutput" and use it within the final request to actually delete the item in question. In addition to this, I've got three other blocks that do similar things, hence the four different functions. These aren't exactly tied to a single function, but I could recode the script, if necessary to. – Bill DeCastro Apr 01 '19 at 20:06