def flatten_sequence(sequence, isinstance=isinstance, StringTypes=
    StringTypes, SequenceTypes=SequenceTypes, do_flatten=do_flatten):
    """Flatten a sequence to a non-nested list.

    Same as flatten(), but it does not handle the single scalar
    case. This is slightly more efficient when one knows that
    the sequence to flatten can not be a scalar.
    """
    result = []
    for item in sequence:
        if isinstance(item, StringTypes) or not isinstance(item, SequenceTypes
            ):
            result.append(item)
        else:
            do_flatten(item, result)
    return result


def psi_K_Mg_Cl_HMW84(T, P):
    """c-c'-a: potassium magnesium chloride [HMW84]."""
    psi = -0.022
    valid = T == 298.15
    return psi, valid


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


@classmethod
def sc_mean(cls, gid, excl, tm_dset, data, excl_dict=None, resolution=64,
    exclusion_shape=None, close=True):
    """
        Compute exclusions weight mean for the sc point from data

        Parameters
        ----------
        gid : int
            gid for supply curve point to analyze.
        excl : str | ExclusionMask
            Filepath to exclusions h5 or ExclusionMask file handler.
        tm_dset : str
            Dataset name in the exclusions file containing the
            exclusions-to-resource mapping data.
        data : ndarray | ResourceDataset
            Array of data or open dataset handler to apply exclusions too
        excl_dict : dict | None
            Dictionary of exclusion LayerMask arugments {layer: {kwarg: value}}
            None if excl input is pre-initialized.
        resolution : int
            Number of exclusion points per SC point along an axis.
            This number**2 is the total number of exclusion points per
            SC point.
        exclusion_shape : tuple
            Shape of the full exclusions extent (rows, cols). Inputing this
            will speed things up considerably.
        offshore_flags : np.ndarray | None
            Array of offshore boolean flags if available from wind generation
            data. None if offshore flag is not available.
        close : bool
            Flag to close object file handlers on exit

        Returns
        -------
        ndarray
            Exclusions weighted means of data for supply curve point
        """
    kwargs = {'excl_dict': excl_dict, 'resolution': resolution,
        'exclusion_shape': exclusion_shape, 'close': close}
    with cls(gid, excl, tm_dset, **kwargs) as point:
        means = point.exclusion_weighted_mean(data)
    return means


def _manifest_datetime_str(date_time):
    """Format datetime as a string for manifest.

    Args:
        date_time (DateTime): Date Time to format to string
    Returns:
        (String): Formated date time string

    """
    return date_time.strftime('%Y%m%dT000000.000Z')


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


@classmethod
def copy(cls, graph, node_label, edge_label='edge'):
    """Create a copy of the graph."""
    copy_nodes_q = 'MATCH (n:{}) CREATE (n1:{}) SET n1=n\n'.format(graph.
        _node_label, node_label)
    graph.execute(copy_nodes_q)
    copy_edges_q = ('MATCH (n:{})-[r:{}]->(m:{}), (n1:{}), (m1:{}) \n'.
        format(graph._node_label, graph._edge_label, graph._node_label,
        node_label, node_label) + """WHERE n1.id=n.id AND m1.id=m.id
""" +
        'MERGE (n1)-[r1:{}]->(m1) SET r1=r\n'.format(edge_label))
    graph.execute(copy_edges_q)
    return cls(driver=graph._driver, node_label=node_label, edge_label=
        edge_label, unique_node_ids=graph.unique_node_ids)


def truncate(s, n):
    """Return s truncated to n characters."""
    if len(s) <= n:
        return s
    s2 = s[:n - 3] + f'...({len(s)})'
    if s.endswith('\n'):
        return s2 + '\n'
    return s2


def safe_latin() ->[str]:
    """[A-Z] excluding (O, I, L)"""
    return list('ABCDEFGHJKMNPQRSTUVWXYZ')


def material_slot_assign():
    """Assign active material slot to selection

    """
    pass


def process_somaticsniper_vcf(job, somaticsniper_vcf, work_dir, univ_options):
    """
    Process the SomaticSniper vcf for accepted calls.  Since the calls are pre-filtered, we just
    obtain the file from the file store and return it.

    :param toil.fileStore.FileID somaticsniper_vcf: fsID for a SomaticSniper generated chromosome
           vcf
    :param str work_dir: Working directory
    :param dict univ_options: Dict of universal options used by almost all tools
    :return: Path to the processed vcf
    :rtype: str
    """
    return job.fileStore.readGlobalFile(somaticsniper_vcf)


@classmethod
def changed(cls, user):
    """The given user instance is ready to be saved"""
    raise NotImplementedError('Implement in subclass')


def update_item_levels(items, to_level, quiet=False):
    """
    Given a list of `items`, update their base level to `level`.  If `quiet`
    is `True`, only errors will be printed.
    """
    num_items = len(items)
    if not quiet:
        if num_items == 1:
            plural = ''
        else:
            plural = 's'
        print(' - Updating {} item{} to level {}'.format(num_items, plural,
            to_level))
    actually_updated = 0
    for item in items:
        if item.level != to_level:
            item.level = to_level
            actually_updated += 1
    if not quiet:
        remaining = num_items - actually_updated
        if actually_updated == 1:
            updated_verb = 'was'
        else:
            updated_verb = 'were'
        if remaining > 0:
            if remaining == 1:
                remaining_verb = 'was'
            else:
                remaining_verb = 'were'
            remaining_txt = ' ({} {} already at that level)'.format(remaining,
                remaining_verb)
        else:
            remaining_txt = ''
        print('   - {} {} updated{}'.format(actually_updated, updated_verb,
            remaining_txt))


def configure_plugins(rootdir, newroot, app):
    """Configure each plugin in pre-chrooted environment."""


def fd(f):
    """Get a filedescriptor from something which could be a file or an fd."""
    return hasattr(f, 'fileno') and f.fileno() or f


def _nominal_kernel(x, y, out):
    """Number of features that match exactly"""
    for i in range(x.shape[0]):
        for j in range(y.shape[0]):
            out[i, j] += (x[(i), :] == y[(j), :]).sum()
    return out


@classmethod
def _constructor(cls, values, axes, **metadata):
    """ Internal API for the constructor: check whether a pre-defined class exists

        values        : array-like
        axes        : Axes instance

        This static method is used whenever a new DimArray needs to be instantiated
        for example after a transformation.

        This makes the sub-classing process easier since only this method needs to be
        overloaded to make the sub-class a "closed" class.

        """
    return cls(values, axes, **metadata)


def _select(state, rank_number):
    """
    rank_number (index):  An index into the archive.

    Returns a GridPoint from the given rank.
    If the rank is empty, raises an Exception.
    If the rank number is outside the archive, raises
    an IndexError.  Don't do that!
    """
    rank = state.archive[rank_number]
    if rank.occupancy == 0:
        raise Exception("Can't select from an empty rank ({}).".format(
            rank_number))
    randint = state.randint
    target = randint(0, rank.occupancy - 1)
    counter = -1
    index = -1
    while counter < target:
        index += 1
        if rank.individuals[index].valid:
            counter += 1
        if index > len(rank.individuals):
            raise Exception('Rank {} is inconsistent!'.format(rank_number))
    return rank.individuals[index].grid_point


def istuple(val):
    """
    check if the entry is a tuple or is a string of tuple

    Parameters
    ----------
    val
        an entry of any type

    Returns
    -------
    bool
        True if the input is either a tuple or a string of tuple, False otherwise

    Notes
    -----
    please note that '(1)' is also a tuple, while (1) is not a tuple

    """
    text = str(val)
    if text[0] == '(' and text[len(text) - 1] == ')':
        return True
    else:
        return False


def parse_cstring_from_stream(stream, stream_pos=None):
    " Parse a C-string from the given stream. The string is returned without\n        the terminating \x00 byte. If the terminating byte wasn't found, None\n        is returned (the stream is exhausted).\n        If stream_pos is provided, the stream is seeked to this position before\n        the parsing is done. Otherwise, the current position of the stream is\n        used.\n        Note: a bytes object is returned here, because this is what's read from\n        the binary file.\n    "
    if stream_pos is not None:
        stream.seek(stream_pos)
    CHUNKSIZE = 64
    chunks = []
    found = False
    while True:
        chunk = stream.read(CHUNKSIZE)
        end_index = chunk.find(b'\x00')
        if end_index >= 0:
            chunks.append(chunk[:end_index])
            found = True
            break
        else:
            chunks.append(chunk)
        if len(chunk) < CHUNKSIZE:
            break
    return b''.join(chunks) if found else None


def write_list_to_file(filename, input_list):
    """Write a list to a text file with each element on a new line

    Parameters
    ----------
    filename : str
        the filename that the list will be saved to

    input_list : list
        list to save

    Examples
    --------
    Find all images ending in ".jpg" in "./images" folder

    .. code-block:: python

        import cloudburst as cb

        some_numbers = [i for i in range(50)]
        cb.write_list_to_file("numbers.txt", some_numbers)
    """
    with open(filename, 'w') as f:
        for item in input_list:
            f.write('{}\n'.format(item))


@classmethod
def whip(cls, bb, h, ip):
    """
        Walks + Hits / IP
        :param bb: base on ball
        :param h: hits
        :param ip: inning pitched
        :return: (float) whip
        """
    return round((bb + h) / ip, 3)


def __setattr__(cls, name, value):
    """Block attempts to reassign Enum members.

        A simple assignment to the class namespace only changes one of the
        several possible ways to get an Enum member from the Enum class,
        resulting in an inconsistent Enumeration.

        """
    member_map = cls.__dict__.get('_member_map_', {})
    if name in member_map:
        raise AttributeError('Cannot reassign members.')
    super().__setattr__(name, value)


def date2period_end(isodate):
    """Transform iso date to last trimino date

    :param isodate:
    :return: YYYY-MM-LAST-DATE-OF-3MONTH
    """
    year, month, _ = isodate.split('-')
    imonth = int(month)
    if imonth <= 3:
        return '%s-%s-%s' % (year, '03', '31')
    elif imonth <= 6:
        return '%s-%s-%s' % (year, '06', '30')
    elif imonth <= 9:
        return '%s-%s-%s' % (year, '09', '30')
    elif imonth <= 12:
        return '%s-%s-%s' % (year, '12', '31')
    else:
        return '%s-%s-%s' % (year, '12', '31')


@staticmethod
def calculate_sale_price(net_price, vat_value):
    """
        Return sale price
        :param net_price:
        :param vat_value:
        :return: float value
        """
    return net_price + vat_value


def print_sparse_stats(X_dtm):
    """
    Prints the stats around a Sparse Matrix (typically) generated in NLP problems.
    """
    print('Shape of Sparse Matrix: ', X_dtm.shape)
    print('Amount of Non-Zero occurences: ', X_dtm.nnz)
    print('    Density: %.2f%%' % (100.0 * X_dtm.nnz / (X_dtm.shape[0] *
        X_dtm.shape[1])))


