def __iter__():
    """Iterate over all the IChainLinks in this chain.

        :return: an IChainLink.
        """


def GT(x=None, y=None):
    """
    Compares two values and returns:
        true when the first value is greater than the second value.
        false when the first value is less than or equivalent to the second value.
    See https://docs.mongodb.com/manual/reference/operator/aggregation/gt/
    for more details
    :param x: first value or expression
    :param y: second value or expression
    :return: Aggregation operator
    """
    if x is None and y is None:
        return {'$gt': []}
    return {'$gt': [x, y]}


def alias_cset(alias, engine, csets):
    """alias a cset to another"""
    return csets[alias]


def shadow(fontid: int, level: int, r: float, g: float, b: float, a: float):
    """Shadow options, enable/disable using SHADOW .

    :param fontid: The id of the typeface as returned by blf.load(), for default font use 0.
    :type fontid: int
    :param level: The blur level, can be 3, 5 or 0.
    :type level: int
    :param r: Shadow color (red channel 0.0 - 1.0).
    :type r: float
    :param g: Shadow color (green channel 0.0 - 1.0).
    :type g: float
    :param b: Shadow color (blue channel 0.0 - 1.0).
    :type b: float
    :param a: Shadow color (alpha channel 0.0 - 1.0).
    :type a: float
    """
    pass


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 cols_to_drop(df, columns):
    """Drop selected columns and return dataframe.

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

    Returns:
        Original dataframe without dropped columns.
    """
    for col in columns:
        df.drop([col], axis=1, inplace=True)
    return df


def func_unset(parser, name):
    """Unsets the variable ``name``."""
    if name.startswith('_'):
        name = '~' + name[1:]
    if name in ('performer:*', 'comment:*', 'lyrics:*'):
        name = name[:-1]
        for key in list(parser.context.keys()):
            if key.startswith(name):
                del parser.context[key]
        return ''
    try:
        del parser.context[name]
    except KeyError:
        pass
    return ''


@staticmethod
def visit_Lambda(_):
    """ Lambda have to be remove before. """
    assert False


def null_query(x):
    """
    Returns false regardless of the document
    passed to the function.
    """
    return False


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

        :param SaveUrlError val:
        :rtype: SaveUrlJobStatus
        """
    return cls('failed', val)


def backimage_zoom(factor: float=1.2):
    """Zoom in/out the background image

    :param factor: Factor
    :type factor: float
    """
    pass


def fib(n):
    """Fibonacci example function

    Args:
      n (int): integer

    Returns:
      int: n-th Fibonacci number
    """
    assert n > 0
    a, b = 1, 1
    for i in range(n - 1):
        a, b = b, a + b
    return a


def getSessionTypes(assoc_type):
    """Return the allowed session types for a given association type"""
    assoc_to_session = {'HMAC-SHA1': ['DH-SHA1', 'no-encryption'],
        'HMAC-SHA256': ['DH-SHA256', 'no-encryption']}
    return assoc_to_session.get(assoc_type, [])


@staticmethod
def user_filter(queryset, user):
    """This method is used to check if the user has access"""
    return queryset.objects.filter(users__in=[user])


@staticmethod
def get_header(**kwargs):
    """
        Override this static method to provide header for the report.
        It gets called with the form's data as arguments.
        Make sure it's a static method. Result should be list of list, where
        each (sub)list is row in header.
        """
    return []


def get_all(isamAppliance, check_mode=False, force=False):
    """
    Get information on existing users
    """
    return isamAppliance.invoke_get('Retrieving users', '/sysaccount/users/v1')


def is_subclass(cls, classinfo):
    """
  A more sensible version of the issubclass builtin
  """
    try:
        return issubclass(cls, classinfo)
    except TypeError:
        return False


def func_pickle(func, suffix='.pik', dir='.'):
    """ standard pickle.dump of function to a NamedTemporaryFile """
    from dill.temp import dump
    return dump(func, suffix=suffix, dir=dir)


def _validate_dict(s, accept_none=False):
    """
	A validation method to check if the input s is a dictionary otherwise raise error if it is not convertable
	"""
    if s is None and accept_none:
        return None
    if isinstance(s, dict):
        return s
    else:
        raise ValueError('{} is not a dictionary!'.format(s))


@staticmethod
def prune_address_space(address_space, sorted_addresses=None):
    """Prune the address space."""
    if sorted_addresses is None:
        sorted_addresses = sorted(address_space, key=len)
    for address in sorted_addresses:
        if address.is_root or address.get_parent() in address_space:
            continue
        else:
            del address_space[address]


@classmethod
def get_at_dirpath(cls, session, dirpath):
    """Return VCS instance at the given path (if under that VCS control)"""
    raise NotImplementedError


@staticmethod
def filter_with_english_letters(buf):
    """
        Returns a copy of ``buf`` that retains only the sequences of English
        alphabet and high byte characters that are not between <> characters.
        Also retains English alphabet and high byte characters immediately
        before occurrences of >.

        This filter can be applied to all scripts which contain both English
        characters and extended ASCII characters, but is currently only used by
        ``Latin1Prober``.
        """
    filtered = bytearray()
    in_tag = False
    prev = 0
    for curr in range(len(buf)):
        buf_char = buf[curr:curr + 1]
        if buf_char == b'>':
            in_tag = False
        elif buf_char == b'<':
            in_tag = True
        if buf_char < b'\x80' and not buf_char.isalpha():
            if curr > prev and not in_tag:
                filtered.extend(buf[prev:curr])
                filtered.extend(b' ')
            prev = curr + 1
    if not in_tag:
        filtered.extend(buf[prev:])
    return filtered


def mix(color1, color2, pos=0.5):
    """
    Return the mix of two colors at a state of :pos:

    Retruns color1 * pos + color2 * (1 - pos)
    The return color always has an alpha value
    """
    if len(color1) < 4:
        color1 += 255,
    if len(color2) < 4:
        color2 += 255,
    opp_pos = 1 - pos
    red = color1[0] * pos + color2[0] * opp_pos
    green = color1[1] * pos + color2[1] * opp_pos
    blue = color1[2] * pos + color2[2] * opp_pos
    alpha = color1[3] * pos + color2[3] * opp_pos
    return int(red), int(green), int(blue), int(alpha)


def get_inst_info(qry_string):
    """Get details for instances that match the qry_string.

    Execute a query against the AWS EC2 client object, that is
    based on the contents of qry_string.

    Args:
        qry_string (str): the query to be used against the aws ec2 client.
    Returns:
        qry_results (dict): raw information returned from AWS.

    """
    qry_prefix = 'EC2C.describe_instances('
    qry_real = qry_prefix + qry_string + ')'
    qry_results = eval(qry_real)
    return qry_results


def tagsContain(f_tn, ec2_obj):
    """
  Similar to isitfit.cost.redshift_common.tagsContain
  """
    if ec2_obj.tags is None:
        return False
    for t in ec2_obj.tags:
        for k in ['Key', 'Value']:
            if f_tn in t[k].lower():
                return True
    return False


def _get_agent_spec(ipobj, interface, port):
    """take a google ipaddr object and port number and produce a net-snmp
    agent specification (see the snmpcmd manpage)"""
    if ipobj.version == 4:
        agent = 'udp:%s:%s' % (ipobj.compressed, port)
    elif ipobj.version == 6:
        if ipobj.is_link_local:
            if interface is None:
                raise RuntimeError(
                    'Cannot create agent specification from link local IPv6 address without an interface'
                    )
            else:
                agent = 'udp6:[%s%%%s]:%s' % (ipobj.compressed, interface, port
                    )
        else:
            agent = 'udp6:[%s]:%s' % (ipobj.compressed, port)
    else:
        raise RuntimeError(
            'Cannot create agent specification for IP address version: %s' %
            ipobj.version)
    return agent


def _set_default_configs(user_settings, default):
    """Set the default value to user settings if user not specified
    the value.
    """
    for key in default:
        if key not in user_settings:
            user_settings[key] = default[key]
    return user_settings


def _get_sign(orientation, xaxis, yaxis):
    """ Helper to find multiplicative factors to get rotations right. """
    sign = {xaxis: 1, yaxis: 1}
    if orientation in ['left', 'bottom']:
        sign[xaxis] = 1
    else:
        sign[xaxis] = -1
    if orientation in ['right', 'bottom']:
        sign[yaxis] = 1
    else:
        sign[yaxis] = -1
    return sign


@staticmethod
def canRead(path: str):
    """
        Returns true if it can read the source defined by path
        :param path: source uri
        :return: True, if source is readable.
        """
    return False


def __and__(i1, i2):
    """ Return the intersection of *i1* and *i2*.

        Return an empty interval if they don't intersect.
        """
    return i1.__class__(i1.x & i2.x, i1.y & i2.y)


@classmethod
def _from_et(cls, root, parent):
    """
        Create a :class:`Junction` from a :code:`junction` element.

        :param root: The element tree tree to parse.
        :param parent: :class:`EagleFilePart` that should hold the resulting :class:`EagleFilePart`
        :rtype: :class:`Junction`
        """
    n = cls()
    n._init_from_et(root, parent)
    return n


def initSecContext(target_name, cred=None, context=None, mech_type=None,
    flags=None, time=0, channel_bindings=None, input_token=None):
    """
    Initializes a GSS Security Context

    (Client)
    This method initializes a GSSAPI security context
    with the given parameters.  The default flags for
    the flags field are MUTUAL and SEQUENCE.

    :param target_name: the name of the target
                        (commonly the server name, retrieved using importName)
    :param cred: The handle for credentials claimed
                 (returned from acquireCred),
                 or None to use the default initiator principal
    :param context: The current context, or None if this is the first call
    :param mech_type: the mechanism type
                      (None for default, otherwise a capsule
                      from :func:`getMechanismType`)
    :param flags: the requested flags
    :type flags: [:class:`RequirementFlag`]
    :param int time: the requested TTL for this context
                     (0 uses the default TTL)
    :param channel_bindings: the requested input channel bindings
                             (currently only None is supported)
    :param bytes input_token: the input token (use None for the first call)
    :returns: a tuple containing
              the (potentially modified) context,
              the actual mechanism type used,
              the output token,
              the actual flags provided,
              the actual TTL for this context,
              and whether or not a continue is needed
              (i.e. (context, MechType, [RequirementFlag],
              bytes, TTL, continue_needed))
    :except GSSError:
    """


def uniq_stable(elems):
    """uniq_stable(elems) -> list

    Return from an iterable, a list of all the unique elements in the input,
    but maintaining the order in which they first appear.

    A naive solution to this problem which just makes a dictionary with the
    elements as keys fails to respect the stability condition, since
    dictionaries are unsorted by nature.

    Note: All elements in the input must be hashable.
    """
    unique = []
    unique_set = set()
    for nn in elems:
        if nn not in unique_set:
            unique.append(nn)
            unique_set.add(nn)
    return unique


def GetNotebookRoot(panes, notebook_id):
    """
    Returns the :class:`~wx.lib.agw.aui.auibook.AuiNotebook` which has the specified `notebook_id`.

    :param `panes`: a list of :class:`AuiPaneInfo` instances;
    :param integer `notebook_id`: the target notebook id.
    """
    for paneInfo in panes:
        if paneInfo.IsNotebookControl(
            ) and paneInfo.notebook_id == notebook_id:
            return paneInfo
    return None


@classmethod
def validate(cls, measurement):
    """Should make some assertions to make sure the value is correct
        :raises: AssertionError
        """
    assert measurement
    assert measurement.unit == cls.metric_unit
    assert measurement.type == cls.metric_type


def configure(config_dict):
    """
        Alter the buildbot config_dict, as defined in master.cfg

        like the master.cfg, this is run out of the main reactor thread, so this can block, but this can't
        call most Buildbot facilities.

        :returns: None
        """


def process_secmet_domain(st, dna, accession, proteinid):
    """
    Convert something that looks like this:
        "NRPS/PKS Domain: PKS_Docking_Nterm (4-31). E-value: 8.6e-08. Score: 22.9;"
        'NRPS/PKS Domain: AMP-binding (30-457). E-value: 1.8e-119. Score: 390.2;'
        "NRPS/PKS Domain: PKS_Docking_Nterm (4-31). E-value: 8.6e-08. Score: 22.9;"
        "NRPS/PKS Domain: PKS_KS (35-460). E-value: 3.1e-185. Score: 607.4;"
        "NRPS/PKS Domain: PKS_AT (570-867). E-value: 4.5e-105. Score: 342.5;"
        "NRPS/PKS Domain: PKS_KR (1132-1312). E-value: 1.7e-55. Score: 179.0;"
        "NRPS/PKS Domain: ACP (1425-1497). E-value: 9.5e-25. Score: 78.0;"
        "NRPS/PKS Domain: PKS_KS (1520-1946). E-value: 1.2e-180. Score: 592.2;"
        "NRPS/PKS Domain: PKS_AT (2056-2356). E-value: 4.3e-112. Score: 365.6;"

    Into:
        {'Domain': 'PKS_Docking_Nterm',
        'E-value': 8.6e-08,
        'Nucleotide_End': 93,
        'Nucleotide_Start': 10,
        'Protein_End': 31,
        'Protein_start': 4,
        'Score': 22.9}

    """
    fulldomain = st.split('Domain:')[1].split('.')[0]
    domain = fulldomain.split('(')[0].strip()
    start = int(fulldomain.split(')')[0].split('(')[1].split('-')[0])
    end = int(fulldomain.split(')')[0].split('(')[1].split('-')[1])
    evalue = float(st.split('E-value:')[1].split('. Score:')[0].strip())
    score = float(st.split('Score:')[1].strip()[:-1])
    return {'Accession': accession, 'Protein_ID': proteinid, 'Domain':
        domain, 'Protein_start': start, 'Nucleotide_Start': start * 3 - 2,
        'Protein_End': end, 'Nucleotide_End': end * 3, 'E_value': evalue,
        'Score': score, 'DNA_Sequence': dna[start * 3 - 2:end * 3]}


def chunk(data, index):
    """
    Split a string into two parts at the input index.
    """
    return data[:index], data[index:]


def hide_collection(collection_index: int=-1, toggle: bool=False):
    """Show only objects in collection (Shift to extend)

    :param collection_index: Collection Index, Index of the collection to change visibility
    :type collection_index: int
    :param toggle: Toggle, Toggle visibility
    :type toggle: bool
    """
    pass


def pack_t21(fn):
    """
    Pack a TAPE21 results file of an FDE job.

    This will reduce the size of a FDE TAPE21 file by
    only keeping the ActiceFragment section and related
    essential information. All information about frozen
    fragments will be deleted. This is useful for manual
    freeze-and-thaw calculations with many subsystems,
    where this packing can reduce the size of the stored
    files significantly.

    @param fn: The filename of the TAPE21 to be packed.
    @type  fn: str
    """
    from scm.plams import KFReader
    import os
    import subprocess
    fn_orig = fn + '.orig'
    os.rename(fn, fn_orig)
    kf_orig = KFReader(fn_orig)
    keepsections = ['General', 'Properties', 'Num Int Params',
        'LinearScaling', 'Geometry']
    atomtypeindices = kf_orig.read('ActiveFrag', 'atomtypeIndices')
    if not kf_orig._sections:
        kf_orig._create_index()
    for section in kf_orig._sections:
        for i in atomtypeindices:
            if section.startswith('Atyp%3i' % i):
                keepsections.append(section)
            elif section.startswith('Atyp%4i' % i):
                keepsections.append(section)
            elif section.startswith('Atyp%5i' % i):
                keepsections.append(section)
        if section.startswith('ActiveFrag'):
            keepsections.append(section)
    subprocess.Popen(['cpkf', fn_orig, fn] + keepsections, stdout=
        subprocess.PIPE, stderr=subprocess.PIPE).wait()
    os.remove(fn_orig)


def get_s3user(module, blade):
    """Return Object Store Account or None"""
    full_user = module.params['account'] + '/' + module.params['name']
    s3user = None
    s3users = blade.object_store_users.list_object_store_users()
    for user in range(0, len(s3users.items)):
        if s3users.items[user].name == full_user:
            s3user = s3users.items[user]
    return s3user


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


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


def find_str(text, sub, start=None, end=None):
    """
    Finds the lowest index of the substring ``sub`` within ``text`` in the range [``start``, ``end``].

    Optional arguments ``start`` and ``end`` are interpreted as in slice notation. However,
    the index returned is relative to the original string ``text`` and not the slice
    ``text[start:end]``.  The function returns -1 if ``sub`` is not found.

    **Note:** The ``find_str()`` function should be used only if you need to know the position
    of ``sub``. To check if ``sub`` is a substring or not, use the in operator::

        >>>
        >>> 'Py' in 'Python'
        True

    :param text: The string to search
    :type text:  ``str``

    :param sub: The substring to search for
    :type sub:  ``str``

    :param start: The start of the search range
    :type start:  ``int``

    :param end: The end of the search range
    :type end:  ``int``

    :return: The lowest index of the substring ``sub`` within ``text`` in the range [``start``, ``end``].
    :rtype:  ``int``
    """
    assert isinstance(text, str), '%s is not a string' % text
    return text.find(sub, start, end)


def pick_type(types):
    """ if there is only one type found
    in a column, then use that. if multiple
    types are found, default back to string.
    """
    type_set = set(types)
    if len(type_set) == 1:
        return list(type_set)[0]
    elif set(['IntType', 'FloatType']) == type_set:
        return 'FloatType'
    else:
        return 'StringType'


def minervac_sanitize(text):
    """Encodes given text to ASCII"""
    return text.encode('ascii', 'ignore')


def group_sort():
    """Sort Bone Groups by their names in ascending order

    """
    pass


@classmethod
def write_buffer(cls):
    """Write buffer to file because buffer is full

        The buffer part is necessary because the GzipFile.write()
        method seems fairly slow.

        """
    assert cls.line_buffer and cls._fileobj
    cls.line_buffer.append(b'')
    cls._fileobj.write(cls._line_sep.join(cls.line_buffer))
    cls.line_buffer = []


def softmax_cross_entropy(data=None, label=None, name=None, attr=None, out=
    None, **kwargs):
    """Calculate cross entropy of softmax output and one-hot label.

    - This operator computes the cross entropy in two steps:
      - Applies softmax function on the input array.
      - Computes and returns the cross entropy loss between the softmax output and the labels.

    - The softmax function and cross entropy loss is given by:

      - Softmax Function:

      .. math:: \\text{softmax}(x)_i = \\frac{exp(x_i)}{\\sum_j exp(x_j)}

      - Cross Entropy Function:

      .. math:: \\text{CE(label, output)} = - \\sum_i \\text{label}_i \\log(\\text{output}_i)

    Example::

      x = [[1, 2, 3],
           [11, 7, 5]]

      label = [2, 0]

      softmax(x) = [[0.09003057, 0.24472848, 0.66524094],
                    [0.97962922, 0.01794253, 0.00242826]]

      softmax_cross_entropy(data, label) = - log(0.66524084) - log(0.97962922) = 0.4281871

    Defined in src/operator/loss_binary_op.cc:L59

    Parameters
    ----------
    data : Symbol
        Input data
    label : Symbol
        Input label

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

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


def getCatalog():
    """Return the catalog of collected message ids as keys of a
        dictionary. The values are a tuple consisting the of the
        filename and the line number at which the message id was
        found.
        """


def skyval2txt(files='*_flt.fits', skyfile='skyfile.txt', skykwd='SKYUSER',
    default_ext=('SCI', '*')):
    """
    A convenience function that allows retrieving computed sky background
    values from image headers and storing them in a text file that can be
    re-used by ``drizzlepac.astrodrizzle.AstroDrizzle()``. This is
    particularly useful when performing sky *matching* on a large number of
    images which takes considerable time. Saving computed sky values to a text
    file allows re-running ``AstroDrizzle()`` without re-computing sky values.

    .. warning::
       The file specified by ``skyfile`` is overwritten without warning.

    .. note::
       Images that do not have specified extensions will be ignored.

    Parameters
    ----------
    files: str
        File name(s), including extension specification if necessary,
        from which sky values should be retrieved:

            * a comma-separated list of valid science image file names
              (see note below) and (optionally) extension specifications,
              e.g.: ``'j1234567q_flt.fits[1], j1234568q_flt.fits[sci,2]'``;

            * an @-file name, e.g., ``'@fits_files_with_skyvals.txt'``.

        .. note::
            **Valid** **science** **image** **file** **names** are:

            * file names of existing FITS, GEIS, or WAIVER FITS files;

            * partial file names containing wildcard characters, e.g.,
              ``'*_flt.fits'``;

            * Association (ASN) tables (must have ``_asn``, or ``_asc``
              suffix), e.g., ``'j12345670_asn.fits'``.

        .. warning::
            @-file names **MAY** **NOT** be followed by an extension
            specification.

        .. warning::
            If an association table or a partial file name with wildcard
            characters is followed by an extension specification, it will be
            considered that this extension specification applies to **each**
            file name in the association table or **each** file name
            obtained after wildcard expansion of the partial file name.

    skyfile: str, optional
        Output "skyfile" to which sky values from the image headers should be
        written out.

    skykwd: str, optional
        Header keyword holding the value of the computed sky background.

    default_ext: int, tuple, optional
        Default extension to be used with image files that to not have
        an extension specified.

    """
    from .parseat import parse_cs_line
    files = parse_cs_line(files, doNotOpenDQ=True, im_fmode='readonly',
        default_ext=default_ext)
    with open(skyfile, 'w') as f:
        for file in files:
            if len(file.fext) < 1:
                file.release_all_images()
                continue
            f.write('{:s}'.format(file.image.filename))
            for ext in file.fext:
                skyval = file.image.hdu[ext].header.get(skykwd, 0.0)
                f.write('\t{:.16g}'.format(skyval))
            f.write('\n')
            file.release_all_images()


def _val(var, is_percent=False):
    """
    Tries to determine the appropriate value of a particular variable that is
    passed in.  If the value is supposed to be a percentage, a whole integer
    will be sought after and then turned into a floating point number between
    0 and 1.  If the value is supposed to be an integer, the variable is cast
    into an integer.

    """
    try:
        if is_percent:
            var = float(int(var.strip('%')) / 100.0)
        else:
            var = int(var)
    except ValueError:
        raise ValueError('invalid watermark parameter: ' + var)
    return var


