def game_loop(): running_players = [] finished_players = [] obstacles = [] goal = Destination([1000, 300], 100, 100, AppColor.GREEN.value) game_error = False gamestate = "running" for i in range(50): ai_player = Player() ai_player.create_Graphic([100, 325], 50, 50, AppColor.GREEN.value) ai_player.representation.set_Image('orange_bacterium_small.png') ai_player.create_Brain() ai_player.score = rect_distance(goal.get_Rect(), ai_player.get_central_Rect()) running_players.append(ai_player) print("Player created") while not game_error: gameDisplay.fill(AppColor.BLACK.value) pygame.draw.rect(gameDisplay, goal.color, goal.get_Rect()) if gamestate == "running": for p in running_players: if p.alive and not p.finished: p.representation.move_Player(display_width, display_height) draw_Player(p) check_crash(p, obstacles, goal) check_win(p, goal) else: running_players.remove(p) finished_players.append(p) if len(running_players) < 1: gamestate = "selection" print("------------------------------------------") print("generation finished playing") print("------------------------------------------") if gamestate == "selection": print(" ") print("selection starts") running_players = Player.create_generation(finished_players) finished_players.clear() print("new Generation ready") print(" ") gamestate = "running" for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() pygame.display.update() clock.tick(50)
def put_route(self, rib_route): assert_prefix_address_family(rib_route.prefix, self.address_family) rib_route.stale = False self.debug("Put %s", rib_route) prefix = rib_route.prefix prefix_str = ip_prefix_str(prefix) if not self.destinations.has_key(prefix_str): destination = Destination(self, prefix) self.destinations.insert(prefix_str, destination) else: destination = self.destinations.get(prefix_str) best_changed = destination.put_route(rib_route) if best_changed: child_prefix_strs = self.destinations.children(prefix_str) self.update_fib(prefix, child_prefix_strs)
def get_next_destination(self): locations_to_go = self.get_locations_to_go() if locations_to_go: clusters = self.build_clusters(locations_to_go) selected_cluster = max(clusters, key=attrgetter('weight')) return selected_cluster.destination_closest_to_car return Destination(self.car.get_location(), 0, None)
def addNamedDestination(self, title, pagenum): """ Add a destination to the pdf, using the specified title and pointing at the specified page number. """ dest = Destination(TextStringObject(title), NumberObject(pagenum), NameObject('/FitH'), NumberObject(826)) self.named_dests.append(dest)
def test_get_nav_end(self): taxonomy = DoubleLinkedList() taxonomy.add('1', 'One') taxonomy.add('2', 'Two') taxonomy.add('3', 'Three') taxonomy.add('4', 'Four') taxonomy.add('5', 'Five') result = Destination.get_nav(taxonomy, '5') self.assertEqual(['Three', 'Four'], result)
def build_destinations(self, clustered_locations): destinations = [ Destination( clustered_location[0], Utils.get_taxicab_distance(clustered_location[0], self.car.get_location()), clustered_location[1]) for clustered_location in clustered_locations ] return destinations
def lunchbot_maybe_load(): destinations = dict() try: with open(LUNCHBOT_FNAME_RATINGS, 'r') as f: current_destination = None while True: line = f.readline() line = line.rstrip('\n') if line == '': break if line[0] == ' ': # rating or visit-time if current_destination is None: raise ValueError() tokens = line.lstrip(' ').split(' ', 1) if len(tokens) != 2: raise ValueError() if tokens[0][0] == '@': time_raw = tokens[0][1:] who = tokens[1] time = int(time_raw) destinations[current_destination].add_visit(who, time) else: rating_raw = tokens[0] user = tokens[1] rating_num = int(rating_raw) user = user.strip() current_rating = destinations[ current_destination].rating current_rating.add_rating(rating_num, user) else: # destination current_destination = line destinations[current_destination] = Destination() except IOError: pass index = 0 try: with open(LUNCHBOT_FNAME_RATEE, 'r') as f: line = f.readline() line = line.rstrip('\n') index = int(line) except IOError: pass return (destinations, index)
def process_destinations(file_name, taxonomies, template_file, output_location): """ Process each <destination> in the xml file into its own file based on `template_file` :param: file_name (string/path) - full_path to the destinations.xml :param: taxonomies (dict) - double linked list of all the menu items :param: template_file (string/path) - full path to the template file :param: output_location (string/path) - where each processed destination should be stored """ destination = None for event, elem in ET.iterparse(file_name, events=('start','end',)): if elem.tag == 'destination': if event == 'start': location = elem.get('title') geo_id = elem.get('atlas_id') # build a destination.html file_name = os.path.join(output_location, "%s.html" % Destination.sanitize_name(location)) fp = open(file_name, 'w') destination = Destination(location, fp) else: destination.write_blocks_to_template(taxonomies, geo_id, template_file) print "processed: %s" % location if destination: for text_item in elem.itertext(): destination.build_block(elem.tag, text_item)
def put_route(self, route): """ Add a RibRoute object to the Destination object associated to the prefix. :param route: (RibRoute) the object to add to the Destination associated to the prefix :return: """ # If there is no Destination object for the prefix, create a new Destination object # for the given prefix and insert it in the Trie if not self.destinations.has_key(route.prefix): prefix_destination = Destination(self, route.prefix) self.destinations.insert(route.prefix, prefix_destination) else: prefix_destination = self.destinations.get(route.prefix) # Insert desired route in destination object best_changed = prefix_destination.put_route(route) # Get children prefixes before performing actions on the prefix (it can be deleted from the Trie) children_prefixes = self.destinations.children( prefix_destination.prefix) # TODO: ask if this case can occur # update_fib = True # if prefix_dest.parent_prefix_dest is not None and \ # prefix_dest.best_route.next_hops == prefix_dest.parent_prefix_dest.best_route.next_hops: # del self.destinations[prefix_dest.prefix] # self.fib.delete_route(prefix_dest.prefix) # update_fib = False # # if update_fib: # If best route changed in Destination object if best_changed: # Update prefix in the fib self.fib.put_route(prefix_destination.best_route) # Try to delete superfluous children if not self._delete_superfluous_children(prefix_destination, children_prefixes): # If children have not been deleted, update them self._update_prefix_children(children_prefixes)
def setUp(self): self.fixture_name = os.path.join(current_dir(), 'tests/fixtures', 'destination_small.xml') self.destination = Destination('testing', StringIO()) # fake a file handle
class TestDestination(unittest.TestCase): def setUp(self): self.fixture_name = os.path.join(current_dir(), 'tests/fixtures', 'destination_small.xml') self.destination = Destination('testing', StringIO()) # fake a file handle def test_init(self): message = "file_pointer is invalid or no write permissions" self.assertRaises(Exception, message, Destination, 'testing', 'not_file') self.assertRaises(Exception, message, Destination, 'testing', open(self.fixture_name, 'r')) def test_build_block(self): expected = "<h2>Money</h2><p>Testing of the buffer</p>\n" block = self.destination.build_block('money', 'Testing of the buffer') self.assertEqual(block, expected) self.assertEqual(self.destination.filled_items, ['money']) def test_build_block_appends(self): expected = "<h2>Money</h2><p>Testing of the buffer</p>\n" block = self.destination.build_block('money', 'Testing of the buffer') block = self.destination.build_block('history', 'more testing') expected += "<h2>History</h2><p>more testing</p>\n" self.assertEqual(block, expected) self.assertEqual(self.destination.filled_items, ['money', 'history']) def test_non_processed_build_block(self): destination = Destination('testing', StringIO(), tags_to_capture=['history', 'overview']) block = self.destination.build_block('air', 'i produce nothing') self.assertEqual(block, '') def test_some_processed_build_block(self): destination = Destination('testing', StringIO(), tags_to_capture=['money', 'history']) expected = "<h2>Money</h2><p>Testing of the buffer</p>\n" block = self.destination.build_block('money', 'Testing of the buffer') block = self.destination.build_block('air', '') self.assertEquals(block, expected) block = self.destination.build_block('history', 'Testing') expected += "<h2>History</h2><p>Testing</p>\n" self.assertEqual(block, expected) def test_get_nav_top(self): taxonomy = DoubleLinkedList() taxonomy.add('1', 'One') taxonomy.add('2', 'Two') taxonomy.add('3', 'Three') taxonomy.add('4', 'Four') taxonomy.add('5', 'Five') result = Destination.get_nav(taxonomy, '1') self.assertEqual(['Two', 'Three'], result) def test_get_nav_middle(self): taxonomy = DoubleLinkedList() taxonomy.add('1', 'One') taxonomy.add('2', 'Two') taxonomy.add('3', 'Three') taxonomy.add('4', 'Four') taxonomy.add('5', 'Five') result = Destination.get_nav(taxonomy, '3') self.assertEqual(['One', 'Two', 'Four', 'Five'], result) def test_get_nav_end(self): taxonomy = DoubleLinkedList() taxonomy.add('1', 'One') taxonomy.add('2', 'Two') taxonomy.add('3', 'Three') taxonomy.add('4', 'Four') taxonomy.add('5', 'Five') result = Destination.get_nav(taxonomy, '5') self.assertEqual(['Three', 'Four'], result)
def test_sanitize_heading(self): self.assertEqual(Destination.sanitize_heading('africa'), 'Africa') self.assertEqual(Destination.sanitize_heading('Port_sudan'), 'Port Sudan') self.assertEqual(Destination.sanitize_heading('KwaZulu-Natal'), 'Kwazulu-Natal')
def test_sanitize_name(self): self.assertEqual(Destination.sanitize_name('Africa'), 'africa') self.assertEqual(Destination.sanitize_name('Port Sudan'), 'port_sudan') self.assertEqual(Destination.sanitize_name('KwaZulu-Natal'), 'kwazulu-natal')
def _buildDestination(self, title, array): page, typ = array[0:2] array = array[2:] return Destination(title, page, typ, *array)
def create_destination(self): # what type of destination is this? youContainer = self.soup.find(id="youContainer") is_area = re.search("You & This Area", youContainer.get_text()) != None is_route = re.search("You & This Route", youContainer.get_text()) != None if not is_area and not is_route: # this is /v/ # not a place you can visit dest = None else: # grab features from html feature = sr.get_general(self.soup) dest = Destination(self.href, feature) # add self href to make recursing easy later dest.hierarchy = dest.hierarchy + [self.href] dest.is_area = is_area dest.is_route = is_route # more scraping is necessary for routes if dest.is_route: dest.update_feature(sr.get_grade(self.soup)) dest.update_feature(sr.get_protect_rate(self.soup)) dest.update_feature(sr.get_star_rating(self.soup)) dest.update_feature(sr.get_type(feature["type"])) return dest
def add(self, destination): assert destination not in self.destinations self.destinations[destination] = Destination()