Myo Utils (myo.utils)

myo.enum

This module provides an Enumeration class to easily implement enumerations in Python. If any non-number data should be added to an enumeration class (eg. a string constant or function), it should be wrapped with the Enumeration.Data class.

class Color(Enumeration):
    red = 1
    green = 3
    blue = 2

    @Enumeration.Data
    @staticmethod
    def get_random():
        return random.choice([Color.red, Color.green, Color.blue])

Enumeration values can be compared directly or by name, but not by their value. Their value can be retrieved using :func`int`. An enumeration value can be used for indexing or passed to a ctypes function as an integer.

print Color.red == 'red'         # True
print Color.red == Color.red     # True
print Color.red == 1             # False
print ['Foo', 'Bar'][Color.red]  # Bar
ctypes.cdll.some_lib.func(Color.red)
class myo.utils.enum.Data(value)

Small class that can be used to specify data on an enumeration that should not be converted and interpreted as an enumeration value.

class myo.utils.enum.Enumeration

This is the base class for listing enumerations. All 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.

An Enumeration object without a name is invalid. This can only be the case when __fallback__ was set to True on the Enumeration class.

class Data(value)

Small class that can be used to specify data on an enumeration that should not be converted and interpreted as an enumeration value.

class myo.utils.enum.EnumerationMeta

This is the meta class for the Enumeration base class which handles the automatic conversion of integer values to instances of the Enumeration class. There are no other types allowed other than int or Data which will be unpacked on the Enumeration class.

If an __fallback__ was defined on class-level as an integer, the Enumeration constructor will not raise a NoSuchEnumerationValue exception if the passed value did not match the enumeration values, but instead return that fallback value.

This fallback is not taken into account when attempting to create a new Enumeration object by a string.

exception myo.utils.enum.NoSuchEnumerationValue

Raised when an Enumeration object was attempted to be created from an integer value but there was no enumeration object for this value.

Note that you can specify __fallback_value__ on an Enumeration class to not let it raise an exception.

myo.macaddr

Provides an immutable MacAddress class.

class myo.utils.macaddr.MacAddress(value)

This class represents an immutable MAC address.

static int_to_string(x)

Converts x being an integral number to a string MAC address. Raises a ValueError if x is a negative number or exceeds the MAC address range.

static string_to_int(s)

Converts s being a string MAC address to an integer version. Raises a ValueError if the string is not a valid MAC address.

myo.platform

Detects the current platform and exposes it as platform and arch members. arch can be either 'x64' or 'x86' and platform can be one of the following:

  • 'Windows'
  • 'Windows (Cygwin)
  • 'Darwin'

If the platform is not supported, EnvironmentError is raised when the module is loaded.

myo.threading

class myo.utils.threading.TimeoutClock(timeout)

This is a utility class to compute the time that should be passed to a Condition variable that could be notified before the timeout exceeded and before the actual data is available.

If timeout is None, the TimeoutClock will always return False when retrieving exceeded.

timer = TimeoutClock(timeout)
with condition:
    while not timer.exceeded and not data_is_available():
        condition.wait(timer.remaining)
    if data_is_available():
        return get_data()
    else:
        # timeout exceeded
exceeded

Returns True if the timeout is exceeded, False if not. Will always return False if the TimeoutClock was initialized with None.

passed

Returns the time passed since the creation of the timer. This always functions, even if the TimeoutClock was initialized with None.

remaining

Returns the time that is remaining to be waited that should be passed to the Condition variables wait() method in the loop. Returns None if the TimeoutClock was initialized with None.

myo.tools

class myo.utils.tools.ShortcutAccess(x, prefix)

Wrapper for any kind of object to make the access to attributes easier. Prefixes all accssed attribute names with the string supplied upon construction.