def set_gpu(gpu_mon, args):
    """
    Sets the GPU visibility based on the passed arguments. Takes an already
    initialized GPUMonitor object. Sets GPUs according to args.force_GPU, if
    specified, otherwise sets first args.num_GPUs free GPUs on the system.

    Stops the GPUMonitor process once GPUs have been set
    If gpu_mon is None, this function does nothing

    Args:
        gpu_mon: An initialized GPUMonitor object or None
        args: argparse arguments

    Returns: The number of GPUs that was actually set (different from
    args.num_GPUs if args.force_GPU is set to more than 1 GPU)
    """
    num_GPUs = args.num_GPUs
    if gpu_mon is not None:
        if not args.force_GPU:
            gpu_mon.await_and_set_free_GPU(N=num_GPUs, sleep_seconds=120)
        else:
            gpu_mon.set_GPUs = args.force_GPU
            num_GPUs = len(args.force_GPU.split(','))
        gpu_mon.stop()
    return num_GPUs


def dummy_check(*args, **kwd):
    """ A dummy function, that will never fail.

        >>> dummy_check()
    """


def _input_wrap(prompt, default=None):
    """
    Run input() with formatted prompt, and return
    The while loop can be used to ensure correct output
    """
    understood = False
    while not understood:
        result = input(prompt.format(default=default)).lower().strip()
        if result in {'y', 'yes'}:
            return True
        if result in {'n', 'no'}:
            return False
        if not result:
            return default
        if result in {'quit', 'q', 'exit'}:
            raise RuntimeError('User quit.')
        if not isinstance(default, bool):
            return result
        print(
            "Error: answer not understood. You can 'quit' or hit ctrl+c to exit."
            )


@classmethod
def reconstruct_common(cls, t, x, y, z=None, initial=None):
    """Reconstruct angles from 3 or more detections

        This function converts the arguments to be suitable for the
        algorithm.

        :param t: arrival times of the detectors in ns.
        :param x,y,z: positions of the detectors in m. The height
                      is ignored.
        :param initial: dictionary containing values from previous
                        reconstructions is ignored.

        """
    return cls.reconstruct(t, x, y)


def is_user_context(context):
    """Indicates if the request context is a normal user."""
    if not context:
        return False
    if context.is_admin:
        return False
    if not context.user_id or not context.project_id:
        return False
    return True


def generate_three_opt_candidates(path, sequence):
    """
    Generates all possible tour connections and filters out a trivial one.

    Parameters
    ----------
    path : np.array of float
        square matrix of distances between all topics
    sequence : list of int
        list of indices to perform swap on

    Yields
    ------
    list of int
        possible tour

    """
    chunk_start = path[:sequence[0] + 1]
    chunk_one = path[sequence[0] + 1:sequence[1] + 1]
    chunk_two = path[sequence[1] + 1:sequence[2] + 1]
    chunk_end = path[sequence[2] + 1:]
    for change_chunks in [True, False]:
        middle_chunks = [chunk_two, chunk_one] if change_chunks else [chunk_one
            , chunk_two]
        for reverse_first_chunk in [True, False]:
            if reverse_first_chunk:
                first_chunk = middle_chunks[0][::-1]
            else:
                first_chunk = middle_chunks[0]
            for reverse_second_chunk in [True, False]:
                if reverse_second_chunk:
                    second_chunk = middle_chunks[1][::-1]
                else:
                    second_chunk = middle_chunks[1]
                if (change_chunks or reverse_first_chunk or
                    reverse_second_chunk):
                    tour = chunk_start + first_chunk + second_chunk + chunk_end
                    yield tour


@staticmethod
def _prepare_requirements(requirements, requires_filters):
    """
        Overrides the filters specified in the decorator with the given ones

        :param requirements: Dictionary of requirements (field → Requirement)
        :param requires_filters: Content of the 'requires.filter' component
                                 property (field → string)
        :return: The new requirements
        """
    if not requires_filters or not isinstance(requires_filters, dict):
        return requirements
    new_requirements = {}
    for field, requirement in requirements.items():
        try:
            explicit_filter = requires_filters[field]
            requirement_copy = requirement.copy()
            requirement_copy.set_filter(explicit_filter)
            new_requirements[field] = requirement_copy
        except (KeyError, TypeError, ValueError):
            new_requirements[field] = requirement
    return new_requirements


@classmethod
def get_exec_name(cls):
    """
        Get the name of this executable
        """
    return 'plainbox'


@classmethod
def _preallocate_query(cls, event, stamp):
    """
        Return the query and update for preallocating a document.

        :param event: Event identifier string
        :param stamp: A UTC datetime indicating the document period
        :type event: str
        :type stamp: datetime.datetime

        """
    period = cls.config_period
    query = {'_id': cls.record_id(event, stamp)}
    update = {}
    for interval in cls.config_intervals:
        key = cls._map_interval(interval)
        query[key] = {'$exists': 0}
        update[key] = cls._preallocate_interval(period, interval, stamp)
    update[cls.meta.event] = event
    update[cls.meta.period] = cls._period(stamp)
    update = {'$set': update}
    return query, update


@classmethod
def from_dict(cls, data, **transmute_options):
    """Creates a new instance of the model from a dictionary."""
    return cls.transmute_from(data, **transmute_options)


def move(filename, source, destination):
    """Move file from source space to destination space.

        Leading path elements are stripped from the filename.

        This method is used to move file from tmp to new or from new to cur.

        """


def smooth_weight():
    """Interpolate weight of selected points

    """
    pass


def negative(data=None, out=None, name=None, **kwargs):
    """Numerical negative of the argument, element-wise.

    The storage type of ``negative`` output depends upon the input storage type:

       - negative(default) = default
       - negative(row_sparse) = row_sparse
       - negative(csr) = csr

    Parameters
    ----------
    data : NDArray
        The input array.

    out : NDArray, optional
        The output NDArray to hold the result.

    Returns
    -------
    out : NDArray or list of NDArrays
        The output of this function.
    """
    return 0,


def _preprocess(df):
    """
    given a DataFrame where records are stored row-wise, rearrange it
    such that records are stored column-wise.
    """
    df = df.stack()
    df.index.rename(['id', 'time'], inplace=True)
    df.name = 'value'
    df = df.reset_index()
    return df


def write_section(outfile, section, settings):
    """Writes a scoring function specific section to the final.ini file"""
    outfile.write('\n[%s]\n' % section)
    for setting, param in settings.items():
        if setting == 'scaling':
            if param[0] == 'scaling_const':
                outfile.write('scaling_const = %f\n' % param[1])
            elif param[0] == 'scaling_rvec':
                outfile.write('scaling_rvec = %s\n' % param[1])
        else:
            outfile.write('%s = %s\n' % (setting, str(param)))


def put_recon_cache_entry(cache_entry, key, item):
    """
    Update a recon cache entry item.

    If ``item`` is an empty dict then any existing ``key`` in ``cache_entry``
    will be deleted. Similarly if ``item`` is a dict and any of its values are
    empty dicts then the corrsponsing key will be deleted from the nested dict
    in ``cache_entry``.

    We use nested recon cache entries when the object auditor
    runs in parallel or else in 'once' mode with a specified subset of devices.

    :param cache_entry: a dict of existing cache entries
    :param key: key for item to update
    :param item: value for item to update
    """
    if isinstance(item, dict):
        if not item:
            cache_entry.pop(key, None)
            return
        if key not in cache_entry or key in cache_entry and not isinstance(
            cache_entry[key], dict):
            cache_entry[key] = {}
        for k, v in item.items():
            if v == {}:
                cache_entry[key].pop(k, None)
            else:
                cache_entry[key][k] = v
    else:
        cache_entry[key] = item


def get_url(cls):
    """
        Gets the template url. This can be a remote url (pointing to a git
        repository) or a local url (pointing to the directory that contains the
        templates)
        """
    pass


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


def get_encodings(hint_encoding='utf-8'):
    """Used to try different encoding.
    Function copied from Odoo 11.0 (odoo.loglevels.get_encodings).
    This piece of code is licensed under the LGPL-v3 and so it is compatible
    with the LGPL-v3 license of OdooRPC::

        - https://github.com/odoo/odoo/blob/11.0/LICENSE
        - https://github.com/odoo/odoo/blob/11.0/COPYRIGHT
    """
    fallbacks = {'latin1': 'latin9', 'iso-8859-1': 'iso8859-15', 'cp1252':
        '1252'}
    if hint_encoding:
        yield hint_encoding
        if hint_encoding.lower() in fallbacks:
            yield fallbacks[hint_encoding.lower()]
    for charset in ['utf8', 'latin1', 'ascii']:
        if not hint_encoding or charset.lower() != hint_encoding.lower():
            yield charset
    from locale import getpreferredencoding
    prefenc = getpreferredencoding()
    if prefenc and prefenc.lower() != 'utf-8':
        yield prefenc
        prefenc = fallbacks.get(prefenc.lower())
        if prefenc:
            yield prefenc


def write(file):
    """Write the entry to the file."""


def write_to_buf(output, filepath_or_buffer=None):
    """
    Write output to a file or buffer. If none is provided, nothing happens.

    Parameters
    ----------
    output : str
        The output to write.
    filepath_or_buffer : str or file handle, default None
        The file or buffer to which to write the output.
    """
    if filepath_or_buffer is None:
        return
    if isinstance(filepath_or_buffer, str):
        f = open(filepath_or_buffer, 'w')
        close_file = True
    else:
        f = filepath_or_buffer
        close_file = False
    try:
        f.write(output)
    finally:
        if close_file:
            f.close()


def selection_set(select: bool=False):
    """Set cursor selection

    :param select: Select, Set selection end rather than cursor
    :type select: bool
    """
    pass


def psi_Mg_CO3_SO4_HMW84(T, P):
    """c-a-a': magnesium carbonate sulfate [HMW84]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


def w_fct(stim, color_control):
    """function for word_task, to modulate strength of word reading based on 1-strength of color_naming ControlSignal"""
    print('color control: ', color_control)
    return stim * (1 - color_control)


@classmethod
def get_real_child_model(cls, data):
    """Returns the real base class specified by the discriminator"""
    discriminator_value = data[cls.json_discriminator_key]
    return cls.discriminator_value_class_map.get(discriminator_value)


def reindex(indexes=None):
    """Re-index a content in the catalog. If a list of indexes
        names is given as argument, only those indexes will re-indexed.
        """


def contrainte(X):
    """
    C dans @see fn Arrow_Hurwicz
    """
    x, y = X
    f = x + 2 * y - 1
    d = [1, 2]
    return f, d


def _is_absolute_iri(v):
    """
    Returns True if the given value is an absolute IRI, False if not.

    :param v: the value to check.

    :return: True if the value is an absolute IRI, False if not.
    """
    return ':' in v


def read_header_marccd(filename):
    """
    return string of parameters found in header in marccd image file .mccd

    - print allsentences  displays the header
    - use allsentences.split('\\n') to get a list
    """
    f = open(filename, 'rb')
    f.seek(2048)
    posbyte = 0
    allsentences = ''
    for _ in list(range(32)):
        tt = f.read(32)
        s1 = tt.strip('\x00')
        if s1 != '':
            allsentences += s1 + '\n'
        posbyte += 32
    tt = f.read(1024)
    s1 = tt.strip('\x00')
    if s1 != '':
        allsentences += s1 + '\n'
    f.close()
    return allsentences


def lineage(resource):
    """
    Return a generator representing the :term:`lineage` of the
    :term:`resource` object implied by the ``resource`` argument.  The
    generator first returns ``resource`` unconditionally.  Then, if
    ``resource`` supplies a ``__parent__`` attribute, return the resource
    represented by ``resource.__parent__``.  If *that* resource has a
    ``__parent__`` attribute, return that resource's parent, and so on,
    until the resource being inspected either has no ``__parent__``
    attribute or which has a ``__parent__`` attribute of ``None``.
    For example, if the resource tree is::

      thing1 = Thing()
      thing2 = Thing()
      thing2.__parent__ = thing1

    Calling ``lineage(thing2)`` will return a generator.  When we turn
    it into a list, we will get::

      list(lineage(thing2))
      [ <Thing object at thing2>, <Thing object at thing1> ]
    """
    while resource is not None:
        yield resource
        try:
            resource = resource.__parent__
        except AttributeError:
            resource = None


def _np_prod(a, axis=None, dtype=None, out=None, keepdims=False):
    """
    Return the product of array elements over a given axis.

    Parameters
    ----------
    a : ndarray
        Input data.
    axis : None or int or tuple of ints, optional
        Axis or axes along which a product is performed.
        The default (`axis` = `None`) is perform a product over all
        the dimensions of the input array. `axis` may be negative, in
        which case it counts from the last to the first axis.
        If this is a tuple of ints, a product is performed on multiple
        axes, instead of a single axis or all the axes as before.
    dtype : data-type, optional
        The data-type of the returned array, as well as of the accumulator
        in which the elements are multiplied.  By default, if `a` is of
        integer type, `dtype` is the default platform integer. (Note: if
        the type of `a` is unsigned, then so is `dtype`.)  Otherwise,
        the dtype is the same as that of `a`.
    out : ndarray, optional
        Alternative output array in which to place the result. It must have
        the same shape as the expected output, but the type of the
        output values will be cast if necessary.
    keepdims : bool, optional
        If this is set to True, the axes which are reduced are left
        in the result as dimensions with size one. With this option,
        the result will broadcast correctly against the original `arr`.

    Returns
    -------
    product_along_axis : ndarray, see `dtype` parameter above.
        An array shaped as `a` but with the specified axis removed.
        Returns a reference to `out` if specified.

    See Also
    --------
    ndarray.prod : equivalent method

    Notes
    -----
    Arithmetic is modular when using integer types, and no error is
    raised on overflow.  That means that, on a 32-bit platform:

    >>> x = np.array([536870910, 536870910, 536870910, 536870910])
    >>> np.prod(x) #random
    array(8.307675e+34)

    Examples
    --------
    By default, calculate the product of all elements:

    >>> np.prod(np.array([1.,2.]))
    array(2.)

    Even when the input array is two-dimensional:

    >>> np.prod(np.array([1.,2.,3.,4.]).reshape((2,2)))
    array(24.)

    But we can also specify the axis over which to multiply:

    >>> np.prod(np.array([1.,2.,3.,4.]).reshape((2,2)), axis=1)
    array([  2.,  12.])

    If the type of `x` is unsigned, then the output type is
    the unsigned platform integer:

    >>> x = np.array([1, 2, 3], dtype=np.uint8)
    >>> np.prod(x).dtype == np.uint8
    True

    If `x` is of a signed integer type, then the output type
    is the default platform integer:

    >>> x = np.array([1, 2, 3], dtype=np.int8)
    >>> np.prod(x).dtype == np.int8
    True
    """
    pass


def properties(lines):
    """Parse properties block

    Returns:
        dict: {property_type: (atom_index, value)}
    """
    results = {}
    for i, line in enumerate(lines):
        type_ = line[3:6]
        if type_ not in ['CHG', 'RAD', 'ISO']:
            continue
        count = int(line[6:9])
        results[type_] = []
        for j in range(count):
            idx = int(line[10 + j * 8:13 + j * 8])
            val = int(line[14 + j * 8:17 + j * 8])
            results[type_].append((idx, val))
    return results


def calculate_number_of_stems_by_type(morphology, node_types):
    """
        Calculate the number of soma stems.
        This is defined as the total number of non-soma child nodes on soma nodes.

        Parameters
        ----------
        morphology: Morphology object

        node_types: list (AXON, BASAL_DENDRITE, APICAL_DENDRITE)
        Type to restrict search to

        Returns
        -------

        Scalar value

    """
    soma = morphology.get_root()
    return len(morphology.get_children_of_node_by_types(soma, node_types))


def plotfred(data, title='tmp', maxi=87654321):
    """DEPRECATED: Plot data should be given as dataframe or fredcode."""
    msg = 'plotfred() DEPRECATED. Instead use get() and plot().'
    raise DeprecationWarning(msg)


@classmethod
def relations(cls):
    """
        Return model relations

        :rtype: dict
        :return:
        """
    meta_class = cls._meta()
    return meta_class.relations


def recherche(li, c):
    """
    Retourne l'index d'un élément ou -1 si non trouvé

    @param      li      liste
    @param      c       élément à trouver
    @return             position

    .. exref::
        :tag: Base
        :title: recherche avec index

        Lorsqu'on cherche un élément dans un tableau, on cherche plus souvent
        sa position que le fait que le tableau contient cet élément.

        ::

            def recherche (li, c) :
                for i,v in enumerate (li) :
                    if v == c : return i
                return -1
            li = [ 45, 32, 43, 56 ]
            print (recherche (li, 43))  # affiche 2

        En python, il existe un fonction simple qui permet de faire ça :

        ::

            print (li.index (43))  # affiche 2

        Lorsque l'élément n'y est pas, on retourne souvent la position ``-1``
        qui ne peut être prise par aucun élément :

        ::

            if c in li : return li.index(c)
            else: return -1

        Même si ce bout de code parcourt deux fois le tableau (une fois déterminer
        sa présence, une seconde fois pour sa position), ce code est souvent plus rapide
        que la première version et la probabilité d'y faire une erreur plus faible.
    """
    if c in li:
        return li.index(c)
    else:
        return -1


def _check_wildcard(value):
    """Always returns true."""
    return True


def add_str_to_keys(dct, key_str):
    """ convert dictionary keys by adding key_str"""
    new_dct = {}
    for key, value in dct.items():
        if key_str in key:
            new_dct[key] = value
        else:
            new_dct[key + key_str] = value
    return new_dct


@classmethod
def is_datasource_for(cls, data, header, **kwargs):
    """Determines if header corresponds to an AIA image"""
    return str(header.get('instrume', '')).startswith('AIA')


def build_relabel_dict(x):
    """Relabel the input ids to continuous ids that starts from zero.

    The new id follows the order of the given node id list.

    Parameters
    ----------
    x : list
      The input ids.

    Returns
    -------
    relabel_dict : dict
      Dict from old id to new id.
    """
    relabel_dict = {}
    for i, v in enumerate(x):
        relabel_dict[v] = i
    return relabel_dict


def shortest_id(name, names):
    """ Return shortest name that isn't a duplicate in names """
    if name in names:
        names.remove(name)
    for i, letter in enumerate(name):
        for other_name in names:
            if other_name[i:i + 1] == letter:
                break
        else:
            break
    return name[0:i + 1]


def need_search_path_refresh(sql):
    """Determines if the search_path should be refreshed by checking if the
    sql has 'set search_path'."""
    return 'set search_path' in sql.lower()


def _related(f1, f2):
    """return True if f1 and f2 filectx have a common ancestor

    Walk back to common ancestor to see if the two files originate
    from the same file. Since workingfilectx's rev() is None it messes
    up the integer comparison logic, hence the pre-step check for
    None (f1 and f2 can only be workingfilectx's initially).
    """
    if f1 == f2:
        return True
    g1, g2 = f1.ancestors(), f2.ancestors()
    try:
        f1r, f2r = f1.linkrev(), f2.linkrev()
        if f1r is None:
            f1 = next(g1)
        if f2r is None:
            f2 = next(g2)
        while True:
            f1r, f2r = f1.linkrev(), f2.linkrev()
            if f1r > f2r:
                f1 = next(g1)
            elif f2r > f1r:
                f2 = next(g2)
            else:
                return f1 == f2
    except StopIteration:
        return False


def geom_from_metadata(meta):
    """Return a geometry from a Landsat scene metadata as returned by USGS api

    Args:
        meta (dict): Landsat scene metadata as returned by Usgs Api

    Example:
        >>> from lsru import Usgs
        >>> from lsru.utils import geom_from_metadata
        >>> import datetime
        >>> from shapely.geometry import shape
        >>> from pprint import pprint

        >>> usgs = Usgs()
        >>> usgs.login()
        >>> scene_list = usgs.search(collection='LANDSAT_8_C1',
        ...                          bbox=(3.5, 43.4, 4, 44),
        ...                          begin=datetime.datetime(2012,1,1),
        ...                          end=datetime.datetime(2016,1,1))
        >>> geom = geom_from_metadata(scene_list[0])
        >>> s = shape(geom)
        >>> pprint(geom)
        >>> print(s.is_valid)

    Returns:
        dict: GeoJson like geometry. CRS is always in longlat (EPSG 4326)
    """
    geom = {'type': 'Polygon', 'coordinates': [[[meta['lowerLeftCoordinate'
        ]['longitude'], meta['lowerLeftCoordinate']['latitude']], [meta[
        'upperLeftCoordinate']['longitude'], meta['upperLeftCoordinate'][
        'latitude']], [meta['upperRightCoordinate']['longitude'], meta[
        'upperRightCoordinate']['latitude']], [meta['lowerRightCoordinate']
        ['longitude'], meta['lowerRightCoordinate']['latitude']], [meta[
        'lowerLeftCoordinate']['longitude'], meta['lowerLeftCoordinate'][
        'latitude']]]]}
    return geom


def lambd_HF_Na_MP98(T, P):
    """n-c: hydrogen-fluoride sodium [MP98]."""
    lambd = 0.011
    valid = T == 298.15
    return lambd, valid


def retrieve_task_id(context):
    """Helper to retrieve the `Task` identifier from the message `body`.
    This helper supports Protocol Version 1 and 2. The Protocol is well
    detailed in the official documentation:
    http://docs.celeryproject.org/en/latest/internals/protocol.html
    """
    headers = context.get('headers')
    body = context.get('body')
    if headers:
        return headers.get('id')
    else:
        return body.get('id')


def add_data(conn, mnemonic, data):
    """Add data of a specific mnemonic to database if it not exists
    Parameters
    ----------
    conn : DBobject
        connection object to access database
    mnemonic : string
        identifies the table
    data : list
        specifies the data
    """
    c = conn.cursor()
    c.execute('SELECT id from {} WHERE start_time= {}'.format(mnemonic,
        data[0]))
    temp = c.fetchall()
    if len(temp) == 0:
        c.execute(
            'INSERT INTO {} (start_time,end_time,data_points,average,deviation)                 VALUES (?,?,?,?,?)'
            .format(mnemonic), data)
        conn.commit()
    else:
        print('data for {} already exists'.format(mnemonic))


@classmethod
def name_to_sl(cls, name):
    """
        Translate an attribute name to States Language.
        """
    return cls._FIELDS[name]


