Sunday, 2 June 2013

Python condition logic

Python condition logic

I'm writing a python program that takes a given sentence in plan English and extracts some commands from it. It's very simple right now, but I was getting some unexpected results from my command parser. After looking into it a bit, it seems my condition-logic wasn't evaluating as I intended it to.
This is a really inelegant way to do it of course, and way too verbose. I'm going to be entirely restructuring it, using possibly neural networks or regular expressions or a combination of them. But I do want to understand the logic behind this error before I move forward, as it's a very import thing to know. Here's a section of the code:
if  (("workspace" or "screen" or "desktop" or "switch")  in command) and (("3" or "three" or "third") in command):
    os.system("xdotool key ctrl+alt+3")
    result = True
The weird thing is that this correct evaluates and enacts the xdotool line if command is "desktop three" but does NOT if command is "switch three" also "workspace 3" works but not "workspace three".
So, my question is, what's happening here? What is the condition flow here, and how is it evaluating? How could I best fix it? I have some ideas (like possibly "workspace" being evaulated simply always to True because it's not binding with "in command" and is being evaluated as a boolean), but I want to get a really solid understanding of it.
Thanks!

No comments:

Post a Comment