|
| 1 | +# pycalc |
| 2 | + |
| 3 | +[](https://travis-ci.org/siarhiejkresik/Epam-2019-Python-Homework) |
| 4 | +Python Programming Language Foundation Hometask (EPAM, 2019). |
| 5 | +For task description see [link](https://github.com/siarhiejkresik/Epam-2019-Python-Homework/tree/master/final_task). |
| 6 | + |
| 7 | +`pycalc` is a command-line calculator implemented in pure Python 3 using Top Down Operator Precedence parsing algorithm (Pratt parser). It receives mathematical expression string as an argument and prints evaluated result. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +`pycalc` supports: |
| 12 | + |
| 13 | +- arithmetic operations (`+`, `-`, `*`, `/`, `//`, `%`, `^` (`^` is a power)); |
| 14 | +- comparison operations (`<`, `<=`, `==`, `!=`, `>=`, `>`); |
| 15 | +- 2 built-in python functions: `abs` and `round`; |
| 16 | +- all functions and constants from standard python module `math` (trigonometry, logarithms, etc.); |
| 17 | +- functions and constants from the modules provided with `-m` or `--use-modules` command-line option; |
| 18 | +- exit with non-zero exit code on errors. |
| 19 | + |
| 20 | +## How to install |
| 21 | + |
| 22 | +1. `git clone <repository_url>` |
| 23 | +2. `cd <repository_name>/final_task/` |
| 24 | +3. `pip3 install --user .` or `sudo -H pip3 install .` |
| 25 | + |
| 26 | +## Examples |
| 27 | + |
| 28 | +### Command line interface: |
| 29 | + |
| 30 | +```shell |
| 31 | +$ pycalc --help |
| 32 | +usage: pycalc [-h] EXPRESSION [-m MODULE [MODULE ...]] |
| 33 | + |
| 34 | +Pure-python command-line calculator. |
| 35 | + |
| 36 | +positional arguments: |
| 37 | + EXPRESSION expression string to evaluate |
| 38 | + |
| 39 | +optional arguments: |
| 40 | + -h, --help show this help message and exit |
| 41 | + -m MODULE [MODULE ...], --use-modules MODULE [MODULE ...] |
| 42 | + additional modules to use |
| 43 | +``` |
| 44 | +
|
| 45 | +### Calculation: |
| 46 | +
|
| 47 | +```shell |
| 48 | +$ pycalc '2+2*2' |
| 49 | +6 |
| 50 | + |
| 51 | +$ pycalc '2+sin(pi)^(2-cos(e))' |
| 52 | +2.0 |
| 53 | +``` |
| 54 | +
|
| 55 | +```shell |
| 56 | +$ pycalc '5+3<=1' |
| 57 | +False |
| 58 | +``` |
| 59 | +
|
| 60 | +```shell |
| 61 | +$ pycalc 'e + pi + tau' |
| 62 | +12.143059789228424 |
| 63 | + |
| 64 | +$ pycalc '1 + inf' |
| 65 | +inf |
| 66 | + |
| 67 | +$ pycalc '1 - inf' |
| 68 | +-inf |
| 69 | + |
| 70 | +$ pycalc 'inf - inf' |
| 71 | +nan |
| 72 | + |
| 73 | +$ pycalc 'nan == nan' |
| 74 | +False |
| 75 | +``` |
| 76 | +
|
| 77 | +### Errors: |
| 78 | +
|
| 79 | +```shell |
| 80 | +$ pycalc '15*(25+1' |
| 81 | +ERROR: syntax error |
| 82 | +15*(25+1 |
| 83 | + ^ |
| 84 | +$ pycalc 'func' |
| 85 | +ERROR: syntax error |
| 86 | +func |
| 87 | +^ |
| 88 | +$ pycalc '10 + 1/0 -3' |
| 89 | +ERROR: division by zero |
| 90 | +10 + 1/0 -3 |
| 91 | + ^ |
| 92 | +$ pycalc '1 + sin(1,2) - 2' |
| 93 | +ERROR: sin() takes exactly one argument (2 given) |
| 94 | +1 + sin(1,2) - 2 |
| 95 | + ^ |
| 96 | +$ pycalc '10^10^10' |
| 97 | +ERROR: math range error |
| 98 | +10^10^10 |
| 99 | + ^ |
| 100 | +$ pycalc '(-1)^0.5' |
| 101 | +ERROR: math domain error |
| 102 | +(-1)^0.5 |
| 103 | + ^ |
| 104 | +$ pycalc '' |
| 105 | +ERROR: empty expression provided |
| 106 | + |
| 107 | +$ pycalc '1514' -m fake calendar nonexistent time |
| 108 | +ERROR: no module(s) named fake, nonexistent |
| 109 | +``` |
| 110 | +
|
| 111 | +### Additional modules: |
| 112 | +
|
| 113 | +```python |
| 114 | +# my_module.py |
| 115 | +def sin(number): |
| 116 | + return 42 |
| 117 | +``` |
| 118 | +
|
| 119 | +```shell |
| 120 | +$ pycalc 'sin(pi/2)' |
| 121 | +1.0 |
| 122 | +$ pycalc 'sin(pi/2)' -m my_module |
| 123 | +42 |
| 124 | +$ pycalc 'THURSDAY' -m calendar |
| 125 | +3 |
| 126 | +$ pycalc 'sin(pi/2) - THURSDAY * 10' -m my_module calendar |
| 127 | +12 |
| 128 | +``` |
| 129 | +
|
| 130 | +## References |
| 131 | +
|
| 132 | +- https://en.wikipedia.org/wiki/Pratt_parser |
| 133 | +- https://tdop.github.io/ |
| 134 | +- http://www.oilshell.org/blog/2017/03/31.html |
| 135 | +- https://engineering.desmos.com/articles/pratt-parser |
0 commit comments