def is_element(o, element_name):
    """Return whether a given object is a certain element generator."""
    return hasattr(o, '__iter__') and hasattr(o, 'element_name'
        ) and o.element_name == element_name


def _get_caption(table):
    """
    Get the caption of the associated wiki table.

    Parameters
    ----------
    table: BeautifulSoup

    Returns
    -------
    str
    """
    caption_text = None
    caption = table.find('caption')
    if caption:
        caption_text = caption.text.strip()
    return caption_text


def opPair(tokenlist):
    """Return pairs of tokens to the caller as a generator."""
    it = iter(tokenlist)
    while 1:
        try:
            yield next(it), next(it)
        except StopIteration:
            break


def fuzzy_get(_dict, aliases, default):
    """
    get a value from a list of keys(aliases),
    return first value which it's key exist in dict,
    if no keys found, return default.
    """
    for name in aliases:
        if name in _dict:
            return _dict[name]
    else:
        return default


def get_class_base(c):
    """Return the class name."""
    return c.name.__class__.__name__


def trunc(f, n):
    """
    Truncates a float f to n decimal places before rounding

    :param float f: The number to truncates
    :param int n: Decimal number to place before rounding
    :returns: The corresponding truncated number
    :rtype: *float*

    """
    slen = len('%.*f' % (n, f))
    return float(str(f)[:slen])


def same_dimension(*quantities, dimension=None):
    """Check that all Quantities have the Same Dimensionality."""
    if not quantities:
        return not dimension
    quantities = iter(quantities)
    first = next(quantities).dimensionality
    if not dimension or dimension == first:
        return all(map(lambda o: o.dimensionality == first, quantities))
    return False


def dummy_fn() ->type(None):
    """
    A placeholder of a function.

    :return:
        None
    """
    pass


def _mlsd2(ftp, path='.'):
    """
    Fall back to using `dir` instead of `mlsd` if not supported.

    This parses a Linux style `ls -l` response to `dir`, but the response may
    be platform dependent.

    Parameters
    ----------
    ftp: ftplib.FTP
    path: str
        Expects to be given path, but defaults to ".".
    """
    lines = []
    minfo = []
    ftp.dir(path, lines.append)
    for line in lines:
        line = line.split()
        this = line[-1], {'modify': ' '.join(line[5:8]), 'unix.owner': line
            [2], 'unix.group': line[3], 'unix.mode': line[0], 'size': line[4]}
        if 'd' == this[1]['unix.mode'][0]:
            this[1]['type'] = 'dir'
        else:
            this[1]['type'] = 'file'
        minfo.append(this)
    return minfo


def update_songs_metadata(xml_target, songs_and_meta, args):
    """ Use metadata to update the information on the target database."""
    for song in xml_target.get_all_songs():
        key = xml_target.get_aat(song)
        if key in songs_and_meta:
            new_rating = songs_and_meta[key]['rating']
            old_rating = xml_target.get_rating(song)
            if old_rating is not None:
                old_rating.text = new_rating
            else:
                xml_target.add_rating(song, new_rating)
            new_playcount = songs_and_meta[key]['playcount']
            old_playcount = xml_target.get_playcount(song)
            if old_playcount is not None:
                if args.reset_playcounts or old_playcount.text is None:
                    old_playcount.text = new_playcount
                else:
                    old_playcount.text = str(int(old_playcount.text) + int(
                        new_playcount))
            else:
                xml_target.add_playcount(song, new_playcount)


@classmethod
def filter(cls, **kwargs):
    """
        Override this to provide a method to filter by kwargs.

        Initially this is being implemented for result sets, with the intention
        to also implement it for query sets, for which Model.objects.filter()
        will return a generator which will not trigger any API requests until
        we attempt to index it or convert it to a list etc.
        """


@classmethod
def names(cls):
    """Implement :meth:`~.solver.Solver.names`."""
    return 'glpk', 'GLPK', 'GNU Linear Programming Kit'


def rgb_to_xyz(red, green, blue):
    """
    Convert standard RGB color to XYZ color.

    :arg int red: RGB value of Red.
    :arg int green: RGB value of Green.
    :arg int blue: RGB value of Blue.
    :returns: Tuple (X, Y, Z) representing XYZ color
    :rtype: tuple

    D65/2° standard illuminant
    """
    rgb = []
    for val in (red, green, blue):
        val /= 255.0
        if val > 0.04045:
            val = pow((val + 0.055) / 1.055, 2.4)
        else:
            val /= 12.92
        val *= 100
        rgb.append(val)
    red, green, blue = rgb
    x_val = red * 0.4124 + green * 0.3576 + blue * 0.1805
    y_val = red * 0.2126 + green * 0.7152 + blue * 0.0722
    z_val = red * 0.0193 + green * 0.1192 + blue * 0.9505
    return x_val, y_val, z_val


@classmethod
def get_timestamp_types(cls):
    """Get the list of all timestamp types"""
    return [cls.DATETIME, cls.TIMESTAMP]


@classmethod
def _from_et(cls, root, parent):
    """
        Create a :class:`Dimension` from a :code:`dimension` element.

        :param root: The element tree tree to parse.
        :param parent: :class:`EagleFilePart` that should hold the resulting :class:`EagleFilePart`
        :rtype: :class:`Dimension`
        """
    n = cls()
    n._init_from_et(root, parent)
    return n


def set_external_data_dir(path):
    """Assign data directory manually

    Otherwise, this will be inferred from the environment variable "FLOWDEC_DATA_DIR" and if
    that is not set will default to `DATA_DIR_DEFAULT`
    Args:
        path: Path containing image data to use for validation and experimentation
    """
    global _DATA_DIR
    _DATA_DIR = path


def replace():
    """Replace text with the specified text

    """
    pass


def filled(f, d):
    """
        Check if a field exists in a dictionary, and if that field is not None or the empty string.
    """
    return f in d and d[f] is not None and d[f] != ''


def seconds(value):
    """Return the equivalent number of seconds, floating-point."""
    return (value.days * 86400.0 + value.seconds + value.microseconds /
        1000000.0)


def get_service_type(f):
    """Retrieves service type from function."""
    return getattr(f, 'service_type', None)


def get_header_lines(header):
    """
    Splits the header into lines, putting multi-line headers together.
    """
    r = []
    lines = header.split(b'\n')
    for line in lines:
        if line.startswith((b' ', b'\t')):
            r[-1] = r[-1] + line[1:]
        else:
            r.append(line)
    return r


@classmethod
def _strip_quotes(cls, val, **kwargs):
    """
        strips quotes from a string
        """
    return val.replace('"', '')


def get_device_type(device_type=0):
    """Return the device type from a device_type list."""
    device_types = {(0): 'Unknown', (1): 'Classic - BR/EDR devices', (2):
        'Low Energy - LE-only', (3): 'Dual Mode - BR/EDR/LE'}
    if device_type in [1, 2, 3]:
        return_value = device_types[device_type]
    else:
        return_value = device_types[0]
    return return_value


def is_orderer_tls_true(opts):
    """Check if tls is enabled for orderer
        Args:
            opts (dict): Nephos options dict.

        Returns:
            bool: return true if tls is enabled for orderer and false otherwise
    """
    if 'tls' in opts['ordering']:
        return opts['ordering']['tls']['enable'] == 'true'
    return False


def HowDidIgetHere(wherecalledonly=False):
    """Show a traceback with calls that brought us to the current location.
    Used for debugging.
    """
    import traceback
    if wherecalledonly:
        i = traceback.format_list(traceback.extract_stack()[:-1])[-2]
        print(i.strip().rstrip())
    else:
        print(70 * '*')
        for i in traceback.format_list(traceback.extract_stack()[:-1]):
            print(i.strip().rstrip())
        print(70 * '*')


def packstrip(bin, p):
    """Creates a Strip which fits into bin.

    Returns the Packages to be used in the strip, the dimensions of the strip as a 3-tuple
    and a list of "left over" packages.
    """
    s = []
    r = []
    ss = sw = sl = 0
    bs = bin.heigth
    sapp = s.append
    rapp = r.append
    ppop = p.pop
    while p and ss <= bs:
        n = ppop(0)
        nh, nw, nl = n.size
        if ss + nh <= bs:
            ss += nh
            sapp(n)
            if nw > sw:
                sw = nw
            if nl > sl:
                sl = nl
        else:
            rapp(n)
    return s, (ss, sw, sl), r + p


def p_loop_stmt(p):
    """loop_stmt : loop_head suite """
    p[0] = ['LOOP'] + p[1] + [p[2]]


@staticmethod
def add_value_to_unwritten_key(result_dict, value):
    """
        Adds a key name to the list of dictionary entries that will not be in the report.data file
        :param result_dict:
        :param value: result_dict entry (string)
        :return:
        """
    return result_dict['unwritten.keys'].append(value)


def random_flip_left_right(data=None, out=None, name=None, **kwargs):
    """

    Defined in src/operator/image/image_random.cc:L200

    Parameters
    ----------
    data : NDArray
        The input.

    out : NDArray, optional
        The output NDArray to hold the result.

    Returns
    -------
    out : NDArray or list of NDArrays
        The output of this function.
    """
    return 0,


def put(zelf, func, *args):
    """ put a new function to execute. """
    if not zelf._running:
        zelf._running = True
        zelf.start()
    zelf._queue.put_nowait((func, args))
    return zelf


def set_precision_provider(precision_provider):
    """
    Set precision provider for Money instances.

    Default precision provider is
    `shuup.utils.babel_precision_provider.get_precision`.

    :type precision_provider: Callable[[str], decimal.Decimal|None]
    :param precision_provider:
      Function which will return precision for given currency code, or
      None for unhandled currency codes, e.g. ``func('USD')`` could
      return ``Decimal('0.01')``.
    """
    assert callable(precision_provider)
    global _precision_provider
    _precision_provider = precision_provider


def bytes2human(n, format='%(value).1f%(symbol)s'):
    """Used by various scripts. See:
    http://goo.gl/zeJZl

    >>> bytes2human(10000)
    '9.8K'
    >>> bytes2human(100001221)
    '95.4M'
    """
    symbols = 'B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
    prefix = {}
    for i, s in enumerate(symbols[1:]):
        prefix[s] = 1 << (i + 1) * 10
    for symbol in reversed(symbols[1:]):
        if n >= prefix[symbol]:
            value = float(n) / prefix[symbol]
            return format % locals()
    return format % dict(symbol=symbols[0], value=n)


def RenumberDockRows(docks):
    """
    Takes a dock and assigns sequential numbers
    to existing rows.  Basically it takes out the gaps so if a
    dock has rows with numbers 0, 2, 5, they will become 0, 1, 2.

    :param `docks`: a list of :class:`AuiDockInfo` structures.
    """
    for ii in range(len(docks)):
        dock = docks[ii]
        dock.dock_row = ii
        for jj in range(len(dock.panes)):
            dock.panes[jj].dock_row = ii
        docks[ii] = dock
    return docks


def isoformat(dt):
    """Return iso-formatted timestamp

    Like .isoformat(), but uses Z for UTC instead of +00:00
    """
    return dt.isoformat().replace('+00:00', 'Z')