def join(ol, separator=','):
    """
        from elist.elist import *
        ol = [1,2,3,4]
        join(ol,separator="-")
    """
    if ol.__len__() == 0:
        return ''
    else:
        pass
    cond = type(ol[0]) == type(b'')
    if cond:
        rslt = b''
    else:
        rslt = ''
    length = ol.__len__()
    for i in range(0, length - 1):
        ele = ol[i]
        if cond:
            pass
        else:
            ele = str(ele)
        rslt = rslt + ele + separator
    if cond:
        rslt = rslt + ol[length - 1]
    else:
        rslt = rslt + str(ol[length - 1])
    return rslt


def eyedropper_colorramp_point():
    """Point-sample a color band

    """
    pass


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

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


def say_hello(to):
    """Say hello to somebody"""
    return f'Hello {to}!'


def p_arg_list_multi(p):
    """arg_list : expr COMMA arg_list"""
    if len(p[3]) > 0:
        p[0] = [p[1]] + p[3]
    else:
        p[0] = [p[1]]


@classmethod
def getEncoding(cls):
    """
        Return an array of character encodings to try to decode the HTML with
        """
    return ['Windows-1252', 'utf8']


def group_dictionaries_by_key(list_of_dicts, key, remove_key=True):
    """
    Takes a list of dictionaries and returns a
    dictionary of lists of dictionaries with the same value for the given key.
    The grouping key is removed by default.
    If the key is not in any dictionary an empty dict is returned.
    """
    dict_of_lists = dict()
    for dicty in list_of_dicts:
        if key not in dicty:
            continue
        dicty_value = dicty[key]
        if remove_key:
            dicty.pop(key)
        if dicty_value in dict_of_lists:
            dict_of_lists[dicty_value].append(dicty)
        else:
            dict_of_lists[dicty_value] = [dicty]
    return dict_of_lists


@classmethod
def stop(cls):
    """Unschedules all watchdogs."""
    cls.observer.unschedule_all()
    cls.running = False


@staticmethod
def to_bool(value):
    """Convert value to bool.

        Args:
            value (bool|str): The value to convert to boolean.

        Returns:
            bool: The boolean value
        """
    return str(value).lower() in ['1', 't', 'true', 'y', 'yes']


def _rep_strip(dit: str, replace_str: str) ->str:
    """Replace first instance of replace_str and left strip"""
    return dit.replace(replace_str, '', 1).lstrip()


def parse_object(repo, objectish):
    """Parse a string referring to an object.

    :param repo: A `Repo` object
    :param objectish: A string referring to an object
    :return: A git object
    :raise KeyError: If the object can not be found
    """
    return repo[objectish]


@staticmethod
def supports_deactivation():
    """Applicable only to scenes."""
    return None


def draw_if_needed(canvas):
    """
    Force canvas to be drawn if there are drawing commands outstanding.

    This is needed if the application has emitted a draw_idle() that hasn't
    had time to process prior to the mouse event detection running.
    """
    if not canvas._isDrawn:
        canvas.draw()


def get_binary_annotations(request, response):
    """Helper method for getting all binary annotations from the request.

    :param request: the Pyramid request object
    :param response: the Pyramid response object
    :returns: binary annotation dict of {str: str}
    """
    route = request.matched_route.pattern if request.matched_route else ''
    annotations = {'http.uri': request.path, 'http.uri.qs': request.path_qs,
        'http.route': route, 'response_status_code': str(response.status_code)}
    settings = request.registry.settings
    if 'zipkin.set_extra_binary_annotations' in settings:
        annotations.update(settings['zipkin.set_extra_binary_annotations'](
            request, response))
    return annotations


def _lists_group(items):
    """ items -> key_to_list_mapping """
    result = {}
    for key, val in items:
        try:
            list_ = result[key]
        except KeyError:
            list_ = [val]
            result[key] = list_
        else:
            list_.append(val)
    return result


def second_arg(x, y, *args, **kw):
    """Returns the second argument unchanged"""
    return y


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

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


def iterate_pairs(mylist):
    """Go through an iterable and return the current item and the next"""
    if len(mylist) < 2:
        return
    for i, d in enumerate(mylist[:-1]):
        yield mylist[i], mylist[i + 1]
    return


def add_argument_disable_mslpm_version_check(parser):
    """Add a ``--disable-mslpm-version-check`` argument to the parser."""
    parser.add_argument('-D', '--disable-mslpm-version-check', action=
        'store_true', default=False, help=
        """Don't check if there is a new version of the
MSL-Package-Manager available."""
        )


@classmethod
def create(cls, **query):
    """
        secure create, mass assignment protected
        """
    return super().create(**cls._filter_attrs(query))


def post_through(mas, agents, states, moves):
    """
    Return the set of states reachable from the given states through actions of
    agents allowed by the given moves for agents.

    mas -- a multi-agents system;
    agents -- a subset of agents of mas;
    states -- a subset of states of mas;
    moves -- a subset of moves for agents.
    """
    return mas.post(states & moves)


def FILE(path: str, method: str, buffer: list=None, value: str=None):
    """ Access a new line on call. """
    if method == 'r':
        """ Populate the buffer. """
        if buffer == None:
            with open(path, method) as f:
                buffer = f.readlines()
        return buffer.pop(0)[:-1], buffer
    if method == 'w':
        with open(path, 'a') as f:
            f.write(value)


def load_xml(finder, module):
    """the builtin xml package attempts to load the _xmlplus module to see if
       that module should take its role instead; ignore the failure to find
       this module, though."""
    module.IgnoreName('_xmlplus')


@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 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)


@staticmethod
def is_collapsed(block):
    """
        Checks if the block is expanded or collased.

        :param block: QTextBlock
        :return: False for an open trigger, True for for closed trigger
        """
    if block is None:
        return False
    state = block.userState()
    if state == -1:
        state = 0
    return bool(state & 134217728)


def WhollyContains(contains, contained):
    """Helper function.

    :param `contains`: the containing shape
    :param `contained`: the contained shape
    :returns: `True` if 'contains' wholly contains 'contained'

    """
    xp1, yp1 = contains.GetX(), contains.GetY()
    xp2, yp2 = contained.GetX(), contained.GetY()
    w1, h1 = contains.GetBoundingBoxMax()
    w2, h2 = contained.GetBoundingBoxMax()
    left1 = xp1 - w1 / 2.0
    top1 = yp1 - h1 / 2.0
    right1 = xp1 + w1 / 2.0
    bottom1 = yp1 + h1 / 2.0
    left2 = xp2 - w2 / 2.0
    top2 = yp2 - h2 / 2.0
    right2 = xp2 + w2 / 2.0
    bottom2 = yp2 + h2 / 2.0
    return (left1 <= left2 and top1 <= top2 and right1 >= right2 and
        bottom1 >= bottom2)


@classmethod
def get_latest_active_customer_for_user(cls, user):
    """Returns last active stripe customer for user"""
    customer = cls.objects.filter(user_id=user.id, is_active=True).last()
    return customer


@staticmethod
def _join_constructions(constructions):
    """Append the constructions after each other by addition. Works for
        both lists and strings """
    result = type(constructions[0])()
    for c in constructions:
        result += c
    return result


def CopyAtts(inVar, outVar):
    """Copy all attributes of one variable to the other

        INPUTS:
        inVar  - netCDF4 variable containing attributes to copy
        outVar - netCDF4 variable to which attributes will be copied
        OUTPUTS:
        outVar - netCDF4 variable with all copied attributes
        """
    for att in inVar.ncattrs():
        outVar.setncattr(att, inVar.getncattr(att))
    return outVar


def parse_alpha(args):
    """
    If args is a list of a single INTEGER or NUMBER token,
    retur its value clipped to the 0..1 range
    Otherwise, return None.
    """
    if len(args) == 1 and args[0].type in ('NUMBER', 'INTEGER'):
        return min(1, max(0, args[0].value))


def get_subjects_tree():
    """Return the tree of subjects.
        """


def make_body(resource, parameters):
    """Creates body request using the parameters.

    :param resource: kind of resource
    :param parameters: list of parameters
    """
    content = {}
    for key in parameters.keys():
        content[key] = parameters[key]
    if resource:
        body = {resource: content}
    else:
        body = content
    return body


def lastRowId():
    """
        Return a C{Deferred} id of the most recently inserted row.
        """


def end_frame_set():
    """Set the end frame

    """
    pass


def ttr(num_types, num_tokens):
    """Calculate Type-Token Ratio (TTR).

    Used formula:
        .. math::
            TTR = rac{V}{N}

    Parameters:
        num_types (int): Absolute number of types.
        num_tokens (int): Absolute number of tokens.
    """
    return num_types / num_tokens


def _is_dunder(name):
    """Returns True if a __dunder__ name, False otherwise."""
    return name[:2] == name[-2:] == '__' and name[2:3] != '_' and name[-3:-2
        ] != '_' and len(name) > 4


def parse_csv_header(line):
    """Parse the CSV header returned by TDS."""
    units = {}
    names = []
    for var in line.split(','):
        start = var.find('[')
        if start < 0:
            names.append(str(var))
            continue
        else:
            names.append(str(var[:start]))
        end = var.find(']', start)
        unitstr = var[start + 1:end]
        eq = unitstr.find('=')
        if eq >= 0:
            units[names[-1]] = unitstr[eq + 2:-1]
    return names, units


def delete(project, version=None):
    """Delete the egg stored for the given project and version. If should
        also delete the project if no versions are left"""


@classmethod
def parse_dat(cls, fn, sep=' '):
    """ Parse Network data depending on type/extension """
    cls.log.debug('opening file: %s' % fn)
    inside = {'vertices': False, 'edges': False}
    for line in open(fn):
        line = line.strip()
        if line.startswith(('ROW LABELS:', '*vertices')) or inside['vertices']:
            if not inside['vertices']:
                inside['vertices'] = True
                continue
            if line.startswith('#') or not line.strip():
                inside['vertices'] = False
            elif line.startswith(('DATA', '*edges')):
                inside['vertices'] = False
                inside['edges'] = True
            else:
                continue
        elif line.startswith(('DATA', '*edges')) or inside['edges']:
            if not inside['edges']:
                inside['edges'] = True
                continue
            if line.startswith('#') or not line.strip() or len(line.split()
                ) < 2:
                inside['edges'] = False
            else:
                splitline = line.split(sep)
                row_size = len(splitline)
                if row_size == 2:
                    v1, v2 = splitline
                    w = 1
                    yield int(v1), int(v2), w, None
                elif row_size == 3:
                    v1, v2 = splitline[0:2]
                    w = int(splitline[2])
                    if w == 0:
                        continue
                    else:
                        yield int(v1), int(v2), w, None
                else:
                    raise NotImplementedError


def delete_offload(module, array):
    """Delete offload target"""
    changed = False
    if module.params['protocol'] == 'nfs':
        try:
            array.disconnect_nfs_offload(module.params['name'])
            changed = True
        except Exception:
            module.fail_json(msg='Failed to delete NFS offload {0}.'.format
                (module.params['name']))
    if module.params['protocol'] == 's3':
        try:
            array.disconnect_nfs_offload(module.params['name'])
            changed = True
        except Exception:
            module.fail_json(msg='Failed to delete S3 offload {0}.'.format(
                module.params['name']))
    module.exit_json(changed=changed)


@staticmethod
def _generate_fletcher_checksum(payload: bytes) ->bytes:
    """Return the checksum for the provided payload"""
    check_a = 0
    check_b = 0
    for char in payload:
        check_a += char
        check_a &= 255
        check_b += check_a
        check_b &= 255
    return bytes((check_a, check_b))


def check_list(cfg, key):
    """
    Verify that config value is a list.

    This is designed to catch the common error of specifying

    .. code-block:: yaml

        option:
            value

    instead of

    .. code-block:: yaml

        option:
            - value

    Parameters
    ----------
    cfg : dict
        Configuration options being checked.
    key: str
        Name of configuration value to be checked.

    Raises
    ------
    TypeError
        If ``key`` is in ``cfg`` and its value is not a list.
    """
    if key in cfg and not isinstance(cfg[key], list):
        raise TypeError(
            "%s should be a list, found '%s'; did you forget to add '-' before each entry?"
             % (key, cfg[key]))


def generator(it):
    """ Creates a generator type from the iterable.

    Args:

        it(iterable):            An iterable to make a generator.

    Returns:

        generator:               A generator made from the iterable.

    Examples:

        >>> generator(range(5))  # doctest: +ELLIPSIS
        <generator object generator at 0x...>

        >>> list(generator(range(5)))
        [0, 1, 2, 3, 4]
    """
    for each in it:
        yield each


def copyfileobj(fsrc, fdst, length=16 * 1024):
    """copy data from file-like object fsrc to file-like object fdst"""
    while 1:
        buf = fsrc.read(length)
        if not buf:
            break
        fdst.write(buf)


def glActiveTexture(texture: int):
    """Select active texture unit.

    :param texture: Constant in GL_TEXTURE0 0 - 8
    :type texture: int
    """
    pass


@classmethod
def validate_security(cls, app, ssl_options=None):
    """Prevent warnings about security from base class"""
    return


def ensure_list(specifier):
    """
    if specifier isn't a list or tuple, makes specifier into a list containing just specifier
    """
    if not isinstance(specifier, list) and not isinstance(specifier, tuple):
        return [specifier]
    return specifier


@classmethod
def modify_url_for_impersonation(cls, url, impersonate_user, username):
    """
        Modify the SQL Alchemy URL object with the user to impersonate if applicable.
        :param url: SQLAlchemy URL object
        :param impersonate_user: Bool indicating if impersonation is enabled
        :param username: Effective username
        """
    if impersonate_user is not None and username is not None:
        url.username = username


def first_word(text, separator=' '):
    """Returns a tuple of the first word and the rest."""
    if text:
        if text.find(separator) > 0:
            return text.split(separator, 1)
        else:
            return text, ''
    else:
        return '', ''


def p_testlist_safe_1(p):
    """testlist_safe : old_test"""
    p[0] = p[1]


def parse_data_row(row: list) ->tuple:
    """
    Utility generator which processes two-item tuples in a list.
    :param row:
    :return: tuple of form (str, str); e.g. ('A10', 'Project/Programme Name')
    """
    for item in row:
        yield item[0], item[1]


def get_cell_umi_read_string(read_id, sep='_'):
    """ extract the umi and cell barcode from the read id (input as a
    string) using the specified separator """
    try:
        return read_id.split(sep)[-1].encode('utf-8'), read_id.split(sep)[-2
            ].encode('utf-8')
    except IndexError:
        raise ValueError(
            'Could not extract UMI or CB from the read ID, pleasecheck UMI and CB are encoded in the read name:%s'
             % read_id)


@staticmethod
def p_axis_pts_optional_list_optional(p):
    """axis_pts_optional_list_optional : empty
                                           | axis_pts_optional_list"""
    p[0] = tuple() if p[1] is None else p[1]


def setAtomsSymbole(atomS, atomN):
    """
    Ceci est une fonction qui transforme une liste de symbole chimique
    et de nombre en une chaine de caractere.
    """
    S = ''
    if len(atomS) == len(atomN):
        for i in range(len(atomS)):
            S = S + ' ' + str(atomS[i]) + ' ' + str(atomN[i])
    return S.strip()


def axis_overlap(ax1, ax2):
    """
    Tests whether two axes overlap vertically
    """
    b1, t1 = ax1.get_position().intervaly
    b2, t2 = ax2.get_position().intervaly
    return t1 > b2 and b1 < t2


def unique_iterator(seq):
    """
    Returns an iterator containing all non-duplicate elements
    in the input sequence.
    """
    seen = set()
    for item in seq:
        if item not in seen:
            seen.add(item)
            yield item


def is_numlike(obj):
    """
    Helper function used to determine if an object behaves like a number.
    """
    try:
        obj + 1.0
    except TypeError:
        return False
    else:
        return True


def core(event):
    """ show core objects. """
    from meds.core import kernel
    mod = kernel.load('meds.core')
    for name in dir(mod):
        if event._parsed.rest and event._parsed.rest not in name:
            continue
        obj = getattr(mod, name)
        event.reply('%s %s' % (name, obj))


def to_readable_bytes(byte_count: (int or None)) ->str:
    """Take in a number of bytes (integer) and write it out in a human readable format (for printing). This
    method will output a number like "23.52 GiBs", "1.3 MiBs", or "324.45 KiBs" for the corresponding byte count.
    1 GiB = 1024 MiBs, 1 MiB = 1024 KiBs, and 1 KiB = 1024 Bytes.

    :param byte_count: The number of bytes to format
    :return: The human-readable string
    """
    if type(byte_count) != int:
        return '??? Bytes'
    if byte_count < 2 ** 20:
        return str(round(byte_count / 2 ** 10, 2)) + ' KiBs'
    if byte_count < 2 ** 30:
        return str(round(byte_count / 2 ** 20, 2)) + ' MiBs'
    else:
        return str(round(byte_count / 2 ** 30, 2)) + ' GiBs'


def move(library, session, source_space, source_offset, source_width,
    destination_space, destination_offset, destination_width, length):
    """Moves a block of data.

    Corresponds to viMove function of the VISA library.

    :param library: the visa library wrapped by ctypes.
    :param session: Unique logical identifier to a session.
    :param source_space: Specifies the address space of the source.
    :param source_offset: Offset of the starting address or register from which to read.
    :param source_width: Specifies the data width of the source.
    :param destination_space: Specifies the address space of the destination.
    :param destination_offset: Offset of the starting address or register to which to write.
    :param destination_width: Specifies the data width of the destination.
    :param length: Number of elements to transfer, where the data width of the elements to transfer
                   is identical to the source data width.
    :return: return value of the library call.
    :rtype: :class:`pyvisa.constants.StatusCode`
    """
    return library.viMove(session, source_space, source_offset,
        source_width, destination_space, destination_offset,
        destination_width, length)


def _snoozed():
    """Returns a query item matching messages that are snoozed."""
    return f'is:snoozed'


@classmethod
def check(cls, identifier):
    """
        Given an identifier, this should check whether the identifier is
        still considered valid or not - or whether it cannot make that
        assumption.

        This should return one of the three following:

            ScanProcess.IS_VALID
                If the identifier is still active and valid, this should
                be returned

            ScanProcess.NOT_VALID
                If the identifier should no longer be considered within
                fracture this should be returned

            ScanProcess.UNKNOWN
                If the identifier is not digestable by this process for
                any reason this should be returned.

        :return: int
        """
    return cls.UNKNOWN


def sample_perturb(counts_frame, crime_type, pct_change):
    """
    Utility function to increase the counts of specific crime types
    after sampling by a given percentage.
    Inputs : counts_frame, the counts of crime dataframe produced by sampler
             crime_type, string of crime type that we want to increase
                         counts for
             pct_change, the percentage change (negative or positive) of crime
                         counts desired.
    Outputs: new_counts_frame, identical dataframe passed but with increased
                               crime counts for specific crime type
    """
    new_counts_frame = counts_frame.copy()
    mask = new_counts_frame.Crime_type == crime_type
    mask_frame = new_counts_frame[mask]
    new_counts_frame.loc[mask, 'Counts'] = round(mask_frame.Counts *
        pct_change, 0)
    new_counts_frame['Counts'] = new_counts_frame['Counts'].astype(int)
    return new_counts_frame


def format_import(names):
    """Format an import line"""
    parts = []
    for _, name, asname in names:
        if asname is None:
            parts.append(name)
        else:
            parts.append(name + ' as ' + asname)
    line = 'import ' + ', '.join(parts) + '\n'
    return line


def cancel_spot_instance_requests(DryRun=None, SpotInstanceRequestIds=None):
    """
    Cancels one or more Spot instance requests. Spot instances are instances that Amazon EC2 starts on your behalf when the bid price that you specify exceeds the current Spot price. Amazon EC2 periodically sets the Spot price based on available Spot instance capacity and current Spot instance requests. For more information, see Spot Instance Requests in the Amazon Elastic Compute Cloud User Guide .
    See also: AWS API Documentation

    Examples
    This example cancels a Spot Instance request.
    Expected Output:

    :example: response = client.cancel_spot_instance_requests(
        DryRun=True|False,
        SpotInstanceRequestIds=[
            'string',
        ]
    )

    :type DryRun: boolean
    :param DryRun: Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation . Otherwise, it is UnauthorizedOperation .

    :type SpotInstanceRequestIds: list
    :param SpotInstanceRequestIds: [REQUIRED]
            One or more Spot instance request IDs.
            (string) --

    :rtype: dict
    :return: {
        'CancelledSpotInstanceRequests': [
            {
                'SpotInstanceRequestId': 'string',
                'State': 'active'|'open'|'closed'|'cancelled'|'completed'
            },
        ]
    }

    """
    pass


@classmethod
def get(cls, *args, **kwargs):
    """
        Returns a single object based on the passed filter constraints.

        This is a pass-through to the model objects().:method:`~cqlengine.queries.get`.
        """
    return cls.objects.get(*args, **kwargs)


def get_model_class(full_name, settings_meta):
    """
    Returns a model class loaded from *full_name*. *settings_meta* is the name
    of the corresponding settings variable (used for error messages).
    """
    from django.core.exceptions import ImproperlyConfigured
    try:
        app_label, model_name = full_name.split('.')
    except ValueError:
        raise ImproperlyConfigured(
            "%s must be of the form 'app_label.model_name'" % settings_meta)
    model_class = None
    try:
        from django.apps import apps
        model_class = apps.get_model(app_label, model_name)
    except ImportError:
        from django.db.models import get_model
        model_class = get_model(app_label, model_name)
    if model_class is None:
        raise ImproperlyConfigured(
            "%s refers to model '%s' that has not been installed" % (
            settings_meta, full_name))
    return model_class


