def WriteControls(filename, data):
    """Write current values to a .imctrl (Image Controls) file"""
    File = open(filename, 'w')
    keys = ['type', 'color', 'wavelength', 'calibrant', 'distance',
        'center', 'Oblique', 'tilt', 'rotation', 'azmthOff',
        'fullIntegrate', 'LRazimuth', 'setdist', 'IOtth', 'outChannels',
        'outAzimuths', 'invert_x', 'invert_y', 'DetDepth', 'calibskip',
        'pixLimit', 'cutoff', 'calibdmin', 'Flat Bkg', 'varyList',
        'binType', 'SampleShape', 'PolaVal', 'SampleAbs', 'dark image',
        'background image', 'twoth']
    for key in keys:
        if key not in data:
            continue
        File.write(key + ':' + str(data[key]) + '\n')
    File.close()


@staticmethod
def _get_name_from_dn(dn):
    """
        Get the instance name from the dn

        :param dn: string containing the distinguished name URL
        :return: string containing the name
        """
    name = dn.split('/sys/tunnel-')[1].split('/')[0]
    return name


def remove_trace(variable, mode, cbname):
    """
    Remove trace from variable.

    Ensure compatibility with old and new trace method.
    mode: "read", "write", "unset" (new syntax)
    """
    try:
        variable.trace_remove(mode, cbname)
    except AttributeError:
        variable.trace_vdelete(mode[0], cbname)


@staticmethod
def isZeroVector(vector):
    """
        Returns true if the Vector given is equal to the zero vector.
        """
    if vector.x == 0 and vector.y == 0 and vector.z == 0:
        return True
    return False


def get_trace_summaries(StartTime=None, EndTime=None, Sampling=None,
    FilterExpression=None, NextToken=None):
    """
    Retrieves IDs and metadata for traces available for a specified time frame using an optional filter. To get the full traces, pass the trace IDs to BatchGetTraces .
    See also: AWS API Documentation

    :example: response = client.get_trace_summaries(
        StartTime=datetime(2015, 1, 1),
        EndTime=datetime(2015, 1, 1),
        Sampling=True|False,
        FilterExpression='string',
        NextToken='string'
    )

    :type StartTime: datetime
    :param StartTime: [REQUIRED]
            The start of the time frame for which to retrieve traces.

    :type EndTime: datetime
    :param EndTime: [REQUIRED]
            The end of the time frame for which to retrieve traces.

    :type Sampling: boolean
    :param Sampling: Set to true to get summaries for only a subset of available traces.

    :type FilterExpression: string
    :param FilterExpression: Specify a filter expression to retrieve trace summaries for services or requests that meet certain requirements.

    :type NextToken: string
    :param NextToken: Specify the pagination token returned by a previous request to retrieve the next page of results.

    :rtype: dict
    :return: {
        'TraceSummaries': [
            {
                'Id': 'string',
                'Duration': 123.0,
                'ResponseTime': 123.0,
                'HasFault': True|False,
                'HasError': True|False,
                'HasThrottle': True|False,
                'IsPartial': True|False,
                'Http': {
                    'HttpURL': 'string',
                    'HttpStatus': 123,
                    'HttpMethod': 'string',
                    'UserAgent': 'string',
                    'ClientIp': 'string'
                },
                'Annotations': {
                    'string': [
                        {
                            'AnnotationValue': {
                                'NumberValue': 123.0,
                                'BooleanValue': True|False,
                                'StringValue': 'string'
                            },
                            'ServiceIds': [
                                {
                                    'Name': 'string',
                                    'Names': [
                                        'string',
                                    ],
                                    'AccountId': 'string',
                                    'Type': 'string'
                                },
                            ]
                        },
                    ]
                },
                'Users': [
                    {
                        'UserName': 'string',
                        'ServiceIds': [
                            {
                                'Name': 'string',
                                'Names': [
                                    'string',
                                ],
                                'AccountId': 'string',
                                'Type': 'string'
                            },
                        ]
                    },
                ],
                'ServiceIds': [
                    {
                        'Name': 'string',
                        'Names': [
                            'string',
                        ],
                        'AccountId': 'string',
                        'Type': 'string'
                    },
                ]
            },
        ],
        'ApproximateTime': datetime(2015, 1, 1),
        'TracesProcessedCount': 123,
        'NextToken': 'string'
    }

    :returns:
    (dict) --
    Name (string) --
    Names (list) --
    (string) --

    AccountId (string) --
    Type (string) --

    """
    pass


def disable_antivirus(session, return_type=None, **kwargs):
    """
    Disables the antivirus engine.

    :type session: zadarapy.session.Session
    :param session: A valid zadarapy.session.Session object.  Required.

    :type return_type: str
    :param return_type: If this is set to the string 'json', this function
        will return a JSON string.  Otherwise, it will return a Python
        dictionary.  Optional (will return a Python dictionary by default).
    """
    path = '/api/antivirus/engine.json'
    return session.delete_api(path=path, return_type=return_type, **kwargs)


@classmethod
def prep_orm_class(cls, orm_cls, schema=None, is_geospatial=False):
    """
        helper method to ready an ORM class (see Base and it's children) according to this Database's settings
        :why?: sometimes you might have classes you want as part of a query, but you don't want those classes
        available in the Database.classes() or Database.sorted_classes(), since these tables are not being loaded, etc..
        """
    if is_geospatial and hasattr(orm_cls, 'add_geometry_column'):
        orm_cls.add_geometry_column()
    if schema:
        orm_cls.set_schema(schema)


def authentication_validation(username, password, access_token):
    """
    Only accept one form of authentication.
    """
    if bool(username) is not bool(password):
        raise Exception(
            'Basic authentication requires a username AND password.')
    if username and access_token or password and access_token:
        raise Exception(
            'Cannot use both Basic Authentication and OAuth2.0. Please use only one authentication method.'
            )


def _mpi_parse_args(args):
    """Parse arguments for MPI comms."""
    from mpi4py import MPI
    nworkers = MPI.COMM_WORLD.Get_size() - 1
    is_master = MPI.COMM_WORLD.Get_rank() == 0
    libE_specs = {'comm': MPI.COMM_WORLD, 'comms': 'mpi'}
    return nworkers, is_master, libE_specs, args.tester_args


@staticmethod
def disp_id(data):
    """ Display first seq of message id only """
    return data['parent_header']['msg_id'].split('-')[0]


def get_tex(filename):
    """ Get the content of the tex file """
    print('Reading {}...'.format(filename))
    with open(filename, 'r') as file:
        return file.read()


def is_cons(w):
    """Return True if no vowels in w."""
    for lt in w:
        if lt in ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']:
            return False
    return True


@classmethod
def search_rec_name(cls, name, clause):
    """
        Return a list of arguments for search on rec_name.
        """
    rec_name = cls._rec_name
    if rec_name not in cls._fields:
        return []
    return [(rec_name,) + tuple(clause[1:])]


def join_version(version_tuple):
    """Return a string representation of version from given VERSION tuple"""
    version = '%s.%s.%s' % version_tuple[:3]
    if version_tuple[3] != 'final':
        version += '-%s' % version_tuple[3]
    return version


def turbulent_Dipprey_Sabersky(Re, Pr, fd, eD):
    """Calculates internal convection Nusselt number for turbulent flows
    in pipe according to [2]_ as shown in [1]_.

    .. math::
        Nu = \\frac{RePr(f/8)}{1 + (f/8)^{0.5}[5.19Re_\\epsilon^{0.2} Pr^{0.44} - 8.48]}

    Parameters
    ----------
    Re : float
        Reynolds number, [-]
    Pr : float
        Prandtl number, [-]
    fd : float
        Darcy friction factor [-]
    eD : float
        Relative roughness, [-]

    Returns
    -------
    Nu : float
        Nusselt number, [-]

    Notes
    -----
    According to [1]_, the limits are:
    1.2 ≤ Pr ≤ 5.94 and 1.4*10^4 ≤ Re ≤ 5E5 and 0.0024 ≤ eD ≤ 0.049.

    Examples
    --------
    >>> turbulent_Dipprey_Sabersky(Re=1E5, Pr=1.2, fd=0.0185, eD=1E-3)
    288.33365198566656

    References
    ----------
    .. [1] Rohsenow, Warren and James Hartnett and Young Cho. Handbook of Heat
       Transfer, 3E. New York: McGraw-Hill, 1998.
    .. [2] Dipprey, D. F., and R. H. Sabersky. “Heat and Momentum Transfer in
       Smooth and Rough Tubes at Various Prandtl Numbers.” International
       Journal of Heat and Mass Transfer 6, no. 5 (May 1963): 329–53.
       doi:10.1016/0017-9310(63)90097-8
    """
    Re_e = Re * eD * (fd / 8.0) ** 0.5
    return Re * Pr * fd / 8.0 / (1 + (fd / 8.0) ** 0.5 * (5.19 * Re_e **
        0.2 * Pr ** 0.44 - 8.48))


def initialize_schema():
    """Creates missing database tables."""


@classmethod
def get_arthur_params_from_url(cls, url):
    """ Get the arthur params given a URL for the data source """
    tokens = url.split(' ', 1)
    return {'url': tokens[0]}


def generate(path, request, **kw):
    """ Generate a URL for the given path """


def false_if_all_true_filter(*value):
    """
    Returns false if all values have a 'true-ish' value.

    Args:
        *value: list of values

    Returns: 'true' if all values have a 'true-ish' value
    """
    for v in value:
        if not v:
            return True
    return False


def timeseries(start='2000-01-01', end='2000-01-31', freq='1s',
    partition_freq='1d', dtypes={'name': str, 'id': int, 'x': float, 'y':
    float}, seed=None, **kwargs):
    """ Create timeseries dataframe with random data

    Parameters
    ----------
    start : datetime (or datetime-like string)
        Start of time series
    end : datetime (or datetime-like string)
        End of time series
    dtypes : dict
        Mapping of column names to types.
        Valid types include {float, int, str, 'category'}
    freq : string
        String like '2s' or '1H' or '12W' for the time series frequency
    partition_freq : string
        String like '1M' or '2Y' to divide the dataframe into partitions
    seed : int (optional)
        Randomstate seed
    kwargs:
        Keywords to pass down to individual column creation functions.
        Keywords should be prefixed by the column name and then an underscore.

    Examples
    --------
    >>> import dask
    >>> df = dask.datasets.timeseries()
    >>> df.head()  # doctest: +SKIP
              timestamp    id     name         x         y
    2000-01-01 00:00:00   967    Jerry -0.031348 -0.040633
    2000-01-01 00:00:01  1066  Michael -0.262136  0.307107
    2000-01-01 00:00:02   988    Wendy -0.526331  0.128641
    2000-01-01 00:00:03  1016   Yvonne  0.620456  0.767270
    2000-01-01 00:00:04   998   Ursula  0.684902 -0.463278
    >>> df = dask.datasets.timeseries(
    ...     '2000', '2010',
    ...     freq='2H', partition_freq='1D', seed=1,  # data frequency
    ...     dtypes={'value': float, 'name': str, 'id': int},  # data types
    ...     id_lam=1000  # control number of items in id column
    ... )
    """
    from dask.dataframe.io.demo import make_timeseries
    return make_timeseries(start=start, end=end, freq=freq, partition_freq=
        partition_freq, seed=seed, dtypes=dtypes, **kwargs)


