@classmethod
def set_property(cls, target, callable, name=None, reify=False):
    """A helper method to apply a single property to an instance."""
    prop = cls.make_property(callable, name=name, reify=reify)
    cls.apply_properties(target, [prop])


def byte_align(size, alignment):
    """Returns the int larger than ``size`` aligned to ``alginment`` bytes."""
    mask = alignment - 1
    if size & mask == 0:
        return size
    else:
        return (size | mask) + 1


def clean_resource_json(resource_json):
    """ The catalog wants to be smaller, let's drop some stuff """
    for a in ('parent_docname', 'parent', 'template', 'repr', 'series'):
        if a in resource_json:
            del resource_json[a]
    props = resource_json['props']
    for prop in ('acquireds', 'style', 'in_nav', 'nav_title', 'weight',
        'auto_excerpt'):
        if prop in props:
            del props[prop]
    return resource_json


def zerocopy_from_dlpack(dlpack_tensor):
    """Create a tensor that shares the dlpack_tensor.

    Parameters
    ----------
    dlpack_tensor : dlpack capsule
        The dlpack tensor.

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


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 from_api(cls, api_result):
    """Construct a new instance from the result of a taskcluster API call"""
    return cls(roleId=api_result['roleId'], description=api_result[
        'description'], scopes=api_result['scopes'])


def clear_parameters(apps, schema_editor):
    """Reverse operation."""
    pass


@classmethod
def get_base_naive(cls, name, ignore=''):
    """ Checks a string for a possible base name of an object (no prefix, no suffix).
            We need to do a full parse to make sure we've ruled out all the possible other parse queries

        :param name: str, string that represents a possible name of an object
        :return: str, the detected basename
        """
    return cls._get_regex_search(name, cls.REGEX_BASENAME, match_index=0,
        ignore=ignore)


@classmethod
def get_default_option(cls):
    """Get default option for migration strategies."""
    return cls.IGNORE.name


def watch_url(video_id):
    """Construct a sanitized YouTube watch url, given a video id.

    :param str video_id:
        A YouTube video identifier.
    :rtype: str
    :returns:
        Sanitized YouTube watch url.
    """
    return 'https://youtube.com/watch?v=' + video_id


def get(isdsAppliance, check_mode=False, force=False):
    """
    Get current configured server type
    """
    return isdsAppliance.invoke_get('Retrieving Server Status',
        '/widgets/server')


@classmethod
def from_file(cls, file_path):
    """Creates a :class:`SimulationInput` by loading a file

        :param file_path: The file path to load
        :type file_path: path-like

        :returns: The simulation input object
        :rtype: An instance of :class:`SimulationInput`
        """
    raise NotImplementedError()


def get_field_by_name_or_raise(model_class, field_name):
    """Get field by name, including inherited fields and M2M fields."""
    return model_class._meta.get_field(field_name)


@classmethod
def get_fields(cls, fields=None, excludes=None):
    """
        Given any explicit fields to include and fields to exclude, add
        additional fields based on the associated model.
        """
    final_fields = {}
    fields = fields or []
    excludes = excludes or []
    if not cls._meta.object_class:
        return final_fields
    for f in cls._meta.object_class._meta.fields:
        if f.name in cls.base_fields:
            continue
        if f.name not in fields:
            continue
        if f.name in excludes:
            continue
        if cls.should_skip_field(f):
            continue
        api_field_class = cls.api_field_from_django_field(f)
        kwargs = {'attribute': f.name, 'help_text': f.help_text,
            'verbose_name': f.verbose_name}
        if f.null is True:
            kwargs['null'] = True
        kwargs['unique'] = f.unique
        if not f.null and f.blank is True:
            kwargs['default'] = ''
            kwargs['blank'] = True
        if f.get_internal_type() == 'TextField':
            kwargs['default'] = ''
        if f.has_default():
            kwargs['default'] = f.default
        if getattr(f, 'auto_now', False):
            kwargs['default'] = f.auto_now
        if getattr(f, 'auto_now_add', False):
            kwargs['default'] = f.auto_now_add
        final_fields[f.name] = api_field_class(**kwargs)
        final_fields[f.name].instance_name = f.name
    return final_fields


@classmethod
def find_by_id(cls, object_id):
    """
        Find an object identified by the unique database id
        :param object_id: the database id
        :return:
        """
    raise NotImplemented('abstract method')


def dashboardActivities():
    """Return list of dashboard activities"""
    activity = ['reprocessing', 'production', 'relval', 'harvesting',
        'storeresults', 'integration', 'test']
    return activity


def item_prefix(item):
    """
    Get the item prefix ('+','++','-','--','').
    """
    if item.startswith('++'):
        prefix = '++'
    elif item.startswith('+'):
        prefix = '+'
    elif item.startswith('--'):
        prefix = '--'
    elif item.startswith('-'):
        prefix = '-'
    else:
        prefix = ''
    return prefix


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

        :param MemberRequestsChangePolicyDetails val:
        :rtype: EventDetails
        """
    return cls('member_requests_change_policy_details', val)


def predict_angles(p0_star, experiment, s0=None):
    """Predict Ewald sphere crossing angle for RLP x, returned in order
    (entering, exiting)."""
    from scitbx import matrix
    import math
    a = matrix.col(experiment.goniometer.get_rotation_axis())
    if s0 is None:
        b = matrix.col(experiment.beam.get_s0())
    else:
        b = s0
    m2 = a.normalize()
    m1 = m2.cross(b.normalize())
    m3 = m1.cross(m2)
    p0_sqr = p0_star.dot(p0_star)
    rho = math.sqrt(p0_sqr - p0_star.dot(m2) ** 2)
    p_star_m3 = (-0.5 * p0_sqr - p0_star.dot(m2) * b.dot(m2)) / b.dot(m3)
    p_star_m2 = p0_star.dot(m2)
    if rho ** 2 < p_star_m3 ** 2:
        return None
    p_star_m1 = math.sqrt(rho ** 2 - p_star_m3 ** 2)
    p0_star_m1 = p0_star.dot(m1)
    p0_star_m2 = p0_star.dot(m2)
    p0_star_m3 = p0_star.dot(m3)
    cp1 = +p_star_m1 * p0_star_m1 + p_star_m3 * p0_star_m3
    cp2 = -p_star_m1 * p0_star_m1 + p_star_m3 * p0_star_m3
    sp1 = +p_star_m1 * p0_star_m3 - p_star_m3 * p0_star_m1
    sp2 = -p_star_m1 * p0_star_m3 - p_star_m3 * p0_star_m1
    return math.atan2(sp1, cp1), math.atan2(sp2, cp2)


def get_local_fields(model):
    """Returns all local fields!?"""
    return model._meta.local_fields


def combine(operator, unit, zero, domains):
    """Returns a new domain expression where all domain components from ``domains``
       have been added together using the binary operator ``operator``. The given
       domains must be normalized.

       :param unit: the identity element of the domains "set" with regard to the operation
                    performed by ``operator``, i.e the domain component ``i`` which, when
                    combined with any domain ``x`` via ``operator``, yields ``x``.
                    E.g. [(1,'=',1)] is the typical unit for AND_OPERATOR: adding it
                    to any domain component gives the same domain.
       :param zero: the absorbing element of the domains "set" with regard to the operation
                    performed by ``operator``, i.e the domain component ``z`` which, when
                    combined with any domain ``x`` via ``operator``, yields ``z``.
                    E.g. [(1,'=',1)] is the typical zero for OR_OPERATOR: as soon as
                    you see it in a domain component the resulting domain is the zero.
       :param domains: a list of normalized domains.
    """
    result = []
    count = 0
    for domain in domains:
        if domain == unit:
            continue
        if domain == zero:
            return zero
        if domain:
            result += domain
            count += 1
    result = [operator] * (count - 1) + result
    return result


@staticmethod
def ftilde(logx, settings):
    """
        Helper function for finding the analytic value of the estimator.
        See "check_by_integrating" docstring for more details.

        ftilde(X) is mean of f(theta) on the iso-likelihood contour
        L(theta) = L(X).
        """
    r = settings.r_given_logx(logx)
    return r ** 2 / settings.n_dim


def get_filenames(ruleset_db, rule):
    """returns a list of all filenames for a rule in a ruleset"""
    from src.praxxis.sqlite import connection
    conn = connection.create_connection(ruleset_db)
    cur = conn.cursor()
    list_filenames = 'SELECT Filename FROM "Filenames" WHERE Rule = ?'
    cur.execute(list_filenames, (rule,))
    conn.commit()
    filenames = cur.fetchall()
    conn.close()
    return filenames


def process(terms):
    """ Transform each term in terms.

        Return the sequence of transformed terms.
        """


def inc(fp):
    """
    Create an table of contents of the article titles.

    :param fp: File-like object of XML
    :returns: Stream of of article title, file offset pairs
        (File offset is for the beginning of the line.)
    :rtype: iterable
    """
    padleft = len(b'    <title>')
    padright = len(b'</title>\n')
    out = {}
    i = fp.tell()
    for line in fp:
        if line.startswith(b'    <title>'):
            yield line[padleft:-padright], i
        elif line == b'  </page>\n':
            i = fp.tell()


@staticmethod
def build_energy_dict(r: dict) ->dict:
    """Build energy dictionary from API response."""
    return {'energy_consumption_of_today': r.get('energyConsumptionOfToday',
        0), 'cost_per_kwh': r.get('costPerKWH', 0), 'max_energy': r.get(
        'maxEnergy', 0), 'total_energy': r.get('totalEnergy', 0),
        'currency': r.get('currency', 0), 'data': r.get('data', 0)}


def create_vault(accountId=None, vaultName=None):
    """
    This operation creates a new vault with the specified name. The name of the vault must be unique within a region for an AWS account. You can create up to 1,000 vaults per account. If you need to create more vaults, contact Amazon Glacier.
    You must use the following guidelines when naming a vault.
    This operation is idempotent.
    An AWS account has full permission to perform all operations (actions). However, AWS Identity and Access Management (IAM) users don't have any permissions by default. You must grant them explicit permission to perform specific actions. For more information, see Access Control Using AWS Identity and Access Management (IAM) .
    For conceptual information and underlying REST API, see Creating a Vault in Amazon Glacier and Create Vault in the Amazon Glacier Developer Guide .
    See also: AWS API Documentation

    Examples
    The following example creates a new vault named my-vault.
    Expected Output:

    :example: response = client.create_vault(
        vaultName='string'
    )

    :type accountId: string
    :param accountId: The AccountId value is the AWS account ID. This value must match the AWS account ID associated with the credentials used to sign the request. You can either specify an AWS account ID or optionally a single '- ' (hyphen), in which case Amazon Glacier uses the AWS account ID associated with the credentials used to sign the request. If you specify your account ID, do not include any hyphens ('-') in the ID.
            Note: this parameter is set to '-' bydefault if no value is not specified.

    :type vaultName: string
    :param vaultName: [REQUIRED]
            The name of the vault.

    :rtype: dict
    :return: {
        'location': 'string'
    }

    :returns:
    accountId (string) -- The AccountId value is the AWS account ID. This value must match the AWS account ID associated with the credentials used to sign the request. You can either specify an AWS account ID or optionally a single '- ' (hyphen), in which case Amazon Glacier uses the AWS account ID associated with the credentials used to sign the request. If you specify your account ID, do not include any hyphens ('-') in the ID.

    Note: this parameter is set to "-" bydefault if no value is not specified.

    vaultName (string) -- [REQUIRED]
    The name of the vault.

    """
    pass


