def from_export(lines, passage_id=None, return_original=False, **kwargs):
    """Converts from parsed text in NeGra export format to a Passage object.

    :param lines: iterable of lines in NeGra export format, describing a single passage.
    :param passage_id: ID to set for passage, overriding the ID from the file
    :param return_original: return triple of (UCCA passage, Export string, sentence ID)

    :return generator of Passage objects
    """
    from semstr.conversion.export import ExportConverter
    return ExportConverter().from_format(lines, passage_id=passage_id,
        return_original=return_original, format=kwargs.get('format'))


@classmethod
def get_display_name(klass):
    """
        Default getter function - returns the `display_name` property
        """
    return klass.display_name


def reduce_func(db, funclist, sep):
    """
    Take a set of functional terms and return only the
     terms that are not the ancestor of any
    other term in the set.

    :param db: reference database
    :param funclist: string that contains functional terms separated by <sep>
    :param sep: character separating the terms in the list
    :return:
    """
    split = set(funclist.split(sep))
    reduced = split.copy()
    for goid in split:
        ancestors = db.get_ancestors(goid)
        reduced.difference_update(ancestors)
    pasted = sep.join(reduced)
    return pasted


def sorted_lines(file):
    """
    Lines of file, sorted.

    Parameters
    ----------
    file : ~pathlib.Path
        Path to file whose lines to read.

    Returns
    -------
    ~typing.List[str]
        Sorted lines of file.
    """
    return sorted(file.read_text().splitlines())


def _get_pull_request(remote_repo, branch_name, base_name):
    """
    Gets the active pull request with the given branch and base

    :param Repository remote_repo:
    :param unicode branch_name:
    :param unicode base_name:
    :return:
    """
    all_active_pull_request = remote_repo.get_pulls(state='open')
    for pull_request in all_active_pull_request:
        if (pull_request.base.ref == base_name and pull_request.head.ref ==
            branch_name):
            return pull_request


def format_output(groups, flag):
    """return formatted string for instance"""
    out = []
    line_format = '{0}\t{1}\t{2}\t{3}\t{4}\t{5}'
    for g in groups['AutoScalingGroups']:
        out.append(line_format.format(g['AutoScalingGroupName'], g[
            'LaunchConfigurationName'], 'desired:' + str(g[
            'DesiredCapacity']), 'max:' + str(g['MaxSize']), 'min:' + str(g
            ['MinSize']), g['CreatedTime'].strftime('%Y/%m/%d %H:%M:%S')))
    return out


def append_lists(l1, l2, **kwargs):
    """
    Appends the values in one list on the end of another list
    """
    l1.extend(l2)
    return l1


def lorem_ipsum(num_sentences=None, num_paragraphs=None):
    """
    Return the given amount of "Lorem ipsum..." text.
    """
    paragraphs = [
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam justo sem, malesuada ut ultricies ac, bibendum eu neque. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean at tellus ut velit dignissim tincidunt. Curabitur euismod laoreet orci semper dignissim. Suspendisse potenti. Vivamus sed enim quis dui pulvinar pharetra. Duis condimentum ultricies ipsum, sed ornare leo vestibulum vitae. Sed ut justo massa, varius molestie diam. Sed lacus quam, tempor in dictum sed, posuere et diam. Maecenas tincidunt enim elementum turpis blandit tempus. Nam lectus justo, adipiscing vitae ultricies egestas, porta nec diam. Aenean ac neque tortor. Cras tempus lacus nec leo ultrices suscipit. Etiam sed aliquam tortor. Duis lacus metus, euismod ut viverra sit amet, pulvinar sed urna.'
        ,
        'Aenean ut metus in arcu mattis iaculis quis eu nisl. Donec ornare, massa ut vestibulum vestibulum, metus sapien pretium ante, eu vulputate lorem augue vestibulum orci. Donec consequat aliquam sagittis. Sed in tellus pretium tortor hendrerit cursus congue sit amet turpis. Sed neque lacus, lacinia ut consectetur eget, faucibus vitae lacus. Integer eu purus ac purus tempus mollis non sed dui. Vestibulum volutpat erat magna. Etiam nisl eros, eleifend a viverra sed, interdum sollicitudin erat. Integer a orci in dolor suscipit cursus. Maecenas hendrerit neque odio. Nulla orci orci, varius id viverra in, molestie vel lacus. Donec at odio quis augue bibendum lobortis nec ac urna. Ut lacinia hendrerit tortor mattis rhoncus. Proin nunc tortor, congue ac adipiscing sit amet, aliquet in lorem. Nulla blandit tempor arcu, ut tempus quam posuere eu. In magna neque, venenatis nec tincidunt vitae, lobortis eget nulla.'
        ,
        'Praesent sit amet nibh turpis, vitae lacinia metus. Ut nisi lacus, feugiat quis feugiat nec, pretium a diam. Aenean bibendum sem eget lorem ullamcorper mattis. Donec elementum purus vel felis vulputate pretium. Duis in ipsum est. Nulla consequat tempor sodales. Donec scelerisque enim eu tellus eleifend imperdiet. Quisque ullamcorper bibendum justo sit amet tincidunt. Donec tempus lacus quis diam varius placerat. Cras metus magna, congue sit amet pulvinar viverra, laoreet vel felis. Praesent sit amet consequat enim. Phasellus arcu nisl, volutpat et molestie a, sagittis a est. Maecenas tincidunt, sem non pharetra mollis, diam nisl ornare tellus, at euismod libero arcu ornare risus. Vestibulum laoreet sollicitudin purus in pharetra. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.'
        ,
        'Nullam pellentesque tempor bibendum. Praesent dictum turpis nec quam consectetur aliquam. Aliquam id turpis nunc. Pellentesque fermentum lacus at tortor auctor venenatis. Maecenas blandit dui lectus. Nunc pellentesque pharetra suscipit. Nullam et metus diam, a congue leo. Curabitur convallis augue in lectus scelerisque non rhoncus lorem molestie. Curabitur in mi a erat dictum pharetra iaculis eu diam.'
        ,
        'Nunc lorem magna, rhoncus sodales mattis quis, tincidunt eu mi. In ultrices, lectus ac porttitor tempor, odio nibh facilisis tortor, ac aliquet nisi ante non felis. Praesent ligula nisl, hendrerit ac volutpat non, varius quis tellus. Sed ornare faucibus elit eget faucibus. Nullam sem tellus, commodo id ullamcorper ut, imperdiet ac eros. Sed quis lorem id urna cursus laoreet et eget lacus. Nullam tristique semper sem, eget tempus sem pellentesque sit amet. Donec sed orci augue, convallis tempor tellus. Sed consequat commodo ante a pretium. Nulla et est mauris. Nullam at massa justo. Proin tempor arcu ac eros suscipit varius. Fusce vestibulum quam placerat tellus imperdiet et venenatis diam tristique. Sed pretium tempor tellus, consequat pulvinar massa pellentesque a.'
        ,
        'Nulla et lorem vel urna fringilla malesuada ut sit amet tortor. Donec id leo mi. Proin sagittis blandit lacus, placerat imperdiet justo pellentesque ac. Cras iaculis aliquam faucibus. Aenean urna nisi, laoreet ac fringilla dignissim, lacinia eget orci. Vivamus porta lacinia dapibus. Aenean molestie, augue sit amet blandit suscipit, tellus turpis ullamcorper purus, ut pretium turpis lorem quis neque. Pellentesque porta dui at arcu mollis tristique. Suspendisse feugiat felis quis felis sollicitudin porttitor.'
        ,
        'Morbi vestibulum, massa quis posuere facilisis, quam lacus porttitor tortor, id fringilla elit velit ac felis. Fusce at luctus risus. Mauris bibendum diam quis odio auctor quis porta massa pellentesque. Proin congue, nisl eu feugiat faucibus, justo orci congue neque, a porta tellus ipsum accumsan turpis. Ut neque enim, dignissim nec fermentum sed, laoreet id orci. Duis fringilla, elit vel tempus porttitor, purus tellus dapibus nisl, eu scelerisque diam lorem vel ante. Ut tempor, urna nec bibendum facilisis, sapien dui ornare lectus, at tempor ligula diam sit amet ligula. Sed a dui in ipsum eleifend egestas.'
        ,
        'Quisque ornare fringilla velit, et tincidunt purus convallis vel. Sed venenatis, risus vitae volutpat rhoncus, sapien lorem lacinia elit, id dictum sapien dui vitae lorem. Praesent aliquet accumsan eros quis tempor. Suspendisse eget justo quis arcu bibendum adipiscing. Phasellus quis erat nec massa elementum porta. Nam venenatis elementum mi vel porta. Nunc vel augue non tellus euismod convallis. Curabitur commodo augue vel augue ultrices in fringilla nunc cursus. Mauris auctor laoreet neque, id gravida velit suscipit eget. Maecenas eget libero in lacus auctor feugiat. Pellentesque in lectus felis, eu dictum tortor. Aenean sagittis, massa malesuada dapibus tincidunt, leo massa imperdiet ante, nec mollis nisl turpis in orci. Proin ut purus et eros sagittis volutpat.'
        ,
        'Donec molestie sem et metus bibendum convallis semper arcu imperdiet. Curabitur quam libero, fermentum vel adipiscing a, cursus at neque. Maecenas cursus risus vestibulum diam ultricies rutrum. Nullam in enim vel lorem accumsan pulvinar. Cras eget viverra turpis. Sed eget lectus urna, eget venenatis libero. Donec porta libero eu est pulvinar pretium. Ut lectus arcu, aliquam et vestibulum euismod, mattis at orci. Fusce dolor lorem, bibendum a dignissim ut, facilisis eu enim. Morbi erat nibh, interdum non ultricies non, porta ac lacus. Curabitur et nunc nec turpis convallis ullamcorper eget vitae mi.'
        ,
        'Curabitur porta molestie sapien, non rhoncus turpis gravida vel. Ut est lacus, elementum eu pretium sit amet, tristique vel orci. Praesent quis suscipit urna. Donec pellentesque molestie tellus sit amet fringilla. Etiam tempus viverra ipsum et tempus. Nunc ut odio imperdiet lorem malesuada bibendum. In aliquam ligula eu sem ullamcorper pulvinar. Quisque sollicitudin placerat dolor et porttitor. Nulla adipiscing lorem id libero aliquet interdum. Suspendisse vehicula fermentum congue. Cras fringilla nisl vitae lectus mollis viverra. Aliquam pharetra lobortis risus, a elementum elit condimentum in. Aenean tincidunt varius faucibus. Nulla non nisi lorem. Suspendisse id sapien a enim lobortis aliquam.'
        ,
        'Aliquam erat volutpat. Maecenas neque leo, mattis eu pretium vel, mattis in ante. Nullam sagittis leo diam. Quisque tempor magna in justo vestibulum eget egestas nibh pellentesque. Pellentesque in enim vitae velit pellentesque hendrerit. Cras ultricies, dui et imperdiet gravida, nunc nisl cursus tortor, sit amet porttitor dolor nibh a justo. Praesent ut mauris vitae turpis lobortis scelerisque a nec ligula. Donec turpis erat, iaculis vel dapibus vel, varius id lorem. Integer et enim erat, at eleifend libero.'
        ,
        'Phasellus id mi ut nunc cursus pellentesque. Aliquam erat volutpat. Vivamus pretium posuere tellus, ac aliquet metus iaculis eget. Curabitur in mi enim. Duis pretium pretium dui, ut iaculis ipsum scelerisque ut. Proin quam dolor, eleifend et porta vitae, cursus molestie lectus. Aenean dignissim laoreet consectetur. Cras iaculis, lectus imperdiet condimentum suscipit, metus nisi egestas arcu, in tempus sem ipsum eu eros. Vestibulum a orci in elit congue euismod quis quis nisi.'
        ,
        'In quis urna leo, at malesuada ipsum. Vestibulum sollicitudin ullamcorper hendrerit. Vestibulum vestibulum mi sodales nulla sagittis commodo. Maecenas nisi lorem, placerat vel aliquet quis, dictum ac ligula. Vestibulum egestas accumsan accumsan. Aenean lobortis pharetra erat convallis pretium. Aliquam consequat facilisis porta. Cras hendrerit nunc et mauris egestas hendrerit. Proin rhoncus, mi id ullamcorper pharetra, ipsum sapien blandit turpis, et ultricies purus neque eget justo. Quisque sodales, nisi in cursus rutrum, elit nibh volutpat lacus, nec sollicitudin erat leo at lectus. Morbi ac dolor mi, vel ultricies quam.'
        ,
        'Sed hendrerit nisl id lectus cursus in adipiscing lorem rutrum. Morbi nisl justo, egestas ac aliquet at, scelerisque luctus sapien. Donec sollicitudin elementum mattis. Praesent semper, ante euismod accumsan gravida, ante neque convallis augue, quis vulputate erat nunc vitae tellus. Duis ac lectus ullamcorper purus commodo luctus. Etiam quis augue in purus molestie imperdiet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam posuere commodo turpis, at pulvinar tortor scelerisque et. Nam vulputate dui sed magna interdum sollicitudin. Nam pulvinar euismod enim vitae malesuada. Aenean non molestie leo. Pellentesque quis lacus mi, et ornare nibh. Etiam pharetra, odio vitae euismod faucibus, nunc urna pulvinar felis, eget molestie est enim sit amet sapien. Vivamus eu neque nulla.'
        ,
        'Mauris eget nibh ut augue malesuada tristique nec quis urna. Vestibulum faucibus, mauris sed posuere volutpat, felis lacus vulputate felis, eget luctus lorem nulla sed velit. Proin et purus nec quam tristique cursus. Nullam adipiscing tortor imperdiet purus facilisis eu luctus nulla vestibulum. Sed pulvinar risus sollicitudin risus fringilla et hendrerit lorem accumsan. Vestibulum venenatis est sit amet nunc gravida nec aliquam arcu adipiscing. Nam quis aliquet mauris. Cras nec neque vitae tellus posuere posuere.'
        ,
        'Nulla facilisi. Vestibulum sit amet dui turpis. Aliquam erat volutpat. In hac habitasse platea dictumst. Morbi in enim nec massa semper tincidunt. Ut fermentum iaculis dui, sed adipiscing dolor porta at. Nam hendrerit libero non nisi ornare eu cursus mauris accumsan. Ut ullamcorper, odio vel ultrices suscipit, metus libero ornare dui, non dapibus est dui vehicula ipsum.'
        ,
        'Nam diam sapien, lacinia vel sollicitudin interdum, faucibus aliquam enim. Mauris tristique iaculis purus eu lacinia. Suspendisse condimentum, dolor a euismod lacinia, leo orci pellentesque orci, non rhoncus turpis lorem sed lacus. Integer velit nisl, rutrum sit amet posuere at, vulputate ultrices tortor. Nullam pharetra, orci tempor dapibus elementum, felis nulla lacinia nunc, quis ultricies dui lectus dictum diam. Praesent eu velit magna, eu lacinia leo. Duis sit amet bibendum dui. Duis tincidunt vulputate dolor eu euismod. Pellentesque nisl sem, mollis ac venenatis a, facilisis vitae ligula. Vivamus sem leo, vestibulum tincidunt iaculis nec, tristique tincidunt mi. Suspendisse imperdiet elit vitae turpis ullamcorper luctus. Aenean in augue mauris. Vivamus nisi libero, dignissim non consectetur sodales, fermentum at sem. Nulla tincidunt fringilla justo quis pulvinar. Nam ac sem sed diam pellentesque egestas vitae ac nisi. Praesent scelerisque dapibus mi vitae tempor.'
        ,
        'Donec tempor, massa non pulvinar suscipit, justo dolor pharetra nisl, ut semper libero lorem non tortor. Integer dapibus arcu viverra nisi hendrerit mattis et ut mauris. Maecenas pulvinar, orci vitae ultricies egestas, orci nisi rutrum justo, eu volutpat nibh odio ac purus. Nulla pellentesque sem eget arcu imperdiet ullamcorper. Curabitur nec magna massa. Morbi lobortis urna sed ligula commodo viverra. Pellentesque molestie, ipsum nec faucibus mollis, neque purus sodales sapien, in convallis nisi libero et lorem. Ut sed rutrum leo. Aliquam eleifend, felis quis ullamcorper consequat, dolor mi vulputate ipsum, lobortis ultricies felis nulla at augue.'
        ,
        'Ut gravida porttitor arcu, malesuada mollis urna vehicula nec. Suspendisse sagittis nulla condimentum libero lacinia sed dapibus dui egestas. Etiam convallis congue ipsum, eu fermentum turpis rutrum id. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Ut nunc eros, sagittis a venenatis et, interdum in leo. Curabitur urna magna, volutpat in mattis ut, adipiscing et ligula. Nam dignissim mattis accumsan. Nulla vehicula felis vel turpis tempus hendrerit. Phasellus rhoncus vulputate massa, tincidunt euismod dui porttitor ac. Sed ut sapien quam, ac egestas odio. Pellentesque at aliquet ante. Donec rhoncus ornare lacus eu ullamcorper. Vestibulum sit amet hendrerit magna. Nulla sed diam nulla.'
        ,
        'Nulla vestibulum sagittis arcu in egestas. Aliquam sed ante justo. Quisque nec dolor nibh, sed feugiat mi. Etiam lorem elit, interdum eu tempor nec, tincidunt eu risus. Fusce id libero augue. Curabitur ultrices, lorem eget mollis fringilla, dolor leo euismod tellus, congue luctus nisi purus vitae urna. Suspendisse tempor orci accumsan sem pretium at accumsan augue tristique. Proin sed turpis at mi feugiat lacinia a nec sem. Suspendisse vel facilisis leo. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Duis ornare enim nec ante adipiscing tincidunt. Maecenas ut justo iaculis leo vestibulum blandit quis vitae mauris. Proin in vestibulum massa.'
        ]
    if num_paragraphs:
        paragraphs = paragraphs[:num_paragraphs]
    text = '\n\n'.join(paragraphs)
    sentences = text.split('.')
    if num_sentences:
        sentences = sentences[:num_sentences]
    lorem = '.'.join(sentences).strip()
    if not lorem.endswith('.'):
        lorem += '.'
    return lorem


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


