def extract_mime_part(part, pid, encode_erros='strict'):
    """
    Use MIME parts of an email message https://docs.python.org/3/library/email.html
    Parameters
    ----------
    part : email.Meesage
    pid : int
    encode_erros : {'strict', 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace'}, default 'strict'
        https://www.tutorialspoint.com/python3/string_decode.htm

    Returns
    -------
    part_data : dict

    """
    part_data = {'pid': pid, 'content_id': part.get('Content-id'),
        'content_main_type': part.get_content_maintype(),
        'content_sub_type': part.get_content_subtype(),
        'content_disposition': part.get_content_disposition(),
        'content_transfer_encoding': part.get('Content-Transfer-Encoding'),
        'content_charset': part.get_content_charset()}
    content_main_type = part_data['content_main_type']
    content_sub_type = part_data['content_sub_type']
    content_charset = part_data['content_charset']
    content_disposition = part_data['content_disposition']
    content_transfer_encoding = part_data['content_transfer_encoding']
    if (content_disposition != 'attachment' and content_main_type == 'text' and
        content_sub_type in ('plain', 'html')):
        isbody = True
        if content_charset is None:
            try:
                payload = part.get_payload(decode=True)
                for charset in ('ascii', 'latin1', 'utf-8', 'iso8859_1',
                    'Windows-1251', 'Windows-1252', 'iso8859_15'):
                    try:
                        payload = payload.decode(charset)
                    except:
                        raise
                decoded = True
            except:
                try:
                    payload = part.get_payload()
                    decoded = False
                except:
                    pass
        else:
            try:
                payload = part.get_payload(decode=True).decode(encoding=
                    content_charset, errors=encode_erros)
                decoded = True
            except:
                try:
                    payload = part.get_payload()
                    decoded = False
                except:
                    pass
        part_data.update({'isbody': isbody, 'payload': payload, 'decoded':
            decoded})
    elif content_main_type == 'message' and content_sub_type == 'rfc822':
        subject = part.get('subject')
        isbody = False
        payload = part.get_payload(decode=True)
        decoded = True
        part_data.update({'isbody': isbody, 'payload': payload, 'decoded':
            decoded})
    elif (content_disposition == 'attachment' or content_disposition ==
        'inline') or part.get_filename() != None:
        isbody = False
        payload = part.get_payload(decode=True)
        decoded = True
        part_data.update({'isbody': isbody, 'payload': payload, 'decoded':
            decoded, 'file_name': part.get_filename()})
    return part_data


def addQuotes(s):
    """wrap a strings with quotes"""
    return '"' + s + '"'


def group_objects_by_model(objects):
    """ Group objects by their models

    Args:
        objects (:obj:`list` of :obj:`Model`): list of model objects

    Returns:
        :obj:`dict`: dictionary with object grouped by their class
    """
    grouped_objects = {}
    for obj in objects:
        if obj.__class__ not in grouped_objects:
            grouped_objects[obj.__class__] = []
        if obj not in grouped_objects[obj.__class__]:
            grouped_objects[obj.__class__].append(obj)
    return grouped_objects


def dict_to_labelvalue(d):
    """return label, value of a single entry dictionary.

    If the dictionary contains more than one key/value pair,
    return the complete dictionary.
    """
    if len(d) == 1:
        return list(d.keys())[0], list(d.values())[0]
    else:
        return 'all', d


@staticmethod
def get_db_attributes(tag):
    """
        Given an <a> tag that we've identified as a page link embed (because it has a
        data-linktype="link" attribute), return a dict of the attributes we should
        have on the resulting <a linktype="link"> element.

        :param tag: the tag dictionary.
        :rtype: dict.
        """
    return {'id': tag['data-id']}


def print_csv():
    """ Returns valid CSV example """
    validcsv = """
    Here is an example of some valid csv rows
    Note: A header is not necessary and will be discarded at run time
    Position is fixed!!! Header does not dictate position.
    Field order must be identical to these rows.
    Script also does not permit additional data. Only the fields listed are permitted.
    --------------------------------------------------------------------------------------------------------
    ip,hostname,description,building,location,username,password,devicetype,last_seen
    10.8.1.1,Admin,,admin,datacenter,username,password,hp_procurve,
    10.8.1.13,Admin-POE-1,,admin,datacenter,username,password,hp_procurve,
    10.8.1.16,Admin-1st-Floor-Conf,,admin,1stfloorconference,username,password,hp_procurve,
    10.8.1.17,Admin-DataCenter-03,,admin,datacenter,username,password,hp_procurve,
    10.8.1.18,Lincoln-01,,lincoln,2ndfloorcloset,username,password,hp_procurve,
    10.10.1.1,RIHS,CoreLayer3Aruba,rihs,,username,password,hp_procurve,
    10.10.1.5,BClosetSwitch1,HPProcurve,rihs,,username,password,hp_procurve,

    """
    return validcsv


def get_text_from_response(r):
    """
    Get a text response in a way that also works for very old request versions.

    :type r: :class:`requests.Response
    :param r: The server's response.
    """
    if hasattr(r, 'text'):
        return r.text
    c = r.content
    try:
        c = c.decode()
    except Exception:
        pass
    return c


@classmethod
def seri_kws(cls):
    """Get a list of valid keyword arguments."""
    return list(set(list(cls._schema_properties.keys()) + cls._oldstyle_kws))


def sound_energy(power, axis=-1):
    """
    Sound energy :math:`J`..

    :param power: Sound power :math:`P`.

    .. math:: J = \\int_{t_1}^{t_2} P(t) \\mathrm{d} t
    """
    return power.sum(axis=axis)


def set_conversion(this_converter):
    """
    Set conversion rules in the conversion module.
    :param this_converter: The conversion rules
    :type this_converter: :class:`Converter`
    """
    global converter, py2ri, py2ro, ri2ro, ri2py
    converter = this_converter
    py2ri = converter.py2ri
    py2ro = converter.py2ro
    ri2ro = converter.ri2ro
    ri2py = converter.ri2py


def _dec_filldoc(func):
    """ Fill docstring of slice time function
    """
    func._doc_template = func.__doc__
    func.__doc__ = func.__doc__.format(**dict(name=func.__name__, pstr=
        """Note: slice 0 is the first slice in the voxel data block

    Parameters
    ----------
    n_slices : int
        Number of slices in volume
    TR : float
        Time to acquire one full volume

    Returns
    -------
    slice_times : (n_slices,) ndarray
        Vectors $t_i i = 0 ... N$ of times, one for each slice, where $t_i$
        gives the time at which slice number $i$ was acquired, relative to the
        beginning of the volume acquisition.
    """
        ))
    return func


def get_raw_clusters_lane(total_sample_summary):
    """TODO: Docstring for get_raw_clusters_lane.

    Args:
        total_sample_summary (TODO): TODO

    Returns: TODO

    """
    raw_clusters_lane = dict(zip(total_sample_summary.keys(), [(0) for t in
        range(len(total_sample_summary.keys()))]))
    for lane, sample_summary in total_sample_summary.items():
        for sample, summary in sample_summary.items():
            raw_clusters_lane[lane] += summary['raw_clusters']
    return raw_clusters_lane


def _read_header_lines(lines, iline, line, log):
    """
    reads a tecplot header

    Examples
    --------
    **Example 1**

    TITLE     = "tecplot geometry and solution file"
    VARIABLES = "x"
    "y"
    "z"
    "rho"
    "u"
    "v"
    "w"
    "p"
    ZONE T=""processor 1""
    n=522437, e=1000503, ZONETYPE=FEBrick
    DATAPACKING=BLOCK

    **Example 2**

    title="Force and Momment Data for forces"
    variables="Iteration"
    "C_L","C_D","C_M_x","C_M_y","C_M_z""C_x","C_y","C_z","C_Lp","C_Dp", "C_Lv", "C_Dv""C_M_xp"
    "C_M_yp","C_M_zp","C_M_xv","C_M_yv""C_M_zv","C_xp","C_yp","C_zp","C_xv","C_yv""C_zv
    "Mass flow","<greek>r</greek>","u"
    "p/p0","T","pt/p0"
    "Tt","Mach"
    "Simulation Time"
    zone,t="forces"
    """
    i = 0
    title_line = ''
    active_key = None
    vars_found = []
    header_lines = []
    while i < 30:
        if len(line) == 0 or line[0] == '#':
            line = lines[iline].strip()
            iline += 1
            i += 1
            continue
        if line[0].isdigit() or line[0] == '-':
            log.debug('breaking...')
            break
        uline = line.upper()
        uline2 = uline.replace(' ', '')
        if 'TITLE=' in uline2:
            title_line += line
            vars_found.append('TITLE')
            active_key = 'TITLE'
        elif 'VARIABLES' in uline2:
            vars_found.append('VARIABLES')
            active_key = 'VARIABLES'
        else:
            if 'ZONE' in uline2:
                vars_found.append('ZONE')
                active_key = 'ZONE'
            if 'ZONETYPE' in uline2:
                vars_found.append('ZONETYPE')
                active_key = 'ZONE'
            if 'DATAPACKING' in uline2:
                vars_found.append('DATAPACKING')
                active_key = 'ZONE'
        if active_key in ['ZONE', 'VARIABLES']:
            header_lines.append(line.strip())
        i += 1
        line = lines[iline].strip()
        iline += 1
    log.debug('vars_found = %s' % vars_found)
    return iline, title_line, header_lines, line


def molecular_weight_error(calculated: float, expected: float):
    """
    Calculate the error between a calculated and expected molecular weight. This method may be used as a validation
    tool for calculated isotope patterns.

    :param calculated: calculated molecular weight (derived from an isotope pattern)
    :param expected: expected (true) molecular weight (derived from the molecular weights of the constituent elements)
    :return: Calculated error. Typically a difference of 3 parts per million (3*10^-6) is deemed an acceptable
        error.
    :rtype: float
    """
    return (calculated - expected) / expected


def p_namespace(t):
    """namespace : NAMESPACE ID LBRACE specification RBRACE"""


@classmethod
def get_real_child_model(cls, data):
    """Returns the real base class specified by the discriminator"""
    discriminator_value = data[cls.json_discriminator_key]
    return cls.discriminator_value_class_map.get(discriminator_value)


@staticmethod
def _helper_gstyle_categorymatch(cat_pats, note):
    """Match categories using a Google-style search string"""
    if not cat_pats:
        return 2
    note_category = note.get('category')
    if not note_category:
        return 0
    cat_pats_matched = 0
    for cat_pat in cat_pats:
        cat_pat = cat_pat.lower()
        for pat in note_category:
            if cat_pat in pat.lower():
                cat_pats_matched += 1
                break
    if cat_pats_matched == len(cat_pats):
        return 1
    return 0


def double_linear_con(progress):
    """
    Returns a linear value (x2) with a flattened tail for the Scheduler

    :param progress: (float) Current progress status (in [0, 1])
    :return: (float) 1 - progress*2 if (1 - progress*2) >= 0.125 else 0.125
    """
    progress *= 2
    eps = 0.125
    if 1 - progress < eps:
        return eps
    return 1 - progress


def bool_to_string(b):
    """Convert a boolean type to string.

    Args:
        b (bool): A Boolean.

    Raises:
        TypeError: [description]

    Returns:
        str: String representation of a bool type.
    """
    s = str(b).lower()
    if s in ['true', 'false']:
        return s
    raise TypeError('Value must be True or False.')


def zerotime(acqu):
    """get digital filter parameters, if any
    """
    try:
        zerotimeposition = float(acqu['DIGITAL_FILTER_SHIFT'])
    except:
        zerotimeposition = 0.0
    return zerotimeposition


@classmethod
def add_from_case_control(cls, line, line_upper, lines, i):
    """add method used by the CaseControl class"""
    value = line_upper.split('=')[1]
    return cls(value)


def _is_switchdev(netdev):
    """Returns True if a netdev has a readable phys_switch_id"""
    try:
        sw_id_file = '/sys/class/net/%s/phys_switch_id' % netdev
        with open(sw_id_file, 'r') as fd:
            phys_switch_id = fd.readline().rstrip()
        if phys_switch_id != '' and phys_switch_id is not None:
            return True
    except (OSError, IOError):
        return False
    return False


def security_group_rule_type(neutron_type):
    """Translate neutron rule type.

    Translate to/from openstack - occi

    :param neutron_type: neutron status
    """
    if neutron_type == 'ingress':
        return 'inbound'
    elif neutron_type == 'egress':
        return 'outbound'
    elif neutron_type == 'inbound':
        return 'ingress'
    elif neutron_type == 'outbound':
        return 'egress'
    else:
        return None


def blend_strings_utf8_validate():
    """Check and fix all strings in current .blend file to be valid UTF-8 Unicode (needed for some old, 2.4x area files)

    """
    pass


def iter_lines(filename):
    """ Iterate over screen names in a file, one per line."""
    with open(filename, 'rt') as idfile:
        for line in idfile:
            screen_name = line.strip()
            if len(screen_name) > 0:
                yield screen_name.split()[0]


def pairwise(iterable):
    """! s -> (s0,s1), (s2,s3), (s3, s4), ..."""
    r = []
    for x in iterable:
        r.append(x)
        if len(r) == 2:
            yield tuple(r)
            r = []
    if len(r) > 0:
        yield r[0], r[1]


def is_somatic(var):
    """
    Return whether the variant is somatically acquired
    """
    return var.INFO.get('SOMATIC')


def get_resource_realms():
    """Return resource realms managed by the component.

        :rtype: `basestring` generator
        """


def admin_confirm_sign_up(UserPoolId=None, Username=None):
    """
    Confirms user registration as an admin without using a confirmation code. Works on any user.
    Requires developer credentials.
    See also: AWS API Documentation

    :example: response = client.admin_confirm_sign_up(
        UserPoolId='string',
        Username='string'
    )

    :type UserPoolId: string
    :param UserPoolId: [REQUIRED]
            The user pool ID for which you want to confirm user registration.

    :type Username: string
    :param Username: [REQUIRED]
            The user name for which you want to confirm user registration.

    :rtype: dict
    :return: {}

    """
    pass


def _update_backref(mapping, oldkey, newkey, value):
    """
    Updates references to make oldkey and newkey refer to the same
    type varable.
    """
    oldrefs = mapping.setdefault('backref', {}).setdefault(oldkey, set())
    newrefs = mapping.setdefault('backref', {}).setdefault(newkey, set())
    for oldref in oldrefs:
        mapping[oldref] = value
        newrefs.add(oldref)
        mapping['backref'][oldref] = newrefs
    mapping[oldkey] = value
    newrefs.add(oldkey)
    mapping['backref'][oldkey] = newrefs


def visstd(a, s=0.1):
    """Normalize the image range for visualization"""
    return (a - a.mean()) / max(a.std(), 0.0001) * s + 0.5


def _enable_plugin(env, module):
    """Enable the given plugin module if it wasn't disabled explicitly."""
    if env.is_component_enabled(module) is None:
        env.enable_component(module)


@classmethod
def from_file(cls, path, encoding, dialect, fields, converters, field_index):
    """Read delimited text from a text file."""
    return cls(open(path, 'r', encoding=encoding), dialect, fields,
        converters, field_index)


def list_replace(the_list, find, replacement):
    """Replace all occurrences of ``find`` with ``replacement`` in ``the_list``."""
    for i, v in enumerate(the_list):
        if v == find:
            the_list[i] = replacement


def prettyPercent(numerator, denominator, format='%5.2f'):
    """output a percent value or "na" if not defined"""
    try:
        x = format % (100.0 * numerator / denominator)
    except (ValueError, ZeroDivisionError):
        x = 'na'
    return x


def get_literal_name(v):
    """ Get a human-friendly name for the given literal.

    :param v: Value
    :type v: *
    :rtype: unicode
    """
    return str(v)


def _use_reflect_func(opt):
    """
    Return True if the reflect function is to be used.
    """
    if opt.reflect_out == None or opt.reflect_in == None:
        return True
    elif opt.algorithm == opt.algo_table_driven:
        if opt.reflect_in != opt.reflect_out:
            return True
    elif opt.algorithm == opt.algo_bit_by_bit:
        if opt.reflect_in:
            return True
        if opt.reflect_out:
            return True
    elif opt.algorithm == opt.algo_bit_by_bit_fast:
        if opt.reflect_in:
            return True
        if opt.reflect_out:
            return True
    return False


def escape_str(val: str) ->str:
    """
    Escape all DN special characters found in s
    with a back-slash (see RFC 4514, section 2.4)
    """
    assert isinstance(val, str), TypeError(
        'Expected str (unicode) for val, got %r' % (val,))
    if not val:
        return val
    val = val.replace('\\', '\\\\')
    val = val.replace(',', '\\,')
    val = val.replace('+', '\\+')
    val = val.replace('"', '\\"')
    val = val.replace('<', '\\<')
    val = val.replace('>', '\\>')
    val = val.replace(';', '\\;')
    val = val.replace('=', '\\=')
    val = val.replace('\x00', '\\\x00')
    if val[-1:] == ' ':
        val = ''.join((val[:-1], '\\ '))
    if val[0:1] == '#' or val[0:1] == ' ':
        val = ''.join(('\\', val))
    return val


@classmethod
def client_path(cls):
    """
        there are two cases:
        1. where the client directory is local to this file's directory (when
           we distribute via pip)
        2. where the client directory is in the parent directory (local development)
        """
    production_client_path = cls.production_client_path()
    development_client_path = cls.development_client_path()
    if development_client_path.exists():
        client_path = development_client_path
    else:
        client_path = production_client_path
    return client_path


def _replace_llvm_memset_declaration(m):
    """Replace `llvm.memset` declaration for llvm7+.

    Used as functor for `re.sub.
    """
    params = list(m.group(1).split(','))
    params.insert(-1, 'i32')
    out = ', '.join(params)
    return '({})'.format(out)


def too_much_points_rejected(all_points_number, current_points_number):
    """
    Check whether the percentage ratio of size of two sets is too high.

    Parameters
    ----------
    all_points_number : int
        The set which contains all points.
    current_points_number : int
        The set which is smaller or equal than all_points_number.

    Returns
    -------
    True or False
        If the ratio is too high it returns True. Otherwise it returns False.
    """
    if 1 - current_points_number / all_points_number > 0.05:
        return True
    else:
        return False


def chunkify(l, n):
    """Chunking list in n parts"""
    for i in range(0, len(l), n):
        yield l[i:i + n]


def decode_pure(digits_values, s):
    """Decodes the string s using the digit, value associations for each
    character.

    Parameters
    ----------
    digits_values: dict
        A dictionary containing the base-10 numbers that each hexadecimal
        number corresponds to.
    s: str
        The contents of the pdb index columns.

    Returns
    -------
    The integer in base-10 corresponding to traditional base-36.
    """
    result = 0
    n = len(digits_values)
    for c in s:
        result *= n
        result += digits_values[c]
    return result


def logoff(lm):
    """ Logoff ESGF, returns True if successful """
    lm.logoff()
    return not lm.is_logged_on()


def nonesorter(elem) ->str:
    """Allow NoneType to be sortable.  Used as a key function."""
    if not elem:
        return ''
    return elem


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 bone_siblings(obj, bone):
    """ Returns a list of the siblings of the given bone.
        This requires that the bones has a parent.

    """
    parent = obj.data.bones[bone].parent
    if parent is None:
        return []
    bones = []
    for b in parent.children:
        if b.name != bone:
            bones += [b.name]
    return bones


@classmethod
def isProxyValid(cls, proxy):
    """ checks if proxy is valid

        :param proxy: PyTango proxy
        :type proxy: :class:`PyTango.DeviceProxy`
        :returns: True if proxy is valid else false
        :rtype: :obj:`bool`

        """
    failed = True
    try:
        proxy.ping()
        failed = False
    except Exception:
        failed = True
    return not failed


def convert_to_float(error):
    """
    Convert error from a string to a float if possible.

    :param error: uncertainty from either 'symerror' or 'asymerror'
    :return: error as a float if possible, otherwise the original string
    """
    if isinstance(error, str):
        error = error.replace('%', '')
    try:
        error = float(error)
    except ValueError:
        pass
    return error


def parse_reqtype(args):
    """Setup our Authentication POST.

    username and setup are only used in APIKEY/PASSWORD Authentication
    """
    setup = {'username': args.get('user')}
    if args.get('token') is not None:
        auth_body = {'auth': {'token': {'id': args.get('token')},
            'tenantName': args.get('tenant')}}
    elif args.get('apikey') is not None:
        prefix = 'RAX-KSKEY:apiKeyCredentials'
        setup['apiKey'] = args.get('apikey')
        auth_body = {'auth': {prefix: setup}}
    elif args.get('password') is not None:
        prefix = 'passwordCredentials'
        setup['password'] = args.get('password')
        auth_body = {'auth': {prefix: setup}}
    else:
        raise AttributeError('No Password, APIKey, or Token Specified')
    return auth_body


def twos_comp(val, bits):
    """compute the 2's compliment of int value val"""
    if val & 1 << bits - 1 != 0:
        val = val - (1 << bits)
    return val


@classmethod
def get_key(cls, oid):
    """
        This function creates a key in which NoSQL store will save the instance with given id.

        :param oid: id of object for which a key should be created.
        :returns: NoSQL store key.
        """
    raise NotImplementedError()


def get_pdb_fragments_and_atoms_from_instructions(list_of_instructions):
    """
    Given a list with the instructions processed it returns a list with containing the following elements:
    [fragment_pdb_file, pdb_atom_name_of_the_core (and H if required), pdb_atom_name_of_the_fragment (and H if required)]
    :param list_of_instructions: list with the instructions read from the instructions file. list
    :return: list with all fragments and atoms that will be used in the future. list
    """
    fragments_pdbs_and_atoms = []
    for instructions in list_of_instructions:
        if type(instructions) == list:
            for single_instruction in instructions:
                fragment_pdb, atom_core, atom_fragment = single_instruction[0
                    ], single_instruction[1], single_instruction[2]
                fragments_pdbs_and_atoms.append((fragment_pdb, atom_core,
                    atom_fragment))
        else:
            fragment_pdb, atom_core, atom_fragment = instructions[0
                ], instructions[1], instructions[2]
            fragments_pdbs_and_atoms.append((fragment_pdb, atom_core,
                atom_fragment))
    return fragments_pdbs_and_atoms


