Toggle Boolean value based on a triple state filter

Question:

I’m having a brain melting time with this. For some reason I thought it would be easier, but I’m struggling with this.

I have an application that a user can config before running based on desired parameters the user wants to test. There are 3 filters that the user can either turn on, turn off, or toggle.

If the user wants a filter on, he will set the filter in the configuration file to True. If the user wants it off, he sets it to False. If however, the user wishes to run the test with the filter on and than again off, he can set the configuration file to toggle

here are examples of filter1, filter2, and filter3 stored in a list.

toggle_state = ["toggle", "toggle", False]
toggle_state = ["toggle", True, "toggle"]
toggle_state = [False, "toggle", True]
toggle_state = [True, False, False]
...

Any combination should be available for testing purposes.

I have implemented nested while loops to accomplish what I’m attempting to do. However, I have had no real success. I have been able to make it work, with just toggle for all three filters.

I stripped out the functions related to my in a simple MUC script below.

#####CODE BLOCK 1######
import time

def toggle_filters():
    toggle_state = ["toggle", "toggle", "toggle"]
    # toggle_state = ["toggle", "toggle", False]
    # toggle_state = ["toggle", "toggle", True]
    # toggle_state = ["toggle", False, "toggle"]
    # toggle_state = ["toggle", True, "toggle"]
    # toggle_state = [False, "toggle", "toggle"]
    # toggle_state = [True, "toggle", "toggle"]

    filter_state = init_filters(toggle_state)

    idx = 2
    complete = 2
    terminate = False

    while True:
        print(f"t{filter_state[0]:<5}{filter_state[1]:<5}{filter_state[2]:<5}")

        ### do something here with the filters ###

        while True:

            if toggle_state[idx] == "toggle" and not filter_state[idx]:
                filter_state[idx] = True
                break
            elif complete < -1:
                terminate = True
                break
            elif toggle_state[idx] == "toggle" and idx == len(toggle_state) - 1:
                filter_state[idx] = False
                if complete != 0:
                    filter_state[complete] = False
                complete -= 1
                if complete < 0:
                    idx = 1
                else:
                    idx = complete
                continue
            elif toggle_state[idx] == "toggle" and idx != len(toggle_state) - 1:
                if complete == 0 and idx == 0:
                    idx += 1
                idx += 1

        if terminate:
            break


def init_filters(toggle_state):
    """..."""

    filters = []
    for idx in toggle_state:

        if idx == "toggle":
            filters.append(False)
        else:
            filters.append(idx)

    return filters


if __name__ == "__main__":
    toggle_filters()

however, when I’ve attempted to add in static values for the filters, it all goes to hell. I updated the toggle_filter() function to start looking for filters that are not set to toggle.

####CODE BLOCK 2####
import time


def toggle_filters():
    # toggle_state = ["toggle", "toggle", "toggle"]
    toggle_state = ["toggle", "toggle", False]
    # toggle_state = ["toggle", "toggle", True]
    # toggle_state = ["toggle", False, "toggle"]
    # toggle_state = ["toggle", True, "toggle"]
    # toggle_state = [False, "toggle", "toggle"]
    # toggle_state = [True, "toggle", "toggle"]

    filter_state = init_filters(toggle_state)

    idx = 2
    complete = 2
    terminate = False

    while True:
        print(f"t{filter_state[0]:<5}{filter_state[1]:<5}{filter_state[2]:<5}")

        ### do something here with the filters ###

        while True:

            if toggle_state[idx] == "toggle" and not filter_state[idx]:
                filter_state[idx] = True
                break
            elif complete < -1:
                terminate = True
                break
            elif toggle_state[idx] == "toggle" and idx == len(toggle_state) - 1:
                filter_state[idx] = False
                if complete != 0:
                    filter_state[complete] = False
                complete -= 1
                if complete < 0:
                    idx = 1
                else:
                    idx = complete
                continue
            elif toggle_state[idx] == "toggle" and idx != len(toggle_state) - 1:
                if complete == 0 and idx == 0:
                    idx += 1
                idx += 1
            elif toggle_state[idx] != "toggle" and idx == len(toggle_state) - 1:
                if complete != 0:
                    pass
                complete -= 1
                if complete < 0:
                    idx = 1
                else:
                    idx = complete
                continue
            elif toggle_state[idx] != "toggle" and idx != len(toggle_state) - 1:
                if complete == 2 and idx == 2:
                    complete = 1
                    idx = complete
                if complete == 1 and idx == 1:
                    complete = 0
                    idx = complete
                else:
                    idx -= 1

        if terminate:
            break


def init_filters(toggle_state):
    """..."""

    filters = []
    for idx in toggle_state:

        if idx == "toggle":
            filters.append(False)
        else:
            filters.append(idx)

    return filters


if __name__ == "__main__":
    toggle_filters()

Which fails each time, and honestly I imagine I’m approaching this from the wrong direction, just based on the shear number of conditions I have to set. Does anyone have any suggestions as to what I should be looking at?

UPDATE:

if you take the first block of code, it will run as is. The output will look like a truth table.

  0  0  0
  0  0  1
  0  1  0
  0  1  1
  1  0  0
  1  0  1
  1  1  0 
  1  1  1

This is when you set the filters to all toggle.

I’ve updated the second code block as a complete MUC.

here the output looks like this

  0  0  0
  0  1  0
  1  1  0

however it should look like this

  0  0  0
  0  1  0
  1  0  0
  1  1  0

depending on which filter you set static, the ouputs are not correct.

Asked By: Michael

||

Answers:

This gives the same output with less complication. itertools.product is a function that gives you all the combinations of each state listed. A TOGGLE filter can be zero or one, while a FALSE or TRUE state only provides a zero or one state, respectively.

Does this manage the states you want?

import itertools

TOGGLE = [0,1]
FALSE = [0]
TRUE = [1]

def toggle_filters(toggle_state):
    for state in itertools.product(*toggle_state):
        print(*state)

toggle_filters([TOGGLE, TOGGLE, TOGGLE])
print()
toggle_filters([TOGGLE, TOGGLE, FALSE])

Output:

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

0 0 0
0 1 0
1 0 0
1 1 0
Answered By: Mark Tolonen
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.