Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MongodbConnection(metaclass=Singleton):
- def __init__(self):
- config = configparser.RawConfigParser()
- config.readfp(open(r'singleton.txt'))
- self.__MONGO_HOST = config.get('mongo_creds', 'MONGO_HOST')
- self.__MONGO_USER = config.get('mongo_creds', 'MONGO_USER')
- self.__MONGO_PORT = int(config.get('mongo_creds', 'MONGO_PORT'))
- self.__MONGO_PASSWORD = config.get('mongo_creds', 'MONGO_PASSWORD')
- self.__MONGO_DATABASE = config.get('mongo_creds', 'MONGO_DATABASE')
- def connect_to_mongodb(self):
- self.client = MongoClient(host=self.__MONGO_HOST, port=self.__MONGO_PORT, username=self.__MONGO_USER, password=self.__MONGO_PASSWORD, authSource=self.__MONGO_DATABASE)
- return self.client
- def execute_mongo_query_and_return_dataframe(self, input_query):
- database = self.client[database_name]
- # sample_query_mongo_shell = database[collection_name].find({})
- return pd.DataFrame(list(input_query))
- def close_connection(self):
- self.client.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement