Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1032 - Argparser calculator #1044

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BasicPythonScripts/CLI Calculator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*/__pycache__
66 changes: 66 additions & 0 deletions BasicPythonScripts/CLI Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# CLI Calculator

## Aim

Calculator via CLI made with Python built-in Argparser


## Purpose

Learn tests and Python Argparser


## Short description of package/script

To run CLI Calculator execute:

- `python calc/args.py --help` - For help
- `python calc/args.py --sum x y` - To sum two numbers
- `python calc/args.py --sub x y` - To subtract two numbers
- `python calc/args.py --mult x y` - To multiply two numbers
- `python calc/args.py --div x y` - To divide two numbers


To run tests execute:
```shell
python -m unittest discover tests
```


## Workflow of the Project

You can execute `python calc/args.py --help` to check all available commands


## Setup instructions

This project is using a Python built-in library, no external package is needed


## Compilation Steps

This project doesn't need to be compiled.


## Output

**Sum function**
![calc_argparser](./calc_argparser.png)

**Help**
![calc_argparser_help](./calc_argparser_helper.png)


## Conclusion

This project helped me to know new functionalities of Python and understand the unittest skeleton


## Author(s)

Willian Lopes


## Disclaimers, if any

Use this section to mention if any particular disclaimer is required
3 changes: 3 additions & 0 deletions BasicPythonScripts/CLI Calculator/calc/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .calc import sub, mult, div, sum_

__all__ = ['sub', 'mult', 'div', 'sum_']
29 changes: 29 additions & 0 deletions BasicPythonScripts/CLI Calculator/calc/args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""CLI Calculator using ArgumentParser."""

from argparse import ArgumentParser

from calc import sum_, sub, mult, div


parser = ArgumentParser(description='Calculator')

parser.add_argument('--sum', help='Sum operation', action='store_true')
parser.add_argument('--sub', help='Subtraction operation', action='store_true')
parser.add_argument('--mult', help='Multiplication operation', action='store_true')
parser.add_argument('--div', help='Division operation', action='store_true')
parser.add_argument('x', type=int, help='First value')
parser.add_argument('y', type=int, help='Second value')

args = parser.parse_args()

if args.sum:
print(f'{sum_(args.x, args.y)}')

if args.sub:
print(f'{sub(args.x, args.y)}')

if args.mult:
print(f'{mult(args.x, args.y)}')

if args.div:
print(f'{div(args.x, args.y)}')
24 changes: 24 additions & 0 deletions BasicPythonScripts/CLI Calculator/calc/calc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@


def sum_(x: int, y: int) -> int:
"""Sum function."""
return x + y


def sub(x: int, y: int) -> int:
"""Substration function."""
return x - y


def mult(x: int, y: int) -> int:
"""Multiplication function."""
return x * y


def div(x: int, y: int) -> int:
"""Division function."""
try:
return x / y

except ZeroDivisionError:
return "Division by zero isn't possible!!"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions BasicPythonScripts/CLI Calculator/tests/test_calc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from unittest import TestCase

from calc import soma, sub, mult, div


class testCalc(TestCase):
def test_should_return_two_values_sum(self):
esperado = 1 + 2
self.assertEqual(esperado, soma(1,2))


def test_should_return_two_values_sub(self):
esperado = 1 - 2
self.assertEqual(esperado, sub(1,2))


def test_should_return_two_values_mult(self):
esperado = 1 * 2
self.assertEqual(esperado, mult(1,2))

def test_should_return_two_values_div(self):
esperado = 1 / 2
self.assertEqual(esperado, div(1,2))


def test_should_return_exceptio_on_division_by_zero(self):
esperado = 'Divisao por zero mal sucedida!!'
self.assertEqual(esperado, div(1,0))
3 changes: 1 addition & 2 deletions BasicPythonScripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@
- [Stock Visualizer](https://github.com/prathimacode-hub/Awesome_Python_Scripts/tree/main/BasicPythonScripts/Stock%20Visualiser)
- [Stone Paper Pencil Rubber Scissor](https://github.com/prathimacode-hub/Awesome_Python_Scripts/tree/main/BasicPythonScripts/Stone%20Paper%20Pencil%20Rubber%20Scissor)
- [Word Cloud](https://github.com/prathimacode-hub/Awesome_Python_Scripts/tree/main/BasicPythonScripts/Word%20Cloud)


- [CLI Calculator](https://github.com/prathimacode-hub/Awesome_Python_Scripts/tree/main/BasicPythonScripts/CLI%20Calculator)