def _extractPitch(nOrP):
    """
    utility function to return either the object itself
    or the `.pitch` if it's a Note.

    >>> p = pitch.Pitch('D#4')
    >>> interval._extractPitch(p) is p
    True
    >>> n = note.Note('E-4')
    >>> interval._extractPitch(n) is n.pitch
    True

    """
    if 'Pitch' in nOrP.classes:
        return nOrP
    else:
        return nOrP.pitch


def default_scrubber(text):
    """
    remove spurious punctuation (for English)
    """
    return text.lower().replace("'", '')


@classmethod
def __str_ins(cls, s: str, ins: str, index: int) ->str:
    """ insert a char into a immutable string
            index start of 0
        """
    return s[:index] + ins + s[index:]


def getTOFbeta(ins, dsp):
    """get TOF peak profile beta

    :param dict ins: instrument parameters with at least 'beat-0', 'beta-1' & 'beta-q'
      as values only
    :param float dsp: d-spacing of peak

    :returns: float getTOFbeta: peak beat

    """
    return ins['beta-0'] + ins['beta-1'] / dsp ** 4 + ins['beta-q'] / dsp ** 2


def xray_wavelength(energy):
    """
    convert energy (keV) to wavelength (angstrom)
    """
    return 12.398 / energy


def render_plugins(plugins, context, placeholder, processors=None):
    """
    Renders a collection of plugins with the given context, using the appropriate processors
    for a given placeholder name, and returns a list containing a "rendered content" string
    for each plugin.

    This is the main plugin rendering utility function, use this function rather than
    Plugin.render_plugin().
    """
    out = []
    total = len(plugins)
    for index, plugin in enumerate(plugins):
        plugin._render_meta.total = total
        plugin._render_meta.index = index
        context.push()
        out.append(plugin.render_plugin(context, placeholder, processors=
            processors))
        context.pop()
    return out


def is_valid_vc_index(vc_index):
    """
    Validates vc_index.  vc_index must be within 0-65535 range.

    :type vc_index: int
    :param vc_index: The vc index to be validated.

    :rtype: bool
    :return: True or False depending on whether vc index passes validation.
    """
    if vc_index is None:
        return False
    vc_index = int(vc_index)
    if vc_index < 0 or vc_index > 65535:
        return False
    return True


def first_neighbours_last(batches, current_batch_idx, nb_left, nb_right):
    """Build a sublist from a large batch list.

    This is used to display batch links for a large table.

    arguments:
     * :param batches: a large sequence (may be a batches as well)
     * :param current_batch_idx: index of the current batch or item
     * :param nb_left: number of neighbours before the current batch
     * :param nb_right: number of neighbours after the current batch

    The returned list gives:
     * the first batch
     * a None separator if necessary
     * left neighbours of the current batch
     * the current batch
     * right neighbours of the current batch
     * a None separator if necessary
     * the last batch

    Example:

      >>> from pyams_batching.batch import first_neighbours_last as f_n_l
      >>> batches = range(100) # it works with real batches as well

    We try to get subsets at different levels:

      >>> for i in range(0,6):
      ...    f_n_l(batches, i, 2, 2)
      [0, 1, 2, None, 99]
      [0, 1, 2, 3, None, 99]
      [0, 1, 2, 3, 4, None, 99]
      [0, 1, 2, 3, 4, 5, None, 99]
      [0, None, 2, 3, 4, 5, 6, None, 99]
      [0, None, 3, 4, 5, 6, 7, None, 99]

      >>> for i in range(93, 99):
      ...    f_n_l(batches, i, 2, 2)
      [0, None, 91, 92, 93, 94, 95, None, 99]
      [0, None, 92, 93, 94, 95, 96, None, 99]
      [0, None, 93, 94, 95, 96, 97, None, 99]
      [0, None, 94, 95, 96, 97, 98, 99]
      [0, None, 95, 96, 97, 98, 99]
      [0, None, 96, 97, 98, 99]

    Try with no previous and no next batch:

      >>> f_n_l(batches, 0, 0, 0)
      [0, None, 99]
      >>> f_n_l(batches, 1, 0, 0)
      [0, 1, None, 99]
      >>> f_n_l(batches, 2, 0, 0)
      [0, None, 2, None, 99]

    Try with only 1 previous and 1 next batch:

      >>> f_n_l(batches, 0, 1, 1)
      [0, 1, None, 99]
      >>> f_n_l(batches, 1, 1, 1)
      [0, 1, 2, None, 99]
      >>> f_n_l(batches, 2, 1, 1)
      [0, 1, 2, 3, None, 99]
      >>> f_n_l(batches, 3, 1, 1)
      [0, None, 2, 3, 4, None, 99]

    Try with incoherent values:

      >>> f_n_l(batches, 0, -4, -10)
      Traceback (most recent call last):
      ...
      AssertionError
      >>> f_n_l(batches, 2000, 3, 3)
      Traceback (most recent call last):
      ...
      AssertionError
    """
    sublist = []
    first_idx = 0
    last_idx = len(batches) - 1
    assert 0 <= current_batch_idx <= last_idx
    assert nb_left >= 0 and nb_right >= 0
    prev_idx = current_batch_idx - nb_left
    next_idx = current_batch_idx + 1
    first_batch = batches[0]
    last_batch = batches[last_idx]
    if first_idx < current_batch_idx:
        sublist.append(first_batch)
    if first_idx + 1 < prev_idx:
        sublist.append(None)
    for i in range(prev_idx, prev_idx + nb_left):
        if first_idx < i:
            sublist.append(batches[i])
    sublist.append(batches[current_batch_idx])
    for i in range(next_idx, next_idx + nb_right):
        if i < last_idx:
            sublist.append(batches[i])
    if next_idx + nb_right < last_idx:
        sublist.append(None)
    if current_batch_idx < last_idx:
        sublist.append(last_batch)
    return sublist


def get_groupids(userid):
    """ Return all the group-related principal identifiers for a user with
        the user principal identifier ``userid`` as a sequence.  If no user
        exists under ``userid``, return ``None``."""


def list_local_disks(GatewayARN=None):
    """
    Returns a list of the gateway's local disks. To specify which gateway to describe, you use the Amazon Resource Name (ARN) of the gateway in the body of the request.
    The request returns a list of all disks, specifying which are configured as working storage, cache storage, or stored volume or not configured at all. The response includes a DiskStatus field. This field can have a value of present (the disk is available to use), missing (the disk is no longer connected to the gateway), or mismatch (the disk node is occupied by a disk that has incorrect metadata or the disk content is corrupted).
    See also: AWS API Documentation

    Examples
    The request returns a list of all disks, specifying which are configured as working storage, cache storage, or stored volume or not configured at all.
    Expected Output:

    :example: response = client.list_local_disks(
        GatewayARN='string'
    )

    :type GatewayARN: string
    :param GatewayARN: [REQUIRED]
            The Amazon Resource Name (ARN) of the gateway. Use the ListGateways operation to return a list of gateways for your account and region.

    :rtype: dict
    :return: {
        'GatewayARN': 'string',
        'Disks': [
            {
                'DiskId': 'string',
                'DiskPath': 'string',
                'DiskNode': 'string',
                'DiskStatus': 'string',
                'DiskSizeInBytes': 123,
                'DiskAllocationType': 'string',
                'DiskAllocationResource': 'string'
            },
        ]
    }

    """
    pass


def default_config() ->dict:
    """Returns default NN architecture/learning parameters"""
    return {'epochs': 3000, 'learning_rate': 0.01, 'beta_1': 0.9, 'beta_2':
        0.999, 'epsilon': 1e-08, 'decay': 0.0, 'hidden_layers': [[32,
        'relu'], [32, 'relu']], 'output_activation': 'linear', 'batch_size':
        32, 'patience': 128}


def get_heat_required_input(data):
    """Returns Heat required user input fields."""
    heat_params = []
    heat_param_dict = data['parameters']
    for key, heat_param in heat_param_dict.items():
        heat_params.append([key, heat_param.get('type'), heat_param.get(
            'default'), heat_param.get('description')])
    return sorted(heat_params)


def set_up_python_package(name, tests=True):
    """
    Set up a basic Python package

    Parameters
    ----------
    name : str
        What is the package name?
    tests : bool, optional
        Should tests be included?
    """
    pass


def _coalesce(elems):
    """Sort and coalesce adjacent ranges (in-place).

    Args:
        elems (list): List of 2-tuples (lo, hi).

    Returns:
        list: List `elems` after sorting in-place and coalescing ranges.

    Raises:
        ValueError: Overlapping ranges.
    """
    elems.sort()
    i = 0
    while i < len(elems) - 1:
        (lo0, hi0), (lo1, hi1) = elems[i:i + 2]
        if not lo0 <= hi0 < lo1 <= hi1:
            raise ValueError('Range overlaps at index %d: %r' % (i, elems[i
                :i + 2]))
        if lo1 == hi0 + 1:
            elems[i:i + 2] = [(lo0, hi1)]
        else:
            i += 1
    return elems


def as_text(value):
    """
	Convert the given value to a string

	:param value: value to convert to a string

	:rtype: str
	"""
    if value is None:
        return ''
    return str(value)


def _extract_annotations_from_span(span):
    """Extract and convert time event annotations to Honeycomb annotations"""
    if span.time_events is None:
        return []
    annotations = []
    for time_event in span.time_events:
        annotation = time_event.annotation
        if not annotation:
            continue
        annotations.append({'timestamp': time_event.timestamp, 'value':
            annotation.description})
    return annotations


def sum_2_level_dict(two_level_dict):
    """Sum all entries in a two level dict

    Arguments
    ----------
    two_level_dict : dict
        Nested dict

    Returns
    -------
    tot_sum : float
        Number of all entries in nested dict
    """
    tot_sum = 0
    for j in two_level_dict.values():
        tot_sum += sum(j.values())
    return tot_sum


def _cdp_split_header_x_y_counts(header_split_count_1, header_split_count_2,
    refs):
    """
    For each header in reference that has > 0 alignments in 1 file

    dict --> {header: (split counts 1:split counts 2)}
    """
    counts_by_ref = {}
    for header in refs.headers():
        if header in header_split_count_1 and header in header_split_count_2:
            counts_by_ref[header] = header_split_count_1[header
                ], header_split_count_2[header]
        elif header in header_split_count_1 and header not in header_split_count_2:
            counts_by_ref[header] = header_split_count_1[header], 0
        elif header not in header_split_count_1 and header in header_split_count_2:
            counts_by_ref[header] = 0, header_split_count_2[header]
    return counts_by_ref


def read_table(file):
    """
    convert to html format table
    """
    data = {}
    with open(file, 'r') as f:
        header = f.readline().replace('\n', '').split('\t')
        for item in header:
            data[item] = []
        for line in f.readlines():
            line = line.replace('\n', '')
            temp = line.split('\t')
            for i in range(len(temp)):
                data[header[i]].append(temp[i])
    return data, header


def read_input(port):
    """Read a value from a GPIO."""
    import RPi.GPIO as GPIO
    return GPIO.input(port)


@staticmethod
def do_null(parent=None, ident=0):
    """
        Handles a TC_NULL opcode

        :param parent:
        :param ident: Log indentation level
        :return: Always None
        """
    return None


@classmethod
def has_field(cls, field_name):
    """
        Return True if the given field name is an allowed field for this model
        """
    return field_name == 'pk' or field_name in cls._fields


