def csv_format(s):
    """
    cleans syntax problems for csv output
  """
    s = s.replace('"', "'")
    s = s.replace('\n', ' ')
    s = s.replace('\x00D', '')
    return '"%s"' % s


def get_name_and_tag(name):
    """
    Split the name if the image into tuple (name, tag).

    * name: (str) name of the image
    * **return:** tuple:
        - repository
        - tag
    """
    name_parts = name.split(':')
    if len(name_parts) == 2:
        repo = name_parts[0]
        tag = name_parts[1]
    else:
        repo = name
        tag = 'latest'
    return repo, tag


def pclass_field_for_type(required, default):
    """
        Create a pyrsistent field for this model object for use with a PClass.

        :param bool required: Whether the field should be mandatory or
            optional.

        :param default: A value which the field will take on if not supplied
            with another one at initialization time.  May be ``NO_DEFAULT`` to
            omit this behavior.

        :return: A pyrsistent field descriptor for an attribute which is
            usable with values of this type.
        """


def discover(isdsAppliance, check_mode=False, force=False):
    """
    Discover available updates
    """
    return isdsAppliance.invoke_get('Discover available updates',
        '/updates/available/discover')


def addPhenotypeSearchOptions(parser):
    """
    Adds options to a phenotype searches command line parser.
    """
    parser.add_argument('--phenotype_association_set_id', '-s', default=
        None, help=
        'Only return phenotypes from this phenotype_association_set.')
    parser.add_argument('--phenotype_id', '-p', default=None, help=
        'Only return this phenotype.')
    parser.add_argument('--description', '-d', default=None, help=
        'Only return phenotypes matching this description.')
    parser.add_argument('--age_of_onset', '-a', default=None, help=
        'Only return phenotypes with this age_of_onset.')
    parser.add_argument('--type', '-T', default=None, help=
        'Only return phenotypes with this type.')


@classmethod
def from_dict(cls, d):
    """Transform the dict to a error object and return the error."""
    return cls(d.get('error'), d.get('@http_status_code'), d.get('warnings'))


def extract_commit_msgs(output, is_git=True):
    """ Returns a list of commit msgs from the given output. """
    msgs = []
    if output:
        msg = []
        for line in output.split('\n'):
            if not line:
                continue
            is_commit_msg = line.startswith(' ') or not line
            if is_commit_msg:
                if is_git and line.startswith('    '):
                    line = line[4:]
                msg.append(line)
            elif msg:
                msgs.append('\n'.join(msg))
                msg = []
        if msg:
            msgs.append('\n'.join(msg))
    return msgs


def solidify(thickness: float=0.01):
    """Create a solid skin by extruding, compensating for sharp angles

    :param thickness: Thickness
    :type thickness: float
    """
    pass


@staticmethod
def get_dayofweek(row):
    """Get day of the week from date"""
    x = row['date']
    try:
        return x.dayofweek
    except Exception as e:
        return 0


def _find_in_sequence(indices, sequence):
    """Helper function for ncon.

    check if the supplied indices appear at the beginning of sequence

    Parameters
    ----------
    indices : list of int
    sequence : list of int

    Returns
    -------
    idcs : list of int
        All the given indices that appear consecutively at the front of sequence
    """
    ptr = 0
    while ptr < len(sequence) and sequence[ptr] in indices:
        ptr = ptr + 1
    rtn_indices = sequence[:ptr]
    sequence = sequence[ptr:]
    return rtn_indices, sequence


def configure_app(app, testing=False):
    """set configuration for application
    """
    app.config.from_object('neocortex.config')
    if testing is True:
        app.config.from_object('neocortex.configtest')
    else:
        app.config.from_envvar('NEOCORTEX_CONFIG', silent=True)


@classmethod
def CreateWithQuantity(cls, quantity, *args, **kwargs):
    """
        This is a secondary interface for creating the object with an existing quantity.

        :param Quantity quantity:
            The quantity for this object

        @param args and kwargs: Those are dependent on the actual class -- this parameters
            are passed directly to _InternalCreateWithQuantity.
        """
    stub = cls.__Stub()
    stub.__class__ = cls
    stub._InternalCreateWithQuantity(quantity, *args, **kwargs)
    return stub


def comma_separate(elements) ->str:
    """Map a list to strings and make comma separated."""
    return ', '.join(map(str, elements))


def _fatal(msg):
    """Print a fatal error to the user.

    :param str msg:
        The message to print.
    """
    print('\x1b[01;31m *\x1b[01;39m An APK cannot be created.')
    print('\x1b[01;31m *\x1b[00;39m {msg}'.format(msg=msg))


def calculate_key(cls, target):
    """Calculate the reference key for this reference.

        Currently this is a two-tuple of the id()'s of the target
        object and the target function respectively.
        """
    try:
        return id(target.im_self), id(target.im_func)
    except AttributeError:
        return id(target.__self__), id(target.__func__)


@staticmethod
def f_btn(boolean):
    """ Return btn bs3 class depending on status

        :param boolean: Boolean
        :return: "btn-success" and "btn-danger" depending on boolean
        """
    if boolean:
        return 'btn-success'
    return 'btn-danger'


@classmethod
def extra_table_metadata(cls, database, table_name, schema_name):
    """Returns engine-specific table metadata"""
    return {}


def is_informational_response(headers):
    """
    Searches a header block for a :status header to confirm that a given
    collection of headers are an informational response. Assumes the header
    block is well formed: that is, that the HTTP/2 special headers are first
    in the block, and so that it can stop looking when it finds the first
    header field whose name does not begin with a colon.

    :param headers: The HTTP/2 header block.
    :returns: A boolean indicating if this is an informational response.
    """
    for n, v in headers:
        if isinstance(n, bytes):
            sigil = b':'
            status = b':status'
            informational_start = b'1'
        else:
            sigil = u':'
            status = u':status'
            informational_start = u'1'
        if not n.startswith(sigil):
            return False
        if n != status:
            continue
        return v.startswith(informational_start)


def validate_spec(index, spec):
    """ Validate the value for a parameter specialization.

    This validator ensures that the value is hashable and not None.

    Parameters
    ----------
    index : int
        The integer index of the parameter being specialized.

    spec : object
        The parameter specialization.

    Returns
    -------
    result : object
        The validated specialization object.

    """
    if spec is None:
        msg = 'cannot specialize template parameter %d with None'
        raise TypeError(msg % index)
    try:
        hash(spec)
    except TypeError:
        msg = "template parameter %d has unhashable type: '%s'"
        raise TypeError(msg % (index, type(spec).__name__))
    return spec


def number_of_vertical_links(shape):
    """Number of vertical links in a structured quad grid.

    Parameters
    ----------
    shape : tuple of int
        Shape of grid of nodes.

    Returns
    -------
    int :
        Number of vertical links in grid.

    Examples
    --------
    >>> from landlab.components.overland_flow._links import number_of_vertical_links
    >>> number_of_vertical_links((3, 4))
    8
    """
    return (shape[0] - 1) * shape[1]


def hide(unselected: bool=False):
    """Hide selected particles

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


def make_declare(loop_orders, dtypes, sub):
    """
    Produce code to declare all necessary variables.

    """
    decl = ''
    for i, (loop_order, dtype) in enumerate(zip(loop_orders, dtypes)):
        var = sub['lv%i' % i]
        decl += """
        %(dtype)s* %(var)s_iter;
        """ % locals()
        for j, value in enumerate(loop_order):
            if value != 'x':
                decl += (
                    """
                npy_intp %(var)s_n%(value)i;
                ssize_t %(var)s_stride%(value)i;
                int %(var)s_jump%(value)i_%(j)i;
                """
                     % locals())
            else:
                decl += (
                    """
                int %(var)s_jump%(value)s_%(j)i;
                """
                     % locals())
    return decl


def yggtime_comm():
    """Plot timing statistics comparing the different communication mechanisms."""
    from yggdrasil import timing
    timing.plot_scalings(compare='commtype')


@classmethod
def from_list(cls, data):
    """Construct an instance from python built-in containers.

        Parameters
        ----------
            data : list
        """
    return cls(*data)


def GraphicsStraightenLine(point1, point2):
    """
    Straighten a line in graphics

    :param `point1`: a point list???
    :param `point2`: a point list???

    """
    dx = point2[0] - point1[0]
    dy = point2[1] - point1[1]
    if dx == 0:
        return
    elif abs(float(dy) / dx) > 1:
        point2[0] = point1[0]
    else:
        point2[1] = point1[1]


def get_figure(ax):
    """
    Retrieves figure from axes. Axes can be either an instance
    of Matplotlib.Axes or a 1D/2D array of Matplotlib.Axes.

    :param ax: Axes or vector/array of Axes
    :return: Matplotlib.Figure
    """
    fig = None
    try:
        fig = ax.get_figure()
    except AttributeError:
        try:
            fig = ax[0].get_figure()
        except AttributeError:
            fig = ax[0][0].get_figure()
    return fig


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


def rip_move(MESH_OT_rip=None, TRANSFORM_OT_translate=None):
    """Rip polygons and move the result

    :param MESH_OT_rip: Rip, Disconnect vertex or edges from connected geometry
    :param TRANSFORM_OT_translate: Translate, Translate (move) selected items
    """
    pass


def __contains__(username):
    """
        Given user login name, is it contained in group?
        """


def cut_multicam(camera: int=1):
    """Cut multi-cam strip and select camera

    :param camera: Camera
    :type camera: int
    """
    pass


def _get_joint_depth(nodes, joint):
    """ How deep in the tree are we?

    :param nodes: Dictionary with skeleton hierarchy.
    :type nodes: dict
    :param joint: Name of the joint in question.
    :type joint: str
    :return: The depth of the joint in the hierarchy.
    :rtype: int
    """
    depth = 0
    parent = nodes[joint]['parent']
    while parent:
        depth += 1
        parent = nodes[parent]['parent']
    return depth


def copy():
    """Copy selected Grease Pencil points and strokes

    """
    pass


def _2_simplify_default_profiles(config):
    """
    The concept of a different 'process' for a profile has been removed and as such the
    default profiles key in the configuration no longer needs a value per process ('verdi', 'daemon').
    We remove the dictionary 'default_profiles' and replace it with a simple value 'default_profile'.
    """
    from aiida.manage.configuration import PROFILE
    default_profiles = config.pop('default_profiles', None)
    if default_profiles and 'daemon' in default_profiles:
        config['default_profile'] = default_profiles['daemon']
    elif default_profiles and 'verdi' in default_profiles:
        config['default_profile'] = default_profiles['verdi']
    elif PROFILE is not None:
        config['default_profile'] = PROFILE.name
    return config


def _get_model_form_field(model, name, formfield_callback=None, **kwargs):
    """
    Utility to create the formfield from a model field.
    When a field is not editable, a ``None`` will be returned.
    """
    field = model._meta.get_field(name)
    if not field.editable:
        return None
    if formfield_callback is None:
        formfield = field.formfield(**kwargs)
    elif not callable(formfield_callback):
        raise TypeError('formfield_callback must be a function or callable')
    else:
        formfield = formfield_callback(field, **kwargs)
    return formfield


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

        :param ShowcaseArchivedType val:
        :rtype: EventType
        """
    return cls('showcase_archived', val)


