def segmentspan(revlog, revs):
    """Get the byte span of a segment of revisions

    revs is a sorted array of revision numbers

    >>> revlog = _testrevlog([
    ...  5,  #0
    ...  10, #1
    ...  12, #2
    ...  12, #3 (empty)
    ...  17, #4
    ... ])

    >>> segmentspan(revlog, [0, 1, 2, 3, 4])
    17
    >>> segmentspan(revlog, [0, 4])
    17
    >>> segmentspan(revlog, [3, 4])
    5
    >>> segmentspan(revlog, [1, 2, 3,])
    7
    >>> segmentspan(revlog, [1, 3])
    7
    """
    if not revs:
        return 0
    end = revlog.end(revs[-1])
    return end - revlog.start(revs[0])


def gain_com(exp, num, value):
    """Change the pmt gain in a job.

    Return a list with parts for the cam command.
    """
    return [('cmd', 'adjust'), ('tar', 'pmt'), ('num', str(num)), ('exp',
        str(exp)), ('prop', 'gain'), ('value', str(value))]


def splitkeyurl(url):
    """
       Splits a Send url into key, urlid and 'prefix' for the Send server
       Should handle any hostname, but will brake on key & id length changes
    """
    key = url[-22:]
    urlid = url[-34:-24]
    service = url[:-43]
    return service, urlid, key


def is_dynamic_XX_field(field_id, cubes_lookup):
    """
    GA has fields that are documented using a placeholder of `XX`, where
    the `XX` is replaced with a number in practice.

    Some of these are standard fields that are generated based on other
    artifacts defined for the profile (e.g., goals). These must be handled
    differently from other fields as well, since the IDs must be
    discovered through their own means.

    If the cubes_lookup map DOES have the `XX` version in it, this
    function assumes that the field is dynamically discovered.
    """
    return 'XX' in field_id and field_id in cubes_lookup and field_id not in [
        'ga:metricXX', 'ga:dimensionXX']


def get_tracklist(response, tracktype='recenttracks'):
    """Read JSON page and get a list of tracks and their info.

    :param response: Response from a request to the server (JSON page of the server)
    :param tracktype: Type of information to download from the server, can be either 'recentracks' or 'lovedtracks'
    :return: list of tracks in the page
    """
    tracklist = response[tracktype]['track']
    return tracklist


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 multi_pMethod(args):
    """
    Runs the pMethod function and returns the results plus the id of the node
    """
    id, pMethod, dataset1, dataset2 = args
    (worst_pvalue, best_pvalue, worst_sim_score, best_sim_score,
        worst_rep_1, worst_rep_2, best_rep_1, best_rep_2) = pMethod(dataset1,
        dataset2)
    return (id, worst_pvalue, best_pvalue, worst_sim_score, best_sim_score,
        worst_rep_1, worst_rep_2, best_rep_1, best_rep_2)


@classmethod
def from_string(cls, s: str):
    """Parse the enum from a string"""
    try:
        return cls[s.upper()]
    except KeyError:
        raise ValueError('%s is not a valid enum for %s' % (s.upper(), cls))


@staticmethod
def generate_shapes(shapes):
    """
        Generate the shapes for the topology

        :param dict shapes: A dict of converted shapes from the old topology
        :return: dict containing two lists (ellipse, rectangle)
        :rtype: dict
        """
    new_shapes = {'ellipse': [], 'rectangle': []}
    for shape in shapes:
        tmp_shape = {}
        for shape_item in shapes[shape]:
            if shape_item != 'type':
                tmp_shape[shape_item] = shapes[shape][shape_item]
        new_shapes[shapes[shape]['type']].append(tmp_shape)
    return new_shapes


@staticmethod
def quote_string(string: str) ->str:
    """
        Enclose a string in double-quotes, as required by LCDd when a
        command argument to LCDd involves embedded whitespace.

        :param string: string enclose in double quotes
        :return: string enclosed in double quotes, with embedded double quotes
                 escaped with a backslash
        """
    escaped = string.replace('"', '\\"')
    return f'"{escaped}"'


def process(reader, *chain):
    """
    The driver function for the :mod:`xlutils.filter` module.

    It takes a chain of one :ref:`reader <reader>`, followed by zero or more
    :ref:`filters <filter>` and ending with one :ref:`writer <writer>`.

    All the components are chained together by the :func:`process` function
    setting their ``next`` attributes appropriately. The
    :ref:`reader <reader>` is then called with the first
    :ref:`filter <filter>` in the chain.
    """
    for i in range(len(chain) - 1):
        chain[i].next = chain[i + 1]
    reader(chain[0])


def noneorstr(s):
    """Turn empty or 'none' string to None."""
    if s.lower() in ('', 'none'):
        return None
    else:
        return s


def GetListFromStr(s: str) ->list:
    """A function to parse str into list.

    :param s: column list str.
    :type s: str.
    :return: the convert result.
    :rtype: list
    """
    return s.strip('[]').replace("'", '').split(', ')


def t_OPEN_TAG(t):
    """<[?%]((php[ \\t\\r\\n]?)|=)?"""
    if '=' in t.value:
        t.type = 'OPEN_TAG_WITH_ECHO'
    t.lexer.lineno += t.value.count('\n')
    t.lexer.begin('php')
    return t


def fix_ra_dec(ra, dec):
    """
    Wraps input RA and DEC values into range expected by the extensions.

    Parameters
    ------------
    RA: array-like, units must be degrees
       Right Ascension values (astronomical longitude)

    DEC: array-like, units must be degrees
       Declination values (astronomical latitude)

    Returns
    --------
    Tuple (RA, DEC): array-like
         RA is wrapped into range [0.0, 360.0]
         Declination is wrapped into range [-90.0, 90.0]

    """
    try:
        input_dtype = ra.dtype
    except:
        msg = 'Input RA array must be a numpy array'
        raise TypeError(msg)
    if ra is None or dec is None:
        msg = 'RA or DEC must be valid arrays'
        raise ValueError(msg)
    if min(ra) < 0.0:
        print(
            'Warning: found negative RA values, wrapping into [0.0, 360.0]  range'
            )
        ra += 180.0
    if max(dec) > 90.0:
        print(
            'Warning: found DEC values more than 90.0; wrapping into [-90.0, 90.0] range'
            )
        dec += 90.0
    return ra.astype(input_dtype), dec.astype(input_dtype)


def totalseconds(timedelta):
    """Return the total number of seconds represented by a datetime.timedelta object, excluding fractions of seconds"""
    return timedelta.seconds + timedelta.days * 24 * 60 * 60


@classmethod
def add_from_case_control(cls, line_upper, lines, i):
    """add method used by the CaseControl class"""
    line = lines[i]
    sline = line_upper.split('=')
    assert len(sline) == 2, sline
    key, value = sline
    try:
        key, set_id = key.split()
    except:
        raise RuntimeError(key)
    assert key.upper() == key, key
    unused_options = int(set_id)
    fivalues = value.rstrip(' ,').split(',')
    if line[-1].strip() == ',':
        i += 1
        while 1:
            if lines[i].strip()[-1] == ',':
                fivalues += lines[i][:-1].split(',')
            else:
                fivalues += lines[i].split(',')
                i += 1
                break
            i += 1
    return cls(set_id, fivalues)


def KK_RC17(w, Rs, R_values, t_values):
    """
    Kramers-Kronig Function: -RC-

    Kristian B. Knudsen (kknu@berkeley.edu / kristianbknudsen@gmail.com)
    """
    return Rs + R_values[0] / (1 + w * 1.0j * t_values[0]) + R_values[1] / (
        1 + w * 1.0j * t_values[1]) + R_values[2] / (1 + w * 1.0j * t_values[2]
        ) + R_values[3] / (1 + w * 1.0j * t_values[3]) + R_values[4] / (1 +
        w * 1.0j * t_values[4]) + R_values[5] / (1 + w * 1.0j * t_values[5]
        ) + R_values[6] / (1 + w * 1.0j * t_values[6]) + R_values[7] / (1 +
        w * 1.0j * t_values[7]) + R_values[8] / (1 + w * 1.0j * t_values[8]
        ) + R_values[9] / (1 + w * 1.0j * t_values[9]) + R_values[10] / (1 +
        w * 1.0j * t_values[10]) + R_values[11] / (1 + w * 1.0j * t_values[11]
        ) + R_values[12] / (1 + w * 1.0j * t_values[12]) + R_values[13] / (
        1 + w * 1.0j * t_values[13]) + R_values[14] / (1 + w * 1.0j *
        t_values[14]) + R_values[15] / (1 + w * 1.0j * t_values[15]
        ) + R_values[16] / (1 + w * 1.0j * t_values[16])


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


def create_occurrence_of_problem(occurrence, problem_hash):
    """
        Persists given `occurrence` and creats relation with a problem of given
        `problem_hash`. Returns `occurrence`.
        """


def get_haplotype_score(var):
    """
    Returns consistency of the site with two segregating haplotypes,
    or None if it isn't present in the VCF.
    """
    return var.INFO.get('HaplotypeScore')


def validate_jhove(filename, jhove=None, ignore=None):
    """Validate TIFF file using jhove -m TIFF-hul.

    Raise ValueError if jhove outputs an error message unless the message
    contains one of the strings in 'ignore'.

    JHOVE does not support bigtiff or more than 50 IFDs.

    See `JHOVE TIFF-hul Module <http://jhove.sourceforge.net/tiff-hul.html>`_

    """
    import subprocess
    if ignore is None:
        ignore = ['More than 50 IFDs']
    if jhove is None:
        jhove = 'jhove'
    out = subprocess.check_output([jhove, filename, '-m', 'TIFF-hul'])
    if b'ErrorMessage: ' in out:
        for line in out.splitlines():
            line = line.strip()
            if line.startswith(b'ErrorMessage: '):
                error = line[14:].decode()
                for i in ignore:
                    if i in error:
                        break
                else:
                    raise ValueError(error)
                break


@classmethod
def _get_casing_permutations(cls, input_string):
    """ Takes a string and gives all possible permutations of casing for comparative purposes

        :param input_string: str, name of object
        :return: Generator(str), iterator of all possible permutations of casing for the input_string
        """
    if not input_string:
        yield ''
    else:
        first = input_string[:1]
        for sub_casing in cls._get_casing_permutations(input_string[1:]):
            yield first.lower() + sub_casing
            yield first.upper() + sub_casing


@classmethod
def get_service_name(cls):
    """returns the canonical name of the service (which is its first
        alias)"""
    return cls.get_service_aliases()[0]


def _get_qpoint_pos(pos):
    """Return the coordinates of a QPointF object."""
    return pos.x(), pos.y()


def partition_node(node):
    """Split a host:port string into (host, int(port)) pair."""
    host = node
    port = 27017
    idx = node.rfind(':')
    if idx != -1:
        host, port = node[:idx], int(node[idx + 1:])
    if host.startswith('['):
        host = host[1:-1]
    return host, port


@staticmethod
def cyan(text):
    """ turns text cyan """
    return f'\x1b[36m{text}\x1b[0m'


def dBmtomw(value):
    """
    Converts dBm to mw

    value: dBm value

    returns: mW value
    """
    return round(10 ** (value / 10), 3)


def abort():
    """ called if processing of the queue needs to be aborted """


@staticmethod
def get_app_name():
    """get name of this application. Will be used to store offsets in database"""
    return 'mon_metrics_kafka_pre_hourly'


def total_secs(td):
    """ *td* is a timedelta object """
    return td.days * 3600 * 24 + td.seconds + td.microseconds / 1000000.0


def p_for_stmt(p):
    """for_stmt : FOR id_list IN testlist_star_expr suite
                | FOR "[" id_list "]" IN testlist_star_expr suite """
    if len(p) == 6:
        p[0] = ['FOR', p[2], p[4], p[5]]
    else:
        p[0] = ['FOR', p[3], p[6], p[7]]


@staticmethod
def _ignorify(patterns=['*~']):
    """
        pathspec implements gitignore style pattern matching. However,
        it doesn't ignore patterns, it *matches* patterns.
        So every pattern needs to be reversed by adding a '!' (or removing)
        it.
        """
    patterns = list(patterns)
    reversed_patterns = ['*']
    for p in patterns:
        if p.startswith('!'):
            reversed_patterns.append(p.replace('!', '', 1))
        else:
            reversed_patterns.append('!' + p)
    return reversed_patterns


def view_as_windows(padded, patch_size, stride=None):
    """
    cut image in sliding windows
    first patch starts at 0, ends at patch_size
    second patch starts at stride, ends at stride + patch_size
    :param padded: padded image, padded according to get_padded_patches
    :param patch_size: size of output patches
    :param stride: stride between patches
    :return: patches
    """
    if stride is None:
        stride = patch_size
    patches = []
    pad = int(patch_size - stride)
    n_cols = int((padded.shape[0] - pad) / stride)
    n_rows = int((padded.shape[1] - pad) / stride)
    for i in range(n_cols):
        for j in range(n_rows):
            patches.append(padded[i * stride:i * stride + patch_size, j *
                stride:j * stride + patch_size])
    return patches


def get_cf_name(env_name, layer_name, stage=None):
    """Produces the CF stack name for layer."""
    cf_name = '{}-{}'.format(env_name, layer_name)
    if stage is not None:
        cf_name = '{}-{}'.format(cf_name, stage)
    return cf_name


def _join_translated(translated_parts, os_sep_class):
    """Join translated glob pattern parts.
    This is different from a simple join, as care need to be taken
    to allow ** to match ZERO or more directories.
    """
    res = ''
    for part in translated_parts[:-1]:
        if part == '.*':
            res += part
        else:
            res += part + os_sep_class
    if translated_parts[-1] == '.*':
        res += '.+'
        res += '({os_sep_class}?.*)?'.format(os_sep_class=os_sep_class)
    else:
        res += translated_parts[-1]
    return res


def crashed(info):
    """
        Process 'user crashed' event.

        Input:
        `info`  # An object with information about event's time, user's
                # callsign, aircraft and position on map.
        """


def postBuildStatic(static):
    """
    Called after building (copying to the build folder) a static file.

    :param static: The static file that was just built.
    :returns: None
    """
    pass


@classmethod
def from_revision(cls, client, path, revision='HEAD', **kwargs):
    """Return dependency from given path and revision."""
    from renku.core.models.entities import Entity
    return cls(entity=Entity.from_revision(client, path, revision), **kwargs)


def _validate_dependencies_met():
    """
    Verifies that PyOpenSSL's package-level dependencies have been met.
    Throws `ImportError` if they are not met.
    """
    from cryptography.x509.extensions import Extensions
    if getattr(Extensions, 'get_extension_for_class', None) is None:
        raise ImportError(
            "'cryptography' module missing required functionality.  Try upgrading to v1.3.4 or newer."
            )
    from OpenSSL.crypto import X509
    x509 = X509()
    if getattr(x509, '_x509', None) is None:
        raise ImportError(
            "'pyOpenSSL' module missing required functionality. Try upgrading to v0.14 or newer."
            )


def dp_unnormalise(y, normalisation_parameters):
    """
    new_y = dp_unnormalise(y, normalisation_parameters)

    Unnormalises the data, y, using the parameters passed in normalisation_parameters.
    """
    y = y * normalisation_parameters['std']
    y = y + normalisation_parameters['mean']
    return y


@classmethod
def search_company(cls, name, clause):
    """
        Searcher for field delivery_date
        """
    return [('sale.company',) + tuple(clause[1:])]


def getHistoryOf(wf_id, ob):
    """ Get the history of an object for a given workflow.

        o 'wf_id' is the id of the selected workflow.

        o 'ob' is the content object.

        o Invoked by workflow definitions.

        o Permission:  Private (Python only)
        """


def _attr_key(attr):
    """Return an appropriate key for an attribute for sorting

    Attributes have a namespace that can be either ``None`` or a string. We
    can't compare the two because they're different types, so we convert
    ``None`` to an empty string first.

    """
    return attr[0][0] or '', attr[0][1]


@classmethod
def node_tooltip(cls, node):
    """Return text for a tooltip for the node."""
    d = cls.node_dict(node)
    d.update(index=node.parent_index() if node.parent else '-', parent=cls.
        node_repr(node.parent) if node.parent else '-')
    if node.is_token:
        if not d['group']:
            d['group'] = '-'
    template = cls.TOKEN_TOOLTIP if node.is_token else cls.CONTEXT_TOOLTIP
    return template.format(**d).strip()


def p_dictorsetmaker_star_1(p):
    """dictorsetmaker_star : COMMA test COLON test"""
    p[0] = [p[2]], [p[4]]


def _upcount(ls):
    """
    count the ratio of upmove days by given a list
    """
    count = 0
    for i in range(len(ls) - 1):
        if ls.iloc[i + 1] > ls.iloc[i]:
            count += 1
    return count / (len(ls) - 1)


def extract_all_words_from_list_names(names_list):
    """
    Extract all words from list with snake case names
    :param names_list: list
    :return: list
    """
    word_list = []
    for name in names_list:
        word_list += name.split('_')
    return word_list


def parseExtn(extn=None):
    """
    Parse a string representing a qualified fits extension name as in the
    output of `parseFilename` and return a tuple ``(str(extname),
    int(extver))``, which can be passed to `astropy.io.fits` functions using
    the 'ext' kw.

    Default return is the first extension in a fits file.

    Examples
    --------

    ::

        >>> parseExtn('sci, 2')
        ('sci', 2)
        >>> parseExtn('2')
        ('', 2)
        >>> parseExtn('sci')
        ('sci', 1)

    """
    if not extn:
        return '', 0
    try:
        lext = extn.split(',')
    except:
        return '', 1
    if len(lext) == 1 and lext[0].isdigit():
        return '', int(lext[0])
    elif len(lext) == 2:
        return lext[0], int(lext[1])
    else:
        return lext[0], 1


def get_action(action):
    """Parse abbreviations for `action`."""
    if not isinstance(action, str):
        return
    elif action.lower().startswith('r'):
        return 'receive'
    elif action.lower().startswith('s'):
        return 'send'


def truncate(string, max_len=1000):
    """returns a truncated to max_len version of a string (or str(string))"""
    string = str(string)
    if len(string) > max_len - 12:
        return string[:max_len] + '...TRUNCATED'
    return string


def train_test_split(x1, x2, percent=0.75):
    """Splits the embedded time series into a training set and testing set.

    Parameters
    ----------
    x1 : 2D array
        Embed time series.
    x2 : 2D array
        Embed time series.
    percent : float
        Percent to use for training set.

    Returns
    -------
    x1tr : 2D array
    x1te : 2D array
    x2tr : 2D array
    x2te : 2D array
    """
    if len(x1) != len(x2):
        print('X1 and X2 are different lengths!')
    split = int(len(x1) * percent)
    x1tr = x1[:split]
    x2tr = x2[:split]
    x1te = x1[split:]
    x2te = x2[split:]
    return x1tr, x1te, x2tr, x2te


def explain_au(fsm, state, phi, psi, states=None):
    """
    Explain why state of fsm satisfies A phi U psi.

    fsm -- the fsm;
    state -- a state of fsm satisfying A phi U psi;
    phi -- the set of states of fsm satifying phi;
    psi -- the set of states of fsm satifying psi;
    states -- a dictionary of state->explanation pairs.
    """
    pass


def get_boolean_attribute_value(attrs, attr_name):
    """ Helper function to convert a string version
    of Boolean attributes to integer for ONNX.
    Takes attribute dictionary and attr_name as
    parameters.
    """
    return 1 if attrs.get(attr_name, 0) in ['True', '1'] else 0


def hashable(obj):
    """ Convert obj to a hashable obj.
    We use the value of some fields from Elasticsearch as keys for dictionaries. This means
    that whatever Elasticsearch returns must be hashable, and it sometimes returns a list or dict."""
    if not obj.__hash__:
        return str(obj)
    return obj


def write_best_to_x(iteration_data):
    """
  Makes the best point the current iterate.

  x, funs, fval = write_best_to_x(iteration_data)"""
    x = iteration_data.xb
    funs = iteration_data.funsb
    fval = iteration_data.fobjb
    return x, funs, fval


@classmethod
def compile_instance_masks(cls):
    """ Compiles instance masks into a master mask that is usable by
                the IO expander. Also determines whether or not the pump
                should be on.
            Method is generalized to support multiple IO expanders
                for possible future expansion.
        """
    number_IO_expanders = (len(cls._list) - 1) / 4 + 1
    cls.master_mask = [0, 0] * number_IO_expanders
    for ctrlobj in cls:
        cls.master_mask[ctrlobj.bank] |= ctrlobj.mask
        if ctrlobj.pump_request == 1:
            cls.master_mask[cls.pump_bank] |= 1 << cls.pump_pin


def check_list(listvar):
    """Turns single items into a list of 1."""
    if not isinstance(listvar, list):
        listvar = [listvar]
    return listvar


@staticmethod
def badges(tags) ->dict:
    """parse badges dict"""
    try:
        if 'badges' in tags and type(tags['badges']) == str:
            badges = {}
            explode = tags['badges'].split(',')
            for item in explode:
                parts = item.split('/')
                if parts[1] == None:
                    return
                badges[parts[0]] = parts[1]
            tags['badges-raw'] = tags['badges']
            tags['badges'] = badges
        if 'badges' in tags and type(tags['badges']) == bool:
            tags['badges-raw'] = None
        return tags
    except:
        pass


def _remove_attributes(attrs, remove_list):
    """
    Removes attributes in the remove list from the input attribute dict
    :param attrs : Dict of operator attributes
    :param remove_list : list of attributes to be removed

    :return new_attr : Dict of operator attributes without the listed attributes.
    """
    new_attrs = {}
    for attr in attrs.keys():
        if attr not in remove_list:
            new_attrs[attr] = attrs[attr]
    return new_attrs