def OneHotEncoding(dataset, column, drop=True, verbose=False):
    """Performs One Hot Encoding (OHE) usally used in Machine Learning.

    .. code-block:: python

        pycof.OneHotEncoding(dataset, column, drop=True, verbose=False)

    :Parameters:
        * **dataset** (:obj:`pandas.DataFrame`): Data Frame on which we apply One Hot Encoding.
        * **column** (:obj:`list`): Column to be converted to dummy variables.
        * **drop** (:obj:`bool`): Drop the column that needs to be converted to dummies (defaults True).
        * **verbose** (:obj:`bool`): Display progression (defaults False).

    :Example:
        >>> print(df)
        ... +----+--------+----------+-----+
        ... | Id | Gender | Category | Age |
        ... +----+--------+----------+-----+
        ... |  1 | Male   |        A |  23 |
        ... |  2 | Female |        B |  21 |
        ... |  3 | Female |        A |  31 |
        ... |  4 | Male   |        C |  22 |
        ... |  5 | Female |        A |  26 |
        ... +----+--------+----------+-----+
        >>> # Encoding columns "Gender" and "Category"
        >>> new_df = pycof.OneHotEncoding(df, column="Gender")
        >>> new_df = pycof.OneHotEncoding(new_df, column="Category")
        >>> print(new_df)
        ... +----+---------------+------------+------------+-----+
        ... | Id | Gender_Female | Category_A | Category_B | Age |
        ... +----+---------------+------------+------------+-----+
        ... |  1 |             0 |          1 |          0 |  23 |
        ... |  2 |             1 |          0 |          1 |  21 |
        ... |  3 |             1 |          1 |          0 |  31 |
        ... |  4 |             0 |          0 |          0 |  22 |
        ... |  5 |             1 |          1 |          0 |  26 |
        ... +----+---------------+------------+------------+-----+

    :Returns:
        :obj:`pandas.DataFrame`: Transformed dataset with One Hot Encoding.
    """
    all_values = dataset[column].unique()
    for val in all_values:
        if verbose:
            print('Encoding for value: ' + str(val))
        dataset[column + '_' + str(val)] = 0
        dataset[column + '_' + str(val)][dataset[column] == val] = 1
    if drop:
        dataset = dataset.drop(columns=[column])
    return dataset


def account_unk_gmm(samp_pred):
    """Account for unknown proportion after one step GMM source prediction

    Args:
        samp_pred(dict): dictionnary of samples class probability predictions
            {samp: {class: proba}}
    Returns:
        pandas DataFrame: sourcepredict sample proba predictions
            with unknown accounted for
    """
    for sample in samp_pred:
        smp_tot = 0
        for source in samp_pred[sample]:
            smp_tot += samp_pred[sample][source]
        samp_pred[sample]['unknown'] = 1 - smp_tot
    return samp_pred


@classmethod
def CreateDefaultSingleton(cls):
    """
        Creates the default singleton instance, that will be used when no singleton has been installed.
        By default, tries to create the class without constructor.

        :rtype: Singleton
        :returns:
            an instance of the singleton subclass
        """
    return cls()


def get_all(isamAppliance, check_mode=False, force=False):
    """
    Retrieving all interfaces
    :rtype: (str, dict)
    """
    return isamAppliance.invoke_get('Retrieving all interfaces', '/net/ifaces')


def references_to_json(resources, references):
    """ Make a JSON/catalog representation of the references db,
     including the count for each """
    dump_references = {}
    for reftype, refvalue in references.items():
        dump_references[reftype] = {}
        for label, reference_resource in refvalue.items():
            target_count = len(reference_resource.get_sources(resources))
            dump_references[reftype][label] = dict(count=target_count,
                docname=reference_resource.docname)
    return dump_references


def DF_calc(classes):
    """
    Calculate chi-squared degree of freedom.

    :param classes: confusion matrix classes
    :type classes : list
    :return: DF as int
    """
    try:
        return (len(classes) - 1) ** 2
    except Exception:
        return 'None'


def analyze_text(filename):
    """Calculate the number of lines, words and characters in a file

    Args:
        filename: The name of the file to analyze.

    Raises:
        IOError: If 'filename' does not exist or can't be read.

    Returns:
        A tuple where the first element is the number of lines in
        the file, the second element is the number of words and the
        third element is the number of characters.
    """
    lines = 0
    words = 0
    characters = 0
    with open(filename, 'rt') as f:
        for line in f:
            lines += 1
            words += len(line.split())
            characters += len(line)
    print(f'\nTotal number of lines in a text file is {lines}\n')
    print(f'Total number of words in a text file is {words}\n')
    print(f'Total number of characters in a text file is {characters}\n')


@classmethod
def get_or_create(cls, id, client=None, **kwargs):
    """Get an existing object from the Descartes Labs catalog or create a new object.

        If the Descartes Labs catalog object is found, and the remainder of the
        arguments do not differ from the values in the retrieved instance, it will be
        returned in the `~descarteslabs.catalog.DocumentState.SAVED` state.

        If the Descartes Labs catalog object is found, and the remainder of the
        arguments update one or more values in the instance, it will be returned in
        the `~descarteslabs.catalog.DocumentState.MODIFIED` state.

        If the Descartes Labs catalog object is not found, it will be created and the
        state will be `~descarteslabs.catalog.DocumentState.UNSAVED`.  Also see the
        example for :py:meth:`save`.

        Parameters
        ----------
        id : str
            The id of the object you are requesting.
        client : CatalogClient, optional
            A `CatalogClient` instance to use for requests to the Descartes Labs
            catalog.  The
            :py:meth:`~descarteslabs.catalog.CatalogClient.get_default_client` will
            be used if not set.
        kwargs : dict, optional
            With the exception of readonly attributes (`created`, `modified`), any
            attribute of a catalog object can be set as a keyword argument (Also see
            `ATTRIBUTES`).

        Returns
        -------
        :py:class:`~descarteslabs.catalog.CatalogObject`
            The requested catalog object that was retrieved or created.

        """
    obj = cls.get(id, client=client)
    if obj is None:
        obj = cls(id=id, client=client, **kwargs)
    else:
        obj.update(**kwargs)
    return obj


def get_resource_string(arn):
    """
    Given an ARN, return the string after the account ID, no matter the ARN format.

    :param arn: arn:partition:service:region:account-id:resourcetype/resource
    :return: resourcetype/resource
    """
    split_arn = arn.split(':')
    resource_string = ':'.join(split_arn[5:])
    return resource_string


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


def dslice(dict_, keys, strict=True):
    """Returns a new dictionary containing only the specified keys"""
    ret = {}
    for key in keys:
        if strict or key in dict_:
            ret[key] = dict_[key]
    return ret


def wye_to_delta(z1, z2, z3):
    """Follows http://home.earthlink.net/~w6rmk/math/wyedelta.htm"""
    summand = z1 * z2 + z2 * z3 + z3 * z1
    return summand / z2, summand / z1, summand / z3


def includeme(config):
    """Add views related to local currency payments."""
    config.include('pyramid_facebook.security')
    config.include('pyramid_facebook.predicates')
    config.include('pyramid_facebook.utility')
    config.add_route('facebook_payments_order', 'orders/{order_id}')
    config.scan('.payments')


@classmethod
def load(cls, partname, content_type, blob, package):
    """
        Called by ``docx.opc.package.PartFactory`` to load an image part from
        a package being opened by ``Document(...)`` call.
        """
    return cls(partname, content_type, blob)


def image_fraction(image, xys):
    """ Return a subimage where x & y are fractions of the width & height. """
    x1, y1, x2, y2 = xys
    x1, x2 = sorted([x1, x2])
    y1, y2 = sorted([y1, y2])
    h, w = image.shape
    return image[int(y1 * h):int(y2 * h), int(x1 * w):int(x2 * w)]


def round_time(seconds, units, rounding):
    """
    Rounds given time into specific units.

    Args:
        seconds: float or int
            Time in seconds.

        units: pero.TIME
            Specifies the rounding units as eny item from the pero.TIME enum.

        rounding: pero.ROUNDING
            Specifies the rounding style as any item from the pero.ROUNDING
            enum.

    Returns:
        float
            Rounded time in seconds.
    """
    pass


def get_application_names():
    """
        Gets a list of application names.
        :returns: a list of application names.
        """


def _cast_mesh(mesh, Nmesh):
    """
    Cast an object to a MeshSource. Nmesh is used only on FKPCatalog

    """
    from .catalog import FKPCatalog
    from .catalogmesh import FKPCatalogMesh
    if not isinstance(mesh, (FKPCatalogMesh, FKPCatalog)):
        raise TypeError(
            'input sources should be a FKPCatalog or FKPCatalogMesh')
    if isinstance(mesh, FKPCatalog):
        mesh = mesh.to_mesh(Nmesh=Nmesh, dtype='f8', compensated=False)
    if Nmesh is not None and any(mesh.attrs['Nmesh'] != Nmesh):
        raise ValueError(
            'Mismatched Nmesh between __init__ and mesh.attrs; if trying to re-sample with a different mesh, specify `Nmesh` as keyword of to_mesh()'
            )
    return mesh


def add_str_to_keys(dct, key_str):
    """ convert dictionary keys by adding key_str"""
    new_dct = {}
    for key, value in dct.iteritems():
        if key_str in key:
            new_dct[key] = value
        else:
            new_dct[key + key_str] = value
    return new_dct


def is_int(value):
    """Return `True` if ``value`` is an integer."""
    if isinstance(value, bool):
        return False
    try:
        int(value)
        return True
    except ValueError:
        return False


def placeholder_t():
    """ Always returns True.
    """
    return True


def get_user_attribute_default(attribute, ctx):
    """Return the default value for the given attribute of the user passed in the context.

    :param attribute: attribute for which to get the current value
    :param ctx: click context which should contain the selected user
    :return: user attribute default value if set, or None
    """
    default = getattr(ctx.params['user'], attribute)
    if not default:
        return None
    return default


@staticmethod
def get_date_fields(facility):
    """
        Return a sequence of field names whose type is datetime
        """
    if facility == 'LCO':
        return ['start', 'end']
    return []


@classmethod
def _get_fieldnames_to_extract(cls):
    """
        Return a list of fieldname to extract - which means dumping
        PID fields.
        """
    fieldnames = cls._get_fieldnames_to_serialize()
    if hasattr(cls, 'pid_fields'):
        for fname in cls.pid_fields:
            if fname in fieldnames:
                fieldnames.remove(fname)
    return fieldnames


def writeScwrlSequenceFile(sequence, filename='x-ray.seq', options=None):
    """
    writing a scwrl sequence file for, e.g.,  generating a mutated protein
    """
    file = open(filename, 'w')
    start = 0
    while len(sequence[start:]) > 60:
        file.write('%s\n' % sequence[start:start + 60])
        start += 60
    file.write('%s\n' % sequence[start:])
    file.close()


@classmethod
def from_stream(cls, stream, marker_code, offset):
    """
        Return an |_SofMarker| instance for the SOFn marker at *offset* in
        stream.
        """
    segment_length = stream.read_short(offset)
    px_height = stream.read_short(offset, 3)
    px_width = stream.read_short(offset, 5)
    return cls(marker_code, offset, segment_length, px_width, px_height)


@staticmethod
def _add_path(path, user_paths):
    """Add to list for later saving and sys.path for now."""
    user_paths.append(path)


