Skip to content
Open

01 #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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()