def load_weather(session, year): swg = StadiumWeatherGetter() for stadium in swg.getStadiums(): yearly_weather = swg.getWeatherForYearAtStadium(stadium, year) for daily_weather in yearly_weather: for hourly_weather in daily_weather: new_weather_object = Weather() new_weather_object.loadWeatherContainer(hourly_weather) try: session.add(new_weather_object) session.commit() except IntegrityError: session.rollback()