From 5fa603745b110d8b59a332e7613dfde7ed2f2c7b Mon Sep 17 00:00:00 2001 From: Satoshi I <46483721+isst001@users.noreply.github.com> Date: Wed, 5 Mar 2025 14:44:02 -0600 Subject: [PATCH] 01 --- .../Student 3/Student 3/model.py | 26 ++++++++++++ .../Student 3/Student 3/pycalc.py | 42 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 calculator-exercise/Files for Students/Student 3/Student 3/model.py create mode 100644 calculator-exercise/Files for Students/Student 3/Student 3/pycalc.py diff --git a/calculator-exercise/Files for Students/Student 3/Student 3/model.py b/calculator-exercise/Files for Students/Student 3/Student 3/model.py new file mode 100644 index 0000000..236c051 --- /dev/null +++ b/calculator-exercise/Files for Students/Student 3/Student 3/model.py @@ -0,0 +1,26 @@ +# model.py + +from pycalc.constants import * + +# Simple "model" to handle calculator functionality +# This function just uses the Python "eval()" function +# to evaluate the expression entered. +def evaluateExpression(expression): + """ + Evaluate the given mathematical expression and return the result. + + This function takes a mathematical expression as input, evaluates it using the `eval()` function, + and returns the result as a string. If an error occurs during evaluation, it returns an error message. + + Args: + expression (str): The mathematical expression to evaluate. + + Returns: + str: The result of the evaluation as a string, or an error message if evaluation fails. + """ + try: + result = str(eval(expression, {}, {})) + except: + result = ERROR_MSG + + return result \ No newline at end of file diff --git a/calculator-exercise/Files for Students/Student 3/Student 3/pycalc.py b/calculator-exercise/Files for Students/Student 3/Student 3/pycalc.py new file mode 100644 index 0000000..1e1623a --- /dev/null +++ b/calculator-exercise/Files for Students/Student 3/Student 3/pycalc.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# calc.py +# https://realpython.com/python-pyqt-gui-calculator/ + +""" Simple calculator built with Python and PyQt5 library """ + +import os +import sys + +from .view import PyCalcUi +from .controller import PyCalcCtrl +from .model import evaluateExpression + +from PyQt5.QtCore import Qt +from PyQt5.QtWidgets import QApplication + +__version__ = "0.1" +__author__ = "Tim Jones" + +def main(): + """ + Main application entry point. + + This function serves as the entry point for the calculator application. It initializes the + calculator GUI, creates instances of the model and controller, and starts the application's main loop. + + Args: + None + + Returns: + None + """ + calc = QApplication(sys.argv) + view = PyCalcUi() # Render calculator GUI + view.show() # Display calculator GUI + model = evaluateExpression # Create instance of model + PyCalcCtrl(model=model, view=view) # Create instance of controller + sys.exit(calc.exec_()) # Execute application main loop + +if __name__ == "__main__": + main() \ No newline at end of file