@classmethod
def adjust_database_uri(cls, uri, selected_schema):
    """Based on a URI and selected schema, return a new URI

        The URI here represents the URI as entered when saving the database,
        ``selected_schema`` is the schema currently active presumably in
        the SQL Lab dropdown. Based on that, for some database engine,
        we can return a new altered URI that connects straight to the
        active schema, meaning the users won't have to prefix the object
        names by the schema name.

        Some databases engines have 2 level of namespacing: database and
        schema (postgres, oracle, mssql, ...)
        For those it's probably better to not alter the database
        component of the URI with the schema name, it won't work.

        Some database drivers like presto accept '{catalog}/{schema}' in
        the database component of the URL, that can be handled here.
        """
    return uri


def stack(*data, **kwargs):
    """Join a sequence of arrays along a new axis.
    The axis parameter specifies the index of the new axis in the dimensions of the
    result. For example, if axis=0 it will be the first dimension and if axis=-1 it
    will be the last dimension.
    Examples::
      x = [1, 2]
      y = [3, 4]
      stack(x, y) = [[1, 2],
                     [3, 4]]
      stack(x, y, axis=1) = [[1, 3],
                             [2, 4]]

    Parameters
    ----------
    data : NDArray[]
        List of arrays to stack
    axis : int, optional, default='0'
        The axis in the result array along which the input arrays are stacked.

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

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


@classmethod
def _file_for_module(cls, module):
    """Return the relevant file for the module."""
    return cls._archive_for_zip_module(module) or cls._file_for_file_module(
        module)


def load_HDF5_to_xarray(filename):
    """Load HDF file into an xarray DataArray using pandas HDFStore

    requries pytables"""
    from pandas import HDFStore
    from xarray import DataArray
    with HDFStore(filename) as f:
        data = f['data']
        if 'metadata' in f.get_storer('data').attrs:
            metadata = f.get_storer('data').attrs.metadata
        else:
            metadata = None
    return DataArray(data, attrs=metadata)


def bg_cb(sess, resp):
    """ Don't do anything with the response """
    pass


def check_sended(socket, data_bytes):
    """
    Make sure all bytes are sent through the socket
    Parameters
    ----------
    socket
    data_bytes

    Returns
    -------

    """
    sended = 0
    while sended < len(data_bytes):
        sended += socket.send(data_bytes[sended:])


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 dirinfo(inst):
    """Return common info useful for dir output
    """
    return sorted(set(dir(type(inst)) + list(inst.__dict__.keys())))


@classmethod
def fromdata(cls, data, header, *args, valid=True, verify=True, **kwargs):
    """Construct frame from data and header.

        Parameters
        ----------
        data : `~numpy.ndarray`
            Array holding data to be encoded.
        header : `~baseband.vlbi_base.header.VLBIHeaderBase`
            Header for the frame.
        *args, **kwargs :
            Any arguments beyond the filehandle are used to help initialize the
            payload, except for ``valid`` and ``verify``, which are passed on
            to the header and class initializers.
        valid : bool, optional
            Whether this payload contains valid data.
        verify : bool, optional
            Whether to verify the header and frame correctness.
        """
    payload = cls._payload_class.fromdata(data, *args, **kwargs)
    return cls(header, payload, valid=valid, verify=verify)


def mpl_redraw_callback(event, tax):
    """
    Callback to properly rotate and redraw text labels when the plot is drawn
    or resized.

    Parameters
    ----------
    event: a matplotlib event
        either 'resize_event' or 'draw_event'
    tax: TernaryAxesSubplot
         the TernaryAxesSubplot
    """
    tax._redraw_labels()


def runValidator(f, value):
    """
    Attempt to run the user supplied validation function

    Fall back to False in the even of any errors
    """
    try:
        return f(value)
    except:
        return False


def stop_if_result_none(result):
    """stop if result none will return True if we should not retry
    when result is none, False otherwise using retrying python package
    """
    do_retry = result is not None
    return do_retry


def number_players_search(weights, players, quota):
    """
    Count the quantity of players before they pass the quota.

    Parameters
    ----------
    weight  : list
                Vector with the weights of each player.
    players : dictionary
                Name of the the players and their weights.
    quota   : int
                Necesary weight to win the game.

    Returns
    -------
    int
        Quantity of players.

    """
    total_power = 0
    number_players = 0
    for player in weights:
        total_power += players[player]
        number_players += 1
        if total_power >= quota:
            return number_players


def decodefilename(filename):
    """Perforce escapes special characters @, #, *, or %
    with %40, %23, %2A, or %25 respectively

    >>> decodefilename(b'portable-net45%252Bnetcore45%252Bwp8%252BMonoAndroid')
    'portable-net45%2Bnetcore45%2Bwp8%2BMonoAndroid'
    >>> decodefilename(b'//Depot/Directory/%2525/%2523/%23%40.%2A')
    '//Depot/Directory/%25/%23/#@.*'
    """
    replacements = [(b'%2A', b'*'), (b'%23', b'#'), (b'%40', b'@'), (b'%25',
        b'%')]
    for k, v in replacements:
        filename = filename.replace(k, v)
    return filename


def get_input_tool_name(step_id, steps):
    """Get the string with the name of the tool that generated an input."""
    inp_provenance = ''
    inp_prov_id = str(step_id)
    if inp_prov_id in steps:
        name = steps[inp_prov_id]['name']
        if 'Input dataset' in name:
            inp_provenance = '(%s)' % name
        else:
            inp_provenance = '(output of **%s** {%% icon tool %%})' % name
    return inp_provenance


def timecode(seconds, seperator='.'):
    """Takes seconds and returns SRT timecode format: e.g. 00:02:36,894"""
    seconds = float(seconds)
    timecode_fmt = (
        '%(hour)02d:%(minute)02d:%(second)02d%(ms_seperator)s%(ms)03d')
    return timecode_fmt % {'hour': seconds // 3600, 'minute': seconds // 60 %
        60, 'second': seconds % 60, 'ms': seconds % 1 * 1000,
        'ms_seperator': seperator}


@classmethod
def create_from_dict(cls, dict):
    """
        Factory method to create a PluginConfig from a python dictionary
        :param dict:
        :return: a PluginConfig
        :rtype: py:class:`toolbox.config.PluginConfig`
        """
    config = cls()
    for k in dict:
        config[k] = dict[k]
    return config


def _a_decode_unicode(stream, offset, count, _):
    """
    'a3sbar',3,3 --> 6,u'bar'
    """
    if len(stream) >= offset + count:
        return offset + count, stream[offset:offset + count].decode('UTF-8')
    else:
        raise ValueError('Invalid stream length', len(stream), offset + count)


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


@classmethod
def _get_subclasses(cls, clazz):
    """
    Finds all subclasses of the ``clazz``.
    """
    result = set()
    queue = [clazz]
    while queue:
        parent = queue.pop()
        for child in parent.__subclasses__():
            if child not in result:
                result.add(child)
                queue.append(child)
    return result


def set_left(stereo_samp, v):
    """Set StereoSample stereo_samp's left value to v."""
    stereo_samp.set_left(v)


@classmethod
def from_string(cls, version_string):
    """ Creates a new EnpkgVersion from its string representation.

        Parameters
        ----------
        version_string : str
            the version string.
        """
    parts = version_string.rsplit('-')
    if len(parts) == 1:
        build = 0
    elif len(parts) == 2:
        try:
            build = int(parts[1])
        except ValueError:
            raise ValueError('Invalid build number: {0!r}'.format(parts[1]))
    else:
        msg = 'Invalid version format: {0!r}'.format(version_string)
        raise ValueError(msg)
    return cls.from_upstream_and_build(parts[0], build)


def calculate_phase(period, obsDur, winSize):
    """ Function to calculate the min and max phase.

        Parameters
        ----------
        period : float
            The period of the transit in days.
        obsdur : float
            The duration of the observation in hours.
        winSize : float
            The window size of transit in hours. Default is 1 hour.

        Returns
        -------
        minphase : float
            The minimum phase constraint.
        maxphase : float
            The maximum phase constraint. """
    minphase = 1.0 - (obsDur + winSize) / 2.0 / 24 / period
    maxphase = 1.0 - (obsDur - winSize) / 2.0 / 24 / period
    return minphase, maxphase


def _g():
    """used only for getting generator_type"""
    yield from range(1)


def unique(l):
    """Returns a new list with all the unique elements in l"""
    return list(set(l))


def p_implements_list(p):
    """implements_list : IMPLEMENTS interface_list
                       | empty"""
    if len(p) == 3:
        p[0] = p[2]
    else:
        p[0] = []


def get_echotime(df):
    """Return Echo Time if present, or None."""
    r = df.get('EchoTime')
    if r is not None:
        r = int(r) if r.is_integer() else float(r)
    return r


def SizeExpsOther(t):
    """Other expenses per policy at time t"""
    return 0


def is_numeric(s):
    """
    It's a useful function for checking if a data is a numeric.

    This function could identify any kinds of numeric: e.g. '0.1', '10', '-2.',
    2.5, etc

    Parameters
    ----------
    s : int, float or string.
        The input could be any kind of single value except the scalable
        type of python like 'list()', 'dict()', 'set()'

    Returns
    -------
        A boolean. Ture if `s` is numeric else False

    Notes
    -----
        http://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-float-in-python
    """
    try:
        float(s)
        return True
    except ValueError:
        return False


def is_multicast(ip):
    """
    Tells whether the specified ip is a multicast address or not

    :param ip: an IPv4 address in dotted-quad string format, for example
      192.168.2.3
    """
    return int(ip.split('.')[0]) in range(224, 239)


@classmethod
def shorten_id(cls, internal_id):
    """
        Returns the shortcode for a numeric media PK

        :param internal_id: numeric ID value
        :return:
        """
    return cls._encode(internal_id)


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 splini(xa, ya, y2a, n, x, y):
    """
/*      INTEGRATE CUBIC SPLINE FUNCTION FROM XA(1) TO X
 *       XA,YA: ARRAYS OF TABULATED FUNCTION IN ASCENDING ORDER BY X
 *       Y2A: ARRAY OF SECOND DERIVATIVES
 *       N: SIZE OF ARRAYS XA,YA,Y2A
 *       X: ABSCISSA ENDPOINT FOR INTEGRATION
 *       Y: OUTPUT VALUE
 */
 """
    yi = 0
    klo = 0
    khi = 1
    while x > xa[klo] and khi < n:
        xx = x
        if khi < n - 1:
            if x < xa[khi]:
                xx = x
            else:
                xx = xa[khi]
        h = xa[khi] - xa[klo]
        a = (xa[khi] - xx) / h
        b = (xx - xa[klo]) / h
        a2 = a * a
        b2 = b * b
        yi += ((1.0 - a2) * ya[klo] / 2.0 + b2 * ya[khi] / 2.0 + ((-(1.0 +
            a2 * a2) / 4.0 + a2 / 2.0) * y2a[klo] + (b2 * b2 / 4.0 - b2 /
            2.0) * y2a[khi]) * h * h / 6.0) * h
        klo += 1
        khi += 1
    y[0] = yi
    return