def append_artist(songs, artist):
    """
    When the songs gathered from the description just contains the
    titles of the songs usually means it's an artist's album.
    If an artist was provided appends the song title to the artist
    using a hyphen (artist - song)
    :param list songs: List of song titles (only song title)
    :param str artist: Artist to search for with the song names
    :return list: song titles along with the artist
    """
    songs_complete = []
    for song in songs:
        song_complete = f'{artist} - {song}'
        songs_complete.append(song_complete)
    return songs_complete


def replace_header(headers, name, value):
    """
    Updates the headers replacing the first occurance of the given name
    with the value provided; asserting that no further occurances
    happen. Note that this is _not_ the same as remove_header and then
    append, as two distinct operations (del followed by an append) are
    not atomic in a threaded environment. Returns the previous header
    value for the provided name, if any.   Clearly one should not use
    this function with ``set-cookie`` or other names that may have more
    than one occurance in the headers.
    """
    name = name.lower()
    i = 0
    result = None
    while i < len(headers):
        if headers[i][0].lower() == name:
            assert not result, "two values for the header '%s' found" % name
            result = headers[i][1]
            headers[i] = name, value
        i += 1
    if not result:
        headers.append((name, value))
    return result


def readConnectionLost():
    """
        Notification of the read connection being closed.

        This indicates peer did half-close of write side. It is now
        the responsibility of the this protocol to call
        loseConnection().  In addition, the protocol MUST make sure a
        reference to it still exists (i.e. by doing a callLater with
        one of its methods, etc.)  as the reactor will only have a
        reference to it if it is writing.

        If the protocol does not do so, it might get garbage collected
        without the connectionLost method ever being called.
        """


def reachedLimit(parameter, analyzer):
    """Checks if the set limit has been reached

  :rtype: bool
  """
    if analyzer.query_size() == analyzer.size():
        return True
    if not parameter.retmax:
        return False
    if analyzer.size() == parameter.retmax:
        return True
    return False


def get_telescope(channel):
    """Identify if a channel is in SAT or LAT

    Parameters
    ----------
    channel : str
        channel tag, e.g. MFH1

    Returns
    -------
    telescope : str
        'SAT' or 'LAT'
    """
    return channel[-2] + 'AT'


def is_namespace(nameStack):
    """Determines if a namespace is being specified"""
    if len(nameStack) == 0:
        return False
    if nameStack[0] == 'namespace':
        return True
    return False


def contains_true(i):
    """Return True if input contains True

    i -- any object that implements __contains__

    Returns True if True is in the input. Both True and 1 count as True.
    Helper function for ConflictMatrix object.
    """
    try:
        if True in i:
            return True
        return False
    except TypeError:
        return False


def exists(key) ->dict:
    """Return condition for the existence of a key"""
    return {key: {'$exists': True}}


def is_between(start, middle, end):
    """Return True if `middle` is between `start` and `end`."""
    return start <= middle and middle <= end


def display_init_git_library(root):
    """ the display function for cloning a git repo"""
    print('Created git library at %s' % root)


def limit(requestContext, seriesList, n):
    """
    Takes one metric or a wildcard seriesList followed by an integer N.

    Only draw the first N metrics. Useful when testing a wildcard in a
    metric.

    Example::

        &target=limit(server*.instance*.memory.free,5)

    Draws only the first 5 instance's memory free.

    """
    return seriesList[0:n]


def collection_json():
    """Returns JSON skeleton for Postman schema."""
    collection_json = {'info': {'name': 'Test_collection', 'description':
        'Generic text used for documentation.', 'schema':
        'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
        }, 'item': []}
    return collection_json


