diff --git a/BasicPythonScripts/CLI Calculator/.gitignore b/BasicPythonScripts/CLI Calculator/.gitignore new file mode 100644 index 000000000..4bae5a6a8 --- /dev/null +++ b/BasicPythonScripts/CLI Calculator/.gitignore @@ -0,0 +1 @@ +*/__pycache__ diff --git a/BasicPythonScripts/CLI Calculator/README.md b/BasicPythonScripts/CLI Calculator/README.md new file mode 100644 index 000000000..aab3c3326 --- /dev/null +++ b/BasicPythonScripts/CLI Calculator/README.md @@ -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 diff --git a/BasicPythonScripts/CLI Calculator/calc/__init__.py b/BasicPythonScripts/CLI Calculator/calc/__init__.py new file mode 100644 index 000000000..6ee7e5652 --- /dev/null +++ b/BasicPythonScripts/CLI Calculator/calc/__init__.py @@ -0,0 +1,3 @@ +from .calc import sub, mult, div, sum_ + +__all__ = ['sub', 'mult', 'div', 'sum_'] diff --git a/BasicPythonScripts/CLI Calculator/calc/args.py b/BasicPythonScripts/CLI Calculator/calc/args.py new file mode 100644 index 000000000..1d742d4bb --- /dev/null +++ b/BasicPythonScripts/CLI Calculator/calc/args.py @@ -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)}') diff --git a/BasicPythonScripts/CLI Calculator/calc/calc.py b/BasicPythonScripts/CLI Calculator/calc/calc.py new file mode 100644 index 000000000..97004cd27 --- /dev/null +++ b/BasicPythonScripts/CLI Calculator/calc/calc.py @@ -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!!" diff --git a/BasicPythonScripts/CLI Calculator/calc_argparser.png b/BasicPythonScripts/CLI Calculator/calc_argparser.png new file mode 100644 index 000000000..e676c2cb1 Binary files /dev/null and b/BasicPythonScripts/CLI Calculator/calc_argparser.png differ diff --git a/BasicPythonScripts/CLI Calculator/calc_argparser_help.png b/BasicPythonScripts/CLI Calculator/calc_argparser_help.png new file mode 100644 index 000000000..0ca73ba5d Binary files /dev/null and b/BasicPythonScripts/CLI Calculator/calc_argparser_help.png differ diff --git a/BasicPythonScripts/CLI Calculator/tests/test_calc.py b/BasicPythonScripts/CLI Calculator/tests/test_calc.py new file mode 100644 index 000000000..13212e4cc --- /dev/null +++ b/BasicPythonScripts/CLI Calculator/tests/test_calc.py @@ -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)) diff --git a/BasicPythonScripts/README.md b/BasicPythonScripts/README.md index 4b821c921..206c0b54c 100644 --- a/BasicPythonScripts/README.md +++ b/BasicPythonScripts/README.md @@ -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)