def nms(boxes, scores, **kwargs):
    """Do Non Maximal Suppression

    As translated from the OpenCV c++ source in
    `nms.inl.hpp <https://github.com/opencv/opencv/blob/ee1e1ce377aa61ddea47a6c2114f99951153bb4f/modules/dnn/src/nms.inl.hpp#L67>`__
    which was in turn inspired by `Piotr Dollar's NMS implementation in EdgeBox. <https://goo.gl/jV3JYS>`_

    This function is not usually called directly.  Instead use :func:`nms.nms.boxes`, :func:`nms.nms.rboxes`,
    or :func:`nms.nms.polygons`

    :param boxes:  the boxes to compare, the structure of the boxes must be compatible with the compare_function.
    :type boxes:  list
    :param scores: the scores associated with boxes
    :type scores: list
    :param kwargs: optional keyword parameters
    :type kwargs: dict (see below)
    :returns: an list of indicies of the best boxes
    :rtype: list
    :kwargs:

    * score_threshold (float): the minimum score necessary to be a viable solution, default 0.3
    * nms_threshold (float): the minimum nms value to be a viable solution, default: 0.4
    * compare_function (function): function that accepts two boxes and returns their overlap ratio, this function must
      accept two boxes and return an overlap ratio
    * eta (float): a coefficient in adaptive threshold formula: \\ |nmsi1|\\ =eta\\*\\ |nmsi0|\\ , default: 1.0
    * top_k (int): if >0, keep at most top_k picked indices. default:0

    .. |nmsi0| replace:: nms_threshold\\ :sub:`i`
    .. |nmsi1| replace:: nms_threshold\\ :sub:`(i+1)`

    """
    if 'eta' in kwargs:
        eta = kwargs['eta']
    else:
        eta = 1.0
    assert 0 < eta <= 1.0
    if 'top_k' in kwargs:
        top_k = kwargs['top_k']
    else:
        top_k = 0
    assert 0 <= top_k
    if 'score_threshold' in kwargs:
        score_threshold = kwargs['score_threshold']
    else:
        score_threshold = 0.3
    assert score_threshold > 0
    if 'nms_threshold' in kwargs:
        nms_threshold = kwargs['nms_threshold']
    else:
        nms_threshold = 0.4
    assert 0 < nms_threshold < 1
    if 'compare_function' in kwargs:
        compare_function = kwargs['compare_function']
    else:
        compare_function = None
    assert compare_function is not None
    if len(boxes) == 0:
        return []
    assert len(scores) == len(boxes)
    assert scores is not None
    scores = help.get_max_score_index(scores, score_threshold, top_k)
    adaptive_threshold = nms_threshold
    indicies = []
    for i in range(0, len(scores)):
        idx = int(scores[i][1])
        keep = True
        for k in range(0, len(indicies)):
            if not keep:
                break
            kept_idx = indicies[k]
            overlap = compare_function(boxes[idx], boxes[kept_idx])
            keep = overlap <= adaptive_threshold
        if keep:
            indicies.append(idx)
        if keep and eta < 1 and adaptive_threshold > 0.5:
            adaptive_threshold = adaptive_threshold * eta
    return indicies


def style_function(datum):
    """
    This example shows how to define a function to specify style values for
    individual data points.

    :param datum:
        A :class:`.Datum` instance for the data row.
    """
    pass


def getcwd():
    """Obtain the current working directory from the dirstate."""


def env_purge_doc(app, env, docname):
    """Remove list of local files for a given document."""
    try:
        del env.nbsphinx_notebooks[docname]
    except KeyError:
        pass
    try:
        del env.nbsphinx_files[docname]
    except KeyError:
        pass
    try:
        del env.nbsphinx_thumbnails[docname]
    except KeyError:
        pass
    env.nbsphinx_widgets.discard(docname)


def provider_parser(subparser):
    """Configure provider parser for Henet."""
    subparser.description = """A provider for Hurricane Electric DNS.
        NOTE: THIS DOES NOT WORK WITH 2-FACTOR AUTHENTICATION.
              YOU MUST DISABLE IT IF YOU'D LIKE TO USE THIS PROVIDER.
        """
    subparser.add_argument('--auth-username', help=
        'specify username for authentication')
    subparser.add_argument('--auth-password', help=
        'specify password for authentication')


def check_iterations(n_iter):
    """Check n_iter input"""
    if not isinstance(n_iter, int):
        raise TypeError('``n_iter`` must be of type ``int``')
    elif n_iter < 1:
        raise ValueError('``n_iter`` must be greater than 0')
    return n_iter


def generate_presigned_url(ClientMethod=None, Params=None, ExpiresIn=None,
    HttpMethod=None):
    """
    Generate a presigned url given a client, its method, and arguments

    :type ClientMethod: string
    :param ClientMethod: The client method to presign for

    :type Params: dict
    :param Params: The parameters normally passed to
            ClientMethod.

    :type ExpiresIn: int
    :param ExpiresIn: The number of seconds the presigned url is valid
            for. By default it expires in an hour (3600 seconds)

    :type HttpMethod: string
    :param HttpMethod: The http method to use on the generated url. By
            default, the http method is whatever is used in the method's model.

    """
    pass


def lock_all():
    """Lock all Grease Pencil layers to prevent them from being accidentally modified

    """
    pass


def p_global_param_value(p):
    """ global_param_value : global_param_str str_value
                           | global_param_str IPADDR
                           | global_param_flag global_param_flag_value
                           | DB_TIME_FORMAT DEFAULT
                           | DB_TIME_FORMAT LOCAL
                           | DDNS_UPDATE_STYLE AD_HOC
                           | DDNS_UPDATE_STYLE INTERIM
                           | DDNS_UPDATE_STYLE NONE
                           | global_param_no_val
    """
    if len(p) > 2:
        p[0] = {p[1]: p[2]}
    else:
        p[0] = {p[1]: None}


def chi2(y, f, sigma, normalize=True):
    """
    Return the chi-square value for a series of measured values y and
    uncertainties sigma that are described by model f.

                    y_i - f_i
    chi2 = sum_i( (-----------)^2 )
                     sigma_i

    Can be normalized by the number of entries.

    Inputs:
    -------
    y       :: np.array; values of the data which is to be described
    f       :: np.array; values predicted by the model
    sigma   :: np.array or float; uncertainties on values y
    normalize
            :: boolean; whether to divide the result by the length of y or not

    """
    res = sum(((y - f) / sigma) ** 2)
    res = res / len(y) if normalize else res
    return res


def setDictDefaults(d, defaults):
    """Sets all defaults for the given dictionary to those contained in a
  second defaults dictionary.  This convenience method calls:

    d.setdefault(key, value)

  for each key and value in the given defaults dictionary.
  """
    for key, val in defaults.items():
        d.setdefault(key, val)
    return d


def remove_key(dict_list, key):
    """remove a key from each dict in a list of dictionaries"""
    for d in dict_list:
        if key in d:
            del d[key]
    return dict_list


def slugify(s, entities=True, decimal=True, hexadecimal=True):
    """Convert a string to a slug representation.

    Normalizes string, converts to lower-case, removes non-alpha characters,
    and converts spaces to hyphens.

    Parts from http://www.djangosnippets.org/snippets/369/

        >>> slugify("Manuel Vázquez Acosta")    # doctest: +SKIP
        'manuel-vazquez-acosta'

    If `s` and `entities` is True (the default) all HTML entities
    are replaced by its equivalent character before normalization::

        >>> slugify("Manuel Vázquez Acosta")   # doctest: +SKIP
        'manuel-vazquez-acosta'

    If `entities` is False, then no HTML-entities substitution is made::

        >>> value = "Manuel Vázquez Acosta"
        >>> slugify(value, entities=False)  # doctest: +SKIP
        'manuel-v-aacute-zquez-acosta'

    If `decimal` is True, then all entities of the form ``&#nnnn`` where
    `nnnn` is a decimal number deemed as a unicode codepoint, are replaced by
    the corresponding unicode character::

        >>> slugify('Manuel Vázquez Acosta')  # doctest: +SKIP
        'manuel-vazquez-acosta'

        >>> value = 'Manuel Vázquez Acosta'
        >>> slugify(value, decimal=False)  # doctest: +SKIP
        'manuel-v-225-zquez-acosta'

    If `hexadecimal` is True, then all entities of the form ``&#nnnn`` where
    `nnnn` is a hexdecimal number deemed as a unicode codepoint, are replaced
    by the corresponding unicode character::

        >>> slugify('Manuel Vázquez Acosta')  # doctest: +SKIP
        'manuel-vazquez-acosta'

        >>> slugify('Manuel Vázquez Acosta', hexadecimal=False)  # doctest: +SKIP  # noqa
        'manuel-v-x00e1-zquez-acosta'

    .. deprecated:: 2.1.0 Use `xotl.tools.strings.slugify`:func:.

    """
    import re
    from xotl.tools.string import slugify
    from xotl.tools.future.codecs import safe_decode
    if not isinstance(s, str):
        s = safe_decode(s)
    if entities:
        try:
            from htmlentitydefs import name2codepoint
        except ImportError:
            from html.entities import name2codepoint
        s = re.sub(str('&(%s);') % str('|').join(name2codepoint), lambda m:
            chr(name2codepoint[m.group(1)]), s)
    if decimal:
        try:
            s = re.sub('&#(\\d+);', lambda m: chr(int(m.group(1))), s)
        except Exception:
            pass
    if hexadecimal:
        try:
            s = re.sub('&#x([\\da-fA-F]+);', lambda m: chr(int(m.group(1),
                16)), s)
        except Exception:
            pass
    return slugify(s, '-')


def check_stacking(primitive, inputs):
    """checks if features in inputs can be used with supplied primitive
       using the stacking rules"""
    if primitive.stack_on_self is False:
        for f in inputs:
            if isinstance(f.primitive, primitive.__class__):
                return False
    if primitive.stack_on_exclude is not None:
        for f in inputs:
            if isinstance(f.primitive, tuple(primitive.stack_on_exclude)):
                return False
    for f in inputs:
        if f.number_output_features > 1:
            return False
    for f in inputs:
        if f.primitive.base_of_exclude is not None:
            if primitive in f.base_of_exclude:
                return False
    for f in inputs:
        if primitive.stack_on_self is True:
            if isinstance(f.primitive, primitive.__class__):
                continue
        if primitive.stack_on is not None:
            if isinstance(f.primitive, tuple(primitive.stack_on)):
                continue
        else:
            continue
        if f.primitive.base_of is not None:
            if primitive.__class__ in f.primitive.base_of:
                continue
        else:
            continue
        return False
    return True


@classmethod
def _build_model_attributes(cls, pool):
    """ Initialize base model attributes. """
    cls._description = cls._name
    cls._table = cls._name.replace('.', '_')
    cls._sequence = None
    cls._log_access = cls._auto
    cls._inherits = {}
    cls._depends = {}
    cls._constraints = {}
    cls._sql_constraints = []
    for base in reversed(cls.__bases__):
        if not getattr(base, 'pool', None):
            cls._description = base._description or cls._description
            cls._table = base._table or cls._table
            cls._sequence = base._sequence or cls._sequence
            cls._log_access = getattr(base, '_log_access', cls._log_access)
        cls._inherits.update(base._inherits)
        for mname, fnames in base._depends.items():
            cls._depends[mname] = cls._depends.get(mname, []) + fnames
        for cons in base._constraints:
            cls._constraints[getattr(cons[0], '__name__', id(cons[0]))] = cons
        cls._sql_constraints += base._sql_constraints
    cls._sequence = cls._sequence or cls._table + '_id_seq'
    cls._constraints = list(cls._constraints.values())
    for parent_name in cls._inherits:
        pool[parent_name]._inherits_children.add(cls._name)
    for child_name in cls._inherit_children:
        child_class = pool[child_name]
        child_class._build_model_attributes(pool)


