-
Notifications
You must be signed in to change notification settings - Fork 175
/
sleep.py
50 lines (43 loc) · 1.48 KB
/
sleep.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
from actions.action import Action
class SleepAction(Action):
"""
Defines the SleepAction - causes the engine to pause before sending a packet.
"""
# Do not select the sleep action during evolutions
frequency = 0
def __init__(self, time=1, environment_id=None):
"""
Initializes the sleep action.
Args:
time (float): How much time the packet should delay before sending
environment_id (str, optional): Environment ID of the strategy this action is a part of
"""
Action.__init__(self, "sleep", "out")
self.terminal = False
self.branching = False
self.time = time
def run(self, packet, logger):
"""
The sleep action simply passes along the packet it was given with an instruction for how long the engine should sleep before sending it.
"""
logger.debug(" - Adding %g sleep to given packet." % self.time)
packet.sleep = self.time
return packet, None
def __str__(self):
"""
Returns a string representation.
"""
s = Action.__str__(self)
s += "{%g}" % self.time
return s
def parse(self, string, logger):
"""
Parses a string representation for this object.
"""
try:
if string:
self.time = float(string)
except ValueError:
logger.exception("Cannot parse time %s" % string)
return False
return True