-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrule_engine.py
88 lines (62 loc) · 2.14 KB
/
rule_engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Write a simple rules engine in Python.
It consists of a daemon script that accepts a keyword and a record
associated with the keyword as input in a loop, on receiving an input it calls
a list of functions that are mapped to the keyword and prints the result.
The mapping of keyword with target functions can be fed at the startup of
the script.
Test mapping (keyword -> list of functions):
{
'sum': ['log', 'sum'],
'sqrt': ['log', 'square_root'],
'division': ['log', 'square_root'],
'greet': ['greet_good_x']
}
Sample input:
division 128 16
division 67 0
greet John Doe
Expected output:
8
integer division or modulo by zero
Good afternoon John Doe!
Use exception handling. Create a custom exception called ActionException which
will be raised by actions (functions).
ActionException will have the reference of the function that raised an error
and the error message.
The main script will catch these exception and print it on stdout.
"""
set_of_rule = {
'sum': ['log', 'sum'],
'sqrt': ['log', 'square_root'],
'division': ['log', 'division'],
'greet': ['greet_good_x']
}
class ActionException(Exception):
def __init__(self, arg):
self.strerror = arg
self.args = {arg}
def sum(a, b):
return "Sum {} + {} = {}".format(a, b, int(a) + int(b))
def greet_good_x(a, b):
return "Good afternoon {} {}!".format(a, b)
def division(a, b):
return "Divison {} / {} = {}".format(a, b, int(a) / int(b))
def square_root(a, b):
return "Sqrt {} ** {} = {}".format(
a, 1 / float(b), float(a) ** (1 / float(b))
)
def main():
while True:
input_value = raw_input(
"Enter input in following format:\nkeyword input1 input2 (space seperated):\n"
).split(' ')
if len(input_value) != 3:
raise ActionException("keyword input1 input2 (space seperated)")
keyword, a, b = input_value
if not set_of_rule.get(keyword):
raise ActionException("Enter wrong keyword")
print eval(set_of_rule[keyword][-1] + "(a,b)")
print "----------------------------------"
if __name__ == "__main__":
main()