def test_enqueue_five_elements_and_iterate_queue(self): queue = Queue() langs = ['python', 'java', 'ruby', 'php', 'go'] for lang in langs: queue.enqueue(lang) self.assertEqual(len(langs), queue.size()) for element in queue: self.assertIn(element, langs)
def bfs(start): start.setDistance(0) start.setPred(None) # Queue for hold vertexes. vertQueue = Queue() vertQueue.enqueue(start) while vertQueue.size() > 0: # Get first value from queue. curVertex = vertQueue.dequeue() # Go through all the neigbors of the current vertex. for nbr in curVertex.getConnections(): # Check if the neighbor was not achieved. if nbr.getColor() == 'white': # Mark neighbor as achieved. nbr.setColor('grey') # Compute distance between current vertex and neighbor. nbr.setDistance(curVertex.getDistance() + 1) # Set predecessor as a current vertex for, # for afterwards recreate the path. nbr.setPred(curVertex) # Add neighbor to end of queue. vertQueue.enqueue(nbr) # Mark current vertex as passed. curVertex.setColor('black')
class PostMessage(App): ''' Join messages from previous processor ''' def __init__(self, config): super(PostMessage, self).__init__() self.queue = Queue(config) def run(self, process=None): self.logger.info('Start ...') def process(self, messages=None): if messages: self.queue.post(messages)
def breadth_first(self): visited = Queue() visited.enqueue(self) while visited: try: node = visited.dequeue() except LookupError: return if node.left: visited.enqueue(node.left) if node.right: visited.enqueue(node.right) yield node
def level_order_traverse(root): if root is None: return queue = Queue() queue.enqueue(root) while not queue.is_empty(): node = queue.dequeue() print node, if node.lchild is not None: queue.enqueue(node.lchild) if node.rchild is not None: queue.enqueue(node.rchild) print
def test_enqueue_five_elements_to_queue_and_dequeue_all_elements(self): queue = Queue() langs = ['python', 'java', 'ruby', 'php', 'go'] for lang in langs: queue.enqueue(lang) self.assertEqual(len(langs), queue.size()) element1 = queue.dequeue() element2 = queue.dequeue() element3 = queue.dequeue() element4 = queue.dequeue() element5 = queue.dequeue() self.assertEqual('python', element1) self.assertEqual('java', element2) self.assertEqual('ruby', element3) self.assertEqual('php', element4) self.assertEqual('go', element5) self.assertTrue(queue.is_empty())
def test_dequeue(self): queue = Queue() queue.enqueue(1) queue.enqueue(2) queue.enqueue(13) queue.enqueue(31) # assert that the item dequeued was the item at the head self.assertEqual(queue.dequeue(), 1) # assert that the item at the head of the linked list was removed self.assertEqual(queue.items.walk(), [2, 13, 31])
def _bfs(self, node_name): search_queue = Queue() search_queue.add(self.root) while len(search_queue) > 0: n = search_queue.remove() if n.name == node_name: return n for c in n.get_children(): search_queue.add(c) return None
def test_enqueue(self): queue = Queue() queue.enqueue(1) queue.enqueue(2) queue.enqueue(13) queue.enqueue(31) self.assertEqual(queue.items.walk(), [1, 2, 13, 31])
def test_enqueue_five_elements_to_queue_and_dequeue_two_elements(self): queue = Queue() langs = ['python', 'java', 'ruby', 'php', 'go'] for lang in langs: queue.enqueue(lang) self.assertEqual(len(langs), queue.size()) element1 = queue.dequeue() element2 = queue.dequeue() self.assertEqual('python', element1) self.assertEqual('java', element2) self.assertEqual(3, queue.size())
def setUp(self): self.data = ["d1", "d2"] self.l1 = Queue() self.l2 = Queue() self.l3 = Queue() self.l2.add(self.data[0]) for datum in self.data: self.l3.add(datum)
def bfs(g, start): start.set_distance(0) start.set_pred(None) vert_queue = Queue() vert_queue.enqueue(start) while vert_queue.size() > 0: current_vert = vert_queue.dequeue()
def bft_print_tree(self): """breadth first traversal method to print a tree """ if self is None: return None # make a queue, add thehfirst node current = self lst = Queue() lst.enqueue(current.value) # while queue is not empty while len(lst) > 0: # remove the current, and print it print(lst.dequeue()) # add all children of the current if current.left: current = current.left lst.enqueue(current.value) if current.right: current = current.right lst.enqueue(current.value)
def __init__(self, main_window): Gtk.Grid.__init__(self) self.main_window = main_window self.set_column_homogeneous = True self.queue = Queue() self.set_column_spacing(100) results_box = Gtk.ListBox() self.results_line = Gtk.ListBox() results_box.set_border_width(4) results_box.add(self.results_line) schedule_button = Gtk.Button('Schedule') schedule_button.connect('clicked', self.schedule) results_box.set_hexpand(True) results_box.add(schedule_button) self.attach(results_box, 0, 0, 1, 1) processes_box = Gtk.ListBox() self.process_lines = Gtk.ListBox() processes_box.add(self.process_lines) add_process_button = Gtk.Button('Add Process') add_process_button.connect('clicked', self.add_process_dialog) processes_box.set_hexpand(True) processes_box.set_border_width(5) processes_box.add(add_process_button) self.attach(processes_box, 3, 0, 1, 1) self.gantt_chart_box = Gtk.Box() self.gantt_chart_box.add(Gtk.Label('GANTT CHART GOES HERE')) self.attach(self.gantt_chart_box, 0, 1, 3, 1)
def bfs(self, current): # Implementation: Iterative 1 if current is None: return queue = Queue() queue.enqueue(current) while current is not None: current = current.next_sibling if queue and current is None: current = queue.dequeue().first_child if current and current.first_child: queue.enqueue(current)
def _get_node(self, element): queue = Queue() queue.enqueue(self.root) while queue: current = queue.dequeue() if current.element == element: return current children = current.get_children() if children: for child in children: queue.enqueue(child) return None
def move(self, element, parent): moving_node = self._get_node(element) destination_node = self._get_node(parent) if not moving_node: raise ElementNotFoundError( "Could not find element {}".format(element)) if not destination_node: raise ElementNotFoundError( "Could not find parent {}".format(parent)) if moving_node == self.root: raise MovingRootError("Cannot move root node.") parent_node = None previous_sibling_node = None queue = Queue() queue.enqueue(self.root) while queue: current = queue.dequeue() if current.first_child == moving_node: parent_node = current if current.next_sibling == moving_node: previous_sibling_node = current if current == moving_node: break children = current.get_children() if children: for child in children: queue.enqueue(child) if parent_node: parent_node.first_child = moving_node.next_sibling if previous_sibling_node: previous_sibling_node.next_sibling = moving_node.next_sibling next_sibling_node = destination_node.first_child destination_node.first_child = moving_node moving_node.next_sibling = next_sibling_node
class TestQueue(unittest.TestCase): def setUp(self): self.data = ["d1", "d2"] self.l1 = Queue() self.l2 = Queue() self.l3 = Queue() self.l2.add(self.data[0]) for datum in self.data: self.l3.add(datum) def test_Queue__init__(self): self.assertEqual(self.l1.head, None) self.assertEqual(self.l1.len, 0) def test_Queue__iter__(self): for n in self.l1: self.assertTrue(False) for l in [self.l2, self.l3]: for n in l: self.assertTrue(repr(n) in self.data) def test_Queue_add(self): len1 = len(self.l1) self.l1.add("foo") self.assertTrue(len(self.l1) - len1 == 1) self.assertTrue(self.l1.remove() == "foo") def test_Queue_remove(self): self.assertTrue(self.l3.remove() == self.data[0]) def test_Queue__len__(self): self.assertEquals(len(self.l1), 0) self.assertEquals(len(self.l2), 1) self.assertEquals(len(self.l3), 2) def test_Queue__repr__(self): self.assertEquals(repr(self.l1), '') self.assertEquals(repr(self.l2), 'd1') self.assertEquals(repr(self.l3), 'd1 -> d2')
def __init__(self): self._queue_one = Queue() self._queue_two = Queue()
def test_peak(self): queue = Queue() queue.enqueue(1) self.assertEqual(queue.peek(), 1) queue.enqueue(2) self.assertEqual(queue.peek(), 1) queue.enqueue(13) self.assertEqual(queue.peek(), 1) queue.dequeue() self.assertEqual(queue.peek(), 2)
def test_empty_list(self): queue = Queue() # assert that an empty linked list is created self.assertEqual(queue.items.head, None) self.assertEqual(queue.items.tail, None) self.assertEqual(queue.items.walk(), [])
def __init__(self, config): super(PostMessage, self).__init__() self.queue = Queue(config)
def test_no_element_in_queue(self): queue = Queue() self.assertTrue(queue.is_empty())
class StackUsingQueue(object): def __init__(self): self._queue_one = Queue() self._queue_two = Queue() def __str__(self): return self._queue_one.__str__() def push(self, element): ''' :param element: element to be pushed onto stack ''' while not self._queue_one.is_empty(): self._queue_two.enqueue(self._queue_one.dequeue()) self._queue_one.enqueue(element) while not self._queue_two.is_empty(): self._queue_one.enqueue(self._queue_two.dequeue()) return def pop(self): return self._queue_one.dequeue()
def test_enqueue_one_element_to_queue(self): queue = Queue() queue.enqueue('python') self.assertEqual(1, queue.size())
class MainWindow(Gtk.Grid): def __init__(self, main_window): Gtk.Grid.__init__(self) self.main_window = main_window self.set_column_homogeneous = True self.queue = Queue() self.set_column_spacing(100) results_box = Gtk.ListBox() self.results_line = Gtk.ListBox() results_box.set_border_width(4) results_box.add(self.results_line) schedule_button = Gtk.Button('Schedule') schedule_button.connect('clicked', self.schedule) results_box.set_hexpand(True) results_box.add(schedule_button) self.attach(results_box, 0, 0, 1, 1) processes_box = Gtk.ListBox() self.process_lines = Gtk.ListBox() processes_box.add(self.process_lines) add_process_button = Gtk.Button('Add Process') add_process_button.connect('clicked', self.add_process_dialog) processes_box.set_hexpand(True) processes_box.set_border_width(5) processes_box.add(add_process_button) self.attach(processes_box, 3, 0, 1, 1) self.gantt_chart_box = Gtk.Box() self.gantt_chart_box.add(Gtk.Label('GANTT CHART GOES HERE')) self.attach(self.gantt_chart_box, 0, 1, 3, 1) def add_process_dialog(self, widget): self.dialog = Gtk.Dialog('Add Process', self.main_window) dialog_main_window = self.dialog.get_content_area() #Row for the text fields first_row = Gtk.Box(spacing=50) #Row for the Add and Cancel buttons second_row = Gtk.Box(spacing=50) #Row for validation information third_row = Gtk.Box(spacing=50) dialog_main_window.add(first_row) dialog_main_window.add(second_row) dialog_main_window.add(third_row) #Add the text fields to the first row self.process_name_input = Gtk.Entry() self.process_name_input.set_text('Process Name') self.process_time_input = Gtk.Entry() self.process_time_input.set_text('4') self.process_arrival_input = Gtk.Entry() self.process_arrival_input.set_text('1') first_row.pack_start(self.process_name_input, True, True, 0) first_row.pack_start(self.process_time_input, True, True, 0) first_row.pack_start(self.process_arrival_input, True, True, 0) #Add the buttons to the second row add_process_button = Gtk.Button('Add') add_process_button.connect('clicked', self.validate) cancel_button = Gtk.Button('Cancel') cancel_button.connect('clicked', self.cancel) second_row.pack_start(add_process_button, True, True, 0) second_row.pack_start(cancel_button, True, True, 0) #Add the validations row self.validations = Gtk.Label() third_row.pack_start(self.validations, True, True, 0) self.dialog.show_all() def cancel(self, widget): self.dialog.close() def validate(self, widget): process_name = self.process_name_input.get_text() process_time = self.process_time_input.get_text() process_arrival = self.process_arrival_input.get_text() try: process_time = int(process_time) process_arrival = int(process_arrival) if (process_arrival < 0 and process_time < 0 and process_name is ''): raise Exception('') self.queue.addProcess( NormalProcess(process_name, process_arrival, process_time)) except Exception as e: self.validations.set_label( 'The Process Name is Required, and the process arrival and Time should be non-negative integers' ) else: self.cancel(widget) self.draw_queue() def draw_queue(self): for child in self.process_lines.get_children(): self.process_lines.remove(child) for process in self.queue.processes: t = str(process.name) + " /// " + str( process.arrival) + " /// " + str(process.time) self.process_lines.add(Gtk.Label(t)) self.process_lines.show_all() def schedule(self, widget): pass def draw_gantt(self, gantt_chart): for child in self.gantt_chart_box.get_children(): self.gantt_chart_box.remove(child) for process in gantt_chart.chart: l = 'X' if process is not None: l = process.name l = Gtk.Button(l) self.gantt_chart_box.add(l) self.gantt_chart_box.show_all()
class TestQueue(unittest.TestCase): def setUp(self): self.queue = Queue() def tearDown(self): return super().tearDown() def test_size(self): self.assertEqual(self.queue.size(), 0) def test_enqueue(self): self.queue.enqueue(1) self.assertEqual(self.queue.size(), 1) def test_dequeue(self): self.queue.enqueue(1) self.assertEqual(self.queue.dequeue(), 1) self.assertEqual(self.queue.size(), 0) def test_front(self): self.queue.enqueue(1) self.assertEqual(self.queue.front(), 1) def test_dequeue_exception(self): self.assertIsNone(self.queue.dequeue()) def test_front_exception(self): self.assertIsNone(self.queue.front())
def setUp(self): self.queue = Queue()
#!/usr/bin/python import sys, os, json sys.dont_write_bytecode = True from queue.queue import Queue import bson.json_util exportdir="/tmp/snerimport" def writefile(filename, data): if not data: data="" with open(filename,'w') as f: f.write(data) f.close() q = Queue() workedlist = q.worked_list(data=True) if workedlist and os.path.exists(exportdir) == False: os.mkdir(exportdir) for item in workedlist: dumpfile = exportdir+"/"+item["id"]+"."+item["type"] if item["type"] == "input": writefile(dumpfile, bson.json_util.dumps(item, indent=2)) else: writefile(dumpfile, item.get("data", ""))
def test_queue(self): q = Queue() q.append(2) q.append(3) self.assertEqual(q.size() == 2, True) val = q.pop() self.assertEqual(val == 2, True) self.assertEqual(q.size() == 1, True) self.assertEqual(q.pop() == 3, True) self.assertEqual(q.pop() == None, True) val = [1, 2, 3, 4, 5] for v in val: q.append(v) for v in val: self.assertEqual(q.pop() == v, True)
def test_queue(self): q = Queue(1) q.enqueue(2) q.enqueue(3) assert q.peek() == 1 assert q.dequeue() == 1 q.enqueue(4) assert q.dequeue() == 2 assert q.dequeue() == 3 assert q.dequeue() == 4 q.enqueue(5) assert q.peek() == 5
#!/usr/bin/python import sys sys.dont_write_bytecode = True from queue.queue import Queue q = Queue() q.flush()
#!/usr/bin/python import sys sys.dont_write_bytecode = True from queue.queue import Queue q = Queue() try: with open(sys.argv[1],'r') as f: content = f.read() items = content.replace(',',' ').replace('\n',' ').split() q = Queue() for i in items: q.work_add(i.rstrip()) except Exception as e: print "ERROR: push to queue failed", e