def sanitize(msg):
    """Sanitize the existing msg, use before adding color annotations."""
    msg = msg.replace('@', '@@')
    msg = msg.replace('{', '{{')
    msg = msg.replace('}', '}}')
    msg = msg.replace('@@!', '@{atexclimation}')
    msg = msg.replace('@@/', '@{atfwdslash}')
    msg = msg.replace('@@_', '@{atunderscore}')
    msg = msg.replace('@@|', '@{atbar}')
    return msg


def add_var(name, value):
    """Registers custom request variable.

    Can be used for better integration with the internal routing subsystem.

    :param str|unicode name:

    :param str|unicode value:

    :rtype: bool

    :raises ValueError: If buffer size is not enough.
    """


@classmethod
def update_by_example(cls, collection, example_data, new_value, keep_null=
    False, wait_for_sync=None, limit=None):
    """
            This will find all documents in the collection that match the specified example object,
            and partially update the document body with the new value specified. Note that document meta-attributes
            such as _id, _key, _from, _to etc. cannot be replaced.

            Note: the limit attribute is not supported on sharded collections. Using it will result in an error.

            Returns result dict of the request.

            :param collection Collection instance
            :param example_data An example document that all collection documents are compared against.
            :param new_value A document containing all the attributes to update in the found documents.

            :param keep_null This parameter can be used to modify the behavior when handling null values.
            Normally, null values are stored in the database. By setting the keepNull parameter to false,
            this behavior can be changed so that all attributes in data with null values will be removed
            from the updated document.

            :param wait_for_sync  if set to true, then all removal operations will instantly be synchronised to disk.
            If this is not specified, then the collection's default sync behavior will be applied.

            :param limit an optional value that determines how many documents to update at most. If limit is
            specified but is less than the number of documents in the collection, it is undefined
            which of the documents will be updated.

            :returns dict
        """
    kwargs = {'newValue': new_value, 'options': {'keepNull': keep_null,
        'waitForSync': wait_for_sync, 'limit': limit}}
    return cls._construct_query(name='update-by-example', collection=
        collection, example=example_data, result=False, **kwargs)


def load(filename):
    """
    load available file
    available exts: .nii(.gz), .mha, .xls(x), .csv, .tsv, .json

    :param filename: file want to load
    :type filename: str
    :return: object
    """
    if '.nii' in filename:
        from nibabel import Nifti1Image
        img = Nifti1Image.load(filename)
    else:
        import pandas as pd
        if '.xls' in filename:
            img = pd.read_excel(filename)
        elif '.csv' in filename:
            img = pd.read_csv(filename)
        elif '.tsv' in filename:
            img = pd.read_table(filename)
        elif '.1D' in filename:
            img = pd.read_csv(filename, header=None, sep='\\s+')
        elif '.json' in filename:
            import json
            img = json.load(open(filename))
        else:
            raise Exception
    return img


def label_offsetter(ax, xoffset_axis):
    """Convert an offset in x axis coordinates (e.g. 0 is left side of x axis, 1 is right side)
    to x and y axis offsets in axis coordinates, where both x and y offsets are the same when
    expressed in display (i.e. pixels) coordinates.

    Inputs:
        * ax (object) - a matplotlib axis object
        * xoffset_axis (scalar) - how much to offset the label from the x axis in axis coordinates
          (e.g. 1 is the length of the entire x axis).

    Examples:

    >>> from matplotlib import pyplot
    >>> import numpy
    >>> fig, ax = pyplot.subplots(1, 1) # Create an empty axis
    >>> axis_pixels = ax.transAxes.transform(numpy.array([[0,0], [1, 1]]))
    >>> xpixels, ypixels = axis_pixels[1] - axis_pixels[0]
    >>> xoffset, yoffset = label_offsetter(ax, xoffset_axis = 0.1) # Offset by 10% of x axis dimensions
    >>> yoffset_true = xoffset*(xpixels/ypixels) # expected yoffset
    >>> numpy.allclose(xoffset, 0.1) # xoffset is unchanged from input
    True
    >>> numpy.allclose(yoffset, yoffset_true) # yoffset is 0.129
    True
    """
    x_origin, y_origin = ax.transAxes.transform((0, 0))
    x_display = ax.transAxes.transform((xoffset_axis, y_origin))[0]
    offset = x_display - x_origin
    xoffset_axis, yoffset_axis = ax.transAxes.inverted().transform((offset +
        x_origin, offset + y_origin))
    return xoffset_axis, yoffset_axis


def noop_function_metadata(wrapped, instance, args, kwargs, return_value):
    """Make sure that metadata is not changed."""
    return {}


def summary_sentences(talker):
    """
  returns summary as a list of sentences,
  each a list of words
  """
    return talker.summary_sentences()


def loadConvert(string, off=0, length=None):
    """
    Converts encoded \\uxxxx to unicode chars
    and changes special saved chars to their original forms

    @example
    loadConvert('\\==', 0, 2) -> '='

    @param  string  string to process
    @param  off     offset, the beginning index to process, defaults 0
    @param  length  length of the string begin from offset to process,
                    defaults to process to the end
    @return         processed string
    """
    out = []
    if length == None:
        end = len(string)
    else:
        end = off + length
    while off < end:
        aChar = string[off]
        off += 1
        if aChar == '\\':
            aChar = string[off]
            off += 1
            if aChar == 'u':
                aChar = (u'\\u' + string[off:off + 4]).encode().decode(
                    'unicode-escape')
                off += 4
            elif aChar == 't':
                aChar = '\t'
            elif aChar == 'r':
                aChar = '\r'
            elif aChar == 'n':
                aChar = '\n'
            elif aChar == 'f':
                aChar = '\x0c'
        out.append(aChar)
    return ''.join(out)


@classmethod
def via_usb(cls, serial_number=None, manufacturer_id=None, model_code=None,
    name=None, board=0, **kwargs):
    """Return a Driver with an underlying USB Instrument resource.

        A connected USBTMC instrument with the specified serial_number, manufacturer_id,
        and model_code is returned. If any of these is missing, the first USBTMC driver
        matching any of the provided values is returned.

        To specify the manufacturer id and/or the model code override the following class attributes::

            class RigolDS1052E(MessageBasedDriver):

                MANUFACTURER_ID = '0x1AB1'
                MODEL_CODE = '0x0588'

        Parameters
        ----------
        serial_number :
            The serial number of the instrument. (Default value = None)
        manufacturer_id :
            The unique identification number of the manufacturer. (Default value = None)
        model_code :
            The unique identification number of the product. (Default value = None)
        name :
            Unique name given within Lantz to the instrument for logging purposes.
            Defaults to one generated based on the class name if not provided.
        board :
            USB Board to use (Default value = 0)
        kwargs :
            keyword arguments passed to the Resource constructor on initialize.
        **kwargs :

        Returns
        -------

        """
    return cls._via_usb('INSTR', serial_number, manufacturer_id, model_code,
        name, board, **kwargs)


@classmethod
def HEAD(cls, *n, **kw):
    """
        Perform a HEAD request using `RESTClient.request()`.
        """
    return cls.IMPL.GET(*n, **kw)


def get_perm_action(perm):
    """Split action from permission of format.

    Input permission can be in the form of '<action>_<object_type>'
    or '<action>'.
    """
    return perm.split('_', 1)[0]


def timedelta2millisecond(td):
    """Get milliseconds from a timedelta."""
    milliseconds = td.days * 24 * 60 * 60 * 1000
    milliseconds += td.seconds * 1000
    milliseconds += td.microseconds / 1000
    return milliseconds


@classmethod
def scan_string(cls, string):
    """Placeholder scanner.

        Scan a string for placeholders.

        Args:
            string (str): String to scan.

        Returns:
            bool: True if string has placeholders.
        """
    for k in cls.storage.iterkeys():
        if '<%s>' % k in string:
            return True
    return False


def prune_graph(g):
    """ Remove unconnected nodes """
    nodes2remove = []
    for n in g.nodes():
        if len(g[n]) == 0:
            nodes2remove.append(n)
    for n in nodes2remove:
        g.remove_node(n)


@classmethod
def best(cls):
    """
        Choose the best CommandSpec class based on environmental conditions.
        """
    return cls


@classmethod
def from_full(cls, color, fg=True):
    """Creates a color from full name or color number"""
    self = cls(fg=fg)
    self._from_full(color)
    return self


def make_ds_list(cell, limit=2.0):
    """
    Generates a list of d-spacings
    """
    print('Generating hkls with unit cell:', cell)
    cell.makerings(limit)
    ds_list = []
    keys = list(cell.ringhkls.keys())
    keys.sort()
    ptype = 0
    for ky in keys:
        ptype = ptype + 1
        ds = ky
        hklmax = -10000000000.0
        hmax = -10000000000.0
        kmax = -10000000000.0
        hkls_in_ring = cell.ringhkls[ky]
        hkls_in_ring.sort()
        hkls_in_ring.reverse()
        print(hkls_in_ring)
        for h, k, l in hkls_in_ring:
            if h + k + l >= hklmax and h >= hmax and k > kmax:
                hklmax = h + k + l
                hmax = h
                kmax = k
                ds_string = '(%d%d%d)' % (h, k, l)
        ds_list.append([ds, ptype, ds_string])
    return ds_list


def isarray(var):
    """
    isarray - Returns True if the variable is a list
    """
    return isinstance(var, (list, tuple))


def struct_fmt(size, signed=False):
    """
    Helper that returns the format for struct.pack, given a size and
    a signedness

    >>> struct_fmt(4, False)
    'I'
    >>> struct_fmt(8, True)
    'q'
    """
    formats = {(1): 'b', (2): 'h', (4): 'i', (8): 'q'}
    assert size in formats, 'unknown size'
    if not signed:
        return formats[size].upper()
    else:
        return formats[size]


@classmethod
def get_running_builds(cls):
    """Returns the number of running builds among all the repos."""
    return cls._running_builds


def translate(name):
    """
        Translate the name from Python to Swagger.

        :param unicode name: The Python name.
        :return unicode: The Swagger name.
        """


def middle_yx(screen):
    """Determins the middle of the screen"""
    xy = screen.getmaxyx()
    screen_y = xy[0]
    screen_x = xy[1]
    return screen_y / 2, screen_x / 2


def name_unicode(ucs):
    """Return 7-bit ascii printable of any string. """
    bits = ord(ucs)
    if 32 <= bits <= 126:
        rep = chr(bits)
    elif bits == 127:
        rep = '^?'
    elif bits < 32:
        rep = '^' + chr((bits & 127 | 32) + 32)
    else:
        rep = '\\x{:02x}'.format(bits)
    return rep


@staticmethod
def unique(l):
    """ Filters duplicate """
    seen = set()
    res = list()
    for i in l:
        if i not in seen:
            res.append(i)
            seen.add(i)
    return res


@classmethod
def from_buffer(cls, bfr):
    """ ListFields do not know enough to parse a buffer.
    """
    raise NotImplementedError('%s cannot be parsed directly from a buffer' %
        (cls,))


