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

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


def try_get_dn_part(subject, oid=None):
    """
    Tries to extracts the OID from the X500 name.
    :param subject:
    :param oid:
    :return:
    """
    try:
        if subject is None:
            return None
        if oid is None:
            return None
        for sub in subject:
            if oid is not None and sub.oid == oid:
                return sub.value
    except:
        pass
    return None


def gen_metric(rundat, tester):
    """
    Generate metrics for a sample path run.

    Parameters
    ----------
    rundat : dict
        Ouput of a chnbase.MOSOSolver.solve call
    tester
        Instantiated object such that 'tester.metric' is callable

    Returns
    -------
    met_data : dict
        keys are iteration number, values are tuples of iteration
        number, number of calls, and metric values.
    """
    met_data = {}
    for nu in rundat['itersoln']:
        les_nu = rundat['itersoln'][nu]
        calls_nu = rundat['simcalls'][nu]
        met_nu = tester.metric(les_nu)
        met_data[nu] = nu, calls_nu, met_nu
    return met_data


def delsong():
    """
    @api {post} /music/collect/delsong 取消收藏单曲
    @apiVersion 1.0.0
    @apiGroup Collect
    @apiDescription 取消收藏单曲

    @apiParam (Headers) {string} token 令牌
    @apiParam (Headers) {String} device_token 设备令牌
    @apiParam (Headers) {String} content_type 内容类型
    @apiParam (Headers) {String} timestamp 时间戳

    @apiParam (Body) {String} itemid 单曲编号

    @apiSuccessExample {json} Success-Response:
    "Success."

    @apiError (Error) 31011 No item id in the request params!

    @apiErrorExample {json} Error-Response:
    {
        "data": "No item id in the request params!"
    }
    """


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


def get_lib_var(string):
    """Returns "lib" and "varname" for a given string of either "varname" or
    "lib.varname" form; lib defaults to None.
    String will often take the form "foo.fasta.some fasta block", in which case
    this function will return ("foo.fasta", "some fasta block"), as splitting
    commences from the right side of the string and stops after the first period
    it encounters."""
    lib, varname = None, None
    if '.' in string:
        lib, varname = string.rsplit('.', 1)
    else:
        varname = string
    return lib, varname


@classmethod
def address(cls, address):
    """ Creates a URI from an address.

        :param str address: of the form ``${SCHEME}://${HOST}:{$PORT}``
        :return: a Pilosa URI
        :type: pilosa.URI
        """
    uri = cls()
    uri._parse(address)
    return uri


def migrate(engine):
    """Return the number of rows migrated."""
    return 0


def paden(lst, len_, pad=0):
    """
    Pad lst to contain at least len_ items by adding pad to the end.
    """
    diff = len_ - len(lst)
    diff = 0 if diff < 0 else diff
    return lst + [pad] * diff


def canSetConstrainTypes():
    """
        Return True if the current user is permitted to constrain addable
        types in this folderish object.
        """


@classmethod
def get(cls, channel: int):
    """
        Get the attribute for an input number.

        :param channel: the channel number (1..4)
        :return: the enum for the desired channel.
        """
    return getattr(cls, 'input_{}'.format(channel))


def AddHeader(request, name, value, append=False):
    """
    See views.BaseView class function for docs
    """
    if append:
        headers = [(name, value)]
        if hasattr(request.response, 'headerlist'):
            headers += list(request.response.headerlist)
        request.response.headerlist = headers
    else:
        request.response.headers[name] = value


def _l2t_subterp(label):
    """Helper function converting subterp labels to text. Assumes label has
    more then one segment"""
    if label[1:] == ['Subpart', 'Interp']:
        return 'Interpretations for Regulation Text of Part ' + label[0]
    elif label[1:] == ['Appendices', 'Interp']:
        return 'Interpretations for Appendices of Part ' + label[0]
    elif len(label) == 4 and label[1] == 'Subpart' and label[3] == 'Interp':
        interpretations_for = 'Interpretations for Subpart '
        return interpretations_for + label[2] + ' of Part ' + label[0]


def raw_find(qry, fields=(), **kwargs):
    """Return a raw psycopg result cursor for the specified query.

        The qry is updated to also contain the container's filter condition.
        ``kwargs`` allows you to pass parameters to sqlbuilder.Select

        Note: The user is responsible of closing the cursor after use.
        """


def find_function_name(func, if_no_name=None):
    """Returns pretty-formatted function name."""
    return getattr(func, '__name__', if_no_name)


def _strip_placeholder_braces(p_matchobj):
    """
    Returns string with conditional braces around placeholder stripped and
    percent sign glued into placeholder character.

    Returned string is composed from 'start', 'before', 'placeholder', 'after',
    'whitespace', and 'end' match-groups of p_matchobj. Conditional braces are
    stripped from 'before' and 'after' groups. 'whitespace', 'start', and 'end'
    groups are preserved without any change.

    Using this function as an 'repl' argument in re.sub it is possible to turn:
        %{(}B{)}
    into:
        (%B)
    """
    before = p_matchobj.group('before') or ''
    placeholder = p_matchobj.group('placeholder')
    after = p_matchobj.group('after') or ''
    whitespace = p_matchobj.group('whitespace') or ''
    return before + '%' + placeholder + after + whitespace


@classmethod
def _process_foreign_keys(cls, foreign_keys, node_data):
    """For each foreign key try to load the actual object so load_bulk
        doesn't fail trying to load an int where django expects a
        model instance
        """
    for key in foreign_keys.keys():
        if key in node_data:
            node_data[key] = foreign_keys[key].objects.get(pk=node_data[key])


@classmethod
def encode_values(cls, entry_payload):
    """Encode all entries of all lists in dictionary to bytes."""
    encoded_dict = {}
    for key, list in entry_payload.items():
        encoded_list = []
        for entry in list:
            encoded_list.append(entry.encode('utf-8'))
            encoded_dict[key] = encoded_list
    return encoded_dict


def speed_dst(distance, time):
    """Usage: speed_dst(distance traveled, time taken)"""
    return distance / time


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


def temp_separation_tc(H_c, fc, T_air, t0, r_ah, r_s, r_x, r_air, cp=1004.16):
    """Compute canopy temperature

    Parameters
    ----------
    H_c : ee.Image
    fc : ee.Image
    T_air : ee.Image
        Air temperature (Kelvin).
    t0 :
    r_ah : ee.Image
    r_s : ee.Image
        Soil aerodynamic resistance to heat transport (s m-1).
    r_x : ee.Image
        Bulk canopy aerodynamic resistance to heat transport (s m-1).
    r_air : ee.Image
    cp :

    Returns
    -------
    T_c : ee.Image
        Canopy temperature (Kelvin).

    """
    T_c_lin = fc.expression(
        '((T_air / r_ah) +  (t0 / r_s / (1 - fc)) +  (H_c * r_x / r_air / cp * ((1 / r_ah) + (1 / r_s) + (1 / r_x)))) / ((1 / r_ah) + (1 / r_s) + (fc / r_s / (1 - fc)))'
        , {'cp': cp, 'fc': fc, 'H_c': H_c, 'r_ah': r_ah, 'r_air': r_air,
        'r_s': r_s, 'r_x': r_x, 't0': t0, 'T_air': T_air})
    Td = fc.expression(
        '(T_c_lin * (1 + (r_s / r_ah))) - (H_c * r_x / r_air / cp * (1 + (r_s / r_x) + (r_s / r_ah))) - (T_air * r_s / r_ah)'
        , {'cp': cp, 'H_c': H_c, 'r_ah': r_ah, 'r_air': r_air, 'r_s': r_s,
        'r_x': r_x, 'T_air': T_air, 'T_c_lin': T_c_lin})
    delta_T_c = fc.expression(
        '((t0 ** 4) - (fc * (T_c_lin ** 4)) - ((1 - fc) * (Td ** 4))) / ((4 * (1 - fc) * (Td ** 3) * (1 + (r_s / r_ah))) + (4 * fc * (T_c_lin ** 3)))'
        , {'fc': fc, 'r_ah': r_ah, 'r_s': r_s, 't0': t0, 'Td': Td,
        'T_c_lin': T_c_lin})
    T_c = fc.expression('T_c_lin + delta_T_c', {'T_c_lin': T_c_lin,
        'delta_T_c': delta_T_c}).where(fc.lt(0.1), t0).where(fc.gt(0.9), t0)
    T_c = T_c.where(T_c.lte(T_air.subtract(10.0)), T_air.subtract(10.0))
    T_c = T_c.where(T_c.gte(T_air.add(50.0)), T_air.add(50.0))
    return T_c


def airspeed_estimate(GLOBAL_POSITION_INT, WIND):
    """estimate airspeed"""
    wind = WIND
    gpi = GLOBAL_POSITION_INT
    from pymavlink.rotmat import Vector3
    import math
    wind3d = Vector3(wind.speed * math.cos(math.radians(wind.direction)),
        wind.speed * math.sin(math.radians(wind.direction)), 0)
    ground = Vector3(gpi.vx * 0.01, gpi.vy * 0.01, 0)
    airspeed = (ground + wind3d).length()
    return airspeed


def replace_string(metadata, field, from_str, to_str):
    """Replace part of a string in given metadata field"""
    new = None
    if from_str in metadata[field]:
        new = metadata[field].replace(from_str, to_str)
    return new


def set_visible_to_all_users(JobFlowIds=None, VisibleToAllUsers=None):
    """
    Sets whether all AWS Identity and Access Management (IAM) users under your account can access the specified clusters (job flows). This action works on running clusters. You can also set the visibility of a cluster when you launch it using the VisibleToAllUsers parameter of  RunJobFlow . The SetVisibleToAllUsers action can be called only by an IAM user who created the cluster or the AWS account that owns the cluster.
    See also: AWS API Documentation

    :example: response = client.set_visible_to_all_users(
        JobFlowIds=[
            'string',
        ],
        VisibleToAllUsers=True|False
    )

    :type JobFlowIds: list
    :param JobFlowIds: [REQUIRED]
            Identifiers of the job flows to receive the new visibility setting.
            (string) --

    :type VisibleToAllUsers: boolean
    :param VisibleToAllUsers: [REQUIRED]
            Whether the specified clusters are visible to all IAM users of the AWS account associated with the cluster. If this value is set to True, all IAM users of that AWS account can view and, if they have the proper IAM policy permissions set, manage the clusters. If it is set to False, only the IAM user that created a cluster can view and manage it.

    """
    pass


def reverse_list_valued_dict(dict_obj):
    """Reverse a list-valued dict, so each element in a list maps to its key.

    Parameters
    ----------
    dict_obj : dict
        A dict where each key maps to a list of unique values. Values are
        assumed to be unique across the entire dict, on not just per-list.

    Returns
    -------
    dict
        A dict where each element in a value list of the input dict maps to
        the key that mapped to the list it belongs to.

    Example
    -------
    >>> dicti = {'a': [1, 2], 'b': [3, 4]}
    >>> reverse_list_valued_dict(dicti)
    {1: 'a', 2: 'a', 3: 'b', 4: 'b'}
    """
    new_dict = {}
    for key in dict_obj:
        for element in dict_obj[key]:
            new_dict[element] = key
    return new_dict


