]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blobdiff - wrpylib/wrvalidators.py
Remove unused imports.
[philipp/winterrodeln/wrpylib.git] / wrpylib / wrvalidators.py
index 7f22cf0dc4205be739ab33c7bff26044fc69fbed..f48bb30f5db1d03bf9549f5b7a3bd1bebe140747 100644 (file)
@@ -12,7 +12,7 @@ import urllib.parse
 import re
 from collections import OrderedDict, namedtuple
 from email.errors import HeaderParseError
-from typing import Tuple, Optional, List, Callable, Union, TypeVar, Dict
+from typing import Tuple, Optional, List, Callable, Union, TypeVar, Dict, NamedTuple
 
 import mwparserfromhell  # https://github.com/earwig/mwparserfromhell
 
@@ -384,7 +384,7 @@ def wikipage_from_str(value: str) -> str:
     '[[Birgitzer Alm]]' => '[[Birgitzer Alm]]'
     """
     if re.match(r'\[\[[^\[\]]+]]$', value) is None:
-        raise ValueError('No valid wiki page name "{}"'.format(value))
+        raise ValueError(f'No valid wiki page name "{value}"')
     return value
 
 
@@ -417,7 +417,7 @@ def email_from_str(value: str) -> str:
     try:
         email.headerregistry.Address(addr_spec=value)
     except HeaderParseError as e:
-        raise ValueError('Invalid email address: {}'.format(value), e)
+        raise ValueError(f'Invalid email address: {value}', e)
     return value
 
 
@@ -511,10 +511,9 @@ opt_phone_comment_opt_enum_converter = FromToConverter(lambda value: opt_phone_c
 # longitude/latitude converter
 # ----------------------------
 
-LonLat = namedtuple('LonLat', ['lon', 'lat'])
-
-
-lonlat_none = LonLat(None, None)
+class LonLat(NamedTuple):
+    lon: float
+    lat: float
 
 
 def lonlat_from_str(value: str) -> LonLat:
@@ -527,15 +526,15 @@ def lonlat_from_str(value: str) -> LonLat:
 
 
 def lonlat_to_str(value: LonLat) -> str:
-    return '{:.6f} N {:.6f} E'.format(value.lat, value.lon)
+    return f'{value.lat:.6f} N {value.lon:.6f} E'
 
 
-def opt_lonlat_from_str(value: str) -> LonLat:
-    return opt_from_str(value, lonlat_from_str, lonlat_none)
+def opt_lonlat_from_str(value: str) -> Optional[LonLat]:
+    return opt_from_str(value, lonlat_from_str, None)
 
 
-def opt_lonlat_to_str(value: LonLat) -> str:
-    return opt_to_str(value, lonlat_to_str, lonlat_none)
+def opt_lonlat_to_str(value: Optional[LonLat]) -> str:
+    return opt_to_str(value, lonlat_to_str, None)
 
 
 opt_lonlat_converter = FromToConverter(opt_lonlat_from_str, opt_lonlat_to_str)
@@ -772,7 +771,7 @@ def wikibox_from_template(template, converter_dict):
     for key, converter in converter_dict.items():
         try:
             if not template.has(key):
-                raise ValueError('Missing parameter "{}"'.format(key))
+                raise ValueError(f'Missing parameter "{key}"')
             result[key] = converter.from_str(str(template.get(key).value.strip()))
         except ValueError as e:
             exceptions_dict[key] = e
@@ -797,9 +796,9 @@ def template_from_str(value, name):
     wikicode = mwparserfromhell.parse(value)
     template_list = wikicode.filter_templates(recursive=False, matches=lambda t: t.name.strip() == name)
     if len(template_list) == 0:
-        raise ValueError('No "{}" template was found'.format(name))
+        raise ValueError(f'No "{name}" template was found')
     if len(template_list) > 1:
-        raise ValueError('{} "{}" templates were found'.format(len(template_list), name))
+        raise ValueError(f'{len(template_list)} "{name}" templates were found')
     return template_list[0]