def fill_in_zeros(fwd_rvs_align_list, ref_len, nt):
    """
    Generate alignment counts for every nucleotide in the reference
    :param fwd_rvs_align_list:  list of sorted forwards and reverse alignments
    :param ref_len: number of nucleotides in the reference sequence (int)
    :param nt: length of the aligned reads (int)
    :return: reference_x_axis ([0,0,...] (list(int)) - length of refseq seq,
             fwd_alignment_y_axis [2,4,5.2,6,....] (list(float)) - sense strand alignment count (positive),
             fwd_rvs_align_list [-3,-4,-5.6,...] (list(float)) - antisense strand alignment count (negative)
    """
    sorted_fwd_alignment = fwd_rvs_align_list[0]
    sorted_rvs_alignment = fwd_rvs_align_list[1]
    fwd_alignment_y_axis = [0] * ref_len
    revs_alignment_y_axis = [0] * ref_len
    reference_x_axis = list(range(0, ref_len))
    for i in sorted_fwd_alignment:
        fwd_alignment_y_axis[i[0] + int(nt / 2)] = i[1]
    for i in sorted_rvs_alignment:
        revs_alignment_y_axis[i[0] - int(nt / 2)] = i[1]
    return reference_x_axis, fwd_alignment_y_axis, revs_alignment_y_axis


def pretty_exception(err: Exception, message: str):
    """Return a pretty error message with the full path of the Exception."""
    return '{} ({}.{}: {})'.format(message, err.__module__, err.__class__.
        __name__, str(err))


def isTestModule(modulename):
    """
    Determine whether a module is a test module
    by given module name.
    """
    filename = modulename.split('.')[-1]
    return '.test.' in modulename and filename.startswith('test')


def set_user_harakiri(timeout=0):
    """Sets user level harakiri.

    :param int timeout: Seconds. ``0`` disable timer.

    :rtype: None
    """


@staticmethod
def insert_column_into_data_for_csv(data_for_csv, columns_dict_value, index):
    """
        Insert a column of data into the data returned from data_for_csv
        :param data_for_csv: rows of csv data
        :type data_for_csv: list
        :param columns_dict_value: title and data information. see extra_column_information in get_csv
        :type columns_dict_value: dict
        :param index: index of column to be inserted
        :type index: int
        """
    columns_dict_value['data'].insert(0, columns_dict_value['title'])
    for i, row in enumerate(data_for_csv):
        row.insert(index, str(columns_dict_value['data'][i]).replace(',', ';'))


def hide_view_set(unselected: bool=False):
    """Temporarily hide objects from the viewport

    :param unselected: Unselected, Hide unselected rather than selected objects
    :type unselected: bool
    """
    pass


def attr_from_element(element, attr_name, default=None):
    """Extract an attribute value from an XML element."""
    value = default
    if attr_name in element.attributes.keys():
        value = element.attributes[attr_name].value
    return value


def color_Teff_relations(V, Ks):
    """
	Estimates stellar effective temperature based on photometry.
	Args:
		V (float): V magnitude of star
		Ks (float): Ks magnitude of star.
	Returns:
		Teff (float): Star effective temperature [K].
	"""
    if V - Ks < 5.05:
        theta = 0.54042 + 0.23676 * (V - Ks) - 0.00796 * (V - Ks) ** 2
        Teff = 5040 / theta
    elif V - Ks > 5.05:
        theta = -0.4809 + 0.8009 * (V - Ks) - 0.1039 * (V - Ks
            ) ** 2 + 0.0056 * (V - Ks) ** 3
        Teff = 5040 / theta + 205.26
    return Teff


def long_syntax(name: str) ->str:
    """
    Syntax for a long option.
    :param name: The option name without any "option" syntax prefix ("--).
    """
    return '--' + name


def getTalesCondition():
    """Returns the TALES expression.
        """