@classmethod
def all(cls, storage_pool):
    """Get all the volumnes for this storage pool.

        Implements GET /1.0/storage-pools/<name>/volumes

        Volumes returned from this method will only have the name
        set, as that is the only property returned from LXD. If more
        information is needed, `StorageVolume.sync` is the method call
        that should be used.

        Note that the storage volume types are 'container', 'image' and
        'custom', and these maps to the names 'containers', 'images' and
        everything else is mapped to 'custom'.

        :param storage_pool: a storage pool object on which to fetch resources
        :type storage_pool: :class:`pylxd.models.storage_pool.StoragePool`
        :returns: a list storage volume if successful
        :rtype: [:class:`pylxd.models.storage_pool.StorageVolume`]
        :raises: :class:`pylxd.exceptions.LXDAPIExtensionNotAvailable` if the
            'storage' api extension is missing.
        """
    storage_pool.client.assert_has_api_extension('storage')
    response = storage_pool.api.volumes.get()
    volumes = []
    for volume in response.json()['metadata']:
        _type, name = volume.split('/')[-2:]
        if _type == 'containers':
            _type = 'container'
        elif _type == 'images':
            _type = 'image'
        else:
            _type = 'custom'
        volumes.append(cls(storage_pool.client, name=name, type=_type,
            storage_pool=storage_pool))
    return volumes


def get_header(mesh):
    """
    For now we only know Element types 1 (line) and 2 (triangle)
    """
    nr_all_nodes = len(mesh['nodes'])
    nr_types = 1
    bandwidth = -1
    for triangle in mesh['elements']['2']:
        diff1 = abs(triangle[0] - triangle[1])
        diff2 = abs(triangle[0] - triangle[2])
        diff3 = abs(triangle[1] - triangle[2])
        diffm = max(diff1, diff2, diff3)
        if diffm > bandwidth:
            bandwidth = diffm
    el_infos = []
    for element in ('2',):
        el = mesh['elements'][element]
        if element == '2':
            el_type = 3
        elif element == '1':
            el_type = 12
        nr = len(el)
        nr_nodes = len(el[0])
        el_infos.append((el_type, nr, nr_nodes))
    for btype in ('12', '11'):
        if btype in mesh['boundaries']:
            el_type = int(btype)
            nr = len(mesh['boundaries'][btype])
            nr_nodes = 2
            if nr > 0:
                nr_types += 1
                el_infos.append((el_type, nr, nr_nodes))
    str_header = ''
    for a, b, c in ([(nr_all_nodes, nr_types, bandwidth)] + el_infos):
        str_header = str_header + '{0}  {1} {2}\n'.format(a, b, c)
    return str_header


def strlist(s):
    """Convert comma seperated file name string to list
  """
    return s.split(',')


def _is_sunder(name):
    """Returns True if a _sunder_ name, False otherwise."""
    return name[0] == name[-1] == '_' and name[1:2] != '_' and name[-2:-1
        ] != '_' and len(name) > 2


@classmethod
def buildModelName(cls, parent_model, relative_name):
    """build an 'absolute' model name from the parent name and the
        'relative' name. parent_model is ignored since there is nothing above
        the Authority object

        Note: This is a basic implementation. You may need to reimplement this
              for a specific scheme if it supports "useParentModel".
        """
    return relative_name


def scatter_row(data, row_index, value):
    """Write the value into the data tensor using the row index.

    This is an out-place write so it can work with autograd.

    Parameters
    ----------
    data : Tensor
        The data tensor to be updated.
    row_index : Tensor
        A 1-D integer tensor containing which rows to be updated.
    value : Tensor
        The new value.

    Returns
    -------
    Tensor
        The new data.
    """
    pass


def sizeof_fmt(num, suffix='B'):
    """
    Calculates file size in human-readable format.
    Copy-pasted from StackOverflow, thanks to this dude:
    https://stackoverflow.com/questions/1094841/
    reusable-library-to-get-human-readable-version-of-file-size
    """
    for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
        if abs(num) < 1024.0:
            return '%3.1f%s%s' % (num, unit, suffix)
        num /= 1024.0
    return '%.1f%s%s' % (num, 'Yi', suffix)


def _need_snippet_displayed(overall_messages_dets, block_messages_dets, *,
    multi_block=False):
    """
    Don't need to see the code snippet displayed when it is already visible:
    * because there is only one block in snippet and there is a block message
      for it (which will display the block i.e. the entire snippet) UNLESS there
      is an overall message separating them
    Otherwise we need it displayed.
    """
    mono_block_snippet = not multi_block
    if (mono_block_snippet and block_messages_dets and not
        overall_messages_dets):
        return False
    return True


def f1_from_roc(fpr, tpr, pos, neg):
    """Calculate f1 score from roc values.

    Parameters
    ----------
        fpr : float
            The false positive rate.
        tpr : float
            The true positive rate.
        pos : int
            The number of positive labels.
        neg : int
            The number of negative labels.

    Returns
    -------
    float
        The f1 score.
    """
    fp = fpr * neg
    fn = (1 - tpr) * pos
    tp = pos - fn
    f1 = tp / (tp + (fn + fp) / 2)
    return f1


def _testFlattenPen():
    """
    >>> from fontPens.printPen import PrintPen
    >>> glyph = _makeTestGlyph()
    >>> pen = FlattenPen(PrintPen(), approximateSegmentLength=10, segmentLines=True)
    >>> glyph.draw(pen)
    pen.moveTo((10, 10))
    pen.lineTo((10.0, 20.0))
    pen.lineTo((10.0, 30.0))
    pen.lineTo((20.0, 30.0))
    pen.lineTo((30.0, 30.0))
    pen.lineTo((30.0, 20.0))
    pen.lineTo((30.0, 10.0))
    pen.lineTo((20.0, 10.0))
    pen.lineTo((10.0, 10.0))
    pen.closePath()
    """


def run_script(script, localvars=None):
    """
    Execute a given project script in the current context.

    Parameters
    ----------
    script : script instance
        The script to execute
    localvars : dict, optional
        If provided it will be used for evaluating the script. In general,
        it can be `localvrs`=``locals()``.

    Returns
    -------
    output of the script if any

    """
    return script.execute(localvars)


def discard(token):
    """Throw away the saved state for a workflow.

        :param token: A unique token identifying this workflow instance.
        :type token: str
        """


def assert_list_data(value: list) ->None:
    """Asserts value is list type."""
    assert isinstance(value, list
        ), f'Input value type must be a list. Actual type: {type(value)}, value: {value}'


def to_css(color):
    """Convert a Toyplot color to a CSS string.

    Parameters
    ----------
    color: :class:`numpy.ndarray`
        Array of RGBA values with dtype = :data:`toyplot.color.dtype`, which is converted to a CSS rgba() color.

    Returns
    -------
    css: :class:`str` containing a CSS color value.
      """
    return 'rgba(%.1f%%,%.1f%%,%.1f%%,%.3f)' % (color['r'] * 100, color['g'
        ] * 100, color['b'] * 100, color['a'])


@staticmethod
def _set_child_joined_alias_using_join_map(child, join_map, alias_map):
    """
        Set the joined alias on the child, for Django <= 1.7.x.
        :param child:
        :param join_map:
        :param alias_map:
        """
    for lhs, table, join_cols in join_map:
        if lhs is None:
            continue
        if lhs == child.alias:
            relevant_alias = child.related_alias
        elif lhs == child.related_alias:
            relevant_alias = child.alias
        else:
            continue
        join_info = alias_map[relevant_alias]
        if join_info.join_type is None:
            continue
        if join_info.lhs_alias in [child.alias, child.related_alias]:
            child.set_joined_alias(relevant_alias)
            break


@staticmethod
def calc_stops(size, start=0.0, end=1.0):
    """Calculates equidistant stops for given number of colors."""
    if size == 0:
        return ()
    stops = []
    step = (end - start) / (size - 1)
    value = start
    while value < end:
        stops.append(value)
        value += step
    stops.append(end)
    return stops


@classmethod
def get(cls, project_id, job_id, model_id=None, data_subset=None):
    """
        Fetches one training predictions job.

        The resulting
        :py:class:`TrainingPredictions <datarobot.models.training_predictions.TrainingPredictions>`
        object will be annotated with `model_id` and `data_subset`.

        Parameters
        ----------
        project_id : str
            The identifier of the project in which the job resides
        job_id : str
            The job id
        model_id : str
            The identifier of the model used for computing training predictions
        data_subset : dr.enums.DATA_SUBSET, optional
            Data subset used for computing training predictions

        Returns
        -------
        job : TrainingPredictionsJob
            The job
        """
    url = cls._job_path(project_id, job_id)
    data, completed_url = cls._data_and_completed_url_for_job(url)
    return cls(data, model_id=model_id, data_subset=data_subset,
        completed_resource_url=completed_url)


@classmethod
def _register(cls, name, func):
    """
        Register a function as an implementation of the named command.
        This is used exclusively for internally implemented commands,
        e.g., "reload"; other extension commands can be implemented as
        functions in the "subway.commands" entrypoint group.

        :param name: The name of the command.
        :param func: The implementing function.
        """
    cls._commands[name] = func


def get_generators_in_mix(db, name='market for electricity, high voltage'):
    """Get names of inputs to electricity mixes"""
    inputs = set()
    for act in db:
        if act['name'] == name:
            for exc in act.technosphere():
                producer = exc.input
                if producer['unit'] == 'kilowatt hour':
                    inputs.add(producer['name'])
    return inputs


def archive(filename, files):
    """Creates archive of list of files with a name."""
    import tarfile
    with tarfile.open(filename, 'w:gz') as tar:
        for f in files:
            print('  compressing ' + f)
            tar.add(f)


def PMCR(val):
    """PubMedCentralRelease"""
    return val


def downgrade():
    """Empty downgrade function for initial revision."""


def verify_columns(api_cols, db_cols):
    """
    Checks if API columns match with DB columns and raises exception if mismatch
    :param api_cols: List of columns names from API call data
    :param db_cols: List of columns names to compare with
    """
    for i, val in enumerate(api_cols):
        if val != db_cols[i]:
            raise Exception(
                "Alpha Vantage API columns and database columns mismatch. API: '%s' and DB: '%s'"
                 % (val, db_cols[i]))


def getRelative(points, pPrevious):
    """Calculates absolute coordinates by adding the previous point to the
    relative coordinates."""
    newPoints = []
    for p in points:
        newP = p[0] + pPrevious[0], p[1] + pPrevious[1]
        newPoints.append(newP)
    return newPoints


def parse_slide_coverpage(data):
    """There's no data for coverpage."""
    return ['\\coverframe', data]


def hidedot():
    """Toggle hide hidden dot files

    """
    pass


