/
pi-svetlana
/
CalculatorPython
Обзор
Документация
Войти
/
pi-svetlana
/
CalculatorPython
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/model/wrapper.py
55 строк
2 KB
pi-svetlana
calculator
17 ноя 2024, 17:19
17 ноя 2024, 17:19
4ec612c
Код
Авторство
О чём код?
import ctypes class GraphPoints(ctypes.Structure): _fields_ = [ ("x_val", ctypes.POINTER(ctypes.c_double)), ("y_val", ctypes.POINTER(ctypes.c_double)), ("x_size", ctypes.c_size_t), ("y_size", ctypes.c_size_t) ] class Model: def __init__(self): self.lib_mod = ctypes.CDLL('libmodel.so') self.lib_mod.WrModel.restype = ctypes.c_void_p self.instance = self.lib_mod.WrModel() self.lib_mod.WrCalculate.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_double, ctypes.c_char_p, ctypes.c_size_t] self.lib_mod.WrCalculate.restype = ctypes.c_int self.lib_mod.WrGetResult.restype = ctypes.c_double self.lib_mod.WrGetResult.argtypes = [ctypes.c_void_p] self.lib_mod.WrCalculateGraphPoints.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_double, ctypes.c_double, ctypes.c_char_p, ctypes.c_size_t] self.lib_mod.WrCalculateGraphPoints.restype = ctypes.c_int self.lib_mod.WrGetGraphPoints.restype = ctypes.POINTER(GraphPoints) self.lib_mod.WrGetGraphPoints.argtypes = [ctypes.c_void_p] def calculate(self, expression, x): error_message = ctypes.create_string_buffer(256) result_code = self.lib_mod.WrCalculate(self.instance, expression.encode('utf-8'), x, error_message, len(error_message)) if result_code != 0: raise RuntimeError(error_message.value.decode('utf-8')) def get_result(self): return self.lib_mod.WrGetResult(self.instance) def calculate_graph(self, expression, x_min, x_max): error_message = ctypes.create_string_buffer(256) result_code = self.lib_mod.WrCalculateGraphPoints(self.instance, expression.encode('utf-8'), x_min, x_max, error_message, len(error_message)) if result_code != 0: raise RuntimeError(error_message.value.decode('utf-8')) def get_graph_points(self): graph_points_ptr = self.lib_mod.WrGetGraphPoints(self.instance) graph_points = graph_points_ptr.contents size_x = graph_points.x_size size_y = graph_points.y_size x_vals = [graph_points.x_val[i] for i in range(size_x)] y_vals = [graph_points.y_val[i] for i in range(size_y)] return x_vals, y_vals