def duplicate_line():
    """Duplicate the current line

    """
    pass


def receivedHistory(room, user, message):
    """
        Past messages from a chat room have been received.

        This occurs when you join a room.
        """


def print_scientific_16(value: float) ->str:
    """
    Prints a value in 16-character scientific notation.
    This is a sub-method and shouldnt typically be called

    .. seealso:: print_float_16 for a better method
    """
    if value == 0.0:
        return '%16s' % '0.'
    python_value = '%16.14e' % value
    svalue, sexponent = python_value.strip().split('e')
    exponent = int(sexponent)
    if abs(value) < 1.0:
        sign = '-'
    else:
        sign = '+'
    sexp2 = str(exponent).strip('-+')
    value2 = float(svalue)
    len_sexp = len(sexp2) + 1
    leftover = 16 - len_sexp
    if value < 0:
        fmt = '%%1.%sf' % (leftover - 3)
    else:
        fmt = '%%1.%sf' % (leftover - 2)
    svalue3 = fmt % value2
    svalue4 = svalue3.strip('0')
    field = '%16s' % (svalue4 + sign + sexp2)
    return field


def hour_to_deg(hour):
    """ Convert argument from hours to degrees.

    :param float hour:
    :return: the hour argument as degrees.
    :rtype: float
    """
    return float(hour) * 360.0 / 24.0


def extract_gender_features(word):
    """ Produce features for classification.
    If you choose to add new features, be sure to delete the pickled
    classifier (or the new features will not be used).
    """
    word = word.lower()
    features = {}
    features['last_letter=%s' % word[-1]] = 1
    features['first_letter=%s' % word[0]] = 1
    if len(word) > 1:
        features['first_2=%s' % word[:2]] = 1
        features['last_2=%s' % word[-2:]] = 1
    if len(word) > 2:
        features['first_3=%s' % word[:3]] = 1
        features['last_3=%s' % word[-3:]] = 1
    if len(word) > 3:
        features['first_4=%s' % word[:4]] = 1
        features['last_4=%s' % word[-4:]] = 1
    if word[0] in 'aeiou':
        features['starts_vowel'] = 1
    else:
        features['starts_consonant'] = 1
    if word[-1] in 'aeiou':
        features['ends_vowel'] = 1
    else:
        features['ends_consonant'] = 1
    features['name=%s' % word] = 1
    return features


def sum_matrix(matrix):
    """gives sum of elements in a matrix.
    matrix = list of list of numbers"""
    ans = 0
    for rows in matrix:
        for el in rows:
            ans += el
    return ans


def multi_all_finite(*data, **kwargs):
    """Check if all the float numbers in all the arrays are finite (used for AMP)

    Defined in src/operator/contrib/all_finite.cc:L133

    Parameters
    ----------
    data : Symbol[]
        Arrays
    num_arrays : int, optional, default='1'
        Number of arrays.
    init_output : boolean, optional, default=1
        Initialize output to 1.

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

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


def get_monthly_report_subject(month_day, person):
    """
    Returns subject string for monthly report email

    `month_day:datetime.date` - any date for the month we are reporting for
    `person:str` - reporting person's name, e.g. 'Jon Doe'
    """
    calendar_time = '{year}/{month:02}'.format(year=month_day.year, month=
        month_day.month)
    subject = 'Monthly report for {person} ({calendar_time})'.format(person
        =person, calendar_time=calendar_time)
    return subject


def p_statement_create_table(p):
    """statement : CREATE TABLE identifier columns"""
    p[0] = p[1:]


@classmethod
def __copy__(cls, the_filter: 'FilterChain') ->'FilterChain':
    """
        Creates a shallow copy of the object.
        """
    new_filter = super().__copy__(the_filter)
    new_filter._filters = the_filter._filters[:]
    return new_filter


def _dlib_proc(obj_func, pp, space_params, result_queue, i, space_vals):
    """
    Add/overwrite problem params with params given from dlib,
    then run objective function and put the result in the given queue
    """
    for j, key in enumerate(space_params):
        pp[key] = space_vals[j]
    result = obj_func(**pp, pid=i)
    result_queue.put((i, result))


def candid(data):
    """ Returns id string for given data dict
    Assumes scanId is defined as:
    datasetId dot scanNum dot subscanNum
    """
    return '{0}_seg{1}-i{2}-dm{3}-dt{4}'.format(data['scanId'], data[
        'segment'], data['integration'], data['dmind'], data['dtind'])


def __repr__(A):
    """For `print` and `pprint`"""
    return A.to_str()


def dissolve_mode(use_verts: bool=False, use_face_split: bool=False,
    use_boundary_tear: bool=False):
    """Dissolve geometry based on the selection mode

    :param use_verts: Dissolve Verts, Dissolve remaining vertices
    :type use_verts: bool
    :param use_face_split: Face Split, Split off face corners to maintain surrounding geometry
    :type use_face_split: bool
    :param use_boundary_tear: Tear Boundary, Split off face corners instead of merging faces
    :type use_boundary_tear: bool
    """
    pass


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

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


def is_valid(name):
    """Check if the name is valid"""
    return bool(name)


@classmethod
def edit_form_entry_help_text_extra(cls):
    """For adding the edit link to edit form entry view.

        :return str:
        """
    return (
        """

              {edit_option_html}

                {delete_text}

            <input type="hidden" value="{form_element_position}"
                   name="form-{counter}-position"
                   id="id_form-{counter}-position"
                   class="form-element-position">
            <input type="hidden" value="{form_element_pk}"
                   name="form-{counter}-id" id="id_form-{counter}-id">
            """
        .format(container_class=cls.form_list_container_class,
        edit_option_html='{edit_option_html}', delete_url='{delete_url}',
        delete_option_class=cls.form_delete_form_entry_option_class,
        delete_text='{delete_text}', form_element_position=
        '{form_element_position}', counter='{counter}', form_element_pk=
        '{form_element_pk}'))


def descentralize():
    """ Descentralize the text to be sent to coupon. """


def convert_kwargs_to_query_params_for_get_request(request_url, kwargs_dict):
    """
    Function to take in a set of kwargs and convert them to query parameters and append them to the request url
    :param request_url:
    :param kwargs_dict:
    :return:
    """
    separator = '?'
    for key in kwargs_dict:
        request_url += '{}{}={}'.format(separator, key, kwargs_dict[key])
        if separator == '?':
            separator = '&'
    return request_url


@classmethod
def from_copy(cls, other):
    """
        Packet copy constructor
        """
    ret = cls()
    ret.app = other.app
    ret.transport = other.transport
    ret.network = other.network
    ret.link = other.link
    ret.payload = other.payload
    ret.type = other.type
    ret.time_stamp = other.time_stamp
    return ret


@staticmethod
def _clean_request_data(data):
    """ Remove empty values, and clean data """
    data['postalcode'] = data['postalcode'][:5]
    if not data['city']:
        del data['city']
    if not data['state']:
        del data['state']
    return data


@classmethod
def resetNavigationHistory(cls):
    """Resets navigation history for the current page.
        """
    return cls.build_send_payload('resetNavigationHistory', {}), None


def rebuild_proxy():
    """Rebuild all selected proxies and timecode indices in the background

    """
    pass


def select_all_toggle():
    """Select or deselect all reports

    """
    pass


def get_supported_interfaces(handler_input):
    """Retrieves the supported interfaces from input request.

    The method returns an
    :py:class:`ask_sdk_model.supported_interfaces.SupportedInterfaces`
    object instance listing each interface that the device
    supports. For example, if ``supported_interfaces`` includes
    ``audio_player``, then you know that the device supports streaming
    audio using the AudioPlayer interface. More information on
    `supportedInterfaces` can be found here :
    https://developer.amazon.com/docs/custom-skills/request-and-response-json-reference.html#system-object

    :param handler_input: The handler input instance that is generally
        passed in the sdk's request and exception components
    :type handler_input: ask_sdk_core.handler_input.HandlerInput
    :return: Instance of
        :py:class:`ask_sdk_model.supported_interfaces.SupportedInterfaces`
        mentioning which all interfaces the device supports
    :rtype: ask_sdk_model.supported_interfaces.SupportedInterfaces
    """
    return (handler_input.request_envelope.context.system.device.
        supported_interfaces)


@classmethod
def handles(cls, raw_connection: object) ->bool:
    """ Returns True if this class can wrap the given connection

        :param raw_connection: An unwrapped database connection to test
        """
    return False


def mask_characters(expression, search_text, target_text, mask_char):
    """Mask characters found by a regular expression with mask character. Mask characters will equal length of masked
    string.
    A different target text to search text may be used, but the developer must ensure that they are compatible.
    This feature may be used to handle combinations of N mask_char(s) differently

    :param expression: regex expression
    :param search_text: text to search for matches
    :param target_text: text to apply mask to
    :param mask_char: character to replace masked characters
    """
    matches = []
    pos = 0
    masked_text = ''
    for match in expression.finditer(search_text):
        matches.append(match)
        masked_text += target_text[pos:match.start()] + mask_char * (match.
            end() - match.start())
        pos = match.end()
    masked_text += target_text[pos:]
    return masked_text, matches


def _pandas_iloc_pre_016(df, inds):
    """Workaround for bug, iloc with empty list, in pandas < 0.16"""
    if len(inds) > 0:
        return df.iloc[inds]
    else:
        return df.iloc[:0]


def hasPolicy(obj):
    """Determine if an object has any assigned versioning policies"""


def __gt__(other):
    """Compare for strict ordering (greater than other).

        If other is not adaptable to IMonth it is considered a TypeError.

        """


def mark_seam(clear: bool=False):
    """Mark selected UV edges as seams

    :param clear: Clear Seams, Clear instead of marking seams
    :type clear: bool
    """
    pass


def get_services(input_file_path, cube_config, output_cube_path, **options):
    """This function builds the services dictionary, which is a simple dict of
    names-to-implementation used by bonobo for runtime injection.

    It will be used on top of the defaults provided by bonobo (fs, http, ...). You can override those defaults, or just
    let the framework define them. You can also define your own services and naming is up to you.

    :return: dict
    """
    return {'output_cube_path': output_cube_path, 'cube_config':
        cube_config, 'input_file_path': input_file_path}


@staticmethod
def operations(nsteps):
    """Returns the number of operations needed for nsteps of the solver.

        :param nsteps: number of steps.

        :returns: a dictionary with the same keys as the timings parameter.
          Each value is the number of operations of the corresponding type for
          ``nsteps`` iterations of the method.
        """
    raise NotImplementedError(
        'operations() has to be overridden by the derived solver class.')


@classmethod
def from_response(cls, response):
    """Return the corresponding error from a response."""
    message = response['error']['en']
    http_code = response['http_code']
    error_code = response['error_code']
    klass = cls.ERRORS.get(error_code)
    return klass(message=message, http_code=http_code, error_code=
        error_code, response=response)


def getExonFromLine(bedline):
    """Extract ALL exon regions from input bed line (must be 12-column). return list of [chrom st end]"""
    ret_lst = []
    line = bedline
    fields = line.rstrip('\r\n').split()
    txStart = int(fields[1])
    chrom = fields[0]
    strand = fields[5]
    geneName = fields[3]
    score = fields[4]
    exon_start = list(map(int, fields[11].rstrip(',').split(',')))
    exon_start = list(map(lambda x: x + txStart, exon_start))
    exon_end = list(map(int, fields[10].rstrip(',').split(',')))
    exon_end = list(map(lambda x, y: x + y, exon_start, exon_end))
    for st, end in zip(exon_start, exon_end):
        ret_lst.append([chrom, st, end])
    return ret_lst


def oracle_create_auto_increment(_table, _column):
    """Work around Oracle's silly unwillingness to support auto increment."""
    result = list()
    _new_seq = (_table.name + '_' + _column.name + '_seq')[0:29]
    result.append('')
    result[0] = 'CREATE SEQUENCE ' + _new_seq + _table.row_separator
    result[0] += 'start with 1' + _table.row_separator
    result[0] += 'increment by 1 ' + _table.row_separator
    result[0] += 'nomaxvalue'
    result.append('')
    result[1] = 'CREATE TRIGGER ' + ('tr_' + _table.name + '_' + _column.name)[
        0:29] + _table.row_separator
    result[1
        ] += 'BEFORE INSERT ON "' + _table.name + '"' + _table.row_separator
    result[1] += ('FOR EACH ROW WHEN (new."' + _column.name +
        '" IS NULL) BEGIN' + _table.row_separator)
    result[1] += ('SELECT ' + _new_seq + '.nextval INTO :new."' + _column.
        name + '" FROM dual;' + _table.row_separator)
    result[1] += 'END;'
    return result


def _data_from_bar(ax, bar_label):
    """Given a matplotlib Axes object and a bar label,
    returns (x,y,w,h) data underlying the bar plot.

    Args:
        ax: The Axes object the data will be extracted from.
        bar_label: The bar label from which you want to extract the data.

    Returns:
        A list of (x,y,w,h) for this bar.

    """
    data = []
    handles = ax.get_legend_handles_labels()
    bar_artists = handles[0][handles[1].index(bar_label)]
    for rectangle in bar_artists:
        data.append([rectangle.get_x(), rectangle.get_y(), rectangle.
            get_width(), rectangle.get_height()])
    return data


def replace_values(dfg1, dfg2):
    """
    Replace edge values specified in a DFG by values from a (potentially bigger) DFG

    Parameters
    -----------
    dfg1
        First specified DFG (where values of edges should be replaces)
    dfg2
        Second specified DFG (from which values should be taken)

    Returns
    -----------
    dfg1
        First specified DFG with overrided values
    """
    for edge in dfg1:
        if edge in dfg2:
            dfg1[edge] = dfg2[edge]
    return dfg1


@staticmethod
def get_services(roles):
    """Translate role names to init system service names.

        Sample usage:

        >>> from pulp_smash import config
        >>> host = config.PulpHost('example.com', {
        ...     'amqp broker': {'service': 'qpidd'},
        ...     'pulp celerybeat': {},
        ... })
        >>> services = config.PulpSmashConfig.get_services(host.roles)
        >>> services == {'pulp_celerybeat', 'qpidd'}
        True

        :param roles: The ``roles`` attribute of a
            :class:`pulp_smash.config.PulpHost`.
        :returns: A set. The services that back the named roles.
        """
    services = set()
    for role in roles:
        if role == 'amqp broker':
            services.add(roles[role]['service'])
        elif role == 'api':
            services.add(roles[role].get('service', 'httpd'))
        elif role in ('pulp celerybeat', 'pulp resource manager',
            'pulp workers'):
            services.add(role.replace(' ', '_'))
        elif role in ('mongod', 'redis', 'squid'):
            services.add(role)
        else:
            continue
    return services


def is_int_like(val):
    """Check if a value looks like an int."""
    try:
        return str(int(val)) == str(val)
    except Exception:
        return False


def worker(func, sigint_handler, input, output):
    """ Generic worker: call <func> for each item pulled from
        the <input> queue until a None gets pulled.

        For each result obtained from calling <func>, push the
        result into <output> queue.

        After receiving a None, close the <output> queue.
        """
    for item in iter(input.get, None):
        output.put(func(item, sigint_handler))
    output.close()
    output.join_thread()


def is_dynamic_cwd_width(x):
    """ Determine if the input is a valid input for the DYNAMIC_CWD_WIDTH
    environement variable.
    """
    return isinstance(x, tuple) and len(x) == 2 and isinstance(x[0], float
        ) and x[1] in set('c%')


def _identity(X):
    """The identity function.
    """
    return X


@staticmethod
def _gen_uniform_knotvector_(degree=0, num_ctrlpts=0):
    """ Generates a uniformly-spaced knot vector using the degree and the number of control points.
        :param degree: degree of the knot vector direction
        :type degree: integer
        :param num_ctrlpts: number of control points on that direction
        :type num_ctrlpts: integer
        :return: knot vector
        :rtype: list
        """
    if degree == 0 or num_ctrlpts == 0:
        raise ValueError('Input values should be different than zero.')
    knot_min = 0.0
    knot_max = 1.0
    m = degree + num_ctrlpts + 1
    knotvector = []
    i = 0
    while i < degree + 1:
        knotvector.append(knot_min)
        i += 1
    num_segments = m - (degree + 1) * 2 + 1
    spacing = (knot_max - knot_min) / num_segments
    midknot = knot_min + spacing
    while i < m - (degree + 1):
        knotvector.append(midknot)
        midknot += spacing
        i += 1
    while i < m:
        knotvector.append(knot_max)
        i += 1
    return knotvector


def print_msg(message):
    """ Arguements passed in by the subscription service:
        client: The MQTT client object
        user_data: User data that was included in the message payload
        message: The message that was received
    """
    print(message.payload.decode())


def xml_subelement_text(xml_elem, subelement, convert=None, default=None,
    quiet=False):
    """
    Return the text of the specified subelement

    For the document below:

    .. code-block:: xml

        <xml_elem>
            <subelement>text</subelement>
        </xml_elem>

    ``"text"`` will be returned

    :param xml_elem: search the children nodes of this element
    :param subelement: name of the subelement whose text will be retrieved
    :param convert: if not None, a callable used to perform the conversion of the text to a certain object type
    :param default: default value if subelement is not found
    :param quiet: if True, don't raise exceptions from conversions, instead use the default value
    :return: The text associated with the sub-element or ``None`` in case of error
    """
    if xml_elem is None or not subelement:
        return None
    subel = xml_elem.find(subelement)
    if subel is None:
        text = default
    else:
        text = subel.text
        if text is None:
            text = default
        elif convert:
            try:
                text = convert(text)
            except:
                if quiet:
                    text = default
                else:
                    raise
    return text


@staticmethod
def _helper_gstyle_wordmatch(word_pats, content):
    """Match note contents based no a Google-style search string"""
    if not word_pats:
        return True
    word_pats_matched = 0
    lowercase_content = content.lower()
    for word_pat in word_pats:
        word_pat = word_pat.lower()
        if word_pat in lowercase_content:
            word_pats_matched += 1
    if word_pats_matched == len(word_pats):
        return True
    return False


def _same_moltype(ref, query):
    """if ref and query have the same states"""
    return set(ref) == set(query)


def updatePulseHeightKeywords(hdr, segment, low, high):
    """Update the screening limit keywords for pulse height.

    This is only used for FUV data, since NUV doesn't have pulse height info.

    Parameters
    ----------
    hdr: pyfits Header object
        header with keywords to be modified

    segment: str
        FUVA or FUVB (last character used to construct keyword names)

    low: float
        value for PHALOWR[AB]

    high: float
        value for PHAUPPR[AB]
    """
    key_low = 'PHALOWR' + segment[-1]
    hdr[key_low] = low
    key_high = 'PHAUPPR' + segment[-1]
    hdr[key_high] = high


def _add_doc(func, doc):
    """Add documentation to a function."""
    func.__doc__ = doc


@classmethod
def methods(cls):
    """return the encryption methods supported"""
    return cls.methodDict


@staticmethod
def can_insert(data):
    """ This can insert any dict, regardless of the contents. """
    return isinstance(data, dict)


def validate_doc(doc, mandatory=None, optional=None):
    """Raises error if a dictionary contains labels that are not in the given
    label lists or if there are labels in the mandatory list that are not in
    the dictionary. Returns the given dictionary (if valid).

    Paramaters
    ----------
    doc: dict
        Dictionary serialization of an object
    mandatory: list(string)
        List of mandatory labels for the dictionary serialization
    optional: list(string), optional
        List of optional labels for the dictionary serialization

    Returns
    -------
    dict

    Raises
    ------
    ValueError
    """
    if mandatory is not None:
        for key in mandatory:
            if key not in doc:
                raise ValueError("missing element '{}'".format(key))
    labels = mandatory if mandatory is not None else list()
    if optional is not None:
        labels = labels + optional
    for key in doc:
        if key not in labels:
            raise ValueError("unknown element '{}'".format(key))
    return doc


def is_image_available(context, image):
    """Check image availability.

    This check is needed in case Nova and Glance are deployed
    without authentication turned on.
    """
    if hasattr(context, 'auth_token') and context.auth_token:
        return True
    if getattr(image, 'visibility', None) == 'public' or context.is_admin:
        return True
    return context.project_id and getattr(image, 'owner', None
        ) == context.project_id