def createExportObj():
    """Creates a dictionary to be used in the hazus.legacy.export method and hazus.legacy.Exporting class

    Returns:
        exportObj: dict -- opt fields are boolean and decide options for exports. The rest of the fields are strings.
    """
    exportObj = {'opt_csv': 1, 'opt_shp': 1, 'opt_report': 1, 'opt_json': 1,
        'study_region': '', 'title': '', 'meta': '', 'output_directory': ''}
    return exportObj


def _squeeze_dimensions(data_array, dimensions=None):
    """Remove dimensions if they are of size 1. The coordinates become scalar coordinates."""
    if dimensions is None:
        dimensions = data_array.dims
    for dim in dimensions:
        if len(data_array[dim]) == 1:
            data_array = data_array.squeeze(dim, drop=False)
    return data_array


def on_mouse_dragged():
    """
    The mouse drag event handler.

    You can redefine this function to handle the mouse drag event.
    """
    pass


def upgrade_scheme(url):
    """
    Upgrade crawled scheme to https, if reasonable. This helps to reduce MITM attacks against the crawler.

    https://chromium.googlesource.com/chromium/src/net/+/master/http/transport_security_state_static.json

    Alternately, the return headers from a site might have strict-transport-security set ... a bit more
    dangerous as we'd have to respect the timeout to avoid permanently learning something that's broken

    TODO: use HTTPSEverwhere? would have to have a fallback if https failed, which it occasionally will
    """
    return url


@staticmethod
def page_fn_footer(canvas, doc):
    """ A footer with the title and print date on the left and the page
        numbers on the right.

        """
    canvas.saveState()
    canvas.setFont('Helvetica', 9)
    canvas.drawString(doc.leftMargin, doc.bottomMargin / 2 + 12, doc.title)
    canvas.drawString(doc.leftMargin, doc.bottomMargin / 2,
        f'Druckdatum: {doc.created}')
    canvas.drawRightString(doc.pagesize[0] - doc.rightMargin, doc.
        bottomMargin / 2, f'{canvas._pageNumber}')
    canvas.restoreState()


def evalcontextfunction(f):
    """This decorator can be used to mark a function or method as an eval
    context callable.  This is similar to the :func:`contextfunction`
    but instead of passing the context, an evaluation context object is
    passed.  For more information about the eval context, see
    :ref:`eval-context`.

    .. versionadded:: 2.4
    """
    f.evalcontextfunction = True
    return f


def _nonzero_avg(items, div=None):
    """Returns the average of a list of items, excluding
    zeros. The optional div argument can be set to a list of
    values to divide each item by when computing the
    average."""
    assert div is None or len(items) == len(div)
    s = 0.0
    c = 0
    for i, val in enumerate(items):
        assert val >= 0
        if val != 0:
            div_i = 1 if div is None else div[i]
            s += val / float(div_i)
            c += 1
    if c == 0:
        return 0
    return s / float(c)


def gpib_send_ifc(library, session):
    """Pulse the interface clear line (IFC) for at least 100 microseconds.

    Corresponds to viGpibSendIFC function of the VISA library.

    :param library: the visa library wrapped by ctypes.
    :param session: Unique logical identifier to a session.
    :return: return value of the library call.
    :rtype: :class:`pyvisa.constants.StatusCode`
    """
    return library.viGpibSendIFC(session)


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 legal_holds_export_cancelled_details(cls, val):
    """
        Create an instance of this class set to the
        ``legal_holds_export_cancelled_details`` tag with value ``val``.

        :param LegalHoldsExportCancelledDetails val:
        :rtype: EventDetails
        """
    return cls('legal_holds_export_cancelled_details', val)


@classmethod
def worldYZ(cls):
    """Construct the world YZ frame.

        Returns
        -------
        :class:`compas.geometry.Frame`
            The world YZ frame.

        Examples
        --------
        >>> frame = Frame.worldYZ()
        >>> frame.point
        Point(0.000, 0.000, 0.000)
        >>> frame.xaxis
        Vector(0.000, 1.000, 0.000)
        >>> frame.yaxis
        Vector(0.000, 0.000, 1.000)
        """
    return cls([0, 0, 0], [0, 1, 0], [0, 0, 1])


@classmethod
def is_datasource_for(cls, data, header, **kwargs):
    """Determines if header corresponds to an MDI image"""
    return header.get('instrume') == 'MDI' or header.get('camera') == 'MDI'


def image_source(*args, **kwargs):
    """Procedural interface for ImageSource"""
    import itk
    instance = itk.ImageSource.New(*args, **kwargs)
    return instance.__internal_call__()


def exact_div(p, d, allow_divzero=False):
    """Find and return an integer n such that p == n * d

    If no such integer exists, this function raises ValueError.

    Both operands must be integers.

    If the second operand is zero, this function will raise ZeroDivisionError
    unless allow_divzero is true (default: False).
    """
    if not isinstance(p, int) or not isinstance(d, int):
        raise TypeError('unsupported operand type(s): %r and %r' % (type(p)
            .__name__, type(d).__name__))
    if d == 0 and allow_divzero:
        n = 0
        if p != n * d:
            raise ValueError('No solution could be found')
    else:
        n, r = divmod(p, d)
        if r != 0:
            raise ValueError('No solution could be found')
    assert p == n * d
    return n


def transcript_pos_transform(tobj, transcript_pos):
    """
	transform the transcript position to genomic position
	0-based
	if strand is "-", return position of strand.
	"""
    length = 0
    strand = tobj.genomic_iv.strand
    genomic_pos = -1
    for i in tobj.genomic_exons:
        if transcript_pos < length + i.length:
            if strand == '+':
                genomic_pos = transcript_pos - length + i.start
            else:
                genomic_pos = i.start_d - (transcript_pos - length)
            break
        length += i.length
    return genomic_pos


def calculate_expected_value(DataFrame, predicted_variable,
    probability_variable, ev_variable='EV'):
    """
    ev = predicted_value * probability

    Parameters
    ----------
    DataFrame:
    predicted_variable:
    probability_variable:

    Returns
    -------
    DataFrame

    """
    DataFrame[ev_variable] = DataFrame[predicted_variable] * DataFrame[
        probability_variable]
    return DataFrame


def select_link_viewer(NODE_OT_select=None, NODE_OT_link_viewer=None):
    """Select node and link it to a viewer node

    :param NODE_OT_select: Select, Select the node under the cursor
    :param NODE_OT_link_viewer: Link to Viewer Node, Link to viewer node
    """
    pass


def pad_line(line):
    """Helper function to pad line to 80 characters in case it is shorter"""
    size_of_line = len(line)
    if size_of_line < 80:
        padding = 80 - size_of_line + 1
        line = line.strip('\n') + ' ' * padding + '\n'
    return line[:81]


def allocate(a_list: list, item):
    """ Too frequent an idiom not to abbreviate. """
    idx = len(a_list)
    a_list.append(item)
    return idx


def p_idlist_id(p):
    """ id_list : id_list COMMA ID
    """
    p[0] = p[1] + ((p[3], p.lineno(3)),)


def chunks(list_, count_items_in_chunk):
    """
    разбить list (l) на куски по n элементов

    :param list_:
    :param count_items_in_chunk:
    :return:
    """
    for i in range(0, len(list_), count_items_in_chunk):
        yield list_[i:i + count_items_in_chunk]


@staticmethod
def getidx(lmax, l, m):
    """Returns index corresponding to (l,m) in an array describing alm up to lmax.

        In HEALPix C++ and healpy, :math:`a_{lm}` coefficients are stored ordered by
        :math:`m`. I.e. if :math:`\\ell_{max}` is 16, the first 16 elements are
        :math:`m=0, \\ell=0-16`, then the following 15 elements are :math:`m=1, \\ell=1-16`,
        then :math:`m=2, \\ell=2-16` and so on until the last element, the 153th, is
        :math:`m=16, \\ell=16`.

        Parameters
        ----------
        lmax : int
          The maximum l, defines the alm layout
        l : int
          The l for which to get the index
        m : int
          The m for which to get the index

        Returns
        -------
        idx : int
          The index corresponding to (l,m)
        """
    return m * (2 * lmax + 1 - m) // 2 + l


def draw_line(labels, pt0, pt1, value=1):
    """Draw a line between two points

    pt0, pt1 are in i,j format which is the reverse of x,y format
    Uses the Bresenham algorithm
    Some code transcribed from http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
    """
    y0, x0 = pt0
    y1, x1 = pt1
    diff_y = abs(y1 - y0)
    diff_x = abs(x1 - x0)
    x = x0
    y = y0
    labels[y, x] = value
    step_x = x1 > x0 and 1 or -1
    step_y = y1 > y0 and 1 or -1
    if diff_y > diff_x:
        remainder = diff_x * 2 - diff_y
        while y != y1:
            if remainder >= 0:
                x += step_x
                remainder -= diff_y * 2
            y += step_y
            remainder += diff_x * 2
            labels[y, x] = value
    else:
        remainder = diff_y * 2 - diff_x
        while x != x1:
            if remainder >= 0:
                y += step_y
                remainder -= diff_x * 2
            x += step_x
            remainder += diff_y * 2
            labels[y, x] = value


def get_setting(settings, name):
    """Get a specific setting from Carbon/Django like settings."""
    res = None
    found = False
    try:
        res = settings[name]
        found = True
    except (TypeError, KeyError):
        try:
            res = getattr(settings, name)
            found = True
        except Exception:
            pass
    return res, found


def trim_text(text, max_len):
    """
    Trim input text to fit the `max_len` condition.

    If trim is needed: keep the first 1/3rd of the budget on the top,
    and the other 2 thirds on the bottom.
    """
    if len(text) <= max_len:
        return text
    trimmed_text = (
        u'{top_third}\n```\n*...trimmed...*\n```\n{bottom_two_third}\n'.
        format(top_third=text[:max_len / 3], bottom_two_third=text[len(text
        ) - 2 * max_len / 3:]))
    return trimmed_text


def pack():
    """Pack the sound into the current blend file

    """
    pass


def newPost(blogid, username, password, struct, publish):
    """Create a new entry - either in draft folder or directly in archive.
        """


@staticmethod
def escape(data):
    """Escape the ',' and '=' characters for 'a=' and 'n=' attributes.

        Replaces '=' with '=3D' and ',' with '=2C'.

        :Parameters:
            - `data`: string to escape
        :Types:
            - `data`: `bytes`
        """
    return data.replace(b'=', b'=3D').replace(b',', b'=2C')


def symbol():
    """
        Return the symbol from the input alphabet to which this input
        corresponds.
        """


def ae_2(provider):
    """Send A_ASSOCIATE-RQ PDU."""
    provider.dul_socket.send(provider.primitive.encode())
    return 'Sta5'


def write_info_inc(f):
    """
    Prototype for info structure
    """
    f.write('// Prototype for info structure\n')
    f.write('extern OSQPInfo info;\n\n')


def reverse_mapping(mapping):
    """
    For every key, value pair, return the mapping for the
    equivalent value, key pair

    >>> reverse_mapping({'a': 'b'}) == {'b': 'a'}
    True
    """
    keys, values = zip(*mapping.items())
    return dict(zip(values, keys))