@staticmethod
def compute_arg_names_hash(keys):
    """
        Converts a list of argument names to a hashable key.
        """
    return hash(tuple(sorted(keys)))


@staticmethod
def definitions_in(specs_dict):
    """
		Traverse specifications dict and detect definitions
		"""
    defs = set()
    stack = [specs_dict]
    while stack:
        top = stack.pop(0)
        for k, v in top.items():
            if isinstance(v, dict):
                if k == 'define':
                    defs.update(v.keys())
                else:
                    stack.append(v)
            elif isinstance(v, list):
                for d in v:
                    if isinstance(d, dict):
                        stack.append(d)
    return defs


@classmethod
def _pk_key(cls):
    """Internal. Returns the attribute name that acts as primary key of the model. Can be overridden by subclasses.

        :returns: str -- the name of the primary key attribute for the model
        """
    return 'uuid'


@classmethod
def reset_sequence(cls, value=None, force=False):
    """Reset the sequence counter.

        Args:
            value (int or None): the new 'next' sequence value; if None,
                recompute the next value from _setup_next_sequence().
            force (bool): whether to force-reset parent sequence counters
                in a factory inheritance chain.
        """
    cls._meta.reset_sequence(value, force=force)


def int_if_not_none(value):
    """
    Returns an int(value) if the value is not None.

    :param value: None or a value that can be converted to an int.
    :return: None or int(value)
    """
    return None if value is None else int(value)


@classmethod
def key_class(cls, algorithm: str):
    """
        Get private key class with algorithm

        :param algorithm: key algorithm
        :return: private key class
        """
    return cls.__key_classes.get(algorithm)


def error_msg_from_exception(e):
    """Translate exception into error message

    Database have different ways to handle exception. This function attempts
    to make sense of the exception object and construct a human readable
    sentence.

    TODO(bkyryliuk): parse the Presto error message from the connection
                     created via create_engine.
    engine = create_engine('presto://localhost:3506/silver') -
      gives an e.message as the str(dict)
    presto.connect('localhost', port=3506, catalog='silver') - as a dict.
    The latter version is parsed correctly by this function.
    """
    msg = ''
    if hasattr(e, 'message'):
        if isinstance(e.message, dict):
            msg = e.message.get('message')
        elif e.message:
            msg = '{}'.format(e.message)
    return msg or '{}'.format(e)


def JS_FILE():
    """
    Hardcoded JS file
    """
    return """
// Find output DOM associated to the DOM element passed as parameter
function findOutputForSlider( element ) {
  var idVal = element.id;
  outputs = document.getElementsByTagName( 'output' );
  for( var i = 0; i < outputs.length; i++ ) {
    if ( outputs[ i ].htmlFor == idVal )
      return outputs[ i ];
  }
}

function getSliderOutputPosition( slider ) {
 // Update output position
 var newPlace,
     minValue;

 var style = window.getComputedStyle( slider, null );
 // Measure width of range input
 sliderWidth = parseInt( style.getPropertyValue( 'width' ), 10 );

 // Figure out placement percentage between left and right of input
 if ( !slider.getAttribute( 'min' ) ) {
   minValue = 0;
 } else {
   minValue = slider.getAttribute( 'min' );
 }
 var newPoint = ( slider.value - minValue ) / ( slider.getAttribute( 'max' ) - minValue );

 // Prevent bubble from going beyond left or right (unsupported browsers)
 if ( newPoint < 0 ) {
   newPlace = 0;
 } else if ( newPoint > 1 ) {
   newPlace = sliderWidth;
 } else {
   newPlace = sliderWidth * newPoint;
 }

 return {
   'position': newPlace + 'px'
 }
}

document.addEventListener( 'DOMContentLoaded', function () {
 // Get all document sliders
 var sliders = document.querySelectorAll( 'input[type="range"].slider' );
 [].forEach.call( sliders, function ( slider ) {
   var output = findOutputForSlider( slider );
   if ( output ) {
     if ( slider.classList.contains( 'has-output-tooltip' ) ) {
       // Get new output position
       var newPosition = getSliderOutputPosition( slider );

       // Set output position
       output.style[ 'left' ] = newPosition.position;
     }

     // Add event listener to update output when slider value change
     slider.addEventListener( 'input', function( event ) {
       if ( event.target.classList.contains( 'has-output-tooltip' ) ) {
         // Get new output position
         var newPosition = getSliderOutputPosition( event.target );

         // Set output position
         output.style[ 'left' ] = newPosition.position;
       }

       // Update output with slider value
       output.value = event.target.value;
     } );
   }
 } );
} );

$(function() {
  $('#gen-form').submit(function(e) {
      e.preventDefault();
      $.ajax({
          type: "POST",
          url: "http://0.0.0.0:8080/predict",
          dataType: "json",
          data: JSON.stringify(getInputValues()),
          beforeSend: function(data) {
              $('#generate-text').addClass("is-loading");
              $('#generate-text').prop("disabled", true);
          },
          success: function(data) {
              $('#generate-text').removeClass("is-loading");
              $('#generate-text').prop("disabled", false);
              $('#tutorial').remove();
              var gentext = data.text;
              if ($("#prompt").length & $("#prompt").val() != '') {
                  var pattern = new RegExp('^' + $("#prompt").val(), 'g');
                  var gentext = gentext.replace(pattern, '' + $("#prompt").val() + '');
              }

              var gentext = gentext.replace(/\\n\\n/g, "

").replace(/\\n/g, "

");
              var html = '
' + gentext + '
';
              $(html).appendTo('#model-output').hide().fadeIn("slow");
          },
          error: function(jqXHR, textStatus, errorThrown) {
              $('#generate-text').removeClass("is-loading");
              $('#generate-text').prop("disabled", false);
              $('#tutorial').remove();
              var html = '
Attention, an error has occurred! Please try again.
';
              $(html).appendTo('#model-output').hide().fadeIn("slow");
          }
      });
  });
  $('#clear-text').click(function(e) {
      $('#model-output').text('')
  });

});

function getInputValues() {
  var inputs = {};
  $("select, textarea, input").each(function() {
      inputs[$(this).attr('id')] = $(this).val();
  });
  return inputs;
}
"""


def get():
    """Fetch the cookie or raise CookieError if a problem occurred

        :returns: (username, password)
        :rtype: tuple
        """


def receive_leave_group(group_state: 'GroupState'):
    """
    Received IGMP Leave packet regarding group GroupState
    """
    group_state.group_state_logger.debug(
        'NonQuerier NoMembersPresent: receive_leave_group')
    return


def discarding_tids_before(tid):
    """
        Remove the tid, and anything older, from the list of cached entries.

        Return the new value to place in the cache. Return None if all values
        were removed and the cached entry should be removed.
        """


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

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


def normalize_list_display_item(field):
    """
    We accept different types in list_display.
    This function handles them and transform into required format.
    """
    try:
        if isinstance(field, str):
            raise ValueError
        field_name, label = field
        render_function = None
    except (TypeError, ValueError):
        field_name, render_function, label = field, None, None
    else:
        if callable(field_name):
            render_function, field_name = field_name, render_function
    return field_name, render_function, label


def formula(individu, period, parameters):
    """
        Plus values de cession
        """
    frag_exon = individu('frag_exon', period)
    arag_exon = individu('arag_exon', period)
    nrag_exon = individu('nrag_exon', period)
    mbic_exon = individu('mbic_exon', period)
    abic_exon = individu('abic_exon', period)
    nbnc_proc = individu('nbnc_proc', period)
    nbic_exon = individu('nbic_exon', period)
    macc_exon = individu('macc_exon', period)
    aacc_exon = individu('aacc_exon', period)
    nacc_exon = individu('nacc_exon', period)
    mbnc_exon = individu('mbnc_exon', period)
    abnc_proc = individu('abnc_proc', period)
    nrag_pvce = individu('nrag_pvce', period)
    abnc_exon = individu('abnc_exon', period)
    nbnc_exon = individu('nbnc_exon', period)
    mncn_exon = individu('mncn_exon', period)
    cncn_exon = individu('cncn_exon', period)
    cncn_jcre = individu('cncn_jcre', period)
    cncn_info = individu('cncn_info', period)
    nbic_pvce = individu('nbic_pvce', period)
    cga = parameters(period).impot_revenu.rpns.cga_taux2
    return (frag_exon + arag_exon + nrag_exon + mbic_exon + abic_exon +
        nbnc_proc * (1 + cga) + nbic_exon + macc_exon + aacc_exon +
        nacc_exon + mbnc_exon + abnc_proc + abnc_exon + nbnc_exon +
        mncn_exon + cncn_exon + cncn_jcre + cncn_info + nbic_pvce + nrag_pvce)


