def block_resource_fitnesses(self, block: block.Block):
        """Returns a map of nodename to average fitness value for this block.
        Assumes that required resources have been checked on all nodes."""

        # Short-circuit! My algorithm is terrible, so it doesn't work well for the edge case where
        # the block has no requirements
        if not block.resources:
            return {n: 1 for n in self.config.nodes.keys()}

        node_fitnesses = {}

        for resource in block.resources:
            resource_fitnesses = self.resource_fitnesses(resource)

            if not resource_fitnesses:
                raise UnassignableBlock(block.name)

            max_fit = max(resource_fitnesses.values())
            min_fit = min(resource_fitnesses.values())

            for node, fitness in resource_fitnesses.items():
                if node not in node_fitnesses:
                    node_fitnesses[node] = {}

                if not fitness:
                    # Since we're rescaling, 0 is now an OK value...
                    # We will check for `is False` after this
                    node_fitnesses[node][resource.describe()] = False
                else:
                    if max_fit - min_fit:
                        node_fitnesses[node][resource.describe()] = (fitness - min_fit) / (max_fit - min_fit)
                    else:
                        # All the values are the same, default to 1
                        node_fitnesses[node][resource.describe()] = 1.0

        res = {}

        for node, res_fits in node_fitnesses.items():
            fit_sum = 0
            for res_desc, fit in res_fits.items():
                if fit is False:
                    fit_sum = False
                    break

                fit_sum += fit

            if fit_sum is False:
                # Skip this node entirely
                res[node] = False
                continue

            res[node] = fit_sum

        return res

def block_resource_fitnesses ( self , block : block . Block ) : if not block . resources : return { n : 1 for n in self . config . nodes . keys ( ) } node_fitnesses = { } for resource in block . resources : resource_fitnesses = self . resource_fitnesses ( resource ) if not resource_fitnesses : raise UnassignableBlock ( block . name ) max_fit = max ( resource_fitnesses . values ( ) ) min_fit = min ( resource_fitnesses . values ( ) ) for node , fitness in resource_fitnesses . items ( ) : if node not in node_fitnesses : node_fitnesses [ node ] = { } if not fitness : node_fitnesses [ node ] [ resource . describe ( ) ] = False else : if max_fit - min_fit : node_fitnesses [ node ] [ resource . describe ( ) ] = ( fitness - min_fit ) / ( max_fit - min_fit ) else : node_fitnesses [ node ] [ resource . describe ( ) ] = 1.0 res = { } for node , res_fits in node_fitnesses . items ( ) : fit_sum = 0 for res_desc , fit in res_fits . items ( ) : if fit is False : fit_sum = False break fit_sum += fit if fit_sum is False : res [ node ] = False continue res [ node ] = fit_sum return res

Returns a map of nodename to average fitness value for this block . Assumes that required resources have been checked on all nodes .


def available_drivers():
    """Returns a list of available drivers names.
    """
    global __modules
    global __available

    if type(__modules) is not list:
        __modules = list(__modules)

    if not __available:
        __available = [d.ahioDriverInfo.NAME
                       for d in __modules
                       if d.ahioDriverInfo.AVAILABLE]

    return __available

def available_drivers ( ) : global __modules global __available if type ( __modules ) is not list : __modules = list ( __modules ) if not __available : __available = [ d . ahioDriverInfo . NAME for d in __modules if d . ahioDriverInfo . AVAILABLE ] return __available

Returns a list of available drivers names .


def map_pin(self, abstract_pin_id, physical_pin_id):
        """Maps a pin number to a physical device pin.

        To make it easy to change drivers without having to refactor a lot of
        code, this library does not use the names set by the driver to identify
        a pin. This function will map a number, that will be used by other
        functions, to a physical pin represented by the drivers pin id. That
        way, if you need to use another pin or change the underlying driver
        completly, you only need to redo the mapping.

        If you're developing a driver, keep in mind that your driver will not
        know about this. The other functions will translate the mapped pin to
        your id before calling your function.

        @arg abstract_pin_id the id that will identify this pin in the
        other function calls. You can choose what you want.

        @arg physical_pin_id the id returned in the driver.
            See `AbstractDriver.available_pins`. Setting it to None removes the
            mapping.
        """
        if physical_pin_id:
            self._pin_mapping[abstract_pin_id] = physical_pin_id
        else:
            self._pin_mapping.pop(abstract_pin_id, None)

def map_pin ( self , abstract_pin_id , physical_pin_id ) : if physical_pin_id : self . _pin_mapping [ abstract_pin_id ] = physical_pin_id else : self . _pin_mapping . pop ( abstract_pin_id , None )

Maps a pin number to a physical device pin .


def set_pin_direction(self, pin, direction):
        """Sets pin `pin` to `direction`.

        The pin should support the requested mode. Calling this function
        on a unmapped pin does nothing. Calling it with a unsupported direction
        throws RuntimeError.

        If you're developing a driver, you should implement
        _set_pin_direction(self, pin, direction) where `pin` will be one of
        your internal IDs. If a pin is set to OUTPUT, put it on LOW state.

        @arg pin pin id you've set using `AbstractDriver.map_pin`
        @arg mode a value from `AbstractDriver.Direction`

        @throw KeyError if pin isn't mapped.
        @throw RuntimeError if direction is not supported by pin.
        """
        if type(pin) is list:
            for p in pin:
                self.set_pin_direction(p, direction)
            return

        pin_id = self._pin_mapping.get(pin, None)
        if pin_id and type(direction) is ahio.Direction:
            self._set_pin_direction(pin_id, direction)
        else:
            raise KeyError('Requested pin is not mapped: %s' % pin)

def
set_pin_direction
(
self
,
pin
,
direction
)
:
if
type
(
pin
)
is
list
:
for
p
in
pin
:
self
.
set_pin_direction
(
p
,
direction
)
return
pin_id
=
self
.
_pin_mapping
.
get
(
pin
,
)
if
pin_id
and
type
(
direction
)
is
ahio
.
Direction
:
self
.
_set_pin_direction
(
pin_id
,
direction
)
else
:
raise
KeyError
(
'Requested pin is not mapped: %s'
%
pin
)


def pin_direction(self, pin):
        """Gets the `ahio.Direction` this pin was set to.

        If you're developing a driver, implement _pin_direction(self, pin)

        @arg pin the pin you want to see the mode
        @returns the `ahio.Direction` the pin is set to

        @throw KeyError if pin isn't mapped.
        """
        if type(pin) is list:
            return [self.pin_direction(p) for p in pin]

        pin_id = self._pin_mapping.get(pin, None)
        if pin_id:
            return self._pin_direction(pin_id)
        else:
            raise KeyError('Requested pin is not mapped: %s' % pin)

def
pin_direction
(
self
,
pin
)
:
if
type
(
pin
)
is
list
:
return
[
self
.
pin_direction
(
p
)
for
p
in
pin
]
pin_id
=
self
.
_pin_mapping
.
get
(
pin
,
)
if
pin_id
:
return
self
.
_pin_direction
(
pin_id
)
else
:
raise
KeyError
(
'Requested pin is not mapped: %s'
%
pin
)

Gets the ahio . Direction this pin was set to .


def set_pin_type(self, pin, ptype):
        """Sets pin `pin` to `type`.

        The pin should support the requested mode. Calling this function
        on a unmapped pin does nothing. Calling it with a unsupported mode
        throws RuntimeError.

        If you're developing a driver, you should implement
        _set_pin_type(self, pin, ptype) where `pin` will be one of your
        internal IDs. If a pin is set to OUTPUT, put it on LOW state.

        @arg pin pin id you've set using `AbstractDriver.map_pin`
        @arg mode a value from `AbstractDriver.PortType`

        @throw KeyError if pin isn't mapped.
        @throw RuntimeError if type is not supported by pin.
        """
        if type(pin) is list:
            for p in pin:
                self.set_pin_type(p, ptype)
            return

        pin_id = self._pin_mapping.get(pin, None)
        if type(ptype) is not ahio.PortType:
            raise KeyError('ptype must be of type ahio.PortType')
        elif pin_id:
            self._set_pin_type(pin_id, ptype)
        else:
            raise KeyError('Requested pin is not mapped: %s' % pin)

def
set_pin_type
(
self
,
pin
,
ptype
)
:
if
type
(
pin
)
is
list
:
for
p
in
pin
:
self
.
set_pin_type
(
p
,
ptype
)
return
pin_id
=
self
.
_pin_mapping
.
get
(
pin
,
)
if
type
(
ptype
)
is
not
ahio
.
PortType
:
raise
KeyError
(
'ptype must be of type ahio.PortType'
)
elif
pin_id
:
self
.
_set_pin_type
(
pin_id
,
ptype
)
else
:
raise
KeyError
(
'Requested pin is not mapped: %s'
%
pin
)


def pin_type(self, pin):
        """Gets the `ahio.PortType` this pin was set to.

        If you're developing a driver, implement _pin_type(self, pin)

        @arg pin the pin you want to see the mode
        @returns the `ahio.PortType` the pin is set to

        @throw KeyError if pin isn't mapped.
        """
        if type(pin) is list:
            return [self.pin_type(p) for p in pin]

        pin_id = self._pin_mapping.get(pin, None)
        if pin_id:
            return self._pin_type(pin_id)
        else:
            raise KeyError('Requested pin is not mapped: %s' % pin)

def
pin_type
(
self
,
pin
)
:
if
type
(
pin
)
is
list
:
return
[
self
.
pin_type
(
p
)
for
p
in
pin
]
pin_id
=
self
.
_pin_mapping
.
get
(
pin
,
)
if
pin_id
:
return
self
.
_pin_type
(
pin_id
)
else
:
raise
KeyError
(
'Requested pin is not mapped: %s'
%
pin
)

Gets the ahio . PortType this pin was set to .


def write(self, pin, value, pwm=False):
        """Sets the output to the given value.

        Sets `pin` output to given value. If the pin is in INPUT mode, do
        nothing. If it's an analog pin, value should be in write_range.
        If it's not in the allowed range, it will be clamped. If pin is in
        digital mode, value can be `ahio.LogicValue` if `pwm` = False, or a
        number between 0 and 1 if `pwm` = True. If PWM is False, the pin will
        be set to HIGH or LOW, if `pwm` is True, a PWM wave with the given
        cycle will be created. If the pin does not support PWM and `pwm` is
        True, raise RuntimeError. The `pwm` argument should be ignored in case
        the pin is analog. If value is not valid for the given
        pwm/analog|digital combination, raise TypeError.

        If you're developing a driver, implement _write(self, pin, value, pwm)

        @arg pin the pin to write to
        @arg value the value to write on the pin
        @arg pwm wether the output should be a pwm wave

        @throw RuntimeError if the pin does not support PWM and `pwm` is True.
        @throw TypeError if value is not valid for this pin's mode and pwm
               value.
        @throw KeyError if pin isn't mapped.
        """
        if type(pin) is list:
            for p in pin:
                self.write(p, value, pwm)
            return

        if pwm and type(value) is not int and type(value) is not float:
            raise TypeError('pwm is set, but value is not a float or int')

        pin_id = self._pin_mapping.get(pin, None)
        if pin_id:
            lpin = self._pin_lin.get(pin, None)
            if lpin and type(lpin['write']) is tuple:
                write_range = lpin['write']
                value = self._linear_interpolation(value, *write_range)
            self._write(pin_id, value, pwm)
        else:
            raise KeyError('Requested pin is not mapped: %s' % pin)


def read(self, pin):
        """Reads value from pin `pin`.

        Returns the value read from pin `pin`. If it's an analog pin, returns
        a number in analog.input_range. If it's digital, returns
        `ahio.LogicValue`.

        If you're developing a driver, implement _read(self, pin)

        @arg pin the pin to read from
        @returns the value read from the pin

        @throw KeyError if pin isn't mapped.
        """
        if type(pin) is list:
            return [self.read(p) for p in pin]

        pin_id = self._pin_mapping.get(pin, None)
        if pin_id:
            value = self._read(pin_id)
            lpin = self._pin_lin.get(pin, None)
            if lpin and type(lpin['read']) is tuple:
                read_range = lpin['read']
                value = self._linear_interpolation(value, *read_range)
            return value
        else:
            raise KeyError('Requested pin is not mapped: %s' % pin)

def
read
(
self
,
pin
)
:
if
type
(
pin
)
is
list
:
return
[
self
.
read
(
p
)
for
p
in
pin
]
pin_id
=
self
.
_pin_mapping
.
get
(
pin
,
)
if
pin_id
:
value
=
self
.
_read
(
pin_id
)
lpin
=
self
.
_pin_lin
.
get
(
pin
,
)
if
lpin
and
type
(
lpin
[
'read'
]
)
is
tuple
:
read_range
=
lpin
[
'read'
]
value
=
self
.
_linear_interpolation
(
value
,
*
read_range
)
return
value
else
:
raise
KeyError
(
'Requested pin is not mapped: %s'
%
pin
)


def set_analog_reference(self, reference, pin=None):
        """Sets the analog reference to `reference`

        If the driver supports per pin reference setting, set pin to the
        desired reference. If not, passing None means set to all, which is the
        default in most hardware. If only per pin reference is supported and
        pin is None, raise RuntimeError.

        If you're developing a driver, implement
        _set_analog_reference(self, reference, pin). Raise RuntimeError if pin
        was set but is not supported by the platform.

        @arg reference the value that describes the analog reference. See
            `AbstractDriver.analog_references`
        @arg pin if the the driver supports it, the pin that will use
            `reference` as reference. None for all.

        @throw RuntimeError if pin is None on a per pin only hardware, or if
            it's a valid pin on a global only analog reference hardware.
        @throw KeyError if pin isn't mapped.
        """
        if pin is None:
            self._set_analog_reference(reference, None)
        else:
            pin_id = self._pin_mapping.get(pin, None)
            if pin_id:
                self._set_analog_reference(reference, pin_id)
            else:
                raise KeyError('Requested pin is not mapped: %s' % pin)

def
set_analog_reference
(
self
,
reference
,
pin
=
)
:
if
pin
is
:
self
.
_set_analog_reference
(
reference
,
)
else
:
pin_id
=
self
.
_pin_mapping
.
get
(
pin
,
)
if
pin_id
:
self
.
_set_analog_reference
(
reference
,
pin_id
)
else
:
raise
KeyError
(
'Requested pin is not mapped: %s'
%
pin
)


def analog_reference(self, pin=None):
        """Returns the analog reference.

        If the driver supports per pin analog reference setting, returns the
        reference for pin `pin`. If pin is None, returns the global analog
        reference. If only per pin reference is supported and pin is None,
        raise RuntimeError.

        If you're developing a driver, implement _analog_reference(self, pin)

        @arg pin if the the driver supports it, the pin that will use
            `reference` as reference. None for all.

        @returns the reference used for pin

        @throw RuntimeError if pin is None on a per pin only hardware, or if
            it's a valid pin on a global only analog reference hardware.
        @throw KeyError if pin isn't mapped.
        """
        if pin is None:
            return self._analog_reference(None)
        else:
            pin_id = self._pin_mapping.get(pin, None)
            if pin_id:
                return self._analog_reference(pin_id)
            else:
                raise KeyError('Requested pin is not mapped: %s' % pin)

def
analog_reference
(
self
,
pin
=
)
:
if
pin
is
:
return
self
.
_analog_reference
(
)
else
:
pin_id
=
self
.
_pin_mapping
.
get
(
pin
,
)
if
pin_id
:
return
self
.
_analog_reference
(
pin_id
)
else
:
raise
KeyError
(
'Requested pin is not mapped: %s'
%
pin
)


def set_pwm_frequency(self, frequency, pin=None):
        """Sets PWM frequency, if supported by hardware

        If the driver supports per pin frequency setting, set pin to the
        desired frequency. If not, passing None means set to all. If only per
        pin frequency is supported and pin is None, raise RuntimeError.

        If you're developing a driver, implement
        _set_pwm_frequency(self, frequency, pin). Raise RuntimeError if pin
        was set but is not supported by the platform.

        @arg frequency pwm frequency to be set, in Hz
        @arg pin if the the driver supports it, the pin that will use
            `frequency` as pwm frequency. None for all/global.

        @throw RuntimeError if pin is None on a per pin only hardware, or if
            it's a valid pin on a global only hardware.
        @throw KeyError if pin isn't mapped.
        """
        if pin is None:
            self._set_pwm_frequency(frequency, None)
        else:
            pin_id = self._pin_mapping.get(pin, None)
            if pin_id:
                self._set_pwm_frequency(frequency, pin_id)
            else:
                raise KeyError('Requested pin is not mapped: %s' % pin)

def
set_pwm_frequency
(
self
,
frequency
,
pin
=
)
:
if
pin
is
:
self
.
_set_pwm_frequency
(
frequency
,
)
else
:
pin_id
=
self
.
_pin_mapping
.
get
(
pin
,
)
if
pin_id
:
self
.
_set_pwm_frequency
(
frequency
,
pin_id
)
else
:
raise
KeyError
(
'Requested pin is not mapped: %s'
%
pin
)

Sets PWM frequency if supported by hardware


def SIRode(y0, time, beta, gamma):
    """Integrate SIR epidemic model

    Simulate a very basic deterministic SIR system.

    :param 2x1 numpy array y0: initial conditions
    :param Ntimestep length numpy array time: Vector of time points that \
    solution is returned at
    :param float beta: transmission rate
    :param float gamma: recovery rate

    :returns: (2)x(Ntimestep) numpy array Xsim: first row S(t), second row I(t)

    """

    Xsim = rk4(SIR_D, y0, time, args=(beta,gamma,))
    Xsim = Xsim.transpose()
    return Xsim

def SIRode ( y0 , time , beta , gamma ) : Xsim = rk4 ( SIR_D , y0 , time , args = ( beta , gamma , ) ) Xsim = Xsim . transpose ( ) return Xsim


def url(self):
        """
        Return the URL of the server.

        :returns: URL of the server
        :rtype: string
        """
        if len(self.drivers) > 0:
            return self.drivers[0].url
        else:
            return self._url

def url ( self ) : if len ( self . drivers ) > 0 : return self . drivers [ 0 ] . url else : return self . _url


def guess_array_memory_usage( bam_readers, dtype, use_strand=False ):
    """Returns an estimate for the maximum amount of memory to be consumed by numpy arrays."""
    ARRAY_COUNT = 5
    if not isinstance( bam_readers, list ):
        bam_readers = [ bam_readers ]
    if isinstance( dtype, basestring ):
        dtype = NUMPY_DTYPES.get( dtype, None )
    use_strand = use_strand + 1 #if false, factor of 1, if true, factor of 2
    dtypes = guess_numpy_dtypes_from_idxstats( bam_readers, default=None, force_dtype=False )
    if not [ dt for dt in dtypes if dt is not None ]:
        #found no info from idx
        dtypes = guess_numpy_dtypes_from_idxstats( bam_readers, default=dtype or numpy.uint64, force_dtype=True )
    elif dtype:
        dtypes = [ dtype if dt else None for dt in dtypes ]
    read_groups = []
    no_read_group = False
    for bam in bam_readers:
        rgs = bam.get_read_groups()
        if rgs:
            for rg in rgs:
                if rg not in read_groups:
                    read_groups.append( rg )
        else:
            no_read_group = True
    read_groups = len( read_groups ) + no_read_group
    max_ref_size = 0
    array_byte_overhead = sys.getsizeof( numpy.zeros( ( 0 ), dtype=numpy.uint64 ) )
    array_count = ARRAY_COUNT * use_strand * read_groups
    for bam in bam_readers:
        for i, ( name, length ) in enumerate( bam.get_references() ):
            if dtypes[i] is not None:
                max_ref_size = max( max_ref_size, ( length + length * dtypes[i]().nbytes * array_count + ( array_byte_overhead * ( array_count + 1 ) ) ) )
    return max_ref_size

def guess_array_memory_usage ( bam_readers , dtype , use_strand = False ) : ARRAY_COUNT = 5 if not isinstance ( bam_readers , list ) : bam_readers = [ bam_readers ] if isinstance ( dtype , basestring ) : dtype = NUMPY_DTYPES . get ( dtype , None ) use_strand = use_strand + 1 dtypes = guess_numpy_dtypes_from_idxstats ( bam_readers , default = None , force_dtype = False ) if not [ dt for dt in dtypes if dt is not None ] : dtypes = guess_numpy_dtypes_from_idxstats ( bam_readers , default = dtype or numpy . uint64 , force_dtype = True ) elif dtype : dtypes = [ dtype if dt else None for dt in dtypes ] read_groups = [ ] no_read_group = False for bam in bam_readers : rgs = bam . get_read_groups ( ) if rgs : for rg in rgs : if rg not in read_groups : read_groups . append ( rg ) else : no_read_group = True read_groups = len ( read_groups ) + no_read_group max_ref_size = 0 array_byte_overhead = sys . getsizeof ( numpy . zeros ( ( 0 ) , dtype = numpy . uint64 ) ) array_count = ARRAY_COUNT * use_strand * read_groups for bam in bam_readers : for i , ( name , length ) in enumerate ( bam . get_references ( ) ) : if dtypes [ i ] is not None : max_ref_size = max ( max_ref_size , ( length + length * dtypes [ i ] ( ) . nbytes * array_count + ( array_byte_overhead * ( array_count + 1 ) ) ) ) return max_ref_size

Returns an estimate for the maximum amount of memory to be consumed by numpy arrays .


def main():
    """Create coverage reports and open them in the browser."""
    usage = "Usage: %prog PATH_TO_PACKAGE"
    parser = optparse.OptionParser(usage=usage)
    parser.add_option(
        "-v", "--verbose",
        action="store_true", dest="verbose", default=False,
        help="Show debug output")
    parser.add_option(
        "-d", "--output-dir",
        action="store", type="string", dest="output_dir",
        default='',
        help="")
    parser.add_option(
        "-t", "--test-args",
        action="store", type="string", dest="test_args",
        default='',
        help=("Pass argument on to bin/test. Quote the argument, " +
              "for instance \"-t '-m somemodule'\"."))
    (options, args) = parser.parse_args()
    if options.verbose:
        log_level = logging.DEBUG
    else:
        log_level = logging.INFO
    logging.basicConfig(level=log_level,
                        format="%(levelname)s: %(message)s")

    curdir = os.getcwd()
    testbinary = os.path.join(curdir, 'bin', 'test')
    if not os.path.exists(testbinary):
        raise RuntimeError("Test command doesn't exist: %s" % testbinary)

    coveragebinary = os.path.join(curdir, 'bin', 'coverage')
    if not os.path.exists(coveragebinary):
        logger.debug("Trying globally installed coverage command.")
        coveragebinary = 'coverage'

    logger.info("Running tests in coverage mode (can take a long time)")
    parts = [coveragebinary, 'run', testbinary]
    if options.test_args:
        parts.append(options.test_args)
    system(" ".join(parts))
    logger.debug("Creating coverage reports...")
    if options.output_dir:
        coverage_dir = options.output_dir
        open_in_browser = False
    else:
        coverage_dir = 'htmlcov'  # The default
        open_in_browser = True
    system("%s html --directory=%s" % (coveragebinary, coverage_dir))
    logger.info("Wrote coverage files to %s", coverage_dir)
    if open_in_browser:
        index_file = os.path.abspath(
            os.path.join(coverage_dir, 'index.html'))
        logger.debug("About to open %s in your webbrowser.", index_file)
        webbrowser.open('file://' + index_file)
        logger.info("Opened reports in your browser.")

Create coverage reports and open them in the browser .


def setup(
            self,
            configuration="ModbusSerialClient(method='rtu',port='/dev/cu.usbmodem14101',baudrate=9600)"
    ):
        """Start a Modbus server.

        The following classes are available with their respective named
        parameters:

        ModbusTcpClient
            host: The host to connect to (default 127.0.0.1)
            port: The modbus port to connect to (default 502)
            source_address: The source address tuple to bind to (default ('', 0))
            timeout: The timeout to use for this socket (default Defaults.Timeout)

        ModbusUdpClient
            host: The host to connect to (default 127.0.0.1)
            port: The modbus port to connect to (default 502)
            timeout: The timeout to use for this socket (default None)

        ModbusSerialClient
            method: The method to use for connection (asii, rtu, binary)
            port: The serial port to attach to
            stopbits: The number of stop bits to use (default 1)
            bytesize: The bytesize of the serial messages (default 8 bits)
            parity: Which kind of parity to use (default None)
            baudrate: The baud rate to use for the serial device
            timeout: The timeout between serial requests (default 3s)

        When configuring the ports, the following convention should be
        respected:

        portname: C1:13 -> Coil on device 1, address 13

        The letters can be:

        C = Coil
        I = Input
        R = Register
        H = Holding

        @arg configuration a string that instantiates one of those classes.

        @throw RuntimeError can't connect to Arduino
        """
        from pymodbus3.client.sync import ModbusSerialClient, ModbusUdpClient, ModbusTcpClient
        self._client = eval(configuration)
        self._client.connect()


def get_exception_from_status_and_error_codes(status_code, error_code, value):
    """
    Return an exception given status and error codes.

    :param status_code: HTTP status code.
    :type status_code: None | int
    :param error_code: Midas Server error code.
    :type error_code: None | int
    :param value: Message to display.
    :type value: string
    :returns: Exception.
    :rtype : pydas.exceptions.ResponseError
    """
    if status_code == requests.codes.bad_request:
        exception = BadRequest(value)
    elif status_code == requests.codes.unauthorized:
        exception = Unauthorized(value)
    elif status_code == requests.codes.forbidden:
        exception = Unauthorized(value)
    elif status_code in [requests.codes.not_found, requests.codes.gone]:
        exception = NotFound(value)
    elif status_code == requests.codes.method_not_allowed:
        exception = MethodNotAllowed(value)
    elif status_code >= requests.codes.bad_request:
        exception = HTTPError(value)
    else:
        exception = ResponseError(value)

    if error_code == -100:  # MIDAS_INTERNAL_ERROR
        exception = InternalError(value)
    elif error_code == -101:  # MIDAS_INVALID_TOKEN
        exception = InvalidToken(value)
    elif error_code == -105:  # MIDAS_UPLOAD_FAILED
        exception = UploadFailed(value)
    elif error_code == -140:  # MIDAS_UPLOAD_TOKEN_GENERATION_FAILED
        exception = UploadTokenGenerationFailed(value)
    elif error_code == -141:  # MIDAS_INVALID_UPLOAD_TOKEN
        exception = InvalidUploadToken(value)
    elif error_code == -150:  # MIDAS_INVALID_PARAMETER
        exception = InvalidParameter(value)
    elif error_code == -151:  # MIDAS_INVALID_POLICY
        exception = InvalidPolicy(value)

    return exception

Return an exception given status and error codes .


def analog_read(self, pin):
        """
        Retrieve the last analog data value received for the specified pin.

        :param pin: Selected pin

        :return: The last value entered into the analog response table.
        """
        with self.data_lock:
            data = self._command_handler.analog_response_table[pin][self._command_handler.RESPONSE_TABLE_PIN_DATA_VALUE]
        return data

def analog_read ( self , pin ) : with self . data_lock : data = self . _command_handler . analog_response_table [ pin ] [ self . _command_handler . RESPONSE_TABLE_PIN_DATA_VALUE ] return data

Retrieve the last analog data value received for the specified pin .


def disable_analog_reporting(self, pin):
        """
        Disables analog reporting for a single analog pin.

        :param pin: Analog pin number. For example for A0, the number is 0.

        :return: No return value
        """
        command = [self._command_handler.REPORT_ANALOG + pin, self.REPORTING_DISABLE]
        self._command_handler.send_command(command)

def disable_analog_reporting ( self , pin ) : command = [ self . _command_handler . REPORT_ANALOG + pin , self . REPORTING_DISABLE ] self . _command_handler . send_command ( command )

Disables analog reporting for a single analog pin .


def disable_digital_reporting(self, pin):
        """
        Disables digital reporting. By turning reporting off for this pin, reporting
        is disabled for all 8 bits in the "port" -

        :param pin: Pin and all pins for this port

        :return: No return value
        """
        port = pin // 8
        command = [self._command_handler.REPORT_DIGITAL + port, self.REPORTING_DISABLE]
        self._command_handler.send_command(command)

def disable_digital_reporting ( self , pin ) : port = pin // 8 command = [ self . _command_handler . REPORT_DIGITAL + port , self . REPORTING_DISABLE ] self . _command_handler . send_command ( command )

Disables digital reporting . By turning reporting off for this pin reporting is disabled for all 8 bits in the port -


def enable_analog_reporting(self, pin):
        """
        Enables analog reporting. By turning reporting on for a single pin.

        :param pin: Analog pin number. For example for A0, the number is 0.

        :return: No return value
        """
        command = [self._command_handler.REPORT_ANALOG + pin, self.REPORTING_ENABLE]
        self._command_handler.send_command(command)

def enable_analog_reporting ( self , pin ) : command = [ self . _command_handler . REPORT_ANALOG + pin , self . REPORTING_ENABLE ] self . _command_handler . send_command ( command )

Enables analog reporting . By turning reporting on for a single pin .


def enable_digital_reporting(self, pin):
        """
        Enables digital reporting. By turning reporting on for all 8 bits in the "port" -
        this is part of Firmata's protocol specification.

        :param pin: Pin and all pins for this port

        :return: No return value
        """
        port = pin // 8
        command = [self._command_handler.REPORT_DIGITAL + port, self.REPORTING_ENABLE]
        self._command_handler.send_command(command)

def enable_digital_reporting ( self , pin ) : port = pin // 8 command = [ self . _command_handler . REPORT_DIGITAL + port , self . REPORTING_ENABLE ] self . _command_handler . send_command ( command )

Enables digital reporting . By turning reporting on for all 8 bits in the port - this is part of Firmata s protocol specification .


def extended_analog(self, pin, data):
        """
        This method will send an extended data analog output command to the selected pin

        :param pin: 0 - 127

        :param data: 0 - 0xfffff
        """
        analog_data = [pin, data & 0x7f, (data >> 7) & 0x7f, (data >> 14) & 0x7f]
        self._command_handler.send_sysex(self._command_handler.EXTENDED_ANALOG, analog_data)

def extended_analog ( self , pin , data ) : analog_data = [ pin , data & 0x7f , ( data >> 7 ) & 0x7f , ( data >> 14 ) & 0x7f ] self . _command_handler . send_sysex ( self . _command_handler . EXTENDED_ANALOG , analog_data )


def get_stepper_version(self, timeout=20):
        """
        Get the stepper library version number.

        :param timeout: specify a time to allow arduino to process and return a version

        :return: the stepper version number if it was set.
        """
        # get current time
        start_time = time.time()

        # wait for up to 20 seconds for a successful capability query to occur

        while self._command_handler.stepper_library_version <= 0:
            if time.time() - start_time > timeout:
                if self.verbose is True:
                    print("Stepper Library Version Request timed-out. "
                          "Did you send a stepper_request_library_version command?")
                return
            else:
                pass
        return self._command_handler.stepper_library_version

Get the stepper library version number .


def i2c_write(self, address, *args):
        """
        Write data to an i2c device.

        :param address: i2c device address

        :param args: A variable number of bytes to be sent to the device
        """
        data = [address, self.I2C_WRITE]
        for item in args:
            data.append(item & 0x7f)
            data.append((item >> 7) & 0x7f)
        self._command_handler.send_sysex(self._command_handler.I2C_REQUEST, data)

def i2c_write ( self , address , * args ) : data = [ address , self . I2C_WRITE ] for item in args : data . append ( item & 0x7f ) data . append ( ( item >> 7 ) & 0x7f ) self . _command_handler . send_sysex ( self . _command_handler . I2C_REQUEST , data )


def i2c_stop_reading(self, address):
        """
        This method stops an I2C_READ_CONTINUOUSLY operation for the i2c device address specified.

        :param address: address of i2c device
        """
        data = [address, self.I2C_STOP_READING]
        self._command_handler.send_sysex(self._command_handler.I2C_REQUEST, data)

def i2c_stop_reading ( self , address ) : data = [ address , self . I2C_STOP_READING ] self . _command_handler . send_sysex ( self . _command_handler . I2C_REQUEST , data )

This method stops an I2C_READ_CONTINUOUSLY operation for the i2c device address specified .


def play_tone(self, pin, tone_command, frequency, duration):
        """
        This method will call the Tone library for the selected pin.
        If the tone command is set to TONE_TONE, then the specified tone will be played.
        Else, if the tone command is TONE_NO_TONE, then any currently playing tone will be disabled.
        It is intended for a future release of Arduino Firmata

        :param pin: Pin number

        :param tone_command: Either TONE_TONE, or TONE_NO_TONE

        :param frequency: Frequency of tone in hz

        :param duration: Duration of tone in milliseconds

        :return: No return value
        """

        # convert the integer values to bytes
        if tone_command == self.TONE_TONE:
            # duration is specified
            if duration:
                data = [tone_command, pin, frequency & 0x7f, (frequency >> 7) & 0x7f, duration & 0x7f, (duration >> 7) & 0x7f]

            else:
                data = [tone_command, pin, frequency & 0x7f, (frequency >> 7) & 0x7f, 0, 0]

            self._command_handler.digital_response_table[pin][self._command_handler.RESPONSE_TABLE_MODE] = \
                self.TONE
        # turn off tone
        else:
            data = [tone_command, pin]
        self._command_handler.send_sysex(self._command_handler.TONE_PLAY, data)

def play_tone ( self , pin , tone_command , frequency , duration ) : if tone_command == self . TONE_TONE : if duration : data = [ tone_command , pin , frequency & 0x7f , ( frequency >> 7 ) & 0x7f , duration & 0x7f , ( duration >> 7 ) & 0x7f ] else : data = [ tone_command , pin , frequency & 0x7f , ( frequency >> 7 ) & 0x7f , 0 , 0 ] self . _command_handler . digital_response_table [ pin ] [ self . _command_handler . RESPONSE_TABLE_MODE ] = self . TONE else : data = [ tone_command , pin ] self . _command_handler . send_sysex ( self . _command_handler . TONE_PLAY , data )

This method will call the Tone library for the selected pin . If the tone command is set to TONE_TONE then the specified tone will be played . Else if the tone command is TONE_NO_TONE then any currently playing tone will be disabled . It is intended for a future release of Arduino Firmata


