@classmethod
def make_request(cls, moop, path, dfi=None, filesize=None):
    """
        Generates a request for RequestFileTransfer

        :param moop: Mode of operation. Can be AddFile(1), DeleteFile(2), ReplaceFile(3), ReadFile(4), ReadDir(5). See :class:`RequestFileTransfer.ModeOfOperation<udsoncan.services.RequestFileTransfer.ModeOfOperation>`
        :type moop: int

        :param path: String representing the path to the target file or directory.
        :type path: string

        :param dfi: DataFormatIdentifier defining the compression and encryption scheme of the data.
                If not specified, the default value of 00 will be used, specifying no encryption and no compression.
                This value is only used when ModeOfOperation is : ``AddFile``, ``ReplaceFile``, ``ReadFile``
        :type dfi: :ref:`DataFormatIdentifier<DataFormatIdentifier>`

        :param filesize: The filesize of the file to write when ModeOfOperation is ``AddFile`` or ``ReplaceFile``.
            If filesize is an object of type :ref:`Filesize<Filesize>`, the uncompressed size and compressed size will be encoded on
            the minimum amount of bytes necessary, unless a ``width`` is explicitly defined. If no compressed size is given or filesize is an ``int``,
            then the compressed size will be set equal to the uncompressed size or the integer value given as specified by ISO-14229
        :type filesize: :ref:`Filesize<Filesize>` or int

        :raises ValueError: If parameters are out of range, missing or wrong type
        """
    from udsoncan import Request, Filesize
    if not isinstance(moop, int):
        raise ValueError('Mode of operation must be an integer')
    if moop not in [cls.ModeOfOperation.AddFile, cls.ModeOfOperation.
        DeleteFile, cls.ModeOfOperation.ReplaceFile, cls.ModeOfOperation.
        ReadFile, cls.ModeOfOperation.ReadDir]:
        raise ValueError('Mode of operation of %d is not a known mode' % moop)
    if not isinstance(path, str):
        raise ValueError('Given path must be a valid string')
    if len(path) <= 0:
        raise ValueError('Path must be a string longer than 0 character')
    path_ascii = path.encode('ascii')
    if len(path_ascii) > 65535:
        raise ValueError(
            'Path length must be smaller or equal than 65535  bytes (16 bits) when encoded in ASCII'
            )
    use_dfi = moop in [cls.ModeOfOperation.AddFile, cls.ModeOfOperation.
        ReplaceFile, cls.ModeOfOperation.ReadFile]
    use_filesize = moop in [cls.ModeOfOperation.AddFile, cls.
        ModeOfOperation.ReplaceFile]
    if use_dfi:
        dfi = cls.normalize_data_format_identifier(dfi)
    elif dfi is not None:
        raise ValueError(
            'DataFormatIdentifier is not needed with ModeOfOperation=%d' % moop
            )
    if use_filesize:
        if filesize is None:
            raise ValueError(
                'A filesize must be given for this mode of operation')
        if isinstance(filesize, int):
            filesize = Filesize(filesize)
        if not isinstance(filesize, Filesize):
            raise ValueError(
                'Given filesize must be a valid Filesize object or an integer')
        if filesize.uncompressed is None:
            raise ValueError(
                'Filesize needs at least an Uncompressed file size')
        if filesize.compressed is None:
            filesize = Filesize(uncompressed=filesize.uncompressed,
                compressed=filesize.uncompressed, width=filesize.get_width())
    elif filesize is not None:
        raise ValueError('Filesize is not needed with ModeOfOperation=%d' %
            moop)
    data = moop.to_bytes(1, 'big')
    data += len(path_ascii).to_bytes(2, 'big')
    data += path_ascii
    if use_dfi:
        data += dfi.get_byte()
    if use_filesize:
        data += filesize.get_width().to_bytes(1, 'big')
        data += filesize.get_uncompressed_bytes()
        data += filesize.get_compressed_bytes()
    request = Request(cls, data=data)
    return request


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


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

        :param TeamProfileChangeLogoDetails val:
        :rtype: EventDetails
        """
    return cls('team_profile_change_logo_details', val)


def setComponentName(component_name):
    """ Change the component name """


def p_eq_expr_2(p):
    """EqualityExpr : EqualityExpr EQ RelationalExpr"""
    p[0] = 'comp', '=', p[1], p[3]


def static_full_sizes(width, height, tilesize):
    """Generator for scaled-down full image sizes.

    Positional arguments:
    width -- width of full size image
    height -- height of full size image
    tilesize -- width and height of tiles

    Yields [sw,sh], the size for each full-region tile that is less than
    the tilesize. This includes tiles up to the full image size if that
    is smaller than the tilesize.
    """
    for level in range(0, 20):
        factor = 2.0 ** level
        sw = int(width / factor + 0.5)
        sh = int(height / factor + 0.5)
        if sw < tilesize and sh < tilesize:
            if sw < 1 or sh < 1:
                break
            yield [sw, sh]


def _empty_fp_events():
    """Container for forceplate events"""
    return dict(R_strikes=[], R_toeoffs=[], L_strikes=[], L_toeoffs=[],
        valid=set(), R_strikes_plate=[], L_strikes_plate=[], our_fp_info={},
        coded='')


@staticmethod
def PDMA_LU(l2, l1, d, u1, u2):
    """LU decomposition of PDM (for testing only)"""
    n = d.shape[0]
    m = u1.shape[0]
    k = n - m
    for i in range(n - 2 * k):
        lam = l1[i] / d[i]
        d[i + k] -= lam * u1[i]
        u1[i + k] -= lam * u2[i]
        l1[i] = lam
        lam = l2[i] / d[i]
        l1[i + k] -= lam * u1[i]
        d[i + 2 * k] -= lam * u2[i]
        l2[i] = lam
    i = n - 4
    lam = l1[i] / d[i]
    d[i + k] -= lam * u1[i]
    l1[i] = lam
    i = n - 3
    lam = l1[i] / d[i]
    d[i + k] -= lam * u1[i]
    l1[i] = lam


def split_param(string, sep=':'):
    """Split a key/value parameter by separator."""
    split = string.split(sep)
    for idx, item in enumerate(split):
        split[idx] = item.strip()
    return split


def vrh(kclay, kqtz, vclay):
    """
    Voigt-Reuss-Hill average to find Kmatrix from clay and qtz components.

    From Smith et al, Geophysics 68(2), 2003.

    Works for any two components.

    Args:
        kclay (float): K_clay.
        kqtz (float): K_quartz.
        vclay (float): V_clay.

    Returns:
        Kvrh, also known as Kmatrix.
    """
    vqtz = 1 - vclay
    kreuss = 1.0 / (vclay / kclay + vqtz / kqtz)
    kvoigt = vclay * kclay + vqtz * kqtz
    kvrh = 0.5 * (kreuss + kvoigt)
    return kvrh


@classmethod
def add_medium(cls, options=None):
    """Adds a medium to an org"""
    cls.command_sub = 'add-medium'
    return cls.execute(cls._construct_command(options))


@staticmethod
def _parse_unit(raw_unit):
    """ Parse needed fields from raw json and return dict representing
            a unit
        """
    unit = {}
    fields = {'serial', 'label', 'address', 'password', 'cryptoSerial'}
    try:
        for field in fields:
            unit[field] = raw_unit[field]
    except KeyError:
        pass
    return unit


@classmethod
def validate(cls, value):
    """
        Validate the value and convert it to its native type, if
        necessary.
        """
    return value


def __len__():
    """Get the number of fields
        """


def discobrackets_end(stream, **params):
    """Discobrackets output suffix is empty.
    """
    pass


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

        :param MemberAccessLevelResult val:
        :rtype: RemoveMemberJobStatus
        """
    return cls('complete', val)


def units_length_preset_add(name: str='', remove_active: bool=False):
    """Add or remove length units preset

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


def can_access_board_backend(request):
    """Whether the given user can access the given board backend."""
    user, board = request.user, request.board
    if board.owner == user:
        return True
    elif user.is_superuser:
        return True
    return False


@staticmethod
def icod(nucl_seq, pos):
    """Retrieves the codon at the specified codon position from a nucleotide sequence

        Parameters
        ----------
        nucl_seq : str
        pos : int

        Returns
        -------
        str
            codon string of length 3

        """
    assert len(nucl_seq) % 3 == 0, ValueError(
        'Sequence is not in multiples of three.')
    assert isinstance(pos, int), ValueError(
        'Position should be an integer value.')
    return nucl_seq[3 * pos:3 * pos + 3]


def calculate_real_size(value):
    """
    check if we have a 32 bit counter overrun
    calculate the real_size; we check if the integer is positive
    """
    if value > 0:
        return value
    real_size = -value + 2147483647
    return real_size


def create_shards(input_list, size):
    """
    Creates shards of size n from the input list.
    @author: Miguel Branco in DQ2 Site Services code
    """
    shard, i = [], 0
    for element in input_list:
        shard.append(element)
        i += 1
        if i == size:
            yield shard
            shard, i = [], 0
    if i > 0:
        yield shard


def figsize(sizex, sizey):
    """Set the default figure size to be [sizex, sizey].

    This is just an easy to remember, convenience wrapper that sets::

      matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
    """
    import matplotlib
    matplotlib.rcParams['figure.figsize'] = [sizex, sizey]


@staticmethod
def _handle_response(res):
    """
        Check for errors and return json response in case of 200 HTTP response code
        :param res: response from a HTTP request
        :return: dict of json response
        """
    if res.status_code != 200:
        if res.status_code == 404:
            raise ValueError('HTTP Response 404 (Not Found): ' + res.text)
        if res.status_code == 403:
            raise ValueError('HTTP Response 403 (Forbidden): ' + res.text)
        if res.status_code == 401:
            raise PermissionError('HTTP Response 401 (Not Authorized): ' +
                res.text)
        if res.status_code == 400:
            raise ValueError('HTTP Response 400 (Validation Error): ' + res
                .text)
        else:
            raise RuntimeError('Unkown HTTP Error: ' + res.text)
    return res.json()


def abs_diff(a_vec, b_vec):
    """ calculate the absolute difference between two vectors """
    if max(a_vec) > max(b_vec):
        return min(a_vec) - max(b_vec)
    return min(b_vec) - max(a_vec)


def get_preferred_uniprot_id_from_mapping(data):
    """
    Takes a list of Ensembl xrefs/ids mapped from a UniProt ID
    and gets the preferred entry (Many-to-one), based on seq
    identity and coverage.

    :param data: list of dictionaries
    :return: (str) preferred UniProt ID
    """
    best_match = None
    curr_ix = -1
    prev_identity = 0
    prev_coverage = 0
    prev_id = '-' * 100
    for ix, entry in enumerate(data):
        if ('ensembl_identity' in entry and 'xref_identity' in entry and
            'xref_start' in entry and 'xref_end' in entry):
            identity = entry['ensembl_identity'] + entry['xref_identity']
            coverage = entry['xref_end'] - entry['xref_start']
            if identity + coverage >= prev_identity + prev_coverage:
                prev_identity = identity
                prev_coverage = coverage
                if len(entry['primary_id']) < len(prev_id):
                    prev_id = entry['primary_id']
                    curr_ix = ix
    if curr_ix != -1 and 'primary_id' in data[curr_ix]:
        best_match = data[curr_ix]['primary_id']
    return best_match


def getGlossaryTermItems():
    """Returns glossary terms in a specific structure

        Item:
        - path -> term path
        - id -> term id
        - title -> term title
        - description -> term description
        - url -> term url

         check security."""


def p_simple_stmt_3(p):
    """simple_stmt : small_stmt simple_stmt_star NEWLINE"""
    p[0] = [p[1]] + p[2]


def _relative_shoots_select(ensembles, rgen, relative):
    """Randomly select the ensemble for 'relative' shooting moves.

    Here we select the ensemble to do the shooting in based on relative
    probabilities. We draw a random number in [0, 1] which is used to
    select the ensemble.

    Parameters
    ----------
    ensembles : list of objects like `PathEnsemble` from `.path`.
        This is a list of the ensembles we are using in the RETIS method
    rgen : object like `RandomGenerator` from `.random_gen`.
        This is a random generator. Here we assume that we can call
        `rgen.rand()` to draw random uniform numbers.
    relative : list of floats
        These are the relative probabilities for the ensembles. We
        assume here that these numbers are normalized.

    Returns
    -------
    out[0] : integer
        The index of the path ensemble to shoot in.
    out[1] : object like `PathEnsemble` from `.path`.
        The selected path ensemble for shooting.
    """
    freq = rgen.rand()
    cumulative = 0.0
    idx = None
    for idx, path_freq in enumerate(relative):
        cumulative += path_freq
        if freq < cumulative:
            break
    try:
        path_ensemble = ensembles[idx]
    except TypeError:
        msg = 'Error in relative shoot frequencies! Aborting!'
        raise ValueError(msg)
    return idx, path_ensemble


def shorten_string(data: str, max_len=15, short_tag: str=None):
    """Shortens thisverylongstring to thi..

    Args:
        data (str): input string
        max_len (int): max len of output
        short_tag (str): string to be used instead of shortened text

    Returns:
        str: the shortened text

    """
    short_tag = short_tag or '...'
    if not data or not isinstance(data, str) or len(data) <= max_len:
        return data
    return data[max_len - len(short_tag)] + short_tag


def replace(name, newobject):
    """ Replace an existing object named ``name`` in this folder with a
        new object ``newobject``.  If there isn't an object named ``name`` in
        this folder, an exception will *not* be raised; instead, the new
        object will just be added.

        This operation is done in terms of a remove and an add.  The Removed
        and WillBeRemoved events will be sent for the old object, and the
        WillBeAdded and Add events will be sent for the new object.
        """


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


def methods_of(obj):
    """Get all callable methods of an object that don't start with underscore

    returns a list of tuples of the form (method_name, method)
    """
    result = []
    for fn in dir(obj):
        if callable(getattr(obj, fn)) and not fn.startswith('_'):
            result.append((fn, getattr(obj, fn), getattr(obj, fn +
                '_description', None)))
    return result


def __get_extension(fname):
    """
    Get file extension.
    """
    return '.' + fname.split('.')[-1]


def delete(key, time=0):
    """ Remove the value stored in the cache under 'key'.

        o 'time', if passed an integer time value (in seconds) during
          which the memcached server will block new writes to this key
          (perhaps useful in preventing race conditions).

        o Return a boolean to indicate success.
        """


@classmethod
def get_prefix(cls):
    """Tag 'key', saved in the database as <prefix>:<value>"""
    return '%s:' % cls.get_name().upper()


def set_blue(pix, b):
    """Set the blue value of Pixel pix to b."""
    pix.set_blue(b)


@classmethod
def get_population_lock_key(cls):
    """
        Return the lock key for this class or workers to use when managing the
        population.
        """
    return f'{cls.get_id_prefix()}_population_contol'


def rep_newlines_with_space(string: str) ->str:
    """Removes newlines and replaces them with spaces.

    Also reduces double spaces to single spaces.
    """
    return string.replace('\n', ' ').replace('  ', ' ')


def is_empty_arraylike(arraylike):
    """Check if arraylike is None or empty.

    Arg:
        arraylike: None or np.ndarray or list or tuple

    Return:
        a boolean indicates if this array is empty
    """
    import numpy as np
    if arraylike is None:
        return True
    if isinstance(arraylike, np.ndarray) and arraylike.size == 0:
        return True
    if isinstance(arraylike, list) and len(arraylike) == 0:
        return True
    if isinstance(arraylike, tuple) and len(arraylike) == 0:
        return True
    return False


@staticmethod
def _error_handler(exception):
    """Handle exceptions (prints the exception by default)."""
    raise exception


@staticmethod
def _split_string_by_space(string):
    """ Converts "H1 L1" to ["H1", "L1"] """
    return string.split(' ')


def extract_adf(archive, compression, cmd, verbosity, interactive, outdir):
    """Extract an ADF archive."""
    return [cmd, archive, '-d', outdir]


def manage_editUserPassword(dn, new_pw, REQUEST):
    """ Change a users password. The *dn* argument contains the full DN
        for the user record in question and new_pw contains the new password.

        Permission: *Manage users*
        """


def popget(d, key, default=None):
    """
    Get the key from the dict, returning the default if not found.

    Parameters
    ----------
    d : dict
        Dictionary to get key from.
    key : object
        Key to get from the dictionary.
    default : object
        Default to return if key is not found in dictionary.

    Returns
    -------
    object

    Examples
    ---------
    >>> d = {'ABC': 5.0}
    >>> popget(d, 'ZPF', 1.0) == 1.0
    True
    >>> popget(d, 'ABC', 1.0) == 5.0
    True

    """
    try:
        return d.pop(key)
    except KeyError:
        return default


def normalise(asset_code):
    """
    Normalise the given asset code to one that is compatible with the inventory
    API. Generally this just involves removing the 'sr' from the front and
    making the result all in uppercase.

    :param str asset_code: The asset code to normalise.
    :returns: A normalised part code.
    :rtype: str
    """
    asset_code = asset_code.strip().upper()
    if asset_code.startswith('SR'):
        return asset_code[2:]
    else:
        return asset_code


@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 is_dictionary_subclass(obj):
    """Returns True if *obj* is a subclass of the dict type. *obj* must be
    a subclass and not the actual builtin dict.

    >>> class Temp(dict): pass
    >>> is_dictionary_subclass(Temp())
    True
    """
    return hasattr(obj, '__class__') and issubclass(obj.__class__, dict
        ) and type(obj) is not dict


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

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

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


def quote_mos(s: str, max_length: int=5000) ->str:
    """ Get Mosel source code representation of a string, without type or bound checking. """
    if '\x00' in s:
        raise ValueError("Null character '\\0' not allowed in string: {}".
            format(repr(s)))
    if len(s) > max_length:
        raise ValueError(
            'The string must not be longer than {} characters: {}.'.format(
            max_length, repr(s)))
    result = s.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n'
        ).replace('\r', '\\r').replace('\t', '\\t')
    return '"' + result + '"'


def clear_bit(index: int, value: int=0) ->int:
    """
    Set the bit on index to zero.

    Parameters
    ----------
    index : int
        The index of the bit to set to zero
    value : int
        The initial byte (as int) in which to set the index bit to zero (default=0x00)

    Returns
    -------
    int
        The changed value (with the cleared bit)
    """
    return value & ~(1 << index)


def quickExample(testfolder=None):
    """
    Example of how to run a Radiance routine for a simple rooftop bifacial system

    """
    import bifacial_radiance
    if testfolder == None:
        testfolder = bifacial_radiance.main._interactive_directory(title=
            'Select or create an empty directory for the Radiance tree')
    demo = bifacial_radiance.RadianceObj('simple_panel', path=testfolder)
    demo.setGround(0.62)
    try:
        epwfile = demo.getEPW(37.5, -77.6)
    except ConnectionError:
        pass
    metdata = demo.readEPW(epwfile)
    cumulativeSky = True
    if cumulativeSky:
        demo.genCumSky(demo.epwfile)
    else:
        demo.gendaylit(metdata, 4020)
    moduletype = 'test'
    moduleDict = demo.makeModule(name=moduletype, x=1.59, y=0.95)
    sceneDict = {'tilt': 10, 'pitch': 1.5, 'clearance_height': 0.2,
        'azimuth': 180, 'nMods': 20, 'nRows': 7}
    scene = demo.makeScene(moduletype=moduletype, sceneDict=sceneDict)
    octfile = demo.makeOct(demo.getfilelist())
    analysis = bifacial_radiance.AnalysisObj(octfile, demo.name)
    frontscan, backscan = analysis.moduleAnalysis(scene, sensorsy=9)
    analysis.analysis(octfile, demo.name, frontscan, backscan)
    print('Annual bifacial ratio average:  %0.3f' % (sum(analysis.Wm2Back) /
        sum(analysis.Wm2Front)))
    return analysis


def update_package_data(distribution):
    """update build_py options to get package_data changes"""
    build_py = distribution.get_command_obj('build_py')
    build_py.finalize_options()


def build_authenticate_header(realm=''):
    """Optional WWW-Authenticate header (401 error)"""
    return {'WWW-Authenticate': 'OAuth realm="%s"' % realm}


@staticmethod
def _sort_resources_per_hosting_device(resources):
    """This function will sort the resources on hosting device.

        The sorting on hosting device is done by looking up the
        `hosting_device` attribute of the resource, and its `id`.

        :param resources: a dict with key of resource name
        :return dict sorted on the hosting device of input resource. Format:
        hosting_devices = {
                            'hd_id1' : {'routers':[routers],
                                        'removed_routers':[routers], .... }
                            'hd_id2' : {'routers':[routers], .. }
                            .......
                            }
        """
    hosting_devices = {}
    for key in resources.keys():
        for r in (resources.get(key) or []):
            if r.get('hosting_device') is None:
                continue
            hd_id = r['hosting_device']['id']
            hosting_devices.setdefault(hd_id, {})
            hosting_devices[hd_id].setdefault(key, []).append(r)
    return hosting_devices


def zero_to_one(table, option):
    """
    normalize from zero to one for row or table
    """
    if option == 'table':
        m = min(min(table))
        ma = max(max(table))
    t = []
    for row in table:
        t_row = []
        if option != 'table':
            m, ma = min(row), max(row)
        for i in row:
            if ma == m:
                t_row.append(0)
            else:
                t_row.append((i - m) / (ma - m))
        t.append(t_row)
    return t


def define(hub):
    """
    Return the definition used by the runtime to insert the conditions of the
    given requisite
    """
    return {'result': True}


def find_matching_objects(model_list, matching_chars, matching_prop='legacy_id'
    ):
    """ find first matching object from a list of classes
        :param model_list: A list of models to search through
        :param matching_chars: A sequence of chars which needs        to be identical to the value of the field
        :param matching_prop: The object's properties which value should match
        return: the matching object or None
    """
    leg_id = matching_chars
    all_qs = []
    for x in model_list:
        kwargs = {matching_prop: matching_chars}
        qs = x.objects.filter(**kwargs)
        if not qs:
            continue
        else:
            all_qs.append(*qs)
        if len(all_qs) == 1:
            return all_qs[0]
        else:
            return None


def get_current(isamAppliance):
    """
    Get permitted features for current user
    """
    return isamAppliance.invoke_get('Get permitted features for current user',
        '/permissions/v1')


@staticmethod
def get_model_select_metric(metrics):
    """
        Return a single numeric metric value that is used for model selection, returns
        the metric itself by default, but usually metrics will be more complicated
        data structures
        """
    return metrics


def lines_equal(a, b):
    """Compare two matplotlib line segments by line style, marker, colour and label.
    Used to match legend items to data items on the axes."""
    return a.get_label() == b.get_label() and a.get_linestyle(
        ) == b.get_linestyle() and a.get_color() == b.get_color()


@classmethod
def search_bm25(cls, term, weights=None, with_score=False, score_alias=
    'score', explicit_ordering=False):
    """Full-text search for selected `term` using BM25 algorithm."""
    return cls._search(term, weights, with_score, score_alias, cls.bm25,
        explicit_ordering)


@classmethod
def parse(cls, event):
    """Given an event dictionary, gets the event schema and applies it to the dictionary, otherwise
        just returns the :class:`BaseEvent` from the dict.

        :param event: A :class:`dict` representing an event
        :return: A :class:`BaseEvent` subclass
        """
    if hasattr(cls, 'schema'):
        new_klass = cls()
        schema = new_klass.schema
        for key in event:
            new_klass[key] = schema.get(key, lambda x: x)(event[key])
    else:
        new_klass = cls(event)
    return new_klass


def to_string(o):
    """Convert a value into a Python string

    Args:
        o: Data to convert

    Returns:
        int: Python string of data
    """
    return str(o)


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


def __len__():
    """ -> the number of OIDs in the cache.
        """


def reduce_coordinate(x):
    """Coordinates sometimes seem to have 80 added to them"""
    if x >= 128:
        return x - 128
    else:
        return x


def remove_outputs(nb):
    """remove the outputs from a notebook"""
    for ws in nb.worksheets:
        for cell in ws.cells:
            if cell.cell_type == 'code':
                cell.outputs = []


def broadcast_dim_conv2d(x):
    """
    Auto broadcast input so that it can fits into a Conv2d
    """
    if x.dim() == 3:
        x = x[:, (None), :, :]
    else:
        raise ValueError(
            'Only support input with shape = (batch, len) or shape = (len)')
    return x


def fixture_api(func):
    """ Decorator to mark the method as a fixture-only object.
    """
    """ Decorator to automatically mark the object as the normal thing.
    """
    func.__is_fixture__ = True
    return func


def dintersect(d1, d2):
    """Signed distance to set intersection of two regions described by signed
    distance functions d1 and d2.

    Not exact the true signed distance function for the difference,
    for example around corners.
    """
    return max(d1, d2)


def get_cluster_key(obj):
    """Get the cluster key for a given k8s object"""
    try:
        namespace = obj['metadata']['namespace']
        name = obj['metadata']['labels']['gateway.dask.org/cluster']
        return f'{namespace}.{name}'
    except KeyError:
        return None


def raise_standardErrorMessage(client=None, REQUEST={}, error_type=None,
    error_value=None, tb=None, error_tb=None, error_message='', tagSearch=
    None, error_log_url=''):
    """Raise standard error message.
        """


@classmethod
def forcing_at_node(cls, m, node, tree, bau, subinterval_len):
    """Calculates the forcing based mitigation leading up to the
        damage calculation in `node`.

        Parameters
        ----------
        m : ndarray
            array of mitigations in each node.
        node : int
            the node for which the forcing is being calculated.

        Returns
        -------
        float
            forcing

        """
    return cls.forcing_and_ghg_at_node(m, node, tree, bau, subinterval_len,
        returning='forcing')


def connectTCP(host, port, factory, timeout=30, bindAddress=None):
    """
        Connect a TCP client.

        @param host: a host name

        @param port: a port number

        @param factory: a L{twisted.internet.protocol.ClientFactory} instance

        @param timeout: number of seconds to wait before assuming the
                        connection has failed.

        @param bindAddress: a (host, port) tuple of local address to bind
                            to, or None.

        @return: An object which provides L{IConnector}. This connector will
                 call various callbacks on the factory when a connection is
                 made, failed, or lost - see
                 L{ClientFactory<twisted.internet.protocol.ClientFactory>}
                 docs for details.
        """


def is_signed(*p):
    """ Returns false unless all types in p are signed
    """
    from symbols.type_ import Type
    try:
        for i in p:
            if not i.type_.is_basic or not Type.is_signed(i.type_):
                return False
        return True
    except:
        pass
    return False


def is_numeric(lit):
    """
    value of numeric: literal, string, int, float, hex, binary
    From http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Python
    """
    if len(lit) <= 0:
        return lit
    if lit == '0':
        return 0
    if len(lit) > 1:
        litneg = lit[1:] if lit[0] == '-' else lit
        if litneg[0] == '0':
            if litneg[1] in 'xX':
                return int(lit, 16)
            elif litneg[1] in 'bB':
                return int(lit, 2)
            else:
                try:
                    return int(lit, 8)
                except ValueError:
                    pass
    try:
        return int(lit)
    except ValueError:
        pass
    try:
        return float(lit)
    except ValueError:
        pass
    try:
        return complex(lit)
    except ValueError:
        pass
    return lit


def search(source, value):
    """Search for value in source database

    Args:
        source (str): database to look into
        value (str): value to look for

    Returns:
        list: occurances found
    """
    return ['found it']


def _mp_initializer(endmembers, im2classify):
    """Declare global variables needed for image classifiers.

    :param endmembers:
    :param im2classify:
    """
    global global_shared_endmembers, global_shared_im2classify
    global_shared_endmembers = endmembers
    global_shared_im2classify = im2classify


def delete_stack(heat_cli, stack):
    """
    Deletes the Heat stack
    :param heat_cli: the OpenStack heat client object
    :param stack: the OpenStack Heat stack object
    """
    heat_cli.stacks.delete(stack.id)


def check_bounds(value):
    """Check if the given value is out-of-bounds (0 to 4095)

    :args: the value to be checked
    :returns: the checked value
    """
    try:
        if int(value) > 4095:
            return 4095
        elif int(value) < 0:
            return 0
        else:
            return value
    except ValueError:
        print('Value was not a number, returned default value of 0')
        return 0


def read_file(file_name, mode='r'):
    """ read file content
    """
    try:
        with open(file_name, mode) as f:
            content = f.read()
    except Exception:
        return None
    return content


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 DebugOn(moduleList):
    """Turn on debugging for selected modules
    MODULE-LIST = list(string)

    Returns: void
    Throws:  nothing

    Turns on debugging flag for selected modules
    """
    global _modulesToDebug, _debugAllModules
    _debugAllModules = 0
    _modulesToDebug = moduleList


def remove_leading(needle, haystack):
    """Remove leading needle string (if exists).

    >>> remove_leading('Test', 'TestThisAndThat')
    'ThisAndThat'
    >>> remove_leading('Test', 'ArbitraryName')
    'ArbitraryName'
    """
    if haystack[:len(needle)] == needle:
        return haystack[len(needle):]
    return haystack


@classmethod
def from_dict(cls, d):
    """
        Restores an object state from a dictionary, used in de-JSONification.

        :param d: the object dictionary
        :type d: dict
        :return: the object
        :rtype: object
        """
    raise Exception('Not implemented!')


def identity(value):
    """Identity function."""
    return value


@staticmethod
def get_class_data_type_key(class_name: str) ->str:
    """Builds class key string based on capital letters of class name i.e. for 'FooBarClass' would return 'fbc'.
        """
    data_type_key = ''
    for letter in class_name:
        if 'A' <= letter <= 'Z':
            data_type_key += letter
    return data_type_key.lower()


def species_list(what_list):
    """
    provide default lists of elements to plot.

    what_list : string
        String name of species lists provided.

        If what_list is "CNONe", then C, N, O and some other light
        elements.

        If what_list is "s-process", then s-process indicators.

    """
    if what_list is 'CNONe':
        list_to_print = ['H-1', 'He-4', 'C-12', 'N-14', 'O-16', 'Ne-20']
    elif what_list is 'sprocess':
        list_to_print = ['Fe-56', 'Ge-70', 'Zn-70', 'Se-76', 'Kr-80',
            'Kr-82', 'Kr-86', 'Sr-88', 'Ba-138', 'Pb-208']
    elif what_list is 'burn_stages':
        list_to_print = ['H-1', 'He-4', 'C-12', 'O-16', 'Ne-20', 'Si-28']
    elif what_list is 'list_marco_1':
        list_to_print = ['C-12', 'O-16', 'Ne-20', 'Ne-22', 'Na-23', 'Fe-54',
            'Fe-56', 'Zn-70', 'Ge-70', 'Se-76', 'Kr-80', 'Kr-82', 'Sr-88',
            'Y-89', 'Zr-96', 'Te-124', 'Xe-130', 'Xe-134', 'Ba-138']
    return list_to_print


def start_replication_task(ReplicationTaskArn=None,
    StartReplicationTaskType=None, CdcStartTime=None):
    """
    Starts the replication task.
    For more information about AWS DMS tasks, see the AWS DMS user guide at Working with Migration Tasks
    See also: AWS API Documentation

    :example: response = client.start_replication_task(
        ReplicationTaskArn='string',
        StartReplicationTaskType='start-replication'|'resume-processing'|'reload-target',
        CdcStartTime=datetime(2015, 1, 1)
    )

    :type ReplicationTaskArn: string
    :param ReplicationTaskArn: [REQUIRED]
            The Amazon Resource Number (ARN) of the replication task to be started.

    :type StartReplicationTaskType: string
    :param StartReplicationTaskType: [REQUIRED]
            The type of replication task.

    :type CdcStartTime: datetime
    :param CdcStartTime: The start time for the Change Data Capture (CDC) operation.

    :rtype: dict
    :return: {
        'ReplicationTask': {
            'ReplicationTaskIdentifier': 'string',
            'SourceEndpointArn': 'string',
            'TargetEndpointArn': 'string',
            'ReplicationInstanceArn': 'string',
            'MigrationType': 'full-load'|'cdc'|'full-load-and-cdc',
            'TableMappings': 'string',
            'ReplicationTaskSettings': 'string',
            'Status': 'string',
            'LastFailureMessage': 'string',
            'StopReason': 'string',
            'ReplicationTaskCreationDate': datetime(2015, 1, 1),
            'ReplicationTaskStartDate': datetime(2015, 1, 1),
            'ReplicationTaskArn': 'string',
            'ReplicationTaskStats': {
                'FullLoadProgressPercent': 123,
                'ElapsedTimeMillis': 123,
                'TablesLoaded': 123,
                'TablesLoading': 123,
                'TablesQueued': 123,
                'TablesErrored': 123
            }
        }
    }

    :returns:
    Must contain from 1 to 255 alphanumeric characters or hyphens.
    First character must be a letter.
    Cannot end with a hyphen or contain two consecutive hyphens.

    """
    pass


def train(req, author, content, ip, spam=True):
    """Train the filter by reporting a false negative or positive.

        The spam keyword argument is `True` if the content should be considered
        spam (a false negative), and `False` if the content was legitimate (a
        false positive).

        returns
           0 (no training),
           1 (training ok),
          -1 (training error) or
          -2 (missing preconditions, e.g. keys)
        """


def with_iter(context_manager):
    """Wrap an iterable in a ``with`` statement, so it closes once exhausted.

    For example, this will close the file when the iterator is exhausted::

        upper_lines = (line.upper() for line in with_iter(open('foo')))

    Any context manager which returns an iterable is a candidate for
    ``with_iter``.

    """
    with context_manager as iterable:
        for item in iterable:
            yield item


def create_typed_link_facet(SchemaArn=None, Facet=None):
    """
    Creates a  TypedLinkFacet . For more information, see Typed link .
    See also: AWS API Documentation

    :example: response = client.create_typed_link_facet(
        SchemaArn='string',
        Facet={
            'Name': 'string',
            'Attributes': [
                {
                    'Name': 'string',
                    'Type': 'STRING'|'BINARY'|'BOOLEAN'|'NUMBER'|'DATETIME',
                    'DefaultValue': {
                        'StringValue': 'string',
                        'BinaryValue': b'bytes',
                        'BooleanValue': True|False,
                        'NumberValue': 'string',
                        'DatetimeValue': datetime(2015, 1, 1)
                    },
                    'IsImmutable': True|False,
                    'Rules': {
                        'string': {
                            'Type': 'BINARY_LENGTH'|'NUMBER_COMPARISON'|'STRING_FROM_SET'|'STRING_LENGTH',
                            'Parameters': {
                                'string': 'string'
                            }
                        }
                    },
                    'RequiredBehavior': 'REQUIRED_ALWAYS'|'NOT_REQUIRED'
                },
            ],
            'IdentityAttributeOrder': [
                'string',
            ]
        }
    )

    :type SchemaArn: string
    :param SchemaArn: [REQUIRED]
            The Amazon Resource Name (ARN) that is associated with the schema. For more information, see arns .

    :type Facet: dict
    :param Facet: [REQUIRED]
            Facet structure that is associated with the typed link facet.
            Name (string) -- [REQUIRED]The unique name of the typed link facet.
            Attributes (list) -- [REQUIRED]An ordered set of attributes that are associate with the typed link. You can use typed link attributes when you need to represent the relationship between two objects or allow for quick filtering of incoming or outgoing typed links.
            (dict) --A typed link attribute definition.
            Name (string) -- [REQUIRED]The unique name of the typed link attribute.
            Type (string) -- [REQUIRED]The type of the attribute.
            DefaultValue (dict) --The default value of the attribute (if configured).
            StringValue (string) --A string data value.
            BinaryValue (bytes) --A binary data value.
            BooleanValue (boolean) --A Boolean data value.
            NumberValue (string) --A number data value.
            DatetimeValue (datetime) --A date and time value.
            IsImmutable (boolean) --Whether the attribute is mutable or not.
            Rules (dict) --Validation rules that are attached to the attribute definition.
            (string) --
            (dict) --Contains an Amazon Resource Name (ARN) and parameters that are associated with the rule.
            Type (string) --The type of attribute validation rule.
            Parameters (dict) --The minimum and maximum parameters that are associated with the rule.
            (string) --
            (string) --

            RequiredBehavior (string) -- [REQUIRED]The required behavior of the TypedLinkAttributeDefinition .

            IdentityAttributeOrder (list) -- [REQUIRED]A range filter that you provide for multiple attributes. The ability to filter typed links considers the order that the attributes are defined on the typed link facet. When providing ranges to typed link selection, any inexact ranges must be specified at the end. Any attributes that do not have a range specified are presumed to match the entire range. Filters are interpreted in the order of the attributes on the typed link facet, not the order in which they are supplied to any API calls.
            (string) --

    :rtype: dict
    :return: {}

    :returns:
    (dict) --

    """
    pass


def crop(clip, x1=None, y1=None, x2=None, y2=None, width=None, height=None,
    x_center=None, y_center=None):
    """
    Returns a new clip in which just a rectangular subregion of the
    original clip is conserved. x1,y1 indicates the top left corner and
    x2,y2 is the lower right corner of the croped region.
    All coordinates are in pixels. Float numbers are accepted.

    To crop an arbitrary rectangle:

    >>> crop(clip, x1=50, y1=60, x2=460, y2=275)

    Only remove the part above y=30:

    >>> crop(clip, y1=30)

    Crop a rectangle that starts 10 pixels left and is 200px wide

    >>> crop(clip, x1=10, width=200)

    Crop a rectangle centered in x,y=(300,400), width=50, height=150 :

    >>> crop(clip,  x_center=300 , y_center=400,
                        width=50, height=150)

    Any combination of the above should work, like for this rectangle
    centered in x=300, with explicit y-boundaries:

    >>> crop(x_center=300, width=400, y1=100, y2=600)

    """
    if width and (x1 is not None or x2 is not None):
        if x1 is not None:
            x2 = x1 + width
        else:
            x1 = x2 - width
    if height and (y1 is not None or y2 is not None):
        if y1 is not None:
            y2 = y1 + height
        else:
            y1 = y2 - height
    if x_center:
        x1, x2 = x_center - width / 2, x_center + width / 2
    if y_center:
        y1, y2 = y_center - height / 2, y_center + height / 2
    if x1 is None:
        x1 = 0
    if y1 is None:
        y1 = 0
    if x2 is None:
        x2 = clip.size[0]
    if y2 is None:
        y2 = clip.size[1]
    return clip.fl_image(lambda pic: pic[int(y1):int(y2), int(x1):int(x2)],
        apply_to=['mask'])


@classmethod
def list_tags(cls, options=None):
    """Build foreman-maintain health list-tags"""
    cls.command_sub = 'list-tags'
    if options is None:
        options = {}
    result = cls._construct_command(options)
    return result


def saveInitialControlFile(jsonParams, originalControlFile):
    """
        Save the adaptive control file jsonParams in originalControlFile

        :param jsonParams: Input control file in JSON format
        :type jsonParams: str
        :param originalControlFile: Path where to save the control file
        :type originalControlFile: str
    """
    with open(originalControlFile, 'w') as f:
        with open(jsonParams, 'r') as fr:
            jsonFile = fr.read()
        f.write(jsonFile)


def fix_pyext(mod_path):
    """
    Fix a module filename path extension to always end with the
    modules source file (i.e. strip compiled/optimized .pyc, .pyo
    extension and replace it with .py).

    """
    if mod_path[-4:] in ['.pyo', 'pyc']:
        mod_path = mod_path[:-1]
    return mod_path


def get_public_key(private_key):
    """Get the public key from a cryptography private key object."""
    return private_key.public_key()


def read_key(pysam_alignment_record):
    """
    Given a `pysam.AlignedSegment` instance, return the attributes identifying
    the *read* it comes from (not the alignment). There may be more than one
    alignment for a read, e.g. chimeric and secondary alignments.
    """
    return (pysam_alignment_record.query_name, pysam_alignment_record.
        is_duplicate, pysam_alignment_record.is_read1,
        pysam_alignment_record.is_read2)


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

        :param GroupLogInfo val:
        :rtype: ParticipantLogInfo
        """
    return cls('group', val)