@classmethod
def remove_finalizer(cls, id):
    """Removes a finalizer associated with this id.

        :param id: The id of the object for which the finalizer will be
            deleted.
        """
    with cls.lock:
        cls.finalizers.pop(id, None)


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

        :param FileRequestDeadline val:
        :rtype: UpdateFileRequestDeadline
        """
    return cls('update', val)


def argtopk_preprocess(a, idx):
    """ Preparatory step for argtopk

    Put data together with its original indices in a tuple.
    """
    return a, idx


@staticmethod
def _delete_port_binding(context, client, port_id, host):
    """Deletes the binding for the given host on the given port.

        :param context: The request context for the operation.
        :param client: keystoneauth1.adapter.Adapter
        :param port_id: ID of the port from which to delete the binding
        :param host: A string name of the host on which the port is bound
        :return: requests.Response object
        """
    return client.delete('/v2.0/ports/%s/bindings/%s' % (port_id, host),
        raise_exc=False, global_request_id=context.global_id)


def MACDFIX(df, n, price='Close'):
    """
    Moving Average Convergence/Divergence Fix 12/26
    """


def build_lines_data(lines):
    """Build byte string from given lines to send to server."""
    result = []
    for line in lines:
        result.append('{:04x}'.format(len(line) + 5).encode())
        result.append(line)
        result.append(b'\n')
    result.append(b'0000')
    return b''.join(result)


def _get_svn_status(output):
    """This function exists to enable mocking the `svn status` output in tests.
    """
    return output[0].decode('utf-8').splitlines()


def version_is_compatible(imp_version, version):
    """Determine whether versions are compatible.

    :param imp_version: The version implemented
    :param version: The version requested by an incoming message.
    """
    if imp_version is None:
        return True
    if version is None:
        return False
    version_parts = version.split('.')
    imp_version_parts = imp_version.split('.')
    try:
        rev = version_parts[2]
    except IndexError:
        rev = 0
    try:
        imp_rev = imp_version_parts[2]
    except IndexError:
        imp_rev = 0
    if int(version_parts[0]) != int(imp_version_parts[0]):
        return False
    if int(version_parts[1]) > int(imp_version_parts[1]):
        return False
    if int(version_parts[1]) == int(imp_version_parts[1]) and int(rev) > int(
        imp_rev):
        return False
    return True


@classmethod
def _cache(cls, app_name):
    """Return cache file."""
    from pathlib import Path
    from appdirs import user_cache_dir
    cache_dir = Path(user_cache_dir(app_name, None))
    cache_dir.mkdir(parents=True, exist_ok=True)
    return cache_dir / cls.STATE_NAME


def csv_loader_config(source_settings, mapping, data_source_meta,
    sample_data={}, column_samples=[]):
    """
    The loaderConfig for a csv_data_loader
    """
    return {'generated': True, 'sourceSettings': source_settings, 'mapping':
        mapping, 'sampleData': sample_data, 'columnSamples': column_samples,
        'datasource': data_source_meta}


def create_cab(archive, compression, cmd, verbosity, interactive, filenames):
    """Create a CAB archive."""
    cmdlist = [cmd, '-r']
    cmdlist.extend(filenames)
    cmdlist.append(archive)
    return cmdlist


def deprecate_activity_type(domain=None, activityType=None):
    """
    Deprecates the specified activity type . After an activity type has been deprecated, you cannot create new tasks of that activity type. Tasks of this type that were scheduled before the type was deprecated will continue to run.
    Access Control
    You can use IAM policies to control this action's access to Amazon SWF resources as follows:
    If the caller does not have sufficient permissions to invoke the action, or the parameter values fall outside the specified constraints, the action fails. The associated event attribute's cause parameter will be set to OPERATION_NOT_PERMITTED. For details and example IAM policies, see Using IAM to Manage Access to Amazon SWF Workflows .
    See also: AWS API Documentation

    :example: response = client.deprecate_activity_type(
        domain='string',
        activityType={
            'name': 'string',
            'version': 'string'
        }
    )

    :type domain: string
    :param domain: [REQUIRED]
            The name of the domain in which the activity type is registered.

    :type activityType: dict
    :param activityType: [REQUIRED]
            The activity type to deprecate.
            name (string) -- [REQUIRED]The name of this activity.
            Note
            The combination of activity type name and version must be unique within a domain.
            version (string) -- [REQUIRED]The version of this activity.
            Note
            The combination of activity type name and version must be unique with in a domain.

    :returns:
    domain (string) -- [REQUIRED]
    The name of the domain in which the activity type is registered.

    activityType (dict) -- [REQUIRED]
    The activity type to deprecate.

    name (string) -- [REQUIRED]The name of this activity.

    Note
    The combination of activity type name and version must be unique within a domain.

    version (string) -- [REQUIRED]The version of this activity.

    Note
    The combination of activity type name and version must be unique with in a domain.

    """
    pass


def bytes_packet(_bytes, termination_string=']'):
    """
    Create a packet containing the amount of bytes for the proceeding data.

    :param _bytes:
    :param termination_string:
    :return:
    """
    return '{}{}'.format(len(_bytes), termination_string)


def describe_volumes(InstanceId=None, StackId=None, RaidArrayId=None,
    VolumeIds=None):
    """
    Describes an instance's Amazon EBS volumes.
    See also: AWS API Documentation

    :example: response = client.describe_volumes(
        InstanceId='string',
        StackId='string',
        RaidArrayId='string',
        VolumeIds=[
            'string',
        ]
    )

    :type InstanceId: string
    :param InstanceId: The instance ID. If you use this parameter, DescribeVolumes returns descriptions of the volumes associated with the specified instance.

    :type StackId: string
    :param StackId: A stack ID. The action describes the stack's registered Amazon EBS volumes.

    :type RaidArrayId: string
    :param RaidArrayId: The RAID array ID. If you use this parameter, DescribeVolumes returns descriptions of the volumes associated with the specified RAID array.

    :type VolumeIds: list
    :param VolumeIds: Am array of volume IDs. If you use this parameter, DescribeVolumes returns descriptions of the specified volumes. Otherwise, it returns a description of every volume.
            (string) --

    :rtype: dict
    :return: {
        'Volumes': [
            {
                'VolumeId': 'string',
                'Ec2VolumeId': 'string',
                'Name': 'string',
                'RaidArrayId': 'string',
                'InstanceId': 'string',
                'Status': 'string',
                'Size': 123,
                'Device': 'string',
                'MountPoint': 'string',
                'Region': 'string',
                'AvailabilityZone': 'string',
                'VolumeType': 'string',
                'Iops': 123
            },
        ]
    }

    """
    pass


def get_neighbors(graph, n, center):
    """Get the neighbors of a networkx graph"""
    nodes = set([center])
    for i in range(n):
        for n in nodes:
            nodes = nodes.union(set(graph.neighbors(n)))
    return nodes


def imperial_subtract(l1, l2):
    """Subtracts one number from another in feet and inches

        Parameters
        ----------
        l1: tuple
            A length in the form (feet, inches)
        l2: tuple
            A length in the form (feet, inches)

        Returns
        -------
        ret : tuple
            Length 1 minus length 2, in (feet, inches)
    """
    total = l1[0] * 12 + l1[1] - (l2[0] * 12 + l2[1])
    feet = total / 12
    inches = total % 12
    return feet, inches


def new_reference(content, name=None):
    """Create a new reference.
        """


def merge_headers(header_map_list):
    """
    Helper function for combining multiple header maps into one.

    :param list(dict) header_map_list: list of maps

    :rtype: dict
    """
    headers = {}
    for header_map in header_map_list:
        if header_map:
            headers.update(header_map)
    return headers


@staticmethod
def _get_map_type(hdu_list, hdu_name):
    """Infer map type from a FITS HDU.

        Only read header, never data, to have good performance.
        """
    if hdu_name is None:
        header = hdu_list[0].header
        if header['NAXIS'] == 0:
            header = hdu_list[1].header
    else:
        header = hdu_list[hdu_name].header
    if 'PIXTYPE' in header and header['PIXTYPE'] == 'HEALPIX':
        return 'hpx'
    else:
        return 'wcs'


def show_table(name, table, i, total):
    """
    Display table info,
    name is tablename
    table is table object
    i is current Index
    total is total of tables
    """
    return '[%d/%d, %s] %s' % (i + 1, total, table.__appname__, name)


def diagonal_distance(i, j):
    """Weight function returning `|i-j|`.
    """
    return abs(i - j)


def get_if_tid_matches(tid):
    """
        Return the ``(state, tid)`` for the given TID.

        A special value of None matches any previously frozen TID.

        If no TID matches, returns None.

        """


def list_visitor(state, components):
    """Return a list of lists to represent the partition.

    Examples
    ========

    >>> states = multiset_partitions_taocp([1, 2, 1])
    >>> s = next(states)
    >>> list_visitor(s, 'abc')  # for multiset 'a b b c'
    [['a', 'b', 'b', 'c']]
    >>> s = next(states)
    >>> list_visitor(s, [1, 2, 3])  # for multiset '1 2 2 3
    [[1, 2, 2], [3]]

    """
    f, lpart, pstack = state
    partition = []
    for i in range(lpart + 1):
        part = []
        for ps in pstack[f[i]:f[i + 1]]:
            if ps.v > 0:
                part.extend([components[ps.c]] * ps.v)
        partition.append(part)
    return partition


def format_uptime(uptime_in_seconds):
    """Format number of seconds into human-readable string.
    :param uptime_in_seconds: The server uptime in seconds.
    :returns: A human-readable string representing the uptime.
    >>> uptime = format_uptime('56892')
    >>> print(uptime)
    15 hours 48 min 12 sec
    """
    m, s = divmod(int(uptime_in_seconds), 60)
    h, m = divmod(m, 60)
    d, h = divmod(h, 24)
    uptime_values = []
    for value, unit in ((d, 'days'), (h, 'hours'), (m, 'min'), (s, 'sec')):
        if value == 0 and not uptime_values:
            continue
        elif value == 1 and unit.endswith('s'):
            unit = unit[:-1]
        uptime_values.append('{0} {1}'.format(value, unit))
    uptime = ' '.join(uptime_values)
    return uptime


def normalize_tag(tag):
    u"""Normalizes tag name.

    >>> normalize_tag('tag-NaMe')
    'tag_name'
    """
    return tag.lower().replace('-', '_')


def df2list(df):
    """
    Converts a dataframe into a list of lists.

    @param      df      DataFrame
    @return             list of lists

    .. faqref::
        :tag: pandas
        :title: Convertir un DataFrame en une liste de listes ?

    ::

        df = DataFrame( ... )
        l  = df.values.tolist()

    .. faqref::
        :tag: pandas
        :title: Comment vérifier que deux DataFrame sont égaux (2) ?

        Comparer deux `DataFrame <http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html>`_
        avec l'opérateur ``==`` ne fonctionne pas.
        On obtient un message d'erreur ::

            ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

        Il faut au préalable convertir le Dataframe en le convertissant en liste ::

            df.values.tolist() == df2.values.tolist()
    """
    return df.vales.tolist()


def add_spelling(ix, fieldnames, commit=True):
    """Adds spelling files to an existing index that was created without
    them, and modifies the schema so the given fields have the ``spelling``
    attribute. Only works on filedb indexes.
    >>> ix = index.open_dir("testindex")
    >>> add_spelling(ix, ["content", "tags"])
    :param ix: a :class:`whoosh.filedb.fileindex.FileIndex` object.
    :param fieldnames: a list of field names to create word graphs for.
    :param force: if True, overwrites existing word graph files. This is only
        useful for debugging.
    """
    from whoosh.automata import fst
    from cylleneus.engine.reading import SegmentReader
    writer = ix.writer()
    storage = writer.storage
    schema = writer.schema
    segments = writer.segments
    for segment in segments:
        ext = segment.codec().FST_EXT
        r = SegmentReader(storage, schema, segment)
        f = segment.create_file(storage, ext)
        gw = fst.GraphWriter(f)
        for fieldname in fieldnames:
            gw.start_field(fieldname)
            for word in r.lexicon(fieldname):
                gw.insert(word)
            gw.finish_field()
        gw.close()
    for fieldname in fieldnames:
        schema[fieldname].spelling = True
    if commit:
        writer.commit(merge=False)


def test_col(serie, test_func, proportion=0.9, skipna=True, num_rows=50):
    """ Tests values of the serie using test_func.
         - skipna : if True indicates that NaNs are not counted as False
         - proportion :  indicates the proportion of values that have to pass the test
    for the serie to be detected as a certain type
    """
    serie = serie[serie.notnull()]
    ser_len = len(serie)
    if ser_len == 0:
        return False
    if proportion == 1:
        for _range in [range(0, min(1, ser_len)), range(min(1, ser_len),
            min(5, ser_len)), range(min(5, ser_len), min(num_rows, ser_len))]:
            if all(serie.iloc[_range].apply(test_func)):
                pass
            else:
                return False
        return True
    else:
        return serie.apply(test_func).sum() > proportion * len(serie)


def order_edges(edges):
    """Return an ordered traversal of the edges of a two-dimensional polygon.

    Parameters
    ----------
    edges: (2, N) ndarray
        List of unordered line segments, where each
        line segment is represented by two unique
        vertex codes.

    Returns
    -------
    ordered_edges: (2, N) ndarray

    """
    edge = edges[0]
    edges = edges[1:]
    ordered_edges = [edge]
    num_max = len(edges)
    while len(edges) > 0 and num_max > 0:
        match = edge[1]
        for search_edge in edges:
            vertex = search_edge[0]
            if match == vertex:
                edge = search_edge
                edges.remove(edge)
                ordered_edges.append(search_edge)
                break
        num_max -= 1
    return ordered_edges


def forg(x, prec=3):
    """
    Yanked from the code for Statsmodels\\iolib\\summary.py
    and adjusted
    """
    if prec == 3:
        if abs(x) >= 10000.0 or abs(x) < 0.0001:
            return '%9.3g' % x
        else:
            return '%9.3f' % x
    elif prec == 4:
        if abs(x) >= 10000.0 or abs(x) < 0.0001:
            return '%10.4g' % x
        else:
            return '%10.4f' % x
    else:
        raise NotImplementedError


def get_tols(method, tol):
    """
    quick function to set the tolerances as a dictionary depending on the method being used
    :param method: str: method to use
    :param tol: tolerance as a float
    :return:
    """
    if method in ['leastsq', 'least_squares']:
        tols = {'ftol': tol, 'xtol': tol}
    else:
        tols = {'ftol': tol}
    return tols


def load_content_dict_line(file, delimiter, startswith, sec_delimiter,
    strip_line=True, replace_old=None, replace_new=None):
    """
    This function load file content in a dictionary spliting the line in two (key-value)
    :param file: File to parse
    :param delimiter: Delimiter to split line
    :param strip_line: Strip line before splitting
    :param replace_old: Replace substring  replace_old with replace_new before splitting
    :param replace_new: Replace substring  replace_old with replace_new before splitting
    :param startswith: Parse line if start with startswith
    :return: Dict with file content
    """
    result = {}
    with open(file) as fin:
        for line in fin:
            if line.startswith(startswith):
                if strip_line:
                    line = line.strip()
                if replace_old:
                    line = line.replace(replace_old, replace_new)
                fields = line.split(delimiter)
                if len(fields) == 2:
                    result[fields[0].strip()] = fields[1].strip().split(
                        sec_delimiter)[0]
    return result


def extrude_region_move(MESH_OT_extrude_region=None, TRANSFORM_OT_translate
    =None):
    """Extrude region and move result

    :param MESH_OT_extrude_region: Extrude Region, Extrude region of faces
    :param TRANSFORM_OT_translate: Move, Move selected items
    """
    pass


def PolsAccHosp(t):
    """Number of policies: Accidental Hospitalization"""
    return 0


def downgrade():
    """Downgrade database."""
    pass


def is_valid_joint_degree(joint_degrees):
    """ Checks whether the given joint degree dictionary is realizable.

    A *joint degree dictionary* is a dictionary of dictionaries, in
    which entry ``joint_degrees[k][l]`` is an integer representing the
    number of edges joining nodes of degree *k* with nodes of degree
    *l*. Such a dictionary is realizable as a simple graph if and only
    if the following conditions are satisfied.

    - each entry must be an integer,
    - the total number of nodes of degree *k*, computed by
      ``sum(joint_degrees[k].values()) / k``, must be an integer,
    - the total number of edges joining nodes of degree *k* with
      nodes of degree *l* cannot exceed the total number of possible edges,
    - each diagonal entry ``joint_degrees[k][k]`` must be even (this is
      a convention assumed by the :func:`joint_degree_graph` function).

    Parameters
    ----------
    joint_degrees :  dictionary of dictionary of integers
        A joint degree dictionary in which entry ``joint_degrees[k][l]``
        is the number of edges joining nodes of degree *k* with nodes of
        degree *l*.

    Returns
    -------
    bool
        Whether the given joint degree dictionary is realizable as a
        simple graph.

    References
    ----------
    .. [1] M. Gjoka, M. Kurant, A. Markopoulou, "2.5K Graphs: from Sampling
       to Generation", IEEE Infocom, 2013.
    .. [2] I. Stanton, A. Pinar, "Constructing and sampling graphs with a
       prescribed joint degree distribution", Journal of Experimental
       Algorithmics, 2012.
    """
    degree_count = {}
    for k in joint_degrees:
        if k > 0:
            k_size = sum(joint_degrees[k].values()) / k
            if not k_size.is_integer():
                return False
            degree_count[k] = k_size
    for k in joint_degrees:
        for l in joint_degrees[k]:
            if not float(joint_degrees[k][l]).is_integer():
                return False
            if k != l and joint_degrees[k][l] > degree_count[k] * degree_count[
                l]:
                return False
            elif k == l:
                if joint_degrees[k][k] > degree_count[k] * (degree_count[k] - 1
                    ):
                    return False
                if joint_degrees[k][k] % 2 != 0:
                    return False
    return True


@classmethod
def merge(cls, spans):
    """Merges overlapping and touches spans in the given list of spans.

        Note that this modifies the original list.

        >>> spans = [Span(1,2), Span(3)]
        >>> Span.merge(spans)
        >>> spans
        [<1-3>]
        """
    i = 0
    while i < len(spans) - 1:
        here = spans[i]
        j = i + 1
        while j < len(spans):
            there = spans[j]
            if there.start > here.end + 1:
                break
            if here.touches(there) or here.overlaps(there):
                here = here.to(there)
                spans[i] = here
                del spans[j]
            else:
                j += 1
        i += 1
    return spans


@staticmethod
def get_setclause(columnsname):
    """
            DumpSQLUtils.get_setclause()

            create a string (a "SET clause") suitable for a SQL request.
        """
    setstring = []
    for columnsname_key in columnsname:
        setstring.append('{0} = ?'.format(columnsname_key))
    setstring = ', '.join(setstring)
    return 'SET ' + setstring


def encode_body(body, frame_size):
    """Generate a sequence of chunks for body where each chunk is less than frame_size"""
    limit = frame_size - 7 - 1
    while body:
        payload, body = body[:limit], body[limit:]
        yield 3, payload


def create(db, model, create_method='', create_method_kwargs=None, **kwargs):
    """Create a single model object."""
    kwargs.update(create_method_kwargs or {})
    created = getattr(model, create_method, model)(**kwargs)
    db.add(created)
    db.flush()
    return created, True


def add_problem_hparams(hparams, problem_name, model_dir, problem_instance):
    """Add problem hparams for the problems."""
    hparams.problems = []
    hparams.problem_instances = []
    p_hparams = problem_instance.get_hparams(hparams)
    hparams.problem_instances.append(problem_instance)
    hparams.problems.append(p_hparams)


def get_fqdn(cls):
    """
    Returns a fully-qualified name for the given class
    """
    return cls.__module__ + '.' + cls.__name__


def extent(xll, yll, cellsize, nrows, ncols, scale=1.0, registration=
    'gridlines'):
    """
    Return the extent (xmin,xmax,ymin,ymax) of an image given the coordinates of
    the lower-left corner, the cellsize and the numbers of rows and columns.
    Registration option controls whether the coordinates indicate the position
    of the centre of the pixels ('pixels') or the corner ('gridlines').

    Returns
    -------
    (xmin,xmax,ymin,ymax)
    """
    if registration == 'gridlines':
        xmin = xll * scale
        xmax = (xll + (ncols - 1) * cellsize) * scale
        ymin = yll * scale
        ymax = (yll + (nrows - 1) * cellsize) * scale
    else:
        xmin = (xll - cellsize / 2.0) * scale
        xmax = (xmin + ncols * cellsize) * scale
        ymin = (yll - cellsize / 2.0) * scale
        ymax = (ymin + nrows * cellsize) * scale
    return xmin, xmax, ymin, ymax


@classmethod
def usable_id(cls, id):
    """ Retrieve id from input which can be ISO, name, country, dc_code."""
    try:
        qry_id = cls.from_dc_code(id)
        if not qry_id:
            qry_id = cls.from_iso(id)
            if qry_id:
                cls.deprecated(
                    'ISO code for datacenter filter use dc_code instead')
        if not qry_id:
            qry_id = cls.from_country(id)
        if not qry_id:
            qry_id = int(id)
    except Exception:
        qry_id = None
    if not qry_id:
        msg = 'unknown identifier %s' % id
        cls.error(msg)
    return qry_id


@classmethod
def process(cls, *, context=None, parameter=None):
    """ Evaluate the RETURN command. """
    context.counter = context.callstack.pop()


@staticmethod
def to_romer(delisle):
    """ Convert delisle to romer """
    return 60 - delisle * 7 / 20


def normalize_ship_name(ship_name: str) ->str:
    """
    Removes the redundant "trip name" after the ship name.

    Args:
        ship_name: The raw string, e.g. 'BOATY MCBOATFACE - MCBF1234'
    Returns:
        The normalized name, e.g. 'BOATY MCBOATFACE'
    """
    if ' - ' not in ship_name:
        return ship_name
    return ship_name.split(' - ')[0]


def _instance_changed(sender, instance, **kwargs):
    """
    Queues an update to the ElasticSearch index.
    """
    from rest_search import queue_add
    doc_type = instance.__class__.__name__
    queue_add({doc_type: [instance.pk]})


@classmethod
def from_data(cls, data):
    """Construct a mesh from structured data.

        Parameters
        ----------
        data : dict
            The data dictionary.

        Returns
        -------
        object
            An object of the type of ``cls``.

        Note
        ----
        This constructor method is meant to be used in conjuction with the
        corresponding *to_data* method.

        """
    mesh = cls()
    mesh.data = data
    return mesh


@classmethod
def Xaxis(cls):
    """Construct a unit vector along the X axis.

        Returns
        -------
        :class:`compas.geometry.Vector`
            A vector with components ``x = 1.0, y = 0.0, z = 0.0``.

        Examples
        --------
        >>> Vector.Xaxis()
        Vector(1.000, 0.000, 0.000)
        """
    return cls(1.0, 0.0, 0.0)


def parse_script_args(args):
    """
	Separate the command line arguments into arguments for pip
	and arguments to Python.

	>>> parse_script_args(['foo', '--', 'bar'])
	(['foo'], ['bar'])

	>>> parse_script_args(['foo', 'bar'])
	(['foo', 'bar'], [])
	"""
    try:
        pivot = args.index('--')
    except ValueError:
        pivot = len(args)
    return args[:pivot], args[pivot + 1:]


def getBeforeCommitHooks():
    """Return iterable producing the registered `addBeforeCommitHook` hooks.

        A triple ``(hook, args, kws)`` is produced for each registered hook.
        The hooks are produced in the order in which they would be invoked
        by a top-level transaction commit.
        """


def setfpl(jobj, fpl, value):
    """
        y = {'c': {'b': 250}}
        setfpl(y,['c','b'],200)
        y
        >>>200
    """
    this = jobj
    lngth = fpl.__len__() - 1
    for i in range(0, lngth):
        key = fpl[i]
        this = this.__getitem__(key)
    this.__setitem__(fpl[-1], value)
    return jobj


def get_rows_to_write(worksheet):
    """Return all rows, and any cells that they contain"""
    rows = {}
    for (row, col), cell in worksheet._cells.items():
        rows.setdefault(row, []).append((col, cell))
    for row_idx in worksheet.row_dimensions:
        if row_idx not in rows:
            rows[row_idx] = []
    return sorted(rows.items())


def spmultiply(a, b, array_out=True):
    """
    Element-wise multiplication function to deal with sparse and dense
    objects. Both objects must be of the same type.

    Parameters
    ----------
    a           : array
                  first multiplication factor. Can either be sparse or dense.
    b           : array
                  second multiplication factor. Can either be sparse or dense.
                  integer.
    array_out   : boolean
                  If True (default) the output object is always a np.array

    Returns
    -------
    ab : array
         elementwise multiplied object. Sparse if a is sparse. Dense otherwise.
    """
    if type(a).__name__ == 'ndarray' and type(b).__name__ == 'ndarray':
        ab = a * b
    elif (type(a).__name__ == 'csr_matrix' or type(a).__name__ == 'csc_matrix'
        ) and (type(b).__name__ == 'csr_matrix' or type(b).__name__ ==
        'csc_matrix'):
        ab = a.multiply(b)
        if array_out:
            if type(ab).__name__ == 'csc_matrix' or type(ab
                ).__name__ == 'csr_matrix':
                ab = ab.toarray()
    else:
        raise Exception(
            "Invalid format for 'spmultiply' argument: %s and %s" % (type(a
            ).__name__, type(b).__name__))
    return ab


def read_lwostring(raw_name):
    """Parse a zero-padded string."""
    i = raw_name.find(b'\x00')
    name_len = i + 1
    if name_len % 2 == 1:
        name_len += 1
    if i > 0:
        name = raw_name[0:i].decode('utf-8', 'ignore')
    else:
        name = ''
    return name, name_len


def p_character_digit(p):
    """character : K_DIGIT
                 | K_DIGIT K_FROM NUMBER K_TO NUMBER
    """
    p[0] = p[0] or []
    if len(p) == 6:
        num_from = p[3]
        num_to = p[5]
    else:
        num_from = 0
        num_to = 9
    p[0].append(('digit', (num_from, num_to)))


def plot_fleur_band_wc(node, labels=None):
    """
    This methods takes an AiiDA output parameter node from a band structure
    workchain and plots a simple band structure
    """
    from masci_tools.vis.plot_methods import plot_bands
    if labels is None:
        labels = []
    if isinstance(node, list):
        if len(node) > 2:
            return
        else:
            node = node[0]
    output_d = node.get_dict()
    path_to_bands_file = output_d.get('bandfile', None)
    print(path_to_bands_file)
    kpath = output_d.get('kpath', {})
    if path_to_bands_file:
        plot_bands(path_to_bands_file, kpath)
    else:
        print('Could not retrieve dos file path from output node')


def get_element_at(element_idx, it):
    """ :return it[element_idx] also for non-indexable iterators. NOTE: exhausts iterator! """
    try:
        return it[element_idx]
    except TypeError:
        pass
    for i, el in enumerate(it):
        if i == element_idx:
            return el
    raise IndexError('Iterator does not have {} elements'.format(element_idx))


@classmethod
def make_views(cls, conn):
    """Create day_trips and day_stop_times views.

        day_trips:  day_trips2 x trips  = days x trips
        day_stop_times: day_trips2 x trips x stop_times = days x trips x stop_times
        """
    conn.execute('DROP VIEW IF EXISTS main.day_trips')
    conn.execute(
        'CREATE VIEW day_trips AS   SELECT day_trips2.*, trips.* FROM day_trips2 JOIN trips USING (trip_I);'
        )
    conn.commit()
    conn.execute('DROP VIEW IF EXISTS main.day_stop_times')
    conn.execute(
        'CREATE VIEW day_stop_times AS   SELECT day_trips2.*, trips.*, stop_times.*, day_trips2.day_start_ut+stop_times.arr_time_ds AS arr_time_ut, day_trips2.day_start_ut+stop_times.dep_time_ds AS dep_time_ut   FROM day_trips2 JOIN trips USING (trip_I) JOIN stop_times USING (trip_I)'
        )
    conn.commit()


def cols_to_percentile(df, columns):
    """Convert data points to their percentile linearized value and return new columns of prefixed data.

    Args:
        df: Pandas dataframe.
        columns: List of columns to transform.

    Returns:
        Original dataframe with additional prefixed columns.
    """
    for col in columns:
        df['pc_lin_' + col] = df[col].rank(method='min').apply(lambda x: (x -
            1) / len(df[col]) - 1)
    return df


def _missing_fields(record):
    """
    Accepts a record from a csv.DictReader object and determines if any
    fields have been filled in with None by the DictReader. Returns a list of
    field names that have been filled in with None type, or false if all fields are
    present.
    """
    missing_fields = False
    for key in record:
        if record[key] is None:
            if not missing_fields:
                missing_fields = []
            missing_fields.append(key)
    return missing_fields


def get_tiled_image_dimensions(grid_size, image_size):
    """

    An image consisting of tiles of itself (or same-sized) images
    will be as close to the same dimensions as the original.

    This returns two tuples - the size of the final output image, and the size of the
    tiles that it will consist of.

    :param grid_size: A 2-tuple (width, height) defining the shape of the grid (in number of images)
    :param image_size: A 2-tuple (width, height) defining the shape of the final image (in pixels)
    :return: two 2-tuples, the size of each tile and the size of the final output image/
    """
    tile_width = image_size[0] / grid_size[0]
    tile_height = image_size[1] / grid_size[0]
    final_height = tile_height * grid_size[1]
    return (tile_width, tile_height), (image_size[0], final_height)


@classmethod
def open_local(cls, blob_filename, username, password):
    """Creates a vault from a locally stored blob"""
    raise NotImplementedError()


def calculate_array_indents(indent=2, last_level=0):
    """
    Determine the start and end indents for a sub-array.
    :param indent: How much are we indenting on each go
    :param last_level: Recursion depth of the calling function
    :return: [int, int]
    """
    val = indent
    if last_level > 0:
        val *= last_level
        val += indent
    return [indent, 0] if last_level == 0 else [indent * last_level +
        indent, indent * last_level]


def response_available():
    """No-op placeholder for easy hookage"""
    pass


def progress_bar(done, total, length=10, bar='#', sep='-'):
    """
    Return a string indicating graphically the percentage of the first argument
    to the second

    >>> progress_bar(3, 6)
    "#####-----"
    """
    fraction = float(done) / total
    bars = int(fraction * length)
    return '{}{}'.format(str(bar) * bars, str(sep) * (length - bars))


@staticmethod
def check_module(target_configuration):
    """ Perform any module specific checks now that the target
        configuration is complete.  target_configuration is the target
        configuration.
        """


def _warn_keyword_parameter(func_name, kwargs):
    """
    Backwards-compatibility with Python 2.7 to allow treating 'warn' as keyword-only.
    """
    if not kwargs:
        return False
    elif len(kwargs) > 1 or 'warn' not in kwargs:
        kwargs.pop('warn', None)
        arg = next(iter(kwargs.keys()))
        raise TypeError('{}() got an unexpected keyword argument {!r}'.
            format(func_name, arg))
    return kwargs['warn']


@classmethod
def load_pkcs1_openssl_der(cls, keyfile):
    """Loads a PKCS#1 DER-encoded public key file from OpenSSL.

        :param keyfile: contents of a DER-encoded file that contains the public
            key, from OpenSSL.
        :return: a PublicKey object
        :rtype: bytes

        """
    from rsa.asn1 import OpenSSLPubKey
    from pyasn1.codec.der import decoder
    from pyasn1.type import univ
    keyinfo, _ = decoder.decode(keyfile, asn1Spec=OpenSSLPubKey())
    if keyinfo['header']['oid'] != univ.ObjectIdentifier('1.2.840.113549.1.1.1'
        ):
        raise TypeError(
            'This is not a DER-encoded OpenSSL-compatible public key')
    return cls._load_pkcs1_der(keyinfo['key'][1:])


def dread(infile, cols):
    """
     reads in specimen, tr, dec, inc int into data[].  position of
     tr, dec, inc, int determined by cols[]
    """
    data = []
    f = open(infile, 'r')
    for line in f.readlines():
        tmp = line.split()
        rec = tmp[0], float(tmp[cols[0]]), float(tmp[cols[1]]), float(tmp[
            cols[2]]), float(tmp[cols[3]])
        data.append(rec)
    f.close()
    return data


@classmethod
def set_rate_limit(cls, limit):
    """Set the rate limit in kilobytes per second."""
    cls.block_size = int(1024 * limit * cls.INTERVAL_LEN)


def add_marker(location: float=(0.0, 0.0)):
    """Place new marker at specified location

    :param location: Location, Location of marker on frame
    :type location: float
    """
    pass


def map_range(x, X_min, X_max, Y_min, Y_max):
    """
    Linear mapping between two ranges of values
    """
    X_range = X_max - X_min
    Y_range = Y_max - Y_min
    XY_ratio = X_range / Y_range
    y = ((x - X_min) / XY_ratio + Y_min) // 1
    return int(y)


def flowStopped(reason):
    """
        The flow has stopped.  The given L{Failure} object indicates why.
        After a L{IFount} invokes this method, it must stop invoking all other
        methods on this L{IDrain}; similarly, this L{IDrain} must stop invoking
        all methods on its L{IFount}.

        @param reason: The reason why the flow has terminated.  This may
            contain any exception type, depending on the L{IFount} delivering
            data to this L{IDrain}.
        @type reason: L{Failure}
        """


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


def policy_vtest():
    """Autoaugment test policy for debugging."""
    policy = [[('TranslateX', 1.0, 4), ('Equalize', 1.0, 10)]]
    return policy


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


def filter_glyph_names(alist, filter):
    """filter `alist' by taking _out_ all glyph names that are in `filter'"""
    count = 0
    extras = []
    for name in alist:
        try:
            filtered_index = filter.index(name)
        except:
            extras.append(name)
    return extras