def set_analog_latch(self, pin, threshold_type, threshold_value, cb=None):
        """
        This method "arms" an analog pin for its data to be latched and saved in the latching table
        If a callback method is provided, when latching criteria is achieved, the callback function is called
        with latching data notification. In that case, the latching table is not updated.

        :param pin: Analog pin number (value following an 'A' designator, i.e. A5 = 5

        :param threshold_type: ANALOG_LATCH_GT | ANALOG_LATCH_LT  | ANALOG_LATCH_GTE | ANALOG_LATCH_LTE

        :param threshold_value: numerical value - between 0 and 1023

        :param cb: callback method

        :return: True if successful, False if parameter data is invalid
        """
        if self.ANALOG_LATCH_GT <= threshold_type <= self.ANALOG_LATCH_LTE:
            if 0 <= threshold_value <= 1023:
                self._command_handler.set_analog_latch(pin, threshold_type, threshold_value, cb)
                return True
        else:
            return False

def set_analog_latch ( self , pin , threshold_type , threshold_value , cb = None ) : if self . ANALOG_LATCH_GT <= threshold_type <= self . ANALOG_LATCH_LTE : if 0 <= threshold_value <= 1023 : self . _command_handler . set_analog_latch ( pin , threshold_type , threshold_value , cb ) return True else : return False

This method arms an analog pin for its data to be latched and saved in the latching table If a callback method is provided when latching criteria is achieved the callback function is called with latching data notification . In that case the latching table is not updated .


def set_digital_latch(self, pin, threshold_type, cb=None):
        """
        This method "arms" a digital pin for its data to be latched and saved in the latching table
        If a callback method is provided, when latching criteria is achieved, the callback function is called
        with latching data notification. In that case, the latching table is not updated.

        :param pin: Digital pin number

        :param threshold_type: DIGITAL_LATCH_HIGH | DIGITAL_LATCH_LOW

        :param cb: callback function

        :return: True if successful, False if parameter data is invalid
        """
        if 0 <= threshold_type <= 1:
            self._command_handler.set_digital_latch(pin, threshold_type, cb)
            return True
        else:
            return False

def set_digital_latch ( self , pin , threshold_type , cb = None ) : if 0 <= threshold_type <= 1 : self . _command_handler . set_digital_latch ( pin , threshold_type , cb ) return True else : return False

This method arms a digital pin for its data to be latched and saved in the latching table If a callback method is provided when latching criteria is achieved the callback function is called with latching data notification . In that case the latching table is not updated .


def servo_config(self, pin, min_pulse=544, max_pulse=2400):
        """
        Configure a pin as a servo pin. Set pulse min, max in ms.

        :param pin: Servo Pin.

        :param min_pulse: Min pulse width in ms.

        :param max_pulse: Max pulse width in ms.

        :return: No return value
        """
        self.set_pin_mode(pin, self.SERVO, self.OUTPUT)
        command = [pin, min_pulse & 0x7f, (min_pulse >> 7) & 0x7f,
                   max_pulse & 0x7f, (max_pulse >> 7) & 0x7f]

        self._command_handler.send_sysex(self._command_handler.SERVO_CONFIG, command)

def servo_config ( self , pin , min_pulse = 544 , max_pulse = 2400 ) : self . set_pin_mode ( pin , self . SERVO , self . OUTPUT ) command = [ pin , min_pulse & 0x7f , ( min_pulse >> 7 ) & 0x7f , max_pulse & 0x7f , ( max_pulse >> 7 ) & 0x7f ] self . _command_handler . send_sysex ( self . _command_handler . SERVO_CONFIG , command )

Configure a pin as a servo pin . Set pulse min max in ms .


def stepper_config(self, steps_per_revolution, stepper_pins):
        """
        Configure stepper motor prior to operation.

        :param steps_per_revolution: number of steps per motor revolution

        :param stepper_pins: a list of control pin numbers - either 4 or 2
        """
        data = [self.STEPPER_CONFIGURE, steps_per_revolution & 0x7f, (steps_per_revolution >> 7) & 0x7f]
        for pin in range(len(stepper_pins)):
            data.append(stepper_pins[pin])
        self._command_handler.send_sysex(self._command_handler.STEPPER_DATA, data)

def stepper_config ( self , steps_per_revolution , stepper_pins ) : data = [ self . STEPPER_CONFIGURE , steps_per_revolution & 0x7f , ( steps_per_revolution >> 7 ) & 0x7f ] for pin in range ( len ( stepper_pins ) ) : data . append ( stepper_pins [ pin ] ) self . _command_handler . send_sysex ( self . _command_handler . STEPPER_DATA , data )

Configure stepper motor prior to operation .


def stepper_step(self, motor_speed, number_of_steps):
        """
        Move a stepper motor for the number of steps at the specified speed

        :param motor_speed: 21 bits of data to set motor speed

        :param number_of_steps: 14 bits for number of steps & direction
                                positive is forward, negative is reverse
        """
        if number_of_steps > 0:
            direction = 1
        else:
            direction = 0
        abs_number_of_steps = abs(number_of_steps)
        data = [self.STEPPER_STEP, motor_speed & 0x7f, (motor_speed >> 7) & 0x7f, (motor_speed >> 14) & 0x7f,
                abs_number_of_steps & 0x7f, (abs_number_of_steps >> 7) & 0x7f, direction]
        self._command_handler.send_sysex(self._command_handler.STEPPER_DATA, data)

def stepper_step ( self , motor_speed , number_of_steps ) : if number_of_steps > 0 : direction = 1 else : direction = 0 abs_number_of_steps = abs ( number_of_steps ) data = [ self . STEPPER_STEP , motor_speed & 0x7f , ( motor_speed >> 7 ) & 0x7f , ( motor_speed >> 14 ) & 0x7f , abs_number_of_steps & 0x7f , ( abs_number_of_steps >> 7 ) & 0x7f , direction ] self . _command_handler . send_sysex ( self . _command_handler . STEPPER_DATA , data )


def stepper_request_library_version(self):
        """
        Request the stepper library version from the Arduino.
        To retrieve the version after this command is called, call
        get_stepper_version
        """
        data = [self.STEPPER_LIBRARY_VERSION]
        self._command_handler.send_sysex(self._command_handler.STEPPER_DATA, data)

def stepper_request_library_version ( self ) : data = [ self . STEPPER_LIBRARY_VERSION ] self . _command_handler . send_sysex ( self . _command_handler . STEPPER_DATA , data )

Request the stepper library version from the Arduino . To retrieve the version after this command is called call get_stepper_version


def open(self, verbose):
        """
        open the serial port using the configuration data
        returns a reference to this instance
        """
        # open a serial port
        if verbose:
            print('\nOpening Arduino Serial port %s ' % self.port_id)

        try:

            # in case the port is already open, let's close it and then
            # reopen it
            self.arduino.close()
            time.sleep(1)
            self.arduino.open()
            time.sleep(1)
            return self.arduino

        except Exception:
            # opened failed - will report back to caller
            raise

def
open
(
self
,
verbose
)
:
if
verbose
:
print
(
'\nOpening Arduino Serial port %s '
%
self
.
port_id
)
try
:
self
.
arduino
.
close
(
)
time
.
sleep
(
1
)
self
.
arduino
.
open
(
)
time
.
sleep
(
1
)
return
self
.
arduino
except
Exception
:
raise


def run(self):
        """
        This method continually runs. If an incoming character is available on the serial port
        it is read and placed on the _command_deque
        @return: Never Returns
        """
        while not self.is_stopped():
            # we can get an OSError: [Errno9] Bad file descriptor when shutting down
            # just ignore it
            try:
                if self.arduino.inWaiting():
                    c = self.arduino.read()
                    self.command_deque.append(ord(c))
                else:
                    time.sleep(.1)
            except OSError:
                pass
            except IOError:
                self.stop()
        self.close()

def run ( self ) : while not self . is_stopped ( ) : try : if self . arduino . inWaiting ( ) : c = self . arduino . read ( ) self . command_deque . append ( ord ( c ) ) else : time . sleep ( .1 ) except OSError : pass except IOError : self . stop ( ) self . close ( )

This method continually runs . If an incoming character is available on the serial port it is read and placed on the _command_deque


def set_brightness(self, brightness):
        """
        Set the brightness level for the entire display
        @param brightness: brightness level (0 -15)
        """
        if brightness > 15:
            brightness = 15
        brightness |= 0xE0
        self.brightness = brightness
        self.firmata.i2c_write(0x70, brightness)

def set_brightness ( self , brightness ) : if brightness > 15 : brightness = 15 brightness |= 0xE0 self . brightness = brightness self . firmata . i2c_write ( 0x70 , brightness )


def set_bit_map(self, shape, color):
        """
        Populate the bit map with the supplied "shape" and color
        and then write the entire bitmap to the display
        @param shape: pattern to display
        @param color: color for the pattern
        """
        for row in range(0, 8):
            data = shape[row]
            # shift data into buffer
            bit_mask = 0x80
            for column in range(0, 8):
                if data & bit_mask:
                    self.set_pixel(row, column, color, True)
                bit_mask >>= 1
        self.output_entire_buffer()

def set_bit_map ( self , shape , color ) : for row in range ( 0 , 8 ) : data = shape [ row ] bit_mask = 0x80 for column in range ( 0 , 8 ) : if data & bit_mask : self . set_pixel ( row , column , color , True ) bit_mask >>= 1 self . output_entire_buffer ( )

Populate the bit map with the supplied shape and color and then write the entire bitmap to the display


def output_entire_buffer(self):
        """
        Write the entire buffer to the display
        """
        green = 0
        red = 0

        for row in range(0, 8):
            for col in range(0, 8):
                if self.display_buffer[row][col] == self.LED_GREEN:
                    green |= 1 << col
                elif self.display_buffer[row][col] == self.LED_RED:
                    red |= 1 << col
                elif self.display_buffer[row][col] == self.LED_YELLOW:
                    green |= 1 << col
                    red |= 1 << col
                elif self.display_buffer[row][col] == self.LED_OFF:
                    green &= ~(1 << col)
                    red &= ~(1 << col)

            self.firmata.i2c_write(0x70, row * 2, 0, green)
            self.firmata.i2c_write(0x70, row * 2 + 1, 0, red)

def output_entire_buffer ( self ) : green = 0 red = 0 for row in range ( 0 , 8 ) : for col in range ( 0 , 8 ) : if self . display_buffer [ row ] [ col ] == self . LED_GREEN : green |= 1 << col elif self . display_buffer [ row ] [ col ] == self . LED_RED : red |= 1 << col elif self . display_buffer [ row ] [ col ] == self . LED_YELLOW : green |= 1 << col red |= 1 << col elif self . display_buffer [ row ] [ col ] == self . LED_OFF : green &= ~ ( 1 << col ) red &= ~ ( 1 << col ) self . firmata . i2c_write ( 0x70 , row * 2 , 0 , green ) self . firmata . i2c_write ( 0x70 , row * 2 + 1 , 0 , red )


def clear_display_buffer(self):
        """
        Set all led's to off.
        """
        for row in range(0, 8):
            self.firmata.i2c_write(0x70, row * 2, 0, 0)
            self.firmata.i2c_write(0x70, (row * 2) + 1, 0, 0)

            for column in range(0, 8):
                self.display_buffer[row][column] = 0

def clear_display_buffer ( self ) : for row in range ( 0 , 8 ) : self . firmata . i2c_write ( 0x70 , row * 2 , 0 , 0 ) self . firmata . i2c_write ( 0x70 , ( row * 2 ) + 1 , 0 , 0 ) for column in range ( 0 , 8 ) : self . display_buffer [ row ] [ column ] = 0


