def saturate_cast(value, dtype, name=None):
    with ops.name_scope(name, 'saturate_cast', [value]) as name:
        value = ops.convert_to_tensor(value, name='value')
        dtype = dtypes.as_dtype(dtype).base_dtype
        in_dtype = value.dtype
        if in_dtype.is_complex:
            if dtype.is_complex:
                real_in_dtype = in_dtype.real_dtype
                real_out_dtype = dtype.real_dtype
                if real_in_dtype.min < real_out_dtype.min or real_in_dtype.max > real_out_dtype.max:
                    value = gen_math_ops._clip_by_value(value, ops.convert_to_tensor(builtins.complex(real_out_dtype.min, real_out_dtype.min), dtype=in_dtype), ops.convert_to_tensor(builtins.complex(real_out_dtype.max, real_out_dtype.max), dtype=in_dtype), name='clamp')
                return cast(value, dtype, name=name)
            else:
                value = real(value)
                logging.warn('Casting complex to real discards imaginary part.')
                in_dtype = in_dtype.real_dtype
        out_real_dtype = dtype.real_dtype
        if forward_compat.forward_compatible(2024, 11, 1) or in_dtype.min < out_real_dtype.min or in_dtype.max > out_real_dtype.max:
            np_dtype = in_dtype.as_numpy_dtype
            try:
                promoted_type = np.promote_types(np_dtype, out_real_dtype.as_numpy_dtype)
            except TypeError:
                promoted_type = float
            min_limit = np_dtype(np.maximum(in_dtype.min, out_real_dtype.min))
            promoted = np.array([min_limit, out_real_dtype.min], dtype=promoted_type)
            if promoted[0] < promoted[1]:
                min_limit = np.nextafter(min_limit, np_dtype(0), dtype=np_dtype)
            max_limit = np_dtype(np.minimum(in_dtype.max, out_real_dtype.max))
            promoted = np.array([max_limit, out_real_dtype.max], dtype=promoted_type)
            if promoted[0] > promoted[1]:
                max_limit = np.nextafter(max_limit, np_dtype(0), dtype=np_dtype)
            value = gen_math_ops._clip_by_value(value, ops.convert_to_tensor(min_limit, dtype=in_dtype), ops.convert_to_tensor(max_limit, dtype=in_dtype), name='clamp')
        return cast(value, dtype, name=name)

Performs a safe saturating cast of `value` to `dtype`.

This function casts the input to `dtype` without overflow.  If
there is a danger that values would over or underflow in the cast, this op
applies the appropriate clamping before the cast.  See `tf.cast` for more
details.

Args:
value: A `Tensor`.
dtype: The desired output `DType`.
name: A name for the operation (optional).

Returns:
`value` safely cast to `dtype`.


def leak(self):
    (capacity, last_leak) = self.storage.mget(self.key_amount, self.key_last_leak, coherent=True)
    now = time.time()
    if last_leak:
        elapsed = (now - last_leak)
        decrement = (elapsed * self.rate)
        new_capacity = max(int((capacity - decrement)), 0)
    else:
        new_capacity = 0
    self.storage.mset({self.key_amount: new_capacity, self.key_last_leak: now})
    return new_capacity

Leak the adequate amount of data from the bucket.

This should be called before any consumption takes place.

Returns:
int: the new capacity of the bucket


def _validate_measure_sampling(self, experiment):
    if (self._shots <= 1):
        self._sample_measure = False
        return
    if hasattr(experiment.config, 'allows_measure_sampling'):
        self._sample_measure = experiment.config.allows_measure_sampling
    else:
        measure_flag = False
        for instruction in experiment.instructions:
            if (instruction.name == 'reset'):
                self._sample_measure = False
                return
            if measure_flag:
                if (instruction.name not in ['measure', 'barrier', 'id', 'u0']):
                    self._sample_measure = False
                    return
            elif (instruction.name == 'measure'):
                measure_flag = True
        self._sample_measure = True

Determine if measure sampling is allowed for an experiment

Args:
experiment (QobjExperiment): a qobj experiment.


def process_openxml_file(filename: str, print_good: bool, delete_if_bad: bool) -> None:
    print_bad = (not print_good)
    try:
        file_good = is_openxml_good(filename)
        file_bad = (not file_good)
        if ((print_good and file_good) or (print_bad and file_bad)):
            print(filename)
        if (delete_if_bad and file_bad):
            log.warning('Deleting: {}', filename)
            os.remove(filename)
    except Exception as e:
        log.critical('Uncaught error in subprocess: {!r}\n{}', e, traceback.format_exc())
        raise

Prints the filename of, or deletes, an OpenXML file depending on whether
it is corrupt or not.

Args:
filename: filename to check
print_good: if ``True``, then prints the filename if the file
appears good.
delete_if_bad: if ``True``, then deletes the file if the file
appears corrupt.


def random_masking(self, sequence, noise=None):
    batch_size, seq_length, dim = sequence.shape
    len_keep = int(seq_length * (1 - self.config.mask_ratio))
    if noise is None:
        noise = torch.rand(batch_size, seq_length, device=sequence.device)
    ids_shuffle = torch.argsort(noise, dim=1).to(sequence.device)
    ids_restore = torch.argsort(ids_shuffle, dim=1).to(sequence.device)
    ids_keep = ids_shuffle[:, :len_keep]
    sequence_unmasked = torch.gather(sequence, dim=1, index=ids_keep.unsqueeze(-1).repeat(1, 1, dim))
    mask = torch.ones([batch_size, seq_length], device=sequence.device)
    mask[:, :len_keep] = 0
    mask = torch.gather(mask, dim=1, index=ids_restore)
    return (sequence_unmasked, mask, ids_restore)

Perform per-sample random masking by per-sample shuffling. Per-sample shuffling is done by argsort random
noise.

Args:
sequence (`torch.LongTensor` of shape `(batch_size, sequence_length, dim)`)
noise (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*) which is
mainly used for testing purposes to control randomness and maintain the reproducibility


def verified(self, institute_id):
    query = {'verb': 'validate', 'institute': institute_id}
    res = []
    validate_events = self.event_collection.find(query)
    for validated in list(validate_events):
        case_id = validated['case']
        var_obj = self.variant(case_id=case_id, document_id=validated['variant_id'])
        case_obj = self.case(case_id=case_id)
        if ((not case_obj) or (not var_obj)):
            continue
        var_obj['case_obj'] = {'display_name': case_obj['display_name'], 'individuals': case_obj['individuals']}
        res.append(var_obj)
    return res

Return all verified variants for a given institute

Args:
institute_id(str): institute id

Returns:
res(list): a list with validated variants


def add_router(self, path, router):

        if self.strict_router_check and not isinstance(router, Router):
            raise TypeError("Expected object of type Router, found %r" % type(router))

        log.info("{} Adding router {} on path {}", id(self), router, path)
        self.middleware.add(path=path,
                            func=router,
                            method_mask=HTTPMethod.ALL,)

Adds a router to the list of routers

Args:
path (str or regex): The path on which the router binds
router (growler.Router): The router which will respond to
requests

Raises:
TypeError: If `strict_router_check` attribute is True and
the router is not an instance of growler.Router.


def get_metadata_attribute(self, metaname):
    metadata_value = self.metadata.get(metaname, None)
    if (metadata_value is None):
        raise NoMetadataException(('No metadata attribute named %s' % metaname))
    if (not isinstance(metadata_value, list)):
        raise TypeError('Metadata is not a list and it should be.')
    if (len(metadata_value) > 1):
        return metadata_value
    else:
        return metadata_value[0]

Get the metadata attribute by the name.

Args:
metaname (:obj:`str`): Name of the attribute

Returns:
:obj:`list` or :obj:`str`: Value(s) of the requested metadata
attribute

Raises:
NoMetadataException: Attribute error
TypeError: Metadata should be a list


def OpenFileEntry(cls, path_spec_object, resolver_context=None):
    file_system = cls.OpenFileSystem(path_spec_object, resolver_context=resolver_context)
    if (resolver_context is None):
        resolver_context = cls._resolver_context
    file_entry = file_system.GetFileEntryByPathSpec(path_spec_object)
    resolver_context.ReleaseFileSystem(file_system)
    return file_entry

Opens a file entry object defined by path specification.

Args:
path_spec_object (PathSpec): path specification.
resolver_context (Optional[Context]): resolver context, where None
represents the built in context which is not multi process safe.

Returns:
FileEntry: file entry or None if the path specification could not be
resolved.


def _parse_exchange_token_response(content):

    resp = {}
    content = _helpers._from_bytes(content)
    try:
        resp = json.loads(content)
    except Exception:

        resp = _helpers.parse_unique_urlencoded(content)

    if resp and 'expires' in resp:
        resp['expires_in'] = resp.pop('expires')

    return resp

Parses response of an exchange token request.

Most providers return JSON but some (e.g. Facebook) return a
url-encoded string.

Args:
content: The body of a response

Returns:
Content as a dictionary object. Note that the dict could be empty,
i.e. {}. That basically indicates a failure.


def secondary_training_status_message(job_description, prev_description):

    if job_description is None or job_description.get('SecondaryStatusTransitions') is None\
            or len(job_description.get('SecondaryStatusTransitions')) == 0:
        return ''

    prev_description_secondary_transitions = prev_description.get('SecondaryStatusTransitions')\
        if prev_description is not None else None
    prev_transitions_num = len(prev_description['SecondaryStatusTransitions'])\
        if prev_description_secondary_transitions is not None else 0
    current_transitions = job_description['SecondaryStatusTransitions']

    if len(current_transitions) == prev_transitions_num:

        transitions_to_print = current_transitions[-1:]
    else:

        transitions_to_print = current_transitions[prev_transitions_num - len(current_transitions):]

    status_strs = []
    for transition in transitions_to_print:
        message = transition['StatusMessage']
        time_str = datetime.utcfromtimestamp(
            time.mktime(job_description['LastModifiedTime'].timetuple())).strftime('%Y-%m-%d %H:%M:%S')
        status_strs.append('{} {} - {}'.format(time_str, transition['Status'], message))

    return '\n'.join(status_strs)

Returns a string contains last modified time and the secondary training job status message.

Args:
job_description: Returned response from DescribeTrainingJob call
prev_description: Previous job description from DescribeTrainingJob call

Returns:
str: Job status string to be printed.


def set_requestable(self, requestable=True):

        self.data['is_requestdata_type'] = requestable
        if requestable:
            self.data['private'] = False

Set the dataset to be of type requestable or not

Args:
requestable (bool): Set whether dataset is requestable. Defaults to True.

Returns:
None


def make_initial_frame_chooser(real_env, frame_stack_size, simulation_random_starts, simulation_flip_first_random_for_beginning, split=tf.estimator.ModeKeys.TRAIN):
    initial_frame_rollouts = real_env.current_epoch_rollouts(split=split, minimal_rollout_frames=frame_stack_size)

    def initial_frame_chooser(batch_size):
        'Frame chooser.'
        deterministic_initial_frames = initial_frame_rollouts[0][:frame_stack_size]
        if (not simulation_random_starts):
            initial_frames = ([deterministic_initial_frames] * batch_size)
        else:
            initial_frames = random_rollout_subsequences(initial_frame_rollouts, batch_size, frame_stack_size)
            if simulation_flip_first_random_for_beginning:
                initial_frames[0] = deterministic_initial_frames
        return np.stack([[frame.observation.decode() for frame in initial_frame_stack] for initial_frame_stack in initial_frames])
    return initial_frame_chooser

Make frame chooser.

