-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_test.py
More file actions
71 lines (59 loc) · 2.42 KB
/
local_test.py
File metadata and controls
71 lines (59 loc) · 2.42 KB
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
import random
import sys
import re
# functions = {
# "filter": (lambda x: int(x) if x.match(r"\d",x) else None),
# "splitter": (lambda x: "mul" if x%2==0 else "sum"),
# # "dehydrate": ()
# "mul": (lambda x: x[0]*x[1]),
# "sum": (lambda x: x[0]+x[1]),
# }
# functions = {
# "spout": ("split", random.choice(["a a a a a",
# "how much wood would the woodchuck chuck if the wouldchuck could chuck wood" ,
# "the cow jumped over the moon", "an apple a day keeps the doctor away",
# "four score and seven years ago",
# "snow white and the seven dwarfs",
# "i am at two with nature"])),
# "split": (lambda sentence: [("count",x) for x in sentence[0].split(' ')] ),
# "count": (lambda inputs: (counts.update({inputs[0]:counts.get(inputs[0], 0)+1}))),
# }
# functions = {
# "spout": ("split", "how much wood would the woodchuck chuck if the wouldchuck could chuck wood"),
# "split": (lambda sentence: [("count",x) for x in sentence[0].split(' ')] ),
# "count": (lambda inputs: (counts.update({inputs[0]:counts.get(inputs[0], 0)+1}))),
# }
counts = {}
def run(functions, inputs):
if isinstance(inputs, basestring):
run(functions,functions.get(inputs))
elif isinstance(inputs, tuple):
run(functions,functions.get(inputs[0])(inputs[1:]))
elif isinstance(inputs, list):
for tup in inputs:
run(functions,tup)
if __name__ == "__main__":
# args = sys.argv[1:]
request = (None,"","""{
"spout": ("split", "how much wood would the woodchuck chuck if the wouldchuck could chuck wood"),
"split": (lambda sentence: [("count",x) for x in sentence[0].split(' ')] ),
"count": (lambda inputs: (counts.update({inputs[0]:counts.get(inputs[0], 0)+1}))),
}
""")
# request = """{
# "spout": ("split", random.choice(["a a a a a",
# "how much wood would the woodchuck chuck if the wouldchuck could chuck wood" ,
# "the cow jumped over the moon", "an apple a day keeps the doctor away",
# "four score and seven years ago",
# "snow white and the seven dwarfs",
# "i am at two with nature"])),
# "split": (lambda sentence: [("count",x) for x in sentence[0].split(' ')] ),
# "count": (lambda inputs: (counts.update({inputs[0]:counts.get(inputs[0], 0)+1}))),
# }
# """
functions = eval(request[2])
if request[0] is None:
run(functions,"spout")
else:
run(functions,request[0])(eval(request[1]))
print(counts)