def raw_locations_query(table_name='%(table_name)s', cell_cname='cell_id',
    antenna_cname='antenna_id', long_cname='longitude', lat_cname='latitude'):
    """
    Extract raw locations of cells and antennas while getting rid of
    potential duplicates.

    Inputs
    ------
    table_name : Name of the table the query is supposed to run on,
                 defaulting to '%(table_name)s', i.e. no substitution.
    cell_cname : Name of the column which holds the ids of single cells/bts.
    antenna_cname : Name of the column which holds the ids of the
                    antennas/towers (i.e. unique GPS coordinates per id).
    long_cname : Name of column which holds the longitude coordinate.
    lat_cname : Name of column which holds the latitude coordinate.

    Output
    ------
    GPS information of availables cells and antennas.
    """
    query = """
        SELECT
          cell_id,
          antenna_id,
          MEAN(longitude) as longitude,
          MEAN(latitude) as latitude
        FROM
        (
            SELECT
              %(cell_cname)s as cell_id,
              %(antenna_cname)s as antenna_id,
              ROUND(%(long_cname)s, 6) as longitude, -- 6 digits are sufficient
              ROUND(%(lat_cname)s, 6) as latitude -- to identify humans
            FROM %(table_name)s
        )
        GROUP BY cell_id, antenna_id -- kick duplicates
    """
    return query % {'table_name': table_name, 'cell_cname': cell_cname,
        'antenna_cname': antenna_cname, 'long_cname': long_cname,
        'lat_cname': lat_cname}


def find_by_request(request):
    """Return the list of spiders names that can handle the given request"""


def reboot(isdsAppliance, check_mode=False, force=False):
    """
    Reboot the appliance
    """
    if check_mode is True:
        return isdsAppliance.create_return_object(changed=True)
    else:
        return isdsAppliance.invoke_post('Rebooting appliance',
            '/diagnostics/restart_shutdown/reboot', {})


@classmethod
def user_exists(cls, *args, **kwargs):
    """
        Return True/False if a User instance exists with the given arguments.
        Arguments are directly passed to filter() manager method.
        """
    raise NotImplementedError('Implement in subclass')


def linear_space(min_value=0, max_value=1.0, length=10, round_op=None):
    """
    Returns a list of linear-spaced values with min_value and max_value as
    the boundaries with the specified number (length) of entries. If
    roundToIntegers is True, each value will be rounded to the nearest
    integer.

    :param min_value:
    :param max_value:
    :param length:
    :param round_op:
    :return:
    """
    out = []
    value = min_value
    length = max(2, length)
    delta = (float(max_value) - float(min_value)) / float(length - 1.0)
    for index in range(length - 1):
        out.append(round_op(value) if round_op else value)
        value += delta
    out.append(round_op(max_value) if round_op else max_value)
    return out


def title_by_user(title, by_label):
    """ Format title with by_label information """
    return '%s%s' % (title, by_label and ' (*)' or '')


def iter_by(iterable, count):
    """
    Returns a generator which will yield certain elements of the iterable. This elements are defined
    by the count steps
    for obj in iter_by(range(7), 3):
        print(obj)
    [0, 1, 2]
    [3, 4, 5]
    [6]
    :param iterable: iterable
    :param count: int, num of
    :return: generator
    """
    i = iter(iterable)
    while True:
        to_yield = list()
        try:
            for n in range(count):
                to_yield.append(i.next())
                yield to_yield
        except StopIteration:
            if to_yield:
                yield to_yield
            break


def _dict_extends(d1, d2):
    """
    Helper function to create a new dictionary with the contents of the two
    given dictionaries. Does not modify either dictionary, and the values are
    copied shallowly. If there are repeates, the second dictionary wins ties.

    The function is written to ensure Skulpt compatibility.

    Args:
        d1 (dict): The first dictionary
        d2 (dict): The second dictionary
    """
    d3 = {}
    for key, value in d1.items():
        d3[key] = value
    for key, value in d2.items():
        d3[key] = value
    return d3


def check_for_number(claimset_data):
    """
    Checks if claimset_data contains tuples with an integer entry.

    :param claimset_data: list of tuples or strings.
    :type claimset_data: list of tuples or strings
    :return: true if tuples with numbers; false if not
    """
    try:
        for entry in claimset_data:
            if len(entry) <= 1:
                return False
            elif not isinstance(entry[0], int):
                return False
        return True
    except:
        return False


def list_resource_record_sets(HostedZoneId=None, StartRecordName=None,
    StartRecordType=None, StartRecordIdentifier=None, MaxItems=None):
    """
    Lists the resource record sets in a specified hosted zone.
    Note the trailing dot, which can change the sort order in some circumstances.
    When multiple records have the same DNS name, the action sorts results by the record type.
    You can use the name and type elements to adjust the beginning position of the list of resource record sets returned:
    The results begin with the first resource record set that the hosted zone contains.
    The results begin with the first resource record set in the list whose name is greater than or equal to Name .
    Amazon Route 53 returns the InvalidInput error.
    The results begin with the first resource record set in the list whose name is greater than or equal to Name , and whose type is greater than or equal to Type .
    This action returns the most current version of the records. This includes records that are PENDING , and that are not yet available on all Amazon Route 53 DNS servers.
    To ensure that you get an accurate listing of the resource record sets for a hosted zone at a point in time, do not submit a ChangeResourceRecordSets request while you're paging through the results of a ListResourceRecordSets request. If you do, some pages may display results without the latest changes while other pages display results with the latest changes.
    See also: AWS API Documentation

    :example: response = client.list_resource_record_sets(
        HostedZoneId='string',
        StartRecordName='string',
        StartRecordType='SOA'|'A'|'TXT'|'NS'|'CNAME'|'MX'|'NAPTR'|'PTR'|'SRV'|'SPF'|'AAAA',
        StartRecordIdentifier='string',
        MaxItems='string'
    )

    :type HostedZoneId: string
    :param HostedZoneId: [REQUIRED]
            The ID of the hosted zone that contains the resource record sets that you want to list.

    :type StartRecordName: string
    :param StartRecordName: The first name in the lexicographic ordering of resource record sets that you want to list.

    :type StartRecordType: string
    :param StartRecordType: The type of resource record set to begin the record listing from.
            Valid values for basic resource record sets: A | AAAA | CNAME | MX | NAPTR | NS | PTR | SOA | SPF | SRV | TXT
            Values for weighted, latency, geo, and failover resource record sets: A | AAAA | CNAME | MX | NAPTR | PTR | SPF | SRV | TXT
            Values for alias resource record sets:
            CloudFront distribution : A or AAAA
            Elastic Beanstalk environment that has a regionalized subdomain : A
            ELB load balancer : A | AAAA
            Amazon S3 bucket : A
            Constraint: Specifying type without specifying name returns an InvalidInput error.

    :type StartRecordIdentifier: string
    :param StartRecordIdentifier: Weighted resource record sets only: If results were truncated for a given DNS name and type, specify the value of NextRecordIdentifier from the previous response to get the next resource record set that has the current DNS name and type.

    :type MaxItems: string
    :param MaxItems: (Optional) The maximum number of resource records sets to include in the response body for this request. If the response includes more than maxitems resource record sets, the value of the IsTruncated element in the response is true , and the values of the NextRecordName and NextRecordType elements in the response identify the first resource record set in the next group of maxitems resource record sets.

    :rtype: dict
    :return: {
        'ResourceRecordSets': [
            {
                'Name': 'string',
                'Type': 'SOA'|'A'|'TXT'|'NS'|'CNAME'|'MX'|'NAPTR'|'PTR'|'SRV'|'SPF'|'AAAA',
                'SetIdentifier': 'string',
                'Weight': 123,
                'Region': 'us-east-1'|'us-east-2'|'us-west-1'|'us-west-2'|'ca-central-1'|'eu-west-1'|'eu-west-2'|'eu-central-1'|'ap-southeast-1'|'ap-southeast-2'|'ap-northeast-1'|'ap-northeast-2'|'sa-east-1'|'cn-north-1'|'ap-south-1',
                'GeoLocation': {
                    'ContinentCode': 'string',
                    'CountryCode': 'string',
                    'SubdivisionCode': 'string'
                },
                'Failover': 'PRIMARY'|'SECONDARY',
                'TTL': 123,
                'ResourceRecords': [
                    {
                        'Value': 'string'
                    },
                ],
                'AliasTarget': {
                    'HostedZoneId': 'string',
                    'DNSName': 'string',
                    'EvaluateTargetHealth': True|False
                },
                'HealthCheckId': 'string',
                'TrafficPolicyInstanceId': 'string'
            },
        ],
        'IsTruncated': True|False,
        'NextRecordName': 'string',
        'NextRecordType': 'SOA'|'A'|'TXT'|'NS'|'CNAME'|'MX'|'NAPTR'|'PTR'|'SRV'|'SPF'|'AAAA',
        'NextRecordIdentifier': 'string',
        'MaxItems': 'string'
    }

    :returns:
    The * must replace the entire label. For example, you can't specify *prod.example.com or prod*.example.com .
    The * can't replace any of the middle labels, for example, marketing.*.example.com.
    If you include * in any position other than the leftmost label in a domain name, DNS treats it as an * character (ASCII 42), not as a wildcard.

    """
    pass


def is_qfunction(obj, qtype=None):
    """

    Check whether an object is a :term:`state-action value function <type-I
    state-action value function>`, or Q-function.

    Parameters
    ----------
    obj

        Object to check.

    qtype : 1 or 2, optional

        Check for specific Q-function type, i.e. :term:`type-I <type-I
        state-action value function>` or :term:`type-II <type-II state-action
        value function>`.

    Returns
    -------
    bool

        Whether ``obj`` is a (type-I/II) Q-function.

    """
    from ..core.value_q import QTypeI, QTypeII
    if qtype is None:
        return isinstance(obj, (QTypeI, QTypeII))
    elif qtype in (1, 1.0, '1', 'i', 'I'):
        return isinstance(obj, QTypeI)
    elif qtype in (2, 2.0, '2', 'ii', 'II'):
        return isinstance(obj, QTypeII)
    else:
        raise ValueError('unexpected qtype: {}'.format(qtype))


def report_delete():
    """Delete selected reports

    """
    pass


def norm_nonFara_Q_C(Rs, Q, n, L='none'):
    """
    Normalziation of a non-faradaic interfacial capacitance (Blocking Electrode)

    Following Brug and Hirschorn's normalization of distribtuion of relaxation times
    Ref.:
        - G.J.Brug et al., J.Electroanal. Chem. Interfacial Electrochem., 176, 275 (1984)

    Kristian B. Knudsen (kknu@berkeley.edu || kristianbknudsen@gmail.com)

    Inputs
    ------------------
    Q = Constant phase element [s^n/ohm]
    n = Exponent of CPE [-]
    Rs = Series Resistance [ohm]

    Optional Inputs
    ------------------
    L = Thickness/length of electrode, used in Porous Electrode Theory [cm]

    Returns
    ------------------
    C_eff = normalized capacitance for a non-faradaic electrode [s/ohm = F]
    """
    if L == 'none':
        C_eff = (Q * Rs ** (1 - n)) ** (1 / n)
    else:
        C_eff = (Q * L * Rs ** (1 - n)) ** (1 / n)
    return C_eff


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