Args:
real_env: T2TEnv to take initial frames from.
frame_stack_size (int): Number of consecutive frames to extract.
simulation_random_starts (bool): Whether to choose frames at random.
simulation_flip_first_random_for_beginning (bool): Whether to flip the first
frame stack in every batch for the frames at the beginning.
split (tf.estimator.ModeKeys or None): Data split to take the frames from,
None means use all frames.

Returns:
Function batch_size -> initial_frames.


def conjugate(self):
    return self.__class__(scalar=self.scalar, vector=(- self.vector))

Quaternion conjugate, encapsulated in a new instance.

For a unit quaternion, this is the same as the inverse.

Returns:
A new Quaternion object clone with its vector part negated


def requestMapIdentity(self, subject, vendorSpecific=None):

        response = self.requestMapIdentityResponse(subject, vendorSpecific)
        return self._read_boolean_response(response)

See Also: requestMapIdentityResponse()

Args:
subject:
vendorSpecific:

Returns:


def run_config_diagnostics(config_path=CONFIG_PATH):

    config = read_config(config_path)
    missing_sections = set()
    malformed_entries = defaultdict(set)
    for section, expected_section_keys in SECTION_KEYS.items():
        section_content = config.get(section)
        if not section_content:
            missing_sections.add(section)
        else:
            for option in expected_section_keys:
                option_value = section_content.get(option)
                if not option_value:
                    malformed_entries[section].add(option)
    return config_path, missing_sections, malformed_entries

Run diagnostics on the configuration file.

Args:
config_path (str): Path to the configuration file.
Returns:
str, Set[str], dict(str, Set[str]): The path to the configuration file, a set of missing
sections and a dict that maps each section to the entries that have either missing or empty
options.


def save_data_files(bs, prefix=None, directory=None):
    filename = 'phonon_band.dat'
    filename = ('{}_phonon_band.dat'.format(prefix) if prefix else filename)
    directory = (directory if directory else '.')
    filename = os.path.join(directory, filename)
    with open(filename, 'w') as f:
        header = '
        f.write(header)
        for band in bs.bands:
            for (d, e) in zip(bs.distance, band):
                f.write('{:.8f} {:.8f}\n'.format(d, e))
            f.write('\n')
    return filename

Write the phonon band structure data files to disk.

Args:
bs (:obj:`~pymatgen.phonon.bandstructure.PhononBandStructureSymmLine`):
The phonon band structure.
prefix (:obj:`str`, optional): Prefix for data file.
directory (:obj:`str`, optional): Directory in which to save the data.

Returns:
str: The filename of the written data file.


def _einsum_matmul_index_helper(gate_indices, number_of_qubits):
    if ((len(gate_indices) + number_of_qubits) > 26):
        raise QiskitError('Total number of free indexes limited to 26')
    tens_in = ascii_lowercase[:number_of_qubits]
    tens_out = list(tens_in)
    mat_left = ''
    mat_right = ''
    for (pos, idx) in enumerate(reversed(gate_indices)):
        mat_left += ascii_lowercase[((- 1) - pos)]
        mat_right += tens_in[((- 1) - idx)]
        tens_out[((- 1) - idx)] = ascii_lowercase[((- 1) - pos)]
    tens_out = ''.join(tens_out)
    return (mat_left, mat_right, tens_in, tens_out)

Return the index string for Numpy.eignsum matrix multiplication.

The returned indices are to perform a matrix multiplication A.v where
the matrix A is an M-qubit matrix, matrix v is an N-qubit vector, and
M <= N, and identity matrices are implied on the subsystems where A has no
support on v.

Args:
gate_indices (list[int]): the indices of the right matrix subsystems
to contract with the left matrix.
number_of_qubits (int): the total number of qubits for the right matrix.

Returns:
tuple: (mat_left, mat_right, tens_in, tens_out) of index strings for
that may be combined into a Numpy.einsum function string.

Raises:
QiskitError: if the total number of qubits plus the number of
contracted indices is greater than 26.


def diet_expert(x, hidden_size, params):

  @fn_with_diet_vars(params)
  def diet_expert_internal(x):
    dim = x.get_shape().as_list()[-1]
    h = tf.layers.dense(x, hidden_size, activation=tf.nn.relu, use_bias=False)
    y = tf.layers.dense(h, dim, use_bias=False)
    y *= tf.rsqrt(tf.to_float(dim * hidden_size))
    return y

  return diet_expert_internal(x)

A two-layer feed-forward network with relu activation on hidden layer.

Uses diet variables.
Recomputes hidden layer on backprop to save activation memory.

Args:
x: a Tensor with shape [batch, io_size]
hidden_size: an integer
params: a diet variable HParams object.

Returns:
a Tensor with shape [batch, io_size]


def extend(self, *bindings):

        self._bindings.extend(self._preprocess(bindings))
        return self

Append the given bindings to this keymap.

Arguments:
*bindings (Binding): Bindings to be added.
Returns:
Keymap: self


def _obj_to_path(obj):
    if (obj is None):
        return obj
    if (inspect.isclass(obj) or inspect.isfunction(obj)):
        fetched = getattr(sys.modules[obj.__module__], obj.__name__, None)
        if (fetched is None):
            raise ValueError(('Object %r must be defined on the top level of a module.' % obj))
        return ('%s.%s' % (obj.__module__, obj.__name__))
    raise TypeError(('Unexpected type %s.' % type(obj)))

Returns the fully qualified path to the object.

Args:
obj: obj must be a new style top level class, or a top level function.
No inner function or static method.

Returns:
Fully qualified path to the object.

Raises:
TypeError: when argument obj has unsupported type.
ValueError: when obj can't be discovered on the top level.


def _retrieve_all_filtered_nodes(self):
    if self._node_filters is None:
        return None
    all_filtered_nodes = set()
    nodes_to_visit = list(self._node_filters)
    while nodes_to_visit:
        node_path = nodes_to_visit.pop(0)
        node_id = self._node_path_to_id[node_path]
        if node_id in all_filtered_nodes:
            continue
        all_filtered_nodes.add(node_id)
        node, setter = self._loaded_nodes.get(node_id, (None, None))
        if node is not None:
            if not isinstance(node, base.Trackable):
                raise TypeError(f"Error when processing dictionary values passed to nodes_to_load.Object at {node_path} is expected to be a checkpointable (i.e. 'trackable') TensorFlow object (e.g. tf.Variable, tf.Module or Keras layer).")
            node._maybe_initialize_trackable()
        for reference in self._proto.nodes[node_id].children:
            child_object, _ = self._loaded_nodes.get(reference.node_id, (None, None))
            if child_object is None and node is not None:
                child_object = node._lookup_dependency(reference.local_name)
                if isinstance(child_object, data_structures.TrackableDataStructure):
                    setter = lambda *args: None
                    self._loaded_nodes[reference.node_id] = (child_object, setter)
            child_path = '{}.{}'.format(node_path, reference.local_name)
            self._node_path_to_id[child_path] = reference.node_id
            nodes_to_visit.append(child_path)
    if 0 in all_filtered_nodes:
        return None
    return all_filtered_nodes

Traverses through the object graph to get the IDs of all nodes to load.

As a side-effect, if node_filters is a dictionary that contains already-
created objects, then the children tracked by those objects will be
added to node_filters.

Returns:
List of all nodes to load, or None if all nodes should be loaded.


def _select_class_id(ids, selected_id):
    ids = sparse_tensor.convert_to_tensor_or_sparse_tensor(ids)
    if isinstance(ids, sparse_tensor.SparseTensor):
        return sparse_ops.sparse_retain(ids, math_ops.equal(ids.values, selected_id))
    ids_shape = array_ops.shape(ids, out_type=dtypes.int64)
    ids_last_dim = array_ops.size(ids_shape) - 1
    filled_selected_id_shape = math_ops.reduced_shape(ids_shape, array_ops.reshape(ids_last_dim, [1]))
    filled_selected_id = array_ops.fill(filled_selected_id_shape, math_ops.cast(selected_id, dtypes.int64))
    result = sets.set_intersection(filled_selected_id, ids)
    return sparse_tensor.SparseTensor(indices=result.indices, values=result.values, dense_shape=ids_shape)

Filter all but `selected_id` out of `ids`.

Args:
ids: `int64` `Tensor` or `SparseTensor` of IDs.
selected_id: Int id to select.

Returns:
`SparseTensor` of same dimensions as `ids`. This contains only the entries
equal to `selected_id`.


def assertAllLessEqual(self, a, comparison_target):
    a, comparison_target = self.evaluate_if_both_tensors(a, comparison_target)
    a = self._GetNdArray(a)
    self.assertLessEqual(np.max(a), comparison_target)

Assert element values are all less than or equal to a target value.

Args:
a: The numpy `ndarray`, or anything that can be converted into a numpy
`ndarray` (including Tensor).
comparison_target: The target value of comparison.


def parse(self, string, root=None):
    phrases = []
    meta = self.meta.search(string)
    while meta:
        pos = meta.start()
        if (meta.group() == '<'):
            (string, child, meta) = self.open_phrase(string, pos)
            if (child and root):
                root.nested.append(child)
            elif child:
                phrases.append(child)
            continue
        elif root:
            if (meta.group() == '('):
                meta = self.meta.search(string, (pos + 1))
                if (meta.group() == ')'):
                    (string, root, meta) = self.handle_arguments(string, root, pos, meta.start())
                    continue
            elif (meta.group() == '>'):
                (string, phrase, meta) = self.close_phrase(string, root, pos)
                if phrase:
                    return (string, phrase)
                continue
        (string, meta) = self.escape_meta(string, pos)
    if (not root):
        return (string, phrases)
    word = re.search('([\\w\\s]+)(?![\\d]*>[\\w\\s]+>)', string)
    what = 'No closing tag found for opening tag'
    if word:
        what += " after expression '{0}'".format(word.group())
    raise errors.ParseError((what + '!'))

Parses a string to handle escaped tags and retrieve phrases.

This method works recursively to parse nested tags. When escaped
tags are found, those are removed from the string. Also argument
sequences are removed from the string. The string returned can
thus be quite different from the string passed.

Arguments:
string (str): The string to parse.
root (Phrase): If in a recursive call, the root/parent phrase.

Returns:
For one, the escaped string (without escape characters and
phrase arguments). For the other, it depends on the stack-depth.
If this is the lowest recursion depth/level (i.e. the stack
call resulting from the first function call in self.beautify()),
it will return a list of phrases. For higher stack levels (
i.e. resulting from recursive function calls from with
self.parse(), for nested phrases), it returns exactly one
Phrase instance.

Raises:
errors.ParseError: If no closing tag could be
found for an opening tag.


def words_string(fake: Faker, n: int) -> str:
    return ' '.join(fake.words(n))

Provide Faker words as a joined string.

Args:
* fake: Faker instance
* n: number of words

Returns:
* string of n words joined by spaces


def get_newest(blocks, layout_blocks):
    layout_temp = list(layout_blocks)
    for i in range(0, len(layout_temp)):
        for k in range(0, len(layout_blocks)):
            if (blocks[layout_temp[i]].ec_hdr.image_seq != blocks[layout_blocks[k]].ec_hdr.image_seq):
                continue
            if (blocks[layout_temp[i]].leb_num != blocks[layout_blocks[k]].leb_num):
                continue
            if (blocks[layout_temp[i]].vid_hdr.sqnum > blocks[layout_blocks[k]].vid_hdr.sqnum):
                del layout_blocks[k]
                break
    return layout_blocks

Filter out old layout blocks from list

Arguments:
List:blocks        -- List of block objects
List:layout_blocks -- List of layout block indexes

Returns:
List -- Newest layout blocks in list


