def decode_json_body():
    raw_data = request.body.read()
    try:
        return json.loads(raw_data)
    except ValueError as e:
        raise HTTPError(400, e.__str__())

Decode ``bottle.request.body`` to JSON.

Returns:
obj: Structure decoded by ``json.loads()``.

Raises:
HTTPError: 400 in case the data was malformed.


def call(self, input_ids: tf.Tensor | None=None, bbox: tf.Tensor | None=None, attention_mask: tf.Tensor | None=None, token_type_ids: tf.Tensor | None=None, position_ids: tf.Tensor | None=None, head_mask: tf.Tensor | None=None, inputs_embeds: tf.Tensor | None=None, pixel_values: tf.Tensor | None=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, training: bool=False) -> Union[TFBaseModelOutput, Tuple[tf.Tensor], Tuple[tf.Tensor, tf.Tensor], Tuple[tf.Tensor, tf.Tensor, tf.Tensor]]:
    outputs = self.layoutlmv3(input_ids=input_ids, bbox=bbox, attention_mask=attention_mask, token_type_ids=token_type_ids, position_ids=position_ids, head_mask=head_mask, inputs_embeds=inputs_embeds, pixel_values=pixel_values, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, training=training)
    return outputs

Returns:

Examples:

```python
>>> from transformers import AutoProcessor, TFAutoModel
>>> from datasets import load_dataset

>>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv3-base", apply_ocr=False)
>>> model = TFAutoModel.from_pretrained("microsoft/layoutlmv3-base")

>>> dataset = load_dataset("nielsr/funsd-layoutlmv3", split="train", trust_remote_code=True)
>>> example = dataset[0]
>>> image = example["image"]
>>> words = example["tokens"]
>>> boxes = example["bboxes"]

>>> encoding = processor(image, words, boxes=boxes, return_tensors="tf")

>>> outputs = model(**encoding)
>>> last_hidden_states = outputs.last_hidden_state
```


def get_stable_entries(self, charge_to_discharge=True):
    list_copy = list(self._stable_entries)
    return (list_copy if charge_to_discharge else list_copy.reverse())

Get the stable entries.

Args:
charge_to_discharge: order from most charge to most discharged
state? Default to True.

Returns:
A list of stable entries in the electrode, ordered by amount of the
working ion.


def get_audience(self, audience_id):
    audience = self.audience_id_map.get(audience_id)
    if audience:
        return audience
    self.logger.error(('Audience ID "%s" is not in datafile.' % audience_id))
    self.error_handler.handle_error(exceptions.InvalidAudienceException(enums.Errors.INVALID_AUDIENCE_ERROR))

Get audience object for the provided audience ID.

Args:
audience_id: ID of the audience.

Returns:
Dict representing the audience.


def run_step(self, rewriter):
    assert rewriter, 'FileRewriter instance required to run FileInRewriterStep.'
    rewriter.files_in_to_out(in_path=self.path_in, out_path=self.path_out)

Do the file in to out rewrite.

Doesn't do anything more crazy than call files_in_to_out on the
rewriter.

Args:
rewriter: pypyr.filesystem.FileRewriter instance.


def plot(self, width=8, height=None, plt=None, dpi=None, **kwargs):
    plt = pretty_plot(width=width, height=height, plt=plt, dpi=dpi)
    color = kwargs.get('color', 'r')
    label = kwargs.get('label', '{} fit'.format(self.__class__.__name__))
    lines = [('Equation of State: %s' % self.__class__.__name__), ('Minimum energy = %1.2f eV' % self.e0), ('Minimum or reference volume = %1.2f Ang^3' % self.v0), ('Bulk modulus = %1.2f eV/Ang^3 = %1.2f GPa' % (self.b0, self.b0_GPa)), ('Derivative of bulk modulus wrt pressure = %1.2f' % self.b1)]
    text = '\n'.join(lines)
    text = kwargs.get('text', text)
    plt.plot(self.volumes, self.energies, linestyle='None', marker='o', color=color)
    (vmin, vmax) = (min(self.volumes), max(self.volumes))
    (vmin, vmax) = ((vmin - (0.01 * abs(vmin))), (vmax + (0.01 * abs(vmax))))
    vfit = np.linspace(vmin, vmax, 100)
    plt.plot(vfit, self.func(vfit), linestyle='dashed', color=color, label=label)
    plt.grid(True)
    plt.xlabel('Volume $\\AA^3$')
    plt.ylabel('Energy (eV)')
    plt.legend(loc='best', shadow=True)
    plt.text(0.4, 0.5, text, transform=plt.gca().transAxes)
    return plt

Plot the equation of state.

Args:
width (float): Width of plot in inches. Defaults to 8in.
height (float): Height of plot in inches. Defaults to width *
golden ratio.
plt (matplotlib.pyplot): If plt is supplied, changes will be made
to an existing plot. Otherwise, a new plot will be created.
dpi:
kwargs (dict): additional args fed to pyplot.plot.
supported keys: style, color, text, label

Returns:
Matplotlib plot object.


def load_hat(self, path):
    hat = cv2.imread(path, cv2.IMREAD_UNCHANGED)
    if (hat is None):
        raise ValueError('No hat image found at `{}`'.format(path))
    (b, g, r, a) = cv2.split(hat)
    return cv2.merge((r, g, b, a))

Loads the hat from a picture at path.

Args:
path: The path to load from

Returns:
The hat data.


def try_generalizing_function_type(self, target: function_type.FunctionType) -> function_type.FunctionType:
    relaxed = target
    for other in self._dispatch_table:
        subtype = relaxed.most_specific_common_subtype([other])
        if subtype is not None:
            relaxed = subtype
    return relaxed

Returns a generalized subtype of the one given.

This heuristic aims to reduce the number of future traces by computing a
type that represents more general function inputs.

The original "experimental_relax_shapes" heuristic identified a known type
which shared a common subtype with the current unknown type and then
traced with that common subtype. However, the notion of "common subtype"
was only limited to shapes. This heuristic extends that to FunctionType.

Returns `target` if a generalized subtype can not be found.

Args:
target: The FunctionType to generalize