@classmethod
def insert(cls, **kwargs):
    """ Insert in the table of the model::

            MyModel.insert(...)

        is equal at::

            mymodel = MyModel(...)
            MyModel.registry.session.add(mymodel)
            MyModel.registry.flush()

        """
    instance = cls(**kwargs)
    cls.registry.add(instance)
    cls.registry.flush()
    return instance


@staticmethod
def addIfValid(sumV, toAdd):
    """
        return the sum of the two given parameters (or None if the case)
        """
    if toAdd is None:
        return sumV
    if sumV is None:
        return toAdd
    return sumV + toAdd


def filter_cannonical(df):
    """Retains only cannonical chromosomes in bed-like df."""
    df = df[df.loc[:, (0)].str.match('chr[0-9|X|Y]+[a|b]?$')]
    return df


def __call__():
    """Returns list of all the children of category."""


def doc_view_manual(doc_id: str=''):
    """Load online manual

    :param doc_id: Doc ID
    :type doc_id: str
    """
    pass


def _graph_encode(graph, coding=None):
    """Encode graph parameters."""
    from hup.base import binary
    if not isinstance(coding, str) or coding.lower() == 'none':
        return graph._graph.copy()
    if coding.lower() == 'base64':
        graph.graph['params'] = binary.pack(graph.graph['params'], encoding
            ='base64')
        for node in graph.nodes():
            graph.node[node]['params'] = binary.pack(graph.node[node][
                'params'], encoding='base64')
        for edge in graph.edges():
            graph.edges[edge]['params'] = binary.pack(graph.edges[edge][
                'params'], encoding='base64')
        graph.graph['coding'] = 'base64'
        return graph
    raise ValueError(
        """could not encode graph parameters:
        unsupported coding '%s'."""
         % coding)


def dms_to_degrees(v):
    """Convert degree/minute/second to decimal degrees."""
    d = float(v[0][0]) / float(v[0][1])
    m = float(v[1][0]) / float(v[1][1])
    s = float(v[2][0]) / float(v[2][1])
    return d + m / 60.0 + s / 3600.0


@classmethod
def _patch(cls, client, auth, endpoint, flavor, compress, secure, timeout,
    args, callback):
    """Implementation of verb PATCH"""
    return cls.__create(endpoint).secure(*cls.__secure(secure)).as_(flavor
        ).auth(client.credentials, method=auth).until(*timeout).compress(
        compress).patch(args, callback=callback)


def multipart(requests):
    """
        Method to collecto multiple requests and
        send it as a batch using **HttpBatch API**.
        """
    pass


def corresponding_directory_perm(perm):
    """Given 4, returns 5. Given 6, returns 7.

    Based on desired file permissions, returns corresponding dir permissions.
    """
    perm = int(perm)
    return perm + 1 if perm % 2 == 0 and perm > 3 else perm


def set_finish_callback(callback):
    """Subscribe a ``callback`` function, to be called when this response is
        finished."""


@staticmethod
def get_custom_param(plot):
    """ Return the custom parameters list which is the parameters list used for special types of plots

        Args:
            plot:

        Returns:

        """
    return plot.custom_parameters


def get_translation(obj, language_code):
    """
    This is an adapter from django-hvad.utils.get_translation(), a function
    to django-parler.models.get_translation() (a model instance method).
    """
    if not obj or not hasattr(obj, 'get_translation'):
        return None
    return obj.get_translation(language_code)


def volume_tetrahedron(v1: 'mathutils.Vector', v2: 'mathutils.Vector', v3:
    'mathutils.Vector', v4: 'mathutils.Vector') ->float:
    """Return the volume formed by a tetrahedron (points can be in any order).

    :param v1: Point1
    :type v1: 'mathutils.Vector'
    :param v2: Point2
    :type v2: 'mathutils.Vector'
    :param v3: Point3
    :type v3: 'mathutils.Vector'
    :param v4: Point4
    :type v4: 'mathutils.Vector'
    """
    pass


def argtopk(input, k, dim, descending=True):
    """Return the indices of the k largest elements of the given input tensor
    along the given dimension.

    If descending is False then the k smallest elements are returned.

    Parameters
    ----------
    input : Tensor
        The input tensor.
    k : int
        The number of elements.
    dim : int
        The dimension to sort along.
    descending : bool
        Controls whether to return largest/smallest elements.
    """
    pass


def convert_py_namespace_to_header_filename(python_namespace):
    """Convert a Python namespace to the name of the root C++ header file.

    Parameters
    ----------
    python_namespace : `str`
        A string describing a Python namespace. For example,
        ``'lsst.example'``.

    Returns
    -------
    header_filename : `str`
        Filename of the root header file.
    """
    parts = python_namespace.split('.')
    return parts[-1] + '.h'


def get_resolution(bbox, size):
    """
    Calculate the highest resolution needed to draw the bbox
    into an image with given size.

    >>> get_resolution((-180,-90,180,90), (256, 256))
    0.703125

    :returns: the resolution
    :rtype: float
    """
    w = abs(bbox[0] - bbox[2])
    h = abs(bbox[1] - bbox[3])
    return min(w / size[0], h / size[1])


def python_file_run(filepath: str=''):
    """Run Python file

    :param filepath: Path
    :type filepath: str
    """
    pass


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


@classmethod
def render_final(cls, deposition):
    """Render page when deposition was *already* successfully completed.

        (i.e a finished workflow is being executed a second time).

        This allows you render e.g. a preview of the record. The distinction
        between render_completed and render_final is primarily useful for the
        REST API (see api_final and api_completed)

        Method can be overwritten by subclasses to provide custom
        user interface.
        """
    return cls.render_completed(deposition)


def get_evac():
    """Get mean vacuum energy, if it has been calculated"""
    from pathlib import Path
    from asr.core import read_json
    evac = None
    if Path('results-asr.gs.json').is_file():
        results = read_json('results-asr.gs.json')
        if 'vacuumlevels' in results.keys():
            evac = results['vacuumlevels']['evacmean']
    return evac


def remove(obj):
    """Remove an object from PostGreSQL.

        The correct collection is determined by object type.
        """


def view_frame():
    """Reset viewable area to show range around current frame

    """
    pass


def ballot_two(ranking):
    """Ballot for the voter's second most-liked candidate.

    Parameters
    ----------
    ranking : str
        A ranking.

    Returns
    -------
    str
        The second candidate.

    Examples
    --------
        >>> ballot_two('abc')
        'b'
    """
    return ranking[1]


def _powerString(s, npow, powchar='**', trailchar=''):
    """Raise string s to power npow with additional formatting."""
    if npow == 0:
        return ''
    elif npow == 1:
        return s + trailchar
    else:
        return s + powchar + str(npow) + trailchar


def get_weeks_between(d1, d2):
    """
    Calculating the difference in weeks between the Mondays within weeks of respective dates

    :param d1: day one, the start date
    :param d2: day two, the end date
    :return: Returns 0 if both dates fall withing one week, 1 if on two consecutive weeks, etc.
    """
    from datetime import timedelta
    if d1 is None or d2 is None:
        return 1
    monday1 = d1 - timedelta(days=d1.weekday())
    monday2 = d2 - timedelta(days=d2.weekday())
    return (monday2 - monday1).days / 7


def p_header_unit(p):
    """header_unit : include
                   | cpp_include
                   | namespace"""


@staticmethod
def lock_visibility_attribute(node):
    """
        Locks visibility attribute of the given node
        :param node: str
        """
    raise NotImplementedError(
        'abstract DCC function lock_visibility_attribute() not implemented!')


def setBroadcastAllowed(enabled):
    """
        Set whether this port may broadcast.

        @param enabled: Whether the port may broadcast.
        @type enabled: L{bool}
        """


def parse_method_path(method_path):
    """ Returns (package, service, method) tuple from parsing method path """
    package_service, method_name = method_path.lstrip('/').rsplit('/', 1)
    package_service = package_service.rsplit('.', 1)
    if len(package_service) == 2:
        return package_service[0], package_service[1], method_name
    return None, package_service[0], method_name


@classmethod
def from_gnaf_dict(cls, **kwags):
    """Create an AbAddressUtility class from a dict containing GNAF
        information.

        :params **kwags: a dict from GNAF format.
        """
    a = {'flat_number_prefix': kwags.get('flat_number_prefix', None),
        'flat_number': kwags.get('flat_number', None), 'flat_number_suffix':
        kwags.get('flat_number_suffix', None), 'number_first_prefix': kwags
        .get('number_first_prefix', None), 'number_first': kwags.get(
        'number_first', None), 'number_first_suffix': kwags.get(
        'number_first_suffix', None), 'number_last_prefix': kwags.get(
        'number_last_prefix', None), 'number_last': kwags.get('number_last',
        None), 'number_last_suffix': kwags.get('number_last_suffix', None),
        'street_name': kwags.get('street_name', None), 'street_type_code':
        kwags.get('street_type_code', None), 'street_suffix_code': kwags.
        get('street_suffix_code', None), 'locality_name': kwags.get(
        'locality_name', None), 'state_abbreviation': kwags.get(
        'state_abbreviation', None), 'postcode': kwags.get('postcode', None)}
    for k, v in a.items():
        if v is None:
            a[k] = ''
    _flat = (
        f"{a['flat_number_prefix']}{a['flat_number']}{a['flat_number_suffix']}"
        )
    _number_first = (
        f"{a['number_first_prefix']}{a['number_first']}{a['number_first_suffix']}"
        )
    _number_last = (
        f"{a['number_last_prefix']}{a['number_last']}{a['number_last_suffix']}"
        )
    _street = (
        f"{a['street_name']} {a['street_type_code']}{' ' + a['street_suffix_code']}"
        )
    address = (
        f"{_flat + '/' if len(_flat) > 0 else ''}{_number_first}{'-' + _number_last if len(_number_last) > 0 else ''} {_street.title()}, {a['locality_name'].title()} {a['state_abbreviation']} {a['postcode']}"
        )
    return cls(address)