@staticmethod
def _add_identifiers_to_request(request, response_id):
    """ Add ``response_id`` to ``request`` meta. """
    meta = request.meta
    meta['__id'] = response_id
    return request.replace(meta=meta)


def _create_fieldname_to_type_map(flat_dict_list):
    """
    This method will create a map of file names to the respective data types
    this is used so we can put type hints in the resultant CSV

    For now this will only populate integer fields

    Parameters:

    flat_dict_list(in) -- This is a list of dictionaries that are already flattened into
                      full path:value
    """
    fieldname_to_type = {}
    for flat_dict in flat_dict_list:
        for key, value in flat_dict.items():
            if key in fieldname_to_type:
                existing_field_type = fieldname_to_type[key]
                if (existing_field_type != 'BAD' and existing_field_type !=
                    type(value)):
                    fieldname_to_type[key] == 'BAD'
            elif type(value) == int:
                fieldname_to_type[key] = 'int'
            elif type(value) == bool:
                fieldname_to_type[key] = 'bool'
    return fieldname_to_type


def get_saved(object):
    """ return the saved attribue of an object. """
    p = ''
    if '_container' in object:
        p = getattr(object._container, 'saved', '')
        if p:
            return p


def unregisterProducer():
    """
        Stop consuming data from a producer, without disconnecting.
        """