def enum_eq(enum, value) ->bool:
    """
    Checks equality of `enum` and `value`.

    Parameters
    ----------
    enum : Enum
        The enumerator instance
    value : any
        The value or enumerator instance to be compared.
    """
    return enum == value or enum.value == value


def save_ds9(output, filename):
    """Save ds9 region output info filename.

    Parameters
    ----------
    output : str
        String containing the full output to be exported as a ds9 region
        file.
    filename : str
        Output file name.

    """
    ds9_file = open(filename, 'wt')
    ds9_file.write(output)
    ds9_file.close()


def make_default_short_help(help, max_length=45):
    """Return a condensed version of help string."""
    words = help.split()
    total_length = 0
    result = []
    done = False
    for word in words:
        if word[-1:] == '.':
            done = True
        new_length = result and 1 + len(word) or len(word)
        if total_length + new_length > max_length:
            result.append('...')
            done = True
        else:
            if result:
                result.append(' ')
            result.append(word)
        if done:
            break
        total_length += new_length
    return ''.join(result)


def get_celestrak_sv(tle):
    """
    Place a text file of Celestrak GPS TLEs into a dictionary with
    keys of the form 'PRN xx' for the SV of interest

    :param tle: Path to two line element set file.
    :return: dict
    """
    GPS_sv_dict = {}
    with open(tle, 'rt') as f:
        for line in f:
            if line[0:3] == 'GPS':
                PRN = line[line.find('PRN'):line.find(')')]
            elif line[0] == '1':
                tle_ln1 = line.strip('\n')
            elif line[0] == '2':
                tle_ln2 = line.strip('\n')
                GPS_sv_dict.update({PRN: (tle_ln1, tle_ln2)})
            else:
                UserWarning('File structure incorrect')
                break
    return GPS_sv_dict


def psi_Ca_HCO3_SO4_HMW84(T, P):
    """c-a-a': calcium bicarbonate sulfate [HMW84]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


def __contains__(item):
    """`x.__contains__(item)` <==> `item in x`"""


def subgraph_exists(tx, subgraph):
    """ Determine whether one or more graph entities all exist within the
    database. Note that if any nodes or relationships in *subgraph* are not
    bound to remote counterparts, this method will return ``False``.

    :param tx:
    :param subgraph: a :class:`.Node`, :class:`.Relationship` or other
                   :class:`.Subgraph`
    :returns: ``True`` if all entities exist remotely, ``False`` otherwise
    """
    graph = tx.graph
    node_ids = set()
    relationship_ids = set()
    for i, node in enumerate(subgraph.nodes):
        if node.graph is graph:
            node_ids.add(node.identity)
        else:
            return False
    for i, relationship in enumerate(subgraph.relationships):
        if relationship.graph is graph:
            relationship_ids.add(relationship.identity)
        else:
            return False
    statement = (
        'OPTIONAL MATCH (a) WHERE id(a) IN {x} OPTIONAL MATCH ()-[r]->() WHERE id(r) IN {y} RETURN count(DISTINCT a) + count(DISTINCT r)'
        )
    parameters = {'x': list(node_ids), 'y': list(relationship_ids)}
    return tx.evaluate(statement, parameters) == len(node_ids) + len(
        relationship_ids)


def _2xpre(s, styles):
    """Helper to transform Pygments HTML output to ReportLab markup"""
    s = s.replace('
', '')
    s = s.replace('
', '')
    s = s.replace('
', '')
    s = s.replace('
', '')
    for k, c in (styles + [('p', '#000000'), ('n', '#000000'), ('err',
        '#000000')]):
        s = s.replace('' % k, '' % c)
    return s


def add_intf_to_map(intf_map, bridge_or_bond, config_type, intf_index, lacp
    =False):
    """Adds an interface to the interface map, after performing validation

    interface_map has a specific structure. this method checks and inserts
    keys if they're missing for the bridge or interface being added.

    :param intf_map: interface map object to which the interface is added
    :param bridge_or_bond: bridge or bond name to which the interface belongs
    :param config_type: type of object - either bridge or bond
    :param intf_index: name or index number of the interface
                       if interface is in nicX format, this will be a
                       numerical index, else name. both would be string.
    :param lacp: boolean flag, specifying whether intf is part of some form of
                 link aggregation or no.
    :return: intf_map after being updated
    """
    if bridge_or_bond not in intf_map:
        intf_map[bridge_or_bond] = {}
    if 'config_type' not in intf_map[bridge_or_bond]:
        intf_map[bridge_or_bond]['config_type'] = config_type
    if 'members' not in intf_map[bridge_or_bond]:
        intf_map[bridge_or_bond]['members'] = []
    if config_type == 'linux_bond' or lacp:
        intf_map[bridge_or_bond]['lacp'] = True
    else:
        intf_map[bridge_or_bond]['lacp'] = False
    intf_map[bridge_or_bond]['members'].append(intf_index)
    return intf_map


def has_vertical(sources: list) ->bool:
    """
    Checks if the list of sources has a vertical interpolation anywhere
    :param sources:
    :return:
    """
    for group in sources:
        for variable in group['variables']:
            if variable.get('vertical', False):
                return True
    return False


def _error(text):
    """Colorize terminal output to bold red."""
    return '\x1b[1m\x1b[31m{text}\x1b[0m'.format(text=text)


@classmethod
def run(cls, excl_fpath, tm_dset, sc_point_method, excl_dict=None,
    area_filter_kernel='queen', min_area=None, check_excl_layers=False,
    resolution=64, gids=None, args=None, kwargs=None, max_workers=None,
    chunk_point_len=1000):
    """Get the supply curve points aggregation summary.

        Parameters
        ----------
        excl_fpath : str
            Filepath to exclusions h5 with techmap dataset.
        tm_dset : str
            Dataset name in the techmap file containing the
            exclusions-to-resource mapping data.
        sc_point_method : method
            Supply Curve Point Method to operate on a single SC point.
        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.
        sc_point_method : method
            Supply Curve Point Method to operate on a single SC point.
        args : list | None
            List of positional args for sc_point_method
        kwargs : dict | None
            Dict of kwargs for sc_point_method
        max_workers : int | None
            Number of cores to run summary on. None is all
            available cpus.
        chunk_point_len : int
            Number of SC points to process on a single parallel worker.

        Returns
        -------
        summary : DataFrame
            Summary of the SC points.
        """
    agg = cls(excl_fpath, tm_dset, excl_dict=excl_dict, area_filter_kernel=
        area_filter_kernel, min_area=min_area, check_excl_layers=
        check_excl_layers, resolution=resolution, gids=gids)
    aggregation = agg.aggregate(sc_point_method, args=args, kwargs=kwargs,
        max_workers=max_workers, chunk_point_len=chunk_point_len)
    return aggregation


def lcstr(astr):
    """lower-case string"""
    return astr.lower()


def check_time(stat, current, deadline):
    """ Check if the submit time of the assignment (last commit time) is before
        the deadline
    """
    if not deadline or current <= deadline:
        return True
    stat['late'] += 1
    return False


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


def value_or_empty(value):
    """Return an empty string for display when value is ``None``.

    :param str value: Value to prepare for display
    :rtype: ``str``
    :return: String representation of ``value``

    """
    return value if value else ''


def dpowint(xlow, xhigh, ylow, yhigh, xval, pwr):
    """Power interpolate the value of `y` between (`xlow`, `ylow`) and
    (`xhigh`, `yhigh`) for a value of `x` and a power `pwr`.
    :param xlow: TODO
    :param xhigh: TODO
    :param ylow: TODO
    :param yhigh: TODO
    :param xval: TODO
    :param pwr: power
    :return: TODO
    """
    EPSLON = 1e-20
    if xhigh - xlow < EPSLON:
        dpowint_ = (yhigh + ylow) / 2.0
    else:
        dpowint_ = ylow + (yhigh - ylow) * ((xval - xlow) / (xhigh - xlow)
            ) ** pwr
    return dpowint_


def currentwlock():
    """Return the wlock if it's held or None."""


def lsum(inlist):
    """
Returns the sum of the items in the passed list.

Usage:   lsum(inlist)
"""
    s = 0
    for item in inlist:
        s = s + item
    return s


def concatenate(input_files, output_file):
    """
    Concatenates the input files into the single output file.

    In debug mode this function adds a comment with the filename
    before the contents of each file.
    """
    from .modules import utils, concat
    if not isinstance(input_files, (list, tuple)):
        raise RuntimeError('Concatenate takes a list of input files.')
    return {'dependencies_fn': utils.no_dependencies, 'compiler_fn': concat
        .concatenate_input_files, 'input': input_files, 'output':
        output_file, 'kwargs': {}}


def _expand_dim_specification(image_shape, dim_spec):
    """Expand mex dimension specification.

    The dimension specification can be 2 or 3 long, it is
    processed in two steps:
    1. If it is of length 2, a -1 is prepended to it
    2. Each dimension with -1 is replaced with the whole corresponding image dimension

    Args:
        image_shape : list(int)
            the shape of the input image, of length 3 (without batch) or 4 (with bach)
        dim_spec : list(int)
            the specification to be expanded

    Returns:
        The expanded dimension specification
    """
    if len(dim_spec) != 2 and len(dim_spec) != 3:
        raise ValueError(
            'Bad dimensions specifications, should be a list of two or three, got %s'
             % dim_spec)
    if len(image_shape) == 3:
        image_shape = [None] + image_shape
    if len(dim_spec) == 2:
        dim_spec = [-1] + list(dim_spec)
    dim_spec = dim_spec[:]
    for i in range(3):
        if dim_spec[i] == -1:
            dim_spec[i] = image_shape[i + 1]
    return dim_spec


def __calculate_layover_time(arrival_time=None, departure_time=None):
    """
    This function calculates layover time between flights

    :return: (str) Layover time in hour and minutes
    """
    dtime = departure_time
    atime = arrival_time
    flight_time_sec = dtime - atime
    minutes, seconds = divmod(flight_time_sec, 60)
    hours, minutes = divmod(minutes, 60)
    total_flight_time = str(hours) + 'h' + ' ' + str(minutes) + 'm'
    return total_flight_time


def code_is_executable(estimator_code):
    """Determine if code string is executable.

    :param str estimator_code: string to check.
    :returns bool: True if executable
    """
    try:
        eval(estimator_code)
        return True
    except:
        return False


def remove_cookie_by_name(cookiejar, name, domain=None, path=None):
    """Unsets a cookie by name, by default over all domains and paths.

    Wraps CookieJar.clear(), is O(n).
    """
    clearables = []
    for cookie in cookiejar:
        if cookie.name == name:
            if domain is None or domain == cookie.domain:
                if path is None or path == cookie.path:
                    clearables.append((cookie.domain, cookie.path, cookie.name)
                        )
    for domain, path, name in clearables:
        cookiejar.clear(domain, path, name)


def save_submissions(problem, y_pred, data_path='.', output_path='.',
    suffix='test'):
    """Custom save a prediction vector in file to, e.g., submit to Kaggle.

    If problem.save_submission is implemented, y_pred is passed to it.
    Otherwise nothing happens (the exception is caught silently).

    Parameters
    ----------
    problem : a problem object
        loaded from problem.py, may implement save_y_pred
    y_pred : a prediction vector
        a vector of predictions to be saved
    data_path : str, (default='.')
        the directory of the ramp-kit to be tested for submission, maybe
        needed by problem.save_y_pred for, e.g., merging with an index vector
    output_path : str, (default='.')
        the directory where (typically) y_pred_<suffix>.csv will be saved
    suffix : str, (default='test')
        suffix in (typically) y_pred_<suffix>.csv, can be used in
        problem.save_y_pred to, e.g., save only test predictions
    """
    try:
        problem.save_submission(y_pred, data_path, output_path, suffix)
    except AttributeError:
        pass