def add_common_arguments(parser):
    """Defines a set of common arguments that are shared between nanviewer and nanslicer"""
    parser.add_argument('base_image', help='Base (structural image)', type=str)
    parser.add_argument('--mask', type=str, help='Mask image')
    parser.add_argument('--base_map', type=str, default='gist_gray', help=
        'Base image colormap to use from Matplotlib, default = gist_gray')
    parser.add_argument('--base_lims', type=float, nargs=2, default=None,
        help='Specify base image window')
    parser.add_argument('--base_lims_p', type=float, nargs=2, default=None,
        help='Specify base image window in %')
    parser.add_argument('--base_scale', type=float, default=1.0, help=
        'Scaling for base image before mapping, default=1.0')
    parser.add_argument('--base_label', type=str, default='', help=
        'Label for base color axis')
    parser.add_argument('--overlay', type=str, help='Add color overlay')
    parser.add_argument('--overlay_map', type=str, default='RdYlBu_r', help
        ='Overlay colormap, default = RdYlBu_r')
    parser.add_argument('--overlay_lim', type=float, nargs=2, default=(-1,
        1), help='Overlay window, default=-1 1')
    parser.add_argument('--overlay_mask', type=str, help='Mask color image')
    parser.add_argument('--overlay_mask_thresh', type=float, help=
        'Overlay mask threshold')
    parser.add_argument('--overlay_scale', type=float, default=1.0, help=
        'Scaling for overlay image before mapping, default=1.0')
    parser.add_argument('--overlay_label', type=str, default='', help=
        'Label for overlay color axis')
    parser.add_argument('--overlay_alpha', type=str, help=
        'Image for transparency-coding of overlay')
    parser.add_argument('--overlay_alpha_lim', type=float, nargs=2, default
        =(0.5, 1.0), help='Overlay Alpha/transparency window, default=0.5 1.0')
    parser.add_argument('--overlay_alpha_scale', type=float, default=1.0,
        help='Scaling factor for the alpha image')
    parser.add_argument('--overlay_alpha_label', type=str, default='1-p',
        help='Label for overlay alpha/transparency axis')
    parser.add_argument('--contour', type=float, action='append', help=
        'Add alpha image contours (can be multiple)')
    parser.add_argument('--contour_color', type=str, action='append',
        default='k', help='Choose contour colors')
    parser.add_argument('--contour_style', type=str, action='append',
        default='-', help='Choose contor line-styles')
    parser.add_argument('--samples', type=int, default=128, help=
        'Number of samples for slicing, default=128')
    parser.add_argument('--interp', type=str, default='hanning', help=
        'Display interpolation mode, default=hanning')
    parser.add_argument('--interp_order', type=int, default=1, help=
        'Data interpolation order, default=1')
    parser.add_argument('--orient', type=str, default='clin', help=
        'Clinical (clin) or Pre-clinical (preclin) orientation')
    return parser


def add_workflow_settings_cli(parser, include_subdax_opts=False):
    """Adds workflow options to an argument parser.

    Parameters
    ----------
    parser : argparse.ArgumentParser
        Argument parser to add the options to.
    include_subdax_opts : bool, optional
        If True, will add output-map, transformation-catalog, and dax-file
        options to the parser. These can be used for workflows that are
        generated as a subdax of another workflow. Default is False.
    """
    wfgrp = parser.add_argument_group('Options for setting workflow files')
    wfgrp.add_argument('--workflow-name', required=True, help=
        'Name of the workflow.')
    wfgrp.add_argument('--tags', nargs='+', default=[], help=
        'Append the given tags to file names.')
    wfgrp.add_argument('--output-dir', default=None, help=
        'Path to directory where the workflow will be written. Default is to use {workflow-name}_output.'
        )
    if include_subdax_opts:
        wfgrp.add_argument('--output-map', default='output.map', help=
            'Path to an output map file. Default is output.map.')
        wfgrp.add_argument('--transformation-catalog', default=None, help=
            'Path to transformation catalog file.')
        wfgrp.add_argument('--dax-file', default=None, help=
            'Path to DAX file. Default is to write to the output directory with name {workflow-name}.dax.'
            )


def capitalize(s: str) ->str:
    """Capitalize the first character of a string."""
    if s == '':
        return ''
    else:
        return s[0].upper() + s[1:]


def active_frames_delete_all():
    """Delete the active frame(s) of all editable Grease Pencil layers

    """
    pass


def write_colspecs(writer):
    """

    Copied from docutils.writers.html4css1.HTMLTranslator,
    except the parameter to writer.body.append function.
    """
    width = 0
    for node in writer.colspecs:
        width += node['colwidth']
    for node in writer.colspecs:
        colwidth = int(node['colwidth'] * 100.0 / width + 0.5)
        writer.body.append('<col class="width-%d%%" />\n' % colwidth)
    writer.colspecs = []


def isdmap(dmethod):
    """Return ``isdmap`` argument for ``.IterStatsConfig`` initialiser.
    """
    if dmethod == 'fista':
        isd = {'Cnstr': 'Cnstr', 'D_F_Btrack': 'F_Btrack', 'D_Q_Btrack':
            'Q_Btrack', 'D_ItBt': 'IterBTrack', 'D_L': 'L', 'D_Rsdl': 'Rsdl'}
    else:
        isd = {'Cnstr': 'Cnstr', 'DPrRsdl': 'PrimalRsdl', 'DDlRsdl':
            'DualRsdl', 'DRho': 'Rho'}
    return isd


def name_getter(dict):
    """Get list of names from a dictionary. Used to get the objective names for EHProblem."""
    namelist = []
    for item in dict:
        name = ''
        for part in item:
            if name == '':
                name = name + part
            else:
                name = name + ':' + part
        namelist.append(name)
    return namelist


def summary(task):
    """Given an ImportTask, produce a short string identifying the
    object.
    """
    if task.is_album:
        return u'{0} - {1}'.format(task.cur_artist, task.cur_album)
    else:
        return u'{0} - {1}'.format(task.item.artist, task.item.title)


def delete_user(UserName=None):
    """
    Deletes the specified IAM user. The user must not belong to any groups or have any access keys, signing certificates, or attached policies.
    See also: AWS API Documentation

    Examples
    The following command removes the IAM user named Bob from the current account.
    Expected Output:

    :example: response = client.delete_user(
        UserName='string'
    )

    :type UserName: string
    :param UserName: [REQUIRED]
            The name of the user to delete.
            This parameter allows (per its regex pattern ) a string of characters consisting of upper and lowercase alphanumeric characters with no spaces. You can also include any of the following characters: =,.@-

    :return: response = client.delete_user(
        UserName='Bob',
    )

    print(response)

    """
    pass


def increment_basis(order_current, interaction_order_current,
    interaction_order_max, incr):
    """
    Increments basis

    Parameters
    ----------
    order_current: int
        Maximum global expansion order.
        The maximum expansion order considers the sum of the orders of combined polynomials together with the
        chosen norm "order_max_norm". Typically this norm is 1 such that the maximum order is the sum of all
        monomial orders.
    interaction_order_current : int
        Current number of random variables, which can interact with each other
        All polynomials are ignored, which have an interaction order greater than specified
    interaction_order_max : int
        Maximum number of random variables, which can interact with each other
        All polynomials are ignored, which have an interaction order greater than specified
    incr : int
        Number of sub-iteration increments

    Returns
    -------
    order : int
        Updated order
    interaction_order : int
        Updated interaction order
    """
    while incr > 0:
        if interaction_order_current + 1 <= min(order_current,
            interaction_order_max):
            interaction_order_current += 1
        else:
            order_current += 1
            interaction_order_current = 1
        incr -= 1
    return order_current, interaction_order_current


@classmethod
def datetimeformat(cls, x, x_format, y_format='%y-%m-%d h:m:s'):
    """
        datetimeformat method takes three input of string type
        input1: datetime value
        input2: datetimeformat of input1
        input3: output datetime format
        output: datetime value is converted into user defined datetime value with format done on input1 using input 3
        """
    x_date, x_time = cls.get_itemformat_value(x, ' ')
    x_format_date, x_format_time = cls.get_itemformat_value(x_format, ' ')
    y_format_date, y_format_time = cls.get_itemformat_value(y_format, ' ')
    output_date = cls.dateformat(x_date, x_format_date, y_format_date)
    output_time = cls.timeformat(x_time, x_format_time, y_format_time)
    output = [output_date, output_time]
    output_datetime = ' '.join(output)
    return output_datetime


def applyMutinfoNorm(mutinfo, entropy, norm='sument'):
    """Apply one of the normalizations discussed in [MLC05]_ to *mutinfo*
    matrix.  *norm* can be one of the following:

      * ``'sument'``: :math:`H(X) + H(Y)`, sum of entropy of columns
      * ``'minent'``: :math:`min\\{H(X), H(Y)\\}`, minimum entropy
      * ``'maxent'``: :math:`max\\{H(X), H(Y)\\}`, maximum entropy
      * ``'mincon'``: :math:`min\\{H(X|Y), H(Y|X)\\}`, minimum conditional
         entropy
      * ``'maxcon'``: :math:`max\\{H(X|Y), H(Y|X)\\}`, maximum conditional
         entropy

    where :math:`H(X)` is the entropy of a column, and
    :math:`H(X|Y) = H(X) - MI(X, Y)`.  Normalization with joint entropy, i.e.
    :math:`H(X, Y)`, can be done using :func:`.buildMutinfoMatrix` *norm*
    argument.

    .. [MLC05] Martin LC, Gloor GB, Dunn SD, Wahl LM. Using information theory
       to search for co-evolving residues in proteins. *Bioinformatics*
       **2005** 21(22):4116-4124."""
    try:
        ndim, shape = mutinfo.ndim, mutinfo.shape
    except AttributeError:
        raise TypeError('mutinfo must be a 2D square array')
    if ndim != 2 or shape[0] != shape[1]:
        raise ValueError('mutinfo must be a 2D square array')
    try:
        ndim, shapent = entropy.ndim, entropy.shape
    except AttributeError:
        raise TypeError('entropy must be a numpy array')
    if ndim != 1:
        raise ValueError('entropy must be a 1D array')
    if shapent[0] != shape[0]:
        raise ValueError('shape of mutinfo and entropy does not match')
    try:
        sw = norm.startswith
    except AttributeError:
        raise TypeError('norm must be a string')
    if sw('sument'):
        norm = lambda i_val, j_val, val: i_val + j_val
    elif sw('minent'):
        norm = lambda i_val, j_val, val: min(i_val, j_val)
    elif sw('maxent'):
        norm = lambda i_val, j_val, val: max(i_val, j_val)
    elif sw('mincon'):
        norm = lambda i_val, j_val, val: min(i_val - val, j_val - val)
    elif sw('maxcon'):
        norm = lambda i_val, j_val, val: max(i_val - val, j_val - val)
    elif sw('joint'):
        raise ValueError(
            'for joint entropy normalization, use buildMutinfoMatrix function')
    else:
        raise ValueError('norm={0} is not a valid normalization type'.
            format(norm))
    mi = mutinfo.copy()
    for i, i_val in enumerate(entropy):
        for j, j_val in enumerate(entropy):
            val = mi[i, j]
            div = norm(i_val, j_val, val)
            if div == 0:
                mi[i, j] = 0
            else:
                mi[i, j] /= div
    return mi


def _get_local_timezone():
    """ Get local timezone using pytz with environment variable, or dateutil.

    If there is a 'TZ' environment variable, pass it to pandas to use pytz and use it as timezone
    string, otherwise use the special word 'dateutil/:' which means that pandas uses dateutil and
    it reads system configuration to know the system local timezone.

    See also:
    - https://github.com/pandas-dev/pandas/blob/0.19.x/pandas/tslib.pyx#L1753
    - https://github.com/dateutil/dateutil/blob/2.6.1/dateutil/tz/tz.py#L1338
    """
    import os
    return os.environ.get('TZ', 'dateutil/:')


def load_marker(i):
    """
    Provide marker via index
    @param i: index
    """
    markers = ['o', 'v', 'D', 's', '*', 'd', '^', 'x', '+']
    return markers[i % len(markers)]