def get_model_export_headers(model):
    """ Get header names, friendly for exporting data to csv/xlsx """
    for field in model._meta.get_fields():
        if ('ManyToManyRel' != field.__class__.__name__ and 'ManyToOneRel' !=
            field.__class__.__name__):
            yield field.name


def type_token_ratio(types, tokens):
    """ Calculate the ratio of types to tokens. """
    return round(float(len(types)) / float(len(tokens)), 4)


def set_debug(debug):
    """Set global DEBUG flag."""
    global DEBUG
    if debug:
        DEBUG = True


def expr2str(expr):
    """Convert a Python expression into a Python code."""
    if isinstance(expr, dict):
        return str(expr)
    elif isinstance(expr, list):
        return '%s' % expr
    elif isinstance(expr, str):
        return "'%s'" % expr
    return '%s' % str(expr)


def boolean(var, var_name):
    """
    Check if *var* is a boolean.

    :raises TypeError: Parameter *var* must be a bool.
    """
    if not isinstance(var, bool):
        raise TypeError('\n\nParameter {} must be a bool.\n'.format(var_name))


@staticmethod
def _is_bad_status(status):
    """True if a stack status is not healthy."""
    return status is None or status not in {'CREATE_COMPLETE',
        'UPDATE_COMPLETE', 'REVIEW_IN_PROGRESS', 'UPDATE_ROLLBACK_COMPLETE'}


def quantile(l, p):
    """Return p quantile of list l. E.g. p=0.25 for q1.

    See:
    http://rweb.stat.umn.edu/R/library/base/html/quantile.html

    """
    l_sort = l[:]
    l_sort.sort()
    n = len(l)
    r = 1 + (n - 1) * p
    i = int(r)
    f = r - i
    if i < n:
        result = (1 - f) * l_sort[i - 1] + f * l_sort[i]
    else:
        result = l_sort[i - 1]
    return result


def SetDefaultSubstances():
    """Fills in default items for the SASD Substances dictionary"""
    return {'Substances': {'vacuum': {'Elements': {}, 'Volume': 1.0,
        'Density': 0.0, 'Scatt density': 0.0, 'XImag density': 0.0},
        'unit scatter': {'Elements': None, 'Volume': None, 'Density': None,
        'Scatt density': 1.0, 'XImag density': 1.0}}}


def listify(x):
    """Turn a value into a list.

    Lists and tuples are turned into lists, everything else is turned
    into a one element list.

    """
    if x is None:
        return None
    elif isinstance(x, (list, tuple)):
        return x
    else:
        return [x]


def compatible_image_options(img_opts, base_opts=None):
    """
    Return ImageOptions that is compatible with all given `img_opts`.

    """
    if any(True for o in img_opts if o.colors == 0):
        colors = 0
    else:
        colors = max(o.colors or 0 for o in img_opts)
    transparent = None
    for o in img_opts:
        if o.transparent == False:
            transparent = False
            break
        if o.transparent == True:
            transparent = True
    if any(True for o in img_opts if o.mode):
        mode = max(o.mode for o in img_opts if o.mode)
    else:
        mode = None
    if base_opts:
        options = base_opts.copy()
        if options.colors is None:
            options.colors = colors
        if options.mode is None:
            options.mode = mode
        if options.transparent is None:
            options.transparent = transparent
    else:
        options = img_opts[0].copy()
        options.colors = colors
        options.transparent = transparent
        options.mode = mode
    return options


def detect_unicode(data):
    """Detect different unicode charsets by looking for BOMs (Byte Order Mark).

    Operate obviously only on `str` objects.
    """
    if data.startswith('ÿþ'):
        return 'utf-16-le'
    elif data.startswith('þÿ'):
        return 'utf-16-be'
    elif data.startswith('ï»¿'):
        return 'utf-8'
    else:
        return None


def name2description(name_str):
    """ Gets the description of the layer contained in the lyp name field.
        It is not strictly necessary to have a description. If none there, it returns ''.

        Default format of the lyp name is
            layer/datatype - phidl_key - description
            or
            phidl_key - description

        Reassign for different layer naming conventions with::

            phidl.utilities.name2description = someOtherFunction(string)
    """
    if name_str is None:
        raise IOError('This layer has no name')
    components = name_str.split(' - ')
    description = ''
    if len(components) > 2:
        description = components[2][1:-1]
    return description


def _argLineno(lexer, text):
    """
    Return a dictionary of the lexer in use, line number at the moment, and line number
    post evaluation of text (useful for passing to interpreter constructors)
    :param lexer: Current lexer instance in use
    :param text: Text being evaluated
    """
    lineno = lexer.lineno
    nextlineno = lineno + text.count('\n')
    return {'lexer': lexer, 'lineno': lineno, 'nextlineno': nextlineno}


def extreme_obs_table(freqtable, number_to_print, n, ascending=True) ->list:
    """Similar to the frequency table, for extreme observations.

    Args:
      freqtable: The frequency table.
      number_to_print: The number of observations to print.
      n: The total number of observations.
      ascending: The ordering of the observations (Default value = True)

    Returns:
        The HTML rendering of the extreme observation table.
    """
    if 'mixed' in freqtable.index.inferred_type:
        freqtable.index = freqtable.index.astype(str)
    sorted_freqtable = freqtable.sort_index(ascending=ascending)
    obs_to_print = sorted_freqtable.iloc[:number_to_print]
    max_freq = max(obs_to_print.values)
    rows = []
    for label, freq in obs_to_print.items():
        rows.append({'label': label, 'width': freq / max_freq if max_freq !=
            0 else 0, 'count': freq, 'percentage': float(freq) / n,
            'extra_class': '', 'n': n})
    return rows


def find_element(strategy, locator, root=None):
    """Finds an element on the page.

        :param strategy: Location strategy to use (type depends on the driver implementation)
        :param locator: Location of target element.
        :param root: (optional) root node.
        :type strategy: str
        :type locator: str
        :type root: web element object or None.
        :return: web element object
        :rtype: it depends on the driver implementation
        """


def p_select_item_list_select_item(p):
    """select_item_list : select_item """
    p[0] = [p[1]]


def bytes_(s, encoding='latin-1', errors='strict'):
    """ This function is a copy of ``pyramid.compat.bytes_``

    If ``s`` is an instance of ``text_type``, return
    ``s.encode(encoding, errors)``, otherwise return ``s``"""
    if isinstance(s, str):
        return s.encode(encoding, errors)
    return s


@staticmethod
def has_battery() ->bool:
    """Return whether the device has a battery."""
    return False


def trailing_zero_bits(mask):
    """Return a count of trailing zero bits in the mask."""
    mask_str = bin(mask)
    mask_len = len(mask_str)
    right_most_set_bit = mask_str.rfind('1')
    if right_most_set_bit > 0:
        return mask_len - right_most_set_bit - 1
    return mask_len


def angle_brackets(s):
    """
    Surrounds the string with 《 》 characters.  Useful when verifying string
    truncation i.e. when checking the UI these characters should be visible.

    :param s: String to surround
    :returns: String surrounded with 《 》
    """
    return u'《{0}》'.format(s)


def beautify_fill(angle_limit: float=3.14159):
    """Rearrange some faces to try to get less degenerated geometry

    :param angle_limit: Max Angle, Angle limit
    :type angle_limit: float
    """
    pass


def test_zip(archive, compression, cmd, verbosity, interactive):
    """Test a ZIP archive."""
    cmdlist = [cmd, '--test']
    if verbosity > 1:
        cmdlist.append('-v')
    cmdlist.append(archive)
    return cmdlist


def real_space_shape_2d_tag_from_real_space_shape_2d(real_space_shape_2d):
    """Generate a sub-grid tag, to customize phase names based on the sub-grid size used.

    This changes the phase name 'phase_name' as follows:

    real_space_shape_2d = None -> phase_name
    real_space_shape_2d = 1 -> phase_name_real_space_shape_2d_2
    real_space_shape_2d = 4 -> phase_name_real_space_shape_2d_4
    """
    if real_space_shape_2d is None:
        return ''
    y = str(real_space_shape_2d[0])
    x = str(real_space_shape_2d[1])
    return '__rs_shape_' + y + 'x' + x


@staticmethod
def _hash_variables(*args):
    """
        Compute hash from a series of variables.

        :param args: Variables to compute hash
        :type args: Object
        :return: Hash data
        :rtype: int
        """
    return hash(args)


def _construct_alphabets(outcomes):
    """
    Core construction of alphabets. No sanity checks.

    """
    from dit.utils import OrderedDict
    outcome_length = len(outcomes[0])
    alphabets = [OrderedDict() for i in range(outcome_length)]
    for outcome in outcomes:
        for i, symbol in enumerate(outcome):
            alphabets[i][symbol] = True
    alphabets = tuple(map(tuple, alphabets))
    return alphabets


def get_array_type(dtype):
    """
    Determines Python array type from the config file array type

    Parameters
    ----------
    dtype : str
        config file data type

    Returns
    -------
    pytype : str
        Python array type

    """
    uptype = dtype.upper()
    if uptype == 'BYTARR':
        return 'BYTE'
    elif uptype == 'INTARR':
        return 'INT'
    elif uptype == 'LONARR':
        return 'LON'
    elif uptype == 'HEXARR':
        return 'HEX'
    elif uptype == 'EXPARR':
        return 'EXP'
    elif uptype == 'FLTARR':
        return 'FLOAT'
    elif uptype == 'DBLARR':
        return 'DOUBLE'
    elif uptype == 'STRARR':
        return 'STRING'
    else:
        raise Exception('ERROR: Unexpected data type ' + uptype)


def pva(a, i, n):
    """ This function is for the present value of money
    invested per period at the end of the month over a
    period of time.
    a = Periodic Investment (1000)
    i = interest rate as decimal (.0675)
    n = the number of compound periods (20)
    Example: pva(1000, .0675, 20)
    """
    return a / i * (1 - 1 / (1 + i) ** n)


@classmethod
def get_model(cls):
    """ Return model associated to current class, and cache it for future uses, to avoid
            multiple rendering of model for each class derived from Jsonable. Private method.

            :return: dict: model associated to current class.
        """
    if cls not in cls.__cached__models__:
        cls.__cached__models__[cls] = cls.build_model()
    return cls.__cached__models__[cls]


def iterkeys(obj, **kwargs):
    """Use this only if compatibility with Python versions before 2.7 is
    required. Otherwise, prefer viewkeys().
    """
    func = getattr(obj, 'iterkeys', None)
    if not func:
        func = obj.keys
    return func(**kwargs)


def mean(vector):
    """
    Calculates the arithmetic mean of the given vector.

    Args:
    -----
        vector : list
            A non-empty list/array of numbers to be averaged.

    Returns:
    --------
        mean : float
            The arithmetic mean of the given vector.
    """
    return sum(vector) / len(vector)