def produce_vcftools_output(output, filename, append_mode=False,
    strip_header=False):
    """
        Creates the vcftools output file

        This function will create an output file from the vcftools stdout.
        Please run `check_vcftools_for_errors` prior to check that vcftools
        finished without error.

        Parameters
        ----------
        output : str
            vcftools stdout
        filename : str
            Specifies the filename for the output file
        append_mode : bool
            Used to create a single output file from multiple calls
        strip_header : bool
            Used to remove the header if not needed

        Returns
        -------
        output : file
            vcftools output file

    """
    if strip_header:
        output = ''.join(output.splitlines(True)[1:])
    if append_mode:
        vcftools_log_file = open(filename, 'a')
    else:
        vcftools_log_file = open(filename, 'w')
    vcftools_log_file.write(str(output))
    vcftools_log_file.close()


def is_primitive(item):
    """
    Determines if the given item is a primitive value (either an int, float,
    str, bool, or None).

    Args:
        item (any): Any value
    Returns:
        bool: Whether the item is a primitive value.
    """
    return isinstance(item, (int, float, str, bool)) or item is None


@staticmethod
def mimetype_to_msgtype(mimetype):
    """Turn a mimetype into a matrix message type."""
    if mimetype.startswith('image'):
        return 'm.image'
    elif mimetype.startswith('video'):
        return 'm.video'
    elif mimetype.startswith('audio'):
        return 'm.audio'
    return 'm.file'


@staticmethod
def split_pred_name(pred_name):
    """
        Static method to split a predicate.
        Splits a predicate in two parts: the name of the predicate, and the
        type it holds.

        :example:
            Foo(bar,baz)
            would be split in "Foo" and "[bar, baz]"

        """
    pred_name = pred_name.strip()
    if not pred_name.endswith(')'):
        pred_name += '()'
    name, args = pred_name.split('(')
    arglist = args.strip(')').split(',')
    return name, arglist


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 post_search(cs, results):
    """
    This hook is called after a (quicklove or regular) search, and is
    passed a list containing the results.

    Elements of this list are 3-tuples:

     - the name of the plan on which the term was found (str)
     - the number of instances found (int)
     - a list of snippets.

    Note that the snippet list may not be the same length as the
    number of instances found.

    Lists are mutable, so results may be filtered by modifying this
    list in-place.

    """
    pass


def readTranscript(file_name):
    """
    Retrieve the contents of the transcript file.

    Parameters
    ----------
    file_name : str
        The path to the transcript file.
    Returns
    -------
    text_to_read : list
        Lines from the transcript file.
    """
    try:
        with open(file_name, 'r', encoding='utf-8') as f:
            text_to_read = f.read()
        if text_to_read != '':
            return text_to_read
        else:
            return 'No transcript file found.'
    except IOError:
        return 'No transcript file found.'


def get_application(context, application):
    """
    Get Application class instance of an application, based upon given name.

    :type context: <behave.runner.Context>
    :param context: Context object that is passed from common_steps.

    :type application: str
    :param application: String of application identification: name.

    :rtype: <qecore.application.Application>
    :return: Application class instance

    .. note::

        Do **NOT** call this by yourself. This function is called by :mod:`common_steps`.
    """
    app_class_to_return = None
    try:
        app_class_to_return = getattr(context, application)
    except AttributeError:
        for app in context.sandbox.applications:
            if app.component == application:
                app_class_to_return = app
                break
    except TypeError:
        app_class_to_return = context.sandbox.default_application
        assert context.sandbox.default_application is not None, 'Default application was not found. Check your environment file!'
    assert app_class_to_return is not None, 'Application was not found. Check your environment or feature file!'
    assert not isinstance(app_class_to_return, str
        ), 'Application class was not found. Usually indication of not installed application.'
    return app_class_to_return


def addln(win, y, x, line, color=None):
    """Add a line to the given window left padding but 100% filled with
    whitespace characters, so that the color appears on the whole line"""
    maxy, maxx = win.getmaxyx()
    length = maxx - 1 - x
    line = bytes(line).ljust(length)[:length]
    if y < 0:
        y = maxy + y
    if x < 0:
        x = maxx + x
    if color:
        win.addstr(y, x, line, color)
    else:
        win.addstr(y, x, line)


def with_defaults(options: dict, defaults_dict: dict=None, **defaults_kwargs):
    """Return a dict where missing keys are filled in by defaults

    >>> options = dict(hello=0)
    >>> with_defaults(options, hello=4, world=5) == dict(hello=0, world=5)
    True
    >>> defaults = dict(hello=4, world=5)
    >>> with_defaults(options, defaults) == dict(hello=0, world=5)
    True
    >>> with_defaults(options, defaults, world=7, yes=3) == dict(hello=0, world=5, yes=3)
    True
    """
    options = options if options else {}
    if defaults_dict:
        options = dict(defaults_dict, **options)
    return dict(defaults_kwargs, **options)


def compute_taw(fc, pwp, depth, fraction):
    """
    Compute total available water
    :param fc: Field capacity
    :param pwp: permanent wilting point
    :param depth: depth of soil in mm
    :param fraction: float value
    :return: a float value for TAW
    """
    return depth * fraction * (fc - pwp)


def create_hapg(Label=None):
    """
    Creates a high-availability partition group. A high-availability partition group is a group of partitions that spans multiple physical HSMs.
    See also: AWS API Documentation

    :example: response = client.create_hapg(
        Label='string'
    )

    :type Label: string
    :param Label: [REQUIRED]
            The label of the new high-availability partition group.

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

    """
    pass


def has_open_quotes(s):
    """Return whether a string has open quotes.

    This simply counts whether the number of quote characters of either type in
    the string is odd.

    Returns
    -------
    If there is an open quote, the quote character is returned.  Else, return
    False.
    """
    if s.count('"') % 2:
        return '"'
    elif s.count("'") % 2:
        return "'"
    else:
        return False


def debug(tokens):
    """
        Just prints tokens
        Used for debugging
        """
    print(tokens)
    return tokens


def _is_paired(fastq, fastq2, single_end):
    """Determines the workflow based on file inputs.

    Args:

    """
    if fastq and fastq2:
        paired_end = True
        interleaved = False
    elif single_end:
        paired_end = False
        interleaved = False
    else:
        paired_end = True
        interleaved = True
    return paired_end, interleaved


@classmethod
def _get_toolkit_to_apic_classmap(cls):
    """
        Gets the APIC class to an acitoolkit class mapping dictionary

        :returns: dict of APIC class names to acitoolkit classes
        """
    return {}


def close(x, y, rtol=1e-05, atol=1e-08):
    """Returns True if x and y are sufficiently close.

    Parameters
    ----------
    rtol : float
        The relative error tolerance.
    atol : float
        The absolute error tolerance.

    """
    return abs(x - y) <= atol + rtol * abs(y)


@classmethod
def remove_sections(cls, string, symbol_or_re, is_re=False):
    """Remove sections."""
    ah_retained, ah_removed = cls.tag_partition(string, cls.section_tag(
        symbol_or_re, is_re), is_re)
    return ''.join(ah_retained)


def untag_resource(ResourceArn=None, TagKeys=None):
    """
    Removes the association of tags from an Amazon DynamoDB resource. You can call UntagResource up to 5 times per second, per account.
    For an overview on tagging DynamoDB resources, see Tagging for DynamoDB in the Amazon DynamoDB Developer Guide .
    See also: AWS API Documentation

    :example: response = client.untag_resource(
        ResourceArn='string',
        TagKeys=[
            'string',
        ]
    )

    :type ResourceArn: string
    :param ResourceArn: [REQUIRED]
            The Amazon DyanamoDB resource the tags will be removed from. This value is an Amazon Resource Name (ARN).

    :type TagKeys: list
    :param TagKeys: [REQUIRED]
            A list of tag keys. Existing tags of the resource whose keys are members of this list will be removed from the Amazon DynamoDB resource.
            (string) --

    """
    pass


def pretty_date(time=False):
    """
    Get a datetime object or a int() Epoch timestamp and return a
    pretty string like 'an hour ago', 'Yesterday', '3 months ago',
    'just now', etc
    """
    from datetime import datetime
    now = datetime.now()
    if type(time) is int:
        diff = now - datetime.fromtimestamp(time)
    elif isinstance(time, datetime):
        diff = now - time
    elif not time:
        diff = now - now
    second_diff = diff.seconds
    day_diff = diff.days
    if day_diff < 0:
        return ''
    if day_diff == 0:
        if second_diff < 10:
            return 'just now'
        if second_diff < 60:
            return str(second_diff) + ' seconds ago'
        if second_diff < 120:
            return 'a minute ago'
        if second_diff < 3600:
            return str(int(second_diff / 60)) + ' minutes ago'
        if second_diff < 7200:
            return 'an hour ago'
        if second_diff < 86400:
            return str(int(second_diff / 3600)) + ' hours ago'
    if day_diff == 1:
        return 'Yesterday'
    if day_diff < 7:
        return str(int(day_diff)) + ' days ago'
    if day_diff < 31:
        return str(int(day_diff / 7)) + ' weeks ago'
    if day_diff < 365:
        return str(int(day_diff / 30)) + ' months ago'
    return str(int(day_diff / 365)) + ' years ago'


def string_bool(argument):
    """
    Converts True, true, False, False in python boolean values
    """
    if argument is None:
        msg = (
            'argument required but none supplied; choose from "True" or "False"'
            )
        raise ValueError(msg)
    elif argument.lower() == 'true':
        return True
    elif argument.lower() == 'false':
        return False
    else:
        raise ValueError('"%s" unknown; choose from "True" or "False"' %
            argument)


def get_tone_from_envelope_val(envelope, instru_low, instru_high):
    """transforms envelope val (-1 ... +1) to tone height"""
    tone = ((instru_high - instru_low) * envelope + instru_high + instru_low
        ) / 2
    tone = int(tone + 0.5)
    return tone


def call(func_name, *args):
    """Performs an [RPC] function call with the given arguments.

    .. warning:: Bytes are returned for Python 3.

    :param str|unicode func_name: Function name to call
        with optional address (if @-syntax is used).

    :param list[str|bytes] args:

    :rtype: bytes|str
    """


def cycles_integrator_preset_add(name: str='', remove_name: bool=False,
    remove_active: bool=False):
    """Add an Integrator Preset

    :param name: Name, Name of the preset, used to make the path name
    :type name: str
    :param remove_name: remove_name
    :type remove_name: bool
    :param remove_active: remove_active
    :type remove_active: bool
    """
    pass


def get_listing_panel(tool, ghidra):
    """ Get the code listing UI element, so we can get up-to-date location/highlight/selection """
    cvs = tool.getService(ghidra.app.services.CodeViewerService)
    return cvs.getListingPanel()


def parse_defaults(defaults_string):
    """Yields default values for a function"""
    if not defaults_string:
        return
    current = ''
    in_quote = None
    for char in defaults_string:
        if current == '' and char == ' ':
            continue
        if char == '"' or char == "'":
            if in_quote and char == in_quote:
                in_quote = None
            elif not in_quote:
                in_quote = char
        elif char == ',' and not in_quote:
            yield current
            current = ''
            continue
        current += char
    yield current