def digital_message(self, data):
        """
        This method handles the incoming digital message.
        It stores the data values in the digital response table.
        Data is stored for all 8 bits of a  digital port

        :param data: Message data from Firmata

        :return: No return value.
        """
        port = data[0]
        port_data = (data[self.MSB] << 7) + data[self.LSB]

        # set all the pins for this reporting port
        # get the first pin number for this report
        pin = port * 8
        for pin in range(pin, min(pin + 8, self.total_pins_discovered)):
            # shift through all the bit positions and set the digital response table
            with self.pymata.data_lock:
                # look at the previously stored value for this pin
                prev_data = self.digital_response_table[pin][self.RESPONSE_TABLE_PIN_DATA_VALUE]
                # get the current value
                self.digital_response_table[pin][self.RESPONSE_TABLE_PIN_DATA_VALUE] = port_data & 0x01
                # if the values differ and callback is enabled for the pin, then send out the callback
                if prev_data != port_data & 0x01:
                    callback = self.digital_response_table[pin][self.RESPONSE_TABLE_CALLBACK]
                    if callback:
                        callback([self.pymata.DIGITAL, pin,
                                  self.digital_response_table[pin][self.RESPONSE_TABLE_PIN_DATA_VALUE]])

                # determine if the latch data table needs to be updated for each pin
                latching_entry = self.digital_latch_table[pin]
                if latching_entry[self.LATCH_STATE] == self.LATCH_ARMED:
                    if latching_entry[self.LATCHED_THRESHOLD_TYPE] == self.DIGITAL_LATCH_LOW:
                        if (port_data & 0x01) == 0:
                            if latching_entry[self.DIGITAL_LATCH_CALLBACK] is not None:
                                self.digital_latch_table[pin] = [0, 0, 0, 0, None]
                                latching_entry[self.DIGITAL_LATCH_CALLBACK](
                                    [self.pymata.OUTPUT | self.pymata.LATCH_MODE,
                                     pin, 0, time.time()])

                            else:
                                updated_latch_entry = latching_entry
                                updated_latch_entry[self.LATCH_STATE] = self.LATCH_LATCHED
                                updated_latch_entry[self.DIGITAL_LATCHED_DATA] = self.DIGITAL_LATCH_LOW
                                # time stamp it
                                updated_latch_entry[self.DIGITAL_TIME_STAMP] = time.time()
                        else:
                            pass
                    elif latching_entry[self.LATCHED_THRESHOLD_TYPE] == self.DIGITAL_LATCH_HIGH:
                        if port_data & 0x01:
                            if latching_entry[self.DIGITAL_LATCH_CALLBACK] is not None:
                                self.digital_latch_table[pin] = [0, 0, 0, 0, None]
                                latching_entry[self.DIGITAL_LATCH_CALLBACK](
                                    [self.pymata.OUTPUT | self.pymata.LATCH_MODE,
                                     pin, 1, time.time()])
                            else:
                                updated_latch_entry = latching_entry
                                updated_latch_entry[self.LATCH_STATE] = self.LATCH_LATCHED
                                updated_latch_entry[self.DIGITAL_LATCHED_DATA] = self.DIGITAL_LATCH_HIGH
                                # time stamp it
                                updated_latch_entry[self.DIGITAL_TIME_STAMP] = time.time()
                        else:
                            pass
                else:
                    pass

            # get the next data bit
            port_data >>= 1

def digital_message ( self , data ) : port = data [ 0 ] port_data = ( data [ self . MSB ] << 7 ) + data [ self . LSB ] pin = port * 8 for pin in range ( pin , min ( pin + 8 , self . total_pins_discovered ) ) : with self . pymata . data_lock : prev_data = self . digital_response_table [ pin ] [ self . RESPONSE_TABLE_PIN_DATA_VALUE ] self . digital_response_table [ pin ] [ self . RESPONSE_TABLE_PIN_DATA_VALUE ] = port_data & 0x01 if prev_data != port_data & 0x01 : callback = self . digital_response_table [ pin ] [ self . RESPONSE_TABLE_CALLBACK ] if callback : callback ( [ self . pymata . DIGITAL , pin , self . digital_response_table [ pin ] [ self . RESPONSE_TABLE_PIN_DATA_VALUE ] ] ) latching_entry = self . digital_latch_table [ pin ] if latching_entry [ self . LATCH_STATE ] == self . LATCH_ARMED : if latching_entry [ self . LATCHED_THRESHOLD_TYPE ] == self . DIGITAL_LATCH_LOW : if ( port_data & 0x01 ) == 0 : if latching_entry [ self . DIGITAL_LATCH_CALLBACK ] is not None : self . digital_latch_table [ pin ] = [ 0 , 0 , 0 , 0 , None ] latching_entry [ self . DIGITAL_LATCH_CALLBACK ] ( [ self . pymata . OUTPUT | self . pymata . LATCH_MODE , pin , 0 , time . time ( ) ] ) else : updated_latch_entry = latching_entry updated_latch_entry [ self . LATCH_STATE ] = self . LATCH_LATCHED updated_latch_entry [ self . DIGITAL_LATCHED_DATA ] = self . DIGITAL_LATCH_LOW updated_latch_entry [ self . DIGITAL_TIME_STAMP ] = time . time ( ) else : pass elif latching_entry [ self . LATCHED_THRESHOLD_TYPE ] == self . DIGITAL_LATCH_HIGH : if port_data & 0x01 : if latching_entry [ self . DIGITAL_LATCH_CALLBACK ] is not None : self . digital_latch_table [ pin ] = [ 0 , 0 , 0 , 0 , None ] latching_entry [ self . DIGITAL_LATCH_CALLBACK ] ( [ self . pymata . OUTPUT | self . pymata . LATCH_MODE , pin , 1 , time . time ( ) ] ) else : updated_latch_entry = latching_entry updated_latch_entry [ self . LATCH_STATE ] = self . LATCH_LATCHED updated_latch_entry [ self . DIGITAL_LATCHED_DATA ] = self . DIGITAL_LATCH_HIGH updated_latch_entry [ self . DIGITAL_TIME_STAMP ] = time . time ( ) else : pass else : pass port_data >>= 1

This method handles the incoming digital message . It stores the data values in the digital response table . Data is stored for all 8 bits of a digital port


def encoder_data(self, data):
        """
        This method handles the incoming encoder data message and stores
        the data in the digital response table.

        :param data: Message data from Firmata

        :return: No return value.
        """
        prev_val = self.digital_response_table[data[self.RESPONSE_TABLE_MODE]][self.RESPONSE_TABLE_PIN_DATA_VALUE]
        val = int((data[self.MSB] << 7) + data[self.LSB])
        # set value so that it shows positive and negative values
        if val > 8192:
            val -= 16384
        pin = data[0]
        with self.pymata.data_lock:
            self.digital_response_table[data[self.RESPONSE_TABLE_MODE]][self.RESPONSE_TABLE_PIN_DATA_VALUE] = val
            if prev_val != val:
                callback = self.digital_response_table[pin][self.RESPONSE_TABLE_CALLBACK]
                if callback is not None:
                    callback([self.pymata.ENCODER, pin,
                              self.digital_response_table[pin][self.RESPONSE_TABLE_PIN_DATA_VALUE]])

def encoder_data ( self , data ) : prev_val = self . digital_response_table [ data [ self . RESPONSE_TABLE_MODE ] ] [ self . RESPONSE_TABLE_PIN_DATA_VALUE ] val = int ( ( data [ self . MSB ] << 7 ) + data [ self . LSB ] ) if val > 8192 : val -= 16384 pin = data [ 0 ] with self . pymata . data_lock : self . digital_response_table [ data [ self . RESPONSE_TABLE_MODE ] ] [ self . RESPONSE_TABLE_PIN_DATA_VALUE ] = val if prev_val != val : callback = self . digital_response_table [ pin ] [ self . RESPONSE_TABLE_CALLBACK ] if callback is not None : callback ( [ self . pymata . ENCODER , pin , self . digital_response_table [ pin ] [ self . RESPONSE_TABLE_PIN_DATA_VALUE ] ] )

This method handles the incoming encoder data message and stores the data in the digital response table .


def sonar_data(self, data):
        """
        This method handles the incoming sonar data message and stores
        the data in the response table.

        :param data: Message data from Firmata

        :return: No return value.
        """
        val = int((data[self.MSB] << 7) + data[self.LSB])
        pin_number = data[0]
        with self.pymata.data_lock:
            sonar_pin_entry = self.active_sonar_map[pin_number]
            # also write it into the digital response table
            self.digital_response_table[data[self.RESPONSE_TABLE_MODE]][self.RESPONSE_TABLE_PIN_DATA_VALUE] = val
            # send data through callback if there is a callback function for the pin
            if sonar_pin_entry[0] is not None:
                # check if value changed since last reading
                if sonar_pin_entry[1] != val:
                    self.active_sonar_map[pin_number][0]([self.pymata.SONAR, pin_number, val])
            # update the data in the table with latest value
            sonar_pin_entry[1] = val
            self.active_sonar_map[pin_number] = sonar_pin_entry

def sonar_data ( self , data ) : val = int ( ( data [ self . MSB ] << 7 ) + data [ self . LSB ] ) pin_number = data [ 0 ] with self . pymata . data_lock : sonar_pin_entry = self . active_sonar_map [ pin_number ] self . digital_response_table [ data [ self . RESPONSE_TABLE_MODE ] ] [ self . RESPONSE_TABLE_PIN_DATA_VALUE ] = val if sonar_pin_entry [ 0 ] is not None : if sonar_pin_entry [ 1 ] != val : self . active_sonar_map [ pin_number ] [ 0 ] ( [ self . pymata . SONAR , pin_number , val ] ) sonar_pin_entry [ 1 ] = val self . active_sonar_map [ pin_number ] = sonar_pin_entry

This method handles the incoming sonar data message and stores the data in the response table .


def send_sysex(self, sysex_command, sysex_data=None):
        """
        This method will send a Sysex command to Firmata with any accompanying data

        :param sysex_command: sysex command

        :param sysex_data: data for command

        :return : No return value.
        """
        if not sysex_data:
            sysex_data = []

        # convert the message command and data to characters
        sysex_message = chr(self.START_SYSEX)
        sysex_message += chr(sysex_command)
        if len(sysex_data):
            for d in sysex_data:
                sysex_message += chr(d)
        sysex_message += chr(self.END_SYSEX)

        for data in sysex_message:
            self.pymata.transport.write(data)

def send_sysex ( self , sysex_command , sysex_data = None ) : if not sysex_data : sysex_data = [ ] sysex_message = chr ( self . START_SYSEX ) sysex_message += chr ( sysex_command ) if len ( sysex_data ) : for d in sysex_data : sysex_message += chr ( d ) sysex_message += chr ( self . END_SYSEX ) for data in sysex_message : self . pymata . transport . write ( data )


def send_command(self, command):
        """
        This method is used to transmit a non-sysex command.

        :param command: Command to send to firmata includes command + data formatted by caller

        :return : No return value.
        """
        send_message = ""
        for i in command:
            send_message += chr(i)

        for data in send_message:
            self.pymata.transport.write(data)

def send_command ( self , command ) : send_message = "" for i in command : send_message += chr ( i ) for data in send_message : self . pymata . transport . write ( data )

This method is used to transmit a non - sysex command .


def system_reset(self):
        """
        Send the reset command to the Arduino.
        It resets the response tables to their initial values

        :return: No return value
        """
        data = chr(self.SYSTEM_RESET)
        self.pymata.transport.write(data)

        # response table re-initialization
        # for each pin set the mode to input and the last read data value to zero
        with self.pymata.data_lock:
            # remove all old entries from existing tables
            for _ in range(len(self.digital_response_table)):
                self.digital_response_table.pop()

            for _ in range(len(self.analog_response_table)):
                self.analog_response_table.pop()

            # reinitialize tables
            for pin in range(0, self.total_pins_discovered):
                response_entry = [self.pymata.INPUT, 0, None]
                self.digital_response_table.append(response_entry)

            for pin in range(0, self.number_of_analog_pins_discovered):
                response_entry = [self.pymata.INPUT, 0, None]
                self.analog_response_table.append(response_entry)