@conf
def msvc_common_flags(conf):
    """
	Setup the flags required for executing the msvc compiler
	"""
    v = conf.env
    v.DEST_BINFMT = 'pe'
    v.append_value('CFLAGS', ['/nologo'])
    v.append_value('CXXFLAGS', ['/nologo'])
    v.append_value('LINKFLAGS', ['/nologo'])
    v.DEFINES_ST = '/D%s'
    v.CC_SRC_F = ''
    v.CC_TGT_F = ['/c', '/Fo']
    v.CXX_SRC_F = ''
    v.CXX_TGT_F = ['/c', '/Fo']
    if (v.MSVC_COMPILER == 'msvc' and v.MSVC_VERSION >= 8 or v.
        MSVC_COMPILER == 'wsdk' and v.MSVC_VERSION >= 6):
        v.CC_TGT_F = ['/FC'] + v.CC_TGT_F
        v.CXX_TGT_F = ['/FC'] + v.CXX_TGT_F
    v.CPPPATH_ST = '/I%s'
    v.AR_TGT_F = v.CCLNK_TGT_F = v.CXXLNK_TGT_F = '/OUT:'
    v.CFLAGS_CRT_MULTITHREADED = v.CXXFLAGS_CRT_MULTITHREADED = ['/MT']
    v.CFLAGS_CRT_MULTITHREADED_DLL = v.CXXFLAGS_CRT_MULTITHREADED_DLL = ['/MD']
    v.CFLAGS_CRT_MULTITHREADED_DBG = v.CXXFLAGS_CRT_MULTITHREADED_DBG = ['/MTd'
        ]
    (v.CFLAGS_CRT_MULTITHREADED_DLL_DBG) = (v.
        CXXFLAGS_CRT_MULTITHREADED_DLL_DBG) = ['/MDd']
    v.LIB_ST = '%s.lib'
    v.LIBPATH_ST = '/LIBPATH:%s'
    v.STLIB_ST = '%s.lib'
    v.STLIBPATH_ST = '/LIBPATH:%s'
    if v.MSVC_MANIFEST:
        v.append_value('LINKFLAGS', ['/MANIFEST'])
    v.CFLAGS_cshlib = []
    v.CXXFLAGS_cxxshlib = []
    v.LINKFLAGS_cshlib = v.LINKFLAGS_cxxshlib = ['/DLL']
    v.cshlib_PATTERN = v.cxxshlib_PATTERN = '%s.dll'
    v.implib_PATTERN = '%s.lib'
    v.IMPLIB_ST = '/IMPLIB:%s'
    v.LINKFLAGS_cstlib = []
    v.cstlib_PATTERN = v.cxxstlib_PATTERN = '%s.lib'
    v.cprogram_PATTERN = v.cxxprogram_PATTERN = '%s.exe'
    v.def_PATTERN = '/def:%s'


def shuffle_analysis(data_0, data_1, n_shuffles=100, **cca_kwargs):
    """
    Perform CCA on shuffled data

    :param data_0:
    :param data_1:
    :param n_shuffles:
    :return:
    """
    pass


def _bound_mean_difference_ci(lower_ci, upper_ci):
    """Bound mean difference and normalized mean difference CI.

    Since the plausible range of mean difference and normalized mean
    difference is [-1, 1], bound the confidence interval to this range.
    """
    lower_ci = lower_ci if lower_ci > -1 else -1
    upper_ci = upper_ci if upper_ci < 1 else 1
    return lower_ci, upper_ci


def select_pinned():
    """Select all pinned UV vertices

    """
    pass


def flip_normals():
    """Flip the direction of selected faces’ normals (and of their vertices)

    """
    pass


@classmethod
def get_deserialization_instance(cls, ctx):
    """Get an empty native type so that the deserialization logic can set
        its attributes.
        """
    if cls.__orig__ is None:
        return cls()
    return cls.__orig__()


@classmethod
def render_select(cls, result, fields=''):
    """Format the result to feed a select with id and name field.

    result: dict
    fields: list

    return: list with id and name

    """
    if not fields:
        fields = cls.ID_KEY, cls.KEYWORD_FIELD
    formatted_result = []
    for p in result['hits']['hits']:
        if fields[0] == '_id':
            first_field = p[fields[0]]
        else:
            first_field = p['_source'][fields[0]]
        formatted_result.append((first_field, p['_source'][fields[1]]))
    return formatted_result


def getShortestString(strings, default=None):
    """
    Gets the shortest string out of a list
    @param strings: A collection of strings
    @type strings: list of str
    @param default: A default value to use if strings is empty
    @type default: str or None
    @return: The shortest string in the list, or the default
    @rtype: str
    """
    if len(strings) == 0:
        return default
    else:
        return min(strings, key=len)


@staticmethod
def _listbytes(data):
    """Python 2 compatible int iterator from str.

        :param data: the string to int iterate upon.
        """
    return map(ord, data)


def orgDatetime(dt, tz):
    """Timezone aware datetime to YYYY-MM-DD DayofWeek HH:MM str in localtime.
    """
    return dt.astimezone(tz).strftime('<%Y-%m-%d %a %H:%M>')


def doc_template_provider(m):
    """ Decorator to mark docstring from method `m` as being a doc template

    We label the docstring of `m` (``m.__doc__``) to be put into the
    ``_doc_templates`` attribute of the enclosing class, when the klass is
    decorated with the ``doc_templater`` decorator.
    """
    m._doc_template = m.__doc__
    m.__doc__ = None
    return m


def percentage_change(old_value, new_value, multiply=True, fail_silently=True):
    """
    Accepts two integers, an old and a new number,
    and then measures the percent change between them.

    The change between the two numbers is determined
    and then divided into the original figure.

    By default, it is then multiplied by 100, and
    returning as a float.

    If you don't want the number multiplied by 100,
    set the 'multiply' kwarg to False.

    If you divide into zero -- an illegal operation -- a null value
    is returned by default. If you prefer for an error to be raised,
    set the kwarg 'fail_silently' to False.

    h3. Example usage

        >> import calculate
        >> calculate.percentage_change(2, 10)
        400.0

    h3. Documentation

        * "percentage_change":http://en.wikipedia.org/wiki/Percentage_change
    """
    change = new_value - old_value
    try:
        percentage_change = change / float(old_value)
        if multiply:
            percentage_change = percentage_change * 100
        return percentage_change
    except ZeroDivisionError:
        if fail_silently:
            return None
        else:
            raise ZeroDivisionError("Sorry. You can't divide into zero.")


def clean_opentea_list_item(item_in, existing_names):
    """Add # to list elements to avoid duplication."""
    out_name = item_in['name']
    item_out = item_in.copy()
    if out_name == '':
        out_name = '#'
    if out_name in existing_names:
        while out_name in existing_names:
            out_name += '#'
    item_out['name'] = out_name
    return out_name, item_out


def _normalize_per_sample(arr):
    """ Divides respective column of arr with its sum. """
    return arr.divide(arr.sum(axis=0), axis=1)


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


@classmethod
def unpack_scalar(cls, dataset, data):
    """
        Given a dataset object and data in the appropriate format for
        the interface, return a simple scalar.
        """
    if data.shape == (1, 1):
        return data[0, 0]
    return data


def createFile(filename, content):
    """Create a file using the given filename and contents)"""
    with open(filename, 'w') as f:
        f.write(content)


@staticmethod
def p_identifier(p):
    """identifier : IDENT"""
    p[0] = p[1]


@classmethod
def dump_str(cls, content, spaces=0):
    """
        Transforms a string to a string

        :param content: the string to transform
        :type content: ``str``

        :param spaces: shift to the right from containing elements
        :type spaces: ``int``

        :return: the related string

        """
    lines = content.split('\n')
    if len(lines) == 1:
        lines = content.split('\\n')
    if len(lines) == 1:
        if len(content) > 0 and content[-1] in ('-', '\\', '|'):
            return '"' + content + '"'
        return content
    text = '|'
    spaces += 2
    for line in lines:
        text += '\n'
        if len(line) > 0:
            text += ' ' * spaces + line
    return text


@staticmethod
def _get_optimizer(learning_rate):
    """ Returns the optimizer to use for this model """
    del learning_rate


def action_from_number(move, player, sw, dice_roll):
    """ Takes the output of the network, samples from the probs, does the move if possible, returns move_type,  the move itself and the move number"""
    board = sw.board
    move_number = move * 1
    start_loc = 9, 9
    for row in board.board:
        for cell in row:
            if cell.player == player:
                start_loc = cell.y, cell.x
    if start_loc == (9, 9):
        action = player, (9, 9), (9, 9), dice_roll, False, False
        return 'dead', action, 0
    drone, pj = False, False
    move_type = ''
    if move // 64 == 0:
        move_type = 'normal'
    elif move // 64 == 1:
        move -= 64
        pj = True
        move_type = 'powerjump'
    else:
        move -= 128
        drone = True
        move_type = 'drone'
    x = move // 8
    y = move % 8
    action = player, start_loc, (x, y), dice_roll, drone, pj
    return move_type, action, move_number


def get_const(tab):
    """
    Finds cols in `tab` whose values are constant (same everywhere).

    A table with zero rows has no const cols.  A table with one row has all
    const cols.

    @return
      A mapping from col name to constant value, for cols whose arrays have
      the same value in every index.
    """
    const = dict()
    if tab.num_rows == 0:
        return const
    for name, arr in tab.arrs.items():
        val = arr[0]
        if (arr == val).all():
            const[name] = val
    return const


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

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

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


def get_profile_index_and_values(config_file, profile_name):
    """Extract profile and its index from config file"""
    initial_profile = {'index': -1, 'profile': None}
    profiles = config_file.get('profiles')
    for index, profile in enumerate(profiles):
        if profile['name'] == profile_name:
            initial_profile['index'] = index
            initial_profile['profile'] = profile
    return initial_profile


def cat_file(session, path):
    """
    Get file content

    :param session: instance of Windows, which hold session to win machine
    :type session: winremote.Windows
    :param path: path to file
    :type path: str
    :returns: content of file
    :rtype: str
    """
    return session.run_cmd('type "%s"' % path)[1]


def changeTimeFormat(runtime):
    """Improves the readability of process-runtime.

    Only useful, if runtime is greater than 60s.
    :float runtime: the runtime in seconds
    :string return: runtime divided in hours, minutes and seconds
    """
    result = ''
    runtime_int = int(runtime)
    runtime_orig = runtime_int
    if runtime_int > 3600:
        result += str(runtime_int / 3600) + 'h:'
        runtime_int = runtime_int % 3600
    result += str('{:02}'.format(runtime_int / 60)) + 'm:'
    runtime_int = runtime_int % 60
    runtime -= float(runtime_orig - runtime_int)
    return result + str('{:05.2f}'.format(runtime)) + 's'