def link():
    """ Return html render method when "do_not_track" is active"""


def object_map(api):
    """get all relevant objects from the server and put it in a single dictionary"""
    uid_mapping = {}
    params1 = {'paging': False}
    resp1 = api.get(endpoint='indicatorTypes', params=params1).json()
    for elem in resp1['indicatorTypes']:
        uid_mapping[elem['id']] = {u'desc': u'{}'.format(elem['displayName'])}
    params2 = {'fields': 'id,name,value', 'paging': False}
    resp2 = api.get(endpoint='constants', params=params2).json()
    for elem in resp2['constants']:
        uid_mapping[elem['id']] = {u'desc': u'[Name: {} - Value: {}]'.
            format(elem['name'], elem['value'])}
    params3 = {'fields': 'id,name,organisationUnits', 'paging': False}
    resp3 = api.get(endpoint='organisationUnitGroups', params=params3).json()
    for elem in resp3['organisationUnitGroups']:
        uid_mapping[elem['id']] = {u'desc': u'[Name: {} - OUG Size: {}]'.
            format(elem['name'], len(elem['organisationUnits']))}
    params4 = {'fields': 'id,name', 'paging': False}
    resp4 = api.get(endpoint='dataElements', params=params4).json()
    for elem in resp4['dataElements']:
        uid_mapping[elem['id']] = {u'desc': u'{}'.format(elem['name'])}
    params5 = {'fields': 'id,name', 'paging': False}
    resp5 = api.get(endpoint='categoryOptionCombos', params=params5).json()
    for elem in resp5['categoryOptionCombos']:
        uid_mapping[elem['id']] = {u'desc': u'{}'.format(elem['name'])}
    params6 = {'fields': 'id,name', 'paging': False}
    resp6 = api.get(endpoint='programs', params=params6).json()
    for elem in resp6['programs']:
        uid_mapping[elem['id']] = {u'desc': u'{}'.format(elem['name'])}
    params7 = {'fields': 'id,name', 'paging': False}
    resp7 = api.get(endpoint='programIndicators', params=params7).json()
    for elem in resp7['programIndicators']:
        uid_mapping[elem['id']] = {u'desc': u'{}'.format(elem['name'])}
    params8 = {'fields': 'id,name', 'paging': False}
    resp8 = api.get(endpoint='trackedEntityAttributes', params=params8).json()
    for elem in resp8['trackedEntityAttributes']:
        uid_mapping[elem['id']] = {u'desc': u'{}'.format(elem['name'])}
    params9 = {'fields': 'id,name', 'paging': False}
    resp9 = api.get(endpoint='programStages', params=params9).json()
    for elem in resp9['programStages']:
        uid_mapping[elem['id']] = {u'desc': u'{}'.format(elem['name'])}
    return uid_mapping


