def __init__(self, pid, url, depth, maxlink, post, cookie, host, regex, authorization): self.result = queue() self.urlhashmap = {} self.thirdqueue = queue() tmp = {} tmp['host'] = host tmp['url'] = url tmp['post'] = post tmp['src'] = '' tmp['referer'] = url tmpqueue = queue() tmpqueue.push(tmp) self.urlhashmap[0] = tmpqueue # 把第一个url(任务队列)放进第一层[0],从这网页中爬去的放在下一层[1],依次 self.host = urlparse.urlparse(url)[1] # self.maxdepth 爬虫深度 self.maxdepth = depth # self.maxlink 最多爬的url树木 self.maxlink = maxlink # 正则匹配内容 self.regex = regex self.auth = authorization self.post = post self.urlmd5 = [] self.depth = 0 self.pid = pid self.auth = authorization self.tmpqueue = queue() self.cookie = cookie self.headers = {"Content-Type": "application/x-www-form-urlencoded"} self.user_agent = "Mozilla/5.0 (Linux; U; Android 4.1.2; zh-cn; HUAWEI MT1-T00 Build/HuaweiMT1-T00) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 AlipayDefined(nt:WIFI,ws:480|805|1.5) AliApp(AP/8.6.0.040305) AlipayClient/8.6.0.040305"
def __init__(self, phjs, pid, url, depth, maxlink, post, cookie, host, regex, authorization): self.target = url self.rules = [] self.domfules = [] self.result = queue() self.urlhashmap = {} self.thirdqueue = queue() tmp = {} tmp['host'] = host tmp['url'] = url tmp['post'] = post tmp['src'] = '' tmp['referer'] = url tmpqueue = queue() tmpqueue.push(tmp) self.urlhashmap[ 0] = tmpqueue # 把第一个url(任务队列)放进第一层[0],从这网页中爬去的放在下一层[1],依次 self.host = urlparse.urlparse(url)[1] # self.maxdepth 爬虫深度 self.maxdepth = depth # self.maxlink 最多爬的url树木 self.maxlink = maxlink # 正则匹配内容 self.regex = regex self.auth = authorization self.post = post self.urlmd5 = [] self.depth = 0 self.phjs = phjs self.pid = pid self.tmpqueue = queue() self.cookie = cookie self.headers = { "Accept-Language": "zh-CN,zh;q=0.8", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36", "Cache-Control": "max-age=0", "Cookie": "Hm_lvt_a4ca63a1a1903a32ce375a3f83ed1ea8=1491900098; _ga=GA1.2.16789343.1489375761; PHPSESSID=5qgseeafq13e570d5hicbjcoj3; jsessionid|JSESSIONID=59f68366110c4900c690eddc02fa08d5; cardNo=8100100000804988; login_arr=a%3A10%3A%7Bs%3A4%3A%22name%22%3Bs%3A8%3A%22testtest%22%3Bs%3A6%3A%22CardNo%22%3Bs%3A16%3A%228100100000804988%22%3Bs%3A7%3A%22VipType%22%3Bs%3A3%3A%22IVM%22%3Bs%3A8%3A%22VipLevel%22%3Bs%3A1%3A%220%22%3Bs%3A9%3A%22FirstName%22%3Bs%3A4%3A%22test%22%3Bs%3A8%3A%22LastName%22%3Bs%3A4%3A%22test%22%3Bs%3A6%3A%22Mobile%22%3Bs%3A11%3A%2218521305769%22%3Bs%3A5%3A%22Email%22%3Bs%3A12%3A%22abc%40wanda.cn%22%3Bs%3A3%3A%22Sex%22%3Bs%3A1%3A%22M%22%3Bs%3A6%3A%22Points%22%3Bs%3A1%3A%220%22%3B%7D; form_username=18521305769; form_password=s%3A6%3A%22321073%22%3B; form_check=1; Hm_lvt_409ce23c3f2dfd3322530519dd81f558=1497858006; Hm_lpvt_409ce23c3f2dfd3322530519dd81f558=1497858129; Hm_lvt_51179d8b3807ddcb0ad60f026cd9028c=1497858006; Hm_lpvt_51179d8b3807ddcb0ad60f026cd9028c=1497858129", "Upgrade-Insecure-Requests": "1", "Accept-Encoding": "gzip, deflate, sdch" } self.headers = str(self.headers) #self.rules = load_site_policey(self.host) #self.domfules = load_site_policey_dom(self.host) self.rules = [1, 2] self.domfules = [3, 4] self.flag = 0 #漏洞标示,0为正常,1为xss
def __init__(self, host = "localhost", port = 6003, parent=None): super(ControlManagement, self).__init__(parent) self.FRAMES_DICT = { 'A' : self.showAxesValues, 'M' : self.setMotorsValues, 'S' : self.setResponse, 'O' : self.outputResponse } self.buttonsSettingsMotors.saveBtn.clicked.connect(self.saveButtonsSettingsMotors) self.buttonsSettingsManipulator.saveBtn.clicked.connect(self.saveButtonsSettingsManipulator) self.stopMotorsBtn.clicked.connect(self.stopMotors) self.modeOnBtn.clicked.connect(self.arm) self.modeOffBtn.clicked.connect(self.disarm) self.outputsControl.stateChanged.connect(self.setOutput) self.host = host self.port = port self.q = queue.queue() self.timer = QTimer() self.timer.setInterval(UPTIME_INTERVAL) self.timer.timeout.connect(self.sendUptime) self.thread = echoThread.echoThread(self, self.host, self.port) self.thread.connectionState.connect(self.initParams) self.thread.start() self.initMotorsControl() self.initManipulatorControl()
def __init__(self, queue, summary, dirOutput, contentType, model, url=None, headers=None, debug=None): WebSocketClientFactory.__init__(self, url=url, headers=headers) self.queue = queue self.summary = summary self.dirOutput = dirOutput self.contentType = contentType self.model = model self.queueProto = queue.queue() self.openHandshakeTimeout = 10 self.closeHandshakeTimeout = 10 # start the thread that takes care of ending the reactor so # the script can finish automatically (without ctrl+c) endingThread = threading.Thread(target=self.endReactor, args=()) endingThread.daemon = True endingThread.start()
def urlFilter(self, item, referer): if item['referer'] == "": item['referer'] = referer try: urlpart = urlparse.urlparse(str(item['url'])) except Exception as e: return False urlmd5 = self.getMd5(item['url'], item['post']) if self.host == urlpart[1]: if urlmd5 not in self.urlmd5: self.urlmd5.append(urlmd5) self.result.push(item) depth = self.depth + 1 if self.urlhashmap.has_key(depth): self.urlhashmap[depth].push(item) else: tmp = queue() tmp.push(item) self.urlhashmap[depth] = tmp else: if urlmd5 not in self.urlmd5: self.thirdqueue.push(item) self.urlmd5.append(urlmd5)
def consumer(): from pymongo import MongoClient from queue import setup, queue conn = MongoClient() db = conn.test queue = queue(db, 'messages', size=16) rec = None while rec is None or rec.get('nop'): rec = next(queue) from time import time n = 0 start = time() for rec in queue: n += 1 duration = time() - start timeper = duration / float(n) * 1000 msgper = float(n) / duration print "%0.2fs for %d messages: %0.2f usec/gen (%d messages/sec)" % (duration, n, timeper, msgper)
def urlFilter(self, item, referer): if item['referer'] == "": item['referer'] = referer try: urlpart = urlparse.urlparse(str(item['url'])) # print urlpart urlpart_topdomain = get_domain(str(item['url'])) # logging.debug("urlpart:%s" % str(urlpart)) except Exception as e: logging.debug("urlpart:%s" % str(urlpart)) return False url_infos = converturl(str(item['url'])) #themd5 = self.make_md5(item['url'], item['post']) themd5 = hash.similarity(item['url'], item['post']) if urlpart and self.host in urlpart[1] and self.extFilter( str(url_infos['ext'])): # 不检测子域名。 #if urlpart_topdomain and urlpart_topdomain in self.host and self.extFilter(str(url_infos['ext'])): if themd5 not in self.urlmd5: # self.xssScan(item['url'], item['post'])#对于同类型url仅作一次检测 self.urlmd5.append(themd5) self.result.push(item) depth = self.depth + 1 if self.urlhashmap.has_key(depth): self.urlhashmap[depth].push(item) else: tmp = queue() tmp.push(item) self.urlhashmap[depth] = tmp else: if themd5 not in self.urlmd5: # self.xssScan(item['url'], item['post'])#对于同类型url仅作一次检测 self.thirdqueue.push(item) self.urlmd5.append(themd5)
def stress_test(): ready = queue() y = 0 #while True: for y in range(1, 100): rand = randint(2, 4) PCBRand = randint(0, 1000) #print("---------Rolling Dice, Start of Round---------") if rand == 1: #print("Adding PCB", PCBRand) ready.add(PCB(PCBRand, priority=randint(1, 4))) elif rand == 2: print("Adding PCB PID: ", PCBRand) ready.add(PCB(PCBRand, priority=randint(1, 4)), position=randint(0, 1)) elif rand == 3: #print("Deleting PCB!") ready.delete() elif rand == 4: print("Deleting a PCB by PID!") print("Trying to delete ", PCBRand) ready.delete(PCBRand) print("Final queue size:", ready.size) #sleep(4) ready.show()
def __init__(self, roidb, num_loaders=4, minibatch_queue_size=64, blobs_queue_capacity=8): self._roidb = roidb self._lock = threading.Lock() self._perm = deque(range(len(self._roidb))) self._cur = 0 # _perm cursor # The minibatch queue holds prepared training data in host (CPU) memory # When training with N > 1 GPUs, each element in the minibatch queue # is actually a partial minibatch which contributes 1 / N of the # examples to the overall minibatch self._minibatch_queue = queue.queue(maxsize=minibatch_queue_size) self._blobs_queue_capacity = blobs_queue_capacity # Random queue name in case one instantiates multple RoIDataLoaders self._loader_id = uuid.uuid4() self._blobs_queue_name = 'roi_blobs_queue_{}'.format(self._loader_id) # Loader threads construct (partial) minibatches and put them on the # minibatch queue self._num_loaders = num_loaders self._num_gpus = cfg.NUM_GPUS self.coordinator = Coordinator() self._output_names = get_minibatch_blob_names() self._shuffle_roidb_inds() self.create_threads()
def deserializeTree(arr): if not arr: return None q = queue() root = Node(arr[0]) q.enqueue(root) index = 0 while not q.isEmpty(): x = q.dequeue() if 2 * index + 1 < len(arr) and arr[2 * index + 1]: x.left = Node(arr[2 * index + 1]) q.enqueue(x.left) else: x.left = None if 2 * index + 2 < len(arr) and arr[2 * index + 2]: x.right = Node(arr[2 * index + 2]) q.enqueue(x.right) else: x.right = None index += 1 return root
def geneCluster(root, graph): visitedSet = set() # build the rootNode nextNodesQueue = queue() rootNode = Node(root) # set the initial condition visitedSet.add(rootNode.nodeId) nextNodesQueue.put(rootNode) # all nodes nodeDict = {} nodeDict[rootNode.nodeId] = rootNode while not nextNodesQueue.empty(): nextNode = nextNodesQueue.get() for node in graph[nextNode.nodeId]: # build a subNode subNode = Node(node) if node not in visitedSet: # mark as visited visitedSet.add(subNode.nodeId) # add the subNode to queue nextNodesQueue.put(subNode) nodeDict[subNode.nodeId] = subNode return visitedSet
def consumer(): from pymongo import MongoClient from queue import setup, queue conn = MongoClient() db = conn.test queue = queue(db, 'messages', size=16) rec = None while rec is None or rec.get('nop'): rec = next(queue) from time import time n = 0 start = time() for rec in queue: n += 1 duration = time() - start timeper = duration / float(n) * 1000 msgper = float(n) / duration print "%0.2fs for %d messages: %0.2f usec/gen (%d messages/sec)" % ( duration, n, timeper, msgper)
def __init__(self, context, subaddr=None, pubaddr=None): threading.Thread.__init__(self) #private data self._queue = queue.queue() self._subsocket = context.socket(zmq.SUB) self._pubsocket = context.socket(zmq.PUB) self._subaddr = subaddr self._pubaddr = pubaddr if type(self._subaddr) is str: self._subaddr = [self._subaddr] if type(self._pubaddr) is str: self._pubaddr = [self._pubaddr] self._sub_connected = False self._pubsub = pubsub() if self._pubaddr is not None: for addr in self._pubaddr: self._pubsocket.bind(addr) self._poller = zmq.Poller() self._poller.register(self._subsocket, zmq.POLLIN) #public data self.shutdown = threading.Event() self.finished = threading.Event() #init self.setDaemon(True) self.start()
def word2vid(keyword): keys = getkeys() consumer_key = keys[0] consumer_secret = keys[1] access_key = keys[2] access_secret = keys[3] line = keyword words = line.split(',') q = queue(words) video_num = 0 print(q.isEmpty()) print(q.length()) while not q.isEmpty(): word = q.items[0] image0 = "images/" + str(video_num) + "-%01d.png" video = word + ".avi" tweets = readtweets(word, consumer_key, consumer_secret, access_key, access_secret) imageflow(video_num, tweets) video_num = video_num + 1 subprocess.call( ['ffmpeg', '-y', '-framerate', '.1', '-i', image0, video]) q.queuedown() return keyword
def __init__(self, soup, url): self.tmpqueue = queue() self.url = url self.post = "" self.method = "" self.tmp = "" self.soup = soup
def __init__( self, usage_filename ): self.usage_filename = usage_filename self.comments = queue.queue() self.variable_library = library.library() self.variable_string_dictionary = \ string_dictionary.string_dictionary()
def __init__(self, h5_file_name, h5_image_name, shuffle=True, prefetch_num=8): # self.img_folder = img_folder self.h5_file_name = h5_file_name self.h5_image = h5_image_name self.shuffle = shuffle self.prefetch_num = prefetch_num self.n_batch = 0 self.n_epoch = 0 # Search the folder to see the number of num_batch self.h5_file = h5py.File(h5_file_name, 'r') self.h5_image = h5py.File(h5_image_name, 'r') num_batch = self.h5_file['image_idxs'].shape[0] # n? if num_batch > 0: print('found %d batches within %s' % (num_batch, h5_file_name)) else: raise RuntimeError('no batches within %s' % (h5_file_name)) self.num_batch = num_batch # 一共有多少个batch # Start prefetching thread self.prefetch_queue = queue.queue(maxsize=prefetch_num) # 读数据的线程,只有一个? self.prefetch_thread = threading.Thread( target=run_prefetch, args=(self.prefetch_queue, self.h5_file, self.h5_image, self.num_batch, self.shuffle)) self.prefetch_thread.daemon = True self.prefetch_thread.start()
def find_island(self, x, y): if (x, y) not in self.map: return None, None, None first = ((x, y), (0, 0)) gateway = None bounds = [0, 0, 0, 0] next = queue() search = set() islands = set() next.put(first) while not next.empty(): for cell in surround(*next.get(), self.size): if cell not in search: search.add(cell) coord, rel = cell # TODO: FIX if coord in self.structs: islands.add(coord) next.put(cell) if (rel[0] < bounds[0]): bounds[0] = rel[0] if (rel[1] < bounds[1]): bounds[1] = rel[1] if (rel[0] > bounds[2]): bounds[2] = rel[0] if (rel[1] > bounds[3]): bounds[3] = rel[1] if coord in self.gateways: gateway = coord island_size = (bounds[2] - bounds[0], bounds[3] - bounds[1]) return islands, gateway, island_size
def test_pop(): q = queue(10) q.push(1) size = len(q) assert q.pop() == 1 assert len(q) == size - 1 assert 1 not in q
def __init__(self, *args, **kwargs): max_queue_size = kwargs.pop('max_queue_size', 10000) super(AsyncGELFHandler, self).__init__(*args, **kwargs) Thread.__init__(self) self.output_queue = queue(maxsize=max_queue_size) # Start thread self.start()
def test_keep_order(): q = queue(10) q.push(1) q.push(2) q.push(3) assert q.pop() == 1 assert q.pop() == 2 assert q.pop() == 3
def initCal(self): """ :return: 无,用于初始化计算 """ # 只需要记元组中前三个数即可表示四个数 for i in range(35): self.dict_a1_3[(a1_3[i][0], a1_3[i][1], a1_3[i][2])] = i for i in range(20): self.dict_a4[(a4[i][0], a4[i][1], a4[i][2])] = i q = queue() # 计算目的结果 self.indexChanger[0], self.indexChanger[1] = self.dict_a1_3[( 4, 0, 0)], self.dict_a1_3[(0, 4, 0)] self.indexChanger[2], self.indexChanger[3] = self.dict_a1_3[( 0, 0, 4)], self.dict_a4[(0, 0, 0)] start_value = self.calcIndex() record[start_value] = 0 q.put(start_value) while not q.empty(): value = q.get() self.getIndex(value) num2 = [0, 0, 0, 0] for i in range(4): for j in range(4): if i == 3: self.array[i][j] = a4[self.indexChanger[i]][j] else: self.array[i][j] = a1_3[self.indexChanger[i]][j] num2[j] = num2[j] + self.array[i][j] point = 0 # 找到不符合目的的point while point < 4 and num2[point] == 4: point = point + 1 for i in range(-1, 2, 2): if 0 <= point + i < 4: for j in range(4): if self.array[j][point + i]: self.array[j][point + i] = self.array[j][point + i] - 1 self.array[j][point] = self.array[j][point] + 1 for z in range(4): if z == 3: self.indexChanger[z] = self.dict_a4[( self.array[z][0], self.array[z][1], self.array[z][2])] else: self.indexChanger[z] = self.dict_a1_3[( self.array[z][0], self.array[z][1], self.array[z][2])] newValue = self.calcIndex() if record.get(newValue, -1) == -1: record[newValue] = record[value] + 1 q.put(newValue) self.array[j][point + i] = self.array[j][point + i] + 1 self.array[j][point] = self.array[j][point] - 1
def hotpotato(names = [], num = None): circleJerk = queue.queue() for name in names: circleJerk.enqueue(name) while circleJerk.size() > 1: for i in range(num): circleJerk.enqueue(circleJerk.dequeue()) circleJerk.dequeue() return circleJerk.dequeue()
def _bfs(self, oper): q = queue() q.push(self.root) oper(self.root) while q: p = q.pop() for v in p.child.itervalues(): oper(p) q.push(v)
def __init__(self, conf): self.conf = conf self.s = socket.socket(conf.af) self.s.setsockopt(socket.sol_socket, socket.so_reuseaddr, 1) self.s.bind(conf.addr) self.s.listen(5) self.running = false self.thread = none self.queue = queue.queue() # report connections and exceptions to client
def bfs(self, start, target): q = queue() q.enqueue(start) while not q.isEmpty(): node = q.dequeue() if (node == target): print('found: ' + str(node)) return for n in self.table[node]: q.enqueue(n)
def __init__(self, q = queue(), host = "localhost", port = 9998, delay = 0.075): super(commThread, self).__init__() self.delay = delay self.host = host self.port = port self.q = q self.stop = False self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.settimeout(2) self.sock.connect((host, port)) self.counter = 0
def hotPotato(namelist , num): que = queue.queue() for names in namelist: que.enqueue(names) while que.size() > 1: for i in range(num): que.enqueue(que.dequeue()) que.dequeue() return que.dequeue()
def __init__(self, q=queue(), host="localhost", port=9998, delay=0.075): super(commThread, self).__init__() self.delay = delay self.host = host self.port = port self.q = q self.stop = False self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.settimeout(2) self.sock.connect((host, port)) self.counter = 0
def main(): #Start threads q = queue.queue() threads = [MyProducer(q)] threads.extend([MyConsumer(q, i) for i in range(8)]) for th in threads: th.start() #Wait for the threads to finish for th in threads: th.join()
def test_enqueue(): test = q.queue() test.enqueue(1) test.enqueue(2) test.enqueue(3) assert test.dequeue() == 1 assert test.dequeue() == 2 assert test.dequeue() == 3 with pytest.raises(Exception) as dequeue_empty: test.dequeue() assert dequeue_empty.value.message == 'Empty Queue!'
def bft_print(self): q = queue() q.enqueue(self) while q.size != 0: current = q.dequeue() print(current.value) if current.left: q.enqueue(current.left) if current.right: q.enqueue(current.right)
def _buildFailPoint(self): """Based on bfs strategy.""" q = queue() for s in self.root.child.itervalues(): s.fail = self.root q.push(s) while q: p = q.pop() for c, s in p.child.iteritems(): s.fail = self.transit(c, p.fail) q.push(s)
def levelOrderTraversal_bfs(self, l=None): if not self.__root: return if not l: l = [] curL, nexL = queue(), queue() curL.enqueue(self.__root) temp = [] while not curL.isEmpty(): node = curL.dequeue() if node: temp.append(node.val) if node.left: nexL.enqueue(node.left) if node.right: nexL.enqueue(node.right) if curL.isEmpty(): l.append(temp) temp = [] curL, nexL = nexL, curL return l
def __init__(self, processed=None, spider=None, blacklist=None): if processed == None: self.processed_urls = [] else: self.processed_urls = processed if spider == None: self.spider_urls = queue() else: self.spider_urls = spider if blacklist == None: self.blacklisted_urls = [] else: self.blacklisted_urls = blacklist
def solver(Board): visited, shortest, end_game = [], [], False line, qtree, A = queue(), queue(), tree(Board.start, []) moves = get_possible_move_positions(Board.Board, Board.start, Board.dimensions) while end_game == False: for i in range(len(moves)): if moves[i] not in visited: visited.append(moves[i]) line.enqueue(moves[i]) qtree.enqueue(A.key[1][A.add_child(moves[i])]) if moves[i] == Board.end: end_game = True break A, moves = qtree.dequeue(), get_possible_move_positions( Board.Board, line.dequeue(), Board.dimensions) while A.key[0] != Board.end: A = qtree.dequeue() while A.key[0] != Board.start: shortest.append(A.key[0]) A = A.parent return shortest
def hotpotato(namelist, num): simqueue = queue() # simqueue = Queue() for name in namelist: simqueue.enqueue(name) while simqueue.size() > 1: print "ke-%d" %simqueue.size() for i in range(num): simqueue.enqueue(simqueue.dequeue()) print i, ": ", simqueue.show() simqueue.dequeue() quit()
def solve(self): start = self.convert_to_tuple(self.board) pred = {} visited = [] frontier = queue.queue() frontier.put(start) while frontier.qsize() > 0: tmp = frontier.get() if tmp == self.goal: path = [] while tmp != start: path.append(pred[tmp][1]) tmp = pred[tmp][0] return path[::-1] if tmp not in visited: visited.append(tmp) tmpboard = self.match(tmp) tmpboard.move_up() if self.convert_to_tuple(tmpboard.board) != tmp: frontier.put(self.convert_to_tuple(tmpboard.board)) if not pred.has_key(self.convert_to_tuple(tmpboard.board)): pred[self.convert_to_tuple(tmpboard.board)]=[tmp, 'up'] tmpboard = self.match(tmp) tmpboard.move_down() if self.convert_to_tuple(tmpboard.board) != tmp: frontier.put(self.convert_to_tuple(tmpboard.board)) if not pred.has_key(self.convert_to_tuple(tmpboard.board)): pred[self.convert_to_tuple(tmpboard.board)]=[tmp, 'down'] tmpboard = self.match(tmp) tmpboard.move_right() if self.convert_to_tuple(tmpboard.board) != tmp: frontier.put(self.convert_to_tuple(tmpboard.board)) if not pred.has_key(self.convert_to_tuple(tmpboard.board)): pred[self.convert_to_tuple(tmpboard.board)]=[tmp, 'right'] tmpboard = self.match(tmp) tmpboard.move_left() if self.convert_to_tuple(tmpboard.board) != tmp: frontier.put(self.convert_to_tuple(tmpboard.board)) if not pred.has_key(self.convert_to_tuple(tmpboard.board)): pred[self.convert_to_tuple(tmpboard.board)]=[tmp, 'left'] raise Exception('There is no solution.')
def Get_LevelOrder(self): Q = queue() Q.enqueue(self.store) accum = [] while (Q.Empty() == False): node = Q.dequeue() #print ('the node[0] is ',str(node[0])) #print node accum = accum + [node[0]] for i in range(0, len(node[1])): Q.enqueue(node[1][i].store) #print accum print accum return accum
def multithreading(data): w = queue() threads = [] data = [[1,2,3],[2,2,2],[3,3,3],[4,4,4]] for i in range(4): t = threading.Thread(target=job,aegs=(data[i], w)) t.start() threads.append(t) for thread in threads: thread.join() results = [] for j in range(4): results.append(w.get()) print(results)
def randomKeysToQueue(coordinationGraph): """ :param coordinationGraph: the coordination graph to randomise the keys of :return: a shuffled queue of keys """ temp = [] q = queue() for key in coordinationGraph.nodesAndConnections.keys(): temp.append(key) random.shuffle(temp) for item in temp: q.put(item) return q
def small_rand_test(): ready = queue() y = 0 for y in range(1, 20): randPID = randint(1, 10) randArrive = (randint(0, 10)) randBurst = (randint(0, 10)) tmpPCB = PCB(randPID, arrival=randArrive, burst=randBurst) simulateAdding = [] simulateAdding.append(tmpPCB) simulateAdding.sort() print(simulateAdding)
def BFS(self): q = queue() node = self.__root if node: q.enqueue(node) else: return [] while not q.isEmpty(): node = q.dequeue() print node.val, if node.left: q.enqueue(node.left) if node.right: q.enqueue(node.right) print ""
def bfs(maze, start, end, walls): #initialize everything you have to, a copy of the maze, a queue, visited and previous so you can go back maze2 = copy.deepcopy(maze) m_queue = queue() visited = copy.deepcopy(walls) prev = copy.deepcopy(maze) steps = 0 nodesExpanded = 0 m_queue.enqueue(start) #push start onto the queue #iterate until the queue is empty while not m_queue.isEmpty(): currentPoint = m_queue.dequeue() #remove a node from the stack and expand it nodesExpanded += 1 if visited[currentPoint[0]][currentPoint[1]] is False: #if you havent visited the node yet visited[currentPoint[0]][currentPoint[1]] = True if(currentPoint[0] == end[0] and currentPoint[1] == end[1]): #break if you've reached the end break else: if not (currentPoint[1] - 1) < 0 and not visited[currentPoint[0]][currentPoint[1] - 1]: prev[currentPoint[0]][currentPoint[1] - 1] = [currentPoint[0], currentPoint[1]] #set the previous for the next node to be the current node m_queue.enqueue([currentPoint[0], currentPoint[1] - 1]) #and add it onto the queue if not (currentPoint[0] + 1) >= len(walls) and not visited[currentPoint[0] + 1][currentPoint[1]]: prev[currentPoint[0] + 1][currentPoint[1]] = [currentPoint[0], currentPoint[1]] m_queue.enqueue([currentPoint[0] + 1, currentPoint[1]]) if not (currentPoint[1] + 1) >= len(walls[0]) and not visited[currentPoint[0]][currentPoint[1] + 1]: prev[currentPoint[0]][currentPoint[1] + 1] = [currentPoint[0], currentPoint[1]] m_queue.enqueue([currentPoint[0], currentPoint[1] + 1]) if not (currentPoint[0] - 1) < 0 and not visited[currentPoint[0] - 1][currentPoint[1]]: prev[currentPoint[0] - 1][currentPoint[1]] = [currentPoint[0], currentPoint[1]] m_queue.enqueue([currentPoint[0] - 1, currentPoint[1]]) current = end steps = 0 while maze[current[0]][current[1]] != 'P': #make out a solution path from the endpoint going backwards incrementing to steps everytime current = prev[current[0]][current[1]] maze2[current[0]][current[1]] = '.' steps += 1 maze2[start[0]][start[1]] = 'P' return maze2, steps, nodesExpanded
def _bfs(g, s): s.type = 1 q = queue(2 * len(g.vertices)) q.enqueue(s) while not q.empty(): u = q.dequeue() for v in g.adj[u]: if u.type == v.type: return False elif v.type == 0: if u.type == 1: v.type = 2 else: v.type = 1 q.enqueue(v) return True
def __init__(self, arguments): signal.signal(signal.SIGINT, self.signalHandler) self.arguments = arguments self.checked = [] self.recursiveQueue = queue() self.recursiveList = [] if (self.arguments.query): self.searchQuery(arguments.query, arguments.google, arguments.bing) if (self.arguments.site): self.recursiveQueue.put(arguments.site) while not self.recursiveQueue.empty(): site = self.recursiveQueue.get() #print("Searching in {0}".format(site)) query = "site:{0}".format(site) if self.arguments.custom: query += " " + self.arguments.custom self.searchQuery(query, arguments.google, arguments.bing)
def __serializeTree(self, node, l=None): if not l: l = [] q = queue() if not node: return [None] else: q.enqueue(node) while not q.isEmpty(): x = q.dequeue() if not x: l.append(None) else: l.append(x.val) q.enqueue(x.left) q.enqueue(x.right) return l
def bfs(self, s): for u in self.vertices: u.d = float("Inf") u.color = 0 u.p = None s.color = 1 s.d = 0 s.p = None q = queue(2 * len(self.vertices)) q.enqueue(s) while not q.empty(): u = q.dequeue() for v in self.adj[u]: if v.color == 0: v.color = 1 v.d = u.d + 1 v.p = u q.enqueue(v) u.color = 2
def __call__(self, func): cache = {} q = queue() # a list of keys @functools.wraps(func) def memoized(*args, **kwargs): hash_ = cPickle.dumps((args, set(kwargs.iteritems()))) try: result = cache[hash_] except KeyError: result = func(*args, **kwargs) if len(q) > self.maxsize: del cache[q.pop()] cache[hash_] = result else: q.remove(hash_) finally: q.push(hash_) return cache[hash_] return memoized
if __name__ == "__main__": from queue import queue print("Queue test") q = queue() #dequeue from empty queue.. exception waited print("is queue empty ? ", q.isEmpty()) try: q.dequeue() except Exception as e: print(e) #test enqueuing in queue list_s = ["hello", "cruel", "world"] for s in list_s: q.enqueue(s) #test iteration print("Iteration over queue") for item in q: print("-> ",item) #checking last element in queue print("(FIFO) First Output Element -->", q.last()) #test dequeueing print("Dequeue operation") for i in range(0,q.size()): print(q.dequeue())
from datetime import date import assignment1 import random import threading import queue # Create queues """ license_q = Queue() translation_q = Queue() eye_test_q = Queue() fail_q = Queue() print_q = Queue() """ license_q = queue.queue("A") translation_q = queue.queue("C") eye_test_q = queue.queue("B") fail_q = queue.queue("F") print_q = queue.queue("P") ## Array to keep customers that are in process busy_customers_eye = [] busy_customers_translate = [] eye_translation_list = [] # Create list to store successful applications fail_list = [] customer_list = [] success_list = [] ## Function to check document consistency - Done!!
def test_cant_pop_from_empty_queue(): q = queue(2) q.pop()
} }) # Set module specific log levels logging.getLogger('librato').setLevel(logging.CRITICAL) logging.getLogger('requests').setLevel(logging.CRITICAL) if _i_am_a_lambda_worker(): logging.getLogger(root_package_name).setLevel(logging.WARNING) logging.getLogger(__name__).setLevel(logging.WARNING) else: logging.getLogger(root_package_name).setLevel(logging.INFO) logging.getLogger(__name__).setLevel(logging.INFO) #amend the logging configuration with a handler streaming to a message queue q = queue(-1) ql = MutableQueueListener(q) qh = QueueHandler(q) logging.root.addHandler(qh) ql.start() def stop_queue_listener(): ql.stop() def _attach_log_handler(handler): ql.addHandler(handler) def _detach_log_handler(handler): ql.removeHandler(handler)
def test_dequeue(): with pytest.raises(Exception) as dequeue_empty: test = q.queue() test.dequeue() #import pdb; pdb.set_trace() assert dequeue_empty.value.message == 'Empty Queue!'
def test_constructor(): test = q.queue() assert test.head == None
# need better error handling /notification for # what are currently "fatal" errors # at minimum: update request status so # we are aware and can check it or restart it import os import sys import time sys.stdout = sys.stderr = open(os.path.dirname(os.path.abspath(__file__)) +'/processing.log', 'a') from queue import queue from cache import cache from documentation import doc queue = queue() cache = cache() doc = doc() queue.cache = cache queue.doc = doc request_id = 0 print '\n------------------------------------------------' print 'Prep Script' print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # run given a request_id via input (does not run extract) if len(sys.argv) == 2: request_id = sys.argv[1]