def _obtain_weights_CSVLogger_filenames(violation_class, train_mode,
    model_name, nb_of_conv_layers_to_fine_tune):
    """Obtains the polished filenames for the weights and the CSVLogger of the model.

    # Arguments
        violation_class: violation_class: one of `cl` (HRA dataset with 2 classes - [i]'child_labour' and [ii]'no violation')
            or `dp` (HRA dataset with 2 classes - [i]'displaced_populations' and [ii]'no violation').
        train_mode: String to declare the train mode of the model (how many layers will be frozen during training).
            - `feature_extraction` taking the convolutional base of a previously-trained network,
                running the new data through it, and training a new classifier on top of the output.
            - `fine_tuning` unfreezing a few of the top layers of a frozen conv. base used for feature extraction,
                and jointly training both the newly added part of the model and these top layers.
        model_name: String to declare the name of the model
        nb_of_conv_layers_to_fine_tune: integer to indicate the number of convolutional
            layers to fine-tune. One of `1`, `2` or `3`.

    # Returns
        Two strings that will serve as the filenames for the weights and the CSVLogger respectively.
    """
    if violation_class == 'cl':
        if train_mode == 'feature_extraction':
            weights_filename = ('trained_models/' + 'cl_' + model_name +
                '_weights_feature_extraction_tf_dim_ordering_tf_kernels.h5')
            CSVLogger_filename = model_name + '_cl_feature_extraction.csv'
        elif nb_of_conv_layers_to_fine_tune == 1:
            weights_filename = ('trained_models/' + 'cl_' + model_name +
                '_weights_one_layer_tf_dim_ordering_tf_kernels.h5')
            CSVLogger_filename = model_name + '_cl_fine_tuning_one_layer.csv'
        elif nb_of_conv_layers_to_fine_tune == 2:
            weights_filename = ('trained_models/' + 'cl_' + model_name +
                '_weights_two_layers_tf_dim_ordering_tf_kernels.h5')
            CSVLogger_filename = model_name + '_cl_fine_tuning_two_layers.csv'
        elif nb_of_conv_layers_to_fine_tune == 3:
            weights_filename = ('trained_models/' + 'cl_' + model_name +
                '_weights_three_layers_tf_dim_ordering_tf_kernels.h5')
            CSVLogger_filename = (model_name +
                '_cl_fine_tuning_three_layers.csv')
        else:
            raise NotImplementedError(
                'The `nb_of_conv_layers_to_fine_tune` argument should be either `1`, `2` or `3`. More than 3 conv. blocks are not supported because the more parameters we are training, the more we are at risk of overfitting.'
                )
    elif violation_class == 'dp':
        if train_mode == 'feature_extraction':
            weights_filename = ('trained_models/' + 'dp_' + model_name +
                '_weights_feature_extraction_tf_dim_ordering_tf_kernels.h5')
            CSVLogger_filename = model_name + '_dp_feature_extraction.csv'
        elif nb_of_conv_layers_to_fine_tune == 1:
            weights_filename = ('trained_models/' + 'dp_' + model_name +
                '_weights_one_layer_tf_dim_ordering_tf_kernels.h5')
            CSVLogger_filename = model_name + '_dp_fine_tuning_one_layer.csv'
        elif nb_of_conv_layers_to_fine_tune == 2:
            weights_filename = ('trained_models/' + 'dp_' + model_name +
                '_weights_two_layers_tf_dim_ordering_tf_kernels.h5')
            CSVLogger_filename = model_name + '_dp_fine_tuning_two_layers.csv'
        elif nb_of_conv_layers_to_fine_tune == 3:
            weights_filename = ('trained_models/' + 'dp_' + model_name +
                '_weights_three_layers_tf_dim_ordering_tf_kernels.h5')
            CSVLogger_filename = (model_name +
                '_dp_cl_fine_tuning_three_layers.csv')
        else:
            raise NotImplementedError(
                'The `nb_of_conv_layers_to_fine_tune` argument should be either `1`, `2` or `3`. More than 3 conv. blocks are not supported because the more parameters we are training, the more we are at risk of overfitting.'
                )
    return weights_filename, CSVLogger_filename


def _filter_none(lst):
    """Filter None values out of lst

    :param lst: list to be filtered
    :return: generator filtering Nones
    """
    return filter(lambda value: value is not None, lst)


def error_on_blank(_value, _error):
    """Raises an error if blank"""
    if _value not in [None, '']:
        return _value
    else:
        raise Exception(_error)


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

        :param FileLikeCommentType val:
        :rtype: EventType
        """
    return cls('file_like_comment', val)


def get_overview_page_analysis_table_args(overview_page_analysis):
    """Creates a gviz DataTable object from an OverviewPageAnalysis proto.

  Args:
    overview_page_analysis: An overview_page_pb2.OverviewPageAnalysis.

  Returns:
    Returns a gviz_api.DataTable
  """
    table_description = [('selfTimePercent', 'number', 'Time (%)'), (
        'cumulativeTimePercent', 'number', 'Cumulative time (%)'), (
        'category', 'string', 'Category'), ('operation', 'string',
        'Operation'), ('flopRate', 'number', 'GFLOPs/Sec')]
    data = []
    for op in overview_page_analysis.top_device_ops:
        row = [op.self_time_fraction, op.cumulative_time_fraction, str(op.
            category), str(op.name), op.flop_rate]
        data.append(row)
    device_compute_16bit_percent = '{:.1f}'.format(overview_page_analysis.
        device_compute_16bit_percent)
    device_compute_32bit_percent = '{:.1f}'.format(overview_page_analysis.
        device_compute_32bit_percent)
    remark_text = overview_page_analysis.remark_text
    remark_color = overview_page_analysis.remark_color
    custom_properties = {'device_compute_16bit_percent':
        device_compute_16bit_percent, 'device_compute_32bit_percent':
        device_compute_32bit_percent, 'remark_text': remark_text,
        'remark_color': remark_color}
    return table_description, data, custom_properties


@classmethod
def add_arguments(cls):
    """
        sub parser document
        """
    return [(('--skip-enable-services',), dict(action='store_true', help=
        'skip enable system services')), (('--skip-upload',), dict(action=
        'store_true', help='skip upload config files')), (('--skip-crontab'
        ,), dict(action='store_true', help='skip upload crontab')), ((
        '--skip-timezone',), dict(action='store_true', help=
        'skip set timezone')), (('--skip-hostname',), dict(action=
        'store_true', help='skip set hostname')), (('--skip-dns',), dict(
        action='store_true', help='skip config dns')), (('--enable-certbot'
        ,), dict(action='store_true', help='enable config ssl certificate')
        ), (('--reload',), dict(nargs='+', help='set reload service')), ((
        '--restart',), dict(nargs='+', help='set restart service')), ((
        '--start',), dict(nargs='+', help='set start service')), (('--stop'
        ,), dict(nargs='+', help='set stop service')), (('--parallel', '-P'
        ), dict(action='store_true', help=
        'default to parallel execution method'))]


@classmethod
def get_option_names(cls):
    """Get names of all options."""
    return list(map(lambda x: x.name, cls.get_options()))


def returns_from_prices(prices):
    """
    Calculate the returns given prices.

    :param prices: adjusted (daily) closing prices of the asset, each row is a
                   date and each column is a ticker/id.
    :type prices: pd.DataFrame
    :return: (daily) returns
    :rtype: pd.DataFrame
    """
    return prices.pct_change().dropna(how='all')


@staticmethod
def __funcMatch(funcObj, line):
    """Returns True if the function object matches"""
    minLine = funcObj.keywordLine
    maxLine = funcObj.colonLine
    for decor in funcObj.decorators:
        if decor.line < minLine:
            minLine = decor.line
    return line >= minLine and line <= maxLine


def _calc_epa_func(thermostat):
    """ Takes an individual thermostat and runs the
    calculate_epa_field_savings_metrics method. This method is necessary for
    the multiprocessing pool as map / imap need a function to run on.

    Parameters
    ----------
    thermostat : thermostat

    Returns
    -------
    results : results from running calculate_epa_field_savings_metrics
    """
    results = thermostat.calculate_epa_field_savings_metrics()
    return results


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

        :param SfTeamJoinFromOobLinkDetails val:
        :rtype: EventDetails
        """
    return cls('sf_team_join_from_oob_link_details', val)


def reject_qualification_request(QualificationRequestId=None, Reason=None):
    """
    The RejectQualificationRequest operation rejects a user's request for a Qualification.
    You can provide a text message explaining why the request was rejected. The Worker who made the request can see this message.
    See also: AWS API Documentation

    :example: response = client.reject_qualification_request(
        QualificationRequestId='string',
        Reason='string'
    )

    :type QualificationRequestId: string
    :param QualificationRequestId: [REQUIRED]
            The ID of the Qualification request, as returned by the ListQualificationRequests operation.

    :type Reason: string
    :param Reason: A text message explaining why the request was rejected, to be shown to the Worker who made the request.

    :rtype: dict
    :return: {}

    :returns:
    (dict) --

    """
    pass


def build_url(handle_or_url):
    """Builds URL for filestack-hosted file based on file handler or,
    if it is already URL - return it."""
    if handle_or_url.startswith('https://') or handle_or_url.startswith(
        'http://'):
        return handle_or_url
    else:
        return 'https://cdn.filestackcontent.com/{0}'.format(handle_or_url)


@staticmethod
def standardCBBFluxByPartition(alpha, beta, scale=1.0, TK3=False):
    """
        calculated CBB flux distribution
        :param beta: fraction of carbon exported as starch
        :param alpha: fraction of trioses (1-beta) exported through TPTGAP
        :param scale: optional, scales all fluxes by this factor
        :param TK3: optional, if set adds a third TK reaction (E4P+S7P=R5P+F6P). Value is 0..1 proportion of vTK1. TK fluxes are adjusted as (vTK1, vTK2, 0) -> ((1-TK3)*vTK1, vTK2+TK3*vTK1, TK3*vTK1)
        :return: dict with fluxes. Keys are standard 13 reactions (7 reversible, among these two standard TKs; 6 irreversible). Normalized so that v_RuBisCO=scale (defaults to 1)
        """
    v = {}
    v['TPI'] = 6.0 - 2.0 * alpha + (2.0 * alpha - 1.0) * beta
    v['RPE'] = 4.0
    v['RPI'] = 2.0
    v['TK1'] = 2.0
    v['TK2'] = 2.0
    v['Ald1'] = 2.0 + beta
    v['Ald2'] = 2.0
    v['RuBisCO'] = 6.0
    v['TPTGAP'] = (2.0 - 2.0 * beta) * alpha
    v['TPTDHAP'] = (2.0 - 2.0 * beta) * (1.0 - alpha)
    v['vstarch'] = beta
    v['FBP'] = 2.0 + beta
    v['SBP'] = 2.0
    if TK3 is not False:
        v['TK3'] = TK3 * v['TK1']
        v['TK2'] = v['TK2'] + TK3 * v['TK1']
        v['TK1'] = (1 - TK3) * v['TK1']
    for r in v.keys():
        v[r] = v[r] / 6.0 * scale
    return v


def get_version(package):
    """Return a package version as a 3-tuple of integers.
    """
    if package == 'numba':
        import numba
        v = numba.__version__.replace('rc', '.').replace('dev', '.').split('.')
        return tuple(map(int, v[:3]))
    raise NotImplementedError(f'get version of package {package}')


@classmethod
def from_geometry(cls, geometry):
    """ Return Extent instance. """
    x1, x2, y1, y2 = geometry.GetEnvelope()
    bbox = x1, y1, x2, y2
    sr = geometry.GetSpatialReference()
    return cls(bbox=bbox, sr=sr)


def get(text=''):
    """
    overpython.get(text="")

    Get user input from console.
    """
    return input(text)


@staticmethod
def _str_to_num(s):
    """Convert a string to either an `int` or a `float`.

        Troublingly, the API likes values like "18", without a trailing
        ".0", for whole numbers. So we use `int`s for integers and
        `float`s for non-whole numbers.
        """
    f = float(s)
    if f == int(f):
        return int(f)
    else:
        return f


def gen_enoslib_roles(resources_or_topology):
    """Generator for the resources or topology."""
    for k1, v1 in resources_or_topology.items():
        for k2, v2 in v1.items():
            if isinstance(v2, dict):
                for k3, v3 in v2.items():
                    yield {'group': k1, 'role': k3, 'flavor': k2, 'number': v3}
            else:
                yield {'group': 'default_group', 'role': k2, 'flavor': k1,
                    'number': v2}


def serialise_double(i, context=None):
    """Serialise a Double value.

    i -- integer (1 or 2)

    (unknown values are treated as 1)

    """
    if i == 2:
        return b'2'
    return b'1'


def case_feature(token):
    """Returns zero or one case features for the `token` string."""
    if token.islower():
        return '*lowercase*'
    if token.isupper():
        return '*uppercase*'
    if token.istitle():
        return '*titlecase*'
    return


def csv_to_json(csv_filepath, json_filepath, fieldnames, ignore_first_line=True
    ):
    """ Convert a CSV file in `csv_filepath` into a JSON file in `json_filepath`.

    Parameters
    ----------
    csv_filepath: str
        Path to the input CSV file.

    json_filepath: str
        Path to the output JSON file. Will be overwritten if exists.

    fieldnames: List[str]
        Names of the fields in the CSV file.

    ignore_first_line: bool
    """
    import csv
    import json
    csvfile = open(csv_filepath, 'r')
    jsonfile = open(json_filepath, 'w')
    reader = csv.DictReader(csvfile, fieldnames)
    rows = []
    if ignore_first_line:
        next(reader)
    for row in reader:
        rows.append(row)
    json.dump(rows, jsonfile)
    jsonfile.close()
    csvfile.close()


def item_assert(item):
    """ Assert that Item is correctly constructed """
    assert isinstance(item.type, str)
    assert isinstance(item.desc, str)
    assert isinstance(item.error, str)
    assert isinstance(item.line_no, int)
    assert isinstance(item.line_offset, int)
    assert item.desc
    assert item.type
    assert item.error


def identify_max_speed_velocity_ratio(velocities, engine_speeds_out,
    idle_engine_speed, stop_velocity):
    """
    Identifies the maximum speed velocity ratio of the gear box [h*RPM/km].

    :param velocities:
        Vehicle velocity [km/h].
    :type velocities: numpy.array

    :param engine_speeds_out:
        Engine speed [RPM].
    :type engine_speeds_out: numpy.array

    :param idle_engine_speed:
        Engine speed idle median and std [RPM].
    :type idle_engine_speed: (float, float)

    :param stop_velocity:
        Maximum velocity to consider the vehicle stopped [km/h].
    :type stop_velocity: float

    :return:
        Maximum speed velocity ratio of the gear box [h*RPM/km].
    :rtype: float
    """
    b = velocities > stop_velocity
    b &= engine_speeds_out > idle_engine_speed[0]
    return max(engine_speeds_out[b] / velocities[b])


def psi_H_Mg_Cl_HMW84(T, P):
    """c-c'-a: hydrogen magnesium chloride [HMW84]."""
    psi = -0.011
    valid = T == 298.15
    return psi, valid


def is_published_or_draft(post):
    """returns True/False based on `published` metadata in post"""
    if post is not None and ('published' in post.meta and post.meta[
        'published'] is True or 'status' in post.meta and post.meta[
        'status'].lower() == 'draft'):
        return post


def laplacian(y, factor=1.0, exp=False):
    """Return the laplacian of `y` times `factor` or the exponential of this.

        Parameters
        ----------
        factor : float
           Additional factor (mostly used with `exp=True`).  The
           implementation must be careful to allow the factor to
           broadcast across the components.
        exp : bool
           If `True`, then compute the exponential of the laplacian.
           This is used for split evolvers.
        """


def generate_presigned_url(ClientMethod=None, Params=None, ExpiresIn=None,
    HttpMethod=None):
    """
    Generate a presigned url given a client, its method, and arguments

    :type ClientMethod: string
    :param ClientMethod: The client method to presign for

    :type Params: dict
    :param Params: The parameters normally passed to
            ClientMethod.

    :type ExpiresIn: int
    :param ExpiresIn: The number of seconds the presigned url is valid
            for. By default it expires in an hour (3600 seconds)

    :type HttpMethod: string
    :param HttpMethod: The http method to use on the generated url. By
            default, the http method is whatever is used in the method's model.

    """
    pass


def delete_proxy():
    """Delete movie clip proxy files from the hard drive

    """
    pass


def unlink():
    """Remove Pose Library from active Object

    """
    pass


def compute_bounds(actor):
    """Compute Bounds of actor.

    Parameters
    ----------
    actor : actor

    """
    actor.GetMapper().GetInput().ComputeBounds()


def avg(l):
    """Returns the average of a list of numbers."""
    return sum(l) / float(len(l))


@staticmethod
def is_security_label():
    """ Determines if the object is a Security Label. """
    return False


def timing(stat, value, rate=1, buf=None, rate_applied=False):
    """
        Log timing information in milliseconds.

        ``stat`` is the name of the metric to record and ``value`` is
        the timing measurement in milliseconds. Note that Statsd
        maintains several data points for each timing metric, so
        timing metrics can take more disk space than counters or
        gauges.
        """


def _fix_label_alt_id(table):
    """
    Utility that fixes the 'label_alt_id' column to match what is
    expected in the mmCIF format.

    :param table: pandas DataFrame object
    :return: returns a modified pandas DataFrame
    """
    table['label_alt_id'] = table['label_alt_id'].str.replace('\\ |\\?', '.')
    table['label_alt_id'] = table['label_alt_id'].fillna('.').astype(str)
    return table


def accentuate(letter):
    """ Function that adds the accentuation mark of a letter:

        :param letter: a letter to be accentuated
        :type letter: str
        :returns: letter accentuated
        :rtype: str

        :Example:

        >>> import chana.syllabificator
        >>> chana.syllabificator.accentuate('a')
        á

    """
    if letter == 'a':
        letter = 'á'
    elif letter == 'e':
        letter = 'é'
    elif letter == 'i':
        letter = 'í'
    elif letter == 'o':
        letter = 'ó'
    elif letter == 'u':
        letter = 'ú'
    return letter


def medial_wall_to_nan(in_file, subjects_dir, target_subject, newpath=None):
    """ Convert values on medial wall to NaNs
    """
    import nibabel as nb
    import numpy as np
    import os
    fn = os.path.basename(in_file)
    if not target_subject.startswith('fs'):
        return in_file
    cortex = nb.freesurfer.read_label(os.path.join(subjects_dir,
        target_subject, 'label', '{}.cortex.label'.format(fn[:2])))
    func = nb.load(in_file)
    medial = np.delete(np.arange(len(func.darrays[0].data)), cortex)
    for darray in func.darrays:
        darray.data[medial] = np.nan
    out_file = os.path.join(newpath or os.getcwd(), fn)
    func.to_filename(out_file)
    return out_file


