def main(): try: client = MongoClient('localhost',27017) DataService.init(client) apps = DataService.retrieve_appinfo() #work flow for app in apps.keys(): user_download_history = DataService.retrieve_user_download_history() top_5_app = calculate_Top_5(app, user_download_history.values()) DataService.update_app_info({"app_id": app},{"$set": {"Top 5": top_5_app}}) except Exception as e: print(e) finally: #clean up work if 'client' in locals(): client.close()