A simple calculator toolkit written in Python, with several UIs, tests, quality assurance facilities, and a Poetry configuration which allows developers to simplify dependency management, and publication on PyPI.
UI code is clearly separated from the business logic. Actually, two sorts of UIs are provided: a command-line interface (CLI) and a graphical user interface (GUI), based on Kivy.
Tests are provided for the business logic, for the CLI, and for the GUI.
All test cases are written using the unittest framework.
Test code is clearly separated from the main code: it is placed in a tests directory.
Quality assurance facilities include:
- test coverage
- static code analysis
Poetry is configures in such a way that:
- a virtual environment is created automatically for this project, in the
.venvdirectory - dependencies are declared in a
pyproject.tomlfile, and installed in the aforementioned environment - the project can be published on PyPI, with a single command
- Python 3.11
- Kivy 2.3
- Coverage.py 7.4.0
- Mypy 1.9.0
- Pytest 8.1.0
- Poetry 1.7.0
The software can be run as either a desktop app or a command-line tool.
Recall restoring the development environment before running the app for the first time.
Open a shell in this directory, and run the following command:
calculator-guiwhich is just a shortcut (created by Poetry) for:
python -m calculator.ui.guiOpen a shell in this directory, and run the following command:
calculator EXPRESSIONwhich is just a shortcut (created by Poetry) for:
python -m calculator.ui.cli EXPRESSIONwhere EXPRESSION is a mathematical expression to be evaluated.
Assumption: you have Poetry installed on your system. To check that: open a shell and run
poetry --version. If thepoetrycommand is lacking, please install Poetry following the instructions here.
-
Open a shell in this directory, and run the following command:
poetry install
this shall:
- create a virtual environment in the
.venvdirectory- please ensure that a
.venvdirectory exists in your project directory, after running this command
- please ensure that a
- install the dependencies declared in the
pyproject.tomlfile
- create a virtual environment in the
-
Activate the virtual environment, by running
poetry shell
you may check that the virtual environment is active by looking at the shell prompt. Alternatively, if the environment is active, you may be able to evaluate expression by means of the
calculator EXPRESSIONcommand.Try running
calculator 1+1and see if the result is2. -
Make sure that VS Code is using the same environment as the one created by Poetry.
- Open the Command Palette (Ctrl+Shift+P, use Command instead of Ctrl on MacOS)
- Type
Python: Select Interpreter - Choose the local environment, i.e. the one having path
./.venv/bin/python
Assumption: you have restored the development environment.
You may exploit the following commands to run all the tests:
python -m unittest discover -v -s testsWhile running the tests, the calculator GUI may quickly appear and disappear, multiple times. This is normal, as the tests are exercising the GUI.
You may exploit the following commands to run only the tests for the business logic:
python -m unittest discover -v -s tests -p 'test_model.py'A similar command may be exploited to test just the CLI (test_cli.py) or the GUI (test_gui.py).
Assumption: you have restored the dependencies, and you are using Visual Studio Code.
Lunch configurations are already provided in the .vscode directory.
You may switch to the "Run and Debug" view, and select the desired configuration from the dropdown menu.
Instructions here: https://code.visualstudio.com/docs/editor/debugging
Assumption: you have restored the dependencies.
Open a shell in this directory, and run the following commands (2 commands in a row):
python -m coverage run -m unittest discover -v -s tests
python -m coverage report -mThe output should be similar to the following:
Name Stmts Miss Cover Missing
------------------------------------------------------
calculator\__init__.py 38 3 92% 13, 39, 48
calculator\ui\cli.py 21 3 86% 16-17, 27
calculator\ui\gui.py 57 8 86% 55-57, 61, 63, 65, 69, 76
tests\test_cli.py 15 0 100%
tests\test_gui.py 30 0 100%
tests\test_model.py 38 0 100%
------------------------------------------------------
TOTAL 199 14 93%
You may make the output more pleasant, by generating a HTML report:
python -m coverage htmlThen, open the generated file htmlcov/index.html in your web browser.
Assumption: you have restored the dependencies.
Open a shell in this directory, and run the following command:
python -m compileall calculator testsIf no error is printed, the code is free from syntax / import errors.
Assumption: you have restored the dependencies.
Open a shell in this directory, and run the following command:
python -m mypy calculator testsThe output should be similar to the following:
calculator\__init__.py:13: error: Unsupported operand types for + ("str" and "int") [operator]
calculator\__init__.py:44: error: Parameterized generics cannot be used with class or instance checks [misc]
calculator\__init__.py:44: error: Argument 2 to "isinstance" has incompatible type "<typing special form>"; expected "_ClassInfo" [arg-type]
calculator\__init__.py:46: error: Returning Any from function declared to return "int | float" [no-any-return]
Found 4 errors in 1 file (checked 6 source files)