]> ToastFreeware Gitweb - chrisu/seepark.git/commitdiff
Use with statement for connection resource handling.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 23 Sep 2025 17:39:13 +0000 (19:39 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 23 Sep 2025 17:39:13 +0000 (19:39 +0200)
owm.py

diff --git a/owm.py b/owm.py
index 307e91061454da269fd558ae1e52216e2e41dcdf..1a55892a1cd0ba7bda3beb25b3dc7e186c107ca9 100755 (executable)
--- a/owm.py
+++ b/owm.py
@@ -82,18 +82,17 @@ def write_db(config, url, weather_json, weather_data):
     db = config.get('database','database')
 
     engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db), echo=False)
     db = config.get('database','database')
 
     engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db), echo=False)
-    conn = engine.connect()
-    row = dict(cityid=config.get('openweathermap', 'cityid'), url=url, result=json.dumps(weather_json))
-    row.update(weather_data)
-    for key, value in row.items():
-        if isinstance(value, float) and math.isnan(value):
-            row[key] = None
-    metadata = sqlalchemy.MetaData()
-    openweathermap_table = Table('openweathermap', metadata, autoload_with=engine)
-    ins = openweathermap_table.insert().prefix_with('IGNORE').values(**row)
-    conn.execute(ins)
-    conn.commit()
-    conn.close()
+    with engine.connect() as conn:
+        row = dict(cityid=config.get('openweathermap', 'cityid'), url=url, result=json.dumps(weather_json))
+        row.update(weather_data)
+        for key, value in row.items():
+            if isinstance(value, float) and math.isnan(value):
+                row[key] = None
+        metadata = sqlalchemy.MetaData()
+        openweathermap_table = Table('openweathermap', metadata, autoload_with=engine)
+        ins = openweathermap_table.insert().prefix_with('IGNORE').values(**row)
+        conn.execute(ins)
+        conn.commit()
 
 
 def main(configfile, debug):
 
 
 def main(configfile, debug):