def plus_or_dot(pieces):
    """Return a + if we don't already have one, else return a ."""
    if '+' in pieces.get('closest-tag', ''):
        return '.'
    return '+'


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

        :param TeamActivityCreateReportDetails val:
        :rtype: EventDetails
        """
    return cls('team_activity_create_report_details', val)


def PmfProbGreater(pmf1, pmf2):
    """Probability that a value from pmf1 is less than a value from pmf2.

    Args:
        pmf1: Pmf object
        pmf2: Pmf object

    Returns:
        float probability
    """
    total = 0
    for v1, p1 in pmf1.Items():
        for v2, p2 in pmf2.Items():
            if v1 > v2:
                total += p1 * p2
    return total


def mute_toggle():
    """Toggle muting of the nodes

    """
    pass


def copy_data_path_button(full_path: bool=False):
    """Copy the RNA data path for this property to the clipboard

    :param full_path: full_path, Copy full data path
    :type full_path: bool
    """
    pass


def repack_blockheader(dic):
    """
    Repack blockheader dic bit flag parameters into status and mode.
    """
    dic['status'] = dic['S_DATA'] * 1 + dic['S_SPEC'] * 2 + dic['S_32'
        ] * 4 + dic['S_FLOAT'] * 8 + dic['S_COMPLEX'] * 16 + dic[
        'S_HYPERCOMPLEX'] * 32 + dic['MORE_BLOCKS'] * 128 + dic['NP_CMPLX'
        ] * 256 + dic['NF_CMPLX'] * 512 + dic['NI_CMPLX'] * 1024 + dic[
        'NI2_CMPLX'] * 2048
    dic['mode'] = dic['NP_PHMODE'] * 1 + dic['NP_AVMODE'] * 2 + dic[
        'NP_PWRMODE'] * 4 + dic['NF_PHMODE'] * 16 + dic['NF_AVMODE'
        ] * 32 + dic['NF_PWRMODE'] * 64 + dic['NI_PHMODE'] * 256 + dic[
        'NI_AVMODE'] * 512 + dic['NI_PWRMODE'] * 1024 + dic['NI2_PHMODE'
        ] * 4096 + dic['NI2_AVMODE'] * 8192 + dic['NI2_PWRMODE'] * 16384
    return dic


def parse_app_params(formatted_params):
    """Parse application parameters from string"""
    from azure.servicefabric.models import ApplicationParameter
    if formatted_params is None:
        return None
    res = []
    for k in formatted_params:
        param = ApplicationParameter(key=k, value=formatted_params[k])
        res.append(param)
    return res


def register_default_patch_baseline(BaselineId=None):
    """
    Defines the default patch baseline.
    See also: AWS API Documentation

    :example: response = client.register_default_patch_baseline(
        BaselineId='string'
    )

    :type BaselineId: string
    :param BaselineId: [REQUIRED]
            The ID of the patch baseline that should be the default patch baseline.

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

    """
    pass


def _combine_baseline_window_stats(baseline_stat_list, prev_stats):
    """
    Combine per baseline window stats into a greater window stat object
    """
    result = prev_stats.copy()
    for stats in baseline_stat_list:
        result.update(stats)
    return result


def subtree_BMG(filename, scenario):
    """Write the (subgraph of) BMG."""
    with open(filename, 'w') as f:
        begin = True
        for u, v in scenario.BMG_subtrees.edges:
            if begin:
                f.write(str(u) + '\t' + str(v))
                begin = False
            else:
                f.write('\n' + str(u) + '\t' + str(v))


def report_duplicate_stats(duplicates):
    """
    Reports list of pairs of statistics that are duplicates.

    INPUTS:
    DUPLICATES : List of tuples of paired statistics that are duplicates produced
                 by the check_duplicate_stats function

    OUTPUTS:
    None

    Author: Peter A. Rochford
        Symplectic, LLC
        www.thesymplectic.com
        prochford@thesymplectic.com

    Created on Apr 23, 2017
    """
    if len(duplicates) == 0:
        return
    print('Duplicate pairs of statistics:')
    for pair in duplicates:
        print(str(pair))


def print_modules(modules):
    """Print status information for a list of modules.

    Parameters
    ----------
    modules: list(vizier.api.client.resources.ModuleResource)
        List of workflow modules
    """
    for m in modules:
        print('[' + m.identifier + '](' + m.state.upper() + ')')
        print(m.external_form)


def connectUNIX(address, factory, timeout=30, checkPID=0):
    """
        Connect a client protocol to a UNIX socket.

        @param address: a path to a unix socket on the filesystem.

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

        @param timeout: number of seconds to wait before assuming the connection
            has failed.

        @param checkPID: if True, check for a pid file to verify that a server
            is listening.  If C{address} is a Linux abstract namespace path,
            this must be C{False}.

        @return: An object which provides L{IConnector}.
        """


def exclude_sounding_level(sounding, nan_mask):
    """
    Function to exclude sounding
    """
    sounding.time = sounding.time[nan_mask]
    sounding.gpm = sounding.gpm[nan_mask]
    sounding.pressure = sounding.pressure[nan_mask]
    sounding.temperature = sounding.temperature[nan_mask]
    sounding.relativehumidity = sounding.relativehumidity[nan_mask]
    sounding.dewpoint = sounding.dewpoint[nan_mask]
    sounding.mixingratio = sounding.mixingratio[nan_mask]
    sounding.windspeed = sounding.windspeed[nan_mask]
    sounding.winddirection = sounding.winddirection[nan_mask]
    sounding.latitude = sounding.latitude[nan_mask]
    sounding.longitude = sounding.longitude[nan_mask]
    sounding.extendedVerticalSoundingSignificance = (sounding.
        extendedVerticalSoundingSignificance[nan_mask])
    return sounding


def file2url(filename):
    """
    Return a completed CodeSkulptor URL ressource from a short `filename`.

    Example given in the `CodeSkulptor file2url documentation`_:
    `file2url('assets-Quick_fox.txt')`
    returns
    `'http://codeskulptor-assets.commondatastorage.googleapis.com/assets-Quick_fox.txt'`

    Example given in the `CodeSkulptor3 urllib2-urlopen documentation`_
    (there is an error in `file2url` documentation):
    `file2url('assets_sample_text.txt')`
    returns
    `'//codeskulptor-assets.commondatastorage.googleapis.com/assets_sample_text.txt'`

    .. _`CodeSkulptor file2url documentation`: http://www.codeskulptor.org/docs.html#file2url
    .. _`CodeSkulptor3 urllib2-urlopen documentation`: https://py3.codeskulptor.org/docs.html#urllib2-urlopen

    :param filename: str

    :raise: ValueError if filename is in a incorrect format
                         (the good format is '^[a-zA-Z][a-zA-Z0-9]*[_-]')

    :return: str
    """
    from re import search
    match = search('^([a-zA-Z][a-zA-Z0-9]*)[_-]', filename)
    if match is None:
        raise ValueError("invalid filename: '{}'".format(filename))
    return 'http://codeskulptor-{}.commondatastorage.googleapis.com/{}'.format(
        match.group(1), filename)


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_string(cls, hash, **context):
    """return parsed instance from hash/configuration string

        :param \\*\\*context:
            context keywords to pass to constructor (if applicable).

        :raises ValueError: if hash is incorrectly formatted

        :returns:
            hash parsed into components,
            for formatting / calculating checksum.
        """
    raise NotImplementedError('%s must implement from_string()' % (cls,))


def isAllowedToEdit(user):
    """
        This method returns true if the given user is allowed to edit the
        contents of the tile based on which group is he member of.
        If no user is provided, it will check on the authenticated member.
        """


def export_pdf(client, file_=None, filename=None, dirname=None, height=None,
    width=None, dpi=None, use_drawing_settings=None):
    """Export a model to a PDF file.

    When use_drawing_settings is true, the Font Stroke option will be set to
    Stroke All Fonts, and the Color Depth option will be set to Grayscale.

    Args:
        client (obj):
            creopyson Client.
        `file_` (str, optional):
            Model name. Defaults is current active model.
        filename (str, optional):
            Destination file name. May also contain a path to the file.
            Defaults is the model name with the appropriate file extension,
            in Creo's working directory.
        dirname (str, optional):
            Destination directory. Defaults is Creo's current
            working directory.
        height (float, optional):
            PDF image height. Defaults is Creo default export height.
        width (float, optional):
            PDF image width. Defaults is Creo default export width.
        dpi (int, optional):
            PDF Image DPI. Default is Creo default export DPI.
        use_drawing_settings (boolean, optional):
            Whether to use special settings for exporting drawings.
            Defaut is False.

    Returns:
        dict:
            dirname (str): Directory of the output file
            filename (str): Name of the output file

    """
    data = {}
    if file_ is not None:
        data['file'] = file_
    else:
        active_file = client.file_get_active()
        if active_file is not None:
            data['file'] = active_file['file']
    if filename:
        data['filename'] = filename
    if dirname:
        data['dirname'] = dirname
    if height:
        data['height'] = height
    if width:
        data['width'] = width
    if dpi:
        data['dpi'] = dpi
    if use_drawing_settings:
        data['use_drawing_settings'] = use_drawing_settings
    return client._creoson_post('interface', 'export_pdf', data)


@property
def fclk2_mhz(cls):
    """The getter method for PL clock 2.

        This method will read the register values, do the calculation,
        and return the current clock rate.

        Returns
        -------
        float
            The returned clock rate measured in MHz.

        """
    return cls.get_pl_clk(2)


def getPossibleGenesD(rHitsL):
    """return dict containing an entry for each gene in species 0 that has
    a best reciprocal hit in all other species.  format of an entry is
    gene:(tuple of recip hit genes in other species). We do this to
    quickly eliminate things we aren't interested in.
    """
    possibleGenesD = {}
    firstRow = rHitsL[0]
    firstDict = firstRow[1]
    for gene in firstDict:
        isPossible = True
        hitsL = [firstDict[gene]]
        for colInd in range(2, len(firstRow)):
            compareDict = firstRow[colInd]
            hit = compareDict.get(gene)
            if hit == None:
                isPossible = False
                break
            else:
                hitsL.append(hit)
        if isPossible:
            possibleGenesD[gene] = tuple(hitsL)
    return possibleGenesD


def dropname(ss, li):
    """drop names from a list of strings,
    names to drop are in space delimited list
    does not change original list
    """
    newli = li[:]
    for item in ss.split():
        newli.remove(item)
    return newli


def get_group_set(policy):
    """Get the set of group names in a policy

    Parameters
    ----------
    policy : dict
        Dictionary defining the policy

    Returns
    -------
    set
        The group names in the policy
    """
    return set(policy.keys()) - {'shared'}


def print_usage():
    """print usage of the utility"""
    print('emojisearch -[swh] search_keyword')


def grant_permission_to_role_no_inherit(role_id, principal_id):
    """Assign the role to the principal without local inherit."""


def nvl(*args):
    """
    Return the first argument, or the second argument if the first was not provided. Serves
    as default value selector.
    """
    count = len(args)
    if count != 2:
        raise ValueError('invalid number of arguments')
    if args[0]:
        return args[0]
    return args[1]


def image_to_str(image):
    """
    Converts a docker image into a readable string using the first tag if available otherwise the id.

    :param image: The image to convert to string
    :type image: Image
    :return: A string representation of the given image
    :rtype: str
    """
    tags = image.tags
    if tags:
        return tags[0]
    return str(image.id)


def asymmetry(spectrogram_L, spectrogram_R):
    """
  returns (spectogram_L - spectrogram_R) / (spectogram_L + spectrogram_R)

  The asymmetry measure should be used for both polarizations recorded for a particular observation.

  On can then perform analysis such as integrating the returned spectrogram to determine if the
  asymmetry of the signals are close to zero, indicating equal signal in both polarizations, or
  close to +-1, indicating a strong asymmetry in the L or R polarization.

  spectrogram_L and spectrogram_R should be Numpy arrays.
  """
    return (spectrogram_L - spectrogram_R) / (spectrogram_L + spectrogram_R)


def create_job(api, job_name, job_class):
    """Creates a new job on the Jenkins dashboard

    :param api:
        Jenkins rest api connection to use for creation of the view`
    :param str job_name:
        the name for this new job
        This name should be unique, different from any other jobs currently
        managed by the Jenkins instance
    :param job_class:
        PyJen plugin class associated with the type of job to be created
    """
    headers = {'Content-Type': 'text/xml'}
    params = {'name': job_name}
    xml_config = job_class.template_config_xml()
    data = xml_config
    args = {'data': data, 'params': params, 'headers': headers}
    api.post(api.url + 'createItem', args)


def includeme(config):
    """
    A hook for inclusion in a Pyramid app. You can set a
    static_view_name by assigning a value to
    ``jslibs.static_view_name`` in settings Defaults to:
    ``u"jslibs_static"``

    config
        An instance of ``pyramid.config.Configurator`` from the calling app.

    Example:

    .. code-block:: python

        ...

        def main(global_config, **settings):
            ...
            config = Configurator(...)
            config.scan('myapp')
            config.include('jslibs')
            ...
    """
    view_name = config.registry.settings.get(u'jslibs.static_view_name',
        u'jslibs_static')
    config.add_static_view(view_name, u'jslibs:resources/')
    config.scan('jslibs')


def normalize_cov_type(cov_type):
    """
    Normalize the cov_type string to a canonical version

    Parameters
    ----------
    cov_type : str

    Returns
    -------
    normalized_cov_type : str
    """
    if cov_type == 'nw-panel':
        cov_type = 'hac-panel'
    if cov_type == 'nw-groupsum':
        cov_type = 'hac-groupsum'
    return cov_type


def quantity_value(halo, field, operator, value, units):
    """
    Filter based on a value in the halo quantities dictionary.

    Parameters
    ----------
    halo : Halo object
        The Halo object to be provided by the HaloCatalog.
    field : string
        The field used for the evaluation.
    operator : string
        The comparison operator to be used ("<", "<=", "==", ">=", ">", etc.)
    value : numneric
        The value to be compared against.
    units : string
        Units of the value to be compared.

    """
    if field not in halo.quantities:
        raise RuntimeError('Halo object does not contain %s quantity.' % field)
    h_value = halo.quantities[field].in_units(units).to_ndarray()
    return eval('%s %s %s' % (h_value, operator, value))


@classmethod
def create(cls, ip_address, tlp_level=0, tags=[]):
    """
        Create a new `IPSample` on the server.

        :param ip_address: The ip address as a string
        :param tlp_level: The TLP-Level
        :param tags: Tags to add to the sample.
        :return: The created sample.
        """
    return cls._create(ip_address=ip_address, tlp_level=tlp_level, tags=tags)


def success(message):
    """Print success message."""
    print('True: ' + str(message))


def dm(listoflists, criterion):
    """
Returns rows from the passed list of lists that meet the criteria in
the passed criterion expression (a string as a function of x; e.g., 'x[3]>=9'
will return all rows where the 4th column>=9 and "x[2]=='N'" will return rows
with column 2 equal to the string 'N').

Usage:   dm (listoflists, criterion)
Returns: rows from listoflists that meet the specified criterion.
"""
    function = 'filter(lambda x: ' + criterion + ',listoflists)'
    lines = eval(function)
    return lines


def sizes_repeated(all_trees, hardlinks):
    """Iterate over all file sizes for which more than two or more files of that
    size exist somewhere across all dbs.
    If hardlinks is True, consider different paths to the same file as distinct
    for this purpose.
    """
    sizes_seen_once, sizes_seen_twice = set(), set()
    for tree in all_trees:
        this_tree_sizes = list(tree.get_all_sizes())
        this_tree_sizes.sort()
        for file_sz in this_tree_sizes:
            if file_sz in sizes_seen_twice:
                continue
            elif file_sz in sizes_seen_once:
                sizes_seen_once.remove(file_sz)
                sizes_seen_twice.add(file_sz)
                yield file_sz
            else:
                files_this_sz = tree.size_to_files(file_sz)
                if len(files_this_sz) > 1 or hardlinks and len(files_this_sz
                    [0].relpaths) > 1:
                    sizes_seen_twice.add(file_sz)
                    yield file_sz
                else:
                    sizes_seen_once.add(file_sz)


def format_path(path):
    """Formats a path as a string, placing / between each component.

    @param path A path in rtctree format, as a tuple with the port name as the
                second component.

    Examples:

    >>> format_path((['localhost:30000', 'manager', 'comp0.rtc'], None))
    'localhost:30000/manager/comp0.rtc'

    >>> format_path((['localhost', 'manager', 'comp0.rtc'], 'in'))
    'localhost/manager/comp0.rtc:in'

    >>> format_path((['/', 'localhost', 'manager', 'comp0.rtc'], None))
    '/localhost/manager/comp0.rtc'

    >>> format_path((['/', 'localhost', 'manager', 'comp0.rtc'], 'in'))
    '/localhost/manager/comp0.rtc:in'

    >>> format_path((['manager', 'comp0.rtc'], None))
    'manager/comp0.rtc'

    >>> format_path((['comp0.rtc'], None))
    'comp0.rtc'

    """
    if path[1]:
        port = ':' + path[1]
    else:
        port = ''
    if type(path[0]) is str:
        return path[0] + port
    if path[0][0] == '/':
        starter = '/'
        path = path[0][1:]
    else:
        starter = ''
        path = path[0]
    return starter + '/'.join(path) + port


@classmethod
def _ignore_path(cls, path, ignore_list=None, white_list=None):
    """Returns a whether a path should be ignored or not."""
    ignore_list = ignore_list or []
    white_list = white_list or []
    return cls._matches_patterns(path, ignore_list
        ) and not cls._matches_patterns(path, white_list)


def __len__(cls):
    """ Use the number of _fields for the class length.
        """
    return len(cls._fields)


def get_exon_dict(metadata_list, strand):
    """ Get an auxiliary dictionary used for filtering

    Parameters
    ----------
    metadata_list: List(Output_metadata).

    Returns
    -------
    exon_dict: dict. (read_frame, pos_mid_1, pos_mid_2, variant_combination)
        -> List[Tuple(output_idx, pos_start, pos_end,)]
    """
    exon_dict = {}
    for metadata in metadata_list:
        coord = metadata.exons_coor
        idx = metadata.output_id
        read_frame = metadata.read_frame.read_phase
        if strand == '+':
            key = read_frame, coord.stop_v1, coord.start_v2
            if key in exon_dict:
                exon_dict[key].append((idx, coord.start_v1, coord.stop_v2))
            else:
                exon_dict[key] = [(idx, coord.start_v1, coord.stop_v2)]
        else:
            key = read_frame, coord.stop_v2, coord.start_v1
            if key in exon_dict:
                exon_dict[key].append((idx, coord.start_v2, coord.stop_v1))
            else:
                exon_dict[key] = [(idx, coord.start_v2, coord.stop_v1)]
    return exon_dict


@classmethod
def pc_str_width(cls):
    """How many characters wide can pc_covered_str be?"""
    width = 3
    if cls._precision > 0:
        width += 1 + cls._precision
    return width


def findbyname(model, name):
    """
    Utility function.
    """
    return model.objects.get(name=name)


def config_fallocate_value(reserve_value):
    """
    Returns fallocate reserve_value as an int or float.
    Returns is_percent as a boolean.
    Returns a ValueError on invalid fallocate value.
    """
    try:
        if str(reserve_value[-1:]) == '%':
            reserve_value = float(reserve_value[:-1])
            is_percent = True
        else:
            reserve_value = int(reserve_value)
            is_percent = False
    except ValueError:
        raise ValueError(
            'Error: %s is an invalid value for fallocate_reserve.' %
            reserve_value)
    return reserve_value, is_percent


def strToBlocks(toConvert, blockSize):
    """
        strToBlocks - Split a string into blocks, of max #blockSize

        @param toConvert <str> - String to convert
        @param blockSize <int> - Max number of items per block

        @return - List of the string split up into blocks.
    """
    ret = []
    i = 0
    lastI = 0
    toConvertLen = len(toConvert)
    if toConvertLen == 0:
        return ['']
    while i < toConvertLen:
        i += blockSize
        ret.append(toConvert[lastI:i])
        lastI = i
    return ret


def svg(filepath: str='', filter_glob: str='*.svg'):
    """Load a SVG file

    :param filepath: File Path, Filepath used for importing the file
    :type filepath: str
    :param filter_glob: filter_glob
    :type filter_glob: str
    """
    pass


def add(f):
    """Mark a file added."""


def _clip_points(gdf, poly):
    """Clip point geometry to the polygon extent.

    Clip an input point GeoDataFrame to the polygon extent of the poly
    parameter. Points that intersect the poly geometry are extracted with
    associated attributes and returned.

    Parameters
    ----------
    gdf : GeoDataFrame, GeoSeries
        Composed of point geometry that will be clipped to the poly.

    poly : (Multi)Polygon
        Reference geometry used to spatially clip the data.

    Returns
    -------
    GeoDataFrame
        The returned GeoDataFrame is a subset of gdf that intersects
        with poly.
    """
    spatial_index = gdf.sindex
    bbox = poly.bounds
    sidx = list(spatial_index.intersection(bbox))
    gdf_sub = gdf.iloc[sidx]
    return gdf_sub[gdf_sub.geometry.intersects(poly)]


def scroll_page_down(event):
    """
    Scroll page down. (Prefer the cursor at the top of the page, after scrolling.)
    """
    w = event.app.layout.current_window
    b = event.app.current_buffer
    if w and w.render_info:
        line_index = max(w.render_info.last_visible_line(), w.
            vertical_scroll + 1)
        w.vertical_scroll = line_index
        b.cursor_position = b.document.translate_row_col_to_index(line_index, 0
            )
        b.cursor_position += b.document.get_start_of_line_position(
            after_whitespace=True)


def name_for_scalar_relationship(base, local_cls, referred_cls, constraint):
    """ Overriding naming schemes. """
    name = referred_cls.__name__.lower() + '_ref'
    return name


def CI_calc(mean, SE, CV=1.96):
    """
    Calculate confidence interval.

    :param mean: mean of data
    :type mean : float
    :param SE: standard error of data
    :type SE : float
    :param CV: critical value
    :type CV:float
    :return: confidence interval as tuple
    """
    try:
        CI_down = mean - CV * SE
        CI_up = mean + CV * SE
        return CI_down, CI_up
    except Exception:
        return 'None', 'None'


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

        :param WriteError val:
        :rtype: RestoreError
        """
    return cls('path_write', val)


def search(graph, start_vertex):
    """
    Breadth first search algorithm

    :param graph:
    :param start_vertex:
    :return:
    """
    if start_vertex not in graph or graph[start_vertex] is None or graph[
        start_vertex] == []:
        return None
    visited, queue = set(), [start_vertex]
    while queue:
        vertex = queue.pop(0)
        if vertex not in visited:
            visited.add(vertex)
            queue.extend(graph[vertex] - visited)
    return visited


def mel2hz(mel):
    """
    Convert a value in Mels to Hertz(Hz).
    :param mel: float
    :return: float
    """
    return 700 * (10 ** (mel / 2595.0) - 1)


def _copy_model_param_hints_EXPR(target, source, *, param_hints_elements=
    None, param_hints_elastic=None):
    """
    Copy parameters (set hints) from Parameters object (source) to a model (target)
    ( Sets the attribute 'expr' in the model hint to the copied parameter name,
      use only on complete models that can be used for model fitting)

    .. warning

       This updates ``target`` in-place

    Parameters
    ----------
    target : lmfit.Model
        The model to be updated
    source : lmfit.Parameters
        The model to copy from
    params_hints_elements : list
       The names of the parameters to copy for element models
    params_hints_ellastic : list
       The names of the parameters to copy for elastic scattering model

    Note: function is part of the fix for compatibility with lmfit 0.9.13.
    Dmitri G. (08/19/2019)
    """
    if param_hints_elements is None:
        param_hints_elements = []
    if param_hints_elastic is None:
        param_hints_elastic = []
    for hint_name, hint_values in target.param_hints.items():
        if hint_name.startswith('compton'):
            continue
        if hint_name.startswith('elastic'):
            params = param_hints_elastic
        else:
            params = param_hints_elements
        for label in params:
            value = source[label].value
            if hint_name.endswith(label
                ) and label != hint_name and not hint_name.startswith('compton'
                ):
                hint_values[label] = value
                hint_values['expr'] = label


def unpack_singleton(x):
    """

    >>> unpack_singleton([[[[1]]]])
    1
    >>> unpack_singleton(np.array(np.datetime64('2000-01-01')))
    array('2000-01-01', dtype='datetime64[D]')
    """
    while isinstance(x, (list, tuple)):
        try:
            x = x[0]
        except (IndexError, TypeError, KeyError):
            break
    return x


@classmethod
def from_api_repr(cls, resource, zone):
    """Factory:  construct a change set given its API representation

        :type resource: dict
        :param resource: change set representation returned from the API.

        :type zone: :class:`google.cloud.dns.zone.ManagedZone`
        :param zone: A zone which holds zero or more change sets.

        :rtype: :class:`google.cloud.dns.changes.Changes`
        :returns: RRS parsed from ``resource``.
        """
    changes = cls(zone=zone)
    changes._set_properties(resource)
    return changes


@classmethod
def from_index(cls, index):
    """Return the Enum value at a given index."""
    return cls.ALL[index]


@classmethod
def view_attributes(cls):
    """Return a list of xpath, attribute name and value"""
    return []


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


@staticmethod
def _spiral(width, height):
    """Spiral generator.

        Parameters
        ----------
        width : `int`
            Spiral width.
        height : `int`
            Spiral height.

        Returns
        -------
        `generator` of (`int`, `int`)
            Points.
        """
    if width == 1:
        for y in range(height - 1, -1, -1):
            yield 0, y
        return
    if height == 1:
        for x in range(width - 1, -1, -1):
            yield x, 0
        return
    if width <= height:
        x0 = width // 2
        if width % 2:
            for y in range(height - 1 - x0, x0 - 1, -1):
                yield x0, y
        x0 -= 1
        y0 = x0
    else:
        y0 = height // 2
        if height % 2:
            for x in range(width - 1 - y0, y0 - 1, -1):
                yield x, y0
        y0 -= 1
        x0 = y0
    while x0 >= 0:
        x1 = width - x0 - 1
        y1 = height - y0 - 1
        for y in range(y0 + 1, y1):
            yield x0, y
        for x in range(x0, x1):
            yield x, y1
        for y in range(y1, y0, -1):
            yield x1, y
        for x in range(x1, x0 - 1, -1):
            yield x, y0
        x0 -= 1
        y0 -= 1


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 matplotlib_links():
    """ Display links to matplotlib documentation and references.
    """
    from IPython.display import display, Markdown
    import sys
    txt = """
    **Matplotlib References**
    - [Comprehensive Matplotlib OOP vs plt Blog Post](https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo)
    - [Markers](https://matplotlib.org/3.1.1/api/markers_api.html)
    - [Colors](https://matplotlib.org/3.1.0/gallery/color/named_colors.html )
    - [Text](https://matplotlib.org/3.1.0/tutorials/text/text_intro.html )
    - [Text Properties](https://matplotlib.org/3.1.1/tutorials/text/text_props.html)
    """
    if 'google.colab' in sys.modules:
        print(txt)
    else:
        display(Markdown(txt))


def on_layer_change(sender, **kwargs):
    """
    Clears the config function cache when
    any of the layers change
    """
    pass


def _get_class_repr(cls, type_, bound, keyfunc, keyfunc_name):
    """Return a class representation using the slice parameters.

        Args:
            type_: The type the class was sliced with.
            bound: The boundaries specified for the values of type_.
            keyfunc: The comparison function used to check the value
                boundaries.
            keyfunc_name: The name of keyfunc.

        Returns:
            A string representing the class.
        """
    if keyfunc is not cls._default:
        return '{}.{}[{}, {}, {}]'.format(cls.__module__, cls.__name__, cls
            ._get_fullname(type_), cls._get_bound_repr(bound), keyfunc_name)
    return '{}.{}[{}, {}]'.format(cls.__module__, cls.__name__, cls.
        _get_fullname(type_), cls._get_bound_repr(bound))


def test_success_junit_xml(test_name):
    """
    Generate JUnit XML file for a unary test suite where the test succeeded.

    @param test_name: Name of test that passed
    @type  test_name: str
    """
    return (
        """<?xml version="1.0" encoding="UTF-8"?>
<testsuite tests="1" failures="0" time="1" errors="0" name="%s">
  <testcase name="test_ran" status="run" time="1" classname="Results">
  </testcase>
</testsuite>"""
         % test_name)


def is_population_diverse(pop, diversity_lim):
    """
    Check if the population is diverse. Returns true if the share
    of identical individuals in the population is higher than
    ``diversity_lim``.

    :param pop: Population
    :param diversity_lim: float (0-1), minimum share of identical individuals
                          defining non-diverse population
    :return: boolean
    """
    identical_count = 0
    total_count = len(pop.individuals)
    genes = list()
    for ind in pop.individuals:
        genes.append(ind.genes)
    for row in genes:
        dup = genes.count(row)
        if dup > identical_count:
            identical_count = dup
    is_diverse = True
    if float(identical_count) / float(total_count) > diversity_lim:
        is_diverse = False
    return is_diverse


def _load_pretrained_tok2vec(nlp, loc):
    """Load pretrained weights for the 'token-to-vector' part of the component
    models, which is typically a CNN. See 'spacy pretrain'. Experimental.
    """
    with loc.open('rb') as file_:
        weights_data = file_.read()
    loaded = []
    for name, component in nlp.pipeline:
        if hasattr(component, 'model') and hasattr(component.model, 'tok2vec'):
            component.tok2vec.from_bytes(weights_data)
            loaded.append(name)
    return loaded


def parametersTable(parameters):
    """
    Return table of parameters as a restructured-text table.
    """
    from pyphs.misc.rst import rstTable
    header = ['Key', 'Description', 'Unit', 'Default']
    return rstTable(header, parameters)


def is_large_truck(df):
    """Return if vehicle is a large truck, per NHTSA convention."""
    yr = df['YEAR']
    body = df['BODY_TYP']
    tow_veh = df['TOW_VEH']
    return yr.between(1975, 1981) & (body.between(53, 59) | (body == 60) &
        (tow_veh == 1)) | yr.between(1982, 1990) & (body.isin([70, 71, 72,
        74, 75, 76, 78]) | (body == 79) & tow_veh.between(1, 5)) | (1991 <= yr
        ) & (body.isin([60, 61, 62, 63, 64, 66, 67, 71, 72, 78]) | (body ==
        79) & tow_veh.between(1, 4))


def identity(x):
    """The identity function

    :math:`y=x`

    Args:
        x (:py:class:`dynet.Expression`): Input expression

    Returns:
        :py:class:`dynet.Expression`: :math:`x`
    """
    return x


def evalUnits(unitStr):
    """
    Evaluate a unit string into ([numerators,...], [denominators,...])
    Examples:
        N m/s^2   =>  ([N, m], [s, s])
        A*s / V   =>  ([A, s], [V,])
    """
    pass


def has_resource(resource_name):
    """Does the package contain the named resource?"""


def cat_acc(y_true, y_pred):
    """Keras loss function for sparse_categorical_accuracy.

    :param y_true: tensor of true class labels.
    :param y_pred: class output scores from network.

    :returns: categorical accuracy.
    """
    from tensorflow.keras import backend as K
    if K.ndim(y_true) == K.ndim(y_pred):
        y_true = K.squeeze(y_true, -1)
    y_pred_labels = K.argmax(y_pred, axis=-1)
    y_pred_labels = K.cast(y_pred_labels, K.floatx())
    return K.cast(K.equal(y_true, y_pred_labels), K.floatx())


def dec2hex(n, uni=1):
    """Convert decimal number to hex string with 4 digits, and more digits if
    the number is larger.

    >>> dec2hex(12)
    '000C'
    >>> dec2hex(100)
    '0064'
    >>> dec2hex(65535)
    'FFFF'
    >>> dec2hex(100000)
    '186A0'
    """
    hexadec = '%X' % n
    if uni == 1:
        while len(hexadec) <= 3:
            hexadec = '0' + str(hexadec)
    return hexadec


def record_activity_task_heartbeat(taskToken=None, details=None):
    """
    Used by activity workers to report to the service that the  ActivityTask represented by the specified taskToken is still making progress. The worker can also (optionally) specify details of the progress, for example percent complete, using the details parameter. This action can also be used by the worker as a mechanism to check if cancellation is being requested for the activity task. If a cancellation is being attempted for the specified task, then the boolean cancelRequested flag returned by the service is set to true .
    This action resets the taskHeartbeatTimeout clock. The taskHeartbeatTimeout is specified in  RegisterActivityType .
    This action does not in itself create an event in the workflow execution history. However, if the task times out, the workflow execution history will contain a ActivityTaskTimedOut event that contains the information from the last heartbeat generated by the activity worker.
    Access Control
    You can use IAM policies to control this action's access to Amazon SWF resources as follows:
    If the caller does not have sufficient permissions to invoke the action, or the parameter values fall outside the specified constraints, the action fails. The associated event attribute's cause parameter will be set to OPERATION_NOT_PERMITTED. For details and example IAM policies, see Using IAM to Manage Access to Amazon SWF Workflows .
    See also: AWS API Documentation

    :example: response = client.record_activity_task_heartbeat(
        taskToken='string',
        details='string'
    )

    :type taskToken: string
    :param taskToken: [REQUIRED]
            The taskToken of the ActivityTask .
            Warning
            taskToken is generated by the service and should be treated as an opaque value. If the task is passed to another process, its taskToken must also be passed. This enables it to provide its progress and respond with results.

    :type details: string
    :param details: If specified, contains details about the progress of the task.

    :rtype: dict
    :return: {
        'cancelRequested': True|False
    }

    :returns:
    taskToken (string) -- [REQUIRED]
    The taskToken of the  ActivityTask .

    Warning
    taskToken is generated by the service and should be treated as an opaque value. If the task is passed to another process, its taskToken must also be passed. This enables it to provide its progress and respond with results.

    details (string) -- If specified, contains details about the progress of the task.

    """
    pass


def _construct_alphabets(outcomes):
    """
    Core construction of alphabets. No sanity checks.

    """
    from dit.utils import OrderedDict
    outcome_length = len(outcomes[0])
    alphabets = [OrderedDict() for i in range(outcome_length)]
    for outcome in outcomes:
        for i, symbol in enumerate(outcome):
            alphabets[i][symbol] = True
    alphabets = tuple(map(tuple, alphabets))
    return alphabets


def get_template_file_name(template_output, input_file_name):
    """
    Returns the template file name.

    :param template_output:
        Template output.
    :type template_output: str

    :param input_file_name:
        Input file name.
    :type input_file_name: str

    :return:
        Template file name.
    :rtype: str
    """
    if template_output == '-':
        return input_file_name
    return template_output


def _zimbraAnnotatedTaskKey(username):
    """Returns the key for zimbra tasks annotations associated with a username."""
    return 'vnccollab.theme.related_zimbra_task.{0}'.format(username)


def first_non_none_response(responses, default=None):
    """Find first non None response in a list of tuples.

    This function can be used to find the first non None response from
    handlers connected to an event.  This is useful if you are interested
    in the returned responses from event handlers. Example usage::

        print(first_non_none_response([(func1, None), (func2, 'foo'),
                                       (func3, 'bar')]))
        # This will print 'foo'

    :type responses: list of tuples
    :param responses: The responses from the ``EventHooks.emit`` method.
        This is a list of tuples, and each tuple is
        (handler, handler_response).

    :param default: If no non-None responses are found, then this default
        value will be returned.

    :return: The first non-None response in the list of tuples.

    """
    for response in responses:
        if response[1] is not None:
            return response[1]
    return default


@classmethod
def _get_ema(cls, df, column, windows):
    """ get exponential moving average

        :param df: data
        :param column: column to calculate
        :param windows: collection of window of exponential moving average
        :return: None
        """
    window = cls.get_only_one_positive_int(windows)
    column_name = '{}_{}_ema'.format(column, window)
    if len(df[column]) > 0:
        df[column_name] = df[column].ewm(ignore_na=False, span=window,
            min_periods=0, adjust=True).mean()
    else:
        df[column_name] = []


def assignment_csv_to_grades(fname):
    """Convert a CSV file from a bSpace assignment to dictionaries
       representing student grades.
    """
    raise NotImplementedError


def is_placeholder(x):
    """Returns whether `x` is a placeholder.

    # Arguments
        x: A candidate placeholder.

    # Returns
        Boolean.

    # Examples
    ```python
        >>> from keras import backend as K
        >>> input_ph = K.placeholder(shape=(2,4,5))
        >>> K.is_placeholder(input_ph)
        True
    ```
    """
    return hasattr(x, '_mxnet_placeholder') and x._mxnet_placeholder


def pretty_args(args):
    """
    Prettify the given argh namespace to be human readable

    :type args: argh.dispatching.ArghNamespace
    :return: the human readable content of the namespace
    """
    values = dict(vars(args))
    if '_functions_stack' in values:
        values['command'] = values['_functions_stack'][0].__name__
        del values['_functions_stack']
    elif 'function' in values:
        values['command'] = values['function'].__name__
        del values['function']
    return '%r' % values


def classfy_orf(orfiv, cdsiv):
    """
	define the orf type according the orf position related to cds region
	"""
    if not cdsiv:
        orftype = 'novel'
    else:
        orftype = 'annotated'
        if orfiv.end == cdsiv.end:
            orftype = 'annotated'
        elif orfiv.end < cdsiv.start:
            orftype = 'uORF'
        elif orfiv.start > cdsiv.end:
            orftype = 'dORF'
        elif orfiv.start < cdsiv.start and orfiv.end < cdsiv.end and orfiv.end > cdsiv.start:
            orftype = 'Overlap_uORF'
        elif cdsiv.start < orfiv.start < cdsiv.end and orfiv.end > cdsiv.end:
            orftype = 'Overlap_dORF'
        elif cdsiv.start < orfiv.start < orfiv.end < cdsiv.end and (cdsiv.
            end - orfiv.end) % 3 != 0:
            orftype = 'internal'
    return orftype


def coarse_pos_e(tags):
    """
	Coarse POS tags of Peykare corpus:
		N: Noun, V: Verb, AJ: Adjective, ADV: Adverb, PRO: Pronoun, DET: Determiner, P: Preposition, POSTP: Postposition, NUM: Number, CONJ: Conjunction, PUNC: Punctuation, RES: Residual, CL: Classifier, INT: Interjection

	>>> coarse_pos_e(['N','COM','SING'])
	'N'
	"""
    try:
        return list(set(tags) & {'N', 'V', 'AJ', 'ADV', 'PRO', 'DET', 'P',
            'POSTP', 'NUM', 'CONJ', 'PUNC', 'CL', 'INT', 'RES'})[0] + ('e' if
            'EZ' in tags else '')
    except:
        return 'N'


@classmethod
def from_splits(cls, train_data, test_data, val_data=None, fmt='UIR',
    rating_threshold=1.0, exclude_unknowns=False, seed=None, verbose=False,
    **kwargs):
    """Constructing evaluation method given data.

        Parameters
        ----------
        train_data: array-like
            Training data

        test_data: array-like
            Test data

        val_data: array-like, optional, default: None
            Validation data

        fmt: str, default: 'UIR'
            Format of the input data. Currently, we are supporting:

            'UIR': User, Item, Rating
            'UIRT': User, Item, Rating, Timestamp

        rating_threshold: float, default: 1.0
            Threshold to decide positive or negative preferences.

        exclude_unknowns: bool, default: False
            Whether to exclude unknown users/items in evaluation.

        seed: int, optional, default: None
            Random seed for reproduce the splitting.

        verbose: bool, default: False
            The verbosity flag.

        Returns
        -------
        method: :obj:`<cornac.eval_methods.BaseMethod>`
            Evaluation method object.

        """
    method = cls(fmt=fmt, rating_threshold=rating_threshold,
        exclude_unknowns=exclude_unknowns, seed=seed, verbose=verbose, **kwargs
        )
    return method.build(train_data=train_data, test_data=test_data,
        val_data=val_data)


@staticmethod
def get_reference(label, link_title=None):
    """
        Get RST text to create a reference to the given label
        :param label: Name of the label to link to
        :param link_title: Text for the link
        :return: String with the inline reference text
        """
    if link_title is not None:
        return ':ref:`%s <%s>`' % (link_title, label)
    else:
        return ':ref:`%s`' % label


def formatDictionary():
    """ returns the rendered bib ref
            refValues must be a dictionnary holding all values
        """


def _specialized_from_args(signatures, args, kwargs):
    """Perhaps this should be implemented in a TreeFragment in Cython code"""
    raise Exception('yet to be implemented')


def string_to_float(string, default=-1.0, **unused_kwargs):
    """Converts string to float, using default when conversion not possible."""
    try:
        return float(string)
    except ValueError:
        return default


def put_flat_sharp(step, acc):
    """get a step and its acc and return the note in tpc notation, return str"""
    ret_note = step
    ret_acc = acc
    if ret_acc > 0:
        for l in range(ret_acc):
            ret_note = ret_note + '#'
    if ret_acc < 0:
        ret_acc = abs(ret_acc)
        for l in range(ret_acc):
            ret_note = ret_note + 'b'
    return ret_note


def to_categorical(series):
    """Convert a series to categorical values."""
    series = series.astype('category')
    series = series.apply(str)
    return series


def pretty_const(value):
    """Make a constant pretty for printing in GUI"""
    words = value.split('_')
    pretty = words[0].capitalize()
    for word in words[1:]:
        pretty += ' ' + word.lower()
    return pretty


def get_saved(obj):
    """ return the saved attribue of an object. """
    p = ''
    if '_container' in obj:
        p = getattr(obj._container, 'saved', '')
        if p:
            return p


def deinterleave(data, b_cap):
    """De-interleaves the bytes from a QR code"""
    n_bufs = len(b_cap)
    bufs = []
    for _ in range(n_bufs):
        bufs.append([])
    b_i = 0
    for i in range(sum(b_cap)):
        b = data[i]
        while b_cap[b_i] <= len(bufs[b_i]):
            b_i = (b_i + 1) % n_bufs
        bufs[b_i].append(b)
        b_i = (b_i + 1) % n_bufs
    buf = ''
    for b in bufs:
        buf += ''.join(b)
    return buf


def get_paginator(operation_name=None):
    """
    Create a paginator for an operation.

    :type operation_name: string
    :param operation_name: The operation name. This is the same name
            as the method name on the client. For example, if the
            method name is create_foo, and you'd normally invoke the
            operation as client.create_foo(**kwargs), if the
            create_foo operation can be paginated, you can use the
            call client.get_paginator('create_foo').

    :rtype: L{botocore.paginate.Paginator}
    """
    pass


def calculate_term_frequencies(tokens):
    """Given a series of `tokens`, produces a sorted list of tuples in the
    format of (term frequency, token).
    """
    frequency_dict = {}
    for token in tokens:
        frequency_dict.setdefault(token, 0)
        frequency_dict[token] += 1
    tf = []
    for token, count in frequency_dict.items():
        tf.append((count, token))
    return sorted(tf, reverse=True)


@staticmethod
def format_alexa_response(speech=None, card=None, title=None):
    """Formats a response to be returned to Alex including speech and a card.

        Args:
            speech (str): The text for Alexa to say.
            card (str): Text for the card.
            title (str): Title for the card.

        Returns:
            None.

        Examples:
            >>> ADAPI.format_alexa_response(speech = "Hello World", card = "Greetings to the world", title = "Hello")

        """
    response = {'shouldEndSession': True}
    if speech is not None:
        response['outputSpeech'] = {'type': 'PlainText', 'text': speech}
    if card is not None:
        response['card'] = {'type': 'Simple', 'title': title, 'content': card}
    speech = {'version': '1.0', 'response': response, 'sessionAttributes': {}}
    return speech


def state_del():
    """Delete current boid state

    """
    pass


def _section(level, title, content, level_chr='=~'):
    """Output a ReST formatted heading followed by the content."""
    return title + '\n' + level_chr[level] * len(title) + '\n\n' + content


@classmethod
def LOG_OPTIONS(cls):
    """Return options checked for logs"""
    return (cls.LOG_OPT_COLORS, cls.LOG_OPT_TAINTS_DICT, cls.LOG_OPT_LEVEL,
        cls.LOG_OPT_FORMAT, cls.LOG_OPT_LOGGER, cls.LOG_OPT_OUTPUT)


def pop():
    """Pop the next mesasge from the queue. The messages is a dict
        conaining a key 'name' with the spider name and other keys as spider
        attributes.

        This method can return a deferred. """


def serialize(config):
    """Return the interpolated and serialized config."""
    output = ''
    for section in config.sections():
        output += '[' + section + ']\n'
        options = config.options(section)
        for option in options:
            output += option + ' = ' + config.get(section, option) + '\n'
        output += '\n'
    return output


def num2ver(ver):
    """
	Converts a string, tuple or version number into an integer. The number is supposed to have at most 4 digits::

		from waflib.Utils import num2ver
		num2ver('1.3.2') == num2ver((1,3,2)) == num2ver((1,3,2,0))

	:type ver: string or tuple of numbers
	:param ver: a version number
	"""
    if isinstance(ver, str):
        ver = tuple(ver.split('.'))
    if isinstance(ver, tuple):
        ret = 0
        for i in range(4):
            if i < len(ver):
                ret += 256 ** (3 - i) * int(ver[i])
        return ret
    return ver


def hex_color(value):
    """Accepts a hexadecimal color `value` in the format ``0xrrggbb`` and
    returns an (r, g, b) tuple where 0.0 <= r, g, b <= 1.0.
    """
    r = (value >> 8 * 2 & 255) / 255.0
    g = (value >> 8 * 1 & 255) / 255.0
    b = (value >> 8 * 0 & 255) / 255.0
    return r, g, b


def emptylanding():
    """This function returns and example landing page"""
    return """# Test Project

This is a great place to start making your documentation!

You should link to a couple of pages:

* [.](testpage1.md){step: True}
* [.](testpage2.md){step: True}

And you should definitely let people know about the {{BOMlink}} page."""


def clean_doc(*values):
    """Clean doc string."""
    for v in values:
        v = v and v.split('---')[0].strip()
        if v:
            return v
    return None


def multikey(commands, key):
    """
    Given an string key, look for the value in commands, if key
    has upper or lower case, consider that.
    """
    result = False
    final_key = key
    command = None
    if key in commands:
        command = commands.get(key)
        result = True
    elif key.lower() in commands:
        final_key = key.lower()
        command = commands.get(final_key)
        result = True
    elif key.upper() in commands:
        final_key = key.upper()
        command = commands.get(final_key)
        result = True
    return result, final_key, command


def _chunk_write(chunk, local_file, progress):
    """Write a chunk to file and update the progress bar."""
    local_file.write(chunk)
    progress.update_with_increment_value(len(chunk))


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

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

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


@classmethod
def _connexion_to_framework_response(cls, response, mimetype, extra_context
    =None):
    """ Cast ConnexionResponse to framework response class """
    return cls._build_response(mimetype=response.mimetype or mimetype,
        status_code=response.status_code, content_type=response.
        content_type, headers=response.headers, data=response.body,
        extra_context=extra_context)


@classmethod
def shot(cls, display_on=None):
    """
        (classmethod & context manager) espressomaker.Espresso.shot(display_on = None)

        Provides a context manager to run blocks of code without letting the system sleep.

        Inputs:
            display_on = Bool. Default state is None, which means default is Espresso.config() setting.

        Yields:
            Context manager.

        Example:

        >>>with Espresso.shot():
        ...    function_1()
        ...    function_2()

        """
    if display_on == None:
        cls.display_on = cls._display_on_set
    else:
        assert isinstance(display_on, bool
            ), '"display_on" setting must be bool (True/False).'
        cls.display_on = display_on
    return cls()._shot(_display_on_shot=cls.display_on)


@staticmethod
def _filter_remove_same_results(results):
    """Removes alignments that aligned to themselves.

        :param results:
        :type results:
        :return:
        :rtype:
        """
    new_results = []
    for r in results:
        query = r['query']
        subj = r['subject']
        k1 = query['origin_key']
        k2 = subj['origin_key']
        qends = query['start'], query['end']
        sends = subj['start'], subj['end']
        if k1 == k2 and qends == sends:
            continue
        else:
            new_results.append(r)
    return new_results


def _updatewrapper(wrap, origfn, unboundwrapper):
    """Copy and add some useful attributes to wrapper"""
    try:
        wrap.__name__ = origfn.__name__
    except AttributeError:
        pass
    wrap.__module__ = getattr(origfn, '__module__')
    wrap.__doc__ = getattr(origfn, '__doc__')
    wrap.__dict__.update(getattr(origfn, '__dict__', {}))
    wrap._origfunc = origfn
    wrap._unboundwrapper = unboundwrapper


def _format_servers_list_networks(networks):
    """Return a formatted string of a server's networks

    :param networks: a Server.networks field
    :rtype: a string of formatted network addresses
    """
    output = []
    for network, addresses in networks.items():
        if not addresses:
            continue
        addresses_csv = ', '.join(addresses)
        group = '%s=%s' % (network, addresses_csv)
        output.append(group)
    return '; '.join(output)


def bone_distance(obj, bone1, bone2):
    """ Returns the distance between two bones.

    """
    vec = obj.data.bones[bone1].head - obj.data.bones[bone2].head
    return vec.length


def compute_df(files):
    """
    Computes document frequency counts by processing a collection of files
    Returns a dictionary of the form {term: doc_freq}
    """
    df_dict = {}
    for file in files:
        term_seen = set()
        for line in file:
            for term in line.split():
                if term not in term_seen:
                    if term not in df_dict:
                        df_dict[term] = 0
                    df_dict[term] += 1
                    term_seen.add(term)
    return df_dict


def always_assert(condition):
    """Assert which works also when code optimization is enabled"""
    if not condition:
        raise AssertionError


def dequote(string):
    """ Removes quotes from around a string """
    if string.startswith('"') and string.endswith('"') or string.startswith("'"
        ) and string.endswith("'"):
        return string[1:-1]
    else:
        return string


def make_html_patterns():
    """Strongly inspired from idlelib.ColorDelegator.make_pat """
    tags = any('builtin', ['<', '[\\?/]?>', '(?<=<).*?(?=[ >])'])
    keywords = any('keyword', [' [\\w:-]*?(?==)'])
    string = any('string', ['".*?"'])
    comment = any('comment', ['<!--.*?-->'])
    multiline_comment_start = any('multiline_comment_start', ['<!--'])
    multiline_comment_end = any('multiline_comment_end', ['-->'])
    return '|'.join([comment, multiline_comment_start,
        multiline_comment_end, tags, keywords, string])


def as_array():
    """
        This function must return a 2D numpy.ndarray, containing the data.
        """
    return None


def merge_bbox(bbox1, bbox2):
    """
    Merge two bboxes.

    >>> merge_bbox((-10, 20, 0, 30), (30, -20, 90, 10))
    (-10, -20, 90, 30)

    """
    minx = min(bbox1[0], bbox2[0])
    miny = min(bbox1[1], bbox2[1])
    maxx = max(bbox1[2], bbox2[2])
    maxy = max(bbox1[3], bbox2[3])
    return minx, miny, maxx, maxy


def set_id_generator(generator):
    """
    Sets the global id generator function (must be a python generator function)

    :param generator: Generator function to use.
    """
    global _id_generator
    _id_generator = generator


@classmethod
def response_payload(cls, result, request_id):
    """JSON v1 response payload."""
    return {'result': result, 'error': None, 'id': request_id}


def __init__(context, request):
    """Adapts a context and a request.
        """


def p_top_statement(p):
    """top_statement : statement
                     | function_declaration_statement
                     | class_declaration_statement
                     | HALT_COMPILER LPAREN RPAREN SEMI"""
    if len(p) == 2:
        p[0] = p[1]
    else:
        pass


def parse_last_period(last):
    """
    Parse the --last value and return the time difference in seconds.
    """
    wordmap = {'hour': '1h', 'day': '1d', 'week': '1w', 'month': '1m'}
    multmap = {'h': 3600, 'd': 86400, 'w': 604800, 'm': 2592000}
    if last in wordmap:
        last = wordmap[last]
    cat = last[-1:].lower()
    if cat not in multmap:
        raise TypeError
    try:
        num = int(last[:-1])
        if num <= 0:
            raise TypeError
    except ValueError:
        raise TypeError
    diff = num * multmap[cat]
    return diff


def groupby_observation_blocks(runs):
    """ Groupby for consecutive runs of the same source"""
    runs = runs.sort_values('run_start')
    new_source = runs.fSourceName != runs.fSourceName.shift(1)
    observation_blocks = new_source.cumsum()
    return runs.groupby(observation_blocks)


def is_number(str_in):
    """
    Check whether or not a string is a number
    """
    try:
        float(str_in)
        return True
    except ValueError:
        return False


def all_continuous_indexesnot_slices(ol, value):
    """
        from elist.elist import *
        ol = [1,"a","a",2,3,"a",4,"a","a","a",5]
        all_continuous_indexesnot_slices(ol,"a")
    """
    rslt = []
    length = ol.__len__()
    cursor = 0
    begin = None
    slice = []
    while cursor < length:
        cond1 = not ol[cursor] == value
        cond2 = begin == None
        if cond1 & cond2:
            begin = cursor
            slice.append(cursor)
        elif cond1 & (not cond2):
            slice.append(cursor)
        elif (not cond1) & (not cond2):
            rslt.append(slice)
            begin = None
            slice = []
        else:
            pass
        cursor = cursor + 1
    if slice:
        rslt.append(slice)
    else:
        pass
    return rslt


@classmethod
def describe_table(cls):
    """
        Returns the result of a DescribeTable operation on this model's table
        """
    return cls._get_connection().describe_table()


def effectiveLengthFromCigar(cigarList):
    """
	Determines the actual length of the cigar sequence relative to the reference
	Ignores leading soft-clipped bases

	Args:
		cigarList: Cigar sequence
	Returns:
		length: Length of the sequence
	"""
    if cigarList is None:
        return 0
    length = 0
    leadingSoftClipped = True
    for char in cigarList:
        if char == 'S' and leadingSoftClipped:
            continue
        if char != 'I':
            length += 1
            leadingSoftClipped = False
    return length


@staticmethod
def strip_textpad(text):
    """
        Attempt to intelligently strip excess whitespace from the output of a
        curses textpad.
        """
    if text is None:
        return text
    if '\n' not in text:
        return text.rstrip()
    stack, current_line = [], ''
    for line in text.split('\n'):
        if line.endswith('  ') or not line:
            stack.append(current_line + line.rstrip())
            current_line = ''
        else:
            current_line += line
    stack.append(current_line)
    for item in stack[::-1]:
        if not item:
            stack.pop()
        else:
            break
    out = '\n'.join(stack)
    return out


def ebs_optimized_supported(instance_type):
    """
    Per http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSOptimized.html
    """
    return instance_type in ('c1.large', 'c3.xlarge', 'c3.2xlarge',
        'c3.4xlarge', 'c4.large', 'c4.xlarge', 'c4.2xlarge', 'c4.4xlarge',
        'c4.8xlarge', 'd2.xlarge', 'd2.2xlarge', 'd2.4xlarge', 'd2.8xlarge',
        'g2.2xlarge', 'i2.xlarge', 'i2.2xlarge', 'i2.4xlarge', 'm1.large',
        'm1.xlarge', 'm2.2xlarge', 'm2.4xlarge', 'm3.xlarge', 'm3.2xlarge',
        'r3.xlarge', 'r3.2xlarge', 'r3.4xlarge')


def format_size(size, threshold=1536):
    """Return file size as string from byte size.

    >>> format_size(1234)
    '1234 B'
    >>> format_size(12345678901)
    '11.50 GiB'

    """
    if size < threshold:
        return '%i B' % size
    for unit in ('KiB', 'MiB', 'GiB', 'TiB', 'PiB'):
        size /= 1024.0
        if size < threshold:
            return '%.2f %s' % (size, unit)


def calculate_chunks_count(data_size: int, chunk_size: int) ->int:
    """Returns number of chunks needed to transfer the data_size split to maximum of chunk_size blocks.

	:param data_size: total data size
	:param chunk_size: maximum size of one block"""
    return data_size // chunk_size + (1 if data_size % chunk_size > 0 else 0)


def base62_decode(string):
    """Decode a Base X encoded string into the number

    Arguments:
    - `string`: The encoded string
    - `alphabet`: The alphabet to use for encoding
    Stolen from: http://stackoverflow.com/a/1119769/1144479
    """
    alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    base = len(alphabet)
    strlen = len(string)
    num = 0
    idx = 0
    for char in string:
        power = strlen - (idx + 1)
        num += alphabet.index(char) * base ** power
        idx += 1
    return int(num)


def n_primes(n):
    """Generates and returns the nth prime number"""
    from math import sqrt
    primes = [2, 3]
    test_value = 5
    while len(primes) < n:
        isPrime = True
        for known_prime in primes:
            if known_prime > sqrt(test_value):
                break
            elif test_value % known_prime == 0:
                isPrime = False
        if isPrime:
            primes.append(test_value)
        test_value += 2
    return primes


def setRenderedValue(value):
    """Set the value to be rendered by the widget.

        Calling this method will override any values provided by the user.

        For input widgets (`IInputWidget` implementations), calling
        this sets the value that will be rendered even if there is
        already user input.

        """


def ajuda(*args):
    """
    Fornece ajuda sobre módulos, palavras chaves ou tôpicos do Pytuguês.

    Pode ser chamada sem argumentos como `ajuda()` para uma ajuda geral sobre
    como utilizar o Pytuguês ou pode ser chamada com um argumento (como em
    `ajuda(binário)` para obter ajuda sobre um tópico específico.
    """
    if not args:
        return help()
    else:
        return help(*args)


def rwrap(some_string):
    """
    Returns red text
    """
    return '\x1b[91m%s\x1b[0m' % some_string


def valid_sample(sample):
    """Check whether a sample is valid.

        sample: sample to be checked

    """
    return sample is not None and isinstance(sample, dict) and len(list(
        sample.keys())) > 0 and not sample.get('__bad__', False)


@classmethod
def is_task(cls, obj):
    """
        Returns true is an object is a build task.
        """
    return isinstance(obj, cls)


@classmethod
def _stripe_object_to_refunds(cls, target_cls, data, charge):
    """
        Retrieves Refunds for a charge
        :param target_cls: The target class to instantiate per refund
        :type target_cls: Type[djstripe.models.Refund]
        :param data: The data dictionary received from the Stripe API.
        :type data: dict
        :param charge: The charge object that refunds are for.
        :type charge: djstripe.models.Refund
        :return:
        """
    refunds = data.get('refunds')
    if not refunds:
        return []
    refund_objs = []
    for refund_data in refunds.auto_paging_iter():
        item, _ = target_cls._get_or_create_from_stripe_object(refund_data,
            refetch=False)
        refund_objs.append(item)
    return refund_objs


def prop_get_z(wf):
    """Return the distance from the initialization of the wavefront to the
    current surface in meters.

    Parameters
    ----------
    wf : obj
        WaveFront class object

    Returns
    -------
    float
        Distance from intialization of the wavefront to the current surface

    """
    return wf.z


@staticmethod
def max(a, b):
    """ Generates the expression for max(a, b)

        :param a: one value. Can be a number or a variable
        :type a: str
        :param b: the other value
        :type b: str
        :return: max number
        :rtype: str
        """
    return '({a}>{b})?{a}:{b}'.format(a=a, b=b)


def _add_form_fields(obj, lines):
    """Improve the documentation of a Django Form class.

    This highlights the available fields in the form.
    """
    lines.append('**Form fields:**')
    lines.append('')
    for name, field in obj.base_fields.items():
        field_type = '{}.{}'.format(field.__class__.__module__, field.
            __class__.__name__)
        tpl = '* ``{name}``: {label} (:class:`~{field_type}`)'
        lines.append(tpl.format(name=name, field=field, label=field.label or
            name.replace('_', ' ').title(), field_type=field_type))


def set_edit_mode(request, state):
    """
    Enable the edit mode; placeholders and plugins will be wrapped in a ``<div>`` that exposes metadata for frontend editing.
    """
    setattr(request, '_fluent_contents_edit_mode', bool(state))


def get_list_in_chunks(input_list, num_items_in_each_chunk=5000):
    """Yield smaller bits of a list"""
    for index in range(0, len(input_list), num_items_in_each_chunk):
        yield input_list[index:index + num_items_in_each_chunk]


def _setAxLabelsFontsize(ax, fontsize):
    """Change the font size of the axis labels
    """
    for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] + ax.
        get_xticklabels() + ax.get_yticklabels()):
        item.set_fontsize(fontsize)


def escape(identifier, ansi_quotes, should_quote):
    """
    Escape identifiers.

    ANSI uses single quotes, but many databases use back quotes.

    """
    if not should_quote(identifier):
        return identifier
    quote = '"' if ansi_quotes else '`'
    identifier = identifier.replace(quote, 2 * quote)
    return '{0}{1}{2}'.format(quote, identifier, quote)


