def forward(self, image_tokens, group_tokens):
    group_tokens = self.norm_tokens(group_tokens)
    image_tokens = self.norm_x(image_tokens)
    projected_group_tokens = self.project_group_token(group_tokens)
    projected_group_tokens = self.pre_assign_attn(projected_group_tokens, image_tokens)
    new_image_tokens, attention = self.assign(projected_group_tokens, image_tokens)
    new_image_tokens += projected_group_tokens
    new_image_tokens = new_image_tokens + self.mlp_channels(self.norm_new_x(new_image_tokens))
    return (new_image_tokens, attention)

Args:
image_tokens (`torch.Tensor`): image tokens, of shape [batch_size, input_length, channels]
group_tokens (`torch.Tensor`): group tokens, [batch_size, num_group_tokens, channels]


def __init__(self, mtf_graph, mesh_shape, mtf_outputs=()):

    self.mtf_graph = mtf_graph
    self.mesh_shape = mesh_shape
    self.mtf_outputs = mtf_outputs

    self._layout_validator = None
    self._graph_interface = None

Initializer.

Args:
mtf_graph: a mtf.Graph.
mesh_shape: an mtf.Shape.
mtf_outputs: an optional iterable of mtf.Tensor, representing the outputs
of the computation.


def get_opener(self, protocol):
    protocol = (protocol or self.default_opener)
    if self.load_extern:
        entry_point = next(pkg_resources.iter_entry_points('fs.opener', protocol), None)
    else:
        entry_point = None
    if (entry_point is None):
        if (protocol in self._protocols):
            opener_instance = self._protocols[protocol]
        else:
            raise UnsupportedProtocol("protocol '{}' is not supported".format(protocol))
    else:
        try:
            opener = entry_point.load()
        except Exception as exception:
            raise EntryPointError('could not load entry point; {}'.format(exception))
        if (not issubclass(opener, Opener)):
            raise EntryPointError('entry point did not return an opener')
        try:
            opener_instance = opener()
        except Exception as exception:
            raise EntryPointError('could not instantiate opener; {}'.format(exception))
    return opener_instance

Get the opener class associated to a given protocol.

Arguments:
protocol (str): A filesystem protocol.

Returns:
Opener: an opener instance.

Raises:
~fs.opener.errors.UnsupportedProtocol: If no opener
could be found for the given protocol.
EntryPointLoadingError: If the returned entry point
is not an `Opener` subclass or could not be loaded
successfully.


def callEventGetAllRpc(self, callback_id, event_name):
    return self._event_client.eventGetAll(callback_id, event_name)

Gets all existing events for the specified identifier without waiting.

This function calls snippet lib's eventGetAll RPC.

Args:
callback_id: str, the callback identifier.
event_name: str, the callback name.

Returns:
A list of event dictionaries.


def FanOut(self, obj, parent=None):

    if parent and obj == parent:
      results = [utils.SmartUnicode(obj).strip()]
    elif isinstance(obj, (string_types, rdf_structs.EnumNamedValue)):
      results = [utils.SmartUnicode(obj).strip()]
    elif isinstance(obj, rdf_protodict.DataBlob):
      results = self.FanOut(obj.GetValue())
    elif isinstance(obj, (collections.Mapping, rdf_protodict.Dict)):
      results = []

      for k, v in iteritems(obj):
        expanded_v = [utils.SmartUnicode(r) for r in self.FanOut(v)]
        results.append("%s:%s" % (utils.SmartUnicode(k), ",".join(expanded_v)))
    elif isinstance(obj, (collections.Iterable,
                          rdf_structs.RepeatedFieldHelper)):
      results = []
      for rslt in [self.FanOut(o, obj) for o in obj]:
        results.extend(rslt)
    else:
      results = [utils.SmartUnicode(obj).strip()]
    return results

Expand values from various attribute types.

Strings are returned as is.
Dictionaries are returned with a key string, and an expanded set of values.
Other iterables are expanded until they flatten out.
Other items are returned in string format.

Args:
obj: The object to expand out.
parent: The parent object: Used to short-circuit infinite recursion.

Returns:
a list of expanded values as strings.


def create_context(pip_version=None, python_version=None):
    if pip_version:
        pip_req = ('pip-%s' % str(pip_version))
    else:
        pip_req = 'pip'
    if python_version:
        ver = Version(str(python_version))
        major_minor_ver = ver.trim(2)
        py_req = ('python-%s' % str(major_minor_ver))
    else:
        package = get_latest_package('python')
        if package:
            major_minor_ver = package.version.trim(2)
        else:
            major_minor_ver = '.'.join(map(str, sys.version_info[:2]))
        py_req = ('python-%s' % str(major_minor_ver))
    request = [pip_req, py_req]
    with convert_errors(from_=(PackageFamilyNotFoundError, PackageNotFoundError), to=BuildError, msg='Cannot run - pip or python rez package is not present'):
        context = ResolvedContext(request)
    pip_variant = context.get_resolved_package('pip')
    pip_package = pip_variant.parent
    print_info(('Using %s (%s)' % (pip_package.qualified_name, pip_variant.uri)))
    return context

Create a context containing the specific pip and python.

Args:
pip_version (str or `Version`): Version of pip to use, or latest if None.
python_version (str or `Version`): Python version to use, or latest if
None.

Returns:
`ResolvedContext`: Context containing pip and python.


def dice_loss(inputs: Tensor, labels: Tensor, num_masks: int) -> Tensor:
    probs = inputs.sigmoid().flatten(1)
    numerator = 2 * (probs * labels).sum(-1)
    denominator = probs.sum(-1) + labels.sum(-1)
    loss = 1 - (numerator + 1) / (denominator + 1)
    loss = loss.sum() / num_masks
    return loss

Compute the DICE loss, similar to generalized IOU for masks as follows:

$$ \mathcal{L}_{\text{dice}(x, y) = 1 - \frac{2 * x \cap y }{x \cup y + 1}} $$

In practice, since `labels` is a binary mask, (only 0s and 1s), dice can be computed as follow

$$ \mathcal{L}_{\text{dice}(x, y) = 1 - \frac{2 * x * y }{x + y + 1}} $$

Args:
inputs (`torch.Tensor`):
A tensor representing a mask.
labels (`torch.Tensor`):
A tensor with the same shape as inputs. Stores the binary classification labels for each element in inputs
(0 for the negative class and 1 for the positive class).
num_masks (`int`):
The number of masks present in the current batch, used for normalization.

Returns:
`torch.Tensor`: The computed loss.


def resolves_for(self, session):

        if self.url:
            self.actual_path = session.current_url
        else:
            result = urlparse(session.current_url)

            if self.only_path:
                self.actual_path = result.path
            else:
                request_uri = result.path
                if result.query:
                    request_uri += "?{0}".format(result.query)

                self.actual_path = request_uri

        if isregex(self.expected_path):
            return self.expected_path.search(self.actual_path)
        else:
            return normalize_url(self.actual_path) == normalize_url(self.expected_path)

Returns whether this query resolves for the given session.

Args:
session (Session): The session for which this query should be executed.

Returns:
bool: Whether this query resolves.


def validate(self, cipher_text, max_timedelta=None):

        if isinstance(cipher_text, six.string_types):
            cipher_text.encode()
        cipher_text = base64.urlsafe_b64decode(cipher_text)
        decrypted = self.encryption_suite.decrypt(cipher_text).decode().split(self.separator)
        message_list, dt = decrypted[:-1], decrypted[-1]
        dt = int(''.join(re.findall(r'\d+', dt)))
        now = int(time.time())
        if max_timedelta and max_timedelta < now - dt:
            raise ValueError('Expired')
        return message_list, dt

Will decrypt the url safe base64 encoded crypted str or bytes array.
Args:
cipher_text: the encrypted text
max_timedelta: maximum timedelta in seconds

Returns:
the original message list


def delete_pipeline_stage(self, pipeline_key, stage_key, sort_by = None):

		if not (pipeline_key and stage_key):
			return requests.codes.bad_request, None

		uri = '/'.join([
						self.api_uri,
						self.pipelines_suffix,
						pipeline_key,
						self.stages_suffix,
						stage_key
						])

		code, data = self._req('delete', uri)

		return code, data

Deletes a stage in the pipeline by stage key and pipeline key
Args:
pipeline_key	key for pipeline
stage_key		key for stage
sort_by			in desc order by 'creationTimestamp' or 'lastUpdatedTimestamp'
returns 		(status code for the GET request, dict of op report)


