Python Programming Language Foundation Hometask (EPAM, 2019).
For task description see link.
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.
pycalc
supports:
- arithmetic operations (
+
,-
,*
,/
,//
,%
,^
(^
is a power)); - comparison operations (
<
,<=
,==
,!=
,>=
,>
); - 2 built-in python functions:
abs
andround
; - all functions and constants from standard python module
math
(trigonometry, logarithms, etc.); - functions and constants from the modules provided with
-m
or--use-modules
command-line option; - exit with non-zero exit code on errors.
git clone <repository_url>
cd <repository_name>/final_task/
pip3 install --user .
orsudo -H pip3 install .
$ pycalc --help
usage: pycalc [-h] EXPRESSION [-m MODULE [MODULE ...]]
Pure-python command-line calculator.
positional arguments:
EXPRESSION expression string to evaluate
optional arguments:
-h, --help show this help message and exit
-m MODULE [MODULE ...], --use-modules MODULE [MODULE ...]
additional modules to use
$ pycalc '2+2*2'
6
$ pycalc '2+sin(pi)^(2-cos(e))'
2.0
$ pycalc '5+3<=1'
False
$ pycalc 'e + pi + tau'
12.143059789228424
$ pycalc '1 + inf'
inf
$ pycalc '1 - inf'
-inf
$ pycalc 'inf - inf'
nan
$ pycalc 'nan == nan'
False
$ pycalc '15*(25+1'
ERROR: syntax error
15*(25+1
^
$ pycalc 'func'
ERROR: syntax error
func
^
$ pycalc '10 + 1/0 -3'
ERROR: division by zero
10 + 1/0 -3
^
$ pycalc '1 + sin(1,2) - 2'
ERROR: sin() takes exactly one argument (2 given)
1 + sin(1,2) - 2
^
$ pycalc '10^10^10'
ERROR: math range error
10^10^10
^
$ pycalc '(-1)^0.5'
ERROR: math domain error
(-1)^0.5
^
$ pycalc ''
ERROR: empty expression provided
$ pycalc '1514' -m fake calendar nonexistent time
ERROR: no module(s) named fake, nonexistent
# my_module.py
def sin(number):
return 42
$ pycalc 'sin(pi/2)'
1.0
$ pycalc 'sin(pi/2)' -m my_module
42
$ pycalc 'THURSDAY' -m calendar
3
$ pycalc 'sin(pi/2) - THURSDAY * 10' -m my_module calendar
12