def generate_source_dict_from_brain(brain):
    """ Reads the relevant information from the brain and generates a dict
            based on thos informations. This dict is later converted to JSON.
        """


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 render_git_describe_long(pieces):
    """TAG-DISTANCE-gHEX[-dirty].

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

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


def _mergeGraph(A, B):
    """Return the merged graph of A and B."""
    for key, val in B.nodes.items():
        if A.has_node(key):
            A.nodes[key]['count'] += val['count']
            for i in range(len(val['pos'])):
                if val['pos'][i] not in A.nodes[key]['pos']:
                    A.nodes[key]['pos'].append(val['pos'][i])
                    A.nodes[key]['lpos'].append(val['lpos'][i])
                    A.nodes[key]['func'].append(val['func'][i])
                    A.nodes[key]['surface'].append(val['surface'][i])
                    A.nodes[key]['yomi'].append(val['yomi'][i])
                    if 'depth' in A.nodes[key]:
                        A.nodes[key]['depth'].append(val['depth'][i])
        else:
            A.add_node(key, **val)
    for key, val in B.edges.items():
        if A.has_edge(*key):
            A.edges[key[0], key[1]]['weight'] += val['weight']
        else:
            A.add_edge(*key, **val)
    return A


@staticmethod
def p_alignment_float32_ieee(p):
    """alignment_float32_ieee : ALIGNMENT_FLOAT32_IEEE NUMERIC"""
    p[0] = p[2]


def unset_utc():
    """Set all datetimes to system time.

    The ``utc`` parameter of other methods will be used.

    This can be changed by calling ``set_utc()``.

    .. versionadded:: 0.1.0
    """
    global _FORCE_UTC
    _FORCE_UTC = False


def roundy(x, prec=1, base=0.5):
    """Round a number to fixed decimal places."""
    if not x:
        x = 0
    return round(base * round(float(x) / base), prec)


def _remove_dups(L):
    """
    Remove duplicates AND preserve the original order of the elements.

    The set class is not guaranteed to do this.
    """
    seen_before = set([])
    L2 = []
    for i in L:
        if i not in seen_before:
            seen_before.add(i)
            L2.append(i)
    return L2


def __contains__(path):
    """Whether a path exists in the manifest."""


@classmethod
def get_label(cls):
    """
        Returns the label for this edge.

        :rtype: str

        """
    return cls._type_name(cls.label)


def bam_iterator(bam):
    """Returns an iterator for the given SAM/BAM file (must be query-sorted).
    In each call, the alignments of a single read are yielded as a 3-tuple: (list of primary pysam.AlignedSegment, list of supplementary pysam.AlignedSegment, list of secondary pysam.AlignedSegment)."""
    alignments = bam.fetch(until_eof=True)
    current_aln = next(alignments)
    current_read_name = current_aln.query_name
    current_prim = []
    current_suppl = []
    current_sec = []
    if current_aln.is_secondary:
        current_sec.append(current_aln)
    elif current_aln.is_supplementary:
        current_suppl.append(current_aln)
    else:
        current_prim.append(current_aln)
    while True:
        try:
            next_aln = next(alignments)
            next_read_name = next_aln.query_name
            if next_read_name != current_read_name:
                yield current_prim, current_suppl, current_sec
                current_read_name = next_read_name
                current_prim = []
                current_suppl = []
                current_sec = []
            if next_aln.is_secondary:
                current_sec.append(next_aln)
            elif next_aln.is_supplementary:
                current_suppl.append(next_aln)
            else:
                current_prim.append(next_aln)
        except StopIteration:
            break
    yield current_prim, current_suppl, current_sec


@classmethod
def get_url_django_name(cls):
    """Return the django url name."""
    if cls.url_django_name:
        return cls.url_django_name
    else:
        return cls.__name__


def reverse_remove_duplicate_renditions(*args, **kwargs):
    """This is a no-op. The migration removes duplicates, we cannot recreate those duplicates."""
    pass


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


def beta_to_kelvin(beta):
    """
    Convert :math:`\\beta` to Kelvin.

    Parameters
    ----------
    beta: float
        Inversion temperature.

    Returns
    -------
    T: float
        Temperature (K).
    """
    kb = 8.6173303e-05
    ev = 1.0 / float(beta)
    T = ev / kb
    return T


def p_named_signal_group(p):
    """named_signal_value : string '(' numbers ')'"""
    p[0] = p[1], tuple(p[3])


@staticmethod
def _getgrnam(name):
    """Wrapper function to protect against `grp` not being available
        on Windows
        """
    import grp
    return grp.getgrnam(name)


@staticmethod
def _filter_tests(function, tests):
    """Filter tests with condition 'function(test_node) == True'"""
    result = {}
    for module_name in tests:
        for test_name in tests[module_name]['tests']:
            if function(module_name, test_name, tests):
                if module_name not in result:
                    result[module_name] = {'ast': tests[module_name]['ast'],
                        'source_path': tests[module_name]['source_path'],
                        'import_valid': tests[module_name]['import_valid'],
                        'tests': {}}
                result[module_name]['tests'][test_name] = tests[module_name][
                    'tests'][test_name]
    return result


def create_user_profile(IamUserArn=None, SshUsername=None, SshPublicKey=
    None, AllowSelfManagement=None):
    """
    Creates a new user profile.
    See also: AWS API Documentation

    :example: response = client.create_user_profile(
        IamUserArn='string',
        SshUsername='string',
        SshPublicKey='string',
        AllowSelfManagement=True|False
    )

    :type IamUserArn: string
    :param IamUserArn: [REQUIRED]
            The user's IAM ARN; this can also be a federated user's ARN.

    :type SshUsername: string
    :param SshUsername: The user's SSH user name. The allowable characters are [a-z], [A-Z], [0-9], '-', and '_'. If the specified name includes other punctuation marks, AWS OpsWorks Stacks removes them. For example, my.name will be changed to myname . If you do not specify an SSH user name, AWS OpsWorks Stacks generates one from the IAM user name.

    :type SshPublicKey: string
    :param SshPublicKey: The user's public SSH key.

    :type AllowSelfManagement: boolean
    :param AllowSelfManagement: Whether users can specify their own SSH public key through the My Settings page. For more information, see Setting an IAM User's Public SSH Key .

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

    """
    pass


def consume(the_plot):
    """Obtain messages logged by game entities since the last call to `consume`.

  This function is meant to be called by "game engine users" (user interfaces,
  environment interfaces, etc.) to obtain the latest set of log messages
  emitted by the game entities. These systems can then dispose of these messages
  in whatever manner is the most appropriate.

  Args:
    the_plot: the pycolab game's `Plot` object.

  Returns:
    The list of all log messages supplied by the `log` method since the last
    time `consume` was called (or ever, if `consume` has never been called).
  """
    messages = the_plot.setdefault('log_messages', [])
    our_messages = messages[:]
    del messages[:]
    return our_messages


def find_hosted_zone(client, domain_name):
    """Find a hosted zone matching domain"""
    paginator = client.get_paginator('list_hosted_zones')
    for page in paginator.paginate():
        for zone in page['HostedZones']:
            if domain_name.endswith(zone['Name'][:-1]):
                return zone
    return None


def remove_ids(g, matches):
    """Given the output of ``match_ids(_parallel)``, returns a list of edges to add,
    and vertices to remove."""
    etab = dict()
    rem = []
    for m in matches:
        rem.append(m[0])
        e = (m[1], m[2]) if m[1] < m[2] else (m[2], m[1])
        if not e in etab:
            etab[e] = [0, 0]
        etab[e][m[3] - 1] += 1
    return etab, rem, [], False


@staticmethod
def _pythonize(value: str):
    """Transform value into something the python interpreter can parse.

        Transform value into pvalue such that:

            value = ast.literal_eval(pvalue)

        This isn't quite true, because if there are no spaces or quote marks in value, then

            pvalue = value
        """
    single_quote = "'"
    double_quote = '"'
    pvalue = value
    if single_quote in value and double_quote in value:
        value = value.replace(single_quote, '\\' + single_quote)
        pvalue = "'{}'".format(value)
    elif single_quote in value:
        pvalue = '"{}"'.format(value)
    elif double_quote in value:
        pvalue = "'{}'".format(value)
    elif ' ' in value:
        pvalue = "'{}'".format(value)
    return pvalue


def set_value_at_index(bitmask, index, value):
    """
    Set a bit value onto an integer bitmask

    eg. set bits 2 and 4 to True
    >>> mask = 0
    >>> mask = set_value_at_index(mask, 2, True)
    >>> mask = set_value_at_index(mask, 4, True)
    >>> print(bin(mask))
    0b10100
    >>> mask = set_value_at_index(mask, 2, False)
    >>> print(bin(mask))
    0b10000

    :param bitmask: existing int bitmask to alter
    :type bitmask: int
    :type index: int
    :type value: bool
    """
    bit_val = 2 ** index
    if value:
        bitmask |= bit_val
    else:
        bitmask &= ~bit_val
    return bitmask


@staticmethod
def format_delimited_rule(rule):
    """Support to build delimitedvalues-based-rules in report

        Parameters
        ----------
        rule : str
            A whip specification rule.
        """
    return '{}_delimitedvalue'.format(rule)


def _test():
    """perform internal tests."""
    import doctest
    doctest.testmod()


def _print_fasta(chimerics, stream):
    """Write chimeric sequences in fasta format."""
    for key, value in chimerics.items():
        chimeric = value[0]
        if chimeric:
            stream.write('>{}\n{}\n'.format(key, chimeric))


def bitwise_contains(a: int, b: int) ->bool:
    """Return True if any bit of mask `b` is contained in `a`."""
    return bool(a & b)


@classmethod
def is_valid(cls, value):
    """Check whether a value is one of the pre-defined values.

        Args:
            value: the width.

        Returns:
            True if the value equals one of the fixed widths, False otherwise.

        """
    return value in cls._values


def mean(enum, count=0, sum=0):
    """returns the mean of enumeration enum,
       count and sum are initial counts and the initial sum.
       This works for enumerations, even where len() is not defined"""
    for e in enum:
        count += 1
        sum += e
    return sum / count


@classmethod
def R(cls, i, location=None):
    """Construct a register token."""
    return cls(cls.REGISTER, i, location)


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


def _create_json(name, connection, description, locked):
    """
    Create a JSON to be used for the REST API call
    """
    json = {'connection': connection, 'type': 'isamruntime', 'name': name,
        'description': description, 'locked': locked}
    return json


def _update_surface_in_ecc_orbits(system, orbital_position, new_geometry_test):
    """
    Function decides how to update surface properties with respect to the degree of change
    in surface geometry given by new_geometry test.
    If true, only points and normals are recalculated, otherwise surface is calculated from scratch.

    :param system: elisa.binary_system.container.OrbitalPositionContainer
    :param orbital_position:  OrbitalPosition list
    :param new_geometry_test: bool; test that will decide, how the following phase will be calculated
    :return: elisa.binary_system.system.BinarySystem; instance with updated geometry
    """
    if new_geometry_test:
        system.build(components_distance=orbital_position.distance)
    else:
        system.build_mesh(component='all', components_distance=
            orbital_position.distance)
        system.build_surface_areas(component='all')
        system.build_faces_orientation(component='all', components_distance
            =orbital_position.distance)
    return system


def _apply_modifications(degrees, modifications):
    """Apply scale degree modifications to a scale degree dictionary."""
    for mod_fn, degree, alter in modifications:
        mod_fn(degrees, degree, alter)


def _indent(stream, indent, *msgs):
    """ write a message to a text stream, with indentation. Also ensures that
    the output encoding of the messages is safe for writing. """
    for x in range(0, indent):
        stream.write('  ')
    for x in msgs:
        stream.write(x.encode('ascii', 'backslashreplace').decode('ascii'))
    stream.write('\n')


def dfa_word_acceptance(dfa: dict, word: list) ->bool:
    """ Checks if a given **word** is accepted by a DFA,
    returning True/false.

    The word w is accepted by a DFA if DFA has an accepting run
    on w. Since A is deterministic,
    :math:`w ∈ L(A)` if and only if :math:`ρ(s_0 , w) ∈ F` .

    :param dict dfa: input DFA;
    :param list word: list of actions ∈ dfa['alphabet'].
    :return: *(bool)*, True if the word is accepted, False in the
             other case.
    """
    current_state = dfa['initial_state']
    for action in word:
        if (current_state, action) in dfa['transitions']:
            current_state = dfa['transitions'][current_state, action]
        else:
            return False
    if current_state in dfa['accepting_states']:
        return True
    else:
        return False


@classmethod
def with_(cls, *relations):
    """
        Begin querying a model with eager loading

        :param relations: The relations to eager load
        :type relations: str or list

        :return: A Builder instance
        :rtype: Builder
        """
    instance = cls()
    return instance.new_query().with_(*relations)


def small_sample(df):
    """output a random sample data csv file for smaller sample analysis"""
    df_s = df.groupby(['CountryID', 'SchoolID']).apply(lambda x: x.sample(
        frac=0.5, random_state=999)).reset_index(drop=True)
    return df_s.to_csv('sample data.csv', encoding='utf-8')


@classmethod
def parse(cls, subheader):
    """ Parse subheader from given string.

        :return DataDescriptionSubheader:

        """
    key, value = subheader.split(': ', 1)
    return cls(key, value)


@staticmethod
def _rename_metric_type(metric):
    """
        FIXME DROPME in 1.0:

        API documentation was illegitimately promoting usage of `metric_type` parameter
        instead of `type`.
        To be consistent and avoid 'backward incompatibilities', properly rename this parameter.
        """
    if 'metric_type' in metric:
        metric['type'] = metric.pop('metric_type')


def do():
    """ Do something with photo
        """


def is_hsts_entire_domain(domain):
    """
    Whether a domain's ROOT endpoint includes all subdomains.
    """
    https = domain.https
    return https.hsts_all_subdomains


@classmethod
def definedThreatEntryTypes(cls):
    """
        Class method to get all threat entry types defined in API v4.

        @return list of all defined threat entry types
        @rtype list of str
        """
    return ['THREAT_ENTRY_TYPE_UNSPECIFIED', 'URL', 'EXECUTABLE']


@classmethod
def list(cls, context, limit=None, marker=None, sort_key=None, sort_dir=None):
    """Return a list of VolumeTarget objects.

        :param context: security context
        :param limit: maximum number of resources to return in a single result
        :param marker: pagination marker for large data sets
        :param sort_key: column to sort results by
        :param sort_dir: direction to sort. "asc" or "desc".
        :returns: a list of :class:`VolumeTarget` objects
        :raises: InvalidParameterValue if sort_key does not exist
        """
    db_targets = cls.dbapi.get_volume_target_list(limit=limit, marker=
        marker, sort_key=sort_key, sort_dir=sort_dir)
    return cls._from_db_object_list(context, db_targets)


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

        :param PaperDesktopPolicyChangedDetails val:
        :rtype: EventDetails
        """
    return cls('paper_desktop_policy_changed_details', val)


