def pre_draw(p5_instance, draw_func):
    """
    We need to run this before the actual draw to insert and update p5 env variables
    """
    global _CTX_MIDDLE, _DEFAULT_FILL, _DEFAULT_LEADMULT, _DEFAULT_STROKE, _DEFAULT_TEXT_FILL
    global ADD, ALT, ARROW, AUTO, AUDIO, AXES, BACKSPACE, BASELINE, BEVEL, BEZIER, BLEND, BLUR, BOLD, BOLDITALIC
    global BOTTOM, BURN, CENTER, CHORD, CLAMP, CLOSE, CONTROL, CORNER, CORNERS, CROSS, CURVE, DARKEST
    global DEG_TO_RAD, DEGREES, DELETE, DIFFERENCE, DILATE, DODGE, DOWN_ARROW, ENTER, ERODE, ESCAPE, EXCLUSION
    global FILL, GRAY, GRID, HALF_PI, HAND, HARD_LIGHT, HSB, HSL, IMAGE, IMMEDIATE, INVERT, ITALIC, LANDSCAPE
    global LEFT, LEFT_ARROW, LIGHTEST, LINE_LOOP, LINE_STRIP, LINEAR, LINES, MIRROR, MITER, MOVE, MULTIPLY, NEAREST
    global NORMAL, OPAQUE, OPEN, OPTION, OVERLAY, P2D, PI, PIE, POINTS, PORTRAIT, POSTERIZE, PROJECT, QUAD_STRIP, QUADRATIC
    global QUADS, QUARTER_PI, RAD_TO_DEG, RADIANS, RADIUS, REPEAT, REPLACE, RETURN, RGB, RIGHT, RIGHT_ARROW
    global ROUND, SCREEN, SHIFT, SOFT_LIGHT, SQUARE, STROKE, SUBTRACT, TAB, TAU, TEXT, TEXTURE, THRESHOLD, TOP
    global TRIANGLE_FAN, TRIANGLE_STRIP, TRIANGLES, TWO_PI, UP_ARROW, VIDEO, WAIT, WEBGL
    global frameCount, focused, displayWidth, displayHeight, windowWidth, windowHeight, width, height
    global disableFriendlyErrors, deviceOrientation, accelerationX, accelerationY, accelerationZ
    global pAccelerationX, pAccelerationY, pAccelerationZ, rotationX, rotationY, rotationZ
    global pRotationX, pRotationY, pRotationZ, turnAxis, keyIsPressed, key, keyCode, mouseX, mouseY, pmouseX, pmouseY
    global winMouseX, winMouseY, pwinMouseX, pwinMouseY, mouseButton, mouseIsPressed, touches, pixels
    _CTX_MIDDLE = p5_instance._CTX_MIDDLE
    _DEFAULT_FILL = p5_instance._DEFAULT_FILL
    _DEFAULT_LEADMULT = p5_instance._DEFAULT_LEADMULT
    _DEFAULT_STROKE = p5_instance._DEFAULT_STROKE
    _DEFAULT_TEXT_FILL = p5_instance._DEFAULT_TEXT_FILL
    ADD = p5_instance.ADD
    ALT = p5_instance.ALT
    ARROW = p5_instance.ARROW
    AUDIO = p5_instance.AUDIO
    AUTO = p5_instance.AUTO
    AXES = p5_instance.AXES
    BACKSPACE = p5_instance.BACKSPACE
    BASELINE = p5_instance.BASELINE
    BEVEL = p5_instance.BEVEL
    BEZIER = p5_instance.BEZIER
    BLEND = p5_instance.BLEND
    BLUR = p5_instance.BLUR
    BOLD = p5_instance.BOLD
    BOLDITALIC = p5_instance.BOLDITALIC
    BOTTOM = p5_instance.BOTTOM
    BURN = p5_instance.BURN
    CENTER = p5_instance.CENTER
    CHORD = p5_instance.CHORD
    CLAMP = p5_instance.CLAMP
    CLOSE = p5_instance.CLOSE
    CONTROL = p5_instance.CONTROL
    CORNER = p5_instance.CORNER
    CORNERS = p5_instance.CORNERS
    CROSS = p5_instance.CROSS
    CURVE = p5_instance.CURVE
    DARKEST = p5_instance.DARKEST
    DEG_TO_RAD = p5_instance.DEG_TO_RAD
    DEGREES = p5_instance.DEGREES
    DELETE = p5_instance.DELETE
    DIFFERENCE = p5_instance.DIFFERENCE
    DILATE = p5_instance.DILATE
    DODGE = p5_instance.DODGE
    DOWN_ARROW = p5_instance.DOWN_ARROW
    ENTER = p5_instance.ENTER
    ERODE = p5_instance.ERODE
    ESCAPE = p5_instance.ESCAPE
    EXCLUSION = p5_instance.EXCLUSION
    FILL = p5_instance.FILL
    GRAY = p5_instance.GRAY
    GRID = p5_instance.GRID
    HALF_PI = p5_instance.HALF_PI
    HAND = p5_instance.HAND
    HARD_LIGHT = p5_instance.HARD_LIGHT
    HSB = p5_instance.HSB
    HSL = p5_instance.HSL
    IMAGE = p5_instance.IMAGE
    IMMEDIATE = p5_instance.IMMEDIATE
    INVERT = p5_instance.INVERT
    ITALIC = p5_instance.ITALIC
    LANDSCAPE = p5_instance.LANDSCAPE
    LEFT = p5_instance.LEFT
    LEFT_ARROW = p5_instance.LEFT_ARROW
    LIGHTEST = p5_instance.LIGHTEST
    LINE_LOOP = p5_instance.LINE_LOOP
    LINE_STRIP = p5_instance.LINE_STRIP
    LINEAR = p5_instance.LINEAR
    LINES = p5_instance.LINES
    MIRROR = p5_instance.MIRROR
    MITER = p5_instance.MITER
    MOVE = p5_instance.MOVE
    MULTIPLY = p5_instance.MULTIPLY
    NEAREST = p5_instance.NEAREST
    NORMAL = p5_instance.NORMAL
    OPAQUE = p5_instance.OPAQUE
    OPEN = p5_instance.OPEN
    OPTION = p5_instance.OPTION
    OVERLAY = p5_instance.OVERLAY
    P2D = p5_instance.P2D
    P3D = p5_instance.WEBGL
    PI = p5_instance.PI
    PIE = p5_instance.PIE
    POINTS = p5_instance.POINTS
    PORTRAIT = p5_instance.PORTRAIT
    POSTERIZE = p5_instance.POSTERIZE
    PROJECT = p5_instance.PROJECT
    QUAD_STRIP = p5_instance.QUAD_STRIP
    QUADRATIC = p5_instance.QUADRATIC
    QUADS = p5_instance.QUADS
    QUARTER_PI = p5_instance.QUARTER_PI
    RAD_TO_DEG = p5_instance.RAD_TO_DEG
    RADIANS = p5_instance.RADIANS
    RADIUS = p5_instance.RADIUS
    REPEAT = p5_instance.REPEAT
    REPLACE = p5_instance.REPLACE
    RETURN = p5_instance.RETURN
    RGB = p5_instance.RGB
    RIGHT = p5_instance.RIGHT
    RIGHT_ARROW = p5_instance.RIGHT_ARROW
    ROUND = p5_instance.ROUND
    SCREEN = p5_instance.SCREEN
    SHIFT = p5_instance.SHIFT
    SOFT_LIGHT = p5_instance.SOFT_LIGHT
    SQUARE = p5_instance.SQUARE
    STROKE = p5_instance.STROKE
    SUBTRACT = p5_instance.SUBTRACT
    TAB = p5_instance.TAB
    TAU = p5_instance.TAU
    TEXT = p5_instance.TEXT
    TEXTURE = p5_instance.TEXTURE
    THRESHOLD = p5_instance.THRESHOLD
    TOP = p5_instance.TOP
    TRIANGLE_FAN = p5_instance.TRIANGLE_FAN
    TRIANGLE_STRIP = p5_instance.TRIANGLE_STRIP
    TRIANGLES = p5_instance.TRIANGLES
    TWO_PI = p5_instance.TWO_PI
    UP_ARROW = p5_instance.UP_ARROW
    VIDEO = p5_instance.VIDEO
    WAIT = p5_instance.WAIT
    WEBGL = p5_instance.WEBGL
    frameCount = p5_instance.frameCount
    focused = p5_instance.focused
    displayWidth = p5_instance.displayWidth
    displayHeight = p5_instance.displayHeight
    windowWidth = p5_instance.windowWidth
    windowHeight = p5_instance.windowHeight
    width = p5_instance.width
    height = p5_instance.height
    disableFriendlyErrors = p5_instance.disableFriendlyErrors
    deviceOrientation = p5_instance.deviceOrientation
    accelerationX = p5_instance.accelerationX
    accelerationY = p5_instance.accelerationY
    accelerationZ = p5_instance.accelerationZ
    pAccelerationX = p5_instance.pAccelerationX
    pAccelerationY = p5_instance.pAccelerationY
    pAccelerationZ = p5_instance.pAccelerationZ
    rotationX = p5_instance.rotationX
    rotationY = p5_instance.rotationY
    rotationZ = p5_instance.rotationZ
    pRotationX = p5_instance.pRotationX
    pRotationY = p5_instance.pRotationY
    pRotationZ = p5_instance.pRotationZ
    turnAxis = p5_instance.turnAxis
    keyIsPressed = p5_instance.keyIsPressed
    key = p5_instance.key
    keyCode = p5_instance.keyCode
    mouseX = p5_instance.mouseX
    mouseY = p5_instance.mouseY
    pmouseX = p5_instance.pmouseX
    pmouseY = p5_instance.pmouseY
    winMouseX = p5_instance.winMouseX
    winMouseY = p5_instance.winMouseY
    pwinMouseX = p5_instance.pwinMouseX
    pwinMouseY = p5_instance.pwinMouseY
    mouseButton = p5_instance.mouseButton
    mouseIsPressed = p5_instance.mouseIsPressed
    touches = p5_instance.touches
    pixels = p5_instance.pixels
    return draw_func()


@classmethod
def alloc(cls, handle, token, size):
    """ Dispatch a cache buffer allocation request to the correct parse job """
    return cls._jobs[handle].alloc_cache(token, size)


def render_pep440_pre(pieces):
    """TAG[.post.devDISTANCE] -- No -dirty.

    Exceptions:
    1: no tags. 0.post.devDISTANCE
    """
    if pieces['closest-tag']:
        rendered = pieces['closest-tag']
        if pieces['distance']:
            rendered += '.post.dev%d' % pieces['distance']
    else:
        rendered = '0.post.dev%d' % pieces['distance']
    return rendered


@staticmethod
def get_name():
    """
        Get the name of the extractor.
        :return: the name of the extractor
        """
    return 'Fast5'


def var_mapper(data, mapping):
    """
    Map data column values to generated values

    Parameters
    ----------
    data : DataFrame
        The DataFrame to access data from
    mapping : dict
        A nested dictionary where the top-level keys are the column
        names.  The values are a dictionary where the keys are
        the variable to generate and the values are dictionaries
        with the old-value / new-value pairs.

    Returns
    -------
    dict of lists

    """
    out = {}
    for col, attrs in mapping.items():
        for aname, attrmap in attrs.items():
            if isinstance(attrmap, dict):
                out[aname] = list(data[col].map(attrmap))
            else:
                out[aname] = list(attrmap(data[col]))
    return out


def replyToMessage(instance, message):
    """ Reply to a IMS message
        """


def with_metaclass(meta, *bases):
    """Create a base class with a metaclass."""
    return meta('NewBase', bases, {})


def draw_if_needed(canvas):
    """
    Force canvas to be drawn if there are drawing commands outstanding.

    This is needed if the application has emitted a draw_idle() that hasn't
    had time to process prior to the mouse event detection running.
    """
    if not canvas._isDrawn:
        canvas.draw()


def get_value():
    """ Returns the opaque value that was stored. """


