def inToPost(expr): ''' takes as input an infix expression and outputs a post fix expression ''' result = [] stack = LifoQueue(maxsize=len(expr)) #loop iterates over every character in the expression for char in expr: # non variables are put on a stack where their precedence will determine the order of transfer onto the result queue if (Helper.isOp(char) or char == '!'): if (stack.empty() or Helper.peek(stack) == '('): stack.put(char) elif (Helper.precedence(Helper.peek(stack)) > Helper.precedence(char)): result.append(stack.get()) stack.put(char) elif (Helper.precedence( Helper.peek(stack)) == Helper.precedence(char)): result.append(char) else: stack.put(char) elif (char == '('): stack.put(char) # consecutively adding operators between operators on result elif (char == ')'): while (not Helper.peek(stack) == '('): result.append(stack.get()) stack.get() # Variables are directly put on result else: result.append(char) # remaining character on the stack are consecutively added to result while (not stack.empty()): result.append(stack.get()) return result
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节点对应子树已经处理完了
class camera_receiver(object): 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 callback(self, image): if self.ros_image.full(): #self.ros_image.get() self.ros_image.empty() self.ros_image.put(image) def __call__(self): def monitor(threadnam): # disable_signals could set to be true in the future #rospy.init_node(self.name,anonymous=True) rospy.Subscriber(self.topic, self.msgtype, self.callback) #monitor() #rospy.spin() self.subthread = thread.start_new_thread(monitor, (self.name, )) def convertor_query(self): if self.msgtype == Image: def bridge(image, **args): return self.bridge.imgmsg_to_cv2(image, **args) return bridge elif self.msgtype == CompressedImage: def bridge(image, **args): img_array_1d = np.fromstring(image.data, np.uint8) cv_img = cv2.imdecode(img_array_1d, 1) #cv2.CV_LOAD_IMAGE_COLOR) return cv_img return bridge else: print( "the msgtype for this receiver in only support sensor_msgs.msg.Image and sensor_msgs.msg.CompressedImage" ) self.__del__() def spit(self, **args): # desired_encoding="passthrough" if self.verbose: print('received image of type: "%s"' % ros_data.format) screen = self.convertor(self.ros_image.get(), **args) return screen def __del__(self): print("the camera_receiver {} was deleted".format(self.name))
def isValid(self, A): stack = LifoQueue() brackets = {'(': ')', '[': ']', '{': '}'} for x in A: if x in brackets.keys(): stack.put(x) else: if stack.empty(): return 0 if x != brackets[stack.get()]: return 0 if stack.empty(): return 1 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 RPN(): expression = raw_input("Please input your expression:\n") expression = expression.replace(' ', '') opStack = LifoQueue(100) result = list() i = 1 token = expression[0] while token != '#': if token == '(': opStack.put(token) elif token == ')': topToken = opStack.get() while topToken != '(': result.append(topToken) if not opStack.empty(): topToken = opStack.get() else: break elif token in '+-': topToken = opStack.get() while topToken != '(': result.append(topToken) if not opStack.empty(): topToken = opStack.get() else: break opStack.put(topToken) opStack.put(token) elif token in '*/': while not opStack.empty(): topToken = opStack.get() if topToken in '*/': result.append(topToken) else: opStack.put(topToken) opStack.put(token) else: tmp = 0 while token in '0123456789': tmp = tmp * 10 + int(token) token = expression[i] i += 1 result.append(tmp) i -= 1 token = expression[i] i += 1 while not opStack.empty(): result.append(opStack.get()) print result
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 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 _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 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
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 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
class MyQueue(): def __init__(self): self.stackNewest = LifoQueue() self.stackOldest = LifoQueue() def shiftStacks(self): if self.stackOldest.empty(): while not self.stackNewest.empty(): self.stackOldest.put(self.stackNewest.get()) def put(self, item): self.stackNewest.put(item) def get(self): self.shiftStacks() return self.stackOldest.get()
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
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
def pushGoals(self, mapNode, start, marker_container, 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, marker_container, x, y) # rospy.sleep(0.01) goalQueue.put(currentgoal) prev = mapNode[str(x) + '_' + str(y)] except KeyError as e: print('end reverse path') self.pub_marker.publish(marker_container) if (isreverted): while not goalQueue.empty(): goalLifo.put(goalQueue.get()) while not goalLifo.empty(): goalQueue.put(goalLifo.get()) if isPathOnService: ### TODO ### call here the local planner service (self.local_planner_service) ### goalQueue: queue of goal to acheive (Posestamped ros message) ### ### self.local_planner_service: service to call the local planner ( TODO need to be created on the ShortPathMng constructor) # # # # # TODO # # # # ### print('') else: while not goalQueue.empty(): self.pub_goal.publish(goalQueue.get()) rospy.sleep(2)
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 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 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 depthLimited(self, depth): leaves = LifoQueue() leaves.put(self.start) while True: if leaves.empty(): return None actual = leaves.get() if actual.goalState(): return actual elif actual.depth is not depth: succ = actual.succ() while not succ.empty(): leaves.put(succ.get())
def stackDFS(Graph, vroot): """ Depth First Search: stack version """ Stack=LifoQueue() Stack.put(vroot) while not Stack.empty(): iV=Stack.get() print ("Visit :", iV) Graph.setVisited(iV) for jV in Graph.VertexList: if Graph.Edges[iV,jV] and not Graph.Visited[jV]: Stack.put(jV)
def visit_in_order_iterative(self, current_node): stack = LifoQueue() done = False while done == False: if current_node: stack.put(current_node) current_node = current_node.left_child else: if stack.empty(): done = True else: current_node = stack.get() self.return_array.append(current.value) current = current_node.right_child
class Stack(object): """docstring for stack""" def __init__(self, maxsize): if maxsize > 65535: self.throw('StackOverflowError') self.queue = LifoQueue(maxsize) # 栈顶元素出栈,并将其返回 def pop(self): isEmpty = self.queue.empty() if isEmpty: self.throw('this stack is null ') else: return self.queue.get() # 推送一个元素至栈顶 def push(self, value): isFull = self.queue.full() if isFull: self.throw('this stack is full') else: self.queue.put(value) def throw(self, msg): raise BaseException(msg) # 获取queue的队列内容 def list(self): return self.queue.queue # 清空队列 def clear(self): isEmpty = self.queue.empty() if not isEmpty: self.queue.get() self.clear()
def cloneGraph(self, node): if not node: return None nodeCopy = UndirectedGraphNode(node.label) visited = {node: nodeCopy} stack = Stack() stack.put(node) while not stack.empty(): node = stack.get() for neighbor in node.neighbors: if neighbor not in visited: visited[neighbor] = UndirectedGraphNode(neighbor.label) stack.put(neighbor) visited[node].neighbors.append(visited[neighbor]) return nodeCopy
def __queue_speech_segment(self, speech_segment): if self.__ignore_sample: return self.__queue_lock.acquire(True) if self.__speech_segments.qsize() == self.__max_queue_size: temp_queue = LifoQueue() while not self.__speech_segments.empty(): temp_queue.put(self.__speech_segments.get()) # Discard the oldest data temp_queue.get() while not temp_queue.empty(): self.__speech_segments.put(temp_queue.get()) self.__speech_segments.put(speech_segment) self.__queue_lock.release()
def dfs_pre_nonrec(tree, proc): ''' 非递归前根序遍历 :param tree: :param proc: :return: ''' stack = LifoQueue() node = tree stack.put(node) while not stack.empty(): node = stack.get() proc(node.data) if node.right is not None: stack.put(node.right) if node.left is not None: stack.put(node.left)
def __init__(self,g,s): queue = LifoQueue() self.marked = {} self.edgeTo = {} self.s = s for v in range(1,g.vertices()+1): self.marked[v]=False self.edgeTo[v]=-1 self.marked[s] = True queue.put(s) while not queue.empty(): v = queue.get() for w in g.adj(v): if not self.marked[w]: queue.put(w) self.marked[w] = True self.edgeTo[w] = v
class BPSolver: BEST_INC_VAL = float("inf") INC_SOL = None def __init__(self): self._pending_nodes = LifoQueue() self._pending_nodes_best_bound = PriorityQueue() def add_node(self, lpbound, node): self._pending_nodes.put(node) self._pending_nodes_best_bound.put((lpbound, node)) def solve(self): # enumerate the initial columns en = ColEnumerator() init_col_list = en.enum(1) # create the root node root = Node(init_col_list, [], self) self._pending_nodes.put(root) self._pending_nodes_best_bound.put((1000, root)) iter_times = 0 while not (self._pending_nodes.empty() or self._pending_nodes_best_bound.empty()): # if iter_times % 2 == 0: print '# Pending nodes', self._pending_nodes.qsize() node_to_process = self._pending_nodes.get() # else: # _, node_to_process = self._pending_nodes_best_bound.get() node_to_process.process() iter_times += 1 used_col = [] for k, v in BPSolver.INC_SOL.iteritems(): if v > 0.001: print k, v used_col.append(k) print 'Final obj val', BPSolver.BEST_INC_VAL print 'Used vehicle', len(used_col)
def find_path(self): # use DFS """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: return capacity[self.sink], parent return 0, parent
def BFS(graph, src, sink, augPath): visited = [False] * len(graph) queue = LifoQueue() queue.put(src) visited[src] = True while not queue.empty(): node = queue.get() for ind, val in enumerate(graph[node]): if not visited[ind] and val > 0: queue.put(ind) visited[ind] = True augPath[ind] = node if visited[sink]: return True else: return False
def DLS(puzzle, solPuzzle, puzSol2, limit): global depth global numCreated global numExpanded global maxFringe q = LifoQueue() q.put((0, puzzle)) visited = list(puzzle) while True: if q.empty(): print "Could not find the solution in the limited search." sys.exit() cp = q.get() current = cp[1] depth = cp[0] if current == solPuzzle or current == puzSol2: return current if depth == limit: continue numExpanded += 1 right = moveRight(current) down = moveDown(current) left = moveLeft(current) up = moveUp(current) if up != None and up not in visited: q.put((depth + 1, up)) # visited.append(up) numCreated += 1 if left != None and left not in visited: q.put((depth + 1, left)) # visited.append(left) numCreated += 1 if down != None and down not in visited: q.put((depth + 1, down)) # visited.append(down) numCreated += 1 if right != None and right not in visited: q.put((depth + 1, right)) # visited.append(right) numCreated += 1 if q.qsize() > maxFringe: maxFringe = q.qsize()
def values(self): ''' 中根序遍历配合生成器,留出了一个顺序遍历接口 yield的时候不应该返回Assoc类对象。因为一旦把这个对象给了用户,你不确定用户会对其做什么修改, 如果修改了value可能会导致原二叉搜索树的崩坏 :return: ''' from Queue import LifoQueue as Stack stack = Stack() bt = self._root stack.put(bt) while not stack.empty(): while bt.left is not None: stack.put(bt) bt = bt.left yield bt.data.value bt = stack.get() yield bt.data.value bt = bt.right
class MenuAction(object): def __init__(self): self.undo_commands = LifoQueue() self.commands = defaultdict(Actions) def set_command(self, item, activate, deactivate): self.commands[item] = Actions(activate, deactivate) def activate(self, item): action = self.commands[item].activate action.execute() self.undo_commands.put(action) def deactivate(self, item): action = self.commands[item].deactivate action.execute() self.undo_commands.put(action) def undo(self): if not self.undo_commands.empty(): self.undo_commands.get().undo()
def kk(number_list, group_len=2): # Karmarkar-Karp heuristic if group_len != 2: sys.exit("unsupported group length: %s for KK algorithm!" % group_len) pairs = LifoQueue() group1, group2 = [], [] heap = [(-1 * i, i) for i in number_list] heapq.heapify(heap) while len(heap) > 1: i, labeli = heapq.heappop(heap) j, labelj = heapq.heappop(heap) pairs.put((labeli, labelj)) heapq.heappush(heap, (i - j, labeli)) group1.append(heapq.heappop(heap)[1]) while not pairs.empty(): pair = pairs.get() if pair[0] in group1: group2.append(pair[1]) elif pair[0] in group2: group1.append(pair[1]) return [group1, group2]
def validTree(self, n, edges): if edges == [] and n == 1: return True elif edges == []: return False visited = [-1 for i in range(0, n)] father = [-1 for i in range(0, n)] adjl = {i: [] for i in range(0, n)} q = LifoQueue() for edge in edges: i = max(edge) j = min(edge) adjl[i].append(j) adjl[j].append(i) q.put(0) visited[0] == 0 count = 0 while count < n: while q.empty() == False: u = q.get() #print u for i in adjl[u]: if visited[i] == 1 and father[u] != i: return False if visited[i] == -1: visited[i] = 0 father[i] = u q.put(i) visited[u] = 1 count += 1 try: next = visited.index(-1) print next except: return True visited[next] = 0 q.put(next) return True
def _searchp_with_letters(self, p, has_letters): indexes, letters = p.get_letters_at_positions() words = set() s = Stack() i = 0 s.put(self.layer[indexes[i]].children[letters[i]]) has_letters += -1 i += 1 # DFS while not s.empty(): node = s.get_nowait() logapp('DEBUG', str(node)) # need to know which level we at, so I can compare also the other indexes/letters and check for words if node.level == p.length: # get me those words son logapp('DEBUG', 'Adding words if they match: ' + str(node.words)) for word in node.words: if p.check(word): words.update([word]) elif node.lengths.has_key(p.length): if has_letters and node.level == indexes[i]: logapp('DEBUG', 'we are at next letter in pattern: ' + letters[i]) #are we at level of next letter? # put only the child that checks with the next letter # BUT only if it goes to a word of the right length if letters[i] in node.lengths[p.length]: s.put(node.children[letters[i]]) logapp('DEBUG', 'there is a path from here, adding child.') i += 1 has_letters += -1 else: # put all the childs that go to a word of the right length logapp('DEBUG', 'Adding all children.') for child in node.lengths[p.length]: s.put(node.children[child]) return words
def inspect_cass_log(config): cass_log_file = get_cass_log_file(config) if not cass_log_file or not os.path.exists(cass_log_file): return None reader = BackwardsFileReader(cass_log_file) lifo = LifoQueue() last_line = reader.readline() lifo.put(last_line) while not re.match('^ERROR', last_line): last_line = reader.readline() if re.match('^\t', last_line): lifo.put(last_line) if re.match('^ERROR', last_line): lifo.put(last_line) if not last_line: break ret_str = "" while not lifo.empty(): ret_str += lifo.get() return ret_str
def DFS(no): global tempo global listaAdjacencia global cores global distancias global oredenacao cores[no] = CINZA pilha = LifoQueue() pilha.put(no) tempo += 1 distancias[no][INICIO] = tempo while not pilha.empty(): no = pilha.get() pilha.put(no) adjacenteBranco = False for adjacente in listaAdjacencia[no]: if cores[adjacente] == BRANCO: adjacenteBranco = True pilha.put(adjacente) tempo += 1 distancias[adjacente][INICIO] = tempo cores[adjacente] = CINZA break if not adjacenteBranco: pilha.get() tempo += 1 distancias[no][FINAL] = tempo cores[no] = PRETO oredenacao.appendleft(no)
from Queue import LifoQueue n = int(raw_input()) for i in xrange(n): s = raw_input() fila = LifoQueue() resposta = 0 for e in s: if e == '<': fila.put(1) elif e == '>' and not fila.empty(): fila.get() resposta += 1 print resposta
class Solver(object): """ Solve CSP problems getting all solutions """ PHASES = ['INIT', 'SOLVING', 'FINISHED'] def __init__(self): """ Init method """ self.constraints = [] self.variables = {} self.public_solutions = [] self.qstate = LifoQueue() self.phase = 'INIT' self.e_mgr = EventManager() self.e_mgr.register_event('solver_on_init') self.e_mgr.register_event('solver_on_end') self.e_mgr.register_event('solver_on_solution') self.e_mgr.register_event('solver_on_backtrack') self.e_mgr.register_event('solver_before_set_value') self.e_mgr.register_event('solver_after_set_value') self.e_mgr.raise_event('solver_on_init') def _push_state(self, variable, value): """ Store in qstate the value of the variable it going to be set next and the state of the all domains """ domain_dump = {} for var in self.variables.itervalues(): # store a copy of the domain domain_dump[var.id] = var.domain[:] self.qstate.put((variable.id, value, domain_dump)) def _pop_state(self): """ Restore previous state """ return self.qstate.get(False) def _back_to_previous_state(self): """ Go back to a previous valid state removing invalid values. If qstate become empty (there is no valid state) return false else true. """ while not self.qstate.empty(): state = self._pop_state() id_var = state[0] value = state[1] domain_dump = state[2] # Restore domains for id, domain in domain_dump.iteritems(): self.variables[id]._restore(domain) # Delete invalid value try: self.variables[id_var].remove_from_domain([value]) return True except: pass return False def reg_variable(self, variable): """ Register a variable to the problem. """ self.variables[variable.id] = variable def _public_solution(self): """ Store in public_solutions a id_variable -> value hash """ solution = {} for id_var, variable in self.variables.iteritems(): solution[id_var] = variable.value self.public_solutions.append(solution) return solution def next_variable(self): """ Choose what variable is the best to be instancied. """ for variable in self.variables.values(): if not variable.instancied: return variable def value(self, variable): """ Choose the better value to the variable. """ return variable.min() def solve(self): """ Get all solutions to the problem. """ while self.iter_solve(): pass return self.public_solutions def iter_solve(self): """ Get all the solutions one by one using a backtracking strategy.""" if self.phase == 'INIT': self.phase = 'SOLVING' elif not self._back_to_previous_state(): # No more solutions self.e_mgr.raise_event('solver_on_end') self.phase = 'FINISHED' return None while not self.all_instancied(): try: # Heuristic var = self.next_variable() value = self.value(var) self.e_mgr.raise_event('solver_before_set_value') self._push_state(var, value) var.set(value) self.e_mgr.raise_event('solver_after_set_value') except BTException, bte: if not self._back_to_previous_state(): # No more solutions self.phase = 'FINISHED' self.e_mgr.raise_event('solver_on_end') return None else: self.e_mgr.raise_event('solver_on_backtrack') # Solution solution = self._public_solution() self.e_mgr.raise_event('solver_on_solution') return solution
from Queue import LifoQueue q = LifoQueue() for i in range(5): q.put(i) print "put: ", i while True: if q.empty(): break else: i = q.get(timeout = 1) print "get :", i
# now the worker threads are processing lets feed the fileQueue, this will block if the # rework file is larger than the queue. primeQueues(fileQueue, dirQueue) if args.debug: queueMsg("\"max\", \"file\", \"dir\", \"results\"") # lets just hang back and wait for the queues to empty print "If you need to pause this job, press Ctrl-C once" time.sleep(1) while not terminateThreads: if args.debug: queueMsg("\"%s\", \"%s\", \"%s\", \"%s\"\n"%(args.queueParams['max'], fileQueue.qsize(), dirQueue.qsize(), resultsQueue.qsize())) time.sleep(.1) if fileQueue.empty() and dirQueue.empty(): queueMsg("\"%s\", \"%s\", \"%s\", \"%s\"\n"%(args.queueParams['max'], fileQueue.qsize(), dirQueue.qsize(), resultsQueue.qsize())) print "waiting for directory queue to clear..." dirQueue.join() print "waiting for file queue to clear..." fileQueue.join() print "waiting for worker processes to complete..." terminateThreads = True print "waiting for results queue to clear..." resultsQueue.join() print "exporting statistics..." exportStats() print "closing files..." for file in fileHandles: fileHandles[file].close() print "cleaning up process files..."
class BlockchainFollower(object): 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 __iter__(self): return self def next(self): return next(self._event_iterator) def __enter__(self): return self def __exit__(self, *args): self.cancel() return False def start(self): self.catchup_thread.start() self.incoming_event_thread.start() def cancel(self): logger = get_logger(__name__) logger.info('BlockchainFollower canceled') self.cancel_flag.set() def _clear_queues(self): if self.cancel_flag.is_set(): return with self.block_ref_queue.mutex: self.block_ref_queue.queue.clear() with self.block_replay_stack.mutex: del self.block_replay_stack.queue[:] with self.incoming_event_queue.mutex: self.incoming_event_queue.queue.clear() def _perform_catchup(self): if not self.should_catchup: self.catchup_complete.set() return logger = get_logger(__name__) while True: if self.cancel_flag.is_set(): return # block until the event consumer thread tells us to start the # blockchain catchup process self.catchup_begin.wait() ref = self.block_ref_queue.get() if self.cancel_flag.is_set(): return if ref_base58(ref) == self.last_known_block_ref: logger.debug('hit last known block: {}'.format( self.last_known_block_ref )) self.catchup_complete.set() continue block = self.cache.get(ref) if block is None: logger.error('Could not get block with ref {}'.format(ref)) return self.block_replay_stack.put(ref) chain = chain_ref(block) if chain is None: logger.debug('Reached genesis block {}'.format(ref)) self.catchup_complete.set() continue self.block_ref_queue.put(chain) def _receive_incoming_events(self): def event_receive_worker(): # first clear out all the queues, and event state, # in case we're being called from the retry helper after a stream # interruption if self.should_catchup: self.catchup_complete.clear() self.catchup_begin.clear() self._clear_queues() first_event_received = False try: stream = self.stream_func() for event in stream: if self.cancel_flag.is_set(): stream.cancel() return if not first_event_received: first_event_received = True block_ref = block_event_ref(event) if block_ref is None: self.catchup_complete.set() else: self.block_ref_queue.put(block_ref) self.catchup_begin.set() self.incoming_event_queue.put(event) except AbortionError as e: if self.cancel_flag.is_set() and e.code == StatusCode.CANCELLED: return else: raise with_retry(event_receive_worker, max_retry_attempts=self.max_retry) def _event_stream(self): logger = get_logger(__name__) while True: if self.cancel_flag.is_set(): return # wait until catchup process signals that it's complete # this will be immediate if catchup is not in progress if self.should_catchup: self.catchup_complete.wait() # get all values from the catchup queue and yield their entries while not self.block_replay_stack.empty(): if self.cancel_flag.is_set(): return block_ref = self.block_replay_stack.get() logger.debug('Replaying block: {}'.format(block_ref)) block = self.cache.get(block_ref) entries = block.get('entries', []) for e in entries: e = self.event_map_fn(block_event_to_rpc_event(e)) if e is not None: yield e self.last_known_block_ref = block_ref block_event = Transactor_pb2.JournalEvent() block_event.journalBlockEvent.reference = ref_base58(block_ref) block_event = self.event_map_fn(block_event) if block_event is not None: yield block_event if self.cancel_flag.is_set(): return # Try to pull an event off of the incoming event queue # If there's no event received within a second, loop back. try: e = self.incoming_event_queue.get(block=False, timeout=1) block_ref = block_event_ref(e) if block_ref is not None: self.last_known_block_ref = block_ref e = self.event_map_fn(e) if e is not None: yield e except QueueEmpty: pass
class Transceiver(object): def __init__(self, config={}, message_handler=None): """Set up a receiver which connects to the messaging hub. :param config: This is a dict in the form:: config = dict( incoming='tcp://localhost:15566', # default outgoing='tcp://localhost:15567', idle_timeout=1000, # milliseconds: ) """ self.log = logging.getLogger("evasion.messenger.endpoint.Transceiver") self.endpoint_uuid = str(uuid.uuid4()) self.exit_time = threading.Event() self.wait_for_exit = threading.Event() self.incoming = None # configured in main(). self.incoming_uri = config.get("incoming", 'tcp://localhost:15566') self.log.info("Recieving on <%s>" % self.incoming_uri) self.outgoing_uri = config.get("outgoing", 'tcp://localhost:15567') self.log.info("Sending on <%s>" % self.outgoing_uri) self.idle_timeout = int(config.get("idle_timeout", 2000)) self.log.info("Idle Timeout (ms): %d" % self.idle_timeout) self.message_handler = message_handler self.sync_message = frames.sync_message( "endpoint-%s" % self.endpoint_uuid ) # Queue up messages to be sent in the main message loop self._out_queue = LifoQueue() def main(self): """Running the main loop sending and receiving. This will keep running until stop() is called. This sets the exit flag causing clean up and shutdown. """ self.exitTime = False context = zmq.Context() incoming = context.socket(zmq.SUB) incoming.setsockopt(zmq.SUBSCRIBE, '') incoming.connect(self.incoming_uri) outgoing = context.socket(zmq.PUSH); outgoing.connect(self.outgoing_uri); def _shutdown(): try: incoming.close() except ZMQError: self.log.exception("main: error calling incoming.close()") try: outgoing.close() except ZMQError: self.log.exception("main: error calling outgoing.close()") try: context.term() except ZMQError: self.log.exception("main: error calling context.term()") try: poller = zmq.Poller() poller.register(incoming, zmq.POLLIN) while not self.exit_time.is_set(): try: events = poller.poll(self.idle_timeout) except ZMQError as e: # 4 = 'Interrupted system call' if e.errno == 4: self.log.info("main: exit time: %s" % e) break else: self.log.info("main: <%s>" % e) break except Exception: self.log.exception("main: fatal error while polling ") break else: if (events > 0): msg = incoming.recv_multipart() self.message_in(tuple(msg)) # Now recover and queued outgoing messages: if not self._out_queue.empty(): message = self._out_queue.get_nowait() if message: try: # send sync hub followed by message. The sync # will kick the hub into life if its just # started: outgoing.send_multipart(self.sync_message) outgoing.send_multipart(message) except ZMQError as e: # 4 = 'Interrupted system call' if e.errno == 4: self.log.info(( "main: sigint or other signal interrupt" ", exit time <%s>" ) % e) break else: self.log.info("main: <%s>" % e) break except Exception: self.log.exception("main: fatal error sending ") break finally: self._out_queue.task_done() finally: self.wait_for_exit.set() _shutdown() def start(self): """Set up zmq communication and start receiving messages from the hub. """ # coverage can't seem to get to this: def _main(notused): # pragma: no cover self.exit_time.clear() self.wait_for_exit.clear() self.main() thread.start_new(_main, (0,)) def stop(self, wait=2): """Stop receiving messages from the hub and clean up. :param wait: The time in seconds to wait before giving up on a clean shutdown. """ self.log.info("stop: shutting down messaging.") self.exit_time.set() self.wait_for_exit.wait(wait) self.log.info("stop: done.") def message_out(self, message): """This sends a message to the messagehub for dispatch to all connected endpoints. :param message: A tuple or list representing a multipart ZMQ message. If the message is not a tuple or list then MessageOutError will be raised. Note: The message is actually queued here so that the main loop will send it when its ready. :returns: None. """ if isinstance(message, list) or isinstance(message, tuple): self._out_queue.put(message) else: m = "The message must be a list or tuple instead of <%s>" % type( message ) raise MessageOutError(m) def message_in(self, message): """Called on receipt of an evasion frame to determine what to do. The message_handler set in the constructer will be called if one was set. If none was set then the message will be logged at the DEBUG level. :param message: A tuple or list representing a multipart ZMQ message. :returns: None. """ if self.message_handler: try: #self.log.debug("message_in: message <%s>" % str(message)) self.message_handler(message) except: self.log.exception("message_in: Error handling message - ") else: self.log.debug("message_in: message <%s>" % str(message))
class Imagery: # Number of simultaneous connections to imagery provider and concurrent placement layouts. # Limit to 1 to prevent worker threads slowing UI too much. connections=1 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) # Worker thread def worker(self): # Each thread gets its own tessellators in thread-local storage tls=threading.local() tls.tess=gluNewTess() gluTessNormal(tls.tess, 0, -1, 0) gluTessProperty(tls.tess, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_NEGATIVE) gluTessCallback(tls.tess, GLU_TESS_VERTEX_DATA, ElevationMeshBase.tessvertex) gluTessCallback(tls.tess, GLU_TESS_EDGE_FLAG, ElevationMeshBase.tessedge) tls.csgt=gluNewTess() gluTessNormal(tls.csgt, 0, -1, 0) gluTessProperty(tls.csgt, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ABS_GEQ_TWO) gluTessCallback(tls.csgt, GLU_TESS_VERTEX_DATA, ElevationMeshBase.tessvertex) gluTessCallback(tls.csgt, GLU_TESS_COMBINE, ElevationMeshBase.tesscombinetris) gluTessCallback(tls.csgt, GLU_TESS_EDGE_FLAG, ElevationMeshBase.tessedge) while True: (fn, args)=self.q.get() if not fn: exit() # Die! fn(tls, *args) self.q.task_done() def exit(self): # Closing down self.filecache.writedir() # kill workers for i in range(Imagery.connections): self.q.put((None, ())) # Top priority! Don't want to hold up program exit # wait for them for t in self.workers: t.join() def reset(self): # Called on reload or on new tile. Empty the cache and forget allocations since: # a) in the case of reload, textures have been dropped and so would need to be reloaded anyway; # b) try to limit cluttering up the VBO with allocations we may not need again; # c) images by straddle multiple tiles and these would need to be recalculated anyway. for placement in self.placementcache.itervalues(): if placement: placement.clearlayout() self.placementcache={} def goto(self, imageryprovider, loc, dist, screensize): if not imageryprovider: imageryprovider=None if imageryprovider!=self.imageryprovider: self.provider_base=None self.provider_url=None self.provider_logo=None self.imageryprovider=imageryprovider if self.imageryprovider not in self.providers: return self.q.put((self.providers[self.imageryprovider], ())) newtile=(int(floor(loc[0])),int(floor(loc[1]))) if not self.provider_url or self.tile!=newtile: # New tile - drop cache of Clutter for placement in self.placementcache.itervalues(): if placement: placement.clearlayout() self.placementcache={} self.tile=newtile self.loc=loc self.placements(dist, screensize) # Kick off any image loading # Return placements to be drawn. May allocate into vertexcache as a side effect. def placements(self, dist, screensize): level0mpp=2*pi*6378137/256 # metres per pixel at level 0 if screensize.width<=0 or not (int(self.loc[0]) or int(self.loc[1])) or not self.provider_url: return [] # Don't do anything on startup. Can't do anything without a valid provider. # layout assumes mesh loaded assert (int(floor(self.loc[0])),int(floor(self.loc[1])),prefs.options&Prefs.ELEVATION) in self.canvas.vertexcache.elevation, '%s %s' % ((int(floor(self.loc[0])),int(floor(self.loc[1])),prefs.options&Prefs.ELEVATION), self.canvas.vertexcache.elevation.keys()) # http://msdn.microsoft.com/en-us/library/bb259689.aspx # http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Resolution_and_Scale width=dist+dist # Width in m of screen (from glOrtho setup) ppm=screensize.width/width # Pixels on screen required by 1 metre, ignoring tilt level=min(int(round(log(ppm*level0mpp*cos(radians(self.loc[0])), 2))), self.provider_levelmax) # zoom level required levelmin=max(13, self.provider_levelmin) # arbitrary - tessellating out at higher levels takes too long level=max(level,levelmin+1) ntiles=2**level # number of tiles per axis at this level #mpp=cos(radians(self.loc[0]))*level0mpp/ntiles # actual resolution at this level #coverage=width/(256*mpp) # how many tiles to cover screen width - in practice varies between ~2.4 and ~4.8 (cx,cy)=self.latlon2xy(self.loc[0], self.loc[1], level) # centre tile #print self.loc, width, screensize.width, 1/ppm, ppm, level, ntiles, cx, cy if __debug__: print "Desire imagery level", level # We're using a Lifo queue, so as the user navigates the most important tiles are processed first. # Should remove from the queue those tiles the user is probably not going to see again, but that's difficult so we don't. # Display 6x6 tiles if available that cover the same area as 3x3 at the next higher level (this is to prevent weirdness when zooming in) cx=2*(cx/2) cy=2*(cy/2) placements=[] needed=set() # Placements at this level failed either cos imagery not available at this location/level or is pending layout fetch=[] seq=[(-2, 3), (-1, 3), (0, 3), (1, 3), (2, 3), (3, 3), (3, 2), (3, 1), (3, 0), (3, -1), (3, -2), (2, -2), (1, -2), (0, -2), (-1, -2), (-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), (-1, 2), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0), (2, -1), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (0, 0)] for i in range(len(seq)): (x,y)=seq[i] placement=self.getplacement(cx+x,cy+y,level, False) # Don't initiate fetch yet if placement and placement.islaidout(): placements.append(placement) else: needed.add(((cx+x)/2,(cy+y)/2)) if 0<=x<=1 and 0<=y<=1: fetch.append((cx+x,cy+y,level, True)) # schedule fetch of the centre 2x2 tiles # Go up and get 5x5 tiles around the centre tile - but only draw them if higher-res imagery not (yet) available. level-=1 cx/=2 cy/=2 fail2=True if self.q.empty(): # If the queue is empty then the first (and low importance) tile starts processing immediately. # So here we add the most important centre tile of 5x5 and ensure it starts processing. placement=self.getplacement(cx,cy,level, True) # Initiate fetch if placement: fail2=False # Some imagery may be available at this level if placement.islaidout() and (cx,cy) in needed: placements.insert(0,placement) # Insert at start so drawn under higher-level needed.remove((cx,cy)) while not self.q.empty(): time.sleep(0) # Allow worker thread to remove from queue # First initiate fetch of higher-level imagery of centre 2x2 for args in fetch: self.getplacement(*args) seq=[(1, -2), (0, -2), (-1, -2), (-2, -1), (-2, 0), (-2, 1), (-1, 2), (0, 2), (1, 2), (2, 1), (2, 0), (2, -1), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (0, 0)] # 5x5 with corners removed #, (0,3), (3,0), (0,-3), (-3,0)] for i in range(len(seq)): (x,y)=seq[i] placement=self.getplacement(cx+x,cy+y,level, True) # Initiate fetch if placement: fail2=False # Some imagery may be available at this level if placement.islaidout() and (abs(x)>1 or abs(y)>1 or (cx+x,cy+y) in needed): placements.insert(0,placement) # Insert at start so drawn under higher-level while fail2 and level>levelmin: # No imagery available at all at higher level. Go up and get 3x3 tiles around the centre tile. level-=1 cx/=2 cy/=2 seq=[(1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (0, 0)] for i in range(len(seq)): (x,y)=seq[i] placement=self.getplacement(cx+x,cy+y,level, True) if placement: fail2=False # Some imagery may be available at this level if placement.islaidout(): placements.insert(0,placement) # Insert at start so drawn under higher-level if __debug__: print "Actual imagery level", level return placements # Helper to return coordinates in a spiral from http://stackoverflow.com/questions/398299/looping-in-a-spiral def spiral(self, N, M): x = y = 0 dx, dy = 0, -1 retval=[] for dumb in xrange(N*M): if abs(x) == abs(y) and [dx,dy] != [1,0] or x>0 and y == 1-x: dx, dy = -dy, dx # corner, change direction if abs(x)>N/2 or abs(y)>M/2: # non-square dx, dy = -dy, dx # change direction x, y = -y+dx, x+dy # jump retval.append((x,y)) x, y = x+dx, y+dy return retval # Returns a laid-out placement if possible, or not laid-out if image is still loading, or None if not available. def getplacement(self,x,y,level,fetch): (name,url)=self.provider_url(x,y,level) if name in self.placementcache: # Already created placement=self.placementcache[name] elif fetch: # Make a new one. We could also do this if the image file is available, but don't since layout is expensive. (north,west)=self.xy2latlon(x,y,level) (south,east)=self.xy2latlon(x+1,y+1,level) placement=DrapedImage(name, 65535, [[Node([west,north,0,1]),Node([east,north,1,1]),Node([east,south,1,0]),Node([west,south,0,0])]]) placement.load(self.canvas.lookup, self.canvas.defs, self.canvas.vertexcache) self.placementcache[name]=placement # Initiate fetch of image and do layout. Prioritise more detail. self.q.put((self.initplacement, (placement,name,url))) else: placement=None # Load it if it's not loaded but is ready to be if placement and not placement.islaidout() and Polygon.islaidout(placement): try: if __debug__: clock=time.clock() filename=self.filecache.get(name) # downloaded image or None self.canvas.vertexcache.allocate_dynamic(placement, True) # couldn't do this in thread context placement.definition.texture=self.canvas.vertexcache.texcache.get(filename, wrap=False, downsample=False, fixsize=True) if __debug__: print "%6.3f time in imagery load for %s" % (time.clock()-clock, placement.name) assert placement.islaidout() except: if __debug__: print_exc() # Some failure - perhaps corrupted image? placement.clearlayout() placement=None self.placementcache[name]=None return placement def latlon2xy(self, lat, lon, level): ntiles=2**level # number of tiles per axis at this level sinlat=sin(radians(lat)) x = int((lon+180) * ntiles/360.0) y = int( (0.5 - (log((1+sinlat)/(1-sinlat)) / fourpi)) * ntiles) return (x,y) def xy2latlon(self, x, y, level): ntiles=float(2**level) # number of tiles per axis at this level lat = 90 - 360 * atan(exp((y/ntiles-0.5)*2*pi)) / pi lon = x*360.0/ntiles - 180 return (lat,lon) def bing_quadkey(self, x, y, level): # http://msdn.microsoft.com/en-us/library/bb259689.aspx i=level quadkey='' while i>0: digit=0 mask = 1 << (i-1) if (x & mask): digit+=1 if (y & mask): digit+=2 quadkey+=('%d' % digit) i-=1 url=self.provider_base % quadkey name=basename(url).split('?')[0] return (name,url) # Called in worker thread - don't do anything fancy since main body of code is not thread-safe def bing_setup(self, tls): try: key='AhATjCXv4Sb-i_YKsa_8lF4DtHwVoicFxl0Stc9QiXZNywFbI2rajKZCsLFIMOX2' h=urlopen('http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?key=%s' % key) d=h.read() h.close() info=json_decode(d) # http://msdn.microsoft.com/en-us/library/ff701707.aspx if 'authenticationResultCode' not in info or info['authenticationResultCode']!='ValidCredentials' or 'statusCode' not in info or info['statusCode']!=200: return res=info['resourceSets'][0]['resources'][0] # http://msdn.microsoft.com/en-us/library/ff701712.aspx self.provider_levelmin=int(res['zoomMin']) self.provider_levelmax=int(res['zoomMax']) self.provider_base=res['imageUrl'].replace('{subdomain}',res['imageUrlSubdomains'][-1]).replace('{culture}','en').replace('{quadkey}','%s') + '&key=' + key # was random.choice(res['imageUrlSubdomains']) but always picking the same server seems to give better caching self.provider_url=self.bing_quadkey if info['brandLogoUri']: filename=self.filecache.fetch(basename(info['brandLogoUri']), info['brandLogoUri']) if filename: image = PIL.Image.open(filename) # yuck. but at least open is lazy self.provider_logo=(filename,image.size[0],image.size[1]) except: if __debug__: print_exc() self.canvas.Refresh() # Might have been waiting on this to get imagery def arcgis_url(self, x, y, level): url=self.provider_base % ("%d/%d/%d" % (level, y, x)) name="arcgis_%d_%d_%d.jpeg" % (level, y, x) return (name,url) # Called in worker thread - don't do anything fancy since main body of code is not thread-safe def arcgis_setup(self, tls): try: h=urlopen('http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer?f=json') d=h.read() h.close() info=json_decode(d) # http://resources.arcgis.com/en/help/rest/apiref/index.html # http://resources.arcgis.com/en/help/rest/apiref/mapserver.html self.provider_levelmin=min([lod['level'] for lod in info['tileInfo']['lods']]) self.provider_levelmax=max([lod['level'] for lod in info['tileInfo']['lods']]) self.provider_base='http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/%s' self.provider_url=self.arcgis_url filename=self.filecache.fetch('logo-med.png', 'http://serverapi.arcgisonline.com/jsapi/arcgis/2.8/images/map/logo-med.png') if filename: image = PIL.Image.open(filename) # yuck. but at least open is lazy self.provider_logo=(filename,image.size[0],image.size[1]) except: if __debug__: print_exc() self.canvas.Refresh() # Might have been waiting on this to get imagery def mq_url(self, x, y, level): url=self.provider_base % ("%d/%d/%d.jpg" % (level, x, y)) name="mq_%d_%d_%d.jpg" % (level, x, y) return (name,url) # Called in worker thread - don't do anything fancy since main body of code is not thread-safe def mq_setup(self, tls): # http://developer.mapquest.com/web/products/open/map try: self.provider_levelmin=0 self.provider_levelmax=18 self.provider_base='http://otile1.mqcdn.com/tiles/1.0.0/map/%s' self.provider_url=self.mq_url filename=self.filecache.fetch('questy.png', 'http://open.mapquest.com/cdn/toolkit/lite/images/questy.png') if filename: image = PIL.Image.open(filename) # yuck. but at least open is lazy self.provider_logo=(filename,image.size[0],image.size[1]) except: if __debug__: print_exc() self.canvas.Refresh() # Might have been waiting on this to get imagery # Called in worker thread - fetches image and does placement layout (which uses it's own tessellator and so is thread-safe). # don't do anything fancy since main body of code is not thread-safe def initplacement(self, tls, placement, name, url): filename=self.filecache.fetch(name, url) if not filename: # Couldn't fetch image - remove corresponding placement self.placementcache[name]=None else: if __debug__: clock=time.clock() placement.layout(self.tile, tls=tls) if not placement.dynamic_data.size: if __debug__: print "DrapedImage layout failed for %s - no tris" % placement.name self.placementcache[name]=None elif __debug__: print "%6.3f time in imagery layout for %s" % (time.clock()-clock, placement.name) self.canvas.Refresh() # Probably wanting to display this - corresponding placement will be loaded and laid out during OnPaint
class mainframe(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.filenm=None self.streamnm = None self.pack(fill=BOTH, expand=1) self.parent = parent self.initplayer() self.player_process = None self.seekthread = None self.fstate = False self.paused = True self.trackmouse = True self.stdout_thread = None self.stream = False self.inhibit_slider_trigger = False self.q = LifoQueue() self.currtime = 0 self.endtime = -1 def initplayer(self): self.parentframe = Frame(self) self.parentframe.pack(fill=BOTH, expand=True) self.videoFrame = Frame(self.parentframe, width=800, height=480) self.videoFrame.pack(side="top", fill="both", expand=True) self.buttonframe = Frame(self.parentframe, padding="2 2 1 1") self.buttonframe.pack(side="bottom", fill="x", expand=False) self.seekbar = Scale(self.buttonframe, from_= 0, to=100, orient=HORIZONTAL) self.seekbar.grid(column=0, columnspan=4, row=0, sticky=[N, E, S, W]) self.seekbar.configure(command=self.seeked) self.selectbutton = Button(self.buttonframe, text="Select File") self.selectbutton.grid(column=0, row=1, sticky=[E,W]) self.streambutton = Button(self.buttonframe, text="Open HTTP", command=self.streamopen) self.streambutton.grid(column=1, row=1, sticky=[E,W]) self.playbutton = Button(self.buttonframe, text="Play") self.playbutton.config(command=self.playpause) self.playbutton.grid(column=2, row=1, sticky=[E,W]) self.fullscreenbutton = Button(self.buttonframe, text="Fullscreen", command=self.togglefullscreen) self.fullscreenbutton.grid(column=3, row=1, sticky=[E,W]) for child in self.buttonframe.winfo_children(): child.grid_configure(padx=5, pady=5) self.buttonframe.rowconfigure(0, weight=1) self.buttonframe.rowconfigure(1, weight=1) self.buttonframe.columnconfigure(0, weight=1) self.buttonframe.columnconfigure(1, weight=1) self.buttonframe.columnconfigure(2, weight=1) self.buttonframe.columnconfigure(3, weight=1) self.selectbutton.configure(command=self.fileopen) self.videoFrame.bind("<Button-1>",self.playpause) self.parent.bind("<F11>", self.togglefullscreen) self.parent.bind("<Motion>",self.mouseops) def mouseops(self,event=None): self.videoFrame.config(cursor="") self.videoFrame.after(5000,self.cursorhandler) if self.trackmouse: x, y = self.parent.winfo_pointerx(), self.parent.winfo_pointery() windowx, windowy = self.parent.winfo_width(), self.parent.winfo_height() if windowy - 30 <= y: if self.fstate: self.buttonframe.pack(side="bottom", fill="x", expand=False) self.trackmouse = False self.parent.after(5000, self.mousetracker) self.inhibit_slider_trigger = False elif self.fstate: self.buttonframe.pack_forget() self.inhibit_slider_trigger = True else: self.inhibit_slider_trigger = True def mousetracker(self): print 'Mouse Tracker' self.trackmouse = True self.videoFrame.after(0,self.mouseops) def cursorhandler(self): self.videoFrame.config(cursor="none") def togglefullscreen(self, event=None): self.fstate = not self.fstate self.parent.attributes("-fullscreen",self.fstate) if self.fstate: self.fullscreenbutton.config(text="Exit Fullscreen") self.buttonframe.pack_forget() self.videoFrame.config(cursor="none") else: self.fullscreenbutton.config(text="Fullscreen") self.buttonframe.pack(side="bottom", fill="x", expand=False) self.videoFrame.after(5000, self.cursorhandler) def fileopen(self): self.filenm = askopenfilename(filetypes=[("Supported Files","*.mp4;*.mkv;*.mpg;*.avi;*.mov"),("All Files","*.*")]) self.stream = False self.play() def streamopen(self): self.streamnm = Dlog(self.parent) if self.streamnm.result is not None: s = str(self.streamnm) else: return if s.startswith('http'): self.stream = True self.play() else: self.stream = False showerror("Error","Incorrect Entry") def play(self): global fifofilename if self.filenm is not None and self.filenm != "": winid = self.videoFrame.winfo_id() if self.mplayer_isrunning(): self.stop() try: self.paused = False self.playbutton.configure(text="Pause") if not self.stream: self.player_process = Popen(["mplayer","-fs","-slave","-quiet","-wid",str(winid),self.filenm],stdin=PIPE, stdout=PIPE) else: self.player_process = Popen(["mplayer","-fs","-slave","-quiet","-wid",str(winid),self.streamnm], stdin=PIPE, stdout=PIPE) self.stdout_thread = Thread(target=self.enqueue_pipe, args=(self.player_process.stdout, self.q)) self.stdout_thread.daemon = True self.stdout_thread.start() self.emptypipe() self.seekthread = Thread(target=self.seekbar_setup, args=()) self.seekthread.daemon = True self.seekthread.start() except: showerror("Error","".join(["Couldn't play video:\n",str(sys.exc_info()[:])])) def getvidtime(self): if self.mplayer_isrunning(): self.command_player("get_time_length") output = self.readpipe() while "ANS_LENGTH" not in output: output = self.readpipe() if "ANS_LENGTH" in output: return output.split('ANS_LENGTH=')[1] else: return 0 def playpause(self, event=None): if self.player_process is None: return self.paused = not self.paused if self.paused: print "VIDEO IS PAUSED /B/RO" self.playbutton.configure(text="Play") else: self.playbutton.configure(text="Pause") self.command_player("pause") def setwh(self,w,h): self.videoFrame.configure(width=w, height=h) def quit(self): print "QUIT CALLED" self.destroy() def mplayer_isrunning(self): if self.player_process is not None: return (self.player_process.poll() is None) else: return False def command_player(self, comd): global fifofilename if self.mplayer_isrunning(): try: self.player_process.stdin.flush() self.player_process.stdin.write("\r\n%s\r\n"%comd) # for _ in itertools.repeat(None,8192): # self.player_process.stdin.write("\n") self.player_process.stdin.flush() except: showerror("Error","Error passing command to mplayer\n%s"%sys.exc_info()[1]) def enqueue_pipe(self, out, q): print 'Working on reading mplayer pipe output...' for line in iter(out.readline, b''): q.put(line) out.close() def seekbar_setup(self): pos = '0' trial = 0 while float(pos)<1: trial += 1 pos = self.getvidtime() self.seekbar.config(to=int(float(pos))) self.endtime = int(float(pos)) Timer(1, self.seekbar_updater).start() def seekbar_updater(self): if not self.paused and self.inhibit_slider_trigger: self.currtime += 1 self.seekbar.set(self.currtime) else: self.currtime = self.seekbar.get() Timer(1, self.seekbar_updater).start() def seeked(self,e): pos = self.seekbar.get() print "We changed pos to :%d"% pos x, y = self.parent.winfo_pointerx(), self.parent.winfo_pointery() windowx, windowy = self.parent.winfo_width(), self.parent.winfo_height() if not self.inhibit_slider_trigger and windowy - 30 <= y: self.command_player("seek %d 2"%pos) if self.paused: self.command_player("pause") def startmousetrack(self): self.trackmouse = True def readpipe(self): line = "" try: line = self.q.get_nowait() except Empty: print "Empty PIPE" finally: return line def emptypipe(self): str = '' try: while not self.q.empty(): str += self.q.get_nowait() except Empty: print "Empty Pipe" finally: return str def stop(self): if self.mplayer_isrunning(): self.player_process.stdin.write("quit\n") self.player_process.stdin.flush() print self.emptypipe() self.player_process = None