@classmethod
def from_data(cls, data, root=None, keypath=tuple()):
    """
        Alternate constructor for 'baby' DataProxies used as sub-dict values.

        Allows creating standalone DataProxy objects while also letting
        subclasses like `.Config` define their own ``__init__`` without
        muddling the two.

        :param dict data:
            This particular DataProxy's personal data. Required, it's the Data
            being Proxied.

        :param root:
            Optional handle on a root DataProxy/Config which needs notification
            on data updates.

        :param tuple keypath:
            Optional tuple describing the path of keys leading to this
            DataProxy's location inside the ``root`` structure. Required if
            ``root`` was given (and vice versa.)

        .. versionadded:: 1.0
        """
    obj = cls()
    obj._set(_config=data)
    obj._set(_root=root)
    obj._set(_keypath=keypath)
    return obj


def parseStreamServer(reactor, *args, **kwargs):
    """
        Parse a stream server endpoint from a reactor and string-only arguments
        and keyword arguments.

        @see: L{IStreamClientEndpointStringParserWithReactor.parseStreamClient}

        @return: a stream server endpoint
        @rtype: L{IStreamServerEndpoint}
        """


def loaded_notebook_message():
    """display function for loading a notebook"""
    print('Loaded notebook:')


def plotting():
    """ Import stuff for plotting"""
    from matplotlib import use
    use('Agg')
    from matplotlib import rc
    font = {'family': 'serif', 'serif': ['cmr10'], 'weight': 'medium',
        'size': 16}
    rc('font', **font)
    rc('text', usetex=True)
    rc('figure', max_open_warning=1000)
    global plt
    import matplotlib.pyplot as plt


@classmethod
def has_entry_in_register(cls, blok, entry, key):
    """ Return True if One Class exist in this blok for this entry

        :param blok: name of the blok
        :param entry: is the existing entry name
        :param key: is the existing key in the entry
        """
    if entry not in cls.loaded_bloks[blok]:
        return False
    if key not in cls.loaded_bloks[blok][entry]:
        return False
    return len(cls.loaded_bloks[blok][entry][key]['bases']) > 0


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


def modify_event_subscription(SubscriptionName=None, SnsTopicArn=None,
    SourceType=None, EventCategories=None, Enabled=None):
    """
    Modifies an existing AWS DMS event notification subscription.
    See also: AWS API Documentation

    :example: response = client.modify_event_subscription(
        SubscriptionName='string',
        SnsTopicArn='string',
        SourceType='string',
        EventCategories=[
            'string',
        ],
        Enabled=True|False
    )

    :type SubscriptionName: string
    :param SubscriptionName: [REQUIRED]
            The name of the AWS DMS event notification subscription to be modified.

    :type SnsTopicArn: string
    :param SnsTopicArn: The Amazon Resource Name (ARN) of the Amazon SNS topic created for event notification. The ARN is created by Amazon SNS when you create a topic and subscribe to it.

    :type SourceType: string
    :param SourceType: The type of AWS DMS resource that generates the events you want to subscribe to.
            Valid values: replication-instance | migration-task

    :type EventCategories: list
    :param EventCategories: A list of event categories for a source type that you want to subscribe to. Use the DescribeEventCategories action to see a list of event categories.
            (string) --

    :type Enabled: boolean
    :param Enabled: A Boolean value; set to true to activate the subscription.

    :rtype: dict
    :return: {
        'EventSubscription': {
            'CustomerAwsId': 'string',
            'CustSubscriptionId': 'string',
            'SnsTopicArn': 'string',
            'Status': 'string',
            'SubscriptionCreationTime': 'string',
            'SourceType': 'string',
            'SourceIdsList': [
                'string',
            ],
            'EventCategoriesList': [
                'string',
            ],
            'Enabled': True|False
        }
    }

    :returns:
    (string) --

    """
    pass


def split_fields(fields_str, fieldwidth=8):
    """Split `fields_str` into fields of length `fieldwidth`
    """
    fields = []
    for start in range(0, len(fields_str), fieldwidth):
        stop = start + fieldwidth
        fields.append(fields_str[start:stop])
    return fields


def find_src_root(metadata):
    """
    Determine source root from src_root or package_dir key.
    Replace package_dir in metadata with src_root.
    Only understands a single root.
    """
    src_root = ''
    if 'package_dir' in metadata:
        try:
            src_root = metadata['package_dir']['']
            del metadata['package_dir']
        except KeyError:
            raise ValueError(
                'Need package_dir mapping "" to a single directory')
    elif 'src_root' in metadata:
        src_root = metadata['src_root']
    metadata['src_root'] = src_root or ''
    return src_root


def get_chall_pref(domain):
    """Return `collections.Iterable` of challenge preferences.

        :param str domain: Domain for which challenge preferences are sought.

        :returns: `collections.Iterable` of challenge types (subclasses of
            :class:`acme.challenges.Challenge`) with the most
            preferred challenges first. If a type is not specified, it means the
            Authenticator cannot perform the challenge.
        :rtype: `collections.Iterable`

        """


def exposure_machine_learner(xdata, ydata, ml_model, print_results=True):
    """Function to fit machine learning predictions. Used by TMLE to generate predicted probabilities of being
    treated (i.e. Pr(A=1 | L))
    """
    try:
        fm = ml_model.fit(X=xdata, y=ydata)
    except TypeError:
        raise TypeError(
            "Currently custom_model must have the 'fit' function with arguments 'X', 'y'. This covers both sklearn and supylearner. If there is a predictive model you would like to use, please open an issue at https://github.com/pzivich/zepid and I can work on adding support"
            )
    if print_results and hasattr(fm, 'summarize'):
        fm.summarize()
    if hasattr(fm, 'predict_proba'):
        g = fm.predict_proba(xdata)
        if g.ndim == 1:
            return g
        else:
            return g[:, (1)]
    elif hasattr(fm, 'predict'):
        g = fm.predict(xdata)
        return g
    else:
        raise ValueError(
            "Currently custom_model must have 'predict' or 'predict_proba' attribute"
            )


def _warn_if_unused_glob(log_printer, globs, used_globs, message):
    """
    Warn if a glob has not been used.

    :param log_printer: The log_printer to handle logging.
    :param globs:       List of globs that were expected to be used.
    :param used_globs:  List of globs that were actually used.
    :param message:     Warning message to display if a glob is unused.
                        The glob which was unused will be added using
                        .format()
    """
    unused_globs = set(globs) - set(used_globs)
    for glob in unused_globs:
        log_printer.warn(message.format(glob))


@classmethod
def create(cls, **kwargs):
    """Create model item, validate provided properties and save it to the collection (if defined)."""
    item = cls(**kwargs)
    if item.__collection__ or cls.Options.collection:
        item.save(mode='insert')
    return item


def douter(a, b):
    """Outer product, last two axes"""
    return a * b.transpose((0, 2, 1))


def parse_entry_tags(entry):
    """Return a list of tag objects of the entry"""
    tags = set()
    for tag in entry.get('tags', []):
        term = tag.get('label') or tag.get('term') or ''
        for item in term.split(','):
            item = item.strip().lower()
            if item:
                tags.add(item)
    return list(tags)


def _CollectHistory_(lookupType, fromVal, toVal, using={}, pattern=''):
    """
    Return a dictionary detailing what, if any, change was made to a record field

    :param string lookupType: what cleaning rule made the change; one of: genericLookup, genericRegex, fieldSpecificLookup, fieldSpecificRegex, normLookup, normRegex, normIncludes, deriveValue, copyValue, deriveRegex
    :param string fromVal: previous field value
    :param string toVal: new string value
    :param dict using: field values used to derive new values; only applicable for deriveValue, copyValue, deriveRegex
    :param string pattern: which regex pattern was matched to make the change; only applicable for genericRegex, fieldSpecificRegex, deriveRegex
    """
    histObj = {}
    if fromVal != toVal:
        histObj[lookupType] = {'from': fromVal, 'to': toVal}
        if lookupType in ['deriveValue', 'deriveRegex', 'copyValue',
            'normIncludes', 'deriveIncludes'] and using != '':
            histObj[lookupType]['using'] = using
        if lookupType in ['genericRegex', 'fieldSpecificRegex', 'normRegex',
            'deriveRegex'] and pattern != '':
            histObj[lookupType]['pattern'] = pattern
    return histObj


def encode_history_file_name(tli):
    """
    Build the history file name based on timeline

    :return str: history file name
    """
    return '%08X.history' % (tli,)


def format_time(seconds):
    """Formats time into mm:ss:xx"""
    minutes, seconds = divmod(seconds, 60)
    centi = '{:.2f}'.format(seconds % 1)[1:]
    return '{:02d}:{:02d}{}'.format(int(minutes), int(seconds), centi)


def line(char='*', number=30):
    """
    Print line of chars.

    :param char: input character
    :type char:str
    :param number: number of characters
    :return: None
    """
    print(char * number)


@staticmethod
def depends_statement(cookbook_name, metadata=None):
    """Return a valid Ruby 'depends' statement for the metadata.rb file."""
    line = "depends '%s'" % cookbook_name
    if metadata:
        if not isinstance(metadata, dict):
            raise TypeError(
                'Stencil dependency options for %s should be a dict of options, not %s.'
                 % (cookbook_name, metadata))
        if metadata:
            line = "%s '%s'" % (line, "', '".join(metadata))
    return line


def _dumx_remove(dumx, dumx_flat, p0):
    """
    remove p0 from dumx
    """
    res = []
    for dx in dumx:
        if p0 not in dx:
            res.append(dx)
            continue
        k = dx.index(p0)
        if k % 2 == 0:
            p0_paired = dx[k + 1]
        else:
            p0_paired = dx[k - 1]
        dx.remove(p0)
        dx.remove(p0_paired)
        dumx_flat.remove(p0)
        dumx_flat.remove(p0_paired)
        res.append(dx)


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 reverse():
    """Reverse *IN PLACE*"""


def file_extensions_all_equal(ext_list):
    """Checks that all file extensions are equal.

    Args:
        ext_list (list): file extensions, eg ['.csv','.csv']

    Returns:
        bool: all extensions are equal to first extension in list?
    """
    return len(set(ext_list)) == 1


def clipMinMax(size, minSize, maxSize):
    """
    Clip the size so it is bigger then minSize but smaller than maxSize.
    """
    return size.expandedTo(minSize).boundedTo(maxSize)


