def gather(strategy, value):
    return nest.map_structure(functools.partial(_gather, strategy), value)

Gathers value from all workers.

This is intended for tests before we implement an official all-gather API.

Args:
strategy: a `tf.distribute.Strategy`.
value: a nested structure of n-dim `tf.distribute.DistributedValue` of
`tf.Tensor`, or of a `tf.Tensor` if the strategy only has one replica.
Cannot contain tf.sparse.SparseTensor.

Returns:
a (n+1)-dim `tf.Tensor`.


def convert_dropout(params, w_name, scope_name, inputs, layers, weights, names):

    print('Converting dropout ...')

    if names == 'short':
        tf_name = 'DO' + random_string(6)
    elif names == 'keep':
        tf_name = w_name
    else:
        tf_name = w_name + str(random.random())

    dropout = keras.layers.Dropout(rate=params['ratio'], name=tf_name)
    layers[scope_name] = dropout(layers[inputs[0]])

Convert dropout.

Args:
params: dictionary with layer parameters
w_name: name prefix in state_dict
scope_name: pytorch scope name
inputs: pytorch node inputs
layers: dictionary with keras tensors
weights: pytorch state_dict
names: use short names for keras layers


def update_defaults(self, new_defaults, respect_none=False):

        for key, value in six.iteritems(new_defaults):
            item = self.get_item(key)
            if item is None:
                raise YapconfItemNotFound("Cannot update default for {0}, "
                                          "there is no config item by the "
                                          "name of {1}".format(key, key), None)

            item.update_default(value, respect_none)

Update items defaults to the values in the new_defaults dict.

Args:
new_defaults (dict): A key-value pair of new defaults to be
applied.
respect_none (bool): Flag to indicate if ``None`` values should
constitute an update to the default.


def cumulative_probabilities(self):
    partition_function = np.sum(self.p)
    return (np.cumsum(self.p) / partition_function)

Cumulative sum of the relative probabilities for all possible jumps.

Args:
None

Returns:
(np.array): Cumulative sum of relative jump probabilities.


def Append(self, item):

    if self._index >= self._size:
      self._index = self._index % self._size

    try:
      self._list[self._index] = item
    except IndexError:
      self._list.append(item)
    self._index += 1

Add an item to the list.

Args:
item (object): item.


def where(self, *constraints: column_expression_builder.ColumnExpressionBuilder) -> 'View':
    for constraint in constraints:
        if constraint.node.return_type != _fhir_path_data_types.Boolean:
            raise ValueError(('view `where` expressions must be boolean predicates', f' got `{constraint.node.to_fhir_path()}`'))
    return View(self._context, self._root_resource, self._fields, self._constraints + tuple(constraints), self._handler)

Returns a new View instance with these added constraints.

Args:
*constraints: a list of FHIRPath expressions to conjuctively constrain the
underlying data.  The returned view will apply the both the current and
additional constraints defined here.


def _add_bound_method(self, bound_method, identify_observed):
    inst = bound_method.__self__
    method_name = bound_method.__name__
    key = self.make_key(bound_method)
    if (key not in self.observers):
        self.observers[key] = ObserverBoundMethod(inst, method_name, identify_observed, (key, self.observers))
        return True
    else:
        return False

Add an bound method as an observer.

Args:
bound_method: The bound method to add as an observer.
identify_observed: See the docstring for add_observer.

Returns:
True if the bound method is added, otherwise False.


def forward(self, inputs_embeds, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None) -> Union[Tuple, BaseModelOutput]:
    output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
    output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    encoder_states = () if output_hidden_states else None
    all_attentions = () if output_attentions else None
    hidden_states = inputs_embeds
    for idx, encoder_layer in enumerate(self.layers):
        if output_hidden_states:
            encoder_states = encoder_states + (hidden_states,)
        if self.gradient_checkpointing and self.training:
            layer_outputs = self._gradient_checkpointing_func(encoder_layer.__call__, hidden_states, output_attentions)
        else:
            layer_outputs = encoder_layer(hidden_states, output_attentions=output_attentions)
        hidden_states = layer_outputs[0]
        if output_attentions:
            all_attentions = all_attentions + (layer_outputs[1],)
    if output_hidden_states:
        encoder_states = encoder_states + (hidden_states,)
    if not return_dict:
        return tuple((v for v in [hidden_states, encoder_states, all_attentions] if v is not None))
    return BaseModelOutput(last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions)

Args:
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation.
This is useful if you want more control over how to convert `input_ids` indices into associated vectors
than the model's internal embedding lookup matrix.
output_attentions (`bool`, *optional*):
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
returned tensors for more detail.
output_hidden_states (`bool`, *optional*):
Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors
for more detail.
return_dict (`bool`, *optional*):
Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.


def filter(self, *query_filter):
    for query in query_filter:
        self.query.append(query)
    return self

Set the query filter to perform the query with

Args:
*query_filter: Simplified Query Language filter


def _CreateAdGroup(client, campaign_id):

  ad_group_service = client.GetService('AdGroupService')

  operations = [{
      'operator': 'ADD',
      'operand': {
          'campaignId': campaign_id,
          'adGroupType': 'SEARCH_DYNAMIC_ADS',
          'name': 'Earth to Mars Cruises
          'status': 'PAUSED',
          'biddingStrategyConfiguration': {
              'bids': [{
                  'xsi_type': 'CpcBid',
                  'bid': {
                      'microAmount': '3000000'
                  },
              }]
          }
      }
  }]

  ad_group = ad_group_service.mutate(operations)['value'][0]
  ad_group_id = ad_group['id']

  print 'Ad group with ID "%d" and name "%s" was created.' % (
      ad_group_id, ad_group['name'])

  return ad_group_id

Creates an ad group.

Args:
client: an AdWordsClient instance.
campaign_id: an integer campaign ID.

Returns:
An integer ad group ID.


def inverse_transform(self, y):
    sklearn.base.check_is_fitted(self)
    xp, _ = sklearn.utils._array_api.get_namespace(y)
    if self.ndim_ == 1 and y.ndim == 2:
        return xp.squeeze(y, axis=1)
    return y

Revert the transformation of transform.

Args:
y: np.ndarray
Transformed numpy array.

Returns:
np.ndarray
If the transformer was fit to a 1D numpy array,
and a 2D numpy array with a singleton second dimension
is passed, it will be squeezed back to 1D. Otherwise, it
will eb left untouched.


def prepare_soap_body(self, method, parameters, namespace):
    tags = []
    for (name, value) in parameters:
        tag = '<{name}>{value}</{name}>'.format(name=name, value=escape(('%s' % value), {'"': '"'}))
        tags.append(tag)
    wrapped_params = ''.join(tags)
    if (namespace is not None):
        soap_body = '<{method} xmlns="{namespace}">{params}</{method}>'.format(method=method, params=wrapped_params, namespace=namespace)
    else:
        soap_body = '<{method}>{params}</{method}>'.format(method=method, params=wrapped_params)
    return soap_body

Prepare the SOAP message body for sending.

Args:
method (str): The name of the method to call.
parameters (list): A list of (name, value) tuples containing
the parameters to pass to the method.
namespace (str): tThe XML namespace to use for the method.

Returns:
str: A properly formatted SOAP Body.


class Iterator(PyDataset):
    white_list_formats = ('png', 'jpg', 'jpeg', 'bmp', 'ppm', 'tif', 'tiff')

    def __init__(self, n, batch_size, shuffle, seed):
        self.n = n
        self.batch_size = batch_size
        self.seed = seed
        self.shuffle = shuffle
        self.batch_index = 0
        self.total_batches_seen = 0
        self.lock = threading.Lock()
        self.index_array = None
        self.index_generator = self._flow_index()

    def _set_index_array(self):
        self.index_array = np.arange(self.n)
        if self.shuffle:
            self.index_array = np.random.permutation(self.n)

    def __getitem__(self, idx):
        if idx >= len(self):
            raise ValueError('Asked to retrieve element {idx}, but the Sequence has length {length}'.format(idx=idx, length=len(self)))
        if self.seed is not None:
            np.random.seed(self.seed + self.total_batches_seen)
        self.total_batches_seen += 1
        if self.index_array is None:
            self._set_index_array()
        index_array = self.index_array[self.batch_size * idx:self.batch_size * (idx + 1)]
        return self._get_batches_of_transformed_samples(index_array)

    def __len__(self):
        return (self.n + self.batch_size - 1)

    def on_epoch_end(self):
        self._set_index_array()

    def reset(self):
        self.batch_index = 0

    def _flow_index(self):
        self.reset()
        while 1:
            if self.seed is not None:
                np.random.seed(self.seed + self.total_batches_seen)
            if self.batch_index == 0:
                self._set_index_array()
            if self.n == 0:
                current_index = 0
            else:
                current_index = self.batch_index * self.batch_size % self.n
            if self.n > current_index + self.batch_size:
                self.batch_index += 1
            else:
                self.batch_index = 0
            self.total_batches_seen += 1
            yield self.index_array[current_index:current_index + self.batch_size]

    def __iter__(self):
        return self

    def __next__(self):
        with self.lock:
            index_array = next(self.index_generator)
        return self._get_batches_of_transformed_samples(index_array)

    def _get_batches_of_transformed_samples(self, index_array):

        raise NotImplementedError

Base class for image data iterators.

DEPRECATED.

Every `Iterator` must implement the `_get_batches_of_transformed_samples`
method.

Args:
n: Integer, total number of samples in the dataset to loop over.
batch_size: Integer, size of a batch.
shuffle: Boolean, whether to shuffle the data between epochs.
seed: Random seeding for data shuffling.


def main(args):
    if (not args):
        raise Exception('Please specify at least one JSON config path')
    inputs = []
    program = []
    outputs = []
    for arg in args:
        with open(arg) as fd:
            config = json.load(fd)
        inputs.extend(config.get('inputs', []))
        program.extend(config.get('program', []))
        outputs.extend(config.get('outputs', []))
    if (not program):
        raise Exception('Please specify a program')
    return run(inputs, program, outputs)

Invokes run function using a JSON file config.

Args:
args: CLI args, which can be a JSON file containing an object whose
attributes are the parameters to the run function. If multiple JSON
files are passed, their contents are concatenated.
Returns:
0 if succeeded or nonzero if failed.
Raises:
Exception: If input data is missing.


def request(self,message,message_type):

        if message_type == MULTIPART:
            raise Exception("Unsupported request type")

        super(Requestor,self).send(message,message_type)

Send a request message of the given type

Args:
- message: the message to publish
- message_type: the type of message being sent


def GetSourceStrings(cls, event):

    formatter_object = cls.GetFormatterObject(event.data_type)
    return formatter_object.GetSources(event)

Retrieves the formatted source strings for a specific event object.

Args:
event (EventObject): event.

Returns:
list[str, str]: short and long version of the source of the event.


def qrandom(n):
    import quantumrandom
    return np.concatenate([quantumrandom.get_data(data_type='uint16', array_length=1024) for i in range(int(np.ceil((n / 1024.0))))])[:n]

Creates an array of n true random numbers obtained from the quantum random
number generator at qrng.anu.edu.au

This function requires the package quantumrandom and an internet connection.

Args:
n (int):
length of the random array

Return:
array of ints:
array of truly random unsigned 16 bit int values