def is_compatible(target_type, check_instance):
    """
    Check the target compatibility with the check instance.

    :param target_type: Target subclass, if None, returns True
    :param check_instance: instance of some Check class
    :return: True if the target is None or compatible with the check instance
    """
    if not target_type:
        return True
    return isinstance(check_instance, target_type.get_compatible_check_class())


def __newobj__(cls, *args):
    """ A compatibility pickler function.

    This function is not part of the public Atom api.

    """
    return cls.__new__(cls, *args)


def manage_afterAdd(item, container):
    """Gets called after being added to a container"""


@staticmethod
def manifest_escape(string):
    """
        Escape colon and equals characters for inclusion in manifest file
        :param string:
        :return: string
        """
    return string.replace(':', '\\:').replace('=', '\\=')


def get_encoding_expected():
    """Return 'N', the number of total shares generated."""


def fro_meetup_geo(geo):
    """ From Meetup Data to geo location"""
    if geo is None or not geo:
        return
    return float(geo)


def _Int(parent, hasProps, propObj, propVal, **kwargs):
    """Creates and returns a widget allowing the user to edit the given
    :class:`.Int` property value. See the :func:`.widgets_number._Number`
    function for more details.

    See the :func:`_String` documentation for details on the parameters.
    """
    from fsleyes_props.widgets_number import _Number
    return _Number(parent, hasProps, propObj, propVal, **kwargs)


@staticmethod
def point_in_front_of_plane(plane, x, y, z):
    """
        Takes a plane ex:
        ```py
        collision_bsp = self.data.tagdata.collision_bsp.STEPTREE[0]
        collision_bsp.planes.STEPTREE[i]
        ```
        And dots the plane and coordinates and compares against d to
        see if the xyz are in front of it.
        """
    return x * plane.i + y * plane.j + z * plane.k >= plane.d


def get_crop_box(crop=None):
    """Return a tuple describing the current crop box used to
        create the web image. The describe the coordinate of the top
        left and the bottom right corner.
        """


def xyz_to_lab(x_val, y_val, z_val):
    """
    Convert XYZ color to CIE-Lab color.

    :arg float x_val: XYZ value of X.
    :arg float y_val: XYZ value of Y.
    :arg float z_val: XYZ value of Z.
    :returns: Tuple (L, a, b) representing CIE-Lab color
    :rtype: tuple

    D65/2° standard illuminant
    """
    xyz = []
    for val, ref in ((x_val, 95.047), (y_val, 100.0), (z_val, 108.883)):
        val /= ref
        if val > 0.008856:
            val = pow(val, 1 / 3.0)
        else:
            val = 7.787 * val + 16 / 116.0
        xyz.append(val)
    x_val, y_val, z_val = xyz
    cie_l = 116 * y_val - 16
    cie_a = 500 * (x_val - y_val)
    cie_b = 200 * (y_val - z_val)
    return cie_l, cie_a, cie_b


def hasCustomAddView():
    """This should be called only if there is ``singleMenuItem`` else return 0"""


def BGR2RGB(bgrInt):
    """Convert an integer in BGR format to an ``(r, g, b)`` tuple.

    ``bgr_int`` is an integer representation of an RGB color, where the R, G,
    and B values are in the range (0, 255), and the three channels are comined
    into ``bgr_int`` by a bitwise ``red | (green << 8) | (blue << 16)``. This
    leaves the red channel in the least-significant bits, making a direct
    translation to a QColor difficult (the QColor constructor accepts an
    integer form, but it assumes the *blue* channel is in the least-significant
    bits).

    Examples:

        >>> BGR2RGB(4227327)
        (255, 128, 64)

    """
    red, green, blue = bgrInt & 255, bgrInt >> 8 & 255, bgrInt >> 16 & 255
    return red, green, blue


@classmethod
def _defaults(cls, config):
    """
        Provide default configuration for a time sheet view.
        """
    title = cls.get_title()
    url_prefix = cls.get_url_prefix()
    config.add_tailbone_permission_group(cls.key, title)
    config.add_tailbone_permission(cls.key, '{}.viewall'.format(cls.key),
        'View full {}'.format(title))
    config.add_route(cls.key, '{}/'.format(url_prefix))
    config.add_view(cls, attr='full', route_name=cls.key, renderer=
        '/shifts/{}.mako'.format(cls.key), permission='{}.viewall'.format(
        cls.key))
    if cls.expose_employee_views:
        config.add_tailbone_permission(cls.key, '{}.view'.format(cls.key),
            'View single employee {}'.format(title))
        config.add_route('{}.employee'.format(cls.key), '{}/employee/'.
            format(url_prefix))
        config.add_view(cls, attr='employee', route_name='{}.employee'.
            format(cls.key), renderer='/shifts/{}.mako'.format(cls.key),
            permission='{}.view'.format(cls.key))
    other_key = 'timesheet' if cls.key == 'schedule' else 'schedule'
    config.add_route('{}.goto.{}'.format(cls.key, other_key), '{}/goto-{}'.
        format(url_prefix, other_key))
    config.add_view(cls, attr='crossview', route_name='{}.goto.{}'.format(
        cls.key, other_key), permission='{}.view'.format(other_key))


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

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

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


@classmethod
def BigInt(cls):
    """Postgres BigInt Type"""
    return cls(int, 'BIGINT')


def check_csrf_token(request, token):
    """ Determine if the supplied ``token`` is valid. Most implementations
        should simply compare the ``token`` to the current value of
        ``get_csrf_token`` but it is possible to verify the token using
        any mechanism necessary using this method.

        Returns ``True`` if the ``token`` is valid, otherwise ``False``.

        """


def psi_Ca_Na_CO3_HMW84(T, P):
    """c-c'-a: calcium sodium carbonate [HMW84]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


def setatrib(obj, attr: str, val):
    """
  Overiding the default getattr

  Arguments
  ---------
  obj : object
        The object from which to get the attribute.
  attr : str
         The attribute in string format.
  """
    attrs = attr.split('[')
    for ii in range(len(attrs)):
        if attrs[ii][-1] == ']':
            attr = attrs[ii][:-1]
        else:
            attr = attrs[ii]
        if ii == len(attrs) - 1:
            setattr(obj, attr, val)
        else:
            obj = getattr(obj, attr)


def box():
    """Define the effective cells."""
    box = []
    for i in range(9):
        for j in range(9):
            b = [0] * 81
            for i2 in range(9):
                b[i2 * 9 + j] = 1
            for j2 in range(9):
                b[i * 9 + j2] = 1
            begin = i // 3 * 27 + j // 3 * 3
            for i2 in [0, 1, 2]:
                for j2 in [0, 1, 2]:
                    b[begin + i2 * 9 + j2] = 1
            b[i * 9 + j] = 0
            bb = []
            for i2 in range(81):
                if b[i2] == 1:
                    bb = bb + [i2]
            box = box + [bb]
    return box


def whiten(series, fftlength, overlap=None, method='median', window='hann',
    detrend='linear'):
    """Whiten a `TimeSeries` against its own ASD

    Parameters
    ----------
    series : `~gwpy.timeseries.TimeSeries`
        the `TimeSeries` data to whiten

    fftlength : `float`
        FFT integration length (in seconds) for ASD estimation

    overlap : `float`, optional
        seconds of overlap between FFTs, defaults to half the FFT length

    method : `str`, optional
        FFT-averaging method, default: ``'median'``,

    window : `str`, `numpy.ndarray`, optional
        window function to apply to timeseries prior to FFT,
        default: ``'hann'``
        see :func:`scipy.signal.get_window` for details on acceptable
        formats

    detrend : `str`, optional
        type of detrending to do before FFT, default: ``'linear'``

    Returns
    -------
    wseries : `~gwpy.timeseries.TimeSeries`
        a whitened version of the input data with zero mean and unit variance

    See Also
    --------
    gwpy.timeseries.TimeSeries.whiten
    """
    if overlap is None:
        overlap = fftlength / 2
    return series.whiten(fftlength=fftlength, overlap=overlap, window=
        window, detrend=detrend, method=method).detrend(detrend)


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 iter_chunks(s, n):
    """
    Iterator that returns elements from s in chunks of size n.
    """
    chunk = []
    for x in s:
        chunk.append(x)
        if len(chunk) == n:
            yield chunk
            chunk = []
    if len(chunk):
        yield chunk


@classmethod
def appropriate_for_environment(cls, backend_name=None):
    """
        Returns true if the current class is likely to be the appropriate
        class to connect to a facedancer given the board_name and other
        environmental factors.

        board: The name of the backend, as typically retreived from the BACKEND
            environment variable, or None to try figuring things out based
            on other environmental factors.
        """
    return False


def ses(count, plural='s', singular=''):
    """
    ses is pronounced "esses".

    Return a string suffix that indicates a singular sense if
    the count is 1, and a plural sense otherwise.

    So, for example:
        log.info("%d item%s found", items, utils.ses(items))
    would log:
        "1 item found" if items was 1
    and:
        "0 items found" if items was 0

    And:
        log.info("%d famil%s found", 1, ses(1, singular='y', plural='ies'))
    or:
        log.info("%d famil%s found", 1, ses(1, 'ies', 'y'))
    would log:
        "1 family found" if items was 1
    and:
        "10 families found" if items was 10

    Note that the default covers pluralization for most English words and the positional
    override is ordered to handle most easily handfle other cases.

    This function officially has the highest comment:code ratio in our codebase.