def takes_kwargs(function):
    """Returns True if the supplied function takes keyword arguments"""
    return bool(function.__code__.co_flags & 8)


def analyzer_resolution(analyzer_information, slit_width=None, slit_number=
    None, pass_energy=10):
    """
    Estimates analyzer resolution from slit dimensioons pass energy, and analyzer radius.
    :param analyzer_information:
    :param slit_width:
    :param slit_number:
    :param pass_energy:
    :return:
    """
    if slit_width is None:
        slit_width = analyzer_information['slits'][slit_number]
    return 1000 * pass_energy * (slit_width / (2 * analyzer_information[
        'radius']))


def set_solid(actor):
    """
    set an actor's look to solid

    :param actor:

    """
    actor.wireframe(value=False)


def _validate_regex_flags(flags: str):
    """Check if input flags are valid.
    """
    allowed_flags = {'g', 'i', 'm', 's', 'u', 'y'}
    for char in flags:
        if char in allowed_flags:
            allowed_flags.remove(char)
        else:
            raise ValueError(
                'Invalid flags. see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp for more details.'
                )


def trunc(data=None, out=None, name=None, **kwargs):
    """Return the element-wise truncated value of the input.

    The truncated value of the scalar x is the nearest integer i which is closer to
    zero than x is. In short, the fractional part of the signed number x is discarded.

    Example::

       trunc([-2.1, -1.9, 1.5, 1.9, 2.1]) = [-2., -1.,  1.,  1.,  2.]

    The storage type of ``trunc`` output depends upon the input storage type:

       - trunc(default) = default
       - trunc(row_sparse) = row_sparse
       - trunc(csr) = csr

    Defined in src/operator/tensor/elemwise_unary_op_basic.cc:L857

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

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

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


def drcomplete(N, Index, r, p, solver=None, gamma=1, rho=1, Z0=None, tol=None):
    """
    Douglas-Rachford proximal splitting for low-rank
    completion through the low-rank inducing Frobenius/spectral norm and
    non-convex Douglas-Rachford for integer-valued r.

    M,rankM,err,D,Z_fix,iter = DRCOMPLETE(N,Index,r,p) determines a low-rank matrix completion
    solution M though the low-rank inducing Frobenius/spectral norm, i.e., M
    is a solution to

    minimize ||M||_{ell_p,r*}
        s.t.   M(Index) = N(Index)
    for p = 2 or p = 'inf', where Index is the logical incident matrix of the
    known entries in N. Therefore, N should contain the correct known entries,
    whereas the unknowns can be arbitrary. Further,
        1. rankM = rank(M)
        2. err = norm(N-M,'fro') (This is only to compare the performance)
        with other methods, when N is explicitly known.
        3. D = solution of the dual problem
        4. Z_fix = fix point of the Douglas-Rachford iterations
        5. iter = total number of Douglas-Rachford iterations

    ... = DRCOMPLETE(N,Index,r,p,option) allows to specify further options:
        1. ... = DRCOMPLETE(N,Index,r,p,...,solver = 'NDR',...) sets the
        solver to the non-convex Douglas-Rachford.
        2. ... = DRCOMPLETE(N,Index,r,p,...,gamma = gamma_val,...) multiplies
        the objective functions with gamma when determining the prox of them.
        The default value is set to gamma = 1.
        3. ... = DRCOMPLETE(N,Index,r,p,...,rho = rho_val,...) set the step
        length update of the fix-point update, i.e.
        Z_{k+1} = Z_k + rho_val*(Y_k - X_k), where 0 < rho_val < 2.
        Default value: rho_val = 1.
        4. ... = DRCOMPLETE(N,Index,r,p,...,Z0 = Z0_val,...) sets the initial
        value of the fix-point iteration, i.e. Z_0 = Z0_val.
        Default choice: Z0 = randn(size(N)).
        5. ... = DRCOMPLETE(N,Index,r,p,...,tol=tol_val,...) sets the relative
        tolerance of:
            + The numerical rank: rankM = rank(M/norm(N0,'fro'),tol_val)
            + Iterations stop: Stop if (Y_k -X_k)/norm(N0,'fro') < tol_val
            + Zero values: E.g., D(abs(D/norm(N0,'fro')) < tol_val) = 0
        where N0[Index] = N[Index] and zero otherwise.
        Default value: tol_val = sqrt(eps), where eps denotes the machine precision constant.

    --------------
    References:
    - C. Grussler and A. Rantzer and P. Giselsson (2018):
    "Low-Rank Optimization with Convex Constraints",
    IEEE Transactions on Automatic Control, DOI: 10.1109/TAC.2018.2813009.

    - C. Grussler and P. Giselsson (2016):
    "Low-Rank Inducing Norms With Optimality Interpreations",
    arXiv:1612.03186v1.

    - C. Grussler and P. Giselsson (2017):
    "Local convergence of proximal splitting methods for rank constrained
    problems", pp. 702-708, IEEE 56th Annual Conference on Decision and Control
    (CDC), DOI: 10.1109/CDC.2017.8263743.

    - C. Grussler (2017):
    "Rank reduction with convex constraints", PhD Thesis,
    Department of Automatic Control, Lund Institute of Technology,
    Lund University, ISBN 978-91-7753-081-7.
    """
    import numpy as np
    from functools import partial
    from lripy.projindex import projindex
    from lripy.proxnonconv import proxnonconv
    from lripy.proxnormrast import proxnormrast
    from lripy.dr import dr
    if tol is None:
        tol = np.sqrt(np.finfo(float).eps)
    dim = N.shape
    if Z0 is None:
        Z0 = np.random.randn(dim[0], dim[1])
    if r > min(dim):
        ValueError('r is larger than min(N.shape)')
    N0 = np.zeros(dim)
    N0[Index] = N[Index]
    tol_N0 = np.linalg.norm(N0) * tol
    if solver == 'NDR':
        M, Z_fix, iter, D = dr(partial(proxnonconv, r=r, p=p), partial(
            projindex, N=N, Index=Index), dim, tol=tol_N0, gamma=gamma, Z0=
            Z0, rho=rho)
    else:
        M, Z_fix, iter, D = dr(partial(proxnormrast, r=r, p=p), partial(
            projindex, N=N, Index=Index), dim, tol=tol_N0, gamma=gamma, Z0=
            Z0, rho=rho)
    err = np.linalg.norm(M - N)
    rankM = np.linalg.matrix_rank(M, tol_N0)
    return M, rankM, err, D, Z_fix, iter


def apply_overwrites_to_context(context, overwrite_context):
    """Modify the given context in place based on the overwrite_context."""
    for variable, overwrite in overwrite_context.items():
        if variable not in context:
            continue
        context_value = context[variable]
        if isinstance(context_value, list):
            if overwrite in context_value:
                context_value.remove(overwrite)
                context_value.insert(0, overwrite)
        else:
            context[variable] = overwrite


def clean(threshold: float=0.001, channels: bool=False):
    """Simplify F-Curves by removing closely spaced keyframes

    :param threshold: Threshold
    :type threshold: float
    :param channels: Channels
    :type channels: bool
    """
    pass


@classmethod
def unlink(cls, graph, origin, target):
    """  Remove all the edges between :param origin: and :param target in a :param graph.
        """
    for edge in graph.edge(origin, target, all_edges=True):
        graph.remove_edge(edge)


def find_in_enum_members(item, enum_members: list):
    """Matches a string in the provided list of member strings.
	The item must be not fully matched.
	Rules:
	- If the member string starts with underscore, it is cut out before the matching continues.
	- The item is matched if a member string starts with the item (item is a prefix of the member)."""
    found = None
    for x in enum_members:
        if x.startswith(item):
            found = x
            break
        if x.startswith('_'):
            if x.strip('_').startswith(item):
                found = x
                break
    return found


def log_error():
    """Nextflow specific function that logs an error upon unexpected failing
    """
    with open('.status', 'w') as status_fh:
        status_fh.write('error')


def select_anno(List):
    """Function to assign the hierarchically right choice of annotation"""
    if 'Exon' in List:
        return 'Exon'
    elif 'Intron' in List:
        return 'Intron'
    elif 'Genic' in List:
        return 'Genic'
    elif 'Intergenic' in List:
        return 'Intergenic'


def _ihexline(address, buffer, end=False, record_type=0):
    """internal use: generate a line with intel hex encoded data"""
    out = []
    if end:
        record_type = 1
    out.append(b':%02X%04X%02X' % (len(buffer), address & 65535, record_type))
    sum = len(buffer) + (address >> 8 & 255) + (address & 255) + (record_type &
        255)
    for b in bytearray(buffer):
        out.append(b'%02X' % (b & 255))
        sum += b & 255
    out.append(b'%02X\r\n' % (-sum & 255))
    return b''.join(out)


@staticmethod
def guard(*args, **kwargs):
    """ Look in storage.

        :returns: False

        """
    return False


@classmethod
def deserialize(cls, data):
    """
        Constructs a message instance from the given data

        :param data: the raw data, minus the type and size info, from rexster
        :type data: str/bytearray

        :rtype: RexProMessage
        """
    raise NotImplementedError


def print_remote_info(parsed_args, remotes):
    """
    Prints the collection of remotes to the console.
    If the "--verbose" argument is provided then the URL will be displayed too.

    :param parsed_args: arguments parsed from the user (may contain verbose arg)
    :type parsed_args: <class 'argparse.Namespace'>
    :param remotes: collection of remotes
    :type remotes: dictionary (key = remote name, value = URL)
    """
    verbose = parsed_args.verbose
    if remotes:
        for key, value in remotes.items():
            if key:
                if verbose:
                    print(key + ': ' + value)
                else:
                    print(key)
    else:
        print('info: no remote found')


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

    The ".dev0" means dirty.

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


def issues2dict(issues):
    """Convert a list of issues to a dict, keyed by issue number."""
    idict = {}
    for i in issues:
        idict[i['number']] = i
    return idict


def get_list(realm, wl, req, fields=None):
    """Returns list of watched elements as dictionaries plus an extra
        dictionary of extra template data, which will be available in the
        template under the name "<realm>data"

        Example:
            data = [ {'name':'example', 'changetime': <DT Object> }, { ... } ]
            extradict = { 'somethingspecial':42, ... }
            return data, extradict
        """
    pass


def unquoted(string: str) ->str:
    """Return the given string, unquoted.

    >>> unquoted('a')
    'a'
    >>> unquoted('"a"')
    'a'
    >>> unquoted('"a')
    '"a'

    """
    if string[0] == '"' and string[-1] == '"':
        return string[1:-1]
    return string


def convert_string_boolean(value):
    """
    this method will convert string boolean to the python boolean type
    """
    if type(value) is str:
        val = value.lower()
        if val == 'yes' or val == 'true':
            return True
        elif val == 'no' or val == 'false':
            return False
    return False


def _pointsFromList(L):
    """
    given a list of coordinates [x0, y0, x1, y1....]
    produce a list of points [(x0,y0), (y1,y0),....]
    """
    P = []
    a = P.append
    for i in range(0, len(L), 2):
        a((L[i], L[i + 1]))
    return P


@classmethod
def list_supported_services(cls):
    """Lists all services supported by this driver

        :return: [String] list of service names
        """
    return cls.SERVICES.keys()


def previewrange_set():
    """Automatically set Preview Range based on range of keyframes

    """
    pass


def flow_cylinder(diameter=250, length=1, viscosity=1, pressure=0.1,
    output_units='nL'):
    """
    calculates flow rate via a cylindrical capillary with a given diameter, length and fluid viscosity under a given pressure drop. Also takes string value to define output units

    Parameters
    ----------
    length : float
        length of a cylinder in meters
    diameter : float
        diameter of round cross-section in micrometer
    viscosity : float
        viscosity in cP or mPa*s
    pressure : float
        pressure difference in atmospheres
    output_units
        output units for the flow. Supports: nL/s, uL/s. Default: nL/s

    Returns
    -------
    flow : float
        flow of fluid in nL/s

    Examples
    --------
    the example of usage.
    D=50um,L = 1, eta = 1, P = 2 -> 31.086 nL/s
    D=10oum,L = 0.7, eta = 23, P = 2 -> 30.893 nL/s

    >>> flow_cylinder(diameter = 50, length = 1, viscosity = 1.0, pressure = 2.0)
    >>> 31.086120666502506
    """
    from numpy import pi
    diameter = diameter * 10 ** -6
    length = length
    viscosity = viscosity * 10 ** -3
    pressure = pressure * 101325
    flow = pi * pressure * diameter ** 4 / (128 * viscosity * length)
    if output_units == 'pL':
        output_factor = 10 ** 15
    elif output_units == 'nL':
        output_factor = 10 ** 12
    elif output_units == 'uL':
        output_factor = 10 ** 9
    elif output_units == 'mL':
        output_factor = 10 ** 6
    elif output_units == 'L':
        output_factor = 10 ** 3
    else:
        output_factor = 10 ** 12
    return flow * output_factor


def is_pronoun(pos_tag):
    """Return ``True`` if ``pos_tag`` is ``PRON``, False otherwise.

    :param pos_tag: Part of Speech tag
    :type pos_tag: string
    :return: True if POS is pronoun
    :rtype: boolean
    """
    return pos_tag == 'PRON'


def verify_indices_all_unique(obj):
    """
    Check that all axes of a pandas object are unique.

    Parameters
    ----------
    obj : pd.Series / pd.DataFrame / pd.Panel
        The object to validate.

    Returns
    -------
    obj : pd.Series / pd.DataFrame / pd.Panel
        The validated object, unchanged.

    Raises
    ------
    ValueError
        If any axis has duplicate entries.
    """
    axis_names = [('index',), ('index', 'columns'), ('items', 'major_axis',
        'minor_axis')][obj.ndim - 1]
    for axis_name, index in zip(axis_names, obj.axes):
        if index.is_unique:
            continue
        raise ValueError('Duplicate entries in {type}.{axis}: {dupes}.'.
            format(type=type(obj).__name__, axis=axis_name, dupes=sorted(
            index[index.duplicated()])))
    return obj


@classmethod
def get_callback(cls, *arguments):
    """Get a callback function for the AST."""
    return cls.run


def index_by_modifier(sequence, modifier, target_index=None):
    """
    Create an index of objects in a sequence. The index's key will be provider by the modifier. Values are single
    objects, so in case of a duplicate key the latest object will persist.

    The modifier is called for each object with that object as an argument and is supposed to
    return the key to be used for that object in the index.

    :param sequence: sequence of objects (list, tuple or otherwise iterable object)
    :param modifier: callable that takes an object and returns a hashable value.
    :param target_index: dict that will be populated with objects from the sequence (optional)
    :return: dict
    """
    index = target_index if target_index is not None else dict()
    for element in sequence:
        key = modifier(element)
        index[key] = element
    return index


def map_dict_to_list(d, width):
    """
    Given a dictionary -> {start: quantity, start: quantity, ...}
    Return a list of tuples [(start, end, quantity), (start, end, quantity), ...]
    """
    assert width >= 0
    for startTimeMillis in d:
        yield int(startTimeMillis), int(startTimeMillis) + width, d[
            startTimeMillis]


def ct_voidp(ptr):
    """Convert a SIP void* type to a ctypes c_void_p"""
    import ctypes
    return ctypes.c_void_p(int(ptr))


def flatten_multiindex(ii, strides):
    """Return the flat index corresponding to the given multiindex."""
    i = 0
    for c, s in zip(ii, strides):
        i += c * s
    return i


@classmethod
def fromEvents(cls, events, unk02=0, bankID=0, volume=127, channelPressure=
    64, polyphonicPressure=50, playerID=0):
    """
        Create a new SSEQ object from a list of sequence events.
        """
    obj = cls(None, unk02, bankID, volume, channelPressure,
        polyphonicPressure, playerID)
    obj.events = events
    return obj


@staticmethod
def _asline(key, val):
    """Convert given key and value list to MIME header line."""
    return key + ': ' + '; '.join(val) + '\n'


def array_reverse_order_changed_distances(size, move):
    """Aid function for delta evaluation.

    Works with the array_reverse_order move type.
    This function returns the pairs who would have an altered evaluation
    value due to the move.

    Parameters
    ----------
    size : int
        The size of the array.
    move : tuple of int
        A tuple of 2 ints that represents a single valid move.

    Returns
    -------
    set of tuple
        this set contains a tuple with every (from,to) pair that would have
        an altered evaluation value due to the move.
        A pair (x, y) and a pair (y, x) are assumed to have different
        evaluation values.

    Examples
    --------
    Some simple examples to demonstrate the behaviour:

    .. doctest::

        >>> from lclpy.evaluation.deltaeval.delta_tsp import \\
        ...     array_reverse_order_changed_distances as \\
        ...         changed_distances
        ... # init
        >>> size = 10
        ... # tests
        ... # since the order of the items in a set might be different,
        ... # they are compared to an equivalent set.
        >>> changed = changed_distances(size, (4, 8))
        >>> changed == {(3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)}
        True
        >>> changed = changed_distances(size, (4, 9))
        >>> changed == {(3, 4), (4, 5), (5, 6),
        ...             (6, 7), (7, 8), (8, 9), (9, 0)}
        True
        >>> changed = changed_distances(size, (0, 4))
        >>> changed == {(9, 0), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5)}
        True
        >>> changed = changed_distances(size, (0, 9))
        >>> changed == {(0, 1), (1, 2), (2, 3), (3, 4), (4, 5),
        ...             (5, 6), (6, 7), (7, 8), (8, 9), (9, 0)}
        True

    """
    changed_dist = set()
    if move[0] == 0:
        changed_dist.add((size - 1, 0))
    else:
        changed_dist.add((move[0] - 1, move[0]))
    if move[1] == size - 1:
        changed_dist.add((size - 1, 0))
    else:
        changed_dist.add((move[1], move[1] + 1))
    for i in range(move[0], move[1]):
        changed_dist.add((i, i + 1))
    return changed_dist


def precision(tp, tn, fp, fn):
    """精确率=(TP+TN)/(TP+TN+FP+FN)"""
    return (tp + tn) / (tp + tn + fp + fn)


def tidy_mothur_classifications(classification_data):
    """Renoves the reported classification percentage agreements that Mothur appends to the classificaitons."""
    new_classification_data = classification_data.copy(deep=True)
    for column in classification_data:
        new_classification_data[column] = new_classification_data[column
            ].str.rsplit('(', 1, expand=True)[0]
    return new_classification_data


def button_release_event(widget, event):
    """ React to a button release event.
    """
    global lastx
    global lasty
    lastx = None
    lasty = None
    return True


@staticmethod
def _log_format_con(window):
    """
        Create an informatinal string for logging leaves.

        Returns:
            str: '<Con: class="{}" instance="{}" title="{}" id={}>'
        """
    return '<Con: class="{}" instance="{}" title="{}" id={}>'.format(window
        .window_class, window.window_instance, window.name, window.id)


@classmethod
def upload_content(cls, options):
    """Upload content to repository."""
    cls.command_sub = 'upload-content'
    return cls.execute(cls._construct_command(options), output_format='csv',
        ignore_stderr=True)


def unsetPermissionForPrincipal(permission_id, principal_id):
    """Remove the permission (either denied or allowed) from the
        principal.
        """


def first():
    """Returns the first ICachableItem available in the ICachableSource or None"""


def summaryHelpEpilog():
    """
    text for help epilog for
    summary
    """
    return 'COMING SOON'


def find_highest_position(cur, node):
    """
    Return highest, not occupied position in the children of ``node``.

    :param node:
    :type node: Node or uuid4
    """
    if node is not None:
        id = str(node)
    else:
        id = None
    sql = """
      SELECT
        MAX(position)
      FROM
        nodes
      WHERE
        parent=%s;
    """
    cur.execute(sql, (id,))
    result = cur.fetchone()['max']
    if result is not None:
        return result
    else:
        return -1


@classmethod
def register_native_mapping(cls, type_id, callback):
    """
        Register a function to convert a core foundation data type into its
        equivalent in python

        :param type_id:
            The CFTypeId for the type

        :param callback:
            A callback to pass the CFType object to
        """
    cls._native_map[int(type_id)] = callback


@classmethod
def to_ordinal(cls, d):
    """Return quarter ordinal for date `d`."""
    return d.year * 4 + d.quarter


def randomize_smiles(smiles):
    """
    Require `RDKit` library.

    Generate a new SMILES string for the same molecule.

    Perform a randomization of a SMILES string must be RDKit sanitizable.
    """
    import random
    import numpy as np
    from rdkit import Chem
    m = Chem.MolFromSmiles(smiles)
    ans = list(range(m.GetNumAtoms()))
    np.random.shuffle(ans)
    nm = Chem.RenumberAtoms(m, ans)
    return Chem.MolToSmiles(nm, canonical=False, isomericSmiles=True,
        kekuleSmiles=False)


@classmethod
def set_precision(cls, precision):
    """Set the number of decimal places used to report percentages."""
    assert 0 <= precision < 10
    cls._precision = precision
    cls._near0 = 1.0 / 10 ** precision
    cls._near100 = 100.0 - cls._near0


def _get_regimes_set(regimes):
    """ Creates a list with regimes in alphabetical order. """
    regimes_set = list(set(regimes))
    if isinstance(regimes_set[0], float):
        regimes_set1 = list(set(map(int, regimes_set)))
        if len(regimes_set1) == len(regimes_set):
            regimes_set = regimes_set1
    regimes_set.sort()
    return regimes_set


def desaturate(rgb, amount):
    """
        desaturate colors by amount
        amount == 0, no change
        amount == 1, grey
    """
    luminosity = sum(rgb) / 3.0
    desat = lambda color: color - amount * (color - luminosity)
    return tuple(map(int, map(desat, rgb)))


def extract_links_self_href(cls, v):
    """Extract the _links.self.href subfield."""
    if not isinstance(v, dict):
        raise TypeError('Field "_links" type must be dict')
    try:
        href = v['self']['href']
    except AttributeError:
        raise ValueError('Field "_links" must contain  ["self"]["href"]')
    return href


def range_vs_date_slash(nsw):
    """Return 'True'1' if nsw looks like a date with a slash in."""
    if '/' not in nsw:
        return False
    elif nsw.count('/') == 2:
        return 1
    elif nsw[0] == '0':
        return 1
    else:
        slash = nsw.find('/')
        first = nsw[:slash]
        second = nsw[slash + 1:]
        if not (first.isdigit() and second.isdigit()):
            return False
        elif len(first) != len(second):
            return 0
        elif int(first) > 12 and int(second) > 12 or int(first) > 31 or int(
            second) > 31:
            return 0
        elif len(first) == 1:
            return False
        elif int(second) > int(first[-len(second):]):
            return 0
        else:
            return 1


@classmethod
def resolve(cls, settings):
    """ Resolve settings dictionaries."""
    return settings or cls()


def __add_roundkey(message, roundkey):
    """
        Input: message - List of lists (4x4 Matrix)
               roundkey - List of lists (4x4 Matrix)

        Output: message - List of lists (4x4 Matrix)

        In this method, each element of the original matrix is XORed with
        corresponding element of roundkey. This way a matrix with new values
        is created.
    """
    for i in range(4):
        for j in range(4):
            message[i][j] = message[i][j] ^ roundkey[i][j]
    return message


def ensure_unicode(s):
    """Not all of PyObjC understands unicode paths very well yet"""
    if isinstance(s, bytes):
        return s.decode('utf-8')
    return s


def getMembers():
    """Returns a list of IMember in the dimension."""


def get_module_version(module):
    """
    Retrieve __version__ variable from an imported module.

    @param module: The module instance.
    @type module: module
    @return: The version hash without the surrounding text. If not present
        return None.
    @rtype: str or None
    """
    if hasattr(module, '__version__'):
        return module.__version__[5:-1]


def info():
    """Return upgrade recipe information."""
    return 'Fixes foreign key relationship.'


def domainmatch(ptrs, domainsuffix):
    """grep for a given domain suffix against a list of validated PTR
    domain names.

    Examples:
    >>> domainmatch(['FOO.COM'], 'foo.com')
    1

    >>> domainmatch(['moo.foo.com'], 'FOO.COM')
    1

    >>> domainmatch(['moo.bar.com'], 'foo.com')
    0

    """
    domainsuffix = domainsuffix.lower()
    for ptr in ptrs:
        ptr = ptr.lower()
        if ptr == domainsuffix or ptr.endswith('.' + domainsuffix):
            return True
    return False


def parity_even_p(state, marked_qubits):
    """
    Calculates the parity of elements at indexes in marked_qubits

    Parity is relative to the binary representation of the integer state.

    :param state: The wavefunction index that corresponds to this state.
    :param marked_qubits: The indexes to be considered in the parity sum.
    :returns: A boolean corresponding to the parity.
    """
    assert isinstance(state, int
        ), '{} is not an integer. Must call parity_even_p with an integer state.'.format(
        state)
    mask = 0
    for q in marked_qubits:
        mask |= 1 << q
    return bin(mask & state).count('1') % 2 == 0


def timer(start, end):
    """
    This function calcualtes the elapsed time between the start of the function until the end of this.
    :param: float: starting time.
    :param: float: ending time.
    :return: str: time elapsed.
    """
    hours, rem = divmod(end - start, 3600)
    minutes, seconds = divmod(rem, 60)
    return '{:0>2}:{:0>2}:{:05.2f}'.format(int(hours), int(minutes), seconds)


def getConditionChangeDays(thrsBool):
    """Returns the days at which the conditions depitcted by the thresholds
        bools array changes state.

    Parameters
    ----------
    thrsBool : numpy array of bools
        Numpy array with the met/unmet condition results
            ('comparePopToThresholds' output).
    Returns
    -------
    list
        List of days where each of the conditions changes.

    """
    shp, days = thrsBool.shape, []
    for i in range(shp[0] - 1):
        a, b = thrsBool[i], thrsBool[i + 1]
        change = a ^ b
        if any(change):
            days.append(i)
    return days


def setBrowserIdCookieByForce(bid):
    """Sets the browser id cookie to browser id 'bid' by force.

        o Useful when you need to 'chain' browser id cookies across domains
          for the same user (perhaps temporarily using query strings).

        o Permission required: Access contents information

        o Raise BrowserIdManagerErr if the 'cookies' namespace isn't
          a browser id namespace.
        """


def update_complited(status: bool):
    """
    上链完成后调用，更新数据库的状态
    :param status: 需要更新的状态值
    :return:
    """
    pass


def put_args_in_db(db, args):
    """
    Takes the args with not-None input values of a function and overwrites
    the values of the corresponding keys in dict db.
    | (args are collected with the built-in function locals(),
    | See example usage below)

    Args:
        :db:
            | dict

    Returns:
        :returns:
            | dict with the values of specific keys overwritten by the
            |      not-None values of corresponding args of a function fcn.

    Example usage:
        | _db = {'c' : 'c1', 'd' : 10, 'e' : {'e1':'hello', 'e2':1000}}
        |
        | def test_put_args_in_db(a, b, db = None, c = None,d = None,e = None):
        |
        |    args = locals().copy()  # get dict with keyword input arguments to
        |                             # function 'test_put_args_in_db'
        |
        |     db = put_args_in_db(db,args) # overwrite non-None args in db copy.
        |
        |    if db is not None: # unpack db for further use
        |        c,d,e = [db[x] for x in sorted(db.keys())]
        |
        |     print(' a : {}'.format(a))
        |     print(' b : {}'.format(b))
        |     print(' db: {}'.format(db))
        |     print(' c : {}'.format(c))
        |     print(' d : {}'.format(d))
        |     print(' e : {}'.format(e))
        |     print('_db: {}'.format(_db))

    """
    if db is not None:
        dbc = db.copy()
        dbc_keys = dbc.keys()
        for argkey in args.keys():
            if args[argkey] is not None:
                if argkey in dbc_keys:
                    dbc[argkey] = args[argkey]
        return dbc
    else:
        return db


def format_bytes(num_bytes):
    """
    Sweet little implementation from
    http://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
    """
    if num_bytes is None:
        return '0kb'
    for unit in ['b', 'kb', 'mb', 'gb']:
        if num_bytes < 1024.0 and num_bytes > -1024.0:
            return '%3.1f%s' % (num_bytes, unit)
        num_bytes /= 1024.0
    return '%3.1f%s' % (num_bytes, 'tb')


def _check_methods(C, *methods):
    """
    Check that all methods listed are in the __dict__ of C, or in the classes of it's mro.
    One trick pony borrowed from collections.abc.
    """
    mro = C.__mro__
    for method in methods:
        for B in mro:
            if method in B.__dict__:
                if B.__dict__[method] is None:
                    return NotImplemented
                break
        else:
            return NotImplemented
    return True


def loadNullTerminatedStringFrom(data, offset, charWidth=1, encoding='latin-1'
    ):
    """
    Load a null-terminated string from data at offset, with the options
    given
    """
    end = data.find(b'\x00' * charWidth, offset)
    return data[offset:end].decode(encoding)


def getCountDict(inlist):
    """list -> count dict"""
    outCD = {}
    for element in inlist:
        if element in outCD:
            outCD[element] += 1
        else:
            outCD[element] = 1
    return outCD


def set_run_validators(run: bool) ->None:
    """
    Set whether or not validators are enabled.
    :param run: whether the validators are run
    """
    if not isinstance(run, bool):
        raise TypeError("'run' must be bool.")
    global _run_validators
    _run_validators = run


def mps2knots(value):
    """float: float: velocity: metres per second to knots."""
    return value * 1.94384


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


def p_print_stmt_plus_2(p):
    """print_stmt_plus : print_stmt_plus COMMA test"""
    p[0] = p[1] + [p[3]]


def get_mode_parameter(mode):
    """
    Map the mode parameter to the server parameter
    """
    if mode == 'job':
        return 'cli'
    elif mode == 'serve':
        return 'serve'
    else:
        return mode


def positioning_headlines(headlines):
    """
    Strips unnecessary whitespaces/tabs if the first header is not left-aligned.
    """
    left_just = False
    for row in headlines:
        if row[-1] == 1:
            left_just = True
            break
    if not left_just:
        for row in headlines:
            row[-1] -= 1
    return headlines


def _check_valid(key, val, valid):
    """Helper to check valid options"""
    if val not in valid:
        raise ValueError('%s must be one of %s, not "%s"' % (key, valid, val))


def object_settings_copy():
    """Copy Rigid Body settings from active object to selected

    """
    pass


def _vars_as_list(argument):
    """Type checking, returns a list for indexing."""
    if type(argument) in [list, tuple]:
        return argument
    elif type(argument) is str:
        return [argument]
    else:
        raise RuntimeError(
            'Invalid argument: accepts only lists, tuples, andstrings.')


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


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

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


@classmethod
def disable(cls):
    """Disables debugger for given page.
        """
    return cls.build_send_payload('disable', {}), None


@classmethod
def getAnyAppWithWindow(cls):
    """Get a random app that has windows.

        Raise a ValueError exception if no GUI applications are found.
        """
    apps = cls._getRunningApps()
    for app in apps:
        pid = app.processIdentifier()
        ref = cls.getAppRefByPid(pid)
        if hasattr(ref, 'windows') and len(ref.windows()) > 0:
            return ref
    raise ValueError('No GUI application found.')


def clearSynchs():
    """Unregister all registered `ISynchronizer` objects.

        This exists to support test cleanup/initialization
        """


def _name_to_child_map(children):
    """
    Create a map of name to child based on a list.
    :param children [LocalFolder/LocalFile] list of children:
    :return: map child.name -> child
    """
    name_to_child = {}
    for child in children:
        name_to_child[child.name] = child
    return name_to_child


def copy_to_selected_button(all: bool=True):
    """Copy property from this object to selected objects or bones

    :param all: All, Copy to selected all elements of the array
    :type all: bool
    """
    pass


def add_channel_arguments(parser):
    """Add arguments only used if running mulled over a whole conda channel."""
    parser.add_argument('--repo-data', dest='repo_data', required=True,
        help=
        'Published repository data. If you want to build all containers for bioconda, this parameter needs to be set to "bioconda"'
        )
    parser.add_argument('--diff-hours', dest='diff_hours', default='25',
        help=
        'If finding all recently changed recipes, use this number of hours.')
    parser.add_argument('--recipes-dir', dest='recipes_dir', default=
        './bioconda-recipes')
    parser.add_argument('--force-rebuild', dest='force_rebuild', action=
        'store_true', help='Rebuild package even if already published.')


@classmethod
def add_common_opts(cls, parser):
    """Add options that are common for several plugins.
        """
    for opt in cls.common_opt_names:
        cls._parser_add_opt(parser, opt)


def big_endian_to_int(value):
    """
    Convert big endian to int

    :param value: big ending value
    :return: int value
    """
    return int.from_bytes(value, byteorder='big')


@classmethod
def get_subclass(cls, obj):
    """ Get the subclass to use when instantiating a polymorphic object.
            The default implementation looks at ``cls.config_polymorphic``
            to get the name of an attribute.  Subclasses automatically
            register their value for that attribute on creation via their
            ``config_polymorphic_identity`` field.  This process is then
            repeated recursively until None is returned (indicating that the
            current class is the correct one)

            This method can be overridden to allow any method you would like
            to use to select subclasses. It should return either the subclass
            to use or None, if the original class should be used.
        """
    if cls.config_polymorphic is None:
        return
    value = obj.get(cls.config_polymorphic)
    value = cls._subclasses.get(value)
    if value == cls or value is None:
        return None
    sub_value = value.get_subclass(obj)
    if sub_value is None:
        return value
    return sub_value


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

    The ".dev0" means dirty.

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


def temporaryDirectory():
    """Return a directory that should be used for uncommitted blob data.

        If Blobs use this, then commits can be performed with a simple rename.
        """


def detectencoding_unicode(input, final=False):
    """
    Detect the encoding of the unicode string ``input``, which contains the
    beginning of a CSS file. The encoding is detected from the charset rule
    at the beginning of ``input``. If there is no charset rule, ``"utf-8"``
    will be returned.

    If the encoding can't be detected yet, ``None`` is returned. ``final``
    specifies whether more data will be available in later calls or not. If
    ``final`` is true, ``detectencoding_unicode()`` will never return ``None``.
    """
    prefix = '@charset "'
    if input.startswith(prefix):
        pos = input.find('"', len(prefix))
        if pos >= 0:
            return input[len(prefix):pos], True
    elif final or not prefix.startswith(input):
        return 'utf-8', False
    return None, False


def apply_func(key_kwargs_, func):
    """
    given some keywords and a function call the function
    :param key_kwargs_: a tuple of (key, args) to use
    :param func: function to call
    :return: the key for this and the value returned from calling the func
    """
    key = key_kwargs_[0]
    kwargs_ = key_kwargs_[1]
    val = func(*kwargs_)
    return key, val


def names(records):
    """Return the column names of the records

    :param records: a list of dictionaries
    :returns: a list of strings

    >>> movies = [
    ... {'title': 'The Holy Grail', 'year': 1975, 'budget': 4E5, 'total_gross': 5E6},
    ... {'title': 'Life of Brian', 'year': 1979, 'budget': 4E6, 'total_gross': 20E6},
    ... {'title': 'The Meaning of Life', 'year': 1983, 'budget': 9E6, 'total_gross': 14.9E6}
    ... ]
    >>> names(movies)
    ['total_gross', 'year', 'budget', 'title']

    """
    return records[0].keys()


@classmethod
def from_data(cls, data):
    """

        Instantiate a new :class:`~nmrpy.data_objects.Fid` object by providing a
        spectral data object as argument. Eg. ::

            fid = Fid.from_data(data)
        """
    new_instance = cls()
    new_instance.data = data
    return new_instance


@staticmethod
def _handle_abbreviations(s):
    """ Handle abbreviations in the settings section """
    assert False


@classmethod
def uri_from_id(cls, viaf_id):
    """Generate a canonical VIAF URI for the specified id"""
    return '%s/%s' % (cls.uri_base, viaf_id)


def common_elements(left, right) ->int:
    """ Returns the number of elements that are common between `left` and `right`"""
    common = set(left) & set(right)
    return len(common)


@classmethod
def from_dictionary(cls, json):
    """
        creates Constraints class instance with the json dictionary.
        :param json: json dictionary
        :return instance of Constraints class with the json data
        """
    try:
        c = cls(json['MaxValue'], json['MinValue'], json['MinIndex'], json[
            'MaxIndex'], json['Link'])
    except KeyError:
        c = cls(json['MaxValue'], json['MinValue'])
    return c


def item_deelgemeente_adapter(obj, request):
    """
    Adapter for rendering a object of
    :class:`crabpy.gateway.crab.Deelgemeente` to json.
    """
    return {'id': obj.id, 'naam': obj.naam, 'gemeente': {'id': obj.gemeente
        .id, 'naam': obj.gemeente.naam}}


def delete():
    """Remove all selected keyframes

    """
    pass


def ob2h5(x):
    """ If x is None, returns "None", otherwise x """
    if x is None:
        return 'None'
    else:
        return x


def real(quaternion):
    """Return real part of quaternion.
    """
    return float(quaternion[0])


@classmethod
def instance_to_filename(klass, img, filename):
    """ Save `img` in our own format, to name implied by `filename`

        This is a class method

        Parameters
        ----------
        img : ``any FileBasedImage`` instance

        filename : str
           Filename, implying name to which to save image.
        """
    img = klass.from_image(img)
    img.to_filename(filename)


def CheckLineIntersection(x1, y1, x2, y2, x3, y3, x4, y4):
    """
    Check for line intersection

    :param `x1`: x1 position
    :param `y1`: y1 position
    :param `x2`: x2 position
    :param `y2`: y2 position
    :param `x3`: x3 position
    :param `y3`: y3 position
    :param `x4`: x4 position
    :param `y4`: y4 position

    :returns: a lenght ratio and a k line???

    """
    denominator_term = (y4 - y3) * (x2 - x1) - (y2 - y1) * (x4 - x3)
    numerator_term = (x3 - x1) * (y4 - y3) + (x4 - x3) * (y1 - y3)
    length_ratio = 1.0
    k_line = 1.0
    if denominator_term < 0.005 and denominator_term > -0.005:
        line_constant = -1.0
    else:
        line_constant = float(numerator_term) / denominator_term
    if line_constant < 1.0 and line_constant > 0.0:
        if y4 - y3 < 0.005 and y4 - y3 > -0.005:
            k_line = (x1 - x3 + line_constant * (x2 - x1)) / (x4 - x3)
        else:
            k_line = (y1 - y3 + line_constant * (y2 - y1)) / (y4 - y3)
        if k_line >= 0 and k_line < 1:
            length_ratio = line_constant
        else:
            k_line = 1
    return length_ratio, k_line


@staticmethod
def parse_perfctr_event(perfctr):
    """
        Parse events in machine description to tuple representation used in Benchmark module.

        Examples:
        >>> parse_perfctr_event('PERF_EVENT:REG[0-3]')
        ('PERF_EVENT', 'REG[0-3]')
        >>> parse_perfctr_event('PERF_EVENT:REG[0-3]:STAY:FOO=23:BAR=0x23')
        ('PERF_EVENT', 'REG[0-3]', {'STAY': None, 'FOO': 23, 'BAR': 35})

        """
    split_perfctr = perfctr.split(':')
    assert len(split_perfctr
        ) >= 2, 'Atleast one colon (:) is required in the event name'
    event_tuple = split_perfctr[:2]
    parameters = {}
    for p in split_perfctr[2:]:
        if '=' in p:
            k, v = p.split('=')
            if v.startswith('0x'):
                parameters[k] = int(v, 16)
            else:
                parameters[k] = int(v)
        else:
            parameters[p] = None
    event_tuple.append(parameters)
    return tuple(event_tuple)


def get_missing_interval(all_intervals, i_min=0, i_max=32):
    """Return a list of missing interval in all_interval
    @all_interval: list of (int, int)
    @i_min: int, minimal missing interval bound
    @i_max: int, maximal missing interval bound"""
    my_intervals = all_intervals[:]
    my_intervals.sort()
    my_intervals.append((i_max, i_max))
    missing_i = []
    last_pos = i_min
    for start, stop in my_intervals:
        if last_pos != start:
            missing_i.append((last_pos, start))
        last_pos = stop
    return missing_i


def _resolve(name):
    """Resolve a dotted name to a global object."""
    name = name.split('.')
    used = name.pop(0)
    found = __import__(used)
    for n in name:
        used = used + '.' + n
        try:
            found = getattr(found, n)
        except AttributeError:
            __import__(used)
            found = getattr(found, n)
    return found