def push(self, key, value, *, section=DataStoreDocumentSection.Data):
    key_notation = '.'.join([section, key])
    result = self._collection.update_one({'_id': ObjectId(self._workflow_id)}, {'$push': {key_notation: self._encode_value(value)}, '$currentDate': {'lastModified': True}})
    return (result.modified_count == 1)

Appends a value to a list in the specified section of the document.

Args:
key (str): The key pointing to the value that should be stored/updated.
It supports MongoDB's dot notation for nested fields.
value: The value that should be appended to a list in the data store.
section (DataStoreDocumentSection): The section from which the data should
be retrieved.

Returns:
bool: ``True`` if the value could be appended, otherwise ``False``.


def uninstall(pkg):

    ret = {'result': None, 'output': ''}

    out = __salt__['cmd.run_all'](FLATPAK_BINARY_NAME + ' uninstall ' + pkg)

    if out['retcode'] and out['stderr']:
        ret['stderr'] = out['stderr'].strip()
        ret['result'] = False
    else:
        ret['stdout'] = out['stdout'].strip()
        ret['result'] = True

    return ret

Uninstall the specified package.

Args:
pkg (str): The package name.

Returns:
dict: The ``result`` and ``output``.

CLI Example:

.. code-block:: bash

salt '*' flatpak.uninstall org.gimp.GIMP


def get_corrections_dict(self, entry):

        corrections = {}
        for c in self.corrections:
            val = c.get_correction(entry)
            if val != 0:
                corrections[str(c)] = val
        return corrections

Returns the corrections applied to a particular entry.

Args:
entry: A ComputedEntry object.

Returns:
({correction_name: value})


def write(self, output_buffer, kmip_version=enums.KMIPVersion.KMIP_1_3):
    if (kmip_version < enums.KMIPVersion.KMIP_1_3):
        raise exceptions.VersionNotSupported('KMIP {} does not support the ProfileInformation object.'.format(kmip_version.value))
    local_buffer = BytearrayStream()
    if self._profile_name:
        self._profile_name.write(local_buffer, kmip_version=kmip_version)
    else:
        raise exceptions.InvalidField('The ProfileInformation structure is missing the profile name field.')
    if self._server_uri:
        self._server_uri.write(local_buffer, kmip_version=kmip_version)
    if self._server_port:
        self._server_port.write(local_buffer, kmip_version=kmip_version)
    self.length = local_buffer.length()
    super(ProfileInformation, self).write(output_buffer, kmip_version=kmip_version)
    output_buffer.write(local_buffer.buffer)

Write the ProfileInformation structure encoding to the data stream.

Args:
output_buffer (stream): A data stream in which to encode
ProfileInformation structure data, supporting a write method.
kmip_version (enum): A KMIPVersion enumeration defining the KMIP
version with which the object will be encoded. Optional,
defaults to KMIP 2.0.

Raises:
InvalidField: Raised if the profile name field is not defined.
VersionNotSupported: Raised when a KMIP version is provided that
does not support the ProfileInformation structure.


def multiply(x1, x2, output_shape=None, name=None):

  if not isinstance(x2, Tensor):
    return ScalarMultiplyOperation(x1, x2).outputs[0]
  with tf.name_scope(name, default_name="mul"):
    x1, x2 = binary_arguments_to_tensors(x1, x2)
    return einsum(
        [x1, x2],
        output_shape=_infer_binary_broadcast_shape(
            x1.shape, x2.shape, output_shape))

Binary multiplication with broadcasting.

Args:
x1: a Tensor
x2: a Tensor
output_shape: an optional Shape
name: an optional string
Returns:
a Tensor


def register_instance(self, instance: '_instance_base.Instance') -> None:

Treating self as a class definition, register an instance of it.

This is used for keeping merging call records on instances when generating
the formal definition of a class. See InterpreterClass and TupleClass.

Args:
instance: An instance of this class (as a BaseValue)


def delete(self, collector_id=None):

        cid = self.collector_id

        if collector_id:
            cid = collector_id

        url = '{0}/{1}'.format(self.url, cid)
        request = requests.delete(url, auth=self.auth)
        try:

            response = request.json()
        except ValueError:

            response = {
                u'message': u'The request completed successfully.',
                u'status': 200,
            }
        return response

Delete a collector from inventory.

Args:
collector_id (int): id of collector (optional)


def calculate_parity(n):
    if (not is_natural(n)):
        raise ValueError('Expected n to be a positive integer.')
    y = 0
    n = abs(n)
    while n:
        y += (n & 1)
        n = (n >> 1)
    return (y & 1)

Calculates and returns the parity of a number.

The parity of a number is ``1`` if the number has an odd number of ones
in its binary representation, otherwise ``0``.

Args:
n (int): the number whose parity to calculate

Returns:
``1`` if the number has an odd number of ones, otherwise ``0``.

Raises:
ValueError: if ``n`` is less than ``0``.


def numeric_function_clean_dataframe(self, axis):

        result = None
        query_compiler = self

        if not axis and len(self.index) == 0:
            result = pandas.Series(dtype=np.int64)

        nonnumeric = [
            col
            for col, dtype in zip(self.columns, self.dtypes)
            if not is_numeric_dtype(dtype)
        ]
        if len(nonnumeric) == len(self.columns):

            if axis:
                result = pandas.Series([np.nan for _ in self.index])
            else:
                result = pandas.Series([0 for _ in self.index])
        else:
            query_compiler = self.drop(columns=nonnumeric)
        return result, query_compiler

Preprocesses numeric functions to clean dataframe and pick numeric indices.

Args:
axis: '0' if columns and '1' if rows.

Returns:
Tuple with return value(if any), indices to apply func to & cleaned Manager.


def gui(discord_token, discord_client_id):

    logger.info("Starting Modis in GUI")

    import tkinter as tk

    logger.debug("Loading packages")
    from modis.discord_modis import gui as discord_modis_gui
    from modis.reddit_modis import gui as reddit_modis_gui
    from modis.facebook_modis import gui as facebook_modis_gui

    logger.debug("Initialising window")

    root = tk.Tk()
    root.minsize(width=800, height=400)
    root.geometry("800x600")
    root.title("Modis Control Panel")

    root.iconbitmap(r"{}/assets/modis.ico".format(file_dir))

    discord = discord_modis_gui.Frame(root, discord_token, discord_client_id)
    discord.grid(column=0, row=0, padx=0, pady=0, sticky="W E N S")

    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)
    discord.columnconfigure(0, weight=1)
    discord.rowconfigure(0, weight=1)

    logger.debug("GUI initialised")

    root.mainloop()

Start Modis in gui format.

Args:
discord_token (str): The bot token for your Discord application
discord_client_id: The bot's client ID


async def connect(self, client_id, conn_string):
    conn_id = self.adapter.unique_conn_id()
    self._client_info(client_id)
    (await self.adapter.connect(conn_id, conn_string))
    self._hook_connect(conn_string, conn_id, client_id)

Connect to a device on behalf of a client.

See :meth:`AbstractDeviceAdapter.connect`.

Args:
client_id (str): The client we are working for.
conn_string (str): A connection string that will be
passed to the underlying device adapter to connect.

Raises:
DeviceServerError: There is an issue with your client_id.
DeviceAdapterError: The adapter had an issue connecting.


def make_timebar(progress=0, duration=0):
    duration_string = api_music.duration_to_string(duration)
    if (duration <= 0):
        return '---'
    time_counts = int(round(((progress / duration) * TIMEBAR_LENGTH)))
    if (time_counts > TIMEBAR_LENGTH):
        time_counts = TIMEBAR_LENGTH
    if (duration > 0):
        bar = ((('│' + (TIMEBAR_PCHAR * time_counts)) + (TIMEBAR_ECHAR * (TIMEBAR_LENGTH - time_counts))) + '│')
        time_bar = '{} {}'.format(bar, duration_string)
    else:
        time_bar = duration_string
    return time_bar

Makes a new time bar string

Args:
progress: How far through the current song we are (in seconds)
duration: The duration of the current song (in seconds)

Returns:
timebar (str): The time bar string


Called at the end of session.

The `session` argument can be used in case the hook wants to run final ops,
such as saving a last checkpoint.

If `session.run()` raises exception other than OutOfRangeError or
StopIteration then `end()` is not called.
Note the difference between `end()` and `after_run()` behavior when
`session.run()` raises OutOfRangeError or StopIteration. In that case
`end()` is called but `after_run()` is not called.

Args:
session: A TensorFlow Session that will be soon closed.


def __init__(self, log_path, config_path, output_path):

        if FLAGS.phantomjs_timeout is not None:
            logging.info(
                'Using FLAGS.phantomjs_timeout which is deprecated in favor'
                'of FLAGS.capture_timeout - please update your config')
            capture_timeout = FLAGS.phantomjs_timeout
        else:
            capture_timeout = FLAGS.capture_timeout
        process_worker.ProcessWorkflow.__init__(
            self, log_path, timeout_seconds=capture_timeout)
        self.config_path = config_path
        self.output_path = output_path

Initializer.

Args:
log_path: Where to write the verbose logging output.
config_path: Path to the screenshot config file to pass
to PhantomJs.
output_path: Where the output screenshot should be written.


def augment(self, dct: NonAugmentedDict, document: Optional[YamlDocument]=None) -> AugmentedDict:
    Validator.instance_of(dict, raise_ex=True, dct=dct)
    for instance in self._extensions:
        nodes = list(dict_find_pattern(dct, **instance.config()))
        for (parent, k, val) in nodes:
            parent.pop(k)
            fragment = instance.apply(ExtensionContext(mentor=self, document=(document or dct), dct=dct, parent_node=parent, node=(k, val)))
            if (fragment is not None):
                parent.update(fragment)
    return dct

Augments the given dictionary by using all the bound extensions.

Args:
dct: Dictionary to augment.
document: The document the dictionary was loaded from.

Returns:
The augmented dictionary.


def get_distrib_version():
    key = 'distrib_ver'
    out, err = run_shell_cmd(cmds_all[PLATFORM][key])
    if err and FLAGS.debug:
        print('Error in detecting distribution version:\n %s' % str(err))
    return out.strip(b'\n')

Retrieves distribution version of the operating system.

Returns:
String that is the distribution version.
e.g. '14.04'


def parse_simple_id(chrom, pos, ref, alt):
    return '_'.join([chrom, pos, ref, alt])

Parse the simple id for a variant

Simple id is used as a human readable reference for a position, it is
in no way unique.

Args:
chrom(str)
pos(str)
ref(str)
alt(str)

Returns:
simple_id(str): The simple human readable variant id


def auto_forward(auto=True):
    global __auto_forward_state
    prev = __auto_forward_state
    __auto_forward_state = auto
    (yield)
    __auto_forward_state = prev

Context for dynamic graph execution mode.

Args:
auto (bool): Whether forward computation is executed during a
computation graph construction.

Returns: bool


