Hello. I'm trying to make a simple language in Python. I have many functions, that handle arguments, and execute the commands. Although, the old way I was actually CALLING these functions was to have a dictionary of all the keywords to functions, and then get() it. Is these an easier way to do this?

Recommended Answers

All 4 Replies

Post example code, so we get better picture of your meaning.

Alright. I have lots of functions, like so; (BTW VarParser is a little module I made)

import VarParser

def echocmd(cmd, variables):
    if cmd.__len__() < 2:
        print "Too little arguments! echo <string>"
    else:
        xprint = ""
        for i in cmd:
            if i != "echo":
                xprint = xprint + str(VarParser.Parse(i, variables))

        print xprint
        variables['_'] = xprint

def varcmd(cmd, variables):
    if cmd.__len__() < 3:
        print "Too little arguments! str <var> <string>"
    else:
        try:
            varlay = ""
            for i in cmd:
                if cmd.index(i) > 1:
                    try:
                        varlay = varlay + str(VarParser.Parse(i, variables))
                    except:
                        print "Error!"
            variables[cmd[1]] = varlay
        except:
            print "Incorrect syntax! str <var> <string>"

def intcmd(cmd, variables):
    try:
        variables[cmd[1][1:]] = int(variables[cmd[1][1:]])
    except:
        print "Incorrect syntax, or variable does not exist!"

def mathcmd(cmd, variables):
    try:
        if(cmd[2] == "+"):
            xmath = (float(VarParser.Parse(cmd[1], variables))) + (float(VarParser.Parse(cmd[3], variables)))
            print xmath
            variables['_'] = xmath
        elif(cmd[2] == "-"):
            xmath = (float(VarParser.Parse(cmd[1], variables))) - (float(VarParser.Parse(cmd[3], variables)))
            print xmath
            variables['_'] = xmath
        elif(cmd[2] == "*"):
            xmath = (float(VarParser.Parse(cmd[1], variables))) * (float(VarParser.Parse(cmd[3], variables)))
            print xmath
            variables['_'] = xmath
        elif(cmd[2] == "/"):
            xmath = (float(VarParser.Parse(cmd[1], variables))) / (float(VarParser.Parse(cmd[3], variables)))
            print xmath
            variables['_'] = xmath
    except:
        print "Incorrect synatx, or variable(s) do not exist!"      
def exit(cmd, variables):
    prompt = raw_input("Are you sure you want to exit? (y/n) ")
    if prompt == "y":
        variables['exitmode'] = 1
        quit()

def invalidcmd(cmd, variables):
    print "Unknown command! Typed \"help\" for help!"

I'm having trouble calling those functions.

Previously, I was using this to call functions

funcdict = { 'echo' : Functions.echocmd,
             'var'  : Functions.varcmd,
             'exit' : Functions.exit,
             'int'  : Functions.intcmd,
             'math' : Functions.mathcmd,
             'help' : helpcmd}

funcdict.get(TypedCmd[0], Functions.invalidcmd)(TypedCmd, variables)

but that is annoying, how I have to list everything I have.

I would like to make that easier on me.

cmd.len() < 2:

I would say this at lest

if len(cmd) < 2:

I would have all functions separate as except for called function, the lines 39-54 are same.

Did you see my MathExpression class code snippet? That is another possible approach. Also you might like to read this article:

http://effbot.org/zone/simple-top-down-parsing.htm

One way is to register command handler functions with a decorator:

FUNCDICT = dict()

def cmd_handler(func):
    assert func.__name__.endswith('cmd')
    FUNCDICT[func.__name__[:-3]] = func
    return func

@cmd_handler
def echocmd(...):
    ...

@cmd_handler
def catcmd(...):
    ...

# etc

Functions are automatically added to FUNCDICT as you write them.

Did you read about the standard module cmd ? See also this code snippet which is very easy to use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.