def convert_slice(params, w_name, scope_name, inputs, layers, weights, names):

    print('Converting slice ...')

    if len(params['axes']) > 1:
        raise AssertionError('Cannot convert slice by multiple dimensions')

    if params['axes'][0] not in [0, 1, 2, 3]:
        raise AssertionError('Slice by dimension more than 3 or less than 0 is not supported')

    def target_layer(x, axis=int(params['axes'][0]), start=int(params['starts'][0]), end=int(params['ends'][0])):
        if axis == 0:
            return x[start:end]
        elif axis == 1:
            return x[:, start:end]
        elif axis == 2:
            return x[:, :, start:end]
        elif axis == 3:
            return x[:, :, :, start:end]

    lambda_layer = keras.layers.Lambda(target_layer)
    layers[scope_name] = lambda_layer(layers[inputs[0]])

Convert slice operation.

Args:
params: dictionary with layer parameters
w_name: name prefix in state_dict
scope_name: pytorch scope name
inputs: pytorch node inputs
layers: dictionary with keras tensors
weights: pytorch state_dict
names: use short names for keras layers


def make_bubble_surface(dims=DEFAULT_DIMS, repeat=3):

    gradients = make_gradients(dims)
    return (
        np.sin((gradients[0] - 0.5) * repeat * np.pi) *
        np.sin((gradients[1] - 0.5) * repeat * np.pi))

Makes a surface from the product of sine functions on each axis.

Args:
dims (pair): the dimensions of the surface to create
repeat (int): the frequency of the waves is set to ensure this many
repetitions of the function

Returns:
surface: A surface.


def discovery(self, logfile=None, tracefile=None):
    self._enable_logging(logfile=logfile, tracefile=tracefile)
    self.log("'discovery' method is deprecated. Please 'connect' with force_discovery=True.")
    self.log('Device discovery process started')
    self.connect(logfile=logfile, force_discovery=True, tracefile=tracefile)
    self.disconnect()

Discover the device details.

This method discover several device attributes.

Args:
logfile (file): Optional file descriptor for session logging. The file must be open for write.
The session is logged only if ``log_session=True`` was passed to the constructor.
It the parameter is not passed then the default *session.log* file is created in `log_dir`.


def ctc_loss_and_grad(logits, labels, label_length, logit_length, unique=None):
    num_labels = _get_dim(logits, 2)
    max_label_seq_length = _get_dim(labels, 1)
    ilabel_log_probs = nn_ops.log_softmax(logits)
    state_log_probs = _ilabel_to_state(labels, num_labels, ilabel_log_probs)
    state_trans_probs = _ctc_state_trans(labels)
    initial_state_log_probs, final_state_log_probs = ctc_state_log_probs(label_length, max_label_seq_length)
    fwd_bwd_log_probs, log_likelihood = _forward_backward_log(state_trans_log_probs=math_ops.log(state_trans_probs), initial_state_log_probs=initial_state_log_probs, final_state_log_probs=final_state_log_probs, observed_log_probs=state_log_probs, sequence_length=logit_length)
    if unique:
        olabel_log_probs = _state_to_olabel_unique(labels, num_labels, fwd_bwd_log_probs, unique)
    else:
        olabel_log_probs = _state_to_olabel(labels, num_labels, fwd_bwd_log_probs)
    grad = math_ops.exp(ilabel_log_probs) - math_ops.exp(olabel_log_probs)
    max_logit_length = _get_dim(logits, 0)
    logit_mask = array_ops.sequence_mask(logit_length, max_logit_length, dtypes.float32)
    logit_mask = array_ops.transpose(logit_mask, perm=[1, 0])
    logit_mask = array_ops.expand_dims(logit_mask, axis=2)
    grad *= logit_mask
    loss = -log_likelihood
    return (loss, grad)

Computes the CTC loss and gradients.

Most users will want fwd_bwd.ctc_loss

This function returns the computed gradient, it does not have a gradient
of its own defined.

Args:
logits: tensor of shape [frames, batch_size, num_labels]
labels: tensor of shape [batch_size, max_label_seq_length]
label_length: tensor of shape [batch_size] Length of reference label
sequence in labels.
logit_length: tensor of shape [batch_size] Length of input sequence in
logits.
unique: (optional) unique label indices as computed by unique(labels) If
supplied, enables an implementation that is faster and more memory
efficient on TPU.

Returns:
loss: tensor of shape [batch_size]
gradient: tensor of shape [frames, batch_size, num_labels]


def _chunk_query(l, n, cn, conn, table, db_type):

    [insert_query_m(l[i:i + n], table, conn, cn, db_type) for i in range(0, len(l), n)]

Call for inserting SQL query in chunks based on n rows

Args:
l (list): List of tuples
n (int): Number of rows
cn (str): Column names
conn (connection object): Database connection object
table (str): Table name
db_type (str): If "sqlite" or "mysql"


def correlation_vector(self, value):

        if value == self._defaults['ai.operation.correlationVector'] and 'ai.operation.correlationVector' in self._values:
            del self._values['ai.operation.correlationVector']
        else:
            self._values['ai.operation.correlationVector'] = value

The correlation_vector property.

Args:
value (string). the property value.


def emit(self, record):

        if record.levelno < logging.getLevelName(self.min_level):
            return

        evt = LogEvent()
        evt.level = record.levelname
        evt.levelno = record.levelno
        evt.timestamp = datetime.fromtimestamp(record.created)
        evt.message = record.message
        evt.filename = record.filename
        evt.lineno = record.lineno
        evt.module = record.module
        evt.funcname = record.funcName
        evt.pathname = record.pathname
        evt.process_id = record.process

        if record.levelno >= 40:
            evt.stacktrace = traceback.format_exc()

        try:
            db.session.add(evt)
            db.session.commit()
        except Exception:
            db.session.rollback()

Persist a record into the database

Args:
record (`logging.Record`): The logging.Record object to store

Returns:
`None`


def _ot_make_closed(self, access_string):

        self.observation_table.sm_vector.append(access_string)
        for i in self.alphabet:
            self.observation_table.smi_vector.append(access_string + i)
            for e in self.observation_table.em_vector:
                self._fill_table_entry(access_string + i, e)

Given a state input_string in Smi that is not equivalent with any state in Sm
this method will move that state in Sm create a corresponding Smi
state and fill the corresponding entries in the table.
Args:
access_string (str): State access string
Returns:
None


def rpc(self, address, rpc_id, *args, **kwargs):
    if isinstance(rpc_id, RPCDeclaration):
        arg_format = rpc_id.arg_format
        resp_format = rpc_id.resp_format
        rpc_id = rpc_id.rpc_id
    else:
        arg_format = kwargs.get('arg_format', None)
        resp_format = kwargs.get('resp_format', None)
    arg_payload = b''
    if (arg_format is not None):
        arg_payload = pack_rpc_payload(arg_format, args)
    self._logger.debug('Sending rpc to %d:%04X, payload=%s', address, rpc_id, args)
    resp_payload = self.call_rpc(address, rpc_id, arg_payload)
    if (resp_format is None):
        return []
    resp = unpack_rpc_payload(resp_format, resp_payload)
    return resp

Immediately dispatch an RPC inside this EmulatedDevice.

This function is meant to be used for testing purposes as well as by
tiles inside a complex EmulatedDevice subclass that need to
communicate with each other.  It should only be called from the main
virtual device thread where start() was called from.

**Background workers may not call this method since it may cause them to deadlock.**

Args:
address (int): The address of the tile that has the RPC.
rpc_id (int): The 16-bit id of the rpc we want to call
*args: Any required arguments for the RPC as python objects.
**kwargs: Only two keyword arguments are supported:
- arg_format: A format specifier for the argument list
- result_format: A format specifier for the result

Returns:
list: A list of the decoded response members from the RPC.


def label(self, main_type, sub_type, unique_id, label, action='ADD', owner=None, params=None):

        params = params or {}

        if owner:
            params['owner'] = owner

        action = action.upper()

        if not sub_type:
            url = '/v2/{}/{}/securityLabels/{}'.format(main_type, unique_id, quote(label))
        else:
            url = '/v2/{}/{}/{}/securityLabels/{}'.format(
                main_type, sub_type, unique_id, quote(label)
            )

        if action == 'ADD':
            return self.tcex.session.post(url, params=params)

        if action == 'DELETE':
            return self.tcex.session.delete(url, params=params)

        if action == 'GET':
            return self.tcex.session.get(url, params=params)

        return None

Args:
owner:
main_type:
sub_type:
unique_id:
label:
action:
params:

Return:


def from_key_counter(cls, key, counter, alg):
    counter = _convert_to_state_tensor(counter)
    key = _convert_to_state_tensor(key)
    alg = random_ops_util.convert_alg_to_int(alg)
    counter.shape.assert_is_compatible_with([_get_state_size(alg) - 1])
    key.shape.assert_is_compatible_with([])
    key = array_ops.reshape(key, [1])
    state = array_ops.concat([counter, key], 0)
    return cls(state=state, alg=alg)

Creates a generator from a key and a counter.

This constructor only applies if the algorithm is a counter-based algorithm.
See method `key` for the meaning of "key" and "counter".

Args:
key: the key for the RNG, a scalar of type STATE_TYPE.
counter: a vector of dtype STATE_TYPE representing the initial counter for
the RNG, whose length is algorithm-specific.,
alg: the RNG algorithm. If None, it will be auto-selected. See
`__init__` for its possible values.

Returns:
The new generator.


def HandleBlockReceived(self, inventory):

        block = IOHelper.AsSerializableWithType(inventory, 'neo.Core.Block.Block')
        if not block:
            return

        blockhash = block.Hash.ToBytes()
        try:
            if blockhash in BC.Default().BlockRequests:
                BC.Default().BlockRequests.remove(blockhash)
        except KeyError:
            pass
        try:
            if blockhash in self.myblockrequests:

                self.heart_beat(HEARTBEAT_BLOCKS)
                self.myblockrequests.remove(blockhash)
        except KeyError:
            pass
        self.leader.InventoryReceived(block)

Process a Block inventory payload.

Args:
inventory (neo.Network.Inventory):


def plane_xz(size=(10, 10), resolution=(10, 10)) -> VAO:
    (sx, sz) = size
    (rx, rz) = resolution
    (dx, dz) = ((sx / rx), (sz / rz))
    (ox, oz) = (((- sx) / 2), ((- sz) / 2))

    def gen_pos():
        for z in range(rz):
            for x in range(rx):
                (yield (ox + (x * dx)))
                (yield 0)
                (yield (oz + (z * dz)))

    def gen_uv():
        for z in range(rz):
            for x in range(rx):
                (yield (x / (rx - 1)))
                (yield (1 - (z / (rz - 1))))

    def gen_normal():
        for _ in range((rx * rz)):
            (yield 0.0)
            (yield 1.0)
            (yield 0.0)

    def gen_index():
        for z in range((rz - 1)):
            for x in range((rx - 1)):
                (yield (((z * rz) + x) + 1))
                (yield ((z * rz) + x))
                (yield (((z * rz) + x) + rx))
                (yield (((z * rz) + x) + 1))
                (yield (((z * rz) + x) + rx))
                (yield ((((z * rz) + x) + rx) + 1))
    pos_data = numpy.fromiter(gen_pos(), dtype=numpy.float32)
    uv_data = numpy.fromiter(gen_uv(), dtype=numpy.float32)
    normal_data = numpy.fromiter(gen_normal(), dtype=numpy.float32)
    index_data = numpy.fromiter(gen_index(), dtype=numpy.uint32)
    vao = VAO('plane_xz', mode=moderngl.TRIANGLES)
    vao.buffer(pos_data, '3f', ['in_position'])
    vao.buffer(uv_data, '2f', ['in_uv'])
    vao.buffer(normal_data, '3f', ['in_normal'])
    vao.index_buffer(index_data, index_element_size=4)
    return vao

