def test_pop_2(self): q = Queue() q.put(2) q.put(1) self.assertEqual(q.pop(), 2) self.assertEqual(q.pop(), 1) self.assertTrue(q.empty())
class EventsHub: """Implements an event aggregator to gather events from both interfaces as well as the application itself. Similar to an Observer pattern. Warning: Events need to be processed regularly by calling `.handle_events()` to avoid overflowing the event queue. Events types: - pygame events (mouse / key press / quit) - tkinter events (mouse / key press / quit) - all buttons - application events, like "AnimationFinished" event """ def __init__(self): self._events = Queue() self._callbacks = {} def raise_event(self, event): self._events.put(event) def add_callback(self, event_name, callback): self._callbacks.setdefault(event_name, []).append(callback) def handle_events(self): while not self._events.empty(): event = self._events.pop() if event.type in [Event.QUIT, Event.NEWFRAME]: # print("Handling", event, self._callbacks.get(event.type, [])) pass for callback in self._callbacks.get(event.type, []): callback(event)
def solve(board, robots, dest, dest_color, num_sols=3, max_depth=20): """ Robots denotes the position and orientation (above or below a diag) of the robots. The robot of color DEST_COLOR must get to the square given by DEST (an (x, y) tuple). A list of Solution objects are returned, NUM_SOLS many of them (the best, second best, etc.) If we haven't found a solution less than MAX_DEPTH, give up """ assert len(robots) == 4, "there must be 4 robots" assert dest_color in ["yellow", "red", "green", "blue"] assert num_sols >= 0 and max_depth >= 0, "Cannot have a negative number of solutions or max depth" print "Trying to move", dest_color, "robot to", dest solutions = [] if num_sols == 0: return solutions # initialize the BFS fringe initial_sol = Solution(robots) bfs_fringe = Queue() bfs_fringe.enqueue(initial_sol) # in loop: dequeue, check if solution, add all next positions to queue curr_depth = 0 num_sols_at_depth = 0 while True: assert not bfs_fringe.empty(), "BFS fringe is empty" # dequeue the Solution at the front of the stack curr_sol = bfs_fringe.dequeue() # if it is a valid solution for the board, add it to the list of solutions to return if board.isSolution(curr_sol, dest, dest_color): solutions.append(curr_sol) if len(solutions) == num_sols: return solutions if curr_sol.depth > curr_depth: print "Examined", num_sols_at_depth, "solutions at depth", curr_depth curr_depth = curr_sol.depth num_sols_at_depth = 0 # check that the depth of this solution does not exceed max depth num_sols_at_depth += 1 if num_sols_at_depth % 100 == 0: print "Examined", num_sols_at_depth, "solutions at depth", curr_depth if curr_sol.depth > max_depth: break # for each of the possible robot moves, make the move, grab the new Solution and enqueue it next_moves = board.allNextMoves(curr_sol) for next_move in next_moves: bfs_fringe.enqueue(next_move) return solutions
def print_weights(self): q = Queue() layer = 0 for neuron in self.input_layer: q.put((neuron, layer + 1)) while not q.empty(): neuron, cur_layer = q.pop() if cur_layer > layer and cur_layer < self.nlayers: print "\nlayer %d:" % cur_layer, layer = cur_layer for connection in neuron.outputs: q.put((connection.tar, layer + 1)) for connection in neuron.outputs: print connection.w, print # print new line
def test_put_2(self): q = Queue() q.put(2) self.assertFalse(q.empty()) q.put(1) self.assertEqual(q.top(), 2)