From 47f084689f8ee70585d6115f4e09e61b6426fb07 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Thu, 4 Jul 2024 09:56:29 +0200
Subject: [PATCH] Addings types, and precision arg

---
 get_coordinates_by_zipcode.py | 36 ++++++++++++++++++++---------------
 test_zipcode.py               | 10 ++++------
 2 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/get_coordinates_by_zipcode.py b/get_coordinates_by_zipcode.py
index 12cff76..6f4352b 100644
--- a/get_coordinates_by_zipcode.py
+++ b/get_coordinates_by_zipcode.py
@@ -5,20 +5,22 @@ e.g. for RegionChooser or download_files_from_LGL_BW.py.
 
 Also accepts multiple Zipcodes, or Zipcode prefix.
 
-    python get_coordinates_by_zipcode.py --help
-    usage: get_coordinates_by_zipcode.py [-h] PLZ [PLZ ...]
+    usage: get_coordinates_by_zipcode.py [-h] [-p PRECISION] PLZ [PLZ ...]
 
     Get WKT geometry for desired PLZs
 
     positional arguments:
-    PLZ         desired PLZs
+    PLZ                   desired PLZs
 
     options:
-    -h, --help  show this help message and exit
+    -h, --help            show this help message and exit
+    -p PRECISION, --precision PRECISION
+                            precision of returned polygon [m]
+
 
 > python get_coordinates_by_zipcode.py 70174
 > python get_coordinates_by_zipcode.py 70567 70569
-> python get_coordinates_by_zipcode.py 70
+> python get_coordinates_by_zipcode.py -p1000 70
 """
 
 # TODO: Write tests
@@ -32,15 +34,16 @@ from shapely.geometry import shape
 from shapely.ops import unary_union
 
 INPUT_FOLDER = Path('plz')
-PLZ_FILENAME = 'plz-5stellig.geojson'
+PLZ_FILENAME: str = 'plz-5stellig.geojson'
 PLZ_SHAPE_FILE = INPUT_FOLDER / PLZ_FILENAME
-PRECISION = 10  # [m]
-ONE_DEGREE = 40e6 / 360  # [m]
+PRECISION: float = 10  # [m]
+ONE_DEGREE: float = 40e6 / 360  # [m]
 
-PLZ_SHAPES = None
+PLZ_SHAPES: dict
+CACHED: bool = False
 
 
-def _download_plz_shapes_if_needed():
+def _download_plz_shapes_if_needed() -> None:
     if not PLZ_SHAPE_FILE.exists():
         from tqdm import tqdm
         import requests
@@ -54,9 +57,9 @@ def _download_plz_shapes_if_needed():
         print('  Done')
 
 
-def _get_plz_shapes():
-    global PLZ_SHAPES
-    if PLZ_SHAPES:
+def _get_plz_shapes() -> dict:
+    global PLZ_SHAPES, CACHED
+    if CACHED:
         return PLZ_SHAPES
 
     _download_plz_shapes_if_needed()
@@ -65,13 +68,14 @@ def _get_plz_shapes():
         with open(PLZ_SHAPE_FILE) as f:
             print('  Done')
             PLZ_SHAPES = json.load(f)
+            CACHED = True
             return PLZ_SHAPES
     except json.decoder.JSONDecodeError:
         PLZ_SHAPE_FILE.unlink()
         raise AttributeError(f"{PLZ_FILENAME} seems to be damaged. Removing it. Please try again!")
 
 
-def get_coordinates_by_zipcode(plz_patterns, precision=PRECISION):
+def get_coordinates_by_zipcode(plz_patterns: list[str], precision: float = PRECISION) -> str:
     plz_shapes = _get_plz_shapes()
     geometries = []
     for plz_pattern in plz_patterns:
@@ -114,5 +118,7 @@ if __name__ == '__main__':
     parser = argparse.ArgumentParser(description='Get WKT geometry for desired PLZs')
     parser.add_argument('plzs', metavar='PLZ', type=str, nargs='+',
                         help='desired PLZs')
+    parser.add_argument('-p', '--precision', default=PRECISION, type=int,
+                        help='precision of returned polygon [m]')
     args = parser.parse_args()
-    get_coordinates_by_zipcode(args.plzs)
+    get_coordinates_by_zipcode(args.plzs, args.precision)
diff --git a/test_zipcode.py b/test_zipcode.py
index f70d40b..9766856 100644
--- a/test_zipcode.py
+++ b/test_zipcode.py
@@ -1,13 +1,11 @@
 import unittest
-import get_coordinates_by_zipcode
+from get_coordinates_by_zipcode import get_coordinates_by_zipcode
 
 
-class TestClass(unittest.TestCase):
+class TestGetCoordinates(unittest.TestCase):
 
-    def test_method(self):
-        self.assertEqual('foo'.upper(), 'FOO')
-        get_coordinates_by_zipcode.get_coordinates_by_zipcode(['70567'])
-        get_coordinates_by_zipcode.get_coordinates_by_zipcode(['70567'], 1_000)
+    def test_get_mohringen(self):
+        coords = get_coordinates_by_zipcode(['70567'])
 
 
 if __name__ == '__main__':
-- 
GitLab