def download_dataset(storage_client, image_batches, target_dir, local_dataset_copy=None):
    for (batch_id, batch_value) in iteritems(image_batches.data):
        batch_dir = os.path.join(target_dir, batch_id)
        os.mkdir(batch_dir)
        for (image_id, image_val) in iteritems(batch_value['images']):
            dst_filename = os.path.join(batch_dir, (image_id + '.png'))
            if local_dataset_copy:
                local_filename = os.path.join(local_dataset_copy, os.path.basename(image_val['image_path']))
                if os.path.exists(local_filename):
                    shutil.copyfile(local_filename, dst_filename)
                    continue
            cloud_path = ((('gs:
            if (not os.path.exists(dst_filename)):
                subprocess.call(['gsutil', 'cp', cloud_path, dst_filename])

Downloads dataset, organize it by batches and rename images.

Args:
storage_client: instance of the CompetitionStorageClient
image_batches: subclass of ImageBatchesBase with data about images
target_dir: target directory, should exist and be empty
local_dataset_copy: directory with local dataset copy, if local copy is
available then images will be takes from there instead of Cloud Storage

Data in the target directory will be organized into subdirectories by batches,
thus path to each image will be "target_dir/BATCH_ID/IMAGE_ID.png"
where BATCH_ID - ID of the batch (key of image_batches.data),
IMAGE_ID - ID of the image (key of image_batches.data[batch_id]['images'])


def raise_not_enough_arguments(self, string):

		requested = errors.number(self.counter + 1)

		number = len(self.positional)

		verb = "was" if number == 1 else "were"

		what = "Requested {} formatting argument for "\
			   "'{}' but only {} {} supplied!"

		what = what.format(requested, string, number, verb)

		raise errors.ArgumentError(what)

Raises an errors.ArgumentError if not enough arguments were supplied.

Takes care of formatting for detailed error messages.

Arguments:
string (str): The string of the phrase for which there weren't enough
arguments.

Raises:
errors.ArgumentError with a detailed error message.


def get_text_config(self, decoder=False) -> 'PretrainedConfig':
    return self.thinker_config.get_text_config()

Returns the config that is meant to be used with text IO. On most models, it is the original config instance
itself. On specific composite models, it is under a set of valid names.

Args:
decoder (`Optional[bool]`, *optional*, defaults to `False`):
If set to `True`, then only search for decoder config names.


def Process(self, parser_mediator, cache=None, database=None, **kwargs):

    if database is None:
      raise ValueError('Invalid database.')

    super(ESEDBPlugin, self).Process(parser_mediator)

    self.GetEntries(
        parser_mediator, cache=cache, database=database, **kwargs)

Determines if this is the appropriate plugin for the database.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
cache (Optional[ESEDBCache]): cache.
database (Optional[pyesedb.file]): ESE database.

Raises:
ValueError: If the database attribute is not valid.


def create(self, value):

        if self._optional and (value is None or len(value) == 0):
            return None

        if hasattr(self._type, 'resource_type'):

            if not isinstance(value, dict):
                raise ValueError("Resources must be specified as a dict of "
                                 "title to parameters")
            if not self._many and len(value) > 1:
                raise ValueError("Only one resource can be provided for this "
                                 "TroposphereType variable")

            result = [
                self._type.from_dict(title, v) for title, v in value.items()
            ]
        else:

            if self._many:
                result = [self._type.from_dict(None, v) for v in value]
            elif not isinstance(value, dict):
                raise ValueError("TroposphereType for a single non-resource"
                                 "type must be specified as a dict of "
                                 "parameters")
            else:
                result = [self._type.from_dict(None, value)]

        if self._validate:
            for v in result:
                v._validate_props()

        return result[0] if not self._many else result

Create the troposphere type from the value.

Args:
value (Union[dict, list]): A dictionary or list of dictionaries
(see class documentation for details) to use as parameters to
create the Troposphere type instance.
Each dictionary will be passed to the `from_dict` method of the
type.

Returns:
Union[list, type]: Returns the value converted to the troposphere
type


def get_supported_connections(self):
    if (self.supported_connections != (- 1)):
        return self.supported_connections
    if (self.api is None):
        return (- 1)
    self._set_state(self._STATE_DONGLE_COMMAND)
    self.api.ble_cmd_system_get_connections()
    self._wait_for_state(self._STATE_DONGLE_COMMAND)
    return self.supported_connections

Returns the number of supported simultaneous BLE connections.

The BLED112 is capable of supporting up to 8 simultaneous BLE connections.
However, the default firmware image has a limit of just 3 devices, which
is a lot easier to run up against. This method retrieves the current value
of this setting.

Returns:
int. The number of supported simultaneous connections, or -1 on error


def __init__(self, structure, norm_strains=None, shear_strains=None,
                 symmetry=False):

        norm_strains = norm_strains or [-0.01, -0.005, 0.005, 0.01]
        shear_strains = shear_strains or [-0.06, -0.03, 0.03, 0.06]

        self.undeformed_structure = structure
        self.deformations = []
        self.def_structs = []

        for ind in [(0, 0), (1, 1), (2, 2)]:
            for amount in norm_strains:
                strain = Strain.from_index_amount(ind, amount)
                self.deformations.append(strain.get_deformation_matrix())

        for ind in [(0, 1), (0, 2), (1, 2)]:
            for amount in shear_strains:
                strain = Strain.from_index_amount(ind, amount)
                self.deformations.append(strain.get_deformation_matrix())

        if symmetry:
            self.sym_dict = symmetry_reduce(self.deformations, structure)
            self.deformations = list(self.sym_dict.keys())
        self.deformed_structures = [defo.apply_to_structure(structure)
                                    for defo in self.deformations]

constructs the deformed geometries of a structure.  Generates
m + n deformed structures according to the supplied parameters.

Args:
structure (Structure): structure to undergo deformation
norm_strains (list of floats): strain values to apply
to each normal mode.
shear_strains (list of floats): strain values to apply
to each shear mode.
symmetry (bool): whether or not to use symmetry reduction.


async def addNodeTag(self, iden, tag, valu=(None, None)):
    buid = s_common.uhex(iden)
    parts = tag.split('.')
    self._reqUserAllowed('tag:add', *parts)
    async with (await self.cell.snap(user=self.user)) as snap:
        with s_provenance.claim('coreapi', meth='tag:add', user=snap.user.iden):
            node = (await snap.getNodeByBuid(buid))
            if (node is None):
                raise s_exc.NoSuchIden(iden=iden)
            (await node.addTag(tag, valu=valu))
            return node.pack()

Add a tag to a node specified by iden.

Args:
iden (str): A hex encoded node BUID.
tag (str):  A tag string.
valu (tuple):  A time interval tuple or (None, None).


def _query_for_individual_audio(self, run, tag, sample, index):
    query_string = urllib.parse.urlencode({'run': run, 'tag': tag, 'sample': sample, 'index': index})
    return query_string

Builds a URL for accessing the specified audio.

This should be kept in sync with _serve_audio_metadata. Note that the URL is
*not* guaranteed to always return the same audio, since audio may be
unloaded from the reservoir as new audio entries come in.

Args:
run: The name of the run.
tag: The tag.
index: The index of the audio entry. Negative values are OK.

Returns:
A string representation of a URL that will load the index-th sampled audio
in the given run with the given tag.


def get_nested_group_users(self, groupname):
    response = self._get((self.rest_url + '/group/user/nested'), params={'groupname': groupname, 'start-index': 0, 'max-results': 99999})
    if (not response.ok):
        return None
    return [u['name'] for u in response.json()['users']]

Retrieves a list of all users that directly or indirectly belong to the given groupname.

Args:
groupname: The group name.

Returns:
list:
A list of strings of user names.


def run(self, horizon: int, initial_state: Optional[StateTensor]=None) -> SimulationOutput:
    trajectory = self.trajectory(horizon, initial_state)
    with tf.Session(graph=self.graph) as sess:
        sess.run(tf.global_variables_initializer())
        non_fluents = sess.run(self._non_fluents)
        (initial_state, states, actions, interms, rewards) = sess.run(trajectory)
    non_fluent_ordering = self._cell._compiler.rddl.domain.non_fluent_ordering
    non_fluents = tuple(zip(non_fluent_ordering, non_fluents))
    state_fluent_ordering = self._cell._compiler.rddl.domain.state_fluent_ordering
    states = tuple(zip(state_fluent_ordering, states))
    interm_fluent_ordering = self._cell._compiler.rddl.domain.interm_fluent_ordering
    interms = tuple(zip(interm_fluent_ordering, interms))
    action_fluent_ordering = self._cell._compiler.rddl.domain.action_fluent_ordering
    actions = tuple(zip(action_fluent_ordering, actions))
    rewards = np.squeeze(rewards)
    outputs = (non_fluents, initial_state, states, actions, interms, rewards)
    return outputs

Builds the MDP graph and simulates in batch the trajectories
with given `horizon`. Returns the non-fluents, states, actions, interms
and rewards. Fluents and non-fluents are returned in factored form.

Note:
All output arrays have shape: (batch_size, horizon, fluent_shape).
Except initial state that has shape: (batch_size, fluent_shape).

Args:
horizon (int): The number of timesteps in the simulation.
initial_state (Optional[Sequence[tf.Tensor]]): The initial state tensors.

Returns:
Tuple[NonFluentsArray, StatesArray, ActionsArray, IntermsArray, np.array]: Simulation ouput tuple.


def __init__(self, input_dim: int, hidden_dim: int, output_dim: int, num_layers: int=3):
    super().__init__()
    in_dims = [input_dim] + [hidden_dim] * (num_layers - 1)
    out_dims = [hidden_dim] * (num_layers - 1) + [output_dim]
    self.layers = []
    for i, (in_dim, out_dim) in enumerate(zip(in_dims, out_dims)):
        activation = nn.ReLU() if i < num_layers - 1 else nn.Identity()
        layer = PredictionBlock(in_dim, out_dim, activation=activation)
        self.layers.append(layer)
        self.add_module(str(i), layer)

A classic Multi Layer Perceptron (MLP).

Args:
input_dim (`int`):
The input dimensions.
hidden_dim (`int`):
The hidden dimensions.
output_dim (`int`):
The output dimensions.
num_layers (int, *optional*, defaults to 3):
The number of layers.


def flash_write(self, addr, data, nbits=None, flags=0):
    self._dll.JLINKARM_BeginDownload(flags)
    self.memory_write(addr, data, nbits=nbits)
    bytes_flashed = self._dll.JLINKARM_EndDownload()
    if (bytes_flashed < 0):
        raise errors.JLinkFlashException(bytes_flashed)
    return bytes_flashed

Writes data to the flash region of a device.

The given number of bits, if provided, must be either ``8``, ``16``, or
``32``.

Args:
self (JLink): the ``JLink`` instance
addr (int): starting flash address to write to
data (list): list of data units to write
nbits (int): number of bits to use for each unit

Returns:
Number of bytes written to flash.


def speech_recognition_bottom(x, model_hparams, vocab_size):

  del vocab_size
  inputs = x
  p = model_hparams

  num_mel_bins = p.audio_num_mel_bins
  num_channels = 3 if p.audio_add_delta_deltas else 1

  with tf.variable_scope("speech_recognition_modality"):
    if p.audio_preproc_in_bottom:

      with tf.variable_scope("fbanks"):
        waveforms = tf.squeeze(inputs, [2, 3])
        mel_fbanks = common_audio.compute_mel_filterbank_features(
            waveforms,
            sample_rate=p.audio_sample_rate,
            dither=p.audio_dither,
            preemphasis=p.audio_preemphasis,
            frame_length=p.audio_frame_length,
            frame_step=p.audio_frame_step,
            lower_edge_hertz=p.audio_lower_edge_hertz,
            upper_edge_hertz=p.audio_upper_edge_hertz,
            num_mel_bins=p.audio_num_mel_bins,
            apply_mask=True)
        if p.audio_add_delta_deltas:
          mel_fbanks = common_audio.add_delta_deltas(mel_fbanks)
        x = tf.reshape(mel_fbanks,
                       common_layers.shape_list(mel_fbanks)[:2] +
                       [num_mel_bins, num_channels])

        nonpadding_mask = 1. - common_attention.embedding_to_padding(x)
        num_of_nonpadding_elements = tf.reduce_sum(
            nonpadding_mask) * num_mel_bins * num_channels

        var_epsilon = 1e-09
        mean = tf.reduce_sum(
            x, axis=[1], keepdims=True) / num_of_nonpadding_elements
        variance = (num_of_nonpadding_elements * mean**2. -
                    2. * mean * tf.reduce_sum(x, axis=[1], keepdims=True) +
                    tf.reduce_sum(x**2, axis=[1], keepdims=True)
                   ) / num_of_nonpadding_elements
        x = (x - mean) * tf.rsqrt(variance + var_epsilon) * tf.expand_dims(
            nonpadding_mask, -1)
    else:
      x = inputs

    x.set_shape([None, None, num_mel_bins, num_channels])

    x = tf.pad(x, [[0, 0], [0, 8], [0, 0], [0, 0]])
    for _ in range(2):
      x = tf.layers.conv2d(
          x, 128, (3, 3), (2, 2), use_bias=False)
      x = common_layers.layer_norm(x)
      x = tf.nn.relu(x)

    xshape = common_layers.shape_list(x)

    x = tf.pad(x, [[0, 0], [0, 2], [0, 0], [0, 0]])
    x = tf.layers.conv2d(x, p.hidden_size, (3, xshape[2]), use_bias=False)

    assert common_layers.shape_list(x)[2] == 1
    x = common_layers.layer_norm(x)
    x = tf.nn.relu(x)
  return x

Use batchnorm instead of CMVN and shorten the stft with strided convs.

Args:
x: float32 tensor with shape [batch_size, len, 1, freqs * channels]
model_hparams: HParams, model hyperparmeters.
vocab_size: int, vocabulary size.

Returns:
float32 tensor with shape [batch_size, shorter_len, 1, hidden_size]


def kill_monitor(self, check_alive=True):
    self._kill_process_type(ray_constants.PROCESS_TYPE_MONITOR, check_alive=check_alive)

Kill the monitor.

Args:
check_alive (bool): Raise an exception if the process was already
dead.


def wav_to_features(sample_rate, clip_duration_ms, window_size_ms, window_stride_ms, feature_bin_count, quantize, preprocess, input_wav, output_c_file):
    sess = tf.compat.v1.InteractiveSession()
    model_settings = models.prepare_model_settings(0, sample_rate, clip_duration_ms, window_size_ms, window_stride_ms, feature_bin_count, preprocess)
    audio_processor = input_data.AudioProcessor(None, None, 0, 0, '', 0, 0, model_settings, None)
    results = audio_processor.get_features_for_wav(input_wav, model_settings, sess)
    features = results[0]
    variable_base = os.path.splitext(os.path.basename(input_wav).lower())[0]
    with gfile.GFile(output_c_file, 'w') as f:
        f.write('\n\n')
        f.write('const int g_%s_width = %d;\n' % (variable_base, model_settings['fingerprint_width']))
        f.write('const int g_%s_height = %d;\n' % (variable_base, model_settings['spectrogram_length']))
        if quantize:
            features_min, features_max = input_data.get_features_range(model_settings)
            f.write('const unsigned char g_%s_data[] = {' % variable_base)
            i = 0
            for value in features.flatten():
                quantized_value = int(round(255 * (value - features_min) / (features_max - features_min)))
                if quantized_value < 0:
                    quantized_value = 0
                if quantized_value > 255:
                    quantized_value = 255
                if i == 0:
                    f.write('\n  ')
                f.write('%d, ' % quantized_value)
                i = (i + 1) % 10
        else:
            f.write('const float g_%s_data[] = {\n' % variable_base)
            i = 0
            for value in features.flatten():
                if i == 0:
                    f.write('\n  ')
                f.write('%f, ' % value)
                i = (i + 1) % 10
        f.write('\n};\n')

Converts an audio file into its corresponding feature map.

Args:
sample_rate: Expected sample rate of the wavs.
clip_duration_ms: Expected duration in milliseconds of the wavs.
window_size_ms: How long each spectrogram timeslice is.
window_stride_ms: How far to move in time between spectrogram timeslices.
feature_bin_count: How many bins to use for the feature fingerprint.
quantize: Whether to train the model for eight-bit deployment.
preprocess: Spectrogram processing mode; "mfcc", "average" or "micro".
input_wav: Path to the audio WAV file to read.
output_c_file: Where to save the generated C source file.


def call(self, hidden_states, 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, encoder_layer_head_mask: tf.Tensor | None=None, past_key_value: Tuple[tf.Tensor] | None=None, training=False) -> Tuple[tf.Tensor, tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]:
    residual = 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
    hidden_states = self.self_attn_layer_norm(hidden_states)
    cross_attn_present_key_value = None
    cross_attn_weights = None
    if encoder_hidden_states is not None:
        residual = 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=encoder_layer_head_mask, past_key_value=cross_attn_past_key_value)
        hidden_states = self.dropout(hidden_states, training=training)
        hidden_states = residual + hidden_states
        hidden_states = self.encoder_attn_layer_norm(hidden_states)
        present_key_value = present_key_value + cross_attn_present_key_value
    residual = 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
    hidden_states = self.final_layer_norm(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
*(config.encoder_attention_heads,)*.
encoder_layer_head_mask (`tf.Tensor`): mask for encoder attention heads in a given layer of
size *(config.encoder_attention_heads,)*.
past_key_value (`Tuple(tf.Tensor)`): cached past key and value projection states


def empirical_statistics(observed_time_series):
    with tf.compat.v1.name_scope('empirical_statistics', values=[observed_time_series]):
        [observed_time_series, mask] = canonicalize_observed_time_series_with_mask(observed_time_series)
        squeezed_series = observed_time_series[(..., 0)]
        if (mask is None):
            (observed_mean, observed_variance) = tf.nn.moments(x=squeezed_series, axes=(- 1))
            observed_initial = squeezed_series[(..., 0)]
        else:
            broadcast_mask = tf.broadcast_to(tf.cast(mask, tf.bool), tf.shape(input=squeezed_series))
            (observed_mean, observed_variance) = missing_values_util.moments_of_masked_time_series(squeezed_series, broadcast_mask=broadcast_mask)
            try:
                observed_initial = missing_values_util.initial_value_of_masked_time_series(squeezed_series, broadcast_mask=broadcast_mask)
            except NotImplementedError:
                tf.compat.v1.logging.warn('Cannot compute initial values for a masked time serieswith dynamic shape; using the mean instead. This willaffect heuristic priors and may change the results ofinference.')
                observed_initial = observed_mean
        observed_stddev = tf.sqrt(observed_variance)
        observed_initial_centered = (observed_initial - observed_mean)
        return (observed_mean, observed_stddev, observed_initial_centered)

Compute statistics of a provided time series, as heuristic initialization.

Args:
observed_time_series: `Tensor` representing a time series, or batch of time
series, of shape either `batch_shape + [num_timesteps, 1]` or
`batch_shape + [num_timesteps]` (allowed if `num_timesteps > 1`).

Returns:
observed_mean: `Tensor` of shape `batch_shape`, giving the empirical
mean of each time series in the batch.
observed_stddev: `Tensor` of shape `batch_shape`, giving the empirical
standard deviation of each time series in the batch.
observed_initial_centered: `Tensor of shape `batch_shape`, giving the
initial value of each time series in the batch after centering
(subtracting the mean).


def getsize(self, path=None, client_kwargs=None, header=None):

        return self._getsize_from_header(self.head(path, client_kwargs, header))

Return the size, in bytes, of path.

Args:
path (str): File path or URL.
client_kwargs (dict): Client arguments.
header (dict): Object header.

Returns:
int: Size in bytes.


def compute_output_shape(self, input_shape):
    if context.executing_eagerly():
        self._maybe_build(input_shape)
        with ops.get_default_graph().as_default():
            graph = func_graph.FuncGraph('graph')
            with graph.as_default():
                input_shape = tf_utils.convert_shapes(input_shape, to_tuples=False)
                inputs = nest.map_structure(base_layer_utils.generate_placeholders_from_shape, input_shape)
                try:
                    outputs = self(inputs, training=False)
                except TypeError as e:
                    raise NotImplementedError("We could not automatically infer the static shape of the layer's output. Please implement the `compute_output_shape` method on your layer (%s)." % self.__class__.__name__) from e
        return nest.map_structure(lambda t: t.shape, outputs)
    raise NotImplementedError

Computes the output shape of the layer.

If the layer has not been built, this method will call `build` on the
layer. This assumes that the layer will later be used with inputs that
match the input shape provided here.

Args:
input_shape: Shape tuple (tuple of integers)
or list of shape tuples (one per output tensor of the layer).
Shape tuples can include None for free dimensions,
instead of an integer.

Returns:
An input shape tuple.


def subcommand(self, *args):

        return Subcommand(*args, directory=self.directory, env_vars=self.env_vars)

Get subcommand acting on a service. Subcommand will run in service directory
and with the environment variables used to run the service itself.

Args:
*args: Arguments to run command (e.g. "redis-cli", "-n", "1")

Returns:
Subcommand object.


def calc_sha(self, checksum):
    with LogTask('Calculating {}'.format(checksum)):
        with open((self.dst + '.hash'), 'wt') as f:
            sha = utils.get_hash(self.dst, checksum)
            f.write(sha)
        self.exported_metadata[checksum] = sha

Calculate the checksum of the new exported disk, write it to
a file, and update this managers 'exported_metadata'.

Args:
checksum(str): The type of the checksum


def holiday_day(self, value=None):

        if value is not None:
            try:
                value = str(value)
            except ValueError:
                raise ValueError('value {} need to be of type str '
                                 'for field `holiday_day`'.format(value))
            if ',' in value:
                raise ValueError('value should not contain a comma '
                                 'for field `holiday_day`')

        self._holiday_day = value

Corresponds to IDD Field `holiday_day`

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

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


def SetParseFns(*positional, **named):

    def _Decorator(fn):
        parse_fns = GetParseFns(fn)
        parse_fns['positional'] = positional
        parse_fns['named'].update(named)
        _SetMetadata(fn, FIRE_PARSE_FNS, parse_fns)
        return fn
    return _Decorator

Set the fns for Fire to use to parse args when calling the decorated fn.

Returns a decorator, which when applied to a function adds metadata to the
function telling Fire how to turn string command line arguments into proper
Python arguments with which to call the function.

A parse function should accept a single string argument and return a value to
be used in its place when calling the decorated function.

Args:
*positional: The functions to be used for parsing positional arguments.
**named: The functions to be used for parsing named arguments.
Returns:
The decorated function, which now has metadata telling Fire how to perform.


def publish_metric(self, metric_name, metric_value, epoch_seconds=None):

        if epoch_seconds is None:
            epoch_seconds = self._reactor.seconds()
        self._client_factory.publish_metric(metric_name, metric_value, int(epoch_seconds))

Record a single hit on a given metric.

Args:
metric_name: The name of the metric to record with Carbon.
metric_value: The value to record with Carbon.
epoch_seconds: Optionally specify the time for the metric hit.

Returns:
None


def to(self, command, **args):

        self.command = command
        self.args = args
        return self

Bind the keys to the specified *command* with some *args*.

Arguments:
command (str): Name of the ST command (e.g. ``insert_snippet``).
**args: Arguments for the command.
Returns:
Binding: self


def shutdown_abort():
    try:
        win32api.AbortSystemShutdown('127.0.0.1')
        return True
    except pywintypes.error as exc:
        (number, context, message) = exc.args
        log.error('Failed to abort system shutdown')
        log.error('nbr: %s', number)
        log.error('ctx: %s', context)
        log.error('msg: %s', message)
        return False

Abort a shutdown. Only available while the dialog box is being
displayed to the user. Once the shutdown has initiated, it cannot be
aborted.

Returns:
bool: ``True`` if successful, otherwise ``False``

CLI Example:

.. code-block:: bash

salt 'minion-id' system.shutdown_abort


def remove_plugin(self, name, force=False):

        url = self._url('/plugins/{0}', name)
        res = self._delete(url, params={'force': force})
        self._raise_for_status(res)
        return True

Remove an installed plugin.

Args:
name (string): Name of the plugin to remove. The ``:latest``
tag is optional, and is the default if omitted.
force (bool): Disable the plugin before removing. This may
result in issues if the plugin is in use by a container.

Returns:
``True`` if successful


def registration_packet(self):
    return (self.hw_type, self.api_info[0], self.api_info[1], self.name, self.fw_info[0], self.fw_info[1], self.fw_info[2], self.exec_info[0], self.exec_info[0], self.exec_info[0], self.slot, self.unique_id)

Serialize this into a tuple suitable for returning from an RPC.

Returns:
tuple: The serialized values.


def StreamMemory(self, process, offset=0, amount=None):

    reader = MemoryReader(process, offset=offset)
    return self.Stream(reader, amount=amount)

Streams chunks of memory of a given process starting at given offset.

Args:
process: A platform-specific `Process` instance.
offset: An integer offset at which the memory stream should start on.
amount: An upper bound on number of bytes to read.

Returns:
Generator over `Chunk` instances.


def __init__(self, conf):
    super(GcsFilesSource, self).__init__(conf)
    self._SetDefaults(conf)
    self._gcs_client = None

Initialize the GcsFilesSource object.

Args:
conf: A dictionary of key/value pairs.

Raises:
RuntimeError: object wasn't initialized with a dict.


def _create_partition_config(self, option: t.Tuple) -> Config:
    copy = cp.deepcopy(self.config.selection)
    out = cp.deepcopy(self.config)
    for idx, key in enumerate(self.config.partition_keys):
        copy[key] = [option[idx]]
    if 'hdate' in copy:
        copy['hdate'] = [generate_hdate(copy['date'][0], v) for v in copy['hdate']]
    out.selection = copy
    return out

Create a config for a single partition option.

Output a config dictionary, overriding the range of values for
each key with the partition instance in 'selection'.
Continuing the example from prepare_partitions, the selection section
would be:
{ 'foo': ..., 'year': ['2020'], 'month': ['01'], ... }
{ 'foo': ..., 'year': ['2020'], 'month': ['02'], ... }
{ 'foo': ..., 'year': ['2020'], 'month': ['03'], ... }

Args:
option: A single item in the range of partition_keys.
config: The download config, including the parameters and selection sections.

Returns:
A configuration with that selects a single download partition.


def GetScripts(self, dest_dir):
    metadata_dict = (self.watcher.GetMetadata() or {})
    try:
        instance_data = metadata_dict['instance']['attributes']
    except KeyError:
        instance_data = None
        self.logger.warning('Instance attributes were not found.')
    try:
        project_data = metadata_dict['project']['attributes']
    except KeyError:
        project_data = None
        self.logger.warning('Project attributes were not found.')
    return (self._GetAttributeScripts(instance_data, dest_dir) or self._GetAttributeScripts(project_data, dest_dir))

Retrieve the scripts to execute.

Args:
dest_dir: string, the path to a directory for storing metadata scripts.

Returns:
dict, a dictionary mapping set metadata keys with associated scripts.


def create_toolbutton(entries, parent=None):
    btn = QtGui.QToolButton(parent)
    menu = QtGui.QMenu()
    actions = []
    for (label, slot) in entries:
        action = add_menu_action(menu, label, slot)
        actions.append(action)
    btn.setPopupMode(QtGui.QToolButton.MenuButtonPopup)
    btn.setDefaultAction(actions[0])
    btn.setMenu(menu)
    return (btn, actions)

Create a toolbutton.

Args:
entries: List of (label, slot) tuples.

Returns:
`QtGui.QToolBar`.


def _expand_sequence(self, seq: List[GridQubit]) -> List[GridQubit]:

        i = 1
        while i < len(seq):
            path = self._find_path_between(seq[i - 1], seq[i], set(seq))
            if path:
                seq = seq[:i] + path + seq[i:]
            else:
                i += 1
        return seq

Tries to expand given sequence with more qubits.

Args:
seq: Linear sequence of qubits.

Returns:
New continuous linear sequence which contains all the qubits from
seq and possibly new qubits inserted in between.


def create(self, ospf_process_id, vrf=None):

        value = int(ospf_process_id)
        if not 0 < value < 65536:
            raise ValueError('ospf as must be between 1 and 65535')
        command = 'router ospf {}'.format(ospf_process_id)
        if vrf:
            command += ' vrf %s' % vrf
        return self.configure(command)

Creates a OSPF process in the specified VRF or the default VRF.

Args:
ospf_process_id (str): The OSPF process Id value
vrf (str): The VRF to apply this OSPF process to
Returns:
bool: True if the command completed successfully
Exception:
ValueError: If the ospf_process_id passed in less
than 0 or greater than 65536


def squareplus(x, b=4):
    if any_symbolic_tensors((x,)):
        return Squareplus(b).symbolic_call(x)
    return backend.nn.squareplus(x, b)

Squareplus activation function.

The Squareplus activation function is defined as:

`f(x) = (x + sqrt(x^2 + b)) / 2`

Args:
x: Input tensor.
b: Smoothness parameter. Defaults to 4.

Returns:
A tensor with the same shape as `x`.

Example:

>>> x = np.array([-1.0, 0.0, 1.0])
>>> x_squareplus = keras.ops.squareplus(x)
>>> print(x_squareplus)
array([0.6180, 1.0000, 1.6180], dtype=float32)


def build_defaults(self, defaults_node):
    proxy_name = defaults_node.defaults_header.proxy_name.text
    config_block_lines = self.__build_config_block(defaults_node.config_block)
    return config.Defaults(name=proxy_name, config_block=config_block_lines)

parse `defaults` sections, and return a config.Defaults

Args:
defaults_node (TreeNode): Description

Returns:
config.Defaults: an object


def randn(*args):
    return standard_normal(size=args)

Returns samples from a normal distribution.

Uses `tf.random_normal`.

Args:
*args: The shape of the output array.

Returns:
An ndarray with shape `args` and dtype `float64`.


def matches(self, other, **kwargs):
    from pymatgen.analysis.structure_matcher import StructureMatcher
    m = StructureMatcher(**kwargs)
    return m.fit(Structure.from_sites(self), Structure.from_sites(other))

Check whether this structure is similar to another structure.
Basically a convenience method to call structure matching fitting.

Args:
other (IStructure/Structure): Another structure.
**kwargs: Same **kwargs as in
:class:`pymatgen.analysis.structure_matcher.StructureMatcher`.

Returns:
(bool) True is the structures are similar under some affine
transformation.


def SaveGDAL(filename, rda):
    if (type(rda) is not rdarray):
        raise Exception('A richdem.rdarray or numpy.ndarray is required!')
    if (not GDAL_AVAILABLE):
        raise Exception('richdem.SaveGDAL() requires GDAL.')
    driver = gdal.GetDriverByName('GTiff')
    data_type = gdal.GDT_Float32
    data_set = driver.Create(filename, xsize=rda.shape[1], ysize=rda.shape[0], bands=1, eType=data_type)
    data_set.SetGeoTransform(rda.geotransform)
    data_set.SetProjection(rda.projection)
    band = data_set.GetRasterBand(1)
    band.SetNoDataValue(rda.no_data)
    band.WriteArray(np.array(rda))
    for (k, v) in rda.metadata.items():
        data_set.SetMetadataItem(str(k), str(v))

Save a GDAL file.

Saves a RichDEM array to a data file in GeoTIFF format.

If you need to do something more complicated, look at the source of this
function.

Args:
filename (str):     Name of the raster file to be created
rda      (rdarray): Data to save.

Returns:
No Return


def Parse(self, raw_data):

    self.results = raw_data
    for f in self.filters:
      self.results = f.Parse(self.results)
    return self.results

Take the results and yield results that passed through the filters.

The output of each filter is used as the input for successive filters.

Args:
raw_data: An iterable series of rdf values.

Returns:
A list of rdf values that matched all filters.


def cmd_rollback_to(context):

  version = get_version_by_value(context, context.rollback_to)
  context.value = version.value
  context.commit_hash = version.commit_hash
  context.build_number = version.build_number
  context.location = version.location
  context.stable = True
  cmd_set(context)

Roll back by finding a specific version in the history of the service and
putting it as the new current version.
Args:
context: a populated EFVersionContext object


class SQuADHead(nn.Module):

    def __init__(self, config):
        super().__init__()
        self.start_n_top = config.start_n_top
        self.end_n_top = config.end_n_top
        self.start_logits = PoolerStartLogits(config)
        self.end_logits = PoolerEndLogits(config)
        self.answer_class = PoolerAnswerClass(config)
        logger.warning_once('[DEPRECATION WARNING] `SQuADHead` is deprecated and will be removed in v4.53. Please use model-specific class, e.g. `XLMSQuADHead`.')

    @replace_return_docstrings(output_type=SquadHeadOutput, config_class=PretrainedConfig)
    def forward(self, hidden_states: torch.FloatTensor, start_positions: Optional[torch.LongTensor]=None, end_positions: Optional[torch.LongTensor]=None, cls_index: Optional[torch.LongTensor]=None, is_impossible: Optional[torch.LongTensor]=None, p_mask: Optional[torch.FloatTensor]=None, return_dict: bool=False) -> Union[SquadHeadOutput, Tuple[torch.FloatTensor]]:

        start_logits = self.start_logits(hidden_states, p_mask=p_mask)
        if start_positions is not None and end_positions is not None:
            for x in (start_positions, end_positions, cls_index, is_impossible):
                if x is not None and x.dim() > 1:
                    x.squeeze_(-1)
            end_logits = self.end_logits(hidden_states, start_positions=start_positions, p_mask=p_mask)
            loss_fct = CrossEntropyLoss()
            start_loss = loss_fct(start_logits, start_positions)
            end_loss = loss_fct(end_logits, end_positions)
            total_loss = (start_loss + end_loss) / 2
            if cls_index is not None and is_impossible is not None:
                cls_logits = self.answer_class(hidden_states, start_positions=start_positions, cls_index=cls_index)
                loss_fct_cls = nn.BCEWithLogitsLoss()
                cls_loss = loss_fct_cls(cls_logits, is_impossible)
                total_loss += cls_loss * 0.5
            return SquadHeadOutput(loss=total_loss) if return_dict else (total_loss,)
        else:
            bsz, slen, hsz = hidden_states.size()
            start_log_probs = nn.functional.softmax(start_logits, dim=-1)
            start_top_log_probs, start_top_index = torch.topk(start_log_probs, self.start_n_top, dim=-1)
            start_top_index_exp = start_top_index.unsqueeze(-1).expand(-1, -1, hsz)
            start_states = torch.gather(hidden_states, -2, start_top_index_exp)
            start_states = start_states.unsqueeze(1).expand(-1, slen, -1, -1)
            hidden_states_expanded = hidden_states.unsqueeze(2).expand_as(start_states)
            p_mask = p_mask.unsqueeze(-1) if p_mask is not None else None
            end_logits = self.end_logits(hidden_states_expanded, start_states=start_states, p_mask=p_mask)
            end_log_probs = nn.functional.softmax(end_logits, dim=1)
            end_top_log_probs, end_top_index = torch.topk(end_log_probs, self.end_n_top, dim=1)
            end_top_log_probs = end_top_log_probs.view(-1, self.start_n_top * self.end_n_top)
            end_top_index = end_top_index.view(-1, self.start_n_top * self.end_n_top)
            start_states = torch.einsum('blh,bl->bh', hidden_states, start_log_probs)
            cls_logits = self.answer_class(hidden_states, start_states=start_states, cls_index=cls_index)
            if not return_dict:
                return (start_top_log_probs, start_top_index, end_top_log_probs, end_top_index, cls_logits)
            else:
                return SquadHeadOutput(start_top_log_probs=start_top_log_probs, start_top_index=start_top_index, end_top_log_probs=end_top_log_probs, end_top_index=end_top_index, cls_logits=cls_logits)

A SQuAD head inspired by XLNet.

Args:
config ([`PretrainedConfig`]):
The config used by the model, will be used to grab the `hidden_size` of the model and the `layer_norm_eps`
to use.


def instantiate_device(virtual_dev, config, loop):
    conf = {}
    if ('device' in config):
        conf = config['device']
    try:
        reg = ComponentRegistry()
        if virtual_dev.endswith('.py'):
            (_name, dev) = reg.load_extension(virtual_dev, class_filter=VirtualIOTileDevice, unique=True)
        else:
            (_name, dev) = reg.load_extensions('iotile.virtual_device', name_filter=virtual_dev, class_filter=VirtualIOTileDevice, product_name='virtual_device', unique=True)
        return dev(conf)
    except ArgumentError as err:
        print(('ERROR: Could not load virtual device (%s): %s' % (virtual_dev, err.msg)))
        sys.exit(1)

Find a virtual device by name and instantiate it

Args:
virtual_dev (string): The name of the pkg_resources entry point corresponding to
the device.  It should be in group iotile.virtual_device.  If virtual_dev ends
in .py, it is interpreted as a python script and loaded directly from the script.
config (dict): A dictionary with a 'device' key with the config info for configuring
this virtual device.  This is optional.

Returns:
VirtualIOTileDevice: The instantiated subclass of VirtualIOTileDevice


def assert_variables_initialized(var_list=None):
    if var_list is None:
        var_list = global_variables() + local_variables()
    if not var_list:
        var_list = []
        for op in ops.get_default_graph().get_operations():
            if op.type in ['Variable', 'VariableV2', 'AutoReloadVariable']:
                var_list.append(op.outputs[0])
    if not var_list:
        return None
    else:
        ranks = []
        for var in var_list:
            with ops.colocate_with(var.op):
                ranks.append(array_ops.rank_internal(var, optimize=False))
        if len(ranks) == 1:
            return ranks[0]
        else:
            return array_ops_stack.stack(ranks)

Returns an Op to check if variables are initialized.

NOTE: This function is obsolete and will be removed in 6 months.  Please
change your implementation to use `report_uninitialized_variables()`.

When run, the returned Op will raise the exception `FailedPreconditionError`
if any of the variables has not yet been initialized.

Note: This function is implemented by trying to fetch the values of the
variables. If one of the variables is not initialized a message may be
logged by the C++ runtime. This is expected.

Args:
var_list: List of `Variable` objects to check. Defaults to the value of
`global_variables().`

Returns:
An Op, or None if there are no variables.


def __params_order_descriptor(self, message_type, path, is_params_class=False):
    path_params = []
    query_params = []
    path_parameter_dict = self.__get_path_parameters(path)
    for field in sorted(message_type.all_fields(), key=(lambda f: f.number)):
        matched_path_parameters = path_parameter_dict.get(field.name, [])
        if (not isinstance(field, messages.MessageField)):
            name = field.name
            if (name in matched_path_parameters):
                path_params.append(name)
            elif (is_params_class and field.required):
                query_params.append(name)
        else:
            for subfield_list in self.__field_to_subfields(field):
                name = '.'.join((subfield.name for subfield in subfield_list))
                if (name in matched_path_parameters):
                    path_params.append(name)
                elif (is_params_class and field.required):
                    query_params.append(name)
    return (path_params + sorted(query_params))

Describe the order of path parameters.

Args:
message_type: messages.Message class, Message with parameters to describe.
path: string, HTTP path to method.
is_params_class: boolean, Whether the message represents URL parameters.

Returns:
Descriptor list for the parameter order.


def _match_section(section, match):
    for i in range(5):
        if (i >= len(section)):
            return None
        if (isinstance(section[i], StringType) and (section[i] in match)):
            return match[section[i]]
    return None

checks whether the `type` of section is in the `match` dictionary

Works around the unknown ordering of s-expressions in each section.
For instance, the `type` is the 3-rd one in for CellBodies
("CellBody"
(Color Yellow)
(CellBody)
(Set "cell10")
)

Returns:
value associated with match[section_type], None if no match


def json_merge_field(field_to, field_from):
    if isinstance(field_from, dict) and 'field' in field_from:
        field_to['field']['name'] = field_from['field']['name']
        field_to['field']['description'] = field_from['field'].get('description', '')
        if 'default' in field_from['field']:
            field_to['field']['default'] = field_from['field']['default']
        if 'prefix' in field_from['field'] or 'prefix' in field_to['field']:
            field_to['field']['prefix'] = field_to['field'].get('prefix', '') + field_from['field'].get('prefix', '')
        if 'suffix' in field_from['field'] or 'suffix' in field_to['field']:
            field_to['field']['suffix'] = field_from['field'].get('suffix', '') + field_to['field'].get('suffix', '')
    else:
        field_to = get_field_value(field_to, field_from)
    return field_to

When a field value is replaced by another field via include.

Replaces the included field with the new field value.
The value can be another field or an actual value.

Args:
field_to - a { 'field':{...}} dictionary.
field_from - a { 'field':{...}} dictionary.

Returns:
A field or value containg the new data.


def set_shutdown(self, default=False, disable=True):

        return self._configure_mlag('shutdown', True, default, disable)

Configures the mlag shutdown value

Default setting for set_shutdown is disable=True, meaning
'no shutdown'. Setting both default and disable to False will
effectively enable shutdown.

Args:
default (bool): Configures the shutdown using the
default keyword
disable (bool): Negates shutdown using the no keyword

Returns:
bool: Returns True if the commands complete successfully


def _utcnow():
    return datetime.datetime.utcnow()

A wrapper function around datetime.datetime.utcnow.

This function is created for unit testing purpose. It's not easy to do
StubOutWithMock with datetime.datetime package.

Returns:
datetime.datetime


def url(request, json_list, nested, url_name='show_{}', ignore_get=None):

    if not ignore_get:
        ignore_get = []
    if isinstance(url_name, str):
        url_string = str(url_name)
        url_name = lambda x: url_string.format(x)
    urls = cache.get('proso_urls')
    if urls is None:
        urls = {}
    else:
        urls = json_lib.loads(urls)
    cache_updated = False
    pass_string = pass_get_parameters_string(request, ignore_get)
    for json in json_list:
        if 'object_type' not in json or 'id' not in json:
            continue
        key = 'show_%s_%s' % (json['object_type'], json['id'])
        if key in urls:
            json['url'] = urls[key]
        else:
            cache_updated = True
            json['url'] = reverse(url_name(json['object_type']), kwargs={'id': json['id']})
            urls[key] = json['url']
        json['url'] = append_get_parameters(json['url'], pass_string)
    if cache_updated:
        cache.set('proso_urls', json_lib.dumps(urls), CACHE_EXPIRATION)

Enrich the given list of objects, so they have URL.

Args:
request (django.http.request.HttpRequest): request which is currently processed
json_list (list): list of dicts (JSON objects to be enriched)
url_name (str|fun): pattern to create a url name taking object_type
ignore_get (list): list of GET parameters which are ignored when the URL is generated

Returns:
list: list of dicts (enriched JSON objects)


def is_commit_id_equal(self, dest, name):

        if not name:

            return False

        return self.get_revision(dest) == name

Return whether the current commit hash equals the given name.

Args:
dest: the repository directory.
name: a string name.


def git_branch_rename(new_name):

    curr_name = git.current_branch(refresh=True).name

    if curr_name not in git.protected_branches():
        log.info("Renaming branch from <33>{}<32> to <33>{}".format(
            curr_name, new_name
        ))
        shell.run('git branch -m {}'.format(new_name))

Rename the current branch

Args:
new_name (str):
New name for the current branch.


def get_pubmed_for_beleditor(pmid: str) -> Mapping[str, Any]:

    pubmed = get_pubmed(pmid)
    pubtator = get_pubtator(pmid)
    pubmed["annotations"] = copy.deepcopy(pubtator["annotations"])

    pubmed = enhance_pubmed_annotations(pubmed)

    return pubmed

Get fully annotated pubmed doc with Pubtator and full entity/annotation_types

Args:
pmid: Pubmed PMID

Returns:
Mapping[str, Any]: pubmed dictionary


def _download_file_from_drive(filename, url):

    confirm_token = None

    confirm_token = None
    session = requests.Session()
    response = session.get(url, stream=True)
    for k, v in response.cookies.items():
        if k.startswith("download_warning"):
            confirm_token = v

    if confirm_token:
        url = url + "&confirm=" + confirm_token

    logger.info("Downloading %s to %s" % (url, filename))

    response = session.get(url, stream=True)

    chunk_size = 16 * 1024
    with open(filename, "wb") as f:
        for chunk in response.iter_content(chunk_size):
            if chunk:
                f.write(chunk)

    statinfo = os.stat(filename)
    logger.info("Successfully downloaded %s, %s bytes." % (filename, statinfo.st_size))

Download filename from google drive unless it's already in directory.

Args:
filename (str): Name of the file to download to (do nothing if it already exists).
url (str): URL to download from.


def _get_fields(ast):

    if not ast.selection_set:

        return [], []

    property_fields = []
    vertex_fields = []
    seen_field_names = set()
    switched_to_vertices = False
    for field_ast in ast.selection_set.selections:
        if not isinstance(field_ast, Field):

            continue

        name = get_ast_field_name(field_ast)
        if name in seen_field_names:

            raise GraphQLCompilationError(u'Encountered repeated field name: {}'.format(name))
        seen_field_names.add(name)

        if is_vertex_field_name(name):
            switched_to_vertices = True
            vertex_fields.append(field_ast)
        else:
            if switched_to_vertices:
                raise GraphQLCompilationError(u'Encountered property field {} '
                                              u'after vertex fields!'.format(name))
            property_fields.append(field_ast)

    return vertex_fields, property_fields

Return a list of vertex fields, and a list of property fields, for the given AST node.

Also verifies that all property fields for the AST node appear before all vertex fields,
raising GraphQLCompilationError if that is not the case.

Args:
ast: GraphQL AST node, obtained from the graphql library

Returns:
tuple of two lists
- the first list contains ASTs for vertex fields
- the second list contains ASTs for property fields


def expand_dims(self, axis):

        if axis == -1:
            axis = self.ndim
        if axis <= self._distaxis:
            subaxis = axis
            new_distaxis = self._distaxis + 1
        else:
            subaxis = axis - 1
            new_distaxis = self._distaxis
        new_subarrays = [expand_dims(ra, subaxis) for ra in self._subarrays]
        return DistArray(new_subarrays, new_distaxis)

Insert a new axis, at a given position in the array shape
Args:
axis (int): Position (amongst axes) where new axis is to be inserted.


def _ScanNode(self, scan_context, scan_node, auto_recurse=True):

    if not scan_context:
      raise ValueError('Invalid scan context.')

    if not scan_node:
      raise ValueError('Invalid scan node.')

    scan_path_spec = scan_node.path_spec

    system_level_file_entry = None
    if scan_node.IsSystemLevel():
      system_level_file_entry = resolver.Resolver.OpenFileEntry(
          scan_node.path_spec, resolver_context=self._resolver_context)

      if system_level_file_entry is None:
        raise errors.BackEndError('Unable to open file entry.')

      if system_level_file_entry.IsDirectory():
        scan_context.SetSourceType(definitions.SOURCE_TYPE_DIRECTORY)
        return

      source_path_spec = self.ScanForStorageMediaImage(scan_node.path_spec)
      if source_path_spec:
        scan_node.scanned = True
        scan_node = scan_context.AddScanNode(source_path_spec, scan_node)

        if system_level_file_entry.IsDevice():
          source_type = definitions.SOURCE_TYPE_STORAGE_MEDIA_DEVICE
        else:
          source_type = definitions.SOURCE_TYPE_STORAGE_MEDIA_IMAGE

        scan_context.SetSourceType(source_type)

        if not auto_recurse:
          return

    source_path_spec = None
    while True:
      if scan_node.IsFileSystem():

        break

      if scan_node.SupportsEncryption():
        self._ScanEncryptedVolumeNode(scan_context, scan_node)

      if scan_context.IsLockedScanNode(scan_node.path_spec):

        break

      source_path_spec = self.ScanForVolumeSystem(scan_node.path_spec)
      if not source_path_spec:

        break

      if not scan_context.HasScanNode(source_path_spec):
        scan_node.scanned = True
        scan_node = scan_context.AddScanNode(source_path_spec, scan_node)

        if system_level_file_entry and system_level_file_entry.IsDevice():
          source_type = definitions.SOURCE_TYPE_STORAGE_MEDIA_DEVICE
        else:
          source_type = definitions.SOURCE_TYPE_STORAGE_MEDIA_IMAGE

        scan_context.SetSourceType(source_type)

      if scan_node.IsVolumeSystemRoot():
        self._ScanVolumeSystemRootNode(
            scan_context, scan_node, auto_recurse=auto_recurse)

        return

      if not auto_recurse and scan_context.updated:
        return

      if not scan_context.updated:
        break

    if scan_node.IsVolumeSystemRoot():
      pass

    elif scan_context.IsLockedScanNode(scan_node.path_spec):

      pass

    elif (scan_node.type_indicator == definitions.TYPE_INDICATOR_VSHADOW and
          auto_recurse and scan_node.path_spec != scan_path_spec):

      pass

    elif not scan_node.IsFileSystem():
      source_path_spec = self.ScanForFileSystem(scan_node.path_spec)
      if not source_path_spec:

        if scan_node.path_spec.type_indicator == definitions.TYPE_INDICATOR_RAW:
          scan_node = scan_context.RemoveScanNode(scan_node.path_spec)

          scan_context.source_type = definitions.SOURCE_TYPE_FILE
        else:
          scan_context.SetSourceType(definitions.SOURCE_TYPE_FILE)

      elif not scan_context.HasScanNode(source_path_spec):
        scan_node.scanned = True
        scan_node = scan_context.AddScanNode(source_path_spec, scan_node)

        if system_level_file_entry and system_level_file_entry.IsDevice():
          source_type = definitions.SOURCE_TYPE_STORAGE_MEDIA_DEVICE
        else:
          source_type = definitions.SOURCE_TYPE_STORAGE_MEDIA_IMAGE

        scan_context.SetSourceType(source_type)

    if not scan_node.scanned:
      scan_node.scanned = True

Scans a node for supported formats.

Args:
scan_context (SourceScannerContext): source scanner context.
scan_node (SourceScanNode): source scan node.
auto_recurse (Optional[bool]): True if the scan should automatically
recurse as far as possible.

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


def _get_db_fields(self, obj):

        for field in obj.indexes:
            yield field, self._zeo_key(field)

Return list of database dictionaries, which are used as indexes for
each attributes.

Args:
cached (bool, default True): Use cached connection to database.

Returns:
list: List of OOBTree's for each item in :attr:`.COMMON_FIELDS`.


def run_bq_pipeline(argv=None):
    parser = argparse.ArgumentParser()
    parser.add_argument('--query', required=True, help='Query to process for the table.')
    parser.add_argument('--output', required=True, help='Output BQ table to write results to.')
    parser.add_argument('--output_schema', dest='output_schema', required=True, help='Schema for output BQ table.')
    parser.add_argument('--use_standard_sql', action='store_true', dest='use_standard_sql', help='Output BQ table to write results to.')
    parser.add_argument('--kms_key', default=None, help='Use this Cloud KMS key with BigQuery.')
    parser.add_argument('--native', default=False, action='store_true', help='Use NativeSources and Sinks.')
    parser.add_argument('--use_json_exports', default=False, action='store_true', help='Use JSON as the file format for exports.')
    known_args, pipeline_args = parser.parse_known_args(argv)
    table_schema = parse_table_schema_from_json(known_args.output_schema)
    kms_key = known_args.kms_key
    options = PipelineOptions(pipeline_args)
    p = TestPipeline(options=options)
    if known_args.native:
        data = p | 'read' >> beam.io.Read(beam.io.BigQuerySource(query=known_args.query, use_standard_sql=known_args.use_standard_sql, kms_key=kms_key))
    else:
        data = p | 'read' >> beam.io.gcp.bigquery.ReadFromBigQuery(query=known_args.query, project=options.view_as(GoogleCloudOptions).project, use_standard_sql=known_args.use_standard_sql, use_json_exports=known_args.use_json_exports, kms_key=kms_key)
    temp_file_format = 'NEWLINE_DELIMITED_JSON' if known_args.use_json_exports else 'AVRO'
    _ = data | 'write' >> beam.io.WriteToBigQuery(known_args.output, schema=table_schema, create_disposition=beam.io.BigQueryDisposition.CREATE_IF_NEEDED, write_disposition=beam.io.BigQueryDisposition.WRITE_EMPTY, temp_file_format=temp_file_format, kms_key=kms_key)
    result = p.run()
    result.wait_until_finish()

Run the sample BigQuery pipeline.

Args:
argv: Arguments to the run function.


def set_ntp_servers(primary_server=None, secondary_server=None, deploy=False):
    ret = {}
    if primary_server:
        query = {'type': 'config', 'action': 'set', 'xpath': "/config/devices/entry[@name='localhost.localdomain']/deviceconfig/system/ntp-servers/primary-ntp-server", 'element': '<ntp-server-address>{0}</ntp-server-address>'.format(primary_server)}
        ret.update({'primary_server': __proxy__['panos.call'](query)})
    if secondary_server:
        query = {'type': 'config', 'action': 'set', 'xpath': "/config/devices/entry[@name='localhost.localdomain']/deviceconfig/system/ntp-servers/secondary-ntp-server", 'element': '<ntp-server-address>{0}</ntp-server-address>'.format(secondary_server)}
        ret.update({'secondary_server': __proxy__['panos.call'](query)})
    if (deploy is True):
        ret.update(commit())
    return ret

Set the NTP servers of the Palo Alto proxy minion. A commit will be required before this is processed.

CLI Example:

Args:
primary_server(str): The primary NTP server IP address or FQDN.

secondary_server(str): The secondary NTP server IP address or FQDN.

deploy (bool): If true then commit the full candidate configuration, if false only set pending change.

.. code-block:: bash

salt '*' ntp.set_servers 0.pool.ntp.org 1.pool.ntp.org
salt '*' ntp.set_servers primary_server=0.pool.ntp.org secondary_server=1.pool.ntp.org
salt '*' ntp.ser_servers 0.pool.ntp.org 1.pool.ntp.org deploy=True


def get_ext(url):
    parsed = urllib.parse.urlparse(url)
    (root, ext) = os.path.splitext(parsed.path)
    return ext.lstrip('.')

Extract an extension from the url.

Args:
url (str): String representation of a url.

Returns:
str: Filename extension from a url (without a dot), '' if extension is not present.


def plotallanvar(data, dt, tmax=10, ax=None, **kwargs):

    if ax is None:
        ax = plt.gca()
    tk, allanvar = allan_variance(data, dt, tmax)
    ax.loglog(tk, allanvar, **kwargs)
    ax.set_xlabel('Time [s]')
    ax.set_ylabel('Allan Variance')
    ax.legend()

Plot Allan variance.

Args:
data (np.ndarray): Input data.
dt (float): Time between each data.
tmax (float): Maximum time.
ax (matplotlib.axes): Axis the figure is plotted on.
kwargs (optional): Plot options passed to ax.plot().


def update_q(self, state_key, action_key, reward_value, next_max_q):

        q = self.extract_q_df(state_key, action_key)

        new_q = q + self.alpha_value * (reward_value + (self.gamma_value * next_max_q) - q)

        self.save_q_df(state_key, action_key, new_q)

Update Q-Value.

Args:
state_key:              The key of state.
action_key:             The key of action.
reward_value:           R-Value(Reward).
next_max_q:             Maximum Q-Value.


def adjust_brightness(img, brightness_factor):

    if not _is_pil_image(img):
        raise TypeError('img should be PIL Image. Got {}'.format(type(img)))

    enhancer = ImageEnhance.Brightness(img)
    img = enhancer.enhance(brightness_factor)
    return img

Adjust brightness of an Image.

Args:
img (PIL Image): PIL Image to be adjusted.
brightness_factor (float):  How much to adjust the brightness. Can be
any non negative number. 0 gives a black image, 1 gives the
original image while 2 increases the brightness by a factor of 2.

Returns:
PIL Image: Brightness adjusted image.


def stateless_random_flip_up_down(image, seed):
    random_func = functools.partial(stateless_random_ops.stateless_random_uniform, seed=seed)
    return _random_flip(image, 0, random_func, 'stateless_random_flip_up_down')

Randomly flip an image vertically (upside down) deterministically.

Guarantees the same results given the same `seed` independent of how many
times the function is called, and independent of global seed settings (e.g.
`tf.random.set_seed`).

Example usage:

>>> image = np.array([[[1], [2]], [[3], [4]]])
>>> seed = (2, 3)
>>> tf.image.stateless_random_flip_up_down(image, seed).numpy().tolist()
[[[3], [4]], [[1], [2]]]

Args:
image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
of shape `[height, width, channels]`.
seed: A shape [2] Tensor, the seed to the random number generator. Must have
dtype `int32` or `int64`. (When using XLA, only `int32` is allowed.)

Returns:
A tensor of the same type and shape as `image`.


async def has_commit_landed_on_repository(self, context, revision):
    if (not _is_git_full_hash(revision)):
        revision = self.get_tag_hash(tag_name=revision)
    repo = self._github_repository.html_url
    url = '/'.join([repo.rstrip('/'), 'branch_commits', revision])
    html_data = (await retry_request(context, url))
    html_text = html_data.strip()
    return (html_text != '')

Tell if a commit was landed on the repository or if it just comes from a pull request.

Args:
context (scriptworker.context.Context): the scriptworker context.
revision (str): the commit hash or the tag name.

Returns:
bool: True if the commit is present in one of the branches of the main repository


def country(self, value=None):
    if (value is not None):
        try:
            value = str(value)
        except ValueError:
            raise ValueError('value {} need to be of type str for field `country`'.format(value))
        if (',' in value):
            raise ValueError('value should not contain a comma for field `country`')
    self._country = value

Corresponds to IDD Field `country`

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

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


def decode_conjure_union_type(cls, obj, conjure_type):

        type_of_union = obj["type"]
        for attr, conjure_field in conjure_type._options().items():
            if conjure_field.identifier == type_of_union:
                attribute = attr
                conjure_field_definition = conjure_field
                break
        else:
            raise ValueError(
                "unknown union type {0} for {1}".format(
                    type_of_union, conjure_type
                )
            )

        deserialized = {}
        if type_of_union not in obj or obj[type_of_union] is None:
            cls.check_null_field(obj, deserialized, conjure_field_definition)
        else:
            value = obj[type_of_union]
            field_type = conjure_field_definition.field_type
            deserialized[attribute] = cls.do_decode(value, field_type)
        return conjure_type(**deserialized)

Decodes json into a conjure union type.

Args:
obj: the json object to decode
conjure_type: a class object which is the union type
we're decoding into
Returns:
An instance of type conjure_type.


def _strip_to_only_final_name(self, summaries):
    result = set()
    for s in summaries:
        if '/' not in s.tag:
            result.add(s)
        else:
            new_tag = s.tag.split('/')[-1]
            result.add(s._replace(tag=new_tag))
    return result

Removes all leading names in a summary

Args:
summaries: A `set` of `_ObservedSummary` values.

Returns:
A new `set` of `_ObservedSummary` values striped of all
name except for the terminal one.


def split_once(self, horizontal: bool, position: int) -> None:
    cdata = self._as_cdata()
    lib.TCOD_bsp_split_once(cdata, horizontal, position)
    self._unpack_bsp_tree(cdata)

Split this partition into 2 sub-partitions.

Args:
horizontal (bool):
position (int):


def GetError(self, filename, parser, lexer):
    logging.debug('\nSymStack: {}\n'.format('\n'.join((str(s) for s in parser.symstack))))
    parser_error = next((error for error in _ALL_PARSER_ERRORS if error.Matches(parser)))
    final_token = parser.symstack[-1]
    error_position = _GetErrorPosition(lexer, final_token)
    error_info = stl.error_formatter.ErrorInfo(id=parser_error.error_id, filename=filename, line=_GetLine(lexer.lexdata, final_token), position=error_position, message=parser_error.error_msg)
    return self.Format(error_info)

Returns an error string based on the state of |parser| and |lexer|.

Args:
filename: The name of the file the error occured in.
parser: A PLY parser that has reached the p_error() state.
lexer: The PLY lexer that |parser| used to lex.

Returns:
A formatted error string.


def _Scroll(self, lines=None):
    if (lines is None):
        lines = self._cli_lines
    if (lines < 0):
        self._displayed -= self._cli_lines
        self._displayed += lines
        if (self._displayed < 0):
            self._displayed = 0
        self._lines_to_show = self._cli_lines
    else:
        self._lines_to_show = lines
    self._lastscroll = lines

Set attributes to scroll the buffer correctly.

Args:
lines: An int, number of lines to scroll. If None, scrolls
by the terminal length.


def download_file_by_name(url, target_folder, file_name, mkdir=False):

    __hdr__ = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 '
               '(KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
               'Accept': 'text/html,application/xhtml+xml,'
               'application/xml;q=0.9,*/*;q=0.8',
               'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
               'Accept-Encoding': 'none',
               'Accept-Language': 'en-US,en;q=0.8',
               'Connection': 'keep-alive'}

    if not os.path.isdir(target_folder):
        if mkdir:
            preparedir(target_folder)
        else:
            created = preparedir(target_folder, False)
            if not created:
                raise ValueError("Failed to find %s." % target_folder)
    file_path = os.path.join(target_folder, file_name)

    if (sys.version_info < (3, 0)):
        _download_py2(url, file_path, __hdr__)
    else:
        _download_py3(url, file_path, __hdr__)

Download a file to a directory.

Args:
url: A string to a valid URL.
target_folder: Target folder for download (e.g. c:/ladybug)
file_name: File name (e.g. testPts.zip).
mkdir: Set to True to create the directory if doesn't exist (Default: False)


def extract_folds_from_ir_blocks(ir_blocks):

    folds = dict()
    remaining_ir_blocks = []
    current_folded_blocks = []
    in_fold_location = None

    for block in ir_blocks:
        if isinstance(block, Fold):
            if in_fold_location is not None:
                raise AssertionError(u'in_fold_location was not None at a Fold block: {} {} '
                                     u'{}'.format(current_folded_blocks, remaining_ir_blocks,
                                                  ir_blocks))

            in_fold_location = block.fold_scope_location
        elif isinstance(block, Unfold):
            if in_fold_location is None:
                raise AssertionError(u'in_fold_location was None at an Unfold block: {} {} '
                                     u'{}'.format(current_folded_blocks, remaining_ir_blocks,
                                                  ir_blocks))

            folds[in_fold_location] = current_folded_blocks
            current_folded_blocks = []
            in_fold_location = None
        else:
            if in_fold_location is not None:
                current_folded_blocks.append(block)
            else:
                remaining_ir_blocks.append(block)

    return folds, remaining_ir_blocks

Extract all @fold data from the IR blocks, and cut the folded IR blocks out of the IR.

Args:
ir_blocks: list of IR blocks to extract fold data from

Returns:
tuple (folds, remaining_ir_blocks):
- folds: dict of FoldScopeLocation -> list of IR blocks corresponding to that @fold scope.
The list does not contain Fold or Unfold blocks.
- remaining_ir_blocks: list of IR blocks that were not part of a Fold-Unfold section.


def _open_streaming_interface(self, connection_id, callback):
    try:
        context = self.connections.get_context(connection_id)
    except ArgumentError:
        callback(connection_id, self.id, False, 'Could not find connection information')
        return
    self._logger.info('Attempting to enable streaming')
    self.connections.begin_operation(connection_id, 'open_interface', callback, self.get_config('default_timeout'))
    try:
        characteristic = context['services'][TileBusService][StreamingChar]
    except KeyError:
        self.connections.finish_operation(connection_id, False, "Can't find characteristic to open streaming interface")
        return
    context['parser'] = IOTileReportParser(report_callback=self._on_report, error_callback=self._on_report_error)
    context['parser'].context = connection_id

    def on_report_chunk_received(report_chunk):
        'Callback function called when a report chunk has been received.'
        context['parser'].add_data(report_chunk)
    self._register_notification_callback(context['connection_handle'], characteristic.value_handle, on_report_chunk_received)
    self.bable.set_notification(enabled=True, connection_handle=context['connection_handle'], characteristic=characteristic, on_notification_set=[self._on_interface_opened, context], on_notification_received=self._on_notification_received, timeout=1.0, sync=False)

Enable streaming interface for this IOTile device

Args:
connection_id (int): The unique identifier for the connection
callback (callback): Callback to be called when this command finishes
callback(conn_id, adapter_id, success, failure_reason)


def getAsGrassAsciiGrid(self, session):

        if type(self.raster) != type(None):

            converter = RasterConverter(sqlAlchemyEngineOrSession=session)

            return converter.getAsGrassAsciiRaster(tableName=self.tableName,
                                                   rasterIdFieldName='id',
                                                   rasterId=self.id,
                                                   rasterFieldName=self.rasterColumnName)

Retrieve the raster in the GRASS ASCII Grid format.

Args:
session (:mod:`sqlalchemy.orm.session.Session`): SQLAlchemy session object bound to PostGIS enabled database.

Returns:
str: GRASS ASCII string.


def AddArguments(cls, argument_group):

    argument_group.add_argument(
        '--index_name', dest='index_name', type=str, action='store',
        default=cls._DEFAULT_INDEX_NAME, help=(
            'Name of the index in ElasticSearch.'))
    argument_group.add_argument(
        '--doc_type', dest='document_type', type=str,
        action='store', default=cls._DEFAULT_DOCUMENT_TYPE, help=(
            'Name of the document type that will be used in ElasticSearch.'))
    argument_group.add_argument(
        '--flush_interval', dest='flush_interval', type=int,
        action='store', default=cls._DEFAULT_FLUSH_INTERVAL, help=(
            'Events to queue up before bulk insert to ElasticSearch.'))
    argument_group.add_argument(
        '--raw_fields', dest='raw_fields', action='store_true',
        default=cls._DEFAULT_RAW_FIELDS, help=(
            'Export string fields that will not be analyzed by Lucene.'))
    argument_group.add_argument(
        '--elastic_user', dest='elastic_user', action='store',
        default=cls._DEFAULT_ELASTIC_USER, help=(
            'Username to use for Elasticsearch authentication.'))
    argument_group.add_argument(
        '--use_ssl', dest='use_ssl', action='store_true',
        help='Enforces use of ssl.')
    argument_group.add_argument(
        '--ca_certificates_file_path', dest='ca_certificates_file_path',
        action='store', type=str, default=cls._DEFAULT_CA_CERTS, help=(
            'Path to a file containing a list of root certificates to trust.'))
    argument_group.add_argument(
        '--elastic_url_prefix', dest='elastic_url_prefix', type=str,
        action='store', default=cls._DEFAULT_URL_PREFIX, help=(
            'URL prefix for elastic search.'))

    ElasticSearchServerArgumentsHelper.AddArguments(argument_group)

Adds command line arguments the helper supports to an argument group.

This function takes an argument parser or an argument group object and adds
to it all the command line arguments this helper supports.

Args:
argument_group (argparse._ArgumentGroup|argparse.ArgumentParser):
argparse group.


def set_key_color(self, color: Tuple[int, int, int]) -> None:

        lib.TCOD_image_set_key_color(self.image_c, color)

Set a color to be transparent during blitting functions.

Args:
color (Union[Tuple[int, int, int], Sequence[int]]):
An (r, g, b) sequence or Color instance.


def wait_for_channel_in_states(raiden: 'RaidenService', payment_network_id: PaymentNetworkID, token_address: TokenAddress, channel_ids: List[ChannelID], retry_timeout: float, target_states: Sequence[str]) -> None:
    chain_state = views.state_from_raiden(raiden)
    token_network = views.get_token_network_by_token_address(chain_state=chain_state, payment_network_id=payment_network_id, token_address=token_address)
    if (token_network is None):
        raise ValueError(f'The token {token_address} is not registered on the network {payment_network_id}.')
    token_network_address = token_network.address
    list_cannonical_ids = [CanonicalIdentifier(chain_identifier=chain_state.chain_id, token_network_address=token_network_address, channel_identifier=channel_identifier) for channel_identifier in channel_ids]
    while list_cannonical_ids:
        canonical_id = list_cannonical_ids[(- 1)]
        chain_state = views.state_from_raiden(raiden)
        channel_state = views.get_channelstate_by_canonical_identifier(chain_state=chain_state, canonical_identifier=canonical_id)
        channel_is_settled = ((channel_state is None) or (channel.get_status(channel_state) in target_states))
        if channel_is_settled:
            list_cannonical_ids.pop()
        else:
            gevent.sleep(retry_timeout)

Wait until all channels are in `target_states`.

Raises:
ValueError: If the token_address is not registered in the
payment_network.

Note:
This does not time out, use gevent.Timeout.


def rprint(sep='\n', end='\n', file=sys.stdout, flush=False):
    try:
        first_item = (yield)
        file.write(str(first_item))
        if flush:
            file.flush()
        while True:
            item = (yield)
            file.write(sep)
            file.write(str(item))
            if flush:
                file.flush()
    except GeneratorExit:
        file.write(end)
        if flush:
            file.flush()

A coroutine sink which prints received items stdout

Args:
sep: Optional separator to be printed between received items.
end: Optional terminator to be printed after the last item.
file: Optional stream to which to print.
flush: Optional flag to force flushing after each item.


def get_gradebook_id(self, gbuuid):

        gradebook = self.get('gradebook', params={'uuid': gbuuid})
        if 'data' not in gradebook:
            failure_messsage = ('Error in get_gradebook_id '
                                'for {0} - no data'.format(
                                    gradebook
                                ))
            log.error(failure_messsage)
            raise PyLmodUnexpectedData(failure_messsage)
        return gradebook['data']['gradebookId']

Return gradebookid for a given gradebook uuid.

Args:
gbuuid (str): gradebook uuid, i.e. ``STELLAR:/project/gbngtest``

Raises:
PyLmodUnexpectedData: No gradebook id returned
requests.RequestException: Exception connection error
ValueError: Unable to decode response content

Returns:
str: value of gradebook id


def set_raw_datadir(self, directory=None):
    if (directory is None):
        self.logger.info('no directory name given')
        return
    if (not os.path.isdir(directory)):
        self.logger.info(directory)
        self.logger.info('directory does not exist')
        return
    self.raw_datadir = directory

Set the directory containing .res-files.

Used for setting directory for looking for res-files.@
A valid directory name is required.

Args:
directory (str): path to res-directory

Example:
>>> d = CellpyData()
>>> directory = "MyData/Arbindata"
>>> d.set_raw_datadir(directory)


def _get_record(self, model_class, record_id):

        url = '{host}/{namespace}/{model}/{id}'.format(
            host=self._host,
            namespace=self._namespace,
            model=self._translate_name(model_class.__name__),
            id=record_id
        )
        data = self._get_json(url)['data']
        fresh_model = model_class(data['attributes'])
        fresh_model.id = data['id']
        fresh_model.validate()
        if self._cache is not None:
            self._cache.set_record(model_class.__name__, fresh_model.id, fresh_model)
        return fresh_model

Get a single record from the API.

Args:
model_class (:class:`cinder_data.model.CinderModel`): A subclass of
:class:`cinder_data.model.CinderModel` of your chosen model.
record_id (int): The id of the record requested.

Returns:
:class:`cinder_data.model.CinderModel`: An instance of model_class or None.


def restart(self, **kwargs):
    return self.client.api.restart(self.id, **kwargs)

Restart this container. Similar to the ``docker restart`` command.

Args:
timeout (int): Number of seconds to try to stop for before killing
the container. Once killed it will then be restarted. Default
is 10 seconds.

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


def add_direct(self, target, var_id, var_type, data):

        data = struct.pack("<H", var_id) + _convert_to_bytes(var_type, data)

        if self.data_size - self.data_index < len(data):
            raise DataError("Not enough space for data in new conig entry", needed_space=len(data), actual_space=(self.data_size - self.data_index))

        new_entry = ConfigEntry(target, var_id, data)

        for entry in self.entries:
            if entry.target == new_entry.target and entry.var_id == new_entry.var_id:
                entry.valid = False

        self.entries.append(new_entry)
        self.data_index += new_entry.data_space()

Directly add a config variable.

This method is meant to be called from emulation scenarios that
want to directly set config database entries from python.

Args:
target (SlotIdentifer): The target slot for this config variable.
var_id (int): The config variable ID
var_type (str): The config variable type
data (bytes or int or str): The data that will be encoded according
to var_type.


def _delocalize_logging_command(self, logging_path, user_project):
    logging_prefix = os.path.splitext(logging_path.uri)[0]
    if (logging_path.file_provider == job_model.P_LOCAL):
        mkdir_cmd = ('mkdir -p "%s"\n' % os.path.dirname(logging_prefix))
        cp_cmd = 'cp'
    elif (logging_path.file_provider == job_model.P_GCS):
        mkdir_cmd = ''
        if user_project:
            cp_cmd = 'gsutil -u {} -mq cp'.format(user_project)
        else:
            cp_cmd = 'gsutil -mq cp'
    else:
        assert False
    copy_logs_cmd = textwrap.dedent('      local cp_cmd="{cp_cmd}"\n      local prefix="{prefix}"\n    ').format(cp_cmd=cp_cmd, prefix=logging_prefix)
    body = textwrap.dedent('      {mkdir_cmd}\n      {copy_logs_cmd}\n    ').format(mkdir_cmd=mkdir_cmd, copy_logs_cmd=copy_logs_cmd)
    return body

Returns a command to delocalize logs.

Args:
logging_path: location of log files.
user_project: name of the project to be billed for the request.

Returns:
eg. 'gs://bucket/path/myfile' or 'gs://bucket/script-foobar-12'


def __extract_file(self, path, fileinfo, destination):
    if ('offset' not in fileinfo):
        self.__copy_extracted(path, destination)
        return
    self.asarfile.seek(self.__absolute_offset(fileinfo['offset']))
    contents = self.asarfile.read(self.__absolute_offset(fileinfo['size']))
    destination_path = os.path.join(destination, path)
    with open(destination_path, 'wb') as fp:
        fp.write(contents)
    LOGGER.debug('Extracted %s to %s', path, destination_path)

Extracts the specified file to the specified destination.

Args:
path (str):
Relative (to the root of the archive) path of the
file to extract.

fileinfo (dict):
Dictionary containing the offset and size of the file
(Extracted from the header).

destination (str):
Directory to extract the archive to.


def listen_loop(self):
    while self.listening:
        try:
            (data, address) = self.sock.recvfrom(self.bufsize)
            self.receive_datagram(data, address)
            if self.stats_enabled:
                self.stats['bytes_recieved'] += len(data)
        except socket.error as error:
            if (error.errno == errno.WSAECONNRESET):
                logger.info('connection reset')
            else:
                raise
    logger.info('Shutting down the listener...')

Starts the listen loop and executes the receieve_datagram method
whenever a packet is receieved.

Args:
None

Returns:
None


def _parse_from_table(html_chunk, what):
    ean_tag = html_chunk.find('tr', fn=must_contain('th', what, 'td'))
    if (not ean_tag):
        return None
    return get_first_content(ean_tag[0].find('td'))

Go thru table data in `html_chunk` and try to locate content of the
neighbor cell of the cell containing `what`.

Returns:
str: Table data or None.


def _get_token_budget(self, question_tokens, max_length=None):
    return (max_length if max_length is not None else self.model_max_length) - self._question_encoding_cost(question_tokens)

Computes the number of tokens left for the table after tokenizing a question, taking into account the max
sequence length of the model.

Args:
question_tokens (`List[String]`):
List of question tokens. Returns: `int`: the number of tokens left for the table, given the model max
length.


def to_xml(self, xmllint=False):
    root = self._tree.getroot()
    ret = ET.tostring(ET.ElementTree(root), pretty_print=True)
    if xmllint:
        ret = xmllint_format(ret)
    return ret

Serialize all properties as pretty-printed XML

Args:
xmllint (boolean): Format with ``xmllint`` in addition to pretty-printing


def get_dimension_type(self, dim):

        dim_obj = self.get_dimension(dim)
        if dim_obj and dim_obj.type is not None:
            return dim_obj.type
        dim_vals = [type(v) for v in self.dimension_values(dim)]
        if len(set(dim_vals)) == 1:
            return dim_vals[0]
        else:
            return None

Get the type of the requested dimension.

Type is determined by Dimension.type attribute or common
type of the dimension values, otherwise None.

Args:
dimension: Dimension to look up by name or by index

Returns:
Declared type of values along the dimension


def model_from_path(model_path, fuzziness=False):

    app_name = '.'.join(model_path.split('.')[:-1])
    model_name = model_path.split('.')[-1]

    if not app_name:
        return None

    module = importlib.import_module(app_name)
    try:
        model = getattr(module, model_name)
    except AttributeError:
        try:
            model = getattr(getattr(module, 'models'), model_name)
        except AttributeError:
            model = get_model(model_name, app_name, fuzziness=fuzziness)

    return model

Find the model class for a given model path like 'project.app.model'

Args:
path (str): dot-delimited model path, like 'project.app.model'

Returns:
Django Model-based class


def extract(self, url=None, raw_html=None):
    crawl_candidate = CrawlCandidate(self.config, url, raw_html)
    return self.__crawl(crawl_candidate)

Extract the most likely article content from the html page

Args:
url (str): URL to pull and parse
raw_html (str): String representation of the HTML page
Returns:
Article: Representation of the article contents \
including other parsed and extracted metadata


def _register_if_needed(cls, run_object):
    if isinstance(run_object, six.string_types):
        return run_object
    elif isinstance(run_object, types.FunctionType):
        if (run_object.__name__ == '<lambda>'):
            logger.warning('Not auto-registering lambdas - resolving as variant.')
            return run_object
        else:
            name = run_object.__name__
            register_trainable(name, run_object)
            return name
    elif isinstance(run_object, type):
        name = run_object.__name__
        register_trainable(name, run_object)
        return name
    else:
        raise TuneError("Improper 'run' - not string nor trainable.")

Registers Trainable or Function at runtime.

Assumes already registered if run_object is a string. Does not
register lambdas because they could be part of variant generation.
Also, does not inspect interface of given run_object.

Arguments:
run_object (str|function|class): Trainable to run. If string,
assumes it is an ID and does not modify it. Otherwise,
returns a string corresponding to the run_object name.

Returns:
A string representing the trainable identifier.


def create_dir(dirname):
    create_dir_v2(dirname)

Creates a directory with the name `dirname`.

Args:
dirname: string, name of the directory to be created

Notes: The parent directories need to exist. Use `tf.io.gfile.makedirs`
instead if there is the possibility that the parent dirs don't exist.

Raises:
errors.OpError: If the operation fails.


def tokenize(self, docs):
    if (self.n_jobs == 1):
        return [self._tokenize(doc) for doc in docs]
    else:
        return parallel(self._tokenize, docs, self.n_jobs)

Tokenizes a document, using a lemmatizer.

Args:
| doc (str)                 -- the text document to process.

Returns:
| list                      -- the list of tokens.


def IsRValueAllowed(clean_lines, linenum, typenames):
    for i in xrange(linenum, 0, (- 1)):
        line = clean_lines.elided[i]
        if Match('GOOGLE_ALLOW_RVALUE_REFERENCES_(?:PUSH|POP)', line):
            if (not line.endswith('PUSH')):
                return False
            for j in xrange(linenum, clean_lines.NumLines(), 1):
                line = clean_lines.elided[j]
                if Match('GOOGLE_ALLOW_RVALUE_REFERENCES_(?:PUSH|POP)', line):
                    return line.endswith('POP')
    line = clean_lines.elided[linenum]
    if Search('\\boperator\\s*=\\s*\\(', line):
        return IsDeletedOrDefault(clean_lines, linenum)
    match = Match('\\s*(?:[\\w<>]+::)*([\\w<>]+)\\s*::\\s*([\\w<>]+)\\s*\\(', line)
    if (match and (match.group(1) == match.group(2))):
        return IsDeletedOrDefault(clean_lines, linenum)
    if Search('\\b(?:explicit|inline)\\s+[\\w<>]+\\s*\\(', line):
        return IsDeletedOrDefault(clean_lines, linenum)
    if Match('\\s*[\\w<>]+\\s*\\(', line):
        previous_line = 'ReturnType'
        if (linenum > 0):
            previous_line = clean_lines.elided[(linenum - 1)]
        if (Match('^\\s*$', previous_line) or Search('[{}:;]\\s*$', previous_line)):
            return IsDeletedOrDefault(clean_lines, linenum)
    while line:
        match = Match('^.*?(\\w+)\\s*&&(.*)$', line)
        if (not match):
            break
        if (match.group(1) not in typenames):
            return False
        line = match.group(2)
    return (line.find('&&') < 0)

Check if RValue reference is allowed on a particular line.

Args:
clean_lines: A CleansedLines instance containing the file.
linenum: The number of the line to check.
typenames: set of type names from template-argument-list.
Returns:
True if line is within the region where RValue references are allowed.


def add_next_tick_callback(self, callback):
    from ..server.callbacks import NextTickCallback
    cb = NextTickCallback(self, None)
    return self._add_session_callback(cb, callback, one_shot=True, originator=self.add_next_tick_callback)

Add callback to be invoked once on the next tick of the event loop.

Args:
callback (callable) :
A callback function to execute on the next tick.

Returns:
NextTickCallback : can be used with ``remove_next_tick_callback``

.. note::
Next tick callbacks only work within the context of a Bokeh server
session. This function will no effect when Bokeh outputs to
standalone HTML or Jupyter notebook cells.