def test_city(self): c1 = City('montreal', 'b', 'c', (45., 73.), 'd', 123, 'e') c2 = City('quebec', 'b', 'c', (46., 71.), 'd', 123, 'e') self.assertAlmostEqual(distance(c1, c2), 191.5, 1) self.assertAlmostEqual(fast_distance(c1, c2), 191.5, 1)
logger.info('removing cities close to a bigger city') timer = Timer(nb_cities_kept) cities_to_remove = set() for country, regions in cities.items(): for region, cs in regions.items(): cs = list(cs.values()) # It's simpler if they are in decreasing order of population cs = sorted(cs, key=lambda x: x.pop, reverse=True) for i, city in enumerate(cs): for j in range(0, i): city2 = cs[j] # Here `city2` is bigger `city` # If it's close and has not been flagged for deletion, we # will remove `city` if (country, region, city2.name) not in cities_to_remove \ and distance(city, city2) < args.too_close: logger.debug('removing %s, too close to %s', city.name, city2.name) cities_to_remove.add((country, region, city.name)) break timer.update() for country, region, city in cities_to_remove: del cities[country][region][city] # flatten the cities cities_flat = [] cities_per_region = defaultdict(list) cities_per_country = defaultdict(list) for country, regions in cities.items(): for region, cs in regions.items():