@classmethod
def empty_stats(cls):
    """Returns empty statistics object."""
    return cls(pages=0, percentage=0)


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 version_from_key(name_version_pairs, default_version=None):
    """
    Some UA strings specify the app version in a separate Version/<float> pair.
    Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) News/582.1 Version/2.0
    If so, return that version number.

    :param name_version_pairs:  [('news', 'News', '582.1'), ('version', 'Version', '2.0')]
    :param default_version: Default version if not found in name_version_pairs
    """
    for code, name, version in name_version_pairs:
        if code == 'version':
            return version
    return default_version


def guess_stream_needs_encoding(fileobj, default=True):
    """
    Guess the type (bytes/unicode) of this stream, and return whether or not it
    requires text to be encoded before written into it.
    """
    try:
        t = type(fileobj.read(0))
        if t is bytes:
            return True
        elif t is str:
            return False
    except Exception:
        pass
    try:
        mode = fileobj.mode
        if 'b' in mode:
            return True
        else:
            return False
    except Exception:
        pass
    return default


@staticmethod
def create_install_requires(requires_list):
    """
        Used to create the text content for the "install requires" portion of the Python
        setup.py file

        :param requires_list: The list of required packages
        :return: The text content for the "install requires" portion of the Python setup.py file
        """
    ret = ''
    first = True
    for req in requires_list:
        ret += """{1}
        "{0}\"""".format(req, '' if first else ',')
        first = False
    return ret


