[Ironpython-users] Gradually executing a python script

Jeff Hardy jdhardy at gmail.com
Tue Jul 24 20:22:42 CEST 2012


On Tue, Jul 24, 2012 at 5:48 AM, Markus Schaber
<m.schaber at 3s-software.com> wrote:
> Another idea is that you could use the “yield return” with yield parameters
> – the script yields the actions, and gets the next snapshot in return. Here,
> you still can synchronize internally.

IIRC, this is how UnrealScript works. It's a bit mind-bending at first
if you learned "traditional" programming, but if you're teaching kids
their minds are free to warp as needed. :)

David Beazley has some great background on how to do this:
http://www.dabeaz.com/generators-uk/index.html
http://www.dabeaz.com/coroutines/index.html (this one, especially, is relevant)

I won't say it's easy, because it's not, but it's incredibly powerful.

Here's a basic overview: you need your scripts to yield back to the
host system at some point, and get an updated state. Using yield, that
would look like:

    def robot_ccw():
        state = yield None
        while True:
            state = yield actions.turn_left

Obviously a real robot would use 'state' to determine what to do, but
that's the idea. Your host then looks like:

    def turn(state):
        for robot in robots:
            action = robot.send(state)
            do_action(robot, action)

On Tue, Jul 24, 2012 at 5:54 AM, Jesper Taxbøl <jesper at taxboel.dk> wrote:
> Agreed that could work :), but would still hang if a kid writes an infinite
> loop without actions inside.
>
> I am really into making something robust for kids to play with.
>

sys.settrace is your friend here. Basically, if a robot tries to
execute too many lines between yields (i.e. per turn), remove it from
the list of valid robots and show an error.

- Jeff


More information about the Ironpython-users mailing list