def _Hypot(lhs=None, rhs=None, out=None, name=None, **kwargs):
    """Given the "legs" of a right triangle, return its hypotenuse.

    Defined in src/operator/tensor/elemwise_binary_op_extended.cc:L79

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

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

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


def describe_cached_iscsi_volumes(VolumeARNs=None):
    """
    Returns a description of the gateway volumes specified in the request. This operation is only supported in the cached volume gateway architecture.
    The list of gateway volumes in the request must be from one gateway. In the response Amazon Storage Gateway returns volume information sorted by volume Amazon Resource Name (ARN).
    See also: AWS API Documentation

    Examples
    Returns a description of the gateway cached iSCSI volumes specified in the request.
    Expected Output:

    :example: response = client.describe_cached_iscsi_volumes(
        VolumeARNs=[
            'string',
        ]
    )

    :type VolumeARNs: list
    :param VolumeARNs: [REQUIRED]
            (string) --

    :rtype: dict
    :return: {
        'CachediSCSIVolumes': [
            {
                'VolumeARN': 'string',
                'VolumeId': 'string',
                'VolumeType': 'string',
                'VolumeStatus': 'string',
                'VolumeSizeInBytes': 123,
                'VolumeProgress': 123.0,
                'SourceSnapshotId': 'string',
                'VolumeiSCSIAttributes': {
                    'TargetARN': 'string',
                    'NetworkInterfaceId': 'string',
                    'NetworkInterfacePort': 123,
                    'LunNumber': 123,
                    'ChapEnabled': True|False
                },
                'CreatedDate': datetime(2015, 1, 1)
            },
        ]
    }

    """
    pass


def get_primary_variable(variable):
    """If :obj:`variable` is distributed, returns the primary component."""
    for attribute_name in ('primary', '_primary'):
        attribute = getattr(variable, attribute_name, None)
        if attribute is not None:
            return attribute
    return variable


def _set_default_configuration():
    """Return a default configuration based on fields defined on the
        schema.

        This should be a dictionary.  But, we have at least one
        exception: the css_class field simply has a string.  This
        might change in the future if this bites us.

        All fields with dictionaries must have, at least, the
        following attributes:

        visibility: (u'on', u'off')
            is the field visible? defaults to u'on'
            XXX: this could be a boolean in the future
        order: (u'1', u'2', ...)
            in which order we are going to display the field?
            XXX: until we fix the UI, we must start with 1 instead of 0
                 because is most intuitive for end users

        the following attribute is available only in TextLine fields:

        htmltag: (u'h1', u'h2', ...)
            defaults to u'h2'
            XXX: this name is a bad one, but I have no proposal for now

        the following attributes are available only in NamedImage fields:

        position: (u'inline', u'left', u'right')
            defaults to u'left'
            XXX: this should be named 'alignment' probably
        imgsize: (any of the available sizes for scaled down images)
            defaults to u'mini 200:200' or first available size
            XXX: this should be named 'scale' probably

        final result will be something like this:

        {'css_class': u'tile_default',
         'date': {'order': u'3', 'visibility': u'on', 'format': 'datetime'},
         'description': {'order': u'1', 'visibility': u'on'},
         'image': {'imgsize': u'mini 200:200',
                   'order': u'2',
                   'position': u'left',
                   'visibility': u'on'},
         'subjects': {'order': u'4', 'visibility': u'on'},
         'title': {'htmltag': u'h2', 'order': u'0', 'visibility': u'on'},
         'uuid': {'htmltag': u'h2', 'order': u'5', 'visibility': u'on'}}

        Obviously some of the fields are not meant to be displayed,
        like css_class, but that's another story.
        """


def remove_extra_spaces(text):
    """Remove extra spaces from a given text"""
    text = ' '.join(text.split())
    return text


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

    :return: name of this resource
    :rtype: str
    """
    if friendly:
        return 'Alignak configuration'
    return 'alignak'


def vertex_color_brightness_contrast(brightness: float=0.0, contrast: float=0.0
    ):
    """Adjust vertex color brightness/contrast

    :param brightness: Brightness
    :type brightness: float
    :param contrast: Contrast
    :type contrast: float
    """
    pass


def convertbits(data, frombits, tobits, pad=True):
    """General power-of-2 base conversion."""
    acc = 0
    bits = 0
    ret = []
    maxv = (1 << tobits) - 1
    max_acc = (1 << frombits + tobits - 1) - 1
    for value in data:
        if value < 0 or value >> frombits:
            return None
        acc = (acc << frombits | value) & max_acc
        bits += frombits
        while bits >= tobits:
            bits -= tobits
            ret.append(acc >> bits & maxv)
    if pad:
        if bits:
            ret.append(acc << tobits - bits & maxv)
    elif bits >= frombits or acc << tobits - bits & maxv:
        return None
    return ret


def setDry_run(dry_run_value):
    """Copy dry_run to a variable that is global for this file.

        Parameters
        ----------
        dry_run : string
            string that is either True or False
    """
    global dry_run
    dry_run = dry_run_value


def gaussian(x, mu, sig):
    """return normal distribution at point x.

   :math:`f(x;\\mu,\\sigma) = \\frac{1}{\\sigma\\sqrt{2\\pi}}e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}}`

   :param x: x
   :type x: float
   :param mu: :math:`\\mu`
   :type mu: float
   :param sigma: :math:`\\sigma`
   :type sigma: float
   :returns: :math:`f(x;\\mu,\\sigma)`
   :rtype: float

   :Example:

   >>> from westpy import *
   >>> gaussian(1.0,2.0,3.0)
   """
    import numpy as np
    return 1.0 / (np.sqrt(2.0 * np.pi) * sig) * np.exp(-np.power((x - mu) /
        sig, 2.0) / 2)


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

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

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


def prepare_blacklist(src, dst, duration=3600, src_port1=None, src_port2=
    None, src_proto='predefined_tcp', dst_port1=None, dst_port2=None,
    dst_proto='predefined_tcp'):
    """
    Create a blacklist entry.

    A blacklist can be added directly from the engine node, or from
    the system context. If submitting from the system context, it becomes
    a global blacklist. This will return the properly formatted json
    to submit.

    :param src: source address, with cidr, i.e. 10.10.10.10/32 or 'any'
    :param dst: destination address with cidr, i.e. 1.1.1.1/32 or 'any'
    :param int duration: length of time to blacklist

    Both the system and engine context blacklist allow kw to be passed
    to provide additional functionality such as adding source and destination
    ports or port ranges and specifying the protocol. The following parameters
    define the ``kw`` that can be passed.

    The following example shows creating an engine context blacklist
    using additional kw::

        engine.blacklist('1.1.1.1/32', '2.2.2.2/32', duration=3600,
            src_port1=1000, src_port2=1500, src_proto='predefined_udp',
            dst_port1=3, dst_port2=3000, dst_proto='predefined_udp')

    :param int src_port1: start source port to limit blacklist
    :param int src_port2: end source port to limit blacklist
    :param str src_proto: source protocol. Either 'predefined_tcp'
        or 'predefined_udp'. (default: 'predefined_tcp')
    :param int dst_port1: start dst port to limit blacklist
    :param int dst_port2: end dst port to limit blacklist
    :param str dst_proto: dst protocol. Either 'predefined_tcp'
        or 'predefined_udp'. (default: 'predefined_tcp')

    .. note:: if blocking a range of ports, use both src_port1 and
        src_port2, otherwise providing only src_port1 is adequate. The
        same applies to dst_port1 / dst_port2. In addition, if you provide
        src_portX but not dst_portX (or vice versa), the undefined port
        side definition will default to all ports.
    """
    json = {}
    directions = {src: 'end_point1', dst: 'end_point2'}
    for direction, key in directions.items():
        json[key] = {'address_mode': 'any'} if 'any' in direction.lower(
            ) else {'address_mode': 'address', 'ip_network': direction}
    if src_port1:
        json.setdefault('end_point1').update(port1=src_port1, port2=
            src_port2 or src_port1, port_mode=src_proto)
    if dst_port1:
        json.setdefault('end_point2').update(port1=dst_port1, port2=
            dst_port2 or dst_port1, port_mode=dst_proto)
    json.update(duration=duration)
    return json


def set_grb_start_end(cp, start, end):
    """
    Function to update analysis boundaries as workflow is generated

    Parameters
    ----------
    cp : pycbc.workflow.configuration.WorkflowConfigParser object
    The parsed configuration options of a pycbc.workflow.core.Workflow.

    start : int
    The start of the workflow analysis time.

    end : int
    The end of the workflow analysis time.

    Returns
    --------
    cp : pycbc.workflow.configuration.WorkflowConfigParser object
    The modified WorkflowConfigParser object.

    """
    cp.set('workflow', 'start-time', str(start))
    cp.set('workflow', 'end-time', str(end))
    return cp


def resolve_name(name):
    """Resolve a name like ``module.object`` to an object and return it.

    Raise `ImportError` if the module or name is not found.
    """
    parts = name.split('.')
    cursor = len(parts) - 1
    module_name = parts[:cursor]
    attr_name = parts[-1]
    while cursor > 0:
        try:
            ret = __import__('.'.join(module_name), fromlist=[attr_name])
            break
        except ImportError:
            if cursor == 0:
                raise
            cursor -= 1
            module_name = parts[:cursor]
            attr_name = parts[cursor]
            ret = ''
    for part in parts[cursor:]:
        try:
            ret = getattr(ret, part)
        except AttributeError:
            raise ImportError(name)
    return ret


@staticmethod
def _run(alive, target, args, kwargs):
    """Run the thread function"""
    alive.set()
    ret = target(*args, **kwargs)
    alive.clear()
    return ret


def is_negate(trial_value):
    """
    Example: !EGFR => (True, EGFR)

    :param trial_value:
    :return:
    """
    negate = (True if trial_value.__class__ is str and trial_value and
        trial_value[0] == '!' else False)
    trial_value = trial_value[1:] if negate else trial_value
    return trial_value, negate


def setparms(hamtrain, spamtrain, hamtest=None, spamtest=None, seed=None):
    """Set HAMTEST/TRAIN and SPAMTEST/TRAIN.
       If seed is not None, also set SEED.
       If (ham|spam)test are not set, set to the same as the (ham|spam)train
       numbers (backwards compat option).
    """
    global HAMTEST, SPAMTEST, HAMTRAIN, SPAMTRAIN, SEED
    HAMTRAIN, SPAMTRAIN = hamtrain, spamtrain
    if hamtest is None:
        HAMTEST = HAMTRAIN
    else:
        HAMTEST = hamtest
    if spamtest is None:
        SPAMTEST = SPAMTRAIN
    else:
        SPAMTEST = spamtest
    if seed is not None:
        SEED = seed


@staticmethod
def get_all_resources(datasets):
    """Get all resources from a list of datasets (such as returned by search)

        Args:
            datasets (List[Dataset]): list of datasets

        Returns:
            List[hdx.data.resource.Resource]: list of resources within those datasets
        """
    resources = []
    for dataset in datasets:
        for resource in dataset.get_resources():
            resources.append(resource)
    return resources


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


def get_dtype(matrix_type: int, precision: str='default'):
    """Reset the type if 'default' not selected"""
    if precision == 'single':
        if matrix_type in [1, 2]:
            dtype = 'float32'
        else:
            dtype = 'complex64'
    elif precision == 'double':
        if matrix_type in [1, 2]:
            dtype = 'float64'
        else:
            dtype = 'complex128'
    elif matrix_type == 1:
        dtype = 'float32'
    elif matrix_type == 2:
        dtype = 'float64'
    elif matrix_type == 3:
        dtype = 'complex64'
    else:
        dtype = 'complex128'
    return dtype