@classmethod
def from_existing_session(cls, session: object, driver_options: str=None
    ) ->'Core':
    """Creates a new Core object with the entered 'session' reused."""
    return cls(None, False, False, driver_options, None, session)


def Hicks(dp, voidage, vs, rho, mu, L=1):
    """Calculates pressure drop across a packed bed of spheres using a
    correlation developed in [1]_, as shown in [2]_. Twenty-third most accurate
    correlation overall in the review of [2]_. Also shown in [3]_.

    .. math::
        f_p = 6.8 \\frac{(1-\\epsilon)^{1.2}}{Re^{0.2}\\epsilon^3}

    .. math::
        f_p = \\frac{\\Delta P d_p}{\\rho v_s^2 L}

    .. math::
        Re = \\frac{\\rho v_s  d_p}{\\mu}

    Parameters
    ----------
    dp : float
        Particle diameter of spheres [m]
    voidage : float
        Void fraction of bed packing [-]
    vs : float
        Superficial velocity of the fluid (volumetric flow rate/cross-sectional
        area)[m/s]
    rho : float
        Density of the fluid [kg/m^3]
    mu : float
        Viscosity of the fluid, [Pa*s]
    L : float, optional
        Length the fluid flows in the packed bed [m]

    Returns
    -------
    dP : float
        Pressure drop across the bed [Pa]

    Notes
    -----
    Valid in [1]_, [2]_, and [3]_ for a range of
    :math:`300 < Re_{Erg} < 60,000`.

    Examples
    --------
    >>> Hicks(dp=0.01, voidage=0.4, vs=1E-3, rho=1E3, mu=1E-3)
    3.631703956680737

    References
    ----------
    .. [1] Hicks, R. E. "Pressure Drop in Packed Beds of Spheres." Industrial
       Engineering Chemistry Fundamentals 9, no. 3 (August 1, 1970): 500-502.
       doi:10.1021/i160035a032.
    .. [2] Erdim, Esra, Ömer Akgiray, and İbrahim Demir. "A Revisit of Pressure
       Drop-Flow Rate Correlations for Packed Beds of Spheres." Powder
       Technology 283 (October 2015): 488-504. doi:10.1016/j.powtec.2015.06.017.
    .. [3] Allen, K. G., T. W. von Backstrom, and D. G. Kroger. "Packed Bed
       Pressure Drop Dependence on Particle Shape, Size Distribution, Packing
       Arrangement and Roughness." Powder Technology 246 (September 2013):
       590-600. doi:10.1016/j.powtec.2013.06.022.
    """
    Re = dp * rho * vs / mu
    fp = 6.8 * (1 - voidage) ** 1.2 / Re ** 0.2 / voidage ** 3
    return fp * rho * vs ** 2 * L / dp


def doc_label_name(context_type):
    """
    Takes a string `context_type` and makes that a standard 'label' string.
    This functions is useful for figuring out the specific label name in
    metadata given the context_type.

    :param context_type: A type of tokenization.
    :type context_type: string

    :returns: label name for `context_type` as string.
    """
    return str(context_type) + '_label'


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


@classmethod
def get_agent_type(cls):
    """Get the name of the instance's class."""
    return cls.__name__


@classmethod
def supported(cls, conn):
    """Return whether password authentication is supported"""
    return conn.password_auth_supported()


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


def get_postgres_pid(conn):
    """Helper to get the PostgreSQL connection PID"""
    qr = conn.execute('select pg_backend_pid()')
    pid = qr.fetchone()[0]
    return pid


def get_generic_args(x):
    """
  Returns the arguments to a generic. If the generic is not specialized,
  the typevars are returned instead.
  """
    return x.__args__ or x.__parameters__


@classmethod
def name(cls):
    """
        Returns the command name, as used by players talking to the bot.

        The default implementation handles two frequent conventions:

        - if class name starts with Cmd, strips this prefix, lowercases and returns
          the rest

        - if class name ends with Command, strips this suffix, lowercases and returns
          the rest

        - otherwise just lowercases the class name.

        So, for example, class CmdObserve will be by default handling command "observe",
        "MailCommand" will handle "mail", and "ListGames" will handle "listgames".

        Of course, the method can be just overridden to support any other name.
        """
    n = cls.__name__
    if n.startswith('Cmd'):
        return n[3:].lower()
    elif n.endswith('Command'):
        return n[:-7].lower()
    else:
        return n.lower()


@classmethod
def from_dict(cls, dct):
    """
        This will return an inheriting validation
        For example, when passing `file` in the `name`, it will
        load the FileExistValidation and will validate using the internal
        logic of that validation

        """
    name = dct.get('name')
    return cls.get(name)(**dct)


def coupon_cancel():
    """
        Cancels the currently open coupon or the last closed open.
        You cannot close a previous coupon if you already created
        a new one.
        """


def get_nested_item(data_dict, key_list):
    """
    obtain the deepest nested item in a nested dict given keys in a list.
    """
    item = data_dict.copy()
    for k in key_list:
        item = item[k]
    return item


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 '+'


@classmethod
def _get_para_from_kwargs(cls, para_name, default_value='', kwargs=dict()):
    """
        从kwargs参数中获取指定参数值

        @param {string} para_name - 参数名
        @param {string} default_value='' - 默认参数值
        @param {dict} kwargs=dict() - 入参字典

        @returns {object} - 返回参数值

        """
    _ret = default_value
    if para_name in kwargs.keys():
        _ret = kwargs[para_name]
    return _ret


def az_rate(val):
    """Convert an integer value to a floating point angular rate."""
    return val * 90.0 / 2 ** 16


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

        :param BinderAddSectionType val:
        :rtype: EventType
        """
    return cls('binder_add_section', val)


def apply_adjustment(df, adj_date, adj_value, adj_type='mul', date_col=
    'date', cols=['open', 'high', 'low', 'close']):
    """
    Apply adjustment to a given stock
    df
        dataframe of the given stock
    adj_date
        date from which the adjustment is
        to be made
    adj_value
        value to be adjusted
    adj_type
        method of adjustment **mul/sub**
        mul means multiply all the values
            such as splits and bonuses
        sub means subtract the values
            such as dividends

    date_col
        date column  on which the adjustment
        is to be applied
    cols
        columns to which the adjustment is to
        be made

    Notes
    -----
    1) You can use negative values to add to the
    stock value by using **adj_type=sub**
    2) Adjustment is applied prior to all dates
    in the dataframe
    3) In case your dataframe has date or
    symbol as indexes, reset them
    """
    df = df.set_index(date_col).sort_index()
    values_on_adj_date = df.loc[adj_date, cols].copy()
    if adj_type == 'mul':
        adjusted_values = (df.loc[:adj_date, (cols)] * adj_value).round(2)
    elif adj_type == 'sub':
        adjusted_values = (df.loc[:adj_date, (cols)] - adj_value).round(2)
    else:
        raise ValueError('adj_type should be either mul or sub')
    df.loc[:adj_date, (cols)] = adjusted_values
    df.loc[adj_date, cols] = values_on_adj_date
    return df.reset_index()


def round_down(num: int, interval: int) ->int:
    """ Rounds down the given number to the nearest multiple of the given interval. """
    return int(num - num % interval)


def p_quantifier_once(p):
    """quantifier : K_ONCE"""
    p[0] = [('once', ())]


def s(df):
    """
    Rescale pandas series or data frame by its standard deviation

    :param df: ``pandas`` ``Series`` or ``DataFrame``; input date
    :return: ``pandas`` ``Series`` or ``DataFrame``; rescaled data
    """
    return df / df.std(axis=0)


def encoderForRequest(request):
    """
        If applicable, returns a L{_IRequestEncoder} instance which will encode
        the request.
        """


def add_samplefilter(filter):
    """
        add_samplefilter

        Add an additional sample filter file

        @param filter: Sample filter file
        @type filter: C{str}
        @return: None
        """


def tris_convert_to_quads(face_threshold: float=0.698132, shape_threshold:
    float=0.698132, uvs: bool=False, vcols: bool=False, seam: bool=False,
    sharp: bool=False, materials: bool=False):
    """Join triangles into quads

    :param face_threshold: Max Face Angle, Face angle limit
    :type face_threshold: float
    :param shape_threshold: Max Shape Angle, Shape angle limit
    :type shape_threshold: float
    :param uvs: Compare UVs
    :type uvs: bool
    :param vcols: Compare VCols
    :type vcols: bool
    :param seam: Compare Seam
    :type seam: bool
    :param sharp: Compare Sharp
    :type sharp: bool
    :param materials: Compare Materials
    :type materials: bool
    """
    pass


@classmethod
def create(cls, **attributes):
    """
        Save a new model an return the instance.

        :param attributes: The instance attributes
        :type attributes: dict

        :return: The new instance
        :rtype: Model
        """
    model = cls(**attributes)
    model.save()
    return model


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

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

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

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


def get_ability_modifier(score) ->int:
    """Returns ability modifier by score."""
    return score is not 0 and int((score - 10) / 2) or 0


def isSoftClipped(cigar):
    """
    see here for more information about this function
    references:
        https://pysam.readthedocs.io/en/latest/api.html
        https://davetang.org/wiki/tiki-index.php?page=SAM
    """
    for op, count in cigar:
        if op in [4, 5, 6]:
            return True
    return False


def psi_Mg_HCO3_SO4_HMW84(T, P):
    """c-a-a': magnesium bicarbonate sulfate [HMW84]."""
    psi = -0.161
    valid = T == 298.15
    return psi, valid


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

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

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


@staticmethod
def p_axis_pts_ref(p):
    """axis_pts_ref : AXIS_PTS_REF IDENT"""
    p[0] = p[2]


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

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

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


def call_menu_pie(name: str=''):
    """Call (draw) a pre-defined pie menu

    :param name: Name, Name of the pie menu
    :type name: str
    """
    pass


@staticmethod
def Dfun(y1, y0):
    """
        J(y1-y0) = J(y1)-J(y0) = -J(y0)
        where J is the jacobian
        """
    return -1


def list_xz(archive, compression, cmd, verbosity, interactive):
    """List a XZ archive."""
    cmdlist = [cmd]
    cmdlist.append('-l')
    if verbosity > 1:
        cmdlist.append('-v')
    cmdlist.append(archive)
    return cmdlist


def format_nums(start_index):
    """
    Expects an integer.
    Brings numbers exceeding three digits in line with API call requirements
    """
    if start_index >= 1000:
        start_format = str(start_index)[:-3] + '%2C' + str(start_index)[-3:]
    else:
        start_format = str(start_index)
    return start_format


def _check_and_append(data, field, out):
    """Check if `field` exists in `data`, and if it does, append to `out`."""
    if field in data:
        out += data[field]