def hal_report_to_terminal(report, base_indent=0):
    """Yield lines from the HalsteadReport to print to the terminal."""
    yield 'h1: {}'.format(report.h1), (), {'indent': 1 + base_indent}
    yield 'h2: {}'.format(report.h2), (), {'indent': 1 + base_indent}
    yield 'N1: {}'.format(report.N1), (), {'indent': 1 + base_indent}
    yield 'N2: {}'.format(report.N2), (), {'indent': 1 + base_indent}
    yield 'vocabulary: {}'.format(report.vocabulary), (), {'indent': 1 +
        base_indent}
    yield 'length: {}'.format(report.length), (), {'indent': 1 + base_indent}
    yield 'calculated_length: {}'.format(report.calculated_length), (), {
        'indent': 1 + base_indent}
    yield 'volume: {}'.format(report.volume), (), {'indent': 1 + base_indent}
    yield 'difficulty: {}'.format(report.difficulty), (), {'indent': 1 +
        base_indent}
    yield 'effort: {}'.format(report.effort), (), {'indent': 1 + base_indent}
    yield 'time: {}'.format(report.time), (), {'indent': 1 + base_indent}
    yield 'bugs: {}'.format(report.bugs), (), {'indent': 1 + base_indent}


@classmethod
def validate(cls, **kwargs):
    """Validate the kwargs and return cleaned data or raise a HTTPError."""
    data = {}
    for key, value in kwargs.items():
        if cls.fields[key].get('multiple'):
            values = value
            for validator in cls.fields[key].get('list_validators', []):
                values = validator.validate(values)
        else:
            values = [value]
        for value in values:
            for validator in cls.fields[key]['validators']:
                value = validator.validate(value)
        if cls.fields[key].get('multiple'):
            data[key] = values
        else:
            data[key] = values[0]
    return data


