Browse Source

Clean up urban centers retry logic

Kirk Trombley 4 years ago
parent
commit
deac63337b
1 changed files with 19 additions and 22 deletions
  1. 19 22
      server/app/point_gen/urban_centers.py

+ 19 - 22
server/app/point_gen/urban_centers.py

@@ -66,34 +66,30 @@ def urban_coord(country_lock, city_retries=10, point_retries=10, max_dist_km=25)
 
 
 class WorldUrbanPointSource(GeoPointSource):
-    def __init__(self, country_retries=20, max_attempts=20):
+    def __init__(self, country_retries=30):
         self.country_retries = country_retries
-        self.max_attempts = max_attempts
-
+    
     def get_name(self):
         return "Urban-global"
 
     def get_points(self, n):
-        # Will make at most self.country_retries * self.max_attempts attempts to call urban_coord
-        attempts = 0
+        # Will make at most self.country_retries * n attempts to call urban_coord
         points = []
-        # TODO tweak this to just go point by point, should be simpler
-        while len(points) < n:
-            if attempts > self.max_attempts:
-                raise ExhaustedSourceError(points)
+        for _ in range(n):
             countries = random.sample(URBAN_CENTERS.keys(), k=min(self.country_retries, len(URBAN_CENTERS)))
-            for c in countries:
+            for country in countries:
                 logger.info(f"Selecting urban centers from {c}")
                 pt = urban_coord(c)
                 if pt is not None:
                     points.append(pt)
                     break
-            attempts += 1
+            else:
+                raise ExhaustedSourceError(points)
         return points
 
 
 class CountryUrbanPointSource(GeoPointSource):
-    def __init__(self, country_lock, max_attempts=20):
+    def __init__(self, country_lock, max_attempts=5):
         self.country_lock = country_lock
         self.max_attempts = max_attempts
 
@@ -101,19 +97,20 @@ class CountryUrbanPointSource(GeoPointSource):
         return f"Urban-{self.country_lock}"
 
     def get_points(self, n):
-        # Will make at most self.max_attempts calls to urban_coord with 100 city retries each
+        # Will make at most self.max_attempts * n calls to urban_coord with 100 city retries each
         attempts = 0
         points = []
-        while len(points) < n:
-            if attempts > self.max_attempts:
+        for _ in range(n):
+            for _ in range(self.max_attempts):
+                pt = urban_coord(
+                    city_retries=100,
+                    country_lock=self.country_lock,
+                )
+                if pt is not None:
+                    points.append(pt)
+                    break
+            else:
                 raise ExhaustedSourceError(points)
-            pt = urban_coord(
-                city_retries=100,
-                country_lock=self.country_lock,
-            )
-            if pt is not None:
-                points.append(pt)
-            attempts += 1
         return points