def get_args():
    """
    Get and parse arguments.
    """
    import argparse
    parser = argparse.ArgumentParser(description='MetaData xml creator')
    parser.add_argument('-f', '--filename', default=[None], nargs=1, help=
        'The SBP log file to extract data from. Default format is sbp json')
    parser.add_argument('-b', '--binary', action='store_true', help=
        'Flag to indicate that the SBP file is a binary sbp file. Default format is sbp json'
        )
    parser.add_argument('-o', '--outfile', default=['output.csv'], nargs=1,
        help='specify the name of the CSV file output.')
    parser.add_argument('-t', '--type', nargs=1, default=['MsgBaselineNED'],
        help='Type of message to interpolate.')
    parser.add_argument('-d', '--debouncetime', type=int, default=[1000],
        nargs=1, help='Specify the debounce time for trigger in ms.')
    parser.add_argument('-v', '--verbose', action='store_true', help=
        'Flag to indicate that the SBP file is a binary sbp file. Default format is sbp json'
        )
    args = parser.parse_args()
    return args


def __call__():
    """Return the text to be displayed at the disclaimer page.
        """


def _object_format(o):
    """ Object arrays containing lists should be printed unambiguously """
    if type(o) is list:
        fmt = 'list({!r})'
    else:
        fmt = '{!r}'
    return fmt.format(o)


def iter_nullstr(file):
    """Read a null-terminated ASCII string from the file.

    This continuously yields strings, with empty strings
    indicting the end of a section.
    """
    chars = bytearray()
    while True:
        char = file.read(1)
        if char == b'\x00':
            string = chars.decode('ascii')
            chars.clear()
            if string == ' ':
                yield ''
            elif string == '':
                return
            else:
                yield string
        elif char == b'':
            raise Exception('Reached EOF without null-terminator in {}!'.
                format(bytes(chars)))
        else:
            chars.extend(char)


def extract_columns(data, *cols):
    """Extract columns specified in the argument list.

>>> chart_data.extract_columns([[10,20], [30,40], [50,60]], 0)
[[10],[30],[50]]
"""
    out = []
    try:
        for r in data:
            col = []
            for c in cols:
                col.append(r[c])
            out.append(col)
    except IndexError:
        raise IndexError('data=%s col=%s' % (data, col))
    return out


def f1(x):
    """f1 is a quadratic with roots at 0 and 1"""
    return x * (x - 1.0)


def conv_input_length(output_length, filter_size, padding, stride):
    """Determines input length of a convolution given output length.

    # Arguments
        output_length: integer.
        filter_size: integer.
        padding: one of `"same"`, `"valid"`, `"full"`.
        stride: integer.

    # Returns
        The input length (integer).
    """
    if output_length is None:
        return None
    assert padding in {'same', 'valid', 'full'}
    if padding == 'same':
        pad = filter_size // 2
    elif padding == 'valid':
        pad = 0
    elif padding == 'full':
        pad = filter_size - 1
    return (output_length - 1) * stride - 2 * pad + filter_size


def _format_links_resource(data):
    """
    Format links of resources while meter_link equaling to 1
    Return formatted list of dicts
    """
    try:
        for elem in data:
            elem._info.setdefault(u'meter_links', [])
            _dicts = elem._info[u'links']
            i = 0
            while i < len(_dicts):
                if _dicts[i][u'rel'] != 'self':
                    elem._info[u'meter_links'].append(_dicts[i])
                    _dicts.pop(i)
                    i -= 1
                i += 1
        return data
    except Exception:
        return data


@staticmethod
def from_dict(input_dict):
    """
        Instantiate an object of a derived class using the information
        in input_dict (built by the to_dict method of the derived class).
        More specifically, after reading the derived class from input_dict,
        it calls the method _build_from_input_dict of the derived class.
        Note: This method should not be overrided in the derived class. In case
        it is needed, please override _build_from_input_dict instate.

        :param dict input_dict: Dictionary with all the information needed to
           instantiate the object.
        """
    import copy
    input_dict = copy.deepcopy(input_dict)
    kernel_class = input_dict.pop('class')
    input_dict['name'] = str(input_dict['name'])
    import GPy
    kernel_class = eval(kernel_class)
    return kernel_class._build_from_input_dict(kernel_class, input_dict)


@classmethod
def warning(cls, message):
    """
        Logs a message with level WARNING on this logger
        :param message: Log record content
        :type message: str
        """
    cls.log('WARNING', message)


def relative_tuple_path(path_orig, path_dest):
    """Takes two path as list of ids and return a new path that is the
    relative path the second against the first.
    """
    path_orig = list(path_orig)
    path_dest = list(path_dest)
    while (path_orig and path_dest) and path_orig[0] == path_dest[0]:
        path_orig.pop(0)
        path_dest.pop(0)
    result_path = ['..'] * len(path_orig)
    result_path.extend(path_dest)
    if not result_path:
        return ['.']
    return result_path


def get_help(command_name, configuration_file):
    """
        Returns the cli help string.

        :param command_name: Name of the cli call (usually sys.argv[0]).
        :type command_name: str
        :param configuration_file: Name of the default configuration file.
        :type configuration_file: str
        :return: The formatted help string ready to be displayed.
        :rtype: str

        .. seealso:: backupper.cli.main
    """
    help_string = (
        """Usage: {} [OPTIONS...]

  -h, --help			Displays the current help and exits.
  -f, --config-file		Specifies an alternative YAML config file (default: {}).
  -b, --backup-dir		Specifies an alternative backup directory (overrides the one set in the YAML config file).
  -d, --delete_old_backups	If true, will delete old backups (overrides the one set in the YAML config file).
"""
        .format(command_name, configuration_file))
    return help_string


def rint(data=None, out=None, name=None, **kwargs):
    """Returns element-wise rounded value to the nearest integer of the input.

    .. note::
       - For input ``n.5`` ``rint`` returns ``n`` while ``round`` returns ``n+1``.
       - For input ``-n.5`` both ``rint`` and ``round`` returns ``-n-1``.

    Example::

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

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

       - rint(default) = default
       - rint(row_sparse) = row_sparse
       - rint(csr) = csr

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

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

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

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


@staticmethod
def extension_from_path(path):
    """Return the extension of the file from the file path

        Parameters
        ----------
        path: str
            path to the results file
        """
    extension = path.split('.')[-1]
    return extension


def _resample_date_offset_to_date_offset(df, target_index, resample_func):
    """Down sample a date offset data to a target index with date offset."""
    new_df = df.resample(target_index.freq).agg(resample_func)
    new_df = new_df[target_index[0]:target_index[-1]]
    return new_df


def getSchemaElements(schemaElementType, unique_name, aslist=False,
    more_restrictions=None, more_properties=None, generate_instance=True):
    """Returns a list of schema elements or a single entry.

        schemaElementType: refers to a key in `schemaElementTypes`
        unique_name: return a specific element
        aslist: return result as list, if false the first in the resultset will be returned
        more_restrictions: dictionary containing more constraints for the request, i.e.
                           {"HIERARCHY_UNIQUE_NAME": "[Sales by Country]"}
        more_properties: dictionary containing more properties for the request,
                           {"ProviderInfo": "YourProvider"}
        generate_instance: If false the resulting entries will be dictionaries, otherwise
                           of the type mentioned in the "ELEMENT_CLASS" entry
                           from the schemaELementType.
                           """


@staticmethod
def check(result):
    """Override to return result or raise error, for APIs which don't use status codes."""
    return result


@classmethod
def get_BZ_integration_grid_shift_Z(cls, calc, **kwargs):
    """Return the shift of the Brillouin zone points along reciprocal lattice vector Z."""
    array = cls._get_BZ_integration_grid_shift(calc, **kwargs)
    if array is not None:
        return array[2]


def display_adding_output(name, output_string):
    """display function for adding output manually to a notebook execution"""
    print('For the last notebook executed (%s), added to the output "%s"' %
        (name, output_string))


def backimage_fit():
    """Fit the background image to the view

    """
    pass


def get_dynsym_datas(elf_data, elf_file, skip_import=True):
    """
    获取符号/方法的相关信息（符号名、符号数据）。
    """
    f = elf_data
    dynsym_datas = []
    symbol_table = elf_file.get_section_by_name('.dynsym')
    if symbol_table:
        for symbol in symbol_table.iter_symbols():
            if (skip_import and symbol.entry.st_size == 0 or symbol.entry.
                st_info.type != 'STT_FUNC'):
                continue
            f.seek(0)
            symbol_addr = symbol.entry.st_value & 65534
            f.seek(symbol_addr)
            symbol_hexs = ''
            size = symbol.entry.st_size
            if symbol.entry.st_size > 80:
                size = 80
            for x in f.read(size):
                op = str(hex(x)).upper()[2:]
                if len(op) == 1:
                    op = '0' + op
                symbol_hexs = symbol_hexs + op
            item = {}
            item['name'] = symbol.name
            item['data'] = symbol_hexs
            dynsym_datas.append(item)
    return dynsym_datas


def zerocopy_from_numpy(np_array):
    """Create a tensor that shares the numpy array.

    Parameters
    ----------
    np_array : numpy.ndarray
        The numpy ndarray.

    Returns
    -------
    Tensor
        A framework-specific tensor.
    """
    pass


def escape_html(text):
    """
    Escapes all html characters in text

    :param str text:
    :rtype: str
    """
    return text.replace('&', '&').replace('<', '<').replace('>', '>')


def shape_key_retime():
    """Resets the timing for absolute shape keys

    """
    pass


def deregister_on_premises_instance(instanceName=None):
    """
    Deregisters an on-premises instance.
    See also: AWS API Documentation

    :example: response = client.deregister_on_premises_instance(
        instanceName='string'
    )

    :type instanceName: string
    :param instanceName: [REQUIRED]
            The name of the on-premises instance to deregister.

    """
    pass


def _run_wsgi_app(*args):
    """This function replaces itself to ensure that the test module is not
    imported unless required.  DO NOT USE!
    """
    global _run_wsgi_app
    from verktyg.test import run_wsgi_app as _run_wsgi_app
    return _run_wsgi_app(*args)


def _resolve_name(name, package, level):
    """Return the absolute name of the module to be imported."""
    if not hasattr(package, 'rindex'):
        raise ValueError("'package' not set to a string")
    dot = len(package)
    for x in range(level, 1, -1):
        try:
            dot = package.rindex('.', 0, dot)
        except ValueError:
            raise ValueError(
                'attempted relative import beyond top-level package')
    return '%s.%s' % (package[:dot], name)


def greater(attrs, inputs, proto_obj):
    """Logical Greater operator with broadcasting."""
    return 'broadcast_greater', attrs, inputs


def _create_combined_words(words, startindex):
    """
    Helper for create_match_bool, used to combine words inside single quotes from a list into a
    single string.
    :param words: List of words.
    :param startindex: Index where search is started.
    :return: (str, int) or (None, 0) if no closing quote is found.
    """
    for i, word in enumerate(words[startindex + 1:]):
        if "'" in word:
            return ' '.join(words[startindex:startindex + i + 2]), i + 1
    return None, 0


def getUsers():
    """Get a sequence of all user objects which reside in the user folder.
        """


def can_preview(file):
    """Determine if file can be previewed."""
    return file.is_local() and file.has_extensions('.md')


