0

I'm relatively new to Python, but I have a lot of experience in languages like C++ and Java. I am trying to parse a string to a function with parameters. This is what I got so far:

def DimLights(percent = 1.0):
    print 'Dimming lights to ' + "{:.2f}".format(percent * 100) + ' percent'

def TurnOffLights():
    print 'Turning off lights'

function_mappings = {'DimLights': DimLights,
                     'TurnOffLights': TurnOffLights}

def select_function():
    while True:
        try:
            return function_mappings[raw_input('Please input the function you want to use')]
        except:
            print 'Invalid function, try again.'

while True:
    function = select_function()
    function()

It's working as long as I don't use any parameters, but I can't think of a solution that would work with parameters. Is there any way I can accomplish this?

3
  • So you want to pass an argument into select_function() that would be in turn passed into the function you select? Commented Mar 24, 2015 at 15:58
  • unsolicited tip: please never leave an except: to catch all errors. You probably want KeyError and some select others here. But you probably don't want SyntaxError et al. Commented Mar 24, 2015 at 15:59
  • @APerson: But what if I have more than one argument? Brian Cain: Thanks, I'll do that Commented Mar 24, 2015 at 16:13

2 Answers 2

1

Use str.split() with its maxsplit argument to strip off just the actual command, and argparse to parse the arguments.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm sorry I'm a total noob when it comes to Python. Where exactly would I use str.split()?
The input is a str, so you'd use it right on the input. inputline = raw_input(...); command, arguments = inputline.split(maxsplit=1)
I don't know why I don't get this. So my input is for example: DimLights(1.0) Now I want to get rid of the'(1.0)' with inputline..split(maxsplit=1), right? And how do I parse the arguments. I feel really dumb right now.
If you simplify the input to "DimLights 1.0" then you can use this. The parens are just noise unless you're either programming or doing math.
Hmm I'm using python 2.7 and it gives me an error: split() takes no keyword arguments Is maxsplit a 3.4 feature?
0

Try this , Not a complete answer.But You can use it:

def DimLights(percent = 1.0):
    print 'Dimming lights to ' + "{:.2f}".format(percent * 100) + ' percent'

def TurnOffLights():
    print 'Turning off lights'

function_mappings = {'DimLights': DimLights,
                     'TurnOffLights': TurnOffLights}

def select_function():
    while True:
        try:
            inp = raw_input('Please input the function you want to use:')
            inp = inp.split()
            return function_mappings[inp[0]], inp[1]
        except:
            print 'Invalid function, try again.'
            break

function, arg = select_function()
function(float(arg))
# while True:
#     function = select_function
#     function()

>>>Please input the function you want to use:DimLights 2.0
Dimming lights to 200.00 percent

Note

Put space between function and parameter. Also parameter is required.Otherwise Index error will occur

1 Comment

Thank you so much! This helped a lot!

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.