Skip to content

Commit 4cb0bc4

Browse files
committed
Create pset7-P4-UserspecifeidTriggers.py
1 parent fe1cb83 commit 4cb0bc4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

pset7-P4-UserspecifeidTriggers.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# PART 4: USER-SPECIFIED TRIGGERS
2+
3+
def makeTrigger(triggerMap, triggerType, params, name):
4+
"""
5+
Takes in a map of names to trigger instance, the type of trigger to make,
6+
and the list of parameters to the constructor, and adds a new trigger
7+
to the trigger map dictionary.
8+
9+
triggerMap: dictionary with names as keys (strings) and triggers as values
10+
triggerType: string indicating the type of trigger to make (ex: "TITLE")
11+
params: list of strings with the inputs to the trigger constructor (ex: ["world"])
12+
name: a string representing the name of the new trigger (ex: "t1")
13+
14+
Modifies triggerMap, adding a new key-value pair for this trigger.
15+
16+
Returns a new instance of a trigger (ex: TitleTrigger, AndTrigger).
17+
"""
18+
19+
if triggerType == "TITLE":
20+
triggerMap[name] = TitleTrigger(params[0])
21+
22+
elif triggerType == "SUBJECT":
23+
triggerMap[name] = SubjectTrigger(params[0])
24+
25+
elif triggerType == "SUMMARY":
26+
triggerMap[name] = SummaryTrigger(params[0])
27+
28+
elif triggerType == "NOT":
29+
triggerMap[name] = NotTrigger(triggerMap[params[0]])
30+
31+
elif triggerType == "AND":
32+
triggerMap[name] = AndTrigger(triggerMap[params[0]], triggerMap[params[1]])
33+
34+
elif triggerType == "OR":
35+
triggerMap[name] = OrTrigger(triggerMap[params[0]], triggerMap[params[1]])
36+
37+
elif triggerType == "PHRASE":
38+
triggerMap[name] = PhraseTrigger(' '.join(params))
39+
return triggerMap[name]
40+

0 commit comments

Comments
 (0)