def psi_H_HSO4_SO4_HMW84(T, P):
    """c-a-a': hydrogen bisulfate sulfate [HMW84]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


def hide_code_cells():
    """
    Hides the code cells of a notebook (for exporting notebooks without code)

    Returns
    -------
        button : <HTML button>
            Button that toggles code cells on/off
    """
    from IPython.display import HTML
    button = HTML(
        """<script>
                        code_show=true;
                        function code_toggle() {
                            if (code_show){
                                $('div.input').hide();
                            } else {
                                $('div.input').show();
                            }
                        code_show = !code_show
                        }
                        $( document ).ready(code_toggle);
                    </script>
    <form action="javascript:code_toggle()">
        <input style="background-color:#441297;
                      color:#FFFFFF;
                      padding:15px;
                      font-family:Calibri, Arial;
                      border:None;
                      border-radius:2px"
               type="submit" value="Click here to toggle code cells.">
    </form>"""
        )
    return button


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


def PolsSurg(t):
    """Number of policies: Surgery"""
    return 0


def kgm32sft3(density_kgm3):
    """Convert density from kg/m^3 to slugs/ft^3"""
    return density_kgm3 * 0.00194032


def goToStep(stepName):
    """Redirect to the step by name."""


def glMapGrid(un: int, u1, u2, vn: int, v1, v2):
    """Define a one- or two-dimensional mesh

    :param un: Specifies the number of partitions in the grid range interval [u1, u2]. Must be positive.
    :type un: int
    :param u2: Specify the mappings for integer grid domain values i=0 and i=un.
    :param vn: Specifies the number of partitions in the grid range interval [v1, v2] (glMapGrid2 only).
    :type vn: int
    :param v2: Specify the mappings for integer grid domain values j=0 and j=vn (glMapGrid2 only).
    """
    pass


@classmethod
def is_not_known_attribute(cls, attr):
    """
        Returns True if and only if the given attribute is NOT recognized by
        this class.
        """
    return attr not in cls.known_attributes


def set_generator(random, args):
    """
    Generates a list containing non-repeated elements of a discrete or
    continuous representation.

    Parameters
    ----------

    random : Random
    args : dict
        representation: set containing the possible values
        max_candidate_size: int, default: 9
        variable_candidate_size: bool, default: True

    Returns
    -------
    list
        A list containing a sample of the elements. If variable_candidate_size is
        True the list size is up to max_candidate_size, otherwise the candidate
        size equals candidate_size
    """
    representation = args.get('representation')
    indices = list(range(len(representation)))
    max_size = args.get('max_size', 9)
    variable_size = args.get('variable_size', True)
    if variable_size and max_size > 1:
        size = random.randint(1, max_size)
    else:
        size = max_size
    candidate = random.sample(indices, size)
    return sorted(candidate)


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 test_metric_filter(filterPattern=None, logEventMessages=None):
    """
    Tests the filter pattern of a metric filter against a sample of log event messages. You can use this operation to validate the correctness of a metric filter pattern.
    See also: AWS API Documentation

    :example: response = client.test_metric_filter(
        filterPattern='string',
        logEventMessages=[
            'string',
        ]
    )

    :type filterPattern: string
    :param filterPattern: [REQUIRED]
            A symbolic description of how CloudWatch Logs should interpret the data in each log event. For example, a log event may contain timestamps, IP addresses, strings, and so on. You use the filter pattern to specify what to look for in the log event message.

    :type logEventMessages: list
    :param logEventMessages: [REQUIRED]
            The log event messages to test.
            (string) --

    :rtype: dict
    :return: {
        'matches': [
            {
                'eventNumber': 123,
                'eventMessage': 'string',
                'extractedValues': {
                    'string': 'string'
                }
            },
        ]
    }

    :returns:
    (string) --
    (string) --

    """
    pass


def strip_magics(source):
    """
    Given the source of a cell, filter out all cell and line magics.
    """
    filtered = []
    for line in source.splitlines():
        if not line.startswith('%') or line.startswith('%%'):
            filtered.append(line)
    return '\n'.join(filtered)


def jacobi(numerator: int, denominator: int) ->int:
    """Compute the Jacobi Symbol."""
    if denominator <= 0 or denominator & 1 == 0:
        raise ValueError('Jacobi parameters are out of function domain')
    j_symbol: int = 1
    numerator %= denominator
    while numerator:
        while numerator & 1 == 0:
            numerator = numerator // 2
            if denominator % 8 in (3, 5):
                j_symbol *= -1
        numerator, denominator = denominator, numerator
        if numerator % 4 == denominator % 4 == 3:
            j_symbol *= -1
        numerator %= denominator
    if denominator == 1:
        return j_symbol
    return 0


def build_optimiser(name, **kwargs):
    """
    build an optimiser
    :param name: the name of optimiser
    :type name: str
    :param kwargs: the arguments that are passed to optimiser
    :return: the created optimiser wrapper
    :rtype: BaseOptimiserWrapper
    """
    optimiser = None
    if name.lower() == 'adam':
        from convtt.train.optimisers import AdamOptimiserWrapper
        optimiser = AdamOptimiserWrapper(**kwargs)
    elif name.lower() == 'scheduledsgd':
        from convtt.train.optimisers import ScheduledSGDOptimiserWrapper
        optimiser = ScheduledSGDOptimiserWrapper(**kwargs)
    return optimiser


def empty_en(en):
    """
    Check if *en* not empty.

    :raises RuntimeError: Run method get_ham first.
    """
    if en.size == 0:
        raise RuntimeError('\n\nRun method get_eig first\n')


def results(res):
    """Return results in a list format suitable by REST server"""
    if not isinstance(res, list):
        return [res]
    return res


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


@classmethod
def from_scf_task(cls, scf_task, ddk_ngkpt, ddk_shiftk, ddk_nband, manager=None
    ):
    """
        Build NscfDdksWork from a scf_task.
        Args:
            scf_task: GS task. Must produce the DEN file required for the NSCF run.
            ddk_ngkpt: k-mesh used for the NSCF run and the non self-consistent DDK tasks.
            ddk_shiftk: k-mesh shifts
            ddk_nband: Number of bands (occupied + empty) used in the NSCF task and the DDKs tasks.
            manager: TaskManager instance. Use default if None.
        Return: NscfDdksWork instance
        """
    new = cls(manager=manager)
    nscf_inp0 = scf_task.input.deepcopy()
    nscf_inp0.set_vars(nband=ddk_nband, prtwf=1)
    nscf_inp0.set_kmesh(ddk_ngkpt, ddk_shiftk, kptopt=1)
    nscf_task0 = new.register_nscf_task(nscf_inp0, deps={scf_task: 'DEN'})
    new.task_with_ks_energies = nscf_task0
    ddk_inputs = nscf_inp0.make_ddk_inputs(kptopt=2)
    new.ddk_tasks = []
    for ddk_inp in ddk_inputs:
        ddk_inp.set_vars(nstep=1, nline=0, prtwf=1)
        t = new.register_task(ddk_inp, deps={nscf_task0: 'WFK'})
        new.ddk_tasks.append(t)
    return new


@property
def str_bits(f):
    """Return readable representation of the bits as a string."""
    return '{}_{}_{}'.format(f.sign_bit[0], ''.join(map(str, f.
        exponent_bits)), ''.join(map(str, f.fraction_bits)))


def delete_organizational_unit(OrganizationalUnitId=None):
    """
    Deletes an organizational unit from a root or another OU. You must first remove all accounts and child OUs from the OU that you want to delete.
    This operation can be called only from the organization's master account.
    See also: AWS API Documentation

    :example: response = client.delete_organizational_unit(
        OrganizationalUnitId='string'
    )

    :type OrganizationalUnitId: string
    :param OrganizationalUnitId: [REQUIRED]
            The unique identifier (ID) of the organizational unit that you want to delete. You can get the ID from the ListOrganizationalUnitsForParent operation.
            The regex pattern for an organizational unit ID string requires 'ou-' followed by from 4 to 32 lower-case letters or digits (the ID of the root that contains the OU) followed by a second '-' dash and from 8 to 32 additional lower-case letters or digits.

    """
    pass


def decode_list(input_props, name):
    """
    Decodes a space-separated list
    """
    val_str = input_props.get(name, None)
    if val_str:
        return val_str.split(' ')
    return []


@classmethod
def input(cls, port, contact):
    """
        Get the NodeID string for a support input.

        :param port: the desired port (1..6)
        :param contact: the desired contact at the port (1..2)
        :return: the node id string
        """
    return getattr(cls, 'in_{}_{}'.format(port, contact))


def _get_context(block):
    """
    Return section, subsection, and unit names for `block`.
    """
    block_names_by_type = {}
    block_iter = block
    while block_iter:
        block_iter_type = block_iter.scope_ids.block_type
        block_names_by_type[block_iter_type
            ] = block_iter.display_name_with_default
        block_iter = block_iter.get_parent() if block_iter.parent else None
    section_name = block_names_by_type.get('chapter', '')
    subsection_name = block_names_by_type.get('sequential', '')
    unit_name = block_names_by_type.get('vertical', '')
    return section_name, subsection_name, unit_name


def cursor_set(x: int=0, y: int=0):
    """Set cursor position

    :param x: X
    :type x: int
    :param y: Y
    :type y: int
    """
    pass


def pgettext(msgid: str, msgctxt: str):
    """Try to translate the given msgid (with optional msgctxt).

    :param msgid: The string to translate.
    :type msgid: str
    :param msgctxt: The translation context (defaults to BLT_I18NCONTEXT_DEFAULT).
    :type msgctxt: str
    :return:  The translated string (or msgid if no translation was found).
    """
    pass


def add_keys(apps, schema_editor):
    """
    We can't import the Post model directly as it may be a newer
    version than this migration expects. We use the historical version.
    """
    QA = apps.get_model('questionanswers', 'QA')
    for obj in QA.objects.all():
        obj.model_obj2 = obj.model_obj
        obj.save()


@classmethod
def from_repr(cls, string):
    """Generate the prior from it's __repr__"""
    return cls._from_repr(string)


def listActions(info=None, object=None):
    """ List known actions.

        o Return a sequence of action objects.

        o Both the 'object' and the 'info' arguments are deprecated and
          ignored (use 'listActionInfos' to filter actions by context).
        """


def remove_typedefs(signature: str) ->str:
    """
    Strips typedef info from a function signature

    :param signature:
        function signature

    :return:
        string that can be used to construct function calls with the same
        variable names and ordering as in the function signature
    """
    typedefs = ['const realtype *', 'const double *', 'const realtype ',
        'double *', 'realtype *', 'const int ', 'int ',
        'SUNMatrixContent_Sparse ']
    for typedef in typedefs:
        signature = signature.replace(typedef, ' ')
    return signature


def validate(value):
    """Validate that the given value is a valid field value.

        Returns nothing but raises an error if the value is invalid.
        It checks everything specific to a Field and also checks
        with the additional constraint.
        """


def import_item(name):
    """
    Useful for importing nested modules such as pymc.gp.cov_funs.isotropic_cov_funs.

    Updated with code copied from IPython under a BSD license.
    """
    package = '.'.join(name.split('.')[0:-1])
    obj = name.split('.')[-1]
    if package:
        module = __import__(package, fromlist=[obj])
        return module.__dict__[obj]
    else:
        return __import__(obj)