Generates a plane on the xz axis of a specific size and resolution.
Normals and texture coordinates are also included.

Args:
size: (x, y) tuple
resolution: (x, y) tuple

Returns:
A :py:class:`demosys.opengl.vao.VAO` instance


def clear_errors():

    data = []
    data.append(0x0B)
    data.append(BROADCAST_ID)
    data.append(RAM_WRITE_REQ)
    data.append(STATUS_ERROR_RAM)
    data.append(BYTE2)
    data.append(0x00)
    data.append(0x00)
    send_data(data)

Clears the errors register of all Herkulex servos

Args:
none


def _decorate_run_options_for_debug(self, run_options, debug_urls, debug_ops='DebugIdentity', node_name_regex_allowlist=None, op_type_regex_allowlist=None, tensor_dtype_regex_allowlist=None, tolerate_debug_op_creation_failures=False):
    run_options.output_partition_graphs = True
    debug_utils.watch_graph(run_options, self._sess.graph, debug_urls=debug_urls, debug_ops=debug_ops, node_name_regex_allowlist=node_name_regex_allowlist, op_type_regex_allowlist=op_type_regex_allowlist, tensor_dtype_regex_allowlist=tensor_dtype_regex_allowlist, tolerate_debug_op_creation_failures=tolerate_debug_op_creation_failures, reset_disk_byte_usage=self._run_call_count == 1 or self._is_disk_usage_reset_each_run())

Modify a RunOptions object for debug tensor watching.

Specifies request for outputting partition graphs. Adds
debug_tensor_watch_opts with proper debug URLs.

Args:
run_options: (RunOptions) the modified RunOptions object.
debug_urls: (list of str) debug URLs to be entered in run_options.
debug_tensor_watch_opts.
debug_ops: (str or list of str) debug op(s) to be used by the debugger.
node_name_regex_allowlist: Regular-expression allowlist for node
name.
op_type_regex_allowlist: Regular-expression allowlist for op type.
tensor_dtype_regex_allowlist: Regular-expression allowlist for tensor
dtype.
tolerate_debug_op_creation_failures: Whether debug op creation failures
are to be tolerated.