def std_soft_prediction(y_true, y_score):
    """Compute the standard deviation of the predicted
    probabilities. This helps us identify if the model is
    always predicting roughly the same probability.
    """
    return y_score.std()


def keyframe_clear_v3d():
    """Remove all keyframe animation for selected objects

    """
    pass


@staticmethod
def get_cost():
    """Return cost of stage."""
    raise NotImplementedError


def WriteParamFile(paramfile, jspath, filetype, reference_track, samples,
    alignments_by_sample, fromindex, toindex):
    """
    Generate paramfile for creating snapshots from the command line
    """
    f = open(paramfile, 'w')
    f.write('var exports = module.exports = {};\n')
    f.write('exports.reference_track = "%s";\n' % reference_track)
    f.write('exports.samples = %s;\n' % str(samples))
    f.write('exports.alignBySample = {\n')
    for sample in alignments_by_sample:
        f.write('"%s": "%s",\n' % (sample, alignments_by_sample[sample]))
    f.write('};\n')
    f.write('exports.fromindex = %s;\n' % fromindex)
    f.write('exports.toindex = %s;\n' % toindex)
    f.write('exports.jspath = "%s";\n' % jspath)
    if filetype in ['html', 'svg']:
        f.write('exports.filetype = "%s";\n' % filetype)
    elif filetype == 'pdf':
        f.write('exports.filetype = "svg";\n')
    else:
        f.write('exports.filetype = "none";\n')
    f.close()


def clamp(v, minv, maxv):
    """clamps a value within a range"""
    if v < minv:
        v = minv
    if v > maxv:
        v = maxv
    return v


