def run(self): if not self.saveinput(): return env = os.environ.copy() env['OMMPROTOCOL_SLAVE'] = '1' env['PYTHONIOENCODING'] = 'latin-1' self.task = Task("OMMProtocol for {}".format(self.filename), cancelCB=self._clear_cb, statusFreq=((1, ), 1)) self.subprocess = Popen( ['ommprotocol', self.filename], stdout=PIPE, stderr=PIPE, progressCB=self._progress_cb, #universal_newlines=True, bufsize=1, env=env) self.progress = SubprocessTask("OMMProtocol", self.subprocess, task=self.task, afterCB=self._after_cb) self.task.updateStatus("Running OMMProtocol") self.queue = LifoQueue() thread = Thread(target=enqueue_output, args=(self.subprocess.stdout, self.queue)) thread.daemon = True # thread dies with the program thread.start() self.ensemble = _TrajProxy() self.molecule = self.ensemble.molecule = self.gui.ui_chimera_models.getvalue( ) self.ensemble.name = 'Trajectory for {}'.format(self.molecule.name) self.ensemble.startFrame = self.ensemble.endFrame = 1 self.movie_dialog = MovieDialog(self.ensemble, externalEnsemble=True) self.gui.Close()
def _onSizeChanged(self): self._cache = _TilesCache(self._current_stack_id, self._sims, maxstacks=self._cache_size) self._dirtyLayerQueue = LifoQueue(self._request_queue_size) self._prefetchQueue = Queue(self._request_queue_size) self.sceneRectChanged.emit(QRectF())
class PooledIncomingQueue(IncomingQueue): def init_queues(self, n=5, buffsize=0, maxsize=1000*1000*1000): maxsize = maxsize / n self.write_executor = ThreadPoolExecutor(poolsize=1, queuesize=100) self.rqfile = FileDequeue(self.qdir, reader=FPSortingQueueFileReader) #self.rqfile = DummyFileDequeue(self.qdir) self.qfiles = [FileEnqueue(self.qdir, suffix=str(i), maxsize=maxsize, buffer=buffsize, executor=self.write_executor) for i in range(n)] self.avail = LifoQueue() for q in self.qfiles: self.avail.put(q) def shutdown(self): super(PooledIncomingQueue, self).shutdown() self.write_executor.shutdown() def add(self, curis): processed = 0 t0 = time.time() enq = self.avail.get() t = time.time() - t0 if t > 0.1: logging.warn('self.avail.get() %.4f', t) try: enq.queue(curis) self.addedcount += len(curis) processed += len(curis) return dict(processed=processed) finally: t0 = time.time() self.avail.put(enq) t = time.time() - t0 if t > 0.1: logging.warn('slow self.avail.put() %.4f', t)
def __init__(self, speech_state_machine): # Speech sampler self.__fs = 16000 self.__sampler_window_duration = 5 # seconds self.__sampler = Speech_Sampler(self.__sampler_window_duration, self.__fs) # Feature builder self.__feature_window_duration = 0.025 # seconds self.__feature_skip_duration = 0.01 # seconds self.__feature_nfilters = 26 self.__feature_nfilters_keep = 13 self.__feature_radius = 2 self.__feature_builder = ASR_Feature_Builder() # Processing self.__ignore_sample = False self.__max_queue_size = 10 self.__pool = multiprocessing.Pool() self.__process_sleep_time = 0.025 # seconds self.__queue_lock = threading.Lock() self.__plot_option = -1 self.__speech_segments = LifoQueue() self.__speech_state_machine = speech_state_machine self.__stop_processing = False self.__feature_builder.set_plot_blocking(True) self.__sampler.add_sample_callback(self.__queue_speech_segment) self.__sampler.hide_spectrogram_plot() self.__sampler.hide_zero_crossing_plot() self.__speech_state_machine.add_speech_match_callback(self.__speech_matched)
class EulerianCycleDFS: """Finding an Eulerian cycle in a multigraph. Attributes ---------- graph : input graph eulerian_cycle : list of nodes (length |E|+1) _graph_copy : graph, private _stack : LIFO queue, private Notes ----- Based on the description from: http://eduinf.waw.pl./inf/alg/001_search/0135.php """ def __init__(self, graph): """The algorithm initialization.""" self.graph = graph if not self._is_eulerian(): raise ValueError("the graph is not eulerian") self.eulerian_cycle = list() self._graph_copy = self.graph.copy() self._stack = LifoQueue() import sys recursionlimit = sys.getrecursionlimit() sys.setrecursionlimit(max(self.graph.v() * 2, recursionlimit)) def run(self, source=None): """Executable pseudocode.""" if source is None: # get first random node source = self.graph.iternodes().next() self._visit(source) while not self._stack.empty(): self.eulerian_cycle.append(self._stack.get()) #del self._stack #del self._graph_copy def _visit(self, source): """Visiting node.""" while self._graph_copy.outdegree(source) > 0: edge = self._graph_copy.iteroutedges(source).next() self._graph_copy.del_edge(edge) self._visit(edge.target) self._stack.put(source) def _is_eulerian(self): """Test if the graph is eulerian.""" if self.graph.is_directed(): # We assume that the graph is strongly connected. for node in self.graph.iternodes(): if self.graph.indegree(node) != self.graph.outdegree(node): return False else: # We assume that the graph is connected for node in self.graph.iternodes(): if self.graph.degree(node) % 2 == 1: return False return True
def __init__(self, canvas): self.providers = { 'Bing': self.bing_setup, 'ArcGIS': self.arcgis_setup, 'MapQuest': self.mq_setup } self.canvas = canvas self.imageryprovider = None self.provider_base = None self.provider_url = None self.provider_logo = None # (filename, width, height) self.provider_levelmin = self.provider_levelmax = 0 self.placementcache = { } # previously created placements (or None if image couldn't be loaded), indexed by quadkey. # placement may not be laid out if image is still being fetched. self.tile = (0, 999) # X-Plane 1x1degree tile - [lat,lon] of SW self.loc = None self.dist = 0 self.filecache = Filecache() # Setup a pool of worker threads self.workers = [] self.q = LifoQueue() for i in range(Imagery.connections): t = threading.Thread(target=self.worker) t.daemon = True # this doesn't appear to work for threads blocked on Queue t.start() self.workers.append(t)
class ConnectionPool(object): def __init__( self, connection_class=Connection, max_connections=1024, timeout=5, **connection_kwargs): self.connection_class = connection_class self.max_connections = max_connections self.connection_kwargs = connection_kwargs self.timeout = timeout self.connections = [] self.reset() def ensure_safe(self): if self.pid != os.getpid(): with self.ensure_lock: # lock for concurrent threadings if self.pid == os.getpid(): # double check return self.disconnect() self.reset() def get_connection(self): self.ensure_safe() try: connection = self.queue.get(block=True, timeout=self.timeout) except Empty: raise ConnectionError('connection pool is full') if not connection: connection = self.make_connection() return connection def make_connection(self): connection = self.connection_class(**self.connection_kwargs) self.connections.append(connection) return connection def release(self, connection): self.ensure_safe() if connection.pid != self.pid: return try: self.queue.put_nowait(connection) except Full: pass def reset(self): self.pid = os.getpid() self.ensure_lock = threading.Lock() self.disconnect() # LifoQueue make use of released connections self.queue = LifoQueue(self.max_connections) self.connections = [] while True: try: self.queue.put_nowait(None) except Full: break def disconnect(self): for connection in self.connections: connection.disconnect()
def __init__(self, *args): super(B2BucketThreaded, self).__init__( *args) num_threads=50 self.queue = LifoQueue(num_threads*2) self.file_locks = defaultdict(Lock) self.running = True self.threads = [] print "Thread ", for i in xrange(num_threads): t = threading.Thread(target=self._file_updater) t.start() self.threads.append(t) print ".", print self.pre_queue_lock = Lock() self.pre_queue_running = True self.pre_queue = LifoQueue(num_threads*2) self.pre_file_dict = {} self.pre_thread = threading.Thread(target=self._prepare_update) self.pre_thread.start()
class DummyMessageHandler(MessageHandler): # TODO(steffen): locking def __init__(self): MessageHandler.__init__(self) self._messages = LifoQueue() self._devices = [] def register(self, device): self._devices.append(device) def read_message(self): return self._messages.get() def write_message_from_device(self, message): self._messages.put(message) def write_message(self, message): for d in self._devices: d.handle_message(message) def has_messages(self): for d in self._devices: d.loop() return not self._messages.empty() def stop(self): pass
def __init__(self, car): self.actions = ('forward', 'stop') #, 'forwardLeft') #, 'backward') self.car = car self.cameras_size = 3 * 32 * 24 self.sensors_size = 3 self.state_size = self.cameras_size + self.sensors_size # 3 cameras + 3 sensors self.taken_actions = LifoQueue() self.reward_sensor = -10 self.reward_step = 0.3 self.reward_goal = 100 df = pd.read_csv("dataset.csv", usecols=[1, 2]) df = np.array(df) self.X = [] self.y = [] for data in df: target = data[1] images = self.read_img(data[0]) self.X.append(images) self.y.append(target) self.dataset_count = len(self.X) self.dataset_index = 0
def dfs_post_nonrec(tree, proc): ''' 非递归后根序遍历 :param tree: :param proc: :return: ''' stack = LifoQueue() node = tree while node is not None or not stack.empty(): while node is not None: # 注意,这一波遍历的规则是能左则左,若不能左往右也行 stack.put(node) if node.left is not None: node = node.left else: node = node.right node = stack.get() # 这个算法的特征之一,处理某个节点的时候,栈中保存着的是这个节点的所有前辈节点 proc(node.data) #### 从这里开始和中根序遍历不一样 #### if stack.empty(): # 若栈空,说明当前节点没有任何前辈节点,即根节点。根节点处理完成后直接跳出程序 break tmp = stack.get() # 由LifoQueue实现的栈没有peek或者top方法,自己模拟一下… stack.put(tmp) if node is tmp.left: # 判断刚才处理的是左子节点还是右子节点 node = tmp.right # 左子节点的话说明右子节点还没处理 else: node = None # 右子节点的话说明tmp节点对应子树已经处理完了
def __init__(self, *args): super(B2BucketThreaded, self).__init__(*args) num_threads = 50 self.queue = LifoQueue(num_threads * 2) self.file_locks = defaultdict(Lock) self.running = True self.threads = [] print "Thread ", for i in xrange(num_threads): t = threading.Thread(target=self._file_updater) t.start() self.threads.append(t) print ".", print self.pre_queue_lock = Lock() self.pre_queue_running = True self.pre_queue = LifoQueue(num_threads * 2) self.pre_file_dict = {} self.pre_thread = threading.Thread(target=self._prepare_update) self.pre_thread.start()
def __init__(self): self.last_timestamp = None # the variance used in the matrix Q self.process_variance = rospy.get_param( "/kalman_filter_node/process_variance", 100) # the variance used in the matrix R for the GPS measurements self.gps_variance = rospy.get_param("/kalman_filter_node/gps_variance", 0.1) # the variance used in the matrix R of the pressure altitude measurements self.pressure_variance = rospy.get_param( "/kalman_filter_node/pressure_variance", 0.1) # the variance used in the matrix R of the vertical velocity measurements self.velocity_variance = rospy.get_param( "/kalman_filter_node/velocity_variance", 0.1) self.queue = LifoQueue() self.sub_pa = rospy.Subscriber('/uav/sensors/pressure_altitude', AltitudeStamped, self.process_pressure_altitude, queue_size=1) self.sub_gps_a = rospy.Subscriber('/uav/sensors/gps_altitude', AltitudeStamped, self.process_gps_altitude, queue_size=1) self.sub_vel = rospy.Subscriber('/uav/sensors/velocity', TwistStamped, self.process_velocity, queue_size=1) self.pub = rospy.Publisher('/uav/sensors/kalman_filter_altitude', AltitudeStamped, queue_size=1) self.mainloop()
def _put_conn(self, host, port, sock): u""" 将连接添加回连接池 会检查连接状态,不正常的连接会被抛弃。 """ if hasattr(self.sock_mod, "get_display_name"): sock_name = self.sock_mod.get_display_name() else: sock_name = None sock_info = 'sock_mod:%s host:%s port:%s' % (sock_name, host, port) if sock: if is_connection_dropped(sock): logging.debug(u'已关闭连接无法添加回连接池。%s' % sock_info) try: sock.close() except: pass else: with self.lock: site_connes = self.site_dict.get(u'%s:%s' % (host, port), None) if site_connes is None: site_connes = LifoQueue(self.max_site_conn) try: site_connes.put(sock) logging.debug(u'添加连接回连接池。 %s' % sock_info) except Full: logging.debug(u'连接池满. %s' % sock_info) try: sock.close() except: pass return self.site_dict[u'%s:%s' % (host, port)] = site_connes
class HierholzerWithEdges: """Finding an Eulerian cycle in a multigraph. Attributes ---------- graph : input graph eulerian_cycle : list of edges (length |E|) _graph_copy : graph, private _stack : LIFO queue, private Notes ----- Based on the description from: https://en.wikipedia.org/wiki/Eulerian_path """ def __init__(self, graph): """The algorithm initialization.""" self.graph = graph if not self._is_eulerian(): raise ValueError("the graph is not eulerian") self.eulerian_cycle = list() self._graph_copy = self.graph.copy() self._stack = LifoQueue() def run(self, source=None): """Executable pseudocode.""" if source is None: # get first random node source = self.graph.iternodes().next() while True: if self._graph_copy.outdegree(source) > 0: edge = self._graph_copy.iteroutedges(source).next() self._stack.put(edge) self._graph_copy.del_edge(edge) source = edge.target else: edge = self._stack.get() source = edge.source self.eulerian_cycle.append(edge) if self._stack.empty(): break self.eulerian_cycle.reverse() #del self._stack #del self._graph_copy def _is_eulerian(self): """Test if the graph is eulerian.""" if self.graph.is_directed(): # We assume that the graph is strongly connected. for node in self.graph.iternodes(): if self.graph.indegree(node) != self.graph.outdegree(node): return False else: # We assume that the graph is connected. for node in self.graph.iternodes(): if self.graph.degree(node) % 2 == 1: return False return True
class EulerianCycleDFS: """Finding an Eulerian cycle in a multigraph, complexity O(E). Attributes ---------- graph : input graph eulerian_cycle : list of nodes (length |E|+1) _graph_copy : graph, private _stack : LIFO queue, private Notes ----- Based on the description from: http://eduinf.waw.pl./inf/alg/001_search/0135.php """ def __init__(self, graph): """The algorithm initialization.""" self.graph = graph if not self._is_eulerian(): raise ValueError("the graph is not eulerian") self.eulerian_cycle = list() self._graph_copy = self.graph.copy() self._stack = LifoQueue() import sys recursionlimit = sys.getrecursionlimit() sys.setrecursionlimit(max(self.graph.v()**2, recursionlimit)) def run(self, source=None): """Executable pseudocode.""" if source is None: # get first random node source = next(self.graph.iternodes()) self._visit(source) while not self._stack.empty(): self.eulerian_cycle.append(self._stack.get()) #del self._stack #del self._graph_copy def _visit(self, source): """Visiting node.""" while self._graph_copy.outdegree(source) > 0: edge = next(self._graph_copy.iteroutedges(source)) self._graph_copy.del_edge(edge) self._visit(edge.target) self._stack.put(source) def _is_eulerian(self): """Test if the graph is eulerian.""" if self.graph.is_directed(): # We assume that the graph is strongly connected. for node in self.graph.iternodes(): if self.graph.indegree(node) != self.graph.outdegree(node): return False else: # We assume that the graph is connected for node in self.graph.iternodes(): if self.graph.degree(node) % 2 == 1: return False return True
def __init__(self, graph): """The algorithm initialization.""" self.graph = graph if not self._is_eulerian(): raise ValueError("the graph is not eulerian") self.eulerian_cycle = list() self._graph_copy = self.graph.copy() self._stack = LifoQueue()
def lifo_queue_usage(): from Queue import LifoQueue lifo_queue = LifoQueue() lifo_queue.put(1) lifo_queue.put(2) print lifo_queue.get() print lifo_queue.get()
def __init__(self, url): # Direct class call `threading.Thread` instead of `super()` for python2 capability threading.Thread.__init__(self) self.lv_url = url self._lilo_head_pool = LifoQueue() self._lilo_jpeg_pool = LifoQueue() self.header = None self.frameinfo = []
def __init__(self): # Queue holding the last speech utterance self._speak_queue = LifoQueue(1) self._session = Session(profile_name="mylespolly") self._polly = self._session.client("polly", region_name="eu-west-1") self._thread = Thread(target=self.run, args=()) self._thread.daemon = True self._thread.start()
def __init__(self, name, topic, msgtype, verbose=False): self.topic = topic self.msgtype = msgtype self.name = name self.bridge = CvBridge() self.convertor = self.convertor_query() self.verbose = verbose self.subthread = None self.ros_image = LifoQueue(maxsize=5)
def __init__(self, parent): super(CoverDelegate, self).__init__(parent) self._animated_size = 1.0 self.animation = QPropertyAnimation(self, 'animated_size', self) self.animation.setEasingCurve(QEasingCurve.OutInCirc) self.animation.setDuration(500) self.set_dimensions() self.cover_cache = CoverCache(limit=gprefs['cover_grid_cache_size']) self.render_queue = LifoQueue() self.animating = None self.highlight_color = QColor(Qt.white)
def __init__(self, parent): QObject.__init__(self, parent) self.count = 0 self.last_saved = -1 self.requests = LifoQueue() t = Thread(name='save-thread', target=self.run) t.daemon = True t.start() self.status_widget = w = SaveWidget(parent) self.start_save.connect(w.start, type=Qt.QueuedConnection) self.save_done.connect(w.stop, type=Qt.QueuedConnection)
def __init__(self, conn, addr, s_container): Thread.__init__(self) self.conn = conn self.conn.settimeout(1.0) self.addr = addr self.scenarios = LifoQueue() self.current_scenario = None self.stop = False self.scenario_stop = False self.buffer = array("B") self.packet_len = 0
def __init__(self, graph): """The algorithm initialization.""" self.graph = graph if not self._is_eulerian(): raise ValueError("the graph is not eulerian") self.eulerian_cycle = list() self._graph_copy = self.graph.copy() self._stack = LifoQueue() import sys recursionlimit = sys.getrecursionlimit() sys.setrecursionlimit(max(self.graph.v()**2, recursionlimit))
class stack(): def __init__(self): self.s = LifoQueue() def push(self, x): self.s.put(x) def pop(self): return self.s.get() def empty(self): return self.s.empty()
def reset(self): self.pid = os.getpid() self.ensure_lock = threading.Lock() self.disconnect() # LifoQueue make use of released connections self.queue = LifoQueue(self.max_connections) self.connections = [] while True: try: self.queue.put_nowait(None) except Full: break
def dfSearch(start, actions, goalTest, depthLimit=False): """Depth-First Search""" queue = LifoQueue() queue.put(start) while True: if queue.empty(): return node node = queue.get() if goalTest(node): return node if (node.depth <= depthLimit) or (depthLimit is False): queue = node.expand(queue, actions)
def __init__(self, comake): self.comake = comake self.root = os.getenv('COMAKEPATH') if not path.isdir(self.root): makedirs(self.root) self.queue = Queue() self.stack = LifoQueue() self.dep_set = set() self.thread = None self.stop = False self.work_num = 4 self.dep_version = {}
class NodeLIFOQueue(object): def __init__(self): self.nodes = LifoQueue() self.priorities = [] def append(self, node, prioriy=1.0): self.nodes.put(node) self.priorities.append(prioriy) def sample(self): return self.nodes.get() def __len__(self): return self.nodes.qsize()
def _visit(self, node, pre_action=None, post_action=None): """Explore the connected component,""" self.time = self.time + 1 self.dd[node] = self.time self.color[node] = "GREY" Q = LifoQueue() Q.put(node) # node is GREY if pre_action: # when Q.put pre_action(node) while not Q.empty(): source = Q.get() # GREY node is processed for edge in self.graph.iteroutedges(source): if self.color[edge.target] == "WHITE": self.parent[edge.target] = source self.dag.add_edge(edge) self.time = self.time + 1 self.dd[edge.target] = self.time self.color[edge.target] = "GREY" Q.put(edge.target) # target is GREY if pre_action: # when Q.put pre_action(edge.target) self.time = self.time + 1 self.ff[source] = self.time self.color[source] = "BLACK" if post_action: # source became BLACK post_action(source)
def __init__(self, tiling, stackedImageSources, cache_size=100, request_queue_size=100000, n_threads=2, layerIdChange_means_dirty=False, parent=None): QObject.__init__(self, parent=parent) # Used for thread debug names self._serial_number = TileProvider._instance_count TileProvider._instance_count += 1 TileProvider._global_instance_list.append(weakref.ref(self)) self.tiling = tiling self.axesSwapped = False self._sims = stackedImageSources self._cache_size = cache_size self._request_queue_size = request_queue_size self._n_threads = n_threads self._layerIdChange_means_dirty = layerIdChange_means_dirty self._current_stack_id = self._sims.stackId self._cache = _TilesCache(self._current_stack_id, self._sims, maxstacks=self._cache_size) self._dirtyLayerQueue = LifoQueue(self._request_queue_size) self._prefetchQueue = Queue(self._request_queue_size) self._sims.layerDirty.connect(self._onLayerDirty) self._sims.visibleChanged.connect(self._onVisibleChanged) self._sims.opacityChanged.connect(self._onOpacityChanged) self._sims.sizeChanged.connect(self._onSizeChanged) self._sims.orderChanged.connect(self._onOrderChanged) self._sims.stackIdChanged.connect(self._onStackIdChanged) if self._layerIdChange_means_dirty: self._sims.layerIdChanged.connect(self._onLayerIdChanged) self._keepRendering = True self._dirtyLayerThreads = [ Thread(target=self._dirtyLayersWorker, name="TileProvider-{}-Worker-{}".format( self._serial_number, i)) for i in range(self._n_threads) ] for thread in self._dirtyLayerThreads: thread.daemon = True [thread.start() for thread in self._dirtyLayerThreads]
def getPath(startingPrime, finalPrime): # print(type(startingPrime)) # print("starting Prime: " + str(startingPrime)) # print(type(finalPrime)) # print("final Prime: " + str(finalPrime)) # your code here #depth limit is 5 #declare stack closedList.clear() stack = LifoQueue() #push <startingPrime (currentPrime), 0 (depth)> into the stack stack.put((startingPrime, 0)) outputString = "" #while stack is not empty while (not stack.empty()): #pop a from stack a = stack.get() #if a.currentPrime == finalPrime if (a[0] == finalPrime): break #else if a.depth >= 5 elif (a[1] >= 5): continue #find all neighbor of currentPrime neighbor = getPossibleActions(a[0]) for i in range(0, len(neighbor)): #set the parent of the neighbor to currentPrime closedList[str(neighbor[i])] = a[0] #push all neighbor as <neighbor,a.depth + 1> into the stack stack.put((neighbor[i], a[1] + 1)) #if(currentPRime != finalPrime) if (a[0] != finalPrime): #unsolvable outputString = 'UNSOLVABLE' else: current = a[0] outputString = "" outputString = str(current) + " " + outputString while (current != startingPrime): current = closedList[str(current)] outputString = str(current) + " " + outputString # outputString = startingPrime + " " + outputString # file = open('output.txt','w') # print >> file,outputString # file.close() sys.stdout.write(outputString + "\n") return
def _find_path_dfs(self): """Finding augmenting paths in the residual network.""" parent = dict((node, None) for node in self.residual.iternodes()) # Capacity of found path to node. capacity = {self.source: float("inf")} Q = LifoQueue() Q.put(self.source) while not Q.empty(): node = Q.get() for edge in self.residual.iteroutedges(node): cap = edge.weight - self.flow[edge.source][edge.target] if cap > 0 and parent[edge.target] is None: parent[edge.target] = edge.source capacity[edge.target] = min(capacity[edge.source], cap) if edge.target != self.sink: Q.put(edge.target) else: # Backtrack search and write flow. target = self.sink while target != self.source: node = parent[target] self.flow[node][target] += capacity[self.sink] self.flow[target][node] -= capacity[self.sink] target = node return capacity[self.sink] return 0
class BSTIterator(object): """ the smallest value of current node is at leftest of current node, in order to reach smallest value, we have to search to leftest, however after return smallest value, we have to return to its father node, since there is no pointer to a node's father, we have to cache a node's father node when searching, this is why stack is used. when a node is pop from stack, if the node have a right node the next smallest node is at right sub tree, the right node and it left node, and left node's left node should be put to stack and waiting for pop """ def __init__(self, root): """ :type root: TreeNode """ self.stack = LifoQueue() self.push(root) def next(self): """ @return the next smallest number :rtype: int """ if not self.stack.empty(): node = self.stack.get() # every time to pop a node from stack, # push right node and its leftest node to stack self.push(node.right) return node.val def hasNext(self): """ @return whether we have a next smallest number :rtype: bool """ if not self.stack.empty(): return True return False def push(self, node): """ push node and its left side node to stack recursively :param node: :return: """ if node is None: return self.stack.put(node) self.push(node.left)
def __init__(self, parent=None): # if current_processer: # raise Exception() self.children = [] self.parent = parent if parent: parent.children.append(self) self.callStack = LifoQueue() self.callDepth = 0 self.env = Globals.Globals self.ast = None self.stackPointer = 0 self.cenv = None self.initialCallDepth = 0 self.shouldAbort = None
def __init__(self, parent, notify=None): QObject.__init__(self, parent) self.count = 0 self.last_saved = -1 self.requests = LifoQueue() self.notify_requests = LifoQueue() self.notify_data = notify t = Thread(name='save-thread', target=self.run) t.daemon = True t.start() t = Thread(name='notify-thread', target=self.notify_calibre) t.daemon = True t.start() self.status_widget = w = SaveWidget(parent) self.start_save.connect(w.start, type=Qt.QueuedConnection) self.save_done.connect(w.stop, type=Qt.QueuedConnection)
def __init__(self, stream_func, catchup = True, last_known_block_ref=None, block_cache = None, event_map_fn = None, max_retry=20): if block_cache is None: block_cache = get_block_cache() self.max_retry = max_retry self.cache = block_cache self.stream_func = stream_func self.block_ref_queue = Queue() self.incoming_event_queue = Queue() self.block_replay_stack = LifoQueue() self.catchup_begin = threading.Event() self.catchup_complete = threading.Event() self.cancel_flag = threading.Event() self.should_catchup = catchup self.last_known_block_ref = ref_base58(last_known_block_ref) if event_map_fn is None: self.event_map_fn = lambda x: x else: self.event_map_fn = event_map_fn self.catchup_thread = threading.Thread( name='blockchain-catchup', target=self._perform_catchup) self.incoming_event_thread = threading.Thread( name='journal-stream-listener', target=self._receive_incoming_events) self._event_iterator = self._event_stream()
def __init__(self, max_connections=10, block_timeout=5, **kwds): self.max_connections = max_connections self.block_timeout = block_timeout # kwds is being sent directly to dbapi, so we pop self._pool_id = kwds.pop('pool_id') self._logobj = kwds.pop('logobj') self.kwds = kwds # make sure max_connections is valid is_valid = isinstance(max_connections, int) and \ max_connections > 0 if not is_valid: raise ValueError('max_connections must be a positive int') # if process id is changed, we will close all connections, # and reinstantiate this object self._pid = os.getpid() # this is where we define the pool, and fill it with None values self._pool = LifoQueue(max_connections) while True: try: self._pool.put_nowait(None) except Full: break # open connections self._connections = []
def __init__(self, download_folder): self.download_folder = download_folder self.log = logging.getLogger('Ydl') self.processes = LifoQueue() self.watcher = threading.Thread(target=self._cleanup, args=(self.processes, self.log)) self.log.info('Starting download watcher process') self.watcher.start()
def __init__(self, canvas): self.providers={'Bing': self.bing_setup, 'ArcGIS': self.arcgis_setup, 'MapQuest': self.mq_setup } self.canvas=canvas self.imageryprovider=None self.provider_base=None self.provider_url=None self.provider_logo=None # (filename, width, height) self.provider_levelmin=self.provider_levelmax=0 self.placementcache={} # previously created placements (or None if image couldn't be loaded), indexed by quadkey. # placement may not be laid out if image is still being fetched. self.tile=(0,999) # X-Plane 1x1degree tile - [lat,lon] of SW self.loc=None self.dist=0 self.filecache=Filecache() # Setup a pool of worker threads self.workers=[] self.q=LifoQueue() for i in range(Imagery.connections): t=threading.Thread(target=self.worker) t.daemon=True # this doesn't appear to work for threads blocked on Queue t.start() self.workers.append(t)
def pushGoals(self,mapNode,start,markerArray,isreverted,isPathOnService): # x=round(int(target['x'])/float(self.RESOLUTION*0.5),0) # y=round(int(target['y'])/float(self.RESOLUTION*0.5),0) revert=[] x=start['x'] y=start['y'] # goalQueue=LifoQueue() goalQueue=Queue() goalLifo=LifoQueue() goalQueue.put(self.createGoal(x,y)) try: prev=mapNode[str(int(x))+'_'+str(int(y))] # FIXME TO CHECK NONE VALUE while prev != None: # x=round(int(prev.split('_')[0])/float(self.RESOLUTION*0.5),0) # y=round(int(prev.split('_')[1])/float(self.RESOLUTION*0.5),0) print 'GOAL -->'+prev x=int(prev.split('_')[0]) y=int(prev.split('_')[1]) currentgoal=self.createGoal(x,y) self.createGoalMarker(currentgoal,markerArray,x,y) rospy.sleep(0.01) goalQueue.put(currentgoal) prev=mapNode[str(x)+'_'+str(y)] except KeyError, e: print 'end reverse path'
class Ydl(object): CMD = "youtube-dl --no-playlist -o '%(title)s.%(ext)s' --audio-quality 0 --extract-audio --audio-format best \"{url}\"" def __init__(self, download_folder): self.download_folder = download_folder self.log = logging.getLogger('Ydl') self.processes = LifoQueue() self.watcher = threading.Thread(target=self._cleanup, args=(self.processes, self.log)) self.log.info('Starting download watcher process') self.watcher.start() @staticmethod def _cleanup(processes, log): while True: log.info('Download watcher tick...') url, p = processes.get() log.info("Waiting for finishing download of " + url) retcode = p.wait() if retcode != 0: log.error('Something went wrong when downloading %s, return code is %d' % (url, retcode)) log.info("Finished downloading " + url) def download(self, url): cmd = Ydl.CMD.format(url=url) args = shlex.split(cmd) self.log.info("Downloading " + url) folder = self.download_folder if not os.path.exists(folder): # noinspection PyBroadException try: os.mkdir(folder) except: self.log.error('Unable to create %s' % folder) return p = subprocess.Popen(args, cwd=folder) self.log.info("Started process of downloader for " + url) self.log.info("Pid == " + p.pid) self.processes.put((url, p)) def __del__(self): try: self.watcher.cancel() except Exception, ex: pass
class PooledEnqueue(object): def __init__(self, qdir, n=5, maxsize=1000*1000*1000, **qargs): maxsize = maxsize / n self.qdir = qdir self.write_executor = ThreadPoolExecutor(poolsize=1, queuesize=100) self.queues = [FileEnqueue(self.qdir, suffix=str(i), maxsize=maxsize, executor=self.write_executor, **qargs) for i in range(n)] self.avail = LifoQueue() for q in self.queues: self.avail.put(q) self.addedcount = 0 def get_status(self): qq = [q.get_status() for q in self.queues] r = dict( buffered=sum(s['buffered'] for s in qq), pending=sum(s['pending'] for s in qq), queues=qq) return r def _flush(self): for q in self.queues: q._flush() def close(self): for q in self.queues: q.close() self.write_executor.shutdown() def queue(self, curis): t0 = time.time() enq = self.avail.get() t = time.time() - t0 if t > 0.1: logging.warn('self.avail.get() %.4f', t) try: enq.queue(curis) self.addedcount += len(curis) finally: t0 = time.time() self.avail.put(enq) t = time.time() - t0 if t > 0.1: logging.warn('slow self.avail.put() %.4f', t)
def get_max_flow(directed_graph, source, sink): residual = {edge: edge.capacity for edges in directed_graph.values() for edge in edges} flow_paths = [] def flow_path(path): max_flow = float("inf") for edge in path: max_flow = min(max_flow, residual[edge]) for edge in path: residual[edge] -= max_flow flow_paths.append((max_flow, path)) bfs_queue = LifoQueue() bfs_queue.put([]) while not bfs_queue.empty(): path = bfs_queue.get() for edge in directed_graph[source if not path else path[-1].to_node]: if residual[edge] > 0: new_path = path[:] new_path.append(edge) if edge.to_node == sink: flow_path(new_path) else: bfs_queue.put(new_path) return flow_paths
def socket_queue(self): if self._socket_queue is None: self._socket_queue = LifoQueue() for i in xrange(self.size): socket = TSocket.TSocket(self.host, self.port) transport = TTransport.TFramedTransport(socket) self._socket_queue.put(transport) return self._socket_queue
def _init_index(self): self.index_set = set() self.index_q = LifoQueue() for page in self.pages.find({'indexed': True}): self.index_set.add(page['page_id']) for page in self.pages.find({'crawled':True, 'indexed': False}): self.index_q.put(page['page_id']) print('Added %d pages to index set' % len(self.index_set)) print('Added %d pages to index queue' % self.index_q.qsize())
class Player(Fighter): """A Player character, inherits from Fighter Returns: A player object Functions: update, calcNewPos Attributes: """ def __init__(self, name, imagelist, colour, screenwidth, screenheight, *groups): super(Player, self).__init__(name, imagelist, colour, screenwidth, screenheight, *groups) self.directionqueue = LifoQueue() self.directiondict = {"up": False, "down": False, "left": False, "right": False} self.hp = 10 def handlekeyevent(self, keyevent): """ Handle input and set direction or attacking based on rules :param keyevent: (dict) Keyed on 'action' (e.g. 'keydown') and 'key' (e.g. 'up', 'fire') :return: """ if keyevent["action"] == "keydown": if keyevent["key"] in self.directiondict: self.directiondict[keyevent["key"]] = True self.directionqueue.put(keyevent["key"]) self.direction = keyevent["key"] self.moving = True elif keyevent["key"] == "fire": self.attacking = True elif keyevent["action"] == "keyup": if keyevent["key"] in self.directiondict: self.directiondict[keyevent["key"]] = False elif keyevent["key"] == "fire": self.attacking = False if keyevent["key"] in self.directiondict and self.moving: if not self.directiondict[self.direction]: while not self.directionqueue.empty(): self.direction = self.directionqueue.get() if self.directiondict[self.direction]: break if self.directionqueue.empty(): self.moving = False for direction, active in self.directiondict.iteritems(): if active: self.direction = direction self.moving = True
class LiveviewStreamThread(threading.Thread): def __init__(self, url): # Direct class call `threading.Thread` instead of `super()` for python2 capability threading.Thread.__init__(self) self.lv_url = url self._lilo_head_pool = LifoQueue() self._lilo_jpeg_pool = LifoQueue() self.header = None self.frameinfo = [] def run(self): sess = urlopen(self.lv_url) while True: header = sess.read(8) ch = common_header(header) data = sess.read(128) payload = payload_header(data, payload_type=ch['payload_type']) if ch['payload_type'] == 1: data_img = sess.read(payload['jpeg_data_size']) assert len(data_img) == payload['jpeg_data_size'] self._lilo_head_pool.put(header) self._lilo_jpeg_pool.put(data_img) elif ch['payload_type'] == 2: self.frameinfo = [] for x in range(payload['frame_count']): data_img = sess.read(payload['frame_size']) self.frameinfo.append(payload_frameinfo(data_img)) sess.read(payload['padding_size']) def get_header(self): if not self.header: try: self.header = self._lilo_head_pool.get_nowait() except Exception as e: self.header = None return self.header def get_latest_view(self): # note this is a blocking call data_img = self._lilo_jpeg_pool.get() # retrive next header try: self.header = self._lilo_head_pool.get_nowait() except Exception as e: self.header = None return data_img def get_frameinfo(self): return self.frameinfo
class ViewManager(object): def __init__(self): self.main_frame = None self.current_view = None self.last_view_stack = LifoQueue() self.header = None self.footer = None def start(self): self._loop = urwid.MainLoop(self.main_frame, self.get_pallette(), unhandled_input=self.on_keypress) self._loop.run() def change_view(self, view): self.last_view_stack.put(self.current_view) self._update_view(view) def close_current_view(self): view = self.last_view_stack.get() self._update_view(view) def initialize_frame(self, view): self.header = urwid.AttrMap(urwid.Text("Press any key", wrap='clip'), 'header') self.footer = SearchBar() self.main_frame = urwid.Frame(view, self.header, self.footer.control, focus_part='body') def _update_view(self, view): self.current_view = view self.main_frame.contents['body'] = ( view, None ) self._loop.draw_screen() def on_keypress(self, input): self.current_view.on_keypress(input) def get_pallette(self): palette = [('header', 'black', 'dark green', 'standout'), ('footer', 'black', 'dark green', 'standout'), ('normal', 'white', 'black'), ('reveal focus', 'white', 'dark blue', 'standout'), ('filename', 'light blue', 'black'), ('diff', 'black', 'dark green', 'standout'), ('added', 'dark green', 'black'), ('deleted', 'dark red', 'black')] return palette
def iterative(path, path_data): stack=LifoQueue() while 1: if not (type(path_data) == dict and path_data): changes.append(self.store_one(path, path_data)) else: for node in path_data: node_path = path + '/' + node node_data = path_data[node] change = self.store_one(node_path, node_data) changes.append(change) if type(node_data) == type(dict()): stack.put([node_path, node_data]) if stack.qsize(): path,path_data=stack.get() continue; break;
def Plan(self, start_config, goal_config): start_time = time.time() if self.visualize and hasattr(self.planning_env, "InitializePlot"): self.planning_env.InitializePlot(goal_config) plan = [] # TODO: Here you will implement the breadth first planner # The return path should be a numpy array # of dimension k x n where k is the number of waypoints # and n is the dimension of the robots configuration space q = LifoQueue() start_id = self.planning_env.discrete_env.ConfigurationToNodeId(start_config) goal_id = self.planning_env.discrete_env.ConfigurationToNodeId(goal_config) found = False q.put(start_id) explored =[start_id] backtrack = {} backtrack[start_id] = None n= 0 while (q.qsize()>0) and not found: current = q.get() successors = self.planning_env.GetSuccessors(current) for successor in successors: if not successor in backtrack: n = n+1 q.put(successor) #explored.append(successor) backtrack[successor] = current if self.visualize: s = self.planning_env.discrete_env.NodeIdToConfiguration(successor) c = self.planning_env.discrete_env.NodeIdToConfiguration(current) self.planning_env.PlotEdge(c,s) if successor == goal_id: found = True break # Shortest Path path = [] path.append(self.planning_env.discrete_env.NodeIdToConfiguration(goal_id)) element = backtrack[goal_id] while element is not None: path.append(self.planning_env.discrete_env.NodeIdToConfiguration(element)) element = backtrack[element] plan = path[::-1] if self.visualize: for i in range(len(path) - 1): self.planning_env.PlotRedEdge(path[i],path[i+1]) print "number of nodes" print n print "time (in seconds):" print time.time()- start_time path_length = 0 for i in range(len(path) - 1): path_length = path_length + self.planning_env.ComputeDistance(self.planning_env.discrete_env.ConfigurationToNodeId(path[i]), self.planning_env.discrete_env.ConfigurationToNodeId(path[i+1])) print "path path_length" print path_length return plan
def __init__(self, parent): super(CoverDelegate, self).__init__(parent) self._animated_size = 1.0 self.animation = QPropertyAnimation(self, 'animated_size', self) self.animation.setEasingCurve(QEasingCurve.OutInCirc) self.animation.setDuration(500) self.set_dimensions() self.cover_cache = CoverCache() self.render_queue = LifoQueue() self.animating = None self.highlight_color = QColor(Qt.white)