def find_token(element, token):
    """ Find a token in the xml file.
    :param element: Search for tokens in this Elementtree element.
    :param token: Token byte to search for
    :type element: Element
    :type token: int
    :return: Child element which matches the token byte.
    :rtype: Element"""
    children = element.findall('{http://merthsoft.com/Tokens}Token')
    if children:
        for child in children:
            byte = int(child.get('byte')[1:], base=16)
            if byte == token:
                return child
    return None


def get_current_categories(request, object):
    """Returns all current categories based on given request. Current
    categories are the current selected category and all parent categories of
    it.
    """
    if object and object.content_type == 'category':
        parents = object.get_parents()
        current_categories = [object]
        current_categories.extend(parents)
    elif object and object.content_type == 'product':
        current_categories = []
        category = object.get_current_category(request)
        while category:
            current_categories.append(category)
            category = category.parent
    else:
        current_categories = []
    return current_categories


@staticmethod
def combine_kw_list(lst, n=100):
    """Combine long kw_list into "+" separated chunks"""
    for ii, jj in enumerate(lst):
        if ii == 0:
            temp = jj
            master = []
        elif len(temp + ' + ' + jj) < n:
            temp += ' + ' + jj
        else:
            master.append(temp)
            temp = jj
    return master


def findRandomLabel(labels, name):
    """
    Because some people are too clever by half, ensure that group labels are unique...
    """
    if name not in labels:
        return name
    i = 0
    while True:
        i += 1
        nameTry = name + '_r' + str(i)
        if nameTry not in labels:
            return nameTry


def restart_program():
    """
    初始化避免异步导致log重复打印
    :return:
    """
    import sys
    import os
    python = sys.executable
    os.execl(python, python, *sys.argv)


def gen_case(test):
    """Generate an ok test case for a test."""
    code_lines = []
    for line in test.input.split('\n'):
        if line.startswith(' '):
            code_lines.append('... ' + line)
        else:
            code_lines.append('>>> ' + line)
    for i in range(len(code_lines) - 1):
        if code_lines[i + 1].startswith('>>>') and len(code_lines[i].strip()
            ) > 3:
            code_lines[i] += ';'
    code_lines.append(test.output)
    return {'code': '\n'.join(code_lines), 'hidden': test.hidden, 'locked':
        False}


def SaveAlgoData(*args, **kwargs):
    """ Stub Anduin function."""
    print('SaveAlgoData', args, kwargs)


@classmethod
def axesNames(cls):
    """ The names of the axes that this inspector visualizes.
            See the parent class documentation for a more detailed explanation.
        """
    return tuple(['Y', 'X'])


@staticmethod
def get_version_name():
    """
        Returns version of the DCC
        :return: str
        """
    return '0.0.0'


def hasBothOrNeitherAngleBrackets(string):
    """Make sure a string either has both '<' and '>' or neither of those angle brackets."""
    left_angle_bracket_in_string = '<' in string
    right_angle_bracket_in_string = '>' in string
    if left_angle_bracket_in_string and right_angle_bracket_in_string:
        return True
    elif left_angle_bracket_in_string or right_angle_bracket_in_string:
        return False
    else:
        return True


def ungzipper(fh, blocksize=16384):
    """
    work-around to get streaming download of http://.../some.gz
    """
    import zlib
    uzip = zlib.decompressobj(16 + zlib.MAX_WBITS)
    data = uzip.decompress(fh.read(blocksize)).split('\n')
    while len(data[0]):
        save = data.pop()
        for line in data:
            yield line
        data = uzip.decompress(fh.read(blocksize)).split('\n')
        data[0] = save + data[0]


def display_text(results):
    """Display search results in raw TEXT format"""
    for result in results:
        print(result)


def extract_transitions_from_app(app):
    """Return all possible actions for configured transitions."""
    transitions_config = app.config.get('CIRCULATION_LOAN_TRANSITIONS', {})
    distinct_actions = set()
    for src_state, transitions in transitions_config.items():
        for t in transitions:
            distinct_actions.add(t.get('trigger', 'next'))
    return distinct_actions


def coerce_tuple(m):
    """
    Coerce the argument to tuple.
    """
    if isinstance(m, str):
        m = m,
    else:
        try:
            iter(m)
        except TypeError:
            m = m,
        else:
            m = tuple(m)
    return m


def delete_build_project(client):
    """
    Deletes the alppb AWS CodeBuild project.

    Parameters
    ----------
    client : botocore.client.codebuild
        A boto3 client for CodeBuild.

    Returns
    -------
    """
    print('Deleting CodeBuild project...')
    client.delete_project(name='alppbBuilder')


def passthrough_challenge_decider(environ, status, headers):
    """ Don't challenge for pre-challenged responses.

    o Assume responsese with 'WWW-Authenticate' or an HTML content type
      are pre-challenged.
    """
    if not status.startswith('401 '):
        return False
    h_dict = dict(headers)
    if 'WWW-Authenticate' in h_dict:
        return False
    ct = h_dict.get('Content-Type')
    if ct is not None:
        return not ct.startswith('text/html')
    return True


@classmethod
def write_xsd(cls) ->None:
    """Write the complete schema file based on the template file
        `config.xsdt`, including the input, flux, and state sequences
        of all application models available at the moment.

        The following example shows that after writing a new schema file,
        method |XMLInterface.validate_xml| does not raise an error when
        applied on the XML configuration file of the `LahnH` example
        project:

        >>> from hydpy.auxs.xmltools import XSDWriter, XMLInterface
        >>> from hydpy import data
        >>> xmlpath = data.get_path('LahnH', 'config.xml')

        >>> import os
        >>> if os.path.exists(XSDWriter.filepath_target):
        ...     os.remove(XSDWriter.filepath_target)

        >>> XMLInterface(xmlpath).validate_xml()    # doctest: +ELLIPSIS
        Traceback (most recent call last):
        ...
        lxml.etree.XMLSchemaParseError: Failed to locate the main schema resource at '...config.xsd'.

        >>> XSDWriter.write_xsd()
        >>> XMLInterface(xmlpath).validate_xml()
        """
    with open(cls.filepath_source) as file_:
        template = file_.read()
    template = template.replace('<!--include model sequence groups-->', cls
        .get_insertion())
    with open(cls.filepath_target, 'w') as file_:
        file_.write(template)


@classmethod
def parse_map_supported_features(cls, value: int):
    """The value parameter is uint32."""
    print('    RFU (31-23)', value >> 23)
    print('    Bit 22 = Message Forwarding', value >> 22 & 1)
    print('    Bit 21 = Owner Status', value >> 21 & 1)
    print('    Bit 20 = Conversationlisting', value >> 20 & 1)
    print('    Bit 19 = MapSupportedFeatures in Connect Request', value >>
        19 & 1)
    print('    Bit 18 = UTC Offset Timestamp Format', value >> 18 & 1)
    print('    Bit 17 = Notification Filtering', value >> 17 & 1)
    print('    Bit 16 = PBAP Contact Cross Reference', value >> 16 & 1)
    print('    Bit 15 = Participant Chat State Change Notification', value >>
        15 & 1)
    print('    Bit 14 = Participant Presence Change Notification', value >>
        14 & 1)
    print('    Bit 13 = Conversation Version Counters', value >> 13 & 1)
    print('    Bit 12 = Folder Version Counter', value >> 12 & 1)
    print('    Bit 11 = Database Identifier', value >> 11 & 1)
    print('    Bit 10 = Persistent Message Handles', value >> 10 & 1)
    print('    Bit 9 = MessagesListing Format Version 1.1', value >> 9 & 1)
    print('    Bit 8 = Message Format Version 1.1', value >> 8 & 1)
    print('    Bit 7 = Event Report Version 1.2', value >> 7 & 1)
    print('    Bit 6 = Extended Event Report 1.1', value >> 6 & 1)
    print('    Bit 5 = Instance Information Feature', value >> 5 & 1)
    print('    Bit 4 = Delete Feature', value >> 4 & 1)
    print('    Bit 3 = Uploading Feature', value >> 3 & 1)
    print('    Bit 2 = Browsing Feature', value >> 2 & 1)
    print('    Bit 1 = Notification Feature', value >> 1 & 1)
    print('    Bit 0 = Notification Registration Feature', value & 1)


@staticmethod
def all_events(events, count):
    """Use as ``evaluate`` to test that all events have triggered"""
    return len(events) == count


def parse_config_select_maps(config, sections):
    """Retrieve the section of the maps to project from a configuration file."""
    good_sections = []
    for key in sections:
        if not config.has_option(key, 'docut') or config.getboolean(key,
            'docut'):
            good_sections.append(key)
    return good_sections


def delete_receipt_filter(FilterName=None):
    """
    Deletes the specified IP address filter.
    For information about managing IP address filters, see the Amazon SES Developer Guide .
    This action is throttled at one request per second.
    See also: AWS API Documentation

    Examples
    The following example deletes an IP address filter:
    Expected Output:

    :example: response = client.delete_receipt_filter(
        FilterName='string'
    )

    :type FilterName: string
    :param FilterName: [REQUIRED]
            The name of the IP address filter to delete.

    :rtype: dict
    :return: {}

    """
    pass


def list_faces(CollectionId=None, NextToken=None, MaxResults=None):
    """
    Returns metadata for faces in the specified collection. This metadata includes information such as the bounding box coordinates, the confidence (that the bounding box contains a face), and face ID. For an example, see  example3 .
    This operation requires permissions to perform the rekognition:ListFaces action.
    See also: AWS API Documentation

    Examples
    This operation lists the faces in a Rekognition collection.
    Expected Output:

    :example: response = client.list_faces(
        CollectionId='string',
        NextToken='string',
        MaxResults=123
    )

    :type CollectionId: string
    :param CollectionId: [REQUIRED]
            ID of the collection from which to list the faces.

    :type NextToken: string
    :param NextToken: If the previous response was incomplete (because there is more data to retrieve), Amazon Rekognition returns a pagination token in the response. You can use this pagination token to retrieve the next set of faces.

    :type MaxResults: integer
    :param MaxResults: Maximum number of faces to return.

    :rtype: dict
    :return: {
        'Faces': [
            {
                'FaceId': 'string',
                'BoundingBox': {
                    'Width': ...,
                    'Height': ...,
                    'Left': ...,
                    'Top': ...
                },
                'ImageId': 'string',
                'ExternalImageId': 'string',
                'Confidence': ...
            },
        ],
        'NextToken': 'string'
    }

    """
    pass


def touch(filepath):
    """
    Creates an empty file at filepath, if it does not already exist
    """
    with open(filepath, 'a'):
        pass


def split_command_from_args(text: (str or None)) ->(str or None, str or None):
    """
    Splits the command (including any target) from its arguments
    :param text: the full message
    :return: (command, args)
    """
    if text is None or len(text) <= 0:
        return None, None
    if ' ' in text:
        return text.split(' ', 1)
    else:
        return text, None


