def append_order_by_clauses(executable, order_by_clauses):
    """Add ORDER BY SQLAlchemy Column objects to a Select object.

    :param executable: SQLAlchemy executable query object.
    :type executable: Select
    :param order_by_clauses: MySQL ORDER BY clause-related SQLAlchemy object(s).
    :type order_by_clauses: Column
    :type order_by_clauses: list
    :returns: MySQL expression-related SQLAlchemy exectuable.
    :rtype: Select
    """
    if order_by_clauses is None:
        return executable
    if isinstance(order_by_clauses, list):
        for clause in order_by_clauses:
            executable = executable.order_by(clause)
    else:
        executable = executable.order_by(order_by_clauses)
    return executable


@classmethod
def _constructor(cls, mapping, configuration):
    """
        A standardized constructor.
        """
    return cls(mapping, sequence_type=configuration)


def sqliteRowToDict(sqliteRow):
    """
    Unpacks a single sqlite row as returned by fetchone
    into a simple dict.

    :param sqliteRow: single row returned from fetchone DB call
    :return: dictionary corresponding to this row
    """
    return dict(zip(sqliteRow.keys(), sqliteRow))


def float_or_int(value):
    """
    To check if the given value is really float or int. If it is int it returns an int type value

    Args:
        value (Union(int, float)): The value to check

    Returns:
        Union(int,float): The value converted to int if possible
    """
    assert isinstance(value, (int, float)
        ), 'The given value is not int or float is instead {}'.format(type(
        value))
    return int(value) if int(value) == value else value


@staticmethod
def _get_node_artifacts(node):
    """ Get a dict will the node artifacts """
    artifacts_dict = {}
    artifacts = node.type_definition.get_value('artifacts', node.entity_tpl,
        True)
    if artifacts:
        if isinstance(artifacts, dict):
            artifacts_dict = artifacts
        else:
            for elem in artifacts:
                artifacts_dict.update(elem)
    return artifacts_dict


def set_name_q_sp(name_x, w_lags, name_q, lag_q, force_all=False):
    """Set the spatial instrument names in regression; return generic name if user
    provides no explicit name."

    Parameters
    ----------
    name_x      : list of strings
                  User provided exogenous variable names.
    w_lags      : int
                  User provided number of spatial instruments lags

    Returns
    -------
    name_q_sp   : list of strings

    """
    if force_all:
        names = name_x
    else:
        names = name_x[1:]
    if lag_q:
        names = names + name_q
    sp_inst_names = []
    for j in names:
        sp_inst_names.append('W_' + j)
    if w_lags > 1:
        for i in range(2, w_lags + 1):
            for j in names:
                sp_inst_names.append('W' + str(i) + '_' + j)
    return sp_inst_names


def repack_fileheader(dic):
    """
    Repack blockheader dic bit flag parameters into status and mode.
    """
    dic['status'] = dic['S_DATA'] * 1 + dic['S_SPEC'] * 2 + dic['S_32'
        ] * 4 + dic['S_FLOAT'] * 8 + dic['S_COMPLEX'] * 16 + dic[
        'S_HYPERCOMPLEX'] * 32 + dic['S_ACQPAR'] * 128 + dic['S_SECND'
        ] * 256 + dic['S_TRANSF'] * 512 + dic['S_NP'] * 2048 + dic['S_NF'
        ] * 4096 + dic['S_NI'] * 8192 + dic['S_NI2'] * 16384
    return dic


def path_node_counter(in_str):
    """
    return number of nodes in path to compare for number of teres in regex for immediate elimination
    :param in_str:
    :return:
    """
    if in_str is '':
        return None
    return in_str.count('/') + 1


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

        :param DeviceSessionArg val:
        :rtype: RevokeDeviceSessionArg
        """
    return cls('web_session', val)


def _fix_php_version(version):
    """ 'Normalize' an EnpkgVersion to a valid composer version
    string.
    """
    return str(version)


@classmethod
def _collect_valid_settings(meta, clsdict):
    """
        Return a sequence containing the enumeration values that are valid
        assignment values. Return-only values are excluded.
        """
    enum_members = clsdict['__members__']
    valid_settings = []
    for member in enum_members:
        valid_settings.extend(member.valid_settings)
    clsdict['_valid_settings'] = valid_settings


@staticmethod
def _combine_leg_labels(labels):
    """Generate label for legs combined in a :class:`LegPipe`.

        Examples
        --------
        >>> self._combine_leg_labels(['a', 'b', '(c.d)'])
        '(a.b.(c.d))'
        """
    return '(' + '.'.join(labels) + ')'


def palette_name(name, length):
    """Create a palette name like CubeYF_8"""
    return '{0}_{1}'.format(name, length)


def getSessionData(create=1):
    """Return a Session Data Object for the current browser id.

        o If there is no current browser id, and create is true,
          return a new Session Data Object.

        o If there is no current browser id and create is false, returns None.

        o Permission required: Access session data
        """


def whitespace_tokenize(text):
    """Runs basic whitespace cleaning and splitting on a peice of text."""
    text = text.strip()
    if not text:
        return []
    tokens = text.split()
    return tokens


def convert_config(config, exporter_class, allow_other):
    """convert config into required exporter format"""
    filters = {}
    preprocs = {}
    other = {}
    for key, val in config.items():
        if key in ['Exporter.filters', 'TemplateExporter.filters']:
            filters.update(config[key])
        if key in ['Exporter.preprocessors', 'TemplateExporter.preprocessors']:
            if preprocs:
                raise ValueError(
                    "'config' contains both Exporter.preprocessors and TemplateExporter.preprocessors"
                    )
            for p in val:
                pname = p.split('.')[-1]
                preprocs[pname] = {'class': p, 'args': {}}
                if pname in ['LatexDocLinks', 'LatexDocHTML']:
                    preprocs[pname]['args']['metapath'] = '${meta_path}'
                    preprocs[pname]['args']['filesfolder'] = '${files_path}'
    for key, val in config.items():
        if key in ['Exporter.filters', 'TemplateExporter.filters',
            'Exporter.preprocessors', 'TemplateExporter.preprocessors']:
            continue
        if key.split('.')[0] in preprocs:
            preprocs[key.split('.')[0]]['args']['.'.join(key.split('.')[1:])
                ] = val
        else:
            other[key] = val
    if other and not allow_other:
        print('Warning: ignoring other args: {}'.format(other))
        other = {}
    output = {'class': exporter_class, 'filters': filters, 'preprocessors':
        list(preprocs.values()), 'other_args': other}
    return output


@classmethod
def predict(cls, row, typed):
    """
    Predicts a Schema from dict-like row object.
    """
    raise NotImplementedError()


def zip_check(file_path, compression_type):
    """
    Check if the file suffix or the compression type indicates that it is
    a zip file.
    """
    if file_path:
        if file_path.split('/')[-1].split('.')[-1] == 'zip':
            return True
    if compression_type == 'zip':
        return True
    else:
        return False


def breakpoints(next_horizon, first_horizon, horizon, debug=False):
    """ Return the list of restart point (breakpoints), if starting from ``first_horizon`` to ``horizon`` with growth function ``next_horizon``.

    - Also return the gap between the last guess for horizon and the true horizon. This gap should not be too large.
    - Nicely print all the values if ``debug=True``.

    - First examples:

    >>> first_horizon = 1000
    >>> horizon = 30000
    >>> breakpoints(next_horizon__arithmetic, first_horizon, horizon)  # doctest: +ELLIPSIS
    ([1000, 1200, 1400, ..., 29800, 30000], 0)
    >>> breakpoints(next_horizon__geometric, first_horizon, horizon)
    ([1000, 2000, 4000, 8000, 16000, 32000], 2000)
    >>> breakpoints(next_horizon__exponential, first_horizon, horizon)
    ([1000, 31622], 1622)
    >>> breakpoints(next_horizon__exponential_slow, first_horizon, horizon)
    ([1000, 1995, 4265, 9838, 24671, 67827], 37827)
    >>> breakpoints(next_horizon__exponential_fast, first_horizon, horizon)
    ([1000, 1000000], 970000)

    - Second examples:

    >>> first_horizon = 5000
    >>> horizon = 1000000
    >>> breakpoints(next_horizon__arithmetic, first_horizon, horizon)  # doctest: +ELLIPSIS
    ([5000, 5200, ..., 999600, 999800, 1000000], 0)
    >>> breakpoints(next_horizon__geometric, first_horizon, horizon)
    ([5000, 10000, 20000, 40000, 80000, 160000, 320000, 640000, 1280000], 280000)
    >>> breakpoints(next_horizon__exponential, first_horizon, horizon)
    ([5000, 353553, 210223755], 209223755)
    >>> breakpoints(next_horizon__exponential_slow, first_horizon, horizon)
    ([5000, 11718, 29904, 83811, 260394, 906137, 3572014], 2572014)
    >>> breakpoints(next_horizon__exponential_fast, first_horizon, horizon)
    ([5000, 25000000], 24000000)

    - Third examples:

    >>> first_horizon = 10
    >>> horizon = 1123456
    >>> breakpoints(next_horizon__arithmetic, first_horizon, horizon)  # doctest: +ELLIPSIS
    ([10, 210, 410, ..., 1123210, 1123410, 1123610], 154)
    >>> breakpoints(next_horizon__geometric, first_horizon, horizon)
    ([10, 20, 40, 80, 160, 320, 640, 1280, 2560, 5120, 10240, 20480, 40960, 81920, 163840, 327680, 655360, 1310720], 187264)
    >>> breakpoints(next_horizon__exponential, first_horizon, horizon)
    ([10, 31, 172, 2255, 107082, 35040856], 33917400)
    >>> breakpoints(next_horizon__exponential_slow, first_horizon, horizon)
    ([10, 12, 15, 19, 25, 34, 48, 70, 107, 170, 284, 499, 928, 1837, 3895, 8903, 22104, 60106, 180638, 606024, 2294768], 1171312)
    >>> breakpoints(next_horizon__exponential_fast, first_horizon, horizon)
    ([10, 100, 10000, 100000000], 98876544)
    """
    i = 0
    t = max(first_horizon, 2)
    times = [t]
    if debug:
        print(
            """