def remove_ansible_std_lines_lists(line):
    """Remove stdout_lines: [] list while taking into account nested run"""
    for i in ('stdout', 'stderr'):
        token = '"%s_lines": ' % i
        if '"%s": ' % i in line and token in line:
            start_pos = line.index(token)
            pos = start_pos + len(token)
            if line[pos:].startswith('[]'):
                continue
            if not line[pos:].startswith('["'):
                print("Ooops: couldn't find %s beginning '[' in %s" % (
                    token, line))
                return line
            quote = False
            escape = False
            while pos < len(line):
                if not escape:
                    if line[pos] == '"':
                        quote = not quote
                    if not quote and line[pos] == ']':
                        break
                if line[pos] == '\\':
                    escape = True
                else:
                    escape = False
                pos += 1
            if pos == len(line):
                print("Ooops: couldn't find %s ending ']' in %s" % (token,
                    line))
                return line
            line = line[:start_pos] + line[pos:]
            line.replace('"%s": ' % token, '\\n')
    return line


@staticmethod
def getWhereData(where):
    """where = None or where is str or where is dict like :{'name=':'testName', 'age>':18}
		"""
    if where is None:
        return '', None
    elif isinstance(where, dict):
        wStr = 'WHERE '
        wData = []
        count = 0
        for k, v in where.items():
            wStr += (count != 0 and 'AND ' or '') + k + '(?) '
            wData.append(v)
            count += 1
        return wStr, wData
    elif isinstance(where, str):
        if where.lower().lstrip().find('where ') != 0:
            where = 'WHERE ' + where
        return where, None


def _validate_center_shape(X, n_centers, centers):
    """Check if centers is compatible with X and n_centers"""
    if len(centers) != n_centers:
        raise ValueError(
            'The shape of the initial centers (%s) does not match the number of clusters %i'
             % (centers.shape, n_centers))
    if centers.shape[1] != X.shape[1]:
        raise ValueError(
            'The number of features of the initial centers %s does not match the number of features of the data %s.'
             % (centers.shape[1], X.shape[1]))


def to_int_or_zero(value):
    """
    Converts given value to an integer or returns 0 if it fails.

    :param value: Arbitrary data type.
    :rtype: int

    .. rubric:: Example

    >>> to_int_or_zero("12")
    12

    >>> to_int_or_zero("x")
    0
    """
    try:
        return int(value)
    except ValueError:
        return 0


def write_widget(obj):
    """ Write a mesh object as a python script for widget use.
    """
    script = ''
    script += (
        'def create_thing_widget(rig, bone_name, size=1.0, bone_transform_name=None):\n'
        )
    script += '    obj = create_widget(rig, bone_name, bone_transform_name)\n'
    script += '    if obj is not None:\n'
    if len(obj.data.vertices) > 0:
        script += '        verts = ['
        for v in obj.data.vertices:
            script += '(' + str(v.co[0]) + '*size, ' + str(v.co[1]
                ) + '*size, ' + str(v.co[2]) + '*size), '
        script += ']\n'
    if len(obj.data.edges) > 0:
        script += '        edges = ['
        for e in obj.data.edges:
            script += '(' + str(e.vertices[0]) + ', ' + str(e.vertices[1]
                ) + '), '
        script += ']\n'
    if len(obj.data.polygons) > 0:
        script += '        faces = ['
        for f in obj.data.polygons:
            script += '('
            for v in f.vertices:
                script += str(v) + ', '
            script += '), '
        script += ']\n'
    script += """
        mesh = obj.data
"""
    script += '        mesh.from_pydata(verts, edges, faces)\n'
    script += '        mesh.update()\n'
    script += '        mesh.update()\n'
    script += '        return obj\n'
    script += '    else:\n'
    script += '        return None\n'
    return script


def add(iterable, ignore=False):
    """Sums iterable. If ignore=True all elements except those that are None are added. If ignore=False, None is returned if any element is None. """
    x = None
    for i in iterable:
        if not i is None:
            if x is None:
                x = i
            else:
                x = x + i
        elif not ignore:
            return None
    return x


@staticmethod
def is_dynamo_stream_event(event):
    """
        Determines if the incoming event is a DynamoDB stream event.
        :param event: The incoming event
        :return: `True` if the event has records. `False` otherwise.
        """
    return 'Records' in event


def getHost():
    """
        Returns L{UNIXAddress}.
        """


@staticmethod
def write_target_build_event_of_setting(cmake_file, property_indent,
    config_condition_expr, property_value, width, **kwargs):
    """ Write target build event functor (helper) """
    del kwargs
    for command in property_value:
        cmake_file.write('{0}    COMMAND {1:>{width}} {2}\n'.format(
            property_indent, config_condition_expr, command, width=width))


def check_name(name, name_list):
    """
    Check the validity of name.

    :param name: test name
    :type name: str
    :param name_list: players names
    :type name_list: list
    :return: validity as bool
    """
    if len(name) != 0 and name not in name_list:
        return True
    return False


def changed(requestContext, seriesList):
    """
    Takes one metric or a wildcard seriesList.
    Output 1 when the value changed, 0 when null or the same
    Example::

        &target=changed(Server01.connections.handled)
    """
    for series in seriesList:
        series.name = series.pathExpression = 'changed(%s)' % series.name
        previous = None
        for index, value in enumerate(series):
            if previous is None:
                series[index] = 0
            elif value is not None and previous != value:
                series[index] = 1
            else:
                series[index] = 0
            previous = value
    return seriesList


@classmethod
def collect_tokens(cls, parseresult, mode):
    """
        Collect the tokens from a (potentially) nested parse result.
        """
    inner = '(%s)' if mode == 'parens' else '[%s]'
    if parseresult is None:
        return []
    tokens = []
    for token in parseresult.asList():
        if isinstance(token, list):
            token = cls.recurse_token(token, inner)
            tokens[-1] = tokens[-1] + token
        else:
            if token.strip() == ',':
                continue
            tokens.append(cls._strip_commas(token))
    return tokens


def verify_node_uuid_uniqueness(apps, schema_editor):
    """Check whether the database contains nodes with duplicate UUIDS.

    Note that we have to redefine this method from aiida.manage.database.integrity.verify_node_uuid_uniqueness
    because the migrations.RunPython command that will invoke this function, will pass two arguments and therefore
    this wrapper needs to have a different function signature.

    :raises: IntegrityError if database contains nodes with duplicate UUIDS.
    """
    from aiida.manage.database.integrity.duplicate_uuid import verify_uuid_uniqueness
    verify_uuid_uniqueness(table='db_dbnode')


def aCommon():
    """Return a string representing the object's value in the
        format: Mar 1, 1997 1:45 pm"""


def context_set_boolean(data_path: str='', value: bool=True):
    """Set a context value

    :param data_path: Context Attributes, RNA context string
    :type data_path: str
    :param value: Value, Assignment value
    :type value: bool
    """
    pass


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


def 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 get_num_elements(dtype, nelstr):
    """
    Checks if data type is an array or a structure.

    Parameters
    ----------
    dtype : str
        data type specifier

    nelstr : str
        number of elements

    Returns
    -------
    nel : int
        number of elements if type is *ARR or STRUCT. 0 otherwise

    isstruct : bool
        true if the type is STRUCT

    """
    uptype = dtype.upper()
    isstruct = False
    nel = 0
    narr = uptype.count('ARR')
    if uptype == 'STRUCT':
        isstruct = True
    if narr > 0 or isstruct is True:
        nel = int(nelstr[0])
    return nel, isstruct


def add_SBO(model):
    """adds SBO terms for demands and exchanges

    This works for models which follow the standard convention for
    constructing and naming these reactions.

    The reaction should only contain the single metabolite being exchanged,
    and the id should be EX_metid or DM_metid
    """
    for r in model.reactions:
        if r.annotation.get('sbo'):
            continue
        if len(r.metabolites) != 1:
            continue
        met_id = list(r._metabolites)[0].id
        if r.id.startswith('EX_') and r.id == 'EX_' + met_id:
            r.annotation['sbo'] = 'SBO:0000627'
        elif r.id.startswith('DM_') and r.id == 'DM_' + met_id:
            r.annotation['sbo'] = 'SBO:0000628'


@classmethod
def remove_class(cls, *args):
    """Remove classes from the group.

        Parameters
        ----------
        *args : `type`
            Classes to remove.
        """
    for cls2 in args:
        try:
            del cls.classes[cls2.__name__]
        except KeyError:
            pass


def new(cls, fileobj):
    """Associate a new VirtualFile with a read fileobject, return id"""
    count = cls.counter
    cls.vfiles[count] = fileobj
    cls.counter = count + 1
    return count


def filter_func(s):
    """
	A filter function to remove some unnecessary tags.
	"""
    if s.isspace() == True or len(s) < 1:
        return False
    else:
        return True


