Code Monkey home page Code Monkey logo

epam-2019-python-homework's Introduction

pycalc

Build Status
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.

Features

pycalc supports:

  • arithmetic operations (+, -, *, /, //, %, ^ (^ is a power));
  • comparison operations (<, <=, ==, !=, >=, >);
  • 2 built-in python functions: abs and round;
  • 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.

How to install

  1. git clone <repository_url>
  2. cd <repository_name>/final_task/
  3. pip3 install --user . or sudo -H pip3 install .

Examples

Command line interface:

$ 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

Calculation:

$ 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

Errors:

$ 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

Additional modules:

# 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

References

epam-2019-python-homework's People

Contributors

siarhiejkresik avatar aliakseibuziuma avatar pythonandgocourses2 avatar

Stargazers

Andrew Johnson avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.