For the growth function {}, named '{}', first guess of the horizon = {} and true horizon = {} ...
 ==> The times will be:"""
            .format(next_horizon, getattr(next_horizon, '__latex_name__',
            '?'), first_horizon, horizon))
    while t < horizon:
        t = next_horizon(i, t)
        i += 1
        times.append(t)
        if debug:
            print('    The {}th breakpoint is {} ...'.format(i, t))
    assert horizon <= t, 'Error: the last guess for horizon = {} was found smaller than the true horizon = {}...'.format(
        t, horizon)
    gap = t - horizon
    if debug:
        print(
            'This last guess for horizon = {} gives a gap = {} against the true horizon {}. Relative difference = {:.3%}...'
            .format(t, gap, horizon, gap / float(horizon)))
    return times, gap


@classmethod
def aggregate_data(cls, all_data, new_batch):
    """
        Aggregate a batch of data, bascically just convert tensors to list of native
        python data
        """
    if new_batch is None:
        return
    simple_list = cls._make_simple_list(new_batch)
    all_data.extend(simple_list)


@staticmethod
def _get_parent_dn(dn):
    """
        Gets the dn of the parent object
        Meant to be overridden by inheriting classes.
        Raises exception if not overridden.

        :returns: string containing dn
        """
    if '/inst-' in dn:
        return dn.split('/sys/inst-')[0]
    elif '/ctx-' in dn:
        return dn.split('/sys/ctx-')[0]
    else:
        return None


def copy():
    """Return a copy of this manifest."""


def surface_interaction(phi, x, Psurf):
    """
    density that should be moved to surface
    """
    for ii in range(len(x))[:len(x) - 1]:
        for jj in range(len(x))[:len(x) - 1 - ii]:
            for kk in range(len(x))[:len(x) - 1 - ii - jj]:
                if ii == 0 and jj == 0:
                    continue
                if ii == 0 and kk == 0:
                    continue
                if jj == 0 and kk == 0:
                    continue
                if ii == 0:
                    amnt = Psurf[ii, jj, kk]
                    dist = (len(x) - 1 - (jj + kk)) // 2
                    rmdr = (len(x) - 1 - (jj + kk)) % 2
                    if rmdr == 0:
                        phi[ii, jj + dist, kk + dist] += amnt * phi[ii, jj, kk
                            ] * 2
                        phi[ii, jj, kk] *= 1 - amnt
                    elif rmdr == 1:
                        phi[ii, jj + dist + 1, kk + dist] += amnt * phi[ii,
                            jj, kk] * 2 / 2.0
                        phi[ii, jj + dist, kk + dist + 1] += amnt * phi[ii,
                            jj, kk] * 2 / 2.0
                        phi[ii, jj, kk] *= 1 - amnt
                elif jj == 0:
                    amnt = Psurf[ii, jj, kk]
                    dist = (len(x) - 1 - (ii + kk)) // 2
                    rmdr = (len(x) - 1 - (ii + kk)) % 2
                    if rmdr == 0:
                        phi[ii + dist, jj, kk + dist] += amnt * phi[ii, jj, kk
                            ] * 2
                        phi[ii, jj, kk] *= 1 - amnt
                    elif rmdr == 1:
                        phi[ii + dist + 1, jj, kk + dist] += amnt * phi[ii,
                            jj, kk] * 2 / 2.0
                        phi[ii + dist, jj, kk + dist + 1] += amnt * phi[ii,
                            jj, kk] * 2 / 2.0
                        phi[ii, jj, kk] *= 1 - amnt
                elif kk == 0:
                    amnt = Psurf[ii, jj, kk]
                    dist = (len(x) - 1 - (ii + jj)) // 2
                    rmdr = (len(x) - 1 - (ii + jj)) % 2
                    if rmdr == 0:
                        phi[ii + dist, jj + dist, kk] += amnt * phi[ii, jj, kk
                            ] * 2
                        phi[ii, jj, kk] *= 1 - amnt
                    elif rmdr == 1:
                        phi[ii + dist + 1, jj + dist, kk] += amnt * phi[ii,
                            jj, kk] * 2 / 2.0
                        phi[ii + dist, jj + dist + 1, kk] += amnt * phi[ii,
                            jj, kk] * 2 / 2.0
                        phi[ii, jj, kk] *= 1 - amnt
                else:
                    amnt = Psurf[ii, jj, kk]
                    dist = (len(x) - 1 - (ii + jj + kk)) // 3
                    rmdr = (len(x) - 1 - (ii + jj + kk)) % 3
                    if rmdr == 0:
                        phi[ii + dist, jj + dist, kk + dist] += amnt * phi[
                            ii, jj, kk] * 2
                        phi[ii, jj, kk] *= 1 - amnt
                    elif rmdr == 1:
                        phi[ii + dist + 1, jj + dist, kk + dist] += amnt * phi[
                            ii, jj, kk] * 2 / 3.0
                        phi[ii + dist, jj + dist + 1, kk + dist] += amnt * phi[
                            ii, jj, kk] * 2 / 3.0
                        phi[ii + dist, jj + dist, kk + dist + 1] += amnt * phi[
                            ii, jj, kk] * 2 / 3.0
                        phi[ii, jj, kk] *= 1 - amnt
                    elif rmdr == 2:
                        phi[ii + dist + 1, jj + dist + 1, kk + dist
                            ] += amnt * phi[ii, jj, kk] * 2 / 3.0
                        phi[ii + dist + 1, jj + dist, kk + dist + 1
                            ] += amnt * phi[ii, jj, kk] * 2 / 3.0
                        phi[ii + dist, jj + dist + 1, kk + dist + 1
                            ] += amnt * phi[ii, jj, kk] * 2 / 3.0
                        phi[ii, jj, kk] *= 1 - amnt
    return phi


def get_string(message, default=None):
    """ Get a string input from the user """
    if default:
        result = input('{} ({}): '.format(message, default))
        if not result:
            result = default
    else:
        result = input('{}: '.format(message))
    return result


@staticmethod
def lists_of_settings_to_merge():
    """ Lists of keys of settings at context that will be merged """
    return ['defines', 'inc_dirs', 'target_link_dirs', 'add_lib_deps',
        'PDB_OUTPUT_DIRECTORY', 'PDB_NAME', 'INTERPROCEDURAL_OPTIMIZATION']


def parse_resource_id(base_url, id):
    """ Parse a full or short resource identifier into base url and id

    http://domain.name/app/resources/id/ -> (http://domain.name/app/, id)
    id -> (base_url, id)
    """
    import posixpath as pp
    try:
        from urllib.parse import urlparse
    except ImportError:
        from urlparse import urlparse
    pr = urlparse(id)
    if pr.scheme and pr.netloc:
        base, sid = pp.split(id)
        return pp.dirname(base), id
    else:
        return base_url, id


def validate_collect(context, param, value):
    """Handle the report collection flag."""
    if value is not None:
        return value
    else:
        return True


def initial_reclassifier(original_classifier, initial_seq):
    """
    If needed this extends the memory depth to be the length of the initial
    sequence
    """
    original_classifier['memory_depth'] = max(len(initial_seq),
        original_classifier['memory_depth'])
    return original_classifier


def birch_murnaghan(volumes, volume0, bulk_modulus0, bulk_deriv0):
    """
    This evaluates the Birch Murnaghan equation of states
    """
    PV = []
    EV = []
    v0 = volume0
    bm = bulk_modulus0
    dbm = bulk_deriv0
    for vol in volumes:
        pv_val = 3 * bm / 2.0 * ((v0 / vol) ** (7 / 3.0) - (v0 / vol) ** (5 /
            3.0)) * (1 + 3 / 4.0 * (dbm - 4) * ((v0 / vol) ** (2 / 3.0) - 1))
        PV.append(pv_val)
        ev_val = 9 * bm * v0 / 16.0 * ((dbm * (v0 / vol) ** (2 / 3.0) - 1) **
            3 * ((v0 / vol) ** (2 / 3.0) - 1) ** 2 * (6 - 4 * (v0 / vol) **
            (2 / 3.0)))
        EV.append(ev_val)
    return EV, PV


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

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

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


def assert_vector2(num_vector):
    """
    Assert that a 2 item vector is numeric.

    :param num_vector: Numeric 2 item vector
    :type num_vector: tuple, list
    :return: None
    """
    assert isinstance(num_vector, (tuple, list)
        ), 'object {0} must be a list or tuple of 2 items'.format(num_vector)
    assert len(num_vector) == 2 and isinstance(num_vector[0], (int, float)
        ) and isinstance(num_vector[1], (int, float)
        ), 'each object of {0} must be integer or float'.format(num_vector)


def _parse_pathargs(path_args, args, kwargs, argspec):
    """
    `path_args`, iterable: Which args/kwargs to use, in order. Numbers are
        args, strings are kwargs.
    `args`, `kwargs`: The value of args/kwargs passed to the original builder
        function.
    `argspec`: Stores values of default kwargs from builder function.
    """
    patharg_values = []
    argnames = argspec.args
    defaults = argspec.defaults
    for arg in path_args:
        arg_type = type(arg)
        if arg_type is int:
            patharg_values.append(args[arg])
        elif arg_type is str:
            try:
                patharg_values.append(kwargs[arg])
            except KeyError:
                nargs = len(argnames) - len(defaults)
                kwarg_idx = argnames.index(arg) - nargs
                patharg_values.append(defaults[kwarg_idx])
        else:
            raise ValueError('Path arg must be int or str.')
    return patharg_values


def checking_new_account_name_action_g(user):
    """Destroy the new account, the user asked to go back to name entry."""
    return user.account.destroy()


@staticmethod
def n_parameters(x):
    """
        Static method that computes the dimensionality p of eta given observations.

        Parameters
        ----------
        x: m-length array of observations

        Returns
        -------
        p: scalar corresponding to dimensionality of eta
        """
    raise NotImplementedError


def metadata():
    """ Return metadata method """


def get_phi_ss(imt, mag, params):
    """
    Returns the single station phi (or it's variance) for a given magnitude
    and intensity measure type according to equation 5.14 of Al Atik (2015)
    """
    C = params[imt]
    if mag <= 5.0:
        phi = C['a']
    elif mag > 6.5:
        phi = C['b']
    else:
        phi = C['a'] + (mag - 5.0) * ((C['b'] - C['a']) / 1.5)
    return phi


def keyingset_button_remove():
    """Remove current UI-active property from current keying set

    """
    pass


def nice_value(x):
    """Round ``x`` to nice value."""
    exp = 1.0
    sign = 1
    if x < 0.0:
        x = -x
        sign = -1
    while x >= 1.0:
        x /= 10.0
        exp *= 10.0
    while x < 0.1:
        x *= 10.0
        exp /= 10.0
    if x >= 0.75:
        return sign * 1.0 * exp
    if x >= 0.375:
        return sign * 0.5 * exp
    if x >= 0.225:
        return sign * 0.25 * exp
    if x >= 0.15:
        return sign * 0.2 * exp
    return sign * 0.1 * exp


def pytest_collectstart(collector):
    """ collector starts collecting. """


def str_int(v):
    """Handle argparse inputs to that could be str or int

    :param v: value to convert
    :return: v as int if int(v) does not raise ValueError

    >>> str_int('test')
    test
    >>> str_int(1)
    1
    """
    try:
        int_v = int(v)
        return int_v
    except ValueError:
        return v


def zeros(shape, dtype, ctx):
    """Create a zero tensor.

    Parameters
    ----------
    shape : tuple of int
        The tensor shape.
    dtype : data type
        It should be one of the values in the data type dict.
    ctx : context
        The device of the result tensor.

    Returns
    -------
    Tensor
        The zero tensor.
    """
    pass


def settings_root(likelihood_name, prior_name, ndim, **kwargs):
    """
    Returns a standard string containing information about settings.

    Parameters
    ----------
    likelihood_name: str
    prior_name: str
    ndim: int
    prior_scale: float or int
    dynamic_goal: float, int or None
    nlive_const: int
    nrepeats: int
    nint: int, optional
    init_step: int, optional

    Returns
    -------
    root: str
    """
    prior_scale = kwargs.pop('prior_scale')
    dynamic_goal = kwargs.pop('dynamic_goal')
    nlive_const = kwargs.pop('nlive_const')
    nrepeats = kwargs.pop('nrepeats')
    ninit = kwargs.pop('ninit', None)
    init_step = kwargs.pop('init_step', None)
    if kwargs:
        raise TypeError('Unexpected **kwargs: {0}'.format(kwargs))
    root = '{}_{}_{}_dg{}'.format(likelihood_name, prior_name, prior_scale,
        dynamic_goal)
    if dynamic_goal is not None:
        assert ninit is not None
        root += '_{}init'.format(ninit)
        if dynamic_goal != 0:
            assert init_step is not None
            root += '_{}is'.format(init_step)
    root += '_{}d_{}nlive_{}nrepeats'.format(ndim, nlive_const, nrepeats)
    return root.replace('.', '_')


@classmethod
def update_attributes_map(klass):
    """
        Attributes for object mapping.
        """
    return {'code': '', 'name': '', 'fallback_code': '', 'default': False,
        'optional': True, 'content_delivery_api': True,
        'content_management_api': True}


@staticmethod
def to_int(raw):
    """Create int intensity value out of raw."""
    return raw[0] * 256 + raw[1]


def static_assert(output, expression, message):
    """Writes a C++ compile-time assertion expression to a file.
    :param output: the output stream.
    :param expression: the expression to check.
    :param message: the string literal that will appear if the expression evaluates to
        false.
    """
    print('static_assert(%s, "%s");' % (expression, message), file=output)


def copy_timeseries(in_tb, out_tb, raster_list=None):
    """Copy the timeseries group with all its systems/rasters."""
    if raster_list is None:
        for system_name in in_tb.keys():
            out_system = out_tb.create(system_name)
            for raster_name in in_tb[system_name].keys():
                out_system.copy(raster_name, in_tb[system_name])
    else:
        for r in raster_list:
            system_name, raster_name = r.split('/', 1)
            if system_name in in_tb and raster_name in in_tb[system_name]:
                if system_name not in out_tb:
                    out_tb.create(system_name)
                out_tb[system_name].copy(raster_name, in_tb[system_name])


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 shifted(x):
    """Shift x values to the range [-0.5, 0.5)"""
    return -0.5 + (x + 0.5) % 1


def _file_iter_range(fp, offset, bytes, maxread=1024 * 1024):
    """ Yield chunks from a range in a file. No chunk is bigger than maxread."""
    fp.seek(offset)
    while bytes > 0:
        part = fp.read(min(bytes, maxread))
        if not part:
            break
        bytes -= len(part)
        yield part


def eval_form(q, form, pars):
    """
    Return the SAS model evaluated at *q*.

    *form* is the SAS model returned from :fun:`core.load_model`.

    *pars* are the parameter values to use when evaluating.
    """
    from sasmodels import direct_model
    kernel = form.make_kernel([q])
    theory = direct_model.call_kernel(kernel, pars)
    kernel.release()
    return theory


@classmethod
def get_config_group_name(cls):
    """Override the get_plugin_name in order to use the document type as
        plugin name. This turns OS::Service::Resource to os_service_resource
        """
    config_name = cls.get_document_type().replace('::', '_').lower()
    return 'resource_plugin:%s' % config_name


def is_null_vector(v):
    """
    Calculate the scalar product of two vectors
    """
    return v == (0, 0)


@staticmethod
def _escape_unicode_char_xuU(c, ord=ord):
    """
        Escape a Unicode code point using `\\xHH` (8-bit), `\\uHHHH` (16-bit),
        or `\\UHHHHHHHH` (24-bit) notation.
        """
    n = ord(c)
    if n < 256:
        return '\\x{0:02x}'.format(n)
    elif n < 65536:
        return '\\u{0:04x}'.format(n)
    return '\\U{0:08x}'.format(n)


def key_function(row, index):
    """
    This example shows how to define a function to extract X and Y values
    from custom data.

    :param row:
        The function will be called with the row data, in whatever format it
        was provided to the :class:`.Series`.
    :param index:
        The row index in the series data will also be provided.
    :returns:
        The function must return a chartable value.
    """
    pass


def is_track_header(line):
    """Returns if the line is a header line used in genome tracks/browers."""
    line = line.strip()
    if line.startswith('#') or line.startswith('track') or line.startswith(
        'browser'):
        return True
    else:
        return False


def YN(b):
    """Return 'Yes' for True and 'No' for False, and ?? for anything
    else."""
    if type(b) != bool:
        return '??'
    if b:
        return 'Yes'
    return 'No'


def get_preferred_sparse_format():
    """Get the preferred sparse matrix format supported by the backend.

    Different backends have their preferred backend. This info is useful when
    constructing a sparse matrix.

    Returns
    -------
    string
        the name of the preferred sparse matrix format.
    """
    pass


def drop_category_exeeding_limit(df, var_name, category_limit):
    """ Drops category if its # of unique variables exceed the limit.
        INPUT:
            df:
                A dataframe of independent features and one dependent y feature
            var_name:
                string, the column name of the dependent y variable in the dataframe
        OUTPUT:
            df:
                A dataframe with one less feature if it exeeds the limit.
    """
    if len(df[var_name].unique()) > category_limit:
        df.drop(var_name, axis=1)
    return df


def count_iterator(it):
    """
	Count the length of an iterable. This consumes the iterator, if it doesn't
	have a len()
	"""
    try:
        return len(it)
    except TypeError:
        return sum(1 for _ in it)


def empty_when_none(_string=None):
    """If _string if None, return an empty string, otherwise return string.
    """
    if _string is None:
        return ''
    else:
        return str(_string)


def is_ipv4(s):
    """ Tests if `s` is a IPv4 address string.
		It only validates the common `x.x.x.x` format and rejects less common ones like `x.x`.
	"""
    try:
        nums = tuple(map(int, s.split('.')))
        return len(nums) == 4 and min(nums) >= 0 and max(nums) <= 255
    except (AttributeError, ValueError):
        return False


@classmethod
def from_bytes(cls, raw_message_data: bytes):
    """Alternative constructor"""
    return cls([(None, raw_message_data)])


def get_elb_policies(policies=None):
    """
    @type policies: Policies
    """
    output = ''
    if policies.app_cookie_stickiness_policies:
        for acsp in policies.app_cookie_stickiness_policies:
            output += '{0}\n'.format(str(acsp))
    if policies.lb_cookie_stickiness_policies:
        for lcsp in policies.lb_cookie_stickiness_policies:
            output += '{0}\n'.format(str(lcsp))
    if policies.other_policies:
        for op in policies.lb_cookie_stickiness_policies:
            output += '{0}\n'.format(str(op))
    if output:
        return output.rstrip()


@staticmethod
def _all_packages(pkg_name):
    """
        >>> list(install_lib._all_packages('foo.bar.baz'))
        ['foo.bar.baz', 'foo.bar', 'foo']
        """
    while pkg_name:
        yield pkg_name
        pkg_name, sep, child = pkg_name.rpartition('.')


def decode_response(client_message, to_object=None):
    """ Decode response from client message"""
    parameters = dict(response=None)
    parameters['response'] = client_message.read_int()
    return parameters


def irep(x, n, dim):
    """Replicate x n-times on a new dimension dim-th dimension"""
    x_shape = x.shape
    shp = x_shape[:dim] + (1,) + x_shape[dim:]
    return x.reshape(shp).repeat(n, axis=dim)


def subtract_plus(joined_fields):
    """Split the feature list by the delimiter to run per-element
    checks
    """
    return joined_fields.split('+')


def to_bytes(url):
    """to_bytes(u"URL") --> 'URL'."""
    if isinstance(url, str):
        try:
            url = url.encode('ASCII').decode()
        except UnicodeError:
            raise UnicodeError('URL ' + repr(url) +
                ' contains non-ASCII characters')
    return url


def faces_indices_vectors_to_matrix(indices, faces):
    """faces_indices_vectors_to_matrix(indices, faces) -> List[List[int]]
    PyPRT outputs the GeneratedModel face information as a list of vertex indices
    and a list of face indices count. This function converts these two lists into
    one list of lists containing the vertex indices per face.

    Parameters:
        indices: List[int]
        faces: List[int]

    Returns:
        List[List[int]]

    Example:
        ``[[1, 0, 3, 2], [4, 5, 6, 7], [0, 1, 5, 4], [1, 2, 6, 5], [2, 3, 7, 6], [3, 0, 4, 7]]
        = faces_indices_vectors_to_matrix(([1, 0, 3, 2, 4, 5, 6, 7, 0, 1, 5, 4, 1, 2, 6, 5,
        2, 3, 7, 6, 3, 0, 4, 7],[4, 4, 4, 4, 4, 4]))``
    """
    faces_as_matrix = []
    offset = 0
    for f in faces:
        ind_per_face = indices[offset:offset + f]
        offset += f
        faces_as_matrix.append(ind_per_face)
    return faces_as_matrix


@property
def all(cls):
    """List of `Option` objects"""
    return list(cls.__get_name_options_mapping().values())


def clothing_outputs(inp):
    """ A dummy function the just duplicate the input to the output.

    <unit>
        <output name="listoutp" type="List" content="Str" description="test" />
        <input name="inp" type="Str" description="test" />
    </unit>
    """
    if inp is None:
        return None
    listoutp = [inp + '0', inp + '1']
    return listoutp


def ensure_tuple(length, tuples):
    """Yield `length`-sized tuples from the given collection.

	Will truncate longer tuples to the desired length, and pad using the leading element if shorter.
	"""
    for elem in tuples:
        if not isinstance(elem, (tuple, list)):
            yield (elem,) * length
            continue
        l = len(elem)
        if l == length:
            yield elem
        elif l > length:
            yield tuple(elem[:length])
        elif l < length:
            yield (elem[0],) * (length - l) + tuple(elem)


@classmethod
def get_required_fields(cls) ->set:
    """Return set REQUIRED response fields"""
    res = cls.TOP_LEVEL_NON_ATTRIBUTES_FIELDS.copy()
    res.update(cls.REQUIRED_FIELDS)
    return res


def mean_obliquity_ecliptic(julian_century):
    """Returns the Mean Obliquity of Ecliptic in Degrees with Julian Century,
    julian_century."""
    mean_obliquity_of_ecliptic = 23 + (26 + (21.448 - julian_century * (
        46.815 + julian_century * (0.00059 - julian_century * 0.001813))) / 60
        ) / 60
    return mean_obliquity_of_ecliptic


def smooth_weight():
    """Interpolate weight of selected points

    """
    pass


def make_OpenGL_matrix(rot, pos):
    """Return an OpenGL compatible (column-major, 4x4 homogeneous)
	transformation matrix from ODE compatible (row-major, 3x3) rotation matrix
	rotation and position vector position.

	The returned matrix format is length-9 tuple.

	"""
    return rot[0], rot[3], rot[6], 0.0, rot[1], rot[4], rot[7], 0.0, rot[2
        ], rot[5], rot[8], 0.0, pos[0], pos[1], pos[2], 1.0


def _gen_param_header(name, doc, defaultValueStr, typeConverter):
    """
    Generates the header part for shared variables

    :param name: param name
    :param doc: param doc
    """
    template = """class Has$Name(Params):
    ""\"
    Mixin for param $name: $doc
    ""\"

    $name = Param(Params._dummy(), "$name", "$doc", typeConverter=$typeConverter)

    def __init__(self):
        super(Has$Name, self).__init__()"""
    if defaultValueStr is not None:
        template += """
        self._setDefault($name=$defaultValueStr)"""
    Name = name[0].upper() + name[1:]
    if typeConverter is None:
        typeConverter = str(None)
    return template.replace('$name', name).replace('$Name', Name).replace(
        '$doc', doc).replace('$defaultValueStr', str(defaultValueStr)).replace(
        '$typeConverter', typeConverter)


@staticmethod
def _report_specified_fields(specified_fields, nrows, nsample):
    """Transform the data objects to report objects

        Parameters
        ----------
        specified_fields : dict
            Dictionary with a `~pywhip.reporters.SpecificationErrorHandler`
            object for each field-specification combination.
        nrows : int
            Total rows of the current document working with, used to calculate
            passed rows as well
        nsample : int
            Number of samples (ordered on the number of rows) to retain for
            reporting purposes
        """
    for field, rules in specified_fields.items():
        for rule, error_report in rules.items():
            specified_fields[field][rule] = specified_fields[field][rule
                ].build_error_report(nrows, nsample)
    return specified_fields


@classmethod
def get_language_code(cls, code, language_format=None):
    """Do any possible formatting needed for language code."""
    if not language_format:
        language_format = cls.language_format
    return getattr(cls, 'get_language_{}'.format(language_format))(code)


def reverse_cmap(name):
    """Return the name of the reverse version of given Matplotlib colormap."""
    if name.endswith('_r'):
        return name[:-2]
    else:
        return name + '_r'


def calculate_pagerange(pagenumber, numpages, pagerange):
    """ Calculate the pagerange for the navigation quicklinks """
    pagerange = max(0, pagerange + pagerange % 2 - 1)
    pagerangestart = max(1, round(pagenumber - (pagerange - 1) / 2))
    pagerangeend = min(round(pagenumber + (pagerange - 1) / 2), numpages) + 1
    return int(pagerange), int(pagerangestart), int(pagerangeend)


def lookupAuthority(name, timeout=None):
    """
        Perform an SOA record lookup.

        @type name: L{bytes} or L{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 get_iter_range(h5object):
    """Read back iteration range data written by ``stamp_iter_range``"""
    return int(h5object.attrs['iter_start']), int(h5object.attrs['iter_stop'])


def list_prds(table):
    """
    List all PRDs in a PRD table.

    :param table: Dictionary of device : PRD list pairs.
    :type table: dict(str: list)
    """
    for key in table.keys():
        print('~{0}~'.format(key))
        for prd in table[key]:
            print(prd)


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


def get_delay_share(trains, threshold):
    """Returns share of trains with delay greater or equal `threshold`.

    Args:
        trains (iterable[Connection]): Iterable holding trains.
        threshold (number): Threshold greater or equal a delay must be
          to be considered.
    """
    delayed = 0
    with_data = 0
    for train in trains:
        if train.delay and train.delay >= threshold:
            delayed += 1
        with_data += 1
    return delayed / with_data


def check_clusterings(labels_true, labels_pred):
    """Check that the labels arrays are 1D and of same dimension.
    Parameters
    ----------
    labels_true : array-like of shape (n_samples,)
        The true labels.
    labels_pred : array-like of shape (n_samples,)
        The predicted labels.
    """
    if labels_true.ndim != 1:
        raise ValueError('labels_true must be 1D: shape is %r' % (
            labels_true.shape,))
    if labels_pred.ndim != 1:
        raise ValueError('labels_pred must be 1D: shape is %r' % (
            labels_pred.shape,))
    return labels_true, labels_pred


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

    .. math::
        C_D = \\frac{3808[(1617933/2030) + (178861/1063)Re + (1219/1084)Re^2]}
        {681Re[(77531/422) + (13529/976)Re - (1/71154)Re^2]}

    Parameters
    ----------
    Re : float
        Particle Reynolds number of the sphere using the surrounding fluid
        density and viscosity, [-]

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

    Notes
    -----
    Range is Re <= 118300

    Examples
    --------
    >>> Mikhailov_Freire(200.)
    0.7514111388018659

    References
    ----------
    .. [1] Mikhailov, M. D., and A. P. Silva Freire. "The Drag Coefficient of
       a Sphere: An Approximation Using Shanks Transform." Powder Technology
       237 (March 2013): 432-35. doi:10.1016/j.powtec.2012.12.033.
    .. [2] Barati, Reza, Seyed Ali Akbar Salehi Neyshabouri, and Goodarz
       Ahmadi. "Development of Empirical Models with High Accuracy for
       Estimation of Drag Coefficient of Flow around a Smooth Sphere: An
       Evolutionary Approach." Powder Technology 257 (May 2014): 11-19.
       doi:10.1016/j.powtec.2014.02.045.
    """
    Cd = 3808.0 * (1617933.0 / 2030.0 + 178861.0 / 1063.0 * Re + 1219.0 /
        1084.0 * Re ** 2) / (681.0 * Re * (77531.0 / 422.0 + 13529.0 /
        976.0 * Re - 1.0 / 71154.0 * Re ** 2))
    return Cd


@classmethod
def keys(cls):
    """returns the names of the timers"""
    return list(cls.timer_end.keys())


def datetimeformat(value, format='%Y-%m-%d %H:%M:%S'):
    """Return a formatted datetime object."""
    return value.strftime(format)


@classmethod
def fromkeys(cls, iterable, value=None):
    """OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S
        and values equal to v (which defaults to None).

        """
    d = cls()
    for key in iterable:
        d[key] = value
    return d


@classmethod
def update(cls, resource, name, size, snapshot_profile, background, cmdline
    =None, kernel=None):
    """ Update this disk. """
    if isinstance(size, tuple):
        prefix, size = size
        if prefix == '+':
            disk_info = cls.info(resource)
            current_size = disk_info['size']
            size = current_size + size
    disk_params = cls.disk_param(name, size, snapshot_profile, cmdline, kernel)
    result = cls.call('hosting.disk.update', cls.usable_id(resource),
        disk_params)
    if background:
        return result
    cls.echo('Updating your disk.')
    cls.display_progress(result)


def __call__(name):
    """Returns a nickname surround by link to profile for entered name"""


@classmethod
def buildmap(cls, items):
    """Parse a list of alias (name, replacement) pairs into a dict of
        alias objects"""
    aliases = {}
    for decl, defn in items:
        a = cls.build(decl, defn)
        aliases[a.name] = a
    return aliases


def generate_AES_key(bytes=32):
    """Generates a new AES key

    Parameters
    ----------
    bytes : int
        number of bytes in key

    Returns
    -------
    key : bytes
    """
    try:
        from Crypto import Random
        return Random.get_random_bytes(bytes)
    except ImportError:
        print('PyCrypto not install. Reading from /dev/random instead')
        with open('/dev/random', 'r') as rand:
            return rand.read(bytes)


def restorebackup(tr, backupname):
    """Restore dirstate by backup file"""


def has_grpcio_protoc():
    """ checks if grpcio-tools protoc is installed"""
    try:
        import grpc_tools.protoc
    except ImportError:
        return False
    return True


def _generateSpecial_Volume3DOpts_dithering(overlayList, displayCtx, source,
    longArg):
    """Handle the deprecated ``Volume3DOpts.dithering`` property. """
    return []


@classmethod
def from_bytes(cls, bytes):
    """ Fixed-length recreation.
        """
    plain_int = super().from_bytes(bytes, 'big', signed=False)
    return cls(plain_int)


def get_keywords():
    """Get the keywords needed to look up the version information."""
    git_refnames = '$Format:%d$'
    git_full = '$Format:%H$'
    git_date = '$Format:%ci$'
    keywords = {'refnames': git_refnames, 'full': git_full, 'date': git_date}
    return keywords


def unsubscribe(requestor, service, nodeIdentifier, subscriber):
    """
        Called when a subscribe request has been received.

        @param requestor: The entity the request originated from.
        @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>}
        @param service: The entity the request was addressed to.
        @type service: L{JID<twisted.words.protocols.jabber.jid.JID>}
        @param nodeIdentifier: The identifier of the node to unsubscribe from.
        @type nodeIdentifier: C{unicode}
        @param subscriber: The entity to be unsubscribed.
        @type subscriber: L{JID<twisted.words.protocols.jabber.jid.JID>}
        @return: A deferred that fires with C{None} when unsubscription has
                 succeeded.
        @rtype: L{Deferred<twisted.internet.defer.Deferred>}
        """


def new_merged_dict(*dicts):
    """Merges dictionaries into a new dictionary. Necessary for python2 and
    python34 since neither have PEP448 implemented."""
    output = {}
    for d in dicts:
        output.update(d)
    return output


def get_authorization_header(request):
    """
    Return request's 'Authorization:' header, as a bytestring.
    From: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/authentication.py
    """
    auth = request.META.get('HTTP_AUTHORIZATION', b'')
    if isinstance(auth, type('')):
        auth = auth.encode('iso-8859-1')
    return auth


def vector_multiply(a, b):
    """ multiply two vectors, vectorized """
    a1, a2 = a
    b1, b2 = b
    return a1 * b1 + a2 * b2


def commit():
    """Finalize the transaction.

        This executes the two-phase commit algorithm for all
        `IDataManager` objects associated with the transaction.
        """


def __strip_list(attrs):
    """ This assumes that if you have a list of outputs you just want the second one (the second class).
    """
    if isinstance(attrs, list):
        return attrs[1]
    else:
        return attrs


def main():
    """ currently does nothing """
    return


def deconv_length(dim_size, stride_size, kernel_size, padding,
    output_padding, dilation=1):
    """Determines output length of a transposed convolution given input length.

    # Arguments
        dim_size: Integer, the input length.
        stride_size: Integer, the stride along the dimension of `dim_size`.
        kernel_size: Integer, the kernel size along the dimension of
            `dim_size`.
        padding: One of `"same"`, `"valid"`, `"full"`.
        output_padding: Integer, amount of padding along the output dimension,
            Can be set to `None` in which case the output length is inferred.
        dilation: dilation rate, integer.

    # Returns
        The output length (integer).
    """
    assert padding in {'same', 'valid', 'full'}
    if dim_size is None:
        return None
    kernel_size = (kernel_size - 1) * dilation + 1
    if output_padding is None:
        if padding == 'valid':
            dim_size = dim_size * stride_size + max(kernel_size -
                stride_size, 0)
        elif padding == 'full':
            dim_size = dim_size * stride_size - (stride_size + kernel_size - 2)
        elif padding == 'same':
            dim_size = dim_size * stride_size
    else:
        if padding == 'same':
            pad = kernel_size // 2
        elif padding == 'valid':
            pad = 0
        elif padding == 'full':
            pad = kernel_size - 1
        dim_size = (dim_size - 1
            ) * stride_size + kernel_size - 2 * pad + output_padding
    return dim_size


def next_fn(rank_fn_dict, rank):
    """Get the filepath for the next interval in rank. The last interval links to the first"""
    prev_rank = 1 if rank == len(rank_fn_dict) else rank + 1
    return rank_fn_dict[prev_rank]


def pitch2hz(p, beta=12, ref_frq=440.0):
    """Convert pitch to frequency.

    Parameters
    ----------
    p : np.ndarray, float
        midi pitch
    beta : int
        equal divisions of the octave
    ref_frq : float, optional
        Description

    Returns
    -------
    frq : np.ndarray, float
        frequency
    """
    return 2 ** ((p - 69) / beta) * ref_frq


def vsum(*args):
    """Return the vector sum of its arguments."""
    xsum = 0.0
    ysum = 0.0
    zsum = 0.0
    for vec in args:
        xsum += vec[0]
        ysum += vec[1]
        zsum += vec[2]
    return xsum, ysum, zsum


def comment_out_details(source):
    """
    Given the source of a cell, comment out  any lines that contain <details>
    """
    filtered = []
    for line in source.splitlines():
        if 'details>' in line:
            filtered.append('<!-- UNCOMMENT DETAILS AFTER RENDERING ' +
                line + ' END OF LINE TO UNCOMMENT -->')
        else:
            filtered.append(line)
    return '\n'.join(filtered)


def _safe_int(string):
    """ Simple function to convert strings into ints without dying. """
    try:
        return int(string)
    except ValueError:
        return string


def read(path):
    """
    Get file contents

    Parameters
    ----------
    path : pathlib.Path
        Path of file to read

    Returns
    -------
    str
        File contents
    """
    with path.open('r') as f:
        return f.read()


def escape(value, char, escape='\\'):
    """
    Escapes the provided string value according to the requested
    target character and escape value. Meaning that all the characters
    are going to be replaced by the escape plus character sequence.

    :type value: String
    :param value: The string that is going to have the target characters
    escaped according to the escape character.
    :type char: String
    :param char: The character that is going to be "target" of escaping.
    :type escape: String
    :param escape: The character to be used for escaping (normally`\\`).
    :rtype: String
    :return: The final string with the target character properly escaped.
    """
    return value.replace(escape, escape + escape).replace(char, escape + char)


def make_gce_instances():
    """This makes new GCE worker nodes.

    Use preemptible instances and startup/shutdown scripts to emulate AWS spot
    fleet behavior and run stuff at cheaper prices.

    TODO: finish this

    """


@classmethod
def info(cls, message):
    """
        Prints a message.

        :param message: The message to output.
        :type: str
        """
    print('\n  %s\n' % message)


def futurefpresent(i, n, PV=1):
    """ Present Value to Future Value
    where FV = future value of money, i is the interest rate etc, n is the number of years. """
    return PV * (1 + i) ** n


def check_region(data, x, y, match):
    """Compares a region to the given """
    w = len(match[0])
    for cy in range(len(match)):
        if match[cy] != data[y + cy][x:x + w]:
            return False
    return True


def mongodb_str_filter(base_field, base_field_type):
    """Prepare filters (kwargs{}) for django queryset for mongodb
       where fields contain strings are checked like
       exact | startswith | contains | endswith

    >>> mongodb_str_filter(21, '1')
    '21'
    >>> mongodb_str_filter(21, '2')
    {'$regex': '^21'}
    >>> mongodb_str_filter(21, '3')
    {'$regex': '.*21.*'}
    >>> mongodb_str_filter(21, '4')
    {'$regex': '21$'}
    """
    q = ''
    base_field = str(base_field)
    if base_field != '':
        if base_field_type == '1':
            q = base_field
        if base_field_type == '2':
            q = {'$regex': str('^' + base_field)}
        if base_field_type == '3':
            q = {'$regex': str('.*' + base_field + '.*')}
        if base_field_type == '4':
            q = {'$regex': str(base_field + '$')}
    return q


def clockspeed(speed):
    """
    Returns a formatted version of a ``speed`` given in MHz.
    """
    if speed > 1000:
        return str(0.1 * int(10 * (float(speed) / 1000))) + ' GHz'
    else:
        return str(speed) + ' MHz'


@staticmethod
def getCreateTable():
    """Create the table to hold the subnet information"""
    return (
        'create table ipv4sn (cidr char(20), minip integer(11), maxip integer(11) )'
        )


def ks_table(df, score_interval):
    """ get the ks table
    """


@staticmethod
def _configure_status_register(mask_register):
    """Translates from a named array to an integer representation value"""
    if isinstance(mask_register, int):
        return mask_register
    integer_representation = 0
    for count, bit_name in enumerate(mask_register.bit_names):
        if bit_name:
            integer_representation += int(getattr(mask_register, bit_name)
                ) << count
    return integer_representation


def check_sw_status_admin(supported_nodes, output):
    """Check if a node is in OPERATIONAL status"""
    lines = output.splitlines()
    for line in lines:
        line = line.strip()
        if len(line) > 0 and line[0].isdigit():
            node = line[0:10].strip()
            if node in supported_nodes:
                sw_status = line[48:62].strip()
                if 'OPERATIONAL' not in sw_status:
                    return False
    return True


def get_calcjob_report(calcjob):
    """
    Return a multi line string representation of the log messages and output of a given calcjob

    :param calcjob: the calcjob node
    :return: a string representation of the log messages and scheduler output
    """
    from aiida import orm
    from aiida.common.datastructures import CalcJobState
    log_messages = orm.Log.objects.get_logs_for(calcjob)
    scheduler_out = calcjob.get_scheduler_stdout()
    scheduler_err = calcjob.get_scheduler_stderr()
    calcjob_state = calcjob.get_state()
    scheduler_state = calcjob.get_scheduler_state()
    report = []
    if calcjob_state == CalcJobState.WITHSCHEDULER:
        state_string = '{}, scheduler state: {}'.format(calcjob_state,
            scheduler_state if scheduler_state else '(unknown)')
    else:
        state_string = '{}'.format(calcjob_state)
    label_string = ' [{}]'.format(calcjob.label) if calcjob.label else ''
    report.append('*** {}{}: {}'.format(calcjob.pk, label_string, state_string)
        )
    if scheduler_out is None:
        report.append('*** Scheduler output: N/A')
    elif scheduler_out:
        report.append('*** Scheduler output:\n{}'.format(scheduler_out))
    else:
        report.append('*** (empty scheduler output file)')
    if scheduler_err is None:
        report.append('*** Scheduler errors: N/A')
    elif scheduler_err:
        report.append('*** Scheduler errors:\n{}'.format(scheduler_err))
    else:
        report.append('*** (empty scheduler errors file)')
    if log_messages:
        report.append('*** {} LOG MESSAGES:'.format(len(log_messages)))
    else:
        report.append('*** 0 LOG MESSAGES')
    for log in log_messages:
        report.append('+-> {} at {}'.format(log.levelname, log.time))
        for message in log.message.splitlines():
            report.append(' | {}'.format(message))
    return '\n'.join(report)


def linalg_det(A=None, out=None, name=None, **kwargs):
    """Compute the determinant of a matrix.
    Input is a tensor *A* of dimension *n >= 2*.

    If *n=2*, *A* is a square matrix. We compute:

      *out* = *det(A)*

    If *n>2*, *det* is performed separately on the trailing two dimensions
    for all inputs (batch mode).

    .. note:: The operator supports float32 and float64 data types only.
    .. note:: There is no gradient backwarded when A is non-invertible (which is
              equivalent to det(A) = 0) because zero is rarely hit upon in float
              point computation and the Jacobi's formula on determinant gradient
              is not computationally efficient when A is non-invertible.

    Examples::

       Single matrix determinant
       A = [[1., 4.], [2., 3.]]
       det(A) = [-5.]

       Batch matrix determinant
       A = [[[1., 4.], [2., 3.]],
            [[2., 3.], [1., 4.]]]
       det(A) = [-5., 5.]

    Defined in src/operator/tensor/la_op.cc:L973

    Parameters
    ----------
    A : NDArray
        Tensor of square matrix

    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 broken_links_by_email(context, data_dict):
    """Only sysadmins can see the broken_links_by_email report."""
    return dict(success=False)


def easy_encode(value, enc_type='utf-8', errors='strict'):
    """ Try to .encode(enc_type) on value.
    """
    try:
        return value.encode(enc_type, errors)
    except AttributeError:
        return value


@staticmethod
def t_TIMEDELTA(tok):
    """([0-9]+[D|d])?[0-9]{2}:[0-9]{2}:[0-9]{2}"""
    tok.value = tok.type, tok.value
    return tok


def _nmono_linesearch(obj, grad, x, d, obj_hist, M=10, sig1=0.1, sig2=0.9,
    gam=0.0001, maxiter=100):
    """
    Implements the non-monotone line search of Grippo et al. (1986),
    as described in Birgin, Martinez and Raydan (2013).

    Parameters
    ----------
    obj : real-valued function
        The objective function, to be minimized
    grad : vector-valued function
        The gradient of the objective function
    x : array_like
        The starting point for the line search
    d : array_like
        The search direction
    obj_hist : array_like
        Objective function history (must contain at least one value)
    M : positive int
        Number of previous function points to consider (see references
        for details).
    sig1 : real
        Tuning parameter, see references for details.
    sig2 : real
        Tuning parameter, see references for details.
    gam : real
        Tuning parameter, see references for details.
    maxiter : int
        The maximum number of iterations; returns Nones if convergence
        does not occur by this point

    Returns
    -------
    alpha : real
        The step value
    x : Array_like
        The function argument at the final step
    obval : Real
        The function value at the final step
    g : Array_like
        The gradient at the final step

    Notes
    -----
    The basic idea is to take a big step in the direction of the
    gradient, even if the function value is not decreased (but there
    is a maximum allowed increase in terms of the recent history of
    the iterates).

    References
    ----------
    Grippo L, Lampariello F, Lucidi S (1986). A Nonmonotone Line
    Search Technique for Newton's Method. SIAM Journal on Numerical
    Analysis, 23, 707-716.

    E. Birgin, J.M. Martinez, and M. Raydan. Spectral projected
    gradient methods: Review and perspectives. Journal of Statistical
    Software (preprint).
    """
    alpha = 1.0
    last_obval = obj(x)
    obj_max = max(obj_hist[-M:])
    for iter in range(maxiter):
        obval = obj(x + alpha * d)
        g = grad(x)
        gtd = (g * d).sum()
        if obval <= obj_max + gam * alpha * gtd:
            return alpha, x + alpha * d, obval, g
        a1 = -0.5 * alpha ** 2 * gtd / (obval - last_obval - alpha * gtd)
        if sig1 <= a1 and a1 <= sig2 * alpha:
            alpha = a1
        else:
            alpha /= 2.0
        last_obval = obval
    return None, None, None, None


@staticmethod
def _run_cli_process(cmd, shell=True):
    """! Runs command as a process and return stdout, stderr and ret code
        @param cmd Command to execute
        @return Tuple of (stdout, stderr, returncode)
        """
    from subprocess import Popen, PIPE
    p = Popen(cmd, shell=shell, stdout=PIPE, stderr=PIPE)
    _stdout, _stderr = p.communicate()
    return _stdout, _stderr, p.returncode


def assign_value(Xj, Xk, csp, assignment):
    """Assign a value to Xk given Xj's (Xk's parent) assignment.
    Return the first value that satisfies the constraints."""
    parent_assignment = assignment[Xj]
    for val in csp.curr_domains[Xk]:
        if csp.constraints(Xj, parent_assignment, Xk, val):
            return val
    return None


def team_converter(team_id):
    """Converts a team's ID to their actual name."""
    team_map = {(1): 'Arsenal', (2): 'Aston Villa', (3): 'Bournemouth', (4):
        'Brighton', (5): 'Burnley', (6): 'Chelsea', (7): 'Crystal Palace',
        (8): 'Everton', (9): 'Leicester', (10): 'Liverpool', (11):
        'Man City', (12): 'Man Utd', (13): 'Newcastle', (14): 'Norwich', (
        15): 'Sheffield Utd', (16): 'Southampton', (17): 'Spurs', (18):
        'Watford', (19): 'West Ham', (20): 'Wolves', None: None}
    return team_map[team_id]


def unauthenticated(func):
    """Adds 'unauthenticated' attribute to decorated function.

    Usage:

    >>> @unauthenticated
    ... def mymethod(f):
    ...     pass
    """
    func.unauthenticated = True
    return func


@classmethod
def _constructor(cls, mapping, configuration):
    """
        A standardized constructor.
        """
    sequence_type, default_factory, pass_key = configuration
    return cls(default_factory, mapping, sequence_type=sequence_type,
        pass_key=pass_key)


def extract_class_identifier_list(oLine):
    """
    Returns a class identifiers list.

    Parameters:

       oLine: (line object)

    Returns: (list of strings)
    """
    sLine = oLine.line.replace(';', '')
    sLine = sLine.split(':')[0]
    sLine = sLine.replace(',', ' ').split()
    if sLine[0].lower() in ['constant', 'variable', 'signal', 'file']:
        sLine = sLine[1:]
    return sLine


def to_bytes(seq):
    """convert a sequence to a bytes type"""
    if isinstance(seq, bytes):
        return seq
    elif isinstance(seq, bytearray):
        return bytes(seq)
    elif isinstance(seq, memoryview):
        return seq.tobytes()
    else:
        b = bytearray()
        for item in seq:
            b.append(item)
        return bytes(b)


def reassemble(data):
    """
        Reverse the transformation done by calling L{received}, so as to
        provide any buffered output as input to the drain where this
        L{IDivertable} is being diverted.

        @param data: The objects which had been returned from L{ITube.received}
            which had been buffered.
        @type data: L{list}

        @return: A list of objects such that, if L{received} was called
            successively with each object in the returned list, C{tube.deliver}
            would be called successively with each object in C{data}.  This
            does not need to be exactly equal to what was originally passed in
            as long as it achieves the same effect.  Any objects which were
            buffered by the L{IDivertable} which did not yet correspond to a
            C{tube.deliver} call should be included as well.
        @rtype: L{list}
        """


@classmethod
def match(cls, name: str) ->bool:
    """
        Check whether this is a valid name for a particular type of tag

        :param name: The name to check
        :return: Whether the name is valid
        """
    return bool(cls.pattern().match(name))


@classmethod
def ce_build(cls, srv, *options, **kwargs):
    """
        Build containers that need to be build locally, and upload them so that they can be started later on.

        USERNAME and PASSWORD for the push have to be provided by the environment or the commandline.
        """
    srvd = cls._ocean._get(srv)
    assert srvd['type'] == 'environment'
    cls._ocean.build(srv, '-R', *options, **kwargs)


def layer_next():
    """Switch to editing action in animation layer above the current action in the NLA Stack

    """
    pass


def remove_dups_wothout_set(head):
    """
    Time Complexity: O(N^2)
    Space Complexity: O(1)
    """
    current = head
    while current:
        runner = current
        while runner.next:
            if runner.next.val == current.val:
                runner.next = runner.next.next
            else:
                runner = runner.next
        current = current.next


def bC_Ca_OH_HMW84(T, P):
    """c-a: calcium hydroxide [HMW84]."""
    b0 = -0.1747
    b1 = -0.2303
    b2 = -5.72
    C0 = 0
    C1 = 0
    alph1 = 2
    alph2 = 12
    omega = -9
    valid = T == 298.15
    return b0, b1, b2, C0, C1, alph1, alph2, omega, valid


def selectable(method):
    """This is called to find out if the method should
        be shown in the slave list of payment methods

        :returns: True if it should be shown
        """


def get_client():
    """ get a simple client for bigquery"""
    from google.cloud import bigquery
    return bigquery.Client()


@classmethod
def normalized_extension(cls, extension):
    """Returns extension in normalized form, i.e. without leading dot and all lower capitals."""
    if not extension:
        return None
    if extension.startswith('.'):
        return extension[1:].lower()
    else:
        return extension.lower()


def ichangekeys(map, keymap, replace=False):
    """Like :func:`changekeys`, but make changes *inplace*."""
    return NotImplemented


@staticmethod
def register_service_into_servicegroups(service, servicegroups):
    """
        Registers a service into the service groups declared in its
        `servicegroups` attribute.

        :param service: The service to register
        :type service:
        :param servicegroups: The servicegroups container
        :type servicegroups:
        :return: None
        """
    if hasattr(service, 'service_description'):
        sname = service.service_description
        shname = getattr(service, 'host_name', '')
        if hasattr(service, 'servicegroups'):
            if isinstance(service.servicegroups, list):
                sgs = service.servicegroups
            else:
                sgs = service.servicegroups.split(',')
            for servicegroup in sgs:
                servicegroups.add_member([shname, sname], servicegroup.strip())


def pad_oid(oid, size):
    """If oid is short, pad right with 0s."""
    result = list(oid)
    while len(result) < size:
        result.append(0)
    return result


def frame_columns():
    """Column names w.r.t frame dataframe."""
    return ('army', 'allegiance', 'alive', 'armor', 'hp', 'x', 'y', 'dir_x',
        'dir_y')


def parse_pubsub_numsub(command, res, **options):
    """
    Result callback, handles different return types
    switchable by the `aggregate` flag.
    """
    aggregate = options.get('aggregate', True)
    if not aggregate:
        return res
    numsub_d = dict()
    for _, numsub_tups in res.items():
        for channel, numsubbed in numsub_tups:
            try:
                numsub_d[channel] += numsubbed
            except KeyError:
                numsub_d[channel] = numsubbed
    ret_numsub = []
    for channel, numsub in numsub_d.items():
        ret_numsub.append((channel, numsub))
    return ret_numsub


def savetable(ofile, bands, cols, data):
    """
    Save tabular data.

    Parameters
    ----------
    ofile : str
        Output file name.
    bands : list
        List of bands.
    cols : list
        List of column headings.
    data : list
        List of arrays containing statistics.

    Returns
    -------
    None.

    """
    fobj = open(ofile, 'a')
    htmp = cols[0]
    for i in cols[1:]:
        htmp += ',' + i
    for k, _ in enumerate(bands):
        fobj.write(bands[k] + '\n')
        fobj.write(htmp + '\n')
        for i, _ in enumerate(data[k]):
            rtmp = str(data[k][i][0])
            for j in range(1, len(data[k][0])):
                rtmp += ',' + str(data[k][i][j])
            fobj.write(rtmp + '\n')
        fobj.write('\n')
    fobj.close()


def writeCalibFile():
    """
    # TODO:   put here DetectorCalibration.OnSaveCalib()

    """
    pass


def interaction_preset_add(name: str='', remove_active: bool=False):
    """Add or remove an Application Interaction Preset

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


def p_args_multiple(p):
    """args : expression COMMA args"""
    p[0] = [p[1]] + p[3]


def euclidean_distance(lat1, lon1, lat2, lon2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)
    """
    from math import radians, cos, sin, asin, sqrt
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    c = 2 * asin(sqrt(a))
    km = 6367 * c
    return km


@classmethod
def get_pl_clk(mcs, clk_idx):
    """This method will return the clock frequency.

        This method is not exposed to users.

        Parameters
        ----------
        clk_idx : int
            The index of the PL clock to be changed, from 0 to 3.

        """
    if clk_idx not in range(4):
        raise ValueError('Valid PL clock index is 0 - 3.')
    pl_clk_reg = mcs.PL_CLK_CTRLS[clk_idx]
    src_clk_idx = pl_clk_reg[mcs.PL_CLK_SRC_FIELD]
    src_clk_mhz = mcs._get_src_clk_mhz(src_clk_idx)
    pl_clk_odiv0 = pl_clk_reg[mcs.PL_CLK_ODIV0_FIELD]
    pl_clk_odiv1 = pl_clk_reg[mcs.PL_CLK_ODIV1_FIELD]
    return round(src_clk_mhz / (pl_clk_odiv0 * pl_clk_odiv1), 6)


def _include_file_data(login, record):
    """Ensure that 'files' field is present in the input record"""
    if 'files' not in record:
        if 'files' in record.get('links', {}):
            url = record['links']['files']
        elif 'id' in record:
            url = login.base_url + 'api/deposit/depositions/{0}/files'.format(
                record['id'])
        else:
            return record
        r = login.session.get(url)
        if r.status_code == 200:
            r_json = r.json()
            record['files'] = r_json
    return record


def zones2list(zones, type='power'):
    """Convert zones Strava response into a list

    Parameters
    ----------
    zones : dict
        Strava API zones response
    type : {"power", "heart_rate"}

    Returns
    -------
    y : list
        Zones boundaries with left edge set to -1 and right to 10000
    """
    y = list(map(lambda x: x['min'], zones[type]['zones']))
    y[0] = -1
    y.append(10000)
    return y


def rotate_inertia(rotation_matrix, inertia):
    """Returns an inertia tensor expressed in a reference frame which has
    been rotated with respect to the frame the inertia tensor is currently
    expressed in.

    Parameters
    ----------
    rotation_matrix : numpy.matrix, shape(3,3)
        Three-dimensional rotation/transformation/direction-cosine matrix
        that transforms a vector in the rotated reference frame into one in
        the current reference frame.
    inertia : numpy.matrix, shape(3,3)
        Three-dimensional cartesian tensor describing the inertia of a rigid
        body in a reference frame.

    Returns
    -------
    rotated_inertia : numpy.matrix, shape(3,3)
        The inertia tensor expressed in the rotated reference frame.

    Notes
    -----

    The provided inertia tensor is expressed in a reference frame, A, and
    there is a reference frame, B, which is rotated with respect to A such
    that a vector, v, expressed in B as v_b can be expressed in A as v_a by
    pre-multiplying by the rotation matrix, R:

    v_a = R * v_b

    Angular momentum of a rigid body expressed in A is defined as:

    H_a = I_a * w_a

    where H_a and w_a are the angular momentum and angular rate vectors,
    respectively and I_a is the inertia tensor, all expressed in A.
    Expressing H_a and w_a in B gives:

    R * H_b = I_a * R * w_b

    R * I_b * w_b = I_a * R * w_b

    So,

    R * I_b = I_a * R

    and thus:

    I_b = R^T * I_a * R

    """
    return rotation_matrix.T * inertia * rotation_matrix


def _mergeeffects(text, start, stop):
    """Insert start sequence at every occurrence of stop sequence

    >>> s = _mergeeffects(b'cyan', b'[C]', b'|')
    >>> s = _mergeeffects(s + b'yellow', b'[Y]', b'|')
    >>> s = _mergeeffects(b'ma' + s + b'genta', b'[M]', b'|')
    >>> s = _mergeeffects(b'red' + s, b'[R]', b'|')
    >>> s
    '[R]red[M]ma[Y][C]cyan|[R][M][Y]yellow|[R][M]genta|'
    """
    parts = []
    for t in text.split(stop):
        if not t:
            continue
        parts.extend([start, t, stop])
    return b''.join(parts)


def _sensorupsampletocellsbyInterpolation(df, cellsy):
    """

    Function for when sensorsy in the results are less than cellsy desired.
    Interpolates the dataframe.

    #2DO: improve interpolation with pandas. right onw it's row by row.

    _sensorupsampletocellsbyInterpolation(df, cellsy)
    """
    import pandas as pd
    import numpy as np
    sensorsy = len(df)
    cellCenterPVM = []
    for i in range(0, cellsy):
        cellCenterPVM.append((i * sensorsy / cellsy + (i + 1) * sensorsy /
            cellsy) / 2)
    df2 = pd.DataFrame()
    for j in range(0, len(df.keys())):
        A = list(df[df.keys()[j]])
        B = np.interp(cellCenterPVM, list(range(0, sensorsy)), A)
        df2[df.keys()[j]] = B
    return df2


@classmethod
def _get_creatable_subclass_by_tagnames(cls):
    """Returns the possible sub classes addable to this class
        """
    dic = {}
    for c in cls.children_classes:
        dic.update(c._get_creatable_class_by_tagnames())
    return dic


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

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

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


def normalize_var(var):
    """Sass defines `foo_bar` and `foo-bar` as being identical, both in
    variable names and functions/mixins.  This normalizes everything to use
    dashes.
    """
    return var.replace('_', '-')


@staticmethod
def chunks(file_names, threads):
    """
        Given a list of file names a number of threads, this method returns a list containing file names.
        The len(chunks) == threads so we can use each item in the list for a single process.
        """
    chunks = []
    for i in range(0, len(file_names), threads):
        chunks.append(file_names[i:i + threads])
    return chunks


def path_list_is_sibling(sib1, sib2, **kwargs):
    """
        from xdict.utils import *
        path_list_is_sibling(['a','b','c'],['a','b','d'])
        path_list_is_sibling(['a','b','c'],['a','b','e'])
        path_list_is_sibling(['a','b','c'],['a','b','d',''])
        path_list_is_sibling(['a','b','c'],['a','e','d'])

    """
    s1s = sib1
    s2s = sib2
    if s1s.__len__() != s2s.__len__():
        return 0
    else:
        s1p = s1s[:-1]
        s2p = s2s[:-1]
        if s1p == s2p:
            return 1
        else:
            return 0


def name_internal_nodes(tree):
    """ Name internal nodes that does not have name

     Parameters
     ----------
     tree : skbio.TreeNode or empress.Tree
         Input tree with labeled tips and partially unlabeled internal nodes or branch lengths.

    Returns
    -------
    skbio.TreeNode or empress.Tree
         Tree with fully labeled internal nodes and branches.
    """
    for i, n in enumerate(tree.postorder(include_self=True)):
        if n.length is None:
            n.length = 1
        if n.name is None:
            new_name = 'y%d' % i
            n.name = new_name


def parse_vault_id(vault_id):
    """
    parse the returned vaults
    vaults.list() returns {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.KeyVault/vaults/{vaultName}'}
    """
    _, _, subscription_id, _, resource_group, *unimportant, vault_name = (
        vault_id.split('/'))
    return subscription_id, resource_group, vault_name


@classmethod
def _from_dict(cls, _dict):
    """Initialize a RelationsOptions object from a json dictionary."""
    args = {}
    if 'model' in _dict:
        args['model'] = _dict.get('model')
    return cls(**args)


@classmethod
def _convert_to_number_format(cls, number_format_dict):
    """
        Convert ``number_format_dict`` to an openpyxl v2.1.0 number format
        initializer.
        Parameters
        ----------
        number_format_dict : dict
            A dict with zero or more of the following keys.
                'format_code' : str
        Returns
        -------
        number_format : str
        """
    return number_format_dict['format_code']


def _unpack_image(pixels):
    """Flatten out pixels and returns a tuple. The first entry is the size of each pixel."""
    unpacked_pixels = []
    try:
        for pix in pixels:
            for val in pix:
                unpacked_pixels.append(val)
        return len(pixels[0]), bytes(unpacked_pixels)
    except TypeError:
        return 1, bytes(pixels)


def coalesce_adjacent_intervals(intervals):
    """Combine a set of datetime 2-tuples, coalescing adjacent intervals into
       larger intervals wherever possible.
    """
    if len(intervals) == 0:
        return intervals
    coalesced_intervals = [intervals[0]]
    for interval in intervals[1:]:
        if coalesced_intervals[-1][1] == interval[0]:
            coalesced_intervals[-1] = coalesced_intervals[-1][0], interval[1]
        else:
            coalesced_intervals.append(interval)
    return coalesced_intervals


def unify_length():
    """Make selected hair the same length

    """
    pass


def pattern2oerp(pattern):
    """Return a SQL expression corresponding to `pattern` (simpler
    representation of the SQL string matching).
    """
    return pattern.replace('*', '%')


def get_text_alignment(pos):
    """Return 'verticalalignment'('va') and 'horizontalalignment'('ha') for given 'pos' (position)"""
    pos = pos.lower()
    va, ha = None, None
    if pos == 'nw':
        va, ha = 'top', 'left'
    elif pos == 'ne':
        va, ha = 'top', 'right'
    elif pos == 'sw':
        va, ha = 'bottom', 'left'
    elif pos == 'se':
        va, ha = 'bottom', 'right'
    else:
        raise ValueError("Unknown value for 'pos': %s" % str(pos))
    return {'va': va, 'ha': ha}


@staticmethod
def _build_variant_label(build, chromosome, position, reference_allele,
    variant_allele, gene_symbols=None):
    """
        Function to build HGVS variant labels
        :param build: {str} build id
        :param chromosome: {str} chromosome
        :param position: {str} variation position as string or int
        :param reference_allele: {str} single letter ref bp
        :param variant_allele: {str} single letter bp change
        :param gene_symbol: {str} gene symbol (hgvs)
        :return: {str} variant label
        """
    variant_label = ''
    prefix = ''
    if gene_symbols and len(gene_symbols) == 1 and gene_symbols[0]:
        prefix = '{0}{1}({2})'.format(build, chromosome, gene_symbols[0])
    else:
        prefix = '{0}{1}'.format(build, chromosome)
    if reference_allele == '-':
        variant_label = '{0}:g.{1}ins{2}'.format(prefix, position,
            variant_allele)
    elif variant_allele == '-':
        variant_label = '{0}:g.{1}del{2}'.format(prefix, position,
            reference_allele)
    else:
        variant_label = '{0}:g.{1}{2}>{3}'.format(prefix, position,
            reference_allele, variant_allele)
    return variant_label


def open_series(filenames=None, first_filename=None, single_frame=None,
    fixed_frames=None, fixed_frame_number=None):
    """
    Create an object to iterate frames through a file series.

    This function is a wrapper over :class:`~file_series.FileSeries` to facilitate
    simple uses of file series iterations.

    :param Union[Generator,Iterator,List] filenames: Ordered list of filenames
        to process as a file series. It also can be a generator, and
        iterator, or :class:`~fabio.file_series.filename_series` or
        :class:`~fabio.file_series.file_series` objects.
    :param str first_filename: If provided iterate filenames from this filename
        and try to consecutivelly open next files. If this argument is specified
        the `filenames` have to unspecified. Internally it uses
        :class:`~fabio.file_series.filename_series` to iterate the filenames.
    :param Union[Bool,None] single_frame: If True, all files are supposed to
        contain only one frame.
    :param Union[Bool,None] fixed_frames: If True, all files are supposed to
        contain the same amount of frames (this fixed amount will be reached
        from the first file of the serie).
    :param Union[Integer,None] fixed_frame_number: If set, all files are
        supposed to contain the same amount of frames (sepecified by this
        argument)
    :rtype: :class:`~file_series.FileSeries`
    """
    from . import file_series
    if filenames is not None and first_filename is not None:
        raise ValueError(
            "'filenames' and 'first_filename' are mutual exclusive")
    if first_filename is not None:
        filenames = file_series.filename_series(filename=first_filename)
    return file_series.FileSeries(filenames=filenames, single_frame=
        single_frame, fixed_frames=fixed_frames, fixed_frame_number=
        fixed_frame_number)


def get_topic(domain_class: type) ->str:
    """
    Returns a string describing a class.

    :param domain_class: A class.
    :returns: A string describing the class.
    """
    return domain_class.__module__ + '#' + getattr(domain_class,
        '__qualname__', domain_class.__name__)


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

        :param TeamSpaceAllocation val:
        :rtype: SpaceAllocation
        """
    return cls('team', val)


def get_box_dimensions(width, height, original_width, original_height, center):
    """Returns proper box coordinate for cropping in Pillow."""
    x_offset = center[0]
    y_offset = center[1]
    width_offset = int(width / 2)
    height_offset = int(height / 2)
    left = x_offset - width_offset
    upper = y_offset - height_offset
    right = x_offset + width_offset
    lower = y_offset + height_offset
    return left, upper, right, lower


def check_regimes(reg_set, N=None, K=None):
    """Check if there are at least two regimes

    Parameters
    ----------
    reg_set     : list
                  List of the regimes IDs

    Returns
    -------
    Returns : nothing
              Nothing is returned

    """
    if len(reg_set) < 2:
        raise Exception(
            'At least 2 regimes are needed to run regimes methods. Please check your regimes variable.'
            )
    if 1.0 * N / len(reg_set) < K + 1:
        raise Exception(
            "There aren't enough observations for the given number of regimes and variables. Please check your regimes variable."
            )


def _contains_span(span0, span1):
    """Return true if span0 contains span1, False otherwise."""
    if span0 == span1 or span0[0] > span1[0] or span0[1] < span1[1]:
        return False
    return True


def use_original_date(original_date, release_date):
    """Return True if the original date should be used in tags."""
    if original_date is None:
        return False
    if release_date is None:
        return True
    original_year = original_date[:4]
    release_year = release_date[:4]
    if original_year == release_year:
        return False
    return True


def load_handlers(name):
    """Load the (URL pattern, handler) tuples for each component."""
    mod = __import__(name, fromlist=['default_handlers'])
    return mod.default_handlers


def get_all_contained_items(item, stoptest=None):
    """
    Recursively retrieve all items contained in another item

    :param text_game_maker.game_objects.items.Item item: item to retrieve items        from
    :param stoptest: callback to call on each sub-item to test whether        recursion should continue. If stoptest() == True, recursion will        continue
    :return: list of retrieved items
    :rtype: [text_game_maker.game_objects.items.Item]
    """
    ret = []
    if not item.is_container:
        return ret
    stack = [item]
    while stack:
        subitem = stack.pop(0)
        for i in subitem.items:
            ret.append(i)
            if i.is_container:
                if stoptest and stoptest(i):
                    continue
                stack.append(i)
    return ret


def sumoftwo(a, b):
    """This is an sumoftwo function that can easily used by it
       sumoftwo(2, 3)
       will return 5
    """
    return a + b


@classmethod
def _ensure_dot(cls, text):
    """
        This function makes sure a string contains a dot at the end
        """
    if text.endswith('.'):
        return text
    return text + '.'


@classmethod
def read_stack(cls, name, default=None, silent=True):
    """Read the current value of the thread."""
    stack = cls.get_stack(name)
    if silent and len(stack) == 0:
        return default
    return stack[-1]


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

    The ".dev0" means dirty.

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


def delta(vectors):
    """ Computes the finite difference on an arralike.

    Parameters
    ----------
    vectors : ndarray
        An array of vectors or scalars.

    Returns
    -------
    ndarray
        An array of vectors or scalars of shape (n-1,...),
        when vectors input have shape (n,...).

    Examples
    --------
    >>> from pyfac.utils import *  # doctest: +SKIP
    >>> delta(np.arange(5))
    array([1, 1, 1, 1])
    >>> delta(np.array([4, 3, 7, 4, 2, 92]))
    array([-1,  4, -3, -2, 90])
    >>> delta(np.array([[1, 0, 0], [0, 1, 3], [7, -13, 4]]))
    array([[ -1,   1,   3],
           [  7, -14,   1]])
    """
    return vectors[1:] - vectors[:-1]


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


@classmethod
def from_token(cls, token):
    """Creates a CellId from a hex encoded cell id string, called a token.

        :param str token:
            A hex representation of the cell id. If the input is shorter than
            16 characters, zeros are appended on the right.
        """
    return cls(int(token.ljust(16, '0'), 16))


def _ensure_cr(text):
    """Remove trailing whitespace and add carriage return.

    Ensures that `text` always ends with a carriage return
    """
    return text.rstrip() + '\n'


@staticmethod
def _image_tag(image):
    """Strips image tag from image URI"""
    tag = ''
    if ':' in image:
        tag = image.split(':')[-1:][0]
    return tag


@classmethod
def decode(cls, packet):
    """Decode bytes in an SSH packet as SFTP statvfs attributes"""
    vfsattrs = cls()
    vfsattrs.bsize = packet.get_uint64()
    vfsattrs.frsize = packet.get_uint64()
    vfsattrs.blocks = packet.get_uint64()
    vfsattrs.bfree = packet.get_uint64()
    vfsattrs.bavail = packet.get_uint64()
    vfsattrs.files = packet.get_uint64()
    vfsattrs.ffree = packet.get_uint64()
    vfsattrs.favail = packet.get_uint64()
    vfsattrs.fsid = packet.get_uint64()
    vfsattrs.flags = packet.get_uint64()
    vfsattrs.namemax = packet.get_uint64()
    return vfsattrs


def disable_defrag(session, return_type=None, **kwargs):
    """
    Disables NAS share (XFS) defragging on the VPSA.

    :type session: zadarapy.session.Session
    :param session: A valid zadarapy.session.Session object.  Required.

    :type return_type: str
    :param return_type: If this is set to the string 'json', this function
        will return a JSON string.  Otherwise, it will return a Python
        dictionary.  Optional (will return a Python dictionary by default).

    :rtype: dict, str
    :returns: A dictionary or JSON data set as a string depending on
        return_type parameter.
    """
    path = '/api/settings/defrag_disable.json'
    return session.post_api(path=path, return_type=return_type, **kwargs)


def entrada(tipo, variavel, valor='', classe='', Identificador='', nome='',
    atrPersonalizado=''):
    """ Cria um campo de entrada personalizado para ser usado nos formularios """
    return (
        "
%s<input type='%s' name='%s' value='%s' class='form-control %s' id='%s' %s />
"
         % (str(nome), str(tipo), str(variavel), str(valor), str(classe),
        str(Identificador), str(atrPersonalizado)))


def _timeout_handler(signum, frame):
    """ This function is called when ssh takes too long to connect. """
    raise IOError('SSH connect timeout')


def parse_arguments(stream, end_token):
    """
    Helper function for function argument parsing.  Pass it a
    `TokenStream` and the delimiter token for the argument section and
    it will extract all position and keyword arguments as well as
    each argument's type info (string literal or not).

    Returns a ``(args, kwargs)`` tuple.
    """
    args = []
    kwargs = {}
    keywords = []
    while stream.current.type != end_token:
        if stream.current.type == 'func_string_arg':
            value = stream.current.value.strip()
            for char in ["'", '"']:
                if value.startswith(char) and value.endswith(char):
                    value = value[1:-1]
                    break
            stream.next()
            if keywords:
                for keyword in keywords:
                    kwargs[keyword] = value
                del keywords[:]
            else:
                args.append((value, 'func_string_arg'))
        elif stream.current.type == 'text':
            args.append((stream.current.value, 'text'))
            stream.next()
        elif stream.current.type == 'func_kwarg':
            keywords.append(stream.current.value)
            stream.next()
        elif stream.current.type == 'func_argument_delimiter':
            stream.next()
        else:
            break
    for keyword in keywords:
        args.append((keyword, 'func_kwarg'))
    return tuple(args), kwargs


def find_double_newline(s):
    """Returns the position just after a double newline in the given string."""
    pos = s.find(b'\r\n\r\n')
    if pos >= 0:
        pos += 4
    return pos


def entries_to_remove(entries, d):
    """
    Function to remove selected entries (key and respective values) from
    a given dict.
    Based on a reply from the user mattbornski [#]_ at stackoverflow.

    Parameters
    ----------
    entries : array
        Set of entried to be removed.
    d : dict
        Dictionary to apply the entried removal.

    References
    ----------
    ..  [#] mattbornski, 2012. http://stackoverflow.com/questions/8995611/removing-multiple-keys-from-a-dictionary-safely

    """
    for k in entries:
        d.pop(k, None)


def containsSpace(string):
    """
    Function to check, if a string contains whitespace characters.

    @param string string to be checked
    @type str
    @return flag indicating the presence of at least one whitespace character
    @rtype bool
    """
    for ch in string:
        if ch.isspace():
            return True
    return False


def process_data(data, dict_):
    """This function process the data for the optimization process"""
    indicator = dict_['ESTIMATION']['indicator']
    outcome = dict_['ESTIMATION']['dependent']
    D = data[indicator].values
    data1 = data[data[indicator] == 1]
    data2 = data[data[indicator] == 0]
    X1 = data1[dict_['TREATED']['order']].values
    X0 = data2[dict_['UNTREATED']['order']].values
    Z1 = data1[dict_['CHOICE']['order']].values
    Z0 = data2[dict_['CHOICE']['order']].values
    Y1 = data1[outcome].values
    Y0 = data2[outcome].values
    return D, X1, X0, Z1, Z0, Y1, Y0


def _coerce_field_name(field_name, field_index):
    """
    Coerce a field_name (which may be a callable) to a string.
    """
    if callable(field_name):
        if field_name.__name__ == '<lambda>':
            return 'lambda' + str(field_index)
        else:
            return field_name.__name__
    return field_name


def elem_Q_fit(params, w):
    """
    Fit Function: -Q-

    Constant Phase Element for Fitting
    """
    Q = params['Q']
    n = params['n']
    return 1 / (Q * (w * 1.0j) ** n)


@classmethod
def from_json(cls, obj):
    """Return an instance of cls decoded from the json string obj.

        The implementation of in this base class just returns cls(str(obj)).

        Args:
            cls: This class.
            obj: The json string to be decoded.

        Returns:
                An instance of cls decoded from obj.
        """
    return cls(str(obj))


@classmethod
def create(cls, **kwargs):
    """ Helper for add() + commit() """
    instance = cls(**kwargs)
    return instance.save()


def regex_pattern_to_url(pattern):
    """
    Take a url regex pattern from urlconf and return a url that matches it
    """
    url = pattern.replace('^', '').rstrip('$')
    if not url.startswith('/'):
        return '/' + url
    return url


def IPtoInt(a, b, c, d):
    """
    Converts a number from a sequence of dotted decimals into a single unsigned int.
    Args:
        a: IP address segment 1 ###.0.0.0
        b: IP address segment 2 0.###.0.0
        c: IP address segment 3 0.0.###.0
        d: IP address segment 4 0.0.0.###

    Returns: The IP address as a simple 32-bit unsigned integer

    """
    return (int(a) << 24) + (int(b) << 16) + (int(c) << 8) + int(d)


def stream_is_client_initiated(stream_id: int) ->bool:
    """
    Returns True if the stream is client initiated.
    """
    return not stream_id & 1


def resumeProducing():
    """
        Produce data for the consumer a single time.

        This tells a producer to produce data for the consumer once
        (not repeatedly, once only). Typically this will be done
        by calling the consumer's write() method a single time with
        produced data.
        """