Merge pull request #77 from williangl/master

Adding cli_calculator on project
This commit is contained in:
Ayush Bhardwaj 2019-10-04 11:04:25 +05:30 committed by GitHub
commit ddddfe1162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 94 additions and 0 deletions

View File

@ -55,6 +55,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)| |[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)| |[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)| |[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) |
|[Find PhoneNumber in String](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Find-PhoneNumber-in-String)|[Austin Zuniga](https://github.com/AustinZuniga)| |[Find PhoneNumber in String](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Find-PhoneNumber-in-String)|[Austin Zuniga](https://github.com/AustinZuniga)|
## How to use : ## How to use :

11
cli_calculator/README.md Normal file
View File

@ -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

View File

@ -0,0 +1,3 @@
from .calc import sub, mult, div, soma
__all__ = ['sub', 'mult', 'div', 'soma']

View File

@ -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)}')

View File

@ -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!!'

View File

@ -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))