def playlist():
    """
    @api {post} /music/flaclist/playlist 热门标签
    @apiVersion 1.0.0
    @apiGroup Apelist
    @apiDescription 热门标签

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

    @apiSuccess (Success) {Object} datainfo 数据详情列表
    @apiSuccess (Success) {String} datainfo.genre 流派类型
    @apiSuccess (Success) {String} datainfo.bitrates 比特率
    @apiSuccess (Success) {String} datainfo.releasedate 发布日期
    @apiSuccess (Success) {String} datainfo.label 标签
    @apiSuccess (Success) {String} datainfo.exclusivity 歌曲版权付费模式（FREE，Normal，Purchase，ALAKA，Streaming，Cache）

    @apiSuccess (Success) {String} datainfo.translatename 翻译名称
    @apiSuccess (Success) {String} datainfo.version 版本
    @apiSuccess (Success) {String} datainfo.duration 歌曲长度
    @apiSuccess (Success) {String} datainfo.side 歌曲位于专辑的片（面）数
    @apiSuccess (Success) {String} datainfo.explicit 脏标？
    @apiSuccess (Success) {String} datainfo.description 描述
    @apiSuccess (Success) {String} datainfo.name 歌曲名
    @apiSuccess (Success) {String} datainfo.tracknumber 声音编号
    @apiSuccess (Success) {String} datainfo.rating 收听总数
    @apiSuccess (Success) {String} datainfo.cpItemID 出版公司单曲编号
    @apiSuccess (Success) {String} datainfo.pinyinname 歌曲拼音名称
    @apiSuccess (Success) {String} datainfo.languagetype 语言类型
    @apiSuccess (Success) {String} datainfo.priceCode 价格代号

    @apiSuccess (Success) {Object} artists 歌手列表
    @apiSuccess (Success) {String} artist.genre 歌手流派类型
    @apiSuccess (Success) {String} artist.region 歌手区域
    @apiSuccess (Success) {String} artist.birthday 歌手生日
    @apiSuccess (Success) {String} artist.itemcount 歌手单曲数量
    @apiSuccess (Success) {String} artist.translatename 歌手翻译名称
    @apiSuccess (Success) {String} artist.imagePathMap 歌手图像路径图？
    @apiSuccess (Success) {String} artist.imagePathMap.value 歌手图像路径图值？
    @apiSuccess (Success) {String} artist.imagePathMap.key 歌手图像路径图键？
    @apiSuccess (Success) {String} artist.artistID 歌手编号
    @apiSuccess (Success) {String} artist.description 歌手描述
    @apiSuccess (Success) {String} artist.name 歌手名称
    @apiSuccess (Success) {String} artist.gender 歌手性别
    @apiSuccess (Success) {String} artist.rating 歌手收听总数
    @apiSuccess (Success) {String} artist.pinyinname 歌手拼音名称
    @apiSuccess (Success) {String} artist.albumcount 歌手专辑数量

    @apiSuccess (Success) {Object} itemID 单曲编号

    @apiSuccess (Success) {Object} album 专辑列表
    @apiSuccess (Success) {String} album.genre 专辑流派类型
    @apiSuccess (Success) {String} album.cpInfo 专辑出版公司信息
    @apiSuccess (Success) {String} album.cpInfo.contentProviderGroupID 版权唱片公司分组编号？
    @apiSuccess (Success) {String} album.cpInfo.cpGroupName 出版公司组名？
    @apiSuccess (Success) {String} album.cpInfo.cpGroupAltName 出版公司组名更改名？
    @apiSuccess (Success) {String} album.cpInfo.cpName 出版公司名称
    @apiSuccess (Success) {String} album.cpInfo.cpCode 出版公司代号
    @apiSuccess (Success) {String} album.cpInfo.contentProviderID 版权唱片公司ID
    @apiSuccess (Success) {String} album.albumID 专辑编号
    @apiSuccess (Success) {String} album.releasedate 专辑发布日期
    @apiSuccess (Success) {String} album.translatename 专辑翻译名称
    @apiSuccess (Success) {String} album.version 专辑版本
    @apiSuccess (Success) {String} album.artists 专辑歌手
    @apiSuccess (Success) {String} album.artists.artistname 专辑歌手名称
    @apiSuccess (Success) {String} album.artists.artistID 专辑歌手编号
    @apiSuccess (Success) {String} album.artists.artisttranslatename 专辑歌手翻译名称
    @apiSuccess (Success) {String} album.artists.artistpinyinname 专辑歌手拼音名称
    @apiSuccess (Success) {String} album.totaltrack 专辑总声音数量
    @apiSuccess (Success) {String} album.imagePathMap 专辑图像路径图
    @apiSuccess (Success) {String} album.imagePathMap.value 专辑图像路径图值
    @apiSuccess (Success) {String} album.imagePathMap.key 专辑图像路径图键
    @apiSuccess (Success) {String} album.explicit 专辑脏标
    @apiSuccess (Success) {String} album.albumPriceCode 专辑价格代号
    @apiSuccess (Success) {String} album.name 专辑名称
    @apiSuccess (Success) {String} album.totaldisk 专辑总磁盘
    @apiSuccess (Success) {String} album.rating 专辑收听总数
    @apiSuccess (Success) {String} album.pinyinname 专辑拼音名称
    @apiSuccess (Success) {String} album.languagetype 专辑语言类型

    @apiSuccess (Success) {Object} itemType 单曲类型

    @apiSuccessExample {json} Success-Response:
    {
        "data": [
            {
                "datainfo": {
                    "genre": "Pop",
                    "bitrates": [
                        "FLAC-1000K-FTD",
                        "LRC-LRC",
                        "MP3-128K-FTD",
                        "MP3-192K-FTD",
                        "MP3-256K-FTD",
                        "MP3-320K-FTD"
                    ],
                    "releasedate": "2014-12-26",
                    "label": "杰威尔音乐",
                    "exclusivity": 1,
                    "translatename": "天涯过客",
                    "version": "",
                    "duration": "04:13",
                    "side": 1,
                    "explicit": 0,
                    "description": "",
                    "name": "天涯过客",
                    "tracknumber": 4,
                    "rating": 257221,
                    "cpItemID": "m2015032713435_FTD",
                    "pinyinname": "Tian Ya Guo Ke",
                    "languagetype": "MAN",
                    "priceCode": ""
                },
                "artists": [
                    {
                        "genre": "Pop",
                        "region": "TW",
                        "birthday": "1979-01-18",
                        "itemcount": 415,
                        "translatename": "Jay Chou",
                        "imagePathMap": [
                            {
                                "value": "/artist/000/6478-PNG-100X100-ARTIST.png",
                                "key": "PNG-100X100-ARTIST"
                            }
                        ],
                        "artistID": 6478,
                        "description": "周杰伦（Jay Chou），1979年1月18日出生在台湾新北市，身兼华语男歌手、词曲创作人、制作人、导演、编剧、监制等。2000年发行首张专辑《Jay》出道，2002年在中国、新加坡、马来西亚、美国等地举办首场世界巡回演唱会。2003年登美国《时代周刊》封面人物。曾四次获得世界音乐大奖中国最畅销艺人奖，并凭借专辑《Jay》《范特西》《叶惠美》《跨时代》四次获金曲奖最佳国语专辑奖，又通过《魔杰座》《跨时代》两度获得金曲奖最佳男歌手奖。2014年、2015年两度获得QQ音乐年度盛典最佳全能艺人奖。2015年获得全球华语榜中榜亚洲最受欢迎全能艺人奖；同年担任《中国好声音第四季》导师。2005年以电影《头文字D》获台湾电影金马奖及香港电影金像奖最佳新人奖。2007年成立杰威尔有限公司，自编自导电影《不能说的秘密》获台湾电影金马奖年度杰出电影奖。2009年入选美国CNN亚洲极具影响力人物，2011年主演好莱坞电影《青蜂侠》进军国际，获美国MTV电影大奖最佳新人提名。2012年登福布斯中国名人榜榜首。2013年自编自导电影《天台爱情》获选纽约电影节闭幕片。2014年加盟好莱坞电影《惊天魔盗团2》。2015年监制电影《一万公里的约定》。演艺事业外，2011年担任华硕笔电设计师并入股香港文化传信集团。2012年开设真爱范特西连锁KTV。2013年受邀北京大学演讲。2016年个人品牌MRJ电商上市。除了个人事业外，周杰伦还热心慈善，多次向内地灾区捐款并募款新建希望小学。",
                        "name": "周杰伦",
                        "gender": "Male",
                        "rating": 91272320,
                        "pinyinname": "Zhou Jie Lun",
                        "albumcount": 38
                    }
                ],
                "itemID": 1247543,
                "album": {
                    "genre": "Pop",
                    "cpInfo": [
                        {
                            "contentProviderGroupID": 707,
                            "cpGroupName": "杰威尔音乐",
                            "cpGroupAltName": "杰威尔音乐",
                            "cpName": "杰威尔音乐",
                            "cpCode": "杰威尔音乐",
                            "contentProviderID": 6157
                        }
                    ],
                    "albumID": 122072,
                    "releasedate": "2014-12-26",
                    "translatename": "哎呦，不错哦",
                    "version": "",
                    "artists": [
                        {
                            "artistname": "周杰伦",
                            "artistID": 6478,
                            "artisttranslatename": "Jay Chou",
                            "artistpinyinname": "Zhou Jie Lun"
                        }
                    ],
                    "totaltrack": 12,
                    "imagePathMap": [
                        {
                            "value": "images/content/imagecache/{SIZE}/contentimages/album/012/122072-JPG-240X240-ALBUM.jpg",
                            "key": "image-pattern"
                        }
                    ],
                    "explicit": 0,
                    "albumPriceCode": -1,
                    "name": "哎呦，不错哦",
                    "totaldisk": 1,
                    "rating": 3213089,
                    "pinyinname": "Ai Yo，Bu Cuo O",
                    "languagetype": "MAN"
                },
                "itemType": "FTD"
            }
        ]
    }
    """


def derivative_listing_to_counts(derivatives, gdim):
    """Convert a derivative listing tuple to a derivative count tuple.

    The derivative d^3 / dy^2 dz is represented
    in counting form as (0, 2, 1) meaning (dx^0, dy^2, dz^1)
    and in listing form as (1, 1, 2) meaning (dy, dy, dz).
    """
    derivative_counts = [0] * gdim
    for d in derivatives:
        derivative_counts[d] += 1
    return tuple(derivative_counts)


def get_obj_coef(me, xid):
    """get_obj_coef with transparent index mapping."""
    if xid == 0:
        return me._get_obj_coef(xid)
    xid = me._colmap.map(xid) if xid else xid
    if xid > 0:
        return me._get_obj_coef(xid)


def cleanup_notifiers(notifiers):
    """Close notifiers instances when the process is killed
    """
    for notifier in notifiers.values():
        notifier.stop()


@staticmethod
def fromPmml(src, pmmlOptions=None, pfaOptions=None, version=None,
    sharedState=None, multiplicity=1, style='pure', debug=False):
    """Translates some types of PMML documents into PFA and creates a collection of scoring engine instances.

        :type src: string
        :param src: a PMML document in XML-serialized form; must be a string
        :type pmmlOptions: dict
        :param pmmlOptions: directives for interpreting the PMML document
        :type pfaOptions: dict
        :param pfaOptions: options that override those found in the PFA document
        :type version: string
        :param version: PFA version number as a "major.minor.release" string
        :type sharedState: titus.genpy.SharedState
        :param sharedState: external state for shared cells and pools to initialize from and modify; pass ``None`` to limit sharing to instances of a single PFA file
        :type multiplicity: positive integer
        :param multiplicity: number of instances to return (default is 1; a single-item collection)
        :type style: string
        :param style: style of scoring engine; only one currently supported: "pure" for pure-Python
        :type debug: bool
        :param debug: if ``True``, print the Python code generated by this PFA document before evaluating
        :rtype: PFAEngine
        :return: a list of scoring engine instances
        """
    raise NotImplementedError(
        'PMML to PFA conversion is not supported in titus2.')