def iterator(url, downloader='default', *args, **kwargs):
    """Parse an App and return a row iterator"""
    from rowgenerators import parse_app_url
    u = parse_app_url(url, downloader=downloader, **kwargs)
    r = u.get_resource()
    t = r.get_target()
    g = t.generator
    return iter(g)


def _check_api_rate_exceeded(r, exit_loop=False):
    """
    Internal function for catching api exceeded error.

    Parameters
    ----------
    r: dict
        response.json() returned from the Petfinder API.

    Returns
    -------
    None
        raises an error and terminates the program

    """
    if isinstance(r, dict):
        if 'status' in r.keys() and r['status'] == 429:
            if exit_loop:
                print(r['title'])
                return True
            else:
                raise RuntimeError(r['title'])


def unpack_hyperopt_vals(vals):
    """
    Unpack values from a hyperopt return dictionary where values are wrapped in a list.
    :param vals: dict
    :return: dict
        copy of the dictionary with unpacked values
    """
    assert isinstance(vals, dict), 'Parameter must be given as dict.'
    ret = {}
    for k, v in list(vals.items()):
        try:
            ret[k] = v[0]
        except (TypeError, IndexError):
            ret[k] = v
    return ret


def finalizeCommands(model_object, commands, desc=None):
    """Used to enable interaction with the model but not push
    commands to the undostack.  In practice:

    1. Call a bunch of commands and don't push them to the undostack AKA:
        cmd.redo()
    2. call finalizeCommands() to push the cummulative change to the stack

    This assumes that the UndoCommands provided this function respresent
    a transition from the initial state to the final state

    Note:
        UndoCommands need to implement specialUndo (e.g. just call normal undo.)
    """
    for c in commands:
        c.specialUndo()
    model_object.undoStack().beginMacro(desc)
    for c in commands:
        model_object.undoStack().push(c)
    model_object.undoStack().endMacro()


