Skip to content

Commit

Permalink
Enumerations with __fallback__ now store the invalid value
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasRosenstein committed Oct 9, 2015
1 parent 50965e6 commit 0c8bd78
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
5 changes: 3 additions & 2 deletions myo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,10 @@ def _(name, *args, **kwargs):
result = result and _('on_emg_data', event.emg)
elif kind == EventType.warmup_completed:
result = result and _('on_warmup_completed', event.warmup_result)
elif kind.name:
warnings.warn('unhandled myo.EventType: {0}'.format(kind.name), RuntimeWarning)
else:
warnings.warn('unknown myo.EventType, maybe you have a new Myo SDK that '
'introduced new event types or you need to update myo-python', RuntimeWarning)
warnings.warn('unknown myo.EventType: {0}'.format(kind.value), RuntimeWarning)

if not _('on_event_finished', kind, event, defaults=False):
result = False
Expand Down
30 changes: 15 additions & 15 deletions myo/lowlevel/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ class Result(Enumeration):
error = 1
error_invalid_argument = 2
error_runtime = 3
__fallback__ = -1
__fallback__ = True


class VibrationType(Enumeration):
short = 0
medium = 1
long = 2
__fallback__ = -1
__fallback__ = True


class StreamEmg(Enumeration):
disabled = 0
enabled = 1
__fallback__ = -1
__fallback__ = True


class Pose(Enumeration):
Expand All @@ -60,7 +60,7 @@ class Pose(Enumeration):
wave_out = 3
fingers_spread = 4
double_tap = 5
__fallback__ = -1
__fallback__ = True
num_poses = Enumeration.Data(6)


Expand All @@ -79,73 +79,73 @@ class EventType(Enumeration):
emg = 11
bettery_level = 12
warmup_completed = 13
__fallback__ = -1
__fallback__ = True


class VersionComponent(Enumeration):
major = 0
minor = 1
patch = 2
__fallback__ = -1
__fallback__ = True


class OrientationIndex(Enumeration):
x = 0
y = 1
z = 2
w = 3
__fallback__ = -1
__fallback__ = True


class HandlerResult(Enumeration):
continue_ = 0
stop = 1
__fallback__ = -1
__fallback__ = True


class LockingPolicy(Enumeration):
none = 0 # Pose events are always sent.
standard = 1 # (default) Pose events are not sent while a Myo is locked.
__fallback__ = -1
__fallback__ = True


class Arm(Enumeration):
right = 0
left = 1
unknown = 2
__fallback__ = -1
__fallback__ = True


class XDirection(Enumeration):
toward_wrist = 0
toward_elbow = 1
unknown = 2
__fallback__ = -1
__fallback__ = True


class UnlockType(Enumeration):
timed = 0
hold = 1
__fallback__ = -1
__fallback__ = True


class UserActionType(Enumeration):
single = 0
__fallback__ = -1
__fallback__ = True


class WarmupState(Enumeration):
unknown = 0
cold = 1
warm = 2
__fallback__ = -1
__fallback__ = True


class WarmupResult(Enumeration):
unknown = 0
success = 1
failed_timeout = 2
__fallback__ = -1
__fallback__ = True


# Backwards compatibility
Expand Down
53 changes: 35 additions & 18 deletions myo/utils/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,17 @@ def __new__(cls, name, bases, data):
# class.
for key, value in enum_values.items():

# Create the new object. We must not use the classes'
# __new__() method as it resolves the object from the
# existing values.
obj = object.__new__(class_)
object.__init__(obj)

obj.value = value
obj.name = key

if key == '__fallback__':
obj.name = '-invalid-'
if key == '__fallback__' and value:
obj = value = True
else:
# Create the new object. We must not use the classes'
# __new__() method as it resolves the object from the
# existing values.
obj = object.__new__(class_)
obj._Enumeration__init(key, value)
class_._values[value] = obj

setattr(class_, key, obj)

return class_
Expand All @@ -154,7 +152,11 @@ class Enumeration(six.with_metaclass(EnumerationMeta)):
components of the class that are integers will be automatically
converted to instances of the Enumeration class. Creating new
instances of the class will only work if the value is an existing
enumeration value. """
enumeration value.
An Enumeration object without a `name` is invalid. This can only
be the case when `__fallback__` was set to True on the Enumeration
class. """

def __new__(cls, value, _allow_fallback=True):
r""" Creates a new instance of the Enumeration. *value* must
Expand All @@ -173,8 +175,10 @@ def __new__(cls, value, _allow_fallback=True):

# If a fallback value was specified, use it
# instead of raising an exception.
if _allow_fallback and cls.__fallback__ is not None:
return cls.__fallback__
if _allow_fallback and cls.__fallback__:
obj = object.__new__(cls)
obj.__init(None, value)
return obj

raise NoSuchEnumerationValue(cls.__name__, value)

Expand All @@ -194,7 +198,14 @@ def __new__(cls, value, _allow_fallback=True):
if type(value) == cls:
return value

raise TypeError('value must be %s or int' % cls.__name__)
raise TypeError('value must be %s, string or int' % cls.__name__)

def __init(self, name, value):
super(Enumeration, self).__init__()
self.name = name
self.value = value
if not isinstance(value, int):
raise TypeError('enumeration value must be int')

def __hash__(self):
return hash(self.value)
Expand All @@ -213,12 +224,18 @@ def __int__(self):
return self.value

def __str__(self):
class_name = self.__class__.__name__
return '<%s: %s>' % (class_name, self.name)
class_name = type(self).__name__
if self.name:
return '<%s: %s>' % (class_name, self.name)
else:
return '<%s: {invalid:%d}>' % (class_name, self.value)

def __repr__(self):
class_name = self.__class__.__name__
return '<%s: [%d] %s>' % (class_name, self.value, self.name)
class_name = type(self).__name__
if self.name:
return '<%s: [%d] %s>' % (class_name, self.value, self.name)
else:
return '<%s: {invalid:%d}>' % (class_name, self.value)

def __index__(self):
return self.value
Expand Down

0 comments on commit 0c8bd78

Please sign in to comment.