def write_markdown_to_file(self, f):
    print('---', file=f)
    print('---', file=f)
    print('<!-- This file is machine generated: DO NOT EDIT! -->', file=f)
    print('', file=f)
    print('
    print('', file=f)
    fullname_f = (lambda name: self._members[name][0])
    anchor_f = (lambda name: _get_anchor(self._module_to_name, fullname_f(name)))
    for (filename, library) in self._filename_to_library_map:
        sorted_names = sorted(library.mentioned, key=(lambda x: (str.lower(x), x)))
        member_names = [n for n in sorted_names if (n in self._members)]
        full_filename = (self._path_prefix + filename)
        links = [('[`%s`](%s
        if links:
            print(('* **[%s](%s)**:' % (library.title, full_filename[:(- 3)])), file=f)
            for link in links:
                print(('  * %s' % link), file=f)
            print('', file=f)

Writes this index to file `f`.

The output is formatted as an unordered list. Each list element
contains the title of the library, followed by a list of symbols
in that library hyperlinked to the corresponding anchor in that
library.

Args:
f: The output file.


def get(self, buffer_type, offset):
    if (buffer_type == u'streaming'):
        chosen_buffer = self.streaming_data
    else:
        chosen_buffer = self.storage_data
    if (offset >= len(chosen_buffer)):
        raise StreamEmptyError('Invalid index given in get command', requested=offset, stored=len(chosen_buffer), buffer=buffer_type)
    return chosen_buffer[offset]

Get a reading from the buffer at offset.

Offset is specified relative to the start of the data buffer.
This means that if the buffer rolls over, the offset for a given
item will appear to change.  Anyone holding an offset outside of this
engine object will need to be notified when rollovers happen (i.e.
popn is called so that they can update their offset indices)

Args:
buffer_type (str): The buffer to pop from (either u"storage" or u"streaming")
offset (int): The offset of the reading to get


def _get_access_from_refresh(self) -> Tuple[(str, float)]:
    headers = self._get_authorization_headers()
    data = {'grant_type': 'refresh_token', 'refresh_token': self.refresh_token}
    r = self.session.post(self.TOKEN_URL, headers=headers, data=data)
    response_data = r.json()
    return (response_data['access_token'], response_data['expires_in'])

Uses the stored refresh token to get a new access token.

This method assumes that the refresh token exists.

Args:
None

Returns:
new access token and expiration time (from now)


def compress_dinf(angle, nodata):

        if MathClass.floatequal(angle, nodata):
            return DEFAULT_NODATA, DEFAULT_NODATA, DEFAULT_NODATA
        taud, d = DinfUtil.check_orthogonal(angle)
        if d != -1:
            return taud, d, 1
        if angle < FlowModelConst.ne:
            a1 = angle
            d = 129
        elif angle < FlowModelConst.n:
            a1 = angle - FlowModelConst.ne
            d = 192
        elif angle < FlowModelConst.nw:
            a1 = angle - FlowModelConst.n
            d = 96
        elif angle < FlowModelConst.w:
            a1 = angle - FlowModelConst.nw
            d = 48
        elif angle < FlowModelConst.sw:
            a1 = angle - FlowModelConst.w
            d = 24
        elif angle < FlowModelConst.s:
            a1 = angle - FlowModelConst.sw
            d = 12
        elif angle < FlowModelConst.se:
            a1 = angle - FlowModelConst.s
            d = 6
        else:
            a1 = angle - FlowModelConst.se
            d = 3
        return angle, d, a1 / PI * 4.0

Compress dinf flow direction to D8 direction with weight follows ArcGIS D8 codes.
Args:
angle: D-inf flow direction angle
nodata: NoData value

Returns:
1. Updated Dinf values
2. Compressed flow direction follows ArcGIS D8 codes rule
3. Weight of the first direction


def _token_endpoint_request(request, token_uri, body):
    body = urllib.parse.urlencode(body)
    headers = {'content-type': _URLENCODED_CONTENT_TYPE}
    response = request(method='POST', url=token_uri, headers=headers, body=body)
    response_body = response.data.decode('utf-8')
    if (response.status != http_client.OK):
        _handle_error_response(response_body)
    response_data = json.loads(response_body)
    return response_data

Makes a request to the OAuth 2.0 authorization server's token endpoint.

Args:
request (google.auth.transport.Request): A callable used to make
HTTP requests.
token_uri (str): The OAuth 2.0 authorizations server's token endpoint
URI.
body (Mapping[str, str]): The parameters to send in the request body.

Returns:
Mapping[str, str]: The JSON-decoded response data.

Raises:
google.auth.exceptions.RefreshError: If the token endpoint returned
an error.


def adafactor_optimizer_from_hparams(hparams, lr):

  if hparams.optimizer_adafactor_decay_type == "Adam":
    decay_rate = adafactor_decay_rate_adam(
        hparams.optimizer_adafactor_beta2)
  elif hparams.optimizer_adafactor_decay_type == "pow":
    decay_rate = adafactor_decay_rate_pow(
        hparams.optimizer_adafactor_memory_exponent)
  else:
    raise ValueError("unknown optimizer_adafactor_decay_type")
  return AdafactorOptimizer(
      multiply_by_parameter_scale=(
          hparams.optimizer_adafactor_multiply_by_parameter_scale),
      learning_rate=lr,
      decay_rate=decay_rate,
      beta1=hparams.optimizer_adafactor_beta1,
      clipping_threshold=hparams.optimizer_adafactor_clipping_threshold,
      factored=hparams.optimizer_adafactor_factored)

Create an Adafactor optimizer based on model hparams.

Args:
hparams: model hyperparameters
lr: learning rate scalar.
Returns:
an AdafactorOptimizer
Raises:
ValueError: on illegal values


def prefer_static_shape(x):
    return prefer_static_value(array_ops.shape(x))

Return static shape of tensor `x` if available, else `tf.shape(x)`.

Args:
x: `Tensor` (already converted).

Returns:
Numpy array (if static shape is obtainable), else `Tensor`.


def __init__(self, begin: int, end: int):

        if begin < 0:
            raise PulseError("Cannot create Interval with negative begin time")
        if end < 0:
            raise PulseError("Cannot create Interval with negative end time")
        self._begin = begin
        self._end = end

Create an interval = (begin, end))

Args:
begin: begin time of this interval
end: end time of this interval

Raises:
PulseError: when invalid time or duration is specified


def RegisterOutputs(cls, output_classes, disabled=False):
    for output_class in output_classes:
        cls.RegisterOutput(output_class, disabled)

Registers output classes.

The output classes are identified based on their NAME attribute.

Args:
output_classes (list[type]): output module classes.
disabled (Optional[bool]): True if the output module is disabled due to
the module not loading correctly or not.

Raises:
KeyError: if output class is already set for the corresponding name.


def counter(self, counter_name, default=0):

    return self._state.counters_map.get(counter_name, default)

Get the current counter value.

Args:
counter_name: name of the counter in string.
default: default value in int if one doesn't exist.

Returns:
Current value of the counter.


def _init_groups(self, string):

        taf_group_pattern =

        group_list = []

        groups = re.findall(taf_group_pattern, string, re.VERBOSE)
        if not groups:
            raise MalformedTAF("No valid groups found")

        for group in groups:
            group_list.append(group)

        return(group_list)

Extracts weather groups (FM, PROB etc.) and populates group list

Args:
TAF report string

Raises:
MalformedTAF: Group decoding error


def get_victim_phone_asset(self, main_type, sub_type, unique_id, asset_id, params=None):

        params = params or {}

        return self.victim_phone_asset(main_type, sub_type, unique_id, asset_id, params=params)

Args:
main_type:
sub_type:
unique_id:
asset_id:
params:

Return:


def slice_shape(self, tensor_shape):

    tensor_layout = self.tensor_layout(tensor_shape)
    ret = []
    for tensor_dim, mesh_axis in zip(
        tensor_shape, tensor_layout.tensor_axis_to_mesh_axis):
      if mesh_axis is None:
        ret.append(tensor_dim.size)
      else:
        mesh_dim = self.shape[mesh_axis]
        if tensor_dim.size % mesh_dim.size != 0:
          raise ValueError(
              "Tensor dimension size not divisible by mesh dimension size:"
              " tensor_shape=%s tensor_layout=%s"
              % (tensor_shape, tensor_layout))
        ret.append(tensor_dim.size
    return ret

Shape of each slice of the Tensor.

Args:
tensor_shape: Shape.

Returns:
list of integers with length tensor_shape.ndims.

Raises:
ValueError: If a Tensor dimension is not divisible by the corresponding
Mesh dimension.


def _validate_snippet_app_on_device(self):
    out = self._adb.shell(f'pm list package --user {self.user_id}')
    if not utils.grep(f'^package:{self.package}$', out):
        raise errors.ServerStartPreCheckError(self._device, f'{self.package} is not installed for user {self.user_id}.')
    out = self._adb.shell('pm list instrumentation')
    matched_out = utils.grep(f'^instrumentation:{self.package}/{_INSTRUMENTATION_RUNNER_PACKAGE}', out)
    if not matched_out:
        raise errors.ServerStartPreCheckError(self._device, f'{self.package} is installed, but it is not instrumented.')
    match = re.search('^instrumentation:(.*)\\/(.*) \\(target=(.*)\\)$', matched_out[0])
    target_name = match.group(3)
    if target_name != self.package:
        out = self._adb.shell(f'pm list package --user {self.user_id}')
        if not utils.grep(f'^package:{target_name}$', out):
            raise errors.ServerStartPreCheckError(self._device, f'Instrumentation target {target_name} is not installed for user {self.user_id}.')

Validates the Mobly Snippet app is available on the device.

To run as an instrumentation test, the Mobly Snippet app must already be
installed and instrumented on the Android device.

Raises:
errors.ServerStartPreCheckError: if the server app is not installed
for the current user.


def update_clinvar_id(self, clinvar_id, submission_id):
    updated_submission = self.clinvar_submission_collection.find_one_and_update({'_id': ObjectId(submission_id)}, {'$set': {'clinvar_subm_id': clinvar_id, 'updated_at': datetime.now()}}, upsert=True, return_document=pymongo.ReturnDocument.AFTER)
    return updated_submission

saves an official clinvar submission ID in a clinvar submission object

Args:
clinvar_id(str): a string with a format: SUB[0-9]. It is obtained from clinvar portal when starting a new submission
submission_id(str): submission_id(str) : id of the submission to be updated

Returns:
updated_submission(obj): a clinvar submission object, updated


def _FormatTokenData(self, token_type, token_data):
    token_data_format_function = self._TOKEN_DATA_FORMAT_FUNCTIONS.get(token_type)
    if token_data_format_function:
        token_data_format_function = getattr(self, token_data_format_function, None)
    if (not token_data_format_function):
        return {}
    return token_data_format_function(token_data)

Formats the token data as a dictionary of values.

Args:
token_type (int): token type.
token_data (object): token data.

Returns:
dict[str, str]: formatted token values or an empty dictionary if no
formatted token values could be determined.


def run_command(self, command, arg=None, is_eval=False):

        mode = is_eval and 'eval' or 'command'

        if isinstance(arg, tuple):
            name, d = arg
        else:
            name, d = arg, {}

        result = getattr(self.connection.admin, mode)(command, name, **d)
        return result

run command on the server

Args:
command - command string
arg - command argument
is_eval - if True execute command as eval

return command's result


def cache():

    is_conditional = request.headers.get("If-Modified-Since") or request.headers.get(
        "If-None-Match"
    )

    if is_conditional is None:
        response = view_get()
        response.headers["Last-Modified"] = http_date()
        response.headers["ETag"] = uuid.uuid4().hex
        return response
    else:
        return status_code(304)

Returns a 304 if an If-Modified-Since header or If-None-Match is present. Returns the same as a GET otherwise.
---
tags:
- Response inspection
parameters:
- in: header
name: If-Modified-Since
- in: header
name: If-None-Match
produces:
- application/json
responses:
200:
description: Cached response
304:
description: Modified


def __init__(self, input_gen):
    self.input_gen = input_gen

Creates a representative dataset.

Args:
input_gen: A generator function that generates input samples for the model
and has the same order, type and shape as the inputs to the model.
Usually, this is a small subset of a few hundred samples randomly
chosen, in no particular order, from the training or evaluation dataset.


def tokenize(self, text: 'TextInput', **kwargs) -> List[str]:
    return PreTrainedTokenizer.tokenize(self, text, **kwargs)

Args:
text: TextInput
Simply calls PreTrainedTokenizer's method


def ParseFileEntry(self, parser_mediator, file_entry):
    stat_object = file_entry.GetStat()
    if (not stat_object):
        return
    file_system_type = self._GetFileSystemTypeFromFileEntry(file_entry)
    event_data = FileStatEventData()
    event_data.file_entry_type = stat_object.type
    event_data.file_size = getattr(stat_object, 'size', None)
    event_data.file_system_type = file_system_type
    event_data.is_allocated = file_entry.IsAllocated()
    if file_entry.access_time:
        event = time_events.DateTimeValuesEvent(file_entry.access_time, definitions.TIME_DESCRIPTION_LAST_ACCESS)
        parser_mediator.ProduceEventWithEventData(event, event_data)
    if file_entry.creation_time:
        event = time_events.DateTimeValuesEvent(file_entry.creation_time, definitions.TIME_DESCRIPTION_CREATION)
        parser_mediator.ProduceEventWithEventData(event, event_data)
    if file_entry.change_time:
        event = time_events.DateTimeValuesEvent(file_entry.change_time, definitions.TIME_DESCRIPTION_CHANGE)
        parser_mediator.ProduceEventWithEventData(event, event_data)
    if file_entry.modification_time:
        event = time_events.DateTimeValuesEvent(file_entry.modification_time, definitions.TIME_DESCRIPTION_MODIFICATION)
        parser_mediator.ProduceEventWithEventData(event, event_data)
    for (time_attribute, usage) in self._TIMESTAMP_DESCRIPTIONS.items():
        posix_time = getattr(stat_object, time_attribute, None)
        if (posix_time is None):
            continue
        nano_time_attribute = '{0:s}_nano'.format(time_attribute)
        nano_time_attribute = getattr(stat_object, nano_time_attribute, None)
        timestamp = (posix_time * 1000000)
        if (nano_time_attribute is not None):
            (micro_time_attribute, _) = divmod(nano_time_attribute, 10)
            timestamp += micro_time_attribute
        if ((file_entry.type_indicator == dfvfs_definitions.TYPE_INDICATOR_TSK) and (not timestamp)):
            continue
        date_time = dfdatetime_posix_time.PosixTimeInMicroseconds(timestamp=timestamp)
        event = time_events.DateTimeValuesEvent(date_time, usage)
        parser_mediator.ProduceEventWithEventData(event, event_data)

Parses a file entry.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
file_entry (dfvfs.FileEntry): a file entry.


def wait_for_vacancy(self, processor_type):

        with self._condition:
            self._condition.wait_for(lambda: (
                self._processor_available(processor_type)
                or self._cancelled_event.is_set()))
            if self._cancelled_event.is_set():
                raise WaitCancelledException()
            processor = self[processor_type].next_processor()
            return processor

Waits for a particular processor type to have the capacity to
handle additional transactions or until is_cancelled is True.

Args:
processor_type (ProcessorType): The family, and version of
the transaction processor.

Returns:
Processor


def registered_identifiers():
    return _REVIVED_TYPE_REGISTRY.keys()

Return all the current registered revived object identifiers.

Returns:
A set of strings.


def _get_element_attr_or_none(document, selector, attribute):

    element = document.cssselect(selector)
    if element:
        return element[0].get(attribute)
    return None

Using a CSS selector, get the element and return the given attribute value, or None if no element.

Args:
document (HTMLElement) - HTMLElement document
selector (str) - CSS selector
attribute (str) - The attribute to get from the element


def Run(self):
    if not self.func:
        raise RuntimeError('Func does not contain a runnable function.')
    return self.func(*self.args)

Execute the function with |args|.

Returns:
Whether the function succeeded to execute.
Raises:
RuntimeError: If self.func is None.


def get_audio_features(self, input_features: torch.FloatTensor, feature_attention_mask: Optional[torch.LongTensor]=None, audio_feature_lengths: Optional[torch.LongTensor]=None):
    if feature_attention_mask is not None:
        audio_feature_lengths = torch.sum(feature_attention_mask, dim=1)
        input_features = input_features.permute(0, 2, 1)[feature_attention_mask.bool()].permute(1, 0)
    else:
        audio_feature_lengths = None
    audio_feat_lengths, audio_output_lengths = self.audio_tower._get_feat_extract_output_lengths(audio_feature_lengths if audio_feature_lengths is not None else feature_attention_mask.sum(-1))
    feature_lens = audio_feature_lengths if audio_feature_lengths is not None else feature_attention_mask.sum(-1)
    audio_outputs = self.audio_tower(input_features, feature_lens=feature_lens, aftercnn_lens=audio_feat_lengths)
    audio_features = audio_outputs.last_hidden_state
    if audio_features.shape[0] != sum(audio_output_lengths.tolist()):
        raise ValueError('length of audio_features should match audio_output_lengths')
    return audio_features

Encodes audios into continuous embeddings that can be forwarded to the language model.

Args:
input_features (`torch.FloatTensor`):
The tensors corresponding to the input audios.
feature_attention_mask (`torch.LongTensor`, *optional*):
Mask to avoid performing attention on padding feature indices. Mask values selected in `[0, 1]`:
audio_feature_lengths (`torch.LongTensor` of shape `(num_audios)`, *optional*):
The length of feature shape of each audio in LLM.


def shard_dimensions(self):
    return [policy.shard_dimension for policy in self._sharding_policies]

Gets the shard dimension of each tuple element.

Returns:
A list of length number_of_tuple_elements, where each list entry
is the shard dimension of that tuple element or None if the
shard dimension has not been set.


def invert(self, copy=False):
    if copy:
        return Striplog([i.invert(copy=True) for i in self])
    else:
        for i in self:
            i.invert()
        self.__sort()
        o = self.order
        self.order = {'depth': 'elevation', 'elevation': 'depth'}[o]
        return

Inverts the striplog, changing its order and the order of its contents.

Operates in place by default.

Args:
copy (bool): Whether to operate in place or make a copy.

Returns:
None if operating in-place, or an inverted copy of the striplog
if not.


def lookupSpatialReferenceID(cls, directory, filename):

        path = os.path.join(directory, filename)

        with open(path, 'r') as f:
            srid = lookupSpatialReferenceID(f.read())

        return srid

Look up spatial reference system using the projection file.

Args:
directory (str):
filename (str):

Return:
int: Spatial Reference ID


def copy_script(self, filename, id_=(- 1)):
    self._copy(filename, id_=id_, file_type=SCRIPT_FILE_TYPE)

Copy a script to the distribution server.

Args:
filename: Full path to file to upload.
id_: ID of Script object to associate with, or -1 for new
Script (default).


def decompress_file(filepath):

    toks = filepath.split(".")
    file_ext = toks[-1].upper()
    from monty.io import zopen
    if file_ext in ["BZ2", "GZ", "Z"]:
        with open(".".join(toks[0:-1]), 'wb') as f_out, \
                zopen(filepath, 'rb') as f_in:
            f_out.writelines(f_in)
        os.remove(filepath)

Decompresses a file with the correct extension. Automatically detects
gz, bz2 or z extension.

Args:
filepath (str): Path to file.
compression (str): A compression mode. Valid options are "gz" or
"bz2". Defaults to "gz".


def _execute_and_process_stdout(self, args, shell, handler):
    proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell, bufsize=1)
    out = '[elided, processed via handler]'
    try:
        while True:
            line = proc.stdout.readline()
            if line:
                handler(line)
            else:
                break
    finally:
        (unexpected_out, err) = proc.communicate()
        if unexpected_out:
            out = ('[unexpected stdout] %s' % unexpected_out)
            for line in unexpected_out.splitlines():
                handler(line)
    ret = proc.returncode
    logging.debug('cmd: %s, stdout: %s, stderr: %s, ret: %s', utils.cli_cmd_to_string(args), out, err, ret)
    if (ret == 0):
        return err
    else:
        raise AdbError(cmd=args, stdout=out, stderr=err, ret_code=ret)

Executes adb commands and processes the stdout with a handler.

Args:
args: string or list of strings, program arguments.
See subprocess.Popen() documentation.
shell: bool, True to run this command through the system shell,
False to invoke it directly. See subprocess.Popen() docs.
handler: func, a function to handle adb stdout line by line.

Returns:
The stderr of the adb command run if exit code is 0.

Raises:
AdbError: The adb command exit code is not 0.


def _do_export(self, remote_function):

        if self._worker.load_code_from_local:
            return

        function = remote_function._function
        function_name_global_valid = function.__name__ in function.__globals__
        function_name_global_value = function.__globals__.get(
            function.__name__)

        if not is_cython(function):
            function.__globals__[function.__name__] = remote_function
        try:
            pickled_function = pickle.dumps(function)
        finally:

            if function_name_global_valid:
                function.__globals__[function.__name__] = (
                    function_name_global_value)
            else:
                del function.__globals__[function.__name__]

        check_oversized_pickle(pickled_function,
                               remote_function._function_name,
                               "remote function", self._worker)
        key = (b"RemoteFunction:" + self._worker.task_driver_id.binary() + b":"
               + remote_function._function_descriptor.function_id.binary())
        self._worker.redis_client.hmset(
            key, {
                "driver_id": self._worker.task_driver_id.binary(),
                "function_id": remote_function._function_descriptor.
                function_id.binary(),
                "name": remote_function._function_name,
                "module": function.__module__,
                "function": pickled_function,
                "max_calls": remote_function._max_calls
            })
        self._worker.redis_client.rpush("Exports", key)

Pickle a remote function and export it to redis.

Args:
remote_function: the RemoteFunction object.


def command(self, cmd_name, callback, *args):

        cmd = JLinkCommand(cmd_name, args, callback)
        self._commands.put(cmd)

Run an asynchronous command.

Args:
cmd_name (int): The unique code for the command to execute.
callback (callable): The optional callback to run when the command finishes.
The signature should be callback(cmd_name, result, exception)
*args: Any arguments that are passed to the underlying command handler


def cfg_to_dot(self, filename):

        with open(filename, 'w', encoding='utf8') as f:
            f.write('digraph{\n')
            for node in self.nodes:
                f.write('{}[label="{}"];\n'.format(node.node_id, str(node)))
                for son in node.sons:
                    f.write('{}->{};\n'.format(node.node_id, son.node_id))

            f.write("}\n")

Export the function to a dot file
Args:
filename (str)


def _get_attribute_for_protocol_matching(self, cls: abstract.BaseValue, name: str, instance: abstract.BaseValue | None, unbind: bool) -> tuple[cfg.Variable | None, bool]:
    if instance:
        valself_instance = instance
    else:
        valself_instance = abstract.Instance(cls, self.ctx)
        if isinstance(cls, abstract.ParameterizedClass):
            for param_name, param in cls.formal_type_parameters.items():
                valself_instance.merge_instance_type_parameter(self._node, param_name, param.instantiate(self._node))
    _, attribute = self.ctx.attribute_handler.get_attribute(self._node, cls, name, valself_instance.to_binding(self._node))
    if attribute:
        return self._resolve_function_attribute_var(attribute, unbind)
    else:
        return (attribute, False)

Gets the specified attribute from cls, for protocol matching.

Args:
cls: The class to get the attribute from.
name: The name of the attribute.
instance: Optionally, an instance of the class.
unbind: Whether to unbind the attribute if it is a bound function.

Returns:
A tuple of the attribute and whether any unbinding occurred.


def download_software_file(filename=None, synch=False):
    if (not filename):
        raise CommandExecutionError('Filename option must not be none.')
    if (not isinstance(synch, bool)):
        raise CommandExecutionError('Synch option must be boolean..')
    if (synch is True):
        query = {'type': 'op', 'cmd': '<request><system><software><download><file>{0}</file></download></software></system></request>'.format(filename)}
    else:
        query = {'type': 'op', 'cmd': '<request><system><software><download><sync-to-peer>yes</sync-to-peer><file>{0}</file></download></software></system></request>'.format(filename)}
    return _get_job_results(query)

Download software packages by filename.

Args:
filename(str): The filename of the PANOS file to download.

synch (bool): If true then the file will synch to the peer unit.

CLI Example:

.. code-block:: bash

salt '*' panos.download_software_file PanOS_5000-8.0.0
salt '*' panos.download_software_file PanOS_5000-8.0.0 True


def update_stack(self, fqn, template, old_parameters, parameters, tags, force_interactive=False, force_change_set=False, stack_policy=None, **kwargs):
    logger.debug('Attempting to update stack %s:', fqn)
    logger.debug('    parameters: %s', parameters)
    logger.debug('    tags: %s', tags)
    if template.url:
        logger.debug('    template_url: %s', template.url)
    else:
        logger.debug('    no template url, uploading template directly.')
    update_method = self.select_update_method(force_interactive, force_change_set)
    return update_method(fqn, template, old_parameters, parameters, stack_policy=stack_policy, tags=tags, **kwargs)

Update a Cloudformation stack.

Args:
fqn (str): The fully qualified name of the Cloudformation stack.
template (:class:`stacker.providers.base.Template`): A Template
object to use when updating the stack.
old_parameters (list): A list of dictionaries that defines the
parameter list on the existing Cloudformation stack.
parameters (list): A list of dictionaries that defines the
parameter list to be applied to the Cloudformation stack.
tags (list): A list of dictionaries that defines the tags
that should be applied to the Cloudformation stack.
force_interactive (bool): A flag that indicates whether the update
should be interactive. If set to True, interactive mode will
be used no matter if the provider is in interactive mode or
not. False will follow the behavior of the provider.
force_change_set (bool): A flag that indicates whether the update
must be executed with a change set.
stack_policy (:class:`stacker.providers.base.Template`): A template
object representing a stack policy.


def connect(self, *args, auto_reconnect=False, **kwargs):
    connection_info = {'auto_reconnect': auto_reconnect, 'args': args, 'kwargs': kwargs}
    self.connect_info['connection'] = connection_info
    if ('user' not in self.connect_info):
        raise Exception('`set_user_info` must be called before connecting to server.')
    connection = loop.create_connection((lambda : self), *args, **kwargs)
    asyncio.Task(connection)

Connects to the given server.

Args:
auto_reconnect (bool): Automatically reconnect on disconnection.

Other arguments to this function are as usually supplied to
:meth:`asyncio.BaseEventLoop.create_connection`.


def __init__(self, name, domain=None, display_name=None, description=None):

    self._name = name
    self._domain = domain
    self._display_name = display_name
    self._description = description
    if not isinstance(self._domain, (Domain, type(None))):
      raise ValueError("not a domain: %r" % (self._domain,))

Create a hyperparameter object.

Args:
name: A string ID for this hyperparameter, which should be unique
within an experiment.
domain: An optional `Domain` object describing the values that
this hyperparameter can take on.
display_name: An optional human-readable display name (`str`).
description: An optional Markdown string describing this
hyperparameter.

Raises:
ValueError: If `domain` is not a `Domain`.


def log_handler(self, handler):

        if not self.opened():
            handler = handler or util.noop
            self._log_handler = enums.JLinkFunctions.LOG_PROTOTYPE(handler)
            self._dll.JLINKARM_EnableLog(self._log_handler)

Setter for the log handler function.

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

Returns:
``None``


def run_pod(self, pod, startup_timeout=120, get_logs=True):

        resp = self.run_pod_async(pod)
        curr_time = dt.now()
        if resp.status.start_time is None:
            while self.pod_not_started(pod):
                delta = dt.now() - curr_time
                if delta.seconds >= startup_timeout:
                    raise AirflowException("Pod took too long to start")
                time.sleep(1)
            self.log.debug('Pod not yet started')

        return self._monitor_pod(pod, get_logs)

Launches the pod synchronously and waits for completion.
Args:
pod (Pod):
startup_timeout (int): Timeout for startup of the pod (if pod is pending for
too long, considers task a failure


def modify(name, beacon_data, **kwargs):

    ret = {'comment': '',
           'result': True}

    current_beacons = list_(return_yaml=False, **kwargs)
    if name not in current_beacons:
        ret['comment'] = 'Beacon {0} is not configured.'.format(name)
        return ret

    if 'test' in kwargs and kwargs['test']:
        ret['result'] = True
        ret['comment'] = 'Beacon: {0} would be modified.'.format(name)
    else:
        try:

            eventer = salt.utils.event.get_event('minion', opts=__opts__)
            res = __salt__['event.fire']({'name': name,
                                          'beacon_data': beacon_data,
                                          'func': 'validate_beacon'},
                                         'manage_beacons')
            if res:
                event_ret = eventer.get_event(
                    tag='/salt/minion/minion_beacon_validation_complete',
                    wait=kwargs.get('timeout', 30))
                valid = event_ret['valid']
                vcomment = event_ret['vcomment']

            if not valid:
                ret['result'] = False
                ret['comment'] = ('Beacon {0} configuration invalid, '
                                  'not modifying.\n{1}'.format(name, vcomment))
                return ret

        except KeyError:

            ret['result'] = False
            ret['comment'] = 'Event module not available. Beacon modify failed.'
            return ret

        if not valid:
            ret['result'] = False
            ret['comment'] = ('Beacon {0} configuration invalid, '
                              'not modifying.\n{1}'.format(name, vcomment))
            return ret

        _current = current_beacons[name]
        _new = beacon_data

        if _new == _current:
            ret['comment'] = 'Job {0} in correct state'.format(name)
            return ret

        _current_lines = []
        for _item in _current:
            _current_lines.extend(['{0}:{1}\n'.format(key, value)
                                  for (key, value) in six.iteritems(_item)])
        _new_lines = []
        for _item in _new:
            _new_lines.extend(['{0}:{1}\n'.format(key, value)
                              for (key, value) in six.iteritems(_item)])
        _diff = difflib.unified_diff(_current_lines, _new_lines)

        ret['changes'] = {}
        ret['changes']['diff'] = ''.join(_diff)

        try:
            eventer = salt.utils.event.get_event('minion', opts=__opts__)
            res = __salt__['event.fire']({'name': name,
                                          'beacon_data': beacon_data,
                                          'func': 'modify'},
                                         'manage_beacons')
            if res:
                event_ret = eventer.get_event(
                    tag='/salt/minion/minion_beacon_modify_complete',
                    wait=kwargs.get('timeout', 30))
                if event_ret and event_ret['complete']:
                    beacons = event_ret['beacons']
                    if name in beacons and beacons[name] == beacon_data:
                        ret['result'] = True
                        ret['comment'] = 'Modified beacon: {0}.'.format(name)
                elif event_ret:
                    ret['result'] = False
                    ret['comment'] = event_ret['comment']
                else:
                    ret['result'] = False
                    ret['comment'] = 'Did not receive the manage event ' \
                                     'before the timeout of {0}s' \
                                     ''.format(kwargs.get('timeout', 30))
                return ret
        except KeyError:

            ret['result'] = False
            ret['comment'] = 'Event module not available. Beacon modify failed.'
    return ret

Modify an existing beacon.

Args:

name (str):
Name of the beacon to configure.

beacon_data (dict):
Dictionary or list containing updated configuration for beacon.

Returns:
dict: Boolean and status message on success or failure of modify.

CLI Example:

.. code-block:: bash

salt '*' beacons.modify ps "[{'salt-master': 'stopped'}, {'apache2': 'stopped'}]"


def _ScanEncryptedVolumeNode(self, scan_context, scan_node):
    if (scan_node.type_indicator == definitions.TYPE_INDICATOR_APFS_CONTAINER):
        container_file_entry = resolver.Resolver.OpenFileEntry(scan_node.path_spec, resolver_context=self._resolver_context)
        fsapfs_volume = container_file_entry.GetAPFSVolume()
        try:
            is_locked = (not apfs_helper.APFSUnlockVolume(fsapfs_volume, scan_node.path_spec, resolver.Resolver.key_chain))
        except IOError as exception:
            raise errors.BackEndError('Unable to unlock APFS volume with error: {0!s}'.format(exception))
    else:
        file_object = resolver.Resolver.OpenFileObject(scan_node.path_spec, resolver_context=self._resolver_context)
        is_locked = ((not file_object) or file_object.is_locked)
        file_object.close()
    if is_locked:
        scan_context.LockScanNode(scan_node.path_spec)
        if (scan_node.type_indicator == definitions.TYPE_INDICATOR_BDE):
            path_spec = self.ScanForFileSystem(scan_node.path_spec.parent)
            if path_spec:
                scan_context.AddScanNode(path_spec, scan_node.parent_node)

Scans an encrypted volume node for supported formats.

Args:
scan_context (SourceScannerContext): source scanner context.
scan_node (SourceScanNode): source scan node.

Raises:
BackEndError: if the scan node cannot be unlocked.
ValueError: if the scan context or scan node is invalid.


def row_limits(self, name=None):
    with ops.name_scope(name, 'RaggedRowLimits', [self]):
        return self._row_partition.row_limits()

Returns the limit indices for rows in this ragged tensor.

These indices specify where the values for each row end in
`self.values`.  `rt.row_limits(self)` is equal to `rt.row_splits[:-1]`.

Args:
name: A name prefix for the returned tensor (optional).

Returns:
A 1-D integer Tensor with shape `[nrows]`.
The returned tensor is nonnegative, and is sorted in ascending order.

#### Example:

>>> rt = tf.ragged.constant([[3, 1, 4, 1], [], [5, 9, 2], [6], []])
>>> print(rt.values)
tf.Tensor([3 1 4 1 5 9 2 6], shape=(8,), dtype=int32)
>>> print(rt.row_limits())  # indices of row limits in rt.values
tf.Tensor([4 4 7 8 8], shape=(5,), dtype=int64)


def shift_and_pad(tensor, shift, axis=0):
    shape = tensor.shape
    rank = len(shape)
    assert (0 <= abs(axis) < rank)
    length = int(shape[axis])
    assert (0 <= abs(shift) < length)
    paddings = ([(0, 0)] * rank)
    begin = ([0] * rank)
    size = ([(- 1)] * rank)
    if (shift > 0):
        paddings[axis] = (shift, 0)
        size[axis] = (length - shift)
    elif (shift < 0):
        paddings[axis] = (0, (- shift))
        begin[axis] = (- shift)
    ret = tf.pad(tf.slice(tensor, begin, size), paddings)
    return ret

Shifts and pads with zero along an axis.

Example:
shift_and_pad([1, 2, 3, 4], 2)  --> [0, 0, 1, 2]
shift_and_pad([1, 2, 3, 4], -2) --> [3, 4, 0, 0]

Args:
tensor: Tensor; to be shifted and padded.
shift: int; number of positions to shift by.
axis: int; along which axis to shift and pad.

Returns:
A Tensor with the same shape as the input tensor.


def sever_sink_ports(self, context, ports, connected_to=None):
    if connected_to:
        source_port_lookup = self._source_port_lookup(ports.get(connected_to, []))
    else:
        source_port_lookup = True
    sink_ports = self._get_flowgraph_ports(ports, SinkPortInfo)
    if (sink_ports and source_port_lookup):
        child = context.block_view(self.mri)
        attribute_values = {}
        for (name, port_info) in sink_ports.items():
            if ((source_port_lookup is True) or (source_port_lookup.get(child[name].value, None) == port_info.port)):
                attribute_values[name] = port_info.disconnected_value
        child.put_attribute_values(attribute_values)

Conditionally sever Sink Ports of the child. If connected_to
is then None then sever all, otherwise restrict to connected_to's
Source Ports

Args:
context (Context): The context to use
ports (dict): {part_name: [PortInfo]}
connected_to (str): Restrict severing to this part


def info(self, message, domain=None):

        if domain is None:
            domain = self.extension_name
        info(message, domain)

Shortcut function for `utils.loggable.info`

Args:
message: see `utils.loggable.info`
domain: see `utils.loggable.info`


def get(self, path_info):

        assert path_info["scheme"] == "local"
        path = path_info["path"]

        if not os.path.exists(path):
            return None

        actual_mtime, actual_size = get_mtime_and_size(path)
        actual_inode = get_inode(path)

        existing_record = self.get_state_record_for_inode(actual_inode)
        if not existing_record:
            return None

        mtime, size, checksum, _ = existing_record
        if self._file_metadata_changed(actual_mtime, mtime, actual_size, size):
            return None

        self._update_state_record_timestamp_for_inode(actual_inode)
        return checksum

Gets the checksum for the specified path info. Checksum will be
retrieved from the state database if available.

Args:
path_info (dict): path info to get the checksum for.

Returns:
str or None: checksum for the specified path info or None if it
doesn't exist in the state database.


def DisableInterfaces(interface):

  set_tested_versions = ['vista', '2008']
  set_args = ['/c', 'netsh', 'set', 'interface', interface, 'DISABLED']
  host_version = platform.platform().lower()
  for version in set_tested_versions:
    if host_version.find(version) != -1:

      res = client_utils_common.Execute(
          'cmd', set_args, time_limit=-1, bypass_whitelist=True)
      return res
  return ('', 'Command not available for this version.', 99, '')

Tries to disable an interface.  Only works on Vista and 7.

Args:
interface: Name of the interface to disable.

Returns:
res which is a tuple of (stdout, stderr, exit_status, time_taken).


def get_version_from_cache_dir(src_file):
    if (src_file is None):
        return None
    tmp_dir = local.path(str(CFG['tmp_dir']))
    if tmp_dir.exists():
        cache_file = (tmp_dir / src_file)
        dir_hash = get_hash_of_dirs(cache_file)
        if (dir_hash is None):
            return None
        if (len(str(dir_hash)) <= 7):
            return str(dir_hash)
        return str(dir_hash)[:7]
    return None

Creates a version for a project out of the hash.

The hash is taken from the directory of the source file.

Args:
src_file: The source file of the project using this function.

Returns:
Either returns the first 8 digits of the hash as string,
the entire hash as a string if the hash consists out of less
than 7 digits or None if the path is incorrect.


def watch_key_to_data(self, debug_watch_key, device_name=None):
    if device_name is None:
        matching_device_names = [name for name in self._watch_key_to_datum if debug_watch_key in self._watch_key_to_datum[name]]
        if not matching_device_names:
            return []
        elif len(matching_device_names) == 1:
            device_name = matching_device_names[0]
        else:
            raise ValueError("The debug watch key '%s' exists on multiple (%d) devices, but device name is not specified." % (debug_watch_key, len(matching_device_names)))
    elif device_name not in self._debug_key_to_datum:
        raise ValueError("There is no device named '%s' consisting of debug watch keys." % device_name)
    return self._watch_key_to_datum[device_name].get(debug_watch_key, [])

Get all `DebugTensorDatum` instances corresponding to a debug watch key.

Args:
debug_watch_key: (`str`) debug watch key.
device_name: (`str`) name of the device. If there is only one device or if
the specified debug_watch_key exists on only one device, this argument
is optional.

Returns:
A list of `DebugTensorDatum` instances that correspond to the debug watch
key. If the watch key does not exist, returns an empty list.

Raises:
ValueError: If there are multiple devices that have the debug_watch_key,
but device_name is not specified.


def _associate_third_party_urls(self, feed_item, creative):
    third_party_urls = []
    for third_party_url in feed_item.get('third_party_urls', []):
        third_party_url_type = FieldMap.THIRD_PARTY_URL_TYPE_MAP.get(third_party_url.get(FieldMap.THIRD_PARTY_URL_TYPE, None))
        if third_party_url_type:
            third_party_urls.append({'thirdPartyUrlType': third_party_url_type, 'url': third_party_url.get(FieldMap.THIRD_PARTY_URL, None)})
    if third_party_urls:
        creative['thirdPartyUrls'] = third_party_urls

Associate third party urls with the respective creative DCM object.

This method transforms all child feed mapped earlier into DCM formatted
associations within the creative object so it can be pushed to the API.

Args:
feed_item: Feed item representing the creative.
creative: DCM creative object being created or updated.


def peek_step(self, val: ArrayValue,
                  sn: "DataNode") -> Tuple[Value, "DataNode"]:

        try:
            return (val[val.index(self.parse_value(sn))], sn)
        except ValueError:
            return None, sn

Return entry value addressed by the receiver + its schema node.

Args:
val: Current value (array).
sn:  Current schema node.


def to_array(data):

    try:
        numpy_data = blosc.unpack_array(data)
    except Exception as e:
        raise ValueError("Could not load numpy data. {}".format(e))

    return numpy_data

Import a blosc array into a numpy array.

Arguments:
data: A blosc packed numpy array

Returns:
A numpy array with data from a blosc compressed array


def _create_batch(signer, transactions):
    txn_ids = [txn.header_signature for txn in transactions]
    batch_header = BatchHeader(signer_public_key=signer.get_public_key().as_hex(), transaction_ids=txn_ids).SerializeToString()
    return Batch(header=batch_header, header_signature=signer.sign(batch_header), transactions=transactions)

Creates a batch from a list of transactions and a public key, and signs
the resulting batch with the given signing key.

Args:
signer (:obj:`Signer`): The cryptographic signer
transactions (list of `Transaction`): The transactions to add to the
batch.

Returns:
`Batch`: The constructed and signed batch.


def _open_interface(self, client, uuid, iface, key):

        conn_id = self._validate_connection('open_interface', uuid, key)
        if conn_id is None:
            return

        conn_data = self._connections[uuid]
        conn_data['last_touch'] = monotonic()

        slug = self._build_device_slug(uuid)

        try:
            resp = yield self._manager.open_interface(conn_id, iface)
        except Exception as exc:
            self._logger.exception("Error in manager open interface")
            resp = {'success': False, 'reason': "Internal error: %s" % str(exc)}

        message = {'type': 'response', 'operation': 'open_interface', 'client': client}
        message['success'] = resp['success']

        if not message['success']:
            message['failure_reason'] = resp['reason']

        self._publish_response(slug, message)

Open an interface on a connected device.

Args:
client (string): The client id who is requesting this operation
uuid (int): The id of the device we're opening the interface on
iface (string): The name of the interface that we're opening
key (string): The key to authenticate the caller


def copy_pkg(self, filename, id_=(- 1)):
    for repo in self._children:
        repo.copy_pkg(filename, id_)

Copy a pkg, dmg, or zip to all repositories.

Args:
filename: String path to the local file to copy.
id_: Integer ID you wish to associate package with for a JDS
or CDP only. Default is -1, which is used for creating
a new package object in the database.


def write_index(fn, index):
    with open(fn, 'wb') as o_file:
        o_file.write(_CHECK_STRING)
        o_file.write(zlib.compress(bytes(index.to_csv(None, index=False, encoding='utf-8'), encoding='utf-8')))

Writes the index to file.

Args:
fn (str): the name of the file that will contain the index.
index (pandas.DataFrame): the index.


def wait_for_task(task_data, task_uri='/tasks'):
    taskid = post_task(task_data, task_uri)
    if isinstance(task_data, str):
        json_data = json.loads(task_data)
    else:
        json_data = task_data
    job = json_data['job'][0]
    env = job.get('credentials')
    task_type = job.get('type')
    timeout = TASK_TIMEOUTS.get(env, dict()).get(task_type, DEFAULT_TASK_TIMEOUT)
    LOG.debug('Task %s will timeout after %s', task_type, timeout)
    return check_task(taskid, timeout)

Run task and check the result.

Args:
task_data (str): the task json to execute

Returns:
str: Task status.


def from_grpc_error(rpc_exc):

    if isinstance(rpc_exc, grpc.Call):
        return from_grpc_status(
            rpc_exc.code(), rpc_exc.details(), errors=(rpc_exc,), response=rpc_exc
        )
    else:
        return GoogleAPICallError(str(rpc_exc), errors=(rpc_exc,), response=rpc_exc)

Create a :class:`GoogleAPICallError` from a :class:`grpc.RpcError`.

Args:
rpc_exc (grpc.RpcError): The gRPC error.

Returns:
GoogleAPICallError: An instance of the appropriate subclass of
:class:`GoogleAPICallError`.


def get_slot_names(self, *args, **kwargs):
    return self._opt.get_slot_names(*args, **kwargs)

Return a list of the names of slots created by the `Optimizer`.

This simply wraps the get_slot_names() from the actual optimizer.

Args:
*args: Arguments for get_slot().
**kwargs: Keyword arguments for get_slot().

Returns:
A list of strings.


def import_optional(mod_name):
    try:
        return import_module(mod_name)
    except ImportError:
        pass
    except Exception:
        msg = 'Failed to import optional module `{}`'.format(mod_name)
        log.exception(msg)

Attempt to import an optional dependency.

Silently returns None if the requested module is not available.

Args:
mod_name (str) : name of the optional module to try to import

Returns:
imported module or None, if import fails


def run(
        self,
        inputs: Dict[str, Union[float, Iterable]],
        torch_size: Optional[int] = None,
    ) -> Union[float, Iterable]:

        for i in self.inputs:
            self.nodes[i]["value"] = inputs[i]

        for func_set in self.function_sets:
            for func_name in func_set:
                lambda_fn = self.nodes[func_name]["lambda_fn"]
                output_node = list(self.successors(func_name))[0]

                signature = self.nodes[func_name]["func_inputs"]
                input_values = [self.nodes[n]["value"] for n in signature]
                res = lambda_fn(*input_values)

                if torch_size is not None and len(signature) == 0:
                    self.nodes[output_node]["value"] = torch.tensor(
                        [res] * torch_size, dtype=torch.double
                    )
                else:
                    self.nodes[output_node]["value"] = res

        return self.nodes[self.output_node]["value"]

Executes the GrFN over a particular set of inputs and returns the
result.

Args:
inputs: Input set where keys are the names of input nodes in the
GrFN and each key points to a set of input values (or just one).

Returns:
A set of outputs from executing the GrFN, one for every set of
inputs.


def shutdown_tpu_system_impl(cluster_resolver, tpu_cluster_resolver_cls):
    if tpu_cluster_resolver_cls is None or not issubclass(tpu_cluster_resolver_cls, cluster_resolver_lib.ClusterResolver) or (not hasattr(tpu_cluster_resolver_cls, 'tpu_hardware_feature')):
        raise TypeError('tpu_cluster_resolver_cls is not tf.distribute.cluster_resolver.TPUClusterResolver.')
    job = None
    if cluster_resolver is None:
        if context.executing_eagerly():
            curr_device = device.DeviceSpec.from_string(context.context().device_name)
            if curr_device.job is not None:
                job = '{}/replica:0/task:0'.format(curr_device.job)
        cluster_resolver = tpu_cluster_resolver_cls('')
    assert isinstance(cluster_resolver, tpu_cluster_resolver_cls)
    tpu_name = compat.as_text(cluster_resolver._tpu)
    if tpu_name not in _INITIALIZED_TPU_SYSTEMS:
        logging.warning('You are shutting down a TPU system %s that has not been initialized.' % tpu_name)
    logging.info('Shutting down the TPU system: %s', tpu_name)
    if context.executing_eagerly():
        if tpu_name not in _LOCAL_MASTERS:
            job = '{}/replica:0/task:0'.format(cluster_resolver.get_job_name())

        @def_function.function(autograph=False)
        def _tpu_shutdown_fn():
            tpu.shutdown_system(job=job)
        run_eagerly = def_function.functions_run_eagerly()
        if run_eagerly:
            logging.warning('It looks like tf.function behavior was disabled, perhaps using tf.config.run_functions_eagerly. tf.tpu.experimental.shutdown_tpu_system requires tf.function to work. This primitive will override the disable.')
            def_function.run_functions_eagerly(False)
        try:
            with ops.device(tpu._tpu_system_device_name(job)):
                _tpu_shutdown_fn()
        finally:
            if run_eagerly is not None:
                def_function.run_functions_eagerly(run_eagerly)
        logging.info('Clearing out eager caches')
        context.context()._clear_caches()
        context.context().clear_kernel_cache()
    elif not ops.executing_eagerly_outside_functions():
        master = cluster_resolver.master()
        cluster_spec = cluster_resolver.cluster_spec()
        session_config = config_pb2.ConfigProto(allow_soft_placement=True)
        if cluster_spec:
            session_config.cluster_def.CopyFrom(cluster_spec.as_cluster_def())
        with ops.Graph().as_default():
            with session_lib.Session(config=session_config, target=master) as sess:
                sess.run(tpu.shutdown_system())
    else:
        raise RuntimeError('initialize_tpu_system is not supported within tf.functions.  You should call initialize_tpu_system outside of your tf.function. ')
    logging.info('Finished shutting down TPU system.')
    if tpu_name in _INITIALIZED_TPU_SYSTEMS:
        del _INITIALIZED_TPU_SYSTEMS[tpu_name]

Implementation for tpu.experimental.shutdown_tpu_system.

Kept separate to avoid tpu_oss code duplication.

Shuts down the TPU devices.

This will clear all caches, even those that are maintained through sequential
calls to tf.tpu.experimental.initialize_tpu_system, such as the compilation
cache.

Args:
cluster_resolver: A tf.distribute.cluster_resolver.TPUClusterResolver,
which provides information about the TPU cluster.
tpu_cluster_resolver_cls: a reference to
tf.distribute.cluster_resolver.TPUClusterResolver so that an instance
of it can be initialized if cluster_resolver is None.

Raises:
RuntimeError: If no TPU devices found for eager execution or if run in a
tf.function.
TypeError: If tpu_cluster_resolver_cls is
not tf.distribute.cluster_resolver.TPUClusterResolver.


def tag(self, resource_id):
    self._request_uri = '{}/{}'.format(self._request_uri, self.tcex.safetag(resource_id))

Update the request URI to include the Tag for specific retrieval.

Args:
resource_id (string): The tag name.


def bin(values, bins, labels=None):
    bins = np.asarray(bins)
    if (labels is None):
        labels = (bins[:(- 1)] + (np.diff(bins) / 2.0))
    else:
        labels = np.asarray(labels)
    dtype = ('float' if (labels.dtype.kind == 'f') else 'O')
    binned = np.full_like(values, (np.nan if (dtype == 'f') else None), dtype=dtype)
    for (lower, upper, label) in zip(bins[:(- 1)], bins[1:], labels):
        condition = ((values > lower) & (values <= upper))
        binned[np.where(condition)[0]] = label
    return binned

Bins data into declared bins

Bins data into declared bins. By default each bin is labelled
with bin center values but an explicit list of bin labels may be
defined.

Args:
values: Array of values to be binned
bins: List or array containing the bin boundaries
labels: List of labels to assign to each bin
If the bins are length N the labels should be length N-1

Returns:
Array of binned values


def apply(self, func, **kwargs):

        self.call_queue.append((func, kwargs))

        def call_queue_closure(data, call_queues):
            result = data.copy()
            for func, kwargs in call_queues:
                try:
                    result = func(result, **kwargs)
                except Exception as e:
                    self.call_queue = []
                    raise e
            return result

        new_data = call_queue_closure(self.data, self.call_queue)
        self.call_queue = []
        return PandasOnPythonFramePartition(new_data)

Apply some callable function to the data in this partition.

Note: It is up to the implementation how kwargs are handled. They are
an important part of many implementations. As of right now, they
are not serialized.

Args:
func: The lambda to apply (may already be correctly formatted)

Returns:
A new `BaseFramePartition` containing the object that has had `func`
applied to it.


def predict_dataset(self, x, **kwargs):
    printout = kwargs.get('printout', None)
    pred = []
    res = []
    x.columns = ['A', 'B']
    for (idx, row) in x.iterrows():
        a = scale(row['A'].reshape((len(row['A']), 1)))
        b = scale(row['B'].reshape((len(row['B']), 1)))
        pred.append(self.predict_proba(a, b, idx=idx))
        if (printout is not None):
            res.append([row['SampleID'], pred[(- 1)]])
            DataFrame(res, columns=['SampleID', 'Predictions']).to_csv(printout, index=False)
    return pred

Generic dataset prediction function.

Runs the score independently on all pairs.

Args:
x (pandas.DataFrame): a CEPC format Dataframe.
kwargs (dict): additional arguments for the algorithms

Returns:
pandas.DataFrame: a Dataframe with the predictions.


def movie_list(self, **kwargs):
    path = self._get_path('movie_list')
    response = self._GET(path, kwargs)
    self._set_attrs_to_values(response)
    return response

Get the list of Movie genres.

Args:
language: (optional) ISO 639-1 code.

Returns:
A dict respresentation of the JSON returned from the API.


def parse_simple_id(chrom, pos, ref, alt):

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

Parse the simple id for a variant

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

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

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


def new_named_tuple(self, base_name, fields):
    nt = namedtuple.NamedTuple(base_name, fields, self.generated_classes)
    self.generated_classes[base_name].append(nt.cls)
    self.add_import('typing', ['NamedTuple'])
    return pytd.NamedType(nt.name)

Return a type for a named tuple (implicitly generates a class).

Args:
base_name: The named tuple's name.
fields: A list of (name, type) tuples.

Returns:
A NamedType() for the generated class that describes the named tuple.


def filter_traceback(fn):
    if sys.version_info.major != 3 or sys.version_info.minor < 7:
        return fn

    def error_handler(*args, **kwargs):
        try:
            if not is_traceback_filtering_enabled():
                return fn(*args, **kwargs)
        except NameError:
            return fn(*args, **kwargs)
        filtered_tb = None
        try:
            return fn(*args, **kwargs)
        except Exception as e:
            filtered_tb = _process_traceback_frames(e.__traceback__)
            raise e.with_traceback(filtered_tb) from None
        finally:
            del filtered_tb
    return tf_decorator.make_decorator(fn, error_handler)

Decorator to filter out TF-internal stack trace frames in exceptions.

Raw TensorFlow stack traces involve many internal frames, which can be
challenging to read through, while not being actionable for end users.
By default, TensorFlow filters internal frames in most exceptions that it
raises, to keep stack traces short, readable, and focused on what's
actionable for end users (their own code).

Arguments:
fn: The function or method to decorate. Any exception raised within the
function will be reraised with its internal stack trace frames filtered
out.

Returns:
Decorated function or method.


def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):
    super(LongInteger, self).read(istream, kmip_version=kmip_version)
    if (self.length is not LongInteger.LENGTH):
        raise exceptions.InvalidPrimitiveLength('invalid long integer length read; expected: {0}, observed: {1}'.format(LongInteger.LENGTH, self.length))
    self.value = unpack('!q', istream.read(self.length))[0]
    self.validate()

Read the encoding of the LongInteger from the input stream.

Args:
istream (stream): A buffer containing the encoded bytes of a
LongInteger. Usually a BytearrayStream object. Required.
kmip_version (KMIPVersion): An enumeration defining the KMIP
version with which the object will be decoded. Optional,
defaults to KMIP 1.0.

Raises:
InvalidPrimitiveLength: if the long integer encoding read in has
an invalid encoded length.


def on_message(self, message):
    if ('content' in message['d']):
        metadata = self._parse_metadata(message)
        message = Message(text=message['d']['content'], metadata=metadata).__dict__
        logger.debug(message)
        self.baseplate.tell(message)

Runs on a create_message event from websocket connection

Args:
message (dict): Full message from Discord websocket connection"


def GetFileSystem(self, path_spec):
    identifier = self._GetFileSystemCacheIdentifier(path_spec)
    return self._file_system_cache.GetObject(identifier)

Retrieves a file system object defined by path specification.

Args:
path_spec (PathSpec): path specification.

Returns:
FileSystem: a file system object or None if not cached.


def is_win_python35_or_earlier():
    return ((sys.platform.startswith('win') and (sys.version_info.major < 3)) or ((sys.version_info.major == 3) and (sys.version_info.minor < 6)))

Convenience method to determine if the current platform is Windows and Python version 3.5 or earlier.

Returns:
bool: True if the current platform is Windows and the Python interpreter is 3.5 or earlier; False otherwise.


def copy_course_videos(source_course_id, destination_course_id):

    if source_course_id == destination_course_id:
        return

    course_videos = CourseVideo.objects.select_related('video', 'video_image').filter(
        course_id=six.text_type(source_course_id)
    )

    for course_video in course_videos:
        destination_course_video, __ = CourseVideo.objects.get_or_create(
            video=course_video.video,
            course_id=destination_course_id
        )
        if hasattr(course_video, 'video_image'):
            VideoImage.create_or_update(
                course_video=destination_course_video,
                file_name=course_video.video_image.image.name
            )

Adds the destination_course_id to the videos taken from the source_course_id

Args:
source_course_id: The original course_id
destination_course_id: The new course_id where the videos will be copied


def _GetFieldPathElementIndex(api_error, field):
    field_path_elements = api_error['fieldPathElements']
    if field_path_elements:
        found_index = [field_path_element['index'] for field_path_element in field_path_elements if (field_path_element['field'] == field)]
        if found_index:
            return found_index
    return None

Retrieve the index of a given field in the api_error's fieldPathElements.

Args:
api_error: a dict containing a partialFailureError returned from the AdWords
API.
field: a str field for which this determines the index in the api_error's
fieldPathElements.

Returns:
An int index of the field path element, or None if the specified field can't
be found in the api_error.


def _GetFileSystemTypeFromFileEntry(self, file_entry):
    if (file_entry.type_indicator != dfvfs_definitions.TYPE_INDICATOR_TSK):
        return file_entry.type_indicator
    file_system = file_entry.GetFileSystem()
    fs_info = file_system.GetFsInfo()
    if fs_info.info:
        type_string = '{0!s}'.format(fs_info.info.ftype)
        if type_string.startswith('TSK_FS_TYPE_'):
            type_string = type_string[12:]
        if type_string.endswith('_DETECT'):
            type_string = type_string[:(- 7)]
    return type_string

Retrieves the file system type indicator of a file entry.

Args:
file_entry (dfvfs.FileEntry): a file entry.

Returns:
str: file system type.


def _get_all_migrations():
    from . import migrations
    package = migrations
    prefix = (package.__name__ + '.')
    all_migrations = []
    for (importer, modname, ispkg) in pkgutil.iter_modules(package.__path__, prefix):
        version = int(modname.split('.')[(- 1)].split('_')[0])
        all_migrations.append((version, modname))
    all_migrations = sorted(all_migrations, key=(lambda x: x[0]))
    return all_migrations

Returns sorted list of all migrations.

Returns:
list of (int, str) tuples: first elem of the tuple is migration number, second if module name.


def __init__(self, config, auth, trix_id, feed_name, parse=True, spreadsheet=None, timezone=None):
    self.config = config
    self.auth = auth
    self.trix_id = trix_id
    self.trix_range = 'A1:AZ'
    self.feed_name = feed_name
    self._parse = parse
    self._timezone = timezone or 'America/New_York'
    if spreadsheet:
        self.spreadsheet = spreadsheet
    else:
        self.spreadsheet = sheets_get(self.config, self.auth, self.trix_id)
    self.raw_feed = self._get_feed()
    self.feed = self._feed_to_dict(parse=self._parse)

Initializes the feed with parameters.

Args:
auth: The authentication scheme to use based on the json configuration
file.
trix_id: Unique identifier of the Google Sheet that represents the
Bulkdozer feed.
feed_name: The name of the feed to initialize.
spreadsheet: Optional, the spreadsheet object representing the Bulkdozer
feed spreadsheet, useful to limit calls to the sheets API and allow
multiple Feed objects to use the same spreadsheet instance. This is used
to determine which tabs exist in the feed so the correct one can be
selected for the entity this Feed object represents.


def endswith(self, search_str):

        for entry in reversed(list(open(self._jrnl_file, 'r'))[-5:]):
            if search_str in entry:
                return True

        return False

Check whether the provided string exists in Journal file.

Only checks the last 5 lines of the journal file. This method is
usually used when tracking a journal from an active Revit session.

Args:
search_str (str): string to search for

Returns:
bool: if True the search string is found


def list_occupied_adb_ports():
    out = AdbProxy().forward('--list')
    clean_lines = str(out, 'utf-8').strip().split('\n')
    used_ports = []
    for line in clean_lines:
        tokens = line.split(' tcp:')
        if len(tokens) != 3:
            continue
        used_ports.append(int(tokens[1]))
    return used_ports

Lists all the host ports occupied by adb forward.

This is useful because adb will silently override the binding if an attempt
to bind to a port already used by adb was made, instead of throwing binding
error. So one should always check what ports adb is using before trying to
bind to a port with adb.

Returns:
A list of integers representing occupied host ports.


def simulate_measurement(self, index: int) -> bool:
    args = self._shard_num_args({'index': index})
    prob_one = np.sum(self._pool.map(_one_prob_per_shard, args))
    result = bool((np.random.random() <= prob_one))
    args = self._shard_num_args({'index': index, 'result': result, 'prob_one': prob_one})
    self._pool.map(_collapse_state, args)
    return result

Simulates a single qubit measurement in the computational basis.

Args:
index: Which qubit is measured.

Returns:
True iff the measurement result corresponds to the |1> state.


def call(self, hidden_states: tf.Tensor, attention_mask: tf.Tensor | None=None, encoder_hidden_states: tf.Tensor | None=None, encoder_attention_mask: tf.Tensor | None=None, layer_head_mask: tf.Tensor | None=None, cross_attn_layer_head_mask: tf.Tensor | None=None, past_key_value: Tuple[tf.Tensor] | None=None, training: Optional[bool]=False) -> Tuple[tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]:
    residual = hidden_states
    hidden_states = self.self_attn_layer_norm(hidden_states)
    self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None
    hidden_states, self_attn_weights, present_key_value = self.self_attn(hidden_states=hidden_states, past_key_value=self_attn_past_key_value, attention_mask=attention_mask, layer_head_mask=layer_head_mask)
    hidden_states = self.dropout(hidden_states, training=training)
    hidden_states = residual + hidden_states
    cross_attn_present_key_value = None
    cross_attn_weights = None
    if encoder_hidden_states is not None:
        residual = hidden_states
        hidden_states = self.encoder_attn_layer_norm(hidden_states)
        cross_attn_past_key_value = past_key_value[-2:] if past_key_value is not None else None
        hidden_states, cross_attn_weights, cross_attn_present_key_value = self.encoder_attn(hidden_states=hidden_states, key_value_states=encoder_hidden_states, attention_mask=encoder_attention_mask, layer_head_mask=cross_attn_layer_head_mask, past_key_value=cross_attn_past_key_value)
        hidden_states = self.dropout(hidden_states, training=training)
        hidden_states = residual + hidden_states
        present_key_value = present_key_value + cross_attn_present_key_value
    residual = hidden_states
    hidden_states = self.final_layer_norm(hidden_states)
    hidden_states = self.activation_fn(self.fc1(hidden_states))
    hidden_states = self.activation_dropout(hidden_states, training=training)
    hidden_states = self.fc2(hidden_states)
    hidden_states = self.dropout(hidden_states, training=training)
    hidden_states = residual + hidden_states
    return (hidden_states, self_attn_weights, cross_attn_weights, present_key_value)

Args:
hidden_states (`tf.Tensor`): input to the layer of shape *(batch, seq_len, embed_dim)*
attention_mask (`tf.Tensor`): attention mask of size
*(batch, 1, tgt_len, src_len)* where padding elements are indicated by very large negative values.
encoder_hidden_states (`tf.Tensor`):
cross attention input to the layer of shape *(batch, seq_len, embed_dim)*
encoder_attention_mask (`tf.Tensor`): encoder attention mask of size
*(batch, 1, tgt_len, src_len)* where padding elements are indicated by very large negative values.
layer_head_mask (`tf.Tensor`): mask for attention heads in a given layer of size
*(decoder_attention_heads,)*
cross_attn_layer_head_mask (`tf.Tensor`): mask for heads of the cross-attention module.
*(decoder_attention_heads,)*
past_key_value (`Tuple(tf.Tensor)`): cached past key and value projection states


def compute(self, x, yerr):
    K = self.kernel.get_value(x)
    K[np.diag_indices_from(K)] += (yerr ** 2)
    self._factor = (cholesky(K, overwrite_a=True, lower=False), False)
    self.log_determinant = (2 * np.sum(np.log(np.diag(self._factor[0]))))
    self.computed = True

Compute and factorize the covariance matrix.

Args:
x (ndarray[nsamples, ndim]): The independent coordinates of the
data points.
yerr (ndarray[nsamples] or float): The Gaussian uncertainties on
the data points at coordinates ``x``. These values will be
added in quadrature to the diagonal of the covariance matrix.


def delay_embedding(data, emb_dim, lag=1):
    data = np.asarray(data)
    min_len = (((emb_dim - 1) * lag) + 1)
    if (len(data) < min_len):
        msg = ('cannot embed data of length {} with embedding dimension {} ' + 'and lag {}, minimum required length is {}')
        raise ValueError(msg.format(len(data), emb_dim, lag, min_len))
    m = ((len(data) - min_len) + 1)
    indices = np.repeat([(np.arange(emb_dim) * lag)], m, axis=0)
    indices += np.arange(m).reshape((m, 1))
    return data[indices]

Perform a time-delay embedding of a time series

Args:
data (array-like):
the data that should be embedded
emb_dim (int):
the embedding dimension
Kwargs:
lag (int):
the lag between elements in the embedded vectors

Returns:
emb_dim x m array:
matrix of embedded vectors of the form
[data[i], data[i+lag], data[i+2*lag], ... data[i+(emb_dim-1)*lag]]
for i in 0 to m-1 (m = len(data)-(emb_dim-1)*lag)


def reviews(self, packageName, filterByDevice=False, sort=2,
                nb_results=None, offset=None):

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

Browse reviews for an application

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

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


def full_tasktrace(self):
    if self.prev_error:
        return (self.prev_error.tasktrace + self.tasktrace)
    else:
        return self.tasktrace

List of all failed tasks caused by this and all previous errors.

Returns:
List[Task]


def RemoveBitbucketServerConnectedRepository(self, request, global_params=None):
    config = self.GetMethodConfig('RemoveBitbucketServerConnectedRepository')
    return self._RunMethod(config, request, global_params=global_params)

Remove a Bitbucket Server repository from an given BitbucketServerConfig's connected repositories. This API is experimental.

Args:
request: (CloudbuildProjectsLocationsBitbucketServerConfigsRemoveBitbucketServerConnectedRepositoryRequest) input message
global_params: (StandardQueryParameters, default: None) global arguments
Returns:
(Empty) The response message.


def update_permissions(self, grp_name, resource, permissions):

        self.project_service.set_auth(self._token_project)
        self.project_service.update_permissions(grp_name, resource, permissions)

Update permissions for the group associated with the given resource.

Args:
grp_name (string): Name of group.
resource (intern.resource.boss.Resource): Identifies which data
model object to operate on
permissions (list): List of permissions to add to the given resource

Raises:
requests.HTTPError on failure.


def _render_trajectories(self, trajectories: Tuple[(NonFluents, Fluents, Fluents, Fluents, np.array)]) -> None:
    if self._verbose:
        (non_fluents, initial_state, states, actions, interms, rewards) = trajectories
        shape = states[0][1].shape
        (batch_size, horizon) = (shape[0], shape[1])
        states = [(s[0], s[1][0]) for s in states]
        interms = [(f[0], f[1][0]) for f in interms]
        actions = [(a[0], a[1][0]) for a in actions]
        rewards = np.reshape(rewards, [batch_size, horizon])[0]
        self._render_batch(non_fluents, states, actions, interms, rewards)

Prints the first batch of simulated `trajectories`.

Args:
trajectories: NonFluents, states, actions, interms and rewards.


def update_video_status(edx_video_id, status):

    try:
        video = _get_video(edx_video_id)
    except Video.DoesNotExist:
        error_message = u"Video not found when trying to update video status with edx_video_id: {0}".format(
            edx_video_id
        )
        raise ValVideoNotFoundError(error_message)

    video.status = status
    video.save()

Update status for an existing video.

Args:
edx_video_id: ID of the video
status: video status

Raises:
Raises ValVideoNotFoundError if the video cannot be retrieved.


def should_include_file_in_search(file_name, extensions, exclude_dirs):
    return (((exclude_dirs is None) or (not any((file_name.startswith(d) for d in exclude_dirs)))) and any((file_name.endswith(e) for e in extensions)))

Whether or not a filename matches a search criteria according to arguments.

Args:
file_name (str): A file path to check.
extensions (list): A list of file extensions file should match.
exclude_dirs (list): A list of directories to exclude from search.

Returns:
A boolean of whether or not file matches search criteria.


def _assert_ranks_condition(x, ranks, static_condition, dynamic_condition, data, summarize):
    for rank in ranks:
        assert_type(rank, dtypes.int32)
    ranks_static = tuple([tensor_util.constant_value(rank) for rank in ranks])
    if not any((r is None for r in ranks_static)):
        for rank_static in ranks_static:
            if rank_static.ndim != 0:
                raise ValueError('Rank must be a scalar.')
        x_rank_static = x.get_shape().ndims
        if x_rank_static is not None:
            if not static_condition(x_rank_static, ranks_static):
                raise ValueError('Static rank condition failed', x_rank_static, ranks_static)
            return control_flow_ops.no_op(name='static_checks_determined_all_ok')
    condition = dynamic_condition(array_ops.rank(x), ranks)
    for rank, rank_static in zip(ranks, ranks_static):
        if rank_static is None:
            this_data = ['Rank must be a scalar. Received rank: ', rank]
            rank_check = assert_rank(rank, 0, data=this_data)
            condition = control_flow_ops.with_dependencies([rank_check], condition)
    return control_flow_assert.Assert(condition, data, summarize=summarize)

Assert `x` has a rank that satisfies a given condition.

Args:
x:  Numeric `Tensor`.
ranks:  Scalar `Tensor`.
static_condition:   A python function that takes
`[actual_rank, given_ranks]` and returns `True` if the condition is
satisfied, `False` otherwise.
dynamic_condition:  An `op` that takes [actual_rank, given_ranks]
and return `True` if the condition is satisfied, `False` otherwise.
data:  The tensors to print out if the condition is false.  Defaults to
error message and first few entries of `x`.
summarize: Print this many entries of each tensor.

Returns:
Op raising `InvalidArgumentError` if `x` fails dynamic_condition.

Raises:
ValueError:  If static checks determine `x` fails static_condition.


def __init__(
        self, bind_addr, gateway,
        minthreads=10, maxthreads=-1, server_name=None,
        peercreds_enabled=False, peercreds_resolve_enabled=False,
    ):

        self.bind_addr = bind_addr
        self.gateway = gateway

        self.requests = threadpool.ThreadPool(
            self, min=minthreads or 1, max=maxthreads,
        )

        if not server_name:
            server_name = self.version
        self.server_name = server_name
        self.peercreds_enabled = peercreds_enabled
        self.peercreds_resolve_enabled = (
            peercreds_resolve_enabled and peercreds_enabled
        )
        self.clear_stats()

Initialize HTTPServer instance.

Args:
bind_addr (tuple): network interface to listen to
gateway (Gateway): gateway for processing HTTP requests
minthreads (int): minimum number of threads for HTTP thread pool
maxthreads (int): maximum number of threads for HTTP thread pool
server_name (str): web server name to be advertised via Server
HTTP header


def RegisterMessage(self, message):

    desc = message.DESCRIPTOR
    self._symbols[desc.full_name] = message
    if desc.file.name not in self._symbols_by_file:
      self._symbols_by_file[desc.file.name] = {}
    self._symbols_by_file[desc.file.name][desc.full_name] = message
    self.pool.AddDescriptor(desc)
    return message

Registers the given message type in the local database.

Args:
message: a message.Message, to be registered.

Returns:
The provided message.


def validate_submission(self, filename):

    self._prepare_temp_dir()

    filename = os.path.abspath(filename)

    if not self._extract_submission(filename):
      return None

    if not self._verify_submission_size():
      return None

    metadata = self._load_and_verify_metadata()
    if not metadata:
      return None
    submission_type = metadata['type']

    if not self._verify_docker_image_size(metadata['container_gpu']):
      return None

    self._prepare_sample_data(submission_type)
    if not self._run_submission(metadata):
      logging.error('Failure while running submission')
      return None
    if not self._verify_output(submission_type):
      logging.warning('Some of the outputs of your submission are invalid or '
                      'missing. You submission still will be evaluation '
                      'but you might get lower score.')
    return metadata

Validates submission.

Args:
filename: submission filename

Returns:
submission metadata or None if submission is invalid


def array(data, tcoords=None, chcoords=None, scalarcoords=None, datacoords=None, attrs=None, name=None):
    array = xr.DataArray(data, dims=('t', 'ch'), attrs=attrs, name=name)
    array.dca._initcoords()
    if (tcoords is not None):
        array.coords.update({key: ('t', tcoords[key]) for key in tcoords})
    if (chcoords is not None):
        array.coords.update({key: ('ch', chcoords[key]) for key in chcoords})
    if (scalarcoords is not None):
        array.coords.update(scalarcoords)
    if (datacoords is not None):
        array.coords.update({key: (('t', 'ch'), datacoords[key]) for key in datacoords})
    return array

Create an array as an instance of xarray.DataArray with Decode accessor.

Args:
data (numpy.ndarray): 2D (time x channel) array.
tcoords (dict, optional): Dictionary of arrays that label time axis.
chcoords (dict, optional): Dictionary of arrays that label channel axis.
scalarcoords (dict, optional): Dictionary of values that don't label any axes (point-like).
datacoords (dict, optional): Dictionary of arrays that label time and channel axes.
attrs (dict, optional): Dictionary of attributes to add to the instance.
name (str, optional): String that names the instance.

Returns:
array (decode.array): Deode array.


def get_signature_request(self, signature_request_id, ux_version=None):

        request = self._get_request()
        parameters = None

        if ux_version is not None:
            parameters = {
                'ux_version': ux_version
            }

        return request.get(self.SIGNATURE_REQUEST_INFO_URL + signature_request_id, parameters=parameters)

Get a signature request by its ID

Args:

signature_request_id (str):     The id of the SignatureRequest to retrieve

ux_version (int):               UX version, either 1 (default) or 2.

Returns:
A SignatureRequest object


def __init__(self, message_type, name=None,
               indexed_fields=None, protocol=None, **kwds):

    if not (isinstance(message_type, type) and
            issubclass(message_type, messages.Message)):
      raise TypeError('MessageProperty argument must be a Message subclass')
    self._message_type = message_type
    if indexed_fields is not None:

      self._indexed_fields = tuple(indexed_fields)

    if protocol is None:
      protocol = _default_protocol
    self._protocol = protocol
    self._protocol_impl = _protocols_registry.lookup_by_name(protocol)
    blob_prop = model.BlobProperty('__%s__' % self._protocol)

    message_class = _make_model_class(message_type, self._indexed_fields,
                                      blob_=blob_prop)
    super(MessageProperty, self).__init__(message_class, name, **kwds)

Constructor.

Args:
message_tyoe: A subclass of protorpc.messages.Message.
name: Optional datastore name (defaults to the property name).
indexed_fields: Optional list of dotted and undotted field names.
protocol: Optional protocol name default 'protobuf'.

Additional keywords arguments specify the same options as
supported by StructuredProperty, except 'indexed'.


def _bigbird_block_rand_mask(from_seq_length, to_seq_length, from_block_size, to_block_size, num_rand_blocks, indices_prng_key: Optional[jax.random.PRNGKey]=None, deterministic: Optional[bool]=True, last_idx: Optional[int]=-1):
    if from_seq_length
        raise ValueError('Error the number of blocks needs to be same!')
    rand_attn = jnp.zeros((from_seq_length
    if deterministic:
        return rand_attn
    middle_seq = jnp.arange(1, to_seq_length
    last = to_seq_length
    if last_idx > 2 * to_block_size:
        last = last_idx
    r = num_rand_blocks
    for i in range(1, from_seq_length
        start = i - 2
        end = i
        if i == 1:
            seq_values = jax.random.permutation(indices_prng_key, middle_seq[2:last])[:r]
            rand_attn = rand_attn.at[i - 1].set(seq_values)
        elif i == 2:
            seq_values = jax.random.permutation(indices_prng_key, middle_seq[3:last])[:r]
            rand_attn = rand_attn.at[i - 1].set(seq_values)
        elif i == from_seq_length
            seq_values = jax.random.permutation(indices_prng_key, middle_seq[:last])[:r]
            rand_attn = rand_attn.at[i - 1].set(seq_values)
        elif i == from_seq_length
            seq_values = jax.random.permutation(indices_prng_key, middle_seq[:last])[:r]
            rand_attn = rand_attn.at[i - 1].set(seq_values)
        elif start > last:
            start = last
            seq_values = jax.random.permutation(indices_prng_key, middle_seq[:start])[:r]
            rand_attn = rand_attn.at[i - 1].set(seq_values)
        elif end + 1 == last:
            seq_values = jax.random.permutation(indices_prng_key, middle_seq[:start])[:r]
            rand_attn = rand_attn.at[i - 1].set(seq_values)
        else:
            concat_values = jnp.concatenate((middle_seq[:start], middle_seq[end + 1:last]))
            seq_values = jax.random.permutation(indices_prng_key, concat_values)[:r]
            rand_attn = rand_attn.at[i - 1].set(seq_values)
    return rand_attn

Create adjacency list of random attention.

Args:
from_seq_length: int. length of from sequence.
to_seq_length: int. length of to sequence.
from_block_size: int. size of block in from sequence.
to_block_size: int. size of block in to sequence.
num_rand_blocks: int. Number of random chunks per row.
indices_prng_key: jax.random.PRNGKey. PRNG key that is used to perform random jax operations.
deterministic: bool. When False random attention will be used.
last_idx: if -1 then num_rand_blocks blocks chosen anywhere in to sequence,
if positive then num_rand_blocks blocks chosen only up to last_idx.

Returns:
adjacency list of size from_seq_length//from_block_size-2 by num_rand_blocks


def vocab_token_counts(text_filepattern, max_lines):

  ret = {}
  for i, line in enumerate(
      _read_filepattern(text_filepattern, max_lines=max_lines)):
    if "," not in line:
      tf.logging.warning("Malformed vocab line
      continue

    token, count = line.rsplit(",", 1)
    ret[_native_to_unicode(token)] = int(count)

  return ret

Read a vocab file and return a dictionary of token counts.

Reads a two-column CSV file of tokens and their frequency in a dataset. The
tokens are presumed to be generated by encode() or the equivalent.

Args:
text_filepattern: A pattern matching one or more files.
max_lines: An integer; maximum total lines to read.

Returns:
a dictionary mapping token to count.


def SMGetJobDictionaries(self, domain='kSMDomainSystemLaunchd'):
    cfstring_launchd = ctypes.c_void_p.in_dll(self.dll, domain)
    return CFArray(self.dll.SMCopyAllJobDictionaries(cfstring_launchd))

Copy all Job Dictionaries from the ServiceManagement.

Args:
domain: The name of a constant in Foundation referencing the domain.
Will copy all launchd services by default.

Returns:
A marshalled python list of dicts containing the job dictionaries.


def append(self, key, value, expire=0, noreply=None):

        if noreply is None:
            noreply = self.default_noreply
        return self._store_cmd(b'append', {key: value}, expire, noreply)[key]

The memcached "append" command.

Args:
key: str, see class docs for details.
value: str, see class docs for details.
expire: optional int, number of seconds until the item is expired
from the cache, or zero for no expiry (the default).
noreply: optional bool, True to not wait for the reply (defaults to
self.default_noreply).

Returns:
True.


def _request(self, method, url, **kwargs):
    resp = self._session.request(method, '{}/{}'.format(self._base_url, url), headers=self._headers, **kwargs)
    try:
        resp.raise_for_status()
    except HTTPError as e:
        logging.error(resp.content)
        raise RestClientError(e)
    return resp

Make HTTP request and return response object

Args:
method (str): GET, POST, PUT, DELETE
url (str): path appended to the base_url to create request
**kwargs: passed directly to a requests.request object


def load_tiff_multipage(tiff_filename, dtype='float32'):
    if (not os.path.isfile(tiff_filename)):
        raise RuntimeError(('could not find file "%s"' % tiff_filename))
    data = tiff.imread(tiff_filename)
    im = []
    while True:
        Xi = numpy.array(data, dtype=dtype)
        if (Xi.ndim == 2):
            Xi = Xi[(numpy.newaxis, ...)]
        im.append(Xi)
        try:
            data.seek((data.tell() + 1))
        except EOFError:
            break
    im = numpy.concatenate(im, axis=0)
    im = numpy.rollaxis(im, 1)
    im = numpy.rollaxis(im, 2)
    return im

Load a multipage tiff into a single variable in x,y,z format.

Arguments:
tiff_filename:     Filename of source data
dtype:             data type to use for the returned tensor

Returns:
Array containing contents from input tiff file in xyz order


def show_path(from_op, tensors, sources):
    if isinstance(from_op, tensor_lib.Tensor):
        from_op = from_op.op
    if not isinstance(tensors, list):
        tensors = [tensors]
    final_ops = [_as_operation(tensor) for tensor in tensors]
    visited_ops = set((x.op for x in sources))
    ops_to_visit = list(final_ops)
    some_op_output = {}
    while ops_to_visit:
        op = ops_to_visit.pop()
        if op in visited_ops:
            continue
        visited_ops.add(op)
        if op == from_op:
            path_op = op
            path = [path_op]
            while path_op not in final_ops:
                path_op = some_op_output[path_op]
                path.append(path_op)
            return ' <- '.join(('%s (%s)' % (x.name, x.type) for x in reversed(path)))
        else:
            for inp in graph_inputs(op):
                if inp not in visited_ops and inp not in sources:
                    some_op_output[inp] = op
                    ops_to_visit.append(inp)
    return '??'

Find one path from `from_op` to any of `tensors`, ignoring `sources`.

Args:
from_op: A `tf.Operation`.
tensors: A `tf.Operation`, a `tf.Tensor`, or a list thereof.
sources: A list of `tf.Tensor`.

Returns:
A python string containing the path, or "??" if none is found.


def write(self, output_buffer, kmip_version=enums.KMIPVersion.KMIP_2_0):
    if (kmip_version < enums.KMIPVersion.KMIP_2_0):
        raise exceptions.VersionNotSupported('KMIP {} does not support the DefaultsInformation object.'.format(kmip_version.value))
    local_buffer = BytearrayStream()
    if self._object_defaults:
        for object_default in self._object_defaults:
            object_default.write(local_buffer, kmip_version=kmip_version)
    else:
        raise exceptions.InvalidField('The DefaultsInformation structure is missing the object defaults field.')
    self.length = local_buffer.length()
    super(DefaultsInformation, self).write(output_buffer, kmip_version=kmip_version)
    output_buffer.write(local_buffer.buffer)

Write the DefaultsInformation structure encoding to the data stream.

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

Raises:
InvalidField: Raised if the object defaults field is not defined.
VersionNotSupported: Raised when a KMIP version is provided that
does not support the DefaultsInformation structure.


def expand_with_style(template, style, data, body_subtree='body'):
    if template.has_defines:
        return template.expand(data, style=style)
    else:
        tokens = []
        execute_with_style_LEGACY(template, style, data, tokens.append, body_subtree=body_subtree)
        return JoinTokens(tokens)

Expand a data dictionary with a template AND a style.

DEPRECATED -- Remove this entire function in favor of expand(d, style=style)

A style is a Template instance that factors out the common strings in several
"body" templates.

Args:
template: Template instance for the inner "page content"
style: Template instance for the outer "page style"
data: Data dictionary, with a 'body' key (or body_subtree


def add_adsorbate(self, molecule, ads_coord, repeat=None, reorient=True):
    if reorient:
        sop = get_rot(self.slab)
        molecule.apply_operation(sop.inverse)
    struct = self.slab.copy()
    if repeat:
        struct.make_supercell(repeat)
    if ('surface_properties' in struct.site_properties.keys()):
        molecule.add_site_property('surface_properties', (['adsorbate'] * molecule.num_sites))
    if ('selective_dynamics' in struct.site_properties.keys()):
        molecule.add_site_property('selective_dynamics', ([[True, True, True]] * molecule.num_sites))
    for site in molecule:
        struct.append(site.specie, (ads_coord + site.coords), coords_are_cartesian=True, properties=site.properties)
    return struct

Adds an adsorbate at a particular coordinate.  Adsorbate
represented by a Molecule object, and is positioned relative
to the input adsorbate coordinate.

Args:
molecule (Molecule): molecule object representing the adsorbate
ads_coord (array): coordinate of adsorbate position
repeat (3-tuple or list): input for making a supercell of slab
prior to placing the adsorbate
reorient (bool): flag on whether to reorient the molecule to
have its z-axis concurrent with miller index