"""
    return singular if count == 1 else plural


def is_a_mapping(var, allow_none=False):
    """ Returns True if var is a dictionary # TODO: ordered dict
    """
    return isinstance(var, dict) or var is None and allow_none


def getTermByToken(token):
    """Return an ITokenizedTerm for the passed-in token.

        If `token` is not represented in the vocabulary, `LookupError`
        is raised.
        """


def harmonic_oscillator_to_xv(actions, angles, potential):
    """
    Transform the input action-angle coordinates to cartesian
    position and velocity for the Harmonic Oscillator potential.

    .. note::

        This function is included as a method of the
        :class:`~gala.potential.HarmonicOscillatorPotential`
        and it is recommended to call
        :meth:`~gala.potential.HarmonicOscillatorPotential.phase_space()` instead.

    Parameters
    ----------
    actions : array_like
    angles : array_like
    potential : Potential
    """
    raise NotImplementedError(
        'Implementation not supported until working with angle-action variables has a better API.'
        )


@staticmethod
def _process_restore_data(data, decompressor, decryptor):
    """Decrypts and decompresses provided data according to args"""
    if decryptor:
        data = decryptor.decrypt(data)
    data = decompressor.decompress(data)
    return data


def _relativize(base: str, current: str) ->str:
    """Remove the base url from the current url."""
    if current.startswith(base):
        return current.replace(base, '', 1)
    return current


def safe_ord(c):
    """
    Returns an integer representing the Unicode code point of the character or int if int argument is passed

    :param c: character or integer
    :return: integer representing the Unicode code point of the character or int if int argument is passed
    """
    if isinstance(c, int):
        return c
    else:
        return ord(c)


def eyedropper_color(use_accumulate: bool=True):
    """Sample a color from the Blender window to store in a property

    :param use_accumulate: Accumulate
    :type use_accumulate: bool
    """
    pass


def should_sync_field(inclusion, selected, default=False):
    """
    Returns True if a field should be synced.

    inclusion: automatic|available|unsupported
    selected: True|False|None
    default: (default: False) True|False

    "automatic" inclusion always returns True:
    >>> should_sync_field("automatic", None, False)
    True
    >>> should_sync_field("automatic", True, False)
    True
    >>> should_sync_field("automatic", False, False)
    True
    >>> should_sync_field("automatic", None, True)
    True
    >>> should_sync_field("automatic", True, True)
    True
    >>> should_sync_field("automatic", False, True)
    True

    "unsupported" inclusion always returns False
    >>> should_sync_field("unsupported", None, False)
    False
    >>> should_sync_field("unsupported", True, False)
    False
    >>> should_sync_field("unsupported", False, False)
    False
    >>> should_sync_field("unsupported", None, True)
    False
    >>> should_sync_field("unsupported", True, True)
    False
    >>> should_sync_field("unsupported", False, True)
    False

    "available" inclusion uses the selected value when set
    >>> should_sync_field("available", True, False)
    True
    >>> should_sync_field("available", False, False)
    False
    >>> should_sync_field("available", True, True)
    True
    >>> should_sync_field("available", False, True)
    False

    "available" inclusion uses the default value when selected is None
    >>> should_sync_field("available", None, False)
    False
    >>> should_sync_field("available", None, True)
    True
    """
    if inclusion == 'automatic':
        return True
    if inclusion == 'unsupported':
        return False
    if selected is not None:
        return selected
    return default


def check_us_buffer(usbuffer):
    """
    Checks that all ultrasound buffer contains all required key values.

    :param usbuffer: the buffer to check
    :raises: KeyError
    :raises: ValueError
    """
    if 'name' not in usbuffer:
        raise KeyError('Buffer configuration must contain a name.')
    if 'start frame' not in usbuffer:
        raise KeyError('Buffer configuration must contain a start frame.')
    if 'end frame' not in usbuffer:
        raise KeyError('Buffer configuration must contain an end frame.')
    if 'x0' not in usbuffer:
        raise KeyError('Buffer configuration must contain x0')
    if 'x1' not in usbuffer:
        raise KeyError('Buffer configuration must contain x1')
    if 'y0' not in usbuffer:
        raise KeyError('Buffer configuration must contain y0')
    if 'y1' not in usbuffer:
        raise KeyError('Buffer configuration must contain y1')
    if 'scan direction' not in usbuffer:
        raise KeyError('Buffer configuration must contain a scan direction.')
    direction = usbuffer.get('scan direction')
    if direction not in ('x', 'y'):
        raise ValueError('scan direction must be either x or y')


def _next_internal_index():
    """Return next available internal index."""
    global _current_internal_index
    _current_internal_index += 1
    return _current_internal_index - 1


def make_string(arg):
    """Used to overload Dunder method __str__ of any class
    This is used to convert <class 'class_name'> to string form and this is also a valid json
    :param arg: arg ~ self, iterate over it and form key-value {attribute: value}
    :return: string value of self(arg) of any class
    """
    res = str()
    is_first = True
    for k, v in arg.__dict__.items():
        if v is not None and v is not '':
            if not is_first:
                res += ','
            if v.__str__()[0] == '{' or v.__str__()[0] == '[':
                res += '"{}":{}'.format(k, v)
            else:
                res += '"{}":"{}"'.format(k, v)
            is_first = False
    if is_first:
        return None
    return '{' + res + '}'


def alpha_operation_sorter(endpoint):
    """ sort endpoints first alphanumerically by path, then by method order """
    path, path_regex, method, callback = endpoint
    method_priority = {'GET': 0, 'POST': 1, 'PUT': 2, 'PATCH': 3, 'DELETE': 4
        }.get(method, 5)
    return path, method_priority


def pad(bytestring, k=16):
    """
    Pad an input bytestring according to PKCS#7

    """
    l = len(bytestring)
    val = k - l % k
    return bytestring + bytearray([val] * val)


def run(_):
    """Function to execute when the post-commit hook is called. """
    pass


def decode_keys(store, encoding='utf-8'):
    """
    If a dictionary has keys that are bytes decode them to a str.

    Parameters
    ------------
    store : dict
      Dictionary with data

    Returns
    ---------
    result : dict
      Values are untouched but keys that were bytes
      are converted to ASCII strings.

    Example
    -----------
    In [1]: d
    Out[1]: {1020: 'nah', b'hi': 'stuff'}

    In [2]: trimesh.util.decode_keys(d)
    Out[2]: {1020: 'nah', 'hi': 'stuff'}
    """
    keys = store.keys()
    for key in keys:
        if hasattr(key, 'decode'):
            decoded = key.decode(encoding)
            if key != decoded:
                store[key.decode(encoding)] = store[key]
                store.pop(key)
    return store


def describe_assessment_runs(assessmentRunArns=None):
    """
    Describes the assessment runs that are specified by the ARNs of the assessment runs.
    See also: AWS API Documentation

    Examples
    Describes the assessment runs that are specified by the ARNs of the assessment runs.
    Expected Output:

    :example: response = client.describe_assessment_runs(
        assessmentRunArns=[
            'string',
        ]
    )

    :type assessmentRunArns: list
    :param assessmentRunArns: [REQUIRED]
            The ARN that specifies the assessment run that you want to describe.
            (string) --

    :rtype: dict
    :return: {
        'assessmentRuns': [
            {
                'arn': 'string',
                'name': 'string',
                'assessmentTemplateArn': 'string',
                'state': 'CREATED'|'START_DATA_COLLECTION_PENDING'|'START_DATA_COLLECTION_IN_PROGRESS'|'COLLECTING_DATA'|'STOP_DATA_COLLECTION_PENDING'|'DATA_COLLECTED'|'START_EVALUATING_RULES_PENDING'|'EVALUATING_RULES'|'FAILED'|'ERROR'|'COMPLETED'|'COMPLETED_WITH_ERRORS',
                'durationInSeconds': 123,
                'rulesPackageArns': [
                    'string',
                ],
                'userAttributesForFindings': [
                    {
                        'key': 'string',
                        'value': 'string'
                    },
                ],
                'createdAt': datetime(2015, 1, 1),
                'startedAt': datetime(2015, 1, 1),
                'completedAt': datetime(2015, 1, 1),
                'stateChangedAt': datetime(2015, 1, 1),
                'dataCollected': True|False,
                'stateChanges': [
                    {
                        'stateChangedAt': datetime(2015, 1, 1),
                        'state': 'CREATED'|'START_DATA_COLLECTION_PENDING'|'START_DATA_COLLECTION_IN_PROGRESS'|'COLLECTING_DATA'|'STOP_DATA_COLLECTION_PENDING'|'DATA_COLLECTED'|'START_EVALUATING_RULES_PENDING'|'EVALUATING_RULES'|'FAILED'|'ERROR'|'COMPLETED'|'COMPLETED_WITH_ERRORS'
                    },
                ],
                'notifications': [
                    {
                        'date': datetime(2015, 1, 1),
                        'event': 'ASSESSMENT_RUN_STARTED'|'ASSESSMENT_RUN_COMPLETED'|'ASSESSMENT_RUN_STATE_CHANGED'|'FINDING_REPORTED'|'OTHER',
                        'message': 'string',
                        'error': True|False,
                        'snsTopicArn': 'string',
                        'snsPublishStatusCode': 'SUCCESS'|'TOPIC_DOES_NOT_EXIST'|'ACCESS_DENIED'|'INTERNAL_ERROR'
                    },
                ],
                'findingCounts': {
                    'string': 123
                }
            },
        ],
        'failedItems': {
            'string': {
                'failureCode': 'INVALID_ARN'|'DUPLICATE_ARN'|'ITEM_DOES_NOT_EXIST'|'ACCESS_DENIED'|'LIMIT_EXCEEDED'|'INTERNAL_ERROR',
                'retryable': True|False
            }
        }
    }

    :returns:
    (string) --

    """
    pass


def model_to_select_list(model_class, filter_dict=None, q_filter=None):
    """
    只选择 id 和 name，用来做列表选择
    :param model_class:
    :param filter_dict:
    :param q_filter:
    :return:
    """
    if filter_dict is None:
        filter_dict = {}
    if q_filter is not None:
        filter_list = [q_filter]
    else:
        filter_list = []
    objects = model_class.objects.filter(*filter_list, **filter_dict).values(
        'id', 'name')
    return list(objects)


def is_title_case(line):
    """Determine if a line is title-case (i.e. the first letter of every
    word is upper-case. More readable than the equivalent all([]) form."""
    for word in line.split(u' '):
        if len(word) > 0 and len(word) > 3 and word[0] != word[0].upper():
            return False
    return True


def helloworld():
    """
    This is a sample py file.
    print helloworld on screen.
    """
    print('Hello, world.')


def create_organization(FeatureSet=None):
    """
    Creates an AWS organization. The account whose user is calling the CreateOrganization operation automatically becomes the master account of the new organization.
    This operation must be called using credentials from the account that is to become the new organization's master account. The principal must also have the relevant IAM permissions.
    By default (or if you set the FeatureSet parameter to ALL ), the new organization is created with all features enabled and service control policies automatically enabled in the root. If you instead choose to create the organization supporting only the consolidated billing features by setting the FeatureSet parameter to CONSOLIDATED_BILLING" , then no policy types are enabled by default and you cannot use organization policies.
    See also: AWS API Documentation

    :example: response = client.create_organization(
        FeatureSet='ALL'|'CONSOLIDATED_BILLING'
    )

    :type FeatureSet: string
    :param FeatureSet: Specifies the feature set supported by the new organization. Each feature set supports different levels of functionality.
            CONSOLIDATED_BILLING : All member accounts have their bills consolidated to and paid by the master account. For more information, see Consolidated Billing in the AWS Organizations User Guide .
            ALL : In addition to all the features supported by the consolidated billing feature set, the master account can also apply any type of policy to any member account in the organization. For more information, see All features in the AWS Organizations User Guide .

    :rtype: dict
    :return: {
        'Organization': {
            'Id': 'string',
            'Arn': 'string',
            'FeatureSet': 'ALL'|'CONSOLIDATED_BILLING',
            'MasterAccountArn': 'string',
            'MasterAccountId': 'string',
            'MasterAccountEmail': 'string',
            'AvailablePolicyTypes': [
                {
                    'Type': 'SERVICE_CONTROL_POLICY',
                    'Status': 'ENABLED'|'PENDING_ENABLE'|'PENDING_DISABLE'
                },
            ]
        }
    }

    """
    pass


@classmethod
def _add_description_factory(cls, descr):
    """Factory method which populates the un-set class variables

        Returns:
          new dataloader class
        """
    for field in ['type', 'defined_as', 'args', 'output_schema', 'info',
        'dependencies', 'path', 'postprocessing']:
        setattr(cls, field, getattr(descr, field))
    return cls


def object_from_string(st):
    """Creates a Python class or function from its fully qualified name.

    :param st: A fully qualified name of a class or a function.
    :return: A function or class object.

    This method is used by serialization code to create a function or class
    from a fully qualified name.

    """
    pos = st.rfind('.')
    if pos < 0:
        raise ValueError('Invalid function or class name %s' % st)
    module_name = st[:pos]
    func_name = st[pos + 1:]
    mod = __import__(module_name, fromlist=[func_name], level=0)
    return getattr(mod, func_name)


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 take(n, listy):
    """Return the first n values from a list or a generator"""
    return list(listy)[:n]


@classmethod
def register(cls, name, transformation):
    """
        Register new transformation function.

        >>> brighter = lambda image, c: image * c
        >>> TransformImage.register('brighter', brighter)
        >>> transform = TransformImage(0).by('brighter', 1.5)

        :param string name: Name of transformation
        :param function transformation: Transformation function.
        """
    cls.transformations[name] = transformation


def wl_hasLock(token, killinvalids=0):
    """ Returns true if the lock identified by the token is attached
        to the object. """


def gen_time_str(t_info):
    """
    从时间信息生成要访问的日志文件名

    20150101

    """
    year = str(t_info.tm_year)
    if t_info.tm_mon < 10:
        month = '0' + str(t_info.tm_mon)
    else:
        month = str(t_info.tm_mon)
    if t_info.tm_mday < 10:
        day = '0' + str(t_info.tm_mday)
    else:
        day = str(t_info.tm_mday)
    return year + month + day


def build_unique_palette(thread_palette, threadlist):
    """Turns a threadlist into a unique index list with the thread palette"""
    chart = [None] * len(thread_palette)
    for thread in set(threadlist):
        index = thread.find_nearest_color_index(thread_palette)
        if index is None:
            break
        thread_palette[index] = None
        chart[index] = thread
    palette = []
    for thread in threadlist:
        palette.append(thread.find_nearest_color_index(chart))
    return palette


def __init__(config, changes_channel, metrics=None):
    """Initialize a Publisher Plugin client."""


@classmethod
def execute(cls, handler_name, **options):
    """
        executes the handler with the given name with given inputs.

        :param str handler_name: handler name to be executed.

        :raises CLIHandlerNotFoundError: cli handler not found error.

        :rtype: int
        """
    return cls._execute_service(handler_name, **options)


@staticmethod
def is_categorical_series(data):
    """true if data is a categorical series"""
    return isinstance(data.values[0], str)


def v_num_str(x):
    """Validate an object that can be coerced to an ``int``."""
    return int(x)


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 ycbcr2rgb(y__, cb_, cr_):
    """Convert the three YCbCr channels to RGB channels.
    """
    kb_ = 0.114
    kr_ = 0.299
    r__ = 2 * cr_ / (1 - kr_) + y__
    b__ = 2 * cb_ / (1 - kb_) + y__
    g__ = (y__ - kr_ * r__ - kb_ * b__) / (1 - kr_ - kb_)
    return r__, g__, b__


def vol_numbers(slice_nos):
    """ Calculate volume numbers inferred from slice numbers `slice_nos`

    The volume number for each slice is the number of times this slice number
    has occurred previously in the `slice_nos` sequence

    Parameters
    ----------
    slice_nos : sequence
        Sequence of slice numbers, e.g. ``[1, 2, 3, 4, 1, 2, 3, 4]``.

    Returns
    -------
    vol_nos : list
        A list, the same length of `slice_nos` giving the volume number for
        each corresponding slice number.
    """
    counter = {}
    vol_nos = []
    for s_no in slice_nos:
        count = counter.setdefault(s_no, 0)
        vol_nos.append(count)
        counter[s_no] += 1
    return vol_nos


def read_file(filename, mode='r'):
    """write_file will open a file, "filename" and write content, "content"
    and properly close the file
    """
    with open(filename, mode) as filey:
        content = filey.readlines()
    return content


def float2Fixed(value):
    """
    The float2Fixed method translates a float into a 1/64 pixel unit-value.
    """
    return int(round(value * 64))


@staticmethod
def _create_path(root, dict_type, path):
    """
        Create nested dicts in `root` using `dict_type` as constructor. Strings
        in `path` are used to construct the keys for nested dicts.

        Args:
            root (dict instance): Root dictionary, where the nested dicts will
                 be created.
            dict_type (dict-like class): Class which will be used to construct
                      dicts - ``dict_type()`.
            path (list/tuple of strings): List of keys for nested
                 ``dict_type``.

        Returns:
            dict: Return last nested dict (dict at last element at `path`).
        """
    for sub_path in path:
        if not isinstance(root.get(sub_path, None), dict):
            root[sub_path] = dict_type()
        root = root[sub_path]
    return root


def guess_type_value_type(none=True):
    """
    @param      none        if True and all values are empty, return None
    @return                 the list of types recognized by guess_type_value
    """
    return [None, str, int, float] if none else [str, int, float]


def spec_sum(ph2):
    """Compute total spectral sum of the real spectral quantity``ph^2``.

    Parameters
    ----------
    model : pyqg.Model instance
        The model object from which `ph` originates
    ph2 : real array
        The field on which to compute the sum

    Returns
    -------
    var_dens : float
        The sum of `ph2`
    """
    ph2 = 2.0 * ph2
    ph2[..., 0] = ph2[..., 0] / 2.0
    ph2[..., -1] = ph2[..., -1] / 2.0
    return ph2.sum(axis=(-1, -2))


@staticmethod
def _G12_to_G1(g12):
    """Calculate G1 from G12"""
    return 0.84293649 * g12


@staticmethod
def get_date_fields(facility):
    """
        Return a sequence of field names whose type is datetime
        """
    if facility == 'LCO':
        return ['start', 'end']
    return []


@classmethod
def _SetSupersedingClass(cls, superseding):
    """Set the class reference attribute.

        @param superseding: A Python class that is a subclass of this class.
        """
    assert superseding is None or issubclass(superseding, cls)
    if superseding is None:
        cls.__dict__.pop(cls.__SupersedingClassAttribute(), None)
    else:
        setattr(cls, cls.__SupersedingClassAttribute(), superseding)
    return superseding


def is_prime(number):
    """Check if a number is prime."""
    if number < 2:
        return False
    if number == 2:
        return True
    if number % 2 == 0:
        return False
    for _ in range(3, int(number ** 0.5) + 1, 2):
        if number % _ == 0:
            return False
    return True


def _list_from_data(data):
    """Get a new list of all the items in the list since the last
    special token, either an assigner or a list.
    Special token denoted by None, which cannot appear otherwise."""
    for index, item in enumerate(reversed(data)):
        if item is None:
            list_ = data[-index:len(data)]
            del data[-index:len(data)]
            return list_


@classmethod
def from_dict(cls, parsed_dict):
    """Create a DefaultLevels object from a dictionary.

        This creates the DefaultLevels object from a dictionary containing a
        m.room.power_levels event. The event structure isn't checked in this
        method.

        This shouldn't be used directly, the `PowerLevelsEvent` method will
        call this method to construct the DefaultLevels object.
        """
    content = parsed_dict['content']
    return cls(content['ban'], content['invite'], content['kick'], content[
        'redact'], content['state_default'], content['events_default'],
        content['users_default'])


def clearobscaches(repo):
    """Remove all obsolescence related cache from a repo

    This remove all cache in obsstore is the obsstore already exist on the
    repo.

    (We could be smarter here given the exact event that trigger the cache
    clearing)"""
    if b'obsstore' in repo._filecache:
        repo.obsstore.caches.clear()


def intersection_ll(ll1, ll2):
    """ compute the intersection between two list of lists
    """
    assert type(ll1) == list
    assert type(ll2) == list
    counts = 0
    for list_i in ll1:
        if list_i in ll2:
            counts += 1
    return counts


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 get_broken_symlink_check_str(variableName):
    """Will take a file variable name and will print bash code that will
    check if the symlink for that file is broken

    This seems to be helping with files not being staged out properly.
    Likely will not be necessary in bug fixed versions of Swift

    Do not force an exit since other checks may occur afer the symlink
    check"""
    return '# See if the symlink is orphaned... if so exit 1\nif [ ! -e $' + variableName + """ ] ; then
	echo "$""" + variableName + ' symlink good" >> $logFile 2>&1\nelse\n\techo "$' + variableName + """ symlink broken" >> $logFile 2>&1
	exit 1
fi

"""


def circle_select(x: int=0, y: int=0, radius: int=1, gesture_mode: int=0):
    """Select UV vertices using circle selection

    :param x: X
    :type x: int
    :param y: Y
    :type y: int
    :param radius: Radius
    :type radius: int
    :param gesture_mode: Gesture Mode
    :type gesture_mode: int
    """
    pass


@staticmethod
def _freshness_params_check(metric, error_msg):
    """
        Check the definition of a freshness metric for consistency, this will
        rise an assertion error if any error is met.

        :param metric: Freshness metric in a dict form.
        :type metric: dict
        :param error_msg: Error message to return in case of error.
        :type error_msg: str
        """
    assert metric['metric'] == 'freshness', error_msg
    assert len(metric) == 3 and 'columns' in metric and ('dateFormat' in
        metric or 'timeFormat' in metric), error_msg
    if 'dateFormat' in metric:
        assert type(metric['dateFormat']) is str
    else:
        assert type(metric['timeFormat']) is str
    columns = metric['columns']
    assert len(columns) > 0, 'Columns list is empty'
    assert type(columns) is list, error_msg
    for col in columns:
        assert type(col) is int or type(col) is str, error_msg


def get_resource_description(resource, format='default', context=None, **kwargs
    ):
    """Return a string representation of the resource, according to the
        `format`.

        :param resource: the `Resource` to describe
        :param format: the kind of description wanted. Typical formats are:
                       `'default'`, `'compact'` or `'summary'`.
        :param context: an optional rendering context to allow rendering rich
                        output (like markup containing links)
        :type context: `ResourceContext`

        Additional keyword arguments can be given as extra information for
        some formats.

        For example, the ticket with the id 123 is represented as:
         - `'#123'` in `'compact'` format,
         - `'Ticket #123'` for the `default` format.
         - `'Ticket #123 (closed defect): This is the summary'` for the
           `'summary'` format

        Note that it is also OK to not define this method if there's no
        special way to represent the resource, in which case the standard
        representations 'realm:id' (in compact mode) or 'Realm id' (in
        default mode) will be used.
        """


def snip_line(line, max_width, split_at):
    """Shorten a line to a maximum length."""
    if len(line) < max_width:
        return line
    return line[:split_at] + ' … ' + line[-(max_width - split_at - 3):]


def sum(input, dim, keepdims=False):
    """Reduce sum the input tensor along the given dim.

    Parameters
    ----------
    input : Tensor
        The input tensor.
    dim : int
        The reduce dim.
    keepdims : bool
        Whether to keep the summed dimension.

    Returns
    -------
    Tensor
        A framework-specific tensor.
    """
    pass


def enumerate_model_initializers(model, add_node=False):
    """
    Enumerates all the initializers of a model.

    :param model: ONNX graph
    :param add_node: if False, the function enumerates
        all output names from every node, otherwise, it
        enumerates tuple (output name, node)
    :return: enumerator
    """
    for node in model.graph.initializer:
        yield (node.name, node) if add_node else node.name


def build_gidx_and_mapping_graph(graph):
    """Build immutable graph index of the whole graph.

    Parameters
    ----------
    graph : GraphAdapter
        Graph

    Returns
    -------
    graph : utils.CtxCachedObject
        Function that generates a immutable graph index on given context
    edge_map : utils.CtxCachedObject
        Function that generates forward and backward edge mapping on given
        context
    nbits : int
        Number of ints needed to represent the graph
    """
    return graph.get_immutable_gidx, None, graph.bits_needed()


def fmt(docstr):
    """Format a docstring for use as documentation in sample config."""
    docstr = docstr.replace('\n', ' ')
    docstr = docstr.strip()
    return docstr


@staticmethod
def value_title(data):
    """Returns a string"""
    time = data.text.split(':', 1)
    return data.text, time[-1]


@staticmethod
def set_list_attributes(element1, element2, attributes):
    """
        Copy a list of attributes of a element for other element.

        :param element1: The element that have attributes copied.
        :type element1: hatemile.util.html.htmldomelement.HTMLDOMElement
        :param element2: The element that copy the attributes.
        :type element2: hatemile.util.html.htmldomelement.HTMLDOMElement
        :param attributes: The list of attributes that will be copied.
        :type attributes: list(str)
        """
    for attribute in attributes:
        if element1.has_attribute(attribute):
            element2.set_attribute(attribute, element1.get_attribute(attribute)
                )


def location_3d_to_region_2d(region: 'bpy.types.Region', rv3d:
    'bpy.types.RegionView3D', coord, default=None) ->'mathutils.Vector':
    """Return the region relative 2d location of a 3d position.

    :param region: region of the 3D viewport, typically bpy.context.region.
    :type region: 'bpy.types.Region'
    :param rv3d: 3D region data, typically bpy.context.space_data.region_3d.
    :type rv3d: 'bpy.types.RegionView3D'
    :param coord: 3d worldspace location.
    :param default: Return this value if coord is behind the origin of a perspective view.
    :return:  2d location
    """
    pass


def find_start_batch(data, s, initial_index=0):
    """
    Find the starting index of the first valid batch of sweep data and its corresponding header.
    :param data: Batch of data coming from the FPGA via the USB port.

    :param s: Settings dictionary
    :param initial_index: 0 if reading a new batch, non zero if finding the next valid sweep within a batch
    :return: Starting index of a sweep data, header of that sweep
    """
    flag_valid_header = False
    for index in range(initial_index, len(data) - s['nbytes_sweep'] - 2):
        current_header = [data[index], data[index + 1]]
        if current_header[0] == s['start'] and data[index + s[
            'nbytes_sweep'] + 2] == s['start']:
            print(
                '[INFO] Found start signal {} at position {} (jumped {} byte).'
                .format(current_header, index, index - initial_index))
            print('[INFO] Next header would read [{}, {}]'.format(data[
                index + s['nbytes_sweep'] + 2], data[index + s[
                'nbytes_sweep'] + 3]))
            flag_valid_header = True
            break
    if flag_valid_header:
        index += 2
    else:
        index = 0
        current_header = [0, 0]
    assert index >= 0
    assert current_header[0] == 127 or current_header[0] == 0
    return index, current_header


@staticmethod
def get_subtype(object_dictionary):
    """
        Given the hash representation of a subtype of this class,
        use the info in the hash to return the class of the subtype.
        """
    type = object_dictionary['instanceType']
    if type == 'compute':
        return 'ComputeInstanceDetails'
    else:
        return 'InstanceConfigurationInstanceDetails'


def _fill_orbitals(orb_coeffs, orb_energies, orb_occupations, oe, coeffs,
    shell_types, restricted):
    """Fill in orbital coefficients, energies and occupation numbers in ``orb``.

    Parameters
    ----------
    orb : Orbitals
        An object to represent the orbitals
    oe : list
        The orbital occupation numbers and energies read with
        ``_read_cp2k_occupations_energies``.
    coeffs : dict
        The orbital coefficients read with ``_read_cp2k_orbital_coeffs``.
    shell_types : np.ndarray
        The array with shell types of the GOBasis instance.
    restricted : bool
        Is wavefunction restricted or unrestricted?
    """
    offset = 0
    offsets = []
    ls = abs(shell_types)
    for l in sorted(set(ls)):
        offsets.append(offset)
        offset += (2 * l + 1) * (l == ls).sum()
    del offset
    iorb = 0
    for l, s, occ, ener in oe:
        cs = coeffs.get((l, s))
        stride = 2 * l + 1
        for m in range(-l, l + 1):
            im = m + l
            orb_energies[iorb] = ener
            orb_occupations[iorb] = occ / float((restricted + 1) * (2 * l + 1))
            for ic in range(len(cs)):
                orb_coeffs[offsets[l] + stride * ic + im, iorb] = cs[ic]
            iorb += 1


@staticmethod
def get_field_format_map(registration):
    """Generates dict which provides registration information for format fields"""
    return {'user_id': registration.user_id, 'user_name': registration.
        full_name, 'user_firstname': registration.first_name,
        'user_lastname': registration.last_name, 'event_id': registration.
        event_id, 'event_title': registration.event.title, 'eventuser_id':
        'e{0}u{1}'.format(registration.event_id, registration.user_id),
        'registration_title': registration.registration_form.title}


@classmethod
def get_cache(cls):
    """Get protected member"""
    return cls._cache


@classmethod
def red(cls, s):
    """Wrap text in red."""
    return cls._wrap_colour(s, cls.RED)


def wait(x, timeout=None, return_when='ALL_COMPLETED'):
    """ Wait until computation has finished

    This is a compatibility alias for ``dask.distributed.wait``.
    If it is applied onto Dask collections without Dask Futures or if Dask
    distributed is not installed then it is a no-op
    """
    try:
        from distributed import wait
        return wait(x, timeout=timeout, return_when=return_when)
    except (ImportError, ValueError):
        return x


def getRolesInContext(object):
    """Get a sequence of the roles assigned to the user in a context.

        Roles include both global roles (ones assigned to the user directly
        inside the user folder) and local roles (assigned in context of the
        passed in object).
        """


def get_key_val_of_max(dict_obj):
    """Returns the key-value pair with maximal value in the given dict.

    Example:
    --------
    >>> dict_obj = {'a':2, 'b':1}
    >>> print(get_key_val_of_max(dict_obj))
    ('a', 2)
    """
    return max(dict_obj.items(), key=lambda item: item[1])


def set_level():
    """
        Set the log level.  Must except atleast one of:
            ``['INFO', 'WARNING', 'ERROR', 'DEBUG', or 'FATAL']``.

        """


def as_formatted_lines(lines):
    """
    Return a text formatted for use in a Debian control file with proper
    continuation for multilines.
    """
    if not lines:
        return ''
    formatted = []
    for line in lines:
        stripped = line.strip()
        if stripped:
            formatted.append(' ' + line)
        else:
            formatted.append(' .')
    return '\n'.join(formatted).strip()


def print_qm_links(c_alpha_dict, c_beta_dict, f_name, mode='w'):
    """
    Note: this needs to be tested. Only ran once to get the protein residues set up correctly.
    @param c_alpha_dict: dict of protein residue to be broken to c_alpha atom id
    @param c_beta_dict: as above, but for c_beta
    @param f_name: The location of the file to write.
    @param mode: default is to write to a new file. Use option to designate to append to existing file.
    """
    with open(f_name, mode) as m_file:
        for resid in c_beta_dict:
            m_file.write(
                """    !! Break resid {} between CA and CB, and cap CB with hydrogen
    &LINK
       MM_INDEX  {}  !! CA
       QM_INDEX  {}  !! CB
       LINK_TYPE  IMOMM
       ALPHA_IMOMM  1.5
    &END LINK
"""
                .format(resid, c_alpha_dict[resid], c_beta_dict[resid]))
    if mode == 'w':
        print('Wrote file: {}'.format(f_name))


@classmethod
def create_from_agilent_string(cls, string: str, name_override: str=None
    ) ->'DADSignalInfo':
    """
        Creates a class instance from a standard Agilent signal description string (e.g. 'DAD1 A, Sig=210,4 Ref=360,100')

        :param string: signal description string
        :param name_override: override for name specification
        :return: DADSignal object
        """
    parsed = cls.get_values_from_agilent_string(string)
    if name_override is not None:
        parsed['name'] = name_override
    return cls(**parsed)


def url_report(stat, stats, file=None):
    """Print a report on the state for this URL.

    Also update the Stats instance.
    """
    if stat.exception:
        stats.add('fail')
        stats.add('fail_' + str(stat.exception.__class__.__name__))
        print(stat.url, 'error', stat.exception, file=file)
    elif stat.next_url:
        stats.add('redirect')
        print(stat.url, stat.status, 'redirect', stat.next_url, file=file)
    elif stat.content_type == 'text/html':
        stats.add('html')
        stats.add('html_bytes', stat.size)
        print(stat.url, stat.status, stat.content_type, stat.encoding, stat
            .size, '%d/%d' % (stat.num_new_urls, stat.num_urls), file=file)
    else:
        if stat.status == 200:
            stats.add('other')
            stats.add('other_bytes', stat.size)
        else:
            stats.add('error')
            stats.add('error_bytes', stat.size)
            stats.add('status_%s' % stat.status)
        print(stat.url, stat.status, stat.content_type, stat.encoding, stat
            .size, file=file)


def _scrub_request(http_request):
    """ Removes email address and password from a client login request.

    Since the mock server saves the request and response in plantext, sensitive
    information like the password should be removed before saving the
    recordings. At the moment only requests sent to a ClientLogin url are
    scrubbed.
    """
    if (http_request and http_request.uri and http_request.uri.path and
        http_request.uri.path.endswith('ClientLogin')):
        http_request._body_parts = []
        http_request.add_form_inputs({'form_data':
            'client login request has been scrubbed'})
    else:
        http_request._body_parts = []
    return http_request


def _timedelta_to_seconds(timedelta):
    """
    >>> _timedelta_to_seconds(datetime.timedelta(hours=3))
    10800
    >>> _timedelta_to_seconds(datetime.timedelta(hours=3, minutes=15))
    11700
    """
    return (timedelta.days * 86400 + timedelta.seconds + timedelta.
        microseconds // 1000)


def parse_bool(value):
    """
    >>> parse_bool(1)
    True
    >>> parse_bool('off')
    False
    >>> parse_bool('foo')
    """
    value = str(value).lower()
    if value in ('on', 'true', 'yes', '1'):
        return True
    if value in ('off', 'false', 'no', '0'):
        return False


def intersect_point_tri(pt: 'mathutils.Vector', tri_p1: 'mathutils.Vector',
    tri_p2: 'mathutils.Vector', tri_p3: 'mathutils.Vector'
    ) ->'mathutils.Vector':
    """Takes 4 vectors: one is the point and the next 3 define the triangle.

    :param pt: Point
    :type pt: 'mathutils.Vector'
    :param tri_p1: First point of the triangle
    :type tri_p1: 'mathutils.Vector'
    :param tri_p2: Second point of the triangle
    :type tri_p2: 'mathutils.Vector'
    :param tri_p3: Third point of the triangle
    :type tri_p3: 'mathutils.Vector'
    :return:  Point on the triangles plane or None if its outside the triangle
    """
    pass


def generate_device_class_methods(use_async: bool) ->str:
    """Generates the default methods for a device class.

    Args:
        use_async (bool): True if asynchronous code should be generated, false otherwise.

    Returns:
        str: The Python code for the default methods of a device class.

    """
    if use_async:
        return """
    def __enter__(self):
        return self

    def __exit__(self):
        raise RuntimeError()

    async def __aenter__(self):
        await self.open()
        return self

    async def __aexit__(self, *args):
        await self.close()

    def __await__(self):
        return self.__aenter__().__await__()

    async def open(self) -> None:
        await self.__client.open()

    async def close(self) -> None:
        await self.__client.close()

"""
    else:
        return """
    def __enter__(self):
        self.open()
        return self

    def __exit__(self, *args):
        self.close()

    def open(self) -> None:
        self.__client.open()

    def close(self) -> None:
        self.__client.close()

    def run(self, timeout: int = None) -> None:
        self.__client.run(timeout)

    def stop(self) -> None:
        self.__client.stop()

    def poll(self) -> None:
        self.__client.poll()

"""


def sort_events(event):
    """
    .. py:function:: sort_events(event)

        The key function used to sort events (both MIDI and Generic)

        :param event: An object of type :class:`MIDIEvent` or (a derrivative)
            :class:`GenericEvent`

        This function should be provided as the ``key`` for both
        ``list.sort()`` and ``sorted()``. By using it sorting will be as
        follows:

        * Events are ordered in time. An event that takes place earlier will
          appear earlier
        * If two events happen at the same time, the secondary sort key is
          ``ord``. Thus a class of events can be processed earlier than
          another. One place this is used in the code is to make sure that note
          off events are processed before note on events.
        * If time and ordinality are the same, they are sorted in the order in
          which they were originally added to the list. Thus, for example, if
          one is making an RPN call one can specify the controller change
          events in the proper order and be sure that they will end up in the
          file that way.
    """
    return event.time, event.ord, event.insertion_order


def truncated_range(value, values):
    """ Provides a validator function that returns the value
    if it is in the range. Otherwise it returns the closest
    range bound.

    :param value: A value to test
    :param values: A set of values that are valid
    """
    if min(values) <= value <= max(values):
        return value
    elif value > max(values):
        return max(values)
    else:
        return min(values)


def pivot_by_2_categories(df, cat1, cat2):
    """
    Create pivot table of df by category 1 and category 2.
    param: df: the dataframe
    param: cat1: the first category
    param: cat2: the second category
    return: pivot table
    """
    df_for_pivot = df[[cat1, cat2]].groupby([cat1, cat2]).size().reset_index(
        name='counts')
    df_pivot = df_for_pivot.pivot(index=cat1, columns=cat2, values='counts')
    return df_pivot


def button_right_pressed(controller):
    """
    Callback function that is triggered, when the right button was pressed

    :param controller:              the controller that triggered the action
    :type controller:               steuer.Controller
    """
    print('ACTION:' + str(controller.number) + ':' + controller.name +
        ':Button Right pressed')


@classmethod
def new(cls, item=None, annotation_definition=None, object_id=None,
    automated=None, metadata=None, frame_num=None, parent_id=None,
    start_time=None, height=None, width=None):
    """
        Create a new annotation object annotations

        :param start_time:
        :param width: annotation item's width
        :param height: annotation item's height
        :param item: item to annotate
        :param annotation_definition: annotation type object
        :param object_id: object_id
        :param automated: is automated
        :param metadata: metadata
        :param frame_num: optional - first frame number if video annotation
        :param parent_id: add parent annotation ID
        :return: annotation object
        """
    if frame_num is None:
        frame_num = 0
    if metadata is None:
        metadata = dict()
    if parent_id is not None:
        if 'system' not in metadata:
            metadata['system'] = dict()
        metadata['system']['parentId'] = parent_id
    frames = dict()
    if item is not None and item.fps is not None:
        fps = item.fps
    else:
        fps = None
    ann_type = None
    if annotation_definition is not None:
        ann_type = annotation_definition.type
    dataset_url = None
    dataset_id = None
    if item is not None:
        dataset_url = item.dataset_url
        dataset_id = item.datasetId
    if start_time is None:
        if fps is not None and frame_num is not None:
            start_time = frame_num / fps if fps != 0 else 0
        else:
            start_time = 0
    if frame_num is None:
        frame_num = 0
    return cls(annotation_definition=annotation_definition, id=None, url=
        None, item_url=None, item=item, item_id=None, creator=None,
        createdAt=None, updatedBy=None, updatedAt=None, object_id=object_id,
        type=ann_type, dataset_url=dataset_url, dataset_id=dataset_id,
        height=height, width=width, metadata=metadata, fps=fps, status=None,
        automated=automated, frames=frames, end_frame=frame_num, end_time=0,
        start_frame=frame_num, start_time=start_time, platform_dict=dict())


def make_comma_list_a_list(elements_to_rocess):
    """Process a list with commas to simple list.

    For example:
    ['elem1','elem2,elem3'] => ['elem1', 'elem2', 'elem3']

    :param elements_to_rocess: list to process
    :return: processed list with elemnts separated
    """
    output_list = []
    for element in elements_to_rocess:
        output_list.extend(element.split(','))
    return list(set(output_list))


@classmethod
def scopes(cls):
    """
        Return a list of valid/understood class scopes.
        Why do we need this? I believe it is not used anywhere.
        """
    return [cls.NONE, cls.ONE, cls.ALL]


def can_paginate(operation_name=None):
    """
    Check if an operation can be paginated.

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

    """
    pass


def K_to_discharge_coefficient(D, Do, K):
    """Converts a standard loss coefficient to a discharge coefficient.

    .. math::
        C = \\sqrt{\\frac{1}{2 \\sqrt{K} \\beta^{4} + K \\beta^{4}}
        - \\frac{\\beta^{4}}{2 \\sqrt{K} \\beta^{4} + K \\beta^{4}} }

    Parameters
    ----------
    D : float
        Upstream internal pipe diameter, [m]
    Do : float
        Diameter of orifice at flow conditions, [m]
    K : float
        Loss coefficient with respect to the velocity and density of the fluid
        just upstream of the orifice, [-]

    Returns
    -------
    C : float
        Coefficient of discharge of the orifice, [-]

    Notes
    -----
    If expansibility is used in the orifice calculation, the result will not
    match with the specified pressure drop formula in [1]_; it can almost
    be matched by dividing the calculated mass flow by the expansibility factor
    and using that mass flow with the loss coefficient.

    This expression was derived with SymPy, and checked numerically. There were
    three other, incorrect roots.

    Examples
    --------
    >>> K_to_discharge_coefficient(D=0.07366, Do=0.05, K=5.2314291729754)
    0.6151200000000001

    References
    ----------
    .. [1] American Society of Mechanical Engineers. Mfc-3M-2004 Measurement
       Of Fluid Flow In Pipes Using Orifice, Nozzle, And Venturi. ASME, 2001.
    .. [2] ISO 5167-2:2003 - Measurement of Fluid Flow by Means of Pressure
       Differential Devices Inserted in Circular Cross-Section Conduits Running
       Full -- Part 2: Orifice Plates.
    """
    beta = Do / D
    beta2 = beta * beta
    beta4 = beta2 * beta2
    root_K = K ** 0.5
    return ((1.0 - beta4) / (2.0 * root_K * beta4 + K * beta4)) ** 0.5


def GCD(x, y):
    """GCD(x:long, y:long): long
    Return the GCD of x and y.
    """
    x = abs(x)
    y = abs(y)
    while x > 0:
        x, y = y % x, x
    return y


@classmethod
def _get_smma(cls, df, column, windows):
    """ get smoothed moving average.

        :param df: data
        :param windows: range
        :return: result series
        """
    window = cls.get_only_one_positive_int(windows)
    column_name = '{}_{}_smma'.format(column, window)
    smma = df[column].ewm(ignore_na=False, alpha=1.0 / window, min_periods=
        0, adjust=True).mean()
    df[column_name] = smma
    return smma


def getEdgeListFromAdjMtx(adj, threshold=0.0, is_undirected=True,
    edge_pairs=None):
    """Function to get edgelist from adjaceny matrix."""
    result = []
    node_num = adj.shape[0]
    if edge_pairs:
        for st, ed in edge_pairs:
            if adj[st, ed] >= threshold:
                result.append((st, ed, adj[st, ed]))
    else:
        for i in range(node_num):
            for j in range(node_num):
                if j == i:
                    continue
                if is_undirected and i >= j:
                    continue
                if adj[i, j] > threshold:
                    result.append((i, j, adj[i, j]))
    return result


@staticmethod
def __ask_for_language(filename, languages):
    """Ask user for language for unknown extension"""
    print(
        "Cannot determine document '%s' type by extension. Select correct type:"
         % filename)
    for i, lang in enumerate(languages):
        print(str.format('  {0}) -> {1}', i + 1, lang))
    language = None
    while not language:
        dtype = input('Enter type number > ')
        try:
            dtype = int(dtype)
            if 1 <= dtype <= len(languages):
                language = languages[dtype - 1]
        except ValueError:
            pass
        if not language:
            print('Invalid value')
    return language


def process_arguement_pairs(arguments):
    """
    Create list of pairs from the arguments.

    Parameters
    ----------
    arguments : Tuple
        tuple list of arguments that are passed into a function
    """
    if not isinstance(arguments, tuple):
        message = 'Expected a tuple of arguments.'
        raise Exception(message)
    pairs = []
    for i in range(0, int(len(arguments) / 2)):
        pairs.append((arguments[2 * i], arguments[2 * i + 1]))
    return pairs


def check_cas_number(cas_no):
    """
	Checks the CAS number to ensure the check digit is valid
	with respect to the rest of the number.

	If the CAS number is valid 0 is returned. If there is a problem the difference
	between the computed check digit and that given as part of the CAS number
	is returned.

	:param cas_no:
	:type cas_no: int
	:return:
	:rtype: int
	"""
    cas_no = abs(int(cas_no))
    check_digit = cas_no % 10
    main_value = (cas_no - check_digit) // 10
    block_2 = main_value % 100
    block_1 = (main_value - block_2) // 100
    last_digit = block_2 % 10
    check_total = last_digit + (block_2 - last_digit) // 10 * 2
    for position, digit in enumerate(str(block_1)[::-1]):
        check_total += int(digit) * (position + 3)
    if check_digit == check_total % 10:
        return 0
    else:
        return check_total % 10 - check_digit


def __build_author_name_expr(author_name, author_email_address):
    """
    Build the name of the author of a message as described in the Internet
    Message Format specification: https://tools.ietf.org/html/rfc5322#section-3.6.2

    :param author_name: complete name of the originator of the message.

    :param author_email_address: address of the mailbox to which the author
        of the message suggests that replies be sent.

    :return: a string representing the author of the message, that is, the
        mailbox of the person or system responsible for the writing of the
        message.  This string is intended to be used as the "From:" field
        of the message.
    """
    assert author_name is not None or author_email_address is not None, 'Both arguments MUST NOT be bull'
    author_name_expr = author_name or author_email_address[:
        author_email_address.find('@')]
    if ' ' in author_name_expr:
        author_name_expr = f'"{author_name_expr}"'
    if author_email_address:
        author_name_expr = f'{author_name_expr} <{author_email_address}>'
    return author_name_expr


@staticmethod
def rename_key(old_key, new_key, commented_map, logger=None):
    """Rename a key in YAML file data that was loaded with the RoundTripLoader (e.g. that contains comments).

        Comments are retained for the renamed key. Order of keys is also maintained.

        Args:
            old_key: The existing key name you want to change.
            new_key: The new key name.
            commented_map: The YAML data CommentMap class (from yaml.load) with
                the key you want to change.
            logger: Optional logger instance which will be used to log this at
                the debug level.

        Returns the updated CommentedMap YAML dict. (Note that this method does not
        change the dict object (e.g. it's changed in place), you you most
        likely don't need to do anything with the returned dict.
        """
    if old_key == new_key or old_key not in commented_map:
        return commented_map
    key_list = list(commented_map.keys())
    for key in key_list:
        if key == old_key:
            if logger:
                logger.debug('Renaming key: %s: -> %s:', old_key, new_key)
            commented_map[new_key] = commented_map[old_key]
            try:
                commented_map.ca.items[new_key] = commented_map.ca.items.pop(
                    old_key)
            except KeyError:
                pass
            del commented_map[old_key]
        else:
            commented_map.move_to_end(key)
    return commented_map


def remove_duplicates_from_list(array):
    """Preserves the order of elements in the list"""
    output = []
    unique = set()
    for a in array:
        if a not in unique:
            unique.add(a)
            output.append(a)
    return output


@staticmethod
def unique_int(values):
    """
        if a list looks like 3,6
        if repeatedly called will return 1,2,4,5,7,8
        """
    last = 0
    for num in values:
        if last not in values:
            break
        else:
            last += 1
    return last


def plot_ejecta_stats():
    """Plot ejecta statistics.
    """
    pass


@classmethod
def hexblock(cls, data, address=None, bits=None, separator=' ', width=8):
    """
        Dump a block of hexadecimal numbers from binary data.
        Also show a printable text version of the data.

        @type  data: str
        @param data: Binary data.

        @type  address: str
        @param address: Memory address where the data was read from.

        @type  bits: int
        @param bits:
            (Optional) Number of bits of the target architecture.
            The default is platform dependent. See: L{HexDump.address_size}

        @type  separator: str
        @param separator:
            Separator between the hexadecimal representation of each character.

        @type  width: int
        @param width:
            (Optional) Maximum number of characters to convert per text line.

        @rtype:  str
        @return: Multiline output text.
        """
    return cls.hexblock_cb(cls.hexline, data, address, bits, width,
        cb_kwargs={'width': width, 'separator': separator})


def update_copy(d, _new=None, **kw):
    """Copy the given dict and update with the given values."""
    d = d.copy()
    if _new:
        d.update(_new)
    d.update(**kw)
    return d


@classmethod
def disable(cls, hash=None):
    """
        return string representing a 'disabled' hash;
        optionally including previously enabled hash
        (this is up to the individual scheme).
        """
    return cls.hash('')


def recf_chebyshev_poly(result, x, special, n, cur_var, pos):
    """Implementation of the recursion formula for Chebyshev polynomials
    of the first kind.
    The recursion formula is
    T_n = 2xT_{n-1} - T_{n-2}.

    .. note::

        The procedure on how an recursion function is built can be found in the
        docstring of the recursion function for
        standard polynomials *recf_standard_poly*.

    :param result: Contains the observations along the first dimension
        and the function values of expansion w.r.t. an observation
        along the second dimension.
    :type result: numpy.ndarray

    :param x: The data to be expanded.
    :type x: numpy.ndarray

    :param special: Index of a special element to be considered in the
        recursion formula. The special element is the first
        Chebyshev polynomial of the current variable.
    :type special: int

    :param n: The order of the function to be computed at this step.
    :type n: int

    :param cur_var: The index of the current variable to be considered in the
        recursion. This value will have to be lower than x.shape[1].
    :type cur_var: int

    :param pos: The index of the element to be computed, along the second
        dimension of result.
    :type pos: int

    :returns: The vectorized result (along the observations) of the
        n-th recursion step of the cur_var-th variable.
    :rtype: numpy.ndarray

    .. admonition:: Reference

        https://en.wikipedia.org/wiki/Chebyshev_polynomials
    """
    return 2.0 * x[:, (cur_var)] * result[:, (pos - 1)] - result[:, (pos - 2)]


def greatest_common_divisor(a, b):
    """
    Get the greatest common divisor of two numbers

    @param a: First number.
    @type  a: C{int}

    @param b: Second number.
    @type  b: C{int}

    @return: Greatest common divisor.
    @rtype:  C{int}
    """
    while b != 0:
        t = b
        b = a % b
        a = t
    return a


def filter_adjacent(iterable):
    """Collapse identical adjacent values"""
    current = type(str('Marker'), (object,), {})
    for item in iterable:
        if item != current:
            current = item
            yield item


def get_translation(pp):
    """Separate intrinsic matrix from translation and convert in lists"""
    kk = pp[:, :-1]
    f_x = kk[0, 0]
    f_y = kk[1, 1]
    x0, y0 = kk[(2), 0:2]
    aa, bb, t3 = pp[0:3, (3)]
    t1 = float((aa - x0 * t3) / f_x)
    t2 = float((bb - y0 * t3) / f_y)
    tt = [t1, t2, float(t3)]
    return kk.tolist(), tt


@classmethod
def _trim_backticks(cls, entity_type):
    """Remove backticks from identifier like `param`, if they exist."""
    if cls._is_quoted(entity_type, '`'):
        return cls._trim_ends(entity_type)
    return entity_type


@classmethod
def _path_to_value(cls, path, parent_dict):
    """Return a value from a dictionary at the given path"""
    keys = cls._path_to_keys(path)
    child_dict = parent_dict
    for key in keys[:-1]:
        child_dict = child_dict.get(key)
        if child_dict is None:
            return
    return child_dict.get(keys[-1])


def mention_items(mentions, min=None, max=None, limit=100):
    """Filter (key, IStatusUpdate) items by mentions.
        min and max are longint IStatusUpdate.id keys.
        limit returns [:limit] most recent items
        """


def Flatten(data=None, name=None, attr=None, out=None, **kwargs):
    """Flattens the input array into a 2-D array by collapsing the higher dimensions.
    .. note:: `Flatten` is deprecated. Use `flatten` instead.
    For an input array with shape ``(d1, d2, ..., dk)``, `flatten` operation reshapes
    the input array into an output array of shape ``(d1, d2*...*dk)``.
    Note that the behavior of this function is different from numpy.ndarray.flatten,
    which behaves similar to mxnet.ndarray.reshape((-1,)).
    Example::
        x = [[
            [1,2,3],
            [4,5,6],
            [7,8,9]
        ],
        [    [1,2,3],
            [4,5,6],
            [7,8,9]
        ]],
        flatten(x) = [[ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.],
           [ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.]]

    Defined in src/operator/tensor/matrix_op.cc:L250

    Parameters
    ----------
    data : Symbol
        Input array.

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

    Returns
    -------
    Symbol
        The result symbol.

    Examples
    --------
    Flatten is usually applied before `FullyConnected`, to reshape the 4D tensor
    produced by convolutional layers to 2D matrix:

    >>> data = Variable('data')  # say this is 4D from some conv/pool
    >>> flatten = Flatten(data=data, name='flat')  # now this is 2D
    >>> SymbolDoc.get_output_shape(flatten, data=(2, 3, 4, 5))
    {'flat_output': (2L, 60L)}

    >>> test_dims = [(2, 3, 4, 5), (2, 3), (2,)]
    >>> op = Flatten(name='flat')
    >>> for dims in test_dims:
    ... x = test_utils.random_arrays(dims)
    ... y = test_utils.simple_forward(op, flat_data=x)
    ... y_np = x.reshape((dims[0], np.prod(dims[1:]).astype('int32')))
    ... print('%s: %s' % (dims, test_utils.almost_equal(y, y_np)))
    (2, 3, 4, 5): True
    (2, 3): True
    (2,): True
    """
    return 0,


def SGErrors(IErr):
    """
    Interprets the error message code from SpcGroup. Used in SpaceGroup.

    :param IErr: see SGError in :func:`SpcGroup`
    :returns:
        ErrString - a string with the error message or "Unknown error"
    """
    ErrString = [' ', 'Less than 2 operator fields were found',
        'Illegal Lattice type, not P, A, B, C, I, F or R',
        'Rhombohedral lattice requires a 3-axis',
        'Minus sign does not preceed 1, 2, 3, 4 or 6',
        'Either a 5-axis anywhere or a 3-axis in field not allowed', ' ',
        'I for COMPUTED GO TO out of range.',
        'An a-glide mirror normal to A not allowed',
        'A b-glide mirror normal to B not allowed',
        'A c-glide mirror normal to C not allowed',
        'D-glide in a primitive lattice not allowed',
        'A 4-axis not allowed in the 2nd operator field',
        'A 6-axis not allowed in the 2nd operator field',
        'More than 24 matrices needed to define group', ' ',
        'Improper construction of a rotation operator',
        'Mirror following a / not allowed',
        'A translation conflict between operators',
        'The 2bar operator is not allowed',
        '3 fields are legal only in R & m3 cubic groups',
        'Syntax error. Expected I -4 3 d at this point', ' ',
        'A or B centered tetragonal not allowed', ' ',
        'unknown error in sgroup', ' ', ' ', ' ',
        'Illegal character in the space group symbol']
    try:
        return ErrString[IErr]
    except:
        return 'Unknown error'


@staticmethod
def get_active_name(root, schema_ids):
    """Get the active schema from a (list of) rule id(s)."""
    active = None
    for schema_id in schema_ids:
        locator = root.find("rule[@id='" + schema_id + "']/active")
        if locator.text == 'true':
            active = root.find("rule[@id='" + schema_id + "']/name").text
            return active


@staticmethod
def fraction_l(system_parameters: dict, y):
    """ Readout as fraction ligand bound """
    return 'Fraction l bound', y / system_parameters['l']


def cdriver2filetype(driver):
    """Convert a connection driver to a file type.

    Args:
        driver (str): The name of the connection driver.

    Returns:
        str: The corresponding file type for the driver.

    """
    _legacy = {'FileInputDriver': 'binary', 'FileOutputDriver': 'binary',
        'AsciiMapInputDriver': 'map', 'AsciiMapOutputDriver': 'map',
        'AsciiFileInputDriver': 'ascii', 'AsciiFileOutputDriver': 'ascii',
        'AsciiTableInputDriver': 'table', 'AsciiTableOutputDriver': 'table',
        'PandasFileInputDriver': 'pandas', 'PandasFileOutputDriver':
        'pandas', 'PickleFileInputDriver': 'pickle',
        'PickleFileOutputDriver': 'pickle', 'PlyFileInputDriver': 'ply',
        'PlyFileOutputDriver': 'ply', 'ObjFileInputDriver': 'obj',
        'ObjFileOutputDriver': 'obj', 'MatInputDriver': 'mat',
        'MatOutputDriver': 'mat'}
    if driver in _legacy:
        return _legacy[driver]
    raise ValueError('%s is not a registered connection driver.' % driver)


@staticmethod
def build_connection_name(connection):
    """
        Build a connections full name based on the module/name of the function.

        :param connection: connection
        :return: connection path
        """
    return '.'.join([connection.__module__, connection.__name__])


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


def transform_param(param: str):
    """
    >>> transform_param('general_practitioner')
    'general-practitioner'
    """
    if param[0] == '_' or param[0] == '.':
        return param
    return param.replace('_', '-')


def xy_offset(x, y, offset_x, offset_y, offset):
    """Increment X and Y coordinates by the given offsets."""
    return x + offset_x * offset, y + offset_y * offset


def select_camera(extend: bool=False):
    """Select the active camera

    :param extend: Extend, Extend the selection
    :type extend: bool
    """
    pass


@staticmethod
def is_primitive():
    """ Return `True`: discrete variables are stored as floats. """
    return True


def get_neighbor_port(graph, neighbor):
    """
    Function for get port for connector according neighbor listener port.
    :param graph: graph
    :param neighbor: neighbor name
    :return: port number
    """
    test = graph.nodes(data=True)[neighbor]
    try:
        if 'listener' in test:
            for item in test['listener']:
                if 'role' in item:
                    if 'inter-route' in item['role']:
                        return item['port']
                else:
                    raise AttributeError
    except AttributeError:
        raise AttributeError("Listener doesn't contains role!")


def parseDate(name):
    """return date part (yymmdd) given data url or file name"""
    return int(name.split('_')[-1].split('.')[0])


def value(_, val, *args):
    """Calculate fill value in VALUE mode."""
    return val


def _do_publish(client):
    """Internal function"""
    message = client._userdata.popleft()
    if isinstance(message, dict):
        client.publish(**message)
    elif isinstance(message, (tuple, list)):
        client.publish(*message)
    else:
        raise TypeError('message must be a dict, tuple, or list')


def sort(oids, reverse=False, limit=None):
    """ Return the intersection of the oids of the folder's order with the
        oids passed in.  If ``reverse`` is True, reverse the result set.  If
        ``limit`` is an integer, return only that number of items (after
        reversing, if reverse is True)."""


def is_char_interrupt(c):
    """Check whether `c` is EOT (end of transmission, ^D)."""
    return c == u'\x04'


@staticmethod
def p_sub_function_optional(p):
    """sub_function_optional : identifier"""
    p[0] = p.slice[1].type, p[1]


def safeint(num):
    """converts to int, regardless
  """
    try:
        v = int(num)
    except:
        v = 0
    return v


def uncertainty_model(value):
    """
    Format whitespace in XML nodes of kind uncertaintyModel
    """
    if value.lstrip().startswith('['):
        return value.strip()
    return ' '.join(value.split())


def _calculate_scores(p1, p2, game):
    """Calculates the scores for two players based their history.

    Parameters
    ----------
    p1: Player
        The first player.
    p2: Player
        The second player.
    game: Game
        Game object used to score rounds in the players' histories.

    Returns
    -------
    int, int
        The scores for the two input players.
    """
    s1, s2 = 0, 0
    for pair in zip(p1.history, p2.history):
        score = game.score(pair)
        s1 += score[0]
        s2 += score[1]
    return s1, s2


def get_required_kwonly_args(argspecs):
    """Determines whether given argspecs implies required keywords-only args
    and returns them as a list. Returns empty list if no such args exist.
    """
    try:
        kwonly = argspecs.kwonlyargs
        if argspecs.kwonlydefaults is None:
            return kwonly
        res = []
        for name in kwonly:
            if not name in argspecs.kwonlydefaults:
                res.append(name)
        return res
    except AttributeError:
        return []


def strip_and_replace_backslashes(path: str) ->str:
    """
    >>> strip_and_replace_backslashes('c:\\\\test')
    'c:/test'
    >>> strip_and_replace_backslashes('\\\\\\\\main\\\\install')
    '//main/install'
    """
    path = path.strip().replace('\\', '/')
    return path


def MgSiO3(mw_atmos, mh=1):
    """Defines properties for MgSiO3 as condensible"""
    if mh != 1:
        raise Exception(
            'Alert: No M/H Dependence in MgSiO3 Routine. Consult your local theorist to determine next steps.'
            )
    gas_mw = 100.4
    gas_mmr = 0.00275
    rho_p = 3.192
    return gas_mw, gas_mmr, rho_p


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

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

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

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


@classmethod
def render(cls, value, token, nomenclate_object, config_query_path=None,
    return_type=list, use_value_in_query_path=True, **kwargs):
    """ Default renderer for a token.  It checks the config for a match, if not found it uses the value provided.

        :param value: str, value we are trying to match (or config setting for the token)
        :param token: str, token we are searching for
        :param nomenclate_object: nomenclate.core.nomenclate.Nomenclate, instance of nomenclate object to query
        :param kwargs: any config settings that relate to the token as found from the nomenclate instance
        :return: str, the resulting syntactically rendered string
        """
    if config_query_path == None:
        config_query_path = nomenclate_object.OPTIONS_PATH + [token]
        if use_value_in_query_path:
            config_query_path += [value]
    config_matches = cls.get_config_match(value, config_query_path,
        return_type, nomenclate_object, **kwargs)
    options = cls.flatten_input(config_matches, value)
    option = cls.process_criteria(token, options, **kwargs
        ) if options else value
    return cls.process_token_augmentations(option, token_attr=getattr(
        nomenclate_object, token))


@classmethod
def _pipeline_prev(cls):
    """
        Identify the previous build step in the build pipeline.
        """
    return cls._ocean._PIPELINE[cls._ocean._PIPELINE.index(cls._PIPELINE[-1
        ]) - 1][1]


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


def providecolorsforlatex():
    """ Provides the dvipsnames colors in pure LaTeX.

    Used when the xcolor option of the same name is not available, e.g.
    within the new ACM LaTeX templates.

    """
    return """% define some COCO/dvipsnames colors because
% ACM style does not allow to use them directly
\\definecolor{NavyBlue}{HTML}{000080}
\\definecolor{Magenta}{HTML}{FF00FF}
\\definecolor{Orange}{HTML}{FFA500}
\\definecolor{CornflowerBlue}{HTML}{6495ED}
\\definecolor{YellowGreen}{HTML}{9ACD32}
\\definecolor{Gray}{HTML}{BEBEBE}
\\definecolor{Yellow}{HTML}{FFFF00}
\\definecolor{GreenYellow}{HTML}{ADFF2F}
\\definecolor{ForestGreen}{HTML}{228B22}
\\definecolor{Lavender}{HTML}{FFC0CB}
\\definecolor{SkyBlue}{HTML}{87CEEB}
\\definecolor{NavyBlue}{HTML}{000080}
\\definecolor{Goldenrod}{HTML}{DDF700}
\\definecolor{VioletRed}{HTML}{D02090}
\\definecolor{CornflowerBlue}{HTML}{6495ED}
\\definecolor{LimeGreen}{HTML}{32CD32}
"""


def set_raise_internal_errors(flag):
    """ If raise_internal_errors is set to True, then the errors in
        node execution are directly raised.

        If False, we will produce a wrapped messsage,
        which contains the line number where the
        node is declared. It is easier for debugging and in Python 3+
        the underlying error message is also printed.
    """
    global _raise_internal_errors
    _raise_internal_errors = flag


def serialize_ntriples(graph, f):
    """Serialize some graph to f as ntriples."""
    for triple in graph:
        f.write(str(triple))


def gather_nd(data=None, indices=None, name=None, attr=None, out=None, **kwargs
    ):
    """Gather elements or slices from `data` and store to a tensor whose
    shape is defined by `indices`.

    Given `data` with shape `(X_0, X_1, ..., X_{N-1})` and indices with shape
    `(M, Y_0, ..., Y_{K-1})`, the output will have shape `(Y_0, ..., Y_{K-1}, X_M, ..., X_{N-1})`,
    where `M <= N`. If `M == N`, output shape will simply be `(Y_0, ..., Y_{K-1})`.

    The elements in output is defined as follows::

      output[y_0, ..., y_{K-1}, x_M, ..., x_{N-1}] = data[indices[0, y_0, ..., y_{K-1}],
                                                          ...,
                                                          indices[M-1, y_0, ..., y_{K-1}],
                                                          x_M, ..., x_{N-1}]

    Examples::

      data = [[0, 1], [2, 3]]
      indices = [[1, 1, 0], [0, 1, 0]]
      gather_nd(data, indices) = [2, 3, 0]

      data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
      indices = [[0, 1], [1, 0]]
      gather_nd(data, indices) = [[3, 4], [5, 6]]

    Parameters
    ----------
    data : Symbol
        data
    indices : Symbol
        indices

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

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


def micros():
    """Returns uWSGI clock microseconds.

    :rtype: long
    """
    return 0