def rr(CPUs, numProcessor, proclist, finish_proc_list, quantum): # initial time time = 0 # create cpus timeline for i in range(numProcessor): CPUs.append(CPU("CPU " + str(i+1))) ready_q = CircularQueue() wait_q = CircularQueue() while True: while len(proclist) != 0 and proclist[0].arrival_time == time: task = proclist.pop(0) ready_q.enqueue(task) for cpu in CPUs: cpu.exec(ready_q, wait_q, finish_proc_list, quantum) w_inc(ready_q) #if ready_q.empty(): transfer_q(wait_q, ready_q) time += 1 cpu_not_working = all([not cpu.working() for cpu in CPUs]) # to finish working, all queue must be empty and no cpu is working if ready_q.empty() and wait_q.empty() and len(proclist) == 0 and cpu_not_working: print("All processes executed!") break return time
def test_enqueue_dequeue_many_chunks(self): """ CHUNKS_COUNT add chunks of size CHUNK_SIZE and check along each enqueue/dequeue call """ queue = CircularQueue() chunks_count = 100 elements_count = 1 for i in range(chunks_count): self.assertEqual(queue.count, 0) chunk_size = i + 1 # enqueue for counter in range(chunk_size): self.assertEqual(queue.count, elements_count-1) queue.enqueue("SMTH") self.assertEqual(queue.count, elements_count) elements_count += 1 # dequeue for counter in range(chunk_size): elements_count -= 1 self.assertEqual(queue.count, elements_count) queue.dequeue() self.assertEqual(queue.count, elements_count-1) self.assertEqual(queue.count, 0)
def test_pop_puts_element_to_back_of_queue(self): queue = CircularQueue() queue.append("4") queue.append("5") popped = queue.popleft() assert popped == "4" assert list(queue) == ["5", "4"]
def __init__(self, capacity=10000, delay_time=10, pop_size=1): self.__queue = CircularQueue(capacity=capacity) self.__set = set() self.__DELAY_TIME = delay_time self.__POP_SIZE = pop_size g_logger.info('Capacity: %d, DelayTime: %d', capacity, self.__DELAY_TIME) pass
def test_enqueue_500_elements_to_list(self): expected = list(range(500)) queue = CircularQueue() # add the items for element in expected: queue.enqueue(element) self.assertEqual(list(queue), expected)
def test_enqueue_dequeue(self): """ Should empty queue """ queue = CircularQueue() test_element = "Some value" queue.enqueue(test_element) el_from_queue = queue.dequeue() self.assertEqual(el_from_queue, test_element) self.assertEqual(queue.count, 0)
def test_circular_motion(self): """ Add and dequeue elements in such a way that they will form a circular queue""" queue = CircularQueue(capacity=5) for num in range(5): queue.enqueue(num) for _ in range(3): queue.dequeue() """ Queue should now have two empty values at the start """ queue.enqueue("CIRCLE HAS BEEN FORMED") self.assertEqual(queue.count, 3) self.assertEqual(list(queue), [3, 4, "CIRCLE HAS BEEN FORMED"])
def __init__(self): print("\n........HOUSE VARIABLES........") self.dealer = Dealer(int(input("House Limit: "))) self.min = int(input("Enter table min: ")) print("\n........PLAYER VARIABLES........") self.player = Player(input("Player Name: "), int(input("Player Limit: "))) #Turn manager self.turn_manager = CircularQueue() self.turn_manager.append(self.player) self.turn_manager.append(self.dealer) self.cur_player = self.turn_manager[0]
def test_initial_capacity_1_enq_deq_20_elements(self): """ Set an initial capacity of 1 and add/remove 20 elements, checking along the way if everything works correctly. The list should resize itself when appropriate""" elements_count = 20 initial_capacity = 1 queue = CircularQueue(initial_capacity) for num in range(elements_count): queue.enqueue(num) self.assertEqual(queue.count, elements_count) for num in range(elements_count): dequeued_num = queue.dequeue() self.assertEqual(dequeued_num, num) self.assertEqual(queue.count, 0)
def test_enqueue_dequeue_100_elements(self): """ Should return an empty queue """ queue = CircularQueue() element_to_add = "ELEMENT" elements_count = 100 # add to queue for _ in range(elements_count): queue.enqueue(element_to_add) # assert self.assertEqual(list(queue), [element_to_add] * elements_count) for idx in range(elements_count): self.assertEqual(queue.count, elements_count - idx) dequeued_element = queue.dequeue() self.assertEqual(element_to_add, dequeued_element) self.assertEqual(queue.count, elements_count - idx - 1) self.assertEqual(queue.count, 0)
def setUp(self): self.test_queue = CircularQueue(4)
def run(self): prelaunch_buffer = CircularQueue(PRELAUNCH_BUFFER_SIZE) print("moving to prelaunch state") while (self.rocket.state != RocketState.GROUND): data = self.sensor.getData() if (self.rocket.state == RocketState.PRELAUNCH): # PRELAUNCH state prelaunch_buffer.add(data) # Checking if the launch vehicle took off if data.total_accel() > TAKEOFF_DETECTION_THRESHOLD: arr = prelaunch_buffer.toArray() self.start_time = arr[ 0].time # setting start time of the launch vehicle arr[-1].event = Event.TAKE_OFF # the latest data will be marked as TAKE_OFF total = 0.0 for point in arr: point.normalize(self.start_time) self.logger.write(point) total += point.pressure self.rocket.state = RocketState.MOTOR_ASCENT self.calculator.setBaseAlt( total / PRELAUNCH_BUFFER_SIZE ) # base altitude is based on average pressure print("moving to motor ascent state") elif (self.rocket.state == RocketState.MOTOR_ASCENT): # MOTOR_ASCENT state data.normalize(self.start_time) self.calculator.compute(data) # Checking if the motor burned up by checking the duration since take off if data.time >= self.rocket.motor_burnout_t + 1: self.rocket.state = RocketState.COAST data.event = Event.MOTOR_BURNOUT print("moving to coast state") self.base_time = data.time self.logger.write(data) elif (self.rocket.state == RocketState.COAST): # COAST state data.normalize(self.start_time) self.calculator.compute(data) if self.ats_state == 0 and (data.time - self.base_time >= 1): # if been in closed state for more than 1 second, start opening the flaps self.ats_state = 2 self.base_time = data.time self.actuator.actuate() data.command = "ACTUATE" elif self.ats_state == 1 and (data.time - self.base_time >= 1): self.ats_state = -1 self.base_time = data.time self.actuator.retract() data.command = "RETRACT" elif self.ats_state == 2: if (data.time - self.base_time < 2): self.actuator.actuate() else: self.ats_state = 1 self.base_time = data.time data.command = "COMPLETED ACTUATION" else: if (data.time - self.base_time < 2): self.actuator.retract() else: self.ats_state = 0 self.base_time = data.time data.command = "COMPLETED RETRACTION" # if self.calculator.predict() > self.rocket.target_alt: # self.actuator.actuate() # else: # self.actuator.retract() print("Current velocity: {}".format( self.calculator.v_current())) # Checking if velocity of the launch vehicle is negative if self.calculator.v_current() < 0: data.event = Event.APOGEE self.actuator.retract() self.rocket.state = RocketState.DESCENT self.apogee_time = data.time print("moving to descent state") self.logger.write(data) elif (self.rocket.state == RocketState.DESCENT): # DESCENT state data.normalize(self.start_time) self.calculator.compute(data) print("Current velocity: {}".format( self.calculator.v_current())) # Detect landing when velocity is be greated than -2 m/s and it has been at least 5 seconds after apogee if self.calculator.v_current( ) > -2 and data.time - self.apogee_time > 5: data.event = Event.TOUCH_DOWN self.rocket.state = RocketState.GROUND print("moving to ground state") self.logger.write(data) # GROUND state for _ in range(5): data = self.sensor.getData() data.normalize(self.start_time) self.logger.write(data) self.logger.purge() self.shutdown()
def test_enqueue_empty_queue(self): """ Should add element """ queue = CircularQueue() queue.enqueue(5) self.assertEqual(queue.count, 1) self.assertEqual(list(queue), [5])
def __init__(self): self.root = Tk() global guiX, guiY guiY = int(self.root.winfo_screenheight() - 250) guiX = int(guiY) self.root.filename = "/" self.timeline = CircularQueue() vbar = AutoScrollbar(self.root, orient='vertical') hbar = AutoScrollbar(self.root, orient='horizontal') vbar.grid(row=2, column=6, sticky='ns') hbar.grid(row=3, column=3, columnspan=2, sticky='we') self.menubar = Menu(self.root) self.root.config(menu=self.menubar) # create a pulldown menu, and add it to the menu bar self.filemenu = Menu(self.menubar) self.filemenu.add_command(label="Abrir", command=self.open_file) self.filemenu.add_command(label="Salvar", command=self.save) self.filemenu.add_command(label="Resetar", command=self.resetImage) self.filemenu.add_separator() self.filemenu.add_command(label="Sair", command=self.root.quit) self.menubar.add_cascade(label="Arquivo", menu=self.filemenu) # pen button self.pen_button = Button(self.root, text='Pen', command=self.use_pen, relief=SUNKEN) self.pen_button.grid(row=1, column=0) # self.brush_button = Button(self.root, text='Brush', command=self.use_brush) # self.brush_button.grid(row=0, column=1, columnspan=2) # self.color_button = Button(self.root, text='Color', command=self.choose_color) # self.color_button.grid(row=0, column=2, columnspan=2) # eraser button self.eraser_button = Button(self.root, text='Eraser', command=self.use_eraser) self.eraser_button.grid(row=1, column=1) # slider size pen and eraser self.choose_size_button = Scale(self.root, from_=1, to=10, orient=HORIZONTAL) self.choose_size_button.grid(row=1, column=3) # move button self.btn_move = Button(self.root, text='Move', command=self.use_move) self.btn_move.grid(row=1, column=4) # open first image to iterate #imageCV = Image.open('Images/result-cell-1.png').resize((guiX, guiY), Image.ANTIALIAS) self.imageCV = cv2.imread('Images/first image.png') self.lin, self.col, _ = self.imageCV.shape #self.imageCV = CellDetectionImage(self.imageCV) # modified image in interface self.imageCV600cell = cv2.resize(self.imageCV, (guiX, guiY), interpolation=cv2.INTER_AREA) self.goldImage = ImageTk.PhotoImage(Image.fromarray(self.imageCV600cell)) # self.goldImage = ImageTk.PhotoImage(imageCV) self.label_image = Label(self.root, image=self.goldImage) self.label_image.grid(row=2, column=0, columnspan=3) self.label_image.bind("<Button>", self.select_area) self.imgright = np.copy(self.imageCV) self.imgOriginalCV = self.imgright.copy() self.image1 = Image.new("RGB", (self.col, self.lin), (0, 0, 0)) self.image1.getcolors() self.draw = ImageDraw.Draw(self.image1) self.imscale = 1.0 self.imageid = None self.delta = 0.90 self.c = Canvas(self.root, bg='white', width=guiX, height=guiY, highlightthickness=0, xscrollcommand=hbar.set, yscrollcommand=vbar.set, cursor="pencil") self.c.grid(row=2, column=3, columnspan=2, sticky='nswe') self.text = self.c.create_text(0, 0, anchor='nw') self.show_image() #self.imgLeft = ImageTk.PhotoImage(Image.open('Images/first image.png').resize((guiX, guiY), Image.ANTIALIAS)) #self.c.create_image(0, 0, image=self.imgLeft, anchor=NW) vbar.configure(command=self.c.yview) # bind scrollbars to the canvas hbar.configure(command=self.c.xview) # Make the canvas expandable self.root.rowconfigure(0, weight=1) self.root.columnconfigure(0, weight=1) # Bind events to the Canvas self.c.bind('<ButtonPress-1>', self.move_from) self.c.bind('<B1-Motion>', self.move_to) self.c.bind('<MouseWheel>', self.wheel) # with Windows and MacOS, but not Linux self.c.bind('<Button-5>', self.wheel) # only with Linux, wheel scroll down self.c.bind('<Button-4>', self.wheel) # only with Linux, wheel scroll up self.c.configure(scrollregion=self.c.bbox('all')) # self.last_image = Button(self.root, text='Last image', command=self.last_img) # self.last_image.grid(row=2, column=0, columnspan=2) self.last_original = Button(self.root, text='Original image', command=self.original_img) self.last_original.grid(row=4, column=0, columnspan=3) # slider size of area self.choose_area = Scale(self.root, from_=0, to=80, orient=HORIZONTAL, command=self.remove_area, length=300) self.choose_area.grid(row=3, column=0) # self.next_image = Button(self.root, text='Next image', command=self.next_img) # self.next_image.grid(row=2, column=4, columnspan=2) self.btn_apply = Button(self.root, text='Apply', command=self.apply_modification) self.btn_apply.grid(row=4, column=3, columnspan=3) self.btn_undo = Button(self.root, text='UNDO', command=self.undo) self.btn_undo.grid(row=0, column=0) self.btn_redo = Button(self.root, text='REDO', command=self.redo) self.btn_redo.grid(row=0, column=1) self.label_image1 = np.zeros([guiX, guiY], dtype=np.uint8) self.imageInMermory = cv2.cvtColor(self.imageCV, cv2.COLOR_BGR2GRAY) self.imageInMermory[self.imageInMermory > 0] = 255 self.open_file() self.move_on = False self.setup() self.translatePortuguese() self.root.mainloop()
def test_dequeue_empty_queue(self): """ Should throw exception """ queue = CircularQueue() with self.assertRaises(Exception): queue.dequeue()
def __init__(self, rocket): self.data_buffer = CircularQueue(10) self.alt_buffer = CircularQueue(10) self.v_buffer = CircularQueue(10) self.rocket = rocket self.base_alt = 0
from circular_queue import CircularQueue q = CircularQueue(5) q.insert(1) q.insert(2) q.insert(3) q.insert(4) q.status() q.insert(5) q.insert(6) q.insert(7) q.insert(8) q.insert(9) print(q)
def circular_queue(): return CircularQueue()
def test_circular_queue_constructor(): queue = CircularQueue() assert len(queue) == 0 assert len(queue._array) == 10
from circular_queue import CircularQueue q = CircularQueue(3) vip = CircularQueue(3) def main(): input1 = input('Add, Serve, or Exit:') input2 = input('Enter the customer name:') input3 = input('Is the customer VIP?') if input1 == 'Serve': if q.is_empty() and vip.is_empty(): print('Error:Both queues are empty') main() else: if vip.is_empty(): print(q.peek(), 'has been served.') q.dequeue() rpp() else: print(vip.peek(), 'has been served.') vip.dequeue() rpp() main() elif input1 == 'Add': if q.size() == 3 and input3 == 'false': print('Error: Normalcustomers queue is full') rpp() main() elif input3 == 'true' and vip.size() == 3: print('Error: VIP customers queueis full')