def ga(n):
    """generates square of even nonnegative integers less than n"""
    for e in range(n):
        if e % 2 == 0:
            yield e * e


@classmethod
def make_path(cls, shards_account, root_container, parent_container,
    timestamp, index):
    """
        Returns a path for a shard container that is valid to use as a name
        when constructing a :class:`~swift.common.utils.ShardRange`.

        :param shards_account: the hidden internal account to which the shard
            container belongs.
        :param root_container: the name of the root container for the shard.
        :param parent_container: the name of the parent container for the
            shard; for initial first generation shards this should be the same
            as ``root_container``; for shards of shards this should be the name
            of the sharding shard container.
        :param timestamp: an instance of :class:`~swift.common.utils.Timestamp`
        :param index: a unique index that will distinguish the path from any
            other path generated using the same combination of
            ``shards_account``, ``root_container``, ``parent_container`` and
            ``timestamp``.
        :return: a string of the form <account_name>/<container_name>
        """
    shard_container = cls._make_container_name(root_container,
        parent_container, timestamp, index)
    return '%s/%s' % (shards_account, shard_container)


def isnum(*xs):
    """ Tests if all of xs are numeric

    :param xs: vals of whatever

    :returns: True or False
    """
    try:
        for x in xs:
            float(x)
        return True
    except (ValueError, TypeError):
        return False


def djb2_hash(text, size):
    """ Implementation of the djb2 hash, a simple hash function with a
    configurable table size.

    ** Do NOT use for cryptography! **

    """
    hash = 5381
    for char in text:
        hash = (hash << 5) + hash + ord(char)
    return hash % size


def instance(rolecls, subj):
    """
    Apply the role class to the subject. This is the default role assignment
    method.

    >>> class Person(object):
    ...     def __init__(self, name): self.name = name
    ...     def am(self): print self.name, 'is'
    >>> class Biker(object):
    ...     __metaclass__ = RoleType
    ...     def bike(self): print self.name, 'bikes'
    ...
    >>> person = Person('Joe')
    >>> biker = Biker(person, method=instance)
    >>> biker # doctest: +ELLIPSIS
    <roles.role.Person+Biker object at 0x...>
    >>> person # doctest: +ELLIPSIS
    <roles.role.Person+Biker object at 0x...>
    >>> person is biker
    True
    >>> biker.bike()
    Joe bikes
    """
    subj.__class__ = rolecls
    return subj


def escape_markup(text):
    """
    Escape markup characters found in the text. Intended to be used when markup
    text is activated on the Label::

        untrusted_text = escape_markup('Look at the example [1]')
        text = '[color=ff0000]' + untrusted_text + '[/color]'
        w = Label(text=text, markup=True)

    .. versionadded:: 1.3.0
    """
    return text.replace('&', '&').replace('[', '&bl;').replace(']', '&br;')


def guess_contains(lines, version_map):
    """Tries to determine if a file includes sqlite
    """
    for line in lines:
        for mapping in version_map:
            if mapping[1] in line:
                return mapping[0]
    signatures = [
        'unable to open a temporary database file for storing temporary tables'
        , 'json_object() requires an even number of arguments',
        'ESCAPE expression must be a single character', 'SQLite version %s']
    for line in lines:
        for signature in signatures:
            if signature in line:
                return 1
    return 0


def p_predicate_in(p):
    """predicate : bit_expr IN '(' list_expr ')'"""
    p[0] = 'IN', p[1], p[4]


@staticmethod
def _extract_procpar_bruker(procpar, filepath):
    """
        Extract some commonly-used NMR parameters (using Bruker denotations)
        and return a parameter dictionary 'params'.
        """
    d1 = procpar['RD']
    reffrq = procpar['SFO1']
    nt = procpar['NS']
    sw_hz = procpar['SW_h']
    sw = procpar['SW']
    with open(filepath + '/pdata/1/procs') as f:
        for i in f.readlines():
            if 'OFFSET' in i:
                sw_left = float(i.split(' ')[1])
    at = procpar['TD'] / (2 * sw_hz)
    rt = at + d1
    acqtime = nt * rt / 60.0
    params = dict(at=at, d1=d1, reffrq=reffrq, rt=rt, nt=nt, acqtime=
        acqtime, sw_left=sw_left, sw=sw, sw_hz=sw_hz)
    return params


def PowerlawPlusConstant(q, A, alpha, C):
    """A power-law curve plus a constant

    Inputs:
    -------
        ``q``: independent variable (momentum transfer)
        ``A``: scaling factor
        ``alpha``: exponent
        ``C``: additive constant

    Formula:
    --------
        ``A*q^alpha + C``
    """
    return A * q ** alpha + C


def _fix_uncommitted_labels(client):
    """Ensure files have correct label instantiation."""
    for dataset in client.datasets.values():
        for file_ in dataset.files:
            try:
                _, commit, _ = client.resolve_in_submodules(client.
                    find_previous_commit(file_.path, revision='HEAD'),
                    file_.path)
                file_.commit = commit
                if 'UNCOMMITTED' in file_._label or '@' not in file_._label:
                    file_._label = file_.default_label()
                    file_._id = file_.default_id()
            except KeyError:
                pass
        dataset.to_yaml()


@classmethod
def fromkeys(cls, iterable, value=None):
    """OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S
        and values equal to v (which defaults to None).

        """
    d = cls()
    for key in iterable:
        d[key] = value
    return d


def relist_streamlines(points, offsets):
    """ Given a representation of a set of streamlines as a large array and
    an offsets array return the streamlines as a list of shorter arrays.

    Parameters
    -----------
    points : array
    offsets : array

    Returns
    -------
    streamlines: sequence
    """
    streamlines = []
    streamlines.append(points[0:offsets[0]])
    for i in range(len(offsets) - 1):
        streamlines.append(points[offsets[i]:offsets[i + 1]])
    return streamlines


@classmethod
def java_udt(cls):
    """
        The class name of the paired Java UDT (could be '', if there
        is no corresponding one).
        """
    return ''


@staticmethod
def filter_with_english_letters(buf):
    """
        Returns a copy of ``buf`` that retains only the sequences of English
        alphabet and high byte characters that are not between <> characters.
        Also retains English alphabet and high byte characters immediately
        before occurrences of >.

        This filter can be applied to all scripts which contain both English
        characters and extended ASCII characters, but is currently only used by
        ``Latin1Prober``.
        """
    filtered = bytearray()
    in_tag = False
    prev = 0
    for curr in range(len(buf)):
        buf_char = buf[curr:curr + 1]
        if buf_char == b'>':
            in_tag = False
        elif buf_char == b'<':
            in_tag = True
        if buf_char < b'\x80' and not buf_char.isalpha():
            if curr > prev and not in_tag:
                filtered.extend(buf[prev:curr])
                filtered.extend(b' ')
            prev = curr + 1
    if not in_tag:
        filtered.extend(buf[prev:])
    return filtered


def p_preprocessor_line_line(p):
    """ preproc_line_line : _LINE INTEGER
    """
    p.lexer.lineno = int(p[2]) + p.lexer.lineno - p.lineno(2)


def collect_driver_info(driver):
    """Build the dictionary that describes this driver."""
    info = {'name': driver.class_name, 'version': driver.version, 'fqn':
        driver.class_fqn, 'description': driver.desc, 'ci_wiki_name':
        driver.ci_wiki_name, 'supported': driver.supported, 'options':
        driver.driver_options}
    return info


def collapse_to_unique_list(*args):
    """Create a new list with all elements from a given lists without reapeated
    elements

    """
    combined = []
    for arg in args:
        for elem in (arg or ()):
            if elem not in combined:
                combined.append(elem)
    return combined


def copy_permissions(original, duplicate, model, apps):
    """Copy permissions from original object to it's duplicate."""
    UserObjectPermission = apps.get_model('guardian', 'UserObjectPermission')
    GroupObjectPermission = apps.get_model('guardian', 'GroupObjectPermission')
    kwargs = {'permission__content_type__model': model, 'object_pk':
        original.id}
    for perm in UserObjectPermission.objects.filter(**kwargs):
        perm.id = None
        perm.object_pk = duplicate.id
        perm.content_object = duplicate
        perm.save(force_insert=True)
    for perm in GroupObjectPermission.objects.filter(**kwargs):
        perm.id = None
        perm.object_pk = duplicate.id
        perm.content_object = duplicate
        perm.save(force_insert=True)


def _service_url_from_hostport(secure, host, port):
    """ Create appropriate service service URL.
        Note: Currently, https does not work for lcoalhost. Though it does work
                work on staging.
    """
    if secure:
        protocol = 'https://'
    else:
        protocol = 'http://'
    return ''.join([protocol, host, ':', str(port), '/_rpc/v1/reports/binary'])


def emojis(config, args):
    """
  List available emojis.
  """
    yield config.github.emojis()


def handle_result(result):
    """ Handles the result string and returns a boolean if possible"""
    if result == 'notapplicable':
        return -1
    if result == 'pass':
        return 1
    return 0


def feed_hdf5_tme(dtsFunc, objQ, vecSplt):
    """
    Feed FIFO queue for placement of voxel time courses segments in hdf5 file.

    Parameters
    ----------
    dtsFunc : h5py dataset
        Dataset within h5py file.
    objQ : queue.Queue
        Queue from which voxel time courses are retrieved.
    vecSplt : np.array
        Indices of chunks of volumes in fMRI time course. Looping volume by
        volume is too slow. Instread, read & write a chunk of volumes at a
        time.

    Notes
    -----
    Place temporal segments (i.e. chunks of volumes) in hdf5 file. There is a
    corresponding function for placement of spatial segments.

    """
    varNumCnk = vecSplt.shape[0]
    for idxChnk in range(varNumCnk - 1):
        varIdx01 = vecSplt[idxChnk]
        varIdx02 = vecSplt[idxChnk + 1]
        dtsFunc[varIdx01:varIdx02, :] = objQ.get()


def client():
    """Don't do anything."""
    pass


@staticmethod
def _interp_temporal(a):
    """
        Helper function for temporal interpolation
        """
    import scipy.interpolate as si
    i, j, old_time, data, new_time, kind = a
    f = si.interp1d(old_time, data, kind=kind)
    new_data = f(new_time)
    return i, j, new_data


def _retain_centroids(numbers, thres):
    """Only keep one number for each cluster within thres of each other"""
    numbers.sort()
    prev = -1
    ret = []
    for n in numbers:
        if prev < 0 or n - prev > thres:
            ret.append(n)
            prev = n
    return ret


def human_time(t):
    """
    >>> human_time(1.5)
    '1.5sec'

    >>> human_time(61)
    '1.0min'

    >>> human_time(3600)
    '60.0min'
    """
    if t > 3600:
        divisor = 3600.0
        unit = 'h'
    elif t > 60:
        divisor = 60.0
        unit = 'min'
    else:
        divisor = 1
        unit = 'sec'
    return f'{round(t / divisor, 2):.1f}{unit}'


def write_home_templates():
    """Writes several basic templates to the current directory includes index.html
    and a series of templates required for registrations"""
    template_names = ['index.html', 'activate.html', 'login.html',
        'logout.html', 'password_change_done.html',
        'password_change_form.html', 'password_reset_complete.html',
        'password_reset_confirm.html', 'password_reset_done.html',
        'password_reset_email.html', 'password_reset_form.html',
        'registration_complete.html', 'registration_form.html']
    pass


def _Plus(lhs=None, rhs=None, name=None, attr=None, out=None, **kwargs):
    """Adds arguments element-wise.

    The storage type of ``elemwise_add`` output depends on storage types of inputs

       - elemwise_add(row_sparse, row_sparse) = row_sparse
       - elemwise_add(csr, csr) = csr
       - elemwise_add(default, csr) = default
       - elemwise_add(csr, default) = default
       - elemwise_add(default, rsp) = default
       - elemwise_add(rsp, default) = default
       - otherwise, ``elemwise_add`` generates output with default storage

    Parameters
    ----------
    lhs : Symbol
        first input
    rhs : Symbol
        second input

    name : string, optional.
        Name of the resulting symbol.

    Returns
    -------
    Symbol
        The result symbol.
    """
    return 0,