def rndPair(geneclass):
    """
    Returns a gene pair, comprising two random
    instances of the given gene class
    """
    return geneclass(), geneclass()


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

        :param ShowcaseRestoredDetails val:
        :rtype: EventDetails
        """
    return cls('showcase_restored_details', val)


@classmethod
def from_tarball(kls, tarball, plugin_dir, upgrade=False):
    """Create a new package from tarball file

        :type tarball: str
        :param tarball: the filename of the tarball with the plugin

        :type plugin_dir: str
        :param plugin_dir: path to install the tarball

        :type upgrade: bool
        :param upgrade: if true, upgrade the package, otherwise failed
            if package is already installed.
        """
    with open(tarball, 'rb') as f:
        return kls.from_tarballfd(f, plugin_dir, upgrade)


def get_template_file_name(template_output, input_file_name):
    """
    Returns the template file name.

    :param template_output:
        Template output.
    :type template_output: str

    :param input_file_name:
        Input file name.
    :type input_file_name: str

    :return:
        Template file name.
    :rtype: str
    """
    if template_output == '-':
        return input_file_name
    return template_output


@classmethod
def enumerate_it(cls, data, start=0):
    """Return enumerate object"""
    if cls.isDict(data) or cls.isList(data):
        if cls.isDict(data):
            data = data.items()
        return list(enumerate(data, start=start))
    raise NotImplementedError('datatype is not a dictionary or list ')


def print_helpers(parser, key):
    """
    Function that prints the helpers of a certain command

    :param parser: The parser which has all the helpers defined in a dict
    :param key: The command which helper is called
    :return: The message of the helper or a message that it has no helpers
    """
    if len(parser.helpers[key]) != 0:
        msg = 'Las opciones para el comando ' + key + ' son: -'
        for opcion in parser.helpers[key]:
            msg += opcion + ' '
    else:
        msg = 'El comando no tiene opciones'
    return msg


def _load_keras_model(model_network_path, model_weight_path, custom_objects
    =None):
    """Load a keras model from disk

    Parameters
    ----------
    model_network_path: str
        Path where the model network path is (json file)

    model_weight_path: str
        Path where the model network weights are (hd5 file)

    custom_objects:
        A dictionary of layers or other custom classes
        or functions used by the model

    Returns
    -------
    model: A keras model
    """
    from keras.models import model_from_json
    import json
    json_file = open(model_network_path, 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    if not custom_objects:
        custom_objects = {}
    loaded_model = model_from_json(loaded_model_json, custom_objects=
        custom_objects)
    loaded_model.load_weights(model_weight_path)
    return loaded_model


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

    The ".dev0" means dirty.

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


def check_ten_percent_voltage_deviation(network):
    """
    Checks if 10% criteria is exceeded.

    Parameters
    ----------
    network : :class:`~.grid.network.Network`

    """
    v_mag_pu_pfa = network.results.v_res()
    if (v_mag_pu_pfa > 1.1).any().any() or (v_mag_pu_pfa < 0.9).any().any():
        message = 'Maximum allowed voltage deviation of 10% exceeded.'
        raise ValueError(message)


@classmethod
def set_parameter(cls, options=None):
    """
        Creates or updates parameter for a record.
        """
    cls.command_sub = 'set-parameter'
    result = cls.execute(cls._construct_command(options))
    return result


def output_file_remove_active_socket():
    """Remove active input from a file output node

    """
    pass


def psi_H_Na_SO4_HMW84(T, P):
    """c-c'-a: hydrogen sodium sulfate [HMW84]."""
    psi = 0.0
    valid = T == 298.15
    return psi, valid


def __unused__tryresolve_conflicts(conflicts, decisions):
    """Try to resolve conflicts with strategies.

    conflicts is a list of tuples on the format
    (path, local_diff, remote_diff, strategy)

    Decisions will be added to the given decisions builder.

    Remaining conflicts will be returned.
    """
    unresolved_conflicts = []
    for conf in conflicts:
        path, ld, rd, strategy = conf
        action_taken = decisions.tryresolve(path, ld, rd, strategy=strategy)
        if not action_taken:
            unresolved_conflicts.append(conf)
    return unresolved_conflicts


def sync_seller_v2_stats_for_day(config, state, stream, sdk_client, token,
    start, metrics, report_dimensions):
    """Sync and output Criteo SellerV2Stats endpoint for one day."""
    pass


def _figsize(profiles, height):
    """Compute size of fig given height."""
    shape = profiles.data.shape[1:]
    count = profiles.data.shape[0]
    hw_ratio = shape[1] / shape[0]
    width = height * hw_ratio * count
    return width, 1.1 * height


def view_zoom_out(location: float=(0.0, 0.0)):
    """Zoom out the view

    :param location: Location, Cursor location in normalized (0.0-1.0) coordinates
    :type location: float
    """
    pass


def _get_param(config, name):
    """Retrieve stringified option from parameters or ini config."""
    value = config.getoption(name)
    if value and isinstance(value, list):
        return ' '.join(value)
    if not value or value == '':
        value = config.getini(name)
    if value is None:
        return ''
    return value


def setup(*args, **kwds):
    """
    Compatibility wrapper.
    """
    try:
        from setuptools import setup
    except ImportError:
        from distutils.core import setup
    return setup(*args, **kwds)


def kwargs_repr(kwargs, **options):
    """Format positional arguments to use in exception handling.

    :params kwargs: dict as obtained in arguments when declared as
           ``**kwargs``.

    :param options: some extra options are used in this function.

           - count: maximum amount of actual parameters to process; after
             reached that amount a tail item is appended with the remainder
             number.  If None is given -the default- the value ``3`` is used.

           - cast: must be a function to convert the value into the
             representation; when no value is given -the default- it's assumed
             ``λ arg: type(arg).__name__``.

           - item_format: the format for each argument type, if no value is
             given the value "{}:{}" is used.  Each item value is
             obtained as with the "cast" function.

           - tail_format: a format string for the tail with the remainder (not
             processed arguments) specification; must contain a positional
             (like "{}") format specifier if obtaining the remainder count is
             desired; when no value is given -the default- the value "..." is
             used; another valid example could be "and {} more".

           - joiner: could be a function or a string to join all parts
             including the tail; if no value is given -the default- the value
             ", " is used (thed equivalent to ``', '.join``).

    For example::

      >>> kwargs_repr({'x': 1, 'y': 2.0, 'z': '3', 'zz': {}})
      'x:int, y:float, z:str, ...'

    """
    count = options.get('count', 3)
    cast = options.get('cast', lambda arg: type(arg).__name__)
    item_format = options.get('item_format', '{}:{}')
    tail_format = options.get('tail_format', '...')
    joiner = options.get('joiner', ', ')
    if isinstance(joiner, str):
        joiner = str(joiner).join
    parts = []
    keys = list(kwargs)
    keys.sort()
    i = 0
    while i < min(count, len(keys)):
        key = keys[i]
        value = kwargs[key]
        parts.append(item_format.format(key, cast(value)))
        i += 1
    rem = len(keys) - i
    if rem > 0:
        parts.append(tail_format.format(rem))
    return joiner(parts)


def init_meta_wishes(manager):
    """Meta wishes usefull with assignement."""
    if 'stream_tables' in manager.config:
        header = manager.config['stream_tables']
        manager.meta['_wishes'] = [None for i in range(len(manager.students))]
        for i in range(len(header)):
            if header[i] == '':
                break
            else:
                student_names = manager.meta[header[i]]
                for student_name in student_names:
                    student = manager.students.get_by_column(0, student_name[0]
                        )
                    student_index = manager.students.body.index(student)
                    manager.meta['_wishes'][student_index] = i


@classmethod
def CreateDerived(cls, category_to_unit_and_exps, resulting_class=None,
    unknown_unit_caption=None):
    """
        Same as _CreateDerived, but always validating the category and units.

        .. see:: _CreateDerived for parameters.
        """
    return cls._CreateDerived(category_to_unit_and_exps, resulting_class,
        unknown_unit_caption=unknown_unit_caption)


def s_sets(p_map, p_key_f, p_key_b, p_value):
    """
    s_sets(p_map, p_key_f, p_key_b, p_value)->None
    """
    try:
        if p_key_f not in p_map:
            p_map[p_key_f] = {p_key_b: p_value}
        else:
            p_map[p_key_f][p_key_b] = p_value
    except:
        return


def _clean_page_wheel(text):
    """
    remove unexpected characters

    @param      text        string
    @return                 string
    """
    text = text.replace('"', "'")
    text = text.replace('‑', '-')
    text = text.replace('.', '.')
    text = text.replace(' · ', '-')
    text = text.replace('–', '-')
    return text


@classmethod
def cookieJar(cls):
    """
        Class method to get a reference to the cookie jar.

        @return reference to the cookie jar (CookieJar)
        """
    if cls._cookieJar is None:
        from .CookieJar.CookieJar import CookieJar
        cls._cookieJar = CookieJar()
    return cls._cookieJar


def printable(zelf, keys=[], skip=[], nokeys=False):
    """ Determine from provided keys list and/or from skipping from a skiplist a displayable string from those attributes. """
    keys = keys or zelf.keys()
    result = []
    for key in keys:
        if key == 'default':
            continue
        if key.startswith('_'):
            continue
        if key in skip:
            continue
        if nokeys:
            result.append('%4s' % zelf[key])
        else:
            result.append('%-8s %3s' % (key, zelf[key]))
    return ' '.join(reversed(sorted(result)))


@classmethod
def makeObservationInfo(cls, **kwargs):
    """Construct an `ObservationInfo` from the supplied parameters.

        Notes
        -----
        The supplied parameters should use names matching the property.
        The type of the supplied value will be checked against the property.
        Any properties not supplied will be assigned a value of `None`.

        Raises
        ------
        KeyError
            Raised if a supplied parameter key is not a known property.
        TypeError
            Raised if a supplied value does not match the expected type
            of the property.
        """
    obsinfo = cls(None)
    unused = set(kwargs)
    for p in cls._PROPERTIES:
        if p in kwargs:
            property = f'_{p}'
            value = kwargs[p]
            if not cls._is_property_ok(p, value):
                raise TypeError(
                    f'Supplied value {value} for property {p} should be of class {cls._PROPERTIES[p][1]} not {value.__class__}'
                    )
            setattr(obsinfo, property, value)
            unused.remove(p)
    if unused:
        raise KeyError(f"Unrecognized properties provided: {', '.join(unused)}"
            )
    return obsinfo


def to_utc(df):
    """
    For use in tests; applied UTC timestamp to DataFrame.
    """
    try:
        df.index = df.index.tz_localize('UTC')
    except TypeError:
        df.index = df.index.tz_convert('UTC')
    return df


def compare_labels(old_labels, new_labels):
    """
    Compares two lists of labels to see if any of them are different
    """
    for i in range(len(old_labels)):
        ol = old_labels[i]
        nl = new_labels[i]
        if ol != nl:
            return False
    return True


