An interpreter for evaluating simple math expressions.
Project description
Python - Math Interpreter
An interpreter, written from scratch in Python, that can evaluate simple math calculations, with a simple Read-Eval-Print-Loop (REPL) command line interface. This project is based on py-simple-math-interpreter, by David Callanan and illustrates the use of software engineering techniques to evolve a prototype code into a professional software.
Since our objective is to create an education/professional example we didn't change the original business logic, i.e., we do not change the interpretor code. Besides the four basic operations ( addition, subtraction, multiplication, and division) we implemented the power operator ^
to calculate a^b
and the modulo operator %
.
How to install and run the REPL
Using pipx (recommended)
To use this project as a standalone command line tool install pipx and run:
pipx install git+https://github.com/ScientificThought/py-math-interpreter
Install using pip
If you want to install this package and use it as a library on another project you ccan use pip
and a virtual environment manager. There are many ways to do that but if you already know how to setup a virtual environment, probably you could proceed your way.
In our development setup we use venv
, which comes with python, on Ubuntu Linux. If you are new to software development, follow the steps we recommend below which is expect to work on any Linux Distribution.
Create a new directory and a new virtual environment for working with mathinterpreter
:
mkdir mynewproject
cd mynewproject
python -m venv env
source env/bin/activate
Note that mynewproject
could be any name of your choice and that instead of using source
you could simply use the .
command. After activating your new environment, install the package from source using pip:
python -m pip install git+https://github.com/ScientificThought/py-math-interpreter.git
At this point you can run the REPL, from the directory in which you installed the package by typing mathinterpreter
in your terminal. This will open the REPL terminal of the interpreter and you can now use it for evaluating math expressions:
$ mathinterpreter
calc > 1+1
2.0
calc > (3.0+2*2)/3
2.3333333333333335
calc > q
$
Just type q
or quit
followed by enter in the REPL to return to your shell. That's it.
How to use this package as Python module
You can import the module mathinterpreter
from your python code and use the function calc
to evaluate expressions, as in the following:
from mathinterpreter import calc
value = calc('1 + 3*(5*2+1)/2')
print(value)
To using mathinterpreter
as a library you will need to import the Lexer, the Parser, and the Interpreter classes. For understanding how the interpreter works we recommend you to look the files mathinterpreter/__main__.py
and mathinterpreter/calculate.py
, which implement the REPL.
What we have done and what we are doing
Current implementation includes:**
Version: 2024.05.07
- library
mathinterpreter
(Lexer, Parser, Interpreter, and acalc()
, to simplify calculations); - a simple command line interface,
mathinterpreter
, using a simple REPL;
Project Software Stack
- pip: for package installation frontend;
- setuptools: for package installation backend;
- pytest: for unit tests;
- black: for automatic code formatting;
- GitHub Action Scripts: for CI Automation;
Planned improvements
We track feature requests with issues. But we aim to implement:
- implement code coverage;
- expand the tests suit;
- improve the cli:
- use
argparse
to improve the cli usage, allowing the user to callmathinterpreter "1+1"
, for example. - make the REPL more user friendly;
- use
Contributing
Please take a look on our contributing guide.
Who we are?
We are Marco Barbosa and Henrique Guidi, colleagues which got involved with computation while doing PhD in Physics at University of São Paulo.
ScientificThoughts was created as an online place to develop interesting small projects to improving our software engineering techniques. Want to join us? Just contact us.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mathinterpreter-0.2.1a0.tar.gz
.
File metadata
- Download URL: mathinterpreter-0.2.1a0.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | df4c5b5ca5588a76ffab13a501ee360a7472b7491011b82823fcaad8b4562828 |
|
MD5 | e614a966d87a7a3eeb52f9a607e3602b |
|
BLAKE2b-256 | 021a6e31d1fe38934a01b02d6c8f016da3e6289e4daf041fcad48b4aa6cb11e2 |
File details
Details for the file mathinterpreter-0.2.1a0-py3-none-any.whl
.
File metadata
- Download URL: mathinterpreter-0.2.1a0-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3d5975359ce876ea895408c3f5803029a1c73d5e1dcd648ad12d131b1218c7e |
|
MD5 | a92cf2312186786d01a02ece7c4451ee |
|
BLAKE2b-256 | f9f262e9c91279662028d2a9867a850c67f17d81b8101e9525f5cddc18ef723d |