def decode_polyline(polyline):
    """Decodes a Polyline string into a list of lat/lng dicts.

    See the developer docs for a detailed description of this encoding:
    https://developers.google.com/maps/documentation/utilities/polylinealgorithm

    :param polyline: An encoded polyline
    :type polyline: string

    :rtype: list of dicts with lat/lng keys
    """
    points = []
    index = lat = lng = 0
    while index < len(polyline):
        result = 1
        shift = 0
        while True:
            b = ord(polyline[index]) - 63 - 1
            index += 1
            result += b << shift
            shift += 5
            if b < 31:
                break
        lat += ~result >> 1 if result & 1 != 0 else result >> 1
        result = 1
        shift = 0
        while True:
            b = ord(polyline[index]) - 63 - 1
            index += 1
            result += b << shift
            shift += 5
            if b < 31:
                break
        lng += ~(result >> 1) if result & 1 != 0 else result >> 1
        points.append({'lat': lat * 1e-05, 'lng': lng * 1e-05})
    return points


def focus_next(event):
    """
    Focus the next visible Window.
    (Often bound to the `Tab` key.)
    """
    event.app.layout.focus_next()


def get_file_size():
    """Get data size as it will be downloaded.
        """


def get_welcome():
    """
        Wait for the 'welcome message' dictionary, sent by the server upon
        first connection.

        :rtype: ``Deferred[dict]``
        :return: the welcome dictionary, when it arrives from the server
        """