def aicc(llf, nobs, df_modelwc):
    """Akaike information criterion (AIC) with small sample correction

    Parameters
    ----------
    llf : float
        value of the loglikelihood
    nobs : int
        number of observations
    df_modelwc : int
        number of parameters including constant

    Returns
    -------
    aicc : float
        information criterion

    References
    ----------
    http://en.wikipedia.org/wiki/Akaike_information_criterion#AICc
    """
    return -2.0 * llf + 2.0 * df_modelwc * nobs / (nobs - df_modelwc - 1.0)


@classmethod
def from_pygments_lexer_cls(cls, lexer_cls):
    """
        Create a :class:`.RegexSync` instance for this Pygments lexer class.
        """
    patterns = {'Python': '^\\s*(class|def)\\s+', 'Python 3':
        '^\\s*(class|def)\\s+', 'HTML': '<[/a-zA-Z]', 'JavaScript':
        '\\bfunction\\b'}
    p = patterns.get(lexer_cls.name, '^')
    return cls(p)


def _is_coro(maybe_coro):
    """Return whether or not the thing is a coro."""
    try:
        return maybe_coro.send and maybe_coro.throw and maybe_coro.close
    except AttributeError:
        return False


def limit_length(s, length):
    """ Add ellipses to overly long strings """
    if s is None:
        return None
    ELLIPSES = '...'
    if len(s) > length:
        return s[:length - len(ELLIPSES)] + ELLIPSES
    return s


def map_range(x, in_min, in_max, out_min, out_max):
    """re-maps a number from one range to another."""
    mapped = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
    if out_min <= out_max:
        return max(min(mapped, out_max), out_min)
    return min(max(mapped, out_max), out_min)


def paths_update():
    """Recalculate paths for bones that already have them

    """
    pass


def _json_error_serializer(req, resp, exception):
    """We don't convert the exception to JSON
    since the response interceptor will handle
    that for us """
    resp.body = exception.to_dict()


def check_perm_structure_preservation(mol, perm):
    """
    checks what percent of a permutation does not break bonds in the molecule--
    so a return value of 1 would be a perfectly preserved bond structure, of 0.5 would mean half of the molecules bonds are broken
    :param mol: the molecule being permuted
    :param perm: the permutation whose structure preservation is being measured
    :return: percent of bonds that are preserved
    """
    if len(mol.bondset) == 0:
        raise ValueError('Molecule does not have any bond information')
    broken = 0
    for origin, destination in enumerate(perm):
        for adjacent in mol.atoms[origin].adjacent:
            if (destination, perm[adjacent]) not in mol.bondset:
                broken += 1
    percent_structure = (len(mol.bondset) - broken) / len(mol.bondset)
    return percent_structure


def previewrange_set():
    """Set Preview Range based on extents of selected Keyframes

    """
    pass


@staticmethod
def p_function_optional_list_optional(p):
    """function_optional_list_optional : empty
                                           | function_optional_list"""
    p[0] = tuple() if p[1] is None else p[1]


def app_version(request):
    """A template variable to display current version."""
    from nsot import __version__
    return {'NSOT_VERSION': __version__}


@classmethod
def register_extension(cls, register_fn):
    """
        Call the register function of an extension. You must override this
        if you provide a custom ModelAdmin class and want your extensions to
        be able to patch stuff in.
        """
    register_fn(cls, None)


def _translateOID(obj):
    """Internal function that translates obj into an __OID
    object if it is a PMI object instance.

    If the object is not a PMI object, returns obj untouched.
    """
    if hasattr(obj, '__pmioid'):
        return obj.__pmioid
    else:
        return obj


def rows_to_display(station_board):
    """
    Iterator for tabular output of board
    """
    yield 'Platform', 'Destination', 'Scheduled', 'Due'
    for service in station_board.train_services:
        yield service.platform, service.destination_text, service.std, service.etd


@classmethod
def create_hap_event(cls, bytesdata):
    """Creates a HAP HTTP EVENT response for the given data.

        @param data: Payload of the request.
        @type data: bytes
        """
    return cls.EVENT_MSG_STUB + str(len(bytesdata)).encode('utf-8'
        ) + b'\r\n' * 2 + bytesdata


def _clear_plugins():
    """Clear the plugin state to the default, i.e., where no plugins are loaded

    """
    global plugin_store
    plugin_store = {'imread': [], 'imsave': [], 'imshow': [],
        'imread_collection': [], 'imshow_collection': [], '_app_show': []}


def _quick_sort_part(lst, mark_l, mark_r):
    """Quick sort partition"""
    pivot_val = lst[mark_l]
    mark_l_cur = mark_l + 1
    mark_r_cur = mark_r
    done = False
    while not done:
        while mark_l_cur <= mark_r_cur and lst[mark_l_cur] <= pivot_val:
            mark_l_cur = mark_l_cur + 1
        while mark_l_cur <= mark_r_cur and lst[mark_r_cur] >= pivot_val:
            mark_r_cur = mark_r_cur - 1
        if mark_r_cur < mark_l_cur:
            done = True
        else:
            lst[mark_l_cur], lst[mark_r_cur] = lst[mark_r_cur], lst[mark_l_cur]
    lst[mark_l], lst[mark_r_cur] = lst[mark_r_cur], lst[mark_l]
    return mark_r_cur


def loopover(vars, outview, inaxes=None, preserve=None, pbar=None):
    """ Loop over a variable
    In: input variable, view of reduced variable, optionally a list of integer indices (preserve)
    to axes that should be loaded in their entirety
    Out: slices into an accumulation array, chunks of the input variable that will go into that part of
    the accumulation array"""
    from pygeode.var import Var
    if isinstance(vars, Var):
        vars = [vars]
    outview = outview.clip()
    if inaxes is None:
        assert len(vars
            ) == 1, 'Implicit reduction axes (inaxes=None) can only be used when looping over one variable.'
        inaxes = vars[0].axes
    inview = outview.map_to(inaxes, strict=False)
    loop = list(inview.loop_mem(preserve=preserve))
    for i, inv in enumerate(loop):
        outv = inv.map_to(outview.axes)
        subpbar = pbar.part(i, len(loop))
        data = []
        for j, v in enumerate(vars):
            vpbar = subpbar.part(j, len(vars))
            try:
                data.append(inv.get(v, pbar=vpbar))
            except StopIteration:
                raise Exception(
                    'Stray StopIteration signal caught.  Unable to retrieve the data.'
                    )
        yield outv.slices, data


def get_lexicon(Name=None):
    """
    Returns the content of the specified pronunciation lexicon stored in an AWS Region. For more information, see Managing Lexicons .
    See also: AWS API Documentation

    Examples
    Returns the content of the specified pronunciation lexicon stored in an AWS Region.
    Expected Output:

    :example: response = client.get_lexicon(
        Name='string'
    )

    :type Name: string
    :param Name: [REQUIRED]
            Name of the lexicon.

    :rtype: dict
    :return: {
        'Lexicon': {
            'Content': 'string',
            'Name': 'string'
        },
        'LexiconAttributes': {
            'Alphabet': 'string',
            'LanguageCode': 'cy-GB'|'da-DK'|'de-DE'|'en-AU'|'en-GB'|'en-GB-WLS'|'en-IN'|'en-US'|'es-ES'|'es-US'|'fr-CA'|'fr-FR'|'is-IS'|'it-IT'|'ja-JP'|'nb-NO'|'nl-NL'|'pl-PL'|'pt-BR'|'pt-PT'|'ro-RO'|'ru-RU'|'sv-SE'|'tr-TR',
            'LastModified': datetime(2015, 1, 1),
            'LexiconArn': 'string',
            'LexemesCount': 123,
            'Size': 123
        }
    }

    """
    pass


def get_active_tetrode(set_filename):
    """in the .set files it will say collectMask_X Y for each tetrode number to tell you if
    it is active or not. T1 = ch1-ch4, T2 = ch5-ch8, etc."""
    active_tetrode = []
    active_tetrode_str = 'collectMask_'
    with open(set_filename) as f:
        for line in f:
            if active_tetrode_str in line:
                tetrode_str, tetrode_status = line.split(' ')
                if int(tetrode_status) == 1:
                    tetrode_str.find('_')
                    tet_number = int(tetrode_str[tetrode_str.find('_') + 1:])
                    active_tetrode.append(tet_number)
    return active_tetrode


@classmethod
def mixed_list_file(cls, filename):
    """
        Read a list of mixed values from a file.

        The file format is:

         - # anywhere in the line begins a comment
         - leading and trailing spaces are ignored
         - empty lines are ignored
         - strings cannot span over a single line
         - integers can be specified as:
            - decimal numbers ("100" is 100)
            - hexadecimal numbers ("0x100" is 256)
            - binary numbers ("0b100" is 4)
            - octal numbers ("0100" is 64)

        @type  filename: str
        @param filename: Name of the file to read.

        @rtype:  list
        @return: List of integers and strings read from the file.
        """
    count = 0
    result = list()
    fd = open(filename, 'r')
    for line in fd:
        count = count + 1
        if '#' in line:
            line = line[:line.find('#')]
        line = line.strip()
        if line:
            try:
                value = cls.integer(line)
            except ValueError:
                value = line
            result.append(value)
    return result


def get_inbound_message(msg_id):
    """
        Get an inbound mesage from the message store.

        :param msg_id:
            The identifier of the message to retrieve.

        :returns:
            A TransportUserMessage, or ``None`` if the message is not found.
            If async, a Deferred is returned instead.
        """


def p_statement_empty(p):
    """statement : SEMI"""
    pass


def grouper(iterable, n):
    """
    grouper([0,1,2,3,4], 3) --> [(0,1,2), (3,4)]
    """
    for i in range(0, len(iterable), n):
        yield iterable[i:i + n]


def notify(title, message, retcode=None):
    """
    adapted from https://gist.github.com/baliw/4020619
    """
    try:
        import Foundation
        import objc
    except ImportError:
        import sys
        import logging
        logger = logging.getLogger(__name__)
        if sys.platform.startswith('darwin') and hasattr(sys, 'real_prefix'):
            logger.error(
                "Using ntfy with the MacOS Notification Center doesn't work within a virtualenv"
                )
            sys.exit(1)
        else:
            raise
    NSUserNotification = objc.lookUpClass('NSUserNotification')
    NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
    notification = NSUserNotification.alloc().init()
    notification.setTitle_(title)
    if message is not None:
        notification.setInformativeText_(message)
    notification.setDeliveryDate_(Foundation.NSDate.date())
    NSUserNotificationCenter.defaultUserNotificationCenter(
        ).scheduleNotification_(notification)


def calulate_loss_of_life(List_V, t):
    """ For list of V values, calculate loss of life in hours
    t = Time Interval (min)
    """
    L = 0
    for V in List_V:
        L += V * t
    LoL = L / 60
    return LoL


def indices2one_hot(indices, nb_classes):
    """
    Convert an iterable of indices to one-hot encoded list.

    You might also be interested in sklearn.preprocessing.OneHotEncoder

    Parameters
    ----------
    indices : iterable
        iterable of indices
    nb_classes : int
        Number of classes
    dtype : type

    Returns
    -------
    one_hot : list

    Examples
    --------
    >>> indices2one_hot([0, 1, 1], 3)
    [[1, 0, 0], [0, 1, 0], [0, 1, 0]]
    >>> indices2one_hot([0, 1, 1], 2)
    [[1, 0], [0, 1], [0, 1]]
    """
    if nb_classes < 1:
        raise ValueError('nb_classes={}, but positive number expected'.
            format(nb_classes))
    one_hot = []
    for index in indices:
        one_hot.append([0] * nb_classes)
        one_hot[-1][index] = 1
    return one_hot


def pageHeader(headline='', tagline=''):
    """
    *Generate a pageHeader - TBS style*

    **Key Arguments:**
        - ``headline`` -- the headline text
        - ``tagline`` -- the tagline text for below the headline

    **Return:**
        - ``pageHeader`` -- the pageHeader
    """
    pageHeader = (
        """

%(headline)s
%(tagline)s

"""
         % locals())
    return pageHeader


def read_os_decrypted_non_windows(source):
    """
    Read from Emotiv hid device.
    :param source: Emotiv hid device
    :return: Next packet from Emotiv device.
    """
    data = source.read(32)
    if data != '':
        return data


def variable_name_to_option_name(variable: str):
    """
    Convert a variable name like `ec2_user` to the Click option name it gets mapped to,
    like `--ec2-user`.
    """
    return '--' + variable.replace('_', '-')


def render_layer_add():
    """Add a render layer

    """
    pass


def add_argument_all(parser):
    """Add an ``--all`` argument to the parser."""
    parser.add_argument('-a', '--all', action='store_true', default=False,
        help='{} all MSL packages.'.format(parser.get_command_name().
        capitalize()))


def del_cookie(response, value):
    """
    Funkcja usuwa ciasteczko.
    """
    response.set_cookie(value, '', expires=0)


def round_vehicles(vehicles):
    """Round values for vehicles to reduce data size. Mutates vehicles."""
    for v in vehicles.values():
        v['x'] = round(v['x'], 2)
        v['y'] = round(v['y'], 2)
        v['speed'] = int(round(v['speed']))
        v['angle'] = int(round(v['angle']))


def _sum_counters(*counters_list):
    """Combine many maps from group to counter to amount."""
    result = {}
    for counters in counters_list:
        for group, counter_to_amount in counters.items():
            for counter, amount in counter_to_amount.items():
                result.setdefault(group, {})
                result[group].setdefault(counter, 0)
                result[group][counter] += amount
    return result


def convert_value_to_code(M_c, cidx, value):
    """
    For a column with categorical data, this function takes the raw value
    (e.g. 'Joe' or 234.23409), which is always encoded as a string, and returns the
    'code': the integer used to represent that value in the underlying representation.

    Note that the underlying store 'code_to_value' is unfortunately named backwards.
    TODO: fix the backwards naming.
    """
    if M_c['column_metadata'][cidx]['modeltype'] == 'normal_inverse_gamma':
        return float(value)
    else:
        return M_c['column_metadata'][cidx]['code_to_value'][str(value)]


def get_notification_params(**params):
    """ Get notification params from raw parameters. """
    create_link = params['create_link']
    result = dict(user=params['user'])
    result['project_link'] = create_link(params.get('repository_url'),
        params['project_name'])
    if params.get('server_name'):
        result['server_link'] = create_link(params.get('public_url'),
            params['server_name'])
    if params.get('commit'):
        result['commit_link'] = create_link(params.get('commit_url'),
            params['commit'])
    if params.get('branch'):
        result['branch_link'] = create_link(params.get('branch_url'),
            params['branch'])
    if params.get('script'):
        preformat = params.get('pre_format') or (lambda x: x)
        result['script'] = preformat(params['script'])
    return result


@staticmethod
def _generate_dates(dates):
    """
        Generate and merge dates object
        :param dates: object with options for query, see doc
        :return: updated opts
        """
    default = {'from': '1h', 'to': None}
    if not dates:
        return default
    default.update(dates)
    return default


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

        :param ShmodelGroupShareType val:
        :rtype: EventType
        """
    return cls('shmodel_group_share', val)


def versionStrToTuple(versionStr):
    """ Converts a version string to tuple

        E.g. 'x.y.z' to (x, y, x)
    """
    versionInfo = []
    for elem in versionStr.split('.'):
        try:
            versionInfo.append(int(elem))
        except:
            versionInfo.append(elem)
    return tuple(versionInfo)


def _add_trits(left, right):
    """
    Adds two individual trits together.

    The result is always a single trit.
    """
    res = left + right
    return res if -2 < res < 2 else (res < 0) - (res > 0)


def _url_collapse_path(path):
    """
    Given a URL path, remove extra '/'s and '.' path elements and collapse
    any '..' references and returns a colllapsed path.

    Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.
    The utility of this function is limited to is_cgi method and helps
    preventing some security attacks.

    Returns: A tuple of (head, tail) where tail is everything after the final /
    and head is everything before it.  Head will always start with a '/' and,
    if it contains anything else, never have a trailing '/'.

    Raises: IndexError if too many '..' occur within the path.

    """
    path_parts = path.split('/')
    head_parts = []
    for part in path_parts[:-1]:
        if part == '..':
            head_parts.pop()
        elif part and part != '.':
            head_parts.append(part)
    if path_parts:
        tail_part = path_parts.pop()
        if tail_part:
            if tail_part == '..':
                head_parts.pop()
                tail_part = ''
            elif tail_part == '.':
                tail_part = ''
    else:
        tail_part = ''
    splitpath = '/' + '/'.join(head_parts), tail_part
    collapsed_path = '/'.join(splitpath)
    return collapsed_path


def isfile(line):
    """
        Files are wrapped in FHS / FTS
        FHS = file header segment
        FTS = file trailer segment
    """
    return line and line.strip()[:3] in ['FHS'] or False


def _process_cls_init(obj):
    """Set the following instance attributes with None or empty values
    (proper values will be set later at model creation):

    __xsimlab_model__ : obj
        :class:`Model` instance to which the process instance is attached.
    __xsimlab_name__ : str
        Name given for this process in the model.
    __xsimlab_state__ : dict or object
        Simulation active data store.
    __xsimlab_state_keys__ : dict
        Dictionary that maps variable names to their corresponding key
        (or list of keys for group variables) in the active store.
        Such keys consist of pairs like `('foo', 'bar')` where
        'foo' is the name of any process in the same model and 'bar' is
        the name of a variable declared in that process.
    __xsimlab_od_keys__ : dict
        Dictionary that maps variable names to the location of their target
        on-demand variable (or a list of locations for group variables).
        Locations are tuples like state keys.

    """
    obj.__xsimlab_model__ = None
    obj.__xsimlab_name__ = None
    obj.__xsimlab_state__ = None
    obj.__xsimlab_state_keys__ = {}
    obj.__xsimlab_od_keys__ = {}


@classmethod
def _cache_key(cls, *args, **kwargs):
    """An AbstractFunction caches on the class type itself."""
    return cls


@staticmethod
def homogenize_types(df, groupby_cols):
    """Converting all GROUPBY columns to strings

        When grouping by a numeric (say FLOAT) column, pydruid returns
        strings in the dataframe. This creates issues downstream related
        to having mixed types in the dataframe

        Here we replace None with <NULL> and make the whole series a
        str instead of an object.
        """
    for col in groupby_cols:
        df[col] = df[col].fillna('<NULL>').astype('unicode')
    return df


@classmethod
def from_args(cls, args):
    """Constructs an Indexer instance from given arguments.

        Instantiates an Indexer instance from the given argparse arguments.
        Uses the ``_parse_args`` method internally, which performs the actual
        argument-to-parameter extraction. As such, the ``_parse_args`` method
        should be overridden in any subclasses with extra parameters.

        Parameters
        ----------
        args : argparse.Namespace
            Arguments to parse.
        """
    return cls(**cls._parse_args(args))


@classmethod
def store_db_obj(cls, in_obj, out_obj):
    """Store the IP DB object. """
    cls.ip_db_obj['in'] = in_obj
    cls.ip_db_obj['out'] = out_obj


def _db_edition_table_format(editions):
    """
    Formats a database edition as summary results for display with "-o table".
    """
    from .custom import is_available
    from collections import OrderedDict
    for e in editions:
        for slo in e['supportedServiceLevelObjectives']:
            sku = slo['sku']
            yield OrderedDict([('serviceObjective', slo['name']), ('sku',
                sku['name']), ('edition', e['name']), ('family', sku[
                'family'] or ' '), ('capacity', sku['capacity']), ('unit',
                slo['performanceLevel']['unit']), ('available',
                is_available(slo['status']))])


@classmethod
def replace_graph(cls, response, serialized):
    """ Replace the rdflib Graph in a view response """
    if cls.is_graph(response):
        return serialized
    return response


def is_root(dmrs, nodeid):
    """
    Check if a node has no incoming links
    """
    return not any(dmrs.get_in(nodeid, itr=True))


@staticmethod
def p_nodes_list_optional(p):
    """nodes_list_optional : empty
                               | nodes_list"""
    p[0] = p[1]


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

        :param BinderRenamePageDetails val:
        :rtype: EventDetails
        """
    return cls('binder_rename_page_details', val)


def __init__(_, mode):
    """
        mode - running mode of this batch. Map containing:
        = {
          incremental - boolean - whether to avoid updating data that did not change (optimization)
          order - string - order specification, limiting scripts to run
          dry_run - boolean - if set, don't execute statements for real
          display - boolean - display more information
          warn - boolean - display warnings (supressed by default)
          rewind - boolean - run in rewind mode
          threads - int - number of threads for parallel processing
          }
        """
    _.mode = {'incremental': False, 'display': False, 'warn': False,
        'dry_run': False, 'rewind': False, 'threads': 1}
    _.mode.update(mode)
    _.sources = []
    _.destination = None
    _.scripts = []


def get_variables(options):
    """
    Read all variables and values from ``-Dvariable=value`` options
    """
    split_eq = lambda x: x.split('=')
    data = dict(map(split_eq, options))
    return data


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


def sort_a_list_of_words(words, dstring_object):
    """
        sort_a_list_of_words function :

        * words : iterable of (unicode) words, like ["Μῆνιν", "ἄειδε", ...]
        * dstring_object, DSTRING object, like new_dstring(language="grc")

        Return an object whose type is type(words), sorted.
    """
    dstring_words = map(dstring_object, words)
    sorted_words = sorted(dstring_words, key=dstring_object.sortingvalue)
    return type(words)(map(dstring_object.__str__, sorted_words))


def array_with_new_shape(array, new_shape):
    """Resize an input array around its centre to a new input shape.

    If a new_shape dimension is smaller than the array's current dimension, the data at the edges is trimmed and
    removedd. If it is larger, the data is padded with zeros.

    If the array has even sized dimensions, the central pixel around which data is trimmed / padded is chosen as
    the top-left pixel of the central quadrant of pixels.

    Parameters
    -----------
    array : aa.Array
        The array which is trimmed / padded to a new 2D shape.
    new_shape : (int, int)
        The new 2D shape of the array.
    """
    return array.resized_from_new_shape(new_shape=new_shape)