def detrend(x, order=1, axis=-1):
    """Detrend the array x.

    Parameters
    ----------
    x : n-d array
        Signal to detrend.
    order : int
        Fit order. Currently must be '0' or '1'.
    axis : int
        Axis of the array to operate on.

    Returns
    -------
    y : array
        The x array detrended.

    Examples
    --------
    As in :func:`scipy.signal.detrend`::

        >>> randgen = np.random.RandomState(9)
        >>> npoints = int(1e3)
        >>> noise = randgen.randn(npoints)
        >>> x = 3 + 2*np.linspace(0, 1, npoints) + noise
        >>> (detrend(x) - noise).max() < 0.01
        True
    """
    from scipy.signal import detrend
    if axis > len(x.shape):
        raise ValueError('x does not have %d axes' % axis)
    if order == 0:
        fit = 'constant'
    elif order == 1:
        fit = 'linear'
    else:
        raise ValueError('order must be 0 or 1')
    y = detrend(x, axis=axis, type=fit)
    return y


def get_class_name(obj):
    """
    Get class name attribute.

    :param Any obj: Object for get class name.

    :return: Class name
    :rtype: str

    """
    if isinstance(obj, type):
        return obj.__name__
    return obj.__class__.__name__


def intersect_line_plane(line_a: 'mathutils.Vector', line_b:
    'mathutils.Vector', plane_co: 'mathutils.Vector', plane_no:
    'mathutils.Vector', no_flip=False) ->'mathutils.Vector':
    """Calculate the intersection between a line (as 2 vectors) and a plane. Returns a vector for the intersection or None.

    :param line_a: First point of the first line
    :type line_a: 'mathutils.Vector'
    :param line_b: Second point of the first line
    :type line_b: 'mathutils.Vector'
    :param plane_co: A point on the plane
    :type plane_co: 'mathutils.Vector'
    :param plane_no: The direction the plane is facing
    :type plane_no: 'mathutils.Vector'
    :return:  The point of intersection or None when not found
    """
    pass


def iside(seq, fn):
    """
    Wrap an iterable with side effects, yielding the input unchanged.
    `fn` will be called as `fn(index, item)` for each `item` in `seq`

    E.g.:

        # print to STDERR to avoid buffering
        import sys
        def tell(index, item):
            if index % 100 == 0:
                print >> sys.stderr, index,

        total = 0
        for x in iside(xrange(10000), tell):
            total += sum(range(1, x))
        print 'total', total

    """
    for index, item in enumerate(seq):
        fn(index, item)
        yield item


def peer(method):
    """Decorator @peer for methods that should only be called by a 'peer'.

    For now this is only a way of visually showing this information.

    """
    return method


def p_line_asm(p):
    """ line : asms NEWLINE
             | asms CO NEWLINE
    """


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


def iterize(obj):
    """
    Converts into or wraps in an iterator.

    If `obj` is an iterable object other than a `str`, returns an iterator.
    Otherwise, returns a one-element iterable of `obj`.

      >>> list(iterize((1, 2, 3)))
      [1, 2, 3]

      >>> list(iterize("Hello!"))
      ['Hello!']
      >>> list(iterize(42))
      [42]

    """
    if isinstance(obj, str):
        return iter((obj,))
    else:
        try:
            return iter(obj)
        except TypeError:
            return iter((obj,))


def get_host_port(spec, default_port):
    """parse 'hostname:22' into a host and port, with the port optional"""
    args = (spec.split(':', 1) + [default_port])[:2]
    args[1] = int(args[1])
    return args[0], args[1]


def gcd(x, y):
    """
    greatest common divisor
    """
    while y != 0:
        x, y = y, x % y
    return x


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


def _get_object_description(target):
    """Return a string describing the *target*"""
    if isinstance(target, list):
        data = '<list, length {}>'.format(len(target))
    elif isinstance(target, dict):
        data = '<dict, length {}>'.format(len(target))
    else:
        data = target
    return data


@classmethod
def is_set(cls, val, flags):
    """
        Check if all bit in flags are set in val
        :param val: int
        :type val: int
        :param flags: int
        :type flags: int
        :return bool
        :rtype bool
        """
    if val & flags == flags:
        return True
    else:
        return False


def project_opened(project):
    """This function is called after opening the project"""


def get_file_binary_content(uri):
    """
    Retrieves the binary content of a file URI.
    :param uri: path to the file to be loaded
    :return: raw bytes list of the file content.
    """
    with open(uri, 'rb') as f:
        c = f.read()
    return c


def load_current(cursor, oid):
    """
        Returns the current state and integer tid for an object.

        *oid* is an integer. Returns (None, None) if object does not
        exist.
        """


def istext(s, text_characters=''.join(map(chr, range(32, 127))) +
    '\n\r\t\x08', threshold=0.3):
    """
    Helper to attempt support of serializing binary text by base64 encoding `s` if this returns False

    Credit: https://www.oreilly.com/library/view/python-cookbook-2nd/0596007973/ch01s12.html
    """
    if isinstance(s, bytes):
        try:
            s = s.decode('utf-8')
        except UnicodeDecodeError:
            return False
    if '\x00' in s:
        return False
    if not s:
        return True
    t = s.translate(str.maketrans('', '', text_characters))
    return len(t) / len(s) <= threshold


@staticmethod
def multiply(bigger, smaller):
    """
        Multiplies two potentials. Order matters.

        :param bigger: Bigger potential.
        :param smaller: Smaller potential.
        """
    for entry in smaller.entries:
        for e in bigger.get_matching_entries(entry):
            d = e.value * entry.value
            e.value = d


def t_dbref(val):
    """
    Convert/validate: Database reference.
    """
    if isinstance(val, str):
        return val
    return val.id


@classmethod
def handle_cursor(cls, cursor, query, session):
    """Handle a live cursor between the execute and fetchall calls

        The flow works without this method doing anything, but it allows
        for handling the cursor and updating progress information in the
        query object"""
    pass


def get_scanned_motor(header):
    """Return string describing which motor was scanned."""
    try:
        return ' '.join(header.start['motors'])
    except (AttributeError, KeyError):
        return ''


def common_prefix_length(seq_a, seq_b):
    """
    Return the length of the common prefix between two sequences.

    Parameters
    ----------
    seq_a : iter
        An iterable holding the first sequence.
    seq_b : iter
        An iterable holding the second sequence.

    Returns
    -------
    length: int
        The length of the common prefix between `seq_a` and `seq_b`.

    Examples
    --------

    >>> import dafsa
    >>> dafsa.utils.common_prefix_length("abcde", "abcDE")
    3
    >>> dafsa.utils.common_prefix_length("abcde", "ABCDE")
    0
    """
    common_prefix_len = 0
    for i in range(min(len(seq_a), len(seq_b))):
        if seq_a[i] != seq_b[i]:
            break
        common_prefix_len += 1
    return common_prefix_len


def get_current_view_name():
    """Returns the name of the simplelayout view for displaying the
        context.
        """


def launch_celery_workers(spec, steps=None, worker_args='',
    just_return_command=False):
    """
    Launch celery workers for the specified MerlinStudy.

    spec                MerlinSpec object
    steps               The steps in the spec to tie the workers to
    worker_args         Optional celery arguments for the workers
    just_return_command Don't execute, just return the command
    """
    queues = spec.make_queue_string(steps)
    worker_command = ' '.join(['celery worker -A merlin', worker_args, '-Q',
        queues])
    if just_return_command:
        return worker_command
    else:
        pass


def edit_matching_entry(program, arguments):
    """Edit the matching entry."""
    entry = program.select_entry(*arguments)
    entry.context.execute('pass', 'edit', entry.name)


def write_word_to_file(words):
    """
    Write words to file
    """
    with open('spell_check_wrong.txt', 'w+') as s_p:
        for word in words:
            s_p.writelines(word)
            s_p.writelines('\n')


def pep440_version(version):
    """Normalize version to PEP 440 standards"""
    from .vendor.pip_shims.shims import parse_version
    return str(parse_version(version))


@classmethod
def color_term_formatter(cls, string, *formatters):
    """Formats the string for a colored terminal."""
    for formatter in formatters:
        if formatter == 'red':
            string = ''.join(('\x1b[91m', string, '\x1b[0m'))
        elif formatter == 'green':
            string = ''.join(('\x1b[92m', string, '\x1b[0m'))
        elif formatter == 'yellow':
            string = ''.join(('\x1b[33m', string, '\x1b[0m'))
        elif formatter == 'blue':
            string = ''.join(('\x1b[94m', string, '\x1b[0m'))
        elif formatter == 'magenta':
            string = ''.join(('\x1b[95m', string, '\x1b[0m'))
        elif formatter == 'cyan':
            string = ''.join(('\x1b[96m', string, '\x1b[0m'))
        if formatter == 'bold':
            string = ''.join(('\x1b[1m', string, '\x1b[22m'))
        if formatter == 'underline':
            string = ''.join(('\x1b[4m', string, '\x1b[24m'))
    return string


def _raise_exception(exception_object):
    """ Raise exception with short traceback """
    exec('raise exception_object from None')


def simulate_until_max_t(max_t,
    _simulate_until_attractor_or_target_substate_or_max_t, initial_state,
    perturbed_nodes_by_t, predecessor_node_lists, truth_tables):
    """
    Simulate for given number of time steps.

    :param max_t: time after which to stop the simulation
    :param _simulate_until_attractor_or_target_substate_or_max_t: [function] to simulate
        until attractor or max_t
    :param initial_state: initial network state
    :param perturbed_nodes_by_t: perturbations of network nodes
    :param predecessor_node_lists: predecessor nodes for each node
    :param truth_tables: truth tables of node update rules
    :return: simulation states
    """
    states, state_codes_since_last_perturbation, *_ = (
        _simulate_until_attractor_or_target_substate_or_max_t(initial_state,
        perturbed_nodes_by_t, predecessor_node_lists, truth_tables))
    last_simulation_t = len(states) - 1
    if last_simulation_t < max_t:
        attractor_instance_first_state_code = (
            state_codes_since_last_perturbation[-1])
        attractor_l = len(state_codes_since_last_perturbation
            ) - state_codes_since_last_perturbation.index(
            attractor_instance_first_state_code) - 1
        attractor_instance_states = states[-attractor_l:]
        remaining_l_to_simulate = max_t - last_simulation_t
        n_attractor_instance_repetitions, n_leftover_states = divmod(
            remaining_l_to_simulate, len(attractor_instance_states))
        states.extend(attractor_instance_states *
            n_attractor_instance_repetitions + attractor_instance_states[:
            n_leftover_states])
    return states


def makeTarget(target, host=None, user=None, forceSSH=False):
    """makeTarget(path[,host,user,forceSSH]) --> [[user@]host:]path"""
    if forceSSH and not host:
        host = 'localhost'
    if host:
        target = '%s:%s' % (host, target)
        if user:
            target = '%s@%s' % (user, target)
    return target


