def time(model):
    """
    Returns the time variable from the given :class:`myokit.Model` `model`.

    The method will raise a :class:`myokit.IncompatibleModelError` if no time
    variable is found.
    """
    time = model.time()
    if time is None:
        raise myokit.IncompatibleModelError(model.name(),
                                            'No time variable found.')
    return time
def label(model, label):
    """
    Returns the variable labelled `label` from the given :class:`myokit.Model`
    `model`.

    The method will raise a :class:`myokit.IncompatibleModelError` if no such
    variable is found.
    """
    var = model.label(label)
    if var is None:
        raise myokit.IncompatibleModelError(
            model.name(), 'No variable found with label "' + str(label) + '".')
    return var
def binding(model, binding):
    """
    Returns the variable bound to `binding` from the given
    :class:`myokit.Model` `model`.
    
    The method will raise a :class:`myokit.IncompatibleModelError` if no such
    variable is found.
    """
    var = model.binding(binding)
    if var is None:
        raise myokit.IncompatibleModelError(model.name(), 'No variable found'
            ' with binding "' + str(binding) + '".')
    return var
def unit(variable, unit):
    """
    Checks if the given variable's unit can be converted into units `unit` and,
    if so, returns the appropriate conversion factor. If not, a
    :class:`myokit.IncompatibleModelError` is raised.

    Example::

        >>> import myokit
        >>> import myokit.lib.multi as multi
        >>> m,p,x = myokit.load('example')
        >>> print(multi.unit(m.label('membrane_potential'), myokit.units.V))
        0.001

    (Because a millivolt can be converted to a volt by multiplying by 0.001)

    """
    try:
        return myokit.Unit.convert(1, variable.unit(), unit)
    except myokit.IncompatibleUnitError:
        raise myokit.IncompatibleModelError(
            variable.model().name(), 'Incompatible units: ' +
            str(variable.unit()) + ' and ' + str(unit) + '.')