def parse_arguments(argv):

  parser = argparse.ArgumentParser(
      formatter_class=argparse.RawDescriptionHelpFormatter,
      description=textwrap.dedent())
  parser.add_argument('--cloud',
                      action='store_true',
                      help='Analysis will use cloud services.')
  parser.add_argument('--output',
                      metavar='DIR',
                      type=str,
                      required=True,
                      help='GCS or local folder')

  input_group = parser.add_argument_group(
      title='Data Source Parameters',
      description='schema is only needed if using --csv')

  input_group.add_argument('--csv',
                           metavar='FILE',
                           type=str,
                           required=False,
                           action='append',
                           help='Input CSV absolute file paths. May contain a '
                                'file pattern.')
  input_group.add_argument('--schema',
                           metavar='FILE',
                           type=str,
                           required=False,
                           help='Schema file path. Only required if using csv files')

  input_group.add_argument('--bigquery',
                           metavar='PROJECT_ID.DATASET.TABLE_NAME',
                           type=str,
                           required=False,
                           help=('Must be in the form project.dataset.table_name'))

  parser.add_argument('--features',
                      metavar='FILE',
                      type=str,
                      required=True,
                      help='Features file path')

  args = parser.parse_args(args=argv[1:])

  if args.cloud:
    if not args.output.startswith('gs:
      raise ValueError('--output must point to a location on GCS')
    if (args.csv and
       not all(x.startswith('gs:
      raise ValueError('--csv must point to a location on GCS')
    if args.schema and not args.schema.startswith('gs:
      raise ValueError('--schema must point to a location on GCS')

  if not args.cloud and args.bigquery:
    raise ValueError('--bigquery must be used with --cloud')

  if not ((args.bigquery and args.csv is None and
           args.schema is None) or
          (args.bigquery is None and args.csv and
           args.schema)):
    raise ValueError('either --csv and --schema must both'
                     ' be set or just --bigquery is set')

  return args

Parse command line arguments.

Args:
argv: list of command line arguments, including program name.

Returns:
An argparse Namespace object.

Raises:
ValueError: for bad parameters


def create_struct(name):

    sid = idc.GetStrucIdByName(name)
    if sid != idaapi.BADADDR:

        raise exceptions.SarkStructAlreadyExists("A struct names {!r} already exists.".format(name))

    sid = idc.AddStrucEx(-1, name, 0)
    if sid == idaapi.BADADDR:
        raise exceptions.SarkStructCreationFailed("Struct creation failed.")

    return sid

Create a structure.

Args:
name: The structure's name

Returns:
The sturct ID

Raises:
exceptions.SarkStructAlreadyExists: A struct with the same name already exists
exceptions.SarkCreationFailed:  Struct creation failed


def deal_with_changeset_stack_policy(self, fqn, stack_policy):

        if stack_policy:
            kwargs = generate_stack_policy_args(stack_policy)
            kwargs["StackName"] = fqn
            logger.debug("Setting stack policy on %s.", fqn)
            self.cloudformation.set_stack_policy(**kwargs)

Set a stack policy when using changesets.

ChangeSets don't allow you to set stack policies in the same call to
update them. This sets it before executing the changeset if the
stack policy is passed in.

Args:
stack_policy (:class:`stacker.providers.base.Template`): A template
object representing a stack policy.


def get_review(review_struct):

    review_fn = _resource_context("review.rst")

    with open(review_fn) as f:
        review = f.read()

    with NamedTemporaryFile(suffix=".png") as qr_file:
        url = pyqrcode.create(review_struct.internal_url)
        url.png(qr_file.name, scale=5)

        qr_file.flush()
        qr_file.seek(0)

        review = Template(review).substitute(
            content=review_struct.get_rst(),
            datum=time.strftime("%d.%m.%Y", time.localtime()),
            cas=time.strftime("%H:%M", time.localtime()),
            resources_path=RES_PATH,
            qr_path=qr_file.name,
        )

        return gen_pdf(
            review,
            open(_resource_context("review_style.json")).read(),
        )

Generate review from `review_struct`.

Args:
review_struct (obj): :class:`.GenerateReview` instance.

Returns:
obj: StringIO file instance containing PDF file.


def get_dataset_split(tmp_dir, split, use_control_set):
    if (not use_control_set):
        dataset_split = {problem.DatasetSplit.TRAIN: [f for f in tf.gfile.Glob(os.path.join(tmp_dir, 'train-novels*.txt'))], problem.DatasetSplit.EVAL: [os.path.join(tmp_dir, 'lambada_control_test_data_plain_text.txt')]}
    return dataset_split[split]

Gives the file paths with regards to the given split.

Args:
tmp_dir: temp directory
split: dataset split
use_control_set: uses control dataset if true.

Returns:
list of file paths.


def summarize_tensors(tensor_dict, tag=None):
    if (tag is None):
        tag = 'tensors/'
    for t_name in list(tensor_dict):
        t = tensor_dict[t_name]
        tf.summary.histogram((tag + t_name), t)

Summarize the tensors.

Args:
tensor_dict: a dictionary of tensors.
tag: name scope of the summary; defaults to tensors/.


def rename(self, container, name):

        url = self._url("/containers/{0}/rename", container)
        params = {'name': name}
        res = self._post(url, params=params)
        self._raise_for_status(res)

Rename a container. Similar to the ``docker rename`` command.

Args:
container (str): ID of the container to rename
name (str): New name for the container

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


def predict_array(self, arr):

        precompute = self.precompute
        self.precompute = False
        pred = super().predict_array(arr)
        self.precompute = precompute
        return pred

This over-ride is necessary because otherwise the learner method accesses the wrong model when it is called
with precompute set to true

Args:
arr: a numpy array to be used as input to the model for prediction purposes
Returns:
a numpy array containing the predictions from the model


def play_alert(zones, alert_uri, alert_volume=20, alert_duration=0, fade_back=False):
    for zone in zones:
        zone.snap = Snapshot(zone)
        zone.snap.snapshot()
        print('snapshot of zone: {}'.format(zone.player_name))
    for zone in zones:
        if zone.is_coordinator:
            if (not zone.is_playing_tv):
                trans_state = zone.get_current_transport_info()
                if (trans_state['current_transport_state'] == 'PLAYING'):
                    zone.pause()
        zone.volume = alert_volume
        zone.mute = False
    print('will play: {} on all coordinators'.format(alert_uri))
    for zone in zones:
        if zone.is_coordinator:
            zone.play_uri(uri=alert_uri, title='Sonos Alert')
    time.sleep(alert_duration)
    for zone in zones:
        print('restoring {}'.format(zone.player_name))
        zone.snap.restore(fade=fade_back)

Demo function using soco.snapshot across multiple Sonos players.

Args:
zones (set): a set of SoCo objects
alert_uri (str): uri that Sonos can play as an alert
alert_volume (int): volume level for playing alert (0 tp 100)
alert_duration (int): length of alert (if zero then length of track)
fade_back (bool): on reinstating the zones fade up the sound?


def result(self):
    self.wait()
    if self._fatal_error:
        raise self._fatal_error
    return self._result

Get the result for a job. This will block if the job is incomplete.

Returns:
The result for the Job.

Raises:
An exception if the Job resulted in an exception.


def _GeneratePathString(self, mediator, pathspec, hashes):
    display_name = mediator.GetDisplayNameForPathSpec(pathspec)
    path_string = '{0:s}:'.format(display_name)
    for (hash_name, hash_value) in sorted(hashes.items()):
        path_string = '{0:s} {1:s}={2:s}'.format(path_string, hash_name, hash_value)
    return path_string

Generates a string containing a pathspec and its hashes.

Args:
mediator (AnalysisMediator): mediates interactions between analysis
plugins and other components, such as storage and dfvfs.
pathspec (dfvfs.Pathspec): the path specification) to generate a string
for.
hashes (dict[str, str]): mapping of hash attribute names to the value of
that hash for the path specification being processed.

Returns:
str: string of the form "display_name: hash_type=hash_value". For example,
"OS:/path/spec: test_hash=4 other_hash=5".


def describe_enum(enum_definition):
    enum_descriptor = EnumDescriptor()
    enum_descriptor.name = enum_definition.definition_name().split('.')[(- 1)]
    values = []
    for number in enum_definition.numbers():
        value = enum_definition.lookup_by_number(number)
        values.append(describe_enum_value(value))
    if values:
        enum_descriptor.values = values
    return enum_descriptor

Build descriptor for Enum class.

Args:
enum_definition: Enum class to provide descriptor for.

Returns:
Initialized EnumDescriptor instance describing the Enum class.


def import_mapping(connection_id, mapping):

    url = os.path.join(settings.HEROKU_CONNECT_API_ENDPOINT,
                       'connections', connection_id, 'actions', 'import')

    response = requests.post(
        url=url,
        json=mapping,
        headers=_get_authorization_headers()
    )
    response.raise_for_status()

Import Heroku Connection mapping for given connection.

Args:
connection_id (str): Heroku Connection connection ID.
mapping (dict): Heroku Connect mapping.

Raises:
requests.HTTPError: If an error occurs uploading the mapping.
ValueError: If the mapping is not JSON serializable.


def GetFeedMapping(client, feed, placeholder_type):
    feed_mapping_service = client.GetService('FeedMappingService', 'v201809')
    attribute_mappings = {}
    more_pages = True
    selector = {'fields': ['FeedMappingId', 'AttributeFieldMappings'], 'predicates': [{'field': 'FeedId', 'operator': 'EQUALS', 'values': [feed['id']]}, {'field': 'PlaceholderType', 'operator': 'EQUALS', 'values': [placeholder_type]}], 'paging': {'startIndex': 0, 'numberResults': PAGE_SIZE}}
    while more_pages:
        page = feed_mapping_service.get(selector)
        if ('entries' in page):
            for feed_mapping in page['entries']:
                for attribute_mapping in feed_mapping['attributeFieldMappings']:
                    if (attribute_mapping['feedAttributeId'] in attribute_mappings):
                        attribute_mappings[attribute_mapping['feedAttributeId']].append(attribute_mapping['fieldId'])
                    else:
                        attribute_mappings[attribute_mapping['feedAttributeId']] = [attribute_mapping['fieldId']]
        selector['paging']['startIndex'] += PAGE_SIZE
        more_pages = (selector['paging']['startIndex'] < int(page['totalNumEntries']))
    return attribute_mappings

Gets the Feed Mapping for a given Feed.

Args:
client: an AdWordsClient instance.
feed: the Feed we are retrieving the Feed Mapping for.
placeholder_type: the Placeholder Type we are looking for.
Returns:
A dictionary containing the Feed Mapping.


def _calculate_hash(files, root):

    file_hash = hashlib.md5()
    for fname in sorted(files):
        f = os.path.join(root, fname)
        file_hash.update((fname + "\0").encode())
        with open(f, "rb") as fd:
            for chunk in iter(lambda: fd.read(4096), ""):
                if not chunk:
                    break
                file_hash.update(chunk)
            file_hash.update("\0".encode())

    return file_hash.hexdigest()

Returns a hash of all of the given files at the given root.

Args:
files (list[str]): file names to include in the hash calculation,
relative to ``root``.
root (str): base directory to analyze files in.

Returns:
str: A hash of the hashes of the given files.


def _init_boto3_clients(self):

        try:
            profile = self._config.get('environment', {}).get('profile')
            region = self._config.get('environment', {}).get('region')
            if profile:
                self._b3Sess = boto3.session.Session(profile_name=profile)
            else:
                self._b3Sess = boto3.session.Session()

            self._s3 = self._b3Sess.client('s3')
            self._cloudFormation = self._b3Sess.client('cloudformation', region_name=region)
            self._ssm = self._b3Sess.client('ssm', region_name=region)

            return True
        except Exception as wtf:
            logging.error('Exception caught in intialize_session(): {}'.format(wtf))
            traceback.print_exc(file=sys.stdout)
            return False

The utililty requires boto3 clients to Cloud Formation and S3. Here is
where we make them.

Args:
None

Returns:
Good or Bad; True or False


def from_json(cls, data):
    optional_keys = ('city', 'state', 'country', 'latitude', 'longitude', 'time_zone', 'elevation', 'station_id', 'source')
    for key in optional_keys:
        if (key not in data):
            data[key] = None
    return cls(data['city'], data['state'], data['country'], data['latitude'], data['longitude'], data['time_zone'], data['elevation'], data['station_id'], data['source'])

Create a location from a dictionary.

Args:
data: {
"city": "-",
"latitude": 0,
"longitude": 0,
"time_zone": 0,
"elevation": 0}


def _fdopen_ver2(self, file_des, mode='r', bufsize=None):
    if (not is_int_type(file_des)):
        raise TypeError('an integer is required')
    try:
        return FakeFileOpen(self.filesystem).call(file_des, mode=mode)
    except IOError as exc:
        self.filesystem.raise_os_error(exc.errno, exc.filename)

Returns an open file object connected to the file descriptor
file_des.

Args:
file_des: An integer file descriptor for the file object requested.
mode: Additional file flags. Currently checks to see if the mode
matches the mode of the requested file object.
bufsize: ignored. (Used for signature compliance with
__builtin__.fdopen)

Returns:
File object corresponding to file_des.

Raises:
OSError: if bad file descriptor or incompatible mode is given.
TypeError: if file descriptor is not an integer.


def AddServiceDescriptor(self, service_desc):

    if not isinstance(service_desc, descriptor.ServiceDescriptor):
      raise TypeError('Expected instance of descriptor.ServiceDescriptor.')

    self._service_descriptors[service_desc.full_name] = service_desc

Adds a ServiceDescriptor to the pool.

Args:
service_desc: A ServiceDescriptor.


def __resource_descriptor(self, resource_path, methods):

    descriptor = {}
    method_map = {}
    sub_resource_index = collections.defaultdict(list)
    sub_resource_map = {}

    resource_path_tokens = resource_path.split('.')
    for service, protorpc_meth_info in methods:
      method_info = getattr(protorpc_meth_info, 'method_info', None)
      path = method_info.get_path(service.api_info)
      method_id = method_info.method_id(service.api_info)
      canonical_method_id = self._get_canonical_method_id(method_id)

      current_resource_path = self._get_resource_path(method_id)

      if (current_resource_path[:len(resource_path_tokens)] !=
          resource_path_tokens):
        raise api_exceptions.ToolError(
            'Internal consistency error in resource path {0}'.format(
                current_resource_path))

      effective_resource_path = current_resource_path[
          len(resource_path_tokens):]

      if effective_resource_path:
        sub_resource_name = effective_resource_path[0]
        new_resource_path = '.'.join([resource_path, sub_resource_name])
        sub_resource_index[new_resource_path].append(
            (service, protorpc_meth_info))
      else:
        method_map[canonical_method_id] = self.__method_descriptor(
            service, method_info, protorpc_meth_info)

    for sub_resource, sub_resource_methods in sub_resource_index.items():
      sub_resource_name = sub_resource.split('.')[-1]
      sub_resource_map[sub_resource_name] = self.__resource_descriptor(
          sub_resource, sub_resource_methods)

    if method_map:
      descriptor['methods'] = method_map

    if sub_resource_map:
      descriptor['resources'] = sub_resource_map

    return descriptor

Describes a resource.

Args:
resource_path: string, the path of the resource (e.g., 'entries.items')
methods: list of tuples of type
(endpoints.Service, protorpc.remote._RemoteMethodInfo), the methods
that serve this resource.

Returns:
Dictionary describing the resource.


def PrepareMatches(self, file_system):

    if self._location is not None:
      self._location_segments = self._SplitPath(
          self._location, file_system.PATH_SEPARATOR)

    elif self._location_regex is not None:
      path_separator = file_system.PATH_SEPARATOR
      if path_separator == '\\':

        path_separator = '\\\\'

      self._location_segments = self._SplitPath(
          self._location_regex, path_separator)

    if self._location_segments is not None:
      self._number_of_location_segments = len(self._location_segments)

Prepare find specification for matching.

Args:
file_system (FileSystem): file system.


def _ParseAndValidateRecord(self, parser_mediator, text_file_object):

    try:
      title = text_file_object.readline(size=self._MAXIMUM_LINE_SIZE)
      url = text_file_object.readline(size=self._MAXIMUM_LINE_SIZE)
      timestamp = text_file_object.readline(size=self._MAXIMUM_LINE_SIZE)
      popularity_index = text_file_object.readline(size=self._MAXIMUM_LINE_SIZE)
    except UnicodeDecodeError:
      return False

    if len(title) == self._MAXIMUM_LINE_SIZE and title[-1] != '\n':
      return False

    if len(url) == self._MAXIMUM_LINE_SIZE and url[-1] != '\n':
      return False

    if len(timestamp) == self._MAXIMUM_LINE_SIZE and timestamp[-1] != '\n':
      return False

    if (len(popularity_index) == self._MAXIMUM_LINE_SIZE and
        popularity_index[-1] != '\n'):
      return False

    title = title.strip()
    url = url.strip()
    timestamp = timestamp.strip()
    popularity_index = popularity_index.strip()

    if not title or not url or not timestamp or not popularity_index:
      return False

    event_data = OperaGlobalHistoryEventData()

    if not self._IsValidUrl(url):
      return False

    event_data.url = url
    if title != url:
      event_data.title = title

    try:
      event_data.popularity_index = int(popularity_index, 10)
      timestamp = int(timestamp, 10)
    except ValueError:
      return False

    if event_data.popularity_index < 0:
      event_data.description = 'First and Only Visit'
    else:
      event_data.description = 'Last Visit'

    date_time = dfdatetime_posix_time.PosixTime(timestamp=timestamp)
    event = time_events.DateTimeValuesEvent(
        date_time, definitions.TIME_DESCRIPTION_LAST_VISITED)
    parser_mediator.ProduceEventWithEventData(event, event_data)

    return True

Parses and validates an Opera global history record.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
text_file_object (dfvfs.TextFile): text file.

Returns:
bool: True if the record was successfully parsed.


def cleanup(context):
    for name in ('work_dir', 'artifact_dir', 'task_log_dir'):
        path = context.config[name]
        if os.path.exists(path):
            log.debug('rm({})'.format(path))
            rm(path)
        makedirs(path)

Clean up the work_dir and artifact_dir between task runs, then recreate.

Args:
context (scriptworker.context.Context): the scriptworker context.


def vr60baro(msg):

    d = hex2bin(data(msg))

    if d[34] == '0':
        return None

    sign = int(d[35])
    value = bin2int(d[36:45])

    if value == 0 or value == 511:
        return 0

    value = value - 512 if sign else value

    roc = value * 32
    return roc

Vertical rate from barometric measurement, this value may be very noisy.

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

Returns:
int: vertical rate in feet/minutes


def memory_zones(self):

        count = self.num_memory_zones()
        if count == 0:
            return list()

        buf = (structs.JLinkMemoryZone * count)()
        res = self._dll.JLINK_GetMemZones(buf, count)
        if res < 0:
            raise errors.JLinkException(res)

        return list(buf)

Gets all memory zones supported by the current target.

Some targets support multiple memory zones.  This function provides the
ability to get a list of all the memory zones to facilate using the
memory zone routing functions.

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

Returns:
A list of all the memory zones as ``JLinkMemoryZone`` structures.

Raises:
JLinkException: on hardware errors.


def create_ltp_package(aleph_record, book_id, ebook_fn, data, url, urn_nbn=None):
    (root_dir, orig_dir, meta_dir) = _create_package_hierarchy(book_id=book_id)
    original_fn = os.path.join(orig_dir, fn_composers.original_fn(book_id, ebook_fn))
    with open(original_fn, 'wb') as f:
        f.write(data)
    metadata_filenames = []
    records = marcxml2mods(marc_xml=aleph_record, uuid=book_id, url=url)
    for (cnt, mods_record) in enumerate(records):
        fn = os.path.join(meta_dir, fn_composers.volume_fn(cnt))
        with open(fn, 'w') as f:
            f.write(mods_record)
        metadata_filenames.append(fn)
    md5_fn = os.path.join(root_dir, fn_composers.checksum_fn(book_id))
    checksums = checksum_generator.generate_hashfile(root_dir)
    with open(md5_fn, 'w') as f:
        f.write(checksums)
    info_fn = os.path.join(root_dir, fn_composers.info_fn(book_id))
    with open(info_fn, 'w') as f:
        f.write(info_composer.compose_info(root_dir=root_dir, files=([original_fn] + metadata_filenames), hash_fn=md5_fn, aleph_record=aleph_record, urn_nbn=urn_nbn))
    return root_dir

Create LTP package as it is specified in specification v1.0 as I understand
it.

Args:
aleph_record (str): XML containing full aleph record.
book_id (str): UUID of the book.
ebook_fn (str): Original filename of the ebook.
data (str/bytes): Ebook's content.
url (str): URL of the publication used when the URL can't be found in
`aleph_record`.
urn_nbn (str, default None): URN:NBN.

Returns:
str: Name of the package's directory in ``/tmp``.


def identify(text):
    filtered_text = set(list(text)).intersection(ALL_CHARS)
    if (len(filtered_text) is 0):
        return None
    if filtered_text.issubset(SHARED_CHARS):
        return EITHER
    if filtered_text.issubset(TRAD_CHARS):
        return TRAD
    if filtered_text.issubset(SIMP_CHARS):
        return SIMP
    if filtered_text.difference(TRAD_CHARS).issubset(SIMP_CHARS):
        return BOTH

Identify whether a string is simplified or traditional Chinese.

Returns:
None: if there are no recognizd Chinese characters.
EITHER: if the test is inconclusive.
TRAD: if the text is traditional.
SIMP: if the text is simplified.
BOTH: the text has characters recognized as being solely traditional
and other characters recognized as being solely simplified.


def test_noninlined_funcdef(self, mode):
    self._maybe_skip(mode)
    with ops.device(_get_device(mode)):
        random_seed.set_random_seed(0)
        x = _input([8, 8])
        y = _matmul_act(x)
        y = _example_noninlined_funcdef(y)
        optimizer = gradient_descent.GradientDescentOptimizer(learning_rate=0.01)
        g = optimizer.compute_gradients(y, [x])
        output = (g, y)
    output_val_ref, output_val, cost_graph = self._run(mode, output)
    node_map = _build_node_map(cost_graph.node)
    self._assert_output_f16(mode, node_map, 'MatMul')
    tol = 0.01 if mode == 'mkl' else 0.001
    atol = 0.01 if test.is_built_with_rocm() else tol
    self.assertAllClose(output_val_ref, output_val, atol=atol, rtol=tol)

Test graph with non-inlined function subgraph.

This requires the grappler pass to handle an OpDef that only appears in the
graph's function registry instead of the global op registry.

Args:
mode: Either 'cuda' or 'mkl'.


def run_function_on_all_workers(self, function, run_on_other_drivers=False):
    if (self.mode is None):
        self.cached_functions_to_run.append(function)
    else:
        pickled_function = pickle.dumps(function)
        function_to_run_id = hashlib.sha1(pickled_function).digest()
        key = (b'FunctionsToRun:' + function_to_run_id)
        function({'worker': self})
        function_exported = self.redis_client.setnx((b'Lock:' + key), 1)
        if (not function_exported):
            return
        check_oversized_pickle(pickled_function, function.__name__, 'function', self)
        self.redis_client.hmset(key, {'driver_id': self.task_driver_id.binary(), 'function_id': function_to_run_id, 'function': pickled_function, 'run_on_other_drivers': str(run_on_other_drivers)})
        self.redis_client.rpush('Exports', key)

Run arbitrary code on all of the workers.

This function will first be run on the driver, and then it will be
exported to all of the workers to be run. It will also be run on any
new workers that register later. If ray.init has not been called yet,
then cache the function and export it later.

Args:
function (Callable): The function to run on all of the workers. It
takes only one argument, a worker info dict. If it returns
anything, its return values will not be used.
run_on_other_drivers: The boolean that indicates whether we want to
run this function on other drivers. One case is we may need to
share objects across drivers.


def scrape(self, url):

        if isinstance(url, str) is False:
            raise TypeError("The type of url must be str.")

        if self.readable_web_pdf is not None and self.readable_web_pdf.is_pdf_url(url) is True:
            web_data = self.readable_web_pdf.url_to_text(url)
        else:
            web_data = ""
            req = urllib.request.Request(url=url)
            with urllib.request.urlopen(req) as f:
                web = f.read().decode('utf-8')
                dom = pq(web)
                [dom(remove_object).remove() for remove_object in self.__remove_object_list]

                for dom_object in self.__dom_object_list:
                    web_data += dom(dom_object).text()

        sleep(1)
        return web_data

Execute Web-Scraping.
The target dom objects are in self.__dom_object_list.

Args:
url:    Web site url.

Returns:
The result. this is a string.

@TODO(chimera0): check URLs format.


def _normalize_field_name(self, field_name) -> str:

        if isinstance(field_name, tuple):
            field_name, _ = field_name

        return field_name

Normalizes a field name into a string by
extracting the field name if it was specified
as a reference to a HStore key (as a tuple).

Arguments:
field_name:
The field name to normalize.

Returns:
The normalized field name.


def model_fn(self, x: core.Tensor) -> Mapping[str, core.Tensor]:
    if math_ops.reduce_sum(x) > 10.0:
        out = math_ops.matmul(x, self.filters_0)
        out = nn_ops.bias_add(out, self.bias_0)
        return {'output': out}
    out = math_ops.matmul(x, self.filters_1)
    out = nn_ops.bias_add(out, self.bias_1)
    return {'output': out}

Runs the input tensor to a branched operations.

The graph is branched by a condition whether the sum of elements of `x`
is greater than 10.

Args:
x: Input tensor.

Returns:
A map of: output key -> output result.


def sample_from_likelihood(self, n_timesteps=10):

        self.latent_state_sequences = lmap(
            lambda A: ltake(
                n_timesteps,
                iterate(
                    lambda s: pd.Series(A @ s.values, index=s.index), self.s0
                ),
            ),
            self.transition_matrix_collection,
        )

        self.observed_state_sequences = [
            [self.sample_observed_state(s) for s in latent_state_sequence]
            for latent_state_sequence in self.latent_state_sequences
        ]

Sample a collection of observed state sequences from the likelihood
model given a collection of transition matrices.

Args:
n_timesteps: The number of timesteps for the sequences.


def __init__(self, request, async, callback=None, callbacks=dict(), root_object=None):

        self._uses_authentication = True
        self._has_timeouted = False

        self._ignore_request_idle = False
        self._xhr_timeout = 3000
        self._response = None
        self._error_message = None
        self._transaction_id = uuid.uuid4().hex

        self._request = request
        self._async = async
        self._callback = callback
        self._callbacks = callbacks
        self._user_info = None
        self._object_last_action_timer = None
        self._root_object = root_object

Intializes a new connection for a given request

NURESTConnection object is in charge of the HTTP call. It relies on request library

Args:
request: the NURESTRequest to send
callback: the method that will be fired after sending
callbacks: a dictionary of user callbacks. Should contains local and remote callbacks


def get_sendback(self, uuid, key):

        def send_back_callback(data):
            self.sendResponse(
                serializers.serialize(data),
                uuid,
                key
            )

        return send_back_callback

Return function for sending progress messages back to original caller.

Args:
uuid (str): UUID of the received message.
key (str): Routing key.

Returns:
fn reference: Reference to function which takes only one data \
argument.


def get_capture_handler_config_by_name(self, name):
    handler_confs = []
    for (address, stream_capturer) in self._stream_capturers.iteritems():
        handler_data = stream_capturer[0].dump_handler_config_data()
        for h in handler_data:
            if (h['handler']['name'] == name):
                handler_confs.append(h)
    return handler_confs

Return data for handlers of a given name.

Args:
name:
Name of the capture handler(s) to return config data for.

Returns:
Dictionary dump from the named capture handler as given by
the :func:`SocketStreamCapturer.dump_handler_config_data` method.


def __init__(self, file_handle):
    if not file_handle.writable():
        raise ValueError('Output stream must be writable')
    self._file_handle = file_handle
    self._coder = RowAsDictJsonCoder()

Initialize an JsonRowWriter.

Args:
file_handle (io.IOBase): Output stream to write to.


def precision(truth, recommend, k=None):

    if len(recommend) == 0:
        if len(truth) == 0:
            return 1.
        return 0.

    if k is None:
        k = len(recommend)
    return count_true_positive(truth, recommend[:k]) / float(k)

Precision@k.

Args:
truth (numpy 1d array): Set of truth samples.
recommend (numpy 1d array): Ordered set of recommended samples.
k (int): Top-k items in `recommend` will be recommended.

Returns:
float: Precision@k.


def _validate_isvalid_orcid(self, isvalid_orcid, field, value):

        if isvalid_orcid and 'ORCID' in value:
            try:
                res = search_orcid(value['ORCID'])
            except ConnectionError:
                warn('network not available, ORCID not validated.')
                return
            except HTTPError:
                self._error(field, 'ORCID incorrect or invalid for ' +
                            value['name']
                            )
                return

            family_name = res['name']['family-name']['value']
            given_name = res['name']['given-names']['value']
            if not compare_name(given_name, family_name, value['name']):
                self._error(field, 'Name and ORCID do not match. Name supplied: ' +
                            value['name'] + '. Name associated with ORCID: ' +
                            ' '.join([given_name, family_name])
                            )

Checks for valid ORCID if given.

Args:
isvalid_orcid (`bool`): flag from schema indicating ORCID to be checked.
field (`str`): 'author'
value (`dict`): dictionary of author metadata.

The rule's arguments are validated against this schema:
{'isvalid_orcid': {'type': 'bool'}, 'field': {'type': 'str'},
'value': {'type': 'dict'}}


def direct_normal_illuminance(self, value=999999.0):
    if (value is not None):
        try:
            value = float(value)
        except ValueError:
            raise ValueError('value {} need to be of type float for field `direct_normal_illuminance`'.format(value))
        if (value < 0.0):
            raise ValueError('value need to be greater or equal 0.0 for field `direct_normal_illuminance`')
    self._direct_normal_illuminance = value

Corresponds to IDD Field `direct_normal_illuminance`
will be missing if >= 999900

Args:
value (float): value for IDD Field `direct_normal_illuminance`
Unit: lux
value >= 0.0
Missing value: 999999.0
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 run(self, xml, **kwargs):
    kwargs['output'] = self.__graph__()
    if isinstance(xml, str):
        try:
            self.source = etree.XML(xml)
        except ValueError:
            try:
                self.source = etree.XML(xml.encode())
            except:
                raise ValueError('Cannot run error {}'.format(sys.exc_info()[0]))
    else:
        self.source = xml
    super(XMLProcessor, self).run(**kwargs)
    self.output = kwargs['output']
    return kwargs['output']

Method takes either an etree.ElementTree or raw XML text
as the first argument.

Args:
xml(etree.ElementTree or text


def db_insert_record(self, table_name, columns):

        bindings = ('?,' * len(columns)).strip(',')
        values = [None] * len(columns)
        sql = 'INSERT INTO {} ({}) VALUES ({})'.format(table_name, ', '.join(columns), bindings)
        cur = self.db_conn.cursor()
        cur.execute(sql, values)

Insert records into DB.

Args:
table_name (str): The name of the table.
columns (list): List of columns for insert statement.


def __init__(self, message=None, host=None):

        self.message = message
        self.hostname = str(host) if host else None

Initialize the GeneralError object.

Args:
message (str): Custom message to be passed to the exceptions. Defaults to *None*.
If *None* then the general class *__doc__* is used.
host (str): Custom string which can be used to enhance the exception message by adding the "`host`: "
prefix to the message string. Defaults to *None*. If `host` is *None* then message stays unchanged.


async def getTempCortex(mods=None):

    with s_common.getTempDir() as dirn:

        async with await Cortex.anit(dirn) as core:
            if mods:
                for mod in mods:
                    await core.loadCoreModule(mod)
            async with core.getLocalProxy() as prox:
                yield prox

Get a proxy to a cortex backed by a temporary directory.

Args:
mods (list): A list of modules which are loaded into the cortex.

Notes:
The cortex and temporary directory are town down on exit.
This should only be called from synchronous code.

Returns:
Proxy to the cortex.


def RegisterDecoder(cls, decoder):
    encoding_method = decoder.ENCODING_METHOD.lower()
    if (encoding_method in cls._decoders):
        raise KeyError('Decoder for encoding method: {0:s} already set.'.format(decoder.ENCODING_METHOD))
    cls._decoders[encoding_method] = decoder

Registers a decoder for a specific encoding method.

Args:
decoder (type): decoder class.

Raises:
KeyError: if the corresponding decoder is already set.


def _add_parameters(self, parameter_map, parameter_list):
    for parameter in parameter_list:
        if parameter.get('$ref'):
            parameter = self.specification['parameters'].get(parameter.get('$ref').split('/')[(- 1)])
        parameter_map[parameter['name']] = parameter

Populates the given parameter map with the list of parameters provided, resolving any reference objects encountered.

Args:
parameter_map: mapping from parameter names to parameter objects
parameter_list: list of either parameter objects or reference objects


def get_ccc_handle_from_uuid(self, uuid):

        if uuid in self.uuid_cccds:
            return self.uuid_cccds[uuid].handle

        char = self.get_characteristic_from_uuid(uuid)
        if char is None:
            return None

        ccc = char.get_descriptor_by_uuid(UUID_GATT_CCC)
        if ccc is not None:
            self.uuid_cccds[uuid] = ccc
        return None if ccc is None else ccc.handle

Utility function to retrieve the client characteristic configuration
descriptor handle for a given characteristic.

Args:
uuid (str): a string containing the hex-encoded UUID

Returns:
None if an error occurs, otherwise an integer handle.


def _GetElementDataTypeDefinition(self, data_type_definition):

    if not data_type_definition:
      raise errors.FormatError('Missing data type definition')

    element_data_type_definition = getattr(
        data_type_definition, 'element_data_type_definition', None)
    if not element_data_type_definition:
      raise errors.FormatError(
          'Invalid data type definition missing element')

    return element_data_type_definition

Retrieves the element data type definition.

Args:
data_type_definition (DataTypeDefinition): data type definition.

Returns:
DataTypeDefinition: element data type definition.

Raises:
FormatError: if the element data type cannot be determined from the data
type definition.


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

Leases a dataflow WorkItem to run.

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


def get_pending_computer_name():
    current = get_computer_name()
    pending = __utils__['reg.read_value']('HKLM', 'SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters', 'NV Hostname')['vdata']
    if pending:
        return (pending if (pending != current) else None)
    return False

Get a pending computer name. If the computer name has been changed, and the
change is pending a system reboot, this function will return the pending
computer name. Otherwise, ``None`` will be returned. If there was an error
retrieving the pending computer name, ``False`` will be returned, and an
error message will be logged to the minion log.

Returns:
str:
Returns the pending name if pending restart. Returns ``None`` if not
pending restart.

CLI Example:

.. code-block:: bash

salt 'minion-id' system.get_pending_computer_name


def configure_and_build(self, show_progress=True, optimized=True,
                            skip_configuration=False):

        if not skip_configuration:
            configuration_command = ['python', 'waf', 'configure', '--enable-examples',
                                     '--disable-gtk', '--disable-python']

            if optimized:
                configuration_command += ['--build-profile=optimized',
                                          '--out=build/optimized']

            subprocess.call(configuration_command, cwd=self.path,
                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        build_process = subprocess.Popen(['python', 'waf', 'build'], cwd=self.path,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE)

        if show_progress:
            line_iterator = self.get_build_output(build_process)
            pbar = None
            try:
                [initial, total] = next(line_iterator)
                pbar = tqdm(line_iterator, initial=initial, total=total,
                            unit='file', desc='Building ns-3', smoothing=0)
                for current, total in pbar:
                    pbar.n = current
            except (StopIteration):
                if pbar is not None:
                    pbar.n = pbar.total
        else:
            build_process.communicate()

Configure and build the ns-3 code.

Args:
show_progress (bool): whether or not to display a progress bar
during compilation.
optimized (bool): whether to use an optimized build. If False, use
a standard ./waf configure.
skip_configuration (bool): whether to skip the configuration step,
and only perform compilation.


def get_security_group_id(name='', env='', region=''):
    vpc_id = get_vpc_id(env, region)
    LOG.info('Find %s sg in %s [%s] in %s', name, env, region, vpc_id)
    url = '{0}/securityGroups/{1}/{2}/{3}?vpcId={4}'.format(API_URL, env, region, name, vpc_id)
    response = requests.get(url, verify=GATE_CA_BUNDLE, cert=GATE_CLIENT_CERT)
    assert response.ok
    result = response.json()
    try:
        security_group_id = result['id']
    except KeyError:
        msg = 'Security group ({0}) not found'.format(name)
        raise SpinnakerSecurityGroupError(msg)
    LOG.info('Found: %s', security_group_id)
    return security_group_id

Get a security group ID.

Args:
name (str): Security Group name to find.
env (str): Deployment environment to search.
region (str): AWS Region to search.

Returns:
str: ID of Security Group, e.g. sg-xxxx.

Raises:
AssertionError: Call to Gate API was not successful.
SpinnakerSecurityGroupError: Security Group _name_ was not found for
_env_ in _region_.


def run_bottleneck_on_image(sess, image_data, image_data_tensor, decoded_image_tensor, resized_input_tensor, bottleneck_tensor):
    resized_input_values = sess.run(decoded_image_tensor, {image_data_tensor: image_data})
    bottleneck_values = sess.run(bottleneck_tensor, {resized_input_tensor: resized_input_values})
    bottleneck_values = np.squeeze(bottleneck_values)
    return bottleneck_values

Runs inference on an image to extract the 'bottleneck' summary layer.

Args:
sess: Current active TensorFlow Session.
image_data: String of raw JPEG data.
image_data_tensor: Input data layer in the graph.
decoded_image_tensor: Output of initial image resizing and preprocessing.
resized_input_tensor: The input node of the recognition graph.
bottleneck_tensor: Layer before the final softmax.

Returns:
Numpy array of bottleneck values.


def supervised_to_dict(dataset, text2self):

    def my_fn(inputs, targets):
        if text2self:
            return {'targets': targets}
        else:
            return {'inputs': inputs, 'targets': targets}
    return dataset.map(my_fn, num_parallel_calls=tf.data.experimental.AUTOTUNE)

Turns a supervised dataset into a dataset with a feature dictionary.

if text2self, then the features dictionary contains a "targets" key.
else, the features dictionary contains "inputs" and "targets" keys.

Args:
dataset: a tf.data.Dataset
text2self: a boolean
Returns:
a tf.data.Dataset


def process_equities(equities: List[str], mask: types.IntTensor=None) -> Tuple[List[str], List[int]]:
    equity_list = cashflow_streams.to_list(equities)
    if mask is not None:
        return (equity_list, mask)
    mask, mask_map, num_unique_equities = cashflow_streams.create_mask(equity_list)
    equity_types = [mask_map[i] for i in range(num_unique_equities)]
    return (equity_types, mask)

Extracts unique equities and computes an integer mask.

#### Example

```python
process_equities(["GOOG", "MSFT", "GOOG", "GOOG"])
# Returns
(['GOOG', 'MSFT'], [0, 1, 0, 0])
```

Args:
equities: A list of equity names.
mask: An optional integer mask for the sorted equity sequence. If supplied,
becomes a no-op.

Returns:
A Tuple of `(equities, mask)` where  `equities` is a list of unique sorted
equities and `mask` is a list of integers which is the mask for `equities`.


def mark_typed_object(self, name, type_object):
    if (not hasattr(type_object, 'dump')):
        raise ArgumentError(('The passed type object %s is missing required method: dump()' % type_object))
    if (not hasattr(type_object, 'Restore')):
        raise ArgumentError(('The passed type object %s is missing required method: Restore()' % type_object))

    def _dump_obj(obj):
        if (obj is None):
            return None
        return obj.dump()

    def _restore_obj(obj):
        if (obj is None):
            return obj
        return type_object.Restore(obj)
    self.mark_complex(name, _dump_obj, _restore_obj)

Mark a property as containing a serializable object.

This convenience method allows you to avoid having to call
``mark_complex()`` whenever you need to serialize a complex object.
This method requires that property ``name`` be a single class that
contains a dump() method and a Restore() class method where
type_object.Restore(x.dump()) == x.

Args:
name (str): The name of the complex property.
type_object: The class object that will be contained inside
this property.


def update_hash(src_file):
    hash_file = (local.path(src_file) + '.hash')
    new_hash = 0
    with open(hash_file, 'w') as h_file:
        new_hash = get_hash_of_dirs(src_file)
        h_file.write(str(new_hash))
    return new_hash

Update the hash for the given file.

Args:
src: The file name.
root: The path of the given file.


def _stream_data(self, chunk=None):
    self._stream_sm_running = True
    if (chunk is None):
        chunk = self._next_streaming_chunk(20)
    if ((chunk is None) or (len(chunk) == 0)):
        self._stream_sm_running = False
        return
    try:
        self._send_notification(StreamingChar.value_handle, chunk)
        self._defer(self._stream_data)
    except bable_interface.BaBLEException as err:
        if (err.packet.status == 'Rejected'):
            time.sleep(0.05)
            self._defer(self._stream_data, [chunk])
        else:
            self._audit('ErrorStreamingReport')
            self._logger.exception('Error while streaming data')

Stream reports to the ble client in 20 byte chunks

Args:
chunk (bytearray): A chunk that should be sent instead of requesting a
new chunk from the pending reports.


def get_commits(self, since_sha=None):
    assert self.tempdir
    cmd = ['git', 'log', '--first-parent', '--reverse', COMMIT_FORMAT]
    if since_sha:
        commits = [self.get_commit(since_sha)]
        cmd.append('{}..HEAD'.format(since_sha))
    else:
        commits = []
        cmd.append('HEAD')
    output = cmd_output(*cmd, cwd=self.tempdir)
    for (sha, date) in chunk_iter(output.splitlines(), 2):
        commits.append(Commit(sha, int(date)))
    return commits

Returns a list of Commit objects.

Args:
since_sha - (optional) A sha to search from


def normalize(model: typing.Dict[str, typing.Any]) -> typing.Dict[str, typing.Dict[str, int]]:
    is_old_format = all([isinstance(v, int) for v in model.values()])
    if is_old_format:
        output = {}
        sorted_items = sorted(model.items(), key=lambda x: x[0])
        groups = itertools.groupby(sorted_items, key=lambda x: x[0].split(':')[0])
        for group in groups:
            output[group[0]] = dict(((item[0].split(':')[-1], item[1]) for item in group[1]))
        return output
    try:
        assert all([isinstance(v, int) for groups in model.values() for v in groups.values()]), 'Scores should be integers'
    except (AssertionError, AttributeError) as e:
        raise Exception('Unsupported model format:', e)
    else:
        return model

Updates a model to the latest format. Does nothing if it's updated already.

Args:
model: A model.
Returns:
An updated model.


def add(self, decorations):
    added = 0
    if isinstance(decorations, list):
        not_repeated = (set(decorations) - set(self._decorations))
        self._decorations.extend(list(not_repeated))
        added = len(not_repeated)
    elif (decorations not in self._decorations):
        self._decorations.append(decorations)
        added = 1
    if (added > 0):
        self._order_decorations()
        self.update()
    return added

Add text decorations on a CodeEditor instance.

Don't add duplicated decorations, and order decorations according
draw_order and the size of the selection.

Args:
decorations (sourcecode.api.TextDecoration) (could be a list)
Returns:
int: Amount of decorations added.


def user_has_access(self, user):
    if (ROLE_ADMIN in user.roles):
        return True
    if self.enabled:
        if (not self.required_roles):
            return True
        for role in self.required_roles:
            if (role in user.roles):
                return True
    return False

Check if a user has access to view information for the account

Args:
user (:obj:`User`): User object to check

Returns:
True if user has access to the account, else false


def create_dir(path):
    full_path = abs_path(path)
    if not os.path.exists(full_path):
        try:
            os.makedirs(full_path)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

Creates a directory if it does not exist already.

Args:
path: The path of the directory to create.


def prepare_subprocess_cmd(subprocess_cmd):

    help_cmd = subprocess_cmd + ['--helpfull']
    help_output = subprocess.run(help_cmd, stdout=subprocess.PIPE).stdout
    help_output = help_output.decode('ascii')
    if 'python' in subprocess_cmd[0]:
        valid_flags = parse_helpfull_output(help_output)
    else:
        valid_flags = parse_helpfull_output(help_output, regex=FLAG_HELP_RE_CC)
    parsed_flags = flags.FlagValues().read_flags_from_files(subprocess_cmd[1:])

    filtered_flags = filter_flags(parsed_flags, valid_flags)
    return [subprocess_cmd[0]] + filtered_flags

Prepares a subprocess command by running --helpfull and masking flags.

Args:
subprocess_cmd: List[str], what would be passed into subprocess.call()
i.e. ['python', 'train.py', '--flagfile=flags']

Returns:
['python', 'train.py', '--train_flag=blah', '--more_flags']


def obtain(self, dest):

        url, rev_options = self.get_url_rev_options(self.url)

        if not os.path.exists(dest):
            self.fetch_new(dest, url, rev_options)
            return

        rev_display = rev_options.to_display()
        if self.is_repository_directory(dest):
            existing_url = self.get_remote_url(dest)
            if self.compare_urls(existing_url, url):
                logger.debug(
                    '%s in %s exists, and has correct URL (%s)',
                    self.repo_name.title(),
                    display_path(dest),
                    url,
                )
                if not self.is_commit_id_equal(dest, rev_options.rev):
                    logger.info(
                        'Updating %s %s%s',
                        display_path(dest),
                        self.repo_name,
                        rev_display,
                    )
                    self.update(dest, url, rev_options)
                else:
                    logger.info('Skipping because already up-to-date.')
                return

            logger.warning(
                '%s %s in %s exists with URL %s',
                self.name,
                self.repo_name,
                display_path(dest),
                existing_url,
            )
            prompt = ('(s)witch, (i)gnore, (w)ipe, (b)ackup ',
                      ('s', 'i', 'w', 'b'))
        else:
            logger.warning(
                'Directory %s already exists, and is not a %s %s.',
                dest,
                self.name,
                self.repo_name,
            )

            prompt = ('(i)gnore, (w)ipe, (b)ackup ',
                      ('i', 'w', 'b'))

        logger.warning(
            'The plan is to install the %s repository %s',
            self.name,
            url,
        )
        response = ask_path_exists('What to do?  %s' % prompt[0], prompt[1])

        if response == 'a':
            sys.exit(-1)

        if response == 'w':
            logger.warning('Deleting %s', display_path(dest))
            rmtree(dest)
            self.fetch_new(dest, url, rev_options)
            return

        if response == 'b':
            dest_dir = backup_dir(dest)
            logger.warning(
                'Backing up %s to %s', display_path(dest), dest_dir,
            )
            shutil.move(dest, dest_dir)
            self.fetch_new(dest, url, rev_options)
            return

        if response == 's':
            logger.info(
                'Switching %s %s to %s%s',
                self.repo_name,
                display_path(dest),
                url,
                rev_display,
            )
            self.switch(dest, url, rev_options)

Install or update in editable mode the package represented by this
VersionControl object.

Args:
dest: the repository directory in which to install or update.


def compute(self, x):

        q_learning = copy(self.__greedy_q_learning)
        q_learning.epsilon_greedy_rate = x[0]
        q_learning.alpha_value = x[1]
        q_learning.gamma_value = x[2]
        if self.__init_state_key is not None:
            q_learning.learn(state_key=self.__init_state_key, limit=int(x[3]))
        else:
            q_learning.learn(limit=x[3])
        q_sum = q_learning.q_df.q_value.sum()
        if q_sum != 0:
            cost = q_learning.q_df.shape[0] / q_sum
        else:
            cost = q_learning.q_df.shape[0] / 1e-4

        return cost

Compute cost.

Args:
x:    `np.ndarray` of explanatory variables.

Returns:
cost


def Write(self, output_writer):

    if self._title:
      output_writer.Write('

    if not self._columns:
      self._columns = ['' for _ in range(0, self._number_of_columns)]

    output_writer.Write(' | '.join(self._columns))
    output_writer.Write('\n')

    output_writer.Write(' | '.join(['---' for _ in self._columns]))
    output_writer.Write('\n')

    for values in self._rows:
      values = ['{0!s}'.format(value) for value in values]
      output_writer.Write(' | '.join(values))
      output_writer.Write('\n')

    output_writer.Write('\n')

Writes the table to the output writer.

Args:
output_writer (OutputWriter): output writer.


def word_matches(s1, s2, n=3):
    return __matches(s1, s2, word_ngrams, n=n)

Word-level n-grams that match between two strings

Args:
s1: a string
s2: another string
n: an int for the n in n-gram

Returns:
set: the n-grams found in both strings


def get_video_features(self, pixel_values: torch.FloatTensor, qformer_input_ids: torch.LongTensor, qformer_attention_mask: Optional[torch.LongTensor]=None, interpolate_pos_encoding: Optional[bool]=False, return_dict: Optional[bool]=False):
    batch_size, frames, channel, height, width = pixel_values.shape
    pixel_values = pixel_values.reshape(batch_size * frames, channel, height, width)
    vision_outputs = self.vision_model(pixel_values=pixel_values, interpolate_pos_encoding=interpolate_pos_encoding, return_dict=True)
    image_embeds = vision_outputs[0]
    image_attention_mask = torch.ones(image_embeds.size()[:-1], dtype=torch.long, device=image_embeds.device)
    query_tokens = self.query_tokens.expand(image_embeds.shape[0], -1, -1)
    query_attention_mask = torch.ones(query_tokens.size()[:-1], dtype=torch.long, device=image_embeds.device)
    if qformer_attention_mask is None:
        qformer_attention_mask = torch.ones_like(qformer_input_ids)
    qformer_input_ids = qformer_input_ids.repeat_interleave(frames, dim=0)
    qformer_attention_mask = qformer_attention_mask.repeat_interleave(frames, dim=0)
    qformer_attention_mask = torch.cat([query_attention_mask, qformer_attention_mask], dim=1)
    query_outputs = self.qformer(input_ids=qformer_input_ids, attention_mask=qformer_attention_mask, query_embeds=query_tokens, encoder_hidden_states=image_embeds, encoder_attention_mask=image_attention_mask, return_dict=True)
    query_output = query_outputs[0][:, :query_tokens.size(1), :]
    language_model_inputs = self.language_projection(query_output)
    language_model_inputs = language_model_inputs.reshape(batch_size, self.config.num_query_tokens * frames, -1)
    if return_dict:
        return (language_model_inputs, vision_outputs, query_outputs)
    return language_model_inputs

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

Args:
pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`):
The tensors corresponding to the input images.


def symm_reduce(self, coords_set, threshold=1e-6):

        surf_sg = SpacegroupAnalyzer(self.slab, 0.1)
        symm_ops = surf_sg.get_symmetry_operations()
        unique_coords = []

        coords_set = [self.slab.lattice.get_fractional_coords(coords)
                      for coords in coords_set]
        for coords in coords_set:
            incoord = False
            for op in symm_ops:
                if in_coord_list_pbc(unique_coords, op.operate(coords),
                                     atol=threshold):
                    incoord = True
                    break
            if not incoord:
                unique_coords += [coords]

        return [self.slab.lattice.get_cartesian_coords(coords)
                for coords in unique_coords]

Reduces the set of adsorbate sites by finding removing
symmetrically equivalent duplicates

Args:
coords_set: coordinate set in cartesian coordinates
threshold: tolerance for distance equivalence, used
as input to in_coord_list_pbc for dupl. checking


def from_file(cls, filename):

        with zopen(filename) as f:
            return cls.from_string(f.read())

Read an Fiesta input from a file. Currently tested to work with
files generated from this class itself.

Args:
filename: Filename to parse.

Returns:
FiestaInput object


def _ReadMemberHeader(self, file_object):
    file_offset = file_object.get_offset()
    member_header = self._ReadStructure(file_object, file_offset, self._MEMBER_HEADER_SIZE, self._MEMBER_HEADER, 'member header')
    if (member_header.signature != self._GZIP_SIGNATURE):
        raise errors.FileFormatError('Unsupported signature: 0x{0:04x}.'.format(member_header.signature))
    if (member_header.compression_method != self._COMPRESSION_METHOD_DEFLATE):
        raise errors.FileFormatError('Unsupported compression method: {0:d}.'.format(member_header.compression_method))
    self.modification_time = member_header.modification_time
    self.operating_system = member_header.operating_system
    if (member_header.flags & self._FLAG_FEXTRA):
        file_offset = file_object.get_offset()
        extra_field_data_size = self._ReadStructure(file_object, file_offset, self._UINT16LE_SIZE, self._UINT16LE, 'extra field data size')
        file_object.seek(extra_field_data_size, os.SEEK_CUR)
    if (member_header.flags & self._FLAG_FNAME):
        file_offset = file_object.get_offset()
        string_value = self._ReadString(file_object, file_offset, self._CSTRING, 'original filename')
        self.original_filename = string_value.rstrip('\x00')
    if (member_header.flags & self._FLAG_FCOMMENT):
        file_offset = file_object.get_offset()
        string_value = self._ReadString(file_object, file_offset, self._CSTRING, 'comment')
        self.comment = string_value.rstrip('\x00')
    if (member_header.flags & self._FLAG_FHCRC):
        file_object.read(2)

Reads a member header.

Args:
file_object (FileIO): file-like object to read from.

Raises:
FileFormatError: if the member header cannot be read.


def to_barrier_key(cls, barrier_index_key):
    barrier_index_path = barrier_index_key.to_path()
    (pipeline_kind, dependent_pipeline_id, unused_kind, purpose) = barrier_index_path[(- 4):]
    barrier_record_path = (pipeline_kind, dependent_pipeline_id, _BarrierRecord.kind(), purpose)
    return db.Key.from_path(*barrier_record_path)

Converts a _BarrierIndex key to a _BarrierRecord key.

Args:
barrier_index_key: db.Key for a _BarrierIndex entity.

Returns:
db.Key for the corresponding _BarrierRecord entity.


def compose_tree_path(tree, issn=False):

    if issn:
        return join(
            "/",
            ISSN_DOWNLOAD_KEY,
            basename(tree.issn)
        )

    return join(
        "/",
        PATH_DOWNLOAD_KEY,
        quote_plus(tree.path).replace("%2F", "/"),
    )

Compose absolute path for given `tree`.

Args:
pub (obj): :class:`.Tree` instance.
issn (bool, default False): Compose URL using ISSN.

Returns:
str: Absolute path of the tree, without server's address and protocol.


def proto_refactor_files(dest_dir, namespace, namespace_path):
    for (dn, dns, fns) in os.walk(dest_dir):
        for fn in fns:
            fn = os.path.join(dn, fn)
            if fnmatch.fnmatch(fn, '*.proto'):
                data = proto_refactor(fn, namespace, namespace_path)
                with open(fn, 'w') as f:
                    f.write(data)

This method runs the refactoring on all the Protobuf files in the
Dropsonde repo.

Args:
dest_dir (str): directory where the Protobuf files lives.
namespace (str): the desired package name (i.e. "dropsonde.py2")
namespace_path (str): the desired path corresponding to the package
name (i.e. "dropsonde/py2")


def Add(self, file_desc_proto):
    proto_name = file_desc_proto.name
    if (proto_name not in self._file_desc_protos_by_file):
        self._file_desc_protos_by_file[proto_name] = file_desc_proto
    elif (self._file_desc_protos_by_file[proto_name] != file_desc_proto):
        raise DescriptorDatabaseConflictingDefinitionError(('%s already added, but with different descriptor.' % proto_name))
    package = file_desc_proto.package
    for message in file_desc_proto.message_type:
        self._file_desc_protos_by_symbol.update(((name, file_desc_proto) for name in _ExtractSymbols(message, package)))
    for enum in file_desc_proto.enum_type:
        self._file_desc_protos_by_symbol['.'.join((package, enum.name))] = file_desc_proto
    for extension in file_desc_proto.extension:
        self._file_desc_protos_by_symbol['.'.join((package, extension.name))] = file_desc_proto
    for service in file_desc_proto.service:
        self._file_desc_protos_by_symbol['.'.join((package, service.name))] = file_desc_proto

Adds the FileDescriptorProto and its types to this database.

Args:
file_desc_proto: The FileDescriptorProto to add.
Raises:
DescriptorDatabaseConflictingDefinitionError: if an attempt is made to
add a proto with the same name but different definition than an
exisiting proto in the database.


def SendSourceFiles(self, request, context):
    return debug_service_pb2.EventReply()

Base implementation of the handling of SendSourceFiles calls.

The base implementation does nothing with the incoming request.
Override in an implementation of the server if necessary.

Args:
request: A `DebuggedSourceFiles` proto, containing the path, content, size
and last-modified timestamp of source files.
context: Server context.

Returns:
A `EventReply` proto.


def _abort_workflow(pb: ProcessingBlock, workflow_stage_dict: dict,
                    docker: DockerSwarmClient):

    _abort_flag = False
    if _abort_flag:
        for workflow_stage in pb.workflow_stages:
            for service_id, _ in \
                    workflow_stage_dict[workflow_stage.id]['services'].items():
                docker.delete_service(service_id)
                LOG.info("Deleted Service Id %s", service_id)
        return True
    return False

Abort the workflow.

TODO(BMo): This function currently does nothing as the abort flag
is hardcoded to False!

This function is used by `execute_processing_block`.

Args:
pb (ProcessingBlock): Configuration database Processing block object.
workflow_stage_dict (dict): Workflow stage metadata dictionary.
docker (DockerClient): Docker Swarm Client object.

Returns:
bool, True if the stage is aborted, otherwise False.


def CreateCampaignWithBiddingStrategy(client, bidding_strategy_id, budget_id):
    campaign_service = client.GetService('CampaignService', version='v201809')
    campaign = {'name': ('Interplanetary Cruise
    operation = {'operator': 'ADD', 'operand': campaign}
    response = campaign_service.mutate([operation])
    new_campaign = response['value'][0]
    print(('Campaign with name "%s", ID "%s" and bidding scheme ID "%s" was created.' % (new_campaign['name'], new_campaign['id'], new_campaign['biddingStrategyConfiguration']['biddingStrategyId'])))
    return new_campaign

Create a Campaign with a Shared Bidding Strategy.

Args:
client: AdWordsClient the client to run the example with.
bidding_strategy_id: string the bidding strategy ID to use.
budget_id: string the shared budget ID to use.

Returns:
dict An object representing a campaign.


def snapshot(self, name):

        return self.get_data(
            "volumes/%s/snapshots/" % self.id,
            type=POST,
            params={"name": name}
        )

Create a snapshot of the volume.

Args:
name: string - a human-readable name for the snapshot


def get_dos(self, partial_dos=False, npts_mu=10000, T=None):
    spin = (self.data.spin if isinstance(self.data.spin, int) else 1)
    (energies, densities, vvdos, cdos) = BL.BTPDOS(self.eband, self.vvband, npts=npts_mu)
    if (T is not None):
        densities = BL.smoothen_DOS(energies, densities, T)
    tdos = Dos((self.efermi / units.eV), (energies / units.eV), {Spin(spin): densities})
    if partial_dos:
        tdos = self.get_partial_doses(tdos=tdos, npts_mu=npts_mu, T=T)
    return tdos

Return a Dos object interpolating bands

Args:
partial_dos: if True, projections will be interpolated as well
and partial doses will be return. Projections must be available
in the loader.
npts_mu: number of energy points of the Dos
T: parameter used to smooth the Dos


def __wizard(rho, epsilon=None):

    if epsilon is None:
        epsilon = 0.

    dim = len(rho)
    rho_wizard = np.zeros([dim, dim])
    v, w = np.linalg.eigh(rho)
    for j in range(dim):
        if v[j] < epsilon:
            tmp = v[j]
            v[j] = 0.

            x = 0.
            for k in range(j + 1, dim):
                x += tmp / (dim - (j + 1))
                v[k] = v[k] + tmp / (dim - (j + 1))
    for j in range(dim):
        rho_wizard = rho_wizard + v[j] * outer(w[:, j])
    return rho_wizard

Returns the nearest positive semidefinite operator to an operator.

This method is based on reference [1]. It constrains positivity
by setting negative eigenvalues to zero and rescaling the positive
eigenvalues.

Args:
rho (array_like): the input operator.
epsilon(float or None): threshold (>=0) for truncating small
eigenvalues values to zero.

Returns:
numpy.array: A positive semidefinite numpy array.


def tensordot(x1, x2, axes=2):
    if any_symbolic_tensors((x1, x2)):
        return Tensordot(axes=axes).symbolic_call(x1, x2)
    return backend.numpy.tensordot(x1, x2, axes=axes)

Compute the tensor dot product along specified axes.

Args:
x1: First tensor.
x2: Second tensor.
axes: - If an integer, N, sum over the last N axes of `x1` and the
first N axes of `x2` in order. The sizes of the corresponding
axes must match.
- Or, a list of axes to be summed over, first sequence applying
to `x1`, second to `x2`. Both sequences must be of the
same length.

Returns:
The tensor dot product of the inputs.


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

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

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

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

Returns:
bool: Whether it exists.


def read(self, size=None):

    if not self._is_open:
      raise IOError('Not opened.')

    return self._vshadow_store.read(size)

Reads a byte string from the file-like object at the current offset.

The function will read a byte string of the specified size or
all of the remaining data if no size was specified.

Args:
size (Optional[int]): number of bytes to read, where None is all
remaining data.

Returns:
bytes: data read.

Raises:
IOError: if the read failed.
OSError: if the read failed.


def _ConvertBool(value, require_str):

  if require_str:
    if value == 'true':
      return True
    elif value == 'false':
      return False
    else:
      raise ParseError('Expected "true" or "false", not {0}.'.format(value))

  if not isinstance(value, bool):
    raise ParseError('Expected true or false without quotes.')
  return value

Convert a boolean value.

Args:
value: A scalar value to convert.
require_str: If True, value must be a str.

Returns:
The bool parsed.

Raises:
ParseError: If a boolean value couldn't be consumed.


def LockedWrite(self, cache_data):
    if isinstance(cache_data, six.text_type):
        cache_data = cache_data.encode(encoding=self._encoding)
    with self._thread_lock:
        if (not self._EnsureFileExists()):
            return False
        with self._process_lock_getter() as acquired_plock:
            if (not acquired_plock):
                return False
            with open(self._filename, 'wb') as f:
                f.write(cache_data)
            return True

Acquire an interprocess lock and write a string.

This method safely acquires the locks then writes a string
to the cache file. If the string is written successfully
the function will return True, if the write fails for any
reason it will return False.

Args:
cache_data: string or bytes to write.

Returns:
bool: success


def spec_filled(self, pos_args, kw_args):
    req_names = self.arg_names
    if (len(self.arg_defaults) > 0):
        req_names = req_names[:(- len(self.arg_defaults))]
    req = [x for x in req_names if (x not in kw_args)]
    return (len(req) <= len(pos_args))

Check if we have enough arguments to call this function.

Args:
pos_args (list): A list of all the positional values we have.
kw_args (dict): A dict of all of the keyword args we have.

Returns:
bool: True if we have a filled spec, False otherwise.


def dismiss_prompt(self, text=None, wait=None):

        with self.driver.dismiss_modal("prompt", text=text, wait=wait):
            yield

Execute the wrapped code, dismissing a prompt.

Args:
text (str | RegexObject, optional): Text to match against the text in the modal.
wait (int | float, optional): Maximum time to wait for the modal to appear after
executing the wrapped code.

Raises:
ModalNotFound: If a modal dialog hasn't been found.


def generate_argument_parser(cls, tree, actions={}):
    (cur_as, cur_subas) = tree
    parser = devassistant_argparse.ArgumentParser(argument_default=argparse.SUPPRESS, usage=argparse.SUPPRESS, add_help=False)
    cls.add_default_arguments_to(parser)
    for arg in cur_as.args:
        arg.add_argument_to(parser)
    if (cur_subas or actions):
        subparsers = cls._add_subparsers_required(parser, dest=settings.SUBASSISTANT_N_STRING.format('0'))
        for subas in sorted(cur_subas, key=(lambda x: x[0].name)):
            for alias in ([subas[0].name] + getattr(subas[0], 'aliases', [])):
                cls.add_subassistants_to(subparsers, subas, level=1, alias=alias)
        for (action, subactions) in sorted(actions.items(), key=(lambda x: x[0].name)):
            cls.add_action_to(subparsers, action, subactions, level=1)
    return parser

Generates argument parser for given assistant tree and actions.

Args:
tree: assistant tree as returned by
devassistant.assistant_base.AssistantBase.get_subassistant_tree
actions: dict mapping actions (devassistant.actions.Action subclasses) to their
subaction dicts
Returns:
instance of devassistant_argparse.ArgumentParser (subclass of argparse.ArgumentParser)