def replace_with_vptq_linear(model, quantization_config=None, modules_to_not_convert=None, current_key_name=None, has_been_replaced=False):
    modules_to_not_convert = ['lm_head'] if not modules_to_not_convert else modules_to_not_convert
    for name, module in model.named_children():
        if current_key_name is None:
            current_key_name = []
        current_key_name.append(name)
        layer_name = '.'.join(current_key_name)
        shared_layer_config = quantization_config.shared_layer_config
        config_for_layers = quantization_config.config_for_layers
        if isinstance(module, nn.Linear) and layer_name not in modules_to_not_convert and (layer_name in config_for_layers or current_key_name[-1] in shared_layer_config):
            layer_params = config_for_layers.get(layer_name, None) or shared_layer_config.get(current_key_name[-1], None)
            with init_empty_weights():
                in_features = module.in_features
                out_features = module.out_features
                model._modules[name] = VQuantLinear(in_features, out_features, vector_lens=layer_params['vector_lens'], num_centroids=layer_params['num_centroids'], num_res_centroids=layer_params['num_res_centroids'], group_num=layer_params['group_num'], group_size=layer_params['group_size'], outlier_size=layer_params['outlier_size'], indices_as_float=layer_params['indices_as_float'], enable_norm=layer_params['enable_norm'], enable_perm=layer_params['enable_perm'], is_indice_packed=True, enable_proxy_error=False, bias=module.bias is not None)
                has_been_replaced = True
                model._modules[name].requires_grad_(False)
        if len(list(module.children())) > 0:
            _, has_been_replaced = replace_with_vptq_linear(module, quantization_config=quantization_config, modules_to_not_convert=modules_to_not_convert, current_key_name=current_key_name, has_been_replaced=has_been_replaced)
        current_key_name.pop(-1)
    return (model, has_been_replaced)

Public method that recursively replaces the Linear layers of the given model with VPTQ quantized layers.
`accelerate` is needed to use this method. Returns the converted model and a boolean that indicates if the
conversion has been successful or not.

Args:
model (`torch.nn.Module`):
The model to convert, can be any `torch.nn.Module` instance.
quantization_config (`VptqConfig`):
The quantization config object that contains the quantization parameters.
modules_to_not_convert (`List[`str`]`, *optional*, defaults to `["lm_head"]`):
Names of the modules to not convert in `VQuantLinear`. In practice we keep the `lm_head` in full precision
for numerical stability reasons.
current_key_name (`list`, *optional*):
A list that contains the current key name. This is used for recursion and should not be passed by the user.
has_been_replaced (`bool`, *optional*):
A boolean that indicates if the conversion has been successful or not. This is used for recursion and
should not be passed by the user.


def get_object(cls, api_token, droplet_id):
    droplet = cls(token=api_token, id=droplet_id)
    droplet.load()
    return droplet

Class method that will return a Droplet object by ID.

Args:
api_token (str): token
droplet_id (int): droplet id


def is_connected(self):
    if (self._client is not None):
        try:
            self._client.server_info()
        except ConnectionFailure:
            return False
        return True
    else:
        return False

Returns the connection status of the data store.

Returns:
bool: ``True`` if the data store is connected to the MongoDB server.


def call_for_each_tower(
            towers, func, devices=None, use_vs=None):

        ret = []
        if devices is not None:
            assert len(devices) == len(towers)
        if use_vs is not None:
            assert len(use_vs) == len(towers)

        tower_names = ['tower{}'.format(idx) for idx in range(len(towers))]

        for idx, t in enumerate(towers):
            device = devices[idx] if devices is not None else '/gpu:{}'.format(t)
            usevs = use_vs[idx] if use_vs is not None else False
            reuse = not usevs and idx > 0
            with tfv1.device(device), _maybe_reuse_vs(reuse), TrainTowerContext(
                    tower_names[idx],
                    vs_name=tower_names[idx] if usevs else '',
                    index=idx, total=len(towers)):
                if len(str(device)) < 10:
                    logger.info("Building graph for training tower {} on device {} ...".format(idx, device))
                else:
                    logger.info("Building graph for training tower {} ...".format(idx))

                with override_to_local_variable(enable=usevs):
                    ret.append(func())
        return ret

Run `func` on all GPUs (towers) and return the results.

Args:
towers (list[int]): a list of GPU id.
func: a lambda to be called inside each tower
devices: a list of devices to be used. By default will use '/gpu:{tower}'
use_vs (list[bool]): list of use_vs to passed to TowerContext

Returns:
List of outputs of ``func``, evaluated on each tower.


def __init__(self, layers=None, name=None):
    super(functional.Functional, self).__init__(name=name, autocast=False)
    self.supports_masking = True
    self._compute_output_and_mask_jointly = True
    self._auto_track_sub_layers = False
    self._inferred_input_shape = None
    self._has_explicit_input_shape = False
    self._input_dtype = None
    self._layer_call_argspecs = {}
    self._created_nodes = set()
    self._graph_initialized = False
    self._use_legacy_deferred_behavior = False
    if layers:
        if not isinstance(layers, (list, tuple)):
            layers = [layers]
        for layer in layers:
            self.add(layer)

Creates a `Sequential` model instance.

Args:
layers: Optional list of layers to add to the model.
name: Optional name for the model.


def has_neigh(tag_name, params=None, content=None, left=True):

    def has_neigh_closure(element):
        if ((not element.parent) or (not (element.isTag() and (not element.isEndTag())))):
            return False
        childs = element.parent.childs
        childs = filter((lambda x: ((x.isTag() and (not x.isEndTag())) or x.getContent().strip() or (x is element))), childs)
        if (len(childs) <= 1):
            return False
        ioe = childs.index(element)
        if (left and (ioe > 0)):
            return is_equal_tag(childs[(ioe - 1)], tag_name, params, content)
        if ((not left) and ((ioe + 1) < len(childs))):
            return is_equal_tag(childs[(ioe + 1)], tag_name, params, content)
        return False
    return has_neigh_closure

This function generates functions, which matches all tags with neighbours
defined by parameters.

Args:
tag_name (str): Tag has to have neighbour with this tagname.
params (dict): Tag has to have neighbour with this parameters.
params (str): Tag has to have neighbour with this content.
left (bool, default True): Tag has to have neigbour on the left, or
right (set to ``False``).

Returns:
bool: True for every matching tag.

Note:
This function can be used as parameter for ``.find()`` method in
HTMLElement.


