0

I have a program where the user should be able to pick and choose commands from a drop down list. In this list, there is also a repeat command, which does basically what a for loop does, so all the commands in between repeat and end repeat should be looped the number of times stated. See picture:

enter image description here

Now, I don't yet know how to programatically handle the repeat-functions. I know that python handles classes like objects, so maybe that can help, but I'm a bit lost.

At the moment I send a list of strings to the thread that handles execution of the commands, and that is parsed and each command is executed.

def command(self, item):
    if item.startswith('Pan'):
        ... do stuff
    elif item.startswith('...'):
        ... do something else

How would I rewrite this so that repeat is a callable function/method ?

enrm
  • 645
  • 1
  • 8
  • 22
  • When the user clicks on "Repeat: 150" you will have to parse the subsequent lines until "End repeat" and send all that information in one step to the other thread. I don't see how else the other thread would know what commands are to be repeated. I don't see how "rewriting" the `command` method is going to get you there. – Paul Cornelius Mar 04 '16 at 09:30

1 Answers1

1

Make a function multi_command which takes multiple commands, and executes them in order. When this function encounters a "repeat", create a list of all the following commands up until you get the corresponding "end repeat". This new list is then a sub-set of your total list. Call multi_command with this list, and afterwards, skip to the command that comes after the "end repeat".

Psuedo-code:

def multi_commands(items):
    highest_idx_already_done = 0
    for idx, item in enumerate(items):
        if highest_idx_already_done > idx:
            continue
        if item == "repeat":
            num_repeats = ...
            sub_items = []
            for sub_item in items[idx+1:]:
                if sub_item == "end repeat":
                   break
                sub_items.append(sub_item[5:]) # Skip indentation
            highest_idx_already_done = idx + len(sub_items)
            for _ in range(num_repeats):
                multi_commands(sub_items)
        else:
            command(item)
acdr
  • 4,538
  • 2
  • 19
  • 45
  • The problem with this approach is that it reads the closest `end repeat` tag, which doesn't necessarily map to the correct `start repeat`.. maybe the indentation should be used to map? – enrm Mar 04 '16 at 10:24
  • It should work for nested commands, yes. It also relies on the indentation to find "end repeat" marks. If you don't want that, you're going to have to keep count of the number of "repeats" and "end_repeats" within the `for sub_item` loop. – acdr Mar 04 '16 at 10:25
  • aah, `multi_command` and `multi_commands` should be one or the other, right? – enrm Mar 04 '16 at 10:37