-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent-driven3.py
65 lines (54 loc) · 1.88 KB
/
event-driven3.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
"""
# Define list of events and what should be implemented when that event happens.
# This list could be origninally an empty dictionary (events = {}).
events = {
"event1": [func1(), func2(), func3()],
"event2": [func2()],
"event3": [func3(), func1(), func3()]
}
Each of these functions are 'subscribers' or 'handlers' of each event.
For instance, func1() is subscribed to event1 and event3.
Ali Dadgar - 2020
"""
events = {}
""" function that registers an event """
# Receives a function and updates 'events' dictionary.
# This is basically subscribing an event
def register(event: str, function: callable):
if not event in events:
# If event is not in the list -> register it.
events[event] = [function]
else:
# The event is already exits -> append to the list.
events[event].append(function)
""" function to dispatch an event """
# Dispatch: mokhabere kardan
# Implements the requested 'event' using the passed 'data'.
# 'data' could be **kwargs as well. This might decrease functions' significance in large projects.
def dispatch(event: str, data):
if not event in events:
# If the requested 'event' does not exist -> we don't need to do anything.
return
else:
# The 'event' exists -> implement it (post the event)
for handler in events[event]:
handler(data)
# NOTE:
# Following are different handlers, that handle required events.
""" Event: puts robot in zero (start) position """
def robot_zero():
pass
""" Event: puts the robot in ground position """
def robot_grount_position():
pass
""" Event: turn on the camera """
def camera_on():
pass
""" Event: turn off the camera """
def camera_off():
pass
def main():
# register required events
register('robotStartPosition', robot_zero)
register('cameraOn', camera_on)
register('robotGroundPosition', robot_grount_position)