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 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_1(self): q = Queue() q.put(1) self.assertEqual(q.top(), 1)
def test_pop_exception(self): with self.assertRaises(AssertionError): q = Queue() q.put(1) q.pop() q.pop()
def test_put_2(self): q = Queue() q.put(2) self.assertFalse(q.empty()) q.put(1) self.assertEqual(q.top(), 2)