def update(*args, **kwds):
    """od.update(E, **F) -> None.  Update od from dict/iterable E and F.

        If E is a dict instance, does:           for k in E: od[k] = E[k]
        If E has a .keys() method, does:         for k in E.keys(): od[k] = E[k]
        Or if E is an iterable of items, does:   for k, v in E: od[k] = v
        In either case, this is followed by:     for k, v in F.items(): od[k] = v

        """
    if len(args) > 2:
        raise TypeError(
            'update() takes at most 2 positional arguments (%d given)' % (
            len(args),))
    elif not args:
        raise TypeError('update() takes at least 1 argument (0 given)')
    self = args[0]
    other = ()
    if len(args) == 2:
        other = args[1]
    if isinstance(other, dict):
        for key in other:
            self[key] = other[key]
    elif hasattr(other, 'keys'):
        for key in other.keys():
            self[key] = other[key]
    else:
        for key, value in other:
            self[key] = value
    for key, value in kwds.items():
        self[key] = value


def irregular_grouper(seq, sizes):
    """
    Yield irregularly sized chunks of a sequence.
    @param seq: a sequence
    @param sizes: an iterable of sizes
    """
    i = 0
    for k in sizes:
        yield seq[i:i + k]
        i += k


def string_to_int(s):
    """Convert a string of bytes into an integer, as per X9.62."""
    result = 0
    for c in s:
        if not isinstance(c, int):
            c = ord(c)
        result = 256 * result + c
    return result


def un_nested(l):
    """Returns list `l` in un-nested form (i.e., if it is a one-element list
       whose first element is a list, returns l [0]).

       This is handy if you want to support the passing of a list to a `*
       args' argument without using `apply`.

       >>> un_nested (list (range (3)))
       [0, 1, 2]
       >>> un_nested ([list (range (3))])
       [0, 1, 2]
       >>> un_nested ([list (range (3)), list (range (2))])
       [[0, 1, 2], [0, 1]]
    """
    if l and len(l) == 1 and isinstance(l[0], (list, tuple)):
        l = l[0]
    return l


def ResolvePath(head, tail=''):
    """
    Smart join of path locations when read items from catalog.
    """
    if not head or head.strip().lstrip('/') == '':
        return tail.strip().lstrip('/')
    if not tail or tail.strip().lstrip('/') == '':
        return head.strip().lstrip('/') if head else ''
    return head.strip().lstrip('/') + '/' + tail.strip().lstrip('/')


def print_hello():
    """Method is signature Hello world method for this project."""
    return 'Binu Says Hello!!!!'


def quantized_concat(*data, **kwargs):
    """Joins input arrays along a given axis.

    The dimensions of the input arrays should be the same except the axis along
    which they will be concatenated.
    The dimension of the output array along the concatenated axis will be equal
    to the sum of the corresponding dimensions of the input arrays.
    All inputs with different min/max will be rescaled by using largest [min, max] pairs.
    If any input holds int8, then the output will be int8. Otherwise output will be uint8.

    Defined in src/operator/quantization/quantized_concat.cc:L108
    This function support variable length of positional input.

    Parameters
    ----------
    data : Symbol[]
        List of arrays to concatenate
    dim : int, optional, default='1'
        the dimension to be concated.

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

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


def sizeof_fmt(num, suffix='B'):
    """ Formats a size in bytes to human readable """
    for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
        if abs(num) < 1024.0:
            return '%3.1f %s%s' % (num, unit, suffix)
        num /= 1024.0
    return '%.1f %s%s' % (num, 'Y', suffix)


def lowercase_validator(ns, value):
    """
    Converts input string to lowercase.

    :param pypsi.namespace.Namespace ns: active namespace
    :param value: input value
    :returns: validated value (in lowercase)
    """
    return value.lower() if value else ''


def write_file(filename, contents):
    """Write contents to file and close file savely"""
    with open(filename, 'wb') as f:
        f.write(contents)


def uninstall(context):
    """Uninstall script"""


@classmethod
def parse(cls, handle):
    """ Parse lines in a file-like object and return list of objects. """
    output = []
    for line in handle:
        if 'Components' in line:
            continue
        output.append(cls._read(line))
    return output


def format_time(arg):
    """Format timespans from seconds to smaller units with the right units"""
    if arg <= 1e-06:
        return '{}ns'.format(int(arg * 1000000000))
    elif arg <= 0.001:
        return '{}μs'.format(int(arg * 1000000))
    elif arg <= 1:
        return '{}ms'.format(int(arg * 1000))
    else:
        return '{}s'.format(int(arg))


def get_pos_hint_x(poshints, sizehintx):
    """Return ``poshints['x']`` if available, or its computed equivalent
    otherwise.

    """
    if 'x' in poshints:
        return poshints['x']
    elif sizehintx is not None:
        if 'center_x' in poshints:
            return poshints['center_x'] - sizehintx / 2
        elif 'right' in poshints:
            return poshints['right'] - sizehintx


@classmethod
def _ber_dec_sequence(cls, data):
    """

        Returns (encoded_payload, num_read)

        """
    return cls._ber_dec_seq_family(data)


def get_station_name(soup, station_num):
    """Extracts station name from string."""
    return soup[station_num].text.strip().split('(')[0].strip()


def human_bytes_fraction(num, total, suffix='B'):
    """ Convert a number to bytes to a human-readable form"""
    for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
        if abs(total) < 1024.0:
            return '%3.1f/%3.1f%s%s' % (num, total, unit, suffix)
        num /= 1024.0
        total /= 1024.0
    return '%.1f%.1f%s%s' % (num, total, 'Yi', suffix)


def wait_job(process, cmd_str):
    """Wait process finish its job
    """
    retcode = process.wait()
    mesg = 'Fail with retcode(%s): %s' % (retcode, cmd_str)
    if retcode != 0:
        raise RuntimeError(mesg)


def touch(path):
    """
    Ensure a regular file exists at `path`.

    If the file already exists, its access and modification time are
    updated.

    (This is a very limited and stripped down version of the ``touch``
    POSIX utility.)
    """
    open(path, 'a').close()


@staticmethod
def cpu_node_count(node):
    """Return count of numa nodes.

        :param node: Targeted node.
        :type node: dict
        :returns: Count of numa nodes.
        :rtype: int
        :raises RuntimeError: If node cpuinfo is not available.
        """
    cpu_info = node.get('cpuinfo')
    if cpu_info is not None:
        return node['cpuinfo'][-1][3] + 1
    else:
        raise RuntimeError('Node cpuinfo not available.')


def _parse_azure_type(string):
    """Converts string representing an Azure RecordSet type to usual type.

        :param string: the Azure type. eg: <Microsoft.Network/dnszones/A>
        :type  string: str

        :type return: str
    """
    return string.split('/')[len(string.split('/')) - 1]


@staticmethod
def __get_target_id(target):
    """
        Returns target process unique id.
        :param lldb.SBTarget target: LLDB target.
        :return: Target process unique id.
        :rtype: int
        """
    process = target.GetProcess()
    """:type: lldb.SBProcess"""
    process_id = process.GetUniqueID()
    """:type: int"""
    return process_id


def afterCompletion():
    """Notify a storage that a transaction has ended.

        The storage may choose to use this opportunity to release resources.

        See ``transaction.interfaces.ISynchronizer.afterCompletion``.
        """


def is_float(dtype):
    """Return True if datatype dtype is a float kind"""
    return 'float' in dtype.name or dtype.name in ['single', 'double']


def calculate_aerodynamic_resistances(f2, velocities):
    """
    Calculates the aerodynamic resistances of the vehicle [N].

    :param f2:
        As used in the dyno and defined by respective guidelines [N/(km/h)^2].
    :type f2: float

    :param velocities:
        Velocity vector [km/h].
    :type velocities: numpy.array | float

    :return:
        Aerodynamic resistance vector [N].
    :rtype: numpy.array | float
    """
    return f2 * velocities ** 2


def __call__(*args, **kw):
    """Render a page template

        This is sometimes overridden to provide additional argument
        binding.
        """


@staticmethod
def reg_score_function(X, y, mean, scale, shape, skewness):
    """ GAS Poisson Regression Update term using gradient only - native Python function

        Parameters
        ----------
        X : float
            datapoint for the right hand side variable

        y : float
            datapoint for the time series

        mean : float
            location parameter for the Poisson distribution

        scale : float
            scale parameter for the Poisson distribution

        shape : float
            tail thickness parameter for the Poisson distribution

        skewness : float
            skewness parameter for the Poisson distribution

        Returns
        ----------
        - Score of the Poisson family
        """
    return X * (y - mean)


def kw_amend(orig, ui, repo, old, extra, pats, opts):
    """Wraps cmdutil.amend expanding keywords after amend."""
    kwt = getattr(repo, '_keywordkwt', None)
    if kwt is None:
        return orig(ui, repo, old, extra, pats, opts)
    with repo.wlock():
        kwt.postcommit = True
        newid = orig(ui, repo, old, extra, pats, opts)
        if newid != old.node():
            ctx = repo[newid]
            kwt.restrict = True
            kwt.overwrite(ctx, ctx.files(), False, True)
            kwt.restrict = False
        return newid


def identify_column_indexes(sheet, cfg):
    """ given a sheet and a config, return a data structure with
	canonical column names as keys, and the index into a row for
	that column (indexed from 0) as values.
	"""
    cols = {}
    headerrow = cfg.getint('layout', 'headerrow')
    headers = sheet.iter_rows(min_row=headerrow, max_row=headerrow)
    for r in headers:
        for c in r:
            for col in map(lambda item: item[0], cfg.items('columnNames')):
                if c.value == cfg.get('columnNames', col):
                    cols[col] = c.column - 1
    return cols


@classmethod
def add_arguments(cls, parser, sys_arg_list=None):
    """
        Callback to add command line options for this plugin to the argparse
        parser.

        Return list with names of new arguments.

        """
    return []


def makeDistancesList(list):
    """Returns a list of distances between adjacent numbers in some input list.

    E.g. [1, 1, 2, 3, 5, 7] -> [0, 1, 1, 2, 2]
    """
    d = []
    for i in range(len(list[:-1])):
        d.append(list[i + 1] - list[i])
    return d


@classmethod
def accepts_default_mpiprocs_per_machine(cls):
    """
        Return True if this JobResource accepts a 'default_mpiprocs_per_machine'
        key, False otherwise.
        """
    return True


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

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

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


def dict_to_serialized_dict(ref, the_dict):
    """Serialize the list of elements to a dictionary

    Used for the retention store

    :param ref: Not used
    :type ref:
    :param the_dict: dictionary to convert
    :type the_dict: dict
    :return: dict of serialized
    :rtype: dict
    """
    result = {}
    for elt in list(the_dict.values()):
        if not getattr(elt, 'serialize', None):
            continue
        result[elt.uuid] = elt.serialize()
    return result