Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import redis
- from django.conf import settings
- class StateDataBase:
- def __init__(self, db_name):
- LIST_BIO_PARAMETRS = (
- 'bloodoxygen',
- 'heartrate',
- 'thermometer',
- 'tonometer',
- 'distance',
- 'calories',
- )
- """Конструктор класса промежуточного состаяния"""
- self.connection = redis.Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT,db=10)
- self.db_name = db_name
- all_connection = self.connection.hgetall(self.db_name)
- for val in LIST_BIO_PARAMETRS:
- if not bytes(val,encoding='utf-8') in all_connection:
- objects = {
- f'{val}' : '',
- }
- print(objects)
- self.connection.hmset(self.db_name, objects)
- def GetAllParams(self) -> json:
- """Возвращает все данные из бд"""
- return self.connection.hgetall(self.db_name)
- def ClearAllState(self):
- """Очищает всю используемую бд"""
- # self.connection.unlink(self.db_name)
- self.connection.flushdb()
- def GetParametrs(self, parametr) -> json:
- """Геттер класса StateDataBase (Возвращает по ключу значение из бд Redis)"""
- try:
- key = bytes(
- parametr,
- encoding='utf-8'
- )
- compound = json.loads(
- self.connection.hgetall(self.db_name)[key].decode('utf-8')
- )
- return compound
- except Exception as e:
- print('==== get_user_parametrs ==== %s ' % (e))
- return None
- def SetParametrs(self, key_parametr, value):
- """Устанавливает по ключу значение переданных через параметры метада , бд Redis"""
- key = bytes(
- key_parametr,
- encoding='utf-8'
- )
- compound = {}
- try:
- compound = json.loads(
- self.connection.hgetall(self.db_name)[key].decode('utf-8')
- )
- except:
- print("No date !")
- value = json.loads(value)
- if compound:
- mapp = compound
- mapp.append(value)
- else:
- mapp = []
- mapp.append(value)
- self.connection.hset(self.db_name, key, json.dumps(mapp))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement