def left_pad(input, length, fill_character=' '):
    """
    Returns a string, which will be padded on the left with characters if necessary.
    If the input string is longer than the specified length, it will be returned unchanged.

    >>> left_pad('foo', 5)
    '  foo'

    >>> left_pad('foobar', 6)
    'foobar'

    >>> left_pad('toolong', 2)
    'toolong'

    >>> left_pad(1, 2, '0')
    '01'

    >>> left_pad(17, 5, 0)
    '00017'

    :param input:
    :param length: The return string's desired length.
    :param fill_character:
    "rtype str:
    """
    return str(input).rjust(length, str(fill_character))


def bind(object):
    """Return a copy of this field which is bound to context.

        The copy of the Field will have the 'context' attribute set
        to 'object'.  This way a Field can implement more complex
        checks involving the object's location/environment.

        Many fields don't need to be bound. Only fields that condition
        validation or properties on an object containing the field
        need to be bound.
        """


def pad_with(vector, pad_width, iaxis, kwargs):
    """
    Taken from Numpy documentation, will pad with zeros to make lofar image same size as other image
    :param vector:
    :param pad_width:
    :param iaxis:
    :param kwargs:
    :return:
    """
    pad_value = kwargs.get('padder', 0)
    vector[:pad_width[0]] = pad_value
    vector[-pad_width[1]:] = pad_value
    return vector


@classmethod
def setSystemWideTimeout(cls, timeout=0.0):
    """Set the system-wide accessibility timeout.

        Optional: timeout (non-negative float; defaults to 0)
                  A value of 0 will reset the timeout to the system default.
        Returns: None.
        """
    return cls.getSystemObject().setTimeout(timeout)


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

        :param PaperExternalViewDefaultTeamDetails val:
        :rtype: EventDetails
        """
    return cls('paper_external_view_default_team_details', val)


def rules_stats(rules, itemsets, n_examples):
    """
    Generate additional stats for rules generated by :obj:`association_rules()`.

    Parameters
    ----------
    rules: iterable
        Rules as output by `association_rules()`.
    itemsets: dict
        The itemsets as obtained by `dict(frequent_itemsets(...))`.
    n_examples: int
        The total number of instances (for calculating coverage, lift,
        and leverage).

    Yields
    ------
    atecedent: frozenset
        The LHS of the association rule.
    consequent: frozenset
        The RHS of the association rule.
    support: int
        Support as an absolute number of instances.
    confidence: float
        The confidence percent, calculated as: ``total_support / lhs_rupport``.
    coverage: float
        Calculated as: ``lhs_support / n_examples``
    strength: float
        Calculated as: ``rhs_support / lhs_examples``
    lift: float
        Calculated as: ``n_examples * total_support / lhs_support / rhs_support``
    leverage: float
        Calculated as: ``(total_support * n_examples - lhs_support * rhs_support) / n_examples**2``

    Examples
    --------
    >>> N = 30
    >>> X = np.random.random((N, 50)) > .9
    >>> itemsets = dict(frequent_itemsets(X, .1))
    >>> rules = association_rules(itemsets, .6)
    >>> list(rules_stats(rules, itemsets, N))
    [(frozenset({15}), frozenset({0}), 3, 0.75, 0.13..., 1.5, 3.75, 0.073...),
     (frozenset({47}), frozenset({22}), 3, 0.6, 0.16..., 1.4, 2.57..., 0.061...),
     (frozenset({27}), frozenset({22}), 4, 0.66..., 0.2, 1.16..., 2.85..., 0.086...),
     (frozenset({19}), frozenset({22}), 3, 0.6, 0.16..., 1.4, 2.57..., 0.061...)]

    """
    assert isinstance(itemsets, dict) and isinstance(next(iter(itemsets),
        frozenset()), frozenset)
    assert n_examples > 0
    for left, right, support, confidence in rules:
        l_support, r_support = itemsets[left], itemsets[right]
        coverage = l_support / n_examples
        strength = r_support / l_support
        lift = n_examples * confidence / r_support
        leverage = (support * n_examples - l_support * r_support
            ) / n_examples ** 2
        yield left, right, support, confidence, coverage, strength, lift, leverage


def _coordinate(start, end, string):
    """Get coordinate from stirng position index"""
    srow = string[:start].count('\n') + 1
    scolsplitlines = string[:start].split('\n')
    if len(scolsplitlines) != 0:
        scolsplitlines = scolsplitlines[len(scolsplitlines) - 1]
    scol = len(scolsplitlines)
    lrow = string[:end + 1].count('\n') + 1
    lcolsplitlines = string[:end].split('\n')
    if len(lcolsplitlines) != 0:
        lcolsplitlines = lcolsplitlines[len(lcolsplitlines) - 1]
    lcol = len(lcolsplitlines) + 1
    return '{}.{}'.format(srow, scol), '{}.{}'.format(lrow, lcol)


@staticmethod
def _send(recipients, text_content=None, html_content=None, sent_from=None,
    subject=None, extra_data=None, attachments=None):
    """
        Handles the actual sending of the notification. Sub classes must override this
        """
    raise NotImplementedError('Must implement send.')


def distance_hue(h1: float, h2: float) ->float:
    """HUE based distance
    distance > 0 -> "clockwise"
    distance < 0 -> "counter-clocwise"
    """
    dist = h2 - h1
    if abs(dist) > 0.5:
        dist = (-1 if dist > 0 else 1) * (1 - abs(dist))
    return dist


@classmethod
def from_unsupported(cls, allowed):
    """
        Compile time error. Restriction syntax is incorrect.
        """
    return cls("Malformed restriction. Allowed '%s' is of type '%s'." % (
        allowed, type(allowed)))


def getContainerKey():
    """
        Return the key under which the object was placed in its
        container.
        """


def _initializer(kwargs):
    """
    Initializer for the multiprocessing pool. It is mainly use to ensure that
    processes on *Windows* correctly inherit from the current domain-range
    scale.

    Parameters
    ----------
    kwargs : dict
        Initialisation arguments.
    """
    global _DOMAIN_RANGE_SCALE
    _DOMAIN_RANGE_SCALE = kwargs.get('scale', 'reference')


def to_rfc822(dt):
    """Given a datetime.datetime, return an RFC 822-formatted unicode.

        Sun, 06 Nov 1994 08:49:37 GMT

    According to RFC 1123, day and month names must always be in English. If
    not for that, this code could use strftime(). It can't because strftime()
    honors the locale and could generated non-English names.

    """
    t = dt.utctimetuple()
    return '%s, %02d %s %04d %02d:%02d:%02d GMT' % (('Mon', 'Tue', 'Wed',
        'Thu', 'Fri', 'Sat', 'Sun')[t[6]], t[2], ('Jan', 'Feb', 'Mar',
        'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')[t[1] -
        1], t[0], t[3], t[4], t[5])


def select_overlap(extend: bool=False):
    """Select all UV faces which overlap each other

    :param extend: Extend, Extend selection rather than clearing the existing selection
    :type extend: bool
    """
    pass


def get_device(DeviceKey=None, AccessToken=None):
    """
    Gets the device.
    See also: AWS API Documentation

    :example: response = client.get_device(
        DeviceKey='string',
        AccessToken='string'
    )

    :type DeviceKey: string
    :param DeviceKey: [REQUIRED]
            The device key.

    :type AccessToken: string
    :param AccessToken: The access token.

    :rtype: dict
    :return: {
        'Device': {
            'DeviceKey': 'string',
            'DeviceAttributes': [
                {
                    'Name': 'string',
                    'Value': 'string'
                },
            ],
            'DeviceCreateDate': datetime(2015, 1, 1),
            'DeviceLastModifiedDate': datetime(2015, 1, 1),
            'DeviceLastAuthenticatedDate': datetime(2015, 1, 1)
        }
    }

    """
    pass


@classmethod
def can_parse(cls, dct):
    """
        Returns True if this class can parse the given dict.  This is based on
        the __slots__ and required_fields of the class, and the keys of the dict.
        This is intended to be used when an element may be one of a number of
        allowed Object types - each type should independently consider if it
        can parse the given element, and the first to report that it can should
        be used.

        :param dct: The dict to consider.
        :type dct: dict

        :returns: True if this class can parse dct into an instance of itself,
                  otherwise False
        :rtype: bool
        """
    if not isinstance(dct, dict):
        return False
    for key in dct.keys():
        if key.startswith('x-'):
            continue
        if key not in cls.__slots__:
            return False
    for key in cls.required_fields:
        if key not in dct:
            return False
    return True


def write_alarm_msg(radar_name, param_name_unit, date_last, target, tol_abs,
    np_trend, value_trend, tol_trend, nevents, np_last, value_last, fname):
    """
    writes an alarm file

    Parameters
    ----------
    radar_name : str
        Name of the radar being controlled
    param_name_unit : str
        Parameter and units
    date_last : datetime object
        date of the current event
    target, tol_abs : float
        Target value and tolerance
    np_trend : int
        Total number of points in trend
    value_trend, tol_trend : float
        Trend value and tolerance
    nevents: int
        Number of events in trend
    np_last : int
        Number of points in the current event
    value_last : float
        Value of the current event
    fname : str
        Name of file where to store the alarm information

    Returns
    -------
    fname : str
        the name of the file where data has written

    """
    with open(fname, 'w', newline='') as txtfile:
        txtfile.write(
            'Weather radar polarimetric parameters monitoring alarm\n')
        if radar_name is not None:
            txtfile.write('Radar name: ' + radar_name + '\n')
        txtfile.write('Parameter [Unit]: ' + param_name_unit + '\n')
        txtfile.write('Date : ' + date_last.strftime('%Y%m%d') + '\n')
        txtfile.write('Target value: ' + str(target) + ' +/- ' + str(
            tol_abs) + '\n')
        if np_trend > 0:
            txtfile.write('Number of points in trend: ' + str(np_trend) +
                ' in ' + str(nevents) + ' events\n')
            txtfile.write('Trend value: ' + str(value_trend) +
                ' (tolerance: +/- ' + str(tol_trend) + ')\n')
        else:
            txtfile.write('Number of points in trend: NA\n')
            txtfile.write('Trend value: NA\n')
        txtfile.write('Number of points: ' + str(np_last) + '\n')
        txtfile.write('Value: ' + str(value_last) + '\n')
        txtfile.close()
    return fname


def view_selected():
    """Zoom the sequencer on the selected strips

    """
    pass


def can_edit(brain):
    """View edit button associated to this brain
        """


def revision(node, _df=None, raw=False):
    """Obtain fulltext data for a node."""


def _get_worker_device(job_name, cluster_spec, task_id, ps_strategy):
    """ Returns the worker device to use """
    if job_name != 'learner':
        return None
    from diplomacy_research.utils.tensorflow import tf
    return tf.train.replica_device_setter(cluster=tf.train.ClusterSpec(
        cluster_spec), worker_device='/job:%s/task:%d' % (job_name, task_id
        ), ps_strategy=ps_strategy)


def p_loop_stmt(p):
    """loop_stmt : loop_head suite """
    p[0] = ['LOOP'] + p[1] + [p[2]]


def get_rect_xmin(data):
    """Find minimum x value from four (x,y) vertices."""
    return min(data[0][0], data[1][0], data[2][0], data[3][0])


def AAIndexLookup(records):
    """ Build a dict of AAIndexObjects hashed by ID """
    result = {}
    for r in records:
        result[r.ID] = r
    return result


@classmethod
def __getPath(cls, node):
    """ provides node path

        :param node: etree node
        :type node: :class:`lxml.etree.Element`
        :returns: node path
        :rtype: :obj:`str`
        """
    name = cls.__getAttr(node, 'name')
    if not name:
        return ''
    attr = False
    while node.getparent() is not None:
        onode = node
        node = node.getparent()
        if onode.tag == 'attribute':
            attr = True
        else:
            attr = False
        if node.tag not in ['group', 'field']:
            return name
        else:
            gname = cls.__getAttr(node, 'name')
            if not gname:
                gname = cls.__getAttr(node, 'type')
                if len(gname) > 2:
                    gname = gname[2:]
            if attr:
                name = gname + '@' + name
            else:
                name = gname + '/' + name
            attr = False
    return name


@staticmethod
def set_current_module(module):
    """Sets the active module name."""
    global _current_module
    _current_module = module


def get_service_type(f):
    """Retrieves service type from function."""
    return getattr(f, 'service_type', None)


def get(name, default=None):
    """Return the form field with the given name

        If the desired firld has a prefix, then the given name should
        be the prefix, a dot, and the unprefixed name.  Otherwise, the
        given name is just the field name.

        Return the default if a field can't be found for the given name.
        """


def filter_none(dictionary):
    """
    Returns items in dictionary with a value other than None

       >>> filter_none({'start':0, 'middle': None})
       {'start': 0}
    """
    return dict(filter(lambda x: x[1] != None, dictionary.items()))


def put_bucket_inventory_configuration(Bucket=None, Id=None,
    InventoryConfiguration=None):
    """
    Adds an inventory configuration (identified by the inventory ID) from the bucket.
    See also: AWS API Documentation

    :example: response = client.put_bucket_inventory_configuration(
        Bucket='string',
        Id='string',
        InventoryConfiguration={
            'Destination': {
                'S3BucketDestination': {
                    'AccountId': 'string',
                    'Bucket': 'string',
                    'Format': 'CSV',
                    'Prefix': 'string'
                }
            },
            'IsEnabled': True|False,
            'Filter': {
                'Prefix': 'string'
            },
            'Id': 'string',
            'IncludedObjectVersions': 'All'|'Current',
            'OptionalFields': [
                'Size'|'LastModifiedDate'|'StorageClass'|'ETag'|'IsMultipartUploaded'|'ReplicationStatus',
            ],
            'Schedule': {
                'Frequency': 'Daily'|'Weekly'
            }
        }
    )

    :type Bucket: string
    :param Bucket: [REQUIRED] The name of the bucket where the inventory configuration will be stored.

    :type Id: string
    :param Id: [REQUIRED] The ID used to identify the inventory configuration.

    :type InventoryConfiguration: dict
    :param InventoryConfiguration: [REQUIRED] Specifies the inventory configuration.
            Destination (dict) -- [REQUIRED] Contains information about where to publish the inventory results.
            S3BucketDestination (dict) -- [REQUIRED] Contains the bucket name, file format, bucket owner (optional), and prefix (optional) where inventory results are published.
            AccountId (string) -- The ID of the account that owns the destination bucket.
            Bucket (string) -- [REQUIRED] The Amazon resource name (ARN) of the bucket where inventory results will be published.
            Format (string) -- [REQUIRED] Specifies the output format of the inventory results.
            Prefix (string) -- The prefix that is prepended to all inventory results.

            IsEnabled (boolean) -- [REQUIRED] Specifies whether the inventory is enabled or disabled.
            Filter (dict) -- Specifies an inventory filter. The inventory only includes objects that meet the filter's criteria.
            Prefix (string) -- [REQUIRED] The prefix that an object must have to be included in the inventory results.
            Id (string) -- [REQUIRED] The ID used to identify the inventory configuration.
            IncludedObjectVersions (string) -- [REQUIRED] Specifies which object version(s) to included in the inventory results.
            OptionalFields (list) -- Contains the optional fields that are included in the inventory results.
            (string) --
            Schedule (dict) -- [REQUIRED] Specifies the schedule for generating inventory results.
            Frequency (string) -- [REQUIRED] Specifies how frequently inventory results are produced.

    """
    pass


@staticmethod
def defval(val, default=None):
    """
        Returns val if is not None, default instead
        :param val:
        :param default:
        :return:
        """
    return val if val is not None else default


def transpose_axes(image, axes, asaxes='CTZYX'):
    """Return image with its axes permuted to match specified axes.

    A view is returned if possible.

    >>> transpose_axes(numpy.zeros((2, 3, 4, 5)), 'TYXC', asaxes='CTZYX').shape
    (5, 2, 1, 3, 4)

    """
    for ax in axes:
        if ax not in asaxes:
            raise ValueError('unknown axis %s' % ax)
    shape = image.shape
    for ax in reversed(asaxes):
        if ax not in axes:
            axes = ax + axes
            shape = (1,) + shape
    image = image.reshape(shape)
    image = image.transpose([axes.index(ax) for ax in asaxes])
    return image


def _set_additional_legend_box(ax, legend_args, first_legend=None):
    """Set the legend box of the annotated time points.

    Warnings
    --------
    This method is intended for internal use only.

    """
    points, labels, legend_kwargs = legend_args
    for old_legend in ax.get_children():
        if old_legend.__class__.__name__ == 'Legend':
            old_legend.remove()
    ax.legend(handles=points, labels=labels, **legend_kwargs)
    if first_legend:
        ax.add_artist(first_legend)
    return ax


@classmethod
def order_and_label(cls, t):
    """Order and label of a discrete type.

        Cf. :meth:`Profile.order_and_label`.

        Examples
        --------
            >>> ProfileOrdinal.order_and_label('abc')
            ('abc', '$r(abc)$')
            >>> ProfileOrdinal.order_and_label('a~b>c')
            ('a~b>c', '$r(a\\\\sim b>c)$')
        """
    if len(t) == 3:
        return t, '$r(%s)$' % t
    else:
        return cls.order_and_label_weak(t)


def vertex_group_select():
    """Select all the vertices assigned to the active vertex group

    """
    pass


def cut_multicam(camera: int=1):
    """Cut multi-cam strip and select camera

    :param camera: Camera
    :type camera: int
    """
    pass


@staticmethod
def on_message(client, message):
    """
        User callback: called when the client has received a message

        :param client: The Pelix MQTT client which received a message
        :param message: The MQTT message
        """
    pass


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

        :param FileRenameType val:
        :rtype: EventType
        """
    return cls('file_rename', val)