@staticmethod
def _build_char_trie(lexicon):
    """
        Builds a char trie, to cater for wildcard ('*') matches.
        """
    trie = {}
    for pattern, cat_names in lexicon.items():
        cursor = trie
        for char in pattern:
            if char == '*':
                cursor['*'] = cat_names
                break
            if char not in cursor:
                cursor[char] = {}
            cursor = cursor[char]
        cursor['$'] = cat_names
    return trie


def pdb_atom_parse(line):
    """
    Extract data from an ATOM or HETATM line.
    """
    if '\n' in line:
        line = line[:line.index('\n')]
    line = line.ljust(80)
    result = {}
    result['serial'] = int(line[6:11])
    result['name'] = line[12:16]
    result['altLoc'] = line[16]
    result['resName'] = line[17:20]
    result['chainID'] = line[21]
    result['resSeq'] = int(line[22:26])
    result['iCode'] = line[26]
    result['x'] = float(line[30:38]) * 0.1
    result['y'] = float(line[38:46]) * 0.1
    result['z'] = float(line[46:54]) * 0.1
    try:
        result['occupancy'] = float(line[54:60])
    except:
        result['occupancy'] = 1.0
    try:
        result['tempFactor'] = float(line[60:66])
    except:
        result['tempFactor'] = 0.0
    result['element'] = line[76:78].strip()
    if result['element'] == '':
        result['element'] = result['name'].lstrip(' 0123456789')[0]
    result['charge'] = line[78:80]
    return result


def yieldroutes(func):
    """ Return a generator for routes that match the signature (name, args)
    of the func parameter. This may yield more than one route if the function
    takes optional keyword arguments. The output is best described by example::

        a()         -> '/a'
        b(x, y)     -> '/b/:x/:y'
        c(x, y=5)   -> '/c/:x' and '/c/:x/:y'
        d(x=5, y=6) -> '/d' and '/d/:x' and '/d/:x/:y'
    """
    import inspect
    path = '/' + func.__name__.replace('__', '/').lstrip('/')
    spec = inspect.getargspec(func)
    argc = len(spec[0]) - len(spec[3] or [])
    path += '/:%s' * argc % tuple(spec[0][:argc])
    yield path
    for arg in spec[0][argc:]:
        path += '/:%s' % arg
        yield path


def setInstanceVersion(version):
    """The version this instance of Plone is on."""


def _normalize_integer_rgb(value):
    """
    Internal normalization function for clipping integer values into
    the permitted range (0-255, inclusive).

    """
    return 0 if value < 0 else 255 if value > 255 else value


def ancestor_finder(resource, predicate, include_self=False):
    """Generate ancestors that satisfy ``predicate``.

    Generator that climbs the tree yielding resources for which
    ``predicate(current_resource)`` returns True.
    """
    resource = resource if include_self else getattr(resource, '__parent__',
        None)
    while resource is not None:
        if predicate(resource):
            yield resource
        resource = getattr(resource, '__parent__', None)


def is_bullet_list_item(lines, index):
    """check if it's a bullet list item"""
    bullets = ['*', '-', '+', '•', '‣', '⁃']
    first_char = lines[index].lstrip().split(' ')[0]
    if first_char in bullets:
        return True
    return False


def read_file(filename):
    """
    Open a file, read and return its data as a string.
    """
    f = open(filename, 'rb')
    raw_data = f.read()
    f.close()
    return raw_data


def getHost():
    """
        Returns L{IPv4Address} or L{IPv6Address}.
        """


@classmethod
def decode_pdf(cls, simula_pdf_file_dict):
    """
        The reverse of :meth:`encode_pdf`.
        """
    return {'filename': simula_pdf_file_dict['filename'], 'data': cls.
        decode_pdfdata(simula_pdf_file_dict['base64data']), 'content_type':
        simula_pdf_file_dict['content_type']}


def set_permutation_symmetry(force_constants):
    """Enforce permutation symmetry to force cosntants by

    Phi_ij_ab = Phi_ji_ba

    i, j: atom index
    a, b: Cartesian axis index

    This is not necessary for harmonic phonon calculation because this
    condition is imposed when making dynamical matrix Hermite in
    dynamical_matrix.py.

    """
    fc_copy = force_constants.copy()
    for i in range(force_constants.shape[0]):
        for j in range(force_constants.shape[1]):
            force_constants[i, j] = (force_constants[i, j] + fc_copy[j, i].T
                ) / 2


@classmethod
def get_exchange_name(cls):
    """
        Get exchange name
        :return: Exchange name string
        """
    return 'Template'


def moved(object, oldParent, oldName, newParent, newName):
    """Send an :class:`~.IObjectMovedEvent` for ``object``.

        ``oldParent`` is the container ``object`` was removed from.
        ``oldName`` was the name used to store ``object`` in ``oldParent``.
        ``newParent`` is the container ``object`` was added to.
        ``newName`` is the name used to store ``object`` in ``newParent``.

        Note that ``newParent`` and ``oldParent`` may be the same if the names
        are different, and vice versa.

        """


def parse_link_label(parser, content):
    """parse link's label in format: [label] (link), return the position of the end
    of label, None if cannot be satisfied"""
    start_pos = content.pos
    if content.peek() != '[':
        return None
    content.advance()
    level = 1
    ret = None
    while not content.is_end():
        if content.peek() == ']':
            content.advance()
            level -= 1
            if level == 0:
                break
        old_pos = content.pos
        parser.skip_token(content)
        if content.peek() == '[':
            content.advance()
            level += 1
        if content.peek() != ']' and content.pos == old_pos:
            content.advance()
    if level == 0:
        ret = content.string[start_pos + 1:content.pos - 1]
    else:
        content.pos = start_pos
    return ret


def write_entry_points(d, fp):
    """Write entry_points.txt from a two-level dict

    Sorts on keys to ensure results are reproducible.
    """
    for group_name in sorted(d):
        fp.write(u'[{}]\n'.format(group_name))
        group = d[group_name]
        for name in sorted(group):
            val = group[name]
            fp.write(u'{}={}\n'.format(name, val))
        fp.write(u'\n')


def convert_units(value, in_units, out_units, standard='codata2014'):
    """Convert a value, from one unit to another."""
    if in_units == out_units:
        return value
    if standard != 'codata2014':
        raise NotImplementedError('{}'.format(standard))
    if in_units == 'hartree' and out_units == 'eV':
        return value * 27.21138602
    elif in_units == 'bohr' and out_units == 'angstrom':
        return value * 0.5291772105638411
    raise NotImplementedError('{} -> {}'.format(in_units, out_units))


def findRelationshipTokenChains(query, maxDepth=None, filter=None,
    targetQuery=None, targetFilter=None, transitiveQueriesFactory=None):
    """find tuples of relationship tokens for searchTerms.
        - query is a dictionary of {indexName: token}
        - maxDepth is None or a positive integer that specifies maximum depth
          for transitive results.  None means that the transitiveMap will be
          followed until a cycle is detected.  It is a ValueError to provide a
          non-None depth if transitiveQueriesFactory is None and
          index.defaultTransitiveQueriesFactory is None.
        - filter is a an optional callable providing IFilter that determines
          whether relationships will be traversed at all.
        - targetQuery is an optional query that specifies that only paths with
          final relationships that match the targetQuery should be returned.
          It represents a useful subset of the jobs that can be done with the
          targetFilter.
        - targetFilter is an optional callable providing IFilter that
          determines whether a given path will be included in results (it will
          still be traversed)
        - optional transitiveQueriesFactory takes the place of the index's
          defaultTransitiveQueriesFactory
        """


@staticmethod
def plugin_name(plugin):
    """Discover the plugin name and return it."""
    return plugin.__class__.__name__.lower()


@staticmethod
def sort_edge(edges):
    """Sort iterable of edges first by left node indices then right.

        Args:
            edges (list[Edge]):
                List of edges to be sorted.

        Returns:
            list[Edge]:
                Sorted list by left and right node indices.
        """
    return sorted(edges, key=lambda x: (x.L, x.R))


@classmethod
def add_parser_arguments(cls, parser, interpreter):
    """
        Add CLI arguments for this activation script.

        :param parser: the CLI parser
        :param interpreter: the interpreter this virtual environment is based of
        """


def glLoadMatrix(m: 'Buffer'):
    """Replace the current matrix with the specified matrix

    :param m: Specifies a pointer to 16 consecutive values, which are used as the elements of a 4x4 column-major matrix.
    :type m: 'Buffer'
    """
    pass


def double_sort(pathways_dictionary):
    """
    Return the keys to a dictionary sorted with top values first
    then for duplicate values sorted alphabetically by key
    """
    sorted_keys = []
    prior_value = ''
    store = []
    for pathway in sorted(pathways_dictionary, key=pathways_dictionary.get,
        reverse=True):
        if prior_value == pathways_dictionary[pathway]:
            if not store:
                store.append(sorted_keys.pop())
            store.append(pathway)
        else:
            if store:
                sorted_keys += sorted(store)
                store = []
            prior_value = pathways_dictionary[pathway]
            sorted_keys.append(pathway)
    if store:
        sorted_keys += sorted(store)
    return sorted_keys


def convert_eq_to_dict(equationstring):
    """
    Converts an equation string to a dictionary
    convert_eq_to_dict('1*Be12Ti->10*Be+1*Be2Ti+5*Be') ->
    {'products': {'Be': 15, 'Be2Ti': 1}, 'educts': {'Be12Ti': 1}}
    """
    eq_dict = {'products': {}, 'educts': {}}
    product_dict = {}
    educt_dict = {}
    eq_split = equationstring.split('->')
    products = eq_split[1].split('+')
    educts = eq_split[0].split('+')
    for product in products:
        p_list = product.split('*')
        product_dict[p_list[-1]] = int(p_list[0]) + product_dict.get(p_list
            [-1], 0)
    for educt in educts:
        e_list = educt.split('*')
        educt_dict[e_list[-1]] = int(e_list[0]) + educt_dict.get(e_list[-1], 0)
    eq_dict['products'] = product_dict
    eq_dict['educts'] = educt_dict
    return eq_dict