@staticmethod
def __new__(S, *more):
    """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
    pass


def unsubscribe(receiver):
    """Unregister an IStatusReceiver. No further status messages will be
        delivered."""


def id_num_digits(parts):
    """Returns the tuple (label, number of digits) for the ID box.

       Receives the splitted text of the template for the exam, which
       may contain a key like 'id-box(9,NIA)' in an odd position, in
       which NIA is the label and 9 the number of digits. If the key
       does not exist in the template, returns (0, None).

    """
    for part in parts[1::2]:
        if part.startswith('id-box'):
            data = part[7:-1].split(',')
            return data[1], int(data[0])
    return None, 0


def item_perceel_adapter(obj, request):
    """
    Adapter for rendering an object of
    :class: `crabpy.gateway.capakey.Perceel` to json.
    """
    return {'id': obj.id, 'sectie': {'id': obj.sectie.id, 'afdeling': {'id':
        obj.sectie.afdeling.id, 'naam': obj.sectie.afdeling.naam,
        'gemeente': {'id': obj.sectie.afdeling.gemeente.id, 'naam': obj.
        sectie.afdeling.gemeente.naam}}}, 'capakey': obj.capakey, 'percid':
        obj.percid, 'centroid': obj.centroid, 'bounding_box': obj.bounding_box}


def get_minutes_of_day(dt):
    """
    计算datetime时刻是一天中第多少分钟
    :param dt:
    :return:
    """
    return dt.hour * 60 + dt.minute


def _qualname(cls):
    """
    Returns a fully qualified name of the class, to avoid name collisions.
    """
    return u'.'.join([cls.__module__, cls.__name__])


def check_bool_option(args: dict, option: str) ->bool:
    """ Check if theres a boolean option with the given name
        - args: the args to check for the option
        - option: the name of the option to check for
        - return: whether option is True or False
    """
    try:
        return args[option] > 0 if type(args[option]) == int else args[option]
    except KeyError:
        return False


@staticmethod
def p_value_range(p):
    """value_range : RANGE"""
    p[0] = p[1]


def octave_calculator(pitch):
    """Returns pitch class and octave for a given absolute pitch.

    >>> octave_calculator(50)
    (2, 4)
    """
    p_class = pitch % 12
    octave = pitch / 12
    return p_class, octave


def namespaced_args(args, namespace):
    """
    Filter from handler.args the keys relevant for namespace, i.e. those
    prefixed with the namespace, or those without a prefix.
    """
    if not namespace.endswith(':'):
        namespace += ':'
    result = {}
    for key, val in args.items():
        if key.startswith(namespace):
            result[key[len(namespace):]] = val
        elif ':' not in key:
            result[key] = val
    return result


@property
def __members__(cls):
    """Returns a mapping of member name->value.

        This mapping lists all enum members, including aliases. Note that this
        is a copy of the internal mapping.

        """
    return cls._member_map_.copy()


def graphql_verify_user(client, variables):
    """
        Verifies an authentication with token
    :param client:
    :param variables: contains a token key that is the token to update
    :return:
    """
    return client.execute(
        """
    mutation VerifyToken($token: String!) {
  verifyToken(token: $token) {
    payload
  }
}"""
        , variables=variables)


def mig20191203c():
    """
  As of 0.17, rm -rf /tmp/isitfit_ec2info.cache/
  """
    import tempfile
    import os
    import glob
    del_p = os.path.join(tempfile.gettempdir(), 'isitfit_ec2info.cache')
    import shutil
    shutil.rmtree(del_p, ignore_errors=True)


@classmethod
def from_df(cls, df: object) ->dict:
    """Constructs a dictionary of sections keyed by the section.ids from a Pandas dataframe."""
    sections = {}
    for index, section_series in df.iterrows():
        sid = section_series['sid']
        sections[sid] = cls.from_series(section_series)
    return sections


def writeSamples(outfile1, outfile2, ncats, catsize, weird, weirdsize):
    """writes two files, differing only on the value of one category"""
    try:
        f = open(outfile1, 'w')
    except IOError:
        print('For some reason I cannot open ' + outfile1)
        print('Check directory permissions and/or file name')
        exit(-1)
    try:
        g = open(outfile2, 'w')
    except:
        print('For some reason I cannot open ' + outfile1)
        print('Check directory permissions and/or file name.')
        exit(-1)
    for i in range(ncats):
        f.write('cat_' + str(i) + '\t' + str(catsize) + '\n')
        g.write('cat_' + str(i) + '\t' + str(catsize) + '\n')
    f.write(weird + '\t0\n')
    g.write(weird + '\t' + str(weirdsize) + '\n')
    f.close()
    g.close()


def join(first_value, second_value, join_value='/'):
    """
    Joins two string making sure that the join value is
    not repeated in the "join point" (no double join).
    The "join point" is guaranteed to be unique.

    :type first_value: String
    :param first_value: The first string to be joined.
    :type second_value: String
    :param second_value: The second string to be joined.
    :type join_value: String
    :param join_value: The join value to be verifies and
    used in the "join point".
    :rtype: String
    :return: The joined string ensured to be with a single
    join point (not repeated).
    """
    first_value = not first_value.endswith(join_value
        ) and first_value or first_value[:-1]
    second_value = not second_value.startswith(join_value
        ) and second_value or second_value[1:]
    string_value = join_value.join((first_value, second_value))
    return string_value


def get_custom_fields_supertypes(custom_fields):
    """
    Returns a list of fields with their aggregate things.
    E.g., [{
        "id": "ga:metric1",
        "kind": "analytics#customMetric",
        "dataTypes": ["string", "currency", ...],
        "type": "METRIC",
        "group": "Custom Fields"
    }, ...]
    """
    super_fields = {}
    for fields in custom_fields.values():
        for field in fields:
            super_field = super_fields.get(field['id'], {'id': field['id'],
                'kind': field.get('kind'), 'dataTypes': set(), 'type':
                field['type'], 'group': field['group']})
            super_field['dataTypes'].add(field['dataType'])
            super_fields[field['id']] = super_field
    return list(super_fields.values())


def xray_energy(wavelength):
    """
    convert energy (keV) to wavelength (angstrom)
    """
    return 12.398 / wavelength


def getIntensity() ->float:
    """returns the intensity of the quantity measured by the device."""
    raise NotImplementedError(
        'method must be implemented to comply with the interface')


def contains(a, b):
    """Same as b in a (note reversed operands)."""
    return b in a


def layer_isolate(affect_visibility: bool=False):
    """Toggle whether the active layer is the only one that can be edited and/or visible

    :param affect_visibility: Affect Visibility, In addition to toggling the editability, also affect the visibility
    :type affect_visibility: bool
    """
    pass


def update(src, dest):
    """Updates the value of src according to dest."""
    if src < dest:
        src += 1
    elif src > dest:
        src -= 1
    return src


def obtain_input(message_text):
    """Perform input command as a function so it can be mocked."""
    return input(message_text)


def __delattr__(name):
    """ Handle activating ghosts before deleting an attribute value.

        "Special" attributes and '_p_*' attributes don't require activation.
        """


def from_yaml(yaml_setup_file):
    """
    Fills job queue from a provided yaml file

    Note: Absolute and relative paths are accepted
    Required keys and values for yaml file(these are case sensitive)

    Alternative-files key accepts nested lists, this will link the files
    in the nested list to the same simulation, thus allowing for numerous
    alternate files to be used.

    Job Scheduler yaml format:
        primary: '/path/to/your/primary/'
        alternative-files:
            - '/path/to/alt/file'
            -
              - '/path/to/alt/file1'
              - '/path/to/alt/file2'
            ..
            - '/path/to/alt/file'

    Optional key value pairs:
        image: 'docker/image' default aaraney/nwm-djs:2.0
        max-jobs: <int> (e.g., 3) default 2
        cpus: <str> (e.g., '0-4') default '0-1'
        mpi-np: <int> (e.g., 3) default
        """
    from .scheduler import Scheduler
    return Scheduler.from_yaml(yaml_setup_file)


def set_code_builder(ctx, param, value):
    """Set the code spec for defaults of following options."""
    from aiida.orm.utils.builders.code import CodeBuilder
    ctx.code_builder = CodeBuilder.from_code(value)
    return value


@staticmethod
def perf_counter_string_from_string_list(counterlist: [str]):
    """ Creates a performance counter typename string. """
    if isinstance(counterlist, str):
        print('Wrong format')
        counterlist = eval(counterlist)
    return 'PAPIPerfLowLevel<' + ', '.join(counterlist) + '>'


def vertex_random(offset: float=0.1, uniform: float=0.0, normal: float=0.0,
    seed: int=0):
    """Randomize vertices

    :param offset: Amount, Distance to offset
    :type offset: float
    :param uniform: Uniform, Increase for uniform offset distance
    :type uniform: float
    :param normal: Normal, Align offset direction to normals
    :type normal: float
    :param seed: Random Seed, Seed for the random number generator
    :type seed: int
    """
    pass


@classmethod
def new_from_obj(cls, obj, data=None, var=None, copy=False, unit=None):
    """Create a new object from another one, copying its attributes.

        Parameters
        ----------
        obj  : `mpdaf.obj.DataArray`
            The object to use as the template for the new object. This
            should be an object based on DataArray, such as an Image,
            Cube or Spectrum.
        data : array-like, optional
            An optional data array, or None to indicate that
            ``obj.data`` should be used. The default is None.
        var : array-like, optional
            An optional variance array, or None to indicate that
            ``obj.var`` should be used, or False to indicate that the
            new object should not have any variances. The default
            is None.
        copy : bool
            Copy the data and variance arrays if True (default False).

        """
    data = obj.data if data is None else data
    if var is None:
        var = obj._var
    elif var is False:
        var = None
    if unit is None:
        unit = obj.unit
    kwargs = dict(filename=obj.filename, data=data, unit=unit, var=var, ext
        =(obj._data_ext, obj._var_ext, obj._dq_ext), copy=copy, data_header
        =obj.data_header.copy(), primary_header=obj.primary_header.copy())
    if cls._has_wcs:
        kwargs['wcs'] = obj.wcs
    if cls._has_wave:
        kwargs['wave'] = obj.wave
    return cls(**kwargs)


def get_shared_secret(priv, pub):
    """ Derive the share secret between ``priv`` and ``pub``

        :param `Base58` priv: Private Key
        :param `Base58` pub: Public Key
        :return: Shared secret
        :rtype: hex

        The shared secret is generated such that::

            Pub(Alice) * Priv(Bob) = Pub(Bob) * Priv(Alice)

    """
    pub_point = pub.point()
    priv_point = int(repr(priv), 16)
    res = pub_point * priv_point
    res_hex = '%032x' % res.x()
    res_hex = '0' * (64 - len(res_hex)) + res_hex
    return res_hex


def shape_cut():
    """Cut hair to conform to the set shape object

    """
    pass


def get_oozie_required_xml_configs(hadoop_conf_dir):
    """Following configs differ from default configs in oozie-default.xml."""
    return {'oozie.service.ActionService.executor.ext.classes':
        'org.apache.oozie.action.email.EmailActionExecutor,org.apache.oozie.action.hadoop.HiveActionExecutor,org.apache.oozie.action.hadoop.ShellActionExecutor,org.apache.oozie.action.hadoop.SqoopActionExecutor,org.apache.oozie.action.hadoop.DistcpActionExecutor'
        , 'oozie.service.SchemaService.wf.ext.schemas':
        'shell-action-0.1.xsd,shell-action-0.2.xsd,shell-action-0.3.xsd,email-action-0.1.xsd,hive-action-0.2.xsd,hive-action-0.3.xsd,hive-action-0.4.xsd,hive-action-0.5.xsd,sqoop-action-0.2.xsd,sqoop-action-0.3.xsd,sqoop-action-0.4.xsd,ssh-action-0.1.xsd,ssh-action-0.2.xsd,distcp-action-0.1.xsd,distcp-action-0.2.xsd,oozie-sla-0.1.xsd,oozie-sla-0.2.xsd'
        , 'oozie.service.JPAService.create.db.schema': 'false',
        'oozie.service.HadoopAccessorService.hadoop.configurations': '*=%s' %
        hadoop_conf_dir}


@classmethod
def checksum(cls, config_file_path):
    """Computes a checksum over content of given file."""
    try:
        with open(config_file_path) as config_file:
            content = config_file.read()
        return cls._checksum(content)
    except FileNotFoundError:
        return None


@classmethod
def parse_dn(cls, dn):
    """
        Parses the pod, node, module, port from a distinguished name
        of the interface.

        :param dn: String containing the interface distinguished name
        :returns: interface_type, pod, node, module, port
        """
    if 'sys' in dn.split('/'):
        return cls._parse_physical_dn(dn)
    elif '/extpaths-' in dn:
        return cls._parse_extpath_dn(dn)
    else:
        return cls._parse_path_dn(dn)


def order_events(events, d=False):
    """
    Group events that occur on the same day, then sort them alphabetically
    by title, then sort by day. Returns a list of tuples that looks like
    [(day: [events])], where day is the day of the event(s), and [events]
    is an alphabetically sorted list of the events for the day.
    """
    ordered_events = {}
    for event in events:
        try:
            for occ in event.occurrence:
                try:
                    ordered_events[occ].append(event)
                except Exception:
                    ordered_events[occ] = [event]
        except AttributeError:
            pass
    if d:
        return ordered_events
    else:
        return sorted(ordered_events.items())


@staticmethod
def vint(buf, pos):
    """Load variable-size int."""
    limit = min(pos + 11, len(buf))
    res = ofs = 0
    while pos < limit:
        b = int(buf[pos])
        res += (b & 127) << ofs
        pos += 1
        ofs += 7
        if b < 128:
            return res, pos
    return 0, -1


def find_nearest_range(origin, distance=1.84467e+19):
    """Find the nearest elements to a point in the distance range.

		Parameters:
		origin (Vector) – Find nearest elements to this point.
		distance (float) – Maximum distance threshold.

		Returns: Returns a list of tuples (Vector location, Vector normal, int index, float distance),

		Return type: list"""
    from . import Vector
    return [(Vector, Vector, 1, 1.0), (Vector, Vector, 1, 1.0), (Vector,
        Vector, 1, 1.0), (Vector, Vector, 1, 1.0)]


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


@classmethod
def generate_single_int_sql_lt(cls, column_name, filter_value):
    """Return tuple with sql and replacement.

        This function builds the sql partial and replacement dict for
        a less-than match for a single integer against a single
        column in the database.

        Args:
            column_name (str): Name of column in DB.
            filter_value (str or int): This is what we look for in the column.
                Coerced to integer.

        Returns:
            tuple: Item 0 contains the SQL partial string. Item 1 contains
                the replacement dictionary.
        """
    column_name_corrected = column_name.replace('_lt', '')
    sql = '{} < :{}'.format(column_name_corrected, column_name_corrected)
    replacement = {column_name_corrected: int(filter_value)}
    return sql, replacement


def shell_type_to_str(shell_type):
    """Convert a shell type into a character"""
    return {(0): 's', (1): 'p', (2): 'd', (3): 'f', (4): 'g', (5): 'h', (6):
        'i'}[abs(shell_type)]


def _get_package_version(package):
    """Get the version for a package (if any)."""
    return package['version'] or 'None'


@staticmethod
def qr(a):
    """Compute the qr factorization of a matrix.

        Factor the matrix `a` as *qr*, where `q` is orthonormal and `r` is
        upper-triangular.

        Parameters
        ----------
        a : tensor, shape (M, N)
            Matrix to be factored.

        Returns
        -------
        Q, R : tensor
        """
    raise NotImplementedError