def p_label_maybe(p): '''label_maybe : ID COLON | ''' if len(p) == 3: p[0] = Node('label', leaf=p[1]) else: p[0] = Node('no_label')
def generate_basic(width, height, controls): map_gen = map_generator.MapGenerator((width, height), 32) block_map = map_gen.generate_block_map(5) numX = map_gen.numX numY = map_gen.numY # numX = int(math.ceil(width/32.0)) # numY = int(math.ceil(height/32.0)) # block_map = [[0] * numY for _ in range(numX)] # for i in range(numX): # for j in range(numY): # if random.random() > 0.95: # block_map[i][j] = -1 world = maptiles.MapTiles(block_map, numX, numY) teams = [] nodes = [] for t in range(number_of_teams): colour = colours.TEAM_COLOURS[t] team = Team(f"Team {t}", colour) node = Node( True, False, Vector(world.width * random.random(), world.height * random.random()), 100, 0, 0) node.team = team team.node = node teams.append(team) nodes.append(node) for i in range(number_of_nodes): check_seed = False while check_seed == False: x = random.random() * world.width y = random.random() * world.height r = random.random() * 100 + 50 check_seed = check_boundary(x, y, 15, world) node = Node(False, False, Vector(x, y), r, 0, 0) node.team = teams[i % len(teams)] nodes.append(node) for i in range(number_of_source_nodes): check_seed = False while check_seed == False: x = random.random() * world.width y = random.random() * world.height r = random.random() * 100 + 50 check_seed = check_boundary(x, y, 15, world) nodes.append(Node(True, True, Vector(x, y), r, 5000, 0)) players = [] t = 0 for control in controls: team = teams[t] t += 1 if t >= len(teams): t = 0 players.append(Player(f"Player {i}", team, control)) return {"nodes": nodes, "teams": teams, "players": players, "world": world}
def add_trnas(my_orfs, G): f = tempfile.NamedTemporaryFile(mode='wt') f.write(">temp\n") f.write(my_orfs.seq) f.seek(0) try: output = Popen(["tRNAscan-SE", "-B", "-q", "-b", f.name], stdout=PIPE, stdin=PIPE, stderr=PIPE).stdout.read() except: sys.stderr.write("Warning: tRNAscan not found, proceding without tRNA masking.\n") return [] # Iterate over the trnas for line in output.splitlines(): # Add in trna column = line.split('\t') start = int(column[2]) stop = int(column[3]) if(start < stop): source = Node('tRNA', 'start', 4, start) target = Node('tRNA', 'stop', 4, stop-2) my_orfs.other_end['t'+str(stop-2)] = start my_orfs.other_end['t'+str(start)] = stop-2 else: source = Node('tRNA', 'stop', -4, stop) target = Node('tRNA', 'start', -4, start-2) my_orfs.other_end['t'+str(start-2)] = stop my_orfs.other_end['t'+str(stop)] = start-2 G.add_edge(Edge(source, target, -Decimal(20)))
def p_includes(p): '''includes : INCLUDE program END_INCLUDE includes | INCLUDE program END_INCLUDE''' if len(p) == 5: p[0] = Node("include", [p[2], p[4]], p[1]) else: p[0] = Node("include", [p[2]], p[1])
def p_mult_T(p): '''mult_T : TIMES | mult_T TIMES''' if len(p) == 2: p[0] = Node("times", leaf='*') else: p[0] = Node("times", leaf='*' + p[1].leaf)
def p_parameter(p): '''parameter : type ID | BYREF type ID''' if len(p) == 3: p[0] = Node("parameter", [p[1]], p[2]) else: p[0] = Node("byref_parameter", [p[2]], p[3])
def prepend(self, data): if self.head == None: self.head = Node(data) else: new_head = Node(data) new_head.next = self.head self.head = new_head
def create_tree(self, frequency_list): node_list = [] tree_nodes = [] #cria a lista com os nós da base for node in frequency_list: frequency = node[0] letter = node[1] node_list.append(Node(frequency, letter)) #junta os nós até o topo da árvore e armazena todos os nós na lista tree while len(node_list) > 1: node_list.sort() node1 = node_list.pop(0) node2 = node_list.pop(0) frequency = node1.get_frequency() + node2.get_frequency() letter = node1.get_letter() + node2.get_letter() #cria o novo nó a partir dos dois anteriores new_node = Node(frequency=frequency, letter=letter, left_child=node1, right_child=node2) node_list.append(new_node) tree_nodes.append(node1) tree_nodes.append(node2) root = node_list.pop(0) tree_nodes.append(root) #cria e retorna um objeto tipo árvore tree = Tree(tree_nodes=tree_nodes) return tree
def p_function_definition(p): '''function_definition : type ID LPAREN parameter_list_maybe RPAREN LBRACE declarations_maybe statements_maybe RBRACE | VOID ID LPAREN parameter_list_maybe RPAREN LBRACE declarations_maybe statements_maybe RBRACE''' if p[1] == "void": temp = Node("void", leaf="void") p[0] = Node("function_definition", [temp, p[4], p[7], p[8]], p[2]) else: p[0] = Node("function_definition", [p[1], p[4], p[7], p[8]], p[2])
def p_function_declaration(p): '''function_declaration : type ID LPAREN parameter_list_maybe RPAREN SEMICOLON | VOID ID LPAREN parameter_list_maybe RPAREN SEMICOLON''' if p[1] == 'void': temp = Node("void", leaf="void") p[0] = Node("function_declaration", [temp, p[4]], p[2]) else: p[0] = Node("function_declaration", [p[1], p[4]], p[2])
def append(self, data): if self.head == None: self.head = Node(data) else: current = self.head while current.next != None: current = current.next current.next = Node(data)
def insert_at_head(self, data): if self.head is None: self.head = Node(data) return node = Node(data) self.head.prev = node node.next = self.head self.head = node
def initplacement(self, tls, x, y, level): key = (x, y, level) if key in self.placementcache: return # was created while this task was queued (name, url, minsize) = self.provider_url(*key) filename = self.filecache.fetch(self.session, name, url, minsize) if not filename: # Couldn't fetch image if filename is False: # Definitively unavailable self.placementcache[key] = False self.canvas.Refresh() # Probably wanting to know this return try: if __debug__: clock = time.clock() texdata = self.canvas.vertexcache.texcache.get(filename, alpha=False, wrap=False, downsample=False, fixsize=True, defer=True) except: if __debug__: print_exc() self.placementcache[key] = False # Couldn't load image self.canvas.Refresh() # Probably wanting to know this return # Make a new placement (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) if isinstance(texdata, tuple): placement.texdata = texdata else: placement.definition.texture = texdata # already in the cache 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 placement = False else: if __debug__: print "%6.3f time in imagery read & layout for %s" % ( time.clock() - clock, placement.name) self.canvas.Refresh( ) # Probably wanting to display this - will be allocated during OnPaint self.placementcache[key] = placement return
def p_block(self, p): ''' block : LBRACKET RBRACKET | LBRACKET questions RBRACKET ''' if len(p) == 4: p[0] = Node('question-list', p[2]) else: p[0] = Node('question-list', [])
def createList(iterator) -> Node: ''' Create a link list with None head and keep the order. ''' head = Node(None, None) head.next = head probe = head for data in iterator: probe.next = Node(data, head) probe = probe.next # node head return head
def p_program(p): '''program : declaration program | includes program | declaration includes | declaration''' if len(p) == 3: if p[2].type == 'program': p[0] = p[2].insert_in_children(0, p[1]) else: p[0] = Node("program", [p[1], p[2]], "program") else: p[0] = Node("program", [p[1]], "program")
def p_type(p): '''type : basic_type | basic_type mult_T''' if len(p) == 2: p[0] = p[1] else: p[0] = Node("pointer", leaf=(p[1].leaf + p[2].leaf))
def p_declarations_maybe(p): '''declarations_maybe : declaration declarations_maybe | ''' if len(p) == 3: p[0] = p[2].insert_in_children(0, p[1]) else: p[0] = Node('declarations', leaf='declarations_maybe')
def p_statements_maybe(p): '''statements_maybe : statement statements_maybe | ''' if len(p) == 3: p[0] = p[2].insert_in_children(0, p[1]) else: p[0] = Node('statements', leaf='statements_maybe')
def p_unary_operator(p): '''unary_operator : AND | TIMES | PLUS | MINUS | LNOT''' p[0] = Node('unary_operator', leaf=p[1])
def p_expression_list(p): '''expression_list : expression COMMA expression_list | expression''' if len(p) == 4: p[0] = p[3].insert_in_children(0, p[1]) else: p[0] = Node('expression_list', [p[1]], 'expression_list')
def test_expand_node(self): """ it should add a child to the node, with the correct actions.""" root_node = Node(self.state, self.bot.hand, 0) node, _ = self.bot.expand_node(root_node) action = list(root_node.children.keys())[0] self.assertTrue(action in self.bot.hand)
def append(self, data): new_node = Node(data) cur = self.head while (cur.next != None): cur = cur.next cur.next = new_node self.length += 1
def __init__(self, problem, node=None): self.problem = problem self.timer_start = time.time() if node is None: self.node = Node(state=problem.get_initial()) else: self.node = node
def insert_after(self, locdata, data): node = Node(data) curr = self.head while curr.data != locdata and curr.next: curr = curr.next node.next = curr.next curr.next = node
def p_expression_maybe(p): '''expression_maybe : expression | ''' if len(p) == 2: p[0] = p[1] else: p[0] = Node('no_expression')
def expand_node(self, node, filter_by_explored=False): _problem = self.problem node_children_list = [] if node.action: _actions = _problem.get_actions_without_opposite(node.action) else: _actions = _problem.ACTIONS for _key in _actions: _action_name = _actions[_key] _state = _problem.get_next_state_by_action(action=_action_name, state=node.state) if _state: if filter_by_explored and self.has_explored(_state.get_hash()): continue _new_node = Node(state=_state, parent=node, action=_key, depth=node.depth + 1, cost=node.cost + 1) node_children_list.append(_new_node) self.num_nodes += len(node_children_list) node.set_children(node_children_list) return node_children_list
def p_array_maybe(p): '''array_maybe : array | ''' if len(p) == 2: p[0] = p[1] else: p[0] = Node('not_array')
def _parse_blocks(self, definition): """ Finds nested block definitions in current block and populates block_defs. Uses class attribute block_regex to find block name, args, and definition. Args: definition (str): Block definition to parse. Returns: A string with all block definitions removed. Used by _parse_elements. """ block_matches = re.finditer(self.__class__.block_regex, definition) for match in block_matches: name = match.group('name') args = match.group('args') defs = match.group('defs') nodes = re.findall(self.__class__.node_regex, args) nodes = [Node(n) for n in nodes] pairs = re.findall(self.__class__.pair_regex, args) pairs = { p[0].strip(): p[1].strip() for p in [pair.split('=') for pair in pairs] } defs = defs.split('\n') self.blocks[name] = Block(name, nodes, defs, mux=self.mux, **pairs) return re.sub(self.__class__.block_regex, '', definition)
def p_expression_list_maybe(p): '''expression_list_maybe : expression_list | ''' if len(p) == 2: p[0] = p[1] else: p[0] = Node('expression_list', leaf='empty')