Yielding through a dict that changes during cycling
I am using Python and the Panda3D framework and already asked the question on our [Forum][1], however I thought I'd ask in a more general Python community. Let's say in a game I have a list of ships, here represented as a dict with id:string pairs. Using the tab keys you can cycle through that dict, using the a key will add a key:value pair to the dict. During play the dict will not only gain key:value pairs, but also lose some and the dict has to be filtered before scanning.

The code I have so far is:

    from itertools import cycle, ifilter, ifilterfalse
    
    class GeneratorTest(DirectObject):
       def __init__(self):
       
          self.ships = {1:"node1", 2:"node2", 3:"node3", 4:"node4", 5:"node5", 6:"node6", 7:"node7"}
          self.ships_iscannable = cycle(ifilter(self.filter_ships, self.ships))
          
          self.accept("tab", self.do_filtered_scan)
          self.accept("a", self.add_ship)
          
       def filter_ships(self, n):
          scannable = [i for i in self.ships if i != 3]
          if n in scannable:
             return True
             
       def do_filtered_scan(self):
          print self.ships_iscannable.next()
          
       def add_ship(self):
          plus_i = sorted(self.ships, reverse=True)[-1] + 1
          self.ships[plus_i] = "node%s" % plus_i
          print plus_i, self.ships
          
    gt = GeneratorTest()      
    
    run()

To make matters worse, I need one version for Python 2.5 and one for 2.6. The latter gained the .next() method, so the 2.5 version had to do without.

Can anybode help?

  [1]: http://www.panda3d.org/forums/viewtopic.php?t=13452
Yielding through a dict that changes during cycling
===

I am using Python and the Panda3D framework and already asked the question on our [Forum][1], however I thought I'd ask in a more general Python community. Let's say in a game I have a list of ships, here represented as a dict with id:string pairs. Using the tab keys you can cycle through that dict, using the a key will add a key:value pair to the dict. During play the dict will not only gain key:value pairs, but also lose some and the dict has to be filtered before scanning.

The code I have so far is:

    from itertools import cycle, ifilter, ifilterfalse
    
    class GeneratorTest(DirectObject):
       def __init__(self):
       
          self.ships = {1:"node1", 2:"node2", 3:"node3", 4:"node4", 5:"node5", 6:"node6", 7:"node7"}
          self.ships_iscannable = cycle(ifilter(self.filter_ships, self.ships))
          
          self.accept("tab", self.do_filtered_scan)
          self.accept("a", self.add_ship)
          
       def filter_ships(self, n):
          scannable = [i for i in self.ships if i != 3]
          if n in scannable:
             return True
             
       def do_filtered_scan(self):
          print self.ships_iscannable.next()
          
       def add_ship(self):
          plus_i = sorted(self.ships, reverse=True)[-1] + 1
          self.ships[plus_i] = "node%s" % plus_i
          print plus_i, self.ships
          
    gt = GeneratorTest()      
    
    run()

To make matters worse, I need one version for Python 2.5 and one for 2.6. The latter gained the .next() method, so the 2.5 version had to do without.

Can anybode help?

  [1]: http://www.panda3d.org/forums/viewtopic.php?t=13452