0

The function in question is:

switch({source}, condition1, Condition Nth, value1, value Nth)

The Way I need to output it involves using 0 and 1s (True/False) and will look like this:

"switch([.]ALARM,1,0,On,Off, Unknown)"

Where the 1 and 0 need to be ints instead of strings.

The program we have to use at work does not allow me to use .format and I was curious is there a way to make this happen.

Example of what I have tried:

First test (index 0 is the 0 and/or 1s in question for both variables):

formula = "switch({source}, \"" + int(conditionOne[0]) + "\""+  "," + "\"" + int(conditionTwo[0]) + "\""+ "," + "\"" + conditionOne[1] + "\"" + "," + "\"" + conditionTwo[1] + "\"" + ", \"Unknown\")"

Second test (the program reads {} as sets instead of formatting:

    test = "switch({source}, \"" + {0} + "\""+  "," + "\"" + {1} + "\""+ "," + "\"" + conditionOne[1] + "\"" + "," + "\"" + conditionTwo[1] + "\"" + ", \"Unknown\")".format(conditionOne[0],conditionTwo[0])

Edit and Answer:

I reached out to ignition personal and found out they use %d, which I have never used since the updates of python 3. I am guessing that program uses an old form of python?

Here is the code that gets it to work, if anyone else out there uses the ignition program:

test3 =   "switch({source}, %d, %d," % (int(conditionOne[0]),int(conditionTwo[0])) + "\"" + conditionOne[1] + "\"" + "," + "\"" + conditionTwo[1] + "\"" + ", \"Unknown\")"
6
  • 1
    Are you allowed to use f-strings? Commented Dec 29, 2022 at 16:32
  • 1
    ... + str(conditionOne[0]) + ... Commented Dec 29, 2022 at 16:32
  • 1
    "Does not allow you to use .format"? Find a new job. Commented Dec 29, 2022 at 16:33
  • 1
    @MarkRansom since f-strings you don't need format at all actually Commented Dec 29, 2022 at 16:37
  • 1
    @Gameplay the point wasn't .format exactly, it was about arbitrary restrictions. Commented Dec 29, 2022 at 16:41

1 Answer 1

1

Edit and Answer:

I reached out to ignition personal and found out they use %d, which I have never used since the updates of python 3. I am guessing that program uses an old form of python?

Here is the code that gets it to work, if anyone else out there uses the ignition program:

test3 =   "switch({source}, %d, %d," % (int(conditionOne[0]),int(conditionTwo[0])) + "\"" + conditionOne[1] + "\"" + "," + "\"" + conditionTwo[1] + "\"" + ", \"Unknown\")"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.