def build_position(poslist):
    """
    Construct a L{Position} object that takes the other positions as include list.

    @param poslist: Sequence of positions to report. First entry is the innermost position,
                    last entry is the nml statement that started it all.
    @type  poslist: C{list} of L{Position}

    @return: Position to attach to an error.
    @rtype:  L{Position}
    """
    if poslist is None or len(poslist) == 0:
        return None
    if len(poslist) == 1:
        return poslist[0]
    pos = poslist[-1]
    pos.includes = pos.includes + poslist[:-1]
    return pos


@staticmethod
def get_address_type(address):
    """
        Get the Bitcoin address type.
        Not available for Liquid.

        :param address: The alphanumeric Bitcoin address

        :return: String representing the address type (P2PKH, P2SH, etc.)
        """
    pass


def _insert_common_sphinx_configs(c, *, project_name):
    """Add common core Sphinx configurations to the state.
    """
    c['project'] = project_name
    c['source_suffix'] = '.rst'
    c['source_encoding'] = 'utf-8-sig'
    c['master_doc'] = 'index'
    c['numfig'] = True
    c['numfig_format'] = {'figure': 'Figure %s', 'table': 'Table %s',
        'code-block': 'Listing %s'}
    c['default_role'] = 'obj'
    c['rst_epilog'] = '\n.. _Astropy: http://astropy.org\n    '
    c['suppress_warnings'] = ['app.add_directive']
    return c


@classmethod
def webSettings(cls):
    """
        Class method to get the web settings of the current profile.

        @return web settings of the current profile
        @rtype QWebEngineSettings
        """
    return cls.webProfile().settings()


def df_memory_usage(df):
    """return the number of megabytes a dataframe/Series uses including index"""
    mem_in_bytes = df.memory_usage(index=True)
    while hasattr(mem_in_bytes, 'shape') and mem_in_bytes.shape:
        mem_in_bytes = mem_in_bytes.sum()
    return mem_in_bytes / 1024.0 / 1024.0


def bC_Ca_HS_HPM88(T, P):
    """c-a: calcium bisulfide [HPM88]."""
    b0 = -0.105
    b1 = 3.43
    b2 = 0
    C0 = 0
    C1 = 0
    alph1 = 2
    alph2 = -9
    omega = -9
    valid = T == 298.15
    return b0, b1, b2, C0, C1, alph1, alph2, omega, valid


def remove_admin_access_permissions(apps, schema_editor):
    """Reverse the above additions of permissions."""
    ContentType = apps.get_model('contenttypes.ContentType')
    Permission = apps.get_model('auth.Permission')
    wagtailadmin_content_type = ContentType.objects.get(app_label=
        'wagtailadmin', model='admin')
    Permission.objects.filter(content_type=wagtailadmin_content_type,
        codename='access_admin').delete()


def _fill_genotype_confidence(field_value, genotype_info_to_fill):
    """Parse the genotype quality (confidence) of this sample at this site and store in the VCFGenotypeInfo.

    Args:
        field_value (str): The value associated with the GQ tag in the format value string.
        genotype_info_to_fill (VCFGenotypeInfo): A partially filled VCFGenotypeInfo object.

    Returns:
        VCFGenotypeInfo: The input VCFGenotypeInfo with genotype_confidence filled in.

    From https://gatkforums.broadinstitute.org/gatk/discussion/1268/what-is-a-vcf-and-how-should-i-interpret-it :
    " GQ : Quality of the assigned genotype.
      The Genotype Quality represents the Phred-scaled confidence that the genotype assignment (GT) is correct, derived
      from the genotype PLs. Specifically, the GQ is the difference between the PL of the second most likely genotype,
      and the PL of the most likely genotype. As noted above, the values of the PLs are normalized so that the most
      likely PL is always 0, so the GQ ends up being equal to the second smallest PL, unless that PL is greater than 99.
      In GATK, the value of GQ is capped at 99 because larger values are not more informative, but they take more space
      in the file. So if the second most likely PL is greater than 99, we still assign a GQ of 99.

      Basically the GQ gives you the difference between the likelihoods of the two most likely genotypes. If it is low,
      you can tell there is not much confidence in the genotype, i.e. there was not enough evidence to confidently
      choose one genotype over another. See the FAQ article on the Phred scale to get a sense of what would be
      considered low.

      Not to be confused with the site-level annotation QUAL; see this FAQ article for an explanation of the differences
      in what they mean and how they should be used."
    """
    genotype_info_to_fill.genotype_confidence = field_value
    return genotype_info_to_fill


def may_inject(context):
    """
    Figure out if we may inject Xtheme editing into this view.

    The requirements are that there is a CBV `view` object in the
    context, and that `view` object does not explicitly opt-out of
    editing with `xtheme_injection = False`

    :param context: Jinja rendering context
    :type context: jinja2.runtime.Context
    :return: Permission bool
    :rtype: bool
    """
    view = context.get('view')
    return bool(view) and bool(getattr(view, 'xtheme_injection', True))


def get_function_restype(node):
    """
    Return the return type from a FuncDecl node.
    """
    return node.decl.type.type.type.names[0]


def addParametersToD(paramL, paramD):
    """Given a list of lines (e.g. from a parameters file) add parameters
to paramD. Each line is a string. Some will be comments or blank
lines, and others will be python assignment statements. We use the
assignment statements to create entries in paramD.
    """
    for s in paramL:
        if s == '' or s.lstrip() == '' or s.lstrip()[0] == '#':
            continue
        key, value = s.rstrip().split('=')
        key = key.strip()
        value = value.strip()
        paramD[key] = eval(value)
    return paramD


@staticmethod
def is_feed_url(url: str) ->bool:
    """
        Return True if URL ending contains valid feed file format.

        :param url: URL string
        :return: bool
        """
    return any(map(url.lower().endswith, ['.rss', '.rdf', '.xml', '.atom',
        '.json']))


@classmethod
def is_supported(cls):
    """ Returns True if this backend can be used.

			# Return value

			If the backend appears to be useable, returns True. Otherwise,
			returns False.

			Note that if this returns False, then the backend should definitely
			not be able to be used. However, just because it returns True
			doesn't mean it will work (e.g., Theano with `force_device=true`,
			but without the requested device installed).
		"""
    raise NotImplementedError


def _fft_out_chunks(a, s, axes):
    """ For computing the output chunks of [i]fft*"""
    if s is None:
        return a.chunks
    chunks = list(a.chunks)
    for i, axis in enumerate(axes):
        chunks[axis] = s[i],
    return chunks


def V_sho(x):
    """ Harmonic oscillator potential => E0 = 0.5. """
    return x ** 2 / 2.0


def __contains__(key) ->bool:
    """
        If contains the resource
        """


@classmethod
def from_es(cls, es):
    """Initialize an ElasticSearch instance"""
    return cls(es=es)


@classmethod
def match(cls, jedec):
    """Tells whether this class support this JEDEC identifier"""
    manufacturer, capacity, revision = jedec[:3]
    if manufacturer != cls.JEDEC_ID:
        return False
    if revision > 1:
        return False
    if capacity not in cls.SIZES:
        return False
    return True


@staticmethod
def producer_init(device, cfg):
    """
        function for producer initialization.

        Args:
            device (str): device for the this process.
            cfg (easycore.common.config.CfgNode): config of this process, you can use it to transfer data
                to `producer_work` and `producer_end` function.
        """
    pass


def path_shift(script_name, path_info, shift=1):
    """ Shift path fragments from PATH_INFO to SCRIPT_NAME and vice versa.

        :return: The modified paths.
        :param script_name: The SCRIPT_NAME path.
        :param script_name: The PATH_INFO path.
        :param shift: The number of path fragments to shift. May be negative to
          change the shift direction. (default: 1)
    """
    if shift == 0:
        return script_name, path_info
    pathlist = path_info.strip('/').split('/')
    scriptlist = script_name.strip('/').split('/')
    if pathlist and pathlist[0] == '':
        pathlist = []
    if scriptlist and scriptlist[0] == '':
        scriptlist = []
    if 0 < shift <= len(pathlist):
        moved = pathlist[:shift]
        scriptlist = scriptlist + moved
        pathlist = pathlist[shift:]
    elif 0 > shift >= -len(scriptlist):
        moved = scriptlist[shift:]
        pathlist = moved + pathlist
        scriptlist = scriptlist[:shift]
    else:
        empty = 'SCRIPT_NAME' if shift < 0 else 'PATH_INFO'
        raise AssertionError('Cannot shift. Nothing left from %s' % empty)
    new_script_name = '/' + '/'.join(scriptlist)
    new_path_info = '/' + '/'.join(pathlist)
    if path_info.endswith('/') and pathlist:
        new_path_info += '/'
    return new_script_name, new_path_info


def _is_png(filename):
    """Determine if a file contains a PNG format image.

  Args:
    filename: string, path of the image file.

  Returns:
    boolean indicating if the image is a PNG.
  """
    return 'n02105855_2933.JPEG' in filename


def _max_full_form_prefix_length(full_form, lemma, min_rule_length):
    """Return the maximum possible length of the prefix (part of the full form preceding the suffix)."""
    full_form_prefix = len(full_form) - min_rule_length
    return min(len(lemma), full_form_prefix)


def setState(state):
    """ set the state of the queue, i.e. its contents """