def is_authenticated(user):
    """Return whether or not a User is authenticated.

    Function provides compatibility following deprecation of method call to
    `is_authenticated()` in Django 2.0.

    This is *only* required to support Django < v1.10 (i.e. v1.9 and earlier),
    as `is_authenticated` was introduced as a property in v1.10.s
    """
    if not hasattr(user, 'is_authenticated'):
        return False
    if callable(user.is_authenticated):
        return user.is_authenticated()
    else:
        return user.is_authenticated


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

        :param FolderOverviewDescriptionChangedType val:
        :rtype: EventType
        """
    return cls('folder_overview_description_changed', val)


def one_r2_ic(y, preds):
    """
    Compute the IC for one R-squared

    @param y: the outcome
    @param preds: the predictions based on a given subset of features

    @return the IC for R-squared
    """
    import sklearn.metrics as skm
    import numpy as np
    mse = skm.mean_squared_error(y_true=y, y_pred=preds)
    var = np.mean((y - np.mean(y)) ** 2)
    ic_mse = (y.reshape(preds.shape) - preds) ** 2 - mse
    ic_var = (y - np.mean(y)) ** 2 - var
    grad = np.array([1.0 / var, -1 * mse / var ** 2])
    return np.dot(grad, np.transpose(np.hstack(ic_mse, ic_var)))


def fraction(start1, start2, end1, end2, read1, read2):
    """Function that performs a first round of merging. If the realigned intervals and SA intervals overlap, and are ca-
    lled within the same iteration (which means that it is the same circle probably) they will be merged"""
    read_match = (read1 == read2) * 1
    distance = abs(start1 - start2) + abs(end1 - end2)
    one_overlap_two = 1 - distance / (end1 - start1)
    two_overlap_one = 1 - distance / (end2 - start2)
    return one_overlap_two + two_overlap_one + read_match


def describe_objects(pipelineId=None, objectIds=None, evaluateExpressions=
    None, marker=None):
    """
    Gets the object definitions for a set of objects associated with the pipeline. Object definitions are composed of a set of fields that define the properties of the object.
    See also: AWS API Documentation

    :example: response = client.describe_objects(
        pipelineId='string',
        objectIds=[
            'string',
        ],
        evaluateExpressions=True|False,
        marker='string'
    )

    :type pipelineId: string
    :param pipelineId: [REQUIRED]
            The ID of the pipeline that contains the object definitions.

    :type objectIds: list
    :param objectIds: [REQUIRED]
            The IDs of the pipeline objects that contain the definitions to be described. You can pass as many as 25 identifiers in a single call to DescribeObjects .
            (string) --

    :type evaluateExpressions: boolean
    :param evaluateExpressions: Indicates whether any expressions in the object should be evaluated when the object descriptions are returned.

    :type marker: string
    :param marker: The starting point for the results to be returned. For the first call, this value should be empty. As long as there are more results, continue to call DescribeObjects with the marker value from the previous call to retrieve the next set of results.

    :rtype: dict
    :return: {
        'pipelineObjects': [
            {
                'id': 'string',
                'name': 'string',
                'fields': [
                    {
                        'key': 'string',
                        'stringValue': 'string',
                        'refValue': 'string'
                    },
                ]
            },
        ],
        'marker': 'string',
        'hasMoreResults': True|False
    }

    """
    pass


def find_e_int(path, csvfile):
    """Gets the dispersion component of the interaction energy (per ion pair) for each configuration, each key of the groups dictionary. This value, when temperature corrected, is the enthalpy of interaction."""
    disp_contribution = 0.0
    elec = 0.0
    if path[0] == '.':
        config = path[2:]
    else:
        config = path
    with open(csvfile, 'r') as f:
        for line in f.readlines()[1:]:
            splitup = line.split(',')
            if splitup[0] == config:
                if len(splitup) == 15 or len(splitup) == 16:
                    disp_contribution = float(splitup[6])
                    elec = float(splitup[4])
                else:
                    disp_contribution = 0.0
                    elec = float(splitup[8])
    return disp_contribution, elec


def write_change(xmltree, changelist_xpath):
    """
    applies the changes from the changelist to the xml tree
    """
    xmltree_new = xmltree
    for element in changelist_xpath:
        xpath = element[0]
        value = element[1]
    return xmltree_new


def min(raw_metric):
    """
    .. note:: Deprecated use `longMin`, `doubleMin' instead
    """
    return {'type': 'min', 'fieldName': raw_metric}


def get_indices(Id, N0, N1=None, N2=None):
    """Gets 3D indices from 1D index. Assumes row-major ordering.

    Parameters
    ----------
    Id : int
        1D index
    N0 : int
        size of the array x
    N1 : int, optional, default=N0
        size of the array y
    N2 : int, optional, default=N0
        size of the array z

    Returns
    (i,j,k) : array, int, dimension=3
        corresponding 3D indices

    """
    N1 = N1 or N0
    N2 = N2 or N0
    i = int(Id / (N1 * N2) % N0)
    j = int((Id - N1 * N2 * i) / N2 % N1)
    k = int((Id - N2 * j - N2 * N1 * i) % N2)
    return i, j, k


@property
def name(cls):
    """The name property is a function of the class - its __name__."""
    return cls.__name__


def dict_from_cookiejar(cj):
    """Returns a key/value dictionary from a CookieJar.

    :param cj: CookieJar object to extract cookies from.
    :rtype: dict
    """
    cookie_dict = {}
    for cookie in cj:
        cookie_dict[cookie.name] = cookie.value
    return cookie_dict


def _reshape_to_one_step(raw_mat, num_steps):
    """
    Reshapes provided data from (pos, step * bin) to (pos * step, bin).
    This is useful when unraveling data for parallel processing.

    Parameters
    -------------
    raw_mat : 2D numpy array
        Data organized as (positions, step * bins)
    num_steps : unsigned int
        Number of spectroscopic steps per pixel (eg - UDVS steps)

    Returns
    --------------
    two_d : 2D numpy array
        Data rearranged as (positions * step, bin)
    """
    num_pos = raw_mat.shape[0]
    num_bins = int(raw_mat.shape[1] / num_steps)
    one_d = raw_mat
    one_d = one_d.reshape(num_bins * num_steps * num_pos)
    two_d = one_d.reshape((num_steps * num_pos, num_bins))
    return two_d


def _split_geo_name(v):
    """split name of form first,middle,last into dict"""
    return dict(zip('first middle last'.split(), v.split(',')))


@staticmethod
def name_resource_dir(name):
    """
        Give the directory name for supporting resources
        """
    return '{}_files'.format(name)


def pad_to(x, k=8):
    """Pad int value up to divisor of k.

    Examples:
        >>> pad_to(31, 8)
        32

    """
    return x + (x % k > 0) * (k - x % k)


def slugify(s, entities=True, decimal=True, hexadecimal=True):
    """Convert a string to a slug representation.

    Normalizes string, converts to lower-case, removes non-alpha characters,
    and converts spaces to hyphens.

    Parts from http://www.djangosnippets.org/snippets/369/

        >>> slugify("Manuel Vázquez Acosta")    # doctest: +SKIP
        'manuel-vazquez-acosta'

    If `s` and `entities` is True (the default) all HTML entities
    are replaced by its equivalent character before normalization::

        >>> slugify("Manuel Vázquez Acosta")   # doctest: +SKIP
        'manuel-vazquez-acosta'

    If `entities` is False, then no HTML-entities substitution is made::

        >>> value = "Manuel Vázquez Acosta"
        >>> slugify(value, entities=False)  # doctest: +SKIP
        'manuel-v-aacute-zquez-acosta'

    If `decimal` is True, then all entities of the form ``&#nnnn`` where
    `nnnn` is a decimal number deemed as a unicode codepoint, are replaced by
    the corresponding unicode character::

        >>> slugify('Manuel Vázquez Acosta')  # doctest: +SKIP
        'manuel-vazquez-acosta'

        >>> value = 'Manuel Vázquez Acosta'
        >>> slugify(value, decimal=False)  # doctest: +SKIP
        'manuel-v-225-zquez-acosta'

    If `hexadecimal` is True, then all entities of the form ``&#nnnn`` where
    `nnnn` is a hexdecimal number deemed as a unicode codepoint, are replaced
    by the corresponding unicode character::

        >>> slugify('Manuel Vázquez Acosta')  # doctest: +SKIP
        'manuel-vazquez-acosta'

        >>> slugify('Manuel Vázquez Acosta', hexadecimal=False)  # doctest: +SKIP  # noqa
        'manuel-v-x00e1-zquez-acosta'

    .. deprecated:: 2.1.0 Use `xotl.tools.strings.slugify`:func:.

    """
    import re
    from xotl.tools.string import slugify
    from xotl.tools.future.codecs import safe_decode
    if not isinstance(s, str):
        s = safe_decode(s)
    if entities:
        try:
            from htmlentitydefs import name2codepoint
        except ImportError:
            from html.entities import name2codepoint
        s = re.sub(str('&(%s);') % str('|').join(name2codepoint), lambda m:
            chr(name2codepoint[m.group(1)]), s)
    if decimal:
        try:
            s = re.sub('&#(\\d+);', lambda m: chr(int(m.group(1))), s)
        except Exception:
            pass
    if hexadecimal:
        try:
            s = re.sub('&#x([\\da-fA-F]+);', lambda m: chr(int(m.group(1),
                16)), s)
        except Exception:
            pass
    return slugify(s, '-')


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


@classmethod
def alg_type_by_name(cls, alg_name):
    """ Algorithms type by name
        """
    for alg in cls:
        if alg.name == alg_name:
            return alg
    return None


def eventstr(event_tuple=None, event=None, register=None, parameters=None):
    """
    Return a LIKWID event string from an event tuple or keyword arguments.

    *event_tuple* may have two or three arguments: (event, register) or
    (event, register, parameters)

    Keyword arguments will be overwritten by *event_tuple*.

    >>> eventstr(('L1D_RE'+'PLACEMENT', 'PMC0', None))
    'L1D_RE'+'PLACEMENT:PMC0'
    >>> eventstr(('L1D_RE'+'PLACEMENT', 'PMC0'))
    'L1D_RE'+'PLACEMENT:PMC0'
    >>> eventstr(('MEM_UOPS_RETIRED_LOADS', 'PMC3', {'EDGEDETECT': None, 'THRESHOLD': 2342}))
    'MEM_UOPS_RETIRED_LOADS:PMC3:EDGEDETECT:THRESHOLD=0x926'
    >>> eventstr(event='DTLB_LOAD_MISSES_WALK_DURATION', register='PMC3')
    'DTLB_LOAD_MISSES_WALK_DURATION:PMC3'
    """
    if len(event_tuple) == 3:
        event, register, parameters = event_tuple
    elif len(event_tuple) == 2:
        event, register = event_tuple
    event_dscr = [event, register]
    if parameters:
        for k, v in sorted(event_tuple[2].items()):
            if type(v) is int:
                k += '={}'.format(hex(v))
            event_dscr.append(k)
    return ':'.join(event_dscr)


def get_attribute_values(log, attribute_key, parameters=None):
    """
    Get the attribute values of the log for the specified attribute along with their count

    Parameters
    ----------
    log
        Log
    attribute_key
        Attribute for which we would like to know the values along with their count
    parameters
        Possible parameters of the algorithm

    Returns
    ----------
    attributes
        Dictionary of attributes associated with their count
    """
    if parameters is None:
        parameters = {}
    attributes = {}
    for trace in log:
        for event in trace:
            if attribute_key in event:
                attribute = event[attribute_key]
                if attribute not in attributes:
                    attributes[attribute] = 0
                attributes[attribute] = attributes[attribute] + 1
    return attributes


@staticmethod
def oracle_all_sequences(_dal):
    """
        Oracle specific: List all sequences in an Oracle database

        :param _dal: A connected instance of DatabaseAbstractionLayer (see qal.dal)
        :return: A list or sequence names
        """
    if not _dal.connected:
        _dal.connect_to_db()
    rows = _dal.query('SELECT SEQUENCE_NAME FROM USER_SEQUENCES')
    sequences = list()
    for row in rows:
        sequences.append(row[0])
    return sequences


def unapprove_version():
    """Unapproved an approved but not yet published version.
        """


@staticmethod
def copy(tensor):
    """Return a copy of the given tensor"""
    raise NotImplementedError


def p_comparison(p):
    """comparison : a_expr
                   | a_expr comp_operator a_expr"""
    if len(p) == 2:
        p[0] = p[1]
    else:
        p[0] = ['MATHOP', p[2], p[1], p[3]]


@classmethod
def add_attributes(cls, **attrs):
    """Add more attributes

        The arguments should be valid Python attribute names
        associated with a type for the new attribute.

        """
    for n, t in attrs.items():
        setattr(cls, n, t)
    cls.__registry__.reregister(cls)


def _get_series_start_end_times(series):
    """Extracts the start and end times of segements defined by a boolean
    pandas Series.

    Parameters
    ----------
    series : pandas boolean Series (n_time,)
        Consecutive Trues define each segement.

    Returns
    -------
    start_times : ndarray, shape (n_segments,)
        Beginning time of each segment based on the index of the series.
    end_times : ndarray, shape (n_segments,)
        End time of each segment based on the index of the series.

    """
    is_start_time = ~series.shift(1).fillna(False) & series
    start_times = series.index[is_start_time].get_values()
    is_end_time = series & ~series.shift(-1).fillna(False)
    end_times = series.index[is_end_time].get_values()
    return start_times, end_times


def summarize_missing_df(dataframe):
    """
    Populate the fraction missing counter from a given DataFrame

    """
    results = {'frac_missing': float((dataframe != 0.0).sum().sum()) /
        float(dataframe.shape[0] * dataframe.shape[1])}
    return results


def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the Nest thermostat.

    No longer in use.
    """


@classmethod
def _build_model_check_base(model_class, cls):
    """ Check whether ``model_class`` can be extended with ``cls``. """
    if model_class._abstract and not cls._abstract:
        msg = (
            "%s transforms the abstract model %r into a non-abstract model. That class should either inherit from AbstractModel, or set a different '_name'."
            )
        raise TypeError(msg % (cls, model_class._name))
    if model_class._transient != cls._transient:
        if model_class._transient:
            msg = (
                "%s transforms the transient model %r into a non-transient model. That class should either inherit from TransientModel, or set a different '_name'."
                )
        else:
            msg = (
                "%s transforms the model %r into a transient model. That class should either inherit from Model, or set a different '_name'."
                )
        raise TypeError(msg % (cls, model_class._name))


@staticmethod
def p_statement_expr(p):
    """statement : expression"""
    p[0] = p[1]


def freestyle_lineset_paste():
    """Paste the buffer content to the active line set

    """
    pass


def to_kb(bytes):
    """to kilobytes, not kibibytes"""
    k = float(bytes) / 1000
    return '{:.0f} KB'.format(k)


def param_invalid_value_info(param_name, default_value):
    """
    Returns info warning an invalid parameter configuration.
    """
    return ('Parameter warning: the configuration of hyper-parameter ' +
        "'{}' is not valid, will use default value '{}'".format(param_name,
        default_value))


@classmethod
def is_exist(cls, key):
    """ Check if one parameter exist for the key

        :param key: key to check
        :rtype: bool
        """
    query = cls.query().filter(cls.key == key)
    return True if query.count() else False


def getNtColours():
    """
    Generates a dictionary which assigns a colour to each nucleotide (plus grey
    for "N" and white for "-")
    Parameters
    ----------
    None

    Returns
    -------
    dict
        Dictionary where keys are single letter nucleotide codes and
        values are hexadecimal codes for colours
    """
    return {'A': '#1ed30f', 'G': '#f4d931', 'T': '#f43131', 'C': '#315af4',
        'N': '#b2b2b2', '-': '#FFFFFF', 'U': '#f43131', 'R': '#b2b2b2', 'Y':
        '#b2b2b2', 'S': '#b2b2b2', 'W': '#b2b2b2', 'K': '#b2b2b2', 'M':
        '#b2b2b2', 'B': '#b2b2b2', 'D': '#b2b2b2', 'H': '#b2b2b2', 'V':
        '#b2b2b2', 'X': '#b2b2b2'}


@classmethod
def def_key(cls):
    """ Get the key used to define a data_type definition."""
    return cls.__def_key


def to_dict_if_string(s):
    """
    Convenience function to ensure dict-like output
    """
    if isinstance(s, str):
        return {s: None}
    else:
        return s


def serpentine_x(x, y, matrix):
    """Every other row is indexed in reverse."""
    if y % 2:
        return matrix.columns - 1 - x, y
    return x, y


def get_all(isamAppliance, check_mode=False, force=False):
    """
    Get all snmp objects
    """
    return isamAppliance.invoke_get('Get all snmp objects',
        '/core/rsp_snmp_objs')


def parse_out_ends(unparsed_string):
    """Parse and remove beginning and end whitespace of a string.

    :param unparsed_string: String with variable terminal whitespaces.
    :type unparsed_string: str
    :returns: String with parsed and removed beginning and ending whitespace.
    :rtype: str
    """
    if not isinstance(unparsed_string, str):
        raise TypeError(f'Paremeter type required is of type list.')
    beginning_trimmed = unparsed_string.lstrip()
    trimmed_string = beginning_trimmed.rstrip()
    return trimmed_string


def extractfields(data, commdct, objkey, fieldlists):
    """get all the objects of objkey.
    fieldlists will have a fieldlist for each of those objects.
    return the contents of those fields"""
    objindex = data.dtls.index(objkey)
    objcomm = commdct[objindex]
    objfields = []
    for dct in objcomm[0:]:
        try:
            thefieldcomms = dct['field']
            objfields.append(thefieldcomms[0])
        except KeyError as err:
            objfields.append(None)
    fieldindexes = []
    for fieldlist in fieldlists:
        fieldindex = []
        for item in fieldlist:
            if isinstance(item, int):
                fieldindex.append(item)
            else:
                fieldindex.append(objfields.index(item) + 0)
        fieldindexes.append(fieldindex)
    theobjects = data.dt[objkey]
    fieldcontents = []
    for theobject, fieldindex in zip(theobjects, fieldindexes):
        innerlst = []
        for item in fieldindex:
            try:
                innerlst.append(theobject[item])
            except IndexError as err:
                break
        fieldcontents.append(innerlst)
    return fieldcontents


def set_trace():
    """Call pdb.set_trace in the calling frame, first restoring
    sys.stdout to the real output stream. Note that sys.stdout is NOT
    reset to whatever it was before the call once pdb is done!
    """
    import pdb
    import sys
    stdout = sys.stdout
    sys.stdout = sys.__stdout__
    pdb.Pdb().set_trace(sys._getframe().f_back)


def annotate_parents_for_tree(tree):
    """Annotate each node in the given tree with its parent.

    >>> import io
    >>> tree = Bio.Phylo.read(io.StringIO("(A, (B, C))"), "newick")
    >>> not any([hasattr(node, "parent") for node in tree.find_clades()])
    True
    >>> tree = annotate_parents_for_tree(tree)
    >>> tree.root.parent is None
    True
    >>> all([hasattr(node, "parent") for node in tree.find_clades()])
    True
    """
    tree.root.parent = None
    for node in tree.find_clades(order='level'):
        for child in node.clades:
            child.parent = node
    return tree


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


def _normalize(file):
    """Convenience function to support path objects."""
    if 'Path' in type(file).__name__:
        return str(file)
    else:
        return file


def comments():
    """ Return all comments (list)
        """


@classmethod
def create(cls, surface, features):
    """
        Create instance of JanomeNode

        Parameters
        ----------
        surface : str
            Surface of the word
        features : janome.Token
            Features analyzed by Janome
        """
    ps = features.part_of_speech.split(',')
    return cls(surface=surface, part=ps[0] if len(ps) > 1 and ps[0] != '*' else
        '', part_detail1=ps[1] if len(ps) > 2 and ps[1] != '*' else '',
        part_detail2=ps[2] if len(ps) > 3 and ps[2] != '*' else '',
        part_detail3=ps[3] if len(ps) > 4 and ps[3] != '*' else '',
        stem_type=features.infl_type if features.infl_type != '*' else '',
        stem_form=features.infl_form if features.infl_form != '*' else '',
        word=features.base_form if features.base_form != '*' else '', kana=
        features.reading if features.reading != '*' else '', pronunciation=
        features.phonetic if features.phonetic != '*' else '')


def process_origx_records(data_file, pdb_file):
    """Takes a :py:class:`.PdbDataFile` and updates it based on the
    ORIGX records in the provided :py:class:`.PdbFile`

    :param PdbDataFile data_file: the Data File to update.
    :param PdbFile pdb_file: The source Pdb File"""
    origx1 = pdb_file.get_record_by_name('ORIGX1')
    origx2 = pdb_file.get_record_by_name('ORIGX2')
    origx3 = pdb_file.get_record_by_name('ORIGX3')
    if origx1 or origx2 or origx3:
        data_file._origx = {'o11': origx1[10:20] if origx1 else None, 'o12':
            origx1[20:30] if origx1 else None, 'o13': origx1[30:40] if
            origx1 else None, 't1': origx1[45:55] if origx1 else None,
            'o21': origx2[10:20] if origx2 else None, 'o22': origx2[20:30] if
            origx2 else None, 'o23': origx2[30:40] if origx2 else None,
            't2': origx2[45:55] if origx2 else None, 'o31': origx3[10:20] if
            origx3 else None, 'o32': origx3[20:30] if origx3 else None,
            'o33': origx3[30:40] if origx3 else None, 't3': origx3[45:55] if
            origx3 else None}
    else:
        data_file._origix = None


def set_num_workers(num_workers):
    """Sets the number of workers that are used in the torch DataLoaders.

    Args:
        num_workers (int): The number of workers that are used for data loading.
        """
    global NUM_WORKERS
    NUM_WORKERS = num_workers


@classmethod
def from_text(cls, line):
    """Create a `Variant` from a `.vcf` formatted line.

        :param line: string representing variant.

        """
    (chrom, pos, ident, ref, alt, qual, filt, info, genotype_keys,
        genotype_values, *others) = line.split('\t')
    pos = int(pos)
    pos -= 1
    gt = cls._sort_genotype_data(dict(zip(genotype_keys.split(':'),
        genotype_values.split(':'))))
    instance = cls(chrom, pos, ref, alt=alt, ident=ident, qual=qual, filt=
        filt, info=info, genotype_data=gt)
    return instance


def merge_result(command, res):
    """
    Merge all items in `res` into a list.

    This command is used when sending a command to multiple nodes
    and they result from each node should be merged into a single list.
    """
    if not isinstance(res, dict):
        raise ValueError('Value should be of dict type')
    result = set([])
    for _, v in res.items():
        for value in v:
            result.add(value)
    return list(result)


def is_anonymized(pnr):
    """Returns True iff the pnr has been anonymized.
    """
    return pnr[0] == '9'


@staticmethod
def read_file(filename):
    """
		Reads data from a file
		"""
    with open(filename, 'rb') as f:
        return f.read().decode('utf-8')


def applications():
    """Return list of applications

    Applications are typically provided by the profile,
    this function is called when "Show all apps" is enabled.

    """
    return []


def decorated_value(tree):
    """
    Get the decorating value from a tree's root node.id.

    Returns `None` if the tree is undecorated

    Parameters
    ----------
    tree: Glycan

    Returns
    -------
    int or None
    """
    try:
        return next(iter(tree.root.id))
    except Exception:
        return None


def do_OP_SIZE(vm):
    """
    >>> import binascii
    >>> s = [b'abcdef']
    >>> do_OP_SIZE(s)
    >>> print(s == [b'abcdef', b''])
    True
    >>> s = [b'abcdef'*1000]
    >>> do_OP_SIZE(s)
    >>> print(binascii.hexlify(s[-1]) == b'7017')
    True
    """
    vm.push_int(len(vm[-1]))


@staticmethod
def __get_list(target_list) ->list:
    """
        target_list が None なら 空の List を
        Noneでなければそのまま返す
        :param target_list:
        :return:
        """
    if target_list is None:
        return []
    else:
        return target_list


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


def _adjust_ratings_based_on_playcounts(top_tracks_to_recommendations_copy):
    """Adjust rating based on associated top track's playcount; the thought is that more frequently listened-to
    tracks should get a greater representation"""
    for top_track in top_tracks_to_recommendations_copy:
        recommendations = top_tracks_to_recommendations_copy[top_track]
        for recommendation in recommendations:
            recommendation.recommendation_rating *= top_track.playcount


def signal_map():
    """Map signal names to it's respective number.

    Returns a dict objet that maps the numeric signals from Linux to the
    corresponding signal name.
    Created from kill -l output on OEL:
    $ kill -l
    1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
    5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
    9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
    13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGSTKFLT
    17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
    21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU
    25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
    29) SIGIO       30) SIGPWR      31) SIGSYS      34) SIGRTMIN
    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4
    39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
    43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
    47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
    51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
    55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
    59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
    63) SIGRTMAX-1  64) SIGRTMAX

    It's an ugly hack, but couldn't find a portable code to produce the
    correct result.
    Not all signals were implemented anyway.
    """
    return dict(zip(list(range(1, 32)), ['SIGHUP', 'SIGINT', 'SIGQUIT',
        'SIGILL', 'SIGTRAP', 'SIGABRT', 'SIGBUS', 'SIGFPE', 'SIGKILL',
        'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGPIPE', 'SIGALRM', 'SIGTERM',
        'SIGSTKFLT', 'SIGCHLD', 'SIGCONT', 'SIGSTOP', 'SIGTSTP', 'SIGTTIN',
        'SIGTTOU', 'SIGURG', 'SIGXPU', 'SIGXFSZ', 'SIGVTALRM', 'SIGPROF',
        'SIGWINCH', 'SIGIO', 'SIGPWR', 'SIGSYS']))


def user_label(user):
    """Given a user ORM FAB object, returns a label"""
    if user:
        if user.first_name and user.last_name:
            return user.first_name + ' ' + user.last_name
        else:
            return user.username


@staticmethod
def get_childnodes_on_node(node, xpath, namespaces=None):
    """
        获取xpath指定的子节点(从节点开始检索)

        @param {Element} node - 开始检索的节点
        @param {string} xpath - - 符合XPath语法的搜索路径
        @param {dict} namespaces=None - 命名空间

        @return {list} - 获取到的节点列表
        """
    return node.xpath(xpath, namespaces=namespaces)


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

        :param AppLogInfo val:
        :rtype: ActorLogInfo
        """
    return cls('app', val)


def info(filenames, variables=None, product=None, type=None):
    """
    Read all the variables from a file, or a specified selection of variables in more detail, and print to stdout.

    :param filenames:   The filenames of the files to read
    :param variables:   The user specified variables of interest
    :param product:   The data reading plugin to use when reading the data
    :param type:   String representing the type of HDF data to read, i.e. 'VD' or 'SD'
    """
    from cis.data_io.products.AProduct import get_variables, get_data
    if variables is not None:
        for user_var in variables:
            print(str(get_data(filenames, user_var, product=product)))
    else:
        for variable in get_variables(filenames, product=product, data_type
            =type):
            print(variable)


def querydict_to_dict(querydict):
    """
    Converts a QueryDict instance (i.e.request params) into a plain
    dictionary
    """
    pure_dict = {}
    for item_key in querydict.keys():
        item_val_list = querydict.getlist(item_key)
        if item_val_list:
            if len(item_val_list) == 0:
                pure_dict[item_key] = None
            if len(item_val_list) == 1:
                pure_dict[item_key] = item_val_list[0]
            else:
                pure_dict[item_key] = item_val_list
        else:
            pure_dict[item_key] = None
    return pure_dict


@classmethod
def get_column_ref(cls, column_name: str) ->str:
    """How to refer to column in the clause"""
    return column_name


def do_entities_overlap(entities):
    """Checks if entities overlap.

    I.e. cross each others start and end boundaries.

    :param entities: list of entities
    :return: boolean
    """
    sorted_entities = sorted(entities, key=lambda e: e['start'])
    for i in range(len(sorted_entities) - 1):
        curr_ent = sorted_entities[i]
        next_ent = sorted_entities[i + 1]
        if next_ent['start'] < curr_ent['end'] and next_ent['entity'
            ] != curr_ent['entity']:
            return True
    return False


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 restore_sizing(edat):
    """Add sizing info to all points.

    Adds fourth argument called "psize" to each point.

    Args:
        edat (dict): extracted geometry data
    """
    for ind in edat['point'].keys():
        edat['point'][ind] = list(edat['point'][ind]) + ['psize']


@classmethod
def rgb_to_hex(cls, rgb):
    """
        Returns RGB color from hexadecimal
        :param rgb:
        :return: str
        """
    return '#%02x%02x%02x' % rgb


@staticmethod
def _stop_separate_timer(timer):
    """
        Stop the separate Thread if an answer has been already provided to the client.

        :param timer: The Timer object
        """
    timer.cancel()


def _replacer(x) ->str:
    """
    Replace a number with its hexadecimal representation. Used to tag
    temporary variables with their calling scope's id.
    """
    try:
        hexin = ord(x)
    except TypeError:
        hexin = x
    return hex(hexin)


def boolean_mask(value):
    """Mask a value to boolean. This can be used for sensitive fields.

    :param value: Any value.
    :returns boolean: The input value casted to boolean.
    """
    return bool(value)


def setLastAccessed():
    """
        Cause the last accessed time to be set to now.
        """


def get_name(obj):
    """Return a name for object checking the usual places
    """
    try:
        return obj.__name__
    except AttributeError:
        return obj.__class__.__name__


@staticmethod
def tags(node, *tag_names):
    """
        Yields tags from list of args, which are met in the node in sequential order.
        """
    for tag_name in tag_names:
        for e in node.findall('.//%s' % tag_name):
            yield e


def get_byte_array(padded_encoded_string):
    """Convert padded encoded string into bytes"""
    byte_array = bytearray()
    length = len(padded_encoded_string)
    for i in range(0, length, 8):
        byte = padded_encoded_string[i:i + 8]
        byte_array.append(int(byte, 2))
    return byte_array


def edit(text=None, editor=None, env=None, require_save=True, extension=
    '.txt', filename=None):
    """Edits the given text in the defined editor.  If an editor is given
    (should be the full path to the executable but the regular operating
    system search path is used for finding the executable) it overrides
    the detected editor.  Optionally, some environment variables can be
    used.  If the editor is closed without changes, `None` is returned.  In
    case a file is edited directly the return value is always `None` and
    `require_save` and `extension` are ignored.

    If the editor cannot be opened a :exc:`UsageError` is raised.

    Note for Windows: to simplify cross-platform usage, the newlines are
    automatically converted from POSIX to Windows and vice versa.  As such,
    the message here will have ``\\n`` as newline markers.

    :param text: the text to edit.
    :param editor: optionally the editor to use.  Defaults to automatic
                   detection.
    :param env: environment variables to forward to the editor.
    :param require_save: if this is true, then not saving in the editor
                         will make the return value become `None`.
    :param extension: the extension to tell the editor about.  This defaults
                      to `.txt` but changing this might change syntax
                      highlighting.
    :param filename: if provided it will edit this file instead of the
                     provided text contents.  It will not use a temporary
                     file as an indirection in that case.
    """
    from ._termui_impl import Editor
    editor = Editor(editor=editor, env=env, require_save=require_save,
        extension=extension)
    if filename is None:
        return editor.edit(text)
    editor.edit_file(filename)


def rle_append_modify(rle, a_r):
    """
    Append (a, r) (unpacked from *a_r*) to the rle list rle.
    Merge with last run when possible.

    MODIFIES rle parameter contents. Returns None.
    """
    a, r = a_r
    if not rle or rle[-1][0] != a:
        rle.append((a, r))
        return
    la, lr = rle[-1]
    rle[-1] = a, lr + r


def unix_time_from_uuid1(uuid_arg):
    """
    Converts a version 1 :class:`uuid.UUID` to a timestamp with the same precision
    as :meth:`time.time()` returns.  This is useful for examining the
    results of queries returning a v1 :class:`~uuid.UUID`.

    :param uuid_arg: a version 1 :class:`~uuid.UUID`
    """
    return (uuid_arg.time - 122192928000000000) / 10000000.0


@classmethod
def from_ps(cls, client, dictionary, **kwargs):
    """
        Construct a container object from the output of GET /containers/json.
        """
    new_dictionary = {'Id': dictionary['Id'], 'Image': dictionary['Image']}
    for name in dictionary.get('Names', []):
        if len(name.split('/')) == 2:
            new_dictionary['Name'] = name
    return cls(client, new_dictionary, **kwargs)


def StringProperty(name: str='', description: str='', default: str='',
    maxlen: int=0, options: set={'ANIMATABLE'}, tags: set={}, subtype: str=
    'NONE', update=None, get=None, set=None):
    """Returns a new string property definition.

    :param name: Name used in the user interface.
    :type name: str
    :param description: Text used for the tooltip and api documentation.
    :type description: str
    :param default: initializer string.
    :type default: str
    :param maxlen: maximum length of the string.
    :type maxlen: int
    :param options: Enumerator in [‘HIDDEN’, ‘SKIP_SAVE’, ‘ANIMATABLE’, ‘LIBRARY_EDITABLE’, ‘PROPORTIONAL’,’TEXTEDIT_UPDATE’].
    :type options: set
    :param tags: Enumerator of tags that are defined by parent class.
    :type tags: set
    :param subtype: Enumerator in [‘FILE_PATH’, ‘DIR_PATH’, ‘FILE_NAME’, ‘BYTE_STRING’, ‘PASSWORD’, ‘NONE’].
    :type subtype: str
    :param update: Function to be called when this value is modified, This function must take 2 values (self, context) and return None. Warning there are no safety checks to avoid infinite recursion.
    :param get: Function to be called when this value is ‘read’, This function must take 1 value (self) and return the value of the property.
    :param set: Function to be called when this value is ‘written’, This function must take 2 values (self, value) and return None.
    """
    pass


def dict_deserialize(iodata):
    """Turn IOData instance back into a dict"""
    return dict(**iodata.as_kwargs())


@classmethod
def parse(cls, subheader):
    """ Parse subheader from given string.

        :return DataDescriptionSubheader:

        """
    key, value = subheader.split(': ', 1)
    return cls(key, value)


def whitening_cholesky(array):
    """ Uniquely results in lower triangular positive diagonal cross-covariance and cross-correlation matrices. """
    pass


def get_setup(job=None):
    """
    Return the resource specific setup.

    :param job: optional job object.
    :return: setup commands (list).
    """
    return []


def draw_bboxes(img, bboxes, **kwarg):
    """draw bboxes on copied img

    Parameters
    ----------
    img : PIL.Image
        image which you want to draw box on
    bboxes : iterable
        [[x_min, y_min, x_max, y_max], ...]
        normalized coordinate. [0, 1]

    Returns
    -------
    c_img: PIL.Image
        drawn copied image
    """
    from PIL import ImageDraw
    img = img.copy()
    w, h = img.size
    draw = ImageDraw.Draw(img)
    for bbox in bboxes:
        draw.rectangle([bbox[0] * w, bbox[1] * h, bbox[2] * w, bbox[3] * h],
            **kwarg)
    del draw
    return img


def getBrowserId(create=1):
    """Return a browser id for the current request.

        o If create is false, return None if there is no browser id associated
          with the current request.

        o If create is true, return a newly-created browser id if
          there is no browser id associated with the current request.

        o This method is useful in conjunction with 'getBrowserIdName' if you
          wish to embed the browser-id-name/browser-id combination as a hidden
          value in a POST-based form.

        o The browser id is opaque, has no business meaning, and its length,
          type, and composition are subject to change.

        o Permission required: Access contents information

        o Raises BrowserIdManagerErr if an ill-formed browser id
          is found in REQUEST.
        """


def _check_replicated_cards(replicated_cards):
    """helper method for ``parse_cards_list``"""
    replicated_card_old = []
    try:
        for replicated_card in replicated_cards:
            assert replicated_card != replicated_card_old
            replicated_card_old = replicated_card
    except AssertionError:
        replicated_card_old = []
        for replicated_card in replicated_cards:
            assert replicated_card != replicated_card_old
            replicated_card_old = replicated_card
        raise


@staticmethod
def evaluate(x, amplitude, x_0, fwhm):
    """
        One dimensional Drude model function
        """
    return amplitude * (fwhm / x_0) ** 2 / ((x / x_0 - x_0 / x) ** 2 + (
        fwhm / x_0) ** 2)


def make_admin_urlpatterns():
    """Imports the default site admin instance and returns a patterns() list
    configured to serve it at /admin/.
    """
    try:
        from django.conf.urls import patterns, include, url
    except ImportError:
        from django.conf.urls.defaults import patterns, include, url
    from django.contrib import admin
    admin.autodiscover()
    return patterns('', url('^admin/', include(admin.site.urls)))


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 __call__(context, request):
    """ Must return an object that implements IResponse. """


def cache(ICachableItem):
    """Updates caches area with latest item information returning ICachedItem if cache updates were required"""


def fmodifier_copy():
    """Copy the F-Modifier(s) of the active F-Curve

    """
    pass


@classmethod
def check_keyword(cls, keyword):
    """Check if keyword is good for search or not.

    keyword: str

    return: boolean

    """
    import unicodedata
    keyword = unicodedata.normalize('NFKC', keyword)
    if not len(keyword.strip()):
        return False
    return True


def convert_by_vocab(vocab, items):
    """Converts a sequence of [tokens|ids] using the vocab."""
    output = []
    for item in items:
        output.append(vocab[item])
    return output


def delete_remote_access_session(arn=None):
    """
    Deletes a completed remote access session and its results.
    See also: AWS API Documentation

    Examples
    The following example deletes a specific remote access session.
    Expected Output:

    :example: response = client.delete_remote_access_session(
        arn='string'
    )

    :type arn: string
    :param arn: [REQUIRED]
            The Amazon Resource Name (ARN) of the sesssion for which you want to delete remote access.

    :rtype: dict
    :return: {}

    """
    pass


def load_vocab(vocab_file):
    """Loads a vocabulary file into a dictionary."""
    vocab = {}
    index = 0
    with open(vocab_file, 'r', encoding='utf-8') as reader:
        while True:
            token = reader.readline()
            if not token:
                break
            token = token.strip()
            vocab[token] = index
            index += 1
    return vocab


def forwards(apps, schema_editor):
    """Set same_as_priority for all same-as contacts """
    same_as_model = apps.get_model('Crm', 'SameAs')
    for same_as in same_as_model.objects.all():
        if same_as.contact_set.count() == 1:
            for contact in same_as.contact_set.all():
                contact.same_as = None
                contact.same_as_priority = 0
                contact.save()
        if same_as.contact_set.count() <= 1:
            same_as.delete()


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


@staticmethod
def getJavaPackage():
    """ Returns package name String. """
    return 'com.microsoft.ml.spark.lime.TabularLIME'


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

        :param MemberSpaceLimitsChangeCapsTypePolicyDetails val:
        :rtype: EventDetails
        """
    return cls('member_space_limits_change_caps_type_policy_details', val)


def __before_pop(collection, _sa_initiator=None):
    """An event which occurs on a before a pop() operation occurs."""
    executor = collection._sa_adapter
    if executor:
        executor.fire_pre_remove_event(_sa_initiator)


def format_sealed(sealed):
    """
    Return a string formatted representation of a node's sealed status

    :param sealed: the value for the sealed attribute of the node
    :return: string representation of seal status
    """
    return 'True' if sealed == 1 else 'False'


@staticmethod
def create_ports(port, mpi, rank):
    """create a list of ports for the current rank"""
    if port == 'random' or port is None:
        ports = {}
    else:
        port = int(port)
        ports = {'REQ': port + 0, 'PUSH': port + 1, 'SUB': port + 2}
    if mpi == 'all':
        for port in ports:
            ports[port] += rank * 3
    return ports


def getValue(source, token):
    """Return a token for the value."""


def getSize():
    """Return the byte-size of the data of the object."""


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 make_docker_image_name(job_config, course, session, mode, prefix='MORF'):
    """
    Create a uniqe name for the current job_config
    :param job_config:
    :return:
    """
    name = '{}-{}-{}-{}-{}'.format(prefix, job_config.morf_id, mode, course,
        session)
    return name


def auto_bin_size(nevt):
    """
  Given the list of event size (counts), return appropriate num of bins,
  which is calculated from sqrt of smallest event size, rounded in 5.
  min of 20, max of 100.

  >>> auto_bin_size(100)
  20
  >>> auto_bin_size(5000)
  70
  >>> auto_bin_size(1E6)
  100
  >>> auto_bin_size(0)  # Even null plot, return minimum number of bins
  20

  """
    minv = 20
    maxv = 100
    val = int(round(1.0 / 5 * nevt ** 0.5) * 5)
    return minv if val < minv else maxv if val > maxv else val


def get_cleaned_personal_identifier(x):
    """
    Returns upper case cleaned personal identifiers (CURP, RFC, NSS)
    :param x:
    :return:
    """
    return str(x).strip().upper().replace('-', '').replace(' ', '')


def _plain_bfs(G, source):
    """A fast BFS node generator

    The direction of the edge between nodes is ignored.

    For directed graphs only.

    """
    Gsucc = G.succ
    Gpred = G.pred
    seen = set()
    nextlevel = {source}
    while nextlevel:
        thislevel = nextlevel
        nextlevel = set()
        for v in thislevel:
            if v not in seen:
                yield v
                seen.add(v)
                nextlevel.update(Gsucc[v])
                nextlevel.update(Gpred[v])


def maybe_evaluate(obj):
    """Attempt to evaluate promise, even if obj is not a promise."""
    try:
        return obj.__maybe_evaluate__()
    except AttributeError:
        return obj


def _transform_positioning(ha, va):
    """Converts matplotlib positioning to pgf node positioning.
    Not quite accurate but the results are equivalent more or less."""
    if ha == 'center' and va == 'center':
        return None
    ha_mpl_to_tikz = {'right': 'east', 'left': 'west', 'center': ''}
    va_mpl_to_tikz = {'top': 'north', 'bottom': 'south', 'center': '',
        'baseline': 'base'}
    return 'anchor={} {}'.format(va_mpl_to_tikz[va], ha_mpl_to_tikz[ha]).strip(
        )