def system_reset ( self ) : data = chr ( self . SYSTEM_RESET ) self . pymata . transport . write ( data ) with self . pymata . data_lock : for _ in range ( len ( self . digital_response_table ) ) : self . digital_response_table . pop ( ) for _ in range ( len ( self . analog_response_table ) ) : self . analog_response_table . pop ( ) for pin in range ( 0 , self . total_pins_discovered ) : response_entry = [ self . pymata . INPUT , 0 , None ] self . digital_response_table . append ( response_entry ) for pin in range ( 0 , self . number_of_analog_pins_discovered ) : response_entry = [ self . pymata . INPUT , 0 , None ] self . analog_response_table . append ( response_entry )

Send the reset command to the Arduino . It resets the response tables to their initial values


def _string_data(self, data):
        """
        This method handles the incoming string data message from Firmata.
        The string is printed to the console

        :param data: Message data from Firmata

        :return: No return value.s
        """
        print("_string_data:")
        string_to_print = []
        for i in data[::2]:
            string_to_print.append(chr(i))
        print("".join(string_to_print))

def _string_data ( self , data ) : print ( "_string_data:" ) string_to_print = [ ] for i in data [ : : 2 ] : string_to_print . append ( chr ( i ) ) print ( "" . join ( string_to_print ) )

This method handles the incoming string data message from Firmata . The string is printed to the console


def run(self):
        """
        This method starts the thread that continuously runs to receive and interpret
        messages coming from Firmata. This must be the last method in this file
        It also checks the deque for messages to be sent to Firmata.
        """
        # To add a command to the command dispatch table, append here.
        self.command_dispatch.update({self.REPORT_VERSION: [self.report_version, 2]})
        self.command_dispatch.update({self.REPORT_FIRMWARE: [self.report_firmware, 1]})
        self.command_dispatch.update({self.ANALOG_MESSAGE: [self.analog_message, 2]})
        self.command_dispatch.update({self.DIGITAL_MESSAGE: [self.digital_message, 2]})
        self.command_dispatch.update({self.ENCODER_DATA: [self.encoder_data, 3]})
        self.command_dispatch.update({self.SONAR_DATA: [self.sonar_data, 3]})
        self.command_dispatch.update({self.STRING_DATA: [self._string_data, 2]})
        self.command_dispatch.update({self.I2C_REPLY: [self.i2c_reply, 2]})
        self.command_dispatch.update({self.CAPABILITY_RESPONSE: [self.capability_response, 2]})
        self.command_dispatch.update({self.PIN_STATE_RESPONSE: [self.pin_state_response, 2]})
        self.command_dispatch.update({self.ANALOG_MAPPING_RESPONSE: [self.analog_mapping_response, 2]})
        self.command_dispatch.update({self.STEPPER_DATA: [self.stepper_version_response, 2]})

        while not self.is_stopped():
            if len(self.pymata.command_deque):
                # get next byte from the deque and process it
                data = self.pymata.command_deque.popleft()

                # this list will be populated with the received data for the command
                command_data = []

                # process sysex commands
                if data == self.START_SYSEX:
                    # next char is the actual sysex command
                    # wait until we can get data from the deque
                    while len(self.pymata.command_deque) == 0:
                        pass
                    sysex_command = self.pymata.command_deque.popleft()
                    # retrieve the associated command_dispatch entry for this command
                    dispatch_entry = self.command_dispatch.get(sysex_command)

                    # get a "pointer" to the method that will process this command
                    method = dispatch_entry[0]

                    # now get the rest of the data excluding the END_SYSEX byte
                    end_of_sysex = False
                    while not end_of_sysex:
                        # wait for more data to arrive
                        while len(self.pymata.command_deque) == 0:
                            pass
                        data = self.pymata.command_deque.popleft()
                        if data != self.END_SYSEX:
                            command_data.append(data)
                        else:
                            end_of_sysex = True

                            # invoke the method to process the command
                            method(command_data)
                            # go to the beginning of the loop to process the next command
                    continue

                # is this a command byte in the range of 0x80-0xff - these are the non-sysex messages

                elif 0x80 <= data <= 0xff:
                    # look up the method for the command in the command dispatch table
                    # for the digital reporting the command value is modified with port number
                    # the handler needs the port to properly process, so decode that from the command and
                    # place in command_data
                    if 0x90 <= data <= 0x9f:
                        port = data & 0xf
                        command_data.append(port)
                        data = 0x90
                    # the pin number for analog data is embedded in the command so, decode it
                    elif 0xe0 <= data <= 0xef:
                        pin = data & 0xf
                        command_data.append(pin)
                        data = 0xe0
                    else:
                        pass

                    dispatch_entry = self.command_dispatch.get(data)

                    # this calls the method retrieved from the dispatch table
                    method = dispatch_entry[0]

                    # get the number of parameters that this command provides
                    num_args = dispatch_entry[1]

                    # look at the number of args that the selected method requires
                    # now get that number of bytes to pass to the called method
                    for i in range(num_args):
                        while len(self.pymata.command_deque) == 0:
                            pass
                        data = self.pymata.command_deque.popleft()
                        command_data.append(data)
                        # go execute the command with the argument list
                    method(command_data)

                    # go to the beginning of the loop to process the next command
                    continue
            else:
                time.sleep(.1)

def run ( self ) : self . command_dispatch . update ( { self . REPORT_VERSION : [ self . report_version , 2 ] } ) self . command_dispatch . update ( { self . REPORT_FIRMWARE : [ self . report_firmware , 1 ] } ) self . command_dispatch . update ( { self . ANALOG_MESSAGE : [ self . analog_message , 2 ] } ) self . command_dispatch . update ( { self . DIGITAL_MESSAGE : [ self . digital_message , 2 ] } ) self . command_dispatch . update ( { self . ENCODER_DATA : [ self . encoder_data , 3 ] } ) self . command_dispatch . update ( { self . SONAR_DATA : [ self . sonar_data , 3 ] } ) self . command_dispatch . update ( { self . STRING_DATA : [ self . _string_data , 2 ] } ) self . command_dispatch . update ( { self . I2C_REPLY : [ self . i2c_reply , 2 ] } ) self . command_dispatch . update ( { self . CAPABILITY_RESPONSE : [ self . capability_response , 2 ] } ) self . command_dispatch . update ( { self . PIN_STATE_RESPONSE : [ self . pin_state_response , 2 ] } ) self . command_dispatch . update ( { self . ANALOG_MAPPING_RESPONSE : [ self . analog_mapping_response , 2 ] } ) self . command_dispatch . update ( { self . STEPPER_DATA : [ self . stepper_version_response , 2 ] } ) while not self . is_stopped ( ) : if len ( self . pymata . command_deque ) : data = self . pymata . command_deque . popleft ( ) command_data = [ ] if data == self . START_SYSEX : while len ( self . pymata . command_deque ) == 0 : pass sysex_command = self . pymata . command_deque . popleft ( ) dispatch_entry = self . command_dispatch . get ( sysex_command ) method = dispatch_entry [ 0 ] end_of_sysex = False while not end_of_sysex : while len ( self . pymata . command_deque ) == 0 : pass data = self . pymata . command_deque . popleft ( ) if data != self . END_SYSEX : command_data . append ( data ) else : end_of_sysex = True method ( command_data ) continue elif 0x80 <= data <= 0xff : if 0x90 <= data <= 0x9f : port = data & 0xf command_data . append ( port ) data = 0x90 elif 0xe0 <= data <= 0xef : pin = data & 0xf command_data . append ( pin ) data = 0xe0 else : pass dispatch_entry = self . command_dispatch . get ( data ) method = dispatch_entry [ 0 ] num_args = dispatch_entry [ 1 ] for i in range ( num_args ) : while len ( self . pymata . command_deque ) == 0 : pass data = self . pymata . command_deque . popleft ( ) command_data . append ( data ) method ( command_data ) continue else : time . sleep ( .1 )

This method starts the thread that continuously runs to receive and interpret messages coming from Firmata . This must be the last method in this file It also checks the deque for messages to be sent to Firmata .


def retrieve_url(self, url):
        """
        Use requests to fetch remote content
        """

        try:
            r = requests.get(url)
        except requests.ConnectionError:
            raise exceptions.RetrieveError('Connection fail')

        if r.status_code >= 400:
            raise exceptions.RetrieveError('Connected, but status code is %s' % (r.status_code))

        real_url = r.url
        content = r.content

        try:
            content_type = r.headers['Content-Type']
        except KeyError:
            content_type, encoding = mimetypes.guess_type(real_url, strict=False)

        self.response = r

        return content_type.lower(), content

def
retrieve_url
(
self
,
url
)
:
try
:
r
=
requests
.
get
(
url
)
except
requests
.
ConnectionError
:
raise
exceptions
.
RetrieveError
(
'Connection fail'
)
if
r
.
status_code
>=
400
:
raise
exceptions
.
RetrieveError
(
'Connected, but status code is %s'
%
(
r
.
status_code
)
)
real_url
=
r
.
url
content
=
r
.
content
try
:
content_type
=
r
.
headers
[
'Content-Type'
]
except
KeyError
:
content_type
,
encoding
=
mimetypes
.
guess_type
(
real_url
,
strict
=
False
)
self
.
response
=
r
return
content_type
.
lower
(
)
,
content


def image_urls(self):
        """
        Combine finder_image_urls and extender_image_urls,
        remove duplicate but keep order
        """

        all_image_urls = self.finder_image_urls[:]
        for image_url in self.extender_image_urls:
            if image_url not in all_image_urls:
                all_image_urls.append(image_url)

        return all_image_urls

def image_urls ( self ) : all_image_urls = self . finder_image_urls [ : ] for image_url in self . extender_image_urls : if image_url not in all_image_urls : all_image_urls . append ( image_url ) return all_image_urls

Combine finder_image_urls and extender_image_urls remove duplicate but keep order


def background_image_finder(pipeline_index,
                            soup,
                            finder_image_urls=[],
                            *args, **kwargs):
    """
    Find image URL in background-image

    Example:

    to
    http://distilleryimage10.ak.instagram.com/bde04558a43b11e28e5d22000a1f979a_7.jpg
    """

    now_finder_image_urls = []

    for tag in soup.find_all(style=True):
        style_string = tag['style']
        if 'background-image' in style_string.lower():
            style = cssutils.parseStyle(style_string)
            background_image = style.getProperty('background-image')
            if background_image:
                for property_value in background_image.propertyValue:
                    background_image_url = str(property_value.value)
                    if background_image_url:
                        if (background_image_url not in finder_image_urls) and \
                           (background_image_url not in now_finder_image_urls):
                            now_finder_image_urls.append(background_image_url)

    output = {}
    output['finder_image_urls'] = finder_image_urls + now_finder_image_urls

    return output

def background_image_finder ( pipeline_index , soup , finder_image_urls = [ ] , * args , ** kwargs ) : now_finder_image_urls = [ ] for tag in soup . find_all ( style = True ) : style_string = tag [ 'style' ] if 'background-image' in style_string . lower ( ) : style = cssutils . parseStyle ( style_string ) background_image = style . getProperty ( 'background-image' ) if background_image : for property_value in background_image . propertyValue : background_image_url = str ( property_value . value ) if background_image_url : if ( background_image_url not in finder_image_urls ) and ( background_image_url not in now_finder_image_urls ) : now_finder_image_urls . append ( background_image_url ) output = { } output [ 'finder_image_urls' ] = finder_image_urls + now_finder_image_urls return output


