]> ToastFreeware Gitweb - chrisu/seepark.git/blobdiff - owm.py
Convert string format function calls to f-strings.
[chrisu/seepark.git] / owm.py
diff --git a/owm.py b/owm.py
index 1a55892a1cd0ba7bda3beb25b3dc7e186c107ca9..e538b2b6ae3386d433ff7c6b4a7fd63baf259f06 100755 (executable)
--- a/owm.py
+++ b/owm.py
@@ -18,7 +18,7 @@ import os
 from pprint import pprint
 
 import sqlalchemy
-from sqlalchemy import create_engine, Table
+from sqlalchemy import create_engine, Table, URL
 
 from seeparklib.openweathermap import openweathermap_json
 
@@ -28,7 +28,7 @@ def fromtimestamp(timestamp):
 
 
 # https://stackoverflow.com/questions/7490660/converting-wind-direction-in-angles-to-text-words
-def degToCompass(num):
+def deg_to_compass(num):
     if num is None or num is math.nan:
         return 'N/A'
     val=int((num/22.5)+.5)
@@ -36,7 +36,7 @@ def degToCompass(num):
     return arr[(val % 16)]
 
 
-def extractweatherdata(w):
+def extract_weather_data(w):
     data = dict(
         datetime = fromtimestamp(w['dt']),
         sunrise = fromtimestamp(w['sys']['sunrise']),
@@ -51,7 +51,7 @@ def extractweatherdata(w):
     )
 
     data['winddegrees'] = w['wind']['deg'] if 'deg' in w['wind'] else math.nan
-    data['winddirection'] = degToCompass(data['winddegrees'])
+    data['winddirection'] = deg_to_compass(data['winddegrees'])
     data['precipitation'] = w['rain']['3h'] if 'rain' in w and w['rain'].get('3h') else math.nan
     data['visibility'] = w.get('visibility', math.nan)
 
@@ -67,11 +67,11 @@ def write_csv(csv_file, weather_data):
             weather_data['datetime'].time(),
             weather_data['sunrise'].time(),
             weather_data['sunset'].time(),
-            "{:.2f}".format(weather_data['temp']),
-            "{:.2f} mm/h".format(weather_data['precipitation']),
-            "{:.1f} km/h {}".format(weather_data['windspeed'], weather_data['winddirection']),
+            f"{weather_data['temp']:.2f}",
+            f"{weather_data['precipitation']:.2f} mm/h",
+            f"{weather_data['windspeed']:.1f} km/h {weather_data['winddirection']}",
             weather_data['weather'],
-            "{}".format(weather_data['cloudiness'])
+            f"{weather_data['cloudiness']}"
         ])
 
 
@@ -81,7 +81,8 @@ def write_db(config, url, weather_json, weather_data):
     host = config.get('database','hostname')
     db = config.get('database','database')
 
-    engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db), echo=False)
+    db_url = URL.create(drivername='mysql+mysqldb', username=user, password=pwd, host=host, database=db)
+    engine = create_engine(db_url, echo=False)
     with engine.connect() as conn:
         row = dict(cityid=config.get('openweathermap', 'cityid'), url=url, result=json.dumps(weather_json))
         row.update(weather_data)
@@ -105,7 +106,7 @@ def main(configfile, debug):
     url, weather_json = openweathermap_json(apikey, cityid)
     if debug:
         pprint(weather_json)
-    weather_data = extractweatherdata(weather_json)
+    weather_data = extract_weather_data(weather_json)
     if debug:
         pprint(weather_data)