Support NATA 2014

Open source project in Python

  • 155 Views
  • 4 Replies
New Topic Respond to this Topic
MrMojoRisin5
MrMojoRisin5
  • Member since: Jun. 21, 2011
  • Offline.
Forum Stats
Member
Level 12
Programmer
Open source project in Python Jun. 12th, 2013 @ 12:25 PM Reply

So, I made something like a "programming language"; not the real deal, but, you know, something just for fun. It's actually an open source project, and you can check it out here :)


-Got to keep on risin'...
-Check out FlashFrame
-Check out my YouTube channel and my blog

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to Open source project in Python Jun. 12th, 2013 @ 02:29 PM Reply

I didn't actually play around with it but I did look through your source code and noticed a few things:

1) Your function devnames:

def devnames():
    string = ""
    for name in developers:
        string += str(name) + ", "
    return string[0:(-2)]

Isn't necessary because you can just use the string.join function:

formatted_names = ", ".join(developers)

Basically you pass the function any iterable sequence and the contents of the string are inserted after each value (each of which is converted to a string) excluding the final value.

2) Your function variable_definer:

def variable_definer():
    global variables
    temp_dict = ""
    # for every item in varnames, set its value to be the correct item in varvals
    for name in varnames:
        pos = varnames.index(name)
        temp_str = "\"" + str(name) + "\"" + ":" + str(varvals[pos]) + ", "
        temp_dict += temp_str
    # make a temporary string-fake dictionary
    variables_temp = "{" + str(temp_dict[0:(-2)]) + "}"
    # make that into a real dictionary
    variables = ast.literal_eval(variables_temp)

Is also not necessary and can be replaced by using the zip function and passing it to a dictionary:

variables = dict(zip(varnames, varvals))

3) These if statements are a bit more complicated than they need to be:

if (x[0:7] == "newvar("):
elif (x[0:7] == "delvar("):
elif (x[0:8] == "showvar("):

Instead you could just grab all the text up to the first opening round bracket and use that new value in your if statements by using string.split:

command = x.split("(")[0]

Then your if statements would become this:

if (command == "newvar"):
elif (command  == "delvar"):
elif (command == "showvar"):

Even if the command given doesn't contain any round brackets it will still work.

However I think a better way to go about this would be to map each command to a function which will get passed any necessary values.
Here's a really quick example:

def add(a,b):
    return int(a)+int(b)

def subtract(a,b):
    return int(a)-int(b)

functions = {
    "add":add,
    "subtract":subtract
}

def handle(user_input):
    data = user_input.split("(") # input is assumed to be in "foo(bar,baz)" format
    command = data[0]
    args = data[1].split(",") # split apart the two arguments, note that the trailing ")" will be included

    if command in functions: # ensure that the command exists as a key of the map
        return functions[command](args[0], args[1].replace(")","")) # pass arguments and remove the trailing bracket

print handle("add(5,4)")
print handle("subtract(11,7)")

And that's it really.
That way you don't need a bunch of ugly if statements to manage; just write your functions as you go and add them to the map.

This is certainly not ideal, but it's quick and easy, and should be a good learning experience. Also note that the example will only work with commands that take exactly two arguments. I'll leave it up to you to figure out how to allow more, or none. :)

4) This is a bit goofy:

while looper < 10:  # this loop runs forever

If you want a constant loop just do either of these:

while 1:
while True:

----------

There may be a few things that I missed. I only took a quick look over it, and now I've got some other things I need to take care of.
Keep up the good work.

P.S.

What you made would be called a REPL (Read-Eval-Print Loop), not a programming language. :)

MrMojoRisin5
MrMojoRisin5
  • Member since: Jun. 21, 2011
  • Offline.
Forum Stats
Member
Level 12
Programmer
Response to Open source project in Python Jun. 13th, 2013 @ 11:06 AM Reply

This has been awfully helpful to me, especially the command-formatting part. And the name too, lol. Do you mind if I make these changes? Should I add "Diki" in the developers?


-Got to keep on risin'...
-Check out FlashFrame
-Check out my YouTube channel and my blog

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to Open source project in Python Jun. 13th, 2013 @ 12:48 PM Reply

At 6/13/13 11:06 AM, MrMojoRisin5 wrote: Do you mind if I make these changes?

Not at all. I helped you out explicitly so you could make the changes and improve your application. :)

At 6/13/13 11:06 AM, MrMojoRisin5 wrote: Should I add "Diki" in the developers?

I wouldn't really consider myself one of the developers since all I did was help you simplify parts of the code, but if you really want to throw my name in there you can.

MrMojoRisin5
MrMojoRisin5
  • Member since: Jun. 21, 2011
  • Offline.
Forum Stats
Member
Level 12
Programmer
Response to Open source project in Python Jun. 13th, 2013 @ 01:28 PM Reply

At 6/13/13 12:48 PM, Diki wrote: I wouldn't really consider myself one of the developers since all I did was help you simplify parts of the code, but if you really want to throw my name in there you can.

I did. Once again, thanks, you helped very much :)


-Got to keep on risin'...
-Check out FlashFrame
-Check out my YouTube channel and my blog

BBS Signature