- 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()