Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- This is a wrapper over the database. It is responsible
- for setting up the connection and executing querys
- """
- __author__ = 'Ionut'
- __version__ = '1.0'
- import redis
- class Database():
- """
- This is a singleton class that can be used
- to communicate with the database
- """
- _connection = None
- def __init__(self):
- """
- Responsible for connecting to the database
- if a connection is not already present
- """
- if Database._connection is None:
- Database._connection = self._connect()
- def _connect(self):
- """
- Connects to the database
- """
- while True:
- connection = redis.StrictRedis()
- if self._test_connection(connection) == 0:
- break
- return connection
- def _test_connection(self, connection):
- """
- Checks if the connection the the redis database
- was successfull
- """
- try:
- connection.set('test', 'test')
- except redis.ConnectionError:
- return -1
- return 0
- def get_connection(self):
- """
- Returns an object that can be used to interact
- with the database
- """
- return self._connection
Advertisement
Add Comment
Please, Sign In to add comment