def flatten(vari):
    if isinstance(vari, Poly):
        shape = int(numpy.prod(vari.shape))
        return reshape(vari, (shape,))
    return numpy.array(vari).flatten()

Flatten a shapeable quantity.

Args:
        vari (chaospy.poly.base.Poly, numpy.ndarray):
            Shapeable input quantity.

Returns:
        (chaospy.poly.base.Poly, numpy.ndarray):
            Same type as ``vari`` with `len(Q.shape)==1`.

Example:
        >>> P = chaospy.reshape(chaospy.prange(4), (2,2))
        >>> print(P)
        [[1, q0], [q0^2, q0^3]]
        >>> print(chaospy.flatten(P))
        [1, q0, q0^2, q0^3]


def doECDHE(statprv_u, statpub_v, ephmprv_u, ephmpub_v, length=64, salt=None, info=None):
    zs = statprv_u.exchange(statpub_v)
    ze = ephmprv_u.exchange(ephmpub_v)
    z = (ze + zs)
    kdf = c_hkdf.HKDF(c_hashes.SHA256(), length=length, salt=salt, info=info, backend=default_backend())
    k = kdf.derive(z)
    return k

Perform one side of an Ecliptic Curve Diffie Hellman Ephemeral key exchange.

Args:
        statprv_u (PriKey): Static Private Key for U
        statpub_v (PubKey: Static Public Key for V
        ephmprv_u (PriKey): Ephemeral Private Key for U
        ephmpub_v (PubKey): Ephemeral Public Key for V
        length (int): Number of bytes to return
        salt (bytes): Salt to use when computing the key.
        info (bytes): Additional information to use when computing the key.

Note:
        This makes no assumption about the reuse of the Ephemeral keys passed
        to the function. It is the caller's responsibility to destroy the keys
        after they are used for doing key generation. This implementation is
        the dhHybrid1 scheme described in NIST 800-56A Revision 2.

Returns:
        bytes: The derived key.


def indicator(self, indicator_type, summary, **kwargs):
    indicator_obj = Indicator(indicator_type, summary, **kwargs)
    return self._indicator(indicator_obj)

Add Indicator data to Batch object.

Args:
        indicator_type (str): The ThreatConnect define Indicator type.
        summary (str): The value for this Indicator.
        confidence (str, kwargs): The threat confidence for this Indicator.
        date_added (str, kwargs): The date timestamp the Indicator was created.
        last_modified (str, kwargs): The date timestamp the Indicator was last modified.
        rating (str, kwargs): The threat rating for this Indicator.
        xid (str, kwargs): The external id for this Indicator.

Returns:
        obj: An instance of Indicator.


def local_reduction_attention(x, block_length, multihead_params):

    @expert_utils.add_name_scope()
    def dot_product_self_local_attention_flattened(q, k, v):
        'Strided block local self-attention.\n\n    No overlap between the blocks.\n\n    Args:\n      q (tf.Tensor): shape [batch, heads, length, depth_k]\n      k (tf.Tensor): shape [batch, heads, length, depth_k]\n      v (tf.Tensor): shape [batch, heads, length, depth_v]\n\n    Returns:\n      tf.Tensor: shape [batch, heads, length, depth_v]\n    '
        (_, num_head, _, depth) = q.get_shape().as_list()

        def pad_and_reshape(x):
            'Split the length dim into [num_block, block_length].'
            length_x = common_layers.shape_list(x)[2]
            x = tf.pad(x, [[0, 0], [0, 0], [0, ((- length_x) % block_length)], [0, 0]])
            x = tf.reshape(x, [common_layers.shape_list(x)[0], num_head, (common_layers.shape_list(x)[2] // block_length), block_length, depth])
            return x
        (q, k, v) = [pad_and_reshape(t) for t in (q, k, v)]
        logits = tf.matmul(q, k, transpose_b=True)
        logits = tf.reshape(logits, [common_layers.shape_list(logits)[0], num_head, common_layers.shape_list(logits)[2], (block_length ** 2)])
        weights = tf.nn.softmax(logits)
        weights = tf.reshape(weights, [common_layers.shape_list(weights)[0], num_head, common_layers.shape_list(weights)[2], block_length, block_length])
        weights = tf.reduce_sum(weights, axis=3, keep_dims=True)
        v_out = tf.matmul(weights, v)
        v_out = tf.squeeze(v_out, axis=3)
        return v_out
    return multihead_attention(x, None, bias=None, output_depth=x.get_shape().as_list()[(- 1)], attention_type=dot_product_self_local_attention_flattened, **multihead_params)

Reduce the length dimension using self attention.

Args:
        x (tf.Tensor): float32 of shape [batch, length, depth]
        block_length (int): Block length for local attention (Compression factor)
        multihead_params (dict): parameters for multihead attention

Returns:
        tf.Tensor: Compressed tensor of shape [batch, length // factor, depth]


def with_scopes_if_required(credentials, scopes):
    if (isinstance(credentials, Scoped) and credentials.requires_scopes):
        return credentials.with_scopes(scopes)
    else:
        return credentials

Creates a copy of the credentials with scopes if scoping is required.

    This helper function is useful when you do not know (or care to know) the
    specific type of credentials you are using (such as when you use
    :func:`google.auth.default`). This function will call
    :meth:`Scoped.with_scopes` if the credentials are scoped credentials and if
    the credentials require scoping. Otherwise, it will return the credentials
    as-is.

Args:
        credentials (google.auth.credentials.Credentials): The credentials to
            scope if necessary.
        scopes (Sequence[str]): The list of scopes to use.

Returns:
        google.auth.credentials.Credentials: Either a new set of scoped
            credentials, or the passed in credentials instance if no scoping
            was required.


def upload_file(self, local_file, dest_path, mimetype):
    self.__validate_storage_path(dest_path)
    if dest_path.endswith('/'):
        raise StorageArgumentException('Must specify target file name in dest_path argument')
    if local_file.endswith(os.path.sep):
        raise StorageArgumentException('Must specify source file name in local_file argument, directory upload not supported')
    new_file = self.api_client.create_file(name=dest_path.split('/').pop(), content_type=mimetype, parent=self.get_parent(dest_path)['uuid'])
    etag = self.api_client.upload_file_content(new_file['uuid'], source=local_file)
    new_file['etag'] = etag
    return new_file

Upload local file content to a storage service destination folder.

Args:
        local_file(str)
        dest_path(str):
            absolute Storage service path '/project' prefix is essential
            suffix should be the name the file will have on in the destination folder
            i.e.: /project/folder/.../file_name
        mimetype(str): set the contentType attribute

Returns:
        The uuid of created file entity as string

Raises:
        StorageArgumentException: Invalid arguments
        StorageForbiddenException: Server response code 403
        StorageNotFoundException: Server response code 404
        StorageException: other 400-600 error codes


def _from_any_pb(pb_type, any_pb):
    msg = pb_type()
    if (not any_pb.Unpack(msg)):
        raise TypeError('Could not convert {} to {}'.format(any_pb.__class__.__name__, pb_type.__name__))
    return msg

Converts an Any protobuf to the specified message type

Args:
        pb_type (type): the type of the message that any_pb stores an instance
            of.
        any_pb (google.protobuf.any_pb2.Any): the object to be converted.

Returns:
        pb_type: An instance of the pb_type message.

Raises:
        TypeError: if the message could not be converted.


def newsim(f, G, y0, name='NewModel', modelType=ItoModel, T=60.0, dt=0.005, repeat=1, identical=True):
    NewModel = newmodel(f, G, y0, name, modelType)
    if (repeat == 1):
        return Simulation(NewModel(), T, dt)
    else:
        return RepeatedSim(NewModel, T, dt, repeat, identical)

Make a simulation of the system defined by functions f and G.

    dy = f(y,t)dt + G(y,t).dW with initial condition y0
    This helper function is for convenience, making it easy to define
    one-off simulations interactively in ipython.

Args:
        f: callable(y, t) (defined in global scope) returning (n,) array
        Vector-valued function to define the deterministic part of the system
        G: callable(y, t) (defined in global scope) returning (n,m) array
        Optional matrix-valued function to define noise coefficients of an Ito
        SDE system.
        y0 (array):  Initial condition
        name (str): Optional class name for the new model
        modelType (type): The type of model to simulate. Must be a subclass of
        nsim.Model, for example nsim.ODEModel, nsim.ItoModel or
        nsim.StratonovichModel. The default is nsim.ItoModel.
        T: Total length of time to simulate, in seconds.
        dt: Timestep for numerical integration.
        repeat (int, optional)
        identical (bool, optional)

Returns:
        Simulation

Raises:
        SimValueError, SimTypeError


def _add_tag(self, tag):
    tags = self.data.get('tags', None)
    if tags:
        if (tag in [x['name'] for x in tags]):
            return False
    else:
        tags = list()
    tags.append({'name': tag})
    self.data['tags'] = tags
    return True

Add a tag

Args:
        tag (str): Tag to add

Returns:
        bool: True if tag added or False if tag already present


def stack1d(*points):
    result = np.empty((2, len(points)), order='F')
    for (index, point) in enumerate(points):
        result[(:, index)] = point
    return result

Fill out the columns of matrix with a series of points.

    This is because ``np.hstack()`` will just make another 1D vector
    out of them and ``np.vstack()`` will put them in the rows.

Args:
        points (Tuple[numpy.ndarray, ...]): Tuple of 1D points (i.e.
            arrays with shape ``(2,)``.

Returns:
        numpy.ndarray: The array with each point in ``points`` as its
        columns.


def create(self, name, command_to_run, description='', environment_variables=None, required_arguments=None, required_arguments_default_values=None, extra_data_to_post=None):
    if (environment_variables is None):
        environment_variables = []
    if (required_arguments is None):
        required_arguments = []
    if (required_arguments_default_values is None):
        required_arguments_default_values = {}
    request_url = (self._client.base_api_url + self.list_url)
    data_to_post = {'name': name, 'description': description, 'command_to_run': command_to_run, 'environment_variables': json.dumps(environment_variables), 'required_arguments': json.dumps(required_arguments), 'required_arguments_default_values': json.dumps(required_arguments_default_values)}
    if (extra_data_to_post is not None):
        data_to_post.update(extra_data_to_post)
    response = self._client.session.post(request_url, data=data_to_post)
    self.validate_request_success(response_text=response.text, request_url=request_url, status_code=response.status_code, expected_status_code=HTTP_201_CREATED)
    return self.response_data_to_model_instance(response.json())

Create a task type.

Args:
        name (str): The name of the task.
        command_to_run (str): The command to run to execute the task.
        description (str, optional): The description of the task type.
        environment_variables (list, optional): The environment
            variables required on the host to execute the task.
        required_arguments (list, optional): The argument names for
            the task type.
        required_arguments_default_values (dict, optional): Default
            values for the tasks required arguments.
        extra_data_to_post (dict, optional): Extra key-value pairs
            to add to the request data. This is useful for
            subclasses which require extra parameters.

Returns:
        :class:`saltant.models.base_task_instance.BaseTaskType`:
            A task type model instance representing the task type
            just created.


def add_prefix(self, name, *args, **kwargs):
    if os.path.exists(self.join(name)):
        raise LagoPrefixAlreadyExistsError(name, self.path)
    self.prefixes[name] = self.prefix_class(self.join(name), *args, **kwargs)
    self.prefixes[name].initialize()
    if (self.current is None):
        self.set_current(name)
    return self.prefixes[name]

Adds a new prefix to the workdir.

Args:
        name(str): Name of the new prefix to add
        *args: args to pass along to the prefix constructor
        *kwargs: kwargs to pass along to the prefix constructor

Returns:
        The newly created prefix

Raises:
        LagoPrefixAlreadyExistsError: if prefix name already exists in the
        workdir


def find_divisors(n):
    if (not isinstance(n, int)):
        raise TypeError('Expecting a strictly positive integer')
    if (n <= 0):
        raise ValueError('Expecting a strictly positive integer')
    for i in range(1, (int((n ** 0.5)) + 1)):
        if ((n % i) == 0):
            divisors = {i, (n // i)}
            for divisor in divisors:
                (yield divisor)

Find all the positive divisors of the given integer n.

Args:
        n (int): strictly positive integer

Returns:
        A generator of all the positive divisors of n

Raises:
        TypeError: if n is not an integer
        ValueError: if n is negative


def _get_package_name(prefix=settings.TEMP_DIR, book_id=None):
    if (book_id is None):
        book_id = str(uuid.uuid4())
    return os.path.join(prefix, book_id)

Return package path. Use uuid to generate package's directory name.

Args:
        book_id (str, default None): UUID of the book.
        prefix (str, default settings.TEMP_DIR): Where the package will be
            stored. Default :attr:`settings.TEMP_DIR`.

Returns:
        str: Path to the root directory.


def from_sasl_name(cls, name=None):
    if (rfc5801 is None):
        raise NotImplementedError('Your GSSAPI implementation does not have support for RFC 5801')
    if isinstance(name, six.text_type):
        name = name.encode(_utils._get_encoding())
    m = rfc5801.inquire_mech_for_saslname(name)
    return cls(m)

Create a Mechanism from its SASL name

Args:
        name (str): SASL name of the desired mechanism

Returns:
        Mechanism: the desired mechanism

Raises:
        GSSError

    :requires-ext:`rfc5801`


def getConParams(virtualhost):
    return pika.ConnectionParameters(host=settings.RABBITMQ_HOST, port=int(settings.RABBITMQ_PORT), virtual_host=virtualhost, credentials=pika.PlainCredentials(settings.RABBITMQ_USER_NAME, settings.RABBITMQ_USER_PASSWORD))

Connection object builder.

Args:
        virtualhost (str): selected virtualhost in rabbitmq

Returns:
        pika.ConnectionParameters: object filled by `constants` from
        :class:`edeposit.amqp.settings`.


def generate_output_whois_nets(self, json_data=None, hr=True, show_name=False, colorize=True):
    if (json_data is None):
        json_data = {}
    output = generate_output(line='0', short=(HR_WHOIS['nets']['_short'] if hr else 'nets'), name=(HR_WHOIS['nets']['_name'] if (hr and show_name) else None), is_parent=True, colorize=colorize)
    count = 0
    for net in json_data['nets']:
        if (count > 0):
            output += self.generate_output_newline(line='1', colorize=colorize)
        count += 1
        output += generate_output(line='1', short=net['handle'], is_parent=True, colorize=colorize)
        for (key, val) in net.items():
            if (val and ('\n' in val)):
                output += generate_output(line='2', short=(HR_WHOIS['nets'][key]['_short'] if hr else key), name=(HR_WHOIS['nets'][key]['_name'] if (hr and show_name) else None), is_parent=(False if ((val is None) or (len(val) == 0)) else True), value=('None' if ((val is None) or (len(val) == 0)) else None), colorize=colorize)
                for v in val.split('\n'):
                    output += generate_output(line='3', value=v, colorize=colorize)
            else:
                output += generate_output(line='2', short=(HR_WHOIS['nets'][key]['_short'] if hr else key), name=(HR_WHOIS['nets'][key]['_name'] if (hr and show_name) else None), value=val, colorize=colorize)
    return output

The function for generating CLI output Legacy Whois networks results.

Args:
        json_data (:obj:`dict`): The data to process. Defaults to None.
        hr (:obj:`bool`): Enable human readable key translations. Defaults
            to True.
        show_name (:obj:`bool`): Show human readable name (default is to
            only show short). Defaults to False.
        colorize (:obj:`bool`): Colorize the console output with ANSI
            colors. Defaults to True.

Returns:
        str: The generated output.


def register(self, key_or_value=None):

    def decorator(value, key):
        self[key] = value
        return value
    if callable(key_or_value):
        return decorator(value=key_or_value, key=None)
    else:
        return (lambda value: decorator(value, key=key_or_value))

Decorator to register a function, or registration itself.

    This is primarily intended for use as a decorator, either with or without
    a key/parentheses.
    ```python
    @my_registry.register('key1')
    def value_fn(x, y, z):
        pass

    @my_registry.register()
    def another_fn(x, y):
        pass

    @my_registry.register
    def third_func():
        pass
    ```

    Note if key_or_value is provided as a non-callable, registration only
    occurs once the returned callback is called with a callable as its only
    argument.
    ```python
    callback = my_registry.register('different_key')
    'different_key' in my_registry  # False
    callback(lambda (x, y): x + y)
    'different_key' in my_registry  # True
    ```

Args:
        key_or_value (optional): key to access the registered value with, or the
        function itself. If `None` (default), `self.default_key` will be called
        on `value` once the returned callback is called with `value` as the only
        arg. If `key_or_value` is itself callable, it is assumed to be the value
        and the key is given by `self.default_key(key)`.

Returns:
        decorated callback, or callback generated a decorated function.


def process_input_data(filename, imager, grid_data, grid_norm, grid_weights):
    ms = oskar.MeasurementSet.open(filename)
    block_start = 0
    num_rows = ms.num_rows
    num_baselines = ((ms.num_stations * (ms.num_stations - 1)) // 2)
    while (block_start < num_rows):
        block_size = (num_rows - block_start)
        if (block_size > num_baselines):
            block_size = num_baselines
        uvw = ms.read_column('UVW', block_start, block_size)
        vis_weights = ms.read_column('WEIGHT', block_start, block_size)
        if (ms.num_pols == 4):
            vis_weights = (0.5 * (vis_weights[(:, 0)] + vis_weights[(:, 3)]))
        for j in range(ms.num_channels):
            coords = ((uvw * (ms.freq_start_hz + (j * ms.freq_inc_hz))) / 299792458.0)
            vis_data = None
            if (not imager.coords_only):
                vis_data = ms.read_vis(block_start, j, 1, block_size)
                if (ms.num_pols == 4):
                    vis_data = (0.5 * (vis_data[(0, :, 0)] + vis_data[(0, :, 3)]))
            grid_norm = imager.update_plane(coords[(:, 0)], coords[(:, 1)], coords[(:, 2)], vis_data, vis_weights, grid_data, grid_norm, grid_weights)
        block_start += block_size
    return grid_norm

Reads visibility data from a Measurement Set.

    The visibility grid or weights grid is updated accordingly.

    Visibility data are read from disk in blocks of size num_baselines.

Args:
        filename (str):                    Name of Measurement Set to open.
        imager (oskar.Imager):             Handle to configured imager.
        grid_data (numpy.ndarray or None): Visibility grid to populate.
        grid_norm (float)                  Current grid normalisation.
        grid_weights (numpy.ndarray):      Weights grid to populate or read.

Returns:
        grid_norm (float):                 Updated grid normalisation.


def symbolic_heisenberg_eom(self, X=None, noises=None, expand_simplify=True):
    (L, H) = (self.L, self.H)
    if (X is None):
        X = OperatorSymbol('X', hs=(L.space | H.space))
    summands = [(I * ((H * X) - (X * H)))]
    for Lk in L.matrix.ravel():
        summands.append(((adjoint(Lk) * X) * Lk))
        summands.append(((- (((adjoint(Lk) * Lk) * X) + ((X * adjoint(Lk)) * Lk))) / 2))
    if (noises is not None):
        if (not isinstance(noises, Matrix)):
            noises = Matrix(noises)
        LambdaT = (noises.adjoint().transpose() * noises.transpose()).transpose()
        assert (noises.shape == L.shape)
        S = self.S
        summands.append(((adjoint(noises) * S.adjoint()) * ((X * L) - (L * X))).expand()[(0, 0)])
        summand = ((((L.adjoint() * X) - (X * L.adjoint())) * S) * noises).expand()[(0, 0)]
        summands.append(summand)
        if len((S.space & X.space)):
            comm = (((S.adjoint() * X) * S) - X)
            summands.append((comm * LambdaT).expand().trace())
    ret = OperatorPlus.create(*summands)
    if expand_simplify:
        ret = ret.expand().simplify_scalar()
    return ret

Compute the symbolic Heisenberg equations of motion of a system
    operator X.  If no X is given, an OperatorSymbol is created in its
    place.  If no noises are given, this correspnds to the
    ensemble-averaged Heisenberg equation of motion.

Args:
        X (Operator): A system operator
        noises (Operator): A vector of noise inputs

Returns:
        Operator: The RHS of the Heisenberg equations of motion of X.


def scan_file(path):
    path = os.path.abspath(path)
    assert os.path.exists(path), ("Unreachable file '%s'." % path)
    result = sh.clamscan(path, no_summary=True, infected=True, _ok_code=[0, 1])
    return _parse_result(result)

Scan `path` for viruses using ``clamscan`` program.

Args:
        path (str): Relative or absolute path of file/directory you need to
                    scan.

Returns:
        dict: ``{filename: ("FOUND", "virus type")}`` or blank dict.

Raises:
        AssertionError: When the internal file doesn't exists.


def replace_nones(list_, repl=(- 1)):
    repl_list = [(repl if (item is None) else (replace_nones(item, repl) if isinstance(item, list) else item)) for item in list_]
    return repl_list

r"""
    Recursively removes Nones in all lists and sublists and replaces them with
    the repl variable

Args:
        list_ (list):
        repl (obj): replacement value

Returns:
        list

    CommandLine:
        python -m utool.util_list --test-replace_nones

Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_list import *  # NOQA
        >>> # build test data
        >>> list_ = [None, 0, 1, 2]
        >>> repl = -1
        >>> # execute function
        >>> repl_list = replace_nones(list_, repl)
        >>> # verify results
        >>> result = str(repl_list)
        >>> print(result)
        [-1, 0, 1, 2]


def max_entropy_distribution(node_indices, number_of_nodes):
    distribution = np.ones(repertoire_shape(node_indices, number_of_nodes))
    return (distribution / distribution.size)

Return the maximum entropy distribution over a set of nodes.

    This is different from the network's uniform distribution because nodes
    outside ``node_indices`` are fixed and treated as if they have only 1
    state.

Args:
        node_indices (tuple[int]): The set of node indices over which to take
            the distribution.
        number_of_nodes (int): The total number of nodes in the network.

Returns:
        np.ndarray: The maximum entropy distribution over the set of nodes.


def reflection(n1, n2):
    r = (abs(((n1 - n2) / (n1 + n2))) ** 2)
    return r

Calculate the power reflection at the interface
    of two refractive index materials.

Args:
        n1 (float): Refractive index of material 1.
        n2 (float): Refractive index of material 2.

Returns:
        float: The percentage of reflected power.


def x_www_form_urlencoded(post_data):
    if isinstance(post_data, dict):
        return '&'.join([u'{}={}'.format(key, value) for (key, value) in post_data.items()])
    else:
        return post_data

convert origin dict to x-www-form-urlencoded

Args:
        post_data (dict):
            {"a": 1, "b":2}

Returns:
        str:
            a=1&b=2


def namespace_for_prefix(self, prefix):
    try:
        ni = self.__lookup_prefix(prefix)
    except PrefixNotFoundError:
        return None
    else:
        return ni.uri

Get the namespace the given prefix maps to.

Args:
        prefix (str): The prefix

Returns:
        str: The namespace, or None if the prefix isn't mapped to
            anything in this set.


def basistransform(self, new_basis, old_basis=None, orthonormalize=True):
    if (old_basis is None):
        old_basis = np.identity(3)
    is_rotation_matrix = np.isclose(np.linalg.det(new_basis), 1)
    if ((not is_rotation_matrix) and orthonormalize):
        new_basis = xyz_functions.orthonormalize_righthanded(new_basis)
        is_rotation_matrix = True
    if is_rotation_matrix:
        return dot(np.dot(new_basis.T, old_basis), self)
    else:
        return dot(np.dot(np.linalg.inv(new_basis), old_basis), self)

Transform the frame to a new basis.

    This function transforms the cartesian coordinates from an
    old basis to a new one. Please note that old_basis and
    new_basis are supposed to have full Rank and consist of
    three linear independent vectors. If rotate_only is True,
    it is asserted, that both bases are orthonormal and right
    handed. Besides all involved matrices are transposed
    instead of inverted.
    In some applications this may require the function
    :func:`xyz_functions.orthonormalize` as a previous step.

Args:
        old_basis (np.array):
        new_basis (np.array):
        rotate_only (bool):

Returns:
        Cartesian: The transformed molecule.


def to_dict(self, remove_nones=False):
    content = {}
    for key in self._translation:
        if hasattr(self, key):
            content[key] = getattr(self, key)
    content['parent_id'] = self.parent_id
    content['item_id'] = self.item_id
    content['restricted'] = self.restricted
    content['title'] = self.title
    if (self.resources != []):
        content['resources'] = [resource.to_dict(remove_nones=remove_nones) for resource in self.resources]
    content['desc'] = self.desc
    return content

Return the dict representation of the instance.

Args:
        remove_nones (bool, optional): Optionally remove dictionary
            elements when their value is `None`.

Returns:
        dict: a dict representation of the `DidlObject`.


def df_first_row_to_dict(df):
    if (df is not None):
        return [dict(r) for (i, r) in df.head(1).iterrows()][0]

First DataFrame row to list of dict

Args:
        df (pandas.DataFrame): A DataFrame with at least one row

Returns:
        A list of dict that looks like:

            [{'C1': 'x'}, {'C2': 'y'}, {'C3': 'z'}]

        from a DataFrame that looks like:

                C1  C2  C3
            1   x   y   z

        Else if `df` is `None`, returns `None`


def get_url_for_get(url, parameters=None):
    spliturl = urlsplit(url)
    getparams = OrderedDict(parse_qsl(spliturl.query))
    if (parameters is not None):
        getparams.update(parameters)
    spliturl = spliturl._replace(query=urlencode(getparams))
    return urlunsplit(spliturl)

Get full url for GET request including parameters

Args:
        url (str): URL to download
        parameters (Optional[Dict]): Parameters to pass. Defaults to None.

Returns:
        str: Full url


def variant(self, document_id, gene_panels=None, case_id=None):
    query = {}
    if case_id:
        query['case_id'] = case_id
        query['variant_id'] = document_id
    else:
        query['_id'] = document_id
    variant_obj = self.variant_collection.find_one(query)
    if variant_obj:
        variant_obj = self.add_gene_info(variant_obj, gene_panels)
        if (variant_obj['chromosome'] in ['X', 'Y']):
            variant_obj['is_par'] = is_par(variant_obj['chromosome'], variant_obj['position'])
    return variant_obj

Returns the specified variant.

Args:
        document_id : A md5 key that represents the variant or "variant_id"
        gene_panels(List[GenePanel])
        case_id (str): case id (will search with "variant_id")

Returns:
        variant_object(Variant): A odm variant object


def extractMonthTariff(self, month):
    ret = namedtuple('ret', ['Month', Field.kWh_Tariff_1, Field.kWh_Tariff_2, Field.kWh_Tariff_3, Field.kWh_Tariff_4, Field.kWh_Tot, Field.Rev_kWh_Tariff_1, Field.Rev_kWh_Tariff_2, Field.Rev_kWh_Tariff_3, Field.Rev_kWh_Tariff_4, Field.Rev_kWh_Tot])
    month += 1
    ret.Month = str(month)
    if ((month < 1) or (month > Extents.Months)):
        ret.kWh_Tariff_1 = ret.kWh_Tariff_2 = ret.kWh_Tariff_3 = ret.kWh_Tariff_4 = str(0)
        ret.Rev_kWh_Tariff_1 = ret.Rev_kWh_Tariff_2 = ret.Rev_kWh_Tariff_3 = ret.Rev_kWh_Tariff_4 = str(0)
        ret.kWh_Tot = ret.Rev_kWh_Tot = str(0)
        ekm_log(('Out of range(Extents.Months) month = ' + str(month)))
        return ret
    base_str = (('Month_' + str(month)) + '_')
    ret.kWh_Tariff_1 = self.m_mons[(base_str + 'Tariff_1')][MeterData.StringValue]
    ret.kWh_Tariff_2 = self.m_mons[(base_str + 'Tariff_2')][MeterData.StringValue]
    ret.kWh_Tariff_3 = self.m_mons[(base_str + 'Tariff_3')][MeterData.StringValue]
    ret.kWh_Tariff_4 = self.m_mons[(base_str + 'Tariff_4')][MeterData.StringValue]
    ret.kWh_Tot = self.m_mons[(base_str + 'Tot')][MeterData.StringValue]
    ret.Rev_kWh_Tariff_1 = self.m_rev_mons[(base_str + 'Tariff_1')][MeterData.StringValue]
    ret.Rev_kWh_Tariff_2 = self.m_rev_mons[(base_str + 'Tariff_2')][MeterData.StringValue]
    ret.Rev_kWh_Tariff_3 = self.m_rev_mons[(base_str + 'Tariff_3')][MeterData.StringValue]
    ret.Rev_kWh_Tariff_4 = self.m_rev_mons[(base_str + 'Tariff_4')][MeterData.StringValue]
    ret.Rev_kWh_Tot = self.m_rev_mons[(base_str + 'Tot')][MeterData.StringValue]
    return ret

Extract the tariff for a single month from the meter object buffer.

Args:
        month (int):  A :class:`~ekmmeters.Months` value or range(Extents.Months).

Returns:
        tuple: The eight tariff period totals for month. The return tuple breaks out as follows:

        ================= ======================================
        kWh_Tariff_1      kWh for tariff period 1 over month.
        kWh_Tariff_2      kWh for tariff period 2 over month
        kWh_Tariff_3      kWh for tariff period 3 over month
        kWh_Tariff_4      kWh for tariff period 4 over month
        kWh_Tot           Total kWh over requested month
        Rev_kWh_Tariff_1  Rev kWh for tariff period 1 over month
        Rev_kWh_Tariff_3  Rev kWh for tariff period 2 over month
        Rev_kWh_Tariff_3  Rev kWh for tariff period 3 over month
        Rev_kWh_Tariff_4  Rev kWh for tariff period 4 over month
        Rev_kWh_Tot       Total Rev kWh over requested month
        ================= ======================================


def add_update_resources(self, resources, ignore_datasetid=False):
    if (not isinstance(resources, list)):
        raise HDXError('Resources should be a list!')
    for resource in resources:
        self.add_update_resource(resource, ignore_datasetid)

Add new or update existing resources with new metadata to the dataset

Args:
        resources (List[Union[hdx.data.resource.Resource,Dict,str]]): A list of either resource ids or resources metadata from either Resource objects or dictionaries
        ignore_datasetid (bool): Whether to ignore dataset id in the resource. Defaults to False.

Returns:
        None


def parse_cscore(infile):
    cscore_dict = {}
    with open(infile, 'r') as f:
        for ll in f.readlines():
            if ll.lower().startswith('model1'):
                l = ll.split()
                cscore = l[1]
                tmscore_full = l[2].split('+-')
                tmscore = tmscore_full[0]
                tmscore_err = tmscore_full[1]
                rmsd_full = l[3].split('+-')
                rmsd = rmsd_full[0]
                rmsd_err = rmsd_full[1]
                cscore_dict['c_score'] = float(cscore)
                cscore_dict['tm_score'] = float(tmscore)
                cscore_dict['tm_score_err'] = float(tmscore_err)
                cscore_dict['rmsd'] = float(rmsd)
                cscore_dict['rmsd_err'] = float(rmsd_err)
    return cscore_dict

Parse the cscore file to return a dictionary of scores.

Args:
        infile (str): Path to cscore

Returns:
        dict: Dictionary of scores


def _ParseDateTimeValue(self, parser_mediator, date_time_value):
    if (date_time_value[14] != 'Z'):
        parser_mediator.ProduceExtractionWarning('invalid date and time value: {0!s}'.format(date_time_value))
        return None
    try:
        year = int(date_time_value[0:4], 10)
        month = int(date_time_value[4:6], 10)
        day_of_month = int(date_time_value[6:8], 10)
        hours = int(date_time_value[8:10], 10)
        minutes = int(date_time_value[10:12], 10)
        seconds = int(date_time_value[12:14], 10)
    except (TypeError, ValueError):
        parser_mediator.ProduceExtractionWarning('invalid date and time value: {0!s}'.format(date_time_value))
        return None
    time_elements_tuple = (year, month, day_of_month, hours, minutes, seconds)
    try:
        return dfdatetime_time_elements.TimeElements(time_elements_tuple=time_elements_tuple)
    except ValueError:
        parser_mediator.ProduceExtractionWarning('invalid date and time value: {0!s}'.format(date_time_value))
        return None

Parses a date time value.

Args:
        parser_mediator (ParserMediator): mediates interactions between parsers
        and other components, such as storage and dfvfs.
        date_time_value (str): date time value
        (CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE) in the format: "YYYYMMDDhhmmssZ".

Returns:
        dfdatetime.TimeElements: date and time extracted from the value or None
        if the value does not represent a valid string.


def lsdirs(root='.', **kwargs):
    paths = ls(root=root, **kwargs)
    if isfile(root):
        return []
    return [_path for _path in paths if isdir(path(root, _path))]

Return only subdirectories from a directory listing.

Args:
        root (str): Path to directory. Can be relative or absolute.
        **kwargs: Any additional arguments to be passed to ls().

Returns:
        list of str: A list of directory paths.

Raises:
        OSError: If root directory does not exist.


def create(self, master_course_id, coach_email, max_students_allowed, title, modules=None):
    payload = {'master_course_id': master_course_id, 'coach_email': coach_email, 'max_students_allowed': max_students_allowed, 'display_name': title}
    if (modules is not None):
        payload['course_modules'] = modules
    resp = self.requester.post(parse.urljoin(self.base_url, '/api/ccx/v0/ccx/'), json=payload)
    try:
        resp.raise_for_status()
    except:
        log.error(resp.json())
        raise
    return resp.json()['ccx_course_id']

Creates a CCX

Args:
        master_course_id (str): edx course id of the master course
        coach_email (str): email of the user to make a coach. This user must exist on edx.
        max_students_allowed (int): Maximum number of students to allow in this ccx.
        title (str): Title of the CCX to be created
        modules (optional list): A list of locator_ids (str) for the modules to enable.

Returns:
        ccx_id (str): The ID of the ccx.


def reviews(self, packageName, filterByDevice=False, sort=2, nb_results=None, offset=None):
    path = (REVIEWS_URL + '?doc={}&sort={}'.format(requests.utils.quote(packageName), sort))
    if (nb_results is not None):
        path += '&n={}'.format(nb_results)
    if (offset is not None):
        path += '&o={}'.format(offset)
    if filterByDevice:
        path += '&dfil=1'
    data = self.executeRequestApi2(path)
    output = []
    for review in data.payload.reviewResponse.getResponse.review:
        output.append(utils.parseProtobufObj(review))
    return output

Browse reviews for an application

Args:
        packageName (str): app unique ID.
        filterByDevice (bool): filter results for current device
        sort (int): sorting criteria (values are unknown)
        nb_results (int): max number of reviews to return
        offset (int): return reviews starting from an offset value

Returns:
        dict object containing all the protobuf data returned from
        the api


def render(self, filename=None, directory=None, view=False, cleanup=False, format=None, renderer=None, formatter=None):
    filepath = self.save(filename, directory)
    if (format is None):
        format = self._format
    rendered = backend.render(self._engine, format, filepath, renderer, formatter)
    if cleanup:
        os.remove(filepath)
    if view:
        self._view(rendered, self._format)
    return rendered

Save the source to file and render with the Graphviz engine.

Args:
        filename: Filename for saving the source (defaults to ``name`` + ``'.gv'``)
        directory: (Sub)directory for source saving and rendering.
        view (bool): Open the rendered result with the default application.
        cleanup (bool): Delete the source file after rendering.
        format: The output format used for rendering (``'pdf'``, ``'png'``, etc.).
        renderer: The output renderer used for rendering (``'cairo'``, ``'gd'``, ...).
        formatter: The output formatter used for rendering (``'cairo'``, ``'gd'``, ...).

Returns:
        The (possibly relative) path of the rendered file.

Raises:
        ValueError: If ``format``, ``renderer``, or ``formatter`` are not known.
        graphviz.RequiredArgumentError: If ``formatter`` is given but ``renderer`` is None.
        graphviz.ExecutableNotFound: If the Graphviz executable is not found.
        subprocess.CalledProcessError: If the exit status is non-zero.
        RuntimeError: If viewer opening is requested but not supported.


def get_tag(self, key, *, case_sensitive=True):
    key = (key if case_sensitive else key.lower())
    for tag in self.resource.tags:
        if (not case_sensitive):
            if (tag.key.lower() == key):
                return tag
        elif (key == tag.key):
            return tag
    return None

Return a tag by key, if found

Args:
        key (str): Name/key of the tag to locate
        case_sensitive (bool): Should tag keys be treated case-sensitive (default: true)

Returns:
        `Tag`,`None`


def find_noncopyable_vars(class_type, already_visited_cls_vars=None):
    assert isinstance(class_type, class_declaration.class_t)
    logger = utils.loggers.cxx_parser
    mvars = class_type.variables((lambda v: (not v.type_qualifiers.has_static)), recursive=False, allow_empty=True)
    noncopyable_vars = []
    if (already_visited_cls_vars is None):
        already_visited_cls_vars = []
    message = ('__contains_noncopyable_mem_var - %s - TRUE - ' + 'contains const member variable')
    for mvar in mvars:
        var_type = type_traits.remove_reference(mvar.decl_type)
        if type_traits.is_const(var_type):
            no_const = type_traits.remove_const(var_type)
            if (type_traits.is_fundamental(no_const) or is_enum(no_const)):
                logger.debug((message + '- fundamental or enum'), var_type.decl_string)
                noncopyable_vars.append(mvar)
            if is_class(no_const):
                logger.debug((message + ' - class'), var_type.decl_string)
                noncopyable_vars.append(mvar)
            if type_traits.is_array(no_const):
                logger.debug((message + ' - array'), var_type.decl_string)
                noncopyable_vars.append(mvar)
        if type_traits.is_pointer(var_type):
            continue
        if class_traits.is_my_case(var_type):
            cls = class_traits.get_declaration(var_type)
            if (cls in already_visited_cls_vars):
                continue
            already_visited_cls_vars.append(cls)
            if is_noncopyable(cls, already_visited_cls_vars):
                logger.debug((message + ' - class that is not copyable'), var_type.decl_string)
                noncopyable_vars.append(mvar)
    logger.debug(("__contains_noncopyable_mem_var - %s - FALSE - doesn't " + 'contain noncopyable members'), class_type.decl_string)
    return noncopyable_vars

Returns list of all `noncopyable` variables.

    If an already_visited_cls_vars list is provided as argument, the returned
    list will not contain these variables. This list will be extended with
    whatever variables pointing to classes have been found.

Args:
        class_type (declarations.class_t): the class to be searched.
        already_visited_cls_vars (list): optional list of vars that should not
            be checked a second time, to prevent infinite recursions.

Returns:
        list: list of all `noncopyable` variables.


def get_symbol(self, symbol):
    self._ensure_symbols_loaded()
    if (type(symbol) is int):
        return self._symbols_by_index[symbol]
    else:
        return self._symbols_by_name[symbol]

Get a specific symbol by index or name.

Args:
        symbol(int or str): The index or name of the symbol to return.

Returns:
        ELF.Symbol: The symbol.

Raises:
        KeyError: The requested symbol does not exist.


def from_lasio_curve(cls, curve, depth=None, basis=None, start=None, stop=None, step=0.1524, run=(- 1), null=(- 999.25), service_company=None, date=None):
    data = curve.data
    unit = curve.unit
    if (depth is not None):
        d = np.diff(depth)
        if (not np.allclose((d - np.mean(d)), np.zeros_like(d))):
            m = 'Irregular sampling in depth is not supported. '
            m += 'Interpolating to regular basis.'
            warnings.warn(m)
            step = np.nanmedian(d)
            (start, stop) = (depth[0], (depth[(- 1)] + 1e-05))
            basis = np.arange(start, stop, step)
            data = np.interp(basis, depth, data)
        else:
            step = np.nanmedian(d)
            start = depth[0]
    if (start is None):
        if (basis is not None):
            start = basis[0]
            step = (basis[1] - basis[0])
        else:
            raise CurveError('You must provide a basis or a start depth.')
    if (step == 0):
        if (stop is None):
            raise CurveError('You must provide a step or a stop depth.')
        else:
            step = ((stop - start) / (curve.data.shape[0] - 1))
    params = {}
    params['mnemonic'] = curve.mnemonic
    params['description'] = curve.descr
    params['start'] = start
    params['step'] = step
    params['units'] = unit
    params['run'] = run
    params['null'] = null
    params['service_company'] = service_company
    params['date'] = date
    params['code'] = curve.API_code
    return cls(data, params=params)

Makes a curve object from a lasio curve object and either a depth
    basis or start and step information.

Args:
        curve (ndarray)
        depth (ndarray)
        basis (ndarray)
        start (float)
        stop (float)
        step (float): default: 0.1524
        run (int): default: -1
        null (float): default: -999.25
        service_company (str): Optional.
        data (str): Optional.

Returns:
        Curve. An instance of the class.


def parse_args(self, arglist=None):
    args = self._parser.parse_args(args=arglist)
    sub_cmd = args.loam_sub_name
    if (sub_cmd is None):
        for (opt, sct) in self._opt_bare.items():
            self._conf[sct][opt] = getattr(args, opt, None)
    else:
        for (opt, sct) in self._opt_cmds[sub_cmd].items():
            self._conf[sct][opt] = getattr(args, opt, None)
    return args

Parse arguments and update options accordingly.

Args:
        arglist (list of str): list of arguments to parse. If set to None,
            ``sys.argv[1:]`` is used.

Returns:
        :class:`Namespace`: the argument namespace returned by the
        :class:`argparse.ArgumentParser`.


def has_auth_params(self, scheme):
    for (k, v) in iteritems(self.schemes[scheme][u'params']):
        if (not v):
            return False
    return True

Check whether all information required for a given auth scheme have
    been supplied.

Args:
        scheme (str): Name of the authentication scheme to check. One of
        Gem-Identify, Gem-Device, Gem-Application

Returns:
        True if all required parameters for the specified scheme are present
        or False otherwise.


def url_builder(self, endpoint, *, root=None, params=None, url_params=None):
    if (root is None):
        root = self.ROOT
    return ''.join([root, endpoint, (('?' + urlencode(url_params)) if url_params else '')]).format(**(params or {}))

Create a URL for the specified endpoint.

Args:
        endpoint (:py:class:`str`): The API endpoint to access.
        root: (:py:class:`str`, optional): The root URL for the
        service API.
        params: (:py:class:`dict`, optional): The values for format
        into the created URL (defaults to ``None``).
        url_params: (:py:class:`dict`, optional): Parameters to add
        to the end of the URL (defaults to ``None``).

Returns:
        :py:class:`str`: The resulting URL.


def default(self, interface, vrid):
    vrrp_str = ('default vrrp %d' % vrid)
    return self.configure_interface(interface, vrrp_str)

Defaults a vrrp instance from an interface

Note:
        This method will attempt to default the vrrp on the node's
        operational config. Default results in the deletion of the
        specified vrrp . If the vrrp does not exist on the
        interface then this method will not perform any changes
        but still return True

Args:
        interface (string): The interface to configure.
        vrid (integer): The vrid number for the vrrp to be defaulted.

Returns:
        True if the vrrp could be defaulted otherwise False (see Node)


def create_identical_dataset_and_algorithm_tuner(parent, additional_parents=None, sagemaker_session=None):
    parent_tuner = HyperparameterTuner.attach(tuning_job_name=parent, sagemaker_session=sagemaker_session)
    return parent_tuner.identical_dataset_and_algorithm_tuner(additional_parents=additional_parents)

Creates a new tuner by copying the request fields from the provided parent to the new instance of
    ``HyperparameterTuner`` followed by addition of warm start configuration with the type as
    "IdenticalDataAndAlgorithm" and ``parents`` as the union of provided list of ``additional_parents`` and the
    ``parent``.

Args:
    parent (str): Primary parent tuning job's name from which the Tuner and Estimator configuration has to be copied
    additional_parents (set{str}): Set of additional parent tuning job's names along with the primary parent tuning
        job name to be used in warm starting the transfer learning tuner.
    sagemaker_session (sagemaker.session.Session): Session object which manages interactions with
            Amazon SageMaker APIs and any other AWS services needed. If not specified, one is created
            using the default AWS configuration chain.

Returns:
    sagemaker.tuner.HyperparameterTuner: a new ``HyperparameterTuner`` object for the warm-started
    hyperparameter tuning job


def QoI_Dist(poly, dist, sample=10000, **kws):
    shape = poly.shape
    poly = polynomials.flatten(poly)
    dim = len(dist)
    samples = dist.sample(sample, **kws)
    qoi_dists = []
    for i in range(0, len(poly)):
        if (dim == 1):
            dataset = poly[i](samples)
        else:
            dataset = poly[i](*samples)
        lo = dataset.min()
        up = dataset.max()
        qoi_dist = distributions.SampleDist(dataset, lo, up)
        qoi_dists.append(qoi_dist)
    qoi_dists = numpy.array(qoi_dists, distributions.Dist)
    qoi_dists = qoi_dists.reshape(shape)
    if (not shape):
        qoi_dists = qoi_dists.item()
    return qoi_dists

Constructs distributions for the quantity of interests.

    The function constructs a kernel density estimator (KDE) for each
    polynomial (poly) by sampling it.  With the KDEs, distributions (Dists) are
    constructed.  The Dists can be used for e.g. plotting probability density
    functions (PDF), or to make a second uncertainty quantification simulation
    with that newly generated Dists.

Args:
        poly (Poly):
            Polynomial of interest.
        dist (Dist):
            Defines the space where the samples for the KDE is taken from the
            poly.
        sample (int):
            Number of samples used in estimation to construct the KDE.

Returns:
        (numpy.ndarray):
            The constructed quantity of interest (QoI) distributions, where
            ``qoi_dists.shape==poly.shape``.

Example:
        >>> dist = chaospy.Normal(0, 1)
        >>> x = chaospy.variable(1)
        >>> poly = chaospy.Poly([x])
        >>> qoi_dist = chaospy.QoI_Dist(poly, dist)
        >>> values = qoi_dist[0].pdf([-0.75, 0., 0.75])
        >>> print(numpy.around(values, 8))
        [0.29143037 0.39931708 0.29536329]


def matchmaker_request(url, token, method, content_type=None, accept=None, data=None):
    headers = Headers()
    headers = {'X-Auth-Token': token}
    if content_type:
        headers['Content-Type'] = content_type
    if accept:
        headers['Accept'] = accept
    req_data = (data or {'timestamp': datetime.datetime.now().timestamp()})
    json_response = None
    try:
        LOG.info('Sending {} request to MME url {}. Data sent: {}'.format(method, url, req_data))
        resp = requests.request(method=method, url=url, headers=headers, data=json.dumps(req_data))
        json_response = resp.json()
        LOG.info('MME server response was:{}'.format(json_response))
        if isinstance(json_response, str):
            json_response = {'message': json_response}
        elif isinstance(json_response, list):
            return json_response
        json_response['status_code'] = resp.status_code
    except Exception as err:
        LOG.info('An error occurred while sending HTTP request to server ({})'.format(err))
        json_response = {'message': str(err)}
    return json_response

Send a request to MatchMaker and return its response

Args:
        url(str): url to send request to
        token(str): MME server authorization token
        method(str): 'GET', 'POST' or 'DELETE'
        content_type(str): MME request Content-Type
        accept(str): accepted response
        data(dict): eventual data to send in request

Returns:
        json_response(dict): server response


def hardware_version(self):
    version = self._dll.JLINKARM_GetHardwareVersion()
    major = ((version / 10000) % 100)
    minor = ((version / 100) % 100)
    return ('%d.%02d' % (major, minor))

Returns the hardware version of the connected J-Link as a
    major.minor string.

Args:
        self (JLink): the ``JLink`` instance

Returns:
        Hardware version string.


def read_nose(in_file):
    suites = {}
    doc_xml = minidom.parse(in_file)
    suite_xml = doc_xml.getElementsByTagName('testsuite')[0]
    for case_xml in suite_xml.getElementsByTagName('testcase'):
        classname = case_xml.getAttribute('classname')
        if (classname not in suites):
            suites[classname] = []
        case = {'name': case_xml.getAttribute('name'), 'time': float(case_xml.getAttribute('time'))}
        skipped_xml = case_xml.getElementsByTagName('skipped')
        if skipped_xml:
            if skipped_xml[0].hasAttribute('type'):
                type = skipped_xml[0].getAttribute('type')
            else:
                type = ''
            case['skipped'] = {'type': type, 'message': skipped_xml[0].getAttribute('message'), 'text': ''.join([child.nodeValue for child in skipped_xml[0].childNodes])}
        failure_xml = case_xml.getElementsByTagName('failure')
        if failure_xml:
            if failure_xml[0].hasAttribute('type'):
                type = failure_xml[0].getAttribute('type')
            else:
                type = ''
            case['failure'] = {'type': type, 'message': failure_xml[0].getAttribute('message'), 'text': ''.join([child.nodeValue for child in failure_xml[0].childNodes])}
        error_xml = case_xml.getElementsByTagName('error')
        if error_xml:
            if error_xml[0].hasAttribute('type'):
                type = error_xml[0].getAttribute('type')
            else:
                type = ''
            case['error'] = {'type': type, 'message': error_xml[0].getAttribute('message'), 'text': ''.join([child.nodeValue for child in error_xml[0].childNodes])}
        suites[classname].append(case)
    return suites

Parse nose-style test reports into a `dict`

Args:
        in_file (:obj:`str`): path to nose-style test report

Returns:
        :obj:`dict`: dictionary of test suites


def from_bytes(b):
    if (len(b) != 64):
        raise ValueError('from_bytes: Signature length != 64.')
    r = int.from_bytes(b[0:32], 'big')
    s = int.from_bytes(b[32:64], 'big')
    return Signature(r, s)

Extracts the r and s components from a byte string.

Args:
        b (bytes): A 64-byte long string. The first 32 bytes are
        extracted as the r component and the second 32 bytes
        are extracted as the s component.

Returns:
        Signature: A Signature object.

Raises:
        ValueError: If signature is incorrect length


def create_releasenotes(project_dir=os.curdir, bugtracker_url=''):
    pkg_info_file = os.path.join(project_dir, 'PKG-INFO')
    if os.path.exists(pkg_info_file):
        return
    with open('RELEASE_NOTES', 'wb') as releasenotes_fd:
        releasenotes_fd.write((get_releasenotes(project_dir=project_dir, bugtracker_url=bugtracker_url).encode('utf-8') + b'\n'))

Creates the release notes file, if not in a package.

Args:
        project_dir(str): Path to the git repo of the project.
        bugtracker_url(str): Url to the bug tracker for the issues.

Returns:
        None

Raises:
        RuntimeError: If the release notes could not be retrieved


def record_factory(app, fields=None):
    record = Record(app, {'$type': Record._type, 'isNew': True, 'applicationId': app.id, 'comments': {'$type': 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Collections.Generic.List`1[[Core.Models.Record.Comments, Core]], mscorlib]], mscorlib'}, 'values': {'$type': 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib'}})
    fields = (fields or {})
    for (name, value) in six.iteritems(fields):
        record[name] = value
    copy_raw = copy.copy(record._raw)
    values_dict = {}
    for (key, value) in six.iteritems(copy_raw['values']):
        if (value is not None):
            values_dict[key] = value
    record._raw['values'] = values_dict
    return record

Return a temporary Record instance to be used for field validation and value parsing

Args:
        app (App): Target App to create a transient Record instance for
        fields (dict): Optional dict of fields and values to set on new Record instance before returning

Returns:
        Record: Unsaved Record instance to be used for validation, creation, etc.


def compile_keywords(keywords):
    mdt = []
    cz_keywords = []
    en_keywords = []
    for keyword in keywords:
        keyword = keyword_to_info(keyword.encode('utf-8'))
        if (not keyword):
            continue
        cz_keywords.append({'uid': keyword['uid'], 'zahlavi': keyword['zahlavi'], 'zdroj': 'czenas'})
        if keyword.get('mdt'):
            mdt.append({'mdt': keyword['mdt'], 'mrf': keyword['mrf']})
        angl_ekvivalent = keyword.get('angl_ekvivalent')
        if angl_ekvivalent:
            en_keywords.append({'zahlavi': angl_ekvivalent, 'zdroj': (keyword.get('zdroj_angl_ekvivalentu') or 'eczenas')})
    return (mdt, cz_keywords, en_keywords)

Translate `keywords` to full keyword records as they are used in Aleph.

    Returns tuple with three lists, each of which is later used in different
    part of the MRC/MARC record.

Args:
        keywords (list): List of keyword strings.

Returns:
        tuple: (mdt_list, cz_keyword_list, en_keyword_list)


def are_you_sure(msg=''):
    print(msg)
    from utool import util_arg
    from utool import util_str
    override = util_arg.get_argflag(('--yes', '--y', '-y'))
    if override:
        print('accepting based on command line flag')
        return True
    valid_ans = ['yes', 'y']
    valid_prompt = util_str.conj_phrase(valid_ans, 'or')
    ans = input(('Are you sure?\n Enter %s to accept\n' % valid_prompt))
    return (ans.lower() in valid_ans)

r"""
    Prompts user to accept or checks command line for -y

Args:
        msg (str):

Returns:
        bool: accept or not


def authenticate_device(self, api_token, device_token, email=None, user_url=None, override=False, fetch=True):
    if (self.context.has_auth_params('Gem-Device') and (not override)):
        raise OverrideError('Gem-Device')
    if ((not api_token) or (not device_token) or ((not email) and (not user_url)) or (not self.context.authorize('Gem-Device', api_token=api_token, user_email=email, user_url=user_url, device_token=device_token))):
        raise AuthUsageError(self.context, 'Gem-Device')
    if fetch:
        user = (self.user(email) if email else self.user())
        return user.refresh()
    else:
        return True

Set credentials for Device authentication.

Args:
        api_token (str): Token issued to your Application through the Gem
        Developer Console.
        device_token (str): Physical device identifier. You will receive this
        from a user.devices.create call or from users.create.
        email (str, optional): User's email address, required if user_url is
        not provided.
        user_url (str, optional): User's Gem url.
        override (boolean, optional): Replace existing Application credentials.
        fetch (boolean, optional): Return the authenticated User.

Returns:
        An User object if `fetch` is True.


def _get_snpeff_transcript(self, transcript_info):
    transcript = Transcript(hgnc_symbol=transcript_info.get('Gene_Name'), transcript_id=transcript_info.get('Feature'), ensembl_id=transcript_info.get('Gene_ID'), biotype=transcript_info.get('Transcript_BioType'), consequence=transcript_info.get('Annotation'), exon=transcript_info.get('Rank'), HGVSc=transcript_info.get('HGVS.c'), HGVSp=transcript_info.get('HGVS.p'))
    return transcript

Create a transcript based on the snpeff annotation

Args:
        transcript_info (dict): A dict with snpeff info

Returns:
        transcript (puzzle.models.Transcript): A Transcripts


def argsort2(indexable, key=None, reverse=False):
    if isinstance(indexable, dict):
        vk_iter = ((v, k) for (k, v) in indexable.items())
    else:
        vk_iter = ((v, k) for (k, v) in enumerate(indexable))
    if (key is None):
        indices = [k for (v, k) in sorted(vk_iter, reverse=reverse)]
    else:
        indices = [k for (v, k) in sorted(vk_iter, key=(lambda vk: key(vk[0])), reverse=reverse)]
    return indices

Returns the indices that would sort a indexable object.

    This is similar to np.argsort, but it is written in pure python and works
    on both lists and dictionaries.

Args:
        indexable (list or dict): indexable to sort by

Returns:
        list: indices: list of indices such that sorts the indexable

Example:
        >>> # DISABLE_DOCTEST
        >>> import utool as ut
        >>> # argsort works on dicts
        >>> dict_ = indexable = {'a': 3, 'b': 2, 'c': 100}
        >>> indices = ut.argsort2(indexable)
        >>> assert list(ut.take(dict_, indices)) == sorted(dict_.values())
        >>> # argsort works on lists
        >>> indexable = [100, 2, 432, 10]
        >>> indices = ut.argsort2(indexable)
        >>> assert list(ut.take(indexable, indices)) == sorted(indexable)
        >>> # argsort works on iterators
        >>> indexable = reversed(range(100))
        >>> indices = ut.argsort2(indexable)
        >>> assert indices[0] == 99


def change_extension(self, filepath, new_extension):
    (filename, ext) = os.path.splitext(filepath)
    return '.'.join([filename, new_extension])

Change final filename extension.

Args:
        filepath (str): A file path (relative or absolute).
        new_extension (str): New extension name (without leading dot) to
            apply.

Returns:
        str: Filepath with new extension.


def pauli_from_char(ch, n=0):
    ch = ch.upper()
    if (ch == 'I'):
        return I
    if (ch == 'X'):
        return X(n)
    if (ch == 'Y'):
        return Y(n)
    if (ch == 'Z'):
        return Z(n)
    raise ValueError('ch shall be X, Y, Z or I')

Make Pauli matrix from an character.

Args:
        ch (str): "X" or "Y" or "Z" or "I".
        n (int, optional): Make Pauli matrix as n-th qubits.

Returns:
        If ch is "X" => X, "Y" => Y, "Z" => Z, "I" => I

Raises:
        ValueError: When ch is not "X", "Y", "Z" nor "I".


def read_samples(self, sr=None, offset=0, duration=None):
    read_duration = self.duration
    if ((offset > 0) and (read_duration is not None)):
        read_duration -= offset
    if (duration is not None):
        if (read_duration is None):
            read_duration = duration
        else:
            read_duration = min(duration, read_duration)
    return self.track.read_samples(sr=sr, offset=(self.start + offset), duration=read_duration)

Read the samples of the utterance.

Args:
        sr (int): If None uses the sampling rate given by the track,
                otherwise resamples to the given sampling rate.
        offset (float): Offset in seconds to read samples from.
        duration (float): If not ``None`` read only this
                        number of seconds in maximum.

Returns:
        np.ndarray: A numpy array containing the samples
                    as a floating point (numpy.float32) time series.


def load_extension(self, path, name_filter=None, class_filter=None, unique=False, component=None):
    import_name = None
    if (component is not None):
        import_name = _ensure_package_loaded(path, component)
    (name, ext) = _try_load_module(path, import_name=import_name)
    if ((name_filter is not None) and (name != name_filter)):
        return []
    found = [(name, x) for x in self._filter_subclasses(ext, class_filter)]
    found = [(name, x) for (name, x) in found if self._filter_nonextensions(x)]
    if (not unique):
        return found
    if (len(found) > 1):
        raise ArgumentError(('Extension %s should have had exactly one instance of class %s, found %d' % (path, class_filter.__name__, len(found))), classes=found)
    elif (len(found) == 0):
        raise ArgumentError(('Extension %s had no instances of class %s' % (path, class_filter.__name__)))
    return found[0]

Load a single python module extension.

    This function is similar to using the imp module directly to load a
    module and potentially inspecting the objects it declares to filter
    them by class.

Args:
        path (str): The path to the python file to load
        name_filter (str): If passed, the basename of the module must match
            name or nothing is returned.
        class_filter (type): If passed, only instance of this class are returned.
        unique (bool): If True (default is False), there must be exactly one object
            found inside this extension that matches all of the other criteria.
        component (IOTile): The component that this extension comes from if it is
            loaded from an installed component.  This is used to properly import
            the extension as a submodule of the component's support package.

Returns:
        list of (name, type): A list of the objects found at the extension path.

        If unique is True, then the list only contains a single entry and that
        entry will be directly returned.


def symbolic_master_equation(self, rho=None):
    (L, H) = (self.L, self.H)
    if (rho is None):
        rho = OperatorSymbol('rho', hs=self.space)
    return (((- I) * ((H * rho) - (rho * H))) + sum(((((Lk * rho) * adjoint(Lk)) - ((((adjoint(Lk) * Lk) * rho) + ((rho * adjoint(Lk)) * Lk)) / 2)) for Lk in L.matrix.ravel())))

Compute the symbolic Liouvillian acting on a state rho

    If no rho is given, an OperatorSymbol is created in its place.
    This correspnds to the RHS of the master equation
    in which an average is taken over the external noise degrees of
    freedom.

Args:
        rho (Operator): A symbolic density matrix operator

Returns:
        Operator: The RHS of the master equation.


def _make_instance(self, node_data):
    node_data['from_db'] = self._from_db
    clone = self.__call__(**node_data)
    clone.setattrs(container=self, _is_item=True)
    for name in self._nodes:
        _name = un_camel(name)
        if (_name in node_data):
            getattr(clone, name)._load_data(node_data[_name])
    _key = clone._get_linked_model_key()
    if _key:
        self.node_dict[_key] = clone
    return clone

Create a ListNode instance from node_data

Args:
        node_data (dict): Data to create ListNode item.

Returns:
        ListNode item.


def calcHairpin(seq, mv_conc=50.0, dv_conc=0.0, dntp_conc=0.8, dna_conc=50.0, temp_c=37, max_loop=30):
    _setThermoArgs(**locals())
    return _THERMO_ANALYSIS.calcHairpin(seq).checkExc()

Calculate the hairpin formation thermodynamics of a DNA sequence.

    **Note that the maximum length of `seq` is 60 bp.** This is a cap suggested
    by the Primer3 team as the longest reasonable sequence length for which
    a two-state NN model produces reliable results (see primer3/src/libnano/thal.h:50).

Args:
        seq (str): DNA sequence to analyze for hairpin formation

        mv_conc (float/int, optional): Monovalent cation conc. (mM)
        dv_conc (float/int, optional): Divalent cation conc. (mM)
        dntp_conc (float/int, optional): dNTP conc. (mM)
        dna_conc (float/int, optional): DNA conc. (nM)
        temp_c (int, optional): Simulation temperature for dG (Celsius)
        max_loop(int, optional): Maximum size of loops in the structure

Returns:
        A `ThermoResult` object with thermodynamic characteristics of the
        hairpin formation.

Raises:
        ``RuntimeError``


def _ReadLayoutDataTypeDefinition(self, definitions_registry, definition_values, data_type_definition_class, definition_name, supported_definition_values):
    return self._ReadDataTypeDefinition(definitions_registry, definition_values, data_type_definition_class, definition_name, supported_definition_values)

Reads a layout data type definition.

Args:
        definitions_registry (DataTypeDefinitionsRegistry): data type definitions
        registry.
        definition_values (dict[str, object]): definition values.
        data_type_definition_class (str): data type definition class.
        definition_name (str): name of the definition.
        supported_definition_values (set[str]): names of the supported definition
        values.

Returns:
        LayoutDataTypeDefinition: layout data type definition.

Raises:
        DefinitionReaderError: if the definitions values are missing or if
        the format is incorrect.


def count_matching(self, selector, offset=0):
    if selector.output:
        data = self.streaming_data
    elif selector.buffered:
        data = self.storage_data
    else:
        raise ArgumentError('You can only pass a buffered selector to count_matching', selector=selector)
    count = 0
    for i in range(offset, len(data)):
        reading = data[i]
        stream = DataStream.FromEncoded(reading.stream)
        if selector.matches(stream):
            count += 1
    return count

Count the number of readings matching selector.

Args:
        selector (DataStreamSelector): The selector that we want to
            count matching readings for.
        offset (int): The starting offset that we should begin counting at.

Returns:
        int: The number of matching readings.


def sync_firmware(self):
    serial_no = self.serial_number
    if self.firmware_newer():
        try:
            self.invalidate_firmware()
            self.update_firmware()
        except errors.JLinkException as e:
            pass
        res = self.open(serial_no=serial_no)
        if self.firmware_newer():
            raise errors.JLinkException('Failed to sync firmware version.')
        return res
    elif self.firmware_outdated():
        try:
            self.update_firmware()
        except errors.JLinkException as e:
            pass
        if self.firmware_outdated():
            raise errors.JLinkException('Failed to sync firmware version.')
        return self.open(serial_no=serial_no)
    return None

Syncs the emulator's firmware version and the DLL's firmware.

    This method is useful for ensuring that the firmware running on the
    J-Link matches the firmware supported by the DLL.

Args:
        self (JLink): the ``JLink`` instance

Returns:
        ``None``


def from_serializable(cls, obj):
    if (obj.get('version', {'bqm_schema': '1.0.0'})['bqm_schema'] != '2.0.0'):
        return cls._from_serializable_v1(obj)
    variables = [(tuple(v) if isinstance(v, list) else v) for v in obj['variable_labels']]
    if obj['use_bytes']:
        ldata = bytes2array(obj['linear_biases'])
        qdata = bytes2array(obj['quadratic_biases'])
        irow = bytes2array(obj['quadratic_head'])
        icol = bytes2array(obj['quadratic_tail'])
    else:
        ldata = obj['linear_biases']
        qdata = obj['quadratic_biases']
        irow = obj['quadratic_head']
        icol = obj['quadratic_tail']
    offset = obj['offset']
    vartype = obj['variable_type']
    bqm = cls.from_numpy_vectors(ldata, (irow, icol, qdata), offset, str(vartype), variable_order=variables)
    bqm.info.update(obj['info'])
    return bqm

Deserialize a binary quadratic model.

Args:
        obj (dict):
            A binary quadratic model serialized by :meth:`~.BinaryQuadraticModel.to_serializable`.

Returns:
        :obj:`.BinaryQuadraticModel`

Example:
        Encode and decode using JSON

        >>> import dimod
        >>> import json
        ...
        >>> bqm = dimod.BinaryQuadraticModel({'a': -1.0, 'b': 1.0}, {('a', 'b'): -1.0}, 0.0, dimod.SPIN)
        >>> s = json.dumps(bqm.to_serializable())
        >>> new_bqm = dimod.BinaryQuadraticModel.from_serializable(json.loads(s))

    See also:
        :meth:`~.BinaryQuadraticModel.to_serializable`

        :func:`json.loads`, :func:`json.load` JSON deserialization functions


def supports_card_actions(channel_id: str, button_cnt: int=100) -> bool:
    max_actions = {Channels.facebook: 3, Channels.skype: 3, Channels.ms_teams: 3, Channels.line: 99, Channels.slack: 100, Channels.emulator: 100, Channels.direct_line: 100, Channels.webchat: 100, Channels.cortana: 100}
    return ((button_cnt <= max_actions[channel_id]) if (channel_id in max_actions) else False)

Determine if a number of Card Actions are supported by a Channel.

Args:
        channel_id (str): The Channel to check if the Card Actions are supported in.
        button_cnt (int, optional): Defaults to 100. The number of Card Actions to check for the Channel.

Returns:
        bool: True if the Channel supports the button_cnt total Card Actions, False if the Channel does not support that number of Card Actions.


def make(world_name, gl_version=GL_VERSION.OPENGL4, window_res=None, cam_res=None, verbose=False):
    holodeck_worlds = _get_worlds_map()
    if (world_name not in holodeck_worlds):
        raise HolodeckException('Invalid World Name')
    param_dict = copy(holodeck_worlds[world_name])
    param_dict['start_world'] = True
    param_dict['uuid'] = str(uuid.uuid4())
    param_dict['gl_version'] = gl_version
    param_dict['verbose'] = verbose
    if (window_res is not None):
        param_dict['window_width'] = window_res[0]
        param_dict['window_height'] = window_res[1]
    if (cam_res is not None):
        param_dict['camera_width'] = cam_res[0]
        param_dict['camera_height'] = cam_res[1]
    return HolodeckEnvironment(**param_dict)

Creates a holodeck environment using the supplied world name.

Args:
        world_name (str): The name of the world to load as an environment. Must match the name of a world in an
            installed package.
        gl_version (int, optional): The OpenGL version to use (Linux only). Defaults to GL_VERSION.OPENGL4.
        window_res ((int, int), optional): The resolution to load the game window at. Defaults to (512, 512).
        cam_res ((int, int), optional): The resolution to load the pixel camera sensors at. Defaults to (256, 256).
        verbose (bool): Whether to run in verbose mode. Defaults to False.

Returns:
        HolodeckEnvironment: A holodeck environment instantiated with all the settings necessary for the specified
            world, and other supplied arguments.


def halted(self):
    result = int(self._dll.JLINKARM_IsHalted())
    if (result < 0):
        raise errors.JLinkException(result)
    return (result > 0)

Returns whether the CPU core was halted.

Args:
        self (JLink): the ``JLink`` instance

Returns:
        ``True`` if the CPU core is halted, otherwise ``False``.

Raises:
        JLinkException: on device errors.


def series_with_permutation(self, other):
    combined_permutation = tuple([self.permutation[p] for p in other.permutation])
    return CPermutation.create(combined_permutation)

Compute the series product with another channel permutation circuit

Args:
        other (CPermutation):

Returns:
        Circuit: The composite permutation circuit (could also be the
            identity circuit for n channels)


def _ParsePropertiesXMLFile(self, xml_data):
    xml_root = ElementTree.fromstring(xml_data)
    properties = {}
    for xml_element in xml_root.iter():
        if (not xml_element.text):
            continue
        (_, _, name) = xml_element.tag.partition('}')
        if (name == 'lpstr'):
            continue
        property_name = self._PROPERTY_NAMES.get(name, None)
        if (not property_name):
            property_name = self._FormatPropertyName(name)
        properties[property_name] = xml_element.text
    return properties

Parses a properties XML file.

Args:
        xml_data (bytes): data of a _rels/.rels XML file.

Returns:
        dict[str, object]: properties.

Raises:
        zipfile.BadZipfile: if the properties XML file cannot be read.


def create_or_update_video_transcript(video_id, language_code, metadata, file_data=None):
    metadata = {prop: value for (prop, value) in six.iteritems(metadata) if ((prop in ['provider', 'language_code', 'file_name', 'file_format']) and value)}
    file_format = metadata.get('file_format')
    if (file_format and (file_format not in list(dict(TranscriptFormat.CHOICES).keys()))):
        raise InvalidTranscriptFormat('{} transcript format is not supported'.format(file_format))
    provider = metadata.get('provider')
    if (provider and (provider not in list(dict(TranscriptProviderType.CHOICES).keys()))):
        raise InvalidTranscriptProvider('{} transcript provider is not supported'.format(provider))
    try:
        video = Video.objects.get(edx_video_id=video_id)
        (video_transcript, __) = VideoTranscript.create_or_update(video, language_code, metadata, file_data)
    except Video.DoesNotExist:
        return None
    return video_transcript.url()

Create or Update video transcript for an existing video.

Args:
        video_id: it can be an edx_video_id or an external_id extracted from external sources in a video component.
        language_code: language code of a video transcript
        metadata (dict): A dict containing (to be overwritten) properties
        file_data (InMemoryUploadedFile): Transcript data to be saved for a course video.

Returns:
        video transcript url


def _init_project_service(self, version):
    project_cfg = self._load_config_section(CONFIG_PROJECT_SECTION)
    self._token_project = project_cfg[CONFIG_TOKEN]
    proto = project_cfg[CONFIG_PROTOCOL]
    host = project_cfg[CONFIG_HOST]
    self._project = ProjectService(host, version)
    self._project.base_protocol = proto
    self._project.set_auth(self._token_project)

Method to initialize the Project Service from the config data

Args:
        version (string): Version of Boss API to use.

Returns:
        None

Raises:
        (KeyError): if given invalid version.


def delete_keyvault_secret(access_token, vault_uri, secret_name):
    endpoint = ''.join([vault_uri, '/secrets/', secret_name, '?api-version=', '7.0'])
    return do_delete(endpoint, access_token)

Deletes a secret from a key vault using the key vault URI.

Args:
        access_token (str): A valid Azure authentication token.
        vault_uri (str): Vault URI e.g. https://myvault.azure.net.
        secret_name (str): Name of the secret to add.

Returns:
        HTTP response. 200 OK.


def set_description(self, name, value=None, default=False, disable=False):
    string = 'description'
    commands = self.command_builder(string, value=value, default=default, disable=disable)
    return self.configure_interface(name, commands)

Configures the interface description

    EosVersion:
        4.13.7M

Args:
        name (string): The interface identifier.  It must be a full
            interface name (ie Ethernet, not Et)
        value (string): The value to set the description to.
        default (boolean): Specifies to default the interface description
        disable (boolean): Specifies to negate the interface description

Returns:
        True if the operation succeeds otherwise False


def _remove_hdxobject(self, objlist, obj, matchon='id', delete=False):
    if (objlist is None):
        return False
    if isinstance(obj, six.string_types):
        obj_id = obj
    elif (isinstance(obj, dict) or isinstance(obj, HDXObject)):
        obj_id = obj.get(matchon)
    else:
        raise HDXError('Type of object not a string, dict or T<=HDXObject')
    if (not obj_id):
        return False
    for (i, objdata) in enumerate(objlist):
        objid = objdata.get(matchon)
        if (objid and (objid == obj_id)):
            if delete:
                objlist[i].delete_from_hdx()
            del objlist[i]
            return True
    return False

Remove an HDX object from a list within the parent HDX object

Args:
        objlist (List[Union[T <= HDXObject,Dict]]): list of HDX objects
        obj (Union[T <= HDXObject,Dict,str]): Either an id or hdx object metadata either from an HDX object or a dictionary
        matchon (str): Field to match on. Defaults to id.
        delete (bool): Whether to delete HDX object. Defaults to False.

Returns:
        bool: True if object removed, False if not


def _row_from_mapping(mapping, schema):
    if (len(schema) == 0):
        raise ValueError(_TABLE_HAS_NO_SCHEMA)
    row = []
    for field in schema:
        if (field.mode == 'REQUIRED'):
            row.append(mapping[field.name])
        elif (field.mode == 'REPEATED'):
            row.append(mapping.get(field.name, ()))
        elif (field.mode == 'NULLABLE'):
            row.append(mapping.get(field.name))
        else:
            raise ValueError('Unknown field mode: {}'.format(field.mode))
    return tuple(row)

Convert a mapping to a row tuple using the schema.

Args:
        mapping (Dict[str, object])
            Mapping of row data: must contain keys for all required fields in
            the schema. Keys which do not correspond to a field in the schema
            are ignored.
        schema (List[google.cloud.bigquery.schema.SchemaField]):
            The schema of the table destination for the rows

Returns:
        Tuple[object]:
            Tuple whose elements are ordered according to the schema.

Raises:
        ValueError: If schema is empty.


def find_pad_index(self, array):
    try:
        return list(array).index(self.pad_value)
    except ValueError:
        return len(array)

Find padding index.

Args:
        array (list): integer list.

Returns:
        idx: padding index.

Example:
        >>> array = [1, 2, 0]
        >>> self.find_pad_index(array)
        2


def decode_jwt_payload(self, access_token=None):
    c = self.get_credentials()
    jwt = (access_token or c.access_token)
    try:
        (_, payload, _) = jwt.split('.')
        rem = (len(payload) % 4)
        if (rem > 0):
            payload += ('=' * (4 - rem))
        try:
            decoded_jwt = b64decode(payload).decode('utf-8')
        except TypeError as e:
            raise PanCloudError(('Failed to base64 decode JWT: %s' % e))
        else:
            try:
                x = loads(decoded_jwt)
            except ValueError as e:
                raise PanCloudError(('Invalid JSON: %s' % e))
    except (AttributeError, ValueError) as e:
        raise PanCloudError(('Invalid JWT: %s' % e))
    return x

Extract payload field from JWT.

Args:
        access_token (str): Access token to decode. Defaults to ``None``.

Returns:
        dict: JSON object that contains the claims conveyed by the JWT.


def CheckTaskToMerge(self, task):
    with self._lock:
        is_abandoned = (task.identifier in self._tasks_abandoned)
        is_processing = (task.identifier in self._tasks_processing)
        is_queued = (task.identifier in self._tasks_queued)
        if ((not is_queued) and (not is_processing) and (not is_abandoned)):
            raise KeyError('Status of task {0:s} is unknown.'.format(task.identifier))
        return (is_queued or is_processing or (is_abandoned and (not task.has_retry)))

Checks if the task should be merged.

Args:
        task (Task): task.

Returns:
        bool: True if the task should be merged.

Raises:
        KeyError: if the task was not queued, processing or abandoned.


def get_image_data(self, ids=None, voxels=None, dense=True):
    if (dense and (ids is None) and (voxels is None)):
        logger.warning('Warning: get_image_data() is being called without specifying a subset of studies or voxels to retrieve. This may result in a very large amount of data (several GB) being read into memory. If you experience any problems, consider returning a sparse matrix by passing dense=False, or pass in a list of ids of voxels to retrieve only a portion of the data.')
    result = self.data
    if (ids is not None):
        idxs = np.where(np.in1d(np.array(self.ids), np.array(ids)))[0]
        result = result[(:, idxs)]
    if (voxels is not None):
        result = result[(voxels, :)]
    return (result.toarray() if dense else result)

Slices and returns a subset of image data.

Args:
        ids (list, array): A list or 1D numpy array of study ids to
            return. If None, returns data for all studies.
        voxels (list, array): A list or 1D numpy array of voxel indices
            (i.e., rows) to return. If None, returns data for all voxels.
        dense (bool): Optional boolean. When True (default), convert the
            result to a dense array before returning. When False, keep as
            sparse matrix.

Returns:
        A 2D numpy array with voxels in rows and studies in columns.


def _brentq_cdf(self, value):
    bound_cdf = partial(scalarize(GaussianKDE.cumulative_distribution), self)

    def f(x):
        return (bound_cdf(x) - value)
    return f

Helper function to compute percent_point.

    As scipy.stats.gaussian_kde doesn't provide this functionality out of the box we need
    to make a numerical approach:

    - First we scalarize and bound cumulative_distribution.
    - Then we define a function `f(x) = cdf(x) - value`, where value is the given argument.
    - As value will be called from ppf we can assume value = cdf(z) for some z that is the
    value we are searching for. Therefore the zeros of the function will be x such that:
    cdf(x) - cdf(z) = 0 => (becasue cdf is monotonous and continous) x = z

Args:
        value(float): cdf value, that is, in [0,1]

Returns:
        callable: function whose zero is the ppf of value.


def _cart_dists(self, s1, s2, avg_lattice, mask, normalization, lll_frac_tol=None):
    if (len(s2) > len(s1)):
        raise ValueError('s1 must be larger than s2')
    if (mask.shape != (len(s2), len(s1))):
        raise ValueError('mask has incorrect shape')
    (vecs, d_2) = pbc_shortest_vectors(avg_lattice, s2, s1, mask, return_d2=True, lll_frac_tol=lll_frac_tol)
    lin = LinearAssignment(d_2)
    s = lin.solution
    short_vecs = vecs[(np.arange(len(s)), s)]
    translation = np.average(short_vecs, axis=0)
    f_translation = avg_lattice.get_fractional_coords(translation)
    new_d2 = np.sum(((short_vecs - translation) ** 2), axis=(- 1))
    return (((new_d2 ** 0.5) * normalization), f_translation, s)

Finds a matching in cartesian space. Finds an additional
    fractional translation vector to minimize RMS distance

Args:
        s1, s2: numpy arrays of fractional coordinates. len(s1) >= len(s2)
        avg_lattice: Lattice on which to calculate distances
        mask: numpy array of booleans. mask[i, j] = True indicates
            that s2[i] cannot be matched to s1[j]
        normalization (float): inverse normalization length

Returns:
        Distances from s2 to s1, normalized by (V/Natom) ^ 1/3
        Fractional translation vector to apply to s2.
        Mapping from s1 to s2, i.e. with numpy slicing, s1[mapping] => s2


def solve_sweep_wavelength(self, structure, wavelengths, filename='wavelength_n_effs.dat', plot=True):
    n_effs = []
    for w in tqdm.tqdm(wavelengths, ncols=70):
        structure.change_wavelength(w)
        self.solve(structure)
        n_effs.append(np.real(self.n_effs))
    if filename:
        self._write_n_effs_to_file(n_effs, (self._modes_directory + filename), wavelengths)
        if plot:
            if MPL:
                title = '$n_{eff}$ vs Wavelength'
                y_label = '$n_{eff}$'
            else:
                title = ('n_{effs} vs Wavelength' % x_label)
                y_label = 'n_{eff}'
            self._plot_n_effs((self._modes_directory + filename), (self._modes_directory + 'fraction_te.dat'), 'Wavelength', 'n_{eff}', title)
    return n_effs

Solve for the effective indices of a fixed structure at
    different wavelengths.

Args:
        structure (Slabs): The target structure to solve
            for modes.
        wavelengths (list): A list of wavelengths to sweep
            over.
        filename (str): The nominal filename to use when saving the
            effective indices.  Defaults to 'wavelength_n_effs.dat'.
        plot (bool): `True` if plots should be generates,
            otherwise `False`.  Default is `True`.

Returns:
        list: A list of the effective indices found for each wavelength.


def extract(self, text: str) -> List[Extraction]:
    doc = self._parser(text)
    extractions = list()
    for sent in doc.sents:
        this_extraction = Extraction(value=sent.text, extractor_name=self.name, start_token=sent[0], end_token=sent[(- 1)], start_char=sent.text[0], end_char=sent.text[(- 1)])
        extractions.append(this_extraction)
    return extractions

Splits text by sentences.

Args:
        text (str): Input text to be extracted.

Returns:
        List[Extraction]: the list of extraction or the empty list if there are no matches.


def unembed_samples(samples, embedding, chain_break_method=None):
    if (chain_break_method is None):
        chain_break_method = majority_vote
    return list(itertools.chain(*(chain_break_method(sample, embedding) for sample in samples)))

Return samples over the variables in the source graph.

Args:
        samples (iterable): An iterable of samples where each sample
            is a dict of the form {v: val, ...} where v is a variable
            in the target model and val is the associated value as
            determined by a binary quadratic model sampler.
        embedding (dict): The mapping from the source graph to the target graph.
            Should be of the form {v: {s, ...}, ...} where v is a node in the
            source graph and s is a node in the target graph.
        chain_break_method (function, optional): The method used to resolve chain
            breaks. Default is :method:`majority_vote`.

Returns:
        list: A list of unembedded samples. Each sample is a dict of the form
        {v: val, ...} where v is a variable in the source graph and val
        is the value associated with the variable.


def get_attributes(self, uuid=None, attribute_names=None):
    batch_item = self._build_get_attributes_batch_item(uuid, attribute_names)
    request = self._build_request_message(None, [batch_item])
    response = self._send_and_receive_message(request)
    results = self._process_batch_items(response)
    return results[0]

Send a GetAttributes request to the server.

Args:
        uuid (string): The ID of the managed object with which the
            retrieved attributes should be associated. Optional, defaults
            to None.
        attribute_names (list): A list of AttributeName values indicating
            what object attributes the client wants from the server.
            Optional, defaults to None.

Returns:
        result (GetAttributesResult): A structure containing the results
            of the operation.


def expand_abbreviations(self, text):
    if (not self.abbreviations):
        raise LexiconError('No abbreviations in lexicon.')

    def chunks(data, SIZE=25):
        '\n            Regex only supports 100 groups for munging callbacks. So we have to\n            chunk the abbreviation dicitonary.\n            '
        it = iter(data)
        for i in range(0, len(data), SIZE):
            (yield {k: data[k] for k in islice(it, SIZE)})

    def cb(g):
        'Regex callback'
        return (self.abbreviations.get(g.group(0)) or g.group(0))
    text = re.sub('w/', 'wi', text)
    for subdict in chunks(self.abbreviations):
        regex = (('(\\b' + '\\b)|(\\b'.join(subdict.keys())) + '\\b)')
        text = re.sub(regex, cb, text)
    return text

Parse a piece of text and replace any abbreviations with their full
    word equivalents. Uses the lexicon.abbreviations dictionary to find
    abbreviations.

Args:
        text (str): The text to parse.

Returns:
        str: The text with abbreviations replaced.


def list_container_instance_groups_sub(access_token, subscription_id):
    endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/providers/Microsoft.ContainerInstance/ContainerGroups', '?api-version=', CONTAINER_API])
    return do_get(endpoint, access_token)

List the container groups in a subscription.

Args:
        access_token (str): A valid Azure authentication token.
        subscription_id (str): Azure subscription id.

Returns:
        HTTP response. JSON list of container groups and their properties.


def _ValidateCacheEntryHeader(self, cache_entry_header):
    return ((cache_entry_header.request_size > 0) and (cache_entry_header.request_size < self._MAXIMUM_URL_LENGTH) and (cache_entry_header.major_format_version == 1) and (cache_entry_header.last_fetched_time > 0) and (cache_entry_header.fetch_count > 0))

Determines whether the values in the cache entry header are valid.

Args:
        cache_entry_header (firefox_cache1_entry_header): cache entry header.

Returns:
        bool: True if the cache entry header is valid.


def search(self, query_string):
    query = self.create_query()
    parser = QueryParser(query_string, query)
    parser.parse()
    return self.query(query)

Performs a search against the index using lunr query syntax.

    Results will be returned sorted by their score, the most relevant
    results will be returned first.

    For more programmatic querying use `lunr.Index.query`.

Args:
        query_string (str): A string to parse into a Query.

Returns:
        dict: Results of executing the query.


def _parse_flowcontrol_receive(self, config):
    value = 'off'
    match = re.search('flowcontrol receive (\\w+)$', config, re.M)
    if match:
        value = match.group(1)
    return dict(flowcontrol_receive=value)

Scans the config block and returns the flowcontrol receive value

Args:
        config (str): The interface config block to scan

Returns:
        dict: Returns a dict object with the flowcontrol receive value
            retrieved from the config block.  The returned dict object
            is intended to be merged into the interface resource dict


def get_course_track_selection_url(course_run, query_parameters):
    try:
        course_root = reverse('course_modes_choose', kwargs={'course_id': course_run['key']})
    except KeyError:
        LOGGER.exception('KeyError while parsing course run data.\nCourse Run: \n[%s]', course_run)
        raise
    url = '{}{}'.format(settings.LMS_ROOT_URL, course_root)
    course_run_url = update_query_parameters(url, query_parameters)
    return course_run_url

Return track selection url for the given course.

Args:
        course_run (dict): A dictionary containing course run metadata.
        query_parameters (dict): A dictionary containing query parameters to be added to course selection url.

Raises:
        (KeyError): Raised when course run dict does not have 'key' key.

Returns:
        (str): Course track selection url.


def GetVolumeByIndex(self, volume_index):
    if (not self._is_parsed):
        self._Parse()
        self._is_parsed = True
    if ((volume_index < 0) or (volume_index >= len(self._volume_identifiers))):
        return None
    volume_identifier = self._volume_identifiers[volume_index]
    return self._volumes[volume_identifier]

Retrieves a specific volume based on the index.

Args:
        volume_index (int): index of the volume.

Returns:
        Volume: a volume or None if not available.


def get_all(self, callsign, timestamp=timestamp_now):
    callsign_data = self._lookup_callsign(callsign, timestamp)
    try:
        cqz = self._lookuplib.lookup_zone_exception(callsign, timestamp)
        callsign_data[const.CQZ] = cqz
    except KeyError:
        pass
    return callsign_data

Lookup a callsign and return all data available from the underlying database

Args:
        callsign (str): Amateur Radio callsign
        timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC)

Returns:
        dict: Dictionary containing the callsign specific data

Raises:
        KeyError: Callsign could not be identified

Example:
        The following code returns all available information from the country-files.com database for the
        callsign "DH1TW"

        >>> from pyhamtools import LookupLib, Callinfo
        >>> my_lookuplib = LookupLib(lookuptype="countryfile")
        >>> cic = Callinfo(my_lookuplib)
        >>> cic.get_all("DH1TW")
        {
            'country': 'Fed. Rep. of Germany',
            'adif': 230,
            'continent': 'EU',
            'latitude': 51.0,
            'longitude': -10.0,
            'cqz': 14,
            'ituz': 28
        }

Note:
        The content of the returned data depends entirely on the injected
        :py:class:`LookupLib` (and the used database). While the country-files.com provides
        for example the ITU Zone, Clublog doesn't. Consequently, the item "ituz"
        would be missing with Clublog (API or XML) :py:class:`LookupLib`.


def snapped_slice(size, frac, n):
    if (size < n):
        n = size
    start = (int(((size * frac) - ceil((n / 2)))) + 1)
    stop = (int(((size * frac) + floor((n / 2)))) + 1)
    buf = 0
    if (stop >= size):
        buf = (size - stop)
    elif (start < 0):
        buf = (0 - start)
    stop += buf
    start += buf
    assert (stop <= size), ('out of bounds [%r, %r]' % (stop, start))
    sl = slice(start, stop)
    return sl

r"""
    Creates a slice spanning `n` items in a list of length `size` at position
    `frac`.

Args:
        size (int): length of the list
        frac (float): position in the range [0, 1]
        n (int): number of items in the slice

Returns:
        slice: slice object that best fits the criteria

    SeeAlso:
        take_percentile_parts

Example:
    Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_list import *  # NOQA
        >>> import utool as ut
        >>> print(snapped_slice(0, 0, 10))
        >>> print(snapped_slice(1, 0, 10))
        >>> print(snapped_slice(100, 0, 10))
        >>> print(snapped_slice(9, 0, 10))
        >>> print(snapped_slice(100, 1, 10))
        pass


def from_file(cls, fp, is_outlook=False):
    log.debug('Parsing email from file {!r}'.format(fp))
    with ported_open(fp) as f:
        message = email.message_from_file(f)
    if is_outlook:
        log.debug('Removing temp converted Outlook email {!r}'.format(fp))
        os.remove(fp)
    return cls(message)

Init a new object from a file path.

Args:
        fp (string): file path of raw email
        is_outlook (boolean): if True is an Outlook email

Returns:
        Instance of MailParser


def _build_endpoint(self, endpoint_name):
    endpoint_relative = settings.get('asmaster_endpoints', endpoint_name)
    return ('%s%s' % (self.host, endpoint_relative))

Generate an enpoint url from a setting name.

Args:
        endpoint_name(str): setting name for the enpoint to build

Returns:
        (str) url enpoint


def estimate_mutual_information(x, y):
    xy = np.concatenate((x, y), axis=1)
    epsilon = _calculate_epsilon(xy)
    h_x = estimate_entropy(x, epsilon)
    h_y = estimate_entropy(y, epsilon)
    h_xy = estimate_entropy(xy, epsilon)
    return max(0, ((h_x + h_y) - h_xy))

Estimate the mutual information of two datasets.

    Mutual information is a measure of dependence between
    two datasets and is calculated as:

        $I(x;y) = H(x) + H(y) - H(x,y)$

    Where H(x) is the Shannon entropy of x. For continuous datasets,
    adapts the Kraskov Estimator [1] for mutual information.

Args:
        x (array-like): An array with shape (n_samples, n_features_x)
        y (array-like): An array with shape (n_samples, n_features_y)

Returns:
        float: A floating point number representing the mutual
            information of x and y. This calculation is *exact*
            for entirely discrete datasets and *approximate* if
            there are continuous columns present.

    References:

    .. [1] A. Kraskov, H. Stogbauer and P. Grassberger, "Estimating mutual
        information". Phys. Rev. E 69, 2004.