def get_activities(slug):
    """Get activity information from TimeSync"""
    p_list = [{'name': 'Documentation', 'slug': slug if slug else 'docs',
        'uuid': 'adf036f5-3d49-4a84-bef9-062b46380bbf', 'revision': 5,
        'created_at': '2014-04-17', 'deleted_at': None, 'updated_at': None}]
    if not slug:
        p_list.append({'name': 'Coding', 'slug': 'dev', 'uuid':
            'adf036f5-3d49-bbbb-rrrr-062b46380bbf', 'revision': 1,
            'created_at': '2014-04-17', 'deleted_at': None, 'updated_at': None}
            )
        p_list.append({'name': 'Planning', 'slug': 'plan', 'uuid':
            'adf036f5-3d49-cccc-ssss-062b46380bbf', 'revision': 1,
            'created_at': '2014-04-17', 'deleted_at': None, 'updated_at': None}
            )
    return p_list


@classmethod
def create_from(cls, vehicle):
    """
            Create a new vehicle from an existing one.
            The vehicle dynamics and target dynamics are copied, other properties are default.

        :param vehicle: a vehicle
        :return: a new vehicle at the same dynamical state
        """
    v = cls(vehicle.road, vehicle.position, heading=vehicle.heading,
        velocity=vehicle.velocity, target_lane_index=vehicle.
        target_lane_index, target_velocity=vehicle.target_velocity, route=
        vehicle.route, timer=getattr(vehicle, 'timer', None))
    return v


def to_gtin14(value):
    """ Convert various product codes to  to GTIN-14.

    Will convert UPC-A (GTIN-12), GTIN-13 (EAN / UCC-13), GTIN-8 (EAN / UCC-8)
    to GTIN-14 by left zero padding.

    See http://www.gtin.info/

    Returns:
      (str): The GTIN-14 code.
    """
    return str(value).zfill(14)


def remove_namespaces(tree):
    """Remove namespaces from all elements in the tree."""
    import re
    for elem in tree.iter():
        if isinstance(elem.tag, ''.__class__):
            elem.tag = re.sub('^\\{[^}]*\\}', '', elem.tag)


@staticmethod
def _escape_attribute_quotes(v, attr_wrapper):
    """
        Escapes quotes with a backslash, except those inside a Django tag
        """
    escaped = []
    inside_tag = False
    for i, _ in enumerate(v):
        if v[i:i + 2] == '{%':
            inside_tag = True
        elif v[i:i + 2] == '%}':
            inside_tag = False
        if v[i] == attr_wrapper and not inside_tag:
            escaped.append('\\')
        escaped.append(v[i])
    return ''.join(escaped)


@staticmethod
def positional_to_model(value):
    """Converts a positional argument to a model value"""
    return value


@classmethod
def get_collection_name(cls):
    """ Returns the collection name used by the class.  If the ``config_collection_name``
            attribute is set it is used, otherwise the name of the class is used."""
    if not hasattr(cls, 'config_collection_name'):
        return cls.__name__
    return cls.config_collection_name


def read_file(path, mode='r'):
    """Read the content of a file."""
    with open(path, mode) as f:
        return f.read()


@classmethod
def build(cls, pcr_map_filename):
    """
        Class method to build a :class:`.PCRasterMap` object
        :param pcr_map_filename: path to PCRaster map file
        :return: PCRasterMap object
        """
    return cls(pcr_map_filename)


def face_select_linked():
    """Select linked faces

    """
    pass


def find_mounted_volume_vm_id(mcs, volume_id):
    """
    Find the vm the volume is mounted to (or None if not mounted)
    The vm might not be running.
    :param mcs: MiniCloudStack instance with valid connection
    :param volume_id: id of volume
    :return: vm_id or 'None'
    """
    vm_id = None
    for v in mcs.list('volumes', id=volume_id):
        attached_vm = getattr(v, 'virtualmachineid', None)
        if attached_vm is not None:
            if hasattr(v, 'deviceid'):
                return attached_vm
    return vm_id


def is_client_auth_required(domain):
    """
    Domain requires client authentication if *any* HTTPS endpoint requires it for full TLS connection.
    """
    https, httpswww = domain.https, domain.httpswww
    return (https.https_client_auth_required or httpswww.
        https_client_auth_required)


def create_date_formats():
    """Generate time and date formats with different delimeters."""
    base_formats = ['%d %m %Y', '%d %m %y', '%Y %m %d']
    base_formats += ['%m %d %Y', '%m %d %y', '%Y %m %d']
    base_formats += ['%d %b %Y', '%d %B %Y']
    date_formats = []
    for separator in ('-', '.', '/', ' '):
        for date_format in base_formats:
            date_formats.append(date_format.replace(' ', separator))
    datetime_formats = []
    time_formats = '%H:%M%Z', '%H:%M:%S', '%H:%M:%S%Z', '%H:%M%z', '%H:%M:%S%z'
    for date_format in date_formats:
        for time_format in time_formats:
            for separator in ('', 'T', ' '):
                datetime_formats.append(date_format + separator + time_format)
    return tuple(date_formats), tuple(datetime_formats)


def aggregate(itvs):
    """Aggregate *NOT overlapping* intervals (intersect must be empty) to
    remove gaps.

    >>> aggregate([])
    []
    >>> aggregate([(1, 2), (3, 4)])
    [(1, 4)]
    >>> aggregate([(3, 4), (1, 2)])
    [(1, 4)]
    """
    lg = len(itvs)
    if lg <= 1:
        return itvs
    itvs = sorted(itvs)
    res = []
    i = 1
    a, b = itvs[0]
    while True:
        if i == lg:
            res.append((a, b))
            return res
        else:
            x, y = itvs[i]
            if x == b + 1:
                b = y
            else:
                res.append((a, b))
                a, b = x, y
            i += 1


def get_doc():
    """Get documentation of this resource

    :return: rst string
    :rtype: str
    """
    return """
    The ``usergroup`` model is used to group several users.

    """


def p_for_expr(p):
    """for_expr : empty
                | non_empty_for_expr"""
    p[0] = p[1]


def _is(node):
    """Shorthand for defining methods in PythonASTWriter (see code below)"""
    return lambda self, x: node


def V_to_rho(V, MW):
    """
    Return the density (rho) in kg/m^3 given the molar volume (V) in
    m^3/mol and molecular weight (MW) in g/mol.

    .. math::
        \\rho = \\frac{MW}{1000\\cdot V}

    Parameters
    ----------
    V : float
        Molar volume, [m^3/mol]
    MW : float
        Molecular weight, [g/mol]

    Returns
    -------
    rho : float
        Density, [kg/m^3]

    Examples
    --------
    >>> rho(0.000132, 86.18)
    652.8787878787879

    """
    return MW / V / 1000.0


def try_encode(string, enc):
    """turn unicode encoding into a functional routine"""
    try:
        return string.encode(enc)
    except UnicodeEncodeError:
        return None


@classmethod
def updateAll(Implementation, critic, query, cached_objects):
    """Execute the query and update all cached objects

           The query must take a single parameter, which is a list of object
           ids. It will be executed with the list of ids of all cached
           objects. Each returned row must have the id of the object as the
           first item, and the implementation constructor must take the row
           as a whole as arguments:

             new_impl = Implementation(*row)"""
    cursor = critic.getDatabaseCursor()
    cursor.execute(query, (list(cached_objects.keys()),))
    for row in cursor:
        cached_objects[row[0]]._set_impl(Implementation(*row))


def swap_kv(kv):
    """Swap a key-value tuple."""
    return kv[1], kv[0]


def obj_make_list(context, list_obj, item_cls, db_list, **extra_args):
    """Construct an object list from a list of primitives.

    This calls item_cls._from_db_object() on each item of db_list, and
    adds the resulting object to list_obj.

    :param:context: Request context
    :param:list_obj: An ObjectListBase object
    :param:item_cls: The VersionedObject class of the objects within the list
    :param:db_list: The list of primitives to convert to objects
    :param:extra_args: Extra arguments to pass to _from_db_object()
    :returns: list_obj
    """
    list_obj.objects = []
    for db_item in db_list:
        item = item_cls._from_db_object(context, item_cls(), db_item, **
            extra_args)
        list_obj.objects.append(item)
    list_obj._context = context
    list_obj.obj_reset_changes()
    return list_obj


def _ensure_requested_network_ordering(accessor, unordered, preferred):
    """Sort a list with respect to the preferred network ordering."""
    if preferred:
        unordered.sort(key=lambda i: preferred.index(accessor(i)))


@classmethod
def get_contact_type(cls, contact_id):
    """
        Get the type ID for the contactID

        Just spoofs it at the moment, not actually the correct race ID
        :return: contactType
        """
    return cls.contactTypes[0]


def p_global_param_no_val(p):
    """ global_param_no_val : AUTHORITATIVE
                            | NOT_AUTHORITATIVE
    """
    p[0] = p[1]


def convert_sigmav(sv, target):
    """Changes the units of <sigma v>.

    Parameters
    ----------
    sv : float
        Cross section in units of MeV^-2 or cm^3 / s.
    target : string
        Units to convert to. Must be "MeV^-2" or "cm^3/s" -- whichever units sv
        is NOT in.

    Returns
    -------
    sv : float
        sv converted to be in the target units.
    """
    hbar2_c3 = 30000000000.0 ** 3 * 6.58e-22 ** 2
    if target == 'cm^3 / s':
        return sv * hbar2_c3
    elif target == 'MeV^-2':
        return sv / hbar2_c3


def rgb2hsv(red, green, blue):
    """Transform a RGB color to a HSV color."""
    cmax = max(red, green, blue)
    cmin = min(red, green, blue)
    delta = cmax - cmin
    if delta == 0:
        hue = 0
    elif cmax == red:
        hue = 60 * ((green - blue) / delta % 6)
    elif cmax == green:
        hue = 60 * ((blue - red) / delta + 2)
    elif cmax == blue:
        hue = 60 * ((red - green) / delta + 4)
    saturation = 0 if delta == 0 else delta / cmax
    return hue, saturation, cmax


def test_stereo(azimuths, takeoffs, polarities, sdr=[]):
    """
	Plots points with given azimuths, takeoff angles, and
	polarities on a stereonet. Will also plot both planes
	of a double-couple given a strike/dip/rake
    """
    import matplotlib.pyplot as plt
    import mplstereonet
    from obspy.imaging.beachball import aux_plane
    print('azimuths:')
    print(azimuths)
    print('takeoffs:')
    print(takeoffs)
    print('polarities:')
    print(polarities)
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='stereonet')
    up = polarities > 0
    dn = polarities < 0
    h_rk = ax.rake(azimuths[up] - 90.0 + 180.0, 90.0 - takeoffs[up], 90, 'ro')
    h_rk = ax.rake(azimuths[dn] - 90.0 + 180.0, 90.0 - takeoffs[dn], 90, 'b+')
    print('HERE we ARE')
    if sdr:
        s2, d2, r2 = aux_plane(*sdr)
        h_rk = ax.plane(sdr[0], sdr[1], 'g')
        h_rk = ax.rake(sdr[0], sdr[1], -sdr[2], 'go')
        h_rk = ax.plane(s2, d2, 'g')
        plt.show()


def col(red, green, blue):
    """Convert the given colours [0, 255] to HTML hex colours."""
    return '#%02x%02x%02x' % (red, green, blue)


def replace_cr_with_newline(message: str):
    """
    TQDM and requests use carriage returns to get the training line to update for each batch
    without adding more lines to the terminal output.  Displaying those in a file won't work
    correctly, so we'll just make sure that each batch shows up on its one line.
    :param message: the message to permute
    :return: the message with carriage returns replaced with newlines
    """
    if '\r' in message:
        message = message.replace('\r', '')
        if not message or message[-1] != '\n':
            message += '\n'
    return message


@classmethod
def fromJSON(cls, data):
    """
        Create a Meme instance from a JSON API response.

        Args:
            data (dict): A JSON object as returned by the get_memes endpoint
        Returns:
            The newly-created Meme object
        Raises:
            RuntimeError: if the JSON object cannot be properly parsed
        """
    try:
        return cls(**data)
    except AttributeError:
        raise RuntimeError(
            'JSON cannot be properly parsed into Meme object: ' + str(data))


def read_perm_file(filename):
    """
    Reads a permutation
    :param filename: Name of perm file
    :return: permutation as a list of numbers

    Check that the permutation is legal, raise ValueError if not
    """
    with open(filename, 'r') as f:
        line = f.read().split()
        used = set()
        result = []
        for num_str in line:
            try:
                num = int(num_str)
            except ValueError:
                raise ValueError(
                    'Invalid permutation %s - only numbers are allowed' % line)
            if num < 1 or num > len(line):
                raise ValueError('Invalid permutation %s - out of range' % line
                    )
            num -= 1
            if num in used:
                raise ValueError(
                    'Invalid permutation %s - number appears twice' % line)
            result.append(num)
            used.add(num)
    return result


def set_epsilon(e):
    """Sets the value of the fuzz factor used in numeric expressions.

    # Arguments
        e: float. New value of epsilon.

    # Example
    ```python
        >>> from keras import backend as K
        >>> K.epsilon()
        1e-07
        >>> K.set_epsilon(1e-05)
        >>> K.epsilon()
        1e-05
    ```
    """
    global _EPSILON
    _EPSILON = e


def calc_tick_coords(u, f, g, dx_unit, dy_unit, distance):
    """
    helper func to calculate tick start and stop coords
    """
    x1, y1 = f(u), g(u)
    x2 = x1 + distance * dy_unit
    y2 = y1 - distance * dx_unit
    return x1, y1, x2, y2


def _par_indices(names):
    """
    Given a list of objects, returns a mapping of objects in that list to the
    index or indices at which that object was found in the list.
    """
    unique = {}
    for idx, name in enumerate(names):
        name = name.upper()
        if name in unique:
            unique[name].append(idx)
        else:
            unique[name] = [idx]
    return unique


def is_iterable(obj):
    """Determine if an object is iterable.

    :param any obj: The object to test
    :returns bool: Whether it is iterable or not

    """
    try:
        for _ in obj:
            break
    except TypeError:
        return False
    else:
        return True


def to_int(data, byteorder='big'):
    """Convert bytes to integer"""
    if isinstance(data, bytes):
        return int.from_bytes(data, byteorder)


@classmethod
def run_hooks(cls, hook_type, *args, **kwargs):
    """Run all hooks of specified type.

        :param cls: class that registers hooks
        :param hook_type: hook type, e.g., '__pre_parse_args__'
        :param args: args to be passed to every hook function
        :param kwargs: kwargs to be passed to every hook function
        """
    hook_funcs = cls._hooks_map.get(hook_type) or []
    for hook_func in hook_funcs:
        hook_func(*args, **kwargs)


@classmethod
def from_named_style(cls, style):
    """
        Convert openpyxl.styles.NamedStyle to equivalent Style.

        :param style: openpyxl.styles.NamedStyle
        :return: Style
        """
    return cls(style)


def subscribe(request):
    """
        Called when a subscribe request has been received.

        @param request: The publish-subscribe request.
        @type request: L{wokkel.pubsub.PubSubRequest}
        @return: A deferred that fires with a
                 L{Subscription<wokkel.pubsub.Subscription>}.
        @rtype: L{Deferred<twisted.internet.defer.Deferred>}
        """


def untag_log_group(logGroupName=None, tags=None):
    """
    Removes the specified tags from the specified log group.
    To list the tags for a log group, use  ListTagsLogGroup . To add tags, use  UntagLogGroup .
    See also: AWS API Documentation

    :example: response = client.untag_log_group(
        logGroupName='string',
        tags=[
            'string',
        ]
    )

    :type logGroupName: string
    :param logGroupName: [REQUIRED]
            The name of the log group.

    :type tags: list
    :param tags: [REQUIRED]
            The tag keys. The corresponding tags are removed from the log group.
            (string) --

    """
    pass


def delete_episode(db, aid, episode):
    """Delete an episode."""
    db.cursor().execute(
        'DELETE FROM episode WHERE aid=:aid AND type=:type AND number=:number',
        {'aid': aid, 'type': episode.type, 'number': episode.number})


def parse_http_list(s):
    """Parse lists as described by RFC 2068 Section 2.

    In particular, parse comma-separated lists where the elements of
    the list may include quoted-strings.  A quoted-string could
    contain a comma.  A non-quoted string could have quotes in the
    middle.  Neither commas nor quotes count if they are escaped.
    Only double-quotes count, not single-quotes.
    """
    res = []
    part = ''
    escape = quote = False
    for cur in s:
        if escape:
            part += cur
            escape = False
            continue
        if quote:
            if cur == '\\':
                escape = True
                continue
            elif cur == '"':
                quote = False
            part += cur
            continue
        if cur == ',':
            res.append(part)
            part = ''
            continue
        if cur == '"':
            quote = True
        part += cur
    if part:
        res.append(part)
    return [part.strip() for part in res]


def txt2lat(lattxt):
    """txt2lat: input txt: N52'14'13.5 or N52 or N52' """
    txt = lattxt.upper().replace('N', '').replace('S', '-')
    neg = txt.count('-') > 0
    if txt.count("'") > 0 or txt.count('"') > 0 or txt.count(chr(176)) > 0:
        txt = txt.replace('"', "'").replace(chr(176), "'")
        degs = txt.split("'")
        div = 1
        lat = 0
        if neg:
            f = -1.0
        else:
            f = 1.0
        for xtxt in degs:
            if len(xtxt) > 0:
                try:
                    lat = lat + f * abs(float(xtxt)) / float(div)
                    div = div * 60
                except:
                    print('txt2lat value error:', lattxt)
                    return 0.0
    else:
        lat = float(txt)
    return lat


def skip_comments_and_empty(f):
    """
    Skip comments and empty lines
    :param f: the tsv file handler
    :return: the first not empty line with no comments
    """
    line = f.readline()
    while line.startswith('#') or line == '\n':
        line = f.readline()
    return line


def reset_min_community_growth(com):
    """Reset the lower bound for the community growth."""
    com.variables.community_objective.lb = 0.0
    com.variables.community_objective.ub = None


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

        :param datetime.datetime val:
        :rtype: GetTeamEventsContinueError
        """
    return cls('reset', val)


def _get_op_submodule_name(op_name, op_name_prefix, submodule_name_list):
    """Get the submodule name of a specific op"""
    assert op_name.startswith(op_name_prefix)
    for submodule_name in submodule_name_list:
        if op_name[len(op_name_prefix):].startswith(submodule_name):
            return submodule_name
    return ''


def setfilename(imagefilename, imageindex, nbdigits=4, CCDLabel=None):
    """
    reconstruct filename string from imagefilename and update filename index with imageindex

    :param imagefilename: filename string (full path or not)
    :param imageindex: index in filename
    :type imageindex: string

    :return filename: input filename with index replaced by input imageindex
    :rtype: string
    """
    if imagefilename.endswith('mccd'):
        lenext = 5
        imagefilename = imagefilename[:-(lenext + nbdigits)
            ] + '{:04d}.mccd'.format(imageindex)
    elif CCDLabel in ('sCMOS', 'sCMOS_fliplr'):
        ext = 'tif'
        lenext = 4
        if imagefilename.endswith('tiff'):
            ext = 'tiff'
            lenext = 5
        if nbdigits is not None:
            if imagefilename.endswith(ext):
                imagefilename = imagefilename[:-(lenext + nbdigits)
                    ] + '{:04d}.{}'.format(imageindex, ext)
            elif imagefilename.endswith(ext + '.gz'):
                imagefilename = imagefilename[:-(lenext + 3 + nbdigits)
                    ] + '{:04d}.{}.gz'.format(imageindex, ext)
        elif imagefilename.endswith(ext):
            prefix, _ = imagefilename.split('.')
            prefix0 = prefix.split('_')[0]
            if imageindex > 9999:
                imagefilename = prefix0 + '_{}.{}'.format(imageindex, ext)
            else:
                imagefilename = prefix0 + '_{:04d}.{}'.format(imageindex, ext)
    elif CCDLabel in ('EIGER_4Mstack',):
        pass
    elif imagefilename.endswith('mar.tiff'):
        imagefilename = imagefilename[:-(9 + nbdigits)
            ] + '{:04d}_mar.tiff'.format(imageindex)
    elif imagefilename.endswith('mar.tif'):
        sname = imagefilename.split('_')
        if len(sname) >= 4:
            sname2 = sname[-2] + '_' + sname[-1]
            prefix = imagefilename.rstrip(sname2)
        else:
            prefix = imagefilename.split('_')[0]
        imagefilename = prefix + '_{}_mar.tif'.format(imageindex)
    elif imagefilename.endswith(('.tif', '.edf')):
        if CCDLabel in ('ImageStar_raw',):
            prefixwihtindex = imagefilename[:-4]
            prefixwithindex_list = list(prefixwihtindex)
            indexnodigit = 0
            while len(prefixwithindex_list) > 0:
                lastelem = prefixwithindex_list.pop(-1)
                print('lastelem', lastelem)
                if not lastelem.isdigit():
                    break
                indexnodigit += 1
            prefix = prefixwihtindex[:-indexnodigit]
            print('prefix', prefix)
            imagefilename = prefix + '{}.tif'.format(imageindex)
        else:
            suffix = imagefilename[-4:]
            prefix = imagefilename[:-(4 + nbdigits)]
            imagefilename = prefix + '{:04d}{}'.format(imageindex, suffix)
    elif imagefilename.endswith('mccd'):
        imagefilename = imagefilename[:-(5 + nbdigits)] + '{:04d}.mccd'.format(
            imageindex)
    elif imagefilename.endswith('edf'):
        imagefilename = imagefilename[:-(4 + nbdigits)] + '{:04d}.edf'.format(
            imageindex)
    elif imagefilename.endswith('unstacked'):
        imagefilename = imagefilename[:-(10 + nbdigits)
            ] + '{:04d}.unstacked'.format(imageindex)
    return imagefilename


def stack_template_key_name(blueprint):
    """Given a blueprint, produce an appropriate key name.

    Args:
        blueprint (:class:`stacker.blueprints.base.Blueprint`): The blueprint
            object to create the key from.

    Returns:
        string: Key name resulting from blueprint.
    """
    name = blueprint.name
    return 'stack_templates/%s/%s-%s.json' % (blueprint.context.get_fqn(
        name), name, blueprint.version)


def pick(dct, *keys):
    """ Returns a copy of the dictionary filtered to only have values for the
        whitelisted keys (or list of valid keys)

        >>> pick({"name": "moe", "age": 50, "userid": "moe1"}, "name", "age")
        {'age': 50, 'name': 'moe'}

    """
    copy = dict()
    for key in keys:
        if key in dct.keys():
            copy[key] = dct[key]
    return copy


def calc_force(df, close, volume, col_name='force'):
    """Calculate force indicator (Elder).

    :param df: Data frame to do and add calculations to
    :param close: Pandas dataframe column name close price
    :param volume: Pandas dataframe column name volume
    :param col_name: Pandas dataframe column name for new force calculations
    :type df: ``pandas.DataFrame()`` [``float``]
    :type close: ``str``
    :type volume: ``str``
    :type col_name: ``str``
    :returns: Input dataframe with added force column
    :rtype: ``pandas.DataFrame()`` [``float``]
    """
    df[col_name] = df[close].diff() * df[volume]
    return df


@classmethod
def get_base_path(cls, script_prefix, api_prefix):
    """Determine an appropriate value for ``basePath`` based on the SCRIPT_NAME and the api common prefix.

        :param str script_prefix: script prefix as defined by django ``get_script_prefix``
        :param str api_prefix: api common prefix
        :return: joined base path
        """
    if script_prefix and script_prefix.endswith('/'):
        script_prefix = script_prefix[:-1]
    if not api_prefix.startswith('/'):
        api_prefix = '/' + api_prefix
    base_path = script_prefix + api_prefix
    if base_path and base_path.endswith('/'):
        base_path = base_path[:-1]
    if not base_path.startswith('/'):
        base_path = '/' + base_path
    return base_path


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

    The ".dev0" means dirty.

    Exceptions:
    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 mean_arterial_pressure(diastolic_bp, systolic_bp):
    """
    The Karvonen Method for target heart rate (THR) - using a range of 50% to 85% intensity. The formula is used to calculate heart rate for exercise at a percentage training intensity.

    args:
        intensity (float): given as a decimal between 0 and 1
        rest (float): resting heart rate, given in beats/minute
        maximum (float): maximum heart rate, given in beats/minute

    Returns:
        float: heart rate for exercise at the given intensity, given in beats/minute
    """
    return (2 * diastolic_bp + systolic_bp) / 3


def tpc_finish(transaction, func=lambda tid: None):
    """Finish the transaction, making any transaction changes permanent.

        See IStorage.store. For objects implementing this interface,
        the return value must be the committed tid. It is used to set the
        serial for objects whose ids were passed to previous store calls
        in the same transaction.
        """


@staticmethod
def _set(s, k, v):
    """
        ``_set({"a":{"b":{"c":2}, "zz":"pp"}}, "a.b.m", "q")={'a': {'b': {'c': 2, 'm': 'q'}, 'zz': 'pp'}}``

        :param s: Dict.
        :param k: Any.
        :param v: Any.
        :return: Dict. after the set
        """
    ks = k.split('.')
    d = s
    for c, i in enumerate(ks):
        if not isinstance(d.get(i), dict):
            if c == len(ks) - 1:
                d[i] = v
                break
            else:
                return s
        d.setdefault(i, {})
        d = d[i]
    return s


def get_keywords():
    """Get the keywords needed to look up the version information."""
    git_refnames = '$Format:%d$'
    git_full = '$Format:%H$'
    git_date = '$Format:%ci$'
    keywords = {'refnames': git_refnames, 'full': git_full, 'date': git_date}
    return keywords


def safezip2(seq1, seq2):
    """A zip that raises an error when the sequences have different length.
    It can only handle two sequences.
    """
    if len(seq1) != len(seq2):
        raise ValueError('Lengths of iterators differ: {} != {}.'.format(
            len(seq1), len(seq2)))
    return zip(seq1, seq2)


def probe_response(R, psh):
    """ Calulates the probe response psh to the sh pattern tsh. R is the 4
    column matrix of translated probe coefficients. """
    pass


def get_member(email):
    """Get the member for the given address.

        *Note* that it is possible for an email to be subscribed to a
        mailing list twice, once through its explicit address and once
        indirectly through a user's preferred address.  In this case,
        this API always returns the explicit address.  Use
        ``get_memberships()`` to return them all.

        :param email: The email address to search for.
        :type email: string
        :return: The member if found, otherwise None
        :rtype: `IMember` or None
        """


def smells_like_url(path):
    """Determine if the given path seems like a URL.

    Currently, only things that start off http://
    https:// or ftp:// are treated as URLs.
    """
    return path.find('://') >= 0 and (path.startswith('http') or path.
        startswith('ftp'))


def maskbits(x: int, n: int) ->int:
    """Mask x & bitmask(n)"""
    if n >= 0:
        return x & (1 << n) - 1
    else:
        return x & -1 << -n


def _to_list(objs):
    """Makes a list of objs"""
    if objs is None:
        return []
    elif not isinstance(objs, list):
        return [objs]
    else:
        return objs


def update_time(p_dict, uuid):
    """Updates time by uuid"""
    updated_param = {'duration': p_dict['duration'] if 'duration' in p_dict
         else 18, 'user': p_dict['user'] if 'user' in p_dict else
        'example-user', 'activities': p_dict['activities'] if 'activities' in
        p_dict else ['qa'], 'project': p_dict['project'] if 'project' in
        p_dict else ['ganeti'], 'notes': p_dict['notes'] if 'notes' in
        p_dict else None, 'issue_uri': p_dict['issue_uri'] if 'issue_uri' in
        p_dict else 'https://github.com/osuosl/ganeti_webmgr/issues/56',
        'date_worked': p_dict['date_worked'] if 'date_worked' in p_dict else
        '2015-08-07', 'created_at': '2014-06-12', 'updated_at':
        '2015-10-18', 'deleted_at': None, 'uuid': uuid, 'revision': 2}
    return updated_param


def gather_basic_facts(device):
    """
    Using the provide Junos Device object, retrieve basic facts about the device.

    Parameters
    ----------
    device : Device
        The Junos device instance

    Returns
    -------
    dict
        A collection of basic facts about the device that will be stored in the database.
    """
    basic_facts = dict()
    basic_facts['os_version'] = device.facts['version']
    basic_facts['hostname'] = device.facts['hostname']
    basic_facts['device_sn'] = device.facts['serialnumber']
    basic_facts['device_model'] = device.facts['model']
    config = device.rpc.get_config(filter_xml='system/services/outbound-ssh')
    servers = config.xpath('.//servers/name')
    server_ipaddr = servers[0].text
    resp = device.rpc.get_route_information(destination=server_ipaddr)
    if_name = resp.xpath('.//via | .//nh-local-interface')[0].text
    basic_facts['mgmt_interface'] = if_name.partition('.')[0]
    if_info = device.rpc.get_interface_information(interface_name=if_name,
        terse=True)
    basic_facts['mgmt_ipaddr'] = if_info.findtext('.//ifa-local').partition('/'
        )[0].strip()
    resp = device.rpc.get_interface_information(interface_name=basic_facts[
        'mgmt_interface'], media=True)
    found = resp.findtext('.//current-physical-address').strip()
    basic_facts['mgmt_macaddr'] = found
    return basic_facts


def oprint(message, files):
    """Prints to a sequence of files."""
    for file in files:
        print(message, file=file)


def distance_to_end(file_obj):
    """
    For an open file object how far is it to the end

    Parameters
    ------------
    file_obj: open file-like object

    Returns
    ----------
    distance: int, bytes to end of file
    """
    position_current = file_obj.tell()
    file_obj.seek(0, 2)
    position_end = file_obj.tell()
    file_obj.seek(position_current)
    distance = position_end - position_current
    return distance


def clear():
    """Clear the queue.

        This method can return a deferred. """


@classmethod
def visit(cls, specification, callback):
    """Invokes ``callback`` directly on ``specification``, which should be a field
        representation, as well as on all field representations nested therein.

        :param dict specification: A field representation, possibly containing nested field
            representations, such as that returned by :func:`.describe()`.
        """
    implementation = cls.types[specification['fieldtype']]
    return implementation._visit_field(specification, callback)


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

        :param SharedContentRemoveLinkExpiryDetails val:
        :rtype: EventDetails
        """
    return cls('shared_content_remove_link_expiry_details', val)


def countElements(array):
    """
    Function purpose: return the occurancies of all values in the array

    Input: array

    Output: array list
    """
    list = []
    for x in array:
        if [x, array.count(x)] not in list:
            list.append([x, array.count(x)])
    return list


@staticmethod
def extract_observation_value(observation, value):
    """Returns observation or observation value."""
    obs_value = observation.get(value)
    if obs_value is None:
        return None
    if isinstance(observation[value], dict):
        obs_sub_value = observation[value].get('value')
        if obs_sub_value is None:
            return None
        return float(obs_sub_value)
    return observation[value]


def enum(*sequential, **named):
    """ Create an enum type, as in the C language.
        You can, as i C, list names without any value, or set a value by using a named argument.
        But, of course, you cannot add unnamed arguments after named arguments...

        :param sequential: list of strings
        :param named: dictionnary of <strings:int value>
        :return: A new type created by using the params.
        :rtype: Enum
    """
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)


def _parameters_exists_in_docstring(docstring):
    """
    Get boolean of whether Parater part exists in docstring
    or not.

    Parameters
    ----------
    docstring : str
        Docstring to be checked.

    Returns
    -------
    result_bool : bool
        If exists, True will be set.
    """
    is_in = """Parameters
    ---""" in docstring
    if not is_in:
        return False
    return True


def childConnectionLost(childFD):
    """
        Called when a file descriptor associated with the child process is
        closed.

        @type childFD: C{int}
        @param childFD: The file descriptor which was closed.
        """


@classmethod
def is_datasource_for(cls, data, header, **kwargs):
    """Determines if header corresponds to an EIT image"""
    return header.get('instrume') == 'EIT'


@classmethod
def type_list(cls, options=None):
    """List type of PaaS instances."""
    return cls.safe_call('paas.type.list', options)


@staticmethod
def _internal_error(error):
    """
        Creates the error message for an internal error.
        """
    url = 'https://github.com/storyscript/storyscript/issues'
    return 'Internal error occured: {error}\nPlease report at {url}'.format(
        error=str(error), url=url)


def output(zelf, txt, addr=None):
    """ output to all bots on fleet. """
    from midz import fleet
    try:
        passwd, text = txt.split(' ', 1)
    except:
        return
    text = text.replace('\x00', '')
    if passwd == zelf.cfg.password:
        for bot in fleet:
            bot.announce(text)


def is_comment(item):
    """Quick check whether an item is a comment (reply) to another post.
    The item can be a Post object or just a raw comment object from the
    blockchain.
    """
    return item['parent_author'] != ''


def provider_parser(subparser):
    """Return the parser for this provider"""
    subparser.description = """
        There are two ways to provide an authentication granting edition to the target CloudFlare DNS zone.
        1 - A Global API key,
            with --auth-username and --auth-token flags.
        2 - An unscoped API token (permissions Zone:Zone(read) + Zone:DNS(edit) for all zones),
            with --auth-token flag.
        3 - A scoped API token (permissions Zone:Zone(read) + Zone:DNS(edit) for one zone),
            with --auth-token and --zone-id flags.
    """
    subparser.add_argument('--auth-username', help=
        'specify email address for authentication (for Global API key only)')
    subparser.add_argument('--auth-token', help=
        'specify token for authentication (Global API key or API token)')
    subparser.add_argument('--zone-id', help=
        'specify the zone id (if set, API token can be scoped to the target zone)'
        )


def user():
    """ The user who issued the calculation or None
        """


def extract_todo(flag, text):
    """Extract an action item line into a dict structure.

    - owner: the owner
    - todo: the proper action item
    - deadline: when the action item needs to be executed
    """
    owner, action_line = text.split(' to ', 1)
    todo = action_line.strip()[:-10]
    todo_date = action_line.strip()[-10:]
    return {'owner': owner.strip(), 'todo': todo.strip(), 'deadline': todo_date
        }


@classmethod
def log_path(cls, project, log):
    """Returns a fully-qualified log resource name string."""
    return cls._LOG_PATH_TEMPLATE.render({'project': project, 'log': log})


def move_transport_entries(apps, schema_editor):
    """Move old transport entries to new model."""
    pf_Transport = apps.get_model('modoboa_postfix_autoreply', 'Transport')
    Transport = apps.get_model('transport', 'Transport')
    to_create = []
    for old_transport in pf_Transport.objects.all():
        to_create.append(Transport(pattern=old_transport.domain, service=
            'autoreply'))
    Transport.objects.bulk_create(to_create)


def zeros(h, w):
    """create a (h x w) matrix of zeros.

    Args:
        h: Height of the matrix.
        w: Width of the matrix.
    """
    return [[(0) for x in range(w)] for y in range(h)]


@staticmethod
def positional_to_model(value):
    """Converts a positional argument to a model value"""
    return value


def add_button(figure):
    """Adds the pause-play button for animation"""
    figure['layout']['updatemenus'] = [{'buttons': [{'args': [None, {
        'frame': {'duration': 500, 'redraw': False}, 'fromcurrent': True,
        'transition': {'duration': 300, 'easing': 'quadratic-in-out'}}],
        'label': 'Play', 'method': 'animate'}, {'args': [[None], {'frame':
        {'duration': 0, 'redraw': False}, 'mode': 'immediate', 'transition':
        {'duration': 0}}], 'label': 'Pause', 'method': 'animate'}],
        'direction': 'left', 'pad': {'r': 10, 't': 87}, 'showactive': False,
        'type': 'buttons', 'x': 0.1, 'xanchor': 'right', 'y': 0, 'yanchor':
        'top'}]


def quote(v):
    """
       >>> quote(u"Bjorn's")
       u'"Bjorn\\'s"'
       >>> quote(u'the "best"')
       u'"the "best""'
    """
    if u'"' in v:
        v = v.replace(u'"', u'"')
    return u'"%s"' % v


def to_networkx(g, node_attrs=None, edge_attrs=None):
    """Convert to networkx graph.

    The edge id will be saved as the 'id' edge attribute.

    Parameters
    ----------
    g : DGLGraph or DGLHeteroGraph
        For DGLHeteroGraphs, we currently only support the
        case of one node type and one edge type.
    node_attrs : iterable of str, optional
        The node attributes to be copied. (Default: None)
    edge_attrs : iterable of str, optional
        The edge attributes to be copied. (Default: None)

    Returns
    -------
    networkx.DiGraph
        The nx graph
    """
    return g.to_networkx(node_attrs, edge_attrs)


def interaction_features_query(table_name='%(table_name)s', c_coord={
    'latitude': 52.52437, 'longitude': 13.41053}, n_home_antennas=1):
    """
    Create interaction features which are part of the final feature set.
    For both event types (call, sms) three features are being created.
    X_dist_mean: The average distance in km of an event where the antenna
                 was part of. This feature tries to capture an indication
                 of reach per antenna.
    X_isolation: Simply the percentage of how many unique antennas an
                 antenna has had interactions with. This feature is bound
                 between 0 and 1.
    X_entropy: Entropy means the informative content of an antenna based
               on the interactions of all antennas. It's being calculated
               with the formula for the Shannon Entropy,
               -sum(probability*log(2, probability)), where probability is
               based on the percentage of events happening between each
               pair of antennas over all events.
    Additionally the feature dist2c tracks the distance of an antenna to
    the GPS coordinates of a capital as defined in the attributes class.

    Inputs
    ------
    table_name : name of the table the query is supposed to run on,
                 defaulting to '%(table_name)s', i.e. no substitution

    Output
    ------
    Partial final features dataframe which holds features
    calculated over interactions of antenna pairs.
    """
    query = """
        SELECT
          antenna_id1 as antenna_id,
          -- distance
          SUM(call_count*dist_km)/SUM(call_count) as calls_dist_mean,
          SUM(sms_count*dist_km)/SUM(sms_count) as sms_dist_mean,
          -- isolation
          COUNT(DISTINCT IF(NOT ISNULL(call_count), antenna_id2, NULL))/
           %(n_home_antennas)s as calls_isolation,
          COUNT(DISTINCT IF(NOT ISNULL(sms_count), antenna_id2, NULL))/
           %(n_home_antennas)s as sms_isolation,
          -- entropy
          -SUM(calls_proportion*LOG(2, calls_proportion)) as calls_entropy,
          -SUM(sms_proportion*LOG(2, sms_proportion)) as sms_entropy,
          dist2c
        FROM
        (
          SELECT
            antenna_id1,
            antenna_id2,
            sms_count,
            call_count,
            sms_count/all_sms as sms_proportion,
            call_count/all_calls as calls_proportion,
            -- haversine distance formula
            2 * asin(
                 sqrt(
                   cos(radians(lat1)) *
                   cos(radians(lat2)) *
                   pow(sin(radians((lon1 - lon2)/2)), 2)
                       +
                   pow(sin(radians((lat1 - lat2)/2)), 2)

                 )
               ) * 6371 dist_km,
             2 * asin(
                  sqrt(
                    cos(radians(lat1)) *
                    cos(radians(%(lat_c)s)) *
                    pow(sin(radians((lon1 - %(lon_c)s)/2)), 2)
                        +
                    pow(sin(radians((lat1 - %(lat_c)s)/2)), 2)

                  )
                ) * 6371 dist2c
          FROM
          (
            SELECT
              antenna_id1,
              al1.latitude as lat1,
              al1.longitude as lon1,
              antenna_id2,
              al2.latitude as lat2,
              al2.longitude as lon2,
              sms_count,
              call_count,
              SUM(sms_count) OVER() AS all_sms,
              SUM(call_count) OVER() AS all_calls
            FROM %(table_name)s ai
            JOIN table_antennas_locations al1
            ON ai.antenna_id1 = al1.antenna_id
            JOIN table_antennas_locations al2
            ON ai.antenna_id2 = al2.antenna_id
          )
        )
        GROUP BY antenna_id, dist2c
    """
    return query % {'table_name': table_name, 'lat_c': c_coord['latitude'],
        'lon_c': c_coord['longitude'], 'n_home_antennas': n_home_antennas}


def budget_line_to_account(line):
    """
    Convert a budget line to an account name.

    :param str line: The line to convert.
    :returns: The account.
    """
    if line[0] == '/':
        line = line[1:]
    line = line.replace('/', ':')
    return 'Expenses:{0}'.format(line)


@classmethod
def _substitute_html_entity(cls, matchobj):
    """Used with a regular expression to substitute the
        appropriate HTML entity for a special character."""
    entity = cls.CHARACTER_TO_HTML_ENTITY.get(matchobj.group(0))
    return '&%s;' % entity


def dump_add_vspacing(buff, vspacing):
    """Post-processing to add some nice-ish spacing for deeper map/list levels."""
    if isinstance(vspacing, int):
        vspacing = ['\n'] * (vspacing + 1)
    buff.seek(0)
    result = list()
    for line in buff:
        level = 0
        line = line.decode('utf-8')
        result.append(line)
        if ':' in line:
            while line.startswith('  '):
                level, line = level + 1, line[2:]
            if len(vspacing) > level and len(result) != 1:
                vspace = vspacing[level]
                vspace = vspace if not isinstance(vspace, int
                    ) else '\n' * vspace
                result.insert(-1, vspace)
    buff.seek(0), buff.truncate()
    buff.write(''.join(result).encode('utf-8'))


def extra_edges(x, y):
    """
	Counts edges that are found in the learned structure but are
	not present in the original structure. (lower is better)
	"""
    extra = 0
    for rv in y.nodes():
        if not x.has_node(rv):
            extra += y.degree(rv)
        else:
            for child in y.children(rv):
                if child not in x.children(rv):
                    extra += 1
    return extra


def is_contained_in(child, parent):
    """
        Return True if child is a child node of the parent.

        Node labels are hierarchical paths, with segments separated
        by '-'. As an edge case, a node label is also a child of itself.
    """
    child_segments = child.split('-')
    parent_segments = parent.split('-')
    return child_segments[:len(parent_segments)] == parent_segments


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


def get_columns(values, cols):
    """Returns number of columns to display."""
    columns = []
    col_size = len(values) // cols
    extra = len(values) % cols
    n = 0
    for i in range(cols):
        s = col_size
        if i + 1 <= extra:
            s += 1
        this_column = values[n:n + s]
        columns.append(this_column)
        n += s
    return columns


def tpfunc(size, duration):
    """
    Calculate the throughput from size and time.
    :param size:        object size
    :param duration:    the time the transfer took
    :return:            the throughput in Bytes/sec
    """
    duration = duration if duration > 0.0001 else 0.0001
    ret = size / duration * 1000
    return ret


def Perp2(a, b):
    """Return a sort of 2d cross product."""
    return a[0] * b[1] - a[1] * b[0]


def require(condition, msg='Requirement failed'):
    """
    Helper to raise ValueError exception if the given condition is not satisfied

    :param condition: a boolean expression to check
    :param msg: message of the exception
    :return: nothing
    """
    if not condition:
        raise ValueError(msg)


def tolin(variable):
    """
    Turn variable into the linear domain.
    Args:
        variable (float): array of elements to be transformed.
    Returns:
        float:array of elements transformed
    """
    return 10 ** (variable / 10)


@staticmethod
def _run_cli_process(cmd, shell=True):
    """! Runs command as a process and return stdout, stderr and ret code
        @param cmd Command to execute
        @return Tuple of (stdout, stderr, returncode)
        """
    from subprocess import Popen, PIPE
    p = Popen(cmd, shell=shell, stdout=PIPE, stderr=PIPE)
    _stdout, _stderr = p.communicate()
    return _stdout, _stderr, p.returncode


def next_key(tuple_of_tuples, key):
    """Processes a tuple of 2-element tuples and returns the key which comes
    after the given key.
    """
    for i, t in enumerate(tuple_of_tuples):
        if t[0] == key:
            try:
                return tuple_of_tuples[i + 1][0]
            except IndexError:
                return None


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 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 type_object_attrgetter(obj, attr, *defargs):
    """
    This implements an improved attrgetter for type objects (i.e. classes)
    that can handle class attributes that are implemented as properties on
    a metaclass.

    Normally `getattr` on a class with a `property` (say, "foo"), would return
    the `property` object itself.  However, if the class has a metaclass which
    *also* defines a `property` named "foo", ``getattr(cls, 'foo')`` will find
    the "foo" property on the metaclass and resolve it.  For the purposes of
    autodoc we just want to document the "foo" property defined on the class,
    not on the metaclass.

    For example::

        >>> class Meta(type):
        ...     @property
        ...     def foo(cls):
        ...         return 'foo'
        ...
        >>> class MyClass(metaclass=Meta):
        ...     @property
        ...     def foo(self):
        ...         ""\"Docstring for MyClass.foo property.""\"
        ...         return 'myfoo'
        ...
        >>> getattr(MyClass, 'foo')
        'foo'
        >>> type_object_attrgetter(MyClass, 'foo')
        <property at 0x...>
        >>> type_object_attrgetter(MyClass, 'foo').__doc__
        'Docstring for MyClass.foo property.'

    The last line of the example shows the desired behavior for the purposes
    of autodoc.
    """
    for base in obj.__mro__:
        if attr in base.__dict__:
            if isinstance(base.__dict__[attr], property):
                return base.__dict__[attr]
            break
    return getattr(obj, attr, *defargs)


def user_details(strategy, details, backend, user=None, *args, **kwargs):
    """Update user details using data from provider."""
    if not user:
        return
    changed = False
    if strategy.setting('NO_DEFAULT_PROTECTED_USER_FIELDS') is True:
        protected = ()
    else:
        protected = ('username', 'id', 'pk', 'email', 'password',
            'is_active', 'is_staff', 'is_superuser')
    protected = protected + tuple(strategy.setting('PROTECTED_USER_FIELDS', [])
        )
    field_mapping = strategy.setting('USER_FIELD_MAPPING', {}, backend)
    for name, value in details.items():
        name = field_mapping.get(name, name)
        if value is None or not hasattr(user, name) or name in protected:
            continue
        current_value = getattr(user, name, None)
        if current_value == value:
            continue
        changed = True
        setattr(user, name, value)
    if changed:
        strategy.storage.user.changed(user)


def height_gpemgh(GPE, mass, gravity):
    """Usage: Find height from gravitational potential energy, mass and gravity"""
    result = GPE / mass * gravity
    return result


def federate(*stores):
    """
    Create a session spec for connecting to a federated store.

    :param stores: List of session specs to federate.
    :type stores: tuple[string]
    :return: A session spec string.
    :rtype: string
    """
    return ' + '.join(stores)


def isSingleMenuItem():
    """Return whether there is single menu item or not."""


@classmethod
def abulk(cls, x):
    """
        calculate the bulk (relaxed) lattice parameter of the Ga_{1-x}In_{x}As
        alloy
        """
    return cls.aGaAs + 0.40505 * x


def prepare_for_fitting(voxels):
    """Return a copy of voxels, prepared for fitting."""
    voxels = voxels.copy()
    for v in voxels:
        if v[0] == 0:
            v[:] = 1
    return voxels


def _get_ipython_color_scheme(app):
    """
    Get the configured IPython color scheme.

    :type app:
      `TerminalIPythonApp`
    :param app:
      An initialized IPython terminal application.
    :rtype:
      ``str``
    """
    try:
        return app.shell.colors
    except AttributeError:
        pass
    try:
        import IPython
        ipapi = IPython.ipapi.get()
        return ipapi.options.colors
    except AttributeError:
        pass
    import IPython
    raise RuntimeError(
        "Couldn't get IPython colors.  Is your IPython version too old (or too new)?  IPython.__version__=%r"
         % IPython.__version__)


@staticmethod
def run(name, metadata):
    """Get options for name regard with metadata."""
    return None


def big_dag_append(dag, big_dag=None, merge_features=True):
    """Calculate the *minimal DAG* or *BigDAG*.

    See :cite:`Martino2006` and notated in :cite:`Martino2012ATK`.

    Parameters
    ----------
    dag : tuple
        A tuple representing a single dag containing:
            + A dictionary from vertices to tuples containing the subtree size,
              frequencies and node ID
            + A dictionary between hashes and vertices
              (representing a valid hash map)
            + A list of ordered vertices, based on inverse topological ordering
            + A dictionary corresponding to edges from vertex to a list of
              adjacent vertices
            + A dictionary of labels

    big_dag : tuple, default=None
        The dag on which the dag will be appended.
        If None: builds it from dag, else the tuple is the same as the format
        of this function output.

    merge_features : bool, default=True
        If True increments frequencies when a same element is found, else keeps
        them as vectors.

    Returns
    -------
    big_dag : tuple
        A tuple representing Big_Dag:
            + A dictionary from vertices to tuples containing the subtree size,
              frequencies and node ID
            + A dictionary between hashes and vertices
              (representing a valid hash map)
            + A dictionary corresponding to edges from vertex to a list of
              adjacent vertices.
            + A dictionary of labels.

    """
    if big_dag is None:
        D_labels = dict()
        D_hash_map = dict()
        D_vertices = dict()
        D_edges = dict()
        nodes_idx = 0
        nf = 1
    else:
        D_vertices, D_hash_map, D_edges, D_labels = big_dag
        nodes_idx = len(D_vertices.keys())
        if not merge_features:
            f = True
            for v in D_vertices.keys():
                D_vertices[v][1].append(0)
                if f:
                    nf = len(D_vertices[v][1])
                    f = False
            if f:
                nf = 1
    vertices, vertices_hash_map, v_ordered, edges, labels = dag
    for q in v_ordered:
        key = vertices[q][2]
        if key in D_hash_map:
            node = D_hash_map[key][0]
            if merge_features:
                D_vertices[node][1] += vertices[q][1]
            else:
                D_vertices[node][1][-1] += vertices[q][1]
        else:
            D_labels[nodes_idx] = labels[q]
            d_edges = list()
            v_nodes = set()
            for c in edges[q]:
                ck = vertices[c][2]
                if ck in D_hash_map:
                    node = D_hash_map[ck][0]
                    if node not in v_nodes:
                        d_edges.append(node)
                        v_nodes.add(node)
            D_edges[nodes_idx] = d_edges
            D_hash_map[key] = [nodes_idx]
            if merge_features:
                freq = vertices[q][1]
            else:
                freq = (nf - 1) * [0] + [vertices[q][1]]
            D_vertices[nodes_idx] = [vertices[q][1], freq, key]
            nodes_idx += 1
    return D_vertices, D_hash_map, D_edges, D_labels


def resample_reindex(tseries, new_index, freq='d', how='mean', fill_value=None
    ):
    """
    Performs a pandas :func:`resample` and :func:`reindex` at the same time.

    This function is syntactic sugar for a very common resampling techinque
    where it's  necessary to force the index to conform to a specific date
    range (e.g. a date range for a particular month) right after resampling.

    It is functionally equivalent to::

        tseries.resample(freq, how).reindex(new_index)
    """
    tseries = tseries.resample(freq, how=how).reindex(new_index)
    if fill_value is not None:
        tseries = tseries.fillna(fill_value)
    return tseries


def generate_canonical_request(method, parsed_url, headers, signed_headers,
    content_sha256):
    """
    Generate canonical request.

    :param method: HTTP method.
    :param parsed_url: Parsed url is input from :func:`urlsplit`
    :param headers: HTTP header dictionary.
    :param content_sha256: Content sha256 hexdigest string.
    """
    lines = [method, parsed_url.path, parsed_url.query]
    header_lines = []
    for header in signed_headers:
        value = headers[header.title()]
        value = str(value).strip()
        header_lines.append(header + ':' + str(value))
    lines = lines + header_lines
    lines.append('')
    lines.append(';'.join(signed_headers))
    lines.append(content_sha256)
    return '\n'.join(lines)


def user_wants_upload():
    """
    Determines whether or not the user wants to upload the extension
    :return: boolean
    """
    choice = input('Do you want to upload your extension right now? :')
    if 'y' in choice or 'Y' in choice:
        return True
    else:
        return False


def get_doc():
    """Get documentation of this resource

    :return: rst string
    :rtype: str
    """
    return """
    The ``serviceescalation`` model is used to define escalated notifications for the hosts.

    See the Alignak documentation regarding the escalations to discover all the features
    and the possibilities behind this Alignak feature.
    """


def KK_RC4_fit(params, w, t_values):
    """
    Kramers-Kronig Function: -RC-

    Kristian B. Knudsen (kknu@berkeley.edu / kristianbknudsen@gmail.com)
    """
    Rs = params['Rs']
    R1 = params['R1']
    R2 = params['R2']
    R3 = params['R3']
    R4 = params['R4']
    return Rs + R1 / (1 + w * 1.0j * t_values[0]) + R2 / (1 + w * 1.0j *
        t_values[1]) + R3 / (1 + w * 1.0j * t_values[2]) + R4 / (1 + w *
        1.0j * t_values[3])


def register_handler(handler):
    """
    Install application-specific HDF5 image handler.

    :param handler: Handler object.
    """
    global _handler
    _handler = handler


@classmethod
def parse_description(cls, fallback_msg='No description'):
    """Action handler description.

        Returns:
            str: First line from docstring or fallback message.
        """
    try:
        return cls.__doc__.splitlines()[0]
    except IndexError:
        return fallback_msg


@staticmethod
def isabs(name):
    """thin wrapper os.path.isabs()"""
    raise NotImplementedError()


@classmethod
def resolve_connection(cls, connection=None):
    """
        Resolve a connection instance.

        :param connection: The connection name
        :type connection: str

        :rtype: eloquent.connections.Connection
        """
    return cls.__resolver.connection(connection)


def is_suspicious(pe):
    """
    unusual locations of import tables
    non recognized section names
    presence of long ASCII strings
    """
    relocations_overlap_entry_point = False
    sequential_relocs = 0
    if hasattr(pe, 'DIRECTORY_ENTRY_BASERELOC'):
        for base_reloc in pe.DIRECTORY_ENTRY_BASERELOC:
            last_reloc_rva = None
            for reloc in base_reloc.entries:
                if (reloc.rva <= pe.OPTIONAL_HEADER.AddressOfEntryPoint <=
                    reloc.rva + 4):
                    relocations_overlap_entry_point = True
                if (last_reloc_rva is not None and last_reloc_rva <= reloc.
                    rva <= last_reloc_rva + 4):
                    sequential_relocs += 1
                last_reloc_rva = reloc.rva
    warnings_while_parsing = False
    warnings = pe.get_warnings()
    if warnings:
        warnings_while_parsing
    pass


def is_string(gxtype):
    """
    Return length of a gxtype string, 0 (False) if not a string.
    Note that this is the number of available bytes in UTF-8 encoding and not
    equivalent to number of Unicode characters
    """
    if gxtype < 0:
        return -gxtype
    else:
        return False


def _stringify_na_values(na_values):
    """ return a stringified and numeric for these values """
    result = []
    for x in na_values:
        result.append(str(x))
        result.append(x)
        try:
            v = float(x)
            if v == int(v):
                v = int(v)
                result.append(f'{v}.0')
                result.append(str(v))
            result.append(v)
        except (TypeError, ValueError, OverflowError):
            pass
        try:
            result.append(int(x))
        except (TypeError, ValueError, OverflowError):
            pass
    return set(result)


def _event_desc(event):
    """For debugging: user friendly representation of the event bitmask returned by poll()."""
    desc = ''
    first = True
    for t in ('POLLIN', 'POLLPRI', 'POLLOUT', 'POLLERR', 'POLLHUP', 'POLLNVAL'
        ):
        if event & globals()[t]:
            if not first:
                desc += '|'
            desc += t
            first = False
    return desc


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 erbs2f(erbs):
    """Converts erb numbers to frequency values

        converts a vector of ERB-rate values
        to the corresponding frequencies in Hz.

        Note that erb values must not exceed 42.79

        Parameters
        ----------
        erb : scalar or array
            Equivalent rectangular bandwidth (ERB) values.

        Returns
        -------
        f : scalar or array
            Frequencies, in Hz.
    """
    return 10 ** (erbs / 21.4 + 2.36) - 229


@staticmethod
def _s2d(css):
    """Parse list of colon-separated address and port to dictionary.

        :param css: list of colon-separated address and port
        :returns: dictionary
        """
    result = []
    for key_val in css:
        key, val = key_val.split(':')
        result.append({'address': key, 'port': int(val)})
    return result


@classmethod
def _mel2hz(cls, mel):
    """
        Convert the given Mel value to Hz frequency.

        :param float mel: the Mel value to convert
        :rtype: float
        """
    return 700.0 * (10 ** (mel / cls.MEL_10) - 1)


@staticmethod
def metadata_updated_on(item):
    """Extracts the update field from a MediaWiki item.

        The timestamp is extracted from 'update' field.
        This date is a UNIX timestamp but needs to be converted to
        a float value.

        :param item: item generated by the backend

        :returns: a UNIX timestamp
        """
    return float(item['update'])


def _sign(translator, expr):
    """Workaround for missing sign function"""
    op = expr.op()
    arg, = op.args
    arg_ = translator.translate(arg)
    return 'intDivOrZero({0}, abs({0}))'.format(arg_)


def run_script():
    """Run active script

    """
    pass


def read_mm_stamp(fh, byteorder, dtype, count, offsetsize):
    """Read FluoView mm_stamp tag from file and return as numpy.ndarray."""
    return fh.read_array(byteorder + 'f8', 8)


def setNibbles(hiNibble, loNibble):
    """
    Returns byte with value set according to hi and lo bits
    Asserts hiNibble and loNibble in range(16)
    >>> setNibbles(8, 14)
    142

    >>> setNibbles(8, 16)
    Traceback (most recent call last):
        ...
    ValueError: Nible value out of range 0-15: (8, 16)
    """
    if not 0 <= hiNibble <= 15 or not 0 <= loNibble <= 15:
        raise ValueError('Nible value out of range 0-15: (%s, %s)' % (
            hiNibble, loNibble))
    return (hiNibble << 4) + loNibble


def rekey(keys_number: int=2):
    """Change the number of keys of selected particles (root and tip keys included)

    :param keys_number: Number of Keys
    :type keys_number: int
    """
    pass


@staticmethod
def _handle_meanings(s):
    """ Handle additional word meanings in the settings section """
    assert False


def get_parameter(f):
    """
    Reads a procpar parameter from a file object.

    Returns a dictionary with the attributes of the parameter.

    """
    dic = dict()
    line = f.readline().decode().split()
    dic['name'] = line[0]
    dic['subtype'] = line[1]
    dic['basictype'] = line[2]
    dic['maxvalue'] = line[3]
    dic['minvalue'] = line[4]
    dic['stepsize'] = line[5]
    dic['Ggroup'] = line[6]
    dic['Dgroup'] = line[7]
    dic['protection'] = line[8]
    dic['active'] = line[9]
    dic['intptr'] = line[10]
    line = f.readline().decode()
    num = int(line.split()[0])
    values = []
    if dic['basictype'] == '1':
        values = line.split()[1:]
    elif dic['basictype'] == '2':
        values.append(line.split('"')[1])
        for i in range(num - 1):
            values.append(f.readline().decode().split('"')[1])
    dic['values'] = values
    line = f.readline().decode()
    dic['enumerable'] = line.split()[0]
    if dic['enumerable'] != '0':
        if dic['basictype'] == '1':
            dic['enumerables'] = line.split()[1:]
        elif dic['basictype'] == '2':
            dic['enumerables'] = line.split('"')[1::2]
    return dic


def in_app(alert, display_type, expiry=None, display=None, actions=None,
    interactive=None, extra=None):
    """In-App push message payload creation.

    :param alert: Required, string
    :param display_type: Required, String. List of valid display_type_
        attributes.
    :keyword expiry: Optional, String specifying an expiry value.
    :keyword display: Optional, A dictionary specifying the display_
        appearance of the in-app message.
    :keyword actions: Optional, Used to perform a defined task.
    :keyword interactive: Optional, A dictionary with two attributes –
        "type" and "button_actions", as generated by interactive().
    :keyword extra: Optional, Object.

    .. _display: https://docs.urbanairship.com/api/ua.html#display-object
    .. _display_type: https://docs.urbanairship.com/api/ua.html#in-app-message

    """
    payload = {}
    payload['alert'] = alert
    payload['display_type'] = display_type
    if expiry is not None:
        payload['expiry'] = expiry
    if display is not None:
        payload['display'] = display
    if actions is not None:
        payload['actions'] = actions
    if interactive is not None:
        payload['interactive'] = interactive
    if extra is not None:
        extra['extra'] = extra
    return payload


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 ascii_encode(non_compatible_string):
    """Primarily used for ensuring terminal display compatibility"""
    if non_compatible_string:
        return non_compatible_string.encode('ascii', errors='ignore').decode(
            'ascii')
    else:
        return ''


def _get_mro(cls):
    """Get an mro for a type or classic class"""
    if not isinstance(cls, type):

        class cls(cls, object):
            pass
        return cls.__mro__[1:]
    return cls.__mro__


def get_byte_size_of_string(string: str) ->int:
    """
    Return the number of bytes of a string
    """
    return len(string.encode('utf-8'))


@classmethod
def _activation_url(cls, watch):
    """Return a URL pointing to a view which :meth:`activates
        <tidings.models.Watch.activate()>` a watch.

        TODO: provide generic implementation of this before liberating.
        Generic implementation could involve a setting to the default
        ``reverse()`` path, e.g. ``'tidings.activate_watch'``.

        """
    raise NotImplementedError


def set_quiet(is_quiet):
    """Sets whether the messaging system is running in quiet mode. Quiet mode
    only prints messages with explicit verbosity specified. If verbosity==None
    and quiet==True, any message with level >= 0 is printed.
    """
    global quiet
    quiet = is_quiet


def getall(key):
    """ Return a list of all values matching the key (may be an empty
        list) """


def search(query, operator='and'):
    """Execute a search given by *query*.

        *query* can be a (unicode) string or an iterable of (unicode) strings.
        *operator* can be either ``'and'`` or ``'or'`` to search for documents
        containing all keywords or any keyword.

        Return an IFSet of docids
        """


def get_selected_item():
    """
        Fetches the currently selected item

        :return: the selected item
        """


def is_single_bool(val):
    """
    Checks whether a variable is a boolean.

    Parameters
    ----------
    val
        The variable to check.

    Returns
    -------
    bool
        True if the variable is a boolean. Otherwise False.

    """
    return type(val) == type(True)


def stems(x, y, ax=None, label=None, **kwargs):
    """
    A faster replacement for stem plot using vlines (= LineCollection)
    LineCollection keywords are supported.
    """
    bottom = kwargs.pop('bottom', 0)
    ax.axhline(bottom, **kwargs)
    ax.vlines(x, y, bottom, label=label, **kwargs)


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


def _validate_positive_int(value):
    """Validate value is a natural number."""
    try:
        value = int(value)
    except ValueError:
        raise ValueError('Could not convert to int')
    if value > 0:
        return value
    else:
        raise ValueError('Only positive values are valid')


def default(_):
    """
    A stub when there is no formatting needed.
    """
    return _


def _memoize(func, *args, **kwargs):
    """A args based function cache."""
    if kwargs:
        key = args, frozenset(kwargs.iteritems())
    else:
        key = args
    cache = func.cache
    if key in cache:
        return cache[key]
    else:
        cache[key] = result = func(*args, **kwargs)
        return result


@staticmethod
def getBin(bit, nbits=None, shift=0):
    """ from https://stackoverflow.com/questions/699866/python-int-to-binary """
    pure = bin(bit)[2:]
    if not nbits:
        nbits = len(pure)
    lpure = len(pure)
    admited_shift = nbits - lpure
    if admited_shift < 0:
        mje = (
            'the number of bits must be more than the bits representation of the number. {} ({}) cant be represented in {} bits'
            )
        raise ValueError(mje.format(pure, bit, nbits))
    if shift > admited_shift:
        mje = 'cant shift {} places for bit {} ({})'
        raise ValueError(mje.format(shift, pure, bit))
    if shift:
        shifted = bin(int(pure, 2) << shift)[2:]
    else:
        shifted = pure
    return shifted.zfill(nbits)


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


def Nu_cylinder_McAdams(Re, Pr):
    """Calculates Nusselt number for crossflow across a single tube
    at a specified `Re` and `Pr`, both evaluated at the film temperature. No
    other wall correction is necessary for this formulation. Also shown in
    [2]_.

    .. math::
        Nu = (0.35 + 0.56 Re^{0.52})Pr^{0.3}

    Parameters
    ----------
    Re : float
        Reynolds number with respect to cylinder diameter, [-]
    Pr : float
        Prandtl number at film temperature, [-]

    Returns
    -------
    Nu : float
        Nusselt number with respect to cylinder diameter, [-]

    Notes
    -----
    Developed with very limited test results for water only.

    This method applies to both the laminar and turbulent regimes.

    Examples
    --------
    >>> Nu_cylinder_McAdams(6071, 0.7)
    46.98179235867934

    References
    ----------
    .. [1] McAdams, William Henry. Heat Transmission. 3E. Malabar, Fla:
       Krieger Pub Co, 1985.
    .. [2] Fand, R. M. "Heat Transfer by Forced Convection from a Cylinder to
       Water in Crossflow." International Journal of Heat and Mass Transfer 8,
       no. 7 (July 1, 1965): 995-1010. doi:10.1016/0017-9310(65)90084-0.
    """
    return (0.35 + 0.56 * Re ** 0.52) * Pr ** 0.3


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


@staticmethod
def attr():
    """
        Return the available attributes for a data_container type object.

        Returns
        -------
        out : dict
            Dictionary of available attributes (dictionary keys) with default
            values.
        """
    return {}


def __setitem__(name, object):
    """Add the given `object` to the container under the given name.

        Raises a ``TypeError`` if the key is not a unicode or ascii string.

        Raises a ``ValueError`` if the key is empty, or if the key contains
        a character which is not allowed in an object name.

        Raises a ``KeyError`` if the key violates a uniqueness constraint.

        The container might choose to add a different object than the
        one passed to this method.

        If the object doesn't implement `IContained`, then one of two
        things must be done:

        1. If the object implements `ILocation`, then the `IContained`
           interface must be declared for the object.

        2. Otherwise, a `ContainedProxy` is created for the object and
           stored.

        The object's `__parent__` and `__name__` attributes are set to the
        container and the given name.

        If the old parent was ``None``, then an `IObjectAddedEvent` is
        generated, otherwise, an `IObjectMovedEvent` is generated.  An
        `IContainerModifiedEvent` is generated for the container.

        If the object replaces another object, then the old object is
        deleted before the new object is added, unless the container
        vetos the replacement by raising an exception.

        If the object's `__parent__` and `__name__` were already set to
        the container and the name, then no events are generated and
        no hooks.  This allows advanced clients to take over event
        generation.
        """


@classmethod
def is_empty2(cls, lower: 'SchemaRange', upper: 'SchemaRange') ->bool:
    """ Determines if the range given by taking lower bounds from lower and upper bound from upper is empty (contains nothing)
            is_integer is assumed to be their disjunction
        """
    is_integer = lower.is_integer or upper.is_integer
    if lower.minimum is not None and upper.maximum is not None:
        if lower.minimum > upper.maximum:
            return True
        if lower.minimum == upper.maximum and (lower.exclusive_minimum or
            upper.exclusive_maximum):
            return True
        if (is_integer and lower.exclusive_minimum and upper.
            exclusive_maximum and lower.minimum + 1 == upper.maximum):
            return True
    return False


def _fits_read_header(hdr):
    """
    Convert pyfits header into dictionary with relevant values

    Parameters
    ----------

    hdr: pyftis.Header
        fits unit

    Returns
    -------
    header: dict
        header dictionary

    alias: dict
        aliases

    units: dict
        units

    comments: dict
        comments/description of keywords
    """
    header = {}
    alias = {}
    units = {}
    comments = {}
    genTerms = ['XTENSION', 'BITPIX', 'NAXIS', 'NAXIS1', 'NAXIS2', 'PCOUNT',
        'GCOUNT', 'TFIELDS', 'EXTNAME']
    fieldTerms = ['TTYPE', 'TFORM', 'TUNIT', 'ALIAS']
    for k, name, comment in hdr.ascard['TTYPE*']:
        comments[name] = comment
        u = hdr.get(k.replace('TYPE', 'UNIT'), None)
        if u is not None:
            units[name] = u
    for k, val, _ in hdr.ascard['ALIAS*']:
        al, orig = val.split('=')
        alias[al] = orig
    header_comments = []
    header_history = []
    for k, v in list(hdr.items()):
        if k not in genTerms and k[:5] not in fieldTerms:
            if k == 'COMMENT':
                header_comments.append(v)
            elif k == 'HISTORY':
                header_history.append(v)
            else:
                header[k] = v
    if len(header_comments) > 0:
        header['COMMENT'] = '\n'.join(header_comments)
    if len(header_history) > 0:
        header['HISTORY'] = '\n'.join(header_history)
    if 'EXTNAME' in hdr:
        header['NAME'] = hdr['EXTNAME']
    return header, alias, units, comments


@classmethod
def hash(cls, hash_cls, key, data, **kwargs):
    """Return hash of data from byte string or unicode string."""
    if isinstance(data, str):
        data = data.encode('utf-8')
    ctx = cls(hash_cls, key, **kwargs)
    ctx.update(data)
    return ctx.finalize()


def write_growth_rates_file(growth_rates, growth_rates_filename):
    """ Write a growth rates data frame to a file.

    Parameters
    ----------
    growth_rates : pandas.DataFrame
        Growth rates information
    growth_rates_filename : str
        Path to file for storing growth rates data frame in CSV format
    """
    growth_rates.to_csv(growth_rates_filename, index=False)
    return