]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Avoid remaining usage of LonLat(None, None), instead None is used.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 29 Sep 2021 21:12:26 +0000 (23:12 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 29 Sep 2021 21:12:26 +0000 (23:12 +0200)
tests/test_wrmwmarkup.py
tests/test_wrvalidators.py
wrpylib/wrmwmarkup.py

index 2be3f6e796db5aa6c18dd3b2fa4fe1086d9cdc9c..7f930a8a384a4476be6d7117c560a8d52599611a 100644 (file)
@@ -16,7 +16,7 @@ class TestSledrun(unittest.TestCase):
             pass
         rodelbahnbox = collections.OrderedDict([
             ('Position', LonLat(9.986508, 47.30982)),
-            ('Position oben', LonLat(None, None)),
+            ('Position oben', None),
             ('Höhe oben', 1244),
             ('Position unten', LonLat(8.506047, 46.20210)),
             ('Höhe unten', None),
@@ -210,7 +210,7 @@ class TestInn(unittest.TestCase):
         class Inn:
             pass
         gasthausbox = collections.OrderedDict()
-        gasthausbox['Position'] = LonLat(None, None)
+        gasthausbox['Position'] = None
         gasthausbox['Höhe'] = None
         gasthausbox['Betreiber'] = None
         gasthausbox['Sitzplätze'] = None
@@ -265,7 +265,7 @@ class TestInn(unittest.TestCase):
         inn.image = None
         inn.sledding_list = 'Nein'
         gasthausbox = inn_to_gasthausbox(inn)
-        self.assertEqual(gasthausbox['Position'], LonLat(None, None))
+        self.assertEqual(gasthausbox['Position'], None)
         self.assertEqual(gasthausbox['Höhe'], None)
         self.assertEqual(gasthausbox['Betreiber'], None)
         self.assertEqual(gasthausbox['Sitzplätze'], None)
@@ -283,7 +283,7 @@ class TestInn(unittest.TestCase):
         class Inn:
             pass
         gasthausbox = collections.OrderedDict()
-        gasthausbox['Position'] = LonLat(None, None)
+        gasthausbox['Position'] = None
         gasthausbox['Höhe'] = None
         gasthausbox['Betreiber'] = None
         gasthausbox['Sitzplätze'] = None
@@ -338,7 +338,7 @@ class TestInn(unittest.TestCase):
         inn.image = None
         inn.sledding_list = None
         gasthausbox = inn_to_gasthausbox(inn)
-        self.assertEqual(gasthausbox['Position'], LonLat(None, None))
+        self.assertEqual(gasthausbox['Position'], None)
         self.assertEqual(gasthausbox['Höhe'], None)
         self.assertEqual(gasthausbox['Betreiber'], None)
         self.assertEqual(gasthausbox['Sitzplätze'], None)
index afe28dd7ab476507bdcd11b09909ba35396cc5c0..6fd5e0ee9067b57940cacdfe15cdf44539112862 100644 (file)
@@ -814,7 +814,7 @@ class TestRodelbahnbox(unittest.TestCase):
         self.assertEqual(LonLat(12.806522, 46.807218), value['Position'])
         self.assertEqual(LonLat(12.818658, 46.799014), value['Position oben'])
         self.assertEqual(1046, value['Höhe oben'])
-        self.assertEqual(LonLat(None, None), value['Position unten'])
+        self.assertEqual(None, value['Position unten'])
         self.assertEqual(None, value['Höhe unten'])
         self.assertEqual(3500, value['Länge'])
         self.assertEqual(2, value['Schwierigkeit'])
@@ -840,7 +840,7 @@ class TestRodelbahnbox(unittest.TestCase):
             ('Position', LonLat(12.806522, 46.807218)),
             ('Position oben', LonLat(12.818658, 46.799014)),
             ('Höhe oben', 1046),
-            ('Position unten', LonLat(None, None)),
+            ('Position unten', None),
             ('Höhe unten', None),
             ('Länge', 3500),
             ('Schwierigkeit', 2),
index f2ea5c871ec7cee1f21a05fd42d98ba59c68004d..2ab28d227efa0ef3ba64ad6141b54a153c48e706 100644 (file)
@@ -3,7 +3,7 @@
 import re
 import xml.etree.ElementTree
 import collections
-from typing import Tuple, Optional, List
+from typing import Tuple, Optional, List, OrderedDict, Union
 
 from mwparserfromhell.nodes import Template
 
@@ -15,17 +15,29 @@ from wrpylib.wrvalidators import LonLat, opt_lonlat_from_str, opt_lonlat_to_str,
     opt_phone_comment_enum_to_str, lift_german_from_str, GASTHAUSBOX_DICT
 
 
-def sledrun_from_rodelbahnbox(value, sledrun):
+def split_lon_lat(value: Optional[LonLat]) -> Union[LonLat, Tuple[None, None]]:
+    if value is None:
+        return None, None
+    return value
+
+
+def join_lon_lat(lon: Optional[float], lat: Optional[float]) -> Optional[LonLat]:
+    if lon is None or lat is None:
+        return None
+    return LonLat(lon, lat)
+
+
+def sledrun_from_rodelbahnbox(value: OrderedDict, sledrun: object):
     """Takes a Rodelbahnbox as returned by rodelbahnbox_from_str (that is, an OrderedDict) and
     updates the sledrun instance with all values present in the Rodelbahnbox. Other values are not
     updated. Does not validate the arguments."""
     # sledrun.page_id = None # this field is not updated because it is not present in the RodelbahnBox
     # sledrun.page_title = None # this field is not updated because it is not present in the RodelbahnBox
     # sledrun.name_url = None # this field is not updated because it is not present in the RodelbahnBox
-    sledrun.position_longitude, sledrun.position_latitude = value['Position']
-    sledrun.top_longitude, sledrun.top_latitude = value['Position oben']
+    sledrun.position_longitude, sledrun.position_latitude = split_lon_lat(value['Position'])
+    sledrun.top_longitude, sledrun.top_latitude = split_lon_lat(value['Position oben'])
     sledrun.top_elevation = value['Höhe oben']
-    sledrun.bottom_longitude, sledrun.bottom_latitude = value['Position unten']
+    sledrun.bottom_longitude, sledrun.bottom_latitude = split_lon_lat(value['Position unten'])
     sledrun.bottom_elevation = value['Höhe unten']
     sledrun.length = value['Länge']
     sledrun.difficulty = value['Schwierigkeit']
@@ -55,10 +67,10 @@ def sledrun_to_rodelbahnbox(sledrun) -> collections.OrderedDict:
     """Takes a sledrun instance that might come from the database and converts it to a OrderedDict ready
     to be formatted as RodelbahnBox."""
     value = collections.OrderedDict()
-    value['Position'] = LonLat(sledrun.position_longitude, sledrun.position_latitude)
-    value['Position oben'] = LonLat(sledrun.top_longitude, sledrun.top_latitude)
+    value['Position'] = join_lon_lat(sledrun.position_longitude, sledrun.position_latitude)
+    value['Position oben'] = join_lon_lat(sledrun.top_longitude, sledrun.top_latitude)
     value['Höhe oben'] = sledrun.top_elevation
-    value['Position unten'] = LonLat(sledrun.bottom_longitude, sledrun.bottom_latitude)
+    value['Position unten'] = join_lon_lat(sledrun.bottom_longitude, sledrun.bottom_latitude)
     value['Höhe unten'] = sledrun.bottom_elevation
     value['Länge'] = sledrun.length
     value['Schwierigkeit'] = sledrun.difficulty
@@ -91,7 +103,7 @@ def inn_from_gasthausbox(value, inn):
         if v == '':
             return None
         return v
-    inn.position_longitude, inn.position_latitude = value['Position']
+    inn.position_longitude, inn.position_latitude = split_lon_lat(value['Position'])
     inn.position_elevation = value['Höhe']
     inn.operator = value['Betreiber']
     inn.seats = value['Sitzplätze']
@@ -112,10 +124,10 @@ def inn_from_gasthausbox(value, inn):
 def inn_to_gasthausbox(inn) -> collections.OrderedDict:
     """Converts an inn class to a dict of Gasthausbox properties. inn is an Inn instance."""
     def convfromdb(val, key):
-        v = '' if value is None else val
+        v = '' if val is None else val
         return GASTHAUSBOX_DICT[key].from_str(v)
     value = collections.OrderedDict()
-    value['Position'] = LonLat(inn.position_longitude, inn.position_latitude)
+    value['Position'] = join_lon_lat(inn.position_longitude, inn.position_latitude)
     value['Höhe'] = inn.position_elevation
     value['Betreiber'] = inn.operator
     value['Sitzplätze'] = inn.seats