@staticmethod
def time_from_s(s):
    """
        Converts seconds into a mm:ss formatted string.

        Arguments:
        s: Number of seconds.

        Returns: String-formatted time period in mm:ss.
        """
    mins = str(int(s // 60)).zfill(2)
    secs = str(int(s % 60)).zfill(2)
    return '%s:%s' % (mins, secs)


def _convert_years_months_to_days(timedeltas):
    """Converts any 'years' or 'months' values in a given timedeltas dict
    to equivalent values of days and adds them to the 'days' value.
    This is to work around a lack of support for months and years in
    Python's timedelta library.
    Assumes 30 days per month, 365 days per year.
    """
    if 'months' in timedeltas:
        timedeltas['days'] = timedeltas.setdefault('days', 0
            ) + 30 * timedeltas['months']
        del timedeltas['months']
    if 'years' in timedeltas:
        timedeltas['days'] = timedeltas.setdefault('days', 0
            ) + 365 * timedeltas['years']
        del timedeltas['years']


def current_service(request):
    """Return the Cornice service matching the specified request.

    :returns: the service or None if unmatched.
    :rtype: cornice.Service
    """
    if request.matched_route:
        services = request.registry.cornice_services
        pattern = request.matched_route.pattern
        try:
            service = services[pattern]
        except KeyError:
            return None
        else:
            return service


def ppcontexts(ctxs, title):
    """Print a list of context for tracing and debugging"""
    print(title)
    for lc, rc in sorted(ctxs):
        print(lc, '_', rc)


def crossing_strand_from_name(link, csname):
    """
    Find crossing strand object from it's name in the format of cslabel above
    """
    for c in link.crossings:
        if c.label == csname[0]:
            return c.crossing_strands()[csname[1]]
    raise Exception('Crossing not found')


def _fmt(text, args=None, kwargs=None):
    """ Utility function. See fmt() """
    if text.find('%') == -1:
        return text
    if not args is None and len(args) > 0:
        assert kwargs is None or len(kwargs
            ) == 0, "Cannot specify both 'args' and 'kwargs'"
        return text % tuple(args)
    if not kwargs is None and len(kwargs) > 0:
        return text % kwargs
    return text


def _format_number_list(number_list):
    """Return string representation of a list of integers,
    using ``,`` as a separator and ``:`` as a range separator.
    """
    ret = ''
    first_in_range = number_list[0]
    last_in_range = number_list[0]
    previous = number_list[0]
    for number in number_list[1:]:
        if number - previous != 1:
            if last_in_range > first_in_range:
                ret += '%d:%d,' % (first_in_range, last_in_range)
            else:
                ret += '%d,' % previous
            first_in_range = number
            last_in_range = number
        else:
            last_in_range = number
        previous = number
    if last_in_range > first_in_range:
        ret += '%d:%d' % (first_in_range, last_in_range)
    else:
        ret += '%d' % previous
    return ret


def updateList(ml):
    """Updates the address of an already registered list.  Will raise a
           UserError if the address is already registered."""


def getNodeIds():
    """
        Return all NodeIDs.

        @return: deferred that returns a list of NodeIDs (C{unicode}).
        """


def check_json_dataset_names(json_dict, *restricted_names):
    """Checks that certain dataset names aren't present in a Vega-Lite JSON.

       This should never really fail, since all of the special dataset names we
       use internally start with "qurro_" (and Altair generates hashes of the
       data somehow to use as dataset names). However, this function is here
       just in the exceedingly rare case that this conflict does actually come
       up (if so, we can figure out a way around it).
    """
    if 'datasets' not in json_dict:
        raise ValueError("Input JSON doesn't have any datasets defined")
    intersection = set(json_dict['datasets'].keys()) & set(restricted_names)
    if len(intersection) > 0:
        raise ValueError(
            'Found the following disallowed dataset name(s) in a JSON: {}'.
            format(intersection))


def remove_http_https_prefix(endpoint):
    """remove http:// or https:// prefix if presented in endpoint"""
    endpoint = endpoint.replace('https://', '')
    endpoint = endpoint.replace('http://', '')
    return endpoint


def cat_arrays(vector_arrays):
    """Return a new |VectorArray| which is a concatenation of the arrays in `vector_arrays`."""
    vector_arrays = list(vector_arrays)
    total_length = sum(map(len, vector_arrays))
    cated_arrays = vector_arrays[0].empty(reserve=total_length)
    for a in vector_arrays:
        cated_arrays.append(a)
    return cated_arrays


def c_modulus(x, y):
    """ C-style modulus operation (different treatment of negative numbers) """
    return x % y if x >= 0 else x % -y


def get_function_from_string(string):
    """
    Return function from given dotted string. It imports correct module
    and than gets needed functon.
    """
    modulename, function_name = string.rsplit('.', 1)
    m = __import__(modulename, globals(), locals(), [function_name])
    return getattr(m, function_name)


def linspace(start, stop, n):
    """Generate a linear interpolation between start and stop with n steps."""
    if n == 1:
        yield stop
        return
    h = (stop - start) / (n - 1)
    for i in range(n):
        yield start + h * i


def to_fixed(s, prec):
    """Convert a raw mpf to a fixed-point big integer"""
    sign, man, exp, bc = s
    offset = exp + prec
    if sign:
        if offset >= 0:
            return -man << offset
        else:
            return -man >> -offset
    elif offset >= 0:
        return man << offset
    else:
        return man >> -offset


@staticmethod
def pinball_states(width=5, ball='*'):
    """Get pinball-style animation states

        Example::

            lh = Spinner(Spinner.pinball_states())

        :param int width: desired state width (default: 5)
        :param str ball: ball character (default: '*')
        :returns: pinball states
        :rtype: [str]
        """
    if width <= 1:
        return '|\\'
    elif width == 2:
        return ['||', '\\/']
    elif width == 3:
        return ['\\*|', '|*/']
    w3 = width - 3
    rstates = []
    lstates = []
    for i in range(1, w3):
        lsp = ' ' * i
        rsp = ' ' * (w3 - i)
        rstates.append('|' + lsp + ball + rsp + '|')
        lstates.append('|' + rsp + ball + lsp + '|')
    sp = ' ' * w3
    return ['\\' + ball + sp + '|'] + rstates + ['|' + sp + ball + '/'
        ] + lstates


def reset_runtime():
    """Reset runtime environment.
    Best effort to reset module data to initial state.
    """
    from Pyautomators import step_registry
    from Pyautomators import matchers
    step_registry.registry = step_registry.StepRegistry()
    step_registry.setup_step_decorators(None, step_registry.registry)
    matchers.ParseMatcher.custom_types = {}
    matchers.current_matcher = matchers.ParseMatcher


def add_repository(config, repository, namespace, name, shard, **kw):
    """ This function is used as a directive of Pyramid Config
    and responsible for registering available SQL repositories.

    :param config: Pyramid configurator instance
    :type config: :class:`pyramid.config.Configurator`
    :param repository: repository class object
    :param namespace: database namespace
    :type namespace: str
    :param name: database name, must be unique
    :type name: str
    :param shard: database shard
    :type shard: str
    """
    repositories = config.registry.settings.setdefault(
        'pacific.db.repositories', {})
    repositories[name] = {'repository': repository, 'namespace': namespace,
        'shard': shard}


def edit(text=None, editor=None, env=None, require_save=True, extension=
    '.txt', filename=None):
    """Edits the given text in the defined editor.  If an editor is given
    (should be the full path to the executable but the regular operating
    system search path is used for finding the executable) it overrides
    the detected editor.  Optionally, some environment variables can be
    used.  If the editor is closed without changes, `None` is returned.  In
    case a file is edited directly the return value is always `None` and
    `require_save` and `extension` are ignored.

    If the editor cannot be opened a :exc:`UsageError` is raised.

    Note for Windows: to simplify cross-platform usage, the newlines are
    automatically converted from POSIX to Windows and vice versa.  As such,
    the message here will have ``\\n`` as newline markers.

    :param text: the text to edit.
    :param editor: optionally the editor to use.  Defaults to automatic
                   detection.
    :param env: environment variables to forward to the editor.
    :param require_save: if this is true, then not saving in the editor
                         will make the return value become `None`.
    :param extension: the extension to tell the editor about.  This defaults
                      to `.txt` but changing this might change syntax
                      highlighting.
    :param filename: if provided it will edit this file instead of the
                     provided text contents.  It will not use a temporary
                     file as an indirection in that case.
    """
    from ._termui_impl import Editor
    editor = Editor(editor=editor, env=env, require_save=require_save,
        extension=extension)
    if filename is None:
        return editor.edit(text)
    editor.edit_file(filename)


@staticmethod
def get_arcs(analysis):
    """
        Hit stats for each branch.

        Returns a flat list where every four values represent a branch:
        1. line-number
        2. block-number (not used)
        3. branch-number
        4. hits (we only get 1/0 from coverage.py)
        """
    if not analysis.has_arcs():
        return None
    if not hasattr(analysis, 'branch_lines'):
        branch_lines = analysis._branch_lines()
    else:
        branch_lines = analysis.branch_lines()
    branches = []
    for l1, l2 in analysis.arcs_executed():
        if l1 in branch_lines:
            branches.extend((l1, 0, abs(l2), 1))
    for l1, l2 in analysis.arcs_missing():
        if l1 in branch_lines:
            branches.extend((l1, 0, abs(l2), 0))
    return branches


@classmethod
def crash(cls):
    """Crashes renderer on the IO thread, generates minidumps.
        """
    return cls.build_send_payload('crash', {}), None


def remove_unsynchronization(data):
    """Remove ID3v2 unsynchronization scheme from data."""
    sync_index = data.find(b'\xff')
    if sync_index == -1:
        return data
    data = bytearray(data)
    d = bytearray()
    while sync_index != -1:
        d += data[:sync_index]
        if data[sync_index + 1:sync_index + 3] == b'\x00\x00':
            d += b'\xff\x00'
            data = data[sync_index + 3:]
        elif data[sync_index + 1:sync_index + 2] == b'\x00':
            d += b'\xff'
            data = data[sync_index + 2:]
        else:
            d += b'\xff'
            data = data[sync_index + 1:]
        sync_index = data.find(b'\xff')
    d += data
    return bytes(d)


def _get_scores_bow(bm25, document):
    """Helper function for retrieving bm25 scores of given `document` in parallel
    in relation to every item in corpus.

    Parameters
    ----------
    bm25 : BM25 object
        BM25 object fitted on the corpus where documents are retrieved.
    document : list of str
        Document to be scored.

    Returns
    -------
    list of (index, float)
        BM25 scores in a bag of weights format.

    """
    return bm25.get_scores_bow(document)


def _close_after_stream(response, chunk_size):
    """Iterate over the content and ensure the response is closed after."""
    for chunk in response.iter_content(chunk_size=chunk_size):
        yield chunk
    response.close()


@staticmethod
def __session_id_cookie_settings(secret=False):
    """ Defines some settings to be used with the session id cookie. """
    cookie_settings = {}
    cookie_settings.setdefault('expires', None)
    cookie_settings.setdefault('expires_days', None)
    if secret:
        cookie_settings['expires_days'] = 30
    return cookie_settings


@classmethod
def getAffinity(cls):
    """Get AWLSIM_AFFINITY.
		Returns a list of host CPU indices or an empty list,
		if all host CPUs are allowed.
		"""
    affinityStr = cls.__getVar('AFFINITY', '')
    affinity = []
    try:
        for cpuIndex in affinityStr.split(','):
            cpuIndex = int(cpuIndex)
            if cpuIndex < 0:
                cpuIndex = cls.__getCpuCount() + cpuIndex
            if cpuIndex < 0:
                cpuIndex = 0
            affinity.append(cpuIndex)
    except ValueError as e:
        affinity = []
    return sorted(set(affinity))


def tabulate(potential, parameters, cutoff='c', rc=2.5, npoints=10000, rmin
    =0.5, fmt='lammps', metadata='', fileout=None, precision=14):
    """Tabulate a potential."""
    from atooms.core.utils import tipify
    from atooms.interaction.potential import PairPotential
    from atooms.interaction.cutoff import CutOff
    if isinstance(parameters, dict):
        param_dict = parameters
    else:
        param_dict = {}
        for param in parameters.split(','):
            key, value = param.split('=')
            param_dict[key] = tipify(value)
    potential = PairPotential(potential, param_dict, (1, 1))
    if cutoff is not None:
        potential.cutoff = CutOff(cutoff, rc)
    rsq, u0, u1, u2 = potential.tabulate(npoints, rmin=rmin, what='uwh')
    r = rsq ** 0.5
    if fmt == 'lammps':
        u1 *= r
        txt = '\n\nPOTENTIAL\nN {}\n\n'.format(len(rsq))
        i = 1
        for x, y, z in zip(r, u0, u1):
            txt += '{} {} {} {}\n'.format(i, x, y, z)
            i += 1
    elif fmt == 'uwh':
        txt = '# {} columns: r, u, w, h\n'.format(metadata)
        for x, y, z, w in zip(r, u0, u1, u2):
            txt += '{:.14g} {:.14g} {:.14g} {:.14g}\n'.format(x, y, z, w)
    else:
        u1 *= r
        txt = '# {} columns: r, u, f\n'.format(metadata)
        for x, y, z in zip(r, u0, u1):
            txt += '{} {} {}\n'.format(x, y, z)
    if fileout is None:
        return txt
    else:
        with open(fileout, 'w') as fh:
            fh.write(txt)


def shamelessly_promote():
    """
    Shamelessly promote our little community.
    """
    """
    click.echo("Need " + click.style("help", fg='green', bold=True) +
               "? Found a " + click.style("bug", fg='green', bold=True) +
               "? Let us " + click.style("know", fg='green', bold=True) + "! :D")
    click.echo("File bug reports on " + click.style("GitHub", bold=True) + " here: "
               + click.style("https://github.com/Miserlou/Zappa", fg='cyan', bold=True))
    click.echo("And join our " + click.style("Slack", bold=True) + " channel here: "
               + click.style("https://slack.zappa.io", fg='cyan', bold=True))
    click.echo("Love!,")
    click.echo(" ~ Team " + click.style("Zappa", bold=True) + "!")
    """


def _messages_iter(msg_iter):
    """Used to iterate through messages."""
    try:
        msg = next(msg_iter)
        yield msg.pop('claim')
        yield msg
        for msg in msg_iter:
            del msg['claim']
            yield msg
    except StopIteration:
        return


def expandFiles(args):
    """Expands any file wildcards contained in args and returns an expanded,
    explicit list.
    """
    return args


def weather_generation_validator(_, __, generation: int) ->None:
    """Only Generation 3 ~ 5 are supported."""
    if generation not in range(3, 6):
        raise ValueError(
            f'Invalid generation: Gen. {generation}. Only Generation 3 ~ 5 are supported.'
            )


def classifiers2string(l):
    """
    shorten the list of classifiers into a string

    @param      l       list
    @return             string

    Example::

        ['Development Status :: 4 - Beta', 'Programming Language :: Python',
         'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7',
         'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.3',
         'Programming Language :: Python :: 3.4', 'License :: OSI Approved :: Apache Software License']
    """
    if l is None:
        return ''
    res = []
    for cl in sorted(set(l)):
        if cl == 'Programming Language :: Python':
            s = 'Python'
        else:
            s = cl.replace('Programming Language :: Python :: ', '')
            s = s.replace('License :: ', '')
            s = s.replace('OSI Approved :: ', 'OSI Approved ')
            s = s.replace('Development Status :: ', '')
            s = s.replace('Operating System :: Microsoft :: ', '')
            s = s.replace('Operating System :: ', '')
            s = s.replace('Implementation :: ', '')
            s = s.replace('Programming Language :: ', '')
            s = s.replace('Environment :: ', '')
            s = s.replace('POSIX :: ', '')
            s = s.replace('BSD :: ', '')
        if s == '3 :: Only':
            s = 'py3'
        elif s == '2 :: Only':
            s = 'py2'
        elif s == 'MacOS :: MacOS X':
            s = 'MacOS/X'
        elif s == 'POSIX :: Linux':
            s = 'Linux'
        elif s == 'POSIX :: BSD :: FreeBSD':
            s = 'FreeBSD'
        elif s == 'X11 Applications :: Qt':
            s = 'Qt'
        elif s.startswith('Windows'):
            s = 'Windows'
        if ('Framework' not in s and 'Topic' not in s and
            'Intended Audience' not in s and 'Natural Language' not in s):
            res.append(s)
    return ', '.join(res)


def truncate(string: str, length: int, append: str='...') ->str:
    """Shorthand for cutting off long strings.

    If length is zero or negative, the string will not be checked or
    truncated. The original string will be returned.
    """
    if len(string) > length > 0:
        return string[:length - len(append)] + append
    return string


def new():
    """Add new paint curve

    """
    pass


def add_dictval_to_list(adict, key, alist):
    """
    Add a value from a dictionary to a list

    Parameters
    ----------
    adict : dictionary
    key : hashable
    alist : list
        List where value should be added
    """
    if key in adict:
        if isinstance(adict[key], str):
            alist.append(adict[key])
        else:
            alist.extend(adict[key])


def render_pep440_pre(pieces):
    """TAG[.post.devDISTANCE] -- No -dirty.

    Exceptions:
    1: no tags. 0.post.devDISTANCE
    """
    if pieces['closest-tag']:
        rendered = pieces['closest-tag']
        if pieces['distance']:
            rendered += '.post.dev%d' % pieces['distance']
    else:
        rendered = '0.post.dev%d' % pieces['distance']
    return rendered


def validate_format(*_arg):
    """
    This method can be overridden with format validation logic.
    """
    return True


def change_frame(frame: float=0.0, snap: bool=False):
    """Interactively change the current frame number

    :param frame: Frame
    :type frame: float
    :param snap: Snap
    :type snap: bool
    """
    pass


def load_from_file(module_path):
    """
    Load a python module from its absolute filesystem path

    Borrowed from django-cms
    """
    from imp import load_module, PY_SOURCE
    imported = None
    if module_path:
        with open(module_path, 'r') as openfile:
            imported = load_module('mod', openfile, module_path, (
                'imported', 'r', PY_SOURCE))
    return imported


def get_scales(args, reg_params):
    """
    Calculate the scaling factors from the coordinate space of the raw data
    (which the cell positions are defined in) to the (usually downsampled)
    reference space that the deformation field is defined in.
    :param args:
    :param reg_params:
    :return:
    """
    x_scale = args.x_pixel_um / float(reg_params.atlas_x_pix_size)
    y_scale = args.y_pixel_um / float(reg_params.atlas_y_pix_size)
    z_scale = args.z_pixel_um / float(reg_params.atlas_z_pix_size)
    return x_scale, y_scale, z_scale


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

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


def p_try_stmt_plus_1(p):
    """try_stmt_plus : except_clause COLON suite"""
    p[1].body = p[3]
    p[0] = [p[1]]


@classmethod
def can_create(cls, interpreter):
    """By default all built-in methods assume that if we can describe it we can create it"""
    if cls.can_describe(interpreter):
        meta = cls.setup_meta(interpreter)
        if meta is not None and meta:
            for src in cls.sources(interpreter):
                if src.exists:
                    if meta.can_copy and not src.can_copy:
                        meta.copy_error = 'cannot copy {}'.format(src)
                    if meta.can_symlink and not src.can_symlink:
                        meta.symlink_error = 'cannot symlink {}'.format(src)
                    if not meta.can_copy and not meta.can_symlink:
                        meta.error = ('neither copy or symlink supported: {}'
                            .format(meta.copy_error, meta.symlink_error))
                else:
                    meta.error = 'missing required file {}'.format(src)
                if meta.error:
                    break
                meta.sources.append(src)
        return meta
    return None


@classmethod
def isEnabled(cls):
    """
        Class method to check, if safe browsing is enabled.

        @return flag indicating the enabled state
        @rtype bool
        """
    return cls.enabled


def aps08_f(x, n):
    """Degree n polynomial"""
    return x * x - (1 - x) ** n


def root_list(*args):
    """
        from elist.elist import *
        from elist.jprint import pobj
        root_list([1],2,[1,2,3])
    """
    return list(args)


def rebuild_command(args):
    """Rebuilds a unicode command string prepared to be stored in a file

    """
    return '%s\n' % u' '.join(args).replace('\\', '\\\\')


def _make_event_body(x):
    """Convert the fields of an event to a list"""
    body = []
    if x:
        body = [x[0]] + x[1]
    return body


def prepare_str_to_sign(req_tgt, hdrs):
    """
    Concatenates Intersight headers in preparation to be RSA signed

    :param req_tgt : http method plus endpoint
    :param hdrs: dict with header keys
    :return: concatenated header authorization string
    """
    ss = ''
    ss = ss + '(request-target): ' + req_tgt.lower() + '\n'
    length = len(hdrs.items())
    i = 0
    for key, value in hdrs.items():
        ss = ss + key.lower() + ': ' + value
        if i < length - 1:
            ss = ss + '\n'
        i += 1
    return ss


@staticmethod
def spec_to_yaml(spec):
    """
        Convert a given specification to yaml. Used by the add_spec function to render a spec
        as YAML in the RST document

        :param spec: Specification data structure
        :type spec: GroupSpec, DatasetSpec, AttributeSpec, LinkSpec

        :return: YAML string for the current specification
        """
    import json
    import sys
    try:
        from ruamel import yaml
    except ImportError:
        import yaml
    clean_spec = json.loads(json.dumps(spec, sort_keys=True, indent=4,
        separators=(',', ': ')))
    if sys.version_info[0] == 3:
        return yaml.dump(clean_spec, default_flow_style=False)
    else:
        return yaml.safe_dump(clean_spec, default_flow_style=False)


def dissolve_verts():
    """Delete selected control points, correcting surrounding handles

    """
    pass


def provider_parser(subparser):
    """Configure provider parser for powerdns"""
    subparser.add_argument('--auth-token', help=
        'specify token for authentication')
    subparser.add_argument('--pdns-server', help='URI for PowerDNS server')
    subparser.add_argument('--pdns-server-id', help=
        'Server ID to interact with')
    subparser.add_argument('--pdns-disable-notify', help=
        'Disable slave notifications from master')


def clear_orientations(link):
    """
    Resets the orientations on the crossings of a link to default values
    """
    link.link_components = None
    for i in link.crossings:
        i.sign = 0
        i.directions.clear()


@classmethod
def set_cluster(cls, value):
    """
        sets the default cluster
        :param value: the cluster name as defined in the cloudmesh yaml file.
        :return:
        """
    cls.set('cluster', value)


def _is_visible(idx_row, idx_col, lengths) ->bool:
    """
    Index -> {(idx_row, idx_col): bool}).
    """
    return (idx_col, idx_row) in lengths


def check_domain_running(conn, guestname, logger):
    """ check if the domain exists, may or may not be active """
    guest_names = []
    ids = conn.listDomainsID()
    for id in ids:
        obj = conn.lookupByID(id)
        guest_names.append(obj.name())
    if guestname not in guest_names:
        logger.error("%s doesn't exist or not running" % guestname)
        return 1
    else:
        return 0


def iso_format(datetime):
    """
    Generates an ISO formatted datetime based on what the LaunchKey API
    expects. This is a standard ISO datetime without microseconds.
    :param datetime: datetime.datetime object
    :return: ISO formatted string IE: 2017-10-03T22:50:15Z
    """
    return datetime.strftime('%Y-%m-%dT%H:%M:%SZ'
        ) if datetime is not None else None


def assure_tuple_or_list(obj):
    """Given an object, wrap into a tuple if not list or tuple
    """
    if isinstance(obj, list) or isinstance(obj, tuple):
        return obj
    return obj,


def check(name, type, country, geometry, **args):
    """Should return True if this area is NEW, False if we should match"""
    return False


@classmethod
def get_cache_attr_name(cls):
    """
        Returns the name of the attribute that should be used to store
        a reference to the fetched/created object on a request.
        """
    return '_{}.{}'.format(cls._meta.app_label, cls._meta.model_name).lower()


@classmethod
def from_separate_bin(cls, data_bin, tag_bin):
    """Given the data and tag master keys in binary, validate and
        unpack them.

        :param bytes data_bin: The binary data master key
        :param bytes tag_bin: The binary tag master key
        :return: Two instances of :class:`AmiiboMasterKey`.
        :raises ValueError: If the binary data is invalid
        """
    data = cls(data_bin, cls.DATA_BIN_SHA256_HEXDIGEST)
    tag = cls(tag_bin, cls.TAG_BIN_SHA256_HEXDIGEST)
    return data, tag


def contrib_inline_aff(contrib_tag):
    """
    Given a contrib tag, look for an aff tag directly inside it
    """
    aff_tags = []
    for child_tag in contrib_tag:
        if child_tag and child_tag.name and child_tag.name == 'aff':
            aff_tags.append(child_tag)
    return aff_tags


def merge_proteins(proteins):
    """ Merge all isozymes that catalyze a given reaction.
    Automatically removes all isozymes with missing score.

    Args:
        proteins (pandas.Series): list of proteins

    Returns:
        str: boolean rule

    """
    proteins = set(proteins.dropna())
    if not proteins:
        return None
    gpr_str = ' or '.join(sorted(proteins))
    if len(proteins) > 1:
        return '(' + gpr_str + ')'
    else:
        return gpr_str


def p_excludes(t):
    """excludes : empty
                | exclude excludes"""
    if len(t) == 2:
        t[0] = []
    elif len(t) == 3:
        t[0] = [t[1]] + t[2]
    else:
        assert False


@classmethod
def has_archiving(cls):
    """Returns whether it supports archiving items on the fetch process.

        :returns: this backend supports items archive
        """
    return True


@classmethod
def get_short(cls, name):
    """ Returns the short name of a Maya asset name or path

        :param name: str, string that represents a possible name of an object
        :return: str, the short name of the maya node
        """
    return name.split('|')[-1].split('//')[-1]


def get_platform_application_attributes(PlatformApplicationArn=None):
    """
    Retrieves the attributes of the platform application object for the supported push notification services, such as APNS and GCM. For more information, see Using Amazon SNS Mobile Push Notifications .
    See also: AWS API Documentation

    :example: response = client.get_platform_application_attributes(
        PlatformApplicationArn='string'
    )

    :type PlatformApplicationArn: string
    :param PlatformApplicationArn: [REQUIRED]
            PlatformApplicationArn for GetPlatformApplicationAttributesInput.

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

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

    """
    pass


def get_input(prompt, operation, check=None):
    """
    Prompt the user for input and apply ``operation`` to input.
    If ``check`` is not None run the check on the converted input

    If value is invalid it will re-prompt user
    """
    while 1:
        try:
            value = input(prompt)
            value = operation(value)
            if check:
                if not check(value):
                    raise Exception
            return value
        except (ValueError, Exception):
            continue


def ceil_to_kib(int_: int) ->int:
    """Return the ceiling of an integer to the nearest kibibyte."""
    return int_ - int_ % -1024


def restore_from_snapshot(SnapshotId=None):
    """
    Restores a directory using an existing directory snapshot.
    When you restore a directory from a snapshot, any changes made to the directory after the snapshot date are overwritten.
    This action returns as soon as the restore operation is initiated. You can monitor the progress of the restore operation by calling the  DescribeDirectories operation with the directory identifier. When the DirectoryDescription.Stage value changes to Active , the restore operation is complete.
    See also: AWS API Documentation

    :example: response = client.restore_from_snapshot(
        SnapshotId='string'
    )

    :type SnapshotId: string
    :param SnapshotId: [REQUIRED]
            The identifier of the snapshot to restore from.

    :rtype: dict
    :return: {}

    """
    pass


@classmethod
def nginx_lets_encrypt_update(cls):
    """Renew the let's encrypt certificate."""
    assert cls._ocean.is_started('nginx')
    cls._acme_get_cert()
    cls._ocean.restart('nginx')


@classmethod
def discover(cls, xnetwork: bool=False):
    """
        This method can be used to sent a discovery broadcast into the network to which all the phantom cameras will
        respond. It will return a list of dicts, where each dict contains the information from one of the cameras
        responses.
        The dicts contain the following keys:
        - protocol:     A string defining the protocol used by the phantom camera (always P16)
        - ip:           A string containing the IP address of the camera in the network
        - port:         A string containing the int port, which the camera uses to accept control connections
        - hwver:        A string identifier for the hardware version of the camera
        - serial:       A string containing the int serial number of the camera

        CHANGELOG

        Added 20.05.2019

        Changed 27.05.2019
        Added additional argument boolean flag "xnetwork". If it is true, that means the camera is connected to the
        10G interface and thus a different IP range has to be used

        :param xnetwork:

        :return:
        """
    discovery_socket = cls.get_discovery_socket(xnetwork)
    cls.send_discovery_request(discovery_socket)
    responses = cls.receive_discovery_responses(discovery_socket)
    camera_dicts = []
    for response in responses:
        camera_dict = cls.parse_discovery_response(response[0].decode(
            'utf-8'), response[1])
        camera_dicts.append(camera_dict)
    return camera_dicts


def _broadcast_weights(da_data, da_weight, keep_attrs=False):
    """broadcasts da_weights to the same shape as da_data and     masks the same missing values"""
    da_data = da_data.copy()
    ones = da_data * 0 + 1
    weights_expanded = ones * da_weight
    if keep_attrs:
        weights_expanded.attrs = da_data.attrs
    return weights_expanded


def trim_middle(arbitrary_string: str, max_length: int) ->str:
    """
    Trim down strings to max_length by cutting out the middle.
    This assumes that the most "interesting" bits are toward
    the beginning and the end.

    Adds a highly unusual '✂✂✂' in the middle where characters
    were stripped out, to avoid not realizing about the stripped
    info.
    """
    size = len(arbitrary_string)
    if size <= max_length:
        return arbitrary_string
    else:
        half_len, is_odd = divmod(max_length, 2)
        first_half = arbitrary_string[:half_len - 1]
        last_half_len = half_len - 2 + is_odd
        if last_half_len > 0:
            last_half = arbitrary_string[last_half_len * -1:]
        else:
            last_half = ''
        return f'{first_half}✂✂✂{last_half}'


def render_articles_summary(term, scroll_idx, height, articles):
    """ Render and return articles summary in-view. """
    _vpadd = 2 if term.height > 25 else 1
    _endline = term.clear_eol + u'\r\n'
    line_no = 0
    output = u''
    _start, _end = scroll_idx, scroll_idx + height // _vpadd
    for line in articles[_start:_end]:
        _subsq_indent = (term.strip_seqs(line[:30]).find(' ') or -1) + 1
        for subline in term.wrap(line, subsequent_indent=u' ' * _subsq_indent):
            if line_no < height:
                output = u''.join((output, subline, _endline))
                line_no += 1
        if line_no + (_vpadd - 1) < height:
            output = u''.join((output, _endline * (_vpadd - 1)))
            line_no += _vpadd - 1
        else:
            break
    remaining = _endline * (height - line_no)
    output = u''.join((output, remaining))
    return output


def rle_len(rle):
    """
    Return the number of characters covered by a run length
    encoded attribute list.
    """
    run = 0
    for v in rle:
        assert type(v) == tuple, repr(rle)
        a, r = v
        run += r
    return run


def validateObject(obj):
    """Validate an object.

        The same semantics as in ``validate()`` apply, except that the values
        are retrieved from the object and not the data dictionary.
        """


@classmethod
def withISO(cls, iso, path=()):
    """
        Pre-wrap the ISO-bearing FilePath in an ISO object.

        Useful for sharing the ISO object's caches amongst all ISOPaths
        accessing it.
        """
    self = cls(path=path)
    self._iso = iso
    return self


def _copy(data=None, out=None, name=None, **kwargs):
    """Returns a copy of the input.

    From:src/operator/tensor/elemwise_unary_op_basic.cc:246

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

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

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


def p_bit_expr(p):
    """bit_expr : simple_expr"""
    p[0] = 'simple_expr', p[1]


@classmethod
def view_list(cls, lst):
    """
        Create a dict that is a view on the given list
        """
    if not isinstance(lst, list):
        raise TypeError(
            '%s.view_list(obj) takes only actual list objects, not %r' % (
            cls.__name__, lst))
    obj = cls()
    obj._items = lst
    return obj


@classmethod
def get_option_color(cls, option):
    """
        Returns QColor depending of the option passed as argument
        :param option: str, Option to get color
        """
    if option == 'Grey':
        color = cls.fromRgbF(0.4, 0.4, 0.4)
    elif option == 'Cancel':
        color = cls.fromRgbF(0.7, 0.5, 0.4)
    elif option == 'OK' or option == 'ok':
        color = cls.fromRgbF(0.5, 0.7, 0.8)
    elif option == 'Warning' or option == 'Warn':
        color = cls.fromRgbF(0.7, 0.2, 0.2)
    elif option == 'Collapse':
        color = cls.fromRgbF(0.15, 0.15, 0.15)
    elif option == 'Subtle':
        color = cls.fromRgbF(0.48, 0.48, 0.6)
    else:
        color = cls.fromRgbF(0.48, 0.48, 0.6)
    if option:
        color.ann = option + ' Color'
    return color


def lookupText(name, timeout=None):
    """
        Perform a TXT record lookup.

        @type name: C{str}
        @param name: DNS name to resolve.

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

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


def zero_grad(model):
    """ set gradient of model to zero without having to use an optimizer object """
    for param in model.parameters():
        if param.grad is not None:
            param.grad.detach_()
            param.grad.zero_()


def socket_set_hwm(socket, hwm=-1):
    """libzmq 2/3/4 compatible sethwm"""
    try:
        socket.sndhwm = socket.rcvhwm = hwm
    except AttributeError:
        socket.hwm = hwm


def var(seq):
    """Return the variance of all values from seq."""
    N = S = S2 = 0.0
    for x in seq:
        N += 1
        S += x
        S2 += x ** 2
    return S2 / N - (S / N) ** 2


@classmethod
def translate_dict_keys_to_data_model(cls, wsme_dict):
    """Translate the keys from wsme class type, to data_model."""
    if not hasattr(cls, '_type_to_model_map'):
        return wsme_dict
    res = {}
    for k, v in wsme_dict.items():
        if k in cls._type_to_model_map:
            k = cls._type_to_model_map[k]
            if '.' in k:
                parent, child = k.split('.')
                if parent not in res:
                    res[parent] = {}
                res[parent][child] = v
                continue
        res[k] = v
    return res


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 split_sbtabs(sbtab_strings):
    """
    Cuts one SBtab string in single SBtab strings if necessary.

    Parameters
    ----------
    sbtab_strings: str
        SBtab table or tables in string representation.

    Returns: list
        List of SBtab tables in string representation.
    """
    sbtabs = []
    sbtab_string = ''
    counter = 1
    for row in sbtab_strings.split('\n'):
        if row.startswith('!!!') or row.startswith('"!!!'):
            continue
        if row.startswith('!!'):
            if sbtab_string == '':
                sbtab_string = row + '\n'
                continue
            else:
                try:
                    if sbtab_string.startswith('!!SBtab'):
                        sbtabs.append(sbtab_string)
                        counter += 1
                    sbtab_string = row + '\n'
                except:
                    print('Warning: Could not write SBtab %s' % counter)
                    counter += 1
        else:
            sbtab_string += row + '\n'
    if sbtab_string.startswith('!!SBtab'):
        sbtabs.append(sbtab_string)
    return sbtabs


def norm_shape(shape):
    """
    Normalize numpy array shapes so they're always expressed as a tuple,
    even for one-dimensional shapes.

    Parameters
        shape - an int, or a tuple of ints

    Returns
        a shape tuple
    """
    try:
        i = int(shape)
        return i,
    except TypeError:
        pass
    try:
        t = tuple(shape)
        return t
    except TypeError:
        pass
    raise TypeError('shape must be an int, or a tuple of ints')


@classmethod
def _get_xml_child_by_name(cls, node, name):
    """ Get the child of an XML element with a prefix and name

        Args:
            node (:obj:`libsbml.XMLNode`): XML node
            name (:obj:`XmlName`): name

        Returns:
            :obj:`libsbml.XMLNode`: child with prefix and name
        """
    matching_children = []
    for i_child in range(node.getNumChildren()):
        child = node.getChild(i_child)
        if child.getPrefix() == name.prefix and child.getName() == name.name:
            matching_children.append(child)
    if len(matching_children) == 1:
        return matching_children[0]
    else:
        return None


def get_response_json(response):
    """
    Safe loads JSON response. If response is not json serialized - return it
    as content key.

    :param response: response
    :type response: requests response object
    :returns: dict -- dictionary with loaded json response
    """
    try:
        response_json = response.json()
    except ValueError:
        response_json = {'content': response.content}
    response_json.update({'status_code': response.status_code, 'ok':
        response.ok})
    return response_json


@classmethod
def get_scheme(cls, hashed_passord):
    """
            Return the scheme of ``hashed_passord`` or raise :attr:`BadHash`

            :param bytes hashed_passord: A hashed password
            :return: The scheme used by the hashed password
            :rtype: bytes
            :raises BadHash: if no valid scheme is found within ``hashed_passord``
        """
    if not hashed_passord[0] == b'{'[0] or b'}' not in hashed_passord:
        raise cls.BadHash(
            '%r should start with the scheme enclosed with { }' %
            hashed_passord)
    scheme = hashed_passord.split(b'}', 1)[0]
    scheme = scheme.upper() + b'}'
    return scheme


@classmethod
def extract_error_message(cls, e):
    """Extract error message for queries"""
    message = str(e)
    try:
        if isinstance(e.args, tuple) and len(e.args) > 1:
            message = e.args[1]
    except Exception:
        pass
    return message


@classmethod
def get_conf(cls, builder, doctree=None):
    """Return a dictionary of slide configuration for this doctree."""
    result = {'theme': builder.config.slide_theme, 'autoslides': builder.
        config.autoslides, 'slide_classes': []}
    if doctree:
        conf_node = cls.get(doctree)
        if conf_node:
            result.update(conf_node.attributes)
    return result


def is_csv(filename):
    """Determine whether file is CSV by checking extension"""
    return filename.split('.')[-1] == 'csv'


@classmethod
def autoScroller(cls):
    """
        Class method to get a reference to the auto scroller.

        @return reference to the auto scroller object
        @rtype AutoScroller
        """
    if cls._autoScroller is None:
        from .AutoScroll.AutoScroller import AutoScroller
        cls._autoScroller = AutoScroller()
    return cls._autoScroller


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 id_paste():
    """Data-blocks from the clipboard are pasted

    """
    pass


def get_packet(bytes_in):
    """
    Get a properly formatted Dynamixel packet with header and checksum.

    :param bytes_in: A list of bytes in the form ``[b0, b1, b2, ..., bn]``

    :returns: A packet in the form of a list of bytes.
    """
    packet_bytes = [255, 255] + bytes_in
    checksum = 0
    for i in range(2, len(packet_bytes)):
        b = packet_bytes[i]
        checksum += b
    checksum = ~checksum & 255
    packet_bytes.append(checksum)
    return packet_bytes


def name_getter(dict):
    """Get list of names from a dictionary. Used to get the objective names for EHProblem."""
    namelist = []
    for item in dict:
        name = ''
        for part in item:
            if name == '':
                name = name + part
            else:
                name = name + ':' + part
        namelist.append(name)
    return namelist


@staticmethod
def int2Char64(_hex):
    """
        Taken From Java APK Source Code
        """
    if _hex >= 0 and _hex < 26:
        return 65 + _hex
    if _hex >= 26 and _hex < 52:
        return 97 + _hex - 26
    if _hex >= 52 and _hex < 62:
        return 48 + _hex - 52
    if _hex == 62:
        return 95
    if _hex == 63:
        return 45
    return -1


def serialize_uint256(n: int) ->bytes:
    """
    Serialize an unsigned integer ``n`` as 32 bytes (256 bits) in big-endian
    order.

    Corresponds directly to the "ser_256(p)" function in BIP32
    (https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#conventions).

    :param n: The integer to be serialized.

    :return: A byte sequence containing the serialization of ``n``.
    """
    return n.to_bytes(32, 'big')


def get_key(s):
    """
    Get data between [ and ] remove ' if exist
    @param s: string to process
    """
    start = s.find('[')
    end = s.find(']')
    if start == -1 or end == -1:
        return None
    if s[start + 1] == "'":
        start += 1
    if s[end - 1] == "'":
        end -= 1
    return s[start + 1:end]


def format_commas(number):
    """
    Rounds a number and formats it with commas i.e. 123,456,789
    """
    if number is None:
        return None
    return '{:,.0f}'.format(number)


def generate_error(code, message, action):
    """
    Generates an error to report test failure.

    Args:
        code(int): error code to display.
        message(str): error string to display.
        action(:class:`niveristand.clientapi.ErrorAction`): action to perform.

    Returns:
        If action is Continue, returns the generated error.

    """
    from niveristand.clientapi._realtimesequencedefinitionapi.erroraction import ErrorAction
    from niveristand import errors
    from niveristand.library._tasks import get_scheduler
    assert isinstance(action, ErrorAction)
    error = errors.SequenceError(code, message, action)
    get_scheduler().get_task_for_curr_thread().error = error
    if action is ErrorAction.ContinueSequenceExecution:
        return error
    else:
        raise error


def gff_record_to_string(gff_record):
    """Convert a `GFF_record` namedtuple to a string representation."""
    fields = list(gff_record)
    fields[3], fields[4] = str(fields[3]), str(fields[4])
    attributes = []
    for k, v in fields[8].items():
        if isinstance(v, list):
            v = ','.join(v)
        attributes.append('%s=%s' % (k, v))
    fields[8] = ';'.join(attributes)
    return '%s\n' % '\t'.join(fields)


def _getch():
    """
    Просьба нажать клавишу на клавиатуре.
    Реализация зависит от используемой ОС (поддерживаем Windows и *nix)
    """
    print('Press any key to continue...')
    try:
        from msvcrt import getch
        getch()
    except ImportError:

        def getch():
            import sys, tty, termios
            fd = sys.stdin.fileno()
            old = termios.tcgetattr(fd)
            try:
                tty.setraw(fd)
                return sys.stdin.read(1)
            finally:
                termios.tcsetattr(fd, termios.TCSADRAIN, old)
        getch()


def maybe_update(sources, destination, func):
    """
    Updates defaults (values) in destination with values from func
    applied to sources, only if defaults (values) not already specified.
    """
    for source in sources:
        for key, value in func(source).items():
            if key not in destination:
                destination[key] = value


def extend_values(dictionary, key, items):
    """Extend the values for that key with the items"""
    values = dictionary.get(key, [])
    try:
        values.extend(items)
    except TypeError:
        raise TypeError('Expected a list, got: %r' % items)
    dictionary[key] = values


def fatal_error(error):
    """Print out the error message that gets passed, then quit the program.

    Inputs:
    error = error message text

    :param error: str
    :return:
    """
    raise RuntimeError(error)


def code_from_builtin(shader_name: str) ->dict:
    """Exposes the internal shader code for query.

    :param shader_name: One of these builtin shader names: { ‘2D_UNIFORM_COLOR’, ‘2D_FLAT_COLOR’, ‘2D_SMOOTH_COLOR’, ‘2D_IMAGE’, ‘3D_UNIFORM_COLOR’, ‘3D_FLAT_COLOR’, ‘3D_SMOOTH_COLOR’}
    :type shader_name: str
    :return:  Vertex, fragment and geometry shader codes.
    """
    pass


def load_numpy_core_numerictypes(finder, module):
    """the numpy.core.numerictypes module adds a number of items to itself
       dynamically; define these to avoid spurious errors about missing
       modules"""
    module.AddGlobalName('bool_')
    module.AddGlobalName('cdouble')
    module.AddGlobalName('complexfloating')
    module.AddGlobalName('csingle')
    module.AddGlobalName('double')
    module.AddGlobalName('float64')
    module.AddGlobalName('float_')
    module.AddGlobalName('inexact')
    module.AddGlobalName('intc')
    module.AddGlobalName('int32')
    module.AddGlobalName('number')
    module.AddGlobalName('single')


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


def create_perm(app, created_models, verbosity, db, **kwargs):
    """
    Creates a fake content type and permission
    to be able to check for permissions
    """
    from django.contrib.auth.models import Permission
    from django.contrib.contenttypes.models import ContentType
    from django import VERSION
    if ContentType._meta.installed and Permission._meta.installed:
        if VERSION >= (1, 8):
            content_type, created = ContentType.objects.get_or_create(app_label
                ='constance', model='config')
        else:
            content_type, created = ContentType.objects.get_or_create(name=
                'config', app_label='constance', model='config')
        permission, created = Permission.objects.get_or_create(name=
            'Can change config', content_type=content_type, codename=
            'change_config')


def is_probabilistic(estimator):
    """
    Returns True if the given estimator returns a y_score for it's decision
    function, e.g. has ``predict_proba`` or ``decision_function`` methods.

    Parameters
    ----------
    estimator : class or instance
        The object to test if is probabilistic, especially a Scikit-Learn
        estimator or Yellowbrick visualizer.
    """
    return any([hasattr(estimator, 'predict_proba'), hasattr(estimator,
        'decision_function')])


def if_voltage_update(v_cur, v_in, alpha, refrac_counts):
    """Calculate change in cell's voltage based on current and incoming voltage, no decay.

    :param v_cur: Current voltages
    :param v_in: Incoming voltages
    :param alpha: Incoming voltage scaling factor
    :param refrac_counts: Number of timesteps left in refractory state for each :class:`Neuron`

    :return: New voltages
    """
    v_delta = alpha * v_in
    non_refrac = refrac_counts == 0
    v_cur += v_delta * non_refrac.to(v_delta.dtype)
    return v_cur


def xld_track_matches():
    """Return a dictionary containing the XLD matches."""
    return {'full_line': [['log4', 'Statistics'], ['good',
        '->Accurately ripped'], ['badish',
        '->Track not present in AccurateRip database'], ['bad',
        '->Rip may not be accurate'], ['bad',
        'List of damaged sector positions +:'], ['badish',
        '\\(\\d+\\) \\d{2}:\\d{2}:\\d{2}'], ['log3',
        '\\/.+\\.(?:[Ff][Ll][Aa][Cc]|[Ww][Aa][Vv]|[Mm][Pp]3|[Aa][Aa][Cc])']
        ], 'crc': ['CRC32 hash \\(test run\\)', 'CRC32 hash'], 'statistics':
        {'bad': ['Read error', 'Skipped \\(treated as error\\)',
        'Inconsistency in error sectors', 'Damaged sector count'], 'badish':
        ['Jitter error \\(maybe fixed\\)', 'Retry sector count',
        'Edge jitter error \\(maybe fixed\\)',
        'Atom jitter error \\(maybe fixed\\)',
        'Drift error \\(maybe fixed\\)',
        'Dropped bytes error \\(maybe fixed\\)',
        'Duplicated bytes error \\(maybe fixed\\)']}}


def count_lines(file):
    """Returns the number of lines in a file."""
    count = 0
    for line in open(file, 'r'):
        count = count + 1
    return count


def create_temporary_directory(prefix_dir=None):
    """Creates a temporary directory and returns its location"""
    from tempfile import mkdtemp
    return mkdtemp(prefix='bloom_', dir=prefix_dir)


@classmethod
def is_arithmetic_operator(cls, operator):
    """
        Checks whether a given operator is arithmetic.
        """
    return operator in cls._arithmetic_types


@staticmethod
def tokenize(stream, separator):
    """
        Tokenize and yield query parameter values.

        :param stream: Input value
        :param separator: Character to use to separate the tokens.
        :return:
        """
    for value in stream:
        for token in value.split(separator):
            if token:
                yield token.strip()


def geo_name(year, release, summary_level):
    """Resolve a year, release and summar level number into a URL geography group name"""
    if int(summary_level) in (140, 150):
        return 'Tracts_Block_Groups_Only'
    else:
        return 'All_Geographies_Not_Tracts_Block_Groups'


def _get_sd_value(dvm, el_name, param_name):
    """Return a single SD value (with unit)."""
    sd_name = param_name + '_' + el_name
    plain_value = dvm.GetFloatValueSD(sd_name.upper())
    return plain_value / 1000


def package_root(varmap):
    """Return correct directory for the root package.

    The "root" package is the one where modules which do not
    belong to a package are located (see the distutils docs).
    This function should not need to be called directly in
    your actual setup script, but it can be useful for testing.
    Note that it looks for a ``package_root`` setup var, which
    can be used as a shortcut if your "root" package is not in
    the standard place (which is your setup script directory),
    rather than having to declare a ``package_dir`` dictionary
    with a ``''`` key.
    """
    try:
        return varmap['package_dir']['']
    except KeyError:
        try:
            result = varmap['package_root']
        except KeyError:
            return '.'
        else:
            dirmap = varmap.setdefault('package_dir', {})
            if '' not in dirmap:
                dirmap[''] = result
            return result


def split_sequence(seq, n):
    """Generates tokens of length n from a sequence.
    The last token may be of smaller length."""
    tokens = []
    while seq:
        tokens.append(seq[:n])
        seq = seq[n:]
    return tokens


@classmethod
def matching_trigger_signal(cls, mysignal, trigger_s):
    """
        Given a physiosignal object (mysignal), return another one with the same timing but
        with 'signal' the trigger_s
        """
    assert isinstance(mysignal, cls
        ), 'You can only add physiosignals to physiodata'
    return cls(label='trigger', signal=trigger_s, samples_per_second=
        mysignal.samples_per_second, sampling_times=mysignal.sampling_times,
        physiostarttime=mysignal.physiostarttime, neuralstarttime=mysignal.
        neuralstarttime)


def get_stack(f, t, botframe, proc_obj=None):
    """Return a stack of frames which the debugger will use for in
    showing backtraces and in frame switching. As such various frame
    that are really around may be excluded unless we are debugging the
    sebugger. Also we will add traceback frame on top if that
    exists."""
    exclude_frame = lambda f: False
    if proc_obj:
        settings = proc_obj.debugger.settings
        if not settings['dbg_trepan']:
            exclude_frame = lambda f: proc_obj.core.ignore_filter.is_included(f
                )
            pass
        pass
    stack = []
    if t and t.tb_frame is f:
        t = t.tb_next
    while f is not None:
        if exclude_frame(f):
            break
        stack.append((f, f.f_lineno))
        f = f.f_back
        pass
    stack.reverse()
    i = max(0, len(stack) - 1)
    while t is not None:
        stack.append((t.tb_frame, t.tb_lineno))
        t = t.tb_next
        pass
    return stack, i


def is_output_node(node):
    """
    Determine whether a RedBaron node may be expected to generate output.

    Parameters
    ----------
    node : <Node>
        a RedBaron Node.

    Returns
    -------
    bool
        True if node may be expected to generate output, otherwise False.
    """
    if node.type == 'print':
        return True
    output_signatures = [('name', 'name', 'call'), ('name', 'name', 'name',
        'call')]
    output_functions = ['setup', 'run_model', 'run_driver',
        'check_partials', 'check_totals', 'list_inputs', 'list_outputs',
        'list_problem_vars']
    if node.type == 'atomtrailers' and len(node.value) in (3, 4):
        sig = []
        for val in node.value:
            sig.append(val.type)
        func_name = node.value[-2].value
        if tuple(sig) in output_signatures and func_name in output_functions:
            return True
    return False


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

        :param SsoChangeCertType val:
        :rtype: EventType
        """
    return cls('sso_change_cert', val)


def fold(s):
    """Center and fold positions into central cell."""
    for p in s.particle:
        p.position -= s.cell.side / 2
        p.fold(s.cell)
    return s


def formatUnits(units):
    """
    Format a unit specification ([numerators,...], [denominators,...])
    into a string (this is the inverse of evalUnits)
    """
    pass


def cubic(cp):
    """ a=b=c, alpha=beta=gamma=90 """
    anew = (cp[0] + cp[1] + cp[2]) / 3.0
    return [anew, anew, anew, 90.0, 90.0, 90.0]


@staticmethod
def camelCaseToDashName(camelCase):
    """
            camelCaseToDashName - Convert a camel case name to a dash-name (like paddingTop to padding-top)

            @param camelCase <str> - A camel-case string

            @return <str> - A dash-name
        """
    camelCaseList = list(camelCase)
    ret = []
    for ch in camelCaseList:
        if ch.isupper():
            ret.append('-')
            ret.append(ch.lower())
        else:
            ret.append(ch)
    return ''.join(ret)


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

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

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


def _transform_session_persistence(persistence):
    """Transforms session persistence object

    :param persistence: the session persistence object
    :returns: dictionary of transformed session persistence values
    """
    return {'type': persistence.type, 'cookie_name': persistence.cookie_name}


def get_deconfounder(name='residualize'):
    """String to Deconfounding Estimator"""
    name = name.lower()
    if name in ('residualize', 'regressout', 'residualize_linear',
        'regressout_linear'):
        from confounds.base import Residualize
        est = Residualize()
    elif name in ('augment', 'pad'):
        from confounds.base import Augment
        est = Augment()
    elif name in ('dummy', 'passthrough'):
        from confounds.base import DummyDeconfounding
        est = DummyDeconfounding()
    else:
        raise ValueError(
            'Unrecognized model name! Choose one of Residualize, Augment or Dummy.'
            )
    return est


def warn(message):
    """Print a warning message."""
    print('WARNING: {0}'.format(message))


def get_inputs(node, kwargs):
    """Helper function to get inputs"""
    name = node['name']
    proc_nodes = kwargs['proc_nodes']
    index_lookup = kwargs['index_lookup']
    inputs = node['inputs']
    attrs = node.get('attrs', {})
    input_nodes = []
    for ip in inputs:
        input_node_id = index_lookup[ip[0]]
        input_nodes.append(proc_nodes[input_node_id].name)
    return name, input_nodes, attrs


@classmethod
def from_file(cls, file_name):
    """
        Loads the parameter setup from a file.

        """
    parameters = cls()
    with open(file_name) as param_file:
        for line in param_file:
            match = cls._match_line(line)
            if match:
                key, value = match
                parameters[key] = value
    return parameters


def encode_varint(num):
    """return an encoded int32, int64, uint32, uint64, sint32, sint64, bool, or
    enum"""
    _next = 1
    values = []
    while _next:
        _next = num >> 7
        shift = 128 if _next else 0
        part = num & 127 | shift
        values.append(part)
        num = _next
    return values


def disable_javaplugin():
    """
        disable_javaplugin

        Disable Java plugin

        @return: None
        """


@staticmethod
def get_events(event_filter, pagination=None):
    """Return an iterable of model.Event objects."""


def rgb2hex(r, g, b):
    """Converts RGB values into Hexadecimal

    Reference
    ---------

    https://stackoverflow.com/a/43572620/8765762
    """
    return '#{:02x}{:02x}{:02x}'.format(r, g, b)


def list_split(val, split_on_comma=True):
    """Try to split each member of a list with comma separator.
    If we don't have to split just return val

    :param val: value to split
    :type val:
    :param split_on_comma:
    :type split_on_comma: bool
    :return: list with members split on comma
    :rtype: list

    >>> list_split(['a,b,c'], False)
    ['a,b,c']

    >>> list_split(['a,b,c'])
    ['a', 'b', 'c']

    >>> list_split('')
    []

    """
    if not split_on_comma:
        return val
    new_val = []
    for subval in val:
        if isinstance(subval, list):
            continue
        new_val.extend(subval.split(','))
    return new_val


def create_zip(archive, compression, cmd, verbosity, interactive, filenames):
    """Create a ZIP archive."""
    cmdlist = [cmd, 'a']
    if not interactive:
        cmdlist.append('-y')
    cmdlist.extend(['-tzip', '-mx=9', '--', archive])
    cmdlist.extend(filenames)
    return cmdlist


def _embedded_frames(frame_list, frame_format):
    """frame_list should be a list of base64-encoded png files"""
    template = '  frames[{0}] = "data:image/{1};base64,{2}"\n'
    embedded = '\n'
    for i, frame_data in enumerate(frame_list):
        embedded += template.format(i, frame_format, frame_data.replace(
            '\n', '\\\n'))
    return embedded


def ppm_error(axis, xref, mzref):
    """
    computes the error from a array of positions (xref) and the theoretical m/z (mzref)
    returns an array with errors in ppm
    xref : array of point coordinates of the reference points
    mzref: array of reference m/z
    """
    return 1000000.0 * (axis.itomz(xref) - mzref) / mzref


def get_change_token_status(ChangeToken=None):
    """
    Returns the status of a ChangeToken that you got by calling  GetChangeToken . ChangeTokenStatus is one of the following values:
    See also: AWS API Documentation

    Examples
    The following example returns the status of a change token with the ID abcd12f2-46da-4fdb-b8d5-fbd4c466928f.
    Expected Output:

    :example: response = client.get_change_token_status(
        ChangeToken='string'
    )

    :type ChangeToken: string
    :param ChangeToken: [REQUIRED]
            The change token for which you want to get the status. This change token was previously returned in the GetChangeToken response.

    :rtype: dict
    :return: {
        'ChangeTokenStatus': 'PROVISIONED'|'PENDING'|'INSYNC'
    }

    """
    pass


def agent(request):
    """
    A context processor that sets the template context variable ``agent`` to
    the value of ``request.agent``.
    """
    return {'agent': getattr(request, 'agent', None)}


def red(string):
    """foreground color to red"""
    return '\x1b[31m' + string + '\x1b[0m'


def get_client_ip(request):
    """Get client's ip address."""
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip_address = x_forwarded_for.split(',')[0]
    else:
        ip_address = request.META.get('REMOTE_ADDR')
    return ip_address


def truncate(st, size=80):
    """
    Truncate string to the maximum given size.
    """
    if size is None:
        return st
    if len(st) > size:
        return st[:size - 3] + '...'
    return st


def _get_url_utils():
    """
    Get URL utilities through versions, despite them being moved and refactored (with new "path()" syntax).
    """
    try:
        from django.urls import get_callable, RegexURLPattern, URLPattern, RegexURLResolver, URLResolver, NoReverseMatch
    except ImportError:
        from django.core.urlresolvers import get_callable, RegexURLPattern, RegexURLPattern as URLPattern, RegexURLResolver, RegexURLResolver as URLResolver, NoReverseMatch
    return (get_callable, RegexURLPattern, URLPattern, RegexURLResolver,
        URLResolver, NoReverseMatch)


def _client_error_status(ex):
    """Get the HTTP status for the given ClientError"""
    resp = ex.response
    return resp.get('Error', {}).get('HTTPStatusCode') or resp.get(
        'ResponseMetadata', {}).get('HTTPStatusCode')


def get_two_by_two_edges(*edges):
    """ create the list of edges

    Parameters
    ----------
    * edges : list or tuple
        each consecutive elements will be an edge

    Returns
    -------
    list of 2-uple for the edges
    """
    two_by_two_edges = []
    for list_of_edge in edges:
        if len(list_of_edge) <= 1:
            raise ValueError('edges must be of length at least 2')
        if not isinstance(list_of_edge, (tuple, list)):
            raise TypeError(
                "argument should be tuple or list, instead i got : '%s' " %
                type(list_of_edge))
        for e1, e2 in zip(list_of_edge[:-1], list_of_edge[1:]):
            two_by_two_edges.append((e1, e2))
    return two_by_two_edges


def getcwd():
    """Return the path from which a canonical path is calculated.

        This path should be used to resolve file patterns or to convert
        canonical paths back to file paths for display. It shouldn't be
        used to get real file paths. Use vfs functions instead.
        """


def describe_load_balancers(AutoScalingGroupName=None, NextToken=None,
    MaxRecords=None):
    """
    Describes the load balancers for the specified Auto Scaling group.
    Note that this operation describes only Classic Load Balancers. If you have Application Load Balancers, use  DescribeLoadBalancerTargetGroups instead.
    See also: AWS API Documentation

    Examples
    This example describes the load balancers attached to the specified Auto Scaling group.
    Expected Output:

    :example: response = client.describe_load_balancers(
        AutoScalingGroupName='string',
        NextToken='string',
        MaxRecords=123
    )

    :type AutoScalingGroupName: string
    :param AutoScalingGroupName: [REQUIRED]
            The name of the group.

    :type NextToken: string
    :param NextToken: The token for the next set of items to return. (You received this token from a previous call.)

    :type MaxRecords: integer
    :param MaxRecords: The maximum number of items to return with this call. The default value is 50 and the maximum value is 100.

    :rtype: dict
    :return: {
        'LoadBalancers': [
            {
                'LoadBalancerName': 'string',
                'State': 'string'
            },
        ],
        'NextToken': 'string'
    }

    :returns:
    Adding - The instances in the group are being registered with the load balancer.
    Added - All instances in the group are registered with the load balancer.
    InService - At least one instance in the group passed an ELB health check.
    Removing - The instances in the group are being deregistered from the load balancer. If connection draining is enabled, Elastic Load Balancing waits for in-flight requests to complete before deregistering the instances.
    Removed - All instances in the group are deregistered from the load balancer.

    """
    pass


def dequote(s):
    """
    Освобождение строки от апострофов или кавычек.

    Выполняется проверка того, что апострофы или кавычки парные.
    Если апострофы или кавыяке не парные, то возвращается исходная строка.
    """
    if s is not None and len(s) > 1 and s[0] == s[-1] and s.startswith(("'",
        '"')):
        return s[1:-1]
    return s


def build_examples(to_predict):
    """
    Builds a list of dicts in input data format from a list of contexts and qas.
    """
    examples = []
    for row in to_predict:
        context = row['context']
        for qa in row['qas']:
            qa['answers'] = [{'text': ' ', 'answer_start': 0}]
            qa['is_impossible']: False
        example = {'context': context, 'qas': row['qas']}
        examples.append(example)
    return examples


def complete_message(buf):
    """returns msg,buf_remaining or None,buf"""
    lines = buf.split('\r\n')
    if len(lines) <= 1:
        return None, buf
    nargs_raw = lines.pop(0)
    assert nargs_raw[0] == '*'
    nargs = int(nargs_raw[1:])
    args = []
    while len(lines) >= 2:
        if lines[0][0] == '+':
            args.append(lines.pop(0))
        elif lines[0][0] == ':':
            args.append(int(lines.pop(0)[1:]))
        elif lines[0][0] == '$':
            if len(lines) < 3:
                return None, buf
            slen, s = int(lines[0][1:]), lines[1]
            if slen != len(s):
                raise ValueError('length mismatch %s %r' % (slen, s))
            lines = lines[2:]
            args.append(s)
        else:
            raise ValueError('expected initial code in %r' % lines)
    if len(args) == nargs:
        return args, '\r\n'.join(lines)
    else:
        return None, buf


def normalise_day(day_frac):
    """Adjust the day fraction to correspond to the current day.
      day_frac is a fractional day, so should be adjusted to fall in the range
      0-1 if necessary (Astro. Alg. p.98)
      TODO: This may not be what you want, if you are concerned about the next
      rise or set, as opposed to one that happened today.
    """
    if day_frac < 0:
        day_frac += 1
    if day_frac > 1:
        day_frac -= 1
    return day_frac


def set(value):
    """sets a value in the cache
        """


def addDefaultQueryFactory(factory):
    """add a default query factory."""


@classmethod
def current(cls):
    """ Return the most recently created FileSystem

        If no instance has been created, then create one with defaults
        """
    if not len(cls._cache):
        return cls()
    else:
        return list(cls._cache.values())[-1]


def filter_default(input_dict, params_default):
    """
    Filter input parameters with default params.

    :param input_dict: input parameters
    :type input_dict : dict
    :param params_default: default parameters
    :type params_default : dict
    :return: modified input_dict as dict
    """
    for i in params_default.keys():
        if i not in input_dict.keys():
            input_dict[i] = params_default[i]
    return input_dict


def pytest_saltfactories_syndic_verify_configuration(request, syndic_config,
    username):
    """
    This hook is called to vefiry the provided syndic configuration
    """


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


@classmethod
def select_best_block(cls, blocks):
    """
        Return best block label selected based on simple heuristic.

        :param blocks: OrderedDict map of label to list of instructions
        """
    if not blocks:
        raise ValueError('No suitable blocks were found in assembly.')
    best_block_label = next(iter(blocks))
    best_metric = cls.compute_block_metric(blocks[best_block_label])
    for label, block in list(blocks.items())[1:]:
        metric = cls.compute_block_metric(block)
        if best_metric < metric:
            best_block_label = label
            best_metric = metric
    return best_block_label