def makeURL(fredcode):
    """Create http address to access FRED's CSV files."""
    return ('http://research.stlouisfed.org/fred2/series/' + fredcode +
        '/downloaddata/' + fredcode + '.csv')


def psi_K_MgOH_Cl_HMW84(T, P):
    """c-c'-a: potassium magnesium-hydroxide chloride [HMW84]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


def fix_django_headers(meta):
    """
	Fix this nonsensical API:
	https://docs.djangoproject.com/en/1.11/ref/request-response/
	https://code.djangoproject.com/ticket/20147
	"""
    ret = {}
    for k, v in meta.items():
        if k.startswith('HTTP_'):
            k = k[len('HTTP_'):]
        elif k not in ('CONTENT_LENGTH', 'CONTENT_TYPE'):
            continue
        ret[k.lower().replace('_', '-')] = v
    return ret


def truthy(value):
    """ Stringy Truthyness
    """
    value = str(value).lower().strip(' ')
    if value in ['none', 'false', '0', 'nope', '', '[]']:
        return False
    else:
        return True


def short_yname(nevra):
    """ given a nevra dict, get a dict of options to pass to functions
    like yum.YumBase.rpmdb.searchNevra(), which expect short names
    (e.g., "rel" instead of "release") """
    rv = nevra.copy()
    if 'version' in rv:
        rv['ver'] = rv['version']
        del rv['version']
    if 'release' in rv:
        rv['rel'] = rv['release']
        del rv['release']
    return rv


def size(n):
    """Return data unit"""
    symbols = 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
    prefix = {}
    for i, s in enumerate(symbols):
        prefix[s] = 1 << (i + 1) * 10
    for s in reversed(symbols):
        if n >= prefix[s]:
            value = float(n) / prefix[s]
            return '%.1f%s' % (value, s)
    return '%sB' % n


def __init__(A):
    """ExecutionRule - a model defined in Swagger"""
    A.discriminator = None


def map_func(x):
    """Execute a function with given arguments, both of them passed as an argument

    This function is used to execute map operations on a function taking an
    arbitrary number of arguments

    Parameters
    ----------
    x : tuple (func, args)
        A tuple where the first argument is the function to execute and the
        second argument is a tuple of arguments
    """
    func, args = x
    return func(*args)


def default_argument_preprocessor(args):
    """Return unmodified args and an empty dict for extras"""
    extras = {}
    return args, extras


def legend_event(win, plot, figure, frame, lfsize, flegend_box, leg_col_box,
    conf, leg_loc_box, ncol_combo, loaded_plot):
    """
    This function change the propertoes of the legend.
    Parameters:
    -----------
    win             FigureCanvas, obj
    plot            subplot, obj
    figure          Figure, obj
    frame           QCheckBox
    lfsize          QSpinBox
    flegend_combo   QComboBox
    leg_col_combo   QComboBox
    ncol_combo      QComboBox
    loaded_plot     conf
    """
    frame_status = frame.isChecked()
    fontsize = lfsize.value()
    flegend = flegend_box.currentText()
    leg_col = leg_col_box.currentText()
    leg_loc = leg_loc_box.currentText()
    ncol = ncol_combo.currentText()
    if frame_status == True:
        conf['Frame'] = 'on'
    if frame_status == False:
        conf['Frame'] = 'off'
    conf['font_size'] = fontsize
    conf['Label_font_color'] = leg_col
    conf['Legend_font'] = flegend
    conf['location'] = leg_loc
    loaded_plot.plotconf['types']['ncol'] = ncol
    leg = plot.legend(loc=leg_loc, fontsize=fontsize, ncol=int(ncol))
    leg.get_frame().set_facecolor('none')
    leg.set_frame_on(frame_status)
    handles, labels = plot.get_legend_handles_labels()
    if len(handles) > 0:
        for i in leg.get_texts():
            i.set_y(-2)
            i.set_color(leg_col)
            i.set_fontname(flegend)
    else:
        leg.remove()
    figure.tight_layout()
    win.draw()
    return frame_status


def smart_map(func, i, *args, **kwargs):
    """
    将单个对象或列表中的每个项传入给定的函数，并返回单个结果或列表结果，类似于 map 函数

    :param func: 传入到的函数
    :param i: 列表或单个对象
    :param args: func 函数所需的 args
    :param kwargs: func 函数所需的 kwargs
    :return: 若传入的为列表，则以列表返回每个结果，反之为单个结果
    """
    if isinstance(i, (list, tuple, set)):
        return list(map(lambda x: func(x, *args, **kwargs), i))
    else:
        return func(i, *args, **kwargs)


@classmethod
def _exported_params(cls):
    """ Define which parameters are exported/serialized to the GUI"""
    return (cls.MP_ID, cls.MP_NAME, cls.MP_PRIORITY, cls.MP_HIERARCHY, cls.
        MP_DEFINITION_ORDER, cls.MP_DOC, cls.MP_VIEW, cls.MP_TYPE)


def fix_newlines(inval, outval):
    """
    Fixes submitted translations by filtering carriage returns and pairing
    newlines at the begging and end of the translated string with the original
    """
    if 0 == len(inval) or 0 == len(outval):
        return outval
    if '\r' in outval and '\r' not in inval:
        outval = outval.replace('\r', '')
    if '\n' == inval[0] and '\n' != outval[0]:
        outval = '\n' + outval
    elif '\n' != inval[0] and '\n' == outval[0]:
        outval = outval.lstrip()
    if '\n' == inval[-1] and '\n' != outval[-1]:
        outval = outval + '\n'
    elif '\n' != inval[-1] and '\n' == outval[-1]:
        outval = outval.rstrip()
    return outval


@classmethod
def from_subread(cls, file_path, sample_data=None, sample_mapping=None, **
    kwargs):
    """Reads expression from a subread output file."""
    return super().from_csv(file_path, sample_data=sample_data,
        sample_mapping=sample_mapping, drop_cols=['Chr', 'Start', 'End',
        'Strand', 'Length'], index_col=0, sep='\t', **kwargs)


def paths_range_update():
    """Update frame range for motion paths from the Scene’s current frame range

    """
    pass


def exclude_lan_ips(local_ip):
    """
    Given a candidate that may be a Public IP address, check that it is not
    a LAN IP address.  If it is not a LAN IP address, return the input
    unchanged.  Otherwise return None.

    Parameters
    ----------
    local_ip : unicode or None

    Returns
    -------
    local_ip : unicode or None
    """
    if isinstance(local_ip, bytes):
        local_ip = local_ip.decode('utf-8')
    if local_ip is None:
        pass
    elif local_ip.startswith('192.168.'):
        local_ip = None
    elif local_ip in ['10.0.0.0', '8.8.8.8']:
        local_ip = None
    else:
        pass
    return local_ip


def break_up_lines(text, max_line_width=80):
    """Break up the text in the given string so no line is longer than
    `max_line_width`. Any existing line endings are left unchanged.

    If any containing word is longer than this line width, then it will
    be broken up in order to meet the line width restrictions. A "word"
    means any consecutive string of characters not separated by whitespace.

    @param text {str} The string in which to break up words.
    @param max_line_width {int} The maximum line width allowed. Default is 80.
    """
    import textwrap
    lines = []
    for line in text.split('\n'):
        if not line:
            lines.append(line)
        else:
            lines += textwrap.wrap(line, max_line_width)
    return '\n'.join(lines)


def dense2cvxopt(value):
    """Converts a NumPy matrix to a CVXOPT matrix.

    Parameters
    ----------
    value : NumPy matrix/ndarray
        The matrix to convert.

    Returns
    -------
    CVXOPT matrix
        The converted matrix.
    """
    import cvxopt
    return cvxopt.matrix(value, tc='d')


def islst(myitem):
    """Check if item is a tuple or list"""
    return isinstance(myitem, tuple) or isinstance(myitem, list)


def release_commit_lock(cursor):
    """Release the commit lock"""


def _get_subquery_from_crosstab_call(ct):
    """
    Crosstabs are a useful feature some sites choose to enable on
    their datastore databases. To support the sql parameter passed
    safely we accept only the simple crosstab(text) form where text
    is a literal SQL string, otherwise raise ValueError
    """
    if not ct.startswith("crosstab('") or not ct.endswith("'::text)"):
        raise ValueError('only simple crosstab calls supported')
    ct = ct[10:-8]
    if "'" in ct.replace("''", ''):
        raise ValueError('only escaped single quotes allowed in query')
    return ct.replace("''", "'")


@staticmethod
def str_to_int(string):
    """ Convert a string or an integer to a `(string, integer)` couple.

        Returns together the original string and its integer representation if
        convertion is successful and lossless. Else returns the original string
        and `None`."""
    try:
        integer = int(string)
    except ValueError:
        return str(string), None
    string = str(string)
    str_int = string.lstrip('0')
    if not str_int:
        str_int = '0'
    if str(integer) != str_int:
        raise TypeError('{!r} string is not equivalent to {!r} integer'.
            format(string, integer))
    return string, integer


def p_fields0(p):
    """
    fields : TERMINAL_FIELD
    """
    p[0] = []


def softmax(input, dim=-1):
    """Apply the softmax function on given dimension.

    Parameters
    ----------
    input : Tensor
        The input tensor.
    dim : int
        The dimension along which to compute softmax.

    Returns
    -------
    Tensor
        The output tensor.
    """
    pass


def to_compressed_string(val, max_length=0):
    """Converts val to a compressed string.

  A compressed string is one with no leading or trailing spaces.

  If val is None, or is blank (all spaces) None is returned.

  If max_length > 0 and  the stripped val is greater than max_length, val[:max_length] is returned.
  """
    if val is None or len(val) == 0:
        return None
    rval = ' '.join(val.split())
    if len(rval) == 0:
        return None
    if max_length == 0:
        return rval
    else:
        return rval[:max_length]


def __init__(source, content):
    """Needs a ISource (from zope schema) and a DX content object"""


@classmethod
def redim(cls, dataset, dimensions):
    """
        Rename coords on the Cube.
        """
    new_dataset = dataset.data.copy()
    for name, new_dim in dimensions.items():
        if name == new_dataset.name():
            new_dataset.rename(new_dim.name)
        for coord in new_dataset.dim_coords:
            if name == coord.name():
                coord.rename(new_dim.name)
    return new_dataset


@staticmethod
def _fix_right_crossed(edges):
    """Change right crossed edges to a horizontal bar.

        :param edges: List of edges is **mutated** when a problem is found.
        """
    for index, (start, end) in enumerate(edges):
        if end > start:
            edges[index] = start, end + 1


@classmethod
def concat(cls, conditions):
    """ Builder method to build conditions[0] OR conditions[1] OR ...
        analogously for AND, ..."""
    concated_condition = conditions[0]
    for condition in conditions[1:]:
        concated_condition = cls(concated_condition, condition)
    return concated_condition


def project(histogram, *args):
    """
    Project to a single axis or several axes on a multidiminsional histogram.
    Provided a list of axis numbers, this will produce the histogram over those
    axes only. Flow bins are used if available.
    """
    return histogram._project(*args)


def create_plane_track():
    """Create new plane track out of selected point tracks

    """
    pass


def to_list(x):
    """Convert a value to a list.

    Parameters
    ----------
    x
        Value.

    Returns
    -------
    `list`

    Examples
    --------
    >>> to_list(0)
    [0]
    >>> to_list({'x': 0})
    [{'x': 0}]
    >>> to_list(x ** 2 for x in range(3))
    [0, 1, 4]
    >>> x = [1, 2, 3]
    >>> to_list(x)
    [1, 2, 3]
    >>> _ is x
    True
    """
    if isinstance(x, list):
        return x
    if not isinstance(x, dict):
        try:
            return list(x)
        except TypeError:
            pass
    return [x]


def get_right(stereo_samp):
    """Return StereoSample stereo_samp's right value."""
    return stereo_samp.get_right()