def _getnodenamefor(self, name):
        "Return the node name where the ``name`` would land to"
        return 'node_' + str(
            (abs(binascii.crc32(b(name)) & 0xffffffff) % self.no_servers) + 1)

Return the node name where the name would land to


def getnodefor(self, name):
        "Return the node where the ``name`` would land to"
        node = self._getnodenamefor(name)
        return {node: self.cluster['nodes'][node]}

Return the node where the name would land to


def object(self, infotype, key):
        "Return the encoding, idletime, or refcount about the key"
        redisent = self.redises[self._getnodenamefor(key) + '_slave']
        return getattr(redisent, 'object')(infotype, key)

Return the encoding idletime or refcount about the key


def _rc_brpoplpush(self, src, dst, timeout=0):
        """
        Pop a value off the tail of ``src``, push it on the head of ``dst``
        and then return it.

        This command blocks until a value is in ``src`` or until ``timeout``
        seconds elapse, whichever is first. A ``timeout`` value of 0 blocks
        forever.
        Not atomic
        """
        rpop = self.brpop(src, timeout)
        if rpop is not None:
            self.lpush(dst, rpop[1])
            return rpop[1]
        return None

def _rc_brpoplpush ( self , src , dst , timeout = 0 ) : rpop = self . brpop ( src , timeout ) if rpop is not None : self . lpush ( dst , rpop [ 1 ] ) return rpop [ 1 ] return None

Pop a value off the tail of src push it on the head of dst and then return it .


def _rc_rpoplpush(self, src, dst):
        """
        RPOP a value off of the ``src`` list and LPUSH it
        on to the ``dst`` list.  Returns the value.
        """
        rpop = self.rpop(src)
        if rpop is not None:
            self.lpush(dst, rpop)
            return rpop
        return None

def _rc_rpoplpush ( self , src , dst ) : rpop = self . rpop ( src ) if rpop is not None : self . lpush ( dst , rpop ) return rpop return None

RPOP a value off of the src list and LPUSH it on to the dst list . Returns the value .


def _rc_smove(self, src, dst, value):
        """
        Move ``value`` from set ``src`` to set ``dst``
        not atomic
        """
        if self.type(src) != b("set"):
            return self.smove(src + "{" + src + "}", dst, value)
        if self.type(dst) != b("set"):
            return self.smove(dst + "{" + dst + "}", src, value)
        if self.srem(src, value):
            return 1 if self.sadd(dst, value) else 0
        return 0

def _rc_smove ( self , src , dst , value ) : if self . type ( src ) != b ( "set" ) : return self . smove ( src + "{" + src + "}" , dst , value ) if self . type ( dst ) != b ( "set" ) : return self . smove ( dst + "{" + dst + "}" , src , value ) if self . srem ( src , value ) : return 1 if self . sadd ( dst , value ) else 0 return 0

Move value from set src to set dst not atomic


def _rc_sunion(self, src, *args):
        """
        Returns the members of the set resulting from the union between
        the first set and all the successive sets.
        """
        args = list_or_args(src, args)
        src_set = self.smembers(args.pop(0))
        if src_set is not set([]):
            for key in args:
                src_set.update(self.smembers(key))
        return src_set

def _rc_sunion ( self , src , * args ) : args = list_or_args ( src , args ) src_set = self . smembers ( args . pop ( 0 ) ) if src_set is not set ( [ ] ) : for key in args : src_set . update ( self . smembers ( key ) ) return src_set

Returns the members of the set resulting from the union between the first set and all the successive sets .


def _rc_sunionstore(self, dst, src, *args):
        """
        Store the union of sets ``src``,  ``args`` into a new
        set named ``dest``.  Returns the number of keys in the new set.
        """
        args = list_or_args(src, args)
        result = self.sunion(*args)
        if result is not set([]):
            return self.sadd(dst, *list(result))
        return 0

def _rc_sunionstore ( self , dst , src , * args ) : args = list_or_args ( src , args ) result = self . sunion ( * args ) if result is not set ( [ ] ) : return self . sadd ( dst , * list ( result ) ) return 0

Store the union of sets src args into a new set named dest . Returns the number of keys in the new set .


def _rc_msetnx(self, mapping):
        """
        Sets each key in the ``mapping`` dict to its corresponding value if
        none of the keys are already set
        """
        for k in iterkeys(mapping):
            if self.exists(k):
                return False

        return self._rc_mset(mapping)

def _rc_msetnx ( self , mapping ) : for k in iterkeys ( mapping ) : if self . exists ( k ) : return False return self . _rc_mset ( mapping )

Sets each key in the mapping dict to its corresponding value if none of the keys are already set


def _rc_rename(self, src, dst):
        """
        Rename key ``src`` to ``dst``
        """
        if src == dst:
            return self.rename(src + "{" + src + "}", src)
        if not self.exists(src):
            return self.rename(src + "{" + src + "}", src)

        self.delete(dst)
        ktype = self.type(src)
        kttl = self.ttl(src)

        if ktype == b('none'):
            return False

        if ktype == b('string'):
            self.set(dst, self.get(src))
        elif ktype == b('hash'):
            self.hmset(dst, self.hgetall(src))
        elif ktype == b('list'):
            for k in self.lrange(src, 0, -1):
                self.rpush(dst, k)
        elif ktype == b('set'):
            for k in self.smembers(src):
                self.sadd(dst, k)
        elif ktype == b('zset'):
            for k, v in self.zrange(src, 0, -1, withscores=True):
                self.zadd(dst, v, k)

        # Handle keys with an expire time set
        kttl = -1 if kttl is None or kttl < 0 else int(kttl)
        if kttl != -1:
            self.expire(dst, kttl)

        return self.delete(src)

def _rc_rename ( self , src , dst ) : if src == dst : return self . rename ( src + "{" + src + "}" , src ) if not self . exists ( src ) : return self . rename ( src + "{" + src + "}" , src ) self . delete ( dst ) ktype = self . type ( src ) kttl = self . ttl ( src ) if ktype == b ( 'none' ) : return False if ktype == b ( 'string' ) : self . set ( dst , self . get ( src ) ) elif ktype == b ( 'hash' ) : self . hmset ( dst , self . hgetall ( src ) ) elif ktype == b ( 'list' ) : for k in self . lrange ( src , 0 , - 1 ) : self . rpush ( dst , k ) elif ktype == b ( 'set' ) : for k in self . smembers ( src ) : self . sadd ( dst , k ) elif ktype == b ( 'zset' ) : for k , v in self . zrange ( src , 0 , - 1 , withscores = True ) : self . zadd ( dst , v , k ) kttl = - 1 if kttl is None or kttl < 0 else int ( kttl ) if kttl != - 1 : self . expire ( dst , kttl ) return self . delete ( src )


def _rc_renamenx(self, src, dst):
        "Rename key ``src`` to ``dst`` if ``dst`` doesn't already exist"
        if self.exists(dst):
            return False

        return self._rc_rename(src, dst)

Rename key src to dst if dst doesn t already exist


def _rc_keys(self, pattern='*'):
        "Returns a list of keys matching ``pattern``"

        result = []
        for alias, redisent in iteritems(self.redises):
            if alias.find('_slave') == -1:
                continue

            result.extend(redisent.keys(pattern))

        return result


def _rc_dbsize(self):
        "Returns the number of keys in the current database"

        result = 0
        for alias, redisent in iteritems(self.redises):
            if alias.find('_slave') == -1:
                continue

            result += redisent.dbsize()

        return result


def prepare(self):
        """Prepare the date in the instance state for serialization.
        """

        # Create a collection for the attributes and elements of
        # this instance.
        attributes, elements = OrderedDict(), []

        # Initialize the namespace map.
        nsmap = dict([self.meta.namespace])

        # Iterate through all declared items.
        for name, item in self._items.items():
            if isinstance(item, Attribute):
                # Prepare the item as an attribute.
                attributes[name] = item.prepare(self)

            elif isinstance(item, Element):
                # Update the nsmap.
                nsmap.update([item.namespace])

                # Prepare the item as an element.
                elements.append(item)

        # Return the collected attributes and elements
        return attributes, elements, nsmap

def prepare ( self ) : attributes , elements = OrderedDict ( ) , [ ] nsmap = dict ( [ self . meta . namespace ] ) for name , item in self . _items . items ( ) : if isinstance ( item , Attribute ) : attributes [ name ] = item . prepare ( self ) elif isinstance ( item , Element ) : nsmap . update ( [ item . namespace ] ) elements . append ( item ) return attributes , elements , nsmap

Prepare the date in the instance state for serialization .


def verify(xml, stream):
    """
    Verify the signaure of an XML document with the given certificate.
    Returns `True` if the document is signed with a valid signature.
    Returns `False` if the document is not signed or if the signature is
    invalid.

    :param lxml.etree._Element xml: The document to sign
    :param file stream: The private key to sign the document with

    :rtype: Boolean
    """
    # Import xmlsec here to delay initializing the C library in
    # case we don't need it.
    import xmlsec

    # Find the <Signature/> node.
    signature_node = xmlsec.tree.find_node(xml, xmlsec.Node.SIGNATURE)
    if signature_node is None:
        # No `signature` node found; we cannot verify
        return False

    # Create a digital signature context (no key manager is needed).
    ctx = xmlsec.SignatureContext()

    # Register <Response/> and <Assertion/>
    ctx.register_id(xml)
    for assertion in xml.xpath("//*[local-name()='Assertion']"):
        ctx.register_id(assertion)

    # Load the public key.
    key = None
    for fmt in [
            xmlsec.KeyFormat.PEM,
            xmlsec.KeyFormat.CERT_PEM]:
        stream.seek(0)
        try:
            key = xmlsec.Key.from_memory(stream, fmt)
            break
        except ValueError:
            # xmlsec now throws when it can't load the key
            pass

    # Set the key on the context.
    ctx.key = key

    # Verify the signature.
    try:
        ctx.verify(signature_node)

        return True

    except Exception:
        return False

def verify ( xml , stream ) : import xmlsec signature_node = xmlsec . tree . find_node ( xml , xmlsec . Node . SIGNATURE ) if signature_node is None : return False ctx = xmlsec . SignatureContext ( ) ctx . register_id ( xml ) for assertion in xml . xpath ( "//*[local-name()='Assertion']" ) : ctx . register_id ( assertion ) key = None for fmt in [ xmlsec . KeyFormat . PEM , xmlsec . KeyFormat . CERT_PEM ] : stream . seek ( 0 ) try : key = xmlsec . Key . from_memory ( stream , fmt ) break except ValueError : pass ctx . key = key try : ctx . verify ( signature_node ) return True except Exception : return False

Verify the signaure of an XML document with the given certificate . Returns True if the document is signed with a valid signature . Returns False if the document is not signed or if the signature is invalid .


def get_queryset(self, request):
        """
        Add number of photos to each gallery.
        """
        qs = super(GalleryAdmin, self).get_queryset(request)
        return qs.annotate(photo_count=Count('photos'))

def get_queryset ( self , request ) : qs = super ( GalleryAdmin , self ) . get_queryset ( request ) return qs . annotate ( photo_count = Count ( 'photos' ) )


def save_model(self, request, obj, form, change):
        """
        Set currently authenticated user as the author of the gallery.
        """
        obj.author = request.user
        obj.save()

def save_model ( self , request , obj , form , change ) : obj . author = request . user obj . save ( )

Set currently authenticated user as the author of the gallery .


def save_formset(self, request, form, formset, change):
        """
        For each photo set it's author to currently authenticated user.
        """
        instances = formset.save(commit=False)
        for instance in instances:
            if isinstance(instance, Photo):
                instance.author = request.user
            instance.save()

