4

I've been working on a general utility script for a while now that basically just accepts user input to preform some task like opening a program. In this program, I define a name "command" as a raw_input and then use if statements to check the list for a command (small example below).

Constantly using if statements is making the program run slowly and so I'm wondering if there is a better way such as maybe a table of commands? I'm pretty new to programming so not sure how to accomplish this.

import os
command = raw_input('What would you like to open:')
if 'skype' in command:
 os.chdir('C:\Program Files (x86)\Skype\Phone')
 os.startfile('Skype.exe')
user
5,2969 gold badges54 silver badges81 bronze badges
asked Dec 24, 2012 at 22:58
6
  • 2
    I doubt the if statements are making the command run slowly. Checking if a substring is in a string is pretty fast. You likely have another problem. Commented Dec 24, 2012 at 23:07
  • 3
    tons of if statements will make your code really un-maintainable and hard to read. so it's a good idea to get rid of them. but agf's right, it's probably not the cause of the slowness. Commented Dec 24, 2012 at 23:22
  • 1
    Yea, an if statement has minimal impact on performance. You would need a lot of them for it to have an impact. Commented Dec 24, 2012 at 23:24
  • Does executing your script now with something like 'skype notepad firefox' start three different processes or just one? Commented Dec 25, 2012 at 0:12
  • Good question Josh. If it does, and you go with my solution you could split the string and loop the if statement. Commented Dec 25, 2012 at 1:22

1 Answer 1

7

You can keep the commands in a dictionary with a tuple, and do something like this to store the commands.

command = {}
command['skype'] = 'C:\Program Files (x86)\Skype\Phone', 'Skype.exe'
command['explorer'] = 'C:\Windows\', 'Explorer.exe'

You could then do the following to execute the correct command based on the user input.

if raw_input.lower().strip() in command: # Check to see if input is defined in the dictionary.
 os.chdir(command[raw_input][0]) # Gets Tuple item 0 (e.g. C:\Program Files.....)
 os.startfile(command[myIraw_inputput][1]) # Gets Tuple item 1 (e.g. Skype.exe)

You can find more information on Dictionaries and Tuples here.

In case you need to allow multiple commands, you can separate them by a space and split the commands into an array.

for input in raw_input.split():
 if input.lower().strip() in command: # Check to see if input is defined in the dictionary.
 os.chdir(command[input][0]) # Gets Tuple item 0 (e.g. C:\Program Files.....)
 os.startfile(command[input][4]) # Gets Tuple item 1 (e.g. Skype.exe)

This would allow you to issue commands like skype explorer, but keep in mind that there are no room for typos, so they need to be an exact match, separated with nothing but white-spaces. As an example you could write explorer, but not explorer!.

answered Dec 24, 2012 at 23:03
Sign up to request clarification or add additional context in comments.

7 Comments

Command would be a dictionary. It would remove the need for a massive amount of if statements, and cut it down to a single. You can then load the data from a alternative data-source, or define it directly in code.
if 'skype' in command where command is a dictionary doesn't test the same thing as if 'skype' in command if command is a string.
I tried it out on my computer and works fine. I am no python expert, but I don't see why this would fail, as long as the input is identical. Just make sure to convert it to lower case.
It depends on if OP cares about the entered input being allowed to be a substring of the intended command. If not, this answer is fine. If so, he would need a loop.
Yep, exactly. I mentioned that in the comment on the questions, but can easily be fixed by a split and loop as you mentioned.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.