Advertisement
Ilya_Bykonya

Untitled

Dec 13th, 2023
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | Source Code | 0 0
  1. # This Python file uses the following encoding: utf-8
  2. from PySide6.QtWidgets import QApplication, QWidget, QSpinBox, QLabel, QPushButton, QVBoxLayout
  3. import pickle
  4. import redis
  5. import sys
  6.  
  7. class RedisMemoizator:
  8.     def __init__(self, host: str, port, user: str, password: str):
  9.         self.__connection = redis.StrictRedis(host=host, username=user, password=password, decode_responses=False)
  10.     def __call__(self, function):
  11.         def memoize(*args, **kwargs):
  12.             print(f'Сalling {function.__name__}() with {args}, {kwargs}')
  13.             try:
  14.                 print(f'\tTry get value: {self._create_key(function, *args, **kwargs)}')
  15.                 return self._get_memoized_value(function, *args, **kwargs)
  16.             except Exception as error:
  17.                 print(f'\tCall function because: {error}')
  18.                 return self._set_memoized_value(function(*args, **kwargs), function, *args, **kwargs)
  19.         return memoize
  20.  
  21.     def _create_key(self, function, *args, **kwargs):
  22.         return f'{function.__name__}|{args}|{kwargs}'
  23.     def _get_memoized_value(self, function, *args, **kwargs):
  24.         print(f'Load value: [{self._create_key(function, *args, **kwargs)}]')
  25.         if not self.__connection.exists(self._create_key(function, *args, **kwargs)):
  26.             raise Exception('Has no key in redis')
  27.         return pickle.loads(self.__connection.get(self._create_key(function, *args, **kwargs)))
  28.     def _set_memoized_value(self, result, function, *args, **kwargs):
  29.         print(f'Save value: [{self._create_key(function, *args, **kwargs)}|{result}]')
  30.         self.__connection.set(self._create_key(function, *args, **kwargs), pickle.dumps(result))
  31.         return result
  32.  
  33.  
  34. @RedisMemoizator(host='178.208.86.244', port=6379, user='', password='')
  35. def fibonacci(number: int) ->int:
  36.     print(f'Calculate fibonacci for number [{number}]')
  37.     if number <= 1:
  38.         return 1
  39.  
  40.     return fibonacci(number - 1) + fibonacci(number - 2)
  41.  
  42. if __name__ == '__main__':
  43.     app = QApplication(sys.argv)
  44.  
  45.     input_value = QSpinBox()
  46.     input_value.setRange(0, 200)
  47.     result_value = QLabel('--')
  48.     calculate_button = QPushButton('Calculate')
  49.     calculate_button.clicked.connect(lambda: result_value.setText(str(fibonacci(input_value.value()))))
  50.  
  51.     window = QWidget()
  52.     layout = QVBoxLayout()
  53.     window.setLayout(layout)
  54.     layout.addWidget(input_value)
  55.     layout.addWidget(calculate_button)
  56.     layout.addWidget(result_value)
  57.     window.show()
  58.  
  59.     sys.exit(app.exec())
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement