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

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

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


def groupby_tx(exons, sambamba=False):
    """Group (unordered) exons per transcript."""
    transcripts = {}
    for exon in exons:
        if sambamba:
            ids = zip(exon['extraFields'][-3].split(','), exon[
                'extraFields'][-2].split(','), exon['extraFields'][-1].
                split(','))
        else:
            ids = exon['elements']
        elements = {}
        for tx_id, gene_id, symbol in ids:
            elements[tx_id] = dict(symbol=symbol, gene_id=gene_id)
        exon['elements'] = elements
        for transcript_id in exon['elements']:
            if transcript_id not in transcripts:
                transcripts[transcript_id] = []
            transcripts[transcript_id].append(exon)
    return transcripts


def describe_receipt_rule(RuleSetName=None, RuleName=None):
    """
    Returns the details of the specified receipt rule.
    For information about setting up receipt rules, see the Amazon SES Developer Guide .
    This action is throttled at one request per second.
    See also: AWS API Documentation

    Examples
    The following example returns the details of a receipt rule:
    Expected Output:

    :example: response = client.describe_receipt_rule(
        RuleSetName='string',
        RuleName='string'
    )

    :type RuleSetName: string
    :param RuleSetName: [REQUIRED]
            The name of the receipt rule set to which the receipt rule belongs.

    :type RuleName: string
    :param RuleName: [REQUIRED]
            The name of the receipt rule.

    :rtype: dict
    :return: {
        'Rule': {
            'Name': 'string',
            'Enabled': True|False,
            'TlsPolicy': 'Require'|'Optional',
            'Recipients': [
                'string',
            ],
            'Actions': [
                {
                    'S3Action': {
                        'TopicArn': 'string',
                        'BucketName': 'string',
                        'ObjectKeyPrefix': 'string',
                        'KmsKeyArn': 'string'
                    },
                    'BounceAction': {
                        'TopicArn': 'string',
                        'SmtpReplyCode': 'string',
                        'StatusCode': 'string',
                        'Message': 'string',
                        'Sender': 'string'
                    },
                    'WorkmailAction': {
                        'TopicArn': 'string',
                        'OrganizationArn': 'string'
                    },
                    'LambdaAction': {
                        'TopicArn': 'string',
                        'FunctionArn': 'string',
                        'InvocationType': 'Event'|'RequestResponse'
                    },
                    'StopAction': {
                        'Scope': 'RuleSet',
                        'TopicArn': 'string'
                    },
                    'AddHeaderAction': {
                        'HeaderName': 'string',
                        'HeaderValue': 'string'
                    },
                    'SNSAction': {
                        'TopicArn': 'string',
                        'Encoding': 'UTF-8'|'Base64'
                    }
                },
            ],
            'ScanEnabled': True|False
        }
    }

    :returns:
    Contain only ASCII letters (a-z, A-Z), numbers (0-9), periods (.), underscores (_), or dashes (-).
    Start and end with a letter or number.
    Contain less than 64 characters.

    """
    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 multi_mode_write(buffer_writer, string_writer, buffer_or_path=None):
    """
    Get a StringIO from different type of inputs.

    Parameters
    ----------
    buffer_writer
    string_writer
    buffer_or_path

    Returns
    -------
    typing.StringIO
    """
    if buffer_or_path is None:
        return string_writer()
    if isinstance(buffer_or_path, str):
        buffer = open(buffer_or_path, 'w')
    else:
        buffer = buffer_or_path
    with buffer:
        buffer_writer(buffer)


def utf8Decode(st):
    """Returns utf7 encoded version of imap utf-7 string"""
    return st.decode('utf-8')


def with_metaclass(meta, *bases):
    """Create a base class with a metaclass."""
    return meta('NewBase', bases, {})


def reset():
    """Reset UV projection

    """
    pass


def resource_file_format(value):
    """Space-separated resource references in filename=httpurl format."""
    try:
        file_name, http_url = value.split('=', 1)
    except ValueError:
        message = (
            'Incorrectly formatted resource reference. Argument values should be in the format filename=httpurl'
            )
        raise ValueError(message)
    return {'file_path': file_name, 'http_url': http_url}


def kindint(knd, numkind):
    """
    >>> kindint('Tex',langkindnum['de'])
    1
    >>> kindint('meta',langkindnum['en'])
    19
    >>> kindint(9,langkindnum['en'])
    9
    """
    try:
        return int(knd)
    except:
        try:
            return numkind[knd]
        except:
            for k in numkind:
                if knd in k:
                    return numkind[k]
            return -1


def stabilize_2d_select():
    """Select tracks which are used for translation stabilization

    """
    pass


def ptr_byref(args, offset=-1):
    """Return the pointer argument passed in by-reference."""
    return args[offset]._obj


def index_field(cls):
    """
        Index field for data, which can be overridden.
        """
    return cls.transaction_id_field()


@staticmethod
def bulk_change_extract_status(extracts, extract_status):
    """
        Given a set of extract objects, update the extract process record to reflect the association and updated status
        as well as the extract record's' status.
        :param extracts: List of ExtractTracking SQLAlchemy objects to be bulk updated.
        :param extract_status: The status to change the extract files to.
        :type extract_status: str
        :return:
        """
    for extract in extracts:
        extract.change_extract_status(new_status=extract_status)


@classmethod
def decode_ssh_private(cls, packet):
    """Decode an SSH format RSA private key"""
    n = packet.get_mpint()
    e = packet.get_mpint()
    d = packet.get_mpint()
    iqmp = packet.get_mpint()
    p = packet.get_mpint()
    q = packet.get_mpint()
    return n, e, d, p, q, d % (p - 1), d % (q - 1), iqmp


def _get_default_service_trust_document():
    """
    Just a string representing the service role policy.
    Includes newlines for pretty printing :)
    """
    return """{
    "Version": "2012-10-17",
    "Statement": [{
        "Sid": "",
        "Effect": "Allow",
        "Principal": {
            "Service": "elasticbeanstalk.amazonaws.com"
        },
        "Action": "sts:AssumeRole",
        "Condition": {
            "StringEquals": {
                "sts:ExternalId": "elasticbeanstalk"
            }
        }
    }]
}"""


def reset():
    """Reset the state of Louie.

    Useful during unit testing.  Should be avoided otherwise.
    """
    global connections, senders, senders_back, plugins
    connections = {}
    senders = {}
    senders_back = {}
    plugins = []


def letter_to_number(letter_grade, scale):
    """Convert a letter grade to a number using a given scale.
       Returns float('Nan') for grades not in the scale."""
    for grade, val, mx, mn in scale:
        if letter_grade == grade:
            return val
    else:
        return float('Nan')


@staticmethod
def percent_cpu_usage(previous, current):
    """Return the percentage cpu time used since previous update.

        :param syskit.Process previous: The Process object from last update.
        :param syskit.Process current: The current Process object.
        """
    last_cpu_time = float(previous.cputime)
    last_clock_time = float(previous.refreshed.timestamp())
    cpu_time = float(current.cputime)
    clock_time = current.refreshed.timestamp()
    cpu_time_passed = cpu_time - last_cpu_time
    clock_time_passed = clock_time - last_clock_time
    if clock_time_passed == 0:
        return 0
    percent_cpu_usage = cpu_time_passed / clock_time_passed * 100
    return percent_cpu_usage


def dedupe_list(l):
    """Remove duplicates from a list preserving the order.

    We might be tempted to use the list(set(l)) idiom, but it doesn't preserve
    the order, which hinders testability and does not work for lists with
    unhashable elements.
    """
    result = []
    for el in l:
        if el not in result:
            result.append(el)
    return result


def _polynomial_conversion(data, conversion):
    """ apply polynomial conversion to data

    Parameters
    ----------------
    data : numpy 1D array
        raw data to be converted to physical value
    conversion : mdfinfo3.info3 conversion block ('CCBlock') dict

    Returns
    -----------
    converted data to physical value
    """
    return (conversion['P2'] - conversion['P4'] * (data - conversion['P5'] -
        conversion['P6'])) / (conversion['P3'] * (data - conversion['P5'] -
        conversion['P6']) - conversion['P1'])


@classmethod
def decode_ssh_public(cls, packet):
    """Decode an SSH format RSA public key"""
    e = packet.get_mpint()
    n = packet.get_mpint()
    return n, e


def get_ustream_data(video):
    """
    Helper to extract video and thumbnail from ustream.
    """
    video.source = 'ustream'
    video.key = video.url.rsplit('/', 1)[1]
    video.embed_src = 'http://www.ustream.tv/embed/'
    if 'recorded' in video.url:
        video.embed_src += '/recorded/'
    video.title = 'Ustream video'
    video.slug = 'ustream-{}'.format(video.key)
    return video


def axis(name=None, cols=None, values=None, units=None):
    """Convenience function for generating axis descriptions when defining MetaArrays"""
    ax = {}
    cNameOrder = ['name', 'units', 'title']
    if name is not None:
        ax['name'] = name
    if values is not None:
        ax['values'] = values
    if units is not None:
        ax['units'] = units
    if cols is not None:
        ax['cols'] = []
        for c in cols:
            if type(c) != list and type(c) != tuple:
                c = [c]
            col = {}
            for i in range(0, len(c)):
                col[cNameOrder[i]] = c[i]
            ax['cols'].append(col)
    return ax


@classmethod
def set_signature(cls, data, signature):
    """
        Set the clearsigned signature
        """
    doc_id = data['id']
    cls.write([cls(doc_id)], {'digital_signature': signature})


def secant_method(x, fx):
    """Applies secant method to find root x of function f(x). If not enough
    (x, f(x)) value pairs are known to apply secant method, a new x value is
    guessed by slightly changing the initial x value"""
    if fx[-1] != 0:
        if len(x) > 1 and fx[-1] != 0 and abs(fx[-1]) != abs(fx[-2]):
            x0 = x[-2]
            x1 = x[-1]
            fx0 = fx[-2]
            fx1 = fx[-1]
            m = (fx1 - fx0) / (x1 - x0)
            return x1 + -fx1 / m
        else:
            return x[0] * 0.9 + 0.0001
    else:
        return x[-1]


@classmethod
def check_caom2_element(cls, caom2_element):
    """
        Checks that an element is a valid caom2_element in the blueprint. It
        checks that it's part of the ObsBlueprint._CAOM2_ELEMENTS
        :param caom2_element: name CAOM2 element to check
        :raises KeyError
        """
    if caom2_element not in cls._CAOM2_ELEMENTS:
        raise KeyError('{} not a valid CAOM2 element name (mispelling?).'.
            format(caom2_element))


def subtract(num1, num2):
    """
    definition: func(arg1, arg2) {...}
    objective : the function returns you arg1 - arg2
    """
    return num1 - num2


@classmethod
def from_store(cls, session_store, tenant_id):
    """
        Get the identity session for the given tenant.
        """
    return session_store.session_for_tenant_id(tenant_id).data_for_api(cls, cls
        )


@staticmethod
def has_arrived(data):
    """Check if the train has already arrived."""
    last_station = data['fermate'][-1]
    if not last_station['effettiva']:
        return False
    return True


def _get_latest_release(releases):
    """Given a list of the releases of hfss, return the latest."""
    return releases[-1]


def mp_initializer(imref, imtgt):
    """Declare global variables needed for self._get_spatial_shifts().

    :param imref:   reference image
    :param imtgt:   target image
    """
    global global_shared_imref, global_shared_im2shift
    global_shared_imref = imref
    global_shared_im2shift = imtgt


def write_to_pir_alignment(pir_alignment_filehandle, seq_type, seq_name, seq):
    """Write the `*.pir` alignment compatible with modeller.

    Parameters
    ----------
    seq_type : str
        One of: ['sequence', 'structure'], in that order.
    seq_name : str
        Name to appear in the alignment.
    seq : str
        Alignment sequence.
    """
    pir_alignment_filehandle.write('>P1;' + seq_name + '\n')
    pir_alignment_filehandle.write(seq_type + ':' + seq_name + ':.:.:.:.::::\n'
        )
    pir_alignment_filehandle.write(seq + '*')
    pir_alignment_filehandle.write('\n\n')


@classmethod
def general(cls):
    """A factory method that creates a :class:`Permissions` with all
        "General" permissions from the official Discord UI set to True."""
    return cls(2080374975)


@staticmethod
def _clear(queue):
    """ Only uses documented methods, but is slower. """
    while not queue.empty():
        queue.get()
        queue.task_done()


def __check_negative(items, zero):
    """Checks a list of strings whether items are valid negative numbers.

    Args:
        items (list of [str,]): The list of items to test.
        zero (bool): Zero status. True includes zero, False excludes zero.

    Returns:
        list of [float,]: The list of items converted to negative numbers.
            Returns None if any one test fails.
    """
    for i, item in enumerate(items):
        try:
            items[i] = float(item)
        except ValueError:
            print('Failed on item: "{}"'.format(item))
            return None
        if zero:
            if items[i] > 0:
                print('Failed on item: "{}"'.format(item))
                return None
        elif items[i] >= 0:
            print('Failed on item: "{}"'.format(item))
            return None
    return items


def get_source_code_markdown(function):
    """Retrieves the source code as a string and appends the markdown
    python syntax notation"""
    import inspect
    from IPython.display import display, Markdown
    source_DF = inspect.getsource(function)
    output = '```python' + '\n' + source_DF + '\n' + '```'
    return output


def _call_fit(fit_method, X, y=None, **kwargs):
    """
    helper function, calls the fit or fit_transform method with the correct
    number of parameters

    fit_method: fit or fit_transform method of the transformer
    X: the data to fit
    y: the target vector relative to X, optional
    kwargs: any keyword arguments to the fit method

    return: the result of the fit or fit_transform method

    WARNING: if this function raises a TypeError exception, test the fit
    or fit_transform method passed to it in isolation as _call_fit will not
    distinguish TypeError due to incorrect number of arguments from
    other TypeError
    """
    try:
        return fit_method(X, y, **kwargs)
    except TypeError:
        return fit_method(X, **kwargs)


def _get_var_node_source_info(var_node):
    """ returns the line number, part and filename
        for a given variable node.
    """
    source = var_node.source[0]
    string_range = var_node.source[1]
    filename = getattr(source, 'name', None)
    line_no = None
    part = None
    if hasattr(source, 'loader'):
        origin = source.loader(source.loadname)[0]
        line_no = 1 + origin[:string_range[0]].count('\n')
        part = origin[string_range[0]:string_range[1]]
    return line_no, part, filename


def camel_to_underscore(name):
    """Convert camelcase style naming to underscore style naming

    e.g. SpamEggs -> spam_eggs """
    import string
    for c in string.ascii_uppercase:
        name = name.replace(c, '_%c' % c)
    return name.strip('_').lower()


@staticmethod
def getJavaPackage():
    """ Returns package name String. """
    return 'com.microsoft.ml.spark.io.http.PartitionConsolidator'


def resend_confirmation_code(ClientId=None, SecretHash=None, Username=None):
    """
    Resends the confirmation (for confirmation of registration) to a specific user in the user pool.
    See also: AWS API Documentation

    :example: response = client.resend_confirmation_code(
        ClientId='string',
        SecretHash='string',
        Username='string'
    )

    :type ClientId: string
    :param ClientId: [REQUIRED]
            The ID of the client associated with the user pool.

    :type SecretHash: string
    :param SecretHash: A keyed-hash message authentication code (HMAC) calculated using the secret key of a user pool client and username plus the client ID in the message.

    :type Username: string
    :param Username: [REQUIRED]
            The user name of the user to whom you wish to resend a confirmation code.

    :rtype: dict
    :return: {
        'CodeDeliveryDetails': {
            'Destination': 'string',
            'DeliveryMedium': 'SMS'|'EMAIL',
            'AttributeName': 'string'
        }
    }

    """
    pass


def packChunk(msg):
    """
    Return msg bytes in a chunk
    """
    lines = []
    size = len(msg)
    lines.append(u'{0:x}\r\n'.format(size).encode('ascii'))
    lines.append(msg)
    lines.append(b'\r\n')
    return b''.join(lines)


@staticmethod
def _normalize_point(point):
    """Normalizes a data point by setting the second element
        (which tells whether the example is positive or negative)
        to either ``True`` or ``False``.

        Returns the new data point as a tuple."""
    return point[0], point[1] > 0


def undo_redo():
    """Undo and redo previous action

    """
    pass


def branches(nodes):
    """Obtain ancestor changesets of specific nodes back to a branch point.

        For each requested node, the peer finds the first ancestor node that is
        a DAG root or is a merge.

        Returns an iterable of iterables with the resolved values for each node.
        """


@classmethod
def decode(cls, obj, encoded):
    """ Decode attributes. """
    for attr, value in encoded.items():
        setattr(obj, attr, value)
    return obj


def clean_multiline_string(multiline_string):
    """Make a multiline string suitable for single-line output."""
    return multiline_string.replace('\n', ' ').replace('  ', ' ')


@staticmethod
def to_dict(source):
    """
        Converts to a dictionary data.
        """
    try:
        return float(source)
    except ValueError:
        return None


def ancestors(parent_table, cycle_idx, walker_idx, ancestor_cycle=0):
    """Given a parent table, step_idx, and walker idx returns the
    ancestor at a given cycle of the walker.

    Input:

    parent: table (n_cycles x n_walkers numpy array): It describes
    how the walkers merged and cloned during the WExplore simulation .

    cycle_idx:

    walker_idx:

    ancestor_cycle:

    Output:

    ancestors: A list of 2x1 tuples indicating the
                   walker and cycle parents

    """
    lineage = [(walker_idx, cycle_idx)]
    previous_walker = walker_idx
    for curr_cycle_idx in range(cycle_idx - 1, ancestor_cycle - 1, -1):
        previous_walker = parent_table[curr_cycle_idx][previous_walker]
        if previous_walker == -1:
            break
        previous_point = previous_walker, curr_cycle_idx
        lineage.insert(0, previous_point)
    return lineage


def module_of(object):
    """Returns the name of the module defining `object`, if possible.

       `module_of` works for classes, functions, and class proxies.
    """
    try:
        object = object.__dict__['Essence']
    except (AttributeError, KeyError, TypeError):
        pass
    result = getattr(object, '__module__', None)
    if not result:
        globals = getattr(object, 'func_globals', None)
        if globals:
            result = globals.get('__name__')
    return result


def acronym_maker(word, upper=True):
    """Makes acronym from Strings

PARAMETERS:
	upper : bool, default=True
		uppercase if True else lowercase
	"""
    acronym = ''
    word_list = word.split(' ')
    for i in word_list:
        acronym = acronym + i[0]
    if upper == False:
        return acronym
    elif upper == True:
        return acronym.upper()


def update_endpoint(ApplicationId=None, EndpointId=None, EndpointRequest=None):
    """
    Use to update an endpoint.

    :example: response = client.update_endpoint(
        ApplicationId='string',
        EndpointId='string',
        EndpointRequest={
            'Address': 'string',
            'Attributes': {
                'string': [
                    'string',
                ]
            },
            'ChannelType': 'APNS'|'GCM',
            'Demographic': {
                'AppVersion': 'string',
                'Locale': 'string',
                'Make': 'string',
                'Model': 'string',
                'ModelVersion': 'string',
                'Platform': 'string',
                'PlatformVersion': 'string',
                'Timezone': 'string'
            },
            'EffectiveDate': 'string',
            'EndpointStatus': 'string',
            'Location': {
                'City': 'string',
                'Country': 'string',
                'Latitude': 123.0,
                'Longitude': 123.0,
                'PostalCode': 'string',
                'Region': 'string'
            },
            'Metrics': {
                'string': 123.0
            },
            'OptOut': 'string',
            'RequestId': 'string',
            'User': {
                'UserAttributes': {
                    'string': [
                        'string',
                    ]
                },
                'UserId': 'string'
            }
        }
    )

    :type ApplicationId: string
    :param ApplicationId: [REQUIRED]

    :type EndpointId: string
    :param EndpointId: [REQUIRED]

    :type EndpointRequest: dict
    :param EndpointRequest: [REQUIRED] Endpoint update request
            Address (string) -- The address or token of the endpoint as provided by your push provider (e.g. DeviceToken or RegistrationId).
            Attributes (dict) -- Custom attributes that your app reports to Amazon Pinpoint. You can use these attributes as selection criteria when you create a segment.
            (string) --
            (list) --
            (string) --

            ChannelType (string) -- The channel type. Valid values: APNS, GCM
            Demographic (dict) -- The endpoint demographic attributes.
            AppVersion (string) -- The version of the application associated with the endpoint.
            Locale (string) -- The endpoint locale in the following format: The ISO 639-1 alpha-2 code, followed by an underscore, followed by an ISO 3166-1 alpha-2 value.
            Make (string) -- The endpoint make, such as such as Apple or Samsung.
            Model (string) -- The endpoint model, such as iPhone.
            ModelVersion (string) -- The endpoint model version.
            Platform (string) -- The endpoint platform, such as ios or android.
            PlatformVersion (string) -- The endpoint platform version.
            Timezone (string) -- The timezone of the endpoint. Specified as a tz database value, such as Americas/Los_Angeles.
            EffectiveDate (string) -- The last time the endpoint was updated. Provided in ISO 8601 format.
            EndpointStatus (string) -- The endpoint status. Can be either ACTIVE or INACTIVE. Will be set to INACTIVE if a delivery fails. Will be set to ACTIVE if the address is updated.
            Location (dict) -- The endpoint location attributes.
            City (string) -- The city where the endpoint is located.
            Country (string) -- Country according to ISO 3166-1 Alpha-2 codes. For example, US.
            Latitude (float) -- The latitude of the endpoint location. Rounded to one decimal (Roughly corresponding to a mile).
            Longitude (float) -- The longitude of the endpoint location. Rounded to one decimal (Roughly corresponding to a mile).
            PostalCode (string) -- The postal code or zip code of the endpoint.
            Region (string) -- The region of the endpoint location. For example, corresponds to a state in US.
            Metrics (dict) -- Custom metrics that your app reports to Amazon Pinpoint.
            (string) --
            (float) --

            OptOut (string) -- Indicates whether a user has opted out of receiving messages with one of the following values: ALL   User receives all messages. NONE   User receives no messages.
            RequestId (string) -- The unique ID for the most recent request to update the endpoint.
            User (dict) -- Custom user-specific attributes that your app reports to Amazon Pinpoint.
            UserAttributes (dict) -- Custom attributes specific to the user.
            (string) --
            (list) --
            (string) --

            UserId (string) -- The unique ID of the user.

    :rtype: dict
    :return: {
        'MessageBody': {
            'Message': 'string',
            'RequestID': 'string'
        }
    }

    :returns:
    (dict) -- 202 response
    MessageBody (dict) -- Simple message object.
    Message (string) -- The error message returned from the API.
    RequestID (string) -- The unique message body ID.

    """
    pass


@staticmethod
def _qargs_int(key, values, default):
    """Return the query arg value as an integer for the specified key or
        return the specified default value.

        :param str key: The key to return the value for
        :param dict values: The query value dict returned by urlparse
        :param int default: The default return value
        :rtype: int

        """
    return int(values.get(key, [default])[0])


def add_security_group(nova, vm, security_group_name):
    """
    Adds a security group to an existing VM
    :param nova: the nova client
    :param vm: the OpenStack server object (VM) to alter
    :param security_group_name: the name of the security group to add
    """
    nova.servers.add_security_group(str(vm.id), security_group_name)


def check_attributes(pointcloud, attribute_list=[]):
    """ Function to check if attributes are present in
        dataframe. Will result in assert error is attribute
        can not be found.

        args:
            df             : pandas dataframe
            attribute_list : list (of attributes to check)
        returns:
            Assertion error or None
    """
    for attr in attribute_list:
        assert attr in pointcloud.columns, 'Error: Attribute not in dataframe.'
    return None


def get_handler_name(message):
    """
    Looks at a message, checks it has a sensible type, and returns the
    handler name for that type.
    """
    if 'type' not in message:
        raise ValueError("Incoming message has no 'type' attribute")
    if message['type'].startswith('_'):
        raise ValueError('Malformed type in message (leading underscore)')
    return message['type'].replace('.', '_')


def filter_tickers(df, na_rate=0.1, thresh=20):
    """
    filter tickers by applying threshold to na values of price
    """
    chosen_columns = df.columns[df.isna().mean() < na_rate]
    chosen2 = chosen_columns[~df[chosen_columns].pct_change().eq(0).rolling
        (thresh).sum().eq(thresh).any()]
    return df[chosen2].ffill()


def sub_double_copy(logs, log):
    """Check two copy only rips and score as T&C Rip."""
    if logs[0].has_deduction('Test & Copy') and len(logs[0].tracks) == len(log
        .tracks):
        for new_track, original_track in zip(log.tracks.values(), logs[0].
            tracks.values()):
            if 'copy crc' in new_track and 'copy crc' in original_track:
                if new_track['copy crc'] != original_track['copy crc']:
                    break
        else:
            logs[0].remove_deduction('Test & Copy')


def __replace_index(selfs, df, indexCol):
    """
        For internal use. If the user requests a certain column be the index, this function puts that column as the first in the data frame df
        """
    if indexCol in df.columns:
        df.set_index(indexCol, drop=True, inplace=True)
        df.reset_index(inplace=True)
    else:
        print('Warning: the column ' + '"' + indexCol +
            '" you requested as the index is not found and therefore not included in the output.'
            )
    return df


def drop_dynamic(images):
    """Remove images which are dynamic.

    Drops all rows, in which the Description contains 'Dynamic'.

    Parameters
    ----------
    images : pd.DataFrame
        This dataframe will be modified.

    Returns
    -------
    pd.DataFrame
        All images that are not dynamic.

    """
    no_dynamic = images[~images['Description'].str.contains('Dynamic')]
    return no_dynamic


def __addColor(text):
    """Provides blue color and bold formatting for passed string"""
    BLUE = '\x1b[94m'
    BOLD = '\x1b[1m'
    UNDERLINE = '\x1b[4m'
    END = '\x1b[0m'
    return '{}{}{}{}{}'.format(BLUE, BOLD, text, END, END)


def create_hkl_group(py_obj, h_group, call_id=0):
    """ Create a new group within the hickle file

    Args:
        h_group (h5.File.group): group to dump data into.
        call_id (int): index to identify object's relative location in the iterable.

    """
    h_subgroup = h_group.create_group('data_%i' % call_id)
    h_subgroup.attrs['type'] = [str(type(py_obj)).encode('ascii', 'ignore')]
    return h_subgroup


def create_network_acl_entry(DryRun=None, NetworkAclId=None, RuleNumber=
    None, Protocol=None, RuleAction=None, Egress=None, CidrBlock=None,
    Ipv6CidrBlock=None, IcmpTypeCode=None, PortRange=None):
    """
    Creates an entry (a rule) in a network ACL with the specified rule number. Each network ACL has a set of numbered ingress rules and a separate set of numbered egress rules. When determining whether a packet should be allowed in or out of a subnet associated with the ACL, we process the entries in the ACL according to the rule numbers, in ascending order. Each network ACL has a set of ingress rules and a separate set of egress rules.
    We recommend that you leave room between the rule numbers (for example, 100, 110, 120, ...), and not number them one right after the other (for example, 101, 102, 103, ...). This makes it easier to add a rule between existing ones without having to renumber the rules.
    After you add an entry, you can't modify it; you must either replace it, or create an entry and delete the old one.
    For more information about network ACLs, see Network ACLs in the Amazon Virtual Private Cloud User Guide .
    See also: AWS API Documentation

    Examples
    This example creates an entry for the specified network ACL. The rule allows ingress traffic from anywhere (0.0.0.0/0) on UDP port 53 (DNS) into any associated subnet.
    Expected Output:

    :example: response = client.create_network_acl_entry(
        DryRun=True|False,
        NetworkAclId='string',
        RuleNumber=123,
        Protocol='string',
        RuleAction='allow'|'deny',
        Egress=True|False,
        CidrBlock='string',
        Ipv6CidrBlock='string',
        IcmpTypeCode={
            'Type': 123,
            'Code': 123
        },
        PortRange={
            'From': 123,
            'To': 123
        }
    )

    :type DryRun: boolean
    :param DryRun: Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation . Otherwise, it is UnauthorizedOperation .

    :type NetworkAclId: string
    :param NetworkAclId: [REQUIRED]
            The ID of the network ACL.

    :type RuleNumber: integer
    :param RuleNumber: [REQUIRED]
            The rule number for the entry (for example, 100). ACL entries are processed in ascending order by rule number.
            Constraints: Positive integer from 1 to 32766. The range 32767 to 65535 is reserved for internal use.

    :type Protocol: string
    :param Protocol: [REQUIRED]
            The protocol. A value of -1 or all means all protocols. If you specify all , -1 , or a protocol number other than tcp , udp , or icmp , traffic on all ports is allowed, regardless of any ports or ICMP types or codes you specify. If you specify protocol 58 (ICMPv6) and specify an IPv4 CIDR block, traffic for all ICMP types and codes allowed, regardless of any that you specify. If you specify protocol 58 (ICMPv6) and specify an IPv6 CIDR block, you must specify an ICMP type and code.

    :type RuleAction: string
    :param RuleAction: [REQUIRED]
            Indicates whether to allow or deny the traffic that matches the rule.

    :type Egress: boolean
    :param Egress: [REQUIRED]
            Indicates whether this is an egress rule (rule is applied to traffic leaving the subnet).

    :type CidrBlock: string
    :param CidrBlock: The IPv4 network range to allow or deny, in CIDR notation (for example 172.16.0.0/24 ).

    :type Ipv6CidrBlock: string
    :param Ipv6CidrBlock: The IPv6 network range to allow or deny, in CIDR notation (for example 2001:db8:1234:1a00::/64 ).

    :type IcmpTypeCode: dict
    :param IcmpTypeCode: ICMP protocol: The ICMP or ICMPv6 type and code. Required if specifying the ICMP protocol, or protocol 58 (ICMPv6) with an IPv6 CIDR block.
            Type (integer) --The ICMP type. A value of -1 means all types.
            Code (integer) --The ICMP code. A value of -1 means all codes for the specified ICMP type.

    :type PortRange: dict
    :param PortRange: TCP or UDP protocols: The range of ports the rule applies to.
            From (integer) --The first port in the range.
            To (integer) --The last port in the range.

    :return: response = client.create_network_acl_entry(
        CidrBlock='0.0.0.0/0',
        Egress=False,
        NetworkAclId='acl-5fb85d36',
        PortRange={
            'From': 53,
            'To': 53,
        },
        Protocol='udp',
        RuleAction='allow',
        RuleNumber=100,
    )

    print(response)

    """
    pass


def is_success(status):
    """
    Check if HTTP status code is successful.

    :param status: http status code
    :returns: True if status is successful, else False
    """
    return 200 <= status <= 299


def _usage():
    """
    Return usage of the main ivy call and an example.
    """
    usage = """
    **Ivy workflow engine**
    Copyright (c) 2014 ETH Zurich, Institute for Astronomy

    Usage:
    ivy [arguments] configuration

    Only arguments already preconfigured in the given configuration will be accepted.
    Note: Dashed '-' will be converted into underlines '_' for all the arguments

    example:
    - ivy --size-x=100 --size-y=100 ufig.config.random
    """
    print(usage)


def update_version(db_settings, params):
    """
    Update the database when a Toyz version changes (if necessary). At the time params
    is a dictionary that requires the keys 'toyz_version' and 'db_version', the versions
    of the Toyz instance and database respectively. In the future, updates to the DB may
    require other parameters.
    """
    toyz_version = params['toyz_version']
    db_version = params['db_version']
    print('No updates needed')


def extended_euclidean_algorithm(u, v):
    """
    Return a tuple @c (m, n, d) such that @c u*m + v*n = d, where @c d is the
    greatest common divisor of @p u and @p v.

    The implementation does not enforce the use of integers for @p u and @p v.
    Thus, elements from other rings that support the @c divmod() operations as
    well as the operators @c //, @c * and @c - should work as well.

    @note      If @p u and @p v are Polynomials, then the returned gcd
               polynomial is not necessarily a monic polynomial; that means the
               gcd may have a leading coefficient other than one. To check
               whether two polynomials are relatively prime, test whether the
               result has degree() zero.

    @exception ZeroDivisionError   if one of @p u or @p v is zero.

    @see       Knuth, D. E., "The Art of Computer Programming",
               volume 2, second edition, p. 342
    """
    if not (u and v):
        raise ZeroDivisionError('cannot determine the gcd of zero')
    larger_remainder, lesser_remainder = u, v
    try:
        larger_scalar, lesser_scalar = u.__class__.one(), u.__class__.zero()
    except AttributeError:
        larger_scalar, lesser_scalar = 1, 0
    while lesser_remainder:
        quotient, next_remainder = divmod(larger_remainder, lesser_remainder)
        next_scalar = larger_scalar - quotient * lesser_scalar
        larger_remainder, larger_scalar = lesser_remainder, lesser_scalar
        lesser_remainder, lesser_scalar = next_remainder, next_scalar
    other_scalar = (larger_remainder - u * larger_scalar) // v
    return larger_scalar, other_scalar, larger_remainder


def update(*args, **kwds):
    """od.update(E, **F) -> None.  Update od from dict/iterable E and F.

        If E is a dict instance, does:           for k in E: od[k] = E[k]
        If E has a .keys() method, does:         for k in E.keys(): od[k] = E[k]
        Or if E is an iterable of items, does:   for k, v in E: od[k] = v
        In either case, this is followed by:     for k, v in F.items(): od[k] = v

        """
    if len(args) > 2:
        raise TypeError(
            'update() takes at most 2 positional arguments (%d given)' % (
            len(args),))
    elif not args:
        raise TypeError('update() takes at least 1 argument (0 given)')
    self = args[0]
    other = ()
    if len(args) == 2:
        other = args[1]
    if isinstance(other, dict):
        for key in other:
            self[key] = other[key]
    elif hasattr(other, 'keys'):
        for key in other.keys():
            self[key] = other[key]
    else:
        for key, value in other:
            self[key] = value
    for key, value in kwds.items():
        self[key] = value


def count(iterable):
    """
	Count the number of items produced by the iterable. Calling this function
	will exhaust the iterator.
	"""
    count = 0
    for item in iterable:
        count += 1
    return count


def column_value(item, args):
    """Return the value stored in a column.

       YAML usage:

       outputs:
         outcol:
           function:         identity
           arguments:        [col(KEYCOL)]
           value:            column_value
           value_arguments:  [col(VALUECOL)]
    """
    return item[args[0]]


def detbg(area, band):
    """
    Nominal background in counts per second per 1.5" pixel.
        See: http://asd.gsfc.nasa.gov/archive/galex/FAQ/counts_background.html

    :param area: The area to calculate the background in square degrees.

    :type area: float

    :param band: The band to use, either 'FUV' or 'NUV'.

    :type band: str

    :returns: float -- The nominal background for the given band, in counts per
        second.
    """
    rate = 0.0001 if band == 'FUV' else 0.001
    return area * rate / (1.5 / (60 * 60)) ** 2.0


def odml_tuple_export(odml_tuples):
    """
    Converts odml style tuples to a parsable string representation.
    Every tuple is represented by brackets '()'. The individual elements of a tuple are
    separated by a semicolon ';'. The individual tuples are separated by a comma ','.
    An odml 3-tuple list of 2 tuples would be serialized to: "[(11;12;13),(21;22;23)]".

    :param odml_tuples: List of odml style tuples.
    :return: string
    """
    str_tuples = ''
    for val in odml_tuples:
        str_val = ';'.join(val)
        if str_tuples:
            str_tuples = '%s,(%s)' % (str_tuples, str_val)
        else:
            str_tuples = '(%s)' % str_val
    return '[%s]' % str_tuples


def transaction_close():
    """close transaction scope"""
    pass


def tokenize_by_camel_case(string, token):
    """
    Tokenize the given String by camel case replacing upper case characters by
    the given token.
    """
    if not string:
        return string
    result = [string[0:1]]
    index = 1
    length = len(string)
    while index < length:
        character = string[index].upper()
        if character != ' ' and character == string[index]:
            result.append(token)
            result.append(string[index].lower())
        else:
            result.append(string[index])
        index += 1
    return ''.join(result)


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


def is_curry_func(f):
    """
    Checks if f is a toolz or cytoolz function by inspecting the available attributes.
    Avoids explicit type checking to accommodate all versions of the curry fn.
    """
    return hasattr(f, 'func') and hasattr(f, 'args') and hasattr(f, 'keywords')


def grv(struct, position):
    """
    This function helps to convert date information for showing proper filtering
    """
    if position == 'year':
        size = 4
    else:
        size = 2
    if struct[position][2]:
        rightnow = str(struct[position][0]).zfill(size)
    elif position == 'year':
        rightnow = '____'
    else:
        rightnow = '__'
    return rightnow


def average(a, b):
    """
    Given two numbers a and b, return their average value.

    Parameters
    ----------
    a : number
      A number
    b : number
      Another number

    Returns
    -------
    res : number
      The average of a and b, computed using 0.5*(a + b)

    Example
    -------
    >>> average(5, 10)
    7.5

    """
    return (a + b) * 0.5


@classmethod
def hascreds(cls, config):
    """Determine if the given configuration has proper credentials

        Parameters
        ----------
        cls : class
            A Session class.
        config : dict
            GDAL configuration as a dict.

        Returns
        -------
        bool

        """
    return 'GOOGLE_APPLICATION_CREDENTIALS' in config


def without_site_packages(path):
    """The inverse operation of site_packages()."""
    return path.rsplit('/lib/', 2)[0]


@staticmethod
def getStatData(pid):
    """
        Collect the data from /proc/<pid>/stat
        :param pid: The pid to get stats about.
        :return: The known indices as dict.
        """
    name_mapping = {(4): 'pgroup', (5): 'session', (21): 'starttime'}
    path = '/proc/{pid}/stat'.format(pid=pid)
    with open(path, 'r') as fi:
        raw_data = fi.read().strip().split()
        data = {}
        for index, name in name_mapping.items():
            data[name] = raw_data[index]
        return data


def _get_text(topnode):
    """Get the text values in the child nodes."""
    text = ''
    for node in topnode.childNodes:
        if node.nodeType == node.TEXT_NODE:
            text = text + node.data
    return text


@staticmethod
def _screen_coords(lengths, position):
    """Translates quote offset into screen coordinates.

        Args:
            lengths: List of line lengths for the word-wrapped quote.
            position: Offset into the quote that we want to translate to screen
                      coordinates.

        Returns:
            Tuple containing X and Y screen coordinates.
        """
    y_position = 0
    for y_position, line_length in enumerate(lengths):
        if position <= line_length:
            break
        position -= line_length + 1
    return position, y_position


def get_metadata_for(name):
    """I return a Deferred that fires with the metadata dictionary for
        a specific named child node. The child name must be a unicode string.
        This metadata is stored in the *edge*, not in the child, so it is
        attached to the parent dirnode rather than the child node.
        I raise NoSuchChildError if I do not have a child by that name."""


@staticmethod
def get_execution_status(exit_code):
    """ return the execution code for a job """
    if exit_code > 0 and exit_code != 137:
        return 'error'
    elif exit_code == 137:
        return 'warning'
    else:
        return 'success'


def true(m):
    """The membership-value is its own truth-value."""
    return m


def fft_ramp_image_filter(*args, **kwargs):
    """Procedural interface for FFTRampImageFilter"""
    import itk
    instance = itk.FFTRampImageFilter.New(*args, **kwargs)
    return instance.__internal_call__()


@classmethod
def from_dict(cls, client, soap_dict):
    """
        Alternative constructor, builds entity object from a dictionary representation of
        a SOAP response created by the SOAP client.
        """
    return cls(client=client, key=soap_dict.get('Key', None), address=
        soap_dict.get('Address', None))


@classmethod
def remove_remote(cls, remotes):
    """Enables dynamically removing nodes
        """
    cls._refresh_resource()
    pass


def _norm_index(i, length, default):
    """For converting s[:3] to s[0:3], s[-1] to s[len(s)-1] and s[0:lots] to s[0:len(s)]"""
    if i is None:
        i = default
    elif i < 0:
        i += length
    return min(max(i, 0), length)


def safe_mod(a, b):
    """ only allow modulo on numbers, not string formating """
    if isinstance(a, str):
        raise TypeError('Sorry, string formating is not supported')
    return a % b


def func_best_model(scan_object, metric='val_acc', asc=False):
    """Picks the best model based on a given metric and
    returns the index number for the model.

    NOTE: for loss 'asc' should be True"""
    import warnings as warnings
    warnings.simplefilter('ignore')
    from ..utils.best_model import best_model, activate_model
    model_no = best_model(scan_object, metric, asc)
    out = activate_model(scan_object, model_no)
    return out


def enable(true_false):
    """Enable/Disable ANSII Color formatting"""
    global __ISON
    __ISON = true_false


@classmethod
def get_by_name(cls, context, name):
    """Find a DB Device Profile and return an Obj Device Profile."""
    db_devprof = cls.dbapi.device_profile_get(context, name)
    obj_devprof = cls._from_db_object(cls(context), db_devprof)
    return obj_devprof


def in_notebook():
    """ Returns ``True`` if the module is running in IPython kernel,
      ``False`` if in IPython shell or other Python shell.
    """
    from IPython import get_ipython
    return get_ipython() is not None


def get_input(prompt, type, error_string, check_list=[], default='', **kwds):
    """
    Return user input after validating it.
    """
    input_validated = False
    if type == 'float':
        while not input_validated:
            input_data = input(prompt)
            try:
                input_data = float(input_data)
                input_validated = True
            except ValueError:
                print(error_string)
    if type == 'float+':
        input_data = input(prompt)
        try:
            input_data = float(input_data)
            if input_data < 0:
                print(error_string)
            else:
                input_validated = True
        except ValueError:
            print(error_string)
    elif type == 'float_or_blank':
        while not input_validated:
            input_data = input(prompt)
            if input_data == '':
                input_data = default
                input_validated = True
            else:
                try:
                    input_data = float(input_data)
                    input_validated = True
                except ValueError:
                    print(error_string)
    elif type == 'float_str_or_blank':
        while not input_validated:
            input_data = input(prompt)
            if input_data == '':
                input_data = default
                input_validated = True
            elif input_data in kwds['str_list']:
                input_validated = True
            else:
                try:
                    input_data = float(input_data)
                    input_validated = True
                except ValueError:
                    print(error_string)
    elif type == 'float+_or_blank':
        while not input_validated:
            input_data = input(prompt)
            if input_data == '':
                input_data = default
                input_validated = True
                return input_data
            try:
                input_data = float(input_data)
                if input_data < 0:
                    print(error_string)
                else:
                    input_validated = True
            except ValueError:
                print(error_string)
    if type == 'int':
        while 1:
            input_data = input(prompt)
            try:
                input_data = int(input_data)
                break
            except ValueError:
                print(error_string)
    if type == 'int_or_str':
        while 1:
            input_data = input(prompt)
            if input_data in kwds['str_list']:
                break
            try:
                input_data = int(input_data)
                try:
                    if input_data < kwds['min']:
                        print('You must enter an integer no less than',
                            kwds['min'])
                except KeyError:
                    pass
                try:
                    if input_data > kwds['max']:
                        print('You must enter an integer less than', kwds[
                            'max'])
                except KeyError:
                    break
            except ValueError:
                print('You must enter an integer')
    elif type == 'list':
        while not input_validated:
            input_data = input(prompt)
            if input_data == '':
                input_data = default
                input_validated = True
                return default
            if input_data in check_list:
                input_validated = True
            else:
                print(error_string)
    return input_data


@classmethod
def get(cls, _offset, _limit, _sort, **kwargs):
    """Get a list of resources."""
    raise NotImplementedError()


def qualify(qualifier, name):
    """Runs function given as 'qualifier' with 'name' as argument. If exception occures, return
    False. Otherwise return whatever qualifier function returns.
    """
    try:
        return qualifier(name)
    except:
        return False


@classmethod
def get_from_string(cls, string_phase):
    """
        Convert string value obtained from k8s API to PodPhase enum value
        :param string_phase: str, phase value from Kubernetes API
        :return: PodPhase
        """
    if string_phase == 'Pending':
        return cls.PENDING
    elif string_phase == 'Running':
        return cls.RUNNING
    elif string_phase == 'Succeeded':
        return cls.SUCCEEDED
    elif string_phase == 'Failed':
        return cls.FAILED
    elif string_phase == 'Unknown':
        return cls.UNKNOWN
    return cls.UNKNOWN


def __call__():
    """ toggle text field method"""


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

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

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


@classmethod
def from_dict(cls, model_dict):
    """Instantiate model from dict"""
    return cls(**model_dict)


def _get_container_ids(existing_logs: list) ->list:
    """
    Extract container-id from existing logs and store them inside a list.
    """
    container_instance_ids = []
    for existing_log in existing_logs:
        for existing_log_stream in existing_log['log_streams']:
            log_stream_name = existing_log_stream['logStreamName']
            container_id = log_stream_name.split('/')[-1]
            container_instance_ids.append(container_id)
    return container_instance_ids


def evaluate(level, step, X):
    """Transform or predict according to the input level.

    This function uses the transform or the predict method on the input
    sklearn-like step according to its level (i.e. imputing, preproc, dimred,
    clustering, none).

    Parameters
    -----------
    level : {'imputing', 'preproc', 'dimred', 'clustering', 'None'}
        The step level.

    step : sklearn-like object
        This might be an Imputer, or a PCA, or a KMeans (and so on...)
        sklearn-like object.

    X : array of float, shape : n_samples x n_features
        The input data matrix.

    Returns
    -----------
    res : array of float
        A matrix projection in case of dimred, a label vector in case of
        clustering, and so on.
    """
    if level in ('imputing', 'preproc', 'dimred', 'None'):
        if hasattr(step, 'embedding_'):
            res = step.embedding_
        else:
            res = step.transform(X)
    elif level == 'clustering':
        if hasattr(step, 'labels_'):
            res = step.labels_
        elif hasattr(step, 'affinity') and step.affinity == 'precomputed':
            if not hasattr(step.estimator, 'labels_'):
                step.estimator.fit(X)
            res = step.estimator.labels_
        else:
            res = step.predict(X)
    return res


def dict_get(d, key, default=None):
    """
    exactly the same as standard python. this is to remind that get() never raises an exception

    https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
    """
    return d.get(key, default)


def draw_box(image, box, color):
    """Draw 3-pixel width bounding boxes on the given image array.
    color: list of 3 int values for RGB.
    """
    y1, x1, y2, x2 = box
    image[y1:y1 + 2, x1:x2] = color
    image[y2:y2 + 2, x1:x2] = color
    image[y1:y2, x1:x1 + 2] = color
    image[y1:y2, x2:x2 + 2] = color
    return image


def add_require_js_path(app, env):
    """Insert the require javascript package url, to pages created from notebooks."""
    config = app.config
    if not (getattr(env, 'ipysphinx_created_from_nb', set()) or config.
        ipysphinx_always_add_jsurls):
        return
    if config.ipysphinx_require_jsurl is None:
        requirejs_path = (
            'https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js'
            )
    else:
        requirejs_path = config.ipysphinx_require_jsurl
    if config.ipysphinx_requirejs_options is None:
        requirejs_options = {'integrity':
            'sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=',
            'crossorigin': 'anonymous'}
    else:
        requirejs_options = config.ipysphinx_requirejs_options
    app.add_js_file(requirejs_path, **requirejs_options)


def provider_parser(subparser):
    """Configure provider parser for PointHQ"""
    subparser.add_argument('--auth-username', help=
        'specify email address for authentication')
    subparser.add_argument('--auth-token', help=
        'specify token for authentication')


def freq_resolution(sr, nfft):
    """
  :param: sr, sample rate
  :param: nfft, fft points
  :return: freq resolution of one point
  """
    return sr / 2 / (nfft / 2)


def _scatter_elemwise_div(lhs=None, rhs=None, out=None, name=None, **kwargs):
    """Divides arguments element-wise.  If the left-hand-side input is 'row_sparse', then
    only the values which exist in the left-hand sparse array are computed.  The 'missing' values
    are ignored.

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

    - _scatter_elemwise_div(row_sparse, row_sparse) = row_sparse
    - _scatter_elemwise_div(row_sparse, dense) = row_sparse
    - _scatter_elemwise_div(row_sparse, csr) = row_sparse
    - otherwise, ``_scatter_elemwise_div`` behaves exactly like elemwise_div and generates output
    with default storage

    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 multipart_content_type(boundary, subtype='mixed'):
    """
    Returns a dict containing a MIME multipart header with the given
    boundary

    >>> multipart_content_type('8K5rNKlLQVyreRNncxOTeg')
    {'Content-Type': 'multipart/mixed; boundary="8K5rNKlLQVyreRNncxOTeg"'}

    >>> multipart_content_type('8K5rNKlLQVyreRNncxOTeg', 'alt')
    {'Content-Type': 'multipart/alt; boundary="8K5rNKlLQVyreRNncxOTeg"'}
    """
    ctype = 'multipart/%s; boundary="%s"' % (subtype, boundary)
    return {'Content-Type': ctype}


@staticmethod
def cmd_sgr(sgr_list):
    """Get a SGR (Set Graphics Rendition) code."""
    return '\x1b[{}m'.format(';'.join(sgr_list))


def get_element_value(xml, element_name):
    """Returns the value of the given element name
        """


def merge_protein_scores(scores):
    """ Merge scores of all isozymes that catalyze a given reaction.
    Calculates the maximum score among all isozymes.

    Args:
        scores (pandas.Series): protein scores

    Returns:
        float: merged score

    """
    return scores.max(skipna=True)


def calculate_corrected_normalised_rotation(norm_rot, bhr):
    """
    Corrected normalised rotation according to Millen (2015)
    Correction is for shear force
    Normalisation is against pseudo uplift angle
    :param norm_rot: Normalised rotation angle
    :param bhr: Ratio of Foundation width to Effective height of superstructure mass
    :return:
    """
    return norm_rot ** (1 - 0.2 * bhr) * 10 ** (0.25 * bhr)


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

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


@classmethod
def _keyattr(cls):
    """Return key attribute name."""
    return cls.KEYFMT.format(cls.KEY)


def _load_generic(packname, package, section, target):
    """Loads the settings for generic options that take FQDN and a boolean value
    (1 or 0).

    Args:
        packname (str): name of the package to get config settings for.
        package: actual package object.
    """
    from acorn.config import settings
    spack = settings(packname)
    if spack.has_section(section):
        secitems = dict(spack.items(section))
        for fqdn, active in secitems.items():
            target[fqdn] = active == '1'


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

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

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


@classmethod
def get_global_scope(cls, scope):
    """
        Get a global scope registered with the model.

        :param scope: The scope to register
        :type scope: orator.orm.scopes.scope.Scope or str
        """
    for key, value in cls._global_scopes[cls].items():
        if isinstance(scope, key):
            return value


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

    The ".dev0" means dirty.

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


def guess_contains_libjpeg_turbo(lines):
    """Tries to determine if a file includes libjpeg-turbo
    """
    for line in lines:
        if 'LIBJPEG' in line:
            return 1
        if ('Caution: quantization tables are too coarse for baseline JPEG' in
            line):
            return 1
        if 'Invalid JPEG file structure: two SOF markers' in line:
            return 1
    return 0


@classmethod
def get_url_tibiadata(cls):
    """
        Gets the URL to the World Overview page in Tibia.com

        Returns
        -------
        :class:`str`
            The URL to the World Overview's page.
        """
    return 'https://api.tibiadata.com/v2/worlds.json'


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


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


def count_send(link_counters):
    """Find the send count in the counters returned by a CommunicationLink."""
    for processor in link_counters.values():
        if 'send' in processor:
            return processor['send']


@classmethod
def unregister_dns(cls, hosts):
    """
        Given a list of 'stacks.Host' objects, this method's
        implementation should handle the de-registration of DNS
        for the given cloud provider (e.g., Route53 on AWS)
        """
    raise NotImplementedError()


def syntax_element(human_readable_name: str) ->str:
    """
    Transforms a name with space and lowercase letters
    to a string suitable as syntax element
    """
    return human_readable_name.upper().replace(' ', '-')


def getMethodName(method_id):
    """
        given a shipping method id, return the name of the method name
        """


def safeEditProperty(obj, key, value, data_type='string'):
    """ An add or edit function, surprisingly useful :) """
    if obj.hasProperty(key):
        obj._updateProperty(key, value)
    else:
        obj._setProperty(key, value, data_type)


def tree_socket_remove():
    """Remove an input or output socket to the current node tree

    """
    pass


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

        :param writer: Bit stream where to write.
        :param value: Zserio string type to write.
        """
    writer.writeString(value)


def identity(arg):
    """
    Function which returns the argument. Used as a default lambda function.

    >>> obj = object()
    >>> obj is identity(obj)
    True

    :param arg: object to take identity of
    :return: return arg
    """
    return arg


@classmethod
def getTimezone(cls):
    """Returns the pytz timezone for the market session."""
    return cls.timezone


def describe_workflow_type(domain=None, workflowType=None):
    """
    Returns information about the specified workflow type . This includes configuration settings specified when the type was registered and other information such as creation date, current status, and so on.
    Access Control
    You can use IAM policies to control this action's access to Amazon SWF resources as follows:
    If the caller does not have sufficient permissions to invoke the action, or the parameter values fall outside the specified constraints, the action fails. The associated event attribute's cause parameter will be set to OPERATION_NOT_PERMITTED. For details and example IAM policies, see Using IAM to Manage Access to Amazon SWF Workflows .
    See also: AWS API Documentation

    :example: response = client.describe_workflow_type(
        domain='string',
        workflowType={
            'name': 'string',
            'version': 'string'
        }
    )

    :type domain: string
    :param domain: [REQUIRED]
            The name of the domain in which this workflow type is registered.

    :type workflowType: dict
    :param workflowType: [REQUIRED]
            The workflow type to describe.
            name (string) -- [REQUIRED]Required. The name of the workflow type.
            Note
            The combination of workflow type name and version must be unique with in a domain.
            version (string) -- [REQUIRED]Required. The version of the workflow type.
            Note
            The combination of workflow type name and version must be unique with in a domain.

    :rtype: dict
    :return: {
        'typeInfo': {
            'workflowType': {
                'name': 'string',
                'version': 'string'
            },
            'status': 'REGISTERED'|'DEPRECATED',
            'description': 'string',
            'creationDate': datetime(2015, 1, 1),
            'deprecationDate': datetime(2015, 1, 1)
        },
        'configuration': {
            'defaultTaskStartToCloseTimeout': 'string',
            'defaultExecutionStartToCloseTimeout': 'string',
            'defaultTaskList': {
                'name': 'string'
            },
            'defaultTaskPriority': 'string',
            'defaultChildPolicy': 'TERMINATE'|'REQUEST_CANCEL'|'ABANDON',
            'defaultLambdaRole': 'string'
        }
    }

    :returns:
    domain (string) -- [REQUIRED]
    The name of the domain in which this workflow type is registered.

    workflowType (dict) -- [REQUIRED]
    The workflow type to describe.

    name (string) -- [REQUIRED]Required. The name of the workflow type.

    Note
    The combination of workflow type name and version must be unique with in a domain.

    version (string) -- [REQUIRED]Required. The version of the workflow type.

    Note
    The combination of workflow type name and version must be unique with in a domain.

    """
    pass


def activity_region(activity_arn):
    """
    Return the region for an AWS activity_arn.

    'arn:aws:states:us-east-2:123456789012:stateMachine:hello_world' => 'us-east-2'
    """
    if activity_arn:
        parts = activity_arn.split(':')
        return parts[3] if len(parts) >= 4 else None
    else:
        return None


def StripUnicode(string, subs='.'):
    """Strip non-ASCII characters from strings

    :param str string: string to strip Unicode characters from
    :param str subs: character(s) to place into string in place of each
      Unicode character. Defaults to '.'

    :returns: a new string with only ASCII characters
    """
    s = ''
    for c in string:
        if ord(c) < 128:
            s += c
        else:
            s += subs
    return s.encode('ascii', 'replace')


def rect_to_tuple(rect):
    """
    Convert a QRectF into a (x, y, width, height) tuple.
    """
    return rect.x(), rect.y(), rect.width(), rect.height()


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


def intersect_line_line(line1_start, line1_end, line2_start, line2_end):
    """
    Find the point where the lines (segments) defined by
    ``(line1_start, line1_end)`` and ``(line2_start, line2_end)``
    intersect.  If no intersection occurs, ``None`` is returned.

    >>> intersect_line_line((3, 0), (8, 10), (0, 0), (10, 10))
    (6, 6)
    >>> intersect_line_line((0, 0), (10, 10), (3, 0), (8, 10))
    (6, 6)
    >>> intersect_line_line((0, 0), (10, 10), (8, 10), (3, 0))
    (6, 6)
    >>> intersect_line_line((8, 10), (3, 0), (0, 0), (10, 10))
    (6, 6)
    >>> intersect_line_line((0, 0), (0, 10), (3, 0), (8, 10))
    >>> intersect_line_line((0, 0), (0, 10), (3, 0), (3, 10))

    Ticket #168:
    >>> intersect_line_line((478.0, 117.0), (478.0, 166.0), (527.5, 141.5), (336.5, 139.5))
    (478.5, 141.48167539267016)
    >>> intersect_line_line((527.5, 141.5), (336.5, 139.5), (478.0, 117.0), (478.0, 166.0))
    (478.5, 141.48167539267016)

    This is a Python translation of the ``lines_intersect``, C Code from
    Graphics Gems II, Academic Press, Inc. The original routine was written
    by Mukesh Prasad.

    EULA: The Graphics Gems code is copyright-protected. In other words, you
    cannot claim the text of the code as your own and resell it. Using the code
    is permitted in any program, product, or library, non-commercial or
    commercial. Giving credit is not required, though is a nice gesture. The
    code comes as-is, and if there are any flaws or problems with any Gems
    code, nobody involved with Gems - authors, editors, publishers, or
    webmasters - are to be held responsible. Basically, don't be a jerk, and
    remember that anything free comes with no guarantee.
    """
    x1, y1 = line1_start
    x2, y2 = line1_end
    x3, y3 = line2_start
    x4, y4 = line2_end
    a1 = y2 - y1
    b1 = x1 - x2
    c1 = x2 * y1 - x1 * y2
    r3 = a1 * x3 + b1 * y3 + c1
    r4 = a1 * x4 + b1 * y4 + c1
    if r3 and r4 and r3 * r4 >= 0:
        return None
    a2 = y4 - y3
    b2 = x3 - x4
    c2 = x4 * y3 - x3 * y4
    r1 = a2 * x1 + b2 * y1 + c2
    r2 = a2 * x2 + b2 * y2 + c2
    if r1 and r2 and r1 * r2 >= 0:
        return None
    denom = a1 * b2 - a2 * b1
    x_num = b1 * c2 - b2 * c1
    y_num = a2 * c1 - a1 * c2
    if not denom:
        return None
    elif isinstance(denom, float):
        offset = abs(denom) / 2
        x = (x_num < 0 and x_num - offset or x_num + offset) / denom
        y = (y_num < 0 and y_num - offset or y_num + offset) / denom
    else:
        offset = abs(denom) // 2
        x = (x_num < 0 and x_num - offset or x_num + offset) // denom
        y = (y_num < 0 and y_num - offset or y_num + offset) // denom
    return x, y


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


@staticmethod
def _format_like(value) ->str:
    """
        Formatting value in the event of LIKE operation
        :param value: str
        :return: str
        """
    return "'{}'".format(value)


def structure(id):
    """Get a list with all the templates in this collection.

        id - the collection id

        All recognized template extensions are reported; unrecognized
        extensions are ignored. Subdirectories are also reported.

        Returned is a list of all entries.

        List entries for templates look like this:

        { 'template': 'template1.st',
        'name': 'template1',
        'extension': '.st'
        'path': 'template1.st',
        }

        template: the name of the template as it is in its immediate
                  directory.
        name: the name of the template without extension
        extension: the template extension
        path: the relative path to the extension from the collection_path

        List entries for sub directories look like this:

        { 'directory': 'subdirname',
        'entries': [ ... ],
        'path': 'subdirname',
        }

        directory: the name of the directory
        entries: the entries of the subdirectory, in a list
        path: the relative path to this directory from the collection_path
        """


def _scope_repr(scope_obj):
    """Uses laurelin constant name representation for scope"""
    try:
        intval = int(scope_obj)
        name = scope_obj.namedValues.getName(intval)
        if name == 'wholeSubtree':
            return 'Scope.SUB'
        elif name == 'singleLevel':
            return 'Scope.ONE'
        elif name == 'baseObject':
            return 'Scope.BASE'
        else:
            return '{0}({1})'.format(scope_obj.__class__.__name__, repr(name))
    except Exception:
        return '{0}(<schema object>)'.format(scope_obj.__class__.__name__)


def removeScore():
    """Remove the score for the context from the index"""


def get_root_sequence(root_node, ref=None, translations=None):
    """
    create a json structure that contains the sequence of the root, both as
    nucleotide and as translations. This allows look-up of the sequence for
    all states, including those that are not variable.

    Parameters
    ----------
    root_node : dict
    	data associated with the node
    ref : str, optional
        filename of the root sequence
    translations : str, optional
        file name of translations

    Returns
    -------
    dict
        dict of nucleotide sequence and translations
    """
    root_sequence = {}
    if ref and translations:
        from Bio import SeqIO
        refseq = SeqIO.read(ref, 'fasta')
        root_sequence['nuc'] = str(refseq.seq)
        for gene in SeqIO.parse(translations, 'fasta'):
            root_sequence[gene.id] = str(gene.seq)
    else:
        root_sequence['nuc'] = root_node['sequence']
        root_sequence.update(root_node['aa_sequences'])
    return root_sequence


def system_reduction_factor(delta_ss, delta_frot, delta_fshear, eta_ss,
    eta_frot, eta_fshear):
    """
    Calculates the system displacement reduction factor based on the foundation and superstrucutre
    displacement reduction factors.
    :param delta_ss: superstructure displacement
    :param delta_frot: displacement due to foundation rotation
    :param delta_fshear: displacement due to soil-foundation shear deformation
    :param eta_ss: superstructure displacement reduction factor
    :param eta_frot: foundation rotation displacement reduction factor
    :param eta_fshear: soil foundation shear deformation displacement reduction factor
    :return:
    """
    delta_total = delta_ss + delta_frot + delta_fshear
    return (delta_ss * eta_ss + delta_frot * eta_frot + delta_fshear *
        eta_fshear) / delta_total


def o2s(obmt):
    """Convert obmt to seconds."""
    return obmt * 21600


def get_url(sandbox=True):
    """Return the url for the sandbox or the real account

    Usage::
        url = get_url(sandbox=sandbox)

    :type sandbox: bool
    :param sandbox: determines whether you get the url for the sandbox
    account (True) or the real account (False).

    :rtype: str
    :returns: the entrypoint url to access the FNAC WebServices

    """
    use_sandbox = {(True): 'https://marketplace.ws.fd-recette.net/api.php/',
        (False): 'https://vendeur.fnac.com/api.php/'}
    return use_sandbox[sandbox]


def car(obj):
    """
    Alias of ``obj[0]``.

    >>> car(loads('(a . b)'))
    Symbol('a')
    >>> car(loads('(a b)'))
    Symbol('a')

    """
    return obj[0]


def validate_type(input, *types):
    """Raise TypeError if the type of ``input`` is one of the args

    If the input value is one of the types specified, just return
    the input value.

    """
    if not isinstance(input, types):
        raise TypeError('Input expected to one of following types: %s' % (
            types,))
    return input


def _extend_instance(obj, cls):
    """Apply mixin to a class instance after creation"""
    base_cls = obj.__class__
    base_cls_name = obj.__class__.__name__
    obj.__class__ = type(base_cls_name, (cls, base_cls), {})


def invalidStatus(context, request, view):
    """
        Return the integer status of that the response should be considering
        that this validator evaluated the request as invalid.

        If more then more validator are involved in failing a request then
        the first validator used will in the validation algorithm will be
        used. This isn't ideal but in practice, this problem is unlikely to
        show up.
        """


@classmethod
def all(cls):
    """
            LogTypes.all takes no arguments and returns a list of all valid log magic numbers (from the protobuf definition)
            that can be retrieved using the AdminClient .getLog method. Log types avaiable are: (0-> Utilizations, 1-> Temperatures,
            2->Drive Capacity, 3-> Drive Configuration, 4->Drive usage statistics, and 5-> Drive messages). This can be passed as
            the sole argument to the AdminClient.getLog function.
        """
    return [cls.UTILIZATIONS, cls.TEMPERATURES, cls.CAPACITIES, cls.
        CONFIGURATION, cls.STATISTICS, cls.MESSAGES, cls.LIMITS]


def xor(*xs):
    """Normalizes all of the arguments to Boolean values and then returns
    the Boolean exclusive-or of all the values. Specifically, this
    function returns True if and only if an odd number of the
    arguments are truthy and False otherwise.

    """
    final = False
    for x in xs:
        if x:
            final = not final
    return final


def require_keys(keys, dikt):
    """Verifies that keys appear in the specified dictionary.

    Parameters
    ----------
    keys : list of str
        List of required keys.

    dikt : dict
        The dictionary that is checked for keys.

    Returns
    -------
    None

    Raises
    ------
    Exception
        Raised when a required key is not present.

    """
    dikt_keys = set(dikt)
    for key in keys:
        if key not in dikt_keys:
            msg = "'%s' is required." % (key,)
            raise Exception(msg)


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 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_children_for(cls, store, parent):
    """Get a list of child accounts for

        :param store:
        :param |account| parent: parent account
        :returns: the child accounts
        :rtype: resultset
        """
    return store.find(cls, parent=parent)


def _calculate_p0(fmep_model, params, engine_capacity, engine_stroke,
    idle_engine_speed_median, engine_fuel_lower_heating_value):
    """
    Calculates the engine power threshold limit [kW].

    :param params:
        CO2 emission model parameters (a2, b2, a, b, c, l, l2, t, trg).

        The missing parameters are set equal to zero.
    :type params: dict

    :param engine_capacity:
        Engine capacity [cm3].
    :type engine_capacity: float

    :param engine_stroke:
        Engine stroke [mm].
    :type engine_stroke: float

    :param idle_engine_speed_median:
        Engine speed idle median [RPM].
    :type idle_engine_speed_median: float

    :param engine_fuel_lower_heating_value:
        Fuel lower heating value [kJ/kg].
    :type engine_fuel_lower_heating_value: float

    :param fmep_model:
        Engine FMEP model.
    :type fmep_model: FMEP

    :return:
        Engine power threshold limit [kW].
    :rtype: float
    """
    engine_cm_idle = idle_engine_speed_median * engine_stroke / 30000.0
    lhv = engine_fuel_lower_heating_value
    wfb_idle, wfa_idle = fmep_model(params, engine_cm_idle, 0, 1)[:2]
    wfa_idle = 3600000.0 / lhv / wfa_idle
    wfb_idle *= 3.0 * engine_capacity / lhv * idle_engine_speed_median
    return -wfb_idle / wfa_idle


def iddofobject(data, commdct, key):
    """from commdct, return the idd of the object key"""
    dtls = data.dtls
    i = dtls.index(key)
    return commdct[i]


def normalize(x, xmin, xmax):
    """Normalize a number to a 0-1 range given a min and max of its set."""
    return float(x - xmin) / float(xmax - xmin)


def get_license_vector(lic):
    """Return pseudo-choice vector given license."""
    data, _ = lic
    vec = {}
    for dim in ['forbidden', 'required', 'permitted']:
        if data[dim] is None:
            continue
        dim_vec = map(lambda x: (x, 5), data[dim])
        vec[dim] = dict(dim_vec)
    return vec


def get_field_from_args_or_session(config, args, field_name):
    """
    We try to get field_name from diffent sources:
    The order of priorioty is following:
   read_default_contract_address - command line argument (--<field_name>)
    - current session configuration (default_<filed_name>)
    """
    rez = getattr(args, field_name, None)
    if rez is not None:
        return rez
    rez = config.get_session_field('default_%s' % field_name,
        exception_if_not_found=False)
    if rez:
        return rez
    raise Exception(
        'Fail to get default_%s from config, should specify %s via --%s parameter'
         % (field_name, field_name, field_name.replace('_', '-')))


@classmethod
def from_dict(cls, classname, d):
    """for (serpent) deserialization"""
    assert classname == cls.serialized_classname
    obj = cls(d['a'], d['b'], d['c'])
    return obj


@classmethod
def id2url(cls, _id):
    """Overloaded in child classes provided by backends."""
    raise NotImplementedError()


def get_principals_and_permissions():
    """Get all principal permission settings.

        Get the principal security assertions here in the form
        of a list of three tuple containing
        (permission id, principal id, setting)
        """


@classmethod
def _encrypt(cls, secret, data):
    """ Placeholder symmetric encryptor.

        Data should be bytes-like. Key should be bytes-like.
        """
    return data


@staticmethod
def _maybe_scale(num):
    """Return inverse of num if num is larger than one."""
    return num if num < 1 else 1 / num


def replace_metrics_class(fname):
    """Replace metrics class name.

    This temporary fix is needed due to compatibility issue with GATK
    4.1.2.0 and MultiQC 1.8. MultiQC searches for CollectWgsMetrics
    instead of WgsMetrics in the report file. Note that this should be
    resolved in the 1.9 release of MultiQC.
    """
    with open(fname, 'r') as report:
        newlines = []
        for line in report.readlines():
            if line == '## METRICS CLASS\tpicard.analysis.WgsMetrics\n':
                line = '## METRICS CLASS\tCollectWgsMetrics$WgsMetrics\n'
                newlines.append(line)
            else:
                newlines.append(line)
    with open(fname, 'w') as report:
        for line in newlines:
            report.writelines(line)


@staticmethod
def is_snake_case(string: str) ->bool:
    """
        Check if a string is snake_case.

        :param string: The string in input
        :return: bool
        """
    return string == string.lower()


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

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


@classmethod
def initialize(cls):
    """ Initialize static data structures, once all trips have been created """
    for trip in cls._all_trips.values():
        trip._initialize()


def device_type(ctx):
    """Return a str representing device type"""
    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 def_default_float_token(parser, token_label='k_float', signed=True,
    require_decimal=False, on_ties=0):
    """Define a token for floats with default label 'k_float'.  If `signed` is true (the
    default) then a leading '+' or '-' is optionally part of the float.  Otherwise
    the sign is not included.  This is sometimes needed when the signs are defined
    as a prefix operators instead."""
    if require_decimal:
        regex = '(\\d+\\.\\d*)([eE][-+]?\\d+)?'
    else:
        regex = '(\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?'
    if signed:
        regex = '[+-]?' + regex
    tok = parser.def_token(token_label, regex, on_ties=on_ties)
    return tok


def tocode(ast_):
    """ From AST to string of code """
    try:
        import astor
        return astor.to_source(ast_)
    except ImportError:
        print("Warning: couldn't find module 'astor'; nothing will be done")


def setContributors(contributors):
    """ Set DCMI Contributor elements - resource collaborators.

        o Permission:  Modify portal content
        """


def load_file(path):
    """Read contents of file

    :param path: File to read
    """
    with open(path, 'r') as fp:
        return fp.read()


def prefix_as_comment(comment_prefix, text):
    """Adds comment prefixes to new lines in comments

    """
    return text.replace('\n', '\n' + comment_prefix)


@staticmethod
def assignee_from_components(components):
    """
            Return the first owner which is assigned to any of the
            components. This is as best as we can to automatically figure
            out who should be assigned to this bug.
        """
    for component in components:
        if component.initial_owner:
            return component.initial_owner
    return None


def short_node_gen(graph, inqueue, dst_sid, procno):
    """
	Adding all nodes from the graph to the inqueue. At the end it adds terminating object in the amount of proccount.
	This function will be threaded.
	"""
    for node in graph.nodes:
        if node == dst_sid:
            continue
        inqueue.put(node)
    for _ in range(procno):
        inqueue.put(None)


def override(d, **kwds):
    """Adds entries from (kwds) to (d) even if they are already set."""
    d.update(kwds)


def isDerivedNode(node):
    """Returns True if this node is built."""
    return node.is_derived()


def translate_string(this_string):
    """
    Do silly translation.

    :param this_string:
    :return: esta_cuerda

    """
    this_string = this_string.replace(' of ', ' de ')
    this_string = this_string.replace(' W ', ' O ')
    this_string = this_string.replace('NW', 'NO')
    this_string = this_string.replace('WNW', 'ONO')
    this_string = this_string.replace('WSW', 'OSO')
    this_string = this_string.replace('SW', 'SO')
    return this_string


def chomp_equals(s):
    """ Input: == zomg ==,
    Output: (2, "zomg")"""
    chomped = s
    equals = 0
    while chomped and chomped[0] == chomped[-1] == '=':
        chomped = chomped[1:-1]
        equals += 1
    return equals, chomped.strip()


def invert_ext(P):
    """
    Inverts extended point coordinate
    :param P:
    :return:
    """
    return P.invert()


def sync_frame_initializer(new_frame, reference_frame):
    """Set the initializers of the new_frame to be the same as the reference_frame,
    for both the default initializer and per-column initializers.

    Parameters
    ----------
    new_frame : Frame
        The frame to set initializers
    reference_frame : Frame
        The frame to copy initializers
    """
    new_frame._default_initializer = reference_frame._default_initializer
    new_frame._initializers = reference_frame._initializers


def has_variety(seq):
    """Return True if there are any different elements in ``seq``.

    Examples
    ========

    >>> has_variety((1, 2, 1))
    True
    >>> has_variety((1, 1, 1))
    False

    """
    for i, s in enumerate(seq):
        if i == 0:
            sentinel = s
        elif s != sentinel:
            return True
    return False


def psi_H_Na_ClO4_PK74(T, P):
    """c-c'-a: hydrogen sodium perchlorate [PK74]."""
    psi = -0.016
    valid = T == 298.15
    return psi, valid


def _get_changed_fields(model_instance):
    """ Return the changed fields data structure
    This will be bound to a model instance & should only ever
    be called from a model instance
    """
    return model_instance._changed_fields


def _mat10_get_bulk_rho_c(bulk, rho, c):
    """.. math:: bulk = c^2 \\rho"""
    if c is not None:
        if rho is not None:
            bulk = c ** 2.0 * rho
        elif bulk is not None:
            rho = bulk / c ** 2.0
        else:
            msg = 'c is the only field defined on the MAT10'
            raise RuntimeError(msg)
    elif bulk is not None:
        if rho is not None:
            try:
                c = (bulk / rho) ** 0.5
            except ValueError:
                msg = 'bulk=%s rho=%s c=sqrt(bulk/rho)=NA' % (bulk, rho)
                raise ValueError(msg)
        else:
            msg = 'c, bulk, and rho are all undefined on tbe MAT10'
            raise RuntimeError(msg)
    else:
        msg = 'c, bulk, and rho are all undefined on tbe MAT10'
        raise RuntimeError(msg)
    return bulk, rho, c


def p_statement_expr(p):
    """statement : expr SEMI"""
    p[0] = p[1]


def normalize(ascii_pinyin):
    """ Normalises common pinyin variants to canonical form."""
    normal_version = ascii_pinyin.lower().replace(' ', '')
    normal_version = normal_version.replace('v', 'ü')
    return normal_version


def update_function_configuration(FunctionName=None, Role=None, Handler=
    None, Description=None, Timeout=None, MemorySize=None, VpcConfig=None,
    Environment=None, Runtime=None, DeadLetterConfig=None, KMSKeyArn=None,
    TracingConfig=None):
    """
    Updates the configuration parameters for the specified Lambda function by using the values provided in the request. You provide only the parameters you want to change. This operation must only be used on an existing Lambda function and cannot be used to update the function's code.
    If you are using the versioning feature, note this API will always update the $LATEST version of your Lambda function. For information about the versioning feature, see AWS Lambda Function Versioning and Aliases .
    This operation requires permission for the lambda:UpdateFunctionConfiguration action.
    See also: AWS API Documentation

    Examples
    This operation updates a Lambda function's configuration
    Expected Output:

    :example: response = client.update_function_configuration(
        FunctionName='string',
        Role='string',
        Handler='string',
        Description='string',
        Timeout=123,
        MemorySize=123,
        VpcConfig={
            'SubnetIds': [
                'string',
            ],
            'SecurityGroupIds': [
                'string',
            ]
        },
        Environment={
            'Variables': {
                'string': 'string'
            }
        },
        Runtime='nodejs'|'nodejs4.3'|'nodejs6.10'|'java8'|'python2.7'|'python3.6'|'dotnetcore1.0'|'nodejs4.3-edge',
        DeadLetterConfig={
            'TargetArn': 'string'
        },
        KMSKeyArn='string',
        TracingConfig={
            'Mode': 'Active'|'PassThrough'
        }
    )

    :type FunctionName: string
    :param FunctionName: [REQUIRED]
            The name of the Lambda function.
            You can specify a function name (for example, Thumbnail ) or you can specify Amazon Resource Name (ARN) of the function (for example, arn:aws:lambda:us-west-2:account-id:function:ThumbNail ). AWS Lambda also allows you to specify a partial ARN (for example, account-id:Thumbnail ). Note that the length constraint applies only to the ARN. If you specify only the function name, it is limited to 64 character in length.

    :type Role: string
    :param Role: The Amazon Resource Name (ARN) of the IAM role that Lambda will assume when it executes your function.

    :type Handler: string
    :param Handler: The function that Lambda calls to begin executing your function. For Node.js, it is the module-name.export value in your function.

    :type Description: string
    :param Description: A short user-defined function description. AWS Lambda does not use this value. Assign a meaningful description as you see fit.

    :type Timeout: integer
    :param Timeout: The function execution time at which AWS Lambda should terminate the function. Because the execution time has cost implications, we recommend you set this value based on your expected execution time. The default is 3 seconds.

    :type MemorySize: integer
    :param MemorySize: The amount of memory, in MB, your Lambda function is given. AWS Lambda uses this memory size to infer the amount of CPU allocated to your function. Your function use-case determines your CPU and memory requirements. For example, a database operation might need less memory compared to an image processing function. The default value is 128 MB. The value must be a multiple of 64 MB.

    :type VpcConfig: dict
    :param VpcConfig: If your Lambda function accesses resources in a VPC, you provide this parameter identifying the list of security group IDs and subnet IDs. These must belong to the same VPC. You must provide at least one security group and one subnet ID.
            SubnetIds (list) --A list of one or more subnet IDs in your VPC.
            (string) --
            SecurityGroupIds (list) --A list of one or more security groups IDs in your VPC.
            (string) --

    :type Environment: dict
    :param Environment: The parent object that contains your environment's configuration settings.
            Variables (dict) --The key-value pairs that represent your environment's configuration settings.
            (string) --
            (string) --

    :type Runtime: string
    :param Runtime: The runtime environment for the Lambda function.
            To use the Python runtime v3.6, set the value to 'python3.6'. To use the Python runtime v2.7, set the value to 'python2.7'. To use the Node.js runtime v6.10, set the value to 'nodejs6.10'. To use the Node.js runtime v4.3, set the value to 'nodejs4.3'. To use the Python runtime v3.6, set the value to 'python3.6'. To use the Python runtime v2.7, set the value to 'python2.7'.
            Note
            You can no longer downgrade to the v0.10.42 runtime version. This version will no longer be supported as of early 2017.

    :type DeadLetterConfig: dict
    :param DeadLetterConfig: The parent object that contains the target ARN (Amazon Resource Name) of an Amazon SQS queue or Amazon SNS topic.
            TargetArn (string) --The Amazon Resource Name (ARN) of an Amazon SQS queue or Amazon SNS topic you specify as your Dead Letter Queue (DLQ).

    :type KMSKeyArn: string
    :param KMSKeyArn: The Amazon Resource Name (ARN) of the KMS key used to encrypt your function's environment variables. If you elect to use the AWS Lambda default service key, pass in an empty string ('') for this parameter.

    :type TracingConfig: dict
    :param TracingConfig: The parent object that contains your function's tracing settings.
            Mode (string) --Can be either PassThrough or Active. If PassThrough, Lambda will only trace the request from an upstream service if it contains a tracing header with 'sampled=1'. If Active, Lambda will respect any tracing header it receives from an upstream service. If no tracing header is received, Lambda will call X-Ray for a tracing decision.

    :rtype: dict
    :return: {
        'FunctionName': 'string',
        'FunctionArn': 'string',
        'Runtime': 'nodejs'|'nodejs4.3'|'nodejs6.10'|'java8'|'python2.7'|'python3.6'|'dotnetcore1.0'|'nodejs4.3-edge',
        'Role': 'string',
        'Handler': 'string',
        'CodeSize': 123,
        'Description': 'string',
        'Timeout': 123,
        'MemorySize': 123,
        'LastModified': 'string',
        'CodeSha256': 'string',
        'Version': 'string',
        'VpcConfig': {
            'SubnetIds': [
                'string',
            ],
            'SecurityGroupIds': [
                'string',
            ],
            'VpcId': 'string'
        },
        'DeadLetterConfig': {
            'TargetArn': 'string'
        },
        'Environment': {
            'Variables': {
                'string': 'string'
            },
            'Error': {
                'ErrorCode': 'string',
                'Message': 'string'
            }
        },
        'KMSKeyArn': 'string',
        'TracingConfig': {
            'Mode': 'Active'|'PassThrough'
        }
    }

    :returns:
    (string) --

    """
    pass


def find_blocksize(file_len):
    """Return a reasonable block size to use on files of length file_len

    If the block size is too big, deltas will be bigger than is
    necessary.  If the block size is too small, making deltas and
    patching can take a really long time.

    """
    if file_len < 4096:
        return 64
    else:
        return int(pow(file_len, 0.5) / 16) * 16


def select_linked():
    """Select all points in same strokes as already selected points

    """
    pass


def get_repo_config_from_environment():
    """Look for repository config in any defined environment variables. If
    found, import to main config."""
    import logging
    import os
    if 'GAD_REPO_URL' not in os.environ:
        return
    logger = logging.getLogger()
    repo_config = {'url': os.environ['GAD_REPO_URL']}
    logger.info(
        "Added configuration for '%s' found in environment variables" % os.
        environ['GAD_REPO_URL'])
    if 'GAD_REPO_BRANCH' in os.environ:
        repo_config['branch'] = os.environ['GAD_REPO_BRANCH']
    if 'GAD_REPO_REMOTE' in os.environ:
        repo_config['remote'] = os.environ['GAD_REPO_REMOTE']
    if 'GAD_REPO_PATH' in os.environ:
        repo_config['path'] = os.environ['GAD_REPO_PATH']
    if 'GAD_REPO_DEPLOY' in os.environ:
        repo_config['deploy'] = os.environ['GAD_REPO_DEPLOY']
    return repo_config