@staticmethod
def _is_eligible_to_retry(task_instance):
    """Is task instance is eligible for retry"""
    return task_instance.try_number <= task_instance.max_tries


def check_for_unsupported_flag(args):
    """Raise an error if the provider doesn't support a provided flag."""
    if args.label and args.provider not in ['test-fails', 'local', 'google',
        'google-v2', 'google-cls-v2']:
        raise ValueError('--label is not supported by the "%s" provider.' %
            args.provider)


def is_empty(my_string):
    """Is the string empty"""
    return not (my_string and my_string.strip())


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 is_coercible_exception(exception: Exception) ->bool:
    """
    Determines whether or not the exception is coercible.
    :param exception: exception to check
    :type exception: Any
    :return: whether or not the exception is coercible
    :rtype: bool
    """
    return hasattr(exception, 'coerce_value') and callable(exception.
        coerce_value)


def remove_cvt_table(tt):
    """Removes cvt table from a fontTools.ttLib.TTFont object"""
    try:
        del tt['cvt ']
    except KeyError:
        pass


def out_of_China(lgt, lat):
    """
    国内现有的坐标加密政策，并非严格按照边境线判断是否坐标是否位于国内，而是以BoundingBox的方式粗略地进行判断，
    位于BoundingBox内的进行加密，位于其外的则保持原样。
    :param lgt: 经度(WGS84坐标系)
    :param lat: 纬度(WGS84坐标系)
    :return: bool. 不在国内返回True，在国内返回False
    """
    return not (73.66 < lgt < 135.05 and 3.86 < lat < 53.55)


@classmethod
def create(cls, filename, metadata, version=None):
    """Create a new MBTiles file. See `Metadata`"""
    if version is None:
        version = cls.Metadata._detect(keys=list(metadata.keys()))
    mbtiles = cls._create(filename=filename, version=version)
    mbtiles.metadata._setup(metadata)
    return mbtiles


def set_signal_receivers(signal, receiver_list):
    """assigns a value of the receiver_list
    to the signal receivers
    """
    signal.receivers = receiver_list


def main() ->None:
    """Execute package entrypoint."""
    print('Test')


def handler_override(app):
    """
    This is a ``post_argument_parsing`` hook that overrides a configured
    handler if defined in ``CementApp.Meta.handler_override_options`` and
    the option is passed at command line with a valid handler label.

    :param app: The application object.

    """
    if app._meta.handler_override_options is None:
        return
    for i in app._meta.handler_override_options.keys():
        if not hasattr(app.pargs, '%s_handler_override' % i):
            continue
        elif getattr(app.pargs, '%s_handler_override' % i) is None:
            continue
        else:
            argument = getattr(app.pargs, '%s_handler_override' % i)
            setattr(app._meta, '%s_handler' % i, argument)
            getattr(app, '_setup_%s_handler' % i)()


def MIMAG_quality_level(row):
    """
    https://doi.org/10.1038/nbt.3893
    """
    if row['Completeness'] > 90.0 and row['Contamination'] < 5.0:
        return 'high_quality'
    elif row['Completeness'] > 50.0 and row['Contamination'] < 10.0:
        return 'medium_quality'
    else:
        return 'low_quality'


@staticmethod
def fit_category_nans(df):
    """ONLY FIT: Convert np.NaNs to a category 'NaN'"""
    for column in df.select_dtypes(include=['category']).columns:
        if df[column].isnull().any():
            df[column].cat.add_categories('NaN', inplace=True)
            df[column].fillna('NaN', inplace=True)


@staticmethod
def _parse_http_url(url):
    """http://xxxx/xx/xx.git"""
    _, uri = url[len('http://'):].split('/', 1)
    return uri


def _to_hass_level(level):
    """Convert the given Skybell (0-100) light level to Home Assistant (0-255)."""
    return int(level * 255 / 100)


def absolute_output_offsets_to_relative(off):
    """
    Relative offsets, prev + cur = next.
    Helps with varint encoding size.
    :param off:
    :return:
    """
    if len(off) == 0:
        return off
    res = sorted(off)
    for i in range(len(off) - 1, 0, -1):
        res[i] -= res[i - 1]
    return res


def get_initial_contact(request):
    """Fill in initial contact form fields from request."""
    initial = {}
    if request.user.is_authenticated:
        initial['name'] = request.user.full_name
        initial['email'] = request.user.email
    return initial


@classmethod
def find_distinct_values(cls, store, attr, exclude_empty=True):
    """Find distinct values for a given attr

        :param store: a store
        :param attr: the attr we are going to get distinct values for
        :param exclude_empty: if ``True``, empty results (``None`` or
            empty strings) will be removed from the results
        :returns: an iterator of the results
        """
    cls.validate_attr(attr)
    results = store.find(cls)
    results.config(distinct=True)
    for value in results.values(attr):
        if exclude_empty and not value:
            continue
        yield value


def intersection(A, D, tangA, tangD):
    """returns the intersection parameters of two evens

    they are defined by:
      x(t) = A + t * tangA
      x(s) = D + s * tangD
    """
    det = -tangA[0] * tangD[1] + tangA[1] * tangD[0]
    try:
        1.0 / det
    except ArithmeticError:
        return None, None
    DA = D[0] - A[0], D[1] - A[1]
    t = (-tangD[1] * DA[0] + tangD[0] * DA[1]) / det
    s = (-tangA[1] * DA[0] + tangA[0] * DA[1]) / det
    return t, s


def get_title_art(ArtNr, default=''):
    """Returns the titel of the art in the Vlaamse Codex.

    :param ArtNr: number of the artikel
    :param default: value if no title is returned
    """
    try:
        return 'Artikel: %s' % ArtNr
    except:
        return default


def _check_keys(data, keys):
    """Verify that the given data dict contains the specified keys."""
    for key in keys:
        if not key in data:
            msg = 'missing %s in data - %s' % (key, str(data.keys()))
            raise ValueError(msg)


def channels_ungroup():
    """Remove selected F-Curves from their current groups

    """
    pass


def func_get(parser, name):
    """Returns the variable ``name`` (equivalent to ``%name%``)."""
    if name.startswith('_'):
        name = '~' + name[1:]
    return parser.context.get(name, '')


@staticmethod
def elementEndTime(el, unused_node):
    """
        Use so that both OffsetTrees, which have elements which do not have a .endTime, and
        TimespanTrees, which have element that have an .endTime but not a duration, can
        use most of the same code.
        """
    return el.endTime


@classmethod
def create(cls, address):
    """ Create server instance unless already exists.

        Server instance is created for unique host / port pairs. For example,
        calling code below will produce two instances:

            >>> Server.create(('127.0.0.1', 8080))
            >>> Server.create(('127.0.0.1', 8081))

        The same service can be mounted under both servers.
        """
    host, port = address
    if cls._instances is None:
        cls._instances = dict()
    if host not in cls._instances:
        cls._instances[host] = dict()
    if port not in cls._instances[host]:
        cls._instances[host][port] = cls(address)
    return cls._instances[host][port]


@classmethod
def _GetValidationConfig(cls):
    """The L{pyxb.ValidationConfig} instance that applies to this class.

        By default this will reference L{pyxb.GlobalValidationConfig}."""
    return cls._validationConfig_


def deprecation(message):
    """For marking deprecated code."""
    import warnings
    warnings.warn(message, DeprecationWarning, stacklevel=2)


def GI_calc(AUC):
    """
    Calculate Gini index.

    :param AUC: AUC (Area under the ROC curve)
    :type AUC: float
    :return: Gini index as float
    """
    try:
        return 2 * AUC - 1
    except TypeError:
        return 'None'


def default_context(plugin, context):
    """
    Return the default context for plugins rendered with a template, which
    simply is a single variable named ``plugin`` containing the plugin
    instance.
    """
    return {'plugin': plugin}


def get_organism_screenids(session, organism, idr_base_url=
    'https://idr.openmicroscopy.org'):
    """
    Return a list of screen ids in IDR
    for given case insensitive organism. (Uses the json api)
    """
    screen_id_list = []
    qs = {'base': idr_base_url, 'key': 'organism', 'value': organism}
    screens_projects_url = '{base}/mapr/api/{key}/?value={value}'
    url = screens_projects_url.format(**qs)
    sr = session.get(url)
    sr.raise_for_status()
    for s in sr.json()['screens']:
        screen_id_list.append(str(s['id']))
    return screen_id_list


@classmethod
def _load_mx_exceptions(cls):
    """ Import mxODBC exception classes into the module namespace,
        as if they had been imported normally. This is done here
        to avoid requiring all SQLAlchemy users to install mxODBC.
        """
    global InterfaceError, ProgrammingError
    from mx.ODBC import InterfaceError
    from mx.ODBC import ProgrammingError


def acn(order=1):
    """Spherical harmonic degree `n` and order `m` for ambisonics order `order`.

    :param order: Ambisonics order.

    Yields tuples `(n, m)` where `n` is the degree and `m` the order.

    Follows ACN.

    === == == ======
    ACN n  m  letter
    === == == ======
    0   0  0  W
    --- -- -- ------
    1   1  -1 Y
    --- -- -- ------
    2   1  0  Z
    --- -- -- ------
    3   1  +1 X
    === == == ======

    """
    for n in range(order + 1):
        for m in range(-n, n + 1):
            yield n, m


def is_iterable(obj):
    """
    Are we being asked to look up a list of things, instead of a single thing?
    We check for the `__iter__` attribute so that this can cover types that
    don't have to be known by this module, such as NumPy arrays.

    Strings, however, should be considered as atomic values to look up, not
    iterables. The same goes for tuples, since they are immutable and therefore
    valid entries.

    We don't need to check for the Python 2 `unicode` type, because it doesn't
    have an `__iter__` attribute anyway.
    """
    return hasattr(obj, '__iter__') and not isinstance(obj, str
        ) and not isinstance(obj, tuple)


def java_ts_to_secs(java_ts):
    """
    Convert java timestamp into unix timestamp.

    :param java_ts: java timestamp in milliseconds.
    :type java_ts: int

    :return: Timestamp in seconds.
    :rtype: int
    """
    return java_ts / 1000


def default_predictor_scoring_fun(cls):
    """Return scores of how important a feature is to the prediction

    Most predictors score output coefficients in the variable
    cls.feature_importances_ and others may use another name for scores, so
    this function bridges the gap

    Parameters
    ----------
    cls : sklearn predictor
        A scikit-learn prediction class, such as ExtraTreesClassifier or
        ExtraTreesRegressor

    Returns
    -------
    scores : pandas.Series
        A (n_features,) size series of how important each feature was to the
        classification (bigger is better)
    """
    return cls.feature_importances_


def dec_identity(ob):
    """
    Identity decorator. If Numba is not available, this decorator is used. The
    decorator does nothing.
    """
    return ob


@classmethod
def return_or_raise(cls, response, expected_status_code):
    """Check for ``expected_status_code``."""
    try:
        if response.status_code in expected_status_code:
            return response
    except TypeError:
        if response.status_code == expected_status_code:
            return response
    raise cls(response)


def remove_if_exists_copy(mylist, item):
    """ Return new list with item removed """
    new_list = []
    for el in mylist:
        if el != item:
            new_list.append(el)
    return new_list