def context_scale_int(data_path: str='', value: float=1.0, always_step:
    bool=True):
    """Scale an int context value

    :param data_path: Context Attributes, RNA context string
    :type data_path: str
    :param value: Value, Assign value
    :type value: float
    :param always_step: Always Step, Always adjust the value by a minimum of 1 when ‘value’ is not 1.0
    :type always_step: bool
    """
    pass


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

        :param ApiSessionLogInfo val:
        :rtype: AccessMethodLogInfo
        """
    return cls('api', val)


def two_sum(a, b):
    """
    Add ``a`` and ``b`` exactly, returning the result as two float64s.
    The first is the approximate sum (with some floating point error)
    and the second is the error of the float64 sum.

    Using the procedure of Shewchuk, 1997,
    Discrete & Computational Geometry 18(3):305-363
    http://www.cs.berkeley.edu/~jrs/papers/robustr.pdf

    Returns
    -------
    sum, err : float64
        Approximate sum of a + b and the exact floating point error
    """
    x = a + b
    eb = x - a
    ea = x - eb
    eb = b - eb
    ea = a - ea
    return x, ea + eb


def to_utf8(y):
    """
    converts an array of integers to utf8 string
    """
    out = []
    for x in y:
        if x < 128:
            out.append(x)
        elif x < 2048:
            out.append(x >> 6 | 192)
            out.append(x & 63 | 128)
        elif x < 65536:
            out.append(x >> 12 | 224)
            out.append(x >> 6 & 63 | 128)
            out.append(x & 63 | 128)
        else:
            out.append(x >> 18 | 240)
            out.append(x >> 12 & 63)
            out.append(x >> 6 & 63 | 128)
            out.append(x & 63 | 128)
    return ''.join(map(chr, out))


@classmethod
def from_getpeername(cls, sock):
    """Creates a :class:`PtrLookup` object based on the IP address of the
        socket's remote address, using :py:meth:`~socket.socket.getpeername`.

        :param sock: The :py:class:`~socket.socket` object to use.
        :returns: A tuple containing the new :class:`PtrLookup` object and the
                  port number from :py:meth:`~socket.socket.getpeername`.

        """
    ip, port = sock.getpeername()
    return cls(ip), port


def p_fun_call_2(p):
    """FunctionCall : function_name LPAREN ArgumentList RPAREN"""
    p[0] = 'function_call', p[1], p[3]


@staticmethod
def snake2camel(letters, delimiter='_'):
    """
        Allow one or more letters convert into CamelCase. (單、複數詞組由"底線"轉"駝峰"")
        'my_model_name' -> 'MyModelName'
        :param letters: input a word. (輸入單詞)
        :param delimiter: customize a split symbol, defual='_'. (指定分隔符，預設='_')
        :return: converted string
        """
    if delimiter not in letters:
        return letters.title()
    return letters.title().replace(delimiter, '')


def def_grads(prims):
    """ Define gradients of primitives """
    prims('normal').def_grad_zero()
    prims('uniform').def_grad_zero()


def jaccard(sample1, sample2):
    """Jaccard similarity between two sets"""
    intersection = len(sample1.intersection(sample2))
    union = len(sample1.union(sample2))
    return intersection / union


def recover():
    """Roll back an interrupted transaction."""


def loop_colors():
    """Provides a looping list of matplotlib colors."""
    return ('red', 'blue', 'green', 'orange', 'purple', 'brown', 'black',
        'cyan', 'yellow')


@classmethod
def get_shapedef(cls, name, params):
    """Get a previously-registered shape of any type by its name"""
    if name in cls.convex_polyhedron_functions:
        return cls.convex_polyhedron_shapedef(name, params)
    elif name in cls.polygon_functions:
        return cls.polygon_shapedef(name, params)
    else:
        raise NotImplementedError()


@classmethod
def is_metadata_filename(cls, fname):
    """
        Returns if the filename is a metadata filename (ends in .data.json).
        """
    return fname.endswith('data.json')


def add_slash(text: str):
    """returns the same text with slash at the end"""
    return text + '/'


def urijoin(*args):
    """Joins given arguments into a URI.

    Trailing and leading slashes are stripped for each argument.

    This code is based on a Rune Kaagaard's answer on Stack Overflow.
    See http://stackoverflow.com/questions/1793261 for more into. The
    code was licensed as cc by-sa 3.0.

    :params *args: list of arguments to join

    :returns: a URI string
    """
    return '/'.join(map(lambda x: str(x).strip('/'), args))


def generate_lists_k_elments_from_list(original_list: list, k: int) ->list:
    """
	Splits a long list in smaller lists of k elements
	"""
    for i in range(0, len(original_list), k):
        yield original_list[i:i + k]


def html_entities_2_standard_html(html_body):
    """
    将html实体名称/实体编号转为html标签
    :param html_body:
    :return:
    """
    char_entities = (' ', ' '), (' ', ' '), ('<', '<'), ('<',
        '<'), ('>', '>'), ('>', '>'), ('&', '&'), ('&', '&'), (
        '"', '"'), ('"', '"')
    for item in char_entities:
        html_body = html_body.replace(item[0], item[1])
    return html_body


def __init__(x):
    """The constructor should set the attribute `x`."""


def is_item(obj):
    """Determine if the object is item-like."""
    return hasattr(obj, 'text')


@staticmethod
def _format_value_to_lower_case(value):
    """
        Formats and replaces values with escape character into value without quotes
        :param value: str
        :return: str
        """
    values = []
    if isinstance(value, list):
        for each in value:
            values.append('{}'.format(each).lower())
        value = values
    else:
        value = value.lower()
    return value


def aggregate_ravens(data, sub_num):
    """
    Aggregate data from the Raven's Matrices task.

    Calculates various summary statistics for the Raven's Matrices task for a
    given subject.

    Parameters
    ----------
    data : dataframe
        Pandas dataframe containing a single subjects trial data for the task.
    sub_num : str
        Subject number to which the data file belongs.

    Returns
    -------
    stats : list
        List containing the calculated data for the subject.
    """
    ravens_rt = data['RT'].mean()
    ravens_count = data['correct'].sum()
    ravens_num_items = data.shape[0]
    ravens_prop = ravens_count / ravens_num_items
    return [sub_num, ravens_rt, ravens_count, ravens_prop, ravens_num_items]


def _getversion(dictionary):
    """
    Returns the version of the rendering settings format.
    Note: Previously min/max was used to set the channel window start/end
    From version 2 on start/end will be used.

    Parameters:
    dictionary (dictionary): The rendering settings as dictionary

    Returns:
    int: The version or 0 if it cannot be determined
    """
    if 'version' not in dictionary:
        for chindex, chdict in dictionary['channels'].items():
            if ('start' in chdict or 'end' in chdict) and ('min' in chdict or
                'max' in chdict):
                return 0
            if 'start' in chdict or 'end' in chdict:
                return 2
            if 'min' in chdict or 'max' in chdict:
                return 1
    else:
        return dictionary['version']
    return 0


def ekm_print_log(output_string):
    """ Simple print predefined module level logging callback.

    Args:
        output_string (str): string to output.

    Returns:

    """
    print(output_string)
    pass


def to_csv(df, filepath, sep=',', header=True, index=True):
    """Save DataFrame as csv.

    Note data is expected to be evaluated.

    Currently delegates to Pandas.

    Parameters
    ----------
    df : DataFrame
    filepath : str
    sep : str, optional
        Separator used between values.
    header : bool, optional
        Whether to save the header.
    index : bool, optional
        Whether to save the index columns.

    Returns
    -------
    None

    See Also
    --------
    pandas.DataFrame.to_csv : https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html

    """
    df.to_pandas().to_csv(filepath, sep=sep, header=header, index=index)


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

        :param ShowcaseAddMemberType val:
        :rtype: EventType
        """
    return cls('showcase_add_member', val)


def glNormal3(nx, ny, nz, v: 'Buffer'):
    """Set the current normal vector

    :param nz: Specify the x, y, and z coordinates of the new current normal. The initial value of the current normal is the unit vector, (0, 0, 1).
    :param v: Specifies a pointer to an array of three elements: the x, y, and z coordinates of the new current normal.
    :type v: 'Buffer'
    """
    pass


@classmethod
def from_node(cls, node):
    """
        Create an :class:`NodeItem` instance and initialize it from a
        :class:`SchemeNode` instance.

        """
    self = cls()
    self.setWidgetDescription(node.description)
    return self


@staticmethod
def metadata_id(item):
    """Extracts the identifier from a Launchpad item."""
    return str(item['bug_data']['id'])


@staticmethod
def p1_comes_after_p2(p1: str, p2: str) ->bool:
    """
        p1 and p2 are both paths (str format). This method is
        a basic comparison method to check if the first parameter p1
        is striclty a parent path of the second parameter p2.

        Returns ``False`` if both paths are identical.
        """
    if not p1.endswith('/'):
        p1 = p1 + '/'
    if not p2.endswith('/'):
        p2 = p2 + '/'
    return p1.startswith(p2) and p1 != p2


def is_active(obj):
    """Helper to test if an object is an active part of the zillion config"""
    if not getattr(obj, 'zillion', None):
        return False
    if not obj.zillion.active:
        return False
    return True


@classmethod
def supports_metadata(cls):
    """returns True if this audio type supports MetaData"""
    return True


def is_valid_topology(ext):
    """ Checks if trajectory format is compatible with Cpptraj """
    formats = 'top', 'pdb', 'prmtop', 'parmtop', 'zip'
    return ext in formats


def put_evaluations(Evaluations=None, ResultToken=None, TestMode=None):
    """
    Used by an AWS Lambda function to deliver evaluation results to AWS Config. This action is required in every AWS Lambda function that is invoked by an AWS Config rule.
    See also: AWS API Documentation

    :example: response = client.put_evaluations(
        Evaluations=[
            {
                'ComplianceResourceType': 'string',
                'ComplianceResourceId': 'string',
                'ComplianceType': 'COMPLIANT'|'NON_COMPLIANT'|'NOT_APPLICABLE'|'INSUFFICIENT_DATA',
                'Annotation': 'string',
                'OrderingTimestamp': datetime(2015, 1, 1)
            },
        ],
        ResultToken='string',
        TestMode=True|False
    )

    :type Evaluations: list
    :param Evaluations: The assessments that the AWS Lambda function performs. Each evaluation identifies an AWS resource and indicates whether it complies with the AWS Config rule that invokes the AWS Lambda function.
            (dict) --Identifies an AWS resource and indicates whether it complies with the AWS Config rule that it was evaluated against.
            ComplianceResourceType (string) -- [REQUIRED]The type of AWS resource that was evaluated.
            ComplianceResourceId (string) -- [REQUIRED]The ID of the AWS resource that was evaluated.
            ComplianceType (string) -- [REQUIRED]Indicates whether the AWS resource complies with the AWS Config rule that it was evaluated against.
            For the Evaluation data type, AWS Config supports only the COMPLIANT , NON_COMPLIANT , and NOT_APPLICABLE values. AWS Config does not support the INSUFFICIENT_DATA value for this data type.
            Similarly, AWS Config does not accept INSUFFICIENT_DATA as the value for ComplianceType from a PutEvaluations request. For example, an AWS Lambda function for a custom Config rule cannot pass an INSUFFICIENT_DATA value to AWS Config.
            Annotation (string) --Supplementary information about how the evaluation determined the compliance.
            OrderingTimestamp (datetime) -- [REQUIRED]The time of the event in AWS Config that triggered the evaluation. For event-based evaluations, the time indicates when AWS Config created the configuration item that triggered the evaluation. For periodic evaluations, the time indicates when AWS Config triggered the evaluation at the frequency that you specified (for example, every 24 hours).

    :type ResultToken: string
    :param ResultToken: [REQUIRED]
            An encrypted token that associates an evaluation with an AWS Config rule. Identifies the rule and the event that triggered the evaluation

    :type TestMode: boolean
    :param TestMode: Use this parameter to specify a test run for PutEvaluations . You can verify whether your AWS Lambda function will deliver evaluation results to AWS Config. No updates occur to your existing evaluations, and evaluation results are not sent to AWS Config.
            Note
            When TestMode is true , PutEvaluations doesn't require a valid value for the ResultToken parameter, but the value cannot be null.

    :rtype: dict
    :return: {
        'FailedEvaluations': [
            {
                'ComplianceResourceType': 'string',
                'ComplianceResourceId': 'string',
                'ComplianceType': 'COMPLIANT'|'NON_COMPLIANT'|'NOT_APPLICABLE'|'INSUFFICIENT_DATA',
                'Annotation': 'string',
                'OrderingTimestamp': datetime(2015, 1, 1)
            },
        ]
    }

    """
    pass


def db():
    """Return mongodb collection"""


def converter(query):
    """method for decoding urls <query>:<str>"""
    try:
        from . import url_coder
    except ImportError:
        import url_coder
    query = url_coder.decoder(str(query))
    return query