def get_overlap(a, b):
    """
    report overlap of coordinates
    """
    return max(0, min(a[1], b[1]) - max(a[0], b[0]))


def get_and_update_or_create(model, unique, update):
    """
    Given a model, a dictionary of lookup arguments, and a dictionary of update
    arguments, this convenience function gets an object and updates it in the
    database if necessary.

    Returns a tuple (object, int) where int is 0 if the object was not updated,
    1 if the object was created, and 2 if the object was updated in the
    database.

    >>> resp = get_and_update_or_create(User, {'username': 'example'}, {'email': 'example@example.com'})
    >>> resp
    (<User: example>, 1)
    >>> resp[0].email
    'example@example.com'
    >>> resp = get_and_update_or_create(User, {'username': 'example'}, {'email': 'example@example.com'})
    >>> resp
    (<User: example>, 0)
    >>> resp[0].email
    'example@example.com'
    >>> resp = get_and_update_or_create(User, {'username': 'example'}, {'email': 'another@example.com'})
    >>> resp
    (<User: example>, 2)
    >>> resp[0].email
    'another@example.com'
    """
    obj, created = model.objects.get_or_create(dict(unique, default=update))
    if created:
        return obj, 1
    modified = False
    for name, val in update.iteritems():
        if getattr(obj, name) != val:
            modified = True
            setattr(obj, name, val)
    if modified:
        obj.save(force_update=True)
        return obj, 2
    return obj, 0