def _objective_func(f, x_full, k_params, alpha, *args):
    """
    The regularized objective function
    """
    x_params = x_full[:k_params]
    x_added = x_full[k_params:]
    return f(x_params, *args) + (alpha * x_added).sum()


@classmethod
def summary(cls, excl_fpath, gen_fpath, tm_dset, excl_dict=None,
    area_filter_kernel='queen', min_area=None, check_excl_layers=False,
    resolution=64, gids=None, res_class_dset=None, res_class_bins=None,
    cf_dset='cf_mean-means', lcoe_dset='lcoe_fcr-means', data_layers=None,
    power_density=None, friction_fpath=None, friction_dset=None, args=None,
    excl_area=0.0081, max_workers=None, offshore_capacity=600,
    offshore_gid_counts=494, offshore_pixel_area=4):
    """Get the supply curve points aggregation summary.

        Parameters
        ----------
        excl_fpath : str
            Filepath to exclusions h5 with techmap dataset.
        gen_fpath : str
            Filepath to .h5 reV generation output results.
        tm_dset : str
            Dataset name in the techmap file containing the
            exclusions-to-resource mapping data.
        excl_dict : dict | None
            Dictionary of exclusion LayerMask arugments {layer: {kwarg: value}}
        area_filter_kernel : str
            Contiguous area filter method to use on final exclusions mask
        min_area : float | None
            Minimum required contiguous area filter in sq-km
        check_excl_layers : bool
            Run a pre-flight check on each exclusion layer to ensure they
            contain un-excluded values
        resolution : int | None
            SC resolution, must be input in combination with gid. Prefered
            option is to use the row/col slices to define the SC point instead.
        gids : list | None
            List of gids to get summary for (can use to subset if running in
            parallel), or None for all gids in the SC extent.
        res_class_dset : str | None
            Dataset in the generation file dictating resource classes.
            None if no resource classes.
        res_class_bins : list | None
            List of floats or ints (bin edges) to convert to list of two-entry
            bin boundaries or list of two-entry bind boundaries in final format
        cf_dset : str
            Dataset name from f_gen containing capacity factor mean values.
        lcoe_dset : str
            Dataset name from f_gen containing LCOE mean values.
        data_layers : None | dict
            Aggregation data layers. Must be a dictionary keyed by data label
            name. Each value must be another dictionary with "dset", "method",
            and "fpath".
        power_density : float | str | None
            Power density in MW/km2 or filepath to variable power
            density file. None will attempt to infer a constant
            power density from the generation meta data technology
        friction_fpath : str | None
            Filepath to friction surface data (cost based exclusions).
            Must be paired with friction_dset.
        friction_dset : str | None
            Dataset name in friction_fpath for the friction surface data.
            Must be paired with friction_fpath. Must be same shape as
            exclusions.
        args : tuple | list | None
            List of summary arguments to include. None defaults to all
            available args defined in the class attr.
        excl_area : float
            Area of an exclusion cell (square km).
        max_workers : int | None
            Number of cores to run summary on. None is all
            available cpus.
        offshore_capacity : int | float
            Offshore resource pixel generation capacity in MW.
        offshore_gid_counts : int
            Approximate number of exclusion pixels that would fall into an
            offshore pixel area.
        offshore_pixel_area : int | float
            Approximate area of offshore resource pixels in km2.

        Returns
        -------
        summary : DataFrame
            Summary of the SC points.
        """
    agg = cls(excl_fpath, gen_fpath, tm_dset, excl_dict, res_class_dset=
        res_class_dset, res_class_bins=res_class_bins, cf_dset=cf_dset,
        lcoe_dset=lcoe_dset, data_layers=data_layers, resolution=resolution,
        power_density=power_density, gids=gids, friction_fpath=
        friction_fpath, friction_dset=friction_dset, area_filter_kernel=
        area_filter_kernel, min_area=min_area, check_excl_layers=
        check_excl_layers)
    summary = agg.summarize(args=args, excl_area=excl_area, max_workers=
        max_workers, offshore_capacity=offshore_capacity,
        offshore_gid_counts=offshore_gid_counts, offshore_pixel_area=
        offshore_pixel_area)
    return summary


def render_git_describe(pieces):
    """TAG[-DISTANCE-gHEX][-dirty].

    Like 'git describe --tags --dirty --always'.

    Exceptions:
    1: no tags. HEX[-dirty]  (note: no 'g' prefix)
    """
    if pieces['closest-tag']:
        rendered = pieces['closest-tag']
        if pieces['distance']:
            rendered += '-%d-g%s' % (pieces['distance'], pieces['short'])
    else:
        rendered = pieces['short']
    if pieces['dirty']:
        rendered += '-dirty'
    return rendered


def Newcombprecangles(epoch1, epoch2):
    """
----------------------------------------------------------------------
Purpose:    Calculate precession angles for a precession in FK4, using
            Newcombs method (Woolard and Clemence angles)
Input:      Besselian start epoch1 and Besselian end epoch2
Returns:    Angles zeta, z, theta in degrees
Reference:  ES 3.214 p.106
Notes:      Newcomb's precession angles for old catalogs (FK4),
            see ES 3.214 p.106
            Input are Besselian epochs!
            Adopted accumulated precession angles from equator
            and equinox at B1950 to 1984 January 1d 0h according
            to ES (table 3.214.1, p 107) are:
            zeta=783.7092, z=783.8009 and theta=681.3883
            The Woolard and Clemence angles (derived in this routine)
            are:
            zeta=783.70925, z=783.80093 and theta=681.38830 (same ES table)
            This routine found (in seconds of arc):
            zeta,z,theta =  783.709246271 783.800934641 681.388298284
            for t1 = 0.1 and t2 = 0.133999566814
            using the lines in the example
Examples:
            >>> b1 = 1950.0
            >>> b2 = celestial.epochs("F1984-01-01")[0]
            >>> print [x*3600 for x in celestial.Newcombprecangles(be1, be2)]
                [783.70924627097793, 783.80093464073127, 681.38829828393466]
----------------------------------------------------------------------
   """
    t1 = (epoch1 - 1850.0) / 1000.0
    t2 = (epoch2 - 1850.0) / 1000.0
    tau = t2 - t1
    d0 = 23035.545
    d1 = 139.72
    d2 = 0.06
    d3 = 30.24
    d4 = -0.27
    d5 = 17.995
    a0 = d0 + t1 * (d1 + d2 * t1)
    a1 = d3 + d4 * t1
    a2 = d5
    zeta_a = tau * (a0 + tau * (a1 + tau * a2))
    d0 = 23035.545
    d1 = 139.72
    d2 = 0.06
    d3 = 109.48
    d4 = 0.39
    d5 = 18.325
    a0 = d0 + t1 * (d1 + d2 * t1)
    a1 = d3 + d4 * t1
    a2 = d5
    z_a = tau * (a0 + tau * (a1 + tau * a2))
    d0 = 20051.12
    d1 = -85.29
    d2 = -0.37
    d3 = -42.65
    d4 = -0.37
    d5 = -41.8
    a0 = d0 + t1 * (d1 + d2 * t1)
    a1 = d3 + d4 * t1
    a2 = d5
    theta_a = tau * (a0 + tau * (a1 + tau * a2))
    return zeta_a / 3600.0, z_a / 3600.0, theta_a / 3600.0


def _to_bytes(v, length, byteorder='big'):
    """For python 3, which has a native implementation of this function."""
    return v.to_bytes(length, byteorder=byteorder)


def equals_lf(line):
    """Return True if line (a text or byte string) equals '
'."""
    return line == ('\n' if isinstance(line, str) else b'\n')


def lookupAddress(name, timeout=None):
    """
        Perform an A record lookup.

        @type name: L{bytes} or L{str}
        @param name: DNS name to resolve.

        @type timeout: Sequence of C{int}
        @param timeout: Number of seconds after which to reissue the query.
            When the last timeout expires, the query is considered failed.

        @rtype: L{Deferred}
        @return: A L{Deferred} which fires with a three-tuple of lists of
            L{twisted.names.dns.RRHeader} instances.  The first element of the
            tuple gives answers.  The second element of the tuple gives
            authorities.  The third element of the tuple gives additional
            information.  The L{Deferred} may instead fail with one of the
            exceptions defined in L{twisted.names.error} or with
            C{NotImplementedError}.
        """


def _update_other_results(results, best):
    """Update the positions and provisional input_sets of ``results`` based on
    performing the contraction result ``best``. Remove any involving the tensors
    contracted.

    Parameters
    ----------
    results : list
        List of contraction results produced by ``_parse_possible_contraction``.
    best : list
        The best contraction of ``results`` i.e. the one that will be performed.

    Returns
    -------
    mod_results : list
        The list of modified results, updated with outcome of ``best`` contraction.
    """
    best_con = best[1]
    bx, by = best_con
    mod_results = []
    for cost, (x, y), con_sets in results:
        if x in best_con or y in best_con:
            continue
        del con_sets[by - int(by > x) - int(by > y)]
        del con_sets[bx - int(bx > x) - int(bx > y)]
        con_sets.insert(-1, best[2][-1])
        mod_con = x - int(x > bx) - int(x > by), y - int(y > bx) - int(y > by)
        mod_results.append((cost, mod_con, con_sets))
    return mod_results


def swap_inputs():
    """Swap the first two inputs for the effect strip

    """
    pass


def forward(apps, schema_editor):
    """
    Updates default value of context field
    """
    if not schema_editor.connection.alias == 'default':
        return
    Config = apps.get_model('config', 'Config')
    for config in Config.objects.filter(context__isnull=True):
        config.context = {}
        config.save()


def corr_shape(ncorr, corr_shape):
    """
    Returns the shape of the correlations, given
    ``ncorr`` and the type of correlation shape requested

    Parameters
    ----------
    ncorr : integer
        Number of correlations
    corr_shape : {'flat', 'matrix'}
        Shape of output correlations

    Returns
    -------
    tuple
        Shape tuple describing the correlation dimensions

        * If ``flat`` returns :code:`(ncorr,)`
        * If ``matrix`` returns

            * :code:`(1,)` if :code:`ncorr == 1`
            * :code:`(2,)` if :code:`ncorr == 2`
            * :code:`(2,2)` if :code:`ncorr == 4`

    """
    if corr_shape == 'flat':
        return ncorr,
    elif corr_shape == 'matrix':
        if ncorr == 1:
            return 1,
        elif ncorr == 2:
            return 2,
        elif ncorr == 4:
            return 2, 2
        else:
            raise ValueError('ncorr not in (1, 2, 4)')
    else:
        raise ValueError("corr_shape must be 'flat' or 'matrix'")


@classmethod
def get_storage_capabilities(cls):
    """Return a dictionary representing the performance capabilities.

        This is needed to evaluate the performance of each driver.
        """
    return cls.STORAGE_CAPABILITIES


def ensure_tuple(val):
    """If val is not a tuple, make it a 1-tuple containing val.

    This is useful for uniform processing of Lua output which can be either a
    single value or a tuple of values.

    """
    if not isinstance(val, tuple):
        return val,
    return val


def should_display_warnings_for(to_type):
    """ Central method where we control whether warnings should be displayed """
    if not hasattr(to_type, '__module__'):
        return True
    elif to_type.__module__ in {'builtins'} or to_type.__module__.startswith(
        'parsyfiles') or to_type.__name__ in {'DataFrame'}:
        return False
    elif issubclass(to_type, int) or issubclass(to_type, str) or issubclass(
        to_type, float) or issubclass(to_type, bool):
        return False
    else:
        return True


@classmethod
def add_collider(cls, collider):
    """
        Add a new collider to the collider list
        :param collider: the collider to be added
        """
    cls.collider_list.append(collider)


