]> git.openstreetmap.org Git - nominatim.git/commitdiff
Merge pull request #3667 from eumiro/simplify-int-float
authorSarah Hoffmann <lonvia@denofr.de>
Sun, 9 Mar 2025 08:44:15 +0000 (09:44 +0100)
committerGitHub <noreply@github.com>
Sun, 9 Mar 2025 08:44:15 +0000 (09:44 +0100)
Simplify  int/float manipulation

src/nominatim_api/search/db_search_builder.py
src/nominatim_api/types.py
src/nominatim_api/v1/classtypes.py
src/nominatim_db/db/connection.py
src/nominatim_db/indexer/progress.py
src/nominatim_db/tools/tiger_data.py
src/nominatim_db/utils/centroid.py

index 2c5d0d18bad121791f100eb845c1cadcfb9c0e3e..ddfddaa64878a0d6f05d297d7427cf4416156259 100644 (file)
@@ -227,7 +227,7 @@ class SearchBuilder:
 
         # To catch remaining results, lookup by name and address
         # We only do this if there is a reasonable number of results expected.
-        exp_count = exp_count / (2**len(addr_tokens)) if addr_tokens else exp_count
+        exp_count /= 2**len(addr_tokens)
         if exp_count < 10000 and addr_count < 20000:
             penalty += 0.35 * max(1 if name_fulls else 0.1,
                                   5 - len(name_partials) - len(addr_tokens))
index 66a3c553f22db94527a9e4e612503e4708209608..e58df47895fa0fc9799745e66ee78b118169ee57 100644 (file)
@@ -144,7 +144,7 @@ class Point(NamedTuple):
         except ValueError as exc:
             raise UsageError('Point parameter needs to be numbers.') from exc
 
-        if x < -180.0 or x > 180.0 or y < -90.0 or y > 90.0:
+        if not -180 <= x <= 180 or not -90 <= y <= 90.0:
             raise UsageError('Point coordinates invalid.')
 
         return Point(x, y)
index 7198b412e2cb9badf31a3910c73b74d90bf48c20..c2fe14539349a82c8b2d607a4ecb0127ed44305d 100644 (file)
@@ -25,8 +25,8 @@ def get_label_tag(category: Tuple[str, str], extratags: Optional[Mapping[str, st
     elif rank < 26 and extratags and 'linked_place' in extratags:
         label = extratags['linked_place']
     elif category == ('boundary', 'administrative'):
-        label = ADMIN_LABELS.get((country or '', int(rank/2)))\
-                or ADMIN_LABELS.get(('', int(rank/2)))\
+        label = ADMIN_LABELS.get((country or '', rank // 2))\
+                or ADMIN_LABELS.get(('', rank // 2))\
                 or 'Administrative'
     elif category[1] == 'postal_code':
         label = 'postcode'
index e960a3fa8a1186450a67b749754155975e8327b8..cc843ab6121e5e809731ede314577485edbfe78e 100644 (file)
@@ -102,10 +102,10 @@ def server_version_tuple(conn: Connection) -> Tuple[int, int]:
         Converts correctly for pre-10 and post-10 PostgreSQL versions.
     """
     version = conn.info.server_version
-    if version < 100000:
-        return (int(version / 10000), int((version % 10000) / 100))
-
-    return (int(version / 10000), version % 10000)
+    major, minor = divmod(version, 10000)
+    if major < 10:
+        minor //= 100
+    return major, minor
 
 
 def postgis_version_tuple(conn: Connection) -> Tuple[int, int]:
index 66c35f062c72d27061f193bff300e39760283e24..b99296e550a735daa30bc6906ded2ffc15db8d3a 100644 (file)
@@ -50,8 +50,8 @@ class ProgressLogger:
         places_per_sec = self.done_places / done_time
         eta = (self.total_places - self.done_places) / places_per_sec
 
-        LOG.warning("Done %d in %d @ %.3f per second - %s ETA (seconds): %.2f",
-                    self.done_places, int(done_time),
+        LOG.warning("Done %d in %.0f @ %.3f per second - %s ETA (seconds): %.2f",
+                    self.done_places, done_time,
                     places_per_sec, self.name, eta)
 
         self.next_info += int(places_per_sec) * self.log_interval
@@ -68,8 +68,8 @@ class ProgressLogger:
             diff_seconds = (rank_end_time - self.rank_start_time).total_seconds()
             places_per_sec = self.done_places / diff_seconds
 
-        LOG.warning("Done %d/%d in %d @ %.3f per second - FINISHED %s\n",
-                    self.done_places, self.total_places, int(diff_seconds),
+        LOG.warning("Done %d/%d in %.0f @ %.3f per second - FINISHED %s\n",
+                    self.done_places, self.total_places, diff_seconds,
                     places_per_sec, self.name)
 
         return self.done_places
index 85110ae53bd13125ebd0bfb6e8a0ab7fd67a20f2..7b8655703e6a250a6833b5934393b09c4121e8b2 100644 (file)
@@ -108,8 +108,7 @@ async def add_tiger_data(data_dir: str, config: Configuration, threads: int,
 
         async with QueryPool(dsn, place_threads, autocommit=True) as pool:
             with tokenizer.name_analyzer() as analyzer:
-                lines = 0
-                for row in tar:
+                for lineno, row in enumerate(tar, 1):
                     try:
                         address = dict(street=row['street'], postcode=row['postcode'])
                         args = ('SRID=4326;' + row['geometry'],
@@ -124,10 +123,8 @@ async def add_tiger_data(data_dir: str, config: Configuration, threads: int,
                                                     %s::INT, %s::TEXT, %s::JSONB, %s::TEXT)""",
                         args)
 
-                    lines += 1
-                    if lines == 1000:
+                    if not lineno % 1000:
                         print('.', end='', flush=True)
-                    lines = 0
 
         print('', flush=True)
 
index a45d958b342214e31fb66153f738a11ecd06fd59..02512336d648efab0157e58d6a8ddeecd6335eb0 100644 (file)
@@ -30,8 +30,8 @@ class PointsCentroid:
         if self.count == 0:
             raise ValueError("No points available for centroid.")
 
-        return (float(self.sum_x/self.count)/10000000,
-                float(self.sum_y/self.count)/10000000)
+        return (self.sum_x / self.count / 10_000_000,
+                self.sum_y / self.count / 10_000_000)
 
     def __len__(self) -> int:
         return self.count
@@ -40,8 +40,8 @@ class PointsCentroid:
         if isinstance(other, Collection) and len(other) == 2:
             if all(isinstance(p, (float, int)) for p in other):
                 x, y = other
-                self.sum_x += int(x * 10000000)
-                self.sum_y += int(y * 10000000)
+                self.sum_x += int(x * 10_000_000)
+                self.sum_y += int(y * 10_000_000)
                 self.count += 1
                 return self