def is_valid_port(port):
    """Check if given port is valid"""
    return 0 <= int(port) <= 65535


def find_first_peak(corr):
    """
    Find row and column indices of the first correlation peak.

    Parameters
    ----------
    corr : np.ndarray
        the correlation map

    Returns
    -------
    i : int
        the row index of the correlation peak

    j : int
        the column index of the correlation peak

    corr_max1 : int
        the value of the correlation peak

    Original code from openPIV pyprocess

    """
    ind = corr.argmax()
    s = corr.shape[1]
    i = ind // s
    j = ind % s
    return i, j, corr.max()


def toordinal():
    """Return the proleptic Gregorian ordinal of the date.

        The same as self.date().toordinal().
        """


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


@staticmethod
def calc_speakman_co2(nd, no, kd, ko, pop_avg_rdil):
    """Calculate CO2 production in mol/hr using the equation of Speakman eqn 17.41 (1997)
                from dilution spaces and isotope turnover rates.
           :param nd: deuterium dilution space in mol
           :param no: oxygen dilution space in mol
           :param kd: deuterium turnover rate in 1/hr
           :param ko: oxygen turnover rate in 1/hr
           :return co2prod: co2 production rate in mol/hr
        """
    n = (no + nd / pop_avg_rdil) / 2
    co2_prod = n / 2.078 * (ko - kd * pop_avg_rdil
        ) - 0.0062 * n * kd * pop_avg_rdil
    return co2_prod