def query(query, timeout=None):
    """
        Dispatch C{query} to the method which can handle its type.

        @type query: L{twisted.names.dns.Query}
        @param query: The DNS query being issued, to which a response is to be
            generated.

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

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


def _erfimage_imshow(ax, ch_idx, tmin, tmax, vmin, vmax, ylim=None, data=
    None, epochs=None, sigma=None, order=None, scalings=None, vline=None,
    x_label=None, y_label=None, colorbar=False, cmap='RdBu_r', vlim_array=None
    ):
    """Plot erfimage on sensor topography."""
    from scipy import ndimage
    import matplotlib.pyplot as plt
    this_data = data[:, (ch_idx), :]
    if vlim_array is not None:
        vmin, vmax = vlim_array[ch_idx]
    if callable(order):
        order = order(epochs.times, this_data)
    if order is not None:
        this_data = this_data[order]
    if sigma > 0.0:
        this_data = ndimage.gaussian_filter1d(this_data, sigma=sigma, axis=0)
    img = ax.imshow(this_data, extent=[tmin, tmax, 0, len(data)], aspect=
        'auto', origin='lower', vmin=vmin, vmax=vmax, picker=True, cmap=
        cmap, interpolation='nearest')
    ax = plt.gca()
    if x_label is not None:
        ax.set_xlabel(x_label)
    if y_label is not None:
        ax.set_ylabel(y_label)
    if colorbar:
        plt.colorbar(mappable=img)


def remove_leading_trailing_whitespace_lines(src):
    """
    Remove any trailing whitespace lines.

    Parameters
    ----------
    src : str
        Input code.

    Returns
    -------
    str
        Code with trailing whitespace lines removed.
    """
    lines = src.splitlines()
    non_whitespace_lines = []
    for i, l in enumerate(lines):
        if l and not l.isspace():
            non_whitespace_lines.append(i)
    imin = min(non_whitespace_lines)
    imax = max(non_whitespace_lines)
    return '\n'.join(lines[imin:imax + 1])


@staticmethod
def setup_subparser(parser, cmd=None):
    """
        Set up a subparser for 'config'
        """
    parser.add_argument('pkg', nargs='?', help=
        'Package for configuration key to query or set')
    parser.add_argument('value', nargs='?', help='New value')
    parser.add_argument('-k', '--key', help=
        'Set or query this key instead of the install state')


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


def populate_display_title(apps, schema_editor):
    """
    Populates display_title for ContentGroup
    """
    ContentGroup = apps.get_model('stacks_contentgroup', 'ContentGroup')
    for cg in ContentGroup.objects.all():
        cg.display_title = cg.name
        cg.save()


def _frange(start, stop, step):
    """range for floats"""
    while start < stop:
        yield start
        start += step


def attach_group_policy(iam, policy_arn, group_name):
    """
    Attach a managed policy (specified by ARN) to a group (specified by
    group name).
    """
    return iam.get_response('AttachGroupPolicy', {'PolicyArn': policy_arn,
        'GroupName': group_name})


def make_clust(net, dist_type='cosine', run_clustering=True, dendro=True,
    requested_views=['pct_row_sum', 'N_row_sum'], linkage_type='average',
    sim_mat=False, filter_sim=0.1, calc_cat_pval=False, sim_mat_views=[
    'N_row_sum'], run_enrichr=None, enrichrgram=None):
    """
  This will calculate multiple views of a clustergram by filtering the
  data and clustering after each filtering. This filtering will keep the top
  N rows based on some quantity (sum, num-non-zero, etc).
  """
    from copy import deepcopy
    import scipy
    from . import calc_clust, run_filter, make_views, make_sim_mat, cat_pval
    from . import enrichr_functions as enr_fun
    df = net.dat_to_df()
    threshold = 0.0001
    df = run_filter.df_filter_row_sum(df, threshold)
    df = run_filter.df_filter_col_sum(df, threshold)
    define_cat_colors = False
    if run_enrichr is not None:
        df = enr_fun.add_enrichr_cats(df, 'row', run_enrichr)
        define_cat_colors = True
    net.df_to_dat(df, define_cat_colors=True)
    inst_dm = calc_clust.cluster_row_and_col(net, dist_type=dist_type,
        linkage_type=linkage_type, run_clustering=run_clustering, dendro=
        dendro, ignore_cat=False, calc_cat_pval=calc_cat_pval)
    all_views = []
    send_df = deepcopy(df)
    if 'N_row_sum' in requested_views:
        all_views = make_views.N_rows(net, send_df, all_views, dist_type=
            dist_type, rank_type='sum')
    if 'N_row_var' in requested_views:
        all_views = make_views.N_rows(net, send_df, all_views, dist_type=
            dist_type, rank_type='var')
    if 'pct_row_sum' in requested_views:
        all_views = make_views.pct_rows(net, send_df, all_views, dist_type=
            dist_type, rank_type='sum')
    if 'pct_row_var' in requested_views:
        all_views = make_views.pct_rows(net, send_df, all_views, dist_type=
            dist_type, rank_type='var')
    which_sim = []
    if sim_mat == True:
        which_sim = ['row', 'col']
    elif sim_mat == 'row':
        which_sim = ['row']
    elif sim_mat == 'col':
        which_sim = ['col']
    if sim_mat is not False:
        sim_net = make_sim_mat.main(net, inst_dm, which_sim, filter_sim,
            sim_mat_views)
        net.sim = {}
        for inst_rc in which_sim:
            net.sim[inst_rc] = sim_net[inst_rc].viz
            if inst_rc == 'row':
                other_rc = 'col'
            elif inst_rc == 'col':
                other_rc = 'row'
            net.sim[inst_rc]['cat_colors'][inst_rc] = net.viz['cat_colors'][
                inst_rc]
            net.sim[inst_rc]['cat_colors'][other_rc] = net.viz['cat_colors'][
                inst_rc]
    else:
        net.sim = {}
    net.viz['views'] = all_views
    if enrichrgram != None:
        net.viz['enrichrgram'] = enrichrgram
    if 'enrichrgram_lib' in net.dat:
        net.viz['enrichrgram'] = True
        net.viz['enrichrgram_lib'] = net.dat['enrichrgram_lib']
    if 'row_cat_bars' in net.dat:
        net.viz['row_cat_bars'] = net.dat['row_cat_bars']


def dbh_data_table(path, contenttype, extension, roottag, nsurl):
    """ Data Table Handler - Returns data_table for all tbl files """
    if extension == 'tbl':
        return 'db_data_table'
    else:
        return False


def noformat_code_macro_confluence(text, nopanel=None):
    """
    Wrap into code block macro
    :param text:
    :param nopanel: (bool) True or False Removes the panel around the content.
    :return:
    """
    if not nopanel:
        nopanel = False
    return (
        """                <ac:structured-macro ac:name="noformat" ac:schema-version="1">
                    <ac:parameter ac:name="nopanel">{nopanel}</ac:parameter>
                    <ac:plain-text-body><![CDATA[{text}]]></ac:plain-text-body>
                </ac:structured-macro>
            """
        .format(nopanel=nopanel, text=text))


def permissions(permissions):
    """Specify the permissions that comprise a role.
        """


@classmethod
def _FinalToString(cls, final_value):
    """Convert a final value to a string."""
    tags = []
    if final_value & cls.STD_extension:
        tags.append('extension')
    if final_value & cls.STD_list:
        tags.append('list')
    if final_value & cls.STD_restriction:
        tags.append('restriction')
    if final_value & cls.STD_union:
        tags.append('union')
    return ' '.join(tags)


def get_all(isamAppliance, check_mode=False, force=False):
    """
    Get all email objects
    """
    return isamAppliance.invoke_get('Get all email objects',
        '/core/rsp_email_objs')


def response_topic_name(topic, id):
    """calculate the name of the reponse topic for a topic"""
    return '%s.response.%s' % (topic, id)


@classmethod
def _getText(cls, node):
    """ provides xml content of the node

        :param node: DOM node
        :type node: :class:`xml.dom.Node`
        :returns: xml content string
        :rtype: :obj:`str`
        """
    xml = cls._toxml(node)
    start = xml.find('>')
    end = xml.rfind('<')
    if start == -1 or end < start:
        return ''
    return xml[start + 1:end].replace('<', '<').replace('>', '>'
        ).replace('"', '"').replace('&', '&')


def convert_surrogate_pair(match):
    """
    Convert a surrogate pair to the single codepoint it represents.

    This implements the formula described at:
    http://en.wikipedia.org/wiki/Universal_Character_Set_characters#Surrogates
    """
    pair = match.group(0)
    codept = 65536 + (ord(pair[0]) - 55296) * 1024 + (ord(pair[1]) - 56320)
    return chr(codept)


def _verify_all_legacy_ndarrays(op_name, func_name, args, out):
    """Verify if all the arrays are legacy ndarrays.

    Parameters
    ----------
    op_name : str
        Operator full name registered in backend.
    func_name : str
        Operator name exposed to users. This is usually the name by stripping off
        the prefix of the full operator names registered in backend.
    args : list of arrays
        Input ndarray arguments to be checked.
    out : ndarray or None or list of ndarrays
        User-provided output ndarrays.
    """
    from ..numpy import ndarray as np_ndarray
    for arr in args:
        if arr is not None and isinstance(arr, np_ndarray):
            raise TypeError(
                'Operator `{}` registered in backend is known as `{}` in Python. This is a legacy operator which can only accept legacy ndarrays, while received an MXNet numpy ndarray. Please call `as_nd_ndarray()` upon the numpy ndarray to convert it to a legacy ndarray, and then feed the converted array to this operator.'
                .format(op_name, func_name))
    if out is None:
        return
    if not isinstance(out, (list, tuple)):
        out = [out]
    for arr in out:
        if arr is not None and isinstance(arr, np_ndarray):
            raise TypeError(
                'Operator `{}` registered in backend is known as `{}` in Python. This is a legacy operator which can only write to legacy ndarrays, while received an MXNet numpy ndarray. Please call `as_nd_ndarray()` upon the numpy ndarray to convert it to a legacy ndarray, and then feed the converted array to this operator.'
                .format(op_name, func_name))


def htmlHeader(output, path, serverName, query=None):
    """Writes an HTML header."""
    if path and path != '/':
        output.write('<title>%s - Status: %s</title>' % (serverName, path))
    else:
        output.write('<title>%s - Status</title>' % serverName)
    output.write(
        """

  """
        )
    output.write('
Stats
')
    output.write('
%s
' % serverName)
    output.write(
        '
<form action="#" method="GET">Filter: <input type="text" name="query" size="20" value="%s"></form>
'
         % (query or ''))


def get_mangled_ignore(model):
    """returns a mangled attribute name specific to the model"""
    return '_{opt.model_name}__{opt.app_label}_cleanup_ignore'.format(opt=
        model._meta)


def is_empty(it):
    """ Returns True if the iterable `it` is already fully consumed. """
    try:
        next(it)
        return False
    except StopIteration:
        return True


def check_positive(**params):
    """Check that parameters are positive as expected

    Raises
    ------
    ValueError : unacceptable choice of parameters
    """
    for p in params:
        if params[p] <= 0:
            raise ValueError('Expected {} > 0, got {}'.format(p, params[p]))


@classmethod
def ensure_indexes(cls):
    """
        Check&create if needed the Document's indexes in database
        """
    if cls.opts.indexes:
        cls.collection.create_indexes(cls.opts.indexes)


def stringify_build(package, build):
    """Write the appropriate string for a build of a package"""
    return '{}-{}'.format(package.str, build)


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 add_resolution_to_reflections(reflections, experiments):
    """Add column d to reflection list"""
    imageset = experiments.imagesets()[0]
    if 'imageset_id' not in reflections:
        reflections['imageset_id'] = reflections['id']
    reflections.centroid_px_to_mm(imageset.get_detector(), imageset.get_scan())
    reflections.map_centroids_to_reciprocal_space(detector=imageset.
        get_detector(), beam=imageset.get_beam(), goniometer=imageset.
        get_goniometer())
    d_spacings = 1 / reflections['rlp'].norms()
    reflections['d'] = d_spacings


def delete_size_constraint_set(SizeConstraintSetId=None, ChangeToken=None):
    """
    Permanently deletes a  SizeConstraintSet . You can't delete a SizeConstraintSet if it's still used in any Rules or if it still includes any  SizeConstraint objects (any filters).
    If you just want to remove a SizeConstraintSet from a Rule , use  UpdateRule .
    To permanently delete a SizeConstraintSet , perform the following steps:
    See also: AWS API Documentation

    Examples
    The following example deletes a size constraint set  with the ID example1ds3t-46da-4fdb-b8d5-abc321j569j5.
    Expected Output:

    :example: response = client.delete_size_constraint_set(
        SizeConstraintSetId='string',
        ChangeToken='string'
    )

    :type SizeConstraintSetId: string
    :param SizeConstraintSetId: [REQUIRED]
            The SizeConstraintSetId of the SizeConstraintSet that you want to delete. SizeConstraintSetId is returned by CreateSizeConstraintSet and by ListSizeConstraintSets .

    :type ChangeToken: string
    :param ChangeToken: [REQUIRED]
            The value returned by the most recent call to GetChangeToken .

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

    :returns:
    SizeConstraintSetId (string) -- [REQUIRED]
    The SizeConstraintSetId of the  SizeConstraintSet that you want to delete. SizeConstraintSetId is returned by  CreateSizeConstraintSet and by  ListSizeConstraintSets .

    ChangeToken (string) -- [REQUIRED]
    The value returned by the most recent call to  GetChangeToken .

    """
    pass


def determine_encoding(env):
    """ Determine and return preferred encoding given session env. """
    from x84.bbs import get_ini
    default_encoding = get_ini(section='session', key='default_encoding'
        ) or 'utf8'
    fallback_encoding = {'ansi': 'cp437', 'ansi-bbs': 'cp437'}.get(env[
        'TERM'], default_encoding)
    return env.get('encoding', fallback_encoding)


@staticmethod
def _store_map_from_type(item_type):
    """
        Return a list with the name of the workstep store corresponding to the item type give.
        :param item_type: The string type of the item. If 'all' all stores will be returned
        :return: str store name, for all item_types except 'all'. If item_type == 'all', a tuple of all the store
        names. If item_type is not recognized, returns None.
        """
    if not item_type or not isinstance(item_type, str):
        return None
    if 'Signal' in item_type:
        return 'sqTrendSeriesStore'
    if 'Condition' in item_type:
        return 'sqTrendCapsuleSetStore'
    if 'Scalar' in item_type:
        return 'sqTrendScalarStore'
    if 'Metric' in item_type:
        return 'sqTrendMetricStore'
    if 'Table' in item_type:
        return 'sqTrendTableStore'
    if item_type == 'all':
        return ('sqTrendSeriesStore', 'sqTrendCapsuleSetStore',
            'sqTrendScalarStore', 'sqTrendMetricStore', 'sqTrendTableStore')
    else:
        return None


def string2requests_proxies(string: str):
    """
    Convert a proxy string for python requests.

    :param str string: Proxy string
    :return: dict
    """
    return {'http': string, 'https': string}


def load_ssl_context(cert_file, pkey_file):
    """Loads an SSL context from a certificate and private key file."""
    from OpenSSL import SSL
    ctx = SSL.Context(SSL.SSLv23_METHOD)
    ctx.use_certificate_file(cert_file)
    ctx.use_privatekey_file(pkey_file)
    return ctx


@classmethod
def get_by_uuid(cls, context, uuid):
    """Find a chassis based on UUID and return a :class:`Chassis` object.

        :param cls: the :class:`Chassis`
        :param context: Security context
        :param uuid: the UUID of a chassis.
        :returns: a :class:`Chassis` object.
        """
    db_chassis = cls.dbapi.get_chassis_by_uuid(uuid)
    chassis = cls._from_db_object(context, cls(), db_chassis)
    return chassis


def convert_taxonomy_xsd_bool(inp):
    """
    Returns true/false given a string loaded from the Taxonomy.  Values to check are based on
    observed values from within the Taxonomy.  If the input is not valid this will always return
    false.

    Args:
        inp (string): String containing booleans in Taxonomy XSD format.

    Returns:
        True or False
    """
    if inp is None:
        return False
    elif inp.strip().lower() in ['true', '1']:
        return True
    else:
        return False


def find_tile(player, name):
    """
    Find an adjacent tile that is connected to the current tile by name

    :param text_game_maker.player.player.Player player: player object
    :param str name: name of adjacent tile to search for
    :return: adjacent matching tile. If no matching tiles are found, None is        returned
    :rtype: text_game_maker.tile.tile.Tile
    """
    for tile in player.current.iterate_directions():
        if tile and name in tile.name:
            return tile
    return None


def get_num_image_channels(module_or_spec, signature=None, input_name=None):
    """Returns expected num_channels dimensions of an image input.

  This is for advanced users only who expect to handle modules with
  image inputs that might not have the 3 usual RGB channels.

  TODO(b/139530454): This does not work yet with TF2.

  Args:
    module_or_spec: a Module or ModuleSpec that accepts image inputs.
    signature: a string with the key of the signature in question.
      If None, the default signature is used.
    input_name: a string with the input name for images. If None, the
      conventional input name `images` for the default signature is used.

  Returns:
    An integer with the number of input channels to the module.

  Raises:
    ValueError: If the channel information is missing or malformed.
  """
    if input_name is None:
        input_name = 'images'
    input_info_dict = module_or_spec.get_input_info_dict(signature)
    try:
        shape = input_info_dict[input_name].get_shape()
    except KeyError:
        raise ValueError("Module is missing input '%s' in signature '%s'." %
            (input_name, signature or 'default'))
    try:
        _, _, _, num_channels = shape.as_list()
        if num_channels is None:
            raise ValueError
    except ValueError:
        raise ValueError(
            'Shape of module input is %s, expected [batch_size, height, width, num_channels] with known num_channels'
             % shape)
    return num_channels


def convert_uuid_to_pvm(uuid):
    """Converts a standard UUID to PowerVM format

    PowerVM uuids always set the byte 0, bit 0 to 0.

    :param uuid: A standard format uuid string
    :returns: A PowerVM compliant uuid
    """
    return '%x%s' % (int(uuid[0], 16) & 7, uuid[1:])


def is_iterable(obj):
    """
    True if an object is iterable, else False
    """
    return hasattr(obj, '__iter__')


def _format_local(local_path, local_is_path):
    """Format a path for log output"""
    if local_is_path:
        return local_path
    else:
        return getattr(local_path, 'name', '<file obj>')


def convert_to_timezone_naive(time_to_freeze):
    """
    Converts a potentially timezone-aware datetime to be a naive UTC datetime
    """
    if time_to_freeze.tzinfo:
        time_to_freeze -= time_to_freeze.utcoffset()
        time_to_freeze = time_to_freeze.replace(tzinfo=None)
    return time_to_freeze


def _get_var_map(id_, name, type_, category, ruleset, rulename, help):
    """
    Returns the variable substitution mapping.
    """
    return {'%id': id_, '%name': name, '%type': type_, '%category':
        category, '%ruleset': ruleset, '%rulename': rulename, '%help': help}


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

    The ".dev0" means dirty.

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


def are_you_sure(name):
    """
    @doc: Get user input about whether or not to delete a project
    @args: name is a str for the project name (corresponds to a directory)
    @return: user input str ('yes' | 'no')
    """
    message = (
        """Enter 'yes' (no quotes) to proceed with deleting the {} project
    and starting over, and 'no' to keep the existing project:
    """
        .format(name))
    ret = input(message)
    return ret


@staticmethod
def positional_to_model(value):
    """Converts a positional argument to a model value"""
    return value


def handle_extensions(extensions):
    """
    Organizes multiple extensions that are separated with commas or passed by
    using --extension/-e multiple times.

    For example: running 'django-admin makemessages -e js,txt -e xhtml -a'
    would result in an extension list: ['.js', '.txt', '.xhtml']

    >>> handle_extensions(['.html', 'html,js,py,py,py,.py', 'py,.py'])
    {'.html', '.js', '.py'}
    >>> handle_extensions(['.html, txt,.tpl'])
    {'.html', '.tpl', '.txt'}
    """
    ext_list = []
    for ext in extensions:
        ext_list.extend(ext.replace(' ', '').split(','))
    for i, ext in enumerate(ext_list):
        if not ext.startswith('.'):
            ext_list[i] = '.%s' % ext_list[i]
    return set(ext_list)


def _is_vdi_a_snapshot(vdi_rec):
    """Ensure VDI is a snapshot, and not cached image."""
    is_a_snapshot = vdi_rec['is_a_snapshot']
    image_id = vdi_rec['other_config'].get('image-id')
    return is_a_snapshot and not image_id


def bit_into_byte(byte, schema, value):
    """
    Update bits in a byte to get new value.

    args:
        - initial byte we are working with.
        - data schema(list): [index, bits]
          - data's starting bit index.
          - number of bits.
        - new value for target.
    returns:
        new byte value (int)
    """
    if type(value) is str:
        value = int(value, 2)
    index, bits = schema
    if index < 0 or index > 7:
        raise ValueError('Schema index must be 0 thru 7')
    if bits < 1 or bits > 8:
        raise ValueError('Schema bits must be 1 thru 8')
    if len(list(bin(byte)[2:])) > 8 or len(list(bin(value)[2:])) > 8:
        raise ValueError('Number too big. Not a byte value.')
    orig_bits = list(bin(byte)[2:].zfill(8))
    value_bits = list(bin(value)[2:].zfill(bits))
    for i in range(0, bits):
        orig_bits[index + i] = value_bits[i]
    return int(''.join(orig_bits), 2)


def simplified_pos(pos, tagset=None, default=''):
    """
    Return a simplified POS tag for a full POS tag `pos` belonging to a tagset `tagset`. By default the WordNet
    tagset is assumed.
    Does the following conversion by default:

    - all N... (noun) tags to 'N'
    - all V... (verb) tags to 'V'
    - all ADJ... (adjective) tags to 'ADJ'
    - all ADV... (adverb) tags to 'ADV'
    - all other to `default`

    Does the following conversion by with ``tagset=='penn'``:

    - all N... (noun) tags to 'N'
    - all V... (verb) tags to 'V'
    - all JJ... (adjective) tags to 'ADJ'
    - all RB... (adverb) tags to 'ADV'
    - all other to `default`

    :param pos: a POS tag
    :param tagset: the tagset used for `pos`
    :param default: default return value when tag could not be simplified
    :return: simplified tag
    """
    if tagset == 'penn':
        if pos.startswith('N') or pos.startswith('V'):
            return pos[0]
        elif pos.startswith('JJ'):
            return 'ADJ'
        elif pos.startswith('RB'):
            return 'ADV'
        else:
            return default
    elif pos.startswith('N') or pos.startswith('V'):
        return pos[0]
    elif pos.startswith('ADJ') or pos.startswith('ADV'):
        return pos[:3]
    else:
        return default


def WriteModulesFile(modules, version, sdir):
    """ Write the contents of the Modules file """
    modules.write('#%Module\n\n')
    modules.write('proc ModulesHelp { } {\n')
    modules.write(
        """    puts stderr "This module sets the path and environment variables for slepc-%s\"
