Math
Sample Commands
on-math --multiply -x 7 -y 9
Source code in linecaspy/math/math.py
class MathClass:
def __init__(self):
pass
def add(self, *args):
"""Add numbers
Args:
args (tuple): of integers
Returns:
int: Of adding numbers
"""
logger.debug(f"args is of type: {type(args)} with data: {args}")
self.type_check(args)
total = 0
for arg in args:
total = total + int(arg)
logger.info(f"Addition result: {total}")
return total
def divide(self, *args):
"""Divides numbers
Args:
args (tuple): of integers
Returns:
int: Of divided numbers
"""
logger.debug(f"args is of type: {type(args)} with data: {args}")
self.type_check(args)
index = 0
for arg in args:
if index == 0:
total = int(arg)
else:
total = total / int(arg)
index += 1
logger.info(f"Division result: {round(total, 2)}")
return total
def multiply(self, *args):
"""Multiplies numbers
Args:
args (tuple): of integers
Returns:
int: Of multiplying numbers
"""
logger.debug(f"args is of type: {type(args)} with data: {args}")
self.type_check(args)
total = 1
for arg in args:
total = total * int(arg)
logger.info(f"Multiplication result: {total}")
return total
def subtract(self, *args):
"""Subtract numbers
Args:
args (tuple): of integers
Returns:
int: Of subtractd numbers
"""
logger.debug(f"args is of type: {type(args)} with data: {args}")
self.type_check(args)
index = 0
for arg in args:
if index == 0:
total = int(arg)
else:
total = total - int(arg)
index += 1
logger.info(f"Subtraction result: {total}")
return total
def type_check(self, item):
"""Checks that input is integers
Args:
item (tuple): Tuple of integers or single integer
"""
logger.debug(f"item is of type: {type(item)} with data: {item}")
if isinstance(item, tuple):
for i in item:
try:
int(i)
except ValueError:
# raise TypeError(f"{i} is not an integer in tuple: {item}")
logger.error(f"{i} is not an integer in tuple: {item}")
sys.exit(1)
return
if not isinstance(item, int):
# raise TypeError(f"{item} is not an integer")
logger.error(f"{item} is not an integer")
sys.exit(1)
add(self, *args)
Add numbers
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args |
tuple |
of integers |
() |
Returns:
| Type | Description |
|---|---|
int |
Of adding numbers |
Source code in linecaspy/math/math.py
def add(self, *args):
"""Add numbers
Args:
args (tuple): of integers
Returns:
int: Of adding numbers
"""
logger.debug(f"args is of type: {type(args)} with data: {args}")
self.type_check(args)
total = 0
for arg in args:
total = total + int(arg)
logger.info(f"Addition result: {total}")
return total
divide(self, *args)
Divides numbers
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args |
tuple |
of integers |
() |
Returns:
| Type | Description |
|---|---|
int |
Of divided numbers |
Source code in linecaspy/math/math.py
def divide(self, *args):
"""Divides numbers
Args:
args (tuple): of integers
Returns:
int: Of divided numbers
"""
logger.debug(f"args is of type: {type(args)} with data: {args}")
self.type_check(args)
index = 0
for arg in args:
if index == 0:
total = int(arg)
else:
total = total / int(arg)
index += 1
logger.info(f"Division result: {round(total, 2)}")
return total
multiply(self, *args)
Multiplies numbers
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args |
tuple |
of integers |
() |
Returns:
| Type | Description |
|---|---|
int |
Of multiplying numbers |
Source code in linecaspy/math/math.py
def multiply(self, *args):
"""Multiplies numbers
Args:
args (tuple): of integers
Returns:
int: Of multiplying numbers
"""
logger.debug(f"args is of type: {type(args)} with data: {args}")
self.type_check(args)
total = 1
for arg in args:
total = total * int(arg)
logger.info(f"Multiplication result: {total}")
return total
subtract(self, *args)
Subtract numbers
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args |
tuple |
of integers |
() |
Returns:
| Type | Description |
|---|---|
int |
Of subtractd numbers |
Source code in linecaspy/math/math.py
def subtract(self, *args):
"""Subtract numbers
Args:
args (tuple): of integers
Returns:
int: Of subtractd numbers
"""
logger.debug(f"args is of type: {type(args)} with data: {args}")
self.type_check(args)
index = 0
for arg in args:
if index == 0:
total = int(arg)
else:
total = total - int(arg)
index += 1
logger.info(f"Subtraction result: {total}")
return total
type_check(self, item)
Checks that input is integers
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item |
tuple |
Tuple of integers or single integer |
required |
Source code in linecaspy/math/math.py
def type_check(self, item):
"""Checks that input is integers
Args:
item (tuple): Tuple of integers or single integer
"""
logger.debug(f"item is of type: {type(item)} with data: {item}")
if isinstance(item, tuple):
for i in item:
try:
int(i)
except ValueError:
# raise TypeError(f"{i} is not an integer in tuple: {item}")
logger.error(f"{i} is not an integer in tuple: {item}")
sys.exit(1)
return
if not isinstance(item, int):
# raise TypeError(f"{item} is not an integer")
logger.error(f"{item} is not an integer")
sys.exit(1)