def inverse_stft_window_fn(frame_step, forward_window_fn=window_ops.hann_window, name=None):

    def inverse_stft_window_fn_inner(frame_length, dtype):

        with ops.name_scope(name, 'inverse_stft_window_fn', [forward_window_fn]):
            frame_step_ = ops.convert_to_tensor(frame_step, name='frame_step')
            frame_step_.shape.assert_has_rank(0)
            frame_length = ops.convert_to_tensor(frame_length, name='frame_length')
            frame_length.shape.assert_has_rank(0)
            forward_window = forward_window_fn(frame_length, dtype=dtype)
            denom = math_ops.square(forward_window)
            overlaps = -(-frame_length
            denom = array_ops.pad(denom, [(0, overlaps * frame_step_ - frame_length)])
            denom = array_ops.reshape(denom, [overlaps, frame_step_])
            denom = math_ops.reduce_sum(denom, 0, keepdims=True)
            denom = array_ops.tile(denom, [overlaps, 1])
            denom = array_ops.reshape(denom, [overlaps * frame_step_])
            return forward_window / denom[:frame_length]
    return inverse_stft_window_fn_inner

Generates a window function that can be used in `inverse_stft`.

Constructs a window that is equal to the forward window with a further
pointwise amplitude correction.  `inverse_stft_window_fn` is equivalent to
`forward_window_fn` in the case where it would produce an exact inverse.

See examples in `inverse_stft` documentation for usage.

Args:
frame_step: An integer scalar `Tensor`. The number of samples to step.
forward_window_fn: window_fn used in the forward transform, `stft`.
name: An optional name for the operation.

Returns:
A callable that takes a window length and a `dtype` keyword argument and
returns a `[window_length]` `Tensor` of samples in the provided datatype.
The returned window is suitable for reconstructing original waveform in
inverse_stft.


def _get_max_page(dom):
    div = dom.find('div', {'class': 'razeniKnihListovani'})
    if (not div):
        return 1
    links = div[0].find('a')
    max_page = filter((lambda x: (('href' in x.params) and ('pageindex=' in x.params['href']))), links)
    max_page = map((lambda x: x.params['href'].split('pageindex=')[(- 1)]), max_page)
    max_page = filter((lambda x: x.isdigit()), max_page)
    max_page = map((lambda x: int(x)), max_page)
    if (not max_page):
        return 1
    return max(max_page)

Try to guess how much pages are in book listing.

Args:
dom (obj): HTMLElement container of the page with book list.

Returns:
int: Number of pages for given category.


def mark_backward(output_tensor, used_node_names):

  op = output_tensor.op
  if op.name in used_node_names:
    return
  used_node_names.add(op.name)
  for input_tensor in op.inputs:
    mark_backward(input_tensor, used_node_names)
  for control_input_op in op.control_inputs:
    used_node_names.add(control_input_op.name)
    for input_tensor in control_input_op.inputs:
      mark_backward(input_tensor, used_node_names)

Function to propagate backwards in the graph and mark nodes as used.

Traverses recursively through the graph from the end tensor, through the op
that generates the tensor, and then to the input tensors that feed the op.
Nodes encountered are stored in used_node_names.

Args:
output_tensor: A Tensor which we start the propagation.
used_node_names: A list of strings, stores the name of nodes we've marked as
visited.


def get_plugin(self, identifier, cls=None):

        if ((cls is None or cls == 'provider')
                and identifier in self.available_providers):
            return self.available_providers[identifier]
        elif ((cls is None or cls == 'checker')
                and identifier in self.available_checkers):
            return self.available_checkers[identifier]
        return Config.load_local_plugin(identifier)

Return the plugin corresponding to the given identifier and type.

Args:
identifier (str): identifier of the plugin.
cls (str): one of checker / provider.

Returns:
Checker/Provider: plugin class.


def _write_credentials_file(credentials_file, credentials):

    data = {'file_version': 2, 'credentials': {}}

    for key, credential in iteritems(credentials):
        credential_json = credential.to_json()
        encoded_credential = _helpers._from_bytes(base64.b64encode(
            _helpers._to_bytes(credential_json)))
        data['credentials'][key] = encoded_credential

    credentials_file.seek(0)
    json.dump(data, credentials_file)
    credentials_file.truncate()

Writes credentials to a file.

Refer to :func:`_load_credentials_file` for the format.

Args:
credentials_file: An open file handle, must be read/write.
credentials: A dictionary mapping user-defined keys to an instance of
:class:`oauth2client.client.Credentials`.


def __init__(self, primals, tangents):
    self._accumulator = pywrap_tfe.TFE_Py_ForwardAccumulatorNew(False)
    self._recording = False
    primal_ids = set()
    for primal in nest.flatten(primals):
        if id(primal) in primal_ids:
            raise ValueError('Tensor {} was specified as a primal multiple times. This may indicate an error. If it was intended, please sum the corresponding tangents.')
        primal_ids.add(id(primal))
    self._watch(primals, tangents)

Specify tensors to watch and their Jacobian-vector products.

Mathematically, `tangents` is a vector right-multiplying the Jacobian matrix
(a Jacobian-vector product) for the function computed while this accumulator
is active. Since JVPs are computed in forward mode as the computation
happens, this vector must be supplied in advance.

Listing a single tensor multiple times in `primals` raises an
exception. Excluding a tensor from `primals` is equivalent to watching it
with a tangent tensor of zeros.

Args:
primals: A tensor or nested structure of tensors to watch.
tangents: A tensor or nested structure of tensors, with the same nesting
structure as `primals`, with each element being a vector with the same
size as the corresponding primal element.

Raises:
ValueError: If the same tensor or variable is specified multiple times in
`primals`.


def pull(self, device_filename, dest_file=None, timeout_ms=None):

    should_return_data = dest_file is None
    if isinstance(dest_file, six.string_types):
      dest_file = open(dest_file, 'w')
    elif dest_file is None:
      dest_file = six.StringIO()
    self.filesync_service.recv(device_filename, dest_file,
                               timeouts.PolledTimeout.from_millis(timeout_ms))
    if should_return_data:
      return dest_file.getvalue()

Pull file from device.

Arguments:
device_filename: The filename on the device to pull.
dest_file: If set, a filename or writable file-like object.
timeout_ms: Expected timeout for the pull.

Returns:
The file data if dest_file is not set, None otherwise.


def load_fasta_file_as_dict_of_seqs(filename):

    results = {}
    records = load_fasta_file(filename)
    for r in records:
        results[r.id] = str(r.seq)

    return results

Load a FASTA file and return the sequences as a dict of {ID: sequence string}

Args:
filename (str): Path to the FASTA file to load

Returns:
dict: Dictionary of IDs to their sequence strings


def check(self, version):

        for disjunct in self._disjuncts:
            if self._check_insersection(version, disjunct):
                return True

        return False

Check that a version is inside this SemanticVersionRange

Args:
version (SemanticVersion): The version to check

Returns:
bool: True if the version is included in the range, False if not


def _clean_isbn(isbn):
    if isinstance(isbn, basestring):
        isbn = list(isbn.lower())
        isbn = filter((lambda x: (x.isdigit() or (x == 'x'))), isbn)
    return map((lambda x: (10 if (x == 'x') else int(x))), isbn)

Remove all non-digit and non "x" characters from given string.

Args:
isbn (str): isbn string, which will be cleaned.

Returns:
list: array of numbers (if "x" is found, it is converted to 10).


def _register_callback(self, cb):

        if isinstance(cb, (list, tuple)):
            for x in cb:
                self._register_callback(x)
            return
        assert isinstance(cb, Callback), cb
        assert not isinstance(self._callbacks, Callbacks), \
            "Cannot register more callbacks after trainer was setup!"
        if not self.is_chief and cb.chief_only:
            logger.warn("Callback {} is chief-only, skipped.".format(str(cb)))
            return False
        else:
            self._callbacks.append(cb)
            return True

Register callbacks to the trainer.
It can only be called before :meth:`Trainer.train()`.

Args:
cb (Callback or [Callback]): a callback or a list of callbacks

Returns:
succeed or not


async def subscriptions(self, request):
    if (not self._accepting):
        return web.Response(status=503)
    web_sock = web.WebSocketResponse()
    (await web_sock.prepare(request))
    async for msg in web_sock:
        if (msg.type == aiohttp.WSMsgType.TEXT):
            (await self._handle_message(web_sock, msg.data))
        elif (msg.type == aiohttp.WSMsgType.ERROR):
            LOGGER.warning('Web socket connection closed with exception %s', web_sock.exception())
            (await web_sock.close())
    (await self._handle_unsubscribe(web_sock))
    return web_sock

Handles requests for new subscription websockets.

Args:
request (aiohttp.Request): the incoming request

Returns:
aiohttp.web.WebSocketResponse: the websocket response, when the
resulting websocket is closed


def create_d1_dn_subject(common_name_str):
    return cryptography.x509.Name([cryptography.x509.NameAttribute(cryptography.x509.oid.NameOID.COUNTRY_NAME, 'US'), cryptography.x509.NameAttribute(cryptography.x509.oid.NameOID.STATE_OR_PROVINCE_NAME, 'California'), cryptography.x509.NameAttribute(cryptography.x509.oid.NameOID.LOCALITY_NAME, 'San Francisco'), cryptography.x509.NameAttribute(cryptography.x509.oid.NameOID.ORGANIZATION_NAME, 'Root CA'), cryptography.x509.NameAttribute(cryptography.x509.oid.NameOID.COMMON_NAME, 'ca.ca.com')])

Create the DN Subject for certificate that will be used in a DataONE environment.

The DN is formatted into a DataONE subject, which is used in authentication,
authorization and event tracking.

Args:
common_name_str: str
DataONE uses simple DNs without physical location information, so only the
``common_name_str`` (``CommonName``) needs to be specified.

For Member Node Client Side certificates or CSRs, ``common_name_str`` is the
``node_id``, e.g., ``urn:node:ABCD`` for production, or
``urn:node:mnTestABCD`` for the test environments.

For a local CA, something like ``localCA`` may be used.

For a locally trusted client side certificate, something like
``localClient`` may be used.


def define_both_methods(class_name, class_dict, old_name, new_name):
    assert ((old_name not in class_dict) or (new_name not in class_dict)), 'Class "{}" cannot define both "{}" and "{}" methods.'.format(class_name, old_name, new_name)
    if (old_name in class_dict):
        class_dict[new_name] = class_dict[old_name]
    elif (new_name in class_dict):
        class_dict[old_name] = class_dict[new_name]

Function to help CamelCase to PEP8 style class methods migration.

For any class definition:
1. Assert it does not define both old and new methods,
otherwise it does not work.
2. If it defines the old method, create the same new method.
3. If it defines the new method, create the same old method.

Args:
class_name: the class name.
class_dict: the class dictionary.
old_name: old method's name.
new_name: new method's name.

Raises:
AssertionError: raised when the class defines both the old_name and
new_name.


def _GetNextLogCountPerToken(token):
    global _log_counter_per_token
    _log_counter_per_token[token] = 1 + _log_counter_per_token.get(token, -1)
    return _log_counter_per_token[token]

Wrapper for _log_counter_per_token.

Args:
token: The token for which to look up the count.

Returns:
The number of times this function has been called with
*token* as an argument (starting at 0)


def requires_grad(self) -> bool:
    if self._rot_mats is not None:
        return self._rot_mats.requires_grad
    elif self._quats is not None:
        return self._quats.requires_grad
    else:
        raise ValueError('Both rotations are None')

Returns the requires_grad property of the underlying rotation

Returns:
The requires_grad property of the underlying tensor


def copy2(src, dst, metadata=None, retry_params=None):
    common.validate_file_path(src)
    common.validate_file_path(dst)
    if (metadata is None):
        metadata = {}
        copy_meta = 'COPY'
    else:
        copy_meta = 'REPLACE'
    metadata.update({'x-goog-copy-source': src, 'x-goog-metadata-directive': copy_meta})
    api = storage_api._get_storage_api(retry_params=retry_params)
    (status, resp_headers, content) = api.put_object(api_utils._quote_filename(dst), headers=metadata)
    errors.check_status(status, [200], src, metadata, resp_headers, body=content)

Copy the file content from src to dst.

Args:
src: /bucket/filename
dst: /bucket/filename
metadata: a dict of metadata for this copy. If None, old metadata is copied.
For example, {'x-goog-meta-foo': 'bar'}.
retry_params: An api_utils.RetryParams for this call to GCS. If None,
the default one is used.

Raises:
errors.AuthorizationError: if authorization failed.
errors.NotFoundError: if an object that's expected to exist doesn't.


def __init__(self, dataframe, map_info):

        self.df = dataframe
        self.map_info = map_info

Reads genotypes from a pandas DataFrame.

Args:
dataframe (pandas.DataFrame): The data.
map_info (pandas.DataFrame): The mapping information.

Note
====
The index of the dataframe should be the sample IDs. The index of
the map_info should be the variant name, and there should be
columns named chrom and pos.


def prepare_for_send(self, full_url=False):

        assert self.url
        assert self.method
        assert self.version

        url_info = self.url_info

        if 'Host' not in self.fields:
            self.fields['Host'] = url_info.hostname_with_port

        if not full_url:
            if url_info.query:
                self.resource_path = '{0}?{1}'.format(url_info.path, url_info.query)
            else:
                self.resource_path = url_info.path
        else:
            self.resource_path = url_info.url

Modify the request to be suitable for HTTP server.

Args:
full_url (bool): Use full URL as the URI. By default, only
the path of the URL is given to the server.


def open_required(func):

        @functools.wraps(func)
        def wrapper(self, *args, **kwargs):

            if not self.opened():
                raise errors.JLinkException('J-Link DLL is not open.')
            elif not self.connected():
                raise errors.JLinkException('J-Link connection has been lost.')
            return func(self, *args, **kwargs)
        return wrapper

Decorator to specify that the J-Link DLL must be opened, and a
J-Link connection must be established.

Args:
func (function): function being decorated

Returns:
The wrapper function.


def create_database_view(self, view: views.View, view_name: str) -> None:
    dataset = f'{self._view_dataset.project}.{self._view_dataset.dataset_id}'
    view_sql = f'CREATE OR REPLACE VIEW `{dataset}.{view_name}` AS\n{self.to_sql(view)}'
    self._client.query(view_sql).result()

Creates a BigQuery view with the given name in the runner's view_dataset.

Args:
view: the FHIR view that creates
view_name: the view name passed to the CREATE OR REPLACE VIEW statement.

Raises:
google.cloud.exceptions.GoogleAPICallError if the job failed.


def save(self, *, auto_commit=False):

        try:
            db.session.add(self.resource)
            if auto_commit:
                db.session.commit()
        except SQLAlchemyError as ex:
            self.log.exception('Failed updating resource: {}'.format(ex))
            db.session.rollback()

Save the resource to the database

Args:
auto_commit (bool): Automatically commit the transaction. Default: `False`

Returns:
`None`


def check_denotation(target_values, predicted_values):

    if len(target_values) != len(predicted_values):
        return False

    for target in target_values:
        if not any(target.match(pred) for pred in predicted_values):
            return False
    return True

Return True if the predicted denotation is correct.

Args:
target_values (list[Value])
predicted_values (list[Value])
Returns:
bool


def get_list_index(lst, index_or_name):

    if isinstance(index_or_name, six.integer_types):
        return index_or_name

    return lst.index(index_or_name)

Return the index of an element in the list.

Args:
lst (list): The list.
index_or_name (int or str): The value of the reference element, or directly its numeric index.

Returns:
(int) The index of the element in the list.


def _text_io_wrapper(stream, mode, encoding, errors, newline):

    if "t" in mode and not hasattr(stream, 'encoding'):
        text_stream = TextIOWrapper(
            stream, encoding=encoding, errors=errors, newline=newline)
        yield text_stream
        text_stream.flush()

    else:
        yield stream

Wrap a binary stream to Text stream.

Args:
stream (file-like object): binary stream.
mode (str): Open mode.
encoding (str): Stream encoding.
errors (str): Decoding error handling.
newline (str): Universal newlines


async def inspect(self, name: str) -> Mapping:
    response = (await self.docker._query_json('images/{name}/json'.format(name=name)))
    return response

Return low-level information about an image

Args:
name: name of the image


def name_from_base(base, max_length=63, short=False):
    timestamp = (sagemaker_short_timestamp() if short else sagemaker_timestamp())
    trimmed_base = base[:((max_length - len(timestamp)) - 1)]
    return '{}-{}'.format(trimmed_base, timestamp)

Append a timestamp to the provided string.

This function assures that the total length of the resulting string is not
longer than the specified max length, trimming the input parameter if necessary.

Args:
base (str): String used as prefix to generate the unique name.
max_length (int): Maximum length for the resulting string.
short (bool): Whether or not to use a truncated timestamp.

Returns:
str: Input parameter with appended timestamp.


Function used to add test classes, has to be implemented by child class.

Args:
config: config_parser.TestRunConfig, the config provided by google3 infra.

Raises:
Error: when setup_suite is not implemented by child class.


def load_orthologs(fo: IO, metadata: dict):
    version = metadata['metadata']['version']
    with timy.Timer('Load Orthologs') as timer:
        arango_client = arangodb.get_client()
        belns_db = arangodb.get_belns_handle(arango_client)
        arangodb.batch_load_docs(belns_db, orthologs_iterator(fo, version), on_duplicate='update')
        log.info('Load orthologs', elapsed=timer.elapsed, source=metadata['metadata']['source'])
        remove_old_ortholog_edges = f
        remove_old_ortholog_nodes = f
        arangodb.aql_query(belns_db, remove_old_ortholog_edges)
        arangodb.aql_query(belns_db, remove_old_ortholog_nodes)
    metadata['_key'] = f"Orthologs_{metadata['metadata']['source']}"
    try:
        belns_db.collection(arangodb.belns_metadata_name).insert(metadata)
    except ArangoError as ae:
        belns_db.collection(arangodb.belns_metadata_name).replace(metadata)

Load orthologs into ArangoDB

Args:
fo: file obj - orthologs file
metadata: dict containing the metadata for orthologs


def EWFGlobPathSpec(file_system, path_spec):
    if (not path_spec.HasParent()):
        raise errors.PathSpecError('Unsupported path specification without parent.')
    parent_path_spec = path_spec.parent
    parent_location = getattr(parent_path_spec, 'location', None)
    if (not parent_location):
        raise errors.PathSpecError('Unsupported parent path specification without location.')
    (parent_location, _, segment_extension) = parent_location.rpartition('.')
    segment_extension_start = segment_extension[0]
    segment_extension_length = len(segment_extension)
    if ((segment_extension_length not in [3, 4]) or (not segment_extension.endswith('01')) or ((segment_extension_length == 3) and (segment_extension_start not in ['E', 'e', 's'])) or ((segment_extension_length == 4) and (not segment_extension.startswith('Ex')))):
        raise errors.PathSpecError('Unsupported parent path specification invalid segment file extension: {0:s}'.format(segment_extension))
    segment_number = 1
    segment_files = []
    while True:
        segment_location = '{0:s}.{1:s}'.format(parent_location, segment_extension)
        kwargs = path_spec_factory.Factory.GetProperties(parent_path_spec)
        kwargs['location'] = segment_location
        if (parent_path_spec.parent is not None):
            kwargs['parent'] = parent_path_spec.parent
        segment_path_spec = path_spec_factory.Factory.NewPathSpec(parent_path_spec.type_indicator, **kwargs)
        if (not file_system.FileEntryExistsByPathSpec(segment_path_spec)):
            break
        segment_files.append(segment_path_spec)
        segment_number += 1
        if (segment_number <= 99):
            if (segment_extension_length == 3):
                segment_extension = '{0:s}{1:02d}'.format(segment_extension_start, segment_number)
            elif (segment_extension_length == 4):
                segment_extension = '{0:s}x{1:02d}'.format(segment_extension_start, segment_number)
        else:
            segment_index = (segment_number - 100)
            if (segment_extension_start in ['e', 's']):
                letter_offset = ord('a')
            else:
                letter_offset = ord('A')
            (segment_index, remainder) = divmod(segment_index, 26)
            third_letter = chr((letter_offset + remainder))
            (segment_index, remainder) = divmod(segment_index, 26)
            second_letter = chr((letter_offset + remainder))
            first_letter = chr((ord(segment_extension_start) + segment_index))
            if (first_letter in ['[', '{']):
                raise RuntimeError('Unsupported number of segment files.')
            if (segment_extension_length == 3):
                segment_extension = '{0:s}{1:s}{2:s}'.format(first_letter, second_letter, third_letter)
            elif (segment_extension_length == 4):
                segment_extension = '{0:s}x{1:s}{2:s}'.format(first_letter, second_letter, third_letter)
    return segment_files

Globs for path specifications according to the EWF naming schema.

Args:
file_system (FileSystem): file system.
path_spec (PathSpec): path specification.

Returns:
list[PathSpec]: path specifications that match the glob.

Raises:
PathSpecError: if the path specification is invalid.
RuntimeError: if the maximum number of supported segment files is
reached.


def unwrap(self, value):
    return self._extended._local_results(value)

Returns the list of all local per-replica values contained in `value`.

DEPRECATED: Please use `experimental_local_results` instead.

Note: This only returns values on the workers initiated by this client.
When using a `tf.distribute.Strategy` like
`tf.distribute.experimental.MultiWorkerMirroredStrategy`, each worker
will be its own client, and this function will only return values
computed on that worker.

Args:
value: A value returned by `experimental_run()`,
`extended.call_for_each_replica()`, or a variable created in `scope`.

Returns:
A tuple of values contained in `value`. If `value` represents a single
value, this returns `(value,).`


def _ShardTestEmbeddings(self, weights, biases, num_shards):
    with ops.Graph().as_default() as g:
        sharded_weights = variable_scope.get_variable('w', partitioner=partitioned_variables.fixed_size_partitioner(num_shards), initializer=constant_op.constant(weights))
        sharded_biases = variable_scope.get_variable('b', partitioner=partitioned_variables.fixed_size_partitioner(num_shards), initializer=constant_op.constant(biases))
        with self.session(graph=g) as sess:
            self.evaluate(variables.global_variables_initializer())
            return self.evaluate([list(sharded_weights), list(sharded_biases)])

Shards the weights and biases returned by _GenerateTestData.

Args:
weights: The weights returned by _GenerateTestData.
biases: The biases returned by _GenerateTestData.
num_shards: The number of shards to create.

Returns:
sharded_weights: A list of size `num_shards` containing all the weights.
sharded_biases: A list of size `num_shards` containing all the biases.


def read_model_from_bytearray(model_bytearray):
    model = convert_bytearray_to_object(model_bytearray)
    if sys.byteorder == 'big':
        byte_swap_tflite_model_obj(model, 'little', 'big')
    for buffer in model.buffers:
        if buffer.offset:
            buffer.data = model_bytearray[buffer.offset:buffer.offset + buffer.size]
            buffer.offset = 0
            buffer.size = 0
    for subgraph in model.subgraphs:
        for op in subgraph.operators:
            if op.largeCustomOptionsOffset:
                op.customOptions = model_bytearray[op.largeCustomOptionsOffset:op.largeCustomOptionsOffset + op.largeCustomOptionsSize]
                op.largeCustomOptionsOffset = 0
                op.largeCustomOptionsSize = 0
    return model

Reads a tflite model as a python object.

Args:
model_bytearray: TFLite model in bytearray format.

Returns:
A python object corresponding to the input tflite file.


def ParseFloat(text):

  try:

    return float(text)
  except ValueError:

    if _FLOAT_INFINITY.match(text):
      if text[0] == '-':
        return float('-inf')
      else:
        return float('inf')
    elif _FLOAT_NAN.match(text):
      return float('nan')
    else:

      try:
        return float(text.rstrip('f'))
      except ValueError:
        raise ValueError('Couldn\'t parse float: %s' % text)

Parse a floating point number.

Args:
text: Text to parse.

Returns:
The number parsed.

Raises:
ValueError: If a floating point number couldn't be parsed.


def get_branch(profile, name):

    ref = "heads/" + name
    data = refs.get_ref(profile, ref)
    return data

Fetch a branch.

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.

name
The name of the branch to fetch.

Returns:
A dict with data baout the branch.


def __init__(self, file_entry):

    super(LVMVolume, self).__init__(file_entry.name)
    self._file_entry = file_entry

Initializes a LVM volume.

Args:
file_entry (LVMFileEntry): a LVM file entry.


def SmartUnicode(string):

  if isinstance(string, Text):
    return string

  if isinstance(string, bytes):
    return string.decode("utf-8", "ignore")

  if compatibility.PY2:
    return str(string).__native__()
  else:
    return str(string)

Returns a unicode object.

This function will always return a unicode object. It should be used to
guarantee that something is always a unicode object.

Args:
string: The string to convert.

Returns:
a unicode object.


def find_mapreduce_yaml(status_file=__file__):

  checked = set()
  yaml = _find_mapreduce_yaml(os.path.dirname(status_file), checked)
  if not yaml:
    yaml = _find_mapreduce_yaml(os.getcwd(), checked)
  return yaml

Traverse directory trees to find mapreduce.yaml file.

Begins with the location of status.py and then moves on to check the working
directory.

Args:
status_file: location of status.py, overridable for testing purposes.

Returns:
the path of mapreduce.yaml file or None if not found.


def __eq__(self, other):

        if type(self) is type(other) and \
                self._name == other._name and \
                self._params == other._params:
            return True
        return False

Two measurement options are the same if they are of the same type
and have the same name and params.

Args:
other (MeasOpts): Other Discriminator/Kernel.

Returns:
bool: are self and other equal.


def separated(self):
    separated_lls = collections.defaultdict(LabelList)
    for label in self.labels:
        separated_lls[label.value].add(label)
    for ll in separated_lls.values():
        ll.idx = self.idx
    return separated_lls

Create a separate Label-List for every distinct label-value.

Returns:
dict: A dictionary with distinct label-values as keys.
Every value is a LabelList containing only labels with the same value.

Example:
>>> ll = LabelList(idx='some', labels=[
>>>     Label('a', start=0, end=4),
>>>     Label('b', start=3.95, end=6.0),
>>>     Label('a', start=7.0, end=10.2),
>>>     Label('b', start=10.3, end=14.0)
>>> ])
>>> s = ll.separate()
>>> s['a'].labels
[Label('a', start=0, end=4), Label('a', start=7.0, end=10.2)]
>>> s['b'].labels
[Label('b', start=3.95, end=6.0), Label('b', start=10.3, end=14.0)]


def _build_predicate_for_coding_in_value_set(expanded_value_set: value_set_pb2.ValueSet, coding_column: Optional[_sql_data_types.Identifier]=None) -> _sql_data_types.StandardSqlExpression:
    codes_per_system = {}
    for concept in expanded_value_set.expansion.contains:
        codes_per_system.setdefault(concept.system.value, []).append(concept.code.value)
    codes_per_system = list(codes_per_system.items())
    codes_per_system.sort(key=operator.itemgetter(0))
    for _, codes in codes_per_system:
        codes.sort()
    if coding_column is None:
        code_col = _sql_data_types.Identifier('code', _sql_data_types.String)
        system_col = _sql_data_types.Identifier('system', _sql_data_types.String)
    else:
        code_col = coding_column.dot('code', _sql_data_types.String)
        system_col = coding_column.dot('system', _sql_data_types.String)
    code_system_predicates = []
    for system, codes in codes_per_system:
        system = _sql_data_types.RawExpression('"%s"' % system, _sql_data_types.String)
        codes = [_sql_data_types.RawExpression('"%s"' % code, _sql_data_types.String) for code in codes]
        code_system_predicates.append(system_col.eq_(system).and_(code_col.in_(codes)))
    return functools.reduce(lambda acc, pred: acc.or_(pred), code_system_predicates)

Builds a predicate asserting the coding column is bound to the value_set.

Ensures that the codings contained in `coding_column` are codings found in
`expanded_value_set`.
Produces SQL like:
(`coding_column`.system = system1 AND `coding_column`.code IN (
code1, code2)) OR
(`coding_column`.system = system2 AND `coding_column`.code IN (
code3, code4))

Args:
expanded_value_set: The expanded value set containing the coding values to
assert membership against.
coding_column: The column containing the coding values. If given, columns
`coding_column`.system and `coding_column`.code will be referenced in
the predicate. If not given, columns 'system' and 'code' will be
referenced.

Returns:
The SQL for the value set binding predicate.


def _build_dict(my_dict, keys, values):
    temp = my_dict
    for (depth, key) in enumerate(keys):
        if (depth < (len(keys) - 1)):
            if (key not in temp):
                temp[key] = dict()
            temp = temp[key]
        elif (key not in temp):
            temp[key] = values
        else:
            temp[key] = {**temp[key], **values}
    return my_dict

Build a dictionary from a set of redis hashes.

keys = ['a', 'b', 'c']
values = {'value': 'foo'}
my_dict = {'a': {'b': {'c': {'value': 'foo'}}}}

Args:
my_dict (dict): Dictionary to add to
keys (list[str]): List of keys used to define hierarchy in my_dict
values (dict): Values to add at to the dictionary at the key
specified by keys

Returns:
dict, new dictionary with values added at keys


def _GetPropertyValue(self, parser_mediator, properties, property_name):
    property_value = properties.get(property_name, None)
    if isinstance(property_value, py2to3.BYTES_TYPE):
        try:
            property_value = property_value.decode('utf-8')
        except UnicodeDecodeError:
            parser_mediator.ProduceExtractionWarning('unable to decode property: {0:s}'.format(property_name))
    return property_value

Retrieves a property value.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
properties (dict[str, object]): properties.
property_name (str): name of the property.

Returns:
str: property value.


def run_inference(examples, serving_bundle):

  batch_size = 64
  if serving_bundle.estimator and serving_bundle.feature_spec:

    preds = serving_bundle.estimator.predict(
      lambda: tf.data.Dataset.from_tensor_slices(
        tf.parse_example([ex.SerializeToString() for ex in examples],
        serving_bundle.feature_spec)).batch(batch_size))

    if serving_bundle.use_predict:
      preds_key = serving_bundle.predict_output_tensor
    elif serving_bundle.model_type == 'regression':
      preds_key = 'predictions'
    else:
      preds_key = 'probabilities'

    values = []
    for pred in preds:
      values.append(pred[preds_key])
    return common_utils.convert_prediction_values(values, serving_bundle)
  elif serving_bundle.custom_predict_fn:

    values = serving_bundle.custom_predict_fn(examples)
    return common_utils.convert_prediction_values(values, serving_bundle)
  else:
    return platform_utils.call_servo(examples, serving_bundle)

Run inference on examples given model information

Args:
examples: A list of examples that matches the model spec.
serving_bundle: A `ServingBundle` object that contains the information to
make the inference request.

Returns:
A ClassificationResponse or RegressionResponse proto.


def reciprocal_no_nan(x, name=None):
    with ops.name_scope(name, 'reciprocal_no_nan', [x]) as scope:
        x = ops.convert_to_tensor(x, name='x')
        one = constant_op.constant(1, dtype=x.dtype.base_dtype, name='one')
        return gen_math_ops.div_no_nan(one, x, name=scope)

Performs a safe reciprocal operation, element wise.

If a particular element is zero, the reciprocal for that element is
also set to zero.

For example:
```python
x = tf.constant([2.0, 0.5, 0, 1], dtype=tf.float32)
tf.math.reciprocal_no_nan(x)  # [ 0.5, 2, 0.0, 1.0 ]
```

Args:
x: A `Tensor` of type `float16`, `float32`, `float64` `complex64` or
`complex128`.
name: A name for the operation (optional).

Returns:
A `Tensor` of same shape and type as `x`.

Raises:
TypeError: x must be of a valid dtype.


def _minimize_peak_memory_list(graph):
    schedule = []
    bytes_freed = {}
    users_of = collections.defaultdict(set)
    in_degree = collections.defaultdict(int)
    operation_id = {}
    priority_queue = []
    for (i, operation_name) in enumerate(graph.get_all_operation_names()):
        operation_id[operation_name] = i
        for input_name in graph.get_operation_input_names(operation_name):
            if (operation_name in users_of[input_name]):
                continue
            users_of[input_name].add(operation_name)
            in_degree[operation_name] += 1
    for operation_name in graph.get_all_operation_names():
        bytes_freed[operation_name] = 0
        for input_name in graph.get_operation_input_names(operation_name):
            if ((len(users_of[input_name]) == 1) and (not graph.is_tensor_final(input_name))):
                bytes_freed[operation_name] += graph.get_tensor_size(input_name)
        for output_name in graph.get_operation_output_names(operation_name):
            if (users_of[output_name] or graph.is_tensor_final(output_name)):
                bytes_freed[operation_name] -= graph.get_tensor_size(output_name)
    for operation_name in graph.get_all_operation_names():
        if (in_degree[operation_name] == 0):
            heapq.heappush(priority_queue, ((- bytes_freed[operation_name]), operation_name))
    while priority_queue:
        (neg_bytes_freed, operation_name) = heapq.heappop(priority_queue)
        if (bytes_freed[operation_name] != (- neg_bytes_freed)):
            continue
        schedule.append(operation_id[operation_name])
        bytes_freed[operation_name] = None
        for output_name in graph.get_operation_output_names(operation_name):
            for other_operation_name in users_of[output_name]:
                in_degree[other_operation_name] -= 1
                if (in_degree[other_operation_name] == 0):
                    heapq.heappush(priority_queue, ((- bytes_freed[other_operation_name]), other_operation_name))
        for input_name in graph.get_operation_input_names(operation_name):
            if (operation_name not in users_of[input_name]):
                continue
            users_of[input_name].remove(operation_name)
            if ((len(users_of[input_name]) != 1) or graph.is_tensor_final(output_name)):
                continue
            (other_operation_name,) = users_of[input_name]
            bytes_freed[other_operation_name] += graph.get_tensor_size(input_name)
            if (in_degree[other_operation_name] > 0):
                continue
            heapq.heappush(priority_queue, ((- bytes_freed[other_operation_name]), other_operation_name))
    return schedule

Computes schedule according to the greedy list heuristic.

Greedy list heuristic: schedule the operation which results in the most bytes
of memory being (immediately) freed.
TODO(joshuawang): Experiment with tiebreaking by preferring more successors.

Args:
graph: an mtf.auto_mtf.graph_interface.GraphInterface.

Returns:
an iterable of integers representing the schedule.


def GetPluginObjects(cls, plugin_names):

    plugin_objects = {}
    for plugin_name, plugin_class in iter(cls._plugin_classes.items()):
      if plugin_name not in plugin_names:
        continue

      plugin_objects[plugin_name] = plugin_class()

    return plugin_objects

Retrieves the plugin objects.

Args:
plugin_names (list[str]): names of plugins that should be retrieved.

Returns:
dict[str, AnalysisPlugin]: analysis plugins per name.


def _create_key_value_cache_tensors(self, shape: Tuple[int, ...], device: torch.device) -> Tuple[torch.Tensor, torch.Tensor]:
    is_cpu_device = device == torch.device('cpu')
    key_cache = torch.zeros(shape, dtype=self._dtype, device=device, pin_memory=is_cpu_device)
    value_cache = torch.zeros(shape, dtype=self._dtype, device=device, pin_memory=is_cpu_device)
    torch._dynamo.mark_static_address(key_cache)
    torch._dynamo.mark_static_address(value_cache)
    return (key_cache, value_cache)

Creates K/V cache tensors on a device. Pins memory for CPU tensors. Marks them as static
addresses for non-CPU tensors.

Args:
shape (`Tuple[int, ...]`): Shape.
device (`torch.device`): Device.

Returns:
Key and value cache tensors as a tuple.


def _get_num_multimodal_tokens(self, image_sizes=None, **kwargs):
    vision_data = {}
    if image_sizes is not None:
        images_kwargs = Idefics3ProcessorKwargs._defaults.get('images_kwargs', {})
        images_kwargs.update(kwargs)
        num_image_patches = [self.image_processor.get_number_of_image_patches(*image_size, images_kwargs) for image_size in image_sizes]
        base_image_length = self.image_seq_len + 3
        col_length = self.image_seq_len + 2
        num_image_tokens = []
        for num_patches in num_image_patches:
            num_cols = num_rows = int(math.sqrt(num_patches - 1))
            row_length = col_length * num_cols + 1
            num_image_tokens.append(base_image_length + row_length * num_rows)
        vision_data.update({'num_image_tokens': num_image_tokens, 'num_image_patches': num_image_patches})
    return MultiModalData(**vision_data)

Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.

Args:
image_sizes (`List[List[int]]`, *optional*):
The input sizes formatted as (height, width) per each image.

Returns:
`MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
input modalities, along with other useful data.


def wbmax(self, value=None):

        if value is not None:
            try:
                value = float(value)
            except ValueError:
                raise ValueError('value {} need to be of type float '
                                 'for field `wbmax`'.format(value))

        self._wbmax = value

Corresponds to IDD Field `wbmax`
Extreme maximum wet-bulb temperature

Args:
value (float): value for IDD Field `wbmax`
Unit: C
if `value` is None it will not be checked against the
specification and is assumed to be a missing value

Raises:
ValueError: if `value` is not a valid value


def Print(self, output_writer):

    if self._names:
      output_writer.Write('\tnames: {0:s}\n'.format(
          ', '.join(self._names)))

Prints a human readable version of the filter.

Args:
output_writer (CLIOutputWriter): output writer.


def _set_class_parser(self, init_parser, methods_to_parse, cls):

        top_level_parents = [init_parser] if init_parser else []
        description = self._description or cls.__doc__
        top_level_parser = argparse.ArgumentParser(description=description,
                                                   parents=top_level_parents,
                                                   add_help=False,
                                                   conflict_handler="resolve")
        top_level_parser.add_argument("-h", "--help", action=FullHelpAction,
                                      help="Display this help message")
        parser_to_method = self._add_sub_parsers(top_level_parser,
                                                 methods_to_parse,
                                                 cls.__name__)

        if init_parser:
            parser_to_method["__init__"] = "__init__"
        top_level_parser.call = self._get_parser_call_method(parser_to_method)
        cls.parser = top_level_parser

Creates the complete argument parser for the decorated class.

Args:
init_parser: argument parser for the __init__ method or None
methods_to_parse: dict of method name pointing to their associated
argument parser
cls: the class we are decorating

Returns:
The decorated class with an added attribute 'parser'


def parse_branch_ref(filename):
    data = open(filename).read().strip()
    items = data.split(' ')
    if len(items) == 1:
        return None
    elif len(items) == 2 and items[0] == 'ref:':
        return items[1].strip()
    else:
        raise RuntimeError('Git directory has unparseable HEAD')

Given a filename of a .git/HEAD file return ref path.

In particular, if git is in detached head state, this will
return None. If git is in attached head, it will return
the branch reference. E.g. if on 'master', the HEAD will
contain 'ref: refs/heads/master' so 'refs/heads/master'
will be returned.

Example: parse_branch_ref(".git/HEAD")
Args:
filename: file to treat as a git HEAD file
Returns:
None if detached head, otherwise ref subpath
Raises:
RuntimeError: if the HEAD file is unparseable.


def get_categorical_feature_names(example):

  features = get_example_features(example)
  return sorted([
      feature_name for feature_name in features
      if features[feature_name].WhichOneof('kind') == 'bytes_list'
  ])

Returns a list of feature names for byte type features.

Args:
example: An example.

Returns:
A list of categorical feature names (e.g. ['education', 'marital_status'] )


def process_action(resource, action, action_issuer='unknown'):

    from cinq_collector_aws import AWSRegionCollector

    func_action = action_mapper[resource.resource_type][action]
    extra_info = {}
    action_status = ActionStatus.UNKNOWN

    if func_action:
        if action_mapper[resource.resource_type]['service_name'] == 'lambda':
            client = get_aws_session(
                AWSAccount.get(dbconfig.get('rds_collector_account', AWSRegionCollector.ns, ''))
            ).client(
                'lambda',
                dbconfig.get('rds_collector_region', AWSRegionCollector.ns, '')
            )
        else:
            client = get_aws_session(AWSAccount(resource.account)).client(
                action_mapper[resource.resource_type]['service_name'],
                region_name=resource.location
            )
        try:
            logger.info(f'Trying to {action} resource {resource.id} for account {resource.account.account_name} / region {resource.location}')
            action_status, extra_info = func_action(client, resource)
            Enforcement.create(resource.account.account_id, resource.id, action, datetime.now(), extra_info)
        except Exception as ex:
            action_status = ActionStatus.FAILED
            logger.exception('Failed to apply action {} to {}: {}'.format(action, resource.id, ex))
        finally:
            auditlog(
                event='{}.{}.{}.{}'.format(action_issuer, resource.resource_type, action, action_status),
                actor=action_issuer,
                data={
                    'resource_id': resource.id,
                    'account_name': resource.account.account_name,
                    'location': resource.location,
                    'info': extra_info
                }
            )
            return action_status
    else:
        logger.error('Failed to apply action {} to {}: Not supported'.format(action, resource.id))
        return ActionStatus.FAILED

Process an audit action for a resource, if possible

Args:
resource (:obj:`Resource`): A resource object to perform the action on
action (`str`): Type of action to perform (`kill` or `stop`)
action_issuer (`str`): The issuer of the action
Returns:
`ActionStatus`


def as_dict(self, verbosity: int=0) -> Dict:
    d = {'@module': self.__class__.__module__, '@class': self.__class__.__name__, 'matrix': self._matrix.tolist()}
    ((a, b, c), (alpha, beta, gamma)) = self.lengths_and_angles
    if (verbosity > 0):
        d.update({'a': a, 'b': b, 'c': c, 'alpha': alpha, 'beta': beta, 'gamma': gamma, 'volume': self.volume})
    return d

Json-serialization dict representation of the Lattice.

Args:
verbosity (int): Verbosity level. Default of 0 only includes the
matrix representation. Set to 1 for more details.


def from_millis(cls, timeout_ms):
    if hasattr(timeout_ms, 'has_expired'):
        return timeout_ms
    if (timeout_ms is None):
        return cls(None)
    return cls((timeout_ms / 1000.0))

Create a new PolledTimeout if needed.

If timeout_ms is already a PolledTimeout, just return it, otherwise create a
new PolledTimeout with the given timeout in milliseconds.

Args:
timeout_ms: PolledTimeout object, or number of milliseconds to use for
creating a new one.

Returns:
A PolledTimeout object that will expire in timeout_ms milliseconds, which
may be timeout_ms itself, or a newly allocated PolledTimeout.


def _request(self, domain, type_name, search_command, db_method, body=None):
    headers = {'Content-Type': 'application/json', 'DB-Method': db_method}
    search_command = self._clean_datastore_path(search_command)
    url = '/v2/exchange/db/{}/{}/{}'.format(domain, type_name, search_command)
    r = self.tcex.session.post(url, data=body, headers=headers, params=self._params)
    data = []
    status = 'Failed'
    if ((not r.ok) or ('application/json' not in r.headers.get('content-type', ''))):
        self.tcex.handle_error(350, [r.status_code, r.text])
    data = r.json()
    status = 'Success'
    return {'data': data, 'response': r, 'status': status}

Make the API request for a Data Store CRUD operation

Args:
domain (string): One of 'local', 'organization', or 'system'.
type_name (string): This is a free form index type name. The ThreatConnect API will use
this resource verbatim.
search_command (string): Search command to pass to ES.
db_method (string): The DB method 'DELETE', 'GET', 'POST', or 'PUT'
body (dict): JSON body


def update_power_state(self, id_or_uri, power_state):
    uri = (self._client.build_uri(id_or_uri) + '/powerState')
    return self._client.update(power_state, uri)

Sets the power state of the specified power delivery device. The device must be an HP Intelligent Outlet.

Args:
id_or_uri:
Can be either the power device id or the uri
power_state:
{"powerState":"On|Off"}

Returns:
str: The power state


def has_unchecked_field(self, locator, **kwargs):

        kwargs["checked"] = False
        return self.has_selector("field", locator, **kwargs)

Checks if the page or current node has a radio button or checkbox with the given label,
value, or id, that is currently unchecked.

Args:
locator (str): The label, name, or id of an unchecked field.
**kwargs: Arbitrary keyword arguments for :class:`SelectorQuery`.

Returns:
bool: Whether it exists.


def get_student_by_email(self, email, students=None):
    if (students is None):
        students = self.get_students()
    email = email.lower()
    for student in students:
        if (student['accountEmail'].lower() == email):
            return (student['studentId'], student)
    return (None, None)

Get a student based on an email address.

Calls ``self.get_students()`` to get list of all students,
if not passed as the ``students`` parameter.

Args:
email (str): student email
students (list): dictionary of students to search, default: None
When ``students`` is unspecified, all students in gradebook
are retrieved.

Raises:
requests.RequestException: Exception connection error
ValueError: Unable to decode response content

Returns:
tuple: tuple of student id and student dictionary.


def _expand_terms(self, terms):

        ret = {
            'keywords': list(),
            'doc': list(),
            'from': None,
            'to': None}

        if not isinstance(terms, dict):
            stp = SearchTermParser()
            terms = stp.parse(terms, term_join=self.backend._and_join)

        if 'about' in terms:
            ret['doc'].append(terms['about'])

        if 'with' in terms:
            ret['doc'].append(terms['with'])

        if 'in' in terms:
            place_vids = self._expand_place_ids(terms['in'])
            ret['keywords'].append(place_vids)

        if 'by' in terms:
            ret['keywords'].append(terms['by'])
        ret['from'] = terms.get('from', None)
        ret['to'] = terms.get('to', None)
        return ret

Expands partition terms to the appropriate fields.

Args:
terms (dict or str):

Returns:
dict: keys are field names, values are query strings


def _get_args_to_parse(args, sys_argv):

    arguments = args if args is not None else sys_argv[1:]
    _LOG.debug("Parsing arguments: %s", arguments)
    return arguments

Return the given arguments if it is not None else sys.argv if it contains
something, an empty list otherwise.

Args:
args: argument to be parsed
sys_argv: arguments of the command line i.e. sys.argv


def get_args(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('--input', required=True, help='Input file to process.')
    parser.add_argument('--output', required=True, help='Output file to write results to.')
    return parser.parse_known_args(argv)

Determines user specified arguments from the given list of arguments.

Args:
argv: all arguments.

Returns:
A pair of argument lists containing known and remaining arguments.


def WriteSessionStart(self):
    self._RaiseIfNotWritable()
    if (self._storage_type != definitions.STORAGE_TYPE_SESSION):
        raise IOError('Unsupported storage type.')
    session_start = self._session.CreateSessionStart()
    self._storage_file.WriteSessionStart(session_start)

Writes session start information.

Raises:
IOError: if the storage type is not supported or
when the storage writer is closed.
OSError: if the storage type is not supported or
when the storage writer is closed.


def convert_tiktoken_to_fast(encoding: Any, output_dir: str):
    output_dir = Path(output_dir)
    output_dir.mkdir(exist_ok=True)
    save_file = output_dir / 'tiktoken' / TIKTOKEN_VOCAB_FILE
    tokenizer_file = output_dir / TOKENIZER_FILE
    save_file_absolute = str(save_file.absolute())
    output_file_absolute = str(tokenizer_file.absolute())
    try:
        from tiktoken import get_encoding
        from tiktoken.load import dump_tiktoken_bpe
        if isinstance(encoding, str):
            encoding = get_encoding(encoding)
        dump_tiktoken_bpe(encoding._mergeable_ranks, save_file_absolute)
    except ImportError:
        raise ValueError('`tiktoken` is required to save a `tiktoken` file. Install it with `pip install tiktoken`.')
    tokenizer = TikTokenConverter(vocab_file=save_file_absolute, pattern=encoding._pat_str, additional_special_tokens=encoding._special_tokens).converted()
    tokenizer.save(output_file_absolute)

Converts given `tiktoken` encoding to `PretrainedTokenizerFast` and saves the configuration of converted tokenizer
on disk.

Args:
encoding (`str` or `tiktoken.Encoding`):
Tokenizer from `tiktoken` library. If `encoding` is `str`, the tokenizer will be loaded with
`tiktoken.get_encoding(encoding)`.
output_dir (`str`):
Save path for converted tokenizer configuration file.


def _contains_tensor(sample: repr_dataset.RepresentativeSample) -> bool:
    return any(map(lambda value: isinstance(value, core.Tensor), sample.values()))

Determines whether `sample` contains any tf.Tensors.

Args:
sample: A `RepresentativeSample`.

Returns:
True iff `sample` contains at least tf.Tensors.


def get_room(self, id):
    if (id not in self._rooms):
        self._rooms[id] = Room(self, id)
    return self._rooms[id]


class WarmUp(schedules.LearningRateSchedule):

    def __init__(self, initial_learning_rate: float, decay_schedule_fn: Callable, warmup_steps: int, power: float=1.0, name: Optional[str]=None):
        super().__init__()
        self.initial_learning_rate = initial_learning_rate
        self.warmup_steps = warmup_steps
        self.power = power
        self.decay_schedule_fn = decay_schedule_fn
        self.name = name

    def __call__(self, step):
        with tf.name_scope(self.name or 'WarmUp') as name:
            global_step_float = tf.cast(step, tf.float32)
            warmup_steps_float = tf.cast(self.warmup_steps, tf.float32)
            warmup_percent_done = global_step_float / warmup_steps_float
            warmup_learning_rate = self.initial_learning_rate * tf.math.pow(warmup_percent_done, self.power)
            return tf.cond(global_step_float < warmup_steps_float, lambda: warmup_learning_rate, lambda: self.decay_schedule_fn(step - self.warmup_steps), name=name)

    def get_config(self):
        return {'initial_learning_rate': self.initial_learning_rate, 'decay_schedule_fn': self.decay_schedule_fn, 'warmup_steps': self.warmup_steps, 'power': self.power, 'name': self.name}

Applies a warmup schedule on a given learning rate decay schedule.

Args:
initial_learning_rate (`float`):
The initial learning rate for the schedule after the warmup (so this will be the learning rate at the end
of the warmup).
decay_schedule_fn (`Callable`):
The schedule function to apply after the warmup for the rest of training.
warmup_steps (`int`):
The number of steps for the warmup part of training.
power (`float`, *optional*, defaults to 1.0):
The power to use for the polynomial warmup (defaults is a linear warmup).
name (`str`, *optional*):
Optional name prefix for the returned tensors during the schedule.


def set_status_code(self, status_code_line):
    self._empty = False
    self.status_code = self._remove_structure_prefix(_InstrumentationStructurePrefixes.STATUS_CODE, status_code_line)
    if self.status_code == _InstrumentationStatusCodes.START:
        self.begin_time = utils.get_current_epoch_time()

Sets the status code for the instrumentation test method, used in
determining the test result.

Args:
status_code_line: string, the raw instrumentation output line that
contains the status code of the instrumentation block.


def get_pair(self, term1, term2):
    key = self.key(term1, term2)
    return self.pairs.get(key, None)

Get the value for a pair of terms.

Args:
term1 (str)
term2 (str)

Returns:
The stored value.


def __init__(self, olecf_item):

    super(OLECFPropertySetStream, self).__init__()
    self._properties = {}
    self.date_time_properties = {}

    self._ReadPropertySet(olecf_item.set)

Initialize an OLECF property set stream.

Args:
olecf_item (pyolecf.property_set_stream): OLECF item.


def true_num_genes(model, custom_spont_id=None):
    true_num = 0
    for gene in model.genes:
        if (not is_spontaneous(gene, custom_id=custom_spont_id)):
            true_num += 1
    return true_num

Return the number of genes in a model ignoring spontaneously labeled genes.

Args:
model (Model):
custom_spont_id (str): Optional custom spontaneous ID if it does not match the regular expression ``[Ss](_|)0001``

Returns:
int: Number of genes excluding spontaneous genes


def _prepare_4d_causal_attention_mask_with_cache_position(attention_mask: torch.Tensor, sequence_length: int, target_length: int, dtype: torch.dtype, cache_position: torch.Tensor, batch_size: int, **kwargs):
    if attention_mask is not None and attention_mask.dim() == 4:
        causal_mask = attention_mask
    else:
        min_dtype = torch.finfo(dtype).min
        causal_mask = torch.full((sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=cache_position.device)
        if sequence_length != 1:
            causal_mask = torch.triu(causal_mask, diagonal=1)
        causal_mask *= torch.arange(target_length, device=cache_position.device) > cache_position.reshape(-1, 1)
        causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1)
        if attention_mask is not None:
            causal_mask = causal_mask.clone()
            mask_length = attention_mask.shape[-1]
            padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :].to(causal_mask.device)
            padding_mask = padding_mask == 0
            causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill(padding_mask, min_dtype)
    return causal_mask

Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape
`(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.

Args:
attention_mask (`torch.Tensor`):
A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape
`(batch_size, 1, query_length, key_value_length)`.
sequence_length (`int`):
The sequence length being processed.
target_length (`int`):
The target length: when generating with static cache, the mask should be as long as the static cache,
to account for the 0 padding, the part of the cache that is not filled yet.
dtype (`torch.dtype`):
The dtype to use for the 4D attention mask.
cache_position (`torch.Tensor`):
Indices depicting the position of the input sequence tokens in the sequence.
batch_size (`torch.Tensor`):
Batch size.


def forward(self, outputs, targets):
    outputs_without_aux = {k: v for k, v in outputs.items() if k != 'auxiliary_outputs'}
    indices = self.matcher(outputs_without_aux, targets)
    num_boxes = sum((len(t['class_labels']) for t in targets))
    num_boxes = torch.as_tensor([num_boxes], dtype=torch.float, device=next(iter(outputs.values())).device)
    world_size = 1
    if is_accelerate_available():
        if PartialState._shared_state != {}:
            num_boxes = reduce(num_boxes)
            world_size = PartialState().num_processes
    num_boxes = torch.clamp(num_boxes / world_size, min=1).item()
    losses = {}
    for loss in self.losses:
        losses.update(self.get_loss(loss, outputs, targets, indices, num_boxes))
    if 'auxiliary_outputs' in outputs:
        for i, auxiliary_outputs in enumerate(outputs['auxiliary_outputs']):
            indices = self.matcher(auxiliary_outputs, targets)
            for loss in self.losses:
                if loss == 'masks':
                    continue
                l_dict = self.get_loss(loss, auxiliary_outputs, targets, indices, num_boxes)
                l_dict = {k + f'_{i}': v for k, v in l_dict.items()}
                losses.update(l_dict)
    return losses

This performs the loss computation.

Args:
outputs (`dict`, *optional*):
Dictionary of tensors, see the output specification of the model for the format.
targets (`List[dict]`, *optional*):
List of dicts, such that `len(targets) == batch_size`. The expected keys in each dict depends on the
losses applied, see each loss' doc.


def _ReadEncryptedData(self, read_size):
    encrypted_data = self._file_object.read(read_size)
    read_count = len(encrypted_data)
    self._encrypted_data = b''.join([self._encrypted_data, encrypted_data])
    (self._decrypted_data, self._encrypted_data) = self._decrypter.Decrypt(self._encrypted_data)
    self._decrypted_data_size = len(self._decrypted_data)
    return read_count

Reads encrypted data from the file-like object.

Args:
read_size (int): number of bytes of encrypted data to read.

Returns:
int: number of bytes of encrypted data read.


def egg_info_writer(cmd, basename, filename):
    setupcfg = next((f for f in setuptools.findall() if (os.path.basename(f) == 'setup.cfg')), None)
    if (not setupcfg):
        return
    parser = six.moves.configparser.ConfigParser()
    parser.read(setupcfg)
    if ((not parser.has_section('rcli')) or (not parser.items('rcli'))):
        return
    config = dict(parser.items('rcli'))
    for (k, v) in six.iteritems(config):
        if (v.lower() in ('y', 'yes', 'true')):
            config[k] = True
        elif (v.lower() in ('n', 'no', 'false')):
            config[k] = False
        else:
            try:
                config[k] = json.loads(v)
            except ValueError:
                pass
    cmd.write_file(basename, filename, json.dumps(config))

Read rcli configuration and write it out to the egg info.

Args:
cmd: An egg info command instance to use for writing.
basename: The basename of the file to write.
filename: The full path of the file to write into the egg info.


def ExamineEvent(self, mediator, event):

    if self._tagging_rules is None:
      if self._autodetect_tag_file_attempt:

        return

      if not self._AttemptAutoDetectTagFile(mediator):
        logger.info(
            'No tag definition file specified, and plaso was not able to '
            'autoselect a tagging file. As no definitions were specified, '
            'no events will be tagged.')
        return

    matched_label_names = []
    for label_name, filter_objects in iter(self._tagging_rules.items()):
      for filter_object in filter_objects:
        if filter_object.Match(event):
          matched_label_names.append(label_name)
          break

    if matched_label_names:
      event_tag = self._CreateEventTag(
          event, self._EVENT_TAG_COMMENT, matched_label_names)

      mediator.ProduceEventTag(event_tag)
      self._number_of_event_tags += 1

Analyzes an EventObject and tags it according to rules in the tag file.

Args:
mediator (AnalysisMediator): mediates interactions between analysis
plugins and other components, such as storage and dfvfs.
event (EventObject): event to examine.


def eval_adiabatic_limit(YABFGN, Ytilde, P0):

    Y, A, B, F, G, N = YABFGN

    Klim = (P0 * (B - A * Ytilde * A) * P0).expand().simplify_scalar()
    Hlim = ((Klim - Klim.dag())/2/I).expand().simplify_scalar()

    Ldlim = (P0 * (G - A * Ytilde * F) * P0).expand().simplify_scalar()

    dN = identity_matrix(N.shape[0]) + F.H * Ytilde * F
    Nlim = (P0 * N * dN * P0).expand().simplify_scalar()

    return SLH(Nlim.dag(), Ldlim.dag(), Hlim.dag())

Compute the limiting SLH model for the adiabatic approximation

Args:
YABFGN: The tuple (Y, A, B, F, G, N)
as returned by prepare_adiabatic_limit.
Ytilde: The pseudo-inverse of Y, satisfying Y * Ytilde = P0.
P0: The projector onto the null-space of Y.

Returns:
SLH: Limiting SLH model


def cond(self, name='cond'):
    with self._name_scope(name):
        return self._cond()

Returns the condition number of this linear operator.

Args:
name:  A name for this `Op`.

Returns:
Shape `[B1,...,Bb]` `Tensor` of same `dtype` as `self`.


def end_run_group(group, session):
    from datetime import datetime
    group.end = datetime.now()
    group.status = 'completed'
    session.commit()

End the run_group successfully.

Args:
group: The run_group we want to complete.
session: The database transaction we will finish.


def delete(self, key):
    dct = self
    keys = key.split('.')
    last_key = keys[(- 1)]
    for k in keys:
        if (k == last_key):
            del dct[k]
            break
        if isinstance(dct, DotDict):
            dct = super(DotDict, dct).__getitem__(k)
        else:
            dct = dct.__getitem__(k)
            if (not isinstance(dct, (DotDict, dict))):
                raise KeyError('Subkey "{}" in "{}" invalid for deletion'.format(k, key))

Remove a value from the `DotDict`.

The `key` parameter can either be a regular string key,
e.g. "foo", or it can be a string key with dot notation,
e.g. "foo.bar.baz", to signify a nested element.

If the key does not exist in the `DotDict`, it will continue
silently.

Args:
key (str): The key to remove.


def _mini_batch_training_op(self, inputs, cluster_idx_list, cluster_centers, total_counts):
    update_ops = []
    for inp, cluster_idx in zip(inputs, cluster_idx_list):
        with ops.colocate_with(inp, ignore_existing=True):
            assert total_counts is not None
            cluster_idx = array_ops.reshape(cluster_idx, [-1])
            unique_ids, unique_idx = array_ops.unique(cluster_idx)
            num_unique_cluster_idx = array_ops.size(unique_ids)
            with ops.colocate_with(total_counts, ignore_existing=True):
                old_counts = array_ops.gather(total_counts, unique_ids)
            with ops.colocate_with(cluster_centers, ignore_existing=True):
                old_cluster_centers = array_ops.gather(cluster_centers, unique_ids)
            count_updates = math_ops.unsorted_segment_sum(array_ops.ones_like(unique_idx, dtype=total_counts.dtype), unique_idx, num_unique_cluster_idx)
            cluster_center_updates = math_ops.unsorted_segment_sum(inp, unique_idx, num_unique_cluster_idx)
            broadcast_shape = array_ops.concat([array_ops.reshape(num_unique_cluster_idx, [1]), array_ops.ones(array_ops.reshape(array_ops.rank(inp) - 1, [1]), dtype=dtypes.int32)], 0)
            cluster_center_updates -= math_ops.cast(array_ops.reshape(count_updates, broadcast_shape), inp.dtype) * old_cluster_centers
            learning_rate = math_ops.reciprocal(math_ops.cast(old_counts + count_updates, inp.dtype))
            learning_rate = array_ops.reshape(learning_rate, broadcast_shape)
            cluster_center_updates *= learning_rate
        update_counts = state_ops.scatter_add(total_counts, unique_ids, count_updates)
        update_cluster_centers = state_ops.scatter_add(cluster_centers, unique_ids, cluster_center_updates)
        update_ops.extend([update_counts, update_cluster_centers])
    return control_flow_ops.group(*update_ops)

Creates an op for training for mini batch case.

Args:
inputs: list of input Tensors.
cluster_idx_list: A vector (or list of vectors). Each element in the
vector corresponds to an input row in 'inp' and specifies the cluster id
corresponding to the input.
cluster_centers: Tensor Ref of cluster centers.
total_counts: Tensor Ref of cluster counts.

Returns:
An op for doing an update of mini-batch k-means.


def execute_command(self, args, parent_environ=None, **subprocess_kwargs):
    if (parent_environ in (None, os.environ)):
        target_environ = {}
    else:
        target_environ = parent_environ.copy()
    interpreter = Python(target_environ=target_environ)
    executor = self._create_executor(interpreter, parent_environ)
    self._execute(executor)
    return interpreter.subprocess(args, **subprocess_kwargs)

Run a command within a resolved context.

This applies the context to a python environ dict, then runs a
subprocess in that namespace. This is not a fully configured subshell -
shell-specific commands such as aliases will not be applied. To execute
a command within a subshell instead, use execute_shell().

Warning:
This runs a command in a configured environ dict only, not in a true
shell. To do that, call `execute_shell` using the `command` keyword
argument.

Args:
args: Command arguments, can be a string.
parent_environ: Environment to interpret the context within,
defaults to os.environ if None.
subprocess_kwargs: Args to pass to subprocess.Popen.

Returns:
A subprocess.Popen object.

Note:
This does not alter the current python session.


def _AddHeader(self, fp):

    text = textwrap.wrap(
        textwrap.dedent(self.config_header), break_on_hyphens=False)
    fp.write('\n'.join(['
    fp.write('\n\n')

Create a file header in the config.

Args:
fp: int, a file pointer for writing the header.


def extract_numerics_alert(event):
    value = event.summary.value[0]
    debugger_plugin_metadata_content = None
    if value.HasField('metadata'):
        plugin_data = value.metadata.plugin_data
        if (plugin_data.plugin_name == constants.DEBUGGER_PLUGIN_NAME):
            debugger_plugin_metadata_content = plugin_data.content
    if (not debugger_plugin_metadata_content):
        raise ValueError('Event proto input lacks debugger plugin SummaryMetadata.')
    debugger_plugin_metadata_content = tf.compat.as_text(debugger_plugin_metadata_content)
    try:
        content_object = json.loads(debugger_plugin_metadata_content)
        device_name = content_object['device']
    except (KeyError, ValueError) as e:
        raise ValueError(('Could not determine device from JSON string %r, %r' % (debugger_plugin_metadata_content, e)))
    debug_op_suffix = ':DebugNumericSummary'
    if (not value.node_name.endswith(debug_op_suffix)):
        raise ValueError(('Event proto input does not have the expected debug op suffix %s' % debug_op_suffix))
    tensor_name = value.node_name[:(- len(debug_op_suffix))]
    elements = tf_debug.load_tensor_from_event(event)
    nan_count = elements[constants.NAN_NUMERIC_SUMMARY_OP_INDEX]
    neg_inf_count = elements[constants.NEG_INF_NUMERIC_SUMMARY_OP_INDEX]
    pos_inf_count = elements[constants.POS_INF_NUMERIC_SUMMARY_OP_INDEX]
    if ((nan_count > 0) or (neg_inf_count > 0) or (pos_inf_count > 0)):
        return NumericsAlert(device_name, tensor_name, event.wall_time, nan_count, neg_inf_count, pos_inf_count)
    return None

Determines whether a health pill event contains bad values.

A bad value is one of NaN, -Inf, or +Inf.

Args:
event: (`Event`) A `tensorflow.Event` proto from `DebugNumericSummary`
ops.

Returns:
An instance of `NumericsAlert`, if bad values are found.
`None`, if no bad values are found.

Raises:
ValueError: if the event does not have the expected tag prefix or the
debug op name is not the expected debug op name suffix.


def limitReal(x, max_denominator=1000000):
    f = Fraction(x).limit_denominator(max_denominator)
    return Real((f.numerator, f.denominator))

Creates an pysmt Real constant from x.

Args:
x (number): A number to be cast to a pysmt constant.
max_denominator (int, optional): The maximum size of the denominator.
Default 1000000.

Returns:
A Real constant with the given value and the denominator limited.


def __call__(self, text, toLang, fromLang=None):

        return self.skype.conn("GET", "{0}/skype/translate".format(SkypeConnection.API_TRANSLATE),
                               params={"from": fromLang or "", "to": toLang, "text": text},
                               auth=SkypeConnection.Auth.SkypeToken).json()

Attempt translation of a string.  Supports automatic language detection if ``fromLang`` is not specified.

Args:
text (str): input text to be translated
toLang (str): country code of output language
fromLang (str): country code of input language


def forward(self, hidden_states: torch.Tensor, level_index: Optional[int]=None, attention_mask: Optional[torch.Tensor]=None, position_embeddings: Optional[torch.Tensor]=None, query_position_embeddings: Optional[torch.Tensor]=None, encoder_hidden_states: Optional[torch.Tensor]=None, encoder_attention_mask: Optional[torch.Tensor]=None, output_attentions: Optional[bool]=False):
    if self.pre_norm:
        outputs = self.forward_pre(hidden_states=hidden_states, level_index=level_index, position_embeddings=position_embeddings, query_position_embeddings=query_position_embeddings, encoder_hidden_states=encoder_hidden_states, encoder_attention_mask=encoder_attention_mask, output_attentions=output_attentions)
    else:
        outputs = self.forward_post(hidden_states=hidden_states, level_index=level_index, position_embeddings=position_embeddings, query_position_embeddings=query_position_embeddings, encoder_hidden_states=encoder_hidden_states, encoder_attention_mask=encoder_attention_mask, output_attentions=output_attentions)
    return outputs

Args:
hidden_states (`torch.FloatTensor`):
Input to the layer of shape `(seq_len, batch, embed_dim)`.
attention_mask (`torch.FloatTensor`):
Attention mask of shape `(1, seq_len, tgt_len, src_len)`.
position_embeddings (`torch.FloatTensor`, *optional*):
Position embeddings that are added to the keys in the masked-attention layer.
query_position_embeddings (`torch.FloatTensor`, *optional*):
Position embeddings that are added to the queries and keys in the self-attention layer.
encoder_hidden_states (`torch.FloatTensor`):
Cross attention input to the layer of shape `(seq_len, batch, embed_dim)`.
encoder_attention_mask (`torch.FloatTensor`):
Encoder attention mask of size`(1, seq_len, tgt_len, src_len)`.
output_attentions (`bool`, *optional*):
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
returned tensors for more detail.


def enable_encryption(self):
    try:
        self.send_state_event('m.room.encryption', {'algorithm': 'm.megolm.v1.aes-sha2'})
        self.encrypted = True
        return True
    except MatrixRequestError:
        return False

Enables encryption in the room.

NOTE: Once enabled, encryption cannot be disabled.

Returns:
True if successful, False if not


def _download_items(db, last_id):

    MAX_RETRY = 20
    MAX_DOC_ID = 10000000

    not_found_cnt = 0
    for doc_id in xrange(last_id, MAX_DOC_ID):
        doc_id += 1
        print "Downloading %d.." % (doc_id)

        if not_found_cnt >= MAX_RETRY:
            print "It looks like this is an end:", doc_id - MAX_RETRY
            break

        try:
            record = _download(doc_id)
        except (DocumentNotFoundException, InvalidAlephBaseException):
            print "\tnot found, skipping"
            not_found_cnt += 1
            continue

        not_found_cnt = 0
        db["item_%d" % doc_id] = record
        db["last_id"] = doc_id - MAX_RETRY if doc_id > MAX_RETRY else 1

        if doc_id % 100 == 0:
            db.commit()

Download items from the aleph and store them in `db`. Start from `last_id`
if specified.

Args:
db (obj): Dictionary-like object used as DB.
last_id (int): Start from this id.


def create_token_type_ids_from_sequences(self, token_ids_0: List[int], token_ids_1: Optional[List[int]]=None) -> List[int]:
    sep = [self.sep_token_id]
    cls = [self.cls_token_id]
    if token_ids_1 is None:
        return len(cls) * [self.cls_token_type_id] + len(token_ids_0 + sep) * [0]
    return len(cls) * [self.cls_token_type_id] + len(token_ids_0 + sep) * [0] + len(token_ids_1 + sep) * [1]

Create a mask from the two sequences passed to be used in a sequence-pair classification task. A Funnel
Transformer sequence pair mask has the following format:

```
2 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
| first sequence    | second sequence |
```

If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s).

Args:
token_ids_0 (`List[int]`):
List of IDs.
token_ids_1 (`List[int]`, *optional*):
Optional second list of IDs for sequence pairs.

Returns:
`List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s).


def occurrence(self, file_name=None, path=None, date=None):
    if (self._indicator_data.get('type') != 'File'):
        return None
    occurrence_obj = FileOccurrence(file_name, path, date)
    self._occurrences.append(occurrence_obj)
    return occurrence_obj

Add a file Occurrence.

Args:
file_name (str, optional): The file name for this occurrence.
path (str, optional): The file path for this occurrence.
date (str, optional): The datetime expression for this occurrence.

Returns:
obj: An instance of Occurrence.


def add_tasks_r(addon_module, package_module, package_name):
    module_dict = package_module.__dict__
    for (attr_name, attr_val) in module_dict.items():
        if isinstance(attr_val, fabric.tasks.WrappedCallableTask):
            addon_module.__dict__[attr_name] = attr_val
        elif ((attr_name != package_name) and isinstance(attr_val, types.ModuleType) and attr_val.__name__.startswith('fabsetup_') and (attr_name.split('.')[(- 1)] != package_name)):
            submodule_name = flo('{addon_module.__name__}.{attr_name}')
            submodule = get_or_create_module_r(submodule_name)
            package_module = attr_val
            add_tasks_r(submodule, package_module, package_name)
            addon_module.__dict__[attr_name] = submodule

Recursively iterate through 'package_module' and add every fabric task
to the 'addon_module' keeping the task hierarchy.

Args:
addon_module(types.ModuleType)
package_module(types.ModuleType)
package_name(str): Required, to avoid redundant addition of tasks

Return: None


def copy(self, source_file_names, destination_file_names):
    if not len(source_file_names) == len(destination_file_names):
        message = 'Unable to copy unequal number of sources and destinations.'
        raise BeamIOError(message)
    src_dest_pairs = list(zip(source_file_names, destination_file_names))
    return self._blobstorageIO().copy_paths(src_dest_pairs)

Recursively copy the file tree from the source to the destination

Args:
source_file_names: list of source file objects that needs to be copied
destination_file_names: list of destination of the new object

Raises:
``BeamIOError``: if any of the copy operations fail


def undetoured_new(cls, *args, **kwargs) -> Any:
    new_method = _global_detour_context.get_original_new(cls)
    if new_method is object.__new__:
        instance = new_method(cls)
    else:
        instance = new_method(cls, *args, **kwargs)
    instance.__init__(*args, **kwargs)
    return instance

Create a new instance of cls without detouring.

If cls.__init__ creates sub-objects, creation of sub-objects
maybe detoured based on current context. For example::

class A:

def __init__(self, x):
if x < 0:
self.child = A(x)
else:
self.x = x

with pg.detour([A, B]):
a = A(-1)
assert isinstance(a, A)
assert isinstance(a.child, B)

Args:
cls: The class whose instance will be created.
*args: Positional arguments to be passed to class __init__ method.
**kwargs: Keyword arguments to be passed to class __init__ method.

Returns:
A instance of `cls`.


def GetAPFSFileEntryByPathSpec(self, path_spec):
    location = getattr(path_spec, 'location', None)
    identifier = getattr(path_spec, 'identifier', None)
    if (identifier is not None):
        fsapfs_file_entry = self._fsapfs_volume.get_file_entry_by_identifier(identifier)
    elif (location is not None):
        fsapfs_file_entry = self._fsapfs_volume.get_file_entry_by_path(location)
    else:
        raise errors.PathSpecError('Path specification missing location and identifier.')
    return fsapfs_file_entry

Retrieves the APFS file entry for a path specification.

Args:
path_spec (PathSpec): a path specification.

Returns:
pyfsapfs.file_entry: file entry.

Raises:
PathSpecError: if the path specification is missing location and
identifier.


def create_team(self, name):

        request = self._get_request()
        return request.post(self.TEAM_CREATE_URL, {"name": name})

Creates a new Team

Creates a new Team and makes you a member. You must not currently belong to a team to invoke.

Args:

name (str): The name of your team

Returns:
A Team object


def _createBitpattern(functioncode, value):

    _checkFunctioncode(functioncode, [5, 15])
    _checkInt(value, minvalue=0, maxvalue=1, description='inputvalue')

    if functioncode == 5:
        if value == 0:
            return '\x00\x00'
        else:
            return '\xff\x00'

    elif functioncode == 15:
        if value == 0:
            return '\x00'
        else:
            return '\x01'

Create the bit pattern that is used for writing single bits.

This is basically a storage of numerical constants.

Args:
* functioncode (int): can be 5 or 15
* value (int): can be 0 or 1

Returns:
The bit pattern (string).

Raises:
TypeError, ValueError


def _ProcessPathSpec(self, extraction_worker, parser_mediator, path_spec):

    self._current_display_name = parser_mediator.GetDisplayNameForPathSpec(
        path_spec)

    try:
      extraction_worker.ProcessPathSpec(parser_mediator, path_spec)

    except KeyboardInterrupt:
      self._abort = True

      self._processing_status.aborted = True
      if self._status_update_callback:
        self._status_update_callback(self._processing_status)

    except dfvfs_errors.CacheFullError:

      self._abort = True
      logger.error((
          'ABORT: detected cache full error while processing '
          'path spec: {0:s}').format(self._current_display_name))

    except Exception as exception:
      parser_mediator.ProduceExtractionWarning((
          'unable to process path specification with error: '
          '{0!s}').format(exception), path_spec=path_spec)

      if getattr(self._processing_configuration, 'debug_output', False):
        logger.warning(
            'Unhandled exception while processing path spec: {0:s}.'.format(
                self._current_display_name))
        logger.exception(exception)

        pdb.post_mortem()

Processes a path specification.

Args:
extraction_worker (worker.ExtractionWorker): extraction worker.
parser_mediator (ParserMediator): parser mediator.
path_spec (dfvfs.PathSpec): path specification.


def _call_method(self, method, req, resp_class):
    payload = req.SerializeToString()
    headers = {'Content-Type': 'application/x-protobuf', 'Content-Length': str(len(payload)), 'X-Goog-Api-Format-Version': '2'}
    (response, content) = self._http.request(('%s:%s' % (self._url, method)), method='POST', body=payload, headers=headers)
    if (response.status != 200):
        raise _make_rpc_error(method, response, content)
    resp = resp_class()
    resp.ParseFromString(content)
    return resp

_call_method call the given RPC method over HTTP.

It uses the given protobuf message request as the payload and
returns the deserialized protobuf message response.

Args:
method: RPC method name to be called.
req: protobuf message for the RPC request.
resp_class: protobuf message class for the RPC response.

Returns:
Deserialized resp_class protobuf message instance.

Raises:
RPCError: The rpc method call failed.


def __init__(self, tlv_type=127, value=None):

        super().__init__()
        self.tlv_type = tlv_type
        self._value = BinaryData() if value is None else value

Create an instance and set its attributes.

Args:
tlv_type (int): Type used by this class. Defaults to 127.
value (:class:`~pyof.foundation.basic_types.BinaryData`):
Value stored by GenericTLV.


def search(self, term):
    return self._result(self._get(self._url('/images/search'), params={'term': term}), True)

Search for images on Docker Hub. Similar to the ``docker search``
command.

Args:
term (str): A term to search for.

Returns:
(list of dicts): The response of the search.

Raises:
:py:class:`docker.errors.APIError`
If the server returns an error.


def _fill_from_default(self, default_job_config):

        if self._job_type != default_job_config._job_type:
            raise TypeError(
                "attempted to merge two incompatible job types: "
                + repr(self._job_type)
                + ", "
                + repr(default_job_config._job_type)
            )

        new_job_config = self.__class__()

        default_job_properties = copy.deepcopy(default_job_config._properties)
        for key in self._properties:
            if key != self._job_type:
                default_job_properties[key] = self._properties[key]

        default_job_properties[self._job_type].update(self._properties[self._job_type])
        new_job_config._properties = default_job_properties

        return new_job_config

Merge this job config with a default job config.

The keys in this object take precedence over the keys in the default
config. The merge is done at the top-level as well as for keys one
level below the job type.

Arguments:
default_job_config (google.cloud.bigquery.job._JobConfig):
The default job config that will be used to fill in self.

Returns:
google.cloud.bigquery.job._JobConfig A new (merged) job config.


def method(self, method):

        self._request.method = method
        self.add_matcher(matcher('MethodMatcher', method))

Defines the HTTP method to match.
Use ``*`` to match any method.

Arguments:
method (str): method value to match. E.g: ``GET``.

Returns:
self: current Mock instance.


def sigmoid_cross_entropy_one_hot(logits, labels, weights_fn=None):
    with tf.variable_scope('sigmoid_cross_entropy_one_hot', values=[logits, labels]):
        del weights_fn
        cross_entropy = tf.losses.sigmoid_cross_entropy(multi_class_labels=labels, logits=logits)
        return (cross_entropy, tf.constant(1.0))

Calculate sigmoid cross entropy for one-hot lanels and logits.

Args:
logits: Tensor of size [batch-size, o=1, p=1, num-classes]
labels: Tensor of size [batch-size, o=1, p=1, num-classes]
weights_fn: Function that takes in labels and weighs examples (unused)
Returns:
cross_entropy (scalar), weights


def exit(self, code=None, msg=None):

        if msg is not None:
            if code in [0, 3] or (code is None and self.exit_code in [0, 3]):
                self.log.info(msg)
            else:
                self.log.error(msg)
            self.message_tc(msg)

        if code is None:
            code = self.exit_code
        elif code in [0, 1, 3]:
            pass
        else:
            self.log.error(u'Invalid exit code')
            code = 1

        if self.default_args.tc_aot_enabled:

            self.playbook.aot_rpush(code)

        self.log.info(u'Exit Code: {}'.format(code))
        sys.exit(code)

Application exit method with proper exit code

The method will run the Python standard sys.exit() with the exit code
previously defined via :py:meth:`~tcex.tcex.TcEx.exit_code` or provided
during the call of this method.

Args:
code (Optional [integer]): The exit code value for the app.
msg (Optional [string]): A message to log and add to message tc output.


def num_employers(self, num_employers):

        if num_employers < 2:
            self._logger.log(
                'warn',
                'Two employers are needed: setting to two'
            )
            num_employers = 2
        self._num_employers = num_employers
        self._logger.log('debug', 'Number of employers set to {}'.format(
            num_employers
        ))
        self._limit = num_employers * len(self._value_ranges)
        self._logger.log('debug', 'Limit set to {}'.format(self._limit))

Sets the number of employer bees; at least two are required

Args:
num_employers (int): number of employer bees


def get_apod(cls, date=None, hd=False):
    instance = cls('planetary/apod')
    filters = {'date': date, 'hd': hd}
    return instance.get_resource(**filters)

Returns Astronomy Picture of the Day

Args:
date: date instance (default = today)

hd: bool if high resolution should be included

Returns:
json


def _set_route(self, ip_dest, next_hop, **kwargs):
    commands = self._build_commands(ip_dest, next_hop, **kwargs)
    delete = kwargs.get('delete', False)
    default = kwargs.get('default', False)
    if delete:
        commands = ('no ' + commands)
    elif default:
        commands = ('default ' + commands)
    return self.configure(commands)

Configure a static route

Args:
ip_dest (string): The ip address of the destination in the
form of A.B.C.D/E
next_hop (string): The next hop interface or ip address
**kwargs['next_hop_ip'] (string): The next hop address on
destination interface
**kwargs['distance'] (string): Administrative distance for this
route
**kwargs['tag'] (string): Route tag
**kwargs['route_name'] (string): Route name
**kwargs['delete'] (boolean): If true, deletes the specified route
instead of creating or setting values for the route
**kwargs['default'] (boolean): If true, defaults the specified
route instead of creating or setting values for the route

Returns:
True if the operation succeeds, otherwise False.


def advth(step):

    rbot, rtop = misc.get_rbounds(step)
    rmean = 0.5 * (rbot + rtop)
    rad = step.rprof['r'].values + rbot
    radio = step.timeinfo['H_int']
    if rbot != 0:
        th_adv = -(rtop**3 - rad**3) / rmean**2 / 3
    else:
        th_adv = rad - rtop
    th_adv *= radio
    th_adv += step.timeinfo['Nutop']
    return th_adv, None

Theoretical advection.

This compute the theoretical profile of total advection as function of
radius.

Args:
step (:class:`~stagpy.stagyydata._Step`): a step of a StagyyData
instance.
Returns:
tuple of :class:`numpy.array` and None: the theoretical advection.
The second element of the tuple is None.


def _ProcessZipFileWithPlugins(self, parser_mediator, zip_file):
    archive_members = zip_file.namelist()
    for plugin in self._plugins:
        try:
            plugin.UpdateChainAndProcess(parser_mediator, zip_file=zip_file, archive_members=archive_members)
        except errors.WrongCompoundZIPPlugin as exception:
            logger.debug('[{0:s}] wrong plugin: {1!s}'.format(self.NAME, exception))

Processes a zip file using all compound zip files.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
zip_file (zipfile.ZipFile): the zip file. It should not be closed in
this method, but will be closed in ParseFileObject().


def _popitem(self, indices=None, name=None):
    if name is None:
        name = '%s_get_nokey' % self._name
    indices, dtypes = self._get_indices_and_dtypes(indices)
    with ops.colocate_with(self._coloc_op):
        key, result = self._popitem_fn(shared_name=self._name, indices=indices, dtypes=dtypes, name=name, capacity=self._capacity, memory_limit=self._memory_limit)
    key = self._create_device_transfers(key)[0]
    result = self._get_return_value(result, indices)
    return (key, result)

If the staging area is ordered, the (key, value) with the smallest key will be returned.

Otherwise, a random (key, value) will be returned.
If the staging area is empty when this operation executes,
it will block until there is an element to dequeue.

Args:
key: Key associated with the required data
indices: Partial list of tensors to retrieve (optional).
A list of integer or string indices.
String indices are only valid if the Staging Area
has names associated with it.
name: A name for the operation (optional)

Returns:
The created op


def match_any(patterns, name):

    if not patterns:
        return True
    return any(match(pattern, name) for pattern in patterns)

Test if a name matches any of a list of patterns.

Will return `True` if ``patterns`` is an empty list.

Arguments:
patterns (list): A list of wildcard pattern, e.g ``["*.py",
"*.pyc"]``
name (str): A filename.

Returns:
bool: `True` if the name matches at least one of the patterns.


def apply_range_set(self, hist: Hist) -> None:

        axis = self.axis(hist)

        assert not isinstance(self.min_val, float)
        assert not isinstance(self.max_val, float)

        min_val = self.min_val(axis)
        max_val = self.max_val(axis)

        self.axis(hist).SetRange(min_val, max_val)

Apply the associated range set to the axis of a given hist.

Note:
The min and max values should be bins, not user ranges! For more, see the binning
explanation in ``apply_func_to_find_bin(...)``.

Args:
hist: Histogram to which the axis range restriction should be applied.
Returns:
None. The range is set on the axis.