def generate_shared_public_key(my_private_key, their_public_pair, generator):
    """
    Generate a shared public pair. Two parties each generate a private key and share
    their public key with the other party over an insecure channel. The shared public
    key can be generated by either side, but not by eavesdroppers.
    """
    p = generator.Point(*their_public_pair)
    return my_private_key * p


def set_node_mat(node_map, node, node_list):
    """
    Adjacent list to adjacent matrix
    :param node_map:
    :param node:
    :param node_list:
    :return: Adjacent matrix
    """
    for x, y, val in node_list:
        node_map[node.index(x)][node.index(y)] = node_map[node.index(y)][
            node.index(x)] = val


def get_characters(lang_data):
    """
    Return a sorted list of characters in the language corpus.

    Parameters
    ----------
    lang_data : list of str
        A list of all paragraphs

    Returns
    -------
    characters : Counter Object
    """
    from collections import Counter
    characters = Counter()
    for paragraph in lang_data:
        characters += Counter(paragraph)
    return characters


def add_new_params(old_grid, new_grid, old_name, new_name):
    """
    Adds new items (parameters) in-place to old dict (of parameters),
    ensuring no overlap in new parameters with old parameters which prevents silent overwrite.

    """
    if new_grid:
        new_params = set(new_grid.keys())
        old_params = set(old_grid.keys())
        if len(old_params.intersection(new_params)) > 0:
            raise ValueError(
                'Overlap in parameters between {} and {} of the chosen pipeline.'
                .format(old_name, new_name))
        old_grid.update(new_grid)
    return


@classmethod
def _propagate_attributes(mcs, bases, attrs, clsname):
    """
        For attributes specified by to_propagate, their values will be a union of
        that specified for cls and its bases (cls values overriding those of bases
        in case of conflicts).

        """
    for prop_attr, attr_cls, attr_collector_cls in mcs.to_propagate:
        should_propagate = False
        propagated = attr_collector_cls(attr_cls)
        for base in bases:
            if hasattr(base, prop_attr):
                propagated += getattr(base, prop_attr) or []
                should_propagate = True
        if prop_attr in attrs:
            pattrs = attrs[prop_attr] or []
            for pa in pattrs:
                if not isinstance(pa, attr_cls):
                    msg = 'Invalid value "{}" for attribute "{}"; must be a {}'
                    raise ValueError(msg.format(pa, prop_attr, attr_cls))
                pa._owner = clsname
            propagated += pattrs
            should_propagate = True
        if should_propagate:
            for p in propagated:
                override = bool(getattr(p, 'override', None))
                overridden = bool(getattr(p, '_overridden', None))
                if override != overridden:
                    msg = "Overriding non existing parameter '{}' inside '{}'"
                    raise ValueError(msg.format(p.name, p._owner))
            attrs[prop_attr] = propagated


@classmethod
def split_path(cls, path):
    """
        Normalise GCS path string into bucket and key.

        Parameters
        ----------
        path : string
            Input path, like `gcs://mybucket/path/to/file`.
            Path is of the form: '[gs|gcs://]bucket[/key]'

        Returns
        -------
            (bucket, key) tuple
        """
    path = cls._strip_protocol(path).lstrip('/')
    if '/' not in path:
        return path, ''
    else:
        return path.split('/', 1)


def sculptmode_toggle():
    """Toggle sculpt mode in 3D view

    """
    pass


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

    :return: name of this resource
    :rtype: str
    """
    if friendly:
        return 'Check request'
    return 'actionforcecheck'


def get_groups(qids):
    """Makes an iterator of query groups on the provided list of query ids.

    Parameters
    ----------
    qids : array_like of shape = [n_samples]
        List of query ids.

    Yields
    ------
    row : (qid, int, int)
        Tuple of query id, from, to.
        ``[i for i, q in enumerate(qids) if q == qid] == range(from, to)``

    """
    prev_qid = None
    prev_limit = 0
    total = 0
    for i, qid in enumerate(qids):
        total += 1
        if qid != prev_qid:
            if i != prev_limit:
                yield prev_qid, prev_limit, i
            prev_qid = qid
            prev_limit = i
    if prev_limit != total:
        yield prev_qid, prev_limit, total


def make_extra_console_formatter(graph):
    """
    Create the default console formatter.

    """
    return {'()': 'microcosm_logging.formatters.ExtraConsoleFormatter',
        'format_string': graph.config.logging.default_format}


def set_metadata_for(name, metadata):
    """I replace any existing metadata for the named child with the new
        metadata. The child name must be a unicode string. This metadata is
        stored in the *edge*, not in the child, so it is attached to the
        parent dirnode rather than the child node. I return a Deferred
        (that fires with this dirnode) when the operation is complete.
        I raise NoSuchChildError if I do not have a child by that name."""


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

    The ".dev0" means dirty.

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


def clean_distance_data(csv_data):
    """Remove redundant column, use location numbers instead of names,
    and convert distance data (a mix of integer and string) to floats.
    """
    for row_index, row in enumerate(csv_data):
        del row[1]
        for col_index, datum in enumerate(row):
            if row_index != 0 and col_index != 0:
                if datum != '':
                    csv_data[row_index][col_index] = float(datum)
        if row_index != 0:
            row[0] = row_index
    for col_index, _ in enumerate(csv_data[0]):
        if col_index != 0:
            csv_data[0][col_index] = col_index


def armature_apply():
    """Apply the current pose as the new rest pose

    """
    pass


def meshdeform_bind(modifier: str=''):
    """Bind mesh to cage in mesh deform modifier

    :param modifier: Modifier, Name of the modifier to edit
    :type modifier: str
    """
    pass


def do_filesizeformat(value, binary=False):
    """Format the value like a 'human-readable' file size (i.e. 13 kB,
    4.1 MB, 102 Bytes, etc).  Per default decimal prefixes are used (Mega,
    Giga, etc.), if the second parameter is set to `True` the binary
    prefixes are used (Mebi, Gibi).
    """
    bytes = float(value)
    base = binary and 1024 or 1000
    prefixes = [binary and 'KiB' or 'kB', binary and 'MiB' or 'MB', binary and
        'GiB' or 'GB', binary and 'TiB' or 'TB', binary and 'PiB' or 'PB',
        binary and 'EiB' or 'EB', binary and 'ZiB' or 'ZB', binary and
        'YiB' or 'YB']
    if bytes == 1:
        return '1 Byte'
    elif bytes < base:
        return '%d Bytes' % bytes
    else:
        for i, prefix in enumerate(prefixes):
            unit = base ** (i + 2)
            if bytes < unit:
                return '%.1f %s' % (base * bytes / unit, prefix)
        return '%.1f %s' % (base * bytes / unit, prefix)


def items():
    """Return a list of (id, object) pairs.
        """


def compare(dict0, dict1, force_inside01=True):
    """
        Draw none frontiers
        """
    return 0


@classmethod
def _from_dict(cls, _dict):
    """Initialize a AnalyticsEngineCustomActionScript object from a json dictionary."""
    return cls.from_dict(_dict)


def bench1(x):
    """A benchmark function for test purposes.

        f(x) = x ** 2

    It has a single minima with f(x*) = 0 at x* = 0.
    """
    return x[0] ** 2


@classmethod
def create(cls, **kwargs):
    """Returns a new instance of the model (without saving it) with the attributes specified in ``kwargs``

        :returns: tag -- a new local instance of the Tag
        """
    return cls(**kwargs)


def rename_nfa_states(nfa: dict, suffix: str):
    """ Side effect on input! Renames all the states of the NFA
    adding a **suffix**.

    It is an utility function to be used to avoid automata to have
    states with names in common.

    Avoid suffix that can lead to special name like "as", "and",...

    :param dict nfa: input NFA.
    :param str suffix: string to be added at beginning of each state name.
    """
    conversion_dict = {}
    new_states = set()
    new_initials = set()
    new_accepting = set()
    for state in nfa['states']:
        conversion_dict[state] = '' + suffix + state
        new_states.add('' + suffix + state)
        if state in nfa['initial_states']:
            new_initials.add('' + suffix + state)
        if state in nfa['accepting_states']:
            new_accepting.add('' + suffix + state)
    nfa['states'] = new_states
    nfa['initial_states'] = new_initials
    nfa['accepting_states'] = new_accepting
    new_transitions = {}
    for transition in nfa['transitions']:
        new_arrival = set()
        for arrival in nfa['transitions'][transition]:
            new_arrival.add(conversion_dict[arrival])
        new_transitions[conversion_dict[transition[0]], transition[1]
            ] = new_arrival
    nfa['transitions'] = new_transitions
    return nfa


def round_time(seconds, units, rounding):
    """
    Rounds given time into specific units.

    Args:
        seconds: float or int
            Time in seconds.

        units: pero.TIME
            Specifies the rounding units as eny item from the pero.TIME enum.

        rounding: pero.ROUNDING
            Specifies the rounding style as any item from the pero.ROUNDING
            enum.

    Returns:
        float
            Rounded time in seconds.
    """
    pass


def _get_file_name(name: str, is_step: bool) ->str:
    """
    Gets a file name for the generated doc files.

    :param name: Name of a folder or file.
    :param is_step: If name is related to step file or not.
    :return: The file name for the doc file.
    """
    file_name = name.replace('_', '-')
    if is_step:
        file_name = f'{file_name}-steps.rst'
    else:
        file_name = f'{file_name}.rst'
    return file_name


def _option_style(option):
    """
    Tell the style of an option.
    1. '-' or '--': naked   (a)
    2. '--abc'    : long    (l)
    3. 'abc'      : commnad (a)
    4. '-abc'     : oldlong (o)
    5. '-a'       : short   (s)
    """
    if option in ('-', '--'):
        return 'a'
    if option.startswith('--'):
        return 'l'
    if not option.startswith('-'):
        return 'f -a'
    if len(option) > 2:
        return 'o'
    return 's'


def CDLXSIDEGAP3METHODS(df):
    """
    Upside/Downside Gap Three Methods
    """


def find_in_enum_members(item, enum_members: list):
    """Matches a string in the provided list of member strings.
	The item must be not fully matched.
	Rules:
	- If the member string starts with underscore, it is cut out before the matching continues.
	- The item is matched if a member string starts with the item (item is a prefix of the member)."""
    found = None
    for x in enum_members:
        if x.startswith(item):
            found = x
            break
        if x.startswith('_'):
            if x.strip('_').startswith(item):
                found = x
                break
    return found


def _devcfg_url(id_=None, runtime=False):
    """
    Generate config path for a device
    :param id_:
    :param runtime:
    :return:
    """
    return '/api/cfg/snmpdevice{}{}'.format('' if not id_ else '/{}'.format
        (id_), '/runtime' if runtime else '')


def mask_hash(hash, show=6, char='*'):
    """
    Return the given hash, with only the first ``show`` number shown. The
    rest are masked with ``char`` for security reasons.
    """
    masked = hash[:show]
    masked += char * len(hash[show:])
    return masked


def get_var(environ, keys, default=None):
    """Try each of the keys in turn before falling back on the default."""
    for key in keys:
        if environ.has_key(key):
            return environ.get(key)
    return default


def __iter__():
    """Iterate over all existing paths."""


@staticmethod
def sanity_check_categorical(df):
    """Sanity check for 'dimensionality explosion' on categorical types
        Args:
            df (dataframe): The dataframe to check the categorical columns
        Returns:
            None
        """
    for column in df.select_dtypes(include='category').columns:
        num_unique = df[column].nunique()
        if num_unique > 20:
            print(
                'WARNING: {:s} will expand into {:d} dimensions! Should not include in feature set!'
                .format(column, num_unique))


@classmethod
def define_metadata(cls, obj):
    """Define properties supported and units."""
    obj.add_properties({'flow_mol': {'method': None, 'units': 'mol/s'},
        'flow_mol_phase_comp': {'method': None, 'units': 'mol/s'},
        'mole_frac': {'method': None, 'units': 'none'}, 'temperature': {
        'method': None, 'units': 'K'}, 'pressure': {'method': None, 'units':
        'Pa'}, 'flow_mol_phase': {'method': None, 'units': 'mol/s'},
        'dens_mol_phase': {'method': '_dens_mol_phase', 'units': 'mol/m^3'},
        'pressure_sat': {'method': '_pressure_sat', 'units': 'Pa'},
        'mole_frac_phase': {'method': '_mole_frac_phase', 'units':
        'no unit'}, 'energy_internal_mol_phase_comp': {'method':
        '_energy_internal_mol_phase_comp', 'units': 'J/mol'},
        'energy_internal_mol_phase': {'method':
        '_enenrgy_internal_mol_phase', 'units': 'J/mol'},
        'enth_mol_phase_comp': {'method': '_enth_mol_phase_comp', 'units':
        'J/mol'}, 'enth_mol_phase': {'method': '_enth_mol_phase', 'units':
        'J/mol'}, 'entr_mol_phase_comp': {'method': '_entr_mol_phase_comp',
        'units': 'J/mol'}, 'entr_mol_phase': {'method': '_entr_mol_phase',
        'units': 'J/mol'}, 'temperature_bubble': {'method':
        '_temperature_bubble', 'units': 'K'}, 'temperature_dew': {'method':
        '_temperature_dew', 'units': 'K'}, 'pressure_bubble': {'method':
        '_pressure_bubble', 'units': 'Pa'}, 'pressure_dew': {'method':
        '_pressure_dew', 'units': 'Pa'}, 'fug_vap': {'method': '_fug_vap',
        'units': 'Pa'}, 'fug_liq': {'method': '_fug_liq', 'units': 'Pa'},
        'dh_vap': {'method': '_dh_vap', 'units': 'J/mol'}, 'ds_vap': {
        'method': '_ds_vap', 'units': 'J/mol.K'}})
    obj.add_default_units({'time': 's', 'length': 'm', 'mass': 'g',
        'amount': 'mol', 'temperature': 'K', 'energy': 'J', 'holdup': 'mol'})


def render_event_time(datetime, whole_day, request, no_time=''):
    """Render time of event human readable and localized to locale of user."""
    if whole_day:
        return no_time
    formatter = request.locale.dates.getFormatter('time', 'short')
    time = formatter.format(datetime)
    if request.locale.id.language == 'de':
        time += ' Uhr'
    return time


def remove_flanking_scores(all_scores, flank_length):
    """ Returns center scores, removing the flanking ones

    Parameters
    ----------
    all_scores: array_like
        An array containting all scores
    flank_length: int
        Number of flanking sites on each side
    """
    assert flank_length > 0
    return all_scores[flank_length:-flank_length]


@staticmethod
def recall_pos_conf(conf):
    """compute recall of the positive class"""
    TN, FP, FN, TP = conf
    if TP + FN == 0:
        float('nan')
    return TP / float(TP + FN)


def delete_object():
    """Delete an object from TimeSync"""
    return [{'status': 200}]


def __ne__(A, other):
    """Returns true if both objects are not equal"""
    return not A == other


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


@classmethod
def add_style(cls, style, fn):
    """Add comment style."""
    if style not in cls.__dict__:
        setattr(cls, style, fn)
        cls.styles.append(style)


def extension_from_filename(filename):
    """
    Parse out filename from any specified extensions.
    Returns rootname and string version of extension name.
    """
    _indx1 = filename.find('[')
    _indx2 = filename.find(']')
    if _indx1 > 0:
        if _indx2 < _indx1:
            raise RuntimeError(
                "Incorrect extension specification in file name '%s'." %
                filename)
        _fname = filename[:_indx1]
        _extn = filename[_indx1 + 1:_indx2].strip()
    else:
        _fname = filename
        _extn = None
    return _fname, _extn


@staticmethod
def is_primitive():
    """Return `False`: string variables are not stored as floats."""
    return False


@staticmethod
def p_bit_operation_optional_list(p):
    """bit_operation_optional_list : bit_operation_optional
                                       | bit_operation_optional bit_operation_optional_list"""
    try:
        p[0] = [p[1]] + p[2]
    except IndexError:
        p[0] = [p[1]]


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


def _find_human_readable_labels(synsets, synset_to_human):
    """Build a list of human-readable labels.

  Args:
    synsets: list of strings; each string is a unique WordNet ID.
    synset_to_human: dict of synset to human labels, e.g.,
      'n02119022' --> 'red fox, Vulpes vulpes'

  Returns:
    List of human-readable strings corresponding to each synset.
  """
    humans = []
    for s in synsets:
        assert s in synset_to_human, 'Failed to find: %s' % s
        humans.append(synset_to_human[s])
    return humans


def broadcast_logical_or(lhs=None, rhs=None, name=None, attr=None, out=None,
    **kwargs):
    """Returns the result of element-wise **logical or** with broadcasting.

    Example::

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

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

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

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

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

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

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