def save_formset ( self , request , form , formset , change ) : instances = formset . save ( commit = False ) for instance in instances : if isinstance ( instance , Photo ) : instance . author = request . user instance . save ( )

For each photo set it s author to currently authenticated user .


def parse_byteranges(cls, environ):
        """
        Outputs a list of tuples with ranges or the empty list
        According to the rfc, start or end values can be omitted
        """
        r = []
        s = environ.get(cls.header_range, '').replace(' ','').lower()
        if s:
            l = s.split('=')
            if len(l) == 2:
                unit, vals = tuple(l)
                if unit == 'bytes' and vals:
                    gen_rng = ( tuple(rng.split('-')) for rng in vals.split(',') if '-' in rng )
                    for start, end in gen_rng:
                        if start or end:
                            r.append( (int(start) if start else None, int(end) if end else None) )
        return r

def
parse_byteranges
(
cls
,
environ
)
:
r
=
[
]
s
=
environ
.
get
(
cls
.
header_range
,
''
)
.
replace
(
' '
,
''
)
.
lower
(
)
if
s
:
l
=
s
.
split
(
'='
)
if
len
(
l
)
==
2
:
unit
,
vals
=
tuple
(
l
)
if
unit
==
'bytes'
and
vals
:
gen_rng
=
(
tuple
(
rng
.
split
(
'-'
)
)
for
rng
in
vals
.
split
(
','
)
if
'-'
in
rng
)
for
start
,
end
in
gen_rng
:
if
start
or
end
:
r
.
append
(
(
int
(
start
)
if
start
else
,
int
(
end
)
if
end
else
)
)
return
r

Outputs a list of tuples with ranges or the empty list According to the rfc start or end values can be omitted


def check_ranges(cls, ranges, length):
        """Removes errored ranges"""
        result = []
        for start, end in ranges:
            if isinstance(start, int) or isinstance(end, int):
                if isinstance(start, int) and not (0 <= start < length):
                    continue
                elif isinstance(start, int) and isinstance(end, int) and not (start <= end):
                    continue
                elif start is None and end == 0:
                    continue
                result.append( (start,end) )
        return result

def check_ranges ( cls , ranges , length ) : result = [ ] for start , end in ranges : if isinstance ( start , int ) or isinstance ( end , int ) : if isinstance ( start , int ) and not ( 0 <= start < length ) : continue elif isinstance ( start , int ) and isinstance ( end , int ) and not ( start <= end ) : continue elif start is None and end == 0 : continue result . append ( ( start , end ) ) return result


def convert_ranges(cls, ranges, length):
        """Converts to valid byte ranges"""
        result = []
        for start, end in ranges:
            if end is None:
                result.append( (start, length-1) )
            elif start is None:
                s = length - end
                result.append( (0 if s < 0 else s, length-1) )
            else:
                result.append( (start, end if end < length else length-1) )
        return result

def convert_ranges ( cls , ranges , length ) : result = [ ] for start , end in ranges : if end is None : result . append ( ( start , length - 1 ) ) elif start is None : s = length - end result . append ( ( 0 if s < 0 else s , length - 1 ) ) else : result . append ( ( start , end if end < length else length - 1 ) ) return result


def condense_ranges(cls, ranges):
        """Sorts and removes overlaps"""
        result = []
        if ranges:
            ranges.sort(key=lambda tup: tup[0])
            result.append(ranges[0])
            for i in range(1, len(ranges)):
                if result[-1][1] + 1 >= ranges[i][0]:
                    result[-1] = (result[-1][0], max(result[-1][1], ranges[i][1]))
                else:
                    result.append(ranges[i])
        return result

def condense_ranges ( cls , ranges ) : result = [ ] if ranges : ranges . sort ( key = lambda tup : tup [ 0 ] ) result . append ( ranges [ 0 ] ) for i in range ( 1 , len ( ranges ) ) : if result [ - 1 ] [ 1 ] + 1 >= ranges [ i ] [ 0 ] : result [ - 1 ] = ( result [ - 1 ] [ 0 ] , max ( result [ - 1 ] [ 1 ] , ranges [ i ] [ 1 ] ) ) else : result . append ( ranges [ i ] ) return result


def social_widget_render(parser, token):
    """ Renders the selected social widget. You can specify optional settings
    that will be passed  to widget template.

    Sample usage:
    {% social_widget_render widget_template ke1=val1 key2=val2 %}

    For example to render Twitter follow button you can use code like this:
    {% social_widget_render 'twitter/follow_button.html' username="ev" %}
    """
    bits = token.split_contents()
    tag_name = bits[0]

    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument" %
                                  tag_name)
    args = []
    kwargs = {}

    bits = bits[1:]

    if len(bits):
        for bit in bits:
            match = kwarg_re.match(bit)
            if not match:
                raise TemplateSyntaxError("Malformed arguments to %s tag" %
                                          tag_name)
            name, value = match.groups()

            if name:
                # Replacing hyphens with underscores because
                # variable names cannot contain hyphens.
                name = name.replace('-', '_')
                kwargs[name] = parser.compile_filter(value)
            else:
                args.append(parser.compile_filter(value))

    return SocialWidgetNode(args, kwargs)

def
social_widget_render
(
parser
,
token
)
:
bits
=
token
.
split_contents
(
)
tag_name
=
bits
[
0
]
if
len
(
bits
)
<
2
:
raise
TemplateSyntaxError
(
"'%s' takes at least one argument"
%
tag_name
)
args
=
[
]
kwargs
=
{
}
bits
=
bits
[
1
:
]
if
len
(
bits
)
:
for
bit
in
bits
:
match
=
kwarg_re
.
match
(
bit
)
if
not
match
:
raise
TemplateSyntaxError
(
"Malformed arguments to %s tag"
%
tag_name
)
name
,
value
=
match
.
groups
(
)
if
name
:
name
=
name
.
replace
(
'-'
,
'_'
)
kwargs
[
name
]
=
parser
.
compile_filter
(
value
)
else
:
args
.
append
(
parser
.
compile_filter
(
value
)
)
return
SocialWidgetNode
(
args
,
kwargs
)

Renders the selected social widget . You can specify optional settings that will be passed to widget template .


def add(self, addend_mat, axis=1):
        """
        In-place addition

        :param addend_mat: A matrix to be added on the Sparse3DMatrix object
        :param axis: The dimension along the addend_mat is added
        :return: Nothing (as it performs in-place operations)
        """
        if self.finalized:
            if axis == 0:
                raise NotImplementedError('The method is not yet implemented for the axis.')
            elif axis == 1:
                for hid in xrange(self.shape[1]):
                    self.data[hid] = self.data[hid] + addend_mat
            elif axis == 2:
                raise NotImplementedError('The method is not yet implemented for the axis.')
            else:
                raise RuntimeError('The axis should be 0, 1, or 2.')
        else:
            raise RuntimeError('The original matrix must be finalized.')


def multiply(self, multiplier, axis=None):
        """
        In-place multiplication

        :param multiplier: A matrix or vector to be multiplied
        :param axis: The dim along which 'multiplier' is multiplied
        :return: Nothing (as it performs in-place operations)
        """
        if self.finalized:
            if multiplier.ndim == 1:
                if axis == 0:  # multiplier is np.array of length |haplotypes|
                    raise NotImplementedError('The method is not yet implemented for the axis.')
                elif axis == 1:  # multiplier is np.array of length |loci|
                    sz = len(multiplier)
                    multiplier_mat = lil_matrix((sz, sz))
                    multiplier_mat.setdiag(multiplier)
                    for hid in xrange(self.shape[1]):
                        self.data[hid] = self.data[hid] * multiplier_mat
                elif axis == 2:  # multiplier is np.array of length |reads|
                    for hid in xrange(self.shape[1]):
                        self.data[hid].data *= multiplier[self.data[hid].indices]
                else:
                    raise RuntimeError('The axis should be 0, 1, or 2.')
            elif multiplier.ndim == 2:
                if axis == 0:  # multiplier is sp.sparse matrix of shape |reads| x |haplotypes|
                    for hid in xrange(self.shape[1]):
                        self.data[hid].data *= multiplier[self.data[hid].indices, hid]
                elif axis == 1:  # multiplier is sp.sparse matrix of shape |reads| x |loci|
                    for hid in xrange(self.shape[1]):
                        self.data[hid] = self.data[hid].multiply(multiplier)
                elif axis == 2:  # multiplier is np.matrix of shape |haplotypes| x |loci|
                    for hid in xrange(self.shape[1]):
                        multiplier_vec = multiplier[hid, :]
                        multiplier_vec = multiplier_vec.ravel()
                        self.data[hid].data *= multiplier_vec.repeat(np.diff(self.data[hid].indptr))
                else:
                    raise RuntimeError('The axis should be 0, 1, or 2.')
            elif isinstance(multiplier, Sparse3DMatrix):  # multiplier is Sparse3DMatrix object
                    for hid in xrange(self.shape[1]):
                        self.data[hid] = self.data[hid].multiply(multiplier.data[hid])
            else:
                raise RuntimeError('The multiplier should be 1, 2 dimensional numpy array or a Sparse3DMatrix object.')
        else:
            raise RuntimeError('The original matrix must be finalized.')


def update_probability_at_read_level(self, model=3):
        """
        Updates the probability of read origin at read level

        :param model: Normalization model (1: Gene->Allele->Isoform, 2: Gene->Isoform->Allele, 3: Gene->Isoform*Allele, 4: Gene*Isoform*Allele)
        :return: Nothing (as it performs in-place operations)
        """
        self.probability.reset()  # reset to alignment incidence matrix
        if model == 1:
            self.probability.multiply(self.allelic_expression, axis=APM.Axis.READ)
            self.probability.normalize_reads(axis=APM.Axis.HAPLOGROUP, grouping_mat=self.t2t_mat)
            haplogroup_sum_mat = self.allelic_expression * self.t2t_mat
            self.probability.multiply(haplogroup_sum_mat, axis=APM.Axis.READ)
            self.probability.normalize_reads(axis=APM.Axis.GROUP, grouping_mat=self.t2t_mat)
            self.probability.multiply(haplogroup_sum_mat.sum(axis=0), axis=APM.Axis.HAPLOTYPE)
            self.probability.normalize_reads(axis=APM.Axis.READ)
        elif model == 2:
            self.probability.multiply(self.allelic_expression, axis=APM.Axis.READ)
            self.probability.normalize_reads(axis=APM.Axis.LOCUS)
            self.probability.multiply(self.allelic_expression.sum(axis=0), axis=APM.Axis.HAPLOTYPE)
            self.probability.normalize_reads(axis=APM.Axis.GROUP, grouping_mat=self.t2t_mat)
            self.probability.multiply((self.allelic_expression * self.t2t_mat).sum(axis=0), axis=APM.Axis.HAPLOTYPE)
            self.probability.normalize_reads(axis=APM.Axis.READ)
        elif model == 3:
            self.probability.multiply(self.allelic_expression, axis=APM.Axis.READ)
            self.probability.normalize_reads(axis=APM.Axis.GROUP, grouping_mat=self.t2t_mat)
            self.probability.multiply((self.allelic_expression * self.t2t_mat).sum(axis=0), axis=APM.Axis.HAPLOTYPE)
            self.probability.normalize_reads(axis=APM.Axis.READ)
        elif model == 4:
            self.probability.multiply(self.allelic_expression, axis=APM.Axis.READ)
            self.probability.normalize_reads(axis=APM.Axis.READ)
        else:
            raise RuntimeError('The read normalization model should be 1, 2, 3, or 4.')