"""
         % version)
    modules.write(
        '    puts stderr "     see https://slepc.upv.es/ for more information"\n'
        )
    modules.write('    puts stderr ""\n}\n')
    modules.write(
        """module-whatis "SLEPc - Scalable Library for Eigenvalue Problem Computations"

"""
        )
    modules.write('module load petsc\n')
    modules.write('set slepc_dir %s\n' % sdir)
    modules.write('setenv SLEPC_DIR $slepc_dir\n')


def _append_include(files):
    """
    Used in .parameters()
    """
    files['cpp'] += '\n# include "parameters.h"'


def syntaxCheck(rules):
    """
        Осуществить синтаксическую проверку текста правил.

        @param rules: правила анализа
        @type rules: C{str}
        """


def convert_docker_params(stack_env=None):
    """Convert Docker* params to "Container" varients for compatibility.

    """
    if stack_env:
        pd = stack_env.get('parameter_defaults', {})
        for k, v in pd.copy().items():
            if k.startswith('Docker') and k.endswith('Image'):
                name = 'Container%s' % k[6:]
                pd.setdefault(name, v)


def center_window(widget, width=None, height=None):
    """ Centres widget in the middle of the screen.

        Args:
            widget (Tk object): widget that needs to be centred.
            width (int, optional): width of the widget that should be used.
                If not specified, widget width is used.
            height (int, optional): height of the widget that should be used.
                If not specified, widget width is used.
    """
    widget.withdraw()
    widget.update_idletasks()
    if width is None:
        width = widget.winfo_reqwidth()
    if height is None:
        height = widget.winfo_reqheight()
    sw = widget.winfo_screenwidth()
    sh = widget.winfo_screenheight()
    x = (sw - width) / 2
    y = (sh - height) / 2
    widget.geometry('%dx%d+%d+%d' % (width, height, x, y))
    widget.deiconify()


def get_name(friendly=False):
    """Get name of this resource

    :return: name of this resource
    :rtype: str
    """
    if friendly:
        return 'Monitored service'
    return 'service'


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


@staticmethod
def _check_activity(activity, _length, _type):
    """
        Verifies that the activity length and the length of the points or triangles are the same.

        Args:
            activity (iterable): Activity at a given location
            _length (int): Count of all of the points or triangles
            _type (string): 'points' or 'triangles'
        """
    if len(activity) != _length:
        raise ValueError(
            'Length of activity must be equal to the length of {}.'.format(
            _type))


def coords_api_to_json_tilt(ang_up):
    """converts from API coordinates to robot coordinates."""
    ang_down = -ang_up
    return ang_down


def init_tables(session, *tables):
    """
    Create table classes in a database.

    Parameters
    ----------
    session : sqlalchemy.orm.Session instance
    tables : mapped table classes

    """
    for itable in tables:
        itable.__table__.create(session.bind)
    session.commit()


def project():
    """
        Return containing project workspace for context or None
        """


def alignStr(x, size, pad=' ', padzero=True):
    """Pad length of `x` to multiple of `size`"""
    if len(x) == 0:
        return pad * size if padzero else x
    x = pad * (-len(x) % size) + x
    return x


def _setvalue(value, typ, size, nulls=None, strip=None):
    """Set a field's value"""
    if nulls and value in nulls:
        value = ''
    if strip:
        value = strip.sub('', value)
    if typ == 'N':
        value = str(value).rjust(size, ' ')
    elif typ == 'D':
        try:
            value = value.strftime('%Y%m%d')
        except AttributeError:
            value = ''
    elif typ == 'L':
        value = str(value)[0].upper()
    else:
        value = str(value)[:size].ljust(size, ' ')
    return value


@classmethod
def getModelRelations(cls):
    """
            getModelRelations - This method should return a list of relations.ForeignRelation objects
                for any relations on this object.

                This is a method instead of a property so that you can import models within this function
                    without creating circular references.

                This method will be called once per class and the value cached.

                    @return dict < <str> : relations.ForeignRelations> - Foreign relations on this model

                        If the key is a string, this model will gain an attribute with that name which will
                          return the related model when accessed.

                        For example, { 'person' : OneToManyRelation('id_person', Person, 'id') }

                          If you access myModelObj.person it will return the Person object related by Person.id = myModel.id_person

                        The key could also be a model class or whatever, and you can get the related object(s)

                          by calling myModelObj.getRelated(#key)
        """
    return []


def rgba_to_hsva(r, g, b, alpha=1.0):
    """Converts a color given by its RGBA coordinates to HSVA coordinates
    (hue, saturation, value, alpha).

    Each of the RGBA coordinates must be in the range [0, 1].
    """
    rgb_min, rgb_max = float(min(r, g, b)), float(max(r, g, b))
    alpha = float(alpha)
    value = float(rgb_max)
    if value <= 0:
        return 0.0, 0.0, 0.0, alpha
    sat = 1.0 - rgb_min / value
    if sat <= 0:
        return 0.0, 0.0, value, alpha
    d = rgb_max - rgb_min
    r = (r - rgb_min) / d
    g = (g - rgb_min) / d
    b = (b - rgb_min) / d
    rgb_max = max(r, g, b)
    if rgb_max == r:
        hue = 0.0 + (g - b) / 6.0
        if hue < 0:
            hue += 1
    elif rgb_max == g:
        hue = 1 / 3.0 + (b - r) / 6.0
    else:
        hue = 2 / 3.0 + (r - g) / 6.0
    return hue, sat, value, alpha


def getFileDialogTitle(msg, title):
    """
    Create nicely-formatted string based on arguments msg and title
    :param msg: the msg to be displayed
    :param title: the window title
    :return: None
    """
    if msg and title:
        return '%s - %s' % (title, msg)
    if msg and not title:
        return str(msg)
    if title and not msg:
        return str(title)
    return None


def validate_default_value(handler, default, norm, param='value'):
    """
    assert helper that quickly validates default value.
    designed to get out of the way and reduce overhead when asserts are stripped.
    """
    assert default is not None, '%s lacks default %s' % (handler.name, param)
    assert norm(default) == default, '%s: invalid default %s: %r' % (handler
        .name, param, default)
    return True


def strpar(cfg, section, key):
    """String representation of a section/key combination"""
    return '[{}] {} = {}'.format(section, key, cfg[section][key])


@staticmethod
def strip_ws_name(range_str):
    """Splits out the worksheet reference, if any, from a range reference."""
    if '!' in range_str:
        sheet, range_str = range_str.rsplit('!', 1)
        return sheet + '!', range_str
    return '', range_str


def customdata_skin_clear():
    """Clear vertex skin layer

    """
    pass


def _has_method(theobj, name):
    """
    Check if the object has a callable method with the given name,
    if yes return the method, otherwise return None

    :param theobj: the object that contains the method
    :param name: the name of the method
    :return: the method or None
    """
    tmp = getattr(theobj, name, None)
    if tmp is not None and callable(tmp):
        return tmp
    else:
        return None


def collection_enable_render():
    """Render the collection

    """
    pass


def add(ctxt, id, str, fuzzy, messages):
    """Add a non-fuzzy translation to the dictionary."""
    if not fuzzy and str:
        if ctxt is None:
            messages[id] = str
        else:
            messages[b'%b\x04%b' % (ctxt, id)] = str


def make_element(builder, tag, content):
    """Make an element with this tag and text content"""
    builder.start(tag, {})
    builder.data(content)
    builder.end(tag)


def ansi2html():
    """
    Script to convert a console output to html.
    """
    import os
    from LbNightlyTools.HTMLUtils import convertFile
    from optparse import OptionParser
    parser = OptionParser(usage='%prog [options] <input> <output>')
    try:
        opts, (input, output) = parser.parse_args()
    except ValueError:
        parser.error('wrong number of arguments')
    return convertFile(input, output)


def get_sync(lucid):
    """Call lucid8824.get_sync, exit on failure"""
    print('Sync:\t\t%s' % lucid.get_sync_source())


@staticmethod
def getJavaPackage():
    """ Returns package name String. """
    return 'com.microsoft.ml.spark.recommendation.RecommendationIndexerModel'


def convertir_booleanos(data):
    """ VALIDACIÓN DE CAMPOS BOOLEANOS ENVIADOS VÍA AJAX.

    Retorna todos los valores enviados vía AJAX convirtiendo a Booleanos los que correspondan.

    """
    for dat in data:
        if data[dat] == 'true':
            data[dat] = True
        if data[dat] == 'false':
            data[dat] = False
    return data