def parse_argument(string):
    """Return a single value for a string understood as a positional
    argument or a |tuple| containing a keyword and its value for a
    string understood as a keyword argument.

    |parse_argument| is intended to be used as a helper function for
    function |execute_scriptfunction| only.  See the following
    examples to see which types of keyword arguments |execute_scriptfunction|
    covers:

    >>> from hydpy.exe.commandtools import parse_argument
    >>> parse_argument('x=3')
    ('x', '3')
    >>> parse_argument('"x=3"')
    '"x=3"'
    >>> parse_argument("'x=3'")
    "'x=3'"
    >>> parse_argument('x="3==3"')
    ('x', '"3==3"')
    >>> parse_argument("x='3==3'")
    ('x', "'3==3'")
    """
    idx_equal = string.find('=')
    if idx_equal == -1:
        return string
    idx_quote = idx_equal + 1
    for quote in ('"', "'"):
        idx = string.find(quote)
        if -1 < idx < idx_quote:
            idx_quote = idx
    if idx_equal < idx_quote:
        return string[:idx_equal], string[idx_equal + 1:]
    return string


@staticmethod
def configure(config_options):
    """ Automatically configures the wallet with the provided configuration options
        """
    raise NotImplementedError(
        'Abstract class, `auto_configure` must be overridden')


def rename_settings(apps, schema_editor):
    """Rename settings."""
    LocalConfig = apps.get_model('core', 'LocalConfig')
    lc = LocalConfig.objects.first()
    condition = (lc and 'admin' in lc._parameters and
        'default_domain_quota' in lc._parameters['admin'])
    if condition:
        lc._parameters['admin']['default_mailbox_quota'] = lc._parameters[
            'admin']['default_domain_quota']
        del lc._parameters['admin']['default_domain_quota']
        lc.save(update_fields=['_parameters'])


def process(mlist, msg, msgdata):
    """Run the handler.

        :param mlist: The mailing list object.
        :param msg: The message object.
        :param msgdata: The message metadata.
        """


def disconnect(code, reason):
    """Return SockJS packet with code and close reason

    `code`
        Closing code
    `reason`
        Closing reason
    """
    return 'c[%d,"%s"]' % (code, reason)


