Is their a better way of doing this? In my_function.py, I have the arguments mapped. The math program calls the print statements.
my_functions.py
#!/usr/bin/python3
import math
import operator
def get_truth(firstnum, op, secondnum):
rel_ops = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
'//': operator.floordiv,
'operator.add': operator.add,
'operator.sub': operator.sub,
'operator.mul': operator.mul,
'operator.truediv': operator.truediv,
'operator.floordiv': operator.floordiv,
'>': operator.gt,
'<': operator.lt,
'>=': operator.ge,
'<=': operator.le,
'==': operator.eq,
'!=': operator.ne,
'operator.gt': operator.gt,
'operator.lt': operator.lt,
'operator.ge': operator.ge,
'operator.le': operator.le,
'operator.eq': operator.eq,
'operator.ne': operator.ne
}
return rel_ops[op](firstnum, secondnum);
math program
#!/usr/bin/env python3
from my_functions import *
print('Testing MDAS')
print('17 + 9 =', get_truth(17, '+', 9))
print('17 - 9 =', get_truth(17, '-', 9))
print('17 * 9 =', get_truth(17, '*', 9))
print('17 / 9 =', round(get_truth(17, '/', 9), 4))
print('17 // 9 =', round(get_truth(17, '//', 9), 4))
print('')
print('17 operator.add 9 =', get_truth(17, 'operator.add', 9))
print('17 operator.sub 9 =', get_truth(17, 'operator.sub', 9))
print('17 operator.mul 9 =', get_truth(17, 'operator.mul', 9))
print('17 operator.truediv 9 =', round(get_truth(17, 'operator.truediv', 9), 4))
print('17 operator.floordiv 9 =', round(get_truth(17, 'operator.floordiv', 9), 4))
print('')
print('Testing truth or false')
print('1.0 > 0.0 is', get_truth(1.0, '>', 0.0))
print('1.0 < 0.0 is', get_truth(1.0, '<', 0.0))
print('1.0 >= 0.0 is', get_truth(1.0, '>=', 0.0))
print('1.0 <= 0.0 is', get_truth(1.0, '<=', 0.0))
print('1.0 == 0.0 is', get_truth(1.0, '==', 0.0))
print('1.0 != 0.0 is', get_truth(1.0, '!=', 0.0))
print('')
print('1.0 operator.gt 0.0 is', get_truth(1.0, 'operator.gt', 0.0))
print('1.0 operator.lt 0.0 is', get_truth(1.0, 'operator.lt', 0.0))
print('1.0 operator.ge 0.0 is', get_truth(1.0, 'operator.ge', 0.0))
print('1.0 operator.le 0.0 is', get_truth(1.0, 'operator.le', 0.0))
print('1.0 operator.eq 0.0 is', get_truth(1.0, 'operator.eq', 0.0))
print('1.0 operator.ne 0.0 is', get_truth(1.0, 'operator.ne', 0.0))
The Output
/home/michael/PycharmProjects/Arguments/venv/bin/python /home/michael/PycharmProjects/Arguments/arguments_relops.py
Testing MDAS
17 + 9 = 26
17 - 9 = 8
17 * 9 = 153
17 / 9 = 1.8889
17 // 9 = 1
17 operator.add 9 = 26
17 operator.sub 9 = 8
17 operator.mul 9 = 153
17 operator.truediv 9 = 1.8889
17 operator.floordiv 9 = 1
Testing truth or false
1.0 > 0.0 is True
1.0 < 0.0 is False
1.0 >= 0.0 is True
1.0 <= 0.0 is False
1.0 == 0.0 is False
1.0 != 0.0 is True
1.0 operator.gt 0.0 is True
1.0 operator.lt 0.0 is False
1.0 operator.ge 0.0 is True
1.0 operator.le 0.0 is False
1.0 operator.eq 0.0 is False
1.0 operator.ne 0.0 is True
Process finished with exit code 0