def elem_to_dict(elem, ns, key_map, value_map={}):
    """Convert an XML etree.Element to a desired dictionary
    as specified by the key map and value map.
    Args:
        elem (etree.Element): An ancestor element
            of the tags specified in the key map.
        ns (string): The namespace to use
            when searching for XML tags.
        key_map (dict): A mapping from desired
            dictionary keys to XML tag names.
        value_map (dict): A mapping from XML tag names to
            dictionaries of mappings from XML text
            values to desired dictionary values.
    Returns:
        The desired dictionary.
    """
    to_dict = {}
    for k, v in key_map.iteritems():
        field = elem.find('.//{0}{1}'.format(ns, v))
        if field is not None:
            text = field.text
            to_dict[k] = value_map.get(v, {}).get(text, text)
    return to_dict


@classmethod
def with_alias(cls, alias):
    """
            Returns interface instance for new MongoEngine connection alias
        """
    return cls(alias)


def __iter__(cls):
    """iterates over the constants defined in the class"""
    cls._build_lookups()
    for constant in sorted(cls._constant_lookup):
        yield constant


def statislink(st):
    """check whether a stat result is a symlink"""
    return False


def render_git_describe_long(pieces):
    """TAG-DISTANCE-gHEX[-dirty].

    Like 'git describe --tags --dirty --always -long'.
    The distance/hash is unconditional.

    Exceptions:
    1: no tags. HEX[-dirty]  (note: no 'g' prefix)
    """
    if pieces['closest-tag']:
        rendered = pieces['closest-tag']
        rendered += '-%d-g%s' % (pieces['distance'], pieces['short'])
    else:
        rendered = pieces['short']
    if pieces['dirty']:
        rendered += '-dirty'
    return rendered


def editsource():
    """Edit UI source code of the active button

    """
    pass


def describe_events(SourceIdentifier=None, SourceType=None, StartTime=None,
    EndTime=None, Duration=None, MaxRecords=None, Marker=None):
    """
    Returns events related to clusters, security groups, snapshots, and parameter groups for the past 14 days. Events specific to a particular cluster, security group, snapshot or parameter group can be obtained by providing the name as a parameter. By default, the past hour of events are returned.
    See also: AWS API Documentation

    :example: response = client.describe_events(
        SourceIdentifier='string',
        SourceType='cluster'|'cluster-parameter-group'|'cluster-security-group'|'cluster-snapshot',
        StartTime=datetime(2015, 1, 1),
        EndTime=datetime(2015, 1, 1),
        Duration=123,
        MaxRecords=123,
        Marker='string'
    )

    :type SourceIdentifier: string
    :param SourceIdentifier: The identifier of the event source for which events will be returned. If this parameter is not specified, then all sources are included in the response.
            Constraints:
            If SourceIdentifier is supplied, SourceType must also be provided.
            Specify a cluster identifier when SourceType is cluster .
            Specify a cluster security group name when SourceType is cluster-security-group .
            Specify a cluster parameter group name when SourceType is cluster-parameter-group .
            Specify a cluster snapshot identifier when SourceType is cluster-snapshot .

    :type SourceType: string
    :param SourceType: The event source to retrieve events for. If no value is specified, all events are returned.
            Constraints:
            If SourceType is supplied, SourceIdentifier must also be provided.
            Specify cluster when SourceIdentifier is a cluster identifier.
            Specify cluster-security-group when SourceIdentifier is a cluster security group name.
            Specify cluster-parameter-group when SourceIdentifier is a cluster parameter group name.
            Specify cluster-snapshot when SourceIdentifier is a cluster snapshot identifier.

    :type StartTime: datetime
    :param StartTime: The beginning of the time interval to retrieve events for, specified in ISO 8601 format. For more information about ISO 8601, go to the ISO8601 Wikipedia page.
            Example: 2009-07-08T18:00Z

    :type EndTime: datetime
    :param EndTime: The end of the time interval for which to retrieve events, specified in ISO 8601 format. For more information about ISO 8601, go to the ISO8601 Wikipedia page.
            Example: 2009-07-08T18:00Z

    :type Duration: integer
    :param Duration: The number of minutes prior to the time of the request for which to retrieve events. For example, if the request is sent at 18:00 and you specify a duration of 60, then only events which have occurred after 17:00 will be returned.
            Default: 60

    :type MaxRecords: integer
    :param MaxRecords: The maximum number of response records to return in each call. If the number of remaining response records exceeds the specified MaxRecords value, a value is returned in a marker field of the response. You can retrieve the next set of records by retrying the command with the returned marker value.
            Default: 100
            Constraints: minimum 20, maximum 100.

    :type Marker: string
    :param Marker: An optional parameter that specifies the starting point to return a set of response records. When the results of a DescribeEvents request exceed the value specified in MaxRecords , AWS returns a value in the Marker field of the response. You can retrieve the next set of response records by providing the returned marker value in the Marker parameter and retrying the request.

    :rtype: dict
    :return: {
        'Marker': 'string',
        'Events': [
            {
                'SourceIdentifier': 'string',
                'SourceType': 'cluster'|'cluster-parameter-group'|'cluster-security-group'|'cluster-snapshot',
                'Message': 'string',
                'EventCategories': [
                    'string',
                ],
                'Severity': 'string',
                'Date': datetime(2015, 1, 1),
                'EventId': 'string'
            },
        ]
    }

    :returns:
    (string) --

    """
    pass


def get_value():
    """ return value of the field
        """


def pkcs11_get_data(pkcs11, slot_id):
    """
    Returns references to private key and certificate stored on the slot_id of
    the smart card.
    """
    private_key = pkcs11.load_private_key(slot_id)
    cert = pkcs11.load_certificate(slot_id)
    return private_key, cert


def always_tuple(value, none=()):
    """
    Some arguments can be a single value, or a sequence (tuple or list).
    This function normalizes the input to always be a sequence.

    :param value: The value that, if non-sequence, should be converted to a
                  sequence
    :param none: Value returned when input is None
    :return: The value as a sequence.
    """
    if value is None:
        return none
    elif hasattr(value, '__iter__') and not isinstance(value, str):
        return tuple(value)
    else:
        return value,


def getlineno(frame):
    """Get the line number from a frame object, allowing for optimization."""
    return frame.f_lineno


@classmethod
def from_local(cls, result):
    """Convert from local statvfs attributes"""
    return cls(result.f_bsize, result.f_frsize, result.f_blocks, result.
        f_bfree, result.f_bavail, result.f_files, result.f_ffree, result.
        f_favail, 0, result.f_flag, result.f_namemax)


def get_vco(verts_orig, loop):
    """
    Get vertex original coordinate from loop
    """
    for vo in verts_orig:
        if vo['vidx'] == loop.vert.index and vo['moved'] is False:
            return vo['vco']
    return loop.vert.co


def beta_spinorbital(MO):
    """ Return the corresponding beta spinorbital index given a molecular orbital (MO) index
    Args:
        MO(int):         molecular orbital index
    Returns:
        (2*MO + 1)(int): beta spin-orbital index
    """
    return 2 * MO + 1


@classmethod
def activate(cls, flow_task, prev_activation, token):
    """Instantiate new task."""
    task = flow_task.flow_class.task_class(process=prev_activation.process,
        flow_task=flow_task, token=token)
    task.save()
    task.previous.add(prev_activation.task)
    activation = cls()
    activation.initialize(flow_task, task)
    activation.perform()
    return activation


def safe_int(val):
    """
    Convert string/float to integer

    If input value is integer - return as-is
    If input value is a hexadecimal (0x00): converts hex to decimal

    Args:
        val: value to convert

    Raises:
        ValueError: if input value can not be converted
    """
    if isinstance(val, int):
        return val
    elif isinstance(val, str) and val.find('x') != -1:
        return int(val, 16)
    else:
        return int(val)


def validkey(key):
    """
    Check if a tag or a key is valid.
    In XML this basically means that the
    key does not start with a number.
    """
    assert isinstance(key, str), 'Key must be string'
    if key[0].isdigit():
        raise ValueError('Key cannot start with a number: {}'.format(key))
    return key


def _tx_resource_for_name(name):
    """ Return the Transifex resource name """
    if name == 'core':
        return 'django.core'
    else:
        return 'django.contrib-%s' % name


def _get_camera_from_id(camera_id, cameras):
    """Get Camera object from ID"""
    camera = list(filter(lambda cam: camera_id == cam.id, cameras))
    if camera:
        return camera[0]
    raise ValueError('No camera found with ID %s' % camera_id)


def broadcast_not_equal(lhs=None, rhs=None, name=None, attr=None, out=None,
    **kwargs):
    """Returns the result of element-wise **not equal to** (!=) comparison operation with broadcasting.

    Example::

       x = [[ 1.,  1.,  1.],
            [ 1.,  1.,  1.]]

       y = [[ 0.],
            [ 1.]]

       broadcast_not_equal(x, y) = [[ 1.,  1.,  1.],
                                    [ 0.,  0.,  0.]]

    Defined in src/operator/tensor/elemwise_binary_broadcast_op_logic.cc:L64

    Parameters
    ----------
    lhs : Symbol
        First input to the function
    rhs : Symbol
        Second input to the function

    name : string, optional.
        Name of the resulting symbol.

    Returns
    -------
    Symbol
        The result symbol.
    """
    return 0,


def _interpret_tx_block(data, vers=3, bord='little'):
    """
    interprets tx block of an mdf file
    @param data: the bytes of the block
    @param vers: mdf file version
    @param bord: byte order of contents
    @note: not every block actually uses version and byte order but referred for consistency purposes
    @return: a dictionary with the contents
    """
    for fmt in ['ascii', 'latin1']:
        try:
            text = data[4:].strip(b'\x00 ').decode(fmt)
            break
        except UnicodeError:
            text = None
    return {'text': text}


@staticmethod
def explode_by_spaces(x):
    """
        Разбивает массив байтов по пробелам,
        игнорируя те пробелы, перед которыми стоит слэш,
        но только если это не двойной слеш
        """
    if isinstance(x, bytearray) or isinstance(x, bytes):
        arr = x
        need_decode = False
    elif isinstance(x, str):
        arr = x.encode('UTF-8')
        need_decode = True
    parts = []
    cur_part = bytearray()
    prev_slash = False
    slash_byte = b'\\'[0]
    state = 0
    l = len(arr)
    for i in range(l):
        if arr[i] == slash_byte:
            if prev_slash:
                cur_part.append(slash_byte)
                prev_slash = False
            else:
                prev_slash = True
        elif arr[i] == 32:
            if prev_slash:
                cur_part.append(32)
                prev_slash = False
            elif len(cur_part) > 0:
                if not need_decode:
                    parts.append(bytes(cur_part))
                else:
                    parts.append(cur_part.decode('UTF-8'))
                cur_part = bytearray()
        elif prev_slash:
            cur_part.append([slash_byte, arr[i]])
        else:
            cur_part.append(arr[i])
    if len(cur_part):
        if not need_decode:
            parts.append(bytes(cur_part))
        else:
            parts.append(cur_part.decode('UTF-8'))
    return parts


@staticmethod
def split(business_id):
    """
        Split a company id to a id number part and a checksum digit.

        @param business_id: a valid finnish business id.
        """
    val = str(business_id)
    if len(val) == 0:
        return '', ''
    parts = val.split('-')
    if len(parts) == 1:
        return parts[0][:-1], parts[0][-1]
    else:
        return parts[0], parts[1]


@classmethod
def prepare(cls, ctx, related):
    """
        Called by QuerySetResource to add necessary select_related-s
        calls to the QuerySet.
        """
    for field in cls.fields:
        try:
            prepare = field.prepare
        except AttributeError:
            pass
        else:
            prepare(ctx, related)
    return related


def __repr__(A):
    """For `print` and `pprint`"""
    return A.to_str()


def get_cigar(var):
    """
    Return the cigar string describing how to align an
    alternate allele to the reference allele
    """
    return var.INFO.get('CIGAR')