def summarize(total, key, value):
    """
    Module function used to collect overall statistics.

    @param total the dictionary for the overall statistics
    @param key the key to be summarize
    @param value the value to be added to the overall statistics
    @return the value added to the overall statistics
    """
    total[key] = total.setdefault(key, 0) + value
    return value


@staticmethod
def set_boolean_field():
    """
        set integer mapping values
        :return dictionary:
        """
    return {'type': 'boolean'}


def negate_filter(value):
    """
    Negates a boolean value.

    If the input is not a boolean, it will be converted to one: bool(value)

    Args:
      value (bool, object): the input
    Returns:
      bool: the inverse
    """
    if not isinstance(value, bool):
        value = bool(value)
    return not value


def merge_dicts(*dict_args):
    """
    Given any number of dicts, shallow copy and merge into a new dict,
    precedence goes to key value pairs in latter dicts.
    """
    result = {}
    for dictionary in dict_args:
        result.update(dictionary)
    return result


def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]


def normalize_none_str(string_or_none):
    """Returns '' IFF given value is None, passthrough otherwise.

    This function normalizes None to the empty string to facilitate
    string concatenation when a variable could be None.
    """
    return '' if string_or_none is None else string_or_none


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


@classmethod
def connect_postgres(cls, host='', username='', password='', port=5432,
    database='', **kwargs):
    """
        Initializes a PostgreSQL connection with the provided credentials.

        @param host -- the host to connect to (default Unix Socket)
        @param username -- the username to login as (optional)
        @param password -- the password to login with (optional)
        @param port -- the port to connect to (default 5432)
        @param database -- the name of the database to connect to (optional)
        """
    try:
        import psycopg2
    except ImportError:
        raise ImportError('Please install the mysql-python package')
    connection = psycopg2.connect(host=host, user=username, password=
        password, port=port, database=database, **kwargs)
    c = connection.cursor()
    return cls(connection, c)


@staticmethod
def _get_is_truncated_value_from_flags(flags: int) ->bool:
    """
        Retrieve the is_truncated value from flags.

        :param flags: an integer representing the flags of a dns message
        :return: value of the is_truncated field as bool
        """
    return (flags & int.from_bytes(b'\x00\x00\x02\x00', 'big')) >> 9 == 1


def escape(string):
    """ Returns the string with control characters for Pattern syntax escaped.
        For example: "hello!" => "hello\\!".
    """
    for ch in ('{', '}', '[', ']', '(', ')', '_', '|', '!', '*', '+', '^'):
        string = string.replace(ch, '\\' + ch)
    return string


def count(data, column, mapping=None, probability=False, return_param=False):
    """ Performs frequency encoding.

        Categories are replaced by their number of occurence.
        Soon: possibility of probability instead of count (normalization)

        :param df: Data
        :param column: Column to encode
        :param mapping: Dictionary {category : value}
        :param probability: If True, return probability instead of frequency
        :param return_param: If True, the mapping is returned
        :return: Encoded data
        :rtype: pd.DataFrame
    """
    categories = data[column].unique()
    if mapping is None:
        mapping = dict()
        for e in data[column]:
            if e in mapping:
                mapping[e] += 1
            else:
                mapping[e] = 1
    else:
        for category in categories:
            if category not in mapping:
                mapping[category] = 0
    if probability:
        factor = 1.0 / sum(mapping.values())
        for k in mapping:
            mapping[k] = float(format(mapping[k] * factor, '.3f'))
    data[column] = data[column].map(mapping)
    if return_param:
        return data, mapping
    return data


def is_cross_origin(request):
    """Compare headers HOST and ORIGIN. Remove protocol prefix from ORIGIN, then
    compare. Return true if they are not equal
    example HTTP_HOST: '127.0.0.1:5000'
    example HTTP_ORIGIN: 'http://127.0.0.1:5000'
    """
    origin = request.environ.get('HTTP_ORIGIN')
    host = request.environ.get('HTTP_HOST')
    if origin is None:
        return False
    if origin.startswith('http://'):
        origin = origin.replace('http://', '')
    elif origin.startswith('https://'):
        origin = origin.replace('https://', '')
    return host != origin


def link_viewer():
    """Link to viewer node

    """
    pass


def dir_dict(obj, raise_exc=False):
    """Return a dictionary of attribute names to values for a given
    object. Unlike ``obj.__dict__``, this function returns all
    attributes on the object, including ones on parent classes.
    """
    ret = {}
    for k in dir(obj):
        try:
            ret[k] = getattr(obj, k)
        except Exception:
            if raise_exc:
                raise
    return ret


def isdatelike(value):
    """
    Returns whether a given value has a date-like interface
    To support the arrow library, we do not check for instanceof(date, (datetime.date, datetime.datetime))

    Returns
    ------------
    bool
    """
    try:
        value.year, value.month, value.day
        return True
    except:
        return False


def _to_db_dict(namespace_id, resource_type_id, model_dict):
    """transform a model dict to a metadef_namespace_resource_type dict"""
    db_dict = {'namespace_id': namespace_id, 'resource_type_id':
        resource_type_id, 'properties_target': model_dict[
        'properties_target'], 'prefix': model_dict['prefix']}
    return db_dict


def controls(email):
    """Determine whether this user controls the given email address.

        :param email: The text email address to register.
        :type email: str
        :return: True if the user controls the given email address.
        :rtype: bool
        """


def dat_extent(dat, axes):
    """
    Get the extent of the dat variable.

    Parameters
    ----------
    dat : PyGMI Data
        PyGMI raster dataset.
    axes : matplotlib.axes._subplots.AxesSubplot
        Matploltib axes.

    Returns
    -------
    left : float
        Left coordinate.
    right : float
        Right coordinate.
    bottom : float
        Bottom coordinate.
    top : float
        Top coordinate.

    """
    left, right, bottom, top = dat.extent
    if right - left > 10000 or top - bottom > 10000:
        axes.xaxis.set_label_text('Eastings (km)')
        axes.yaxis.set_label_text('Northings (km)')
        left /= 1000.0
        right /= 1000.0
        top /= 1000.0
        bottom /= 1000.0
    else:
        axes.xaxis.set_label_text('Eastings (m)')
        axes.yaxis.set_label_text('Northings (m)')
    return left, right, bottom, top


def quality_to_ascii(quality, phred=33):
    """Quality to ASCII conversion

    :param int phred: offset (defaults to 33)

    ::

         >>> quality_to_ascii(65)
         b

    """
    return chr(quality + phred)


def regular_slices(length, width, offset):
    """Iterate over regular slices on a linear chain.

    Put slices on a linear chain as follows:

    >>> n = 5
    >>> [tuple(range(*s.indices(n))) for s in regular_slices(n, 3, 2)]
    [(0, 1, 2), (2, 3, 4)]
    >>> n = 7
    >>> [tuple(range(*s.indices(n))) for s in regular_slices(n, 3, 2)]
    [(0, 1, 2), (2, 3, 4), (4, 5, 6)]

    The scheme is illustrated by the following figure:

    .. tabularcolumns:: |c|c|c|

    +--------------------------+------------+
    | ###### ``width`` ####### |            |
    +------------+-------------+------------+
    | ``offset`` | ``overlap`` | ``offset`` |
    +------------+-------------+------------+
    |            | ####### ``width`` ###### |
    +------------+--------------------------+

    .. todo:: This table needs cell borders in the HTML output (->
              CSS) and the tabularcolumns command doesn't work.

    Note that the overlap may be larger than, equal to or smaller than
    zero.

    We enforce that the last slice coincides with the end of the
    chain, i.e. :code:`(length - width) / offset` must be integer.  We
    produce :code:`(length - width) / offset + 1` slices and the i-th
    slice is :code:`slice(offset * i, offset * i + width)`, with
    :code:`i` starting at zero.

    :param int length: The length of the chain.
    :param int width: The width of each slice.
    :param int offset: Difference between starting positions of
        successive slices. First slice starts at 0.
    :returns: Iterator over slices.

    """
    assert (length - width
        ) % offset == 0, 'length {}, width {}, offset {}'.format(length,
        width, offset)
    num_slices = (length - width) // offset + 1
    for i in range(num_slices):
        yield slice(offset * i, offset * i + width)


def getid(obj):
    """Retrieves an id from object or integer.

    Abstracts the common pattern of allowing both an object or an object's
    ID as a parameter when dealing with relationships.
    """
    try:
        return obj.id
    except AttributeError:
        return obj


def put_block(data, segnum, salt):
    """
        Add a block and salt to the share.
        """


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


@staticmethod
def concat_url(endpoint, url):
    """Concatenate endpoint and final URL.

        E.g., "http://keystone/v2.0/" and "/tokens" are concatenated to
        "http://keystone/v2.0/tokens".

        :param endpoint: the base URL
        :param url: the final URL
        """
    return '%s/%s' % (endpoint.rstrip('/'), url.strip('/'))


def mu_none(T, P):
    """n-n-n: no interaction effect."""
    mu = 0
    valid = T > 0
    return mu, valid


def get_matches(tokens, qbegin, automaton):
    """
    Yield tuples of automaton matches positions as (match end, match value) from
    matching `tokens` sequence of token ids starting at the `qbegin` absolute
    query start position position using the `automaton`.
    """
    qtokens = tuple(tokens)
    for qend, matched_value in automaton.iter(qtokens):
        qend = qbegin + qend + 1
        yield qend, matched_value


def get_cmd_word(cmd, d_width, d_length):
    """Build the command word.

    Note
    ----
    The returned command word has the following format:
    Bit [0]     : valid bit.
    Bit [2:1]   : command data width.
    Bit [3]     : command type (read or write).
    Bit [15:8]  : command burst length.
    Bit [31:16] : unused.

    Parameters
    ----------
    cmd : int
        Either 1 (read processor register) or 0 (write processor register).
    d_width : int
        Command data width.
    d_length : int
        Command burst length (currently only supporting d_length 1).

    Returns
    -------
    int
        The command word following a specific format.

    """
    word = 1
    word = word | d_width - 1 << 1
    word = word | cmd << 3
    word = word | d_length << 8
    word = word | 0 << 16
    return word


def __del__(manager):
    """
        cleanly shut down when all references to this object are lost
        """
    manager.shutdown()


@classmethod
def definedPlatformTypes(cls):
    """
        Class method to get all platform types defined in API v4.

        @return list of all defined platform types
        @rtype list of str
        """
    return ['PLATFORM_TYPE_UNSPECIFIED', 'WINDOWS', 'LINUX', 'ANDROID',
        'OSX', 'IOS', 'ANY_PLATFORM', 'ALL_PLATFORMS', 'CHROME']


def split_start_line(start_line):
    """ Split the start line (see RFC2616 section 4). """
    return start_line.rstrip().split(None, 2)


@classmethod
def from_data(cls, data):
    """Construct a sphere from its data representation.

        Parameters
        ----------
        data : :obj:`dict`
            The data dictionary.

        Returns
        -------
        Sphere
            The constructed sphere.

        Examples
        --------
        >>> from compas.geometry import Sphere
        >>> data = {'point': [1., 2., 3.], 'radius': 4.}
        >>> sphere = Sphere.from_data(data)

        """
    sphere = cls([0, 0, 0], 1)
    sphere.data = data
    return sphere