def __init__(self, model_file, input_arrays=None, input_shapes=None, output_arrays=None, custom_objects=None):
    super(TFLiteKerasModelConverter, self).__init__(experimental_debug_info_func=None)
    if context.executing_eagerly():
        if input_arrays or output_arrays:
            raise ValueError('`input_arrays` and `output_arrays` are unsupported with Eager mode. If your model requires any of these parameters, please use disable_eager_execution().')
        keras_model = keras_deps.get_load_model_function()(model_file, custom_objects)
        function = _trace_model_call(keras_model)
        concrete_func = function.get_concrete_function()
        frozen_func = _convert_to_constants.convert_variables_to_constants_v2(concrete_func, lower_control_flow=False)
        _set_tensor_shapes(frozen_func.inputs, input_shapes)
        self._keras_model = keras_model
        self._graph_def = frozen_func.graph.as_graph_def()
        self._input_tensors = frozen_func.inputs
        self._output_tensors = frozen_func.outputs
        self._debug_info_func = _build_debug_info_func(frozen_func.graph)
        return
    keras_deps.get_clear_session_function()()
    keras_model = keras_deps.get_load_model_function()(model_file, custom_objects)
    sess = keras_deps.get_get_session_function()()
    if input_arrays:
        input_tensors = _get_tensors_from_tensor_names(sess.graph, input_arrays)
    else:
        input_tensors = keras_model.inputs
    if output_arrays:
        output_tensors = _get_tensors_from_tensor_names(sess.graph, output_arrays)
    else:
        output_tensors = keras_model.outputs
    _set_tensor_shapes(input_tensors, input_shapes)
    graph_def = _freeze_graph(sess, input_tensors, output_tensors)
    self._keras_model = keras_model
    self._graph_def = graph_def
    self._input_tensors = input_tensors
    self._output_tensors = output_tensors
    self._debug_info_func = _build_debug_info_func(sess.graph)

Constructor for TFLiteConverter.

Args:
model_file: Full filepath of HDF5 file containing the tf.keras model.
input_arrays: List of input tensors to freeze graph with. Uses input
arrays from SignatureDef when none are provided. (default None)
input_shapes: Dict of strings representing input tensor names to list of
integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}).
Automatically determined when input shapes is None (e.g., {"foo" :
None}). (default None)
output_arrays: List of output tensors to freeze graph with. Uses output
arrays from SignatureDef when none are provided. (default None)
custom_objects: Dict mapping names (strings) to custom classes or
functions to be considered during model deserialization. (default None)

Raises:
ValueError: Invalid arguments.


def set_storage(self, storage):

        if isinstance(storage, BaseStorage):
            self.storage = storage
        elif isinstance(storage, dict):
            if 'backend' not in storage and 'root_dir' in storage:
                storage['backend'] = 'FileSystem'
            try:
                backend_cls = getattr(storage_package, storage['backend'])
            except AttributeError:
                try:
                    backend_cls = import_module(storage['backend'])
                except ImportError:
                    self.logger.error('cannot find backend module %s',
                                      storage['backend'])
                    sys.exit()
            kwargs = storage.copy()
            del kwargs['backend']
            self.storage = backend_cls(**kwargs)
        else:
            raise TypeError('"storage" must be a storage object or dict')

Set storage backend for downloader

For full list of storage backend supported, please see :mod:`storage`.

Args:
storage (dict or BaseStorage): storage backend configuration or instance


def _projected_entity_to_message(ent, message_type):

  msg = message_type()
  analyzed = _analyze_indexed_fields(ent._projection)
  for name, sublist in analyzed.iteritems():
    prop = ent._properties[name]
    val = prop._get_value(ent)
    assert isinstance(prop, model.StructuredProperty) == bool(sublist)
    if sublist:
      field = message_type.field_by_name(name)
      assert isinstance(field, messages.MessageField)
      assert prop._repeated == field.repeated
      if prop._repeated:
        assert isinstance(val, list)
        val = [_projected_entity_to_message(v, field.type) for v in val]
      else:
        assert isinstance(val, prop._modelclass)
        val = _projected_entity_to_message(val, field.type)
    setattr(msg, name, val)
  return msg

Recursive helper for _from_base_type() to convert an entity to a message.

Args:
ent: A Model instance.
message_type: A Message subclass.

Returns:
An instance of message_type.


def call(self, hidden_states: tf.Tensor, attention_mask: Optional[tf.Tensor]=None, image_hidden_states: Optional[tf.Tensor]=None, image_attention_mask: Optional[tf.Tensor]=None, cross_attention_gate: Optional[tf.Tensor]=None, output_attentions: Optional[bool]=False, use_cache: Optional[bool]=False, past_key_value: Optional[Tuple[tf.Tensor]]=None) -> Tuple[tf.Tensor, Optional[Tuple[tf.Tensor, tf.Tensor]]]:
    if image_hidden_states is None:
        raise ValueError('`image_hidden_states` is required for Idefics cross attention module which are visual features to be conditioned on.')
    if cross_attention_gate is None:
        raise ValueError('`cross_attention_gate` is required for Idefics cross attention module to zero-out the cross-attention hidden_states attending to no images.')
    if past_key_value is not None:
        raise NotImplementedError('Past key value states are not implemented for Idefics cross attention module.')
    residual = hidden_states
    hidden_states = self.input_layernorm(hidden_states)
    hidden_states, self_attn_weights, present_key_value = self.cross_attn(hidden_states=hidden_states, key_value_states=image_hidden_states, attention_mask=image_attention_mask, output_attentions=output_attentions)
    hidden_states = tf.nn.dropout(hidden_states, rate=self.config)
    mask = tf.cast(cross_attention_gate == 0, dtype=hidden_states.dtype)
    mask = tf.expand_dims(mask, -1)
    hidden_states = tf.where(tf.broadcast_to(mask, tf.shape(hidden_states)) == 1, tf.zeros_like(hidden_states), hidden_states)
    hidden_states = residual + self.act_cross_attn(self.alpha_cross_attn) * hidden_states
    residual = hidden_states
    hidden_states = self.post_attention_layernorm(hidden_states)
    hidden_states = self.mlp(hidden_states)
    hidden_states = tf.nn.dropout(hidden_states, rate=self.config)
    hidden_states = residual + self.act_dense(self.alpha_dense) * hidden_states
    outputs = (hidden_states,)
    if output_attentions:
        outputs += (self_attn_weights,)
    if use_cache:
        outputs += (present_key_value,)
    return outputs

