user1825415
user1825415

Reputation: 43

how do i use a line from a text file as a function input in python?

I have written a function which allows you to control turtle module through python shell, here's a part of it:

import turtle
turtle.showturtle()
def turtle_commands():
    instructions = input().split()
    i = instructions[0]
    if len(instructions) == 2:
        if i == 'forward' :
            n = int(instructions[1])
            turtle.forward(n)

for example when you type in

forward 100

the turtle moves forward 100 pixels. I've done the same thing with most turtle commands- backwards, left, right, penup, pendown, color and so on.

My question is, is there any way to load these commands from a text file? i was thinking of something like that

instructions = input().split()
i = instructions[0]
if i == 'load' :
    n = str(instructions[1])
    l = open(n, 'r')
    while True:
        line = l.readline()
        turtle_commands(line) #i don't really know what i did here, but hopefully you get the point
        if not line:
            break

The program must accept commands from both file and shell. Thank you for your answers.

Upvotes: 0

Views: 291

Answers (4)

volcano
volcano

Reputation: 3582

There is a very simple solution - use geattr function. If you have a sequence of space/end-of-line commands:

instructions = data.split()
commands = instructions[::2]
params   = instructions[1::2]
for command, param in zip(commands,params):
    try:
        getattr(turtle, command)(int(param))
    except AttributeError:
        print('Bad command name:{}'.format(command))

Upvotes: 0

Marc Cohen
Marc Cohen

Reputation: 3808

Should be pretty simple -- just change your turtle_commands() function to get its input from an argument rather than the input() function, like this:

def turtle_commands(command):
    instructions = command.split()
    i = instructions[0]
    if len(instructions) == 2:
        if i == 'forward' :
            n = int(instructions[1])
            turtle.forward(n)

Then, call your function with the input commands you read from your file, just as you've done in your proposed code with the line turtle_commands(line).

Upvotes: 1

Nathan
Nathan

Reputation: 4347

Assuming all commands are in the format <command> <pixels>:

# Create a dictionary of possible commands, with the values being pointers
# to the actual function - so that we can call the commands like so:
# commands[command](argument)
commands = {
    'forward': turtle.forward,
    'backwards': turtle.backward,
    'left': turtle.left,
    'right': turtle.right
    # etc., etc.
}

# Use the `with` statement for some snazzy, automatic
# file setting-up and tearing-down
with open('instructions_file', 'r') as instructions:
    for instruction in instructions:  # for line in intructions_file
        # split the line into command, pixels
        instruction, pixels = instruction.split()

        # If the parsed instruction is in `commands`, then run it.
        if instruction in commands:
            commands[instruction](pixels)
        else:
        # If it's not, then raise an error.
            raise()

Upvotes: 3

Shawn Zhang
Shawn Zhang

Reputation: 1884

Using map() under itertools will be enough. l.readlines() will return all lines in the file as a list, and map builtin function will iterate through all elements in the list and supply them as arguments to the function turtle_commands

map(turtle_commands, [ int(_) for _ in l.readlines() ] )

map() will supply a list of parameters to the function.

map(function, params_list)

>>> map(lambda x: x + 1, [1, 2, 3, 4, 5, 6])
[2, 3, 4, 5, 6, 7]

Upvotes: 0

Related Questions