Best Python code snippet using ATX
steerbench_parser.py
Source:steerbench_parser.py
...106 self.simulation_state.move_agents_from_obstacles()107 def get_sim_state(self):108 return self.simulation_state109 def parse_header(self, header_elem):110 bounds = self.parse_bounds(header_elem.find('steerbench:worldBounds', self.namespace))111 self.simulation_state.bounds = [bounds[0], bounds[1], bounds[4], bounds[5]]112 def parse_agents(self, root):113 agents = root.findall('steerbench:agent', self.namespace)114 for agent in agents:115 self.parse_agent(agent)116 agent_regions = root.findall('steerbench:agentRegion', self.namespace)117 for region in agent_regions:118 self.parse_agent_region(region)119 def parse_agent(self, element):120 name = element.find('steerbench:name', self.namespace).text121 initial_config = element.find('steerbench:initialConditions', self.namespace)122 pos = self.parse_vector(initial_config.find('steerbench:position', self.namespace),123 self.simulation_state.bounds)124 direction = self.parse_vector(initial_config.find('steerbench:direction', self.namespace), [-1, 1, -1, 1])125 goal_config = element.find('steerbench:goalSequence', self.namespace)126 goals = []127 for target in goal_config.findall('steerbench:seekStaticTarget', self.namespace):128 goal_pos = self.parse_vector(target.find('steerbench:targetLocation', self.namespace),129 self.simulation_state.bounds)130 type = float(target.find('steerbench:targetLocation', self.namespace).131 find('steerbench:y', self.namespace).text)132 width = float(target.find('steerbench:desiredSpeed', self.namespace).text)133 height = float(target.find('steerbench:timeDuration', self.namespace).text)134 box = [width, height]135 goals.append(Goal(pos=goal_pos, type=type, box=box))136 orientation = math.atan2(direction[1, 0], direction[0, 0])137 if "_" in name:138 index = int(name.split("_")[1])139 color = rainbow[index]140 else:141 color = rainbow[self.rainbow_index]142 self.rainbow_index += 1143 self.simulation_state.agents.append(144 Agent(pos=pos, radius=0.5, orientation=orientation, goals=goals,145 id=len(self.simulation_state.agents), color=color)146 )147 def parse_agent_region(self, element):148 num = int(element.find('steerbench:numAgents', self.namespace).text)149 bounds = self.parse_bounds(element.find('steerbench:regionBounds', self.namespace))150 initial_config = element.find('steerbench:initialConditions', self.namespace)151 color = rainbow[self.rainbow_index % 7]152 self.rainbow_index += 1153 for i in range(num):154 pos = np.array([[random.uniform(bounds[0], bounds[1])], [random.uniform(bounds[4], bounds[5])]])155 direction = self.parse_vector(initial_config.find('steerbench:direction', self.namespace), [-1, 1, -1, 1])156 goal_config = element.find('steerbench:goalSequence', self.namespace)157 goals = []158 for target in goal_config.findall('steerbench:seekStaticTarget', self.namespace):159 goal_pos = self.parse_vector(target.find('steerbench:targetLocation', self.namespace),160 [bounds[0], bounds[1], bounds[4], bounds[5]])161 type = float(target.find('steerbench:targetLocation', self.namespace).162 find('steerbench:y', self.namespace).text)163 width = float(target.find('steerbench:desiredSpeed', self.namespace).text)164 height = float(target.find('steerbench:timeDuration', self.namespace).text)165 box = [width, height]166 goals.append(Goal(pos=goal_pos, type=type, box=box))167 orientation = math.atan2(direction[1, 0], direction[0, 0])168 self.simulation_state.agents.append(169 Agent(pos=pos, radius=0.5, orientation=orientation, goals=goals,170 id=len(self.simulation_state.agents), color=color)171 )172 def parse_vector(self, element, bounds):173 if element.find('steerbench:random', self.namespace) is not None:174 return np.array([175 [random.uniform(bounds[0], bounds[1])],176 [random.uniform(bounds[2], bounds[3])]177 ])178 else:179 return np.array([180 [float(element.find('steerbench:x', self.namespace).text)],181 [float(element.find('steerbench:z', self.namespace).text)]182 ])183 def parse_obstacles(self, root):184 obstacles = root.findall('steerbench:obstacle', self.namespace)185 for obstacle in obstacles:186 self.parse_obstacle(obstacle)187 obstacle_regions = root.findall('steerbench:obstacleRegion', self.namespace)188 for region in obstacle_regions:189 self.parse_obstacle_region(region)190 def parse_obstacle(self, element):191 bounds = self.parse_bounds(element)192 type = int(element.find('steerbench:ymin', self.namespace).text)193 self.simulation_state.obstacles.append(194 Obstacle(bounds[1] - bounds[0], bounds[5] - bounds[4], bounds[0], bounds[4],195 type, len(self.simulation_state.obstacles)))196 def parse_obstacle_region(self, region_elem):197 bounds = self.parse_bounds(region_elem.find('steerbench:regionBounds', self.namespace))198 num = int(region_elem.find('steerbench:numObstacles', self.namespace).text)199 size = float(region_elem.find('steerbench:obstacleSize', self.namespace).text)200 for i in range(num):201 self.simulation_state.obstacles.append(202 Obstacle(size, size, random.uniform(bounds[0], bounds[1]), random.uniform(bounds[4], bounds[5]),203 0, len(self.simulation_state.obstacles)))204 def parse_bounds(self, bounds_elem):205 bounds = [min(float(bounds_elem.find('steerbench:xmin', self.namespace).text),206 float(bounds_elem.find('steerbench:xmax', self.namespace).text)),207 max(float(bounds_elem.find('steerbench:xmin', self.namespace).text),208 float(bounds_elem.find('steerbench:xmax', self.namespace).text)),209 min(float(bounds_elem.find('steerbench:ymin', self.namespace).text),210 float(bounds_elem.find('steerbench:ymax', self.namespace).text)),211 max(float(bounds_elem.find('steerbench:ymin', self.namespace).text),212 float(bounds_elem.find('steerbench:ymax', self.namespace).text)),213 min(float(bounds_elem.find('steerbench:zmin', self.namespace).text),214 float(bounds_elem.find('steerbench:zmax', self.namespace).text)),215 max(float(bounds_elem.find('steerbench:zmin', self.namespace).text),216 float(bounds_elem.find('steerbench:zmax', self.namespace).text)),217 ]218 return bounds
FEParser.py
Source:FEParser.py
...19 self.parse_expr()20 self.expect(')')21 if self.test_lookahead('|'):22 self.expect('|')23 self.parse_bounds()24 elif self.test_lookahead('<'):25 self.expect('<')26 self.parse_expr()27 self.expect(',')28 self.parse_expr()29 self.expect('>')30 if self.test_lookahead('_'):31 self.parse_bounds()32 elif self.test_lookahead(NAME):33 self.expect(NAME)34 else:35 self.expect(NUMBER)36 return self.pop()37 def parse_bounds (self):38 self.push('bounds')39 self.expect('_')40 self.expect(NAME)41 return self.pop()42 def parse_constraint (self):43 self.push('constraint')44 self.parse_expr()45 while self.test_lookahead('='):...
script.py
Source:script.py
1def parse_bounds(low, high, item, max_value):2 serie = range(max_value + 1)3 for char in item:4 middle = len(serie) // 25 if char == low:6 serie = serie[:middle]7 elif char == high:8 serie = serie[middle:]9 else:10 raise Exception("Invalid char")11 if len(serie) == 1:12 return serie[0]13 raise Exception("Should not end here")14def get_seat_id(row, column):15 return row * 8 + column16lines = []17with open('input', 'r') as input_file:18 for line in input_file:19 lines.append(line.strip())20max_seat_id = -121rows = {22 i: [] for i in range(127 + 1)23}24all_seats_ids = []25for item in lines:26 row = parse_bounds("F", "B", item[:7], 127)27 column = parse_bounds("L", "R", item[-3:], 7)28 seat_id = get_seat_id(row, column)29 all_seats_ids.append(seat_id)30 max_seat_id = max(seat_id, max_seat_id)31 rows[row].append(column)32print(f"Max seat id: {max_seat_id}")33missing_seat_ids = []34for key, row in rows.items():35 all_rows = [i for i in range(7 + 1)]36 missing = set(all_rows) - set(row)37 if len(missing) > 0:38 for num in missing:39 missing_seat_ids.append(get_seat_id(key, num))40for seat_id in missing_seat_ids:41 if seat_id - 1 in all_seats_ids and seat_id + 1 in all_seats_ids:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!