Args:
hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
attention_mask (`tf.Tensor`, *optional*): attention mask of size
`(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
output_attentions (`bool`, *optional*):
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
returned tensors for more detail.
use_cache (`bool`, *optional*):
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
(see `past_key_values`).
past_key_value (`Tuple(tf.Tensor)`, *optional*): cached past key and value projection states
no_images (`bool`, *optional*, defaults to `False`): If `True` the vision part is ignored


def assertNotAllClose(self, a, b, rtol=1e-06, atol=1e-06, msg=None):
    try:
        self.assertAllClose(a, b, rtol=rtol, atol=atol, msg=msg)
    except AssertionError:
        return
    msg = msg or ''
    raise AssertionError('The two values are close at all elements. %s' % msg)

Assert that two numpy arrays, or Tensors, do not have near values.

Args:
a: The expected numpy `ndarray`, or anything that can be converted into a
numpy `ndarray` (including Tensor), or any arbitrarily nested of
structure of these.
b: The actual numpy `ndarray`, or anything that can be converted into a
numpy `ndarray` (including Tensor), or any arbitrarily nested of
structure of these.
rtol: relative tolerance.
atol: absolute tolerance.
msg: Optional message to report on failure.

Raises:
AssertionError: If `a` and `b` are unexpectedly close at all elements.


def iter(self, keyed=False, extended=False):
    if self.closed:
        message = 'Stream is closed. Please call "stream.open()" first.'
        raise exceptions.TabulatorException(message)
    iterator = chain(self.__sample_extended_rows, self.__parser.extended_rows)
    iterator = self.__apply_processors(iterator)
    for (row_number, headers, row) in iterator:
        if (row_number > self.__row_number):
            self.__row_number = row_number
            if extended:
                (yield (row_number, headers, row))
            elif keyed:
                (yield dict(zip(headers, row)))
            else:
                (yield row)

Iterate over the rows.

Each row is returned in a format that depends on the arguments `keyed`
and `extended`. By default, each row is returned as list of their
values.

Args:
keyed (bool, optional): When True, each returned row will be a
`dict` mapping the header name to its value in the current row.
For example, `[{'name': 'J Smith', 'value': '10'}]`. Ignored if
``extended`` is True. Defaults to False.
extended (bool, optional): When True, returns each row as a tuple
with row number (starts at 1), list of headers, and list of row
values. For example, `(1, ['name', 'value'], ['J Smith', '10'])`.
Defaults to False.

Returns:
Iterator[Union[List[Any], Dict[str, Any], Tuple[int, List[str], List[Any]]]]:
The row itself. The format depends on the values of `keyed` and
`extended` arguments.

Raises:
exceptions.TabulatorException: If the stream is closed.


def has_error(self):
    return next((True for cr in self.component_results if cr.has_error()), False)

Returns whether there was a business logic error when fetching data
for any components for this property.

Returns:
boolean


def match(self, patterns, limits=None):
    if limits is None:
        limits = [None] * len(patterns)
    else:
        err_msg = 'Patterns and limits should be equal in length'
        assert len(patterns) == len(limits), err_msg

    def _match(pattern, limit):

        if pattern.endswith('/') or pattern.endswith('\\'):
            pattern += '*'
        prefix_or_dir = re.match('^[^[*?]*', pattern).group(0)
        file_metadatas = []
        if prefix_or_dir == pattern:
            if self.exists(pattern):
                file_metadatas = [self.metadata(pattern)]
        else:
            if self.has_dirs():
                prefix_dirname = self._url_dirname(prefix_or_dir)
                if not prefix_dirname == prefix_or_dir:
                    logger.debug('Changed prefix_or_dir %r -> %r', prefix_or_dir, prefix_dirname)
                    prefix_or_dir = prefix_dirname
            logger.debug('Listing files in %r', prefix_or_dir)
            file_metadatas = self._list(prefix_or_dir)
        metadata_list = []
        for file_metadata in self.match_files(file_metadatas, pattern):
            if limit is not None and len(metadata_list) >= limit:
                break
            metadata_list.append(file_metadata)
        return MatchResult(pattern, metadata_list)
    exceptions = {}
    result = []
    for pattern, limit in zip(patterns, limits):
        try:
            result.append(_match(pattern, limit))
        except Exception as e:
            exceptions[pattern] = e
    if exceptions:
        raise BeamIOError('Match operation failed', exceptions)
    return result

Find all matching paths to the patterns provided.

See Also:
:meth:`translate_pattern`

Patterns ending with '/' or '\' will be appended with '*'.

Args:
patterns: list of string for the file path pattern to match against
limits: list of maximum number of responses that need to be fetched

Returns: list of ``MatchResult`` objects.

Raises:
``BeamIOError``: if any of the pattern match operations fail


def AddDateTimeRange(self, time_value, start_time_string=None, end_time_string=None):
    if (not isinstance(time_value, py2to3.STRING_TYPES)):
        raise ValueError('Filter type must be a string.')
    if ((start_time_string is None) and (end_time_string is None)):
        raise ValueError('Filter must have either a start or an end date time value.')
    time_value_lower = time_value.lower()
    if (time_value_lower not in self._SUPPORTED_TIME_VALUES):
        raise ValueError('Unsupported time value: {0:s}.'.format(time_value))
    start_date_time = None
    if start_time_string:
        start_date_time = time_elements.TimeElementsInMicroseconds()
        start_date_time.CopyFromDateTimeString(start_time_string)
    end_date_time = None
    if end_time_string:
        end_date_time = time_elements.TimeElementsInMicroseconds()
        end_date_time.CopyFromDateTimeString(end_time_string)
    if ((None not in (start_date_time, end_date_time)) and (start_date_time > end_date_time)):
        raise ValueError('Invalid date time value start must be earlier than end.')
    self._date_time_ranges.append(self._DATE_TIME_RANGE_TUPLE(time_value_lower, start_date_time, end_date_time))

Adds a date time filter range.

The time strings are formatted as:
YYYY-MM-DD hh:mm:ss.######[+-]##:##
Where # are numeric digits ranging from 0 to 9 and the seconds
fraction can be either 3 or 6 digits. The time of day, seconds fraction
and timezone offset are optional. The default timezone is UTC.

Args:
time_value (str): time value, such as, atime, ctime, crtime, dtime, bkup
and mtime.
start_time_string (str): start date and time value string.
end_time_string (str): end date and time value string.

Raises:
ValueError: If the filter is badly formed.


def Unregister(self, name):

    precondition.AssertType(name, Text)

    try:
      del self._constructors[name]
    except KeyError:
      raise ValueError("Constructor with name '%s' is not registered" % name)

Unregisters a constructor.

Args:
name: A name of the constructor to unregister.

Raises:
ValueError: If constructor with specified name has never been registered.


def _construct_linebreak_token(self, d: Dict) -> List[Dict]:

        result = []
        num_break = int(d["length"][0]) if d["length"] else 1
        if num_break:
            s = ''
            for i in range(num_break):
                s += '\n'
            this_token = {attrs.LOWER: s}
            result.append(this_token)
            s += ' '
            this_token = {attrs.LOWER: s}
            result.append(this_token)
        result = self._add_common_constrain(result, d)

        return result

Construct a shape token
Args:
d: Dict

Returns: List[Dict]


def frombase(path1, path2):
    if (not isparent(path1, path2)):
        raise ValueError('path1 must be a prefix of path2')
    return path2[len(path1):]

Get the final path of ``path2`` that isn't in ``path1``.

Arguments:
path1 (str): A PyFilesytem path.
path2 (str): A PyFilesytem path.

Returns:
str: the final part of ``path2``.

Example:
>>> frombase('foo/bar/', 'foo/bar/baz/egg')
'baz/egg'


def singleOrPair(obj):

    if len(list(obj.__class__.__mro__)) <= 2:
        return 'Neither'
    else:

        if ancestorJr(obj) is Pair:
            return 'Pair'
        elif ancestor(obj) is Single:
            return 'Single'
        else:
            return 'Neither'

Chech an object is single or pair or neither.

Of course,, all pairs are single, so what the function is really detecting is whether an object is only single or at the same time a pair.

Args:
obj (object): Literally anything.

Returns:
str: 'Single', or 'Pair', or 'Neither'


def coarse_grain(G, ncg):
    if (ncg <= 1):
        return G
    G = numpy.asarray(G)
    (nbin, remainder) = divmod(G.shape[(- 1)], ncg)
    if (remainder != 0):
        nbin += 1
    return numpy.transpose([(numpy.sum(G[(..., i:(i + ncg))], axis=(- 1)) / G[(..., i:(i + ncg))].shape[(- 1)]) for i in numpy.arange(0, (ncg * nbin), ncg)])

Coarse-grain last index of array ``G``.

Bin the last index of array ``G`` in bins of width ``ncg``, and
replace each bin by its average. Return the binned results.

Args:
G: Array to be coarse-grained.
ncg: Bin width for coarse-graining.


def match(self, request):
    if (self._times <= 0):
        raise PookExpiredMock('Mock expired')
    for test in self.filters:
        if (not test(request, self)):
            return (False, [])
    for mapper in self.mappers:
        request = mapper(request, self)
        if (not request):
            raise ValueError('map function must return a request object')
    (matches, errors) = self.matchers.match(request)
    if (not matches):
        return (False, errors)
    self._calls.append(request)
    self._matches += 1
    if (not self._persist):
        self._times -= 1
    if self._error:
        raise self._error
    for callback in self.callbacks:
        callback(request, self)
    return (True, [])

Matches an outgoing HTTP request against the current mock matchers.

This method acts like a delegator to `pook.MatcherEngine`.

Arguments:
request (pook.Request): request instance to match.

Raises:
Exception: if the mock has an exception defined.

Returns:
tuple(bool, list[Exception]): ``True`` if the mock matches
the outgoing HTTP request, otherwise ``False``. Also returns
an optional list of error exceptions.


def get_content_field(self, name):

        fields = self._content.findall(name)
        if not fields:
            return None
        elif len(fields) == 1:
            return etree_to_dict(fields[0])[name]
        else:
            return [etree_to_dict(field)[name] for field in fields]

Get the contents of a specific subtag from Clusterpoint Storage's response's content tag.

Args:
name -- A name string of the content's subtag to be returned.

Returns:
A dict representing the contents of the specified field or a list of dicts
if there are multiple fields with that tag name. Returns None if no field found.


def cs20(msg):

    chars = '

    d = hex2bin(data(msg))

    cs = ''
    cs += chars[bin2int(d[8:14])]
    cs += chars[bin2int(d[14:20])]
    cs += chars[bin2int(d[20:26])]
    cs += chars[bin2int(d[26:32])]
    cs += chars[bin2int(d[32:38])]
    cs += chars[bin2int(d[38:44])]
    cs += chars[bin2int(d[44:50])]
    cs += chars[bin2int(d[50:56])]

    return cs

Aircraft callsign

Args:
msg (String): 28 bytes hexadecimal message (BDS40) string

Returns:
string: callsign, max. 8 chars


def _DictToListOfStrings(self, data_dict):
    ret_list = []
    for (key, value) in iter(data_dict.items()):
        if (key in ('body', 'datetime', 'type', 'room', 'rooms', 'id')):
            continue
        ret_list.append('{0:s} = {1!s}'.format(key, value))
    return ret_list

Converts a dictionary into a list of strings.

Args:
data_dict (dict[str, object]): dictionary to convert.

Returns:
list[str]: list of strings.


def average_datetimes(dt_list):
    if (sys.version_info < (3, 3)):
        import time

        def timestamp_func(dt):
            return time.mktime(dt.timetuple())
    else:
        timestamp_func = datetime.timestamp
    total = [timestamp_func(dt) for dt in dt_list]
    return datetime.fromtimestamp((sum(total) / len(total)))

Average a series of datetime objects.

.. note::

This function assumes all datetime objects are naive and in the same
time zone (UTC).

Args:
dt_list (iterable): Datetime objects to average

Returns: Average datetime as a datetime object


def MapByteStream(
      self, byte_stream, byte_offset=0, context=None, **unused_kwargs):

    data_type_size = self._data_type_definition.GetByteSize()
    self._CheckByteStreamSize(byte_stream, byte_offset, data_type_size)

    try:
      struct_tuple = self._operation.ReadFrom(byte_stream[byte_offset:])
      mapped_value = self.MapValue(*struct_tuple)

    except Exception as exception:
      error_string = (
          'Unable to read: {0:s} from byte stream at offset: {1:d} '
          'with error: {2!s}').format(
              self._data_type_definition.name, byte_offset, exception)
      raise errors.MappingError(error_string)

    if context:
      context.byte_size = data_type_size

    return mapped_value

Maps the data type on a byte stream.

Args:
byte_stream (bytes): byte stream.
byte_offset (Optional[int]): offset into the byte stream where to start.
context (Optional[DataTypeMapContext]): data type map context.

Returns:
object: mapped value.

Raises:
MappingError: if the data type definition cannot be mapped on
the byte stream.


def constant_time_string_compare(a, b):

    try:
        return hmac.compare_digest(a, b)
    except AttributeError:

        if len(a) != len(b):
            return False

        result = 0

        for x, y in zip(a, b):
            result |= ord(x) ^ ord(y)

        return result == 0

Helper for comparing string in constant time, independent
of the python version being used.

Args:
a (str): A string to compare
b (str): A string to compare


def show_history(self, status=None, nids=None, full_history=False, metadata=False):
    (nrows, ncols) = get_terminal_size()
    works_done = []
    for task in self.iflat_tasks(status=status, nids=nids):
        work = task.work
        if (work not in works_done):
            works_done.append(work)
            if (work.history or full_history):
                cprint(make_banner(str(work), width=ncols, mark='='), **work.status.color_opts)
                print(work.history.to_string(metadata=metadata))
        if (task.history or full_history):
            cprint(make_banner(str(task), width=ncols, mark='='), **task.status.color_opts)
            print(task.history.to_string(metadata=metadata))
    if (self.history or full_history):
        cprint(make_banner(str(self), width=ncols, mark='='), **self.status.color_opts)
        print(self.history.to_string(metadata=metadata))

Print the history of the flow to stdout.

Args:
status: if not None, only the tasks with this status are select
full_history: Print full info set, including nodes with an empty history.
nids: optional list of node identifiers used to filter the tasks.
metadata: print history metadata (experimental)


def _extract_direct(self, *, stream):

        def normal_dct_rgb():

            DEFAULT_CT_RGB = 1
            ct = self.filter_decodeparms[0][1].get('/ColorTransform', DEFAULT_CT_RGB)
            return self.mode == 'RGB' and ct == DEFAULT_CT_RGB

        def normal_dct_cmyk():

            DEFAULT_CT_CMYK = 0
            ct = self.filter_decodeparms[0][1].get('/ColorTransform', DEFAULT_CT_CMYK)
            return self.mode == 'CMYK' and ct == DEFAULT_CT_CMYK

        if self.filters == ['/CCITTFaxDecode']:
            data = self.obj.read_raw_bytes()
            stream.write(self._generate_ccitt_header(data))
            stream.write(data)
            return '.tif'
        elif self.filters == ['/DCTDecode'] and (
            self.mode == 'L' or normal_dct_rgb() or normal_dct_cmyk()
        ):
            buffer = self.obj.get_raw_stream_buffer()
            stream.write(buffer)
            return '.jpg'

        raise UnsupportedImageTypeError()

Attempt to extract the image directly to a usable image file

If there is no way to extract the image without decompressing or
transcoding then raise an exception. The type and format of image
generated will vary.

Args:
stream: Writable stream to write data to


def estimate_tokens(self, input_dict: Dict[str, Union[torch.Tensor, Any]]) -> int:
    if not hasattr(self, 'warnings_issued'):
        self.warnings_issued = {}
    if self.main_input_name in input_dict:
        return input_dict[self.main_input_name].numel()
    elif 'estimate_tokens' not in self.warnings_issued:
        logger.warning('Could not estimate the number of tokens of the input, floating-point operations will not be computed')
        self.warnings_issued['estimate_tokens'] = True
    return 0

Helper function to estimate the total number of tokens from the model inputs.

Args:
inputs (`dict`): The model inputs.

Returns:
`int`: The total number of tokens.


def is_truthy(value, default=False):

    if value is None:
        return False

    if isinstance(value, bool):
        return value

    if isinstance(value, int):
        return value > 0

    trues = ('1', 'true', 'y', 'yes', 'ok')
    falses = ('', '0', 'false', 'n', 'none', 'no')

    if value.lower().strip() in falses:
        return False

    elif value.lower().strip() in trues:
        return True

    else:
        if default:
            return default
        else:
            raise ValueError('Invalid argument given to truthy: {0}'.format(value))

Evaluate a value for truthiness

>>> is_truthy('Yes')
True
>>> is_truthy('False')
False
>>> is_truthy(1)
True

Args:
value (Any): Value to evaluate
default (bool): Optional default value, if the input does not match the true or false values

Returns:
True if a truthy value is passed, else False


def Verify(self, mempool):

        if not super(ClaimTransaction, self).Verify(mempool):
            return False

        otherclaimTxs = [tx for tx in mempool if tx is ClaimTransaction and tx is not self]
        for other in otherclaimTxs:

            if len([list(filter(lambda x: x in self.Claims, otherClaims)) for otherClaims in other.Claims]):
                return False

        txResult = None
        for tx in self.GetTransactionResults():
            if tx.AssetId == Blockchain.SystemCoin().Hash:
                txResult = tx
                break

        if txResult is None or txResult.Amount > Fixed8(0):
            return False

        try:
            return Blockchain.CalculateBonusIgnoreClaimed(self.Claims, False) == -txResult.Amount

        except Exception as e:
            logger.error('Could not calculate bonus: %s ' % e)

        return False

Verify the transaction.

Args:
mempool:

Returns:
bool: True if verified. False otherwise.


def __recv(self, size=4096):
    data = self.socket.recv(size)
    if (not data):
        raise NNTPError('Failed to read from socket')
    self.__buffer.write(data)

Reads data from the socket.

Raises:
NNTPError: When connection times out or read from socket fails.


def categorize(values, categories, default=None):

    uniq_cats = list(unique_iterator(values))
    cats = []
    for c in values:
        if isinstance(categories, list):
            cat_ind = uniq_cats.index(c)
            if cat_ind < len(categories):
                cat = categories[cat_ind]
            else:
                cat = default
        else:
            cat = categories.get(c, default)
        cats.append(cat)
    return np.asarray(cats)

Maps discrete values to supplied categories.

Replaces discrete values in input array with a fixed set of
categories defined either as a list or dictionary.

Args:
values: Array of values to be categorized
categories: List or dict of categories to map inputs to
default: Default value to assign if value not in categories

Returns:
Array of categorized values


def content(self, request, id):

        gist = self.send(request, id).json()

        def convert(data):
            return base64.b64decode(data).decode('utf-8')

        content = {}
        for name, data in gist['files'].items():
            content[name] = convert(data['content'])

        return content

Returns the content of the gist

Arguments:
request: an initial request object
id:      the gist identifier

Returns:
A dict containing the contents of each file in the gist


def from_structure(cls, structure, ff_elements=None, atom_style="charge"):

        s = structure.get_sorted_structure()
        box, symmop = lattice_2_lmpbox(s.lattice)
        coords = symmop.operate_multi(s.cart_coords)
        site_properties = s.site_properties
        if "velocities" in site_properties:
            velos = np.array(s.site_properties["velocities"])
            rot = SymmOp.from_rotation_and_translation(symmop.rotation_matrix)
            rot_velos = rot.operate_multi(velos)
            site_properties.update({"velocities": rot_velos})
        boxed_s = Structure(box.to_lattice(), s.species, coords,
                            site_properties=site_properties,
                            coords_are_cartesian=True)

        symbols = list(s.symbol_set)
        if ff_elements:
            symbols.extend(ff_elements)
        elements = sorted(Element(el) for el in set(symbols))
        mass_info = [tuple([i.symbol] * 2) for i in elements]
        ff = ForceField(mass_info)
        topo = Topology(boxed_s)
        return cls.from_ff_and_topologies(box=box, ff=ff, topologies=[topo],
                                          atom_style=atom_style)

Simple constructor building LammpsData from a structure without
force field parameters and topologies.

Args:
structure (Structure): Input structure.
ff_elements ([str]): List of strings of elements that must
be present due to force field settings but not
necessarily in the structure. Default to None.
atom_style (str): Choose between "atomic" (neutral) and
"charge" (charged). Default to "charge".


def _apply_credentials(auto_refresh=True, credentials=None,
                           headers=None):

        token = credentials.get_credentials().access_token
        if auto_refresh is True:
            if token is None:
                token = credentials.refresh(
                    access_token=None, timeout=10)
            elif credentials.jwt_is_expired():
                token = credentials.refresh(timeout=10)
        headers.update(
            {'Authorization': "Bearer {}".format(token)}
        )

Update Authorization header.

Update request headers with latest `access_token`. Perform token
`refresh` if token is ``None``.

Args:
auto_refresh (bool): Perform token refresh if access_token is ``None`` or expired. Defaults to ``True``.
credentials (class): Read-only credentials.
headers (class): Requests `CaseInsensitiveDict`.


def strace_data_access_event(self, operation, address, data, data_mask=None, access_width=4, address_range=0):
    cmd = enums.JLinkStraceCommand.TRACE_EVENT_SET
    event_info = structs.JLinkStraceEventInfo()
    event_info.Type = enums.JLinkStraceEvent.DATA_ACCESS
    event_info.Op = operation
    event_info.AccessSize = int(access_width)
    event_info.Addr = int(address)
    event_info.Data = int(data)
    event_info.DataMask = int((data_mask or 0))
    event_info.AddrRangeSize = int(address_range)
    handle = self._dll.JLINK_STRACE_Control(cmd, ctypes.byref(event_info))
    if (handle < 0):
        raise errors.JLinkException(handle)
    return handle

Sets an event to trigger trace logic when data access is made.

Data access corresponds to either a read or write.

Args:
self (JLink): the ``JLink`` instance.
operation (int): one of the operations in ``JLinkStraceOperation``.
address (int): the address of the load/store data.
data (int): the data to be compared the event data to.
data_mask (int): optional bitmask specifying bits to ignore in
comparison.
acess_width (int): optional access width for the data.
address_range (int): optional range of address to trigger event on.

Returns:
An integer specifying the trace event handle.  This handle should be
retained in order to clear the event at a later time.

Raises:
JLinkException: on error.


def sg_any(tensor, opt):
    r
    return tf.reduce_any(tensor, axis=opt.axis, keep_dims=opt.keep_dims, name=opt.name)

r"""Computes the "logical or" of elements across axis of a tensor.

See `tf.reduce_any()` in tensorflow.

Args:
tensor: A `Tensor` (automatically given by chain).
opt:
axis : A tuple/list of integers or an integer. The axis to reduce.
keep_dims: If true, retains reduced dimensions with length 1.
name: If provided, replace current tensor's name.

Returns:
A `Tensor`.


def _multi_worker_session(kwargs):
    strategy = None
    for _, v in kwargs.items():
        if isinstance(v, distribute_lib.StrategyBase):
            if strategy is not None:
                logging.warning('The test uses multiple strategies. Skipping entering a session that is configured for the strategy.')
                return ops.NullContextmanager()
            strategy = v
    if context.executing_eagerly() or not isinstance(strategy, collective_all_reduce_strategy.CollectiveAllReduceStrategy):
        return ops.NullContextmanager()
    sess_config = copy.deepcopy(context.context().config)
    sess_config = strategy.update_config_proto(sess_config)
    target = strategy.cluster_resolver.master()
    return session.Session(config=sess_config, target=target).as_default()

Returns a context manager that enters a session that is configured for the MultiWorkerMirroredStrategy.

Args:
kwargs: a dict. Keyword arguments passed to the test.

Returns:
A context manager. If MultiWorkerMirroredStrategy is the  one and only one
strategy in kwargs and it's in graph mode, it's the session that is
configured for that strategy.  Otherwise, it's a no-op context manager.


def _parse_mtu(self, config):
    match = re.search('mtu (\\d+)', config)
    return dict(mtu=int(match.group(1)))

Parses the config block and returns the configured IP MTU value

The provided configuration block is scanned and the configured value
for the IP MTU is returned as a dict object.  The IP MTU value is
expected to always be present in the provided config block

Args:
config (str): The interface configuration block to parse

Return:
dict: A dict object intended to be merged into the resource dict


def print_schema_results(results, level=0):

    for error in results.errors:
        print_level(logger.error, _RED + "[X] %s", level, error)

Print JSON Schema validation errors to stdout.

Args:
results: An instance of ObjectValidationResults.
level: The level at which to print the results.


def get_id_transcripts(self, hgnc_id, build='37'):

        transcripts = self.transcripts(build=build, hgnc_id=hgnc_id)

        identifier_transcripts = set()
        longest = None
        nr = []
        xm = []
        for tx in transcripts:
            enst_id = tx['transcript_id']

            if not longest:
                longest = enst_id
            refseq_id = tx.get('refseq_id')
            if not refseq_id:
                continue

            if 'NM' in refseq_id:
                identifier_transcripts.add(enst_id)
            elif 'NR' in refseq_id:
                nr.append(enst_id)
            elif 'XM' in refseq_id:
                xm.append(enst_id)

        if identifier_transcripts:
            return identifier_transcripts

        if nr:
            return set([nr[0]])

        if xm:
            return set([xm[0]])

        return set([longest])

Return a set with identifier transcript(s)

Choose all refseq transcripts with NM symbols, if none where found choose ONE with NR,
if no NR choose ONE with XM. If there are no RefSeq transcripts identifiers choose the
longest ensembl transcript.

Args:
hgnc_id(int)
build(str)

Returns:
identifier_transcripts(set)


def _is_univariate_marginal(self, index_points):
    num_index_points = tf.compat.dimension_value(index_points.shape[(- (self.kernel.feature_ndims + 1))])
    if (num_index_points is None):
        warnings.warn('Unable to detect statically whether the number of index_points is 1. As a result, defaulting to treating the marginal GP at `index_points` as a multivariate Gaussian. This makes some methods, like `cdf` unavailable.')
    return (num_index_points == 1)

True if the given index_points would yield a univariate marginal.

Args:
index_points: the set of index set locations at which to compute the
marginal Gaussian distribution. If this set is of size 1, the marginal is
univariate.

Returns:
is_univariate: Boolean indicating whether the marginal is univariate or
multivariate. In the case of dynamic shape in the number of index points,
defaults to "multivariate" since that's the best we can do.


def get_interface(self):
    raise NotImplementedError('Base class should not be called directly!')

This function returns The interface used to configure the sniffer,
e.g. 'wlan0'.

Returns:
The interface (string) used to configure the sniffer. Corresponds to
the 'Interface' key of the sniffer configuration.


def update_parser(self, parser):
    self._parser = parser
    ini_str = argparse_to_ini(parser)
    configp = configparser.ConfigParser(allow_no_value=True)
    configp.read_dict(self._config)
    configp.read_string(ini_str)
    self._config.update({s: dict(configp.items(s)) for s in configp.sections()})

Update config dictionary with declared arguments in an argparse.parser
New variables will be created, and existing ones overridden.

Args:
parser (argparse.ArgumentParser): parser to read variables from


def __init__(self, app, env, region, prop_path):

        self.app_name = app
        self.env = env
        self.region = region
        self.properties = get_properties(prop_path)
        generated = get_details(app=self.app_name)
        self.group = generated.data['project']

        try:
            self.pipeline = self.properties['pipeline']['lambda']
        except KeyError:
            raise RequiredKeyNotFound("Lambda key in pipeline.json is required.")

        self.runtime = self.pipeline['runtime']
        self.description = self.pipeline['app_description']
        self.handler = self.pipeline['handler']
        self.vpc_enabled = self.pipeline['vpc_enabled']

        self.settings = get_properties(prop_path, env=self.env, region=self.region)
        app = self.settings['app']
        self.lambda_environment = app['lambda_environment']
        self.memory = app['lambda_memory']
        self.role = app.get('lambda_role') or generated.iam()['lambda_role']
        self.timeout = app['lambda_timeout']
        self.concurrency_limit = app.get('lambda_concurrency_limit')

        self.role_arn = get_role_arn(self.role, self.env, self.region)

        self.session = boto3.Session(profile_name=self.env, region_name=self.region)
        self.lambda_client = self.session.client('lambda')

Lambda function object.

Args:
app (str): Application name
env (str): Environment/Account
region (str): AWS Region
prop_path (str): Path of environment property file


def _parse_deploy(self, deploy_values: dict, service_config: dict):
    mode = {}
    for d_value in deploy_values:
        if ('restart_policy' in d_value):
            restart_spec = docker.types.RestartPolicy(**deploy_values[d_value])
            service_config['restart_policy'] = restart_spec
        if ('placement' in d_value):
            for (constraints_key, constraints_value) in deploy_values[d_value].items():
                service_config[constraints_key] = constraints_value
        if ('mode' in d_value):
            mode[d_value] = deploy_values[d_value]
        if ('replicas' in d_value):
            mode[d_value] = deploy_values[d_value]
        if ('resources' in d_value):
            resource_spec = self._parse_resources(deploy_values, d_value)
            service_config['resources'] = resource_spec
    mode_spec = docker.types.ServiceMode(**mode)
    service_config['mode'] = mode_spec

Parse deploy key.

Args:
deploy_values (dict): deploy configuration values
service_config (dict): Service configuration


def drop(self, index=None, columns=None):
    if self._is_transposed:
        return self.transpose().drop(index=columns, columns=index).transpose()
    if (index is None):
        new_data = self.data
        new_index = self.index
    else:

        def delitem(df, internal_indices=[]):
            return df.drop(index=df.index[internal_indices])
        numeric_indices = list(self.index.get_indexer_for(index))
        new_data = self.data.apply_func_to_select_indices(1, delitem, numeric_indices, keep_remaining=True)
        new_index = self.index[(~ self.index.isin(index))]
    if (columns is None):
        new_columns = self.columns
        new_dtypes = self.dtypes
    else:

        def delitem(df, internal_indices=[]):
            return df.drop(columns=df.columns[internal_indices])
        numeric_indices = list(self.columns.get_indexer_for(columns))
        new_data = new_data.apply_func_to_select_indices(0, delitem, numeric_indices, keep_remaining=True)
        new_columns = self.columns[(~ self.columns.isin(columns))]
        new_dtypes = self.dtypes.drop(columns)
    return self.__constructor__(new_data, new_index, new_columns, new_dtypes)

Remove row data for target index and columns.

Args:
index: Target index to drop.
columns: Target columns to drop.

Returns:
A new QueryCompiler.


def zeros(shape, dtype=None, **kwargs):
    data = np.zeros(shape, dtype)
    return dc.array(data, **kwargs)

Create an array of given shape and type, filled with zeros.

Args:
shape (sequence of ints): 2D shape of the array.
dtype (data-type, optional): Desired data-type for the array.
kwargs (optional): Other arguments of the array (*coords, attrs, and name).

Returns:
array (decode.array): Decode array filled with zeros.


def __init__(self, rate=None, burst_size=None, prec_level=None):

        super().__init__(MeterBandType.OFPMBT_DSCP_REMARK, rate, burst_size)
        self.prec_level = prec_level

Create a MeterBandDscpRemark with the optional parameters below.

Args:
rate (int): Rate for remarking packets.
burst_size (int): Size of bursts.
prec_level (int): Number of precendence level to substract.


def _create_config_proto(self) -> tpu_embedding_configuration_pb2.TPUEmbeddingConfiguration:
    config_proto = tpu_embedding_configuration_pb2.TPUEmbeddingConfiguration()
    learning_rate_index = {r: i for i, r in enumerate(self._dynamic_learning_rates)}
    for table in self._table_config:
        table._set_table_descriptor(config_proto.table_descriptor.add(), self._strategy.extended.num_hosts, learning_rate_index)
    table_to_id = {table: i for i, table in enumerate(self._table_config)}
    for feature, output_shape in zip(nest.flatten(self._feature_config), self._output_shapes):
        feature_descriptor = config_proto.feature_descriptor.add()
        if feature.name:
            feature_descriptor.name = feature.name
        feature_descriptor.table_id = table_to_id[feature.table]
        feature_descriptor.input_shape.extend(output_shape.as_list())
    config_proto.mode = tpu_embedding_configuration_pb2.TPUEmbeddingConfiguration.TRAINING
    num_replica = self._strategy.num_replicas_in_sync
    num_cores_per_replica = self._num_cores_per_replica or 1
    config_proto.num_hosts = self._strategy.extended.num_hosts
    config_proto.num_tensor_cores = num_replica * num_cores_per_replica
    config_proto.sharding_strategy = tpu_embedding_configuration_pb2.TPUEmbeddingConfiguration.DIV_DEFAULT
    config_proto.pipeline_execution_with_tensor_core = self._pipeline_execution_with_tensor_core
    if self._num_cores_per_replica:
        config_proto.spmd_sharding.enabled = True
        config_proto.spmd_sharding.num_cores_per_replica = self._num_cores_per_replica
    return config_proto

Creates the TPUEmbeddingConfiguration proto.

This proto is used to initialize the TPU embedding engine.

Returns:
A TPUEmbeddingConfiguration proto.


def load_dict_values(self, db_key: str, dict_keys: List[str], hierarchical: bool=False) -> List:
    result = []
    if (not hierarchical):
        _values = self._db.hmget(db_key, *dict_keys)
        result = [ast.literal_eval(_value) for _value in _values]
    else:
        db_keys = self._db.keys(pattern=(db_key + '*'))
        for _db_key in db_keys:
            for name in _db_key.split(':')[1:]:
                if (name in dict_keys):
                    _values = self._load_values(_db_key)
                    result.append(_values)
            _values = self._db.hmget(_db_key, *dict_keys)
            for (i, value) in enumerate(_values):
                try:
                    _values[i] = ast.literal_eval(value)
                except SyntaxError:
                    pass
                except ValueError:
                    pass
            result += [value for value in _values if (value is not None)]
    return result

Load values from a dictionary with the specified dict_keys.

Args:
db_key (str): Key where the dictionary is stored
dict_keys (List[str]): Keys within the dictionary to load.
hierarchical (bool): If True, expect the dictionary to have been
stored hierarchically. If False, expect the dictionary to have
been stored flat.

Returns:
object: The value stored at dict_key in the dictionary stored at
key


def _range_along_dimension(range_dim, shape):
    rank = len(shape)
    if (range_dim >= rank):
        raise ValueError('Cannot calculate range along non-existent index.')
    indices = tf.range(start=0, limit=shape[range_dim])
    indices = tf.reshape(indices, shape=[(1 if (i != range_dim) else shape[range_dim]) for i in range(rank)])
    return tf.tile(indices, [(shape[i] if (i != range_dim) else 1) for i in range(rank)])

Construct a Tensor whose values are the index along a dimension.

Construct a Tensor that counts the distance along a single dimension. This is
useful, for example, when constructing an identity matrix,

>>> x = _range_along_dimension(0, [2, 2]).eval()
>>> x
array([[0, 0],
[1, 1]], dtype=int32)

>>> y = _range_along_dimension(1, [2, 2]).eval()
>>> y
array([[0, 1],
[0, 1]], dtype=int32)

>>> tf.cast(tf.equal(x, y), dtype=tf.int32).eval()
array([[1, 0],
[0, 1]], dtype=int32)

Args:
range_dim: int. Dimension to count indices on.
shape: 1D Tensor of ints. Shape of Tensor to construct.

Returns:
A Tensor whose values are the same as the range along dimension range_dim.

Raises:
ValueError: If range_dim isn't a valid dimension.


def write_to_hdf5(self, filename_out, *args, **kwargs):
    t0 = time.time()
    self.__update_header()
    if self.container.isheavy():
        self.__write_to_hdf5_heavy(filename_out)
    else:
        self.__write_to_hdf5_light(filename_out)
    t1 = time.time()
    logger.info(('Conversion time: %2.2fsec' % (t1 - t0)))

Write data to HDF5 file.
It check the file size then decides how to write the file.

Args:
filename_out (str): Name of output file


def create_tree(profile, tree):
    resource = '/trees'
    payload = {'tree': tree}
    data = api.post_request(profile, resource, payload)
    return prepare(data)

Create a new tree.

Args:

profile
A profile generated from ``simplygithub.authentication.profile``.
Such profiles tell this module (i) the ``repo`` to connect to,
and (ii) the ``token`` to connect with.

tree
A list of blob objects (each with a path, mode, type, and
content or sha) to put in the tree.

Returns:
A dict with data about the tree.


def EnableNetworkInterfaces(
      self, interfaces, logger, dhclient_script=None):

    if os.path.exists(self.network_path):
      self._DisableNetworkManager(interfaces, logger)
    helpers.CallDhclient(interfaces, logger)

Enable the list of network interfaces.

Args:
interfaces: list of string, the output device names to enable.
logger: logger object, used to write to SysLog and serial port.
dhclient_script: string, the path to a dhclient script used by dhclient.


def prefetch_users(persistent_course_grades):
    users = User.objects.filter(id__in=[grade.user_id for grade in persistent_course_grades])
    return {user.id: user for user in users}

Prefetch Users from the list of user_ids present in the persistent_course_grades.

Arguments:
persistent_course_grades (list): A list of PersistentCourseGrade.

Returns:
(dict): A dictionary containing user_id to user mapping.


def unique_array(arr):

    if not len(arr):
        return np.asarray(arr)
    elif pd:
        if isinstance(arr, np.ndarray) and arr.dtype.kind not in 'MO':

            return pd.unique(arr)

        values = []
        for v in arr:
            if (isinstance(v, datetime_types) and
                not isinstance(v, cftime_types)):
                v = pd.Timestamp(v).to_datetime64()
            values.append(v)
        return pd.unique(values)
    else:
        arr = np.asarray(arr)
        _, uniq_inds = np.unique(arr, return_index=True)
        return arr[np.sort(uniq_inds)]

Returns an array of unique values in the input order.

Args:
arr (np.ndarray or list): The array to compute unique values on

Returns:
A new array of unique values


def _transform_binary_composition_to_expression(expression, node, context):
    if (expression.operator not in constants.SUPPORTED_OPERATORS):
        raise NotImplementedError(u'Filter operation "{}" is not supported by the SQL backend.'.format(expression.operator))
    sql_operator = constants.SUPPORTED_OPERATORS[expression.operator]
    left = _expression_to_sql(expression.left, node, context)
    right = _expression_to_sql(expression.right, node, context)
    if (sql_operator.cardinality == constants.CARDINALITY_UNARY):
        (left, right) = _get_column_and_bindparam(left, right, sql_operator)
        clause = getattr(left, sql_operator.name)(right)
        return clause
    elif (sql_operator.cardinality == constants.CARDINALITY_BINARY):
        clause = getattr(sql_expressions, sql_operator.name)(left, right)
        return clause
    elif (sql_operator.cardinality == constants.CARDINALITY_LIST_VALUED):
        (left, right) = _get_column_and_bindparam(left, right, sql_operator)
        right.expanding = True
        clause = getattr(left, sql_operator.name)(right)
        return clause
    raise AssertionError(u'Unreachable, operator cardinality {} for compiler expression {} is unknown'.format(sql_operator.cardinality, expression))

Transform a BinaryComposition compiler expression into a SQLAlchemy expression.

Recursively calls _expression_to_sql to convert its left and right sub-expressions.

Args:
expression: expression, BinaryComposition compiler expression.
node: SqlNode, the SqlNode the expression applies to.
context: CompilationContext, global compilation state and metadata.

Returns:
Expression, SQLAlchemy expression.


def split_raster(rs, split_shp, field_name, temp_dir):

        UtilClass.rmmkdir(temp_dir)
        ds = ogr_Open(split_shp)
        lyr = ds.GetLayer(0)
        lyr.ResetReading()
        ft = lyr.GetNextFeature()
        while ft:
            cur_field_name = ft.GetFieldAsString(field_name)
            for r in rs:
                cur_file_name = r.split(os.sep)[-1]
                outraster = temp_dir + os.sep + \
                            cur_file_name.replace('.tif', '_%s.tif' %
                                                  cur_field_name.replace(' ', '_'))
                subprocess.call(['gdalwarp', r, outraster, '-cutline', split_shp,
                                 '-crop_to_cutline', '-cwhere',
                                 "'%s'='%s'" % (field_name, cur_field_name), '-dstnodata',
                                 '-9999'])
            ft = lyr.GetNextFeature()
        ds = None

Split raster by given shapefile and field name.

Args:
rs: origin raster file.
split_shp: boundary (ESRI Shapefile) used to spilt raster.
field_name: field name identify the spilt value.
temp_dir: directory to store the spilt rasters.


def _CreateRouteShapesFolder(self, schedule, parent, route, style_id=None, visible=True):
    shape_id_to_trips = {}
    for trip in route.trips:
        if trip.shape_id:
            shape_id_to_trips.setdefault(trip.shape_id, []).append(trip)
    if (not shape_id_to_trips):
        return None
    shape_id_to_trips_items = shape_id_to_trips.items()
    shape_id_to_trips_items.sort((lambda a, b: cmp(len(b[1]), len(a[1]))))
    folder = self._CreateFolder(parent, 'Shapes', visible)
    for (shape_id, trips) in shape_id_to_trips_items:
        trip_ids = [trip.trip_id for trip in trips]
        name = ('%s (trips: %d)' % (shape_id, len(trips)))
        description = ('Trips using this shape (%d in total): %s' % (len(trips), ', '.join(trip_ids)))
        placemark = self._CreatePlacemark(folder, name, style_id, visible, description)
        self._CreateLineStringForShape(placemark, schedule.GetShape(shape_id))
    return folder

Create a KML Folder for the shapes of a route.

The folder contains a placemark for each shape referenced by a trip in the
route. If there are no such shapes, no folder is created and None is
returned.

Args:
schedule: The transitfeed.Schedule instance.
parent: The parent ElementTree.Element instance.
route: The transitfeed.Route instance.
style_id: The id of a style to use if not None.
visible: Whether the placemark is initially visible or not.

Returns:
The Folder ElementTree.Element instance or None.


def sendfrom(self, user_id, dest_address, amount, minconf=1):

        amount = Decimal(amount).quantize(self.quantum, rounding=ROUND_HALF_EVEN)
        txhash = self.rpc.call("sendfrom",
            user_id, dest_address, float(str(amount)), minconf
        )
        self.logger.debug("Send %s %s from %s to %s" % (str(amount), self.coin,
                                                        str(user_id), dest_address))
        self.logger.debug("Transaction hash: %s" % txhash)
        return txhash

Send coins from user's account.

Args:
user_id (str): this user's unique identifier
dest_address (str): address which is to receive coins
amount (str or Decimal): amount to send (eight decimal points)
minconf (int): ensure the account has a valid balance using this
many confirmations (default=1)

Returns:
str: transaction ID


def garbage_collection(time_limit=YEAR/12.0):

    expired_request_infos = (
        ri for ri in DATABASE.values()
        if ri.creation_ts + time_limit <= time.time()
    )

    for ri in expired_request_infos:
        del DATABASE[ri.url]

Collect and remove all :class:`.RequestInfo` objects older than
`time_limit` (in seconds).

Args:
time_limit (float, default YEAR / 2): Collect objects older than
this limit.


def padding_to_length(padding):
    non_padding = (1.0 - padding)
    return tf.to_int32(tf.reduce_sum(non_padding, axis=(- 1)))

Calculate the length of mask based on padding.

Args:
padding: a Tensor with shape [..., length].
Returns:
a Tensor with shape [...].


def _init_request_logging(self, app):

        enabled = not app.config.get(CONF_DISABLE_REQUEST_LOGGING, False)

        if not enabled:
            return

        self._requests_middleware = WSGIApplication(
            self._key, app.wsgi_app, telemetry_channel=self._channel)

        app.wsgi_app = self._requests_middleware

Sets up request logging unless ``APPINSIGHTS_DISABLE_REQUEST_LOGGING``
is set in the Flask config.

Args:
app (flask.Flask). the Flask application for which to initialize the extension.


def residual_block_layer(inputs, hparams):
    kernel = (hparams.res_kernel_size, hparams.res_kernel_size)
    x = inputs
    for i in range(hparams.num_res_layers):
        with tf.variable_scope(('res_conv_%d' % i)):
            y = common_layers.conv_block(common_layers.layer_norm(x, hparams.hidden_size, name='lnorm'), hparams.hidden_size, [((1, 1), kernel)], strides=(1, 1), padding='SAME', name='residual_conv')
            y = common_layers.conv_block(y, hparams.hidden_size, [((1, 1), (1, 1))], strides=(1, 1), padding='SAME', name='residual_dense')
            x = common_layers.layer_postprocess(x, y, hparams)
    return x

Residual block over inputs.

Runs a residual block consisting of
conv: kernel_size x kernel_size
conv: 1x1
dropout, add and normalize according to hparams.layer_postprocess_sequence.

Args:
inputs: Tensor of shape [batch, height, width, hparams.hidden_size].
hparams: HParams.

Returns:
Tensor of shape [batch, height, width, hparams.hidden_size].


def NamedSelector(name, fields, description=None, type_attributes=DEFAULT_TYPE_ATTRIBUTES):

    check.str_param(name, 'name')
    check_user_facing_fields_dict(fields, 'NamedSelector named "{}"'.format(name))

    class _NamedSelector(_ConfigSelector):
        def __init__(self):
            super(_NamedSelector, self).__init__(
                key=name,
                name=name,
                fields=fields,
                description=description,
                type_attributes=type_attributes,
            )

    return _NamedSelector

A :py:class`Selector` with a name, allowing it to be referenced by that name.

Args:
name (str):
fields (Dict[str, Field])


def call_each(seq):

    try:
        reduce(lambda _, y: y(), seq)
    except TypeError as e:
        if text_type(e) != "reduce() of empty sequence with no initial value":
            raise

Calls each element of sequence to invoke the side effect.

Args:
seq:

Returns: None


def predict_features(self, df_features, df_target, nh=20, idx=0, dropout=0.0, activation_function=th.nn.ReLU, lr=0.01, l1=0.1, batch_size=(- 1), train_epochs=1000, test_epochs=1000, device=None, verbose=None, nb_runs=3):
    (device, verbose) = SETTINGS.get_default(('device', device), ('verbose', verbose))
    x = th.FloatTensor(scale(df_features.values)).to(device)
    y = th.FloatTensor(scale(df_target.values)).to(device)
    out = []
    for i in range(nb_runs):
        model = FSGNN_model([(x.size()[1] + 1), nh, 1], dropout=dropout, activation_function=activation_function).to(device)
        out.append(model.train(x, y, lr=0.01, l1=0.1, batch_size=(- 1), train_epochs=train_epochs, test_epochs=test_epochs, device=device, verbose=verbose))
    return list(np.mean(np.array(out), axis=0))

For one variable, predict its neighbours.

Args:
df_features (pandas.DataFrame):
df_target (pandas.Series):
nh (int): number of hidden units
idx (int): (optional) for printing purposes
dropout (float): probability of dropout (between 0 and 1)
activation_function (torch.nn.Module): activation function of the NN
lr (float): learning rate of Adam
l1 (float): L1 penalization coefficient
batch_size (int): batch size, defaults to full-batch
train_epochs (int): number of train epochs
test_epochs (int): number of test epochs
device (str): cuda or cpu device (defaults to ``cdt.SETTINGS.default_device``)
verbose (bool): verbosity (defaults to ``cdt.SETTINGS.verbose``)
nb_runs (int): number of bootstrap runs

Returns:
list: scores of each feature relatively to the target


def update_vlan(self, name, vid, vni):
    cmd = ('vxlan vlan %s vni %s' % (vid, vni))
    return self.configure_interface(name, cmd)

Adds a new vlan to vni mapping for the interface

EosVersion:
4.13.7M

Args:
vlan (str, int): The vlan id to map to the vni
vni (str, int): The vni value to use

Returns:
True if the command completes successfully


def delete_vnet(access_token, subscription_id, resource_group, name):

    endpoint = ''.join([get_rm_endpoint(),
                        '/subscriptions/', subscription_id,
                        '/resourceGroups/', resource_group,
                        '/providers/Microsoft.Network/virtualNetworks/', name,
                        '?api-version=', NETWORK_API])
    return do_delete(endpoint, access_token)

Delete a virtual network.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.
resource_group (str): Azure resource group name.
name (str): Name of the VNet.

Returns:
HTTP response. VNet JSON body.


def __init__(self, target='', graph=None, config=None):
    _python_session_create_counter.get_cell().increase_by(1)
    if graph is None:
        self._graph = ops.get_default_graph()
    else:
        if not isinstance(graph, ops.Graph):
            raise TypeError(f'Argument `graph` must be a tf.Graph, but got "{type(graph).__name__}"')
        self._graph = graph
    self._closed = False
    if target is not None:
        try:
            self._target = compat.as_bytes(target)
        except TypeError:
            if isinstance(target, config_pb2.ConfigProto):
                raise TypeError(f'Argument `target` must be a string, but got "{type(target).__name__}". Did you do "Session(config)" instead of "Session(config=config)"?')
            raise TypeError(f'Argument `target` must be a string, but got "{type(target).__name__}"')
    else:
        self._target = None
    self._delete_lock = threading.Lock()
    self._dead_handles = []
    if config is None:
        config = context.context().config
    if not isinstance(config, config_pb2.ConfigProto):
        raise TypeError(f'Argument `config` must be a tf.ConfigProto, but got "{type(config).__name__}"')
    if mixed_precision_global_state.is_mixed_precision_graph_rewrite_enabled() and config.graph_options.rewrite_options.auto_mixed_precision != rewriter_config_pb2.RewriterConfig.OFF:
        new_config = config_pb2.ConfigProto()
        new_config.CopyFrom(config)
        new_config.graph_options.rewrite_options.auto_mixed_precision = rewriter_config_pb2.RewriterConfig.ON
        config = new_config
    elif config.graph_options.rewrite_options.auto_mixed_precision != rewriter_config_pb2.RewriterConfig.ON:
        mixed_precision_global_state.set_non_mixed_precision_session_created(True)
    self._config = config
    self._add_shapes = config.graph_options.infer_shapes
    self._session = None
    opts = tf_session.TF_NewSessionOptions(target=self._target, config=config)
    try:
        with self._graph._c_graph.get() as c_graph:
            self._session = tf_session.TF_NewSessionRef(c_graph, opts)
    finally:
        tf_session.TF_DeleteSessionOptions(opts)

Constructs a new TensorFlow session.

Args:
target: (Optional) The TensorFlow execution engine to connect to.
graph: (Optional) The graph to be used. If this argument is None, the
default graph will be used.
config: (Optional) ConfigProto proto used to configure the session. If no
config is specified, the global default will be used. The global default
can be configured via the tf.config APIs.

Raises:
tf.errors.OpError: Or one of its subclasses if an error occurs while
creating the TensorFlow session.
TypeError: If one of the arguments has the wrong type.


def from_pymatgen_molecule(cls, molecule):

        new = cls(atoms=[el.value for el in molecule.species],
                  coords=molecule.cart_coords)
        return new._to_numeric()

Create an instance of the own class from a pymatgen molecule

Args:
molecule (:class:`pymatgen.core.structure.Molecule`):

Returns:
Cartesian:


def StatEntryFromPath(path, pathspec, ext_attrs=True):
    try:
        stat = filesystem.Stat.FromPath(path)
    except (IOError, OSError) as error:
        logging.error("Failed to obtain stat for '%s': %s", pathspec, error)
        return rdf_client_fs.StatEntry(pathspec=pathspec)
    return StatEntryFromStat(stat, pathspec, ext_attrs=ext_attrs)

Builds a stat entry object from a given path.

Args:
path: A path (string value) to stat.
pathspec: A `PathSpec` corresponding to the `path`.
ext_attrs: Whether to include extended file attributes in the result.

Returns:
`StatEntry` object.