def _clone_attrs(obj):
    """Clone deeply any collection attributes of the passed-in object."""
    attrs = vars(obj).copy()
    ccsteps = attrs.get('_csteps', None)
    if ccsteps:
        attrs['_csteps'] = ccsteps.copy()
    ctags = attrs['_tags']
    if ctags:
        attrs['_tags'] = attrs['_tags'].copy()
    return attrs


@classmethod
def get_class(mcs):
    """ Generates new class to gether logic of all available extensions
            ::

                mc = ExtensibleType._("MyClass")
                @six.add_metaclass(mc)
                class MyClassBase(object):
                    pass

                # get class with all extensions enabled
                MyClass = mc.get_class()

        """
    if mcs._generated_class is None:
        mcs._generated_class = type(mcs._cls_name, tuple(mcs._base_classes),
            {'_generated': True})
    return mcs._generated_class


def lambd_CO2_Mg_HMW84(T, P):
    """n-c: carbon-dioxide magnesium [HMW84]."""
    lambd = 0.183
    valid = T == 298.15
    return lambd, valid


def render_pep440_old(pieces):
    """TAG[.postDISTANCE[.dev0]] .

    The ".dev0" means dirty.

    Eexceptions:
    1: no tags. 0.postDISTANCE[.dev0]
    """
    if pieces['closest-tag']:
        rendered = pieces['closest-tag']
        if pieces['distance'] or pieces['dirty']:
            rendered += '.post%d' % pieces['distance']
            if pieces['dirty']:
                rendered += '.dev0'
    else:
        rendered = '0.post%d' % pieces['distance']
        if pieces['dirty']:
            rendered += '.dev0'
    return rendered


def validate_ip(s):
    """Validate IPv4 address format."""
    a = s.split('.')
    if len(a) != 4:
        return None
    for x in a:
        if not x.isdigit():
            return None
        i = int(x)
        if i < 0 or i > 255:
            return None
    return s


def get_issuer(realm):
    """
    Get correct issuer to validate the JWT against. If an internal URL is
    configured for the server it will be replaced with the public one.

    :param django_keycloak.models.Realm realm:
    :return: issuer
    :rtype: str
    """
    issuer = realm.well_known_oidc['issuer']
    if realm.server.internal_url:
        return issuer.replace(realm.server.internal_url, realm.server.url, 1)
    return issuer


def convert_dict_to_csv(log_dict):
    """
    This function will convert a log dict into csv file by recursively find the list in the
    dict and save the list with its key as a single csv file, the
    :param log_dict:
    :return: list, as the dict as each element with
    {'csv_file_name', 'csv_keys', 'csv_row_data'}
    """
    raise NotImplementedError


def log10(ic):
    """
        Element-wise base 10 log of an `ImageCollection`.

        If the `ImageCollection` is empty, returns the empty `ImageCollection`.

        Example
        -------
        >>> import descarteslabs.workflows as wf
        >>> col = wf.ImageCollection.from_id("landsat:LC08:01:RT:TOAR",
        ...     start_datetime="2017-01-01", end_datetime="2017-05-30")
        >>> col.log10().compute(geoctx) # doctest: +SKIP
        ImageCollectionResult of length 2:
        ...
        """
    from ..math import arithmetic
    return arithmetic.log10(ic)


def _track_arguments(name, args):
    """Parse function arguments and return (artist, track, mbid).  Only use
    with function that take those arguments."""
    if len(args) == 1:
        mbid = args[0]
        artist, track = None, None
    elif len(args) == 2:
        artist, track = args
        mbid = None
    else:
        raise TypeError('{0}() takes either (artist, track) or (mbid,)'.
            format(name))
    return artist, track, mbid


@classmethod
def export_to_hdf5(cls, h5_file, model, eids):
    """exports the elements in a vectorized way"""
    pids = []
    nodes = []
    thetas = []
    for eid in eids:
        element = model.elements[eid]
        pids.append(element.pid)
        nodes.append(element.nodes)
        thetas.append(element.theta)
    h5_file.create_dataset('eid', data=eids)
    h5_file.create_dataset('pid', data=pids)
    h5_file.create_dataset('nodes', data=nodes)
    h5_file.create_dataset('theta', data=thetas)


def get_value_from_prefs(prefs_file, key):
    """Reads the next line, splits the text based on the key, and formats."""
    raw = prefs_file.readline().split(key + '=')
    return raw[1].rstrip()


def list_platforms():
    """
    Simple script to extract the list of requested platforms from the slot
    configuration file.
    """
    __author__ = 'Marco Clemencic <marco.clemencic@cern.ch>'
    import os
    import sys
    from LbNightlyTools.Configuration import findSlot
    usage = 'Usage: %s configuration_file' % os.path.basename(sys.argv[0])
    if '-h' in sys.argv or '--help' in sys.argv:
        print(usage)
        sys.exit(0)
    if len(sys.argv) != 2:
        print(usage, file=sys.stderr)
        sys.exit(1)
    print(' '.join(findSlot(sys.argv[1]).platforms))


@classmethod
def init_cli(cls, parser):
    """Set up the argument list for this product
        """
    cls.init_data_options(parser)
    cls.init_plot_options(parser)


def _actual_index(arg):
    """Turn a string in a integer or slice."""
    if ':' in arg:
        idxs = arg.split(':')
        if len(idxs) > 3:
            raise ValueError('{} is an invalid slice'.format(arg))
        idxs[0] = int(idxs[0]) if idxs[0] else None
        idxs[1] = int(idxs[1]) if idxs[1] else None
        if len(idxs) == 3:
            idxs[2] = int(idxs[2]) if idxs[2] else None
        else:
            idxs = idxs[0:2] + [1]
        return slice(*idxs)
    return int(arg)


@classmethod
def get_by_hostname(cls, context, hostname, online=True):
    """Get a Conductor record by its hostname.

        :param cls: the :class:`Conductor`
        :param context: Security context
        :param hostname: the hostname on which a Conductor is running
        :param online: Specify the expected ``online`` field value for the
                       conductor to be retrieved. The ``online`` field is
                       ignored if this value is set to None.
        :returns: a :class:`Conductor` object.
        """
    db_obj = cls.dbapi.get_conductor(hostname, online=online)
    conductor = cls._from_db_object(context, cls(), db_obj)
    return conductor


def getid(obj):
    """Return id if argument is a Resource.

    Abstracts the common pattern of allowing both an object or an object's ID
    (UUID) as a parameter when dealing with relationships.
    """
    try:
        if obj.uuid:
            return obj.uuid
    except AttributeError:
        pass
    try:
        return obj.id
    except AttributeError:
        return obj


def debool(p):
    """Boolean function to Python `bool` value"""
    return p(True)(False)


def _shift(x: list, n: int):
    """
    works similar to np.roll
    :param x:
    :param n:
    :return:
    """
    return x[n:] + x[:n]


def to_float(val):
    """Convert val to float (or raise Exception)

    :param val: value to convert
    :type val:
    :return: float(val)
    :rtype: float
    """
    return float(val)


def getTOFsigDeriv(dsp):
    """get derivatives of TOF peak profile sigma^2 wrt sig-0, sig-1, & sig-q

    :param float dsp: d-spacing of peak

    :returns: list getTOFsigDeriv: d(sig0/d(sig-0), d(sig)/d(sig-1) & d(sig)/d(sig-q)

    """
    return 1.0, dsp ** 2, dsp ** 4, dsp


@classmethod
def get(cls, name, default=None):
    """Return currency object with matching name or ticker."""
    for inst in cls.instances.values():
        if name in (inst.name, inst.ticker):
            return inst
    else:
        return default


def fix_gaps(y):
    """Linearly interpolates over NaNs in 1D input array x

    Args
    ----
    x: ndarray
        array containing NaNs to interpolate over

    Returns
    -------
    y_interp: ndarray
        array with NaN values replaced by interpolated values
    """
    import numpy
    not_nan = ~numpy.isnan(y)
    xp = numpy.where(not_nan)
    yp = y[not_nan]
    x_interp = numpy.arange(len(xp))
    y_interp = numpy.interp(x_interp, xp, yp)
    return y_interp


def s2b(s):
    """
    String to binary.
    """
    ret = []
    for c in s:
        ret.append(bin(ord(c))[2:].zfill(8))
    return ''.join(ret)


def __len__():
    """Return the number of valid values, or sys.maxint."""


def _get_default_unit_format(config):
    """
    Get the default unit format as specified in the VOTable spec.
    """
    return 'cds'


def _episode_key(episode):
    """
    Returns key used to sort episodes based on season and episode number.
    """
    return episode['season_number'], episode['episode_number']


def super_overridden(cls):
    """
    This decorator just serves as a reminder that the super function behaves
    differently. It doesn't actually do anything, that happens inside
    ``classhook.hook_class``.
    """
    cls.__rootpy_have_super_overridden = True
    return cls


def hover(hover_color='gold'):
    """DataFrame Styler: Called by highlight to highlight row below cursor.
        Changes html background color.

        Parameters:

        hover_Color
    """
    from IPython.display import HTML
    return dict(selector='tr:hover', props=[('background-color', '%s' %
        hover_color)])


def get_query_params(request):
    """ Get query parameters of the Django request object. """
    return request.GET


def set_cache_protected_period(connection: 'Connection', protected_period: int
    ) ->None:
    """
    Set the cache's half-life.

    Parameters
    ----------
    connection : "Connection"
    protected_period : int, default None
        Optionally specify a number of seconds within which cache entries are excluded.
    """
    sql = (
        f"UPDATE cache.cache_config SET value='{int(protected_period)}' WHERE key='protected_period'"
        )
    with connection.engine.begin() as trans:
        trans.execute(sql)


def formula(foyer_fiscal, period, parameters):
    """
        Attention : Pour les années avant 2013, cette formule n'est pas entièrement correcte car le taux de la CSG n'était pas unique (distinction revenus du patrimoine et revenus de placement)
        """
    assiette_csg_revenus_capital = foyer_fiscal('assiette_csg_revenus_capital',
        period)
    P = parameters(period).taxation_capital.prelevements_sociaux
    total = (P.prelevement_social.revenus_du_patrimoine + P.caps.
        revenus_du_patrimoine + P.prelevements_solidarite.revenus_du_patrimoine
        )
    return -assiette_csg_revenus_capital * total


def canonical_piecewise_quadratic_interpolation(x, nodal_vals):
    """
    Piecewise quadratic interpolation of nodes at [0,0.5,1]
    Assumes all values are in [0,1].
    """
    assert x.ndim == 1
    assert nodal_vals.shape[0] == 3
    vals = nodal_vals[0] * (1.0 - 3.0 * x + 2.0 * x ** 2) + nodal_vals[1] * (
        4.0 * x - 4.0 * x ** 2) + nodal_vals[2] * (-x + 2.0 * x ** 2)
    return vals


@classmethod
def _scrub_data(cls, data):
    """Remove sensitive attributes from response data"""
    if 'username' in data:
        del data['username']
    if 'password' in data:
        del data['password']
    return data


def check_domain_state(conn, guestname, logger):
    """ if a guest with the same name exists, remove it """
    running_guests = []
    ids = conn.listDomainsID()
    for id in ids:
        obj = conn.lookupByID(id)
        running_guests.append(obj.name())
    if guestname in running_guests:
        logger.info('A guest with the same name %s is running!' % guestname)
        logger.info('destroy it...')
        domobj = conn.lookupByName(guestname)
        domobj.destroy()
    defined_guests = conn.listDefinedDomains()
    if guestname in defined_guests:
        logger.info('undefine the guest with the same name %s' % guestname)
        domobj = conn.lookupByName(guestname)
        domobj.undefine()
    return 0


def Phred_scores_color_change(plot_input, colors):
    """Employs the desired colouring scheme for the plotting module."""
    for plot in plot_input:
        for box in plot['boxes']:
            box.set(color='black', linewidth=1)
        for patch, color in zip(plot['boxes'], colors):
            patch.set_facecolor(color)
        for whisker in plot['whiskers']:
            whisker.set(color='black', linewidth=1)
        for median in plot['medians']:
            median.set(color='black', linewidth=1)
        for flier in plot['fliers']:
            flier.set(marker='', markersize=1, markerfacecolor=None,
                markeredgecolor=None)