def decode_ascii(s):
    """
    In Python 2 this is a no-op.  Strings are left alone.  In Python 3 this
    will be replaced with a function that actually decodes ascii bytes to
    unicode.
    """
    return s


def extend_adaptation_space(space, parameter, states):
    """
    Add set of states to an adaptation space for a given parameter.
    If the parameter is not already defined, add it to the space.
    Otherwise, extend the already defined space

    :param space: (out) Space to update
    :type space: dict[Parameter, set]
    :type parameter: Parameter
    :type states: set
    :return: Passed space with updated set
    """
    parameter_space = space.get(parameter, None)
    if parameter_space:
        parameter_space.update(states)
    else:
        space[parameter] = states.copy()
    return space


def nt_error(status):
    """
    Evaluates to TRUE if the return value specified by Status is an error
    type (0xC0000000 - 0xFFFFFFFF).
    """
    return 3221225472 <= status <= 4294967295


def check_if_domain_exists(name, dtypes):
    """Check if a domain already exists.

    We not only look for domains, we also look for every object that
    could conflict with a domain (domain alias, etc.)

    """
    for dtype, label in dtypes:
        if dtype.objects.filter(name=name).exists():
            return label
    return None


def dna2prot(gx, loc=[]):
    """
    Computes the protein coordinate based on a genomic coordinate and potentially overlapping locations.
    gx: zero based genomic coordinate
    loc: a list of (start, end) pairs
    """
    nt = 0
    ns = ''
    px = []
    for start, end in loc:
        if start <= gx < end:
            nt = nt + (gx - start)
            div, mod = divmod(nt, 3)
            px.append((gx, nt, div, mod))
        else:
            nt += end - start
    return px


def _flush_pdf():
    """ Generates a PDF using the current layout as the template and
            returns the chunk of bytes.
        """
    return ''


def _is_flag_group(obj):
    """Returns `True` if `obj` is an `h5py.Group` that looks like
    if contains a flag
    """
    import h5py
    return isinstance(obj, h5py.Group) and isinstance(obj.get('active'),
        h5py.Dataset) and isinstance(obj.get('known'), h5py.Dataset)


def zipper_merge(*lists):
    """
    Combines lists by alternating elements from them.

    Combining lists [1,2,3], ['a','b','c'] and [42,666,99] results in
    [1,'a',42,2,'b',666,3,'c',99]

    The lists should have equal length or they are assumed to have the length of
    the shortest list.

    This is known as alternating merge or zipper merge.
    """
    return list(sum(zip(*lists), ()))


def oddPeak(indx, peaks):
    """needs a doc string"""
    noOdd = True
    for peak in peaks:
        H = peak[4:7]
        if H[indx] % 2:
            noOdd = False
    return noOdd


def update_a_cluster(base, cluster_id, profile_id=None, name=None,
    expected_status='SUCCEEDED', metadata=None, timeout=None, wait_timeout=None
    ):
    """Utility function that updates a Senlin cluster.

    Update a cluster and return it after it is ACTIVE.
    """
    params = {'cluster': {'profile_id': profile_id, 'metadata': metadata,
        'name': name, 'timeout': timeout}}
    res = base.client.update_obj('clusters', cluster_id, params)
    action_id = res['location'].split('/actions/')[1]
    base.client.wait_for_status('actions', action_id, expected_status,
        wait_timeout)
    return res['body']


def triplets(iterable):
    """
    >>> list(triplets([1,2,3,4,5]))
    [(1, 2, 3), (2, 3, 4), (3, 4, 5)]
    """
    iterable = iter(iterable)
    backlog = next(iterable), next(iterable)
    for item in iterable:
        yield backlog[0], backlog[1], item
        backlog = backlog[1], item


def pause_toggle(realm):
    """Toggle the output screen's pausing."""
    output_window = realm.factory.gui.output_window
    if output_window.paused:
        output_window.unpause()
    else:
        output_window.pause()


def delete_cached_file(cache, filename):
    """Delete cached file if present.

    :param cache: dictionary to hold opaque cache.
    :param filename: filename to delete
    """
    try:
        del cache[filename]
    except KeyError:
        pass


@staticmethod
def _get_error_code(error_value):
    """Get the error code from the error_value accounting for Python
        version differences.

        :rtype: int

        """
    if not error_value:
        return None
    if hasattr(error_value, 'errno'):
        return error_value.errno
    else:
        return error_value[0]


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

        :param ShowcaseUnresolveCommentType val:
        :rtype: EventType
        """
    return cls('showcase_unresolve_comment', val)


def list_set(seq):
    """Similar to `list(set(seq))`, but maintains the order of `seq` while eliminating duplicates

    In general list(set(L)) will not have the same order as the original list.
    This is a list(set(L)) function that will preserve the order of L.

    Arguments:
      seq (iterable): list, tuple, or other iterable to be used to produce an ordered `set()`

    Returns:
      iterable: A copy of `seq` but with duplicates removed, and distinct elements in the same order as in `seq`

    Examples:
      >>> list_set([2.7,3,2,2,2,1,1,2,3,4,3,2,42,1])
      [2.7, 3, 2, 1, 4, 42]
      >>> list_set(['Zzz','abc', ('what.', 'ever.'), 0, 0.0, 'Zzz', 0.00, 'ABC'])
      ['Zzz', 'abc', ('what.', 'ever.'), 0, 'ABC']
    """
    new_list = []
    for i in seq:
        if i not in new_list:
            new_list += [i]
    return type(seq)(new_list)


def psi_Ca_HCO3_OH_HMW84(T, P):
    """c-a-a': calcium bicarbonate hydroxide [HMW84]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