def merge_dicts(*dict_args):
    """
    Given any number of dicts, shallow copy and merge into a new dict.
    If the same key appars multiple times, priority goes to key/value
    pairs in latter dicts.
    """
    result = {}
    for dictionary in dict_args:
        result.update(dictionary)
    return result


def Haider_Levenspiel(Re):
    """Calculates drag coefficient of a smooth sphere using the method in
    [1]_ as described in [2]_.

    .. math::
        C_D=\\frac{24}{Re}(1+0.1806Re^{0.6459})+\\left(\\frac{0.4251}{1
        +\\frac{6880.95}{Re}}\\right)

    Parameters
    ----------
    Re : float
        Reynolds number of the sphere, [-]

    Returns
    -------
    Cd : float
        Drag coefficient [-]

    Notes
    -----
    Range is Re <= 2E5
    An improved version of this correlation is in Brown and Lawler.

    Examples
    --------
    >>> Haider_Levenspiel(200.)
    0.7959551680251666

    References
    ----------
    .. [1] Haider, A., and O. Levenspiel. "Drag Coefficient and Terminal
       Velocity of Spherical and Nonspherical Particles." Powder Technology
       58, no. 1 (May 1989): 63-70. doi:10.1016/0032-5910(89)80008-7.
    .. [2] Barati, Reza, Seyed Ali Akbar Salehi Neyshabouri, and Goodarz
       Ahmadi. "Development of Empirical Models with High Accuracy for
       Estimation of Drag Coefficient of Flow around a Smooth Sphere: An
       Evolutionary Approach." Powder Technology 257 (May 2014): 11-19.
       doi:10.1016/j.powtec.2014.02.045.
    """
    Cd = 24.0 / Re * (1 + 0.1806 * Re ** 0.6459) + 0.4251 / (1 + 6880.95 / Re)
    return Cd


def doKeywordArgs(keys, d=None):
    """
    Return a result dict that is a copy of the keys dict
    with missing items replaced by defaults in d dict.
    """
    if d is None:
        d = {}
    result = {}
    for key, default_val in d.items():
        isBool = default_val in (True, False)
        val = keys.get(key)
        if isBool and val in (True, 'True', 'true'):
            result[key] = True
        elif isBool and val in (False, 'False', 'false'):
            result[key] = False
        elif val is None:
            result[key] = default_val
        else:
            result[key] = val
    return result


def apply_func(key_kwargs_, func):
    """
    given some keywords and a function call the function
    :param key_kwargs_: a tuple of (key, args) to use
    :param func: function to call
    :return: the key for this and the value returned from calling the func
    """
    key = key_kwargs_[0]
    kwargs_ = key_kwargs_[1]
    val = func(*kwargs_)
    return key, val


def composite(image1, image2, mask):
    """
    Create composite image by blending images using a transparency mask.

    :param image1: The first image.
    :param image2: The second image.  Must have the same mode and
       size as the first image.
    :param mask: A mask image.  This image can have mode
       "1", "L", or "RGBA", and must have the same size as the
       other two images.
    """
    image = image2.copy()
    image.paste(image1, None, mask)
    return image


def get_filtered_dict(original_dict, whitelisted_keys):
    """Gets a dictionary filtered by whitelisted keys

    :param original_dict: The original dictionary of arguments to source keys
        and values.
    :param whitelisted_key: A list of keys to include in the filtered
        dictionary.

    :returns: A dictionary containing key/values from the original dictionary
        whose key was included in the whitelist
    """
    filtered_dict = {}
    for key, value in original_dict.items():
        if key in whitelisted_keys:
            filtered_dict[key] = value
    return filtered_dict


def scroller_activate():
    """Scroll view by mouse click and drag

    """
    pass


@classmethod
def from_raw(cls, raw_arg):
    """create Arg from RawArg instance
        """
    return cls(raw_arg.name, raw_arg.alias, raw_arg.required, raw_arg.default)


def sector_size(node=None, parent=None, attr_index=None, rawdata=None,
    new_value=None, **kwargs):
    """Size getter for the getting the byte size of a sector in the FAT."""
    if new_value is None:
        if 'sector_shift' in kwargs:
            return 1 << kwargs['sector_shift']
        try:
            return 1 << parent.get_root().data.header.sector_shift
        except AttributeError:
            return 0


def to_tcl_path(path):
    """
    Return a path string in a format suitable for TCL.
    """
    return str(path.resolve()).replace('\\', '/')


def is_num(any_param):
    """
    Check is number or string, if number, return it
    :param any_param:
    :return: int or False
    """
    if isinstance(any_param, int):
        return any_param
    else:
        try:
            num = int(any_param)
            return num
        except ValueError:
            return False


def BoolVectorProperty(name: str='', description: str='', default: list=(
    False, False, False), options: set={'ANIMATABLE'}, subtype: str='NONE',
    size: int=3, update=None, get=None, set=None):
    """Returns a new vector boolean property definition.

    :param name: Name used in the user interface.
    :type name: str
    :param description: Text used for the tooltip and api documentation.
    :type description: str
    :param default: sequence of booleans the length of size.
    :type default: list
    :param options: Enumerator in [‘HIDDEN’, ‘SKIP_SAVE’, ‘ANIMATABLE’, ‘LIBRARY_EDITABLE’, ‘PROPORTIONAL’,’TEXTEDIT_UPDATE’].
    :type options: set
    :param subtype: Enumerator in [‘COLOR’, ‘TRANSLATION’, ‘DIRECTION’, ‘VELOCITY’, ‘ACCELERATION’, ‘MATRIX’, ‘EULER’, ‘QUATERNION’, ‘AXISANGLE’, ‘XYZ’, ‘COLOR_GAMMA’, ‘LAYER’, ‘NONE’].
    :type subtype: str
    :param size: Vector dimensions in [1, 32].
    :type size: int
    :param update: Function to be called when this value is modified, This function must take 2 values (self, context) and return None. Warning there are no safety checks to avoid infinite recursion.
    :param get: Function to be called when this value is ‘read’, This function must take 1 value (self) and return the value of the property.
    :param set: Function to be called when this value is ‘written’, This function must take 2 values (self, value) and return None.
    """
    pass


def elgamal_keygen(params):
    """ generate an El Gamal key pair """
    G, o, g, h = params
    d = o.random()
    gamma = d * g
    return d, gamma


def decorate_tree(tree, decorate_value):
    """
    Transform each node in the tree's id value
    into a tuple of `(decorate_value, node.id)`
    to use common ids across identical graphs but
    discriminable between attached copies.

    Parameters
    ----------
    tree: Glycan
    decorate_value: int

    Returns
    -------
    Glycan
    """
    for node in tuple(tree):
        node.id = decorate_value, node.id
        for i, substituent in node.substituents():
            substituent.id = decorate_value, substituent.id
    return tree


def re_space_out(t):
    """
    Given an input string, return a regular expression that matches any
    sequence of the characters in the input text, separated by arbitrary
    number of white spaces in between, e.g.:

    r'\\foo' ==> r'\\s*\\\\\\s*f\\s*o\\s*o\\s*'
    which would match:
    r'  \\  fo  o  '

    'a\\tb' ==> '\\\\s*a\\\\s*\\t\\\\s*b\\\\s*'
    which would match:
    ' \\t\\n  a\\r\\t\\v b  \\n '

    """
    return '\\s*'.join([''] + list(t) + [''])


def expose(obj):
    """Set obj._exposed = True and return obj."""
    obj._exposed = True
    return obj


def cols_to_cube_root_normalize(df, columns):
    """Convert data points to their normalized cube root value so all values are between 0-1 and return new columns of prefixed data.

    Args:
        df: Pandas dataframe.
        columns: List of columns to transform.

    Returns:
        Original dataframe with additional prefixed columns.
    """
    for col in columns:
        df['cube_root_' + col] = (df[col] - df[col].min()) / (df[col].max() -
            df[col].min()) ** (1 / 3)
    return df


def group_add():
    """Add an object to a new group

    """
    pass


def world_add():
    """Add Rigid Body simulation world to the current scene

    """
    pass


@classmethod
def by_token(cls, token):
    """Get entry for a given token."""
    return cls.first(where=(cls.token == token,))


def escape_macros(value):
    """Escapes replacement macros in text."""
    if type(value) is str:
        return value.replace('$(', '$_(')
    else:
        return value


def sentence_position(i, size):
    """Different sentence positions indicate different
    probability of being an important sentence.
    """
    normalized = i * 1.0 / size
    if normalized > 1.0:
        return 0
    elif normalized > 0.9:
        return 0.15
    elif normalized > 0.8:
        return 0.04
    elif normalized > 0.7:
        return 0.04
    elif normalized > 0.6:
        return 0.06
    elif normalized > 0.5:
        return 0.04
    elif normalized > 0.4:
        return 0.05
    elif normalized > 0.3:
        return 0.08
    elif normalized > 0.2:
        return 0.14
    elif normalized > 0.1:
        return 0.23
    elif normalized > 0:
        return 0.17
    else:
        return 0


@classmethod
def remove_importer(cls, importer):
    """ Removes the importer from the list of active importers.
        If the importer is not in the list, this is a no-op.

        """
    importers = cls.__importers
    if importer in importers:
        importers.remove(importer)


@staticmethod
def should(vm):
    """
        Return True if branching should occur, based on the virtual machine's state.
        """
    raise NotImplementedError


def single_representation(n, N_au, N_class, N_tot):
    """Compute the representation of a population in a given areal unit"""
    if N_class != 0 and N_au != 0:
        return n / N_au / (N_class / N_tot)
    else:
        return float('nan')