@classmethod
def create(cls, model=None, parent=None, autoCommit=True, defaults=None,
    title=''):
    """
        Prompts the user to create a new record.

        :param      model  | <subclass of orb.Table>
                    parent | <QWidget>
                    autoCommit | <bool>
                    defaults | <dict> || None

        :return     <orb.Table> || None
        """
    if model is None:
        model = cls.tableType()
        if model is None:
            return None
    dlg = cls.getDialog('create', parent)
    if not title:
        title = 'Create {0}'.format(type(model).__name__)
    dlg.setWindowTitle(title)
    widget = dlg._mainwidget
    widget.setRecord(model())
    widget.setAutoCommitOnSave(autoCommit)
    if defaults:
        widget.loadValues(defaults)
    else:
        widget.loadValues({})
    result = dlg.exec_()
    if result or widget.saveSignalBlocked():
        output = widget.record()
    else:
        output = None
    return output


def _format_time(total_seconds):
    """Format a time interval in seconds as a colon-delimited string [h:]m:s"""
    total_mins, seconds = divmod(int(total_seconds), 60)
    hours, mins = divmod(total_mins, 60)
    if hours != 0:
        return f'{hours:d}:{mins:02d}:{seconds:02d}'
    else:
        return f'{mins:02d}:{seconds:02d}'