def _NoGradient(name=None, attr=None, out=None, **kwargs):
    """Place holder for variable who cannot perform gradient

    Parameters
    ----------

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

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


@classmethod
def global_config(cls, key, *args):
    """ This reads or sets the global settings stored in class.settings. """
    if args:
        cls.settings = cls.settings.copy()
        cls.settings[key] = args[0]
    else:
        return cls.settings[key]


def bin_to_dec(binary_number):
    """
    Accepts an integer binary number in a string.
    Returns a decimal number.
    """
    bits_list = []
    for bit in binary_number:
        bits_list.append(int(bit))
    decimal_number = 0
    for bit_position, bit in enumerate(reversed(bits_list)):
        decimal_number += bit * 2 ** bit_position
    return decimal_number


def exclude():
    """Returns a list of patterns to exclude from a search. Add
    terms as required."""
    exclude = ['*/alignment*', '*/trash*', '*/_Archived*', '*/backup*',
        '*/Commissioning*', '*/Test*', '*/.AppleDouble*', '*LaserX*',
        '*LaserZ*', '*Copy*', '*._*', '.DS_Store']
    return exclude


def _expand_task_collection(factory):
    """Parse task collection task factory object, and return task list.
    :param dict factory: A loaded JSON task factory object.
    """
    return factory.tasks


def inside(fctn):
    """
    Put two profile points (outer, inner) on the inside of sewing mark
    :param fctn:
    :return:
    """
    return lambda p1, p2: fctn(2 * p1 - p2, p1)


def clone_receipt_rule_set(RuleSetName=None, OriginalRuleSetName=None):
    """
    Creates a receipt rule set by cloning an existing one. All receipt rules and configurations are copied to the new receipt rule set and are completely independent of the source rule set.
    For information about setting up rule sets, see the Amazon SES Developer Guide .
    This action is throttled at one request per second.
    See also: AWS API Documentation

    Examples
    The following example creates a receipt rule set by cloning an existing one:
    Expected Output:

    :example: response = client.clone_receipt_rule_set(
        RuleSetName='string',
        OriginalRuleSetName='string'
    )

    :type RuleSetName: string
    :param RuleSetName: [REQUIRED]
            The name of the rule set to create. The name must:
            Contain only ASCII letters (a-z, A-Z), numbers (0-9), periods (.), underscores (_), or dashes (-).
            Start and end with a letter or number.
            Contain less than 64 characters.

    :type OriginalRuleSetName: string
    :param OriginalRuleSetName: [REQUIRED]
            The name of the rule set to clone.

    :rtype: dict
    :return: {}

    """
    pass


@staticmethod
def get_elastic_mappings(es_major):
    """Get Elasticsearch mapping.

        Non dynamic discovery of type for:
            * data.attachments.ts

        :param es_major: major version of Elasticsearch, as string
        :returns:        dictionary with a key, 'items', with the mapping
        """
    mapping = """
             {
                "dynamic":true,
                    "properties": {
                        "data": {
                            "properties": {
                                "attachments": {
                                    "dynamic":false,
                                    "properties": {}
                                },
                                "channel_info": {
                                    "properties": {
                                        "latest": {
                                            "dynamic": false,
                                            "properties": {}
                                        }
                                    }
                                },
                                "root": {
                                   "dynamic":false,
                                    "properties": {}
                                }
                            }
                        }
                    }
            }
            """
    return {'items': mapping}


def add_labels_to_pull_request(repo, pull_request, *labels):
    """Adds a label to a pull request via the issue object
    Args:
    * repo: Repository object
    * pull_request: ShortPullRequest object that exists in repo
    * labels: list(str) of labels to add to the pull request"""
    issue = repo.issue(pull_request.number)
    issue.add_labels(*labels)


def control_program_action(amount, asset, control_program):
    """
    Get control program action.

    :param amount: bytom amount.
    :type amount: int
    :param asset: bytom asset.
    :type asset: str
    :param control_program: bytom control program.
    :type control_program: str
    :returns: dict -- bytom control program action.

    >>> from shuttle.providers.bytom.utils import control_program_action
    >>> control_program_action(bytom_amount, bytom_asset, bytom_control_program)
    {...}
    """
    return dict(amount=amount, asset=asset, control_program=control_program,
        type=str('control_program'))


def is_overcontact(morphology):
    """
    Is string equal to `over-contact`?
    """
    return morphology in ['over-contact']


def pprint(node, verbose=True):
    """
    Shortcut to pretty print Iteration/Expression trees.
    """
    from devito.ir.iet import printAST
    print(printAST(node, verbose))


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


def strip_usage(s):
    """Strip "usage: " string from beginning of string if present"""
    if s.startswith('usage: '):
        return s.replace('usage: ', '', 1)
    else:
        return s


@staticmethod
def domains(domains):
    """
        list domains:  The list of domains to be pretty printed.
        """
    if 'resData' in domains:
        domains = domains['resData']
    output = '\n%i domains:\n' % len(domains['domain'])
    for domain in domains['domain']:
        output += 'Domain: %s (Status: %s)\n' % (domain['domain'], domain[
            'status'])
    return output


def bC_Na_B3O3OH4_FW86(T, P):
    """c-a: sodium triborate [FW86]."""
    b0 = -0.056
    b1 = -0.91
    b2 = 0
    C0 = 0
    C1 = 0
    alph1 = 2
    alph2 = -9
    omega = -9
    valid = T == 298.15
    return b0, b1, b2, C0, C1, alph1, alph2, omega, valid


@classmethod
def load_from_file(cls, file_path):
    """
        load Image from provided file

        :param file_path: str, path to the file
        :return: Image instance
        """
    raise NotImplementedError('load_from_file method is not implemented')


def need_redirect(resp):
    """ check whether redirect is necessary. """
    if resp.status == 301 or resp.status == 302:
        url = resp.getheader('Location')
        return url


def merge_lines(lines):
    """Merge lines when matching and contextual lines overlap.

    When matching and contextual lines overlap, some lines are
    duplicated. This function merges all lines so that they appear
    only once and have the right "line kind".
    """
    sorted_lines = sorted(lines, key=lambda l: (l[2], l[3]))
    seen = set()
    for repo, filename, line_number, line_kind, line in sorted_lines:
        if line_number in seen:
            continue
        seen.add(line_number)
        yield repo, filename, line_number, line_kind, line


def left_pad(input, length, fill_character=' '):
    """
    Returns a string, which will be padded on the left with characters if necessary.
    If the input string is longer than the specified length, it will be returned unchanged.

    >>> left_pad('foo', 5)
    '  foo'

    >>> left_pad('foobar', 6)
    'foobar'

    >>> left_pad('toolong', 2)
    'toolong'

    >>> left_pad(1, 2, '0')
    '01'

    >>> left_pad(17, 5, 0)
    '00017'

    :param input:
    :param length: The return string's desired length.
    :param fill_character:
    "rtype str:
    """
    return str(input).rjust(length, str(fill_character))


@staticmethod
def parse_docs(docs):
    """returns tuple of (name, docs)"""
    if not docs:
        return __name__, '<no documentation>'
    docs = docs.strip().split('\n')
    for i, line in enumerate(docs):
        docs[i] = line.strip()
    return docs[0], ' '.join(docs[1:]) if len(docs[1:]
        ) else '<no documentation>'


def unquote_header_value(value, is_filename=False):
    """Unquotes a header value.  (Reversal of :func:`quote_header_value`).
    This does not use the real unquoting but what browsers are actually
    using for quoting.

    :param value: the header value to unquote.
    :rtype: str
    """
    if value and value[0] == value[-1] == '"':
        value = value[1:-1]
        if not is_filename or value[:2] != '\\\\':
            return value.replace('\\\\', '\\').replace('\\"', '"')
    return value


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 __ne__(A, other):
    """Returns true if both objects are not equal"""
    return not A == other


def allbindings(obj):
    """ return dict with all attrname/obj bindings for a given obj.

        note that using the builtin vars() would not give the attributes
        of base classes whereas calling 'dir()' gives the names
        of inherited attributes.
    """
    d = {}
    for name in dir(obj):
        try:
            d[name] = getattr(obj, name)
        except:
            pass
    return d


def boolean_flag(name, configurable, set_help='', unset_help=''):
    """Helper for building basic --trait, --no-trait flags.

    Parameters
    ----------

    name : str
        The name of the flag.
    configurable : str
        The 'Class.trait' string of the trait to be set/unset with the flag
    set_help : unicode
        help string for --name flag
    unset_help : unicode
        help string for --no-name flag

    Returns
    -------

    cfg : dict
        A dict with two keys: 'name', and 'no-name', for setting and unsetting
        the trait, respectively.
    """
    set_help = set_help or 'set %s=True' % configurable
    unset_help = unset_help or 'set %s=False' % configurable
    cls, trait = configurable.split('.')
    setter = {cls: {trait: True}}
    unsetter = {cls: {trait: False}}
    return {name: (setter, set_help), ('no-' + name): (unsetter, unset_help)}


def _yes_or_no(input_text) ->bool:
    """prompts for a yes or no choices for |input_text|"""
    choice = ''
    while not choice.startswith(('y', 'n')):
        choice = input(f'{input_text} (y/n)').lower()
    return True if choice.startswith('y') else False


@classmethod
def get_BZ_integration_grid_Y(cls, calc, **kwargs):
    """Return a number of points in the Brillouin zone along reciprocal lattice vector Y."""
    array = cls._get_BZ_integration_grid(calc, **kwargs)
    if array is not None:
        return array[1]


def cmd_appe(args):
    """Append to a file. Write access required.
        """


def takes_args(function):
    """Returns True if the supplied functions takes extra non-keyword arguments"""
    return bool(function.__code__.co_flags & 4)


def build_path_breadcrumb(path):
    """
    Generates a list of tuples as (parent_dir, file_name).
    First list element contains None as parent_dir.

    @param path: is the path string to process
    @return: a list of tuples as (parent_dir, file_name)

    Ex.
        build_path_breadcrumb("/images/foo/bar.jpg")

        returns
        [(None, 'images'), ('images', 'foo'), ('images/foo', 'bar.jpg')]
    """
    if path.startswith('/'):
        path = path[1:]
    l = []
    res = []
    if path.endswith('/'):
        path = path[:-1]
    for d in path.split('/'):
        res.append(('/'.join(l) if l else None, d))
        l.append(d)
    return res


def getParent():
    """Return the parent of this object."""


def migrate_v0_to_v1(project):
    """Migrate from schema version 0 to version 1."""
    pass


@staticmethod
def get_internal_summary(provider):
    """
        SKProductInternal summary.

        :param SKProductInternal.SKProductInternalSyntheticProvider provider: SKProductInternal provider.
        :return: SKProductInternal summary.
        :rtype: str
        """
    return provider.summary()


@classmethod
def new(cls, id=None, **kwargs):
    """
        Creates a new instance, filling out the models data with the keyword
        arguments passed, so long as those keywords are not in the protected
        items array.
        """
    return cls(id=id, **kwargs)


def shape_generator(img, shape):
    """
    From a generator of items (i, r), return
    (i, r.reshape(shape))
    """
    for i, r in img:
        r.shape = shape
        yield i, r


@classmethod
def include(cls, name, range_key, includes):
    """ Create an index that projects key attributes plus some others """
    return cls(cls.INCLUDE, name, range_key, includes)


def add(fn, key):
    """
    Add a key to an `authorized_keys` file.
    """
    if hasattr(fn, 'read'):
        fh = fn
        fh.seek(0, 2)
        close_fh = False
    else:
        fh = open(fn, mode='a+', encoding='utf-8')
        close_fh = True
    try:
        fh.write(key.getvalue())
        fh.write('\n')
    finally:
        if close_fh:
            fh.close()


@classmethod
def remove_event_detect(cls, channel):
    """Remove all callbacks from the event detection for the given gpio channel."""
    cls.CALLBACKS.pop(channel, None)
    cls.MODES.pop(channel, None)


def refine_markers(backwards: bool=False):
    """Refine selected markers positions by running the tracker from track’s reference to current frame

    :param backwards: Backwards, Do backwards tracking
    :type backwards: bool
    """
    pass


def uniq(objs):
    """Eliminates duplicated elements."""
    done = set()
    for obj in objs:
        if obj in done:
            continue
        done.add(obj)
        yield obj
    return


def cut_prefix(value, prefix):
    """Removes the leading `prefix` if exists, else return `value`
    unchanged.

    """
    from xotl.tools.future.codecs import safe_encode, safe_decode
    if isinstance(value, str) and isinstance(prefix, bytes):
        prefix = safe_decode(prefix)
    elif isinstance(value, bytes) and isinstance(prefix, str):
        prefix = safe_encode(prefix)
    return value[len(prefix):] if value.startswith(prefix) else value


def aa_7(provider):
    """Send A-ABORT PDU."""
    provider.dul_socket.send(provider.primitive.encode())
    return 'Sta13'


def win32_restrict_file_to_user(fname):
    """Secure a windows file to read-only access for the user.
    Follows guidance from win32 library creator:
    http://timgolden.me.uk/python/win32_how_do_i/add-security-to-a-file.html

    This method should be executed against an already generated file which
    has no secrets written to it yet.

    Parameters
    ----------

    fname : unicode
        The path to the file to secure
    """
    import win32api
    import win32security
    import ntsecuritycon as con
    admins = win32security.CreateWellKnownSid(win32security.
        WinBuiltinAdministratorsSid)
    user, _domain, _type = win32security.LookupAccountName('', win32api.
        GetUserName())
    sd = win32security.GetFileSecurity(fname, win32security.
        DACL_SECURITY_INFORMATION)
    dacl = win32security.ACL()
    dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.
        FILE_GENERIC_READ | con.FILE_GENERIC_WRITE, user)
    dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.
        FILE_ALL_ACCESS, admins)
    sd.SetSecurityDescriptorDacl(1, dacl, 0)
    win32security.SetFileSecurity(fname, win32security.
        DACL_SECURITY_INFORMATION, sd)


def clean_dist(A):
    """ Clean a distribution to accelerate further computation (drop element of the support with proba less than 2^-300)
    :param A: input law (dictionnary)
    """
    B = {}
    for x, y in A.items():
        if y > 2 ** -300:
            B[x] = y
    return B


def supprime_extension(filename, ext):
    """Supprime l'éventuelle extension ext du nom de fichier filename.

    - ext est une chaîne de caractères quelconque.

    >>> supprime_extension("plop.tex", ".tex")
    'plop'
    >>> supprime_extension("plop.tex", ".pdf")
    'plop.tex'
    """
    if filename.endswith(ext):
        return filename[:-len(ext)]
    return filename


def make_virtual_offset(block_start_offset, within_block_offset):
    """Compute a BGZF virtual offset from block start and within block offsets.

    The BAM indexing scheme records read positions using a 64 bit
    'virtual offset', comprising in C terms:

    block_start_offset << 16 | within_block_offset

    Here block_start_offset is the file offset of the BGZF block
    start (unsigned integer using up to 64-16 = 48 bits), and
    within_block_offset within the (decompressed) block (unsigned
    16 bit integer).

    >>> make_virtual_offset(0, 0)
    0
    >>> make_virtual_offset(0, 1)
    1
    >>> make_virtual_offset(0, 2**16 - 1)
    65535
    >>> make_virtual_offset(0, 2**16)
    Traceback (most recent call last):
    ...
    ValueError: Require 0 <= within_block_offset < 2**16, got 65536

    >>> 65536 == make_virtual_offset(1, 0)
    True
    >>> 65537 == make_virtual_offset(1, 1)
    True
    >>> 131071 == make_virtual_offset(1, 2**16 - 1)
    True

    >>> 6553600000 == make_virtual_offset(100000, 0)
    True
    >>> 6553600001 == make_virtual_offset(100000, 1)
    True
    >>> 6553600010 == make_virtual_offset(100000, 10)
    True

    >>> make_virtual_offset(2**48, 0)
    Traceback (most recent call last):
    ...
    ValueError: Require 0 <= block_start_offset < 2**48, got 281474976710656

    """
    if within_block_offset < 0 or within_block_offset >= 65536:
        raise ValueError('Require 0 <= within_block_offset < 2**16, got %i' %
            within_block_offset)
    if block_start_offset < 0 or block_start_offset >= 281474976710656:
        raise ValueError('Require 0 <= block_start_offset < 2**48, got %i' %
            block_start_offset)
    return block_start_offset << 16 | within_block_offset


def fix_location_header(request, response):
    """
    Ensures that we always use an absolute URI in any location header in the
    response. This is required by RFC 2616, section 14.30.

    Code constructing response objects is free to insert relative paths, as
    this function converts them to absolute paths.
    """
    if 'Location' in response and request.get_host():
        response['Location'] = request.build_absolute_uri(response['Location'])
    return response


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 to_best_int_float(val):
    """Get best type for value between int and float

    :param val: value
    :type val:
    :return: int(float(val)) if int(float(val)) == float(val), else float(val)
    :rtype: int | float

    >>> to_best_int_float("20.1")
    20.1

    >>> to_best_int_float("20.0")
    20

    >>> to_best_int_float("20")
    20
    """
    integer = int(float(val))
    flt = float(val)
    if integer == flt:
        return integer
    return flt


@staticmethod
def detect_file_content(path):
    """ Check if a file is valid entry file, by check its front matter """
    with open(path, 'rt', encoding='utf8') as mf:
        all_lines = ''.join(mf.readlines())
        try:
            start_index = all_lines.index('---\n', 0)
            end_index = all_lines.index('---\n', 4)
            all_lines.index('\nid: ', start_index, end_index)
            all_lines.index('\ntitle: ', start_index, end_index)
        except Exception:
            return False
        else:
            return True


@staticmethod
def user_parse(data):
    """Parse information from provider."""
    yield 'id', data.get('user_id')


@classmethod
def unpack(cls, string):
    """@brief Unpack the runlength encoded table data.

        Terence implemented packed table initializers, because Java has a
        size restriction on .class files and the lookup tables can grow
        pretty large. The generated JavaLexer.java of the Java.g example
        would be about 15MB with uncompressed array initializers.

        Python does not have any size restrictions, but the compilation of
        such large source files seems to be pretty memory hungry. The memory
        consumption of the python process grew to >1.5GB when importing a
        15MB lexer, eating all my swap space and I was to impatient to see,
        if it could finish at all. With packed initializers that are unpacked
        at import time of the lexer module, everything works like a charm.

        """
    ret = []
    for i in range(0, len(string) - 1, 2):
        n, v = ord(string[i]), ord(string[i + 1])
        if v == 65535:
            v = -1
        ret += [v] * n
    return ret


@classmethod
def _days_to_seconds(cls, val, **kwargs):
    """
        converts a number of days to seconds
        """
    zero_value = kwargs.get('zero_value', 0)
    if val is not None:
        if val == 0:
            return zero_value
        return val * 86400
    else:
        return 'Not Defined'


def is_installed():
    """
  Returns whether the C extension is installed correctly.
  """
    try:
        from ccurl import Curl as CCurl
    except ImportError:
        return False
    else:
        from iota.crypto import Curl
        return issubclass(Curl, CCurl)


def convert_to_list(data):
    """Convert a value into a list.

    :param data: The value to convert.
    :return: A new list wrapped around 'data' whereupon the list is empty
        if 'data' is None.
    """
    if data is None:
        return []
    elif hasattr(data, '__iter__') and not isinstance(data, str):
        return list(data)
    else:
        return [data]


@task
def get_tasks():
    """Get all paver-defined tasks."""
    from paver.tasks import environment
    for task in environment.get_tasks():
        print(task.shortname)


def dim_ratios(axis, figsize):
    """Get the proportions of the figure taken up by each axes

    adapted from seaborn
    """
    figdim = figsize[axis]
    dendrogram = min(2.0 / figdim, 0.2)
    colorbar_width = 0.8 * dendrogram
    colorbar_height = 0.2 * dendrogram
    if axis == 0:
        ratios = [colorbar_width, colorbar_height]
    else:
        ratios = [colorbar_height, colorbar_width]
    ratios += [0.8]
    return ratios


def write(packet):
    """
        Write packet to address we are connected to.
        """


def escape_ansi(line):
    """It removes the ansi codes from a string."""
    import re
    ansi_escape = re.compile('(\\x9B|\\x1B\\[)[0-?]*[ -\\/]*[@-~]')
    return ansi_escape.sub('', line)


@staticmethod
def getSiteDomain():
    """Needs to be overriden in each adapter class."""
    return 'no such domain'


def update(res, field, desired_flavor, detected_flavors):
    """Process RES by matching FIELD.attrib['name'] against RES.PATTERN.
    If there is a match M, M.group(2) must evaluate to the name of the
    flavor of the field.  If this flavor matches the specified flavor,
    we have an exact match and RES.value is set to field.text, RES.name
    is set to field.attrib['name'].
    If the flavor of the field is empty and RES.value is None we have a
    default match and RES is updated like for an exact match.
    DETECTED_FLAVOR is a set of flavors found.
    """
    m = res.pattern.match(field.attrib['name'])
    if m is None:
        return
    this_flavor = None
    if m.lastindex is not None:
        this_flavor = m.group(2)
        detected_flavors |= {this_flavor}
    if (this_flavor == desired_flavor or this_flavor is None and res.value is
        None):
        res.name = field.attrib['name']
        res.value = field.text


def _debug(ui, *msg, **opts):
    """write debug output for Phabricator if ``phabricator.debug`` is set

    Specifically, this avoids dumping Conduit and HTTP auth chatter that is
    printed with the --debug argument.
    """
    if ui.configbool(b'phabricator', b'debug'):
        flag = ui.debugflag
        try:
            ui.debugflag = True
            ui.write(*msg, **opts)
        finally:
            ui.debugflag = flag


@classmethod
def not_keyword(cls, f):
    """
        Method to flag a public method as not a keyword. Wrapped by
        not_keyword function as a decorator. In is_method_excluded we'll check this.
        :param f: The function to designate as not a keyword
        :type f: callable
        :returns: callable

        """
    cls._exclusions[f.__name__] = True
    return f


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 real_ip(request):
    """
    Behind a Wsgi (Nginx) server.
    """
    return request.META.get('HTTP_X_REAL_IP') or None


def string_to_hex(st, length):
    """ Convert a string like 00:00 in to hex 0x0000 format"""
    tmp = '{0:#x}'.format(int(st.replace(':', '').lstrip('0'), length))
    return tmp


def transform_point(point, matrix):
    """Transform point by matrix.

    :param list point: 2-item list
    :param list matrix: 6-item list representing transformation matrix
    :returns list: 2-item transformed point
    """
    x, y = point
    a, b, c, d, e, f = matrix
    new_x = x * a + y * c + e
    new_y = x * b + y * d + f
    return [new_x, new_y]


def shell_escape_single_quote(command):
    """Escape single quotes for use in a shell single quoted string
    Explanation:

    (1) End first quotation which uses single quotes.
    (2) Start second quotation, using double-quotes.
    (3) Quoted character.
    (4) End second quotation, using double-quotes.
    (5) Start third quotation, using single quotes.

    If you do not place any whitespaces between (1) and (2), or between
    (4) and (5), the shell will interpret that string as a one long word
    """
    return command.replace("'", '\'"\'"\'')


def convert_post_to_VERB(request, verb):
    """
    Force Django to process the VERB.
    """
    if request.method == verb:
        if not hasattr(request, '_read_started'):
            request._read_started = False
        if hasattr(request, '_post'):
            del request._post
            del request._files
        try:
            request.method = 'POST'
            request._load_post_and_files()
            request.method = verb
        except AttributeError:
            request.META['REQUEST_METHOD'] = 'POST'
            request._load_post_and_files()
            request.META['REQUEST_METHOD'] = verb
        setattr(request, verb, request.POST)
    return request


def check_received_length(sock, length):
    """
    Make sure all bytes (length) that should be received are received through the socket
    Parameters
    ----------
    sock
    length

    Returns
    -------

    """
    l = 0
    data_bytes = b''
    while l < length:
        if l < length - 4096:
            data_bytes_tmp = sock.recv(4096)
        else:
            data_bytes_tmp = sock.recv(length - l)
        l += len(data_bytes_tmp)
        data_bytes += data_bytes_tmp
    return data_bytes


def rk_dependency_graph(rkm):
    """Construct the dependency graph of the stages of a Runge-Kutta method."""
    import networkx as nx
    import numpy as np
    s = len(rkm)
    K = np.zeros((s + 1, s + 1))
    K[:, :-1] = np.vstack((rkm.A, rkm.b))
    G = nx.from_numpy_matrix(np.abs(K.T) > 0, nx.DiGraph())
    return G


def get(key, default=None):
    """Return the annotation stored under key, or default if not found.
        """


def ReducedElectricalField(E, p=101325, T=293.15):
    """Reduced electric field strength calculation using ideal gas law.

    Args:
        E       :   Electric field.
        p       :   Ambient pressure during discharge.
        T       :   Ambient temperature during discharge.

    Returns:
        Returns the reduced electric field strength.
    """
    kB = 1.3806485279e-23
    N = p / (kB * T)
    Td = E / N
    return Td / 1e-20


def bytesToNumber(bytes):
    """Convert a sequence of bytes (eg bytearray) into integer."""
    total = 0
    multiplier = 1
    for count in range(len(bytes) - 1, -1, -1):
        byte = bytes[count]
        total += multiplier * byte
        multiplier *= 256
    return total


@classmethod
def from_existing(cls, keystone_session, id):
    """
        Attach to an existing lease by ID. When using in conjunction with the
        context manager, it will *not* delete the lease at the end.
        """
    return cls(keystone_session, _preexisting=True, _id=id)


def get_vert_ngrams_center(c):
    """Not implemented."""
    return


def push(cpu, value, size):
    """
        Writes a value in the stack.

        :param value: the value to put in the stack.
        :param size: the size of the value.
        """
    assert size in (8, 16, cpu.address_bit_size)
    cpu.STACK = cpu.STACK - size // 8
    base, _, _ = cpu.get_descriptor(cpu.read_register('SS'))
    address = cpu.STACK + base
    cpu.write_int(address, value, size)


def _improve_answer_span(doc_tokens, input_start, input_end, tokenizer,
    orig_answer_text):
    """Returns tokenized answer spans that better match the annotated answer."""
    tok_answer_text = ' '.join(tokenizer.tokenize(orig_answer_text))
    for new_start in range(input_start, input_end + 1):
        for new_end in range(input_end, new_start - 1, -1):
            text_span = ' '.join(doc_tokens[new_start:new_end + 1])
            if text_span == tok_answer_text:
                return new_start, new_end
    return input_start, input_end


def strtobytes(s):
    """Return the bytes of the string representation of an object."""
    return str(s).encode('latin-1')


@classmethod
def add_op2_data(cls, data, comment=''):
    """
        Adds a CPLSTN6 card from the OP2

        Parameters
        ----------
        data : List[varies]
            a list of fields defined in OP2 format
        comment : str; default=''
            a comment for the card

        """
    eid = data[0]
    pid = data[1]
    nids = data[2:8]
    theta = data[8]
    assert len(nids) == 6, 'error on CPLSTN6'
    return cls(eid, pid, nids, theta, comment=comment)


def create_median_otsu_brain_mask(dwi_info, protocol, output_fname=None, **
    kwargs):
    """Create a brain mask and optionally write it.

    It will always return the mask. If output_fname is set it will also write the mask.

    Args:
        dwi_info (string or tuple or image): the dwi info, either:

            - the filename of the input file;
            - or a tuple with as first index a ndarray with the DWI and as second index the header;
            - or only the image as an ndarray

        protocol (string or :class:`~mdt.protocols.Protocol`): The filename of the protocol file or a Protocol object
        output_fname (string): the filename of the output file. If None, no output is written.
            If dwi_info is only an image also no file is written.
        **kwargs: the additional arguments for the function median_otsu.

    Returns:
        ndarray: The created brain mask
    """
    from mdt.lib.masking import create_median_otsu_brain_mask, create_write_median_otsu_brain_mask
    if output_fname:
        if not isinstance(dwi_info, (str, tuple, list)):
            raise ValueError(
                'No header obtainable, can not write the brain mask.')
        return create_write_median_otsu_brain_mask(dwi_info, protocol,
            output_fname, **kwargs)
    return create_median_otsu_brain_mask(dwi_info, protocol, **kwargs)


def choplist(n, seq):
    """Groups every n elements of the list."""
    r = []
    for x in seq:
        r.append(x)
        if len(r) == n:
            yield tuple(r)
            r = []
    return


def _count0Bits(num):
    """Find the highest bit set to 0 in an integer."""
    num = int(num)
    if num < 0:
        raise ValueError('Only positive Numbers please: %s' % num)
    ret = 0
    while num > 0:
        if num & 1 == 1:
            break
        num = num >> 1
        ret += 1
    return ret


@staticmethod
def _img_invert(img_thr):
    """Invert boolean image.

        Parameters
        ----------
        img_thr : NumPy array
            Boolean image in NumPy format.

        """
    img_inv = (~img_thr.astype(bool)).astype(int)
    return img_inv


def wind_speed_to_hour_flux(wind_speed: float) ->float:
    """
    | Converts wind speed into a hourly flux.
    | m/s to m\\ :sup:`3` /h =>
    | m/s to m\\ :sup:`3` /s = 1\\ :sup:`2`
    | m\\ :sup:`3`/s to m\\ :sup:`3`/h = 3600s/h

    :param wind_speed: Wind speed in m/s
    :type wind_speed: float
    :return: Wind flux in m\\ :sup:`3`/h
    :rtype: float
    """
    return wind_speed * 3600


def get_iteration(conn, config, **kwargs):
    """Get data from every interation of each method for each instance

    Parameter:
        - conn (object): mysql connection
        - config (dict): configuration of experiment
        - kwargs:
            + method_id
            + instance_id
    """
    cur = conn.cursor()
    values = []
    for key in kwargs:
        values.append('%s=%d' % (key, kwargs[key]))
    query = 'SELECT * FROM iteration WHERE %s ' % ' AND '.join(values)
    cur.execute(query)
    for _ in cur.fetchall():
        yield _


def breakpoint():
    """
    Adds a breakpoint to the debug map
    """
    pass


def Access(NewType, Accessed):
    """
	Return code for VHDL Access declaration of type pointer.
	"""
    return '\ntype {0} is access {1};'.format(NewType, Accessed)


def _normalize_validate(validate):
    """
    Coerces the validate attribute on a Marshmallow field to a consistent type.

    The validate attribute on a Marshmallow field can either be a single
    Validator or a collection of Validators.

    :param Validator|list[Validator] validate:
    :rtype: list[Validator]
    """
    if callable(validate):
        return [validate]
    else:
        return validate


def get_match(match, index, default=''):
    """
    Returns a value from match list for a given index. In the list is out of
    bounds `default` is returned.

    """
    if index >= len(match):
        return default
    else:
        return match[index]


def req_yaml_template(pip=False, version=True, build=False):
    """
    Builds a template string for requirement lines in the YAML format.

    :pip: [bool] Whether to build a Conda requirement line or a pip line
    :version: [bool] Includes the version template
    :build: [bool] Includes the build template.  Makes version "=*" if the
        `version` is False for Conda packages.

    :returns: [str] The requirement string template.
    """
    template_str = '{name}'
    if version:
        template_str += '=={version}' if pip else '={version}'
    if build and not pip:
        if not version:
            template_str += '=*'
        template_str += '={build_string}'
    return template_str


def _parens_around_char(label):
    """Place parens around first character of label.

    :param str label: Must contain at least one character

    """
    return '({first}){rest}'.format(first=label[0], rest=label[1:])


def p_compound_stmt_3(p):
    """compound_stmt : for_stmt"""
    p[0] = [p[1]]


def context_path(context):
    """Returns the physical path for brains and objects alike."""
    if hasattr(context, 'getPath'):
        return context.getPath().split('/')
    else:
        return context.getPhysicalPath()


def mach_from_pr(p_c, p_e, gamma):
    """Find the exit Mach number from the pressure ratio.

    Arguments:
        p_c (scalar): Nozzle stagnation chamber pressure [units: pascal].
        p_e (scalar): Nozzle exit static pressure [units: pascal].
        gamma (scalar): Exhaust gas ratio of specific heats [units: dimensionless].

    Returns:
        scalar: Exit Mach number [units: dimensionless].
    """
    return (2 / (gamma - 1) * ((p_e / p_c) ** ((1 - gamma) / gamma) - 1)
        ) ** 0.5


def normalize_phone_number(phone_number):
    """
	Normalize the phone number by removing all spaces and dashes.
	"""
    if phone_number is None:
        return None
    return phone_number.strip().replace(' ', '').replace('-', '')


@classmethod
def general(cls):
    """A factory method that creates a :class:`Permissions` with all
        "General" permissions from the official Discord UI set to True."""
    return cls(2080374975)


def rawX10_to_bytes(rawX10):
    """Return the byte value of a raw X10 command."""
    yield rawX10 >> 4
    yield rawX10 & 15


@staticmethod
def create(**kwargs):
    """Create an empty map object.

        This method accepts generic options listed below, as well as options
        for `HpxMap` and `WcsMap` objects. For WCS-specific options, see
        `WcsMap.create` and for HPX-specific options, see `HpxMap.create`.

        Parameters
        ----------
        frame : str
            Coordinate system, either Galactic ("galactic") or Equatorial
            ("icrs").
        map_type : {'wcs', 'wcs-sparse', 'hpx', 'hpx-sparse', 'region'}
            Map type.  Selects the class that will be used to
            instantiate the map.
        binsz : float or `~numpy.ndarray`
            Pixel size in degrees.
        skydir : `~astropy.coordinates.SkyCoord`
            Coordinate of map center.
        axes : list
            List of `~MapAxis` objects for each non-spatial dimension.
            If None then the map will be a 2D image.
        dtype : str
            Data type, default is 'float32'
        unit : str or `~astropy.units.Unit`
            Data unit.
        meta : `dict`
            Dictionary to store meta data.
        region : `~regions.SkyRegion`
            Sky region used for the region map.

        Returns
        -------
        map : `Map`
            Empty map object.
        """
    from .hpxmap import HpxMap
    from .wcsmap import WcsMap
    from .regionnd import RegionNDMap
    map_type = kwargs.setdefault('map_type', 'wcs')
    if 'wcs' in map_type.lower():
        return WcsMap.create(**kwargs)
    elif 'hpx' in map_type.lower():
        return HpxMap.create(**kwargs)
    elif map_type == 'region':
        _ = kwargs.pop('map_type')
        return RegionNDMap.create(**kwargs)
    else:
        raise ValueError(f'Unrecognized map type: {map_type!r}')


def _find_temp_tensors(grads, forward_ops):
    """
    find all the tensors that are used for computing grads and has been
    computed during forward
    :param grads:
    :param forward_ops:
    :return:
    """
    import sys
    is_py2 = sys.version[0] == '2'
    if is_py2:
        import Queue as queue
    else:
        import queue as queue
    queue = queue.Queue()
    for grad in grads:
        queue.put(grad)
    temp_tensors = set()
    visited = set()
    while not queue.empty():
        tensor = queue.get()
        if tensor is None:
            continue
        else:
            visited.add(tensor.name)
            if tensor.op.type == 'Placeholder':
                continue
            if tensor.op.name in forward_ops:
                temp_tensors.add(tensor.name)
                continue
            for input_tensor in tensor.op.inputs:
                if input_tensor.name not in visited:
                    queue.put(input_tensor)
    return temp_tensors