def _rescale_e(betweenness, n, normalized, directed=False):
    """edge_betweenness_centrality_subset helper."""
    if normalized:
        if n <= 1:
            scale = None
        else:
            scale = 1.0 / (n * (n - 1))
    elif not directed:
        scale = 0.5
    else:
        scale = None
    if scale is not None:
        for v in betweenness:
            betweenness[v] *= scale
    return betweenness


def get_config_value(config, field_names):
    """get value for a config entry

    :param config:
    :param field_names:

    """
    buf = {}
    for field in field_names:
        if field in config:
            buf[field] = config.get(field)
    if len(buf.items()) > 1:
        raise RuntimeError('More than one field alias is specified: {}'.
            format(buf))
    if len(buf.items()) > 0:
        return list(buf.items())[0]
    else:
        return None, None


@classmethod
def from_server(cls, client: 'FractalClient', name: str) ->'Collection':
    """Creates a new class from a server

        Parameters
        ----------
        client : FractalClient
            A FractalClient connected to a server
        name : str
            The name of the collection to pull from.

        Returns
        -------
        Collection
            A constructed collection.

        """
    if not client.__class__.__name__ == 'FractalClient':
        raise TypeError('Expected a FractalClient as first argument, found {}.'
            .format(type(client)))
    class_name = cls.__name__.lower()
    tmp_data = client.get_collection(class_name, name, full_return=True)
    if tmp_data.meta.n_found == 0:
        raise KeyError('Warning! `{}: {}` not found.'.format(class_name, name))
    return cls.from_json(tmp_data.data[0], client=client)


def delete_policy(AutoScalingGroupName=None, PolicyName=None):
    """
    Deletes the specified Auto Scaling policy.
    Deleting a policy deletes the underlying alarm action, but does not delete the alarm, even if it no longer has an associated action.
    See also: AWS API Documentation

    Examples
    This example deletes the specified Auto Scaling policy.
    Expected Output:

    :example: response = client.delete_policy(
        AutoScalingGroupName='string',
        PolicyName='string'
    )

    :type AutoScalingGroupName: string
    :param AutoScalingGroupName: The name of the Auto Scaling group.

    :type PolicyName: string
    :param PolicyName: [REQUIRED]
            The name or Amazon Resource Name (ARN) of the policy.

    :return: response = client.delete_policy(
        AutoScalingGroupName='my-auto-scaling-group',
        PolicyName='ScaleIn',
    )

    print(response)

    """
    pass


def _get_coverage_to_confident_error_idxs(coverages, preds, confidences, y_true
    ):
    """Returns a list of confident error indices for each coverage"""
    sorted_confidences = list(sorted(confidences, reverse=True))
    cov_to_confident_error_idxs = []
    for coverage in coverages:
        threshold = sorted_confidences[int(coverage * len(preds))]
        confident_mask = confidences >= threshold
        confident_error_mask = (y_true != preds) * confident_mask
        confident_error_idx = confident_error_mask.nonzero()[0]
        cov_to_confident_error_idxs.append(confident_error_idx)
    return cov_to_confident_error_idxs


def timestamp_to_seconds(timestamp):
    """
    seconds since midnight derived from timestamp hh:mm:ss
    """
    h = int(timestamp[0:2])
    m = int(timestamp[3:5])
    s = int(timestamp[6:8])
    seconds = 60 ** 2 * h + 60 * m + s
    return seconds


@staticmethod
def _calc_bbox(slices):
    """
        Calculate a minimal bounding box in the form ``((ymin, ymax),
        (xmin, xmax))``.  Note these are pixel locations, not slice
        indices.  For ``mode='partial'``, the bounding box indices are
        for the valid (non-filled) cutout values.
        """
    return (slices[0].start, slices[0].stop - 1), (slices[1].start, slices[
        1].stop - 1)


@classmethod
def is_attempt_filename(cls, fname):
    """
        Returns if the filename is an attempt filename (ends in .attempt.json).
        """
    return fname.endswith('attempt.json')


def manage_main(trueself, self, REQUEST, update_menu=0):
    """Implement a contents view by redirecting to the true view
        """
    REQUEST.RESPONSE.redirect(self.DestinationURL() + '/manage_main')


def _sizestr(size_in_g):
    """Convert the specified size into a string value."""
    return '%sG' % size_in_g


def _is_cmake_configure_argument(arg):
    """Return True if ``arg`` is a relevant argument to pass to cmake when configuring a project."""
    for cmake_arg in ('-C', '-D'):
        if arg.startswith(cmake_arg):
            return True
    return False


def line(num=11, char='#'):
    """
    Print line of char.

    :param num: number of character in this line
    :type num : int
    :param char: character
    :type char : str
    :return: None
    """
    print(char * num)


def construct_search(**query):
    """
    simplified utility to construct query dict for the wos query post

    :param query: form AI="A-1234-5678", PY="2016-2018", etc.
    :return: the query dict as the input of WosQuery
    """
    search_dict = {}
    i = 0
    for key, value in query.items():
        i += 1
        vi = 'value(input' + str(i) + ')'
        vs = 'value(select' + str(i) + ')'
        search_dict[vi] = value
        search_dict[vs] = key
    l = len(query)
    for i in range(l):
        for j in range(i + 1, l):
            vb = 'value(bool_' + str(i + 1) + '_' + str(j + 1) + ')'
            search_dict[vb] = 'AND'
    search_dict['fieldCount'] = l
    return search_dict


def getWave(Parms):
    """returns wavelength from Instrument parameters dictionary

    :param dict Parms: Instrument parameters;
        must contain:
        Lam: single wavelength
        or
        Lam1: Ka1 radiation wavelength

    :returns: float wave: wavelength

    """
    try:
        return Parms['Lam'][1]
    except KeyError:
        return Parms['Lam1'][1]


def km2deg(dist, mpp, ppd):
    """Return dist converted from kilometers to degrees."""
    return 1000 * dist / (mpp * ppd)


def split_tilechains(combined_tilechain_list):
    """
    Function which takes the output of combine_tilechain lists and splits into appropriate
    number of 8xn tilechains
    :param combined_tilechain_list:
    :return: list of single tilechain lists in 8*n matrix format
    """
    list_of_tcs = []
    row = 0
    num_chains = len(combined_tilechain_list) // 8
    for i in range(num_chains):
        tile = combined_tilechain_list[row:row + 8]
        row = row + 8
        list_of_tcs.append(tile)
    return list_of_tcs


@classmethod
def from_name(cls, name):
    """Convert name to enum element.

        :param str name:
            String with name to convert.
        :returns:
            Enum class object.
        :raises:
            NotImplementedError
        """
    for item in cls:
        if item.name == name:
            return item
    raise NotImplementedError("'{}' is no name of '{}'".format(name, cls.
        __name__))


def _get_best_indexes(logits, n_best_size):
    """Get the n-best logits from a list."""
    index_and_score = sorted(enumerate(logits), key=lambda x: x[1], reverse
        =True)
    best_indexes = []
    for i in range(len(index_and_score)):
        if i >= n_best_size:
            break
        best_indexes.append(index_and_score[i][0])
    return best_indexes


def _mask_for_bits(i):
    """Generate a mask to grab `i` bits from an int value."""
    return (1 << i) - 1


def reset():
    """Reset UV projection

    """
    pass


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

        :param PaperEnabledUsersGroupAdditionDetails val:
        :rtype: EventDetails
        """
    return cls('paper_enabled_users_group_addition_details', val)


def elemwise_div(lhs=None, rhs=None, out=None, name=None, **kwargs):
    """Divides arguments element-wise.

    The storage type of ``elemwise_div`` output is always dense

    Parameters
    ----------
    lhs : NDArray
        first input
    rhs : NDArray
        second input

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

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


def p_blocks0(p):
    """
    blocks : blocks header_block
    """
    p[0] = p[1] + [p[2]]


def copy_splines():
    """Copy selected splines to clipboard

    """
    pass


def make_status_readable(magnet):
    """
        Returns a readable status
    """
    status = magnet['handle'].status()
    if not status:
        return 'None'
    return '%.2f%% (d: %.1f kb/s up: %.1f kB/s p: %d)\r' % (status.progress *
        100, status.download_rate / 1000, status.upload_rate / 1000, status
        .num_peers)


def appendData(filename, data):
    """Append a binary set of data to the end of a MRC file. This should only be used in conjunction with
    writeHeader() above.

    Parameters
    ----------
        filename: str
            Name of the MRC file with pre-initiated header and some data already written.
        data: ndarray
            Data to append to the file.

    """
    with open(filename, 'ab') as fid:
        fid.seek(0, 2)
        fid.write(data)


def ask_yes_no(question, default=True):
    """
    Ask a yes/no question via raw_input() and return their answer.

    "question" is a string that is presented to the user.
    "default" is the presumed answer if the user just hits <Enter>.
        It must be "yes" (the default), "no" or None (meaning
        an answer is required of the user).

    The "answer" return value is one of True or False.
    """
    valid = {'yes': True, 'y': True, 'ye': True, 'no': False, 'n': False}
    if default is None:
        prompt = ' [y/n] '
    elif default is True:
        prompt = ' [Y/n] '
    elif default is False:
        prompt = ' [y/N] '
    else:
        raise ValueError('invalid default answer: `%s`' % default)
    while True:
        choice = input(question + prompt).strip().lower()
        print()
        if default is not None and choice == '':
            return default
        elif choice in valid.keys():
            return valid[choice]
        else:
            print('Please respond with `yes` or `no`.')


@classmethod
def get_apps(cls):
    """
        See :obj:`.apps`. Defaults to returning :obj:`.apps`, but can be overridden.
        """
    return cls.apps


def _get_system_scope(auth_ref):
    """Return the scope information of a system scoped token."""
    if auth_ref.system_scoped:
        if auth_ref.system.get('all'):
            return 'all'


def limit(value, min_val, max_val):
    """Return a value clipped to the range [min_val, max_val]."""
    return max(min_val, min(value, max_val))


def true_if_empty_or_filter(value, *or_values):
    """
    Returns 'true' if the provided value is empty or matches any of the provided values.

    Args:
      value (object): the value
      or_values (list): or_values
    Result:
      boolean: the result
    """
    if not isinstance(value, bool) and not value:
        return True
    elif value in or_values:
        return True
    else:
        return False


def release_eq(release, config, env, volumes):
    """
    Given a release, see if its config, env, and volumes match those
    passed in.

    Note that this function does *not* check whether the app and version are
    the same.  You should do that in SQL to narrow the list of relevant
    releases before using this function to check the equality of the
    YAML-formatted fields.
    """
    r_config = release.config_yaml or {}
    if r_config != config:
        return False
    r_env = release.env_yaml or {}
    if r_env != env:
        return False
    r_volumes = release.volumes or []
    volumes = volumes or []
    if r_volumes != volumes:
        return False
    return True