def structured_score(row):
    """Function to be called after the mail_segmenting function as it requires a "structured_body" column.
    Return a set with the different parts tag (ex : "HELLO", "BODY", etc.) present, or "EMPTY" if any,  in the last
    email of the conversation .

    Parameters
    ----------
    row : pd.Series
        Row of an email DataFrame

    Returns
    -------
    tags_set : a set of the different parts tag found in the email or EMPTY if None
    length of tags_set : count of parts tag found in the email

    """
    parts_type = []
    for part in row['structured_body'][0]['structured_text']['text']:
        parts_type.append(part['tags'])
    tags_set = set(parts_type)
    return tags_set or set(['EMPTY']), len(tags_set)


def _parent(entity):
    """Return the first encountered parent which is an ArchiveUnit"""
    while entity.cw_etype not in ('SEDAArchiveTransfer', 'SEDAArchiveUnit',
        'SEDABinaryDataObject', 'SEDAPhysicalDataObject'):
        entity = entity.cw_adapt_to('IContained').parent
    return entity


def p_InterfaceMembers(p):
    """InterfaceMembers : InterfaceMembers ExtendedAttributeList InterfaceMember"""
    p[3].extended_attributes = p[2]
    p[0] = p[1] + [p[3]]


def edittranslation_init():
    """Edit i18n in current language for the active button

    """
    pass


@staticmethod
def _getattr_property(obj, attrname):
    """Property-aware getattr to use in object finding.

        If attrname represents a property, return it unevaluated (in case it has
        side effects or raises an error.

        """
    if not isinstance(obj, type):
        try:
            attr = getattr(type(obj), attrname)
        except AttributeError:
            pass
        else:
            if isinstance(attr, property):
                return attr
    return getattr(obj, attrname)


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


@staticmethod
def handle(_event, _event_data, listener_data):
    """
        Handles a SpawnJobMessage received event.

        :param * _event: Not used.
        :param * _event_data: Not used.
        :param (enarksh.spawner.Spawner.Spawner,int) listener_data: The spawner and the PID of the processes of the job.
        """
    del _event, _event_data
    spawner, pid = listener_data
    spawner.remove_job_handler(pid)


@staticmethod
def extension():
    """Return archive extension."""
    return '.tar.gz'