@classmethod
def from_json(cls, s):
    """ return a DimArray, from a json string

        >>> s = '{"values":[[1.0,2.0,3.0],[4.0,5.0,6.0]],"dims":["a","b"],"labels":[[0,1],[10,11,12]],"shape":[2,3],"ndim":2,"meta":{}}'
        >>> DimArray.from_json(s)
        dimarray: 6 non-null elements (0 null)
        0 / a (2): 0 to 1
        1 / b (3): 10 to 12
        array([[1., 2., 3.],
               [4., 5., 6.]])
        """
    import json
    jsondict = json.loads(s)
    return cls.from_jsondict(jsondict)


def sasimport(modname):
    """Import specified module from the SasView sas package.

    modname  -- absolute module name contained in the sas package.

    When specified import does not work directly, try older API-s and raise
    DeprecationWarning.  Raise ImportError if nothing works.
    """
    if not modname.startswith('sas.'):
        emsg = 'Module name must start with "sas."'
        raise ValueError(emsg)
    mobj = None
    try:
        exec('import %s as mobj' % modname)
    except ImportError:
        pass
    else:
        return mobj
    modsans = 'sans' + modname[3:]
    import warnings
    wfmt = (
        'Using obsolete package %r instead of %r.  Please install SasView 3.1 or the srfit-sasview package from Anaconda.'
        )
    wmsg = wfmt % (modsans, modname)
    try:
        exec('import %s as mobj' % modsans)
        warnings.warn(wmsg, DeprecationWarning)
    except ImportError:
        pass
    else:
        return mobj
    if modname.startswith('sas.dataloader'):
        modloader = 'DataLoader' + modname[14:]
        wmsg = wfmt % (modloader, modname)
        try:
            exec('import %s as mobj' % modloader)
            warnings.warn(wmsg, DeprecationWarning)
        except ImportError:
            pass
        else:
            return mobj
    exec('import %s as mobj' % modname)
    raise AssertionError('The import above was supposed to fail.')


def tracks_delete():
    """Delete selected NLA-Tracks and the strips they contain

    """
    pass


@staticmethod
def max_or(a, b, c, d, w):
    """
        Upper bound of result of ORing 2-intervals.

        :param a: Lower bound of first interval
        :param b: Upper bound of first interval
        :param c: Lower bound of second interval
        :param d: Upper bound of second interval
        :param w: bit width
        :return: Upper bound of ORing 2-intervals
        """
    m = 1 << w - 1
    while m != 0:
        if b & d & m != 0:
            temp = b - m | m - 1
            if temp >= a:
                b = temp
                break
            temp = d - m | m - 1
            if temp >= c:
                d = temp
                break
        m >>= 1
    return b | d


def _add_tagged_relationship(t_x, tag_search, tag_other_end):
    """
    This function all create or update the directional tagged relationship between the twitter
    account being search to the tag found in searching the twitter account.
    :param t_x: Neo4j driver transaction.
    :param tag_search: Twitter tag of who is being searched.
    :param tag_other_end: Twitter tag of who has been found.
    :return:
    """
    tag_search = tag_search.lower()
    tag_other_end = tag_other_end.lower()
    t_x.run('MATCH (n:Person) WHERE n.twitter_tag={tag_search} ' +
        'MATCH (m:Person) WHERE m.twitter_tag={tag_other_end} ' +
        'MERGE (n)-[t:Tagged]->(m) ' +
        'ON CREATE SET t.times_mentioned = 1 ' +
        'ON MATCH SET t.times_mentioned = t.times_mentioned + 1',
        tag_search=tag_search, tag_other_end=tag_other_end)


def _group_by_count_distinct_chunk(s):
    """The function applied to the individual partition (map)
    """
    return s.apply(lambda x: list(set(x)))


@staticmethod
def intersect(A, B, P, Q):
    """
         判断两条线段AB和PQ是否相交（接触默认为不是相交）
         :param A: dict, 线段AB的起点
         :param B: dict, 线段AB的终点
         :param P: dict, 目标点, 要判断其是否在网格中, PQ的起点
         :param Q: dict, 以P为起点, 水平作一条线段到网格边界(最大横坐标处), 终点为Q
         :return: 返回布尔值是或否
        """
    if A['y'] > P['y'] and B['y'] > P['y'] or A['x'] < P['x'] and B['x'] < P[
        'x'] or A['y'] < P['y'] and B['y'] < P['y']:
        return False
    x = (P['y'] - A['y']) * (B['x'] - A['x']) / (B['y'] - A['y']) + A['x']
    if P['x'] < x <= Q['x']:
        print('有交点, 且交点在PQ之间', A, B)
        return True
    else:
        return False


def _uuid_representer(dumper, data):
    """Add UUID serializer for YAML."""
    return dumper.represent_str(str(data))


def iter_fields(hdr, onlyvalues=False, fields=None):
    """ splits header into lines
        if onlyvalues does not return field names
        if fields returns only specified fields (forces onlyvalues)"""
    if fields:
        for key in hdr:
            if key in fields:
                yield hdr[key]
    elif onlyvalues:
        for val in hdr.values():
            if val:
                yield str(val)
    else:
        for line in repr(hdr).strip().splitlines():
            yield line


def listenSSL(port, factory, contextFactory, backlog=50, interface=''):
    """
        Connects a given protocol factory to the given numeric TCP/IP port.
        The connection is a SSL one, using contexts created by the context
        factory.

        @param port: a port number on which to listen

        @param factory: a L{twisted.internet.protocol.ServerFactory} instance

        @param contextFactory: a L{twisted.internet.ssl.ContextFactory} instance

        @param backlog: size of the listen queue

        @param interface: the hostname to bind to, defaults to '' (all)
        """


def __eq__(other):
    """
        Determines if two requests are the same object.

        @param other: Another object whose identity will be compared
            to this instance's.

        @return: L{True} when the two are the same object and L{False}
            when not.
        @rtype: L{bool}
        """


def soften_datetime(dt):
    """
    Zeros the microseconds on a datetime so that it resolution is at seconds,
    which is required to store the data.
    """
    return dt.replace(microsecond=0)


def update_api_key(apiKey=None, patchOperations=None):
    """
    Changes information about an  ApiKey resource.
    See also: AWS API Documentation

    :example: response = client.update_api_key(
        apiKey='string',
        patchOperations=[
            {
                'op': 'add'|'remove'|'replace'|'move'|'copy'|'test',
                'path': 'string',
                'value': 'string',
                'from': 'string'
            },
        ]
    )

    :type apiKey: string
    :param apiKey: [REQUIRED]
            The identifier of the ApiKey resource to be updated.

    :type patchOperations: list
    :param patchOperations: A list of update operations to be applied to the specified resource and in the order specified in this list.
            (dict) -- A single patch operation to apply to the specified resource. Please refer to http://tools.ietf.org/html/rfc6902#section-4 for an explanation of how each operation is used.
            op (string) --An update operation to be performed with this PATCH request. The valid value can be 'add', 'remove', or 'replace'. Not all valid operations are supported for a given resource. Support of the operations depends on specific operational contexts. Attempts to apply an unsupported operation on a resource will return an error message.
            path (string) --The op operation's target, as identified by a JSON Pointer value that references a location within the targeted resource. For example, if the target resource has an updateable property of {'name':'value'} , the path for this property is /name . If the name property value is a JSON object (e.g., {'name': {'child/name': 'child-value'}} ), the path for the child/name property will be /name/child~1name . Any slash ('/') character appearing in path names must be escaped with '~1', as shown in the example above. Each op operation can have only one path associated with it.
            value (string) --The new target value of the update operation. When using AWS CLI to update a property of a JSON value, enclose the JSON object with a pair of single quotes in a Linux shell, e.g., '{'a': ...}'. In a Windows shell, see Using JSON for Parameters .
            from (string) --Not supported.

    :rtype: dict
    :return: {
        'id': 'string',
        'value': 'string',
        'name': 'string',
        'customerId': 'string',
        'description': 'string',
        'enabled': True|False,
        'createdDate': datetime(2015, 1, 1),
        'lastUpdatedDate': datetime(2015, 1, 1),
        'stageKeys': [
            'string',
        ]
    }

    :returns:
    (string) --

    """
    pass


def sub(x, y):
    """
    Subtract two matrices, at least one of which is sparse.

    This method will provide the right op according
    to the inputs.

    Parameters
    ----------
    x
        A matrix variable.
    y
        A matrix variable.

    Returns
    -------
    A sparse matrix
        `x` - `y`

    Notes
    -----
    At least one of `x` and `y` must be a sparse matrix.

    The grad will be structured only when one of the variable will be a dense
    matrix.

    """
    return x + -y


def extract_json_weights(strModelFile: str, strPath: str='./'):
    """
    From a keras .h5 model file, generate a json file and a numpy weight file

    :param strModelFile: str Model file name.
    :param strPath: str Path to save the model json file and weight files. The json file is saved to `strPath/models/` and weights to `strPath/weights/`
    """
    import tensorflow as tf
    import os
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    session = tf.Session(config=config)
    from tensorflow import keras
    import numpy as np
    keras_model = keras.models.load_model(strModelFile)
    model_name = strModelFile[:-3].split('/')[-1]
    if not os.path.isdir(strPath + '/models/'):
        os.makedirs(strPath + '/models/')
    with open(strPath + '/models/' + model_name + '.json', 'w') as jsonFile:
        jsonFile.write(keras_model.to_json())
    if not os.path.isdir(strPath + '/weights/'):
        os.makedirs(strPath + '/weights/')
    model_weights = keras_model.get_weights()
    np.savez(strPath + '/weights/' + model_name, *model_weights)


def dataframe_difference(df1, df2, which=None):
    """Find rows which are different between two DataFrames."""
    comparison_df = df1.merge(df2, indicator=True, how='outer')
    if which is None:
        diff_df = comparison_df[comparison_df['_merge'] != 'both']
    else:
        diff_df = comparison_df[comparison_df['_merge'] == which]
    return diff_df


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 delete_hsm_configuration(HsmConfigurationIdentifier=None):
    """
    Deletes the specified Amazon Redshift HSM configuration.
    See also: AWS API Documentation

    :example: response = client.delete_hsm_configuration(
        HsmConfigurationIdentifier='string'
    )

    :type HsmConfigurationIdentifier: string
    :param HsmConfigurationIdentifier: [REQUIRED]
            The identifier of the Amazon Redshift HSM configuration to be deleted.

    """
    pass


@classmethod
def compatible_aspect_ratio(cls, size):
    """
        Helper method to check aspect ratio for standard uploads

        :param size: tuple of (width, height)
        :return: True/False
        """
    min_ratio, max_ratio = 4.0 / 5.0, 90.0 / 47.0
    width, height = size
    this_ratio = 1.0 * width / height
    return True


def physical_separation(d, theta):
    """
    Returns separation between two objects in the plane of the sky in AU given distance and parallax
    Distance and parallax must be astropy unit objects.
    Args:
        d (float): distance
        theta (float): parallax
    Return:
        separation in AU
    Written by: Logan Pearce, 2017
    """
    from astropy import units as u
    d = d.to(u.pc)
    theta = theta.to(u.arcsec)
    a = d * theta
    return a.to(u.au, equivalencies=u.dimensionless_angles())