def scan_setup_py():
    """Validate the contents of setup.py against Versioneer's expectations."""
    found = set()
    setters = False
    errors = 0
    with open('setup.py', 'r') as f:
        for line in f.readlines():
            if 'import versioneer' in line:
                found.add('import')
            if 'versioneer.get_cmdclass()' in line:
                found.add('cmdclass')
            if 'versioneer.get_version()' in line:
                found.add('get_version')
            if 'versioneer.VCS' in line:
                setters = True
            if 'versioneer.versionfile_source' in line:
                setters = True
    if len(found) != 3:
        print('')
        print('Your setup.py appears to be missing some important items')
        print('(but I might be wrong). Please make sure it has something')
        print('roughly like the following:')
        print('')
        print(' import versioneer')
        print(' setup( version=versioneer.get_version(),')
        print('        cmdclass=versioneer.get_cmdclass(),  ...)')
        print('')
        errors += 1
    if setters:
        print("You should remove lines like 'versioneer.VCS = ' and")
        print("'versioneer.versionfile_source = ' . This configuration")
        print('now lives in setup.cfg, and should be removed from setup.py')
        print('')
        errors += 1
    return errors


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

        :param PaperContentPermanentlyDeleteType val:
        :rtype: EventType
        """
    return cls('paper_content_permanently_delete', val)


def check_pre(v, g, order):
    """Check if all predecessors have already been placed."""
    if order.get_position(v) > -2:
        return False
    for v2 in g.predecessors(v):
        if order.get_position(v2) == -2:
            return False
    return True


def format_git_describe(git_str, pep440=False):
    """format the result of calling 'git describe' as a python version"""
    if '-' not in git_str:
        formatted_str = git_str
    else:
        git_str = git_str.replace('-', '.post', 1)
        if pep440:
            formatted_str = git_str.split('-')[0]
        else:
            formatted_str = git_str.replace('-g', '+git')
    if formatted_str.startswith('v'):
        formatted_str = formatted_str[1:]
    return formatted_str


def getRelativeContentPath(content):
    """ Return the site-relative path for 'content'

        o The site is the parent of the tool.

        o Return a sequence of path elements.

        o Permission:  Public
        """


def count_jobs(squeue_output, whoami):
    """
    Parse SLURM's ``squeue`` output and return a quadruple `(R, Q, r,
    q)` where:

      * `R` is the total number of running jobs (from any user);
      * `Q` is the total number of queued jobs (from any user);
      * `r` is the number of running jobs submitted by user `whoami`;
      * `q` is the number of queued jobs submitted by user `whoami`

    The `squeue_output` must contain the results of an invocation of
    ``squeue --noheader --format='%i^%T^%u^%U^%r^%R'``.
    """
    total_running = 0
    total_queued = 0
    own_running = 0
    own_queued = 0
    for line in squeue_output.split('\n'):
        if line == '':
            continue
        jobid, state, username, uid, reason, nodelist = line.split('^')
        if state in ['RUNNING', 'COMPLETING']:
            total_running += 1
            if username == whoami:
                own_running += 1
        elif state in ['PENDING', 'CONFIGURING']:
            total_queued += 1
            if username == whoami:
                own_queued += 1
    return total_running, total_queued, own_running, own_queued


def rip_move(MESH_OT_rip=None, TRANSFORM_OT_translate=None):
    """Rip polygons and move the result

    :param MESH_OT_rip: Rip, Disconnect vertex or edges from connected geometry
    :param TRANSFORM_OT_translate: Move, Move selected items
    """
    pass


@classmethod
def select_old_method(cls, dim, old_method):
    """Retrieve all algorithms which are fitting the requirements from
        old_method. Valid input are "numpy", "cython", "bbox" or "splitpixel",
        "lut", "csr", "nosplit_csr", "full_csr", "lut_ocl" and "csr_ocl".
        """
    results = []
    for v in cls._registry.values():
        if v.dimension == dim and v.old_method_name == old_method:
            results.append(v)
    if results:
        return results
    dim = int(dim)
    method = cls.parse_old_method(old_method)
    _, split, algo, impl, target = method
    if target in ['cpu', 'gpu', None]:
        target_type = target
        target = None
    else:
        target_type = None
    return cls.select_method(dim, split, algo, impl, target_type=
        target_type, target=target)


def validate_equalsize(a, b):
    """Check if two arrays have the same lenght."""
    if len(a) != len(b):
        raise ValueError('Arrays must have the same lenght.')


def color(text, color=None, background=None, light=False, enabled=True):
    """
    Return text in desired color if coloring enabled

    Available colors: black red green yellow blue magenta cyan white.
    Alternatively color can be prefixed with "light", e.g. lightgreen.
    """
    colors = {'black': 30, 'red': 31, 'green': 32, 'yellow': 33, 'blue': 34,
        'magenta': 35, 'cyan': 36, 'white': 37}
    if not enabled:
        return text
    if color and color.startswith('light'):
        light = True
        color = color[5:]
    color = color and ';{0}'.format(colors[color]) or ''
    background = background and ';{0}'.format(colors[background] + 10) or ''
    light = light and 1 or 0
    start = '\x1b[{0}{1}{2}m'.format(light, color, background)
    finish = '\x1b[1;m'
    return ''.join([start, text, finish])


@classmethod
def from_dict(cls, database_dict):
    """
        Given a dict, which contains the database config, this method will create and return a new DatabaseConfig
        object from it.
        The dict can contain any subset of the following keys, every key not used it substituted with the default item:
        - engine
        - host
        - database
        - port
        - user
        - password

        CHANGELOG

        Added 09.06.2019

        :param database_dict:
        :return:
        """
    argument_dict = cls.DEFAULT_DICT
    argument_dict.update(database_dict)
    return cls(**argument_dict)


def delete_thing(thingName=None, expectedVersion=None):
    """
    Deletes the specified thing.
    See also: AWS API Documentation

    :example: response = client.delete_thing(
        thingName='string',
        expectedVersion=123
    )

    :type thingName: string
    :param thingName: [REQUIRED]
            The name of the thing to delete.

    :type expectedVersion: integer
    :param expectedVersion: The expected version of the thing record in the registry. If the version of the record in the registry does not match the expected version specified in the request, the DeleteThing request is rejected with a VersionConflictException .

    :rtype: dict
    :return: {}

    """
    pass


def _append_to_command(cmd, data):
    """Append list or single value to command."""
    if not isinstance(data, (list, tuple)):
        data = [data]
    cmd.extend(data)


def getNounVerb(rhinoMorph, rn, data, eomi=True, combineN=False):
    """명사류와 동사류를 가지고 옵니다"""
    morphed_data_NNGNNP = ''
    morphed_data_VVVAXR = ''
    for data_each in data:
        morphs, poses = rhinoMorph.wholeResult_list(rn, data_each, eomi=
            eomi, combineN=combineN)
        NNGNNP = []
        VVVAXR = []
        for i, pose in enumerate(poses):
            if pose == 'NNG' or pose == 'NNP' or pose == 'NP' or pose == 'XR':
                NNGNNP.append(morphs[i] + ' ')
            elif pose == 'VV' or pose == 'VA' or pose == 'XR' or pose == 'VX' or pose == 'VCP' or pose == 'VCN':
                VVVAXR.append(morphs[i] + ' ')
        joined_data_each_NNGNNP = ' '.join(NNGNNP)
        joined_data_each_VVVAXR = ' '.join(VVVAXR)
        if joined_data_each_NNGNNP:
            morphed_data_NNGNNP += joined_data_each_NNGNNP
        if joined_data_each_VVVAXR:
            morphed_data_VVVAXR += joined_data_each_VVVAXR
    return morphed_data_NNGNNP, morphed_data_VVVAXR


def resource_isdir(resource_name):
    """Is the named resource a directory?  (like ``os.path.isdir()``)"""


def set_property(name, value):
    """ Sets a property to be changed
        """


def coalesce_volume_dates(in_volumes, in_dates, indexes):
    """Sums volumes between the indexes and ouputs
    dates at the indexes
    in_volumes : original volume list
    in_dates : original dates list
    indexes : list of indexes

    Returns
    -------
    volumes: new volume array
    dates: new dates array
    """
    volumes, dates = [], []
    for i in range(len(indexes)):
        dates.append(in_dates[indexes[i]])
        to_sum_to = indexes[i + 1] if i + 1 < len(indexes) else len(in_volumes)
        volumes.append(sum(in_volumes[indexes[i]:to_sum_to]))
    return volumes, dates


def formula(individu, period, parameters):
    """
        Calcul du salaire net d'après définition INSEE
        net = net de csg et crds
        """
    salaire_imposable = individu('salaire_imposable', period)
    crds_salaire = individu('crds_salaire', period)
    csg_imposable_salaire = individu('csg_imposable_salaire', period)
    return salaire_imposable + crds_salaire + csg_imposable_salaire


def calculate_co2_emission(phases_co2_emissions, phases_distances):
    """
    Calculates the CO2 emission of the cycle [CO2g/km].

    :param phases_co2_emissions:
        CO2 emission of cycle phases [CO2g/km].
    :type phases_co2_emissions: numpy.array

    :param phases_distances:
        Cycle phases distances [km].
    :type phases_distances: numpy.array | float

    :return:
        CO2 emission value of the cycle [CO2g/km].
    :rtype: float
    """
    n = sum(phases_co2_emissions * phases_distances)
    if isinstance(phases_distances, float):
        d = phases_distances * len(phases_co2_emissions)
    else:
        d = sum(phases_distances)
    return float(n / d)


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


def write_filter(arr, fname, norm=False):
    """
    Convert convolution array into SExtractor-readable file.
    """
    file = open(fname, 'w')
    if norm:
        arr /= arr.sum()
        file.write('CONV NONORM\n')
    else:
        file.write('CONV NORM\n')
    for row in arr.astype('str'):
        line = ' '.join(row)
        file.write(line)
        file.write('\n')
    file.close()


def _py3bytes(l):
    """Converts a list of bytes to Python 3 bytes."""
    return bytes(l)


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

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

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


@classmethod
def _from_dict(cls, _dict):
    """Initialize a DialogLogMessage object from a json dictionary."""
    args = {}
    if 'level' in _dict:
        args['level'] = _dict.get('level')
    else:
        raise ValueError(
            "Required property 'level' not present in DialogLogMessage JSON")
    if 'message' in _dict:
        args['message'] = _dict.get('message')
    else:
        raise ValueError(
            "Required property 'message' not present in DialogLogMessage JSON")
    return cls(**args)


def read_last_line(fh, offset=256):
    """ Read last line from a seekable file. Start reading
    from buff before end of file, and double backwards seek
    until line break is found. If reached beginning of file
    (no lines), just return whole file
    """
    fh.seek(0, 2)
    size = fh.tell()
    while offset < size:
        fh.seek(-offset, 2)
        lines = fh.readlines()
        if len(lines) > 1:
            return lines[-1]
        offset *= 2
    fh.seek(0, 0)
    return fh.readlines()[-1]


@classmethod
def from_json(cls, _json, project, client_api, bots=None):
    """
        Build a Bot entity object from a json

        :param client_api:
        :param _json: _json response from host
        :param project: project entity
        :param bots: Bots repository
        :return: User object
        """
    return cls(createdAt=_json.get('createdAt', None), name=_json.get(
        'firstName', None), updatedAt=_json.get('updatedAt', None),
        last_name=_json.get('lastName', None), username=_json.get(
        'username', None), avatar=_json.get('avatar', None), email=_json.
        get('email', None), type=_json.get('type', None), org=_json.get(
        'org', None), id=_json.get('id', None), project=project, client_api
        =client_api, bots=bots)


def x11colors():
    """
    Get the x11 colors

    Returns
    -------
        The x11 colors
    """
    return {'aliceblue': 'F0F8FF', 'antiquewhite': 'FAEBD7', 'aqua':
        '00FFFF', 'aquamarine': '7FFFD4', 'azure': 'F0FFFF', 'beige':
        'F5F5DC', 'bisque': 'FFE4C4', 'black': '000000', 'blanchedalmond':
        'FFEBCD', 'blue': '0000FF', 'blueviolet': '8A2BE2', 'brown':
        'A52A2A', 'burlywood': 'DEB887', 'cadetblue': '5F9EA0',
        'chartreuse': '7FFF00', 'chocolate': 'D2691E', 'coral': 'FF7F50',
        'cornflowerblue': '6495ED', 'cornsilk': 'FFF8DC', 'crimson':
        'DC143C', 'cyan': '00FFFF', 'darkblue': '00008B', 'darkcyan':
        '008B8B', 'darkgoldenrod': 'B8860B', 'darkgray': 'A9A9A9',
        'darkgreen': '006400', 'darkgrey': 'A9A9A9', 'darkkhaki': 'BDB76B',
        'darkmagenta': '8B008B', 'darkolivegreen': '556B2F', 'darkorange':
        'FF8C00', 'darkorchid': '9932CC', 'darkred': '8B0000', 'darksalmon':
        'E9967A', 'darkseagreen': '8FBC8F', 'darkslateblue': '483D8B',
        'darkslategray': '2F4F4F', 'darkslategrey': '2F4F4F',
        'darkturquoise': '00CED1', 'darkviolet': '9400D3', 'deeppink':
        'FF1493', 'deepskyblue': '00BFFF', 'dimgray': '696969', 'dimgrey':
        '696969', 'dodgerblue': '1E90FF', 'firebrick': 'B22222',
        'floralwhite': 'FFFAF0', 'forestgreen': '228B22', 'fuchsia':
        'FF00FF', 'gainsboro': 'DCDCDC', 'ghostwhite': 'F8F8FF', 'gold':
        'FFD700', 'goldenrod': 'DAA520', 'gray': '808080', 'green':
        '008000', 'greenyellow': 'ADFF2F', 'grey': '808080', 'honeydew':
        'F0FFF0', 'hotpink': 'FF69B4', 'indianred': 'CD5C5C', 'indigo':
        '4B0082', 'ivory': 'FFFFF0', 'khaki': 'F0E68C', 'lavender':
        'E6E6FA', 'lavenderblush': 'FFF0F5', 'lawngreen': '7CFC00',
        'lemonchiffon': 'FFFACD', 'lightblue': 'ADD8E6', 'lightcoral':
        'F08080', 'lightcyan': 'E0FFFF', 'lightgoldenrodyellow': 'FAFAD2',
        'lightgray': 'D3D3D3', 'lightgreen': '90EE90', 'lightgrey':
        'D3D3D3', 'lightpink': 'FFB6C1', 'lightsalmon': 'FFA07A',
        'lightseagreen': '20B2AA', 'lightskyblue': '87CEFA',
        'lightslategray': '778899', 'lightslategrey': '778899',
        'lightsteelblue': 'B0C4DE', 'lightyellow': 'FFFFE0', 'lime':
        '00FF00', 'limegreen': '32CD32', 'linen': 'FAF0E6', 'magenta':
        'FF00FF', 'maroon': '800000', 'mediumaquamarine': '66CDAA',
        'mediumblue': '0000CD', 'mediumorchid': 'BA55D3', 'mediumpurple':
        '9370DB', 'mediumseagreen': '3CB371', 'mediumslateblue': '7B68EE',
        'mediumspringgreen': '00FA9A', 'mediumturquoise': '48D1CC',
        'mediumvioletred': 'C71585', 'midnightblue': '191970', 'mintcream':
        'F5FFFA', 'mistyrose': 'FFE4E1', 'moccasin': 'FFE4B5',
        'navajowhite': 'FFDEAD', 'navy': '000080', 'oldlace': 'FDF5E6',
        'olive': '808000', 'olivedrab': '6B8E23', 'orange': 'FFA500',
        'orangered': 'FF4500', 'orchid': 'DA70D6', 'palegoldenrod':
        'EEE8AA', 'palegreen': '98FB98', 'paleturquoise': 'AFEEEE',
        'palevioletred': 'DB7093', 'papayawhip': 'FFEFD5', 'peachpuff':
        'FFDAB9', 'peru': 'CD853F', 'pink': 'FFC0CB', 'plum': 'DDA0DD',
        'powderblue': 'B0E0E6', 'purple': '800080', 'red': 'FF0000',
        'rosybrown': 'BC8F8F', 'royalblue': '4169E1', 'saddlebrown':
        '8B4513', 'salmon': 'FA8072', 'sandybrown': 'F4A460', 'seagreen':
        '2E8B57', 'seashell': 'FFF5EE', 'sienna': 'A0522D', 'silver':
        'C0C0C0', 'skyblue': '87CEEB', 'slateblue': '6A5ACD', 'slategray':
        '708090', 'slategrey': '708090', 'snow': 'FFFAFA', 'springgreen':
        '00FF7F', 'steelblue': '4682B4', 'tan': 'D2B48C', 'teal': '008080',
        'thistle': 'D8BFD8', 'tomato': 'FF6347', 'turquoise': '40E0D0',
        'violet': 'EE82EE', 'wheat': 'F5DEB3', 'white': 'FFFFFF',
        'whitesmoke': 'F5F5F5', 'yellow': 'FFFF00', 'yellowgreen': '9ACD32'}


@staticmethod
def z_score(data2d, axis=1):
    """Standarize the mean and variance of the data axis

        Parameters
        ----------
        data2d : pandas.DataFrame
            Data to normalize
        axis : int
            Which axis to normalize across. If 0, normalize across rows, if 1,
            normalize across columns.

        Returns
        -------
        normalized : pandas.DataFrame
            Noramlized data with a mean of 0 and variance of 1 across the
            specified axis.
        """
    if axis == 1:
        z_scored = data2d
    else:
        z_scored = data2d.T
    z_scored = (z_scored - z_scored.mean()) / z_scored.std()
    if axis == 1:
        return z_scored
    else:
        return z_scored.T


def _parse_placeholder(token):
    """Infers paramstyle, name from sqlparse.tokens.Name.Placeholder."""
    import re
    import sqlparse
    if not isinstance(token, sqlparse.sql.Token
        ) or token.ttype != sqlparse.tokens.Name.Placeholder:
        raise TypeError()
    if token.value == '?':
        return 'qmark', None
    matches = re.search('^:([1-9]\\d*)$', token.value)
    if matches:
        return 'numeric', int(matches.group(1)) - 1
    matches = re.search('^:([a-zA-Z]\\w*)$', token.value)
    if matches:
        return 'named', matches.group(1)
    if token.value == '%s':
        return 'format', None
    matches = re.search('%\\((\\w+)\\)s$', token.value)
    if matches:
        return 'pyformat', matches.group(1)
    raise RuntimeError('{}: invalid placeholder'.format(token.value))


def wl_lockItems(killinvalids=0):
    """ Returns (key, value) pairs of locktoken, lock.

        if 'killinvalids' is true, invalid locks (locks whose timeout
        has been exceeded) will be deleted"""


def MakeKey(key_type, name, location, *args):
    """Given a key type, make a new key of given name
    and location."""
    return key_type.MakeKey(name, location, *args)


@classmethod
def get_json_data(cls, exclude=None, minimize=True):
    """
        Get json representation of this document.

        If there is data from the server then the last fetched
        data is used otherwise the untethered version of the data
        is used to create the json document. To ensure you get
        the latest data refresh the document.

        @exclude: (list)    A list of key names to additionally
        exclude from the returned json data. This is merged
        with __exclude__ if present

        @minimize: (bool)  A boolean (True/False) value depicting
        if references should be expanded into full blown JSON objects
        or left as uids.
        """
    pass


def nameCode(node):
    """ Record pattern that happens to be a single name
		as a Clone of the original pattern.
		~ This allows independant further process. """
    pass


@classmethod
def from_evt(cls, evt):
    """
        Initializes a new instance of given class by copying all data.

        Args:
            evt: pero.KeyEvt
                Source event from which to copy the data.

        Returns:
            cls instance
                New instance of requested class.
        """
    return cls(native=evt.native, view=evt.view, control=evt.control, key=
        evt.key, char=evt.char, pressed=evt.pressed, alt_down=evt.alt_down,
        cmd_down=evt.cmd_down, ctrl_down=evt.ctrl_down, shift_down=evt.
        shift_down)


def is_meta_resource_type_mutable(context, meta_resource_type):
    """Return True if the meta_resource_type is mutable in this context."""
    if context.is_admin:
        return True
    if context.owner is None:
        return False
    if meta_resource_type.namespace:
        return meta_resource_type.namespace.owner == context.owner
    else:
        return False


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 make_square(ax):
    """ Make axes frame square instead of equal scale """
    x0, x1 = ax.get_xlim()
    y0, y1 = ax.get_ylim()
    ax.set_aspect(abs(x1 - x0) / abs(y1 - y0))


def ut(cd, dp, rhog, rhos):
    """
    Calculate terminal velocity of a single particle based on Equation 28 on
    page 80 in the Kunii and Levenspiel book [1]_ where :math:`C_D` is an
    experimentally determined drag coefficient.

    .. math:: u_t = \\left( \\frac{4 d_p\\, (\\rho_s - \\rho_g) g}{3 \\rho_g\\, C_D} \\right)^{1/2}

    Parameters
    ----------
    cd : float
        Drag coefficient [-]
    dp : float
        Diameter of particle [m]
    rhog : float
        Density of gas [kg/m^3]
    rhos : float
        Density of solid [kg/m^3]

    Returns
    -------
    ut : float
        Terminal velocity [m/s]

    Example
    -------
    >>> ut(11.6867, 0.00016, 1.2, 2600)
    0.6227

    References
    ----------
    .. [1] Daizo Kunii and Octave Levenspiel. Fluidization Engineering.
           Butterworth-Heinemann, 2nd edition, 1991.
    """
    g = 9.81
    tm1 = 4 * dp * (rhos - rhog) * g
    tm2 = 3 * rhog * cd
    ut = (tm1 / tm2) ** (1 / 2)
    return ut


def build_type_flags(compiler, build_type, vs_toolset=None):
    """
    returns flags specific to the build type (Debug, Release, etc.)
    (-s, -g, /Zi, etc.)
    """
    if not compiler or not build_type:
        return ''
    if str(compiler) == 'Visual Studio':
        if vs_toolset and 'clang' in str(vs_toolset):
            flags = {'Debug': ['-gline-tables-only', '-fno-inline', '-O0'],
                'Release': ['-O2'], 'RelWithDebInfo': ['-gline-tables-only',
                '-O2', '-fno-inline'], 'MinSizeRel': []}.get(build_type, [
                '-O2', '-Ob2'])
        else:
            flags = {'Debug': ['-Zi', '-Ob0', '-Od'], 'Release': ['-O2',
                '-Ob2'], 'RelWithDebInfo': ['-Zi', '-O2', '-Ob1'],
                'MinSizeRel': ['-O1', '-Ob1']}.get(build_type, [])
        return flags
    elif str(compiler) in ['clang', 'gcc', 'apple-clang', 'qcc']:
        flags = {'Debug': ['-g'], 'Release': ['-O3', '-s'] if str(compiler) ==
            'gcc' else ['-O3'], 'RelWithDebInfo': ['-O2', '-g'],
            'MinSizeRel': ['-Os']}.get(build_type, [])
        return flags
    elif str(compiler) == 'sun-cc':
        flags = {'Debug': ['-g'], 'Release': ['-xO3'], 'RelWithDebInfo': [
            '-xO2', '-g'], 'MinSizeRel': ['-xO2', '-xspace']}.get(build_type,
            [])
        return flags
    return ''


def lifo_replace(number_of_episodes):
    """ Return the index of the latest element added in the list """
    return number_of_episodes - 1


def assume_role(RoleArn=None, RoleSessionName=None, Policy=None,
    DurationSeconds=None, ExternalId=None, SerialNumber=None, TokenCode=None):
    """
    Returns a set of temporary security credentials (consisting of an access key ID, a secret access key, and a security token) that you can use to access AWS resources that you might not normally have access to. Typically, you use AssumeRole for cross-account access or federation. For a comparison of AssumeRole with the other APIs that produce temporary credentials, see Requesting Temporary Security Credentials and Comparing the AWS STS APIs in the IAM User Guide .
    For cross-account access, imagine that you own multiple accounts and need to access resources in each account. You could create long-term credentials in each account to access those resources. However, managing all those credentials and remembering which one can access which account can be time consuming. Instead, you can create one set of long-term credentials in one account and then use temporary security credentials to access all the other accounts by assuming roles in those accounts. For more information about roles, see IAM Roles (Delegation and Federation) in the IAM User Guide .
    For federation, you can, for example, grant single sign-on access to the AWS Management Console. If you already have an identity and authentication system in your corporate network, you don't have to recreate user identities in AWS in order to grant those user identities access to AWS. Instead, after a user has been authenticated, you call AssumeRole (and specify the role with the appropriate permissions) to get temporary security credentials for that user. With those temporary security credentials, you construct a sign-in URL that users can use to access the console. For more information, see Common Scenarios for Temporary Credentials in the IAM User Guide .
    The temporary security credentials are valid for the duration that you specified when calling AssumeRole , which can be from 900 seconds (15 minutes) to a maximum of 3600 seconds (1 hour). The default is 1 hour.
    The temporary security credentials created by AssumeRole can be used to make API calls to any AWS service with the following exception: you cannot call the STS service's GetFederationToken or GetSessionToken APIs.
    Optionally, you can pass an IAM access policy to this operation. If you choose not to pass a policy, the temporary security credentials that are returned by the operation have the permissions that are defined in the access policy of the role that is being assumed. If you pass a policy to this operation, the temporary security credentials that are returned by the operation have the permissions that are allowed by both the access policy of the role that is being assumed, * and * the policy that you pass. This gives you a way to further restrict the permissions for the resulting temporary security credentials. You cannot use the passed policy to grant permissions that are in excess of those allowed by the access policy of the role that is being assumed. For more information, see Permissions for AssumeRole, AssumeRoleWithSAML, and AssumeRoleWithWebIdentity in the IAM User Guide .
    To assume a role, your AWS account must be trusted by the role. The trust relationship is defined in the role's trust policy when the role is created. That trust policy states which accounts are allowed to delegate access to this account's role.
    The user who wants to access the role must also have permissions delegated from the role's administrator. If the user is in a different account than the role, then the user's administrator must attach a policy that allows the user to call AssumeRole on the ARN of the role in the other account. If the user is in the same account as the role, then you can either attach a policy to the user (identical to the previous different account user), or you can add the user as a principal directly in the role's trust policy
    You can optionally include multi-factor authentication (MFA) information when you call AssumeRole . This is useful for cross-account scenarios in which you want to make sure that the user who is assuming the role has been authenticated using an AWS MFA device. In that scenario, the trust policy of the role being assumed includes a condition that tests for MFA authentication; if the caller does not include valid MFA information, the request to assume the role is denied. The condition in a trust policy that tests for MFA authentication might look like the following example.
    For more information, see Configuring MFA-Protected API Access in the IAM User Guide guide.
    To use MFA with AssumeRole , you pass values for the SerialNumber and TokenCode parameters. The SerialNumber value identifies the user's hardware or virtual MFA device. The TokenCode is the time-based one-time password (TOTP) that the MFA devices produces.
    See also: AWS API Documentation

    Examples
    Expected Output:

    :example: response = client.assume_role(
        RoleArn='string',
        RoleSessionName='string',
        Policy='string',
        DurationSeconds=123,
        ExternalId='string',
        SerialNumber='string',
        TokenCode='string'
    )

    :type RoleArn: string
    :param RoleArn: [REQUIRED]
            The Amazon Resource Name (ARN) of the role to assume.

    :type RoleSessionName: string
    :param RoleSessionName: [REQUIRED]
            An identifier for the assumed role session.
            Use the role session name to uniquely identify a session when the same role is assumed by different principals or for different reasons. In cross-account scenarios, the role session name is visible to, and can be logged by the account that owns the role. The role session name is also used in the ARN of the assumed role principal. This means that subsequent cross-account API requests using the temporary security credentials will expose the role session name to the external account in their CloudTrail logs.
            The regex used to validate this parameter is a string of characters consisting of upper- and lower-case alphanumeric characters with no spaces. You can also include underscores or any of the following characters: =,.@-

    :type Policy: string
    :param Policy: An IAM policy in JSON format.
            This parameter is optional. If you pass a policy, the temporary security credentials that are returned by the operation have the permissions that are allowed by both (the intersection of) the access policy of the role that is being assumed, and the policy that you pass. This gives you a way to further restrict the permissions for the resulting temporary security credentials. You cannot use the passed policy to grant permissions that are in excess of those allowed by the access policy of the role that is being assumed. For more information, see Permissions for AssumeRole, AssumeRoleWithSAML, and AssumeRoleWithWebIdentity in the IAM User Guide .
            The format for this parameter, as described by its regex pattern, is a string of characters up to 2048 characters in length. The characters can be any ASCII character from the space character to the end of the valid character list (u0020-u00FF). It can also include the tab (u0009), linefeed (u000A), and carriage return (u000D) characters.
            Note
            The policy plain text must be 2048 bytes or shorter. However, an internal conversion compresses it into a packed binary format with a separate limit. The PackedPolicySize response element indicates by percentage how close to the upper size limit the policy is, with 100% equaling the maximum allowed size.

    :type DurationSeconds: integer
    :param DurationSeconds: The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) to 3600 seconds (1 hour). By default, the value is set to 3600 seconds.
            Note
            This is separate from the duration of a console session that you might request using the returned credentials. The request to the federation endpoint for a console sign-in token takes a SessionDuration parameter that specifies the maximum length of the console session, separately from the DurationSeconds parameter on this API. For more information, see Creating a URL that Enables Federated Users to Access the AWS Management Console in the IAM User Guide .

    :type ExternalId: string
    :param ExternalId: A unique identifier that is used by third parties when assuming roles in their customers' accounts. For each role that the third party can assume, they should instruct their customers to ensure the role's trust policy checks for the external ID that the third party generated. Each time the third party assumes the role, they should pass the customer's external ID. The external ID is useful in order to help third parties bind a role to the customer who created it. For more information about the external ID, see How to Use an External ID When Granting Access to Your AWS Resources to a Third Party in the IAM User Guide .
            The regex used to validated this parameter is a string of characters consisting of upper- and lower-case alphanumeric characters with no spaces. You can also include underscores or any of the following characters: =,.@:/-

    :type SerialNumber: string
    :param SerialNumber: The identification number of the MFA device that is associated with the user who is making the AssumeRole call. Specify this value if the trust policy of the role being assumed includes a condition that requires MFA authentication. The value is either the serial number for a hardware device (such as GAHT12345678 ) or an Amazon Resource Name (ARN) for a virtual device (such as arn:aws:iam::123456789012:mfa/user ).
            The regex used to validate this parameter is a string of characters consisting of upper- and lower-case alphanumeric characters with no spaces. You can also include underscores or any of the following characters: =,.@-

    :type TokenCode: string
    :param TokenCode: The value provided by the MFA device, if the trust policy of the role being assumed requires MFA (that is, if the policy includes a condition that tests for MFA). If the role being assumed requires MFA and if the TokenCode value is missing or expired, the AssumeRole call returns an 'access denied' error.
            The format for this parameter, as described by its regex pattern, is a sequence of six numeric digits.

    :rtype: dict
    :return: {
        'Credentials': {
            'AccessKeyId': 'string',
            'SecretAccessKey': 'string',
            'SessionToken': 'string',
            'Expiration': datetime(2015, 1, 1)
        },
        'AssumedRoleUser': {
            'AssumedRoleId': 'string',
            'Arn': 'string'
        },
        'PackedPolicySize': 123
    }

    """
    pass


def get(maybe_dict, key: str, default=None):
    """returns ``default`` if ``maybe_dict`` is not a ``dict``"""
    return maybe_dict.get(key, default) if isinstance(maybe_dict, dict
        ) else default


def ffill_mon_data(data):
    """
    author: Wang Luzhou
    update: 2018-08-14
    将数据进行月度级别的填充，并向前复制覆盖na值
    日度数据也可以转换为月度数据
    :param data:
    :return:

    Examples
    --------
    当我们调用人民币定存利率的时候，我们收到的是不规则的低频数据，
    因此需要依靠这个函数将数据转换为月度数据
    """
    return data.resample('M').last().ffill().dropna()


def _is_val_unknown(val):
    """Determine if a number value represents UNKNOWN from PyISY."""
    return val == -1 * float('inf')


def flip_chrom(chrom):
    """Return opposite of chr# or # for chromosome name."""
    if isinstance(chrom, int):
        chrom = str(chrom)
    if chrom.startswith('chr'):
        return chrom[3:]
    return 'chr' + chrom


def get_row_sql(row):
    """Function to get SQL to create column from row in PROC CONTENTS."""
    postgres_type = row['postgres_type']
    if postgres_type == 'timestamp':
        postgres_type = 'text'
    return '"' + row['name'].lower() + '" ' + postgres_type


@staticmethod
def getJavaPackage():
    """ Returns package name String. """
    return 'com.microsoft.ml.spark.cognitive.SimpleDetectAnomalies'


def cyclic_xranges(start_time, duration, cycle_time):
    """
    Where a range crosses the cycle time boundary, split into 2 ranges.

    Parameters
    ----------
    start_time: float
    duration: float
    cycle_time: float

    Returns
    -------
    list
        List of operation time ranges.
    """
    if 0 <= start_time < cycle_time:
        if start_time + duration > cycle_time:
            return [(0, duration - (cycle_time - start_time)), (start_time,
                cycle_time - start_time)]
        return [(start_time, duration)]
    raise ValueError('start time out of range')


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

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

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


def youtube_dl_available():
    """Check if `youtube-dl` is available in the installation."""
    try:
        import youtube_dl
        return True
    except ImportError:
        return False


def get_write_stats_options(**kwargs):
    """
    Get optional arguments for write_stats function.

    Retrieves the keywords supplied to the WRITE_STATS function (**KWARGS),
    and returns the values in a OPTION dicitonary. Default values are
    assigned to selected optional arguments. The function will terminate
    with an error if an unrecognized optional argument is supplied.

    INPUTS:
    **kwargs : keyword argument list

    OUTPUTS:
    option : data structure containing option values.
    option['title']     : title descriptor for data set.
    option['overwrite'] : boolean to overwrite Excel file.

    LIST OF OPTIONS:
      A title description for each dataset TITLE can be optionally
    provided as well as an overwrite option if the file name currently
    exists.

    title = title : title descriptor for each data set in data, e.g.
                   'Expt. 01.0'
    overwrite = boolean : true/false flag to overwrite Excel file

    Author: Peter A. Rochford
    Acorn Science & Innovation
    prochford@acornsi.com

    Created on Dec 10, 2016

    @author: rochfordp
    """
    from skill_metrics import check_on_off
    nargin = len(kwargs)
    option = {}
    option['title'] = ''
    option['overwrite'] = False
    if nargin == 0:
        return option
    for optname, optvalue in kwargs.items():
        optname = optname.lower()
        if not optname in option:
            raise ValueError('Unrecognized option: ' + optname)
        else:
            option[optname] = optvalue
            if optname == 'overwrite':
                option['overwrite'] = check_on_off(option['overwrite'])
    return option


def modify(target, identifier=None):
    """Return a ghost manipulator object to create, modify and
        update it following a standard API.
        """


def CDL3STARSINSOUTH(df):
    """
    Three Stars In The South
    """