def format_millisecond(millisecond, model='auto'):
    """
    将毫秒数格式化为时长字符串格式（毫秒数 -> 00:00:25.173格式）

    :param millisecond: 毫秒数
    :param model: 模式(默认auto:当小时为00时省略)
    :returns: 时长h:m:s.ms或m:s.ms格式字符串
    """
    ms = millisecond % 1000
    seconds = int(millisecond / 1000)
    s = int(seconds % 60)
    m = int(seconds // 60 % 60)
    h = int(seconds // 60 // 60)
    if model == 'auto' and h == 0:
        return '%02d:%02d.%d' % (m, s, ms)
    return '%02d:%02d:%02d.%d' % (h, m, s, ms)


def find_node(prev: bool=False):
    """Search for named node and allow to select and activate it

    :param prev: Previous
    :type prev: bool
    """
    pass


def generate_kv_list(features):
    """Render cvlm key-value pairs to text from a dictionary."""
    pieces = []
    for k, v in sorted(features.items()):
        if v == 1:
            pieces.append(str(k))
        else:
            pieces.append('%s=%s' % (k, v))
    return ' '.join(pieces)


def reg2bins(beg, end):
    """
    Calculate the list of bins that may overlap with region [beg,end) (zero-based)
    Adapted directly from SAM spec.
    :param beg:
    :param end:
    :return:
    """
    end -= 1
    bins = [0]
    for k in range(1 + (beg >> 26), 2 + (end >> 26)):
        bins.append(k)
    for k in range(9 + (beg >> 23), 10 + (end >> 23)):
        bins.append(k)
    for k in range(73 + (beg >> 20), 74 + (end >> 20)):
        bins.append(k)
    for k in range(585 + (beg >> 17), 586 + (end >> 17)):
        bins.append(k)
    for k in range(4681 + (beg >> 14), 4682 + (end >> 14)):
        bins.append(k)
    return bins


def suffix_kwd(name):
    """Add an underscore suffix if name if a Python keyword."""
    from keyword import iskeyword
    return '{}_'.format(name) if iskeyword(name) else name


@staticmethod
def get_list():
    """
        Gets the list of banks.

        Please note this method is a mapping from the paynlsdk2.client.transaction.Transaction.get_banks() method,
        that returns the internal List object of banks.
        :return: List of banks
        :rtype: List[BankDetails]
        """
    from paynlsdk2.client.transaction import Transaction
    return Transaction.get_banks()


def set_dotnotation(original: dict, dotnotation: str, value=None) ->dict:
    """Use dotted notation to create set values in a dict,
    e.g:
    >>> dotattr({}, 'some.path.in.dict', 'value')
    { "some": { "path": { "in": {"dict": "value" }}}}

    >>> dotattr({'credentials': {'username': 'test'}}, 'credentials.username')
    'test'
    """
    original = original or {}
    parts = dotnotation.split('.')
    current = original
    for part in parts:
        if part is parts[-1]:
            current[part] = value
        elif part not in current:
            current[part] = {}
        current = current[part]
    return original


def assert_type(objects, types, raise_error=False):
    """Assert object(s) from allowed types (instances).

    Parameters
    ----------
    objects :
        Single object or a list of objects
    types :
        Single type or a list of types.
    raise_error :
        Whether to raise error.
    Return:
    ----------
    Return boolean value if raise_error=False.
    """
    if not isinstance(objects, list):
        objects = [objects]
    if not isinstance(types, list):
        types = [types]
    for object in objects:
        if not any([isinstance(object, type) for type in types]):
            if raise_error:
                raise ValueError(
                    'Invalid input data type. List of allowed types: {}. Got {} instead'
                    .format(str(types)[1:-1], type(object)))
            else:
                return False
    return True


def array_reverse_order_transform_next_index_to_current_index(position, move):
    """Transforms the position depending on the move.

    Works with the array_swap move type.
    This function transforms the position so that it can be used as the indice
    in the unaltered array, yet return the value it would have had if the move
    was actually performed and the position was used as indice.

    Parameters
    ----------
    position : int
        The index that one wants to use in the array if the move was performed.
    move : tuple of int
        A tuple with that represents a single, unique move.

    Returns
    -------
    int
        The index in the unaltered array that has the same value as the
        location in an array where the move was performed.

    Examples
    --------
    Some simple examples, the move remains the same, but the position changes:

    .. doctest::

        >>> from lclpy.evaluation.deltaeval.delta_qap \\
        ...     import array_reverse_order_transform_next_index_to_current_index \\
        ...         as transform_next_index_to_current_index
        ... # tests
        >>> transform_next_index_to_current_index(0, (1, 4))
        0
        >>> transform_next_index_to_current_index(1, (1, 4))
        4
        >>> transform_next_index_to_current_index(2, (1, 4))
        3
        >>> transform_next_index_to_current_index(3, (1, 4))
        2
        >>> transform_next_index_to_current_index(4, (1, 4))
        1
        >>> transform_next_index_to_current_index(5, (1, 4))
        5

    """
    if (position >= move[0]) & (position <= move[1]):
        offset = position - move[0]
        position = move[1] - offset
    return position


@staticmethod
def write(writer, value):
    """
        Writes Zserio varint32 type to the bit stream.

        :param writer: Bit stream where to write.
        :param value: Zserio varint32 type to write.
        """
    writer.writeVarInt32(value)


def is_pending(item):
    """
        returns if item is in the pending list
        """


def _GetEmbedding(model, name):
    """Function to get embedding for users or items

    Params:
        model (keras model): Keras model which has embedding layer
        names (string): Name of the user or item embedding layer

    Returns:
        embedding (numpy matrix): Embedding layer for user or item
    """
    embedding = model.get_layer(name=name).get_weights()[0]
    return embedding


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

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

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


@classmethod
def from_int(cls, integer):
    """
        Constructs a `Deleted` using the `tinyint` value of the `rev_deleted`
        column of the `revision` MariaDB table.

        * DELETED_TEXT = 1
        * DELETED_COMMENT = 2
        * DELETED_USER = 4
        * DELETED_RESTRICTED = 8
        """
    bin_string = bin(integer)
    return cls(text=len(bin_string) >= 1 and bin_string[-1] == '1', comment
        =len(bin_string) >= 2 and bin_string[-2] == '1', user=len(
        bin_string) >= 3 and bin_string[-3] == '1', restricted=len(
        bin_string) >= 4 and bin_string[-4] == '1')


def _prefix(kindpats):
    """Whether all the patterns match a prefix (i.e. recursively)"""
    for kind, pat, source in kindpats:
        if kind not in (b'path', b'relpath'):
            return False
    return True


def select_nearest_point(target, points, threshold):
    """Select data point nearest to a given point in time.

    :param datetime.datetime target: the target timestamp for the point.
    :param points: an iterator of :class:`Point`s to search.
    :param float threshold: the maximum number of seconds the selected
        point can be away from the target timestamp.

    :raises ValueError: if the selected data point exceeds the threshold.

    :returns: the :class:`Point` nearest to ``target``.
    """
    sorted_points = sorted(points, key=lambda p: abs(target - p.timestamp))
    if not sorted_points:
        raise ValueError('No points given')
    if abs(target - sorted_points[0].timestamp).total_seconds() > threshold:
        raise ValueError('No metric point within {} seconds'.format(threshold))
    return sorted_points[0]


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 fix_carets(expr):
    """Converts carets to exponent symbol in string"""
    import re as _re
    caret = _re.compile('[\\^]')
    return caret.sub('**', expr)


def calculation_special_again(productListA, special_price, value_num):
    """
    用大于等于特价值/数量值的商品(非特例品) 计算公式值。 返回新的特价价格
    :param productListA:
    :param special_price:
    :param value_num:
    :return:
    """
    ge_specialvalue_qtty_sum = 0
    ge_specialvalue_amt_sum = 0
    small_specialvalue_amt_sum = 0
    for product in productListA:
        for seat in product.productSeatList:
            if seat.seat == False and seat.is_run_other_pro != False:
                if seat.is_discount == 'y':
                    if seat.amt_receivable >= special_price / value_num:
                        ge_specialvalue_amt_sum += seat.amt_receivable
                        ge_specialvalue_qtty_sum += seat.qtty
                    else:
                        small_specialvalue_amt_sum += seat.amt_receivable
                else:
                    small_specialvalue_amt_sum += seat.amt_receivable
    if ge_specialvalue_qtty_sum > 0:
        return (special_price / value_num * ge_specialvalue_qtty_sum +
            small_specialvalue_amt_sum)
    else:
        return special_price


@classmethod
def create(cls, index):
    """ Create a new index """
    return cls('Create', None, index=index)


@classmethod
def find(cls, key):
    """Look up user database entry"""
    res = cls.db.search(cls.model.single(key))
    try:
        [(dn, attrs)] = res
    except ValueError:
        return None
    return cls(dn, attrs)


@classmethod
def _clean_data(cls, *args, **kwargs):
    """
        Convert raw data into a dictionary with plot-type specific methods.

        The result of the cleaning operation should be a dictionary.
        If the dictionary contains a 'data' field it will be passed directly
        (ensuring appropriate formatting). Otherwise, it should be a
        dictionary of data-type specific array data (e.g. 'points',
        'timeseries'), which will be labeled appropriately
        (see _check_unkeyed_arrays).
        """
    datadict = cls.clean(*args, **kwargs)
    if 'data' in datadict:
        data = datadict['data']
        data = cls._ensure_dict_or_list(data)
    else:
        data = {}
        for key in datadict:
            if key == 'images':
                data[key] = datadict[key]
            else:
                d = cls._ensure_dict_or_list(datadict[key])
                data[key] = cls._check_unkeyed_arrays(key, d)
    return data


def remove_www(hostname):
    """
    Removes www. from the beginning of the address. Only for
    routing purposes. www.test.com/login/ and test.com/login/ should
    find the same tenant.
    """
    if hostname.startswith('www.'):
        return hostname[4:]
    return hostname


def strnbytes(nbytes):
    """
    Return number of bytes in a human readable unit of KiB, MiB or GiB.

    Parameter
    ---------
    nbytes: int
        Number of bytes, to be displayed in a human readable way.

    Example
    -------
    >>> a = np.empty((100,100))
    >>> strnbytes(a.nbytes)
    '78.125 KiB'

    """
    if nbytes < 1024:
        return str(nbytes) + ' bytes'
    elif nbytes < 1048576:
        return str(nbytes / 2 ** 10) + ' KiB'
    elif nbytes < 1073741824:
        return str(nbytes / 2 ** 20) + ' MiB'
    else:
        return str(nbytes / 2 ** 30) + ' GiB'


def fs_join(*args):
    """Like os.path.join, but never returns '' chars"""
    from os.path import join
    return join(*args).replace('\\', '/')


def download():
    """Downloads the file at the specified path"""
    raise NotImplementedError


@classmethod
def get_by_userid(cls, user_id):
    """Get profile by user identifier.

        :param user_id: Identifier of a :class:`~invenio_accounts.models.User`.
        :returns: A :class:`~invenio_userprofiles.models.UserProfile` instance
            or ``None``.
        """
    return cls.query.filter_by(user_id=user_id).one_or_none()


@staticmethod
def unpad(data):
    """ Un-Padding for data

        :param data: Data to be un-padded
        :return: Data with removed padding """
    p = None
    for x in data[::-1]:
        if x is 0:
            continue
        elif x is not 0:
            p = x
            break
    data = data[::-1]
    data = data[p:]
    return data[::-1]


def sub_sample(pointcloud, n=100):
    """ Function to randomly subsample pointcloud to n
        number of points. If n is larger than pointcloud
        None is returned.

        args:
            pointcloud : pandas dataframe
            n          : int number of point to return
        returns:
            pandas dataframe
    """
    n_greater = False
    if pointcloud.values.shape[0] > n:
        pointcloud = pointcloud.sample(n=n)
    elif pointcloud.values.shape[0] < n:
        n_greater = True
    if n_greater == True:
        pointcloud = None
    return pointcloud


def filter_none(attribute, default):
    """Handle attributes of model components that are optional in SBML."""
    if attribute is None:
        return default
    else:
        return attribute


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

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


def _slotnames(cls):
    """Return a list of slot names for a given class.

    This needs to find slots defined by the class and its bases, so we
    can't simply return the __slots__ attribute.  We must walk down
    the Method Resolution Order and concatenate the __slots__ of each
    class found there.  (This assumes classes don't modify their
    __slots__ attribute to misrepresent their slots after the class is
    defined.)
    """
    names = cls.__dict__.get('__slotnames__')
    if names is not None:
        return names
    names = []
    if not hasattr(cls, '__slots__'):
        pass
    else:
        for c in cls.__mro__:
            if '__slots__' in c.__dict__:
                slots = c.__dict__['__slots__']
                if isinstance(slots, str):
                    slots = slots,
                for name in slots:
                    if name in ('__dict__', '__weakref__'):
                        continue
                    elif name.startswith('__') and not name.endswith('__'):
                        stripped = c.__name__.lstrip('_')
                        if stripped:
                            names.append('_%s%s' % (stripped, name))
                        else:
                            names.append(name)
                    else:
                        names.append(name)
    try:
        cls.__slotnames__ = names
    except:
        pass
    return names


@classmethod
def get_selected_path(cls, parse_result):
    """
        This method returns the path to the selected node as a `/` seperated string.

        :param argparse.Namespace parse_result: A namespace object returned by
            :py:meth:`argparse.ArgumentParser.parse_args`.
        :rtype: str
        """
    path_list = cls.get_selected_path_list(parse_result)
    return '/'.join(path_list)


def always(_):
    """Predicate function that returns ``True`` always.

  :param _:
      Argument
  :returns:
      ``True``.
  """
    return True


def globally_show_history():
    """If false, never show history link
        """


def _get_size_url(**kwargs):
    """Get url for QCEW data by size.

    Note that size data is only available for the first quarter of each year
    Parameters
    ----------
    year : string
        'YYY'.
    size : string
        For all establishment size classes and titles see:
        http://www.bls.gov/cew/doc/titles/size/size_titles.htm

    Returns
    -------
    string
        URL that will be used for main function.

    """
    for arg in ['year', 'size']:
        if arg not in kwargs:
            raise ValueError('Must specify {}'.format(arg))
    url = 'http://data.bls.gov/cew/data/api/[YEAR]/1/size/[SIZE].csv'
    url = url.replace('[YEAR]', kwargs.get('year'))
    url = url.replace('[SIZE]', kwargs.get('size'))
    return url


@staticmethod
def down(volumes=False):
    """
        Returns a docker-compose 'down' command string.

        :param volumes: Remove volumes
        :return:
        """
    return 'docker-compose down {}'.format('-v' if volumes else '')


def word_count(text):
    """ The word-count of the given text. Goes through the string exactly
    once and has constant memory usage. Not super sophisticated though.

    """
    if not text:
        return 0
    count = 0
    inside_word = False
    for char in text:
        if char.isspace():
            inside_word = False
        elif not inside_word:
            count += 1
            inside_word = True
    return count


def getCurrencySymbol():
    """returns the currency symbol"""


def _register_instance(cls, instance):
    """
        registers the given instance.
        """
    cls._instance = instance


def pack_ipv6(ipv6address):
    """
    Packs an IPv4 address.
    """
    yield ipv6address.packed


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

        :param TeamMergeRequestCanceledType val:
        :rtype: EventType
        """
    return cls('team_merge_request_canceled', val)


def get_brats_regions():
    """
    this is only valid for the brats data in here where the labels are 1, 2, and 3. The original brats data have a
    different labeling convention!
    :return:
    """
    regions = {'whole tumor': (1, 2, 3), 'tumor core': (2, 3),
        'enhancing tumor': (3,)}
    return regions


def replace_indexes(listy, idxs_to_replace, new_values):
    """
    Remove given indexes and insert a new set of values into the given list.
    """
    for idx in reversed(sorted(idxs_to_replace)):
        del listy[idx]
    idx_to_add = min(idxs_to_replace)
    for value in reversed(new_values):
        listy.insert(idx_to_add, value)


def generic_find_fk_constraint_names(table, columns, referenced, insp):
    """Utility to find foreign-key constraint names in alembic migrations"""
    names = set()
    for fk in insp.get_foreign_keys(table):
        if fk['referred_table'] == referenced and set(fk['referred_columns']
            ) == columns:
            names.add(fk['name'])
    return names


def untag(item_type, item_id, user_id, *tags):
    """User <user_id> removes tags <*tags> from <item_type> <item_id>"""


def inner_plaintext_printer(func):
    """
    mark a method as plaintext printer
    """
    func.__plaintext_printer__ = True
    return func


def set_wireframe(actor):
    """
    set an actor's look to wireframe

    :param actor:

    """
    actor.wireframe(value=True)


def constraints_copy():
    """Copy constraints to other selected objects

    """
    pass


def _name(cls: type, function) ->str:
    """Return the name of a function accessed through a descriptor."""
    return function.__get__(None, cls).__name__


def quote_spaces(arg):
    """Generic function for putting double quotes around any string that
    has white space in it."""
    if ' ' in arg or '\t' in arg:
        return '"%s"' % arg
    else:
        return str(arg)


def find_set_selected():
    """Find specified text and set as selected

    """
    pass


def hide_render_clear_all():
    """Reveal all render objects by setting the hide render flag

    """
    pass


@classmethod
def _get_base_vectors(cls, base):
    """Get unit vectors and scale factors from (unit)spherical base.

        Parameters
        ----------
        base : instance of ``self.base_representation``
            The points for which the unit vectors and scale factors should be
            retrieved.

        Returns
        -------
        unit_vectors : dict of `CartesianRepresentation`
            In the directions of the coordinates of base.
        scale_factors : dict of `~astropy.units.Quantity`
            Scale factors for each of the coordinates.  The scale factor for
            longitude does not include the cos(lat) factor.

        Raises
        ------
        TypeError : if the base is not of the correct type
        """
    cls._check_base(base)
    return base.unit_vectors(), base.scale_factors(omit_coslat=True)


@staticmethod
def to_embedded(name=None, description=None, netbios_name=None, domain=None,
    workgroup=None, is_standalone=None, cifs_username=None):
    """
        Constructs an embeded object of `UnityCifsServerInfo`.

        :param name: User-specified name for the SMB server.
        :param description: Description of the SMB server.
        :param netbios_name: Computer Name of the SMB server in Windows
            network.
        :param domain: Domain name where SMB server is registered in
            Active Directory, if applicable.
        :param workgroup: (Applies to stand-alone SMB servers.) Windows
            network workgroup for the SMB server.
        :param is_standalone: Indicates whether the SMB server is standalone.
            Values are:
                true - SMB server is standalone.
                false - SMB server is joined to the Active Directory.
        :param cifs_username: User name for authentication to CIFS server
            on the source VDM. it could be the same as Active Directory
            domain user name in some cases, and which will be used
            initialized to the Active Directory domain user name in GUI side.
        :return:
        """
    return {'name': name, 'description': description, 'netbiosName':
        netbios_name, 'domain': domain, 'workgroup': workgroup,
        'isStandalone': is_standalone, 'cifsUsername': cifs_username}


def __init__(A):
    """ResourceId - a model defined in Swagger"""
    A.discriminator = None


def keepsaccade(i, j, sim_lenx, sim_leny, sim_x, sim_y, sim_theta, sim_len,
    sim_dur, data):
    """
    Helper function for scanpath simplification. If no simplification can be
    performed on a particular saccade, this functions stores the original data.
    :param i: current index
    :param j: current index
    :param sim_lenx: list
    :param sim_leny: list
    :param sim_x: list
    :param sim_y: list
    :param sim_theta: list
    :param sim_len: list
    :param sim_dur: list
    :param data: eyedata, list of list
    """
    sim_lenx.insert(j, data['saccade_lenx'][i])
    sim_leny.insert(j, data['saccade_leny'][i])
    sim_x.insert(j, data['saccade_x'][i])
    sim_y.insert(j, data['saccade_y'][i])
    sim_theta.insert(j, data['saccade_theta'][i])
    sim_len.insert(j, data['saccade_rho'][i])
    sim_dur.insert(j, data['fixation_dur'][i])
    i += 1
    j += 1
    return sim_lenx, sim_leny, sim_x, sim_y, sim_theta, sim_len, sim_dur, i, j


def add_dose_time_to_mtz(hklin, hklout, doses, times=None):
    """Add doses and times from dictionaries doses, times (optional)
    to hklin to produce hklout. The dictionaries are indexed by the
    BATCH column in hklin. Will raise exception if no BATCH column."""
    from iotbx import mtz
    from cctbx.array_family import flex
    mtz_obj = mtz.object(file_name=hklin)
    batch_column = None
    batch_dataset = None
    for crystal in mtz_obj.crystals():
        for dataset in crystal.datasets():
            for column in dataset.columns():
                if column.label() == 'BATCH':
                    batch_column = column
                    batch_dataset = dataset
    if not batch_column:
        raise RuntimeError('no BATCH column found in %s' % hklin)
    batch_column_values = batch_column.extract_values(not_a_number_substitute
        =-1)
    dose_column = batch_dataset.add_column(label='DOSE', type='R')
    dose_column_values = flex.float()
    if times:
        time_column = batch_dataset.add_column(label='TIME', type='R')
        time_column_values = flex.float()
    valid = flex.bool()
    for b in batch_column_values:
        valid.append(True)
        dose_column_values.append(doses.get(b, -1.0))
        if times:
            time_column_values.append(times.get(b, -1.0))
    dose_column.set_values(values=dose_column_values, selection_valid=valid)
    if times:
        time_column.set_values(values=time_column_values, selection_valid=valid
            )
    mtz_obj.write(file_name=hklout)


def make_int32(v):
    """Converts (truncates, two's compliments) a number to an int32."""
    if v > 2147483647:
        return -1 * ((~v & 4294967295) + 1)
    return v


@classmethod
def handle_response(cls, handler, payload):
    """JSON v1 response handler.  Both 'error' and 'result'
        should exist with exactly one being None.
        Unfortunately many 1.0 clients behave like 2.0, and just send
        one or the other.
        """
    error = payload.get('error')
    if error is None:
        handler(payload.get('result'), None)
    else:
        handler(None, cls.canonical_error(error))


@staticmethod
def GetFunctionText(heading, name):
    """Returns the needed text to automatically document a function in RSF/sphinx"""
    und = '-' * len(heading)
    return '\n\n%s\n%s\n\n.. autofunction:: %s\n\n' % (heading, und, name)


def get_editable():
    """Get the editable version (may be object itself). Returns
        ``None`` if there is no such version.
        """


def validate_old_rho_data(old_dict, FS):
    """ check if the old hashes match the new ones,
    otherwise we have to throw away all the old data
    """
    if 'hash_vib' not in old_dict or 'hash_rho' not in old_dict:
        old_dict.clear()
    elif old_dict['hash_vib'] != FS.hash_vib or old_dict['hash_rho'
        ] != FS.hash_rho:
        old_dict.clear()
    old_dict['hash_vib'] = FS.hash_vib
    old_dict['hash_rho'] = FS.hash_rho
    return


@classmethod
def from_string(cls, string):
    """Create a ShaFile from a string."""
    obj = cls()
    obj.set_raw_string(string)
    return obj


def group_remove():
    """Remove the active bone group

    """
    pass


def add_host_config_params_dns(docker_host, host_config_params=None):
    """Add dns input params

    This is not a generic implementation. This method will setup dns with the
    expectation that a local consul agent is running on the docker host and will
    service the dns requests.

    Args:
    -----
    docker_host (string): Docker host ip address which will be used as the dns server
    host_config_params (dict): Target dict to accumulate host config inputs

    Returns:
    --------
    Updated host_config_params
    """
    if host_config_params == None:
        host_config_params = {}
    host_config_params['dns'] = [docker_host]
    host_config_params['dns_search'] = ['service.consul']
    host_config_params['extra_hosts'] = {'consul': docker_host}
    return host_config_params


def pop_pseudo_headers(entries):
    """Remove and return HTTP/2 `pseudo-headers`__ from a list of headers.

    __ https://tools.ietf.org/html/rfc7540#section-8.1.2.1

    :param entries:
        A list of header name-value pairs,
        as would be passed to :class:`httpolice.Request`
        or :class:`httpolice.Response`.
        It will be modified in-place by removing all names
        that start with a colon (:).
    :return: A dictionary of the removed pseudo-headers.
    """
    i = 0
    r = {}
    while i < len(entries):
        name, value = entries[i]
        if name.startswith(u':'):
            r[name] = value
            del entries[i]
        else:
            i += 1
    return r


def lerp(start, stop, amount):
    """Linearly interpolate the start value to the stop value.

    Examples ::

        >>> lerp(0, 10, 0.0)
        0.0

        >>> lerp(0, 10, 0.5)
        5.0

        >>> lerp(0, 10, 0.8)
        8.0

        >>> lerp(0, 10, 1.0)
        10.0

    :param start: The start value

    :param stop: The stop value

    :param amount: The amount by which to interpolate. (:math:`0 \\leq
        amount \\leq 1`).
    :type amount: float

    """
    return start + amount * (stop - start)


def list_bonus_payments(HITId=None, AssignmentId=None, NextToken=None,
    MaxResults=None):
    """
    The ListBonusPayments operation retrieves the amounts of bonuses you have paid to Workers for a given HIT or assignment.
    See also: AWS API Documentation

    :example: response = client.list_bonus_payments(
        HITId='string',
        AssignmentId='string',
        NextToken='string',
        MaxResults=123
    )

    :type HITId: string
    :param HITId: The ID of the HIT associated with the bonus payments to retrieve. If not specified, all bonus payments for all assignments for the given HIT are returned. Either the HITId parameter or the AssignmentId parameter must be specified

    :type AssignmentId: string
    :param AssignmentId: The ID of the assignment associated with the bonus payments to retrieve. If specified, only bonus payments for the given assignment are returned. Either the HITId parameter or the AssignmentId parameter must be specified

    :type NextToken: string
    :param NextToken: Pagination token

    :type MaxResults: integer
    :param MaxResults:

    :rtype: dict
    :return: {
        'NumResults': 123,
        'NextToken': 'string',
        'BonusPayments': [
            {
                'WorkerId': 'string',
                'BonusAmount': 'string',
                'AssignmentId': 'string',
                'Reason': 'string',
                'GrantTime': datetime(2015, 1, 1)
            },
        ]
    }

    """
    pass


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


def handles_response_head(request_func):
    """
    This function decorator may be used to mark a method as usable for
    intercepting response head content.

    handles_response_head will accept an HttpResponse object and implement
    the logic that will define the FilterActions to be applied
    to the request
    """
    request_func._handles_response_head = True
    return request_func


def get_class_name(form_db_name, grid_db_name):
    """ makes form db_name uppercase + grid db_name """
    capital_form_db_name = form_db_name[0].upper() + form_db_name[1:]
    class_name = capital_form_db_name + grid_db_name
    return class_name


@classmethod
def hline(cls, x=None, y=None, char='-', n=None):
    """
        Insert a horizontal line at position, at most n characters or width of
        window.

        :param x: optional x value
        :param y: optional y value
        :param char: the character to print, default '-'
        :param n: the number of characters, default rest of width of window
        """
    x, y = cls._fix_xy(x, y)
    n = cls.WIDTH if n is None else n
    return cls.WINDOW.hline(y, x, char, n)


def _cat_format(dimension, x, _):
    """Categorical axis tick formatter function.  Returns the name of category
    `x` in `dimension`.  Used with `matplotlib.ticker.FuncFormatter`."""
    return str(dimension.categories[int(x)])


def cross_vectors_xy(u, v):
    """Compute the cross product of two vectors, assuming they lie in the XY-plane.

    Parameters
    ----------
    u : sequence of float
        XY(Z) coordinates of the first vector.
    v : sequence of float
        XY(Z) coordinates of the second vector.

    Returns
    -------
    list
        The cross product of the two vectors.
        This vector will be perpendicular to the XY plane.

    Examples
    --------
    >>> cross_vectors_xy([1.0, 0.0], [0.0, 1.0])
    [0.0, 0.0, 1.0]

    >>> cross_vectors_xy([1.0, 0.0, 0.0], [0.0, 1.0, 0.0])
    [0.0, 0.0, 1.0]

    >>> cross_vectors_xy([1.0, 0.0, 1.0], [0.0, 1.0, 1.0])
    [0.0, 0.0, 1.0]

    """
    return [0.0, 0.0, u[0] * v[1] - u[1] * v[0]]


def list_ip_sets(NextMarker=None, Limit=None):
    """
    Returns an array of  IPSetSummary objects in the response.
    See also: AWS API Documentation

    Examples
    The following example returns an array of up to 100 IP match sets.
    Expected Output:

    :example: response = client.list_ip_sets(
        NextMarker='string',
        Limit=123
    )

    :type NextMarker: string
    :param NextMarker: If you specify a value for Limit and you have more IPSets than the value of Limit , AWS WAF returns a NextMarker value in the response that allows you to list another group of IPSets . For the second and subsequent ListIPSets requests, specify the value of NextMarker from the previous response to get information about another batch of ByteMatchSets .

    :type Limit: integer
    :param Limit: Specifies the number of IPSet objects that you want AWS WAF to return for this request. If you have more IPSet objects than the number you specify for Limit , the response includes a NextMarker value that you can use to get another batch of IPSet objects.

    :rtype: dict
    :return: {
        'NextMarker': 'string',
        'IPSets': [
            {
                'IPSetId': 'string',
                'Name': 'string'
            },
        ]
    }

    """
    pass


def set_icon(main):
    """
    Set weather icon
    :param main: weather status
    :type main: str
    :return: icon
    :rtype: str
    """
    data = {'Clear': '☀', 'Clouds': '☁', 'Rain': '☂', 'Snow': '❄',
        'Thunderstorm': '☈', 'Drizzle': '☃', 'Extreme': '⚠'}
    try:
        return data[main]
    except KeyError:
        return ''


def efficiency_capacity_demand_difference(slots, events, X, **kwargs):
    """
    A function that calculates the total difference between demand for an event
    and the slot capacity it is scheduled in.
    """
    overflow = 0
    for row, event in enumerate(events):
        for col, slot in enumerate(slots):
            overflow += (event.demand - slot.capacity) * X[row, col]
    return overflow


def is_leap(year):
    """Determine whether this is a leap year."""
    return year % 4 == 3 or year % 4 == -1


@staticmethod
def p_record_layout_optional(p):
    """record_layout_optional : fnc_values
                                  | identification
                                  | axis_pts_x
                                  | axis_pts_y
                                  | axis_pts_z
                                  | axis_rescale_x
                                  | axis_rescale_y
                                  | axis_rescale_z
                                  | no_axis_pts_x
                                  | no_axis_pts_y
                                  | no_axis_pts_z
                                  | no_rescale_x
                                  | no_rescale_y
                                  | no_rescale_z
                                  | fix_no_axis_pts_x
                                  | fix_no_axis_pts_y
                                  | fix_no_axis_pts_z
                                  | src_addr_x
                                  | src_addr_y
                                  | src_addr_z
                                  | rip_addr_x
                                  | rip_addr_y
                                  | rip_addr_z
                                  | rip_addr_w
                                  | shift_op_x
                                  | shift_op_y
                                  | shift_op_z
                                  | offset_x
                                  | offset_y
                                  | offset_z
                                  | dist_op_x
                                  | dist_op_y
                                  | dist_op_z
                                  | alignment_byte
                                  | alignment_word
                                  | alignment_long
                                  | alignment_float32_ieee
                                  | alignment_float64_ieee
                                  | reserved"""
    p[0] = p.slice[1].type, p[1]


def client_host(server_host):
    """Return the host on which a client can connect to the given listener."""
    if server_host == '0.0.0.0':
        return '127.0.0.1'
    if server_host in ('::', '::0', '::0.0.0.0'):
        return '::1'
    return server_host


def _append_example_to_data(data, example):
    """Append the given example to the data dictionary."""
    example_id = example['id']
    patient_id = example['patient']
    if patient_id in data:
        if example_id in data[example['patient']]:
            assert example_id == data[patient_id][example_id]['id']
            assert patient_id == data[patient_id][example_id]['patient']
            assert example['breast'] == data[patient_id][example_id]['breast']
            assert example['image'] == data[patient_id][example_id]['image']
            assert example['view'] == data[patient_id][example_id]['view']
            data[patient_id][example_id]['abnormalities'].extend(example[
                'abnormalities'])
        else:
            data[example['patient']][example['id']] = example
    else:
        data[example['patient']] = {example['id']: example}


def def_infix_op(parser, operator_token_label, prec, assoc, not_in_tree=
    False, precond_fun=None, precond_priority=0, construct_label=None,
    val_type=None, arg_types=None, eval_fun=None, ast_data=None):
    """This just calls the more general method `def_multi_infix_op`."""
    return parser.def_infix_multi_op([operator_token_label], prec, assoc,
        not_in_tree=not_in_tree, precond_fun=precond_fun, precond_priority=
        precond_priority, construct_label=construct_label, val_type=
        val_type, arg_types=arg_types, eval_fun=eval_fun, ast_data=ast_data)


def abstractMethod():
    """ This should be called when an abstract method is called that should have been
    implemented by a subclass. It should not be called in situations where no implementation
    (i.e. a 'pass' behavior) is acceptable. """
    raise NotImplementedError('Method not implemented!')


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

        :param PasswordStrengthRequirementsChangePolicyType val:
        :rtype: EventType
        """
    return cls('password_strength_requirements_change_policy', val)


def retain(data=None, indices=None, out=None, name=None, **kwargs):
    """Pick rows specified by user input index array from a row sparse matrix
    and save them in the output sparse matrix.

    Example::

      data = [[1, 2], [3, 4], [5, 6]]
      indices = [0, 1, 3]
      shape = (4, 2)
      rsp_in = row_sparse_array(data, indices)
      to_retain = [0, 3]
      rsp_out = retain(rsp_in, to_retain)
      rsp_out.data = [[1, 2], [5, 6]]
      rsp_out.indices = [0, 3]

    The storage type of ``retain`` output depends on storage types of inputs

    - retain(row_sparse, default) = row_sparse
    - otherwise, ``retain`` is not supported

    Defined in src/operator/tensor/sparse_retain.cc:L53

    Parameters
    ----------
    data : NDArray
        The input array for sparse_retain operator.
    indices : NDArray
        The index array of rows ids that will be retained.

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

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


def __ne__(A, other):
    """Returns true if both objects are not equal"""
    return not A == other


@classmethod
def removeConverter(cls, conv):
    """Remove converter conv from the list of converters registered in
        cls.

        Raises ValueError if the converter is not present."""
    cls._converters.remove(conv)


def paste_tile(bg, fg):
    """Pastes multiple copies of fg onto bg in a tiled pattern."""
    nw = bg.size[0] / fg.size[0] + 1
    nh = bg.size[1] / fg.size[1] + 1
    for t in range(nh):
        for l in range(nw):
            bg.paste(fg, (l * fg.size[0], t * fg.size[1]))
    return bg


def exact_div(p, d, allow_divzero=False):
    """Find and return an integer n such that p == n * d

    If no such integer exists, this function raises ValueError.

    Both operands must be integers.

    If the second operand is zero, this function will raise ZeroDivisionError
    unless allow_divzero is true (default: False).
    """
    if not isinstance(p, int) or not isinstance(d, int):
        raise TypeError('unsupported operand type(s): %r and %r' % (type(p)
            .__name__, type(d).__name__))
    if d == 0 and allow_divzero:
        n = 0
        if p != n * d:
            raise ValueError('No solution could be found')
    else:
        n, r = divmod(p, d)
        if r != 0:
            raise ValueError('No solution could be found')
    assert p == n * d
    return n


def read_extra_files(cfg):
    """Read the list of extra files from the config file.

    Returns a list of 2-tuples: (file, destination_directory), which can be
    passed as the ``extra_files`` parameter to :class:`nsist.InstallerBuilder`.
    """
    lines = cfg.get('Include', 'files', fallback='').splitlines()
    pairs = []
    for line in lines:
        if '>' in line:
            file, dest = line.rsplit('>', 1)
            pairs.append((file.strip(), dest.strip()))
        else:
            pairs.append((line, '$INSTDIR'))
    return pairs


def assemble_api_url(domain, operators, protocol='https'):
    """Assemble the requests api url."""
    return '{}://{}{}'.format(protocol, domain, operators)


def get_fields(fields, label):
    """ 搜索DICT中的label 获取值"""
    for res_dict in fields:
        if res_dict['label'] == label:
            return res_dict['value']
    return ''


def value_length(tokens: list):
    """Calculates string length from given tokens"""
    l = 0
    for token in tokens:
        l += len(token) if isinstance(token, str) else len(token.name)
    return l


def _parse_refraction(line, lines):
    """Parse Energy [eV]  ref_ind_xx    ref_ind_zz    extinct_xx    extinct_zz"""
    split_line = line.split()
    energy = float(split_line[0])
    ref_ind_xx = float(split_line[1])
    ref_ind_zz = float(split_line[2])
    extinct_xx = float(split_line[3])
    extinct_zz = float(split_line[4])
    return {'energy': energy, 'ref_ind_xx': ref_ind_xx, 'ref_ind_zz':
        ref_ind_zz, 'extinct_xx': extinct_xx, 'extinct_zz': extinct_zz}


def set_prefs(prefs):
    """This function is called before opening the project"""
    prefs['ignored_resources'] = ['*.pyc', '*~', '.ropeproject', '.hg',
        '.svn', '_svn', '.git', '__pycache__', '.tox', '.env',
        'node_modules', 'bower_components']
    prefs['save_objectdb'] = True
    prefs['compress_objectdb'] = False
    prefs['automatic_soa'] = True
    prefs['soa_followed_calls'] = 0
    prefs['perform_doa'] = True
    prefs['validate_objectdb'] = True
    prefs['max_history_items'] = 32
    prefs['save_history'] = True
    prefs['compress_history'] = False
    prefs['indent_size'] = 4
    prefs['extension_modules'] = []
    prefs['import_dynload_stdmods'] = True
    prefs['ignore_syntax_errors'] = False
    prefs['ignore_bad_imports'] = False


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

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

        The shared secret is generated such that::

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

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


def mag(c):
    """Return magnitude of a Number or Quantity"""
    return getattr(c, 'magnitude', c)


def write(packet, addr=None):
    """
        Write packet to given address.

        @param addr: a tuple of (ip, port). For connected transports must
                     be the address the transport is connected to, or None.
                     In non-connected mode this is mandatory.

        @raise twisted.internet.error.MessageLengthError: C{packet} was too
        long.
        """


@staticmethod
def build_sub_expression(name, ne_ref=None, operator='union'):
    """
        Static method to build and return the proper json for a sub-expression.
        A sub-expression would be the grouping of network elements used as a
        target match. For example, (network A or network B) would be considered
        a sub-expression. This can be used to compound sub-expressions before
        calling create.

        :param str name: name of sub-expression
        :param list ne_ref: network elements references
        :param str operator: exclusion (negation), union, intersection (default: union)
        :return: JSON of subexpression. Use in :func:`~create` constructor
        """
    ne_ref = [] if ne_ref is None else ne_ref
    json = {'name': name, 'ne_ref': ne_ref, 'operator': operator}
    return json


def to_conllu(passage, test=False, enhanced=True, preprocess=True, **kwargs):
    """ Convert from a Passage object to a string in Universal Dependencies format (conllu)

    :param passage: the Passage object to convert
    :param test: whether to omit the head and deprel columns. Defaults to False
    :param enhanced: whether to include enhanced edges
    :param preprocess: preprocess the converted dependency graph before returning it?

    :return list of lines representing the semantic dependencies in the passage
    """
    from semstr.conversion.conllu import ConlluConverter
    return ConlluConverter(enhanced=enhanced).to_format(passage, test=test,
        preprocess=preprocess, format=kwargs.get('format'))


def pype_return_f_args(accum, *fArgs):
    """
    FArgs is a tuple, but we want it to be a list - it's just neater.
    """
    return list(fArgs)


@classmethod
def all(cls, limit=None, offset=0):
    """
        Return all data with specified limit and offset
        """
    return cls.find(limit=limit, offset=offset)


def yield_fixedblocks(infile, blocksize):
    """Generator, yields each block of ``blocksize`` bytes in the input file.

    :param infile: file to read and separate in blocks.
    :param blocksize: block size in bytes.
    :returns: a generator that yields the contents of each block
    """
    while True:
        block = infile.read(blocksize)
        read_bytes = len(block)
        if read_bytes == 0:
            break
        yield block
        if read_bytes < blocksize:
            break


Numscrypt-0.0.40//numscrypt/linalg/eigen_mpmath - Copy.pyfile:/numscrypt/linalg/eigen_mpmath - Copy.py:function:hessenberg_reduce_0/hessenberg_reduce_0

def hessenberg_reduce_0(ctx, A, T):
    """
    This routine computes the (upper) Hessenberg decomposition of a square matrix A.
    Given A, an unitary matrix Q is calculated such that

               Q' A Q = H              and             Q' Q = Q Q' = 1

    where H is an upper Hessenberg matrix, meaning that it only contains zeros
    below the first subdiagonal. Here ' denotes the hermitian transpose (i.e.
    transposition and conjugation).

    parameters:
      A         (input/output) On input, A contains the square matrix A of
                dimension (n,n). On output, A contains a compressed representation
                of Q and H.
      T         (output) An array of length n containing the first elements of
                the Householder reflectors.
    """
    n = A.rows
    if n <= 2:
        return
    for i in range(n - 1, 1, -1):
        scale = 0
        for k in range(0, i):
            scale += abs(ctx.re(A[i, k])) + abs(ctx.im(A[i, k]))
        scale_inv = 0
        if scale != 0:
            scale_inv = 1 / scale
        if scale == 0 or ctx.isinf(scale_inv):
            T[i] = 0
            A[i, i - 1] = 0
            continue
        H = 0
        for k in range(0, i):
            A[i, k] *= scale_inv
            rr = ctx.re(A[i, k])
            ii = ctx.im(A[i, k])
            H += rr * rr + ii * ii
        F = A[i, i - 1]
        f = abs(F)
        G = ctx.sqrt(H)
        A[i, i - 1] = -G * scale
        if f == 0:
            T[i] = G
        else:
            ff = F / f
            T[i] = F + G * ff
            A[i, i - 1] *= ff
        H += G * f
        H = 1 / ctx.sqrt(H)
        T[i] *= H
        for k in range(0, i - 1):
            A[i, k] *= H
        for j in range(0, i):
            G = ctx.conj(T[i]) * A[j, i - 1]
            for k in range(0, i - 1):
                G += ctx.conj(A[i, k]) * A[j, k]
            A[j, i - 1] -= G * T[i]
            for k in range(0, i - 1):
                A[j, k] -= G * A[i, k]
        for j in range(0, n):
            G = T[i] * A[i - 1, j]
            for k in range(0, i - 1):
                G += A[i, k] * A[k, j]
            A[i - 1, j] -= G * ctx.conj(T[i])
            for k in range(0, i - 1):
                A[k, j] -= G * ctx.conj(A[i, k])


def __pyexpander_helper(*args, **kwargs):
    """a helper function needed at runtime.

    This evaluates named arguments.
    """
    if len(args) == 1:
        fn = args[0]
    elif len(args) > 1:
        raise ValueError('only one unnamed argument is allowed')
    else:
        fn = None
    return fn, kwargs


def have_pyrex():
    """
    Return True if Cython or Pyrex can be imported.
    """
    pyrex_impls = 'Cython.Distutils.build_ext', 'Pyrex.Distutils.build_ext'
    for pyrex_impl in pyrex_impls:
        try:
            __import__(pyrex_impl, fromlist=['build_ext']).build_ext
            return True
        except Exception:
            pass
    return False


def get_dotted_field(input_dict, accessor_string):
    """Gets data from a dictionary using a dotted accessor-string

    :param input_dict: A nested dictionary
    :type input_dict: dict
    :param accessor_string: A dotted path description i.e. "DBLinks.KEGG"
    :type accessor_string: str
    :return: The value in the nested dict
    """
    current_data = input_dict
    for chunk in accessor_string.split('.'):
        current_data = current_data.get(chunk, {})
    return current_data


def searchGroups(**criteria):
    """Search for groups matching a set of criteria.

        The criteria are a dictionary mapping group properties
        to values and have the semantics declared by
        IPluggableAuthService.searchGroups().
        """


def barotropic_eqlat_lwa(ylat, vort, area, dmu, n_points, planet_radius=
    6378000.0):
    """
    Compute equivalent latitude and wave activity on a barotropic sphere.

    Parameters
    ----------
    ylat : sequence or array_like
        1-d numpy array of latitude (in degree) with equal spacing in ascending order; dimension = nlat.
    vort : ndarray
        2-d numpy array of vorticity values; dimension = (nlat, nlon).
    area : ndarray
        2-d numpy array specifying differential areal element of each grid point; dimension = (nlat, nlon).
    dmu: sequence or array_like
        1-d numpy array of latitudinal differential length element (e.g. dmu = cos(lat) d(lat)). Size = nlat.
    n_points : int, default None
        Analysis resolution to calculate equivalent latitude. If input as None, it will be initialized as *nlat*.
    planet_radius : float, default 6.378e+6
        radius of spherical planet of interest consistent with input 'area'.

    Returns
    -------
    qref : ndarray
        1-d numpy array of value Q(y) where latitude y is given by ylat; dimension = (nlat).
    lwa_result : ndarray
        2-d numpy array of local wave activity values;
                    dimension = [nlat_s x nlon]
    """
    from hn2016_falwa import basis
    nlat = vort.shape[0]
    nlon = vort.shape[1]
    if n_points is None:
        n_points = nlat
    qref, dummy = basis.eqvlat(ylat, vort, area, n_points, planet_radius=
        planet_radius)
    lwa_result, dummy = basis.lwa(nlon, nlat, vort, qref, dmu)
    return qref, lwa_result


def example_whitelist():
    """
    Example whitelist entry point used for testing.

    """
    return ['world']


def Probability2(yes, no):
    """Computes the probability corresponding to given odds.

    Example: yes=2, no=1 means 2:1 odds in favor, or 2/3 probability.

    yes, no: int or float odds in favor
    """
    return yes / (yes + no)


def _get_list_of_int_from_csv(csv):
    """
    Get a list of integer from one line CSV.

    Parameters
    ----------
    csv : str
        One line CSV string.

    Returns
    -------
    result_list : list of str
        List of splitted integer.
    """
    if csv == '':
        return []
    ignore_info_id_list = csv.split(',')
    for i, info_id in enumerate(ignore_info_id_list):
        ignore_info_id_list[i] = int(info_id)
    return ignore_info_id_list


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

    """
    pass


@classmethod
def set_current_span(cls, span):
    """
        Set the given span as the current span in the execution context.
        """


@staticmethod
def is_positive(a):
    """
        returns true if a given atom is a positively charged

        :param a: a `ccdc.molecule.Atom` instance
        :return: bool, true if the atom is positively charged
        """
    return a.formal_charge > 0


@staticmethod
def read(sile, *args, **kwargs):
    """ Reads dynamical matrix from `Sile` using `read_dynamical_matrix`.

        Parameters
        ----------
        sile : Sile, str or pathlib.Path
            a `Sile` object which will be used to read the dynamical matrix.
            If it is a string it will create a new sile using `get_sile`.
        * : args passed directly to ``read_dynamical_matrix(,**)``
        """
    from sisl.io import get_sile, BaseSile
    if isinstance(sile, BaseSile):
        return sile.read_dynamical_matrix(*args, **kwargs)
    else:
        with get_sile(sile) as fh:
            return fh.read_dynamical_matrix(*args, **kwargs)


@classmethod
def utcnow(cls):
    """
        Return the current UTC date and time as :class:`.Zulu` object.

        Returns:
            :class:`.Zulu`
        """
    return cls.now()


def get_list_id_from_knr(line):
    """
    Takes a line from csv file with a candidate number (knr) in it and
    returns the derived listnr for this candidate. Will also handle the new
    WabstiC Standard 2018.
    """
    if '.' in line.knr:
        return line.knr.split('.')[0]
    return line.knr[0:-2]


def is_unibipartite(graph):
    """Internal function that returns whether the given graph is a uni-directional
    bipartite graph.

    Parameters
    ----------
    graph : GraphIndex
        Input graph

    Returns
    -------
    bool
        True if the graph is a uni-bipartite.
    """
    src, dst, _ = graph.edges()
    return set(src.tonumpy()).isdisjoint(set(dst.tonumpy()))


def is_notebook() ->bool:
    """
    Checks if the shell is in a notebook.

    :return: `True` if the shell is for a notebook, `False` otherwise
    """
    try:
        from IPython import get_ipython
        shell = get_ipython().__class__.__name__
        if shell == 'ZMQInteractiveShell':
            return True
        elif shell == 'TerminalInteractiveShell':
            return False
        else:
            return False
    except (NameError, ImportError):
        return False


def remove_render_slot():
    """Remove the current render slot

    """
    pass