From f5046cde5a565bb438195ebbe8369d471052666e Mon Sep 17 00:00:00 2001 From: willian GL Date: Thu, 3 Oct 2019 22:54:18 -0300 Subject: [PATCH] Adding cli_calculator on project and also updating readme --- README.md | 9 +++++---- cli_calculator/README.md | 11 +++++++++++ cli_calculator/calc/__init__.py | 3 +++ cli_calculator/calc/args.py | 29 +++++++++++++++++++++++++++++ cli_calculator/calc/calc.py | 22 ++++++++++++++++++++++ cli_calculator/tests/test_calc.py | 28 ++++++++++++++++++++++++++++ 6 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 cli_calculator/README.md create mode 100644 cli_calculator/calc/__init__.py create mode 100644 cli_calculator/calc/args.py create mode 100644 cli_calculator/calc/calc.py create mode 100644 cli_calculator/tests/test_calc.py diff --git a/README.md b/README.md index 89b4a09..626f1e9 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ So far, the following projects have been integrated to this repo: |[Gmail Mailing Script](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/mailing) |[mayank-kapur](https://github.com/kapurm17) | |[Handwrting DNN recognizer](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Handwriting_Recognizer) |[Chris]() | |[HTML Table to List](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/HTML_Table_to_List) | [Nitish Srivastava](https://github.com/nitish-iiitd)| -|[Image circle formatter](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Image-Circulator) |[Berk Gureken](https://github.com/bureken) | +|[Image circle formatter](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Image-Circulator) |[Berk Gureken](https://github.com/bureken) | |[Image To PDF](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/images2pdf)|[msaoudallah](https://github.com/msaoudallah)| |[Instadp Web Scrapper](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/InstadpShower)|[Psychiquest](https://github.com/psychiquest)| |[Own IP locator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Location_Of_Own_IP_Adress)|[Chris]()| @@ -53,6 +53,7 @@ So far, the following projects have been integrated to this repo: |[Youtube video downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Youtube_Video_Downloader)|[Christopher He](https://github.com/hecris)| |[Zabbix API](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/zabbix_api)|[msg4sunny](https://github.com/msg4sunny)| |[Zip password cracker](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/zip_password_cracker)|[umar abdullahi](https://github.com/umarbrowser)| +|[CLI Calculator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/cli_calculator)|[Willian GL](https://github.com/williangl) | ## How to use : @@ -63,14 +64,14 @@ Remember to star the repo if you love the scipts~ :wink: ## Contribuition Guidelines : - Make a **separate folder** for your script. -- There shouldn't be any **spaces** between the names of the script. (Use underscore or dash Symbol) +- There shouldn't be any **spaces** between the names of the script. (Use underscore or dash Symbol) - :x: Script One - :heavy_check_mark: Script_One - :heavy_check_mark: Script-One - The Folder should contain the followings - - - Main Python Script, + - Main Python Script, - Supporting files for the Script (If any) - A separate `README.md` File with proper documentation. - + - Feel Free to add your script in the [project's list](https://github.com/hastagAB/Awesome-Python-Scripts#what-do-we-have) above. diff --git a/cli_calculator/README.md b/cli_calculator/README.md new file mode 100644 index 0000000..2d2955e --- /dev/null +++ b/cli_calculator/README.md @@ -0,0 +1,11 @@ +# calc_argparser +Calculadora via CLI + +# Instructions +To run CLI_Calculator execute: + +- `python args.py -h` - For help +- `python args.py --sum x y` - To sum two numbers +- `python args.py --sub x y` - To substraction two numbers +- `python args.py --mult x y` - To multiplication two numbers +- `python args.py --div x y` - To divide two numbers diff --git a/cli_calculator/calc/__init__.py b/cli_calculator/calc/__init__.py new file mode 100644 index 0000000..7383c1c --- /dev/null +++ b/cli_calculator/calc/__init__.py @@ -0,0 +1,3 @@ +from .calc import sub, mult, div, soma + +__all__ = ['sub', 'mult', 'div', 'soma'] diff --git a/cli_calculator/calc/args.py b/cli_calculator/calc/args.py new file mode 100644 index 0000000..f5c2d4f --- /dev/null +++ b/cli_calculator/calc/args.py @@ -0,0 +1,29 @@ +"""Calculadora utilizando ArgumentParser.""" + +from argparse import ArgumentParser + +from calc import soma, sub, mult, div + + +parser = ArgumentParser(description='Calculadora') + +parser.add_argument('--sum', help='Operação de soma', action='store_true') +parser.add_argument('--sub', help='Operação de subtração', action='store_true') +parser.add_argument('--mult', help='Operação de multiplicação', action='store_true') +parser.add_argument('--div', help='Operação de divisão', action='store_true') +parser.add_argument('x', type=int, help='Primeiro valor') +parser.add_argument('y', type=int, help='Segundo valor') + +args = parser.parse_args() + +if args.sum: + print(f'{soma(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/cli_calculator/calc/calc.py b/cli_calculator/calc/calc.py new file mode 100644 index 0000000..d424dbe --- /dev/null +++ b/cli_calculator/calc/calc.py @@ -0,0 +1,22 @@ + +def soma(x :int, y: int) -> int: + """Função de soma.""" + return x + y + + +def sub(x :int, y: int) -> int: + """Função de subtração.""" + return x - y + +def mult(x :int, y: int) -> int: + """Função de multiplicação.""" + return x * y + + +def div(x :int, y: int) -> int: + """Função de divisão.""" + try: + return x / y + + except ZeroDivisionError: + return 'Divisao por zero mal sucedida!!' diff --git a/cli_calculator/tests/test_calc.py b/cli_calculator/tests/test_calc.py new file mode 100644 index 0000000..13212e4 --- /dev/null +++ b/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))