def _dot_(prefix, name):
    """(INTERNAL) Period-join C{prefix} and C{name}.
    """
    return prefix + '.' + str(name)


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

        :param PaperContentRemoveMemberType val:
        :rtype: EventType
        """
    return cls('paper_content_remove_member', val)


def compile_Livneh2013_locations(maptable):
    """
    Compile a list of file URLs for Livneh et al. 2013 (CIG)

    maptable: (dataframe) a dataframe that contains the FID, LAT, LONG_, and ELEV for each interpolated data file
    """
    locations = []
    for ind, row in maptable.iterrows():
        basename = '_'.join(['data', str(row['LAT']), str(row['LONG_'])])
        url = [
            'http://www.cses.washington.edu/rocinante/Livneh/Livneh_WWA_2013/forcs_dhsvm/'
            , basename]
        locations.append(''.join(url))
    return locations


def printable_fields(fields):
    """Return printable fields.

    :param fields list of tuble with (label, vallue)
    """
    for field in fields:
        yield field[0] + '=' + field[1]


def exclude_goalkeepers(data_frame):
    """Remove goalkeepers and return the DataFrame.

    Go through the `data_frame` find all the tuples with the players'
    Position column set to "GK" and remove them. Return the `data_frame`
    with no goalkeeper tuples.

    Parameters
    ----------
    data_frame : pandas.DataFrame
    DataFrame containing FIFA19 data set including goalkeepers.

    Returns
    -------
    data_frame : pandas.DataFrame
    DataFrame containing FIFA19 data set with goalkeepers' tuples
    removed.

    Notes
    -----
    This function can be used when preprocessing the FIFA19 dataset to
    perform Machine Learning as a goalkeeper is a peculiar player  and
    all his properties vary compared to other positions on the field.

    Examples
    --------
    >>> data = pd.read_csv("data.csv")
    >>> print(data[['Name', 'Position']][0:5]) #print first few rows
    Name Position
    0           L. Messi       RF
    1  Cristiano Ronaldo       ST
    2          Neymar Jr       LW
    3             De Gea       GK
    4       K. De Bruyne      RCM
    >>> data = exclude_goalkeepers(data)
    >>> print(data[['Name', 'Position']][0:5]) #print the same number of rows
    Name Position
    0           L. Messi       RF
    1  Cristiano Ronaldo       ST
    2          Neymar Jr       LW
    4       K. De Bruyne      RCM
    5          E. Hazard       LF
    """
    goalkeepers = data_frame[data_frame['Position'] == 'GK']
    data_frame.drop(goalkeepers.index, inplace=True)
    return data_frame


def findPlayer(players, key):
    """Identify a player from their playing token.
    Returns the player with the matching token key."""
    for player in players:
        if player.token.tk == key:
            return player


def load_GifImagePlugin(finder, module):
    """The GifImagePlugin module optionally imports the _imaging_gif module"""
    module.IgnoreName('_imaging_gif')


@classmethod
def find_module(cls, fullname, path=None):
    """Find the built-in module.

        If 'path' is ever specified then the search is considered a failure.

        This method is deprecated.  Use find_spec() instead.

        """
    spec = cls.find_spec(fullname, path)
    return spec.loader if spec is not None else None


def remove_tags(branch, graph, old_tip, parents):
    """Remove tags on revisions between old_tip and new_tip.

    :param branch: Branch to remove tags from
    :param graph: Graph object for branch repository
    :param old_tip: Old branch tip
    :param parents: New parents
    :return: Names of the removed tags
    """
    reverse_tags = branch.tags.get_reverse_tag_dict()
    ancestors = graph.find_unique_ancestors(old_tip, parents)
    removed_tags = []
    for revid, tags in reverse_tags.items():
        if revid not in ancestors:
            continue
        for tag in tags:
            branch.tags.delete_tag(tag)
            removed_tags.append(tag)
    return removed_tags


def timeBoundExecution(algo, maxtime):
    """ wrap the algo, to stop execution after it has used all its allocated time """
    return algo


def project_opened(project):
    """This function is called after opening the project"""


def estimate_num_machines(structure):
    """
     1 <= num_atoms <= 10 -> 1 machine
    11 <= num_atoms <= 20 -> 2 machine

    :param structure: [description]
    :type structure: [type]
    :return: [description]
    :rtype: [type]
    """
    from math import ceil
    number_of_atoms = len(structure.sites)
    return ceil(number_of_atoms / 10)


def move_slide(presentation, old_index, new_index):
    """
    Move a slide's index number.
    """
    xml_slides = presentation.slides._sldIdLst
    slides = list(xml_slides)
    xml_slides.remove(slides[old_index])
    xml_slides.insert(new_index, slides[old_index])


def randint(upper, random_state):
    """
    upper: an int that represent an upper bound
    random_state: an object of numpy.random.RandomState
    """
    return random_state.randint(upper)


def escape(text):
    """Escape &, < and >."""
    return text.replace('&', '&').replace('<', '<').replace('>', '>')


def get_migration_data():
    """
    Support for Django custom user models
    See this blog post for inspiration

    http://kevindias.com/writing/django-custom-user-models-south-and-reusable-apps/
    https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/core/migrations/0005_auto__chg_field_sitepermission_user__del_unique_sitepermission_user.py
    """
    try:
        from django.contrib.auth import get_user_model
    except ImportError:
        from django.contrib.auth.models import User
    else:
        User = get_user_model()
    user_orm_label = '%s.%s' % (User._meta.app_label, User._meta.object_name)
    user_model_label = '%s.%s' % (User._meta.app_label, User._meta.module_name)
    return User, user_orm_label, user_model_label


def get_previewable():
    """Get the previewable version (may be the object itself if no
        versioning).
        Returns None if there is no such version.
        """


def Remap(value, minmax1, minmax2):
    """ Map from range minmax1 to range minmax2 """
    length1 = minmax1[1] - minmax1[0]
    length2 = minmax2[1] - minmax2[0]
    if length1 == 0:
        return 0
    percentage = (value - minmax1[0]) / length1
    return percentage * length2 + minmax2[0]


def dict_match(matcher, matchee):
    """
    Returns True if each key,val pair is present in the matchee
    """
    for key, val in matcher.items():
        if not matchee.get(key):
            return False
        elif not isinstance(matchee.get(key), list):
            mval = [matchee.get(key)]
        else:
            mval = matchee.get(key)
        if isinstance(val, list):
            for item in val:
                if item not in mval:
                    return False
        elif val not in mval:
            return False
    return True


def __new__(cls, *args, **kwds):
    """Autoincrements the value of each item added to the enum."""
    value = len(cls.__members__) + 1
    obj = object.__new__(cls)
    obj._value_ = value
    return obj


def leftovercol(sizes, x, y, dx, dy):
    """
    Compute relative x-axis start, y-axis start, width, and height of remaining
    treemap area when remaining height exceeds remaining width (dx < dy)

    :param sizes: List of values normalized with respect to overall height (dy)
        and width values (dx)
    :param x: Remaining area start point on treemap x-axis
    :param y: Remaining area start point on treemap y-axis
    :param dx: Remaining treemap width
    :param dy: Remaining treemap height
    :return: Tuple of remaining area dimensions
    """
    covered_area = sum(sizes)
    height = covered_area / dx
    leftover_x = x
    leftover_y = dy
    leftover_dx = dx
    leftover_dy = dy - height
    return leftover_x, leftover_y, leftover_dx, leftover_dy


def _Cobject(cls, ctype):
    """(INTERNAL) New instance from ctypes.
    """
    o = object.__new__(cls)
    o._as_parameter_ = ctype
    return o


def filter_process_by_name(name):
    """Filter process by process name."""
    if name in ['cp', 'tee', 'date', 'Null', 'recon-all']:
        return True
    return False


def mask_list(mask):
    """Return the list of set bits in a mask as integers."""
    set_bits = []
    for position, bit in enumerate(reversed(bin(mask)[2:])):
        if bit == '1':
            set_bits.append(int('1{}'.format('0' * position), base=2))
    return set_bits


def add_handler(logger, handler):
    """
    Add a logger handler and skip if the same type already exists.

    :param logger: Logger
    :param handler: handler instance
    """
    handler_type = type(handler)
    for log_handler in logger.handlers:
        if isinstance(log_handler, handler_type):
            return
    logger.addHandler(handler)


def _get_docstring_default_value(var_doc):
    """
    Get the description of argument's default value from docstring.

    Parameters
    ----------
    var_doc : str
        Docstring's part of argument.

    Returns
    -------
    default_val : str
        Description of the defautl value.
    """
    default_val = var_doc.split('\n')[0]
    is_in = ', default ' in default_val or '(default ' in default_val
    if not is_in:
        return ''
    default_val = default_val.split('default')[1]
    default_val = default_val.split(',')[0]
    default_val = default_val.replace(')', '')
    default_val = default_val.strip()
    return default_val


@staticmethod
def _get_out_blocks(n_blocks):
    """
        Helper function that builds empty lists of lists to be filled as
        parameter of type COLLECTION_INOUT
        """
    return [[object() for _ in range(n_blocks[1])] for _ in range(n_blocks[0])]


def lte(value, other):
    """Checks if `value` is less than or equal to `other`.

    Args:
        value (number): Value to compare.
        other (number): Other value to compare.

    Returns:
        bool: Whether `value` is less than or equal to `other`.

    Example:

        >>> lte(5, 3)
        False
        >>> lte(3, 5)
        True
        >>> lte(5, 5)
        True

    .. versionadded:: 3.3.0
    """
    return value <= other


def tidy_key(key):
    """Sanitize summary number key."""
    key = key[:-1].replace(' ', '-')
    key = key.replace('.', '-')
    key = key.replace('_', '-')
    return key.strip()


def is_command(client, command_input, service_id=None, timeout=60):
    """Invokes an administrative command on the given Infrastructure Service
    instance.
    """
    client.invoke_infrastructure_command(command_input, service_id, timeout)


def chunked_list(_list, _chunk_size=50):
    """
    Break lists into small lists for processing:w
    """
    for i in range(0, len(_list), _chunk_size):
        yield _list[i:i + _chunk_size]


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

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


@classmethod
def get_transitions(cls) ->list:
    """
        Returns a dict of statuses with their transitions.
        If not specified for status, any transition is valid.
        """
    return {}


def create_upload_form_attributes(prefix, input_type, name):
    """Creates attribute dicts for the switchable upload form

    :type prefix: str
    :param prefix: prefix (environment, template) of field
    :type input_type: str
    :param input_type: field type (file, raw, url)
    :type name: str
    :param name: translated text label to display to user
    :rtype: dict
    :return: an attribute set to pass to form build
    """
    attributes = {'class': 'switched', 'data-switch-on': prefix + 'source'}
    attributes['data-' + prefix + 'source-' + input_type] = name
    return attributes


@staticmethod
def build_command(commands, name, driver='local', description=None):
    """Builds a command to be added to the pool of available commands."""
    return {'driver': driver, 'name': name, 'commands': commands,
        'description': description}