def lookupZone(name, timeout=None):
    """
        Perform an AXFR record lookup.

        NB This is quite different from other DNS requests. See
        U{http://cr.yp.to/djbdns/axfr-notes.html} for more
        information.

        NB Unlike other C{lookup*} methods, the timeout here is not a
        list of ints, it is a single int.

        @type name: C{str}
        @param name: DNS name to resolve.

        @type timeout: C{int}
        @param timeout: When this timeout expires, the query is
            considered failed.

        @rtype: L{Deferred}
        @return: A L{Deferred} which fires with a three-tuple of lists of
            L{twisted.names.dns.RRHeader} instances.
            The first element of the tuple gives answers.
            The second and third elements are always empty.
            The L{Deferred} may instead fail with one of the
            exceptions defined in L{twisted.names.error} or with
            C{NotImplementedError}.
        """


def _replace_register(flow_params, register_number, register_value):
    """Replace value from flows to given register number

    'register_value' key in dictionary will be replaced by register number
    given by 'register_number'

    :param flow_params: Dictionary containing defined flows
    :param register_number: The number of register where value will be stored
    :param register_value: Key to be replaced by register number

    """
    try:
        reg_port = flow_params[register_value]
        del flow_params[register_value]
        flow_params['reg{:d}'.format(register_number)] = reg_port
    except KeyError:
        pass


def trans_temperature(number, src=None, dest='K'):
    """
    transfer temperature from one unit to another
    Input:
        number: int/float/string, like 100/100.4/100.23C/100.23 Celsius
        src: default None, read from number, but could be given
        dest: default K
    Output:
        temperature number with dest as unit
    """
    import re
    VALID_TEMPERATURE_UNITS = ['k', 'c', 'f']
    if isinstance(number, str):
        res = re.match('^(\\d+\\.?\\d*)\\s*([A-Za-z]+)$', number)
        if re.match('^\\d+\\.*\\d*$', number):
            number = float(number)
        elif res:
            number, src = float(res[1]), res[2]
        else:
            raise ValueError(
                f'given number {number} is not a int/float or number with unit'
                )
    if not src:
        raise ValueError(f'src unit not given')
    src = src.lower()[0]
    dest = dest.lower()[0]
    assert src in VALID_TEMPERATURE_UNITS and dest in VALID_TEMPERATURE_UNITS, f'src:{src}, dest:{dest}'
    assert isinstance(number, (int, float)), 'number should be a int/float'
    if src == dest:
        return number
    if src == 'k':
        number -= 298.15
    elif src == 'f':
        number = (number - 32) * 5 / 9
    if dest == 'k':
        number += 298.15
    elif dest == 'f':
        number = (number + 32) * 9 / 5
    return number


@classmethod
def user_error(cls, val):
    """
        Create an instance of this class set to the ``user_error`` tag with
        value ``val``.

        :param SharingUserError val:
        :rtype: RemoveFileMemberError
        """
    return cls('user_error', val)


@classmethod
def match(cls, *args):
    """Match to return the plugin which is appropriate to load"""


def is_task_list(fn):
    """Check if a function is a task list.

    Return:
        boolean: if a function is a task list.
    """
    return getattr(fn, '__garcon__', {}).get('list')


@classmethod
def tasks_str(cls):
    """
        Human-readable register contents dump.
        """
    return ','.join(cls.task_names())


def load_args():
    """ Load possible arguments
        :return: A list of tuple (arg_type, arg_name, arg_value, arg_desc)
    """
    return [('int', 'draw_model_id', -1,
        'The model ID of the value function.'), ('float', 'draw_coeff', 1.0,
        'The coefficient to apply to the draw loss')]


def getSets(unique_name=None):
    """Returns a list of ISet in the catalog optionally
        matching the given name."""


def take(n):
    """
        first `n` things of incoming things

        @param n: number of things
        """


def darken(col):
    """Darken Color col by 35%."""
    col.make_darker()


def is_hiragana(word):
    """Checks is the word is a Japanese hiragana.

  This is using the unicode codepoint range for hiragana.
  https://en.wikipedia.org/wiki/Hiragana_(Unicode_block)

  Args:
    word (str): A word.

  Returns:
    bool: True if the word is a hiragana.
  """
    return len(word) == 1 and 12353 <= ord(word) <= 12447


def disable_directory(DirectoryArn=None):
    """
    Disables the specified directory. Disabled directories cannot be read or written to. Only enabled directories can be disabled. Disabled directories may be reenabled.
    See also: AWS API Documentation

    :example: response = client.disable_directory(
        DirectoryArn='string'
    )

    :type DirectoryArn: string
    :param DirectoryArn: [REQUIRED]
            The ARN of the directory to disable.

    :rtype: dict
    :return: {
        'DirectoryArn': 'string'
    }

    """
    pass


def nona(df):
    """Eliminate any row in a dataframe containing NA, NaN nulls."""
    return df.dropna()


def payment_create(payment):
    """This is called when a payment is created

        :param payment: the created payment
        """


def get_bytes_from_wsgi(environ, key, default):
    """
    Get a value from the WSGI environ dictionary as bytes.

    key and default should be strings.
    """
    value = environ.get(key, default)
    return value.encode('iso-8859-1')


def reveal_metaelems():
    """Reveal all hidden metaelements

    """
    pass


def delete_manager(module, array):
    """Delete SNMP Manager"""
    changed = True
    if not module.check_mode:
        try:
            array.delete_snmp_manager(module.params['name'])
        except Exception:
            module.fail_json(msg='Delete SNMP manager {0} failed'.format(
                module.params['name']))
    module.exit_json(changed=changed)


def render_pep440_old(pieces):
    """TAG[.postDISTANCE[.dev0]] .

    The ".dev0" means dirty.

    Eexceptions:
    1: no tags. 0.postDISTANCE[.dev0]
    """
    if pieces['closest-tag']:
        rendered = pieces['closest-tag']
        if pieces['distance'] or pieces['dirty']:
            rendered += '.post%d' % pieces['distance']
            if pieces['dirty']:
                rendered += '.dev0'
    else:
        rendered = '0.post%d' % pieces['distance']
        if pieces['dirty']:
            rendered += '.dev0'
    return rendered


def ggsave(plot, *arg, **kwargs):
    """
    Save a ggplot object as an image file

    Use :meth:`ggplot.save` instead
    """
    return plot.save(*arg, **kwargs)


def build_url(port, host):
    """
    Returns full url combining host(if not specified will use the output of :func:`python:socket.gethostname`) & port

    :param port: integer string for the port to be used by the :class:`flask:flask.Flask` process
    :type port: str
    :param host: hostname, can start with 'http://', 'https://' or just the hostname itself
    :type host: str, optional
    :return: str
    """
    final_port = ':{}'.format(port) if port is not None else ''
    if (host or '').startswith('http'):
        return '{}{}'.format(host, final_port)
    return 'http://{}{}'.format(host, final_port)


def _add_doc(func, doc):
    """Add documentation to a function."""
    func.__doc__ = doc


def hasBrowserId():
    """Return true if there is a browser id for the current request.

        o Permission required: Access contents information

        o Does *not* raise an error if the request contains a broken
          browser id.
        """


def owner_id(request):
    """ Retrieve an owner_id for a request.

        :type request:
            A Klein request object.
        :param request:
            The request to authenticate.

        :return:
            A deferred that fires with either an owner_id or None if the
            request could not be authenticated.
        """


def get_num_ending(number: int, endings: (tuple or list)) ->str:
    """
    Feature returns the ending for plural words based on the number and array of endings

    Source (JS/PHP): https://habrahabr.ru/post/105428/
    :param number: The number by which you want to generate the end
    :param endings: Array (or tuple) of words or endings for numbers (1, 4, 5) e. g. ('яблоко', 'яблока', 'яблок')
    :return:
    """
    if len(endings) == 1:
        endings = list(endings) + [endings[0], endings[0]]
    elif len(endings) == 2:
        endings = list(endings) + [endings[1]]
    num = number % 100
    if 11 <= num <= 19:
        return endings[2]
    num %= 10
    if num == 1:
        return endings[0]
    elif 2 <= num <= 4:
        return endings[1]
    return endings[2]


def xld_track_matches():
    """Return a dictionary containing the XLD matches."""
    return {'full_line': [['log4', 'Statistics'], ['good',
        '->Accurately ripped'], ['badish',
        '->Track not present in AccurateRip database'], ['bad',
        '->Rip may not be accurate'], ['bad',
        'List of damaged sector positions +:'], ['badish',
        '\\(\\d+\\) \\d{2}:\\d{2}:\\d{2}'], ['log3',
        '\\/.+\\.(?:[Ff][Ll][Aa][Cc]|[Ww][Aa][Vv]|[Mm][Pp]3|[Aa][Aa][Cc])']
        ], 'crc': ['CRC32 hash \\(test run\\)', 'CRC32 hash'], 'statistics':
        {'bad': ['Read error', 'Skipped \\(treated as error\\)',
        'Inconsistency in error sectors', 'Damaged sector count'], 'badish':
        ['Jitter error \\(maybe fixed\\)', 'Retry sector count',
        'Edge jitter error \\(maybe fixed\\)',
        'Atom jitter error \\(maybe fixed\\)',
        'Drift error \\(maybe fixed\\)',
        'Dropped bytes error \\(maybe fixed\\)',
        'Duplicated bytes error \\(maybe fixed\\)']}}


def termToWordIds(text):
    """Return a sequence of ids of the words parsed from the text.

        The input text may be either a string or a list of strings.

        Parse the text as if they are search terms, and skips words
        that aren't in the lexicon.
        """


def associate_route_table(DryRun=None, SubnetId=None, RouteTableId=None):
    """
    Associates a subnet with a route table. The subnet and route table must be in the same VPC. This association causes traffic originating from the subnet to be routed according to the routes in the route table. The action returns an association ID, which you need in order to disassociate the route table from the subnet later. A route table can be associated with multiple subnets.
    For more information about route tables, see Route Tables in the Amazon Virtual Private Cloud User Guide .
    See also: AWS API Documentation

    Examples
    This example associates the specified route table with the specified subnet.
    Expected Output:

    :example: response = client.associate_route_table(
        DryRun=True|False,
        SubnetId='string',
        RouteTableId='string'
    )

    :type DryRun: boolean
    :param DryRun: Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation . Otherwise, it is UnauthorizedOperation .

    :type SubnetId: string
    :param SubnetId: [REQUIRED]
            The ID of the subnet.

    :type RouteTableId: string
    :param RouteTableId: [REQUIRED]
            The ID of the route table.

    :rtype: dict
    :return: {
        'AssociationId': 'string'
    }

    """
    pass


def censorStudentNumber(n):
    """Replace some parts of student number with astericks."""
    n = str(n)
    r = n[:2] + '****' + n[-2:]
    assert len(n) == len(r)
    return r


def set_scene_frames():
    """Set scene’s start and end frame to match clip’s start frame and length

    """
    pass


@classmethod
def band_bins(cls, bins, **kwargs):
    """
        Must yield the center/crossover bins needed for filter creation.

        Parameters
        ----------
        bins : numpy array
            Center/crossover bins used for the creation of filters.
        kwargs : dict, optional
            Additional parameters for for the creation of filters
            (e.g. if the filters should overlap or not).

        """
    raise NotImplementedError('needs to be implemented by sub-classes')


@classmethod
def get_from_queryset(cls, queryset, path_fragment):
    """
        Called by containing QuerySetResource to filter the QuerySet down
        to a single item -- represented by this ModelResource
        """
    attr, type_ = cls.published_key
    kwargs = dict()
    kwargs[attr] = type_(path_fragment)
    return queryset.get(**kwargs)


def iterListeners():
    """return iterator of all available listeners"""


def choose_parsimonious_states(tree, ps_feature, out_feature):
    """
    Calculates the statistics for parsimony, and copies the parsimonious states to out_feature.

    :param ps_feature: feature containing each node parsimonious states
    :type ps_feature: str
    :param out_feature: feature where the node parsimonious states are to be copied
    :type out_feature: str
    :param tree: the tree of interest
    :type tree: ete3.Tree
    :return: number of ancestral scenarios selected, number of unresolved nodes, and total numbest of selected states
    :rtype: tuple(int, int, int)
    """
    num_scenarios = 1
    unresolved_nodes = 0
    num_states = 0
    for node in tree.traverse():
        states = getattr(node, ps_feature)
        node.add_feature(out_feature, states)
        n = len(states)
        num_scenarios *= n
        unresolved_nodes += 1 if n > 1 else 0
        num_states += n
    return num_scenarios, unresolved_nodes, num_states


def get_default_output(directory, algo):
    """ Get Default Output """
    res = directory
    if algo == 'tar':
        res += '.tar'
    elif algo == 'gzip':
        res += '.tar.gz'
    elif algo == 'bzip2':
        res += '.tar.bz2'
    elif algo == 'xz':
        res += '.tar.xz'
    elif algo == 'zip':
        res += '.zip'
    return res


def has_values(l):
    """
    Returns True if a 2-nested list contains at least one truthy value.

    :param l: a nested list

    :return:
    """
    for row in l:
        for value in row:
            if value:
                return True
    return False


def identify_n_gears(gear_box_ratios):
    """
    Identify the number of gears [-].

    :param gear_box_ratios:
        Gear box ratios [-].
    :type gear_box_ratios: dict[int | float]

    :return:
        Number of gears [-].
    :rtype: int
    """
    return max(gear_box_ratios)


@classmethod
def clear_expired(cls):
    """
        Remove expired sessions from the session store.
        If this operation isn't possible on a given backend, it should raise
        NotImplementedError. If it isn't necessary, because the backend has
        a built-in expiration mechanism, it should be a no-op.
        """
    raise NotImplementedError('This backend does not support clear_expired().')


def bC_none(T, P):
    """c-a: no interaction effect."""
    b0 = 0
    b1 = 0
    b2 = 0
    C0 = 0
    C1 = 0
    alph1 = -9
    alph2 = -9
    omega = -9
    valid = T > 0
    return b0, b1, b2, C0, C1, alph1, alph2, omega, valid


def _add_doc(func, doc):
    """Add documentation to a function."""
    func.__doc__ = doc


def options(opt):
    """
	Command-line options
	"""
    opt.add_option('--want-rpath', action='store_true', default=False, dest
        ='want_rpath', help='enable the rpath for qt libraries')
    opt.add_option('--header-ext', type='string', default='', help=
        'header extension for moc files', dest='qt_header_ext')
    for i in 'qtdir qtbin qtlibs'.split():
        opt.add_option('--' + i, type='string', default='', dest=i)
    opt.add_option('--translate', action='store_true', help=
        'collect translation strings', dest='trans_qt4', default=False)


@classmethod
def create(cls, root, schema):
    """
        Create an object based on the root tag name.
        @param root: An XML root element.
        @type root: L{Element}
        @param schema: A schema object.
        @type schema: L{schema.Schema}
        @return: The created object.
        @rtype: L{SchemaObject}
        """
    fn = cls.tags.get(root.name)
    if fn is not None:
        return fn(schema, root)
    else:
        return None


def encode_boolean(obj):
    """lower the case to get a json/js boolean"""
    return str(obj).lower()


def get_list_item(lines):
    """Get most used list item from text.

    Meaning: probably a dash, maybe a star.
    """
    unordered_list = []
    for line in lines:
        stripped = line.strip()
        if len(stripped) < 3:
            continue
        if stripped[1] != ' ':
            continue
        prefix = stripped[0]
        white = line.find(prefix)
        unordered_list.append('{}{}'.format(' ' * white, prefix))
    best = '-'
    count = 0
    for key in set(unordered_list):
        new_count = unordered_list.count(key)
        if new_count > count:
            best = key
            count = new_count
    return best


def intersection(setA, setB):
    """
    intersection of two dictionaries with basic datatype
    """
    setC = {}
    for key, value in setA.items():
        if key in setB and setB[key] == value:
            setC[key] = value
    return setC


def _is_namespace_visible(context, namespace):
    """Return true if namespace is visible in this context"""
    if context.is_admin:
        return True
    if namespace.get('visibility', '') == 'public':
        return True
    if namespace['owner'] is None:
        return True
    if context.owner is not None:
        if context.owner == namespace['owner']:
            return True
    return False


def hex_to_int(hex_string, signed=True):
    """Returns hex_string converted to int. Method can work with signed 2's complement any length.

    :param hex_string: hex string. Example 'DEADBEEF' or 'deadbeef'.
    :param signed: boolean. True or False, indicating if hex signed. Default True
    :return: int representation of hex_string
    """
    if signed:
        bits = len(bytearray.fromhex(hex_string)) * 8
        val = int('0x' + hex_string, 16)
        if val & 1 << bits - 1 != 0:
            val = val - (1 << bits)
        return val
    else:
        return int(hex_string, 16)


def subtract(data, params=None):
    """
    Subtract function aggregation.

    Example config:

    .. code-block:: python

        config = {
            ...
            'fields': ['timestamp', 'x'],
            'aggregations': [
                {
                    'func': 'subtract',
                    'field': 'x',
                    'params': {
                        'by': 10
                    }
                }
            ],
            ...
        }
    """
    kwargs = params or {}
    constant = kwargs.get('by', 0)
    return data - constant


def get_url(repo):
    """
        Returns the URL for the given repo
        and None if not found.

        :return: url or None
        """


def _scale_time(age):
    """Create a scalar such that the maximum age is 1."""
    return 1.0 / age.max()


def heading() ->int:
    """Gives the compass heading, calculated from the above readings, as an
    integer in the range from 0 to 360, representing the angle in degrees,
    clockwise, with north as 0.
    If the compass has not been calibrated, then this will call ``calibrate``.
    """


@staticmethod
def _get_wall_upload_server(session, group_id):
    """
        https://vk.com/dev/photos.getWallUploadServer
        """
    response = session.fetch('photos.getWallUploadServer', group_id=group_id)
    return response['upload_url']


def acknowledge_third_party_job(jobId=None, nonce=None, clientToken=None):
    """
    Confirms a job worker has received the specified job. Only used for partner actions.
    See also: AWS API Documentation

    :example: response = client.acknowledge_third_party_job(
        jobId='string',
        nonce='string',
        clientToken='string'
    )

    :type jobId: string
    :param jobId: [REQUIRED]
            The unique system-generated ID of the job.

    :type nonce: string
    :param nonce: [REQUIRED]
            A system-generated random number that AWS CodePipeline uses to ensure that the job is being worked on by only one job worker. Get this number from the response to a GetThirdPartyJobDetails request.

    :type clientToken: string
    :param clientToken: [REQUIRED]
            The clientToken portion of the clientId and clientToken pair used to verify that the calling entity is allowed access to the job and its details.

    :rtype: dict
    :return: {
        'status': 'Created'|'Queued'|'Dispatched'|'InProgress'|'TimedOut'|'Succeeded'|'Failed'
    }

    """
    pass


def get_timeout(val):
    """ convert the timeout value into a float or None """
    if val is None:
        return val
    timeout = float(val)
    if timeout <= 0:
        return None
    return timeout


def gather_row(data, row_index):
    """Slice out the data given the row index.

    Parameters
    ----------
    data : Tensor
        The data tensor
    row_index : Tensor
        A 1-D integer tensor containing which rows to be sliced out.

    Returns
    -------
    Tensor
        The sliced data. The first dimension should equal to ``len(row_index)``.
    """
    pass


def decode_dif_data_field(data_field):
    """
    Returns the data description and the number of bytes the data is made up of
    Will return false if data_length cannot be predetermined.
    :param data_field:
    :return data_description, data_length:
    """
    if data_field == 0:
        return 'No Data', 0
    elif data_field == 1:
        return '8 Bit Integer', 1
    elif data_field == 2:
        return '16 Bit Integer', 2
    elif data_field == 3:
        return '24 Bit Integer', 3
    elif data_field == 4:
        return '32 Bit Integer', 4
    elif data_field == 5:
        return '32 Bit Real', 4
    elif data_field == 6:
        return '48 Bit Integer', 6
    elif data_field == 7:
        return '64 Bit Integer', 7
    elif data_field == 8:
        return 'Selection for Readout', False
    elif data_field == 9:
        return '2 digit BCD', 1
    elif data_field == 10:
        return '4 digit BCD', 2
    elif data_field == 11:
        return '6 digit BCD', 3
    elif data_field == 12:
        return '8 digit BCD', 4
    elif data_field == 13:
        return 'Variable Length', False
    elif data_field == 14:
        return '12 digit BCD', 6
    elif data_field == 15:
        return 'Special Functions', 1
    else:
        raise ValueError(
            f'Data in data_field is not within specified range. 0-15 is allowed. Data used: {data_field}'
            )


def getClientIP():
    """
        Return the IP address of the client who submitted this request.

        @returns: the client IP address or C{None} if the request was submitted
            over a transport where IP addresses do not make sense.
        @rtype: L{str} or C{NoneType}
        """


def _match_histograms(histo1, histo2, bin_x, overlap):
    """Function to match two histograms.

    The matching is done so that the integral of the overlapping regions
    of the two histograms are equal.

    Parameters
    ----------
    histo1 : numpy.array
        The first histogram.
    histo2 : numpy.array
        The second histogram, this is the histogram that will be scaled.
    bin_x : numpy.array
        This is the bin mid-points of the histograms. Note that we
        assume here that `histo1` and `histo2` are obtained using the
        same number of bins and limits.
    overlap : object like list (or tuple or numpy.array).
        This is the overlapping region.

    Returns
    -------
    out[0] : numpy.array
        A scaled version of second input histogram `histo2`.
    out[1] : float
        The calculated scale factor.
    """
    int1, int2 = 0.0, 0.0
    for histi, histj, bin_xi in zip(histo1, histo2, bin_x):
        if overlap[0] <= bin_xi < overlap[1]:
            int1 += histi
            int2 += histj
    if int2 == 0.0:
        scale_factor = 1.0
    else:
        scale_factor = int1 / int2
    return histo2 * scale_factor, scale_factor


def query_successful(api_response):
    """This function reviews the API response from the Community API to verify whether or not the call was successful.

    :param api_response: The response from the API in JSON format
    :type api_response: dict
    :returns: Boolean indicating whether or not the API call was successful
    """
    try:
        success_values = ['successful', 'success']
        successful = True if api_response['status'
            ] in success_values else False
    except (KeyError, IndexError, ValueError, TypeError):
        successful = False
    return successful


def render_pep440_pre(pieces):
    """TAG[.post.devDISTANCE] -- No -dirty.

    Exceptions:
    1: no tags. 0.post.devDISTANCE
    """
    if pieces['closest-tag']:
        rendered = pieces['closest-tag']
        if pieces['distance']:
            rendered += '.post.dev%d' % pieces['distance']
    else:
        rendered = '0.post.dev%d' % pieces['distance']
    return rendered


@classmethod
def from_path(cls, path, section='passlib', encoding='utf-8'):
    """create new CryptContext instance from an INI-formatted file.

        this functions exactly the same as :meth:`from_string`,
        except that it loads from a local file.

        :type path: str
        :arg path:
            path to local file containing INI-formatted config.

        :type section: str
        :param section:
            option name of section to read from, defaults to ``"passlib"``.

        :type encoding: str
        :arg encoding:
            encoding used to load file, defaults to ``"utf-8"``.

        :returns:
            new CryptContext instance, configured based on the parameters
            stored in the file *path*.

        .. versionadded:: 1.6

        .. seealso:: :meth:`from_string` for an equivalent usage example.
        """
    self = cls(_autoload=False)
    self.load_path(path, section=section, encoding=encoding)
    return self