Revision 13362ced
client/python/hlrc_client/RobotEmotion.py | ||
---|---|---|
36 | 36 |
SURPRISED = 4 |
37 | 37 |
FEAR = 5 |
38 | 38 |
|
39 |
def __init__(self, emotion = NEUTRAL): |
|
40 |
self.value = emotion |
|
41 |
self.time_ms = 1000 |
|
39 |
# mappings from values to strings and vice versa |
|
40 |
__value_as_str = dict(zip([NEUTRAL, HAPPY, SAD, ANGRY, SURPRISED, FEAR], |
|
41 |
['neutral', 'happy', 'sad', 'angry', 'surprised', 'fear'])) |
|
42 |
__str_as_value = dict([(s,v) for v,s in __value_as_str.iteritems()]) |
|
43 |
|
|
44 |
def __init__(self, emotion = NEUTRAL, duration = 1.): |
|
45 |
""" constructor |
|
46 |
param emotion: emotion constant |
|
47 |
param duration: duration of emotion in seconds |
|
48 |
""" |
|
49 |
try: |
|
50 |
self.value = int(emotion) |
|
51 |
try: |
|
52 |
self.__value_as_str[self.value] |
|
53 |
except KeyError: |
|
54 |
raise Exception('invalid emotion id: %d' % self.value) |
|
55 |
except ValueError: |
|
56 |
try: |
|
57 |
self.value = self.__str_as_value[emotion.lower()] |
|
58 |
except KeyError: |
|
59 |
raise Exception('invalid emotion: %s' % emotion) |
|
60 |
self.time_ms = int(1000*duration) # convert from s to ms |
|
42 | 61 |
|
43 | 62 |
def value_as_str(self): |
44 |
if (self.value == RobotEmotion.NEUTRAL): |
|
45 |
return "neutral" |
|
46 |
elif (self.value == RobotEmotion.HAPPY): |
|
47 |
return "happy" |
|
48 |
elif (self.value == RobotEmotion.SAD): |
|
49 |
return "sad" |
|
50 |
elif (self.value == RobotEmotion.ANGRY): |
|
51 |
return "angry" |
|
52 |
elif (self.value == RobotEmotion.SURPRISED): |
|
53 |
return "surprised" |
|
54 |
elif (self.value == RobotEmotion.FEAR): |
|
55 |
return "fear" |
|
56 |
else: |
|
63 |
try: |
|
64 |
return self.__value_as_str[self.value] |
|
65 |
except KeyError: |
|
57 | 66 |
return "INVALID EMOTION TYPE" |
58 | 67 |
|
59 | 68 |
def __str__(self): |
Also available in: Unified diff