def _strip_debug_nodes(meta_graph_def: meta_graph_pb2.MetaGraphDef) -> None:

    def erase_regular_node_attributes(node: node_def_pb2.NodeDef) -> None:

        attributes_to_remove = [attribute for attribute in node.attr.keys() if not attribute.startswith('_')]
        for attribute in attributes_to_remove:
            node.attr.pop(attribute)

    def prune_all_non_t_attributes(node: node_def_pb2.NodeDef) -> None:

        if 'T' in node.attr:
            t_value = node.attr['T']
            node.ClearField('attr')
            node.attr['T'].CopyFrom(t_value)
        else:
            node.ClearField('attr')

    def is_control_input(name: str) -> str:

        return name and name[0] == '^'

    def as_control_dep(name: str) -> str:

        return '^' + name.split(':')[0]

    def maybe_do_strip(node: node_def_pb2.NodeDef) -> None:

        if node.op == 'Assert' or node.op == 'PrintV2':
            node.op = 'NoOp'
            erase_regular_node_attributes(node)
            new_inputs = []
            for inp in node.input:
                if not is_control_input(inp):
                    new_inputs.append(as_control_dep(inp))
                else:
                    new_inputs.append(inp)
            node.ClearField('input')
            node.input.extend(new_inputs)
        elif node.op == 'CheckNumerics' or node.op == 'Print':
            node.op = 'Identity'
            prune_all_non_t_attributes(node)
            for i in range(1, len(node.input)):
                if not is_control_input(node.input[i]):
                    node.input[i] = as_control_dep(node.input[i])
    for node in meta_graph_def.graph_def.node:
        maybe_do_strip(node)
    for func in meta_graph_def.graph_def.library.function:
        for node in func.node_def:
            maybe_do_strip(node)

An experimental function to remove debug nodes from the final graph.

This function removes all Assert and CheckNumerics nodes from the meta_graph.
It strips the operators in both the nodes and in all of the function defs,
with the Assert ops being replaced by `NoOp`s and the CheckNumerics ops being
transformed into `Identity` ops. In addition to this, it creates control
inputs for the nodes that are not relevant for the op. For more information
about control inputs please see go/how-tensors-flow#control-dependencies.

Args:
meta_graph_def: The meta_graph that will be exported.


def getall(self):
    vrfs_re = re.compile('(?<=^vrf definition\\s)(\\w+)', re.M)
    response = dict()
    for vrf in vrfs_re.findall(self.config):
        response[vrf] = self.get(vrf)
    return response

Returns a dict object of all VRFs in the running-config

Returns:
A dict object of VRF attributes


def body(self, features):

    features["targets"] = features["inputs"]
    is_training = self.hparams.mode == tf.estimator.ModeKeys.TRAIN

    inputs = tf.to_float(features["targets_raw"])

    z = tf.random_uniform([self.hparams.batch_size,
                           self.hparams.bottleneck_bits],
                          minval=-1, maxval=1, name="z")

    out_shape = common_layers.shape_list(inputs)[1:4]
    g = self.generator(z, is_training, out_shape)

    losses = self.losses(inputs, g)

    summary_g_image = tf.reshape(
        g[0, :], [1] + common_layers.shape_list(inputs)[1:])
    tf.summary.image("generated", summary_g_image, max_outputs=1)

    if is_training:
      return tf.zeros_like(inputs), losses
    return tf.reshape(g, tf.shape(inputs)), losses

Body of the model.

Args:
features: a dictionary with the tensors.

Returns:
A pair (predictions, losses) where predictions is the generated image
and losses is a dictionary of losses (that get added for the final loss).


def delete(self, alias_name, timeout=(- 1)):
    uri = ((self.URI + '/') + alias_name)
    return self._client.delete(uri, timeout=timeout)

Revokes a certificate signed by the internal CA. If client certificate to be revoked is RabbitMQ_readonly,
then the internal CA root certificate, RabbitMQ client certificate and RabbitMQ server certificate will be
regenerated. This will invalidate the previous version of RabbitMQ client certificate and the RabbitMQ server
will be restarted to read the latest certificates.

Args:
alias_name (str): Alias name.
timeout:
Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation
in OneView, just stop waiting for its completion.


def PushEvent(self, event):

    macb_group_identifier, content_identifier = self._GetEventIdentifiers(event)

    heap_values = (macb_group_identifier or '', content_identifier, event)
    heapq.heappush(self._heap, heap_values)

Pushes an event onto the heap.

Args:
event (EventObject): event.


def export_to_dir(network, export_dir):
    package_path = ding0.__path__[0]
    network.export_to_csv_folder(os.path.join(package_path, 'output', 'debug', 'grid', export_dir))

Exports PyPSA network as CSV files to directory

Args:
network: pypsa.Network
export_dir: str
Sub-directory in output/debug/grid/ where csv Files of PyPSA network are exported to.


def outer_id(self, value):

        if value == self._defaults['outerId'] and 'outerId' in self._values:
            del self._values['outerId']
        else:
            self._values['outerId'] = value

The outer_id property.

Args:
value (int). the property value.


def api_info(self, headers=None):

        return self.transport.forward_request(
            method='GET',
            path=self.api_prefix,
            headers=headers,
        )

Retrieves information provided by the API root endpoint
``'/api/v1'``.

Args:
headers (dict): Optional headers to pass to the request.

Returns:
dict: Details of the HTTP API provided by the BigchainDB
server.


def _ParseIndexTable(self, file_object):
    cache_address_map = self._GetDataTypeMap('uint32le')
    file_offset = file_object.get_offset()
    cache_address_data = file_object.read(4)
    while (len(cache_address_data) == 4):
        try:
            value = self._ReadStructureFromByteStream(cache_address_data, file_offset, cache_address_map)
        except (ValueError, errors.ParseError) as exception:
            raise errors.ParseError('Unable to map cache address at offset: 0x{0:08x} with error: {1!s}'.format(file_offset, exception))
        if value:
            cache_address = CacheAddress(value)
            self.index_table.append(cache_address)
        file_offset += 4
        cache_address_data = file_object.read(4)

Parses the index table.

Args:
file_object (dfvfs.FileIO): a file-like object to parse.

Raises:
ParseError: if the index table cannot be read.


def ToJsonString(self):
    if ((self.seconds < 0) or (self.nanos < 0)):
        result = '-'
        seconds = ((- self.seconds) + int(((0 - self.nanos)
        nanos = ((0 - self.nanos) % 1000000000.0)
    else:
        result = ''
        seconds = (self.seconds + int((self.nanos
        nanos = (self.nanos % 1000000000.0)
    result += ('%d' % seconds)
    if ((nanos % 1000000000.0) == 0):
        return (result + 's')
    if ((nanos % 1000000.0) == 0):
        return (result + ('.%03ds' % (nanos / 1000000.0)))
    if ((nanos % 1000.0) == 0):
        return (result + ('.%06ds' % (nanos / 1000.0)))
    return (result + ('.%09ds' % nanos))

Converts Duration to string format.

Returns:
A string converted from self. The string format will contains
3, 6, or 9 fractional digits depending on the precision required to
represent the exact Duration value. For example: "1s", "1.010s",
"1.000000100s", "-3.100s"


def SetOption(self, section, option, value, overwrite=True):

    if not overwrite and self.config.has_option(section, option):
      return
    if not self.config.has_section(section):
      self.config.add_section(section)
    self.config.set(section, option, str(value))

Set the value of an option in the config file.

Args:
section: string, the section of the config file to check.
option: string, the option to set the value of.
value: string, the value to set the option.
overwrite: bool, True to overwrite an existing value in the config file.


def simulate(self, action):

    with tf.name_scope('environment/simulate'):
      if action.dtype in (tf.float16, tf.float32, tf.float64):
        action = tf.check_numerics(action, 'action')
      observ_dtype = self._parse_dtype(self._batch_env.observation_space)
      observ, reward, done = tf.py_func(
          lambda a: self._batch_env.step(a)[:3], [action],
          [observ_dtype, tf.float32, tf.bool], name='step')
      observ = tf.check_numerics(observ, 'observ')
      reward = tf.check_numerics(reward, 'reward')
      return tf.group(
          self._observ.assign(observ),
          self._action.assign(action),
          self._reward.assign(reward),
          self._done.assign(done))

Step the batch of environments.

The results of the step can be accessed from the variables defined below.

Args:
action: Tensor holding the batch of actions to apply.

Returns:
Operation.


def last(series, order_by=None):

    if order_by is not None:
        series = order_series_by(series, order_by)
    last_s = series.iloc[series.size - 1]
    return last_s

Returns the last value of a series.

Args:
series (pandas.Series): column to summarize.

Kwargs:
order_by: a pandas.Series or list of series (can be symbolic) to order
the input series by before summarization.


def plot(self, **plot_kwargs: Any) -> None:

        fig = plt.figure()
        plt.plot(self._num_cfds_seq, self._gnd_state_probs, 'ro-',
                 figure=fig, **plot_kwargs)
        plt.xlabel(r"Number of Cliffords", figure=fig)
        plt.ylabel('Ground State Probability', figure=fig)
        fig.show()

Plots the average ground state probability vs the number of
Cliffords in the RB study.

Args:
**plot_kwargs: Arguments to be passed to matplotlib.pyplot.plot.


def batch_slice(dist, params_event_ndims, params_overrides, slices):
    if (not isinstance(slices, collections.Sequence)):
        slices = (slices,)
    (orig_dist, slice_overrides_seq) = getattr(dist, PROVENANCE_ATTR, (dist, []))
    slice_overrides_seq += [(slices, params_overrides)]
    dist = _apply_slice_sequence(orig_dist, params_event_ndims, slice_overrides_seq)
    setattr(dist, PROVENANCE_ATTR, (orig_dist, slice_overrides_seq))
    return dist

Slices `dist` along its batch dimensions. Helper for tfd.Distribution.

Args:
dist: A `tfd.Distribution` instance.
params_event_ndims: A `dict` of `str->int` indicating the number of
dimensions of a given parameter required to parameterize a single event.
params_overrides: A `dict` of parameter overrides. (e.g. from
`Distribution.copy`).
slices: A `slice` or `int` or `int` `Tensor` or `tf.newaxis` or `tuple`
thereof. (e.g. the argument of a `__getitem__` method).

Returns:
new_dist: A batch-sliced `tfd.Distribution`.


def sg_lookup(tensor, opt):
    assert (opt.emb is not None), 'emb is mandatory.'
    return tf.nn.embedding_lookup(opt.emb, tensor, name=opt.name)

r"""Looks up the `tensor`, which is the embedding matrix.

Args:
tensor: A tensor ( automatically given by chain )
opt:
emb: A 2-D `Tensor`. An embedding matrix.
name: If provided, replace current tensor's name.

Returns:
A `Tensor`.


def take_ownership(self, **kwargs):
    path = ('%s/%s/take_ownership' % (self.manager.path, self.get_id()))
    server_data = self.manager.gitlab.http_post(path, **kwargs)
    self._update_attrs(server_data)

Update the owner of a pipeline schedule.

Args:
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabOwnershipError: If the request failed


def gene_by_alias(self, symbol, build='37'):
    res = self.hgnc_collection.find({'hgnc_symbol': symbol, 'build': build})
    if (res.count() == 0):
        res = self.hgnc_collection.find({'aliases': symbol, 'build': build})
    return res

Return a iterable with hgnc_genes.

If the gene symbol is listed as primary the iterable will only have
one result. If not the iterable will include all hgnc genes that have
the symbol as an alias.

Args:
symbol(str)
build(str)

Returns:
res(pymongo.Cursor(dict))


def delete(self, model_name):

    full_name = model_name
    if not model_name.startswith('projects/'):
      full_name = ('projects/%s/models/%s' % (self._project_id, model_name))
    response = self._api.projects().models().delete(name=full_name).execute()
    if 'name' not in response:
      raise Exception('Invalid response from service. "name" is not found.')
    _util.wait_for_long_running_operation(response['name'])

Delete a model.

Args:
model_name: the name of the model. It can be a model full name
("projects/[project_id]/models/[model_name]") or just [model_name].


def _slice_ragged_row_dimension(rt_input, row_key):
    if row_key.start is None and row_key.stop is None and (row_key.step is None):
        return rt_input
    new_starts = rt_input.row_splits[:-1][row_key]
    new_limits = rt_input.row_splits[1:][row_key]
    zero_pad = array_ops.zeros([1], rt_input.row_splits.dtype)
    if row_key.step is None or row_key.step == 1:
        new_splits = array_ops.concat([zero_pad[array_ops.size(new_starts):], new_starts[:1], new_limits], axis=0)
        values_start = new_splits[0]
        values_limit = new_splits[-1]
        return ragged_tensor.RaggedTensor.from_row_splits(rt_input.values[values_start:values_limit], new_splits - values_start, validate=False)
    else:
        return _build_ragged_tensor_from_value_ranges(new_starts, new_limits, 1, rt_input.values)

Slice the outer dimension of `rt_input` according to the given `slice`.

Args:
rt_input: The `RaggedTensor` to slice.
row_key: The `slice` object that should be used to slice `rt_input`.

Returns:
A `RaggedTensor` containing the indicated slice of `rt_input`.


def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
    line = clean_lines.elided[linenum]
    match = Search(pattern, line)
    if (not match):
        return False
    context = line[0:(match.start(1) - 1)]
    if Match('.*\\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\\s*$', context):
        return False
    if (linenum > 0):
        for i in xrange((linenum - 1), max(0, (linenum - 5)), (- 1)):
            context = (clean_lines.elided[i] + context)
    if Match('.*\\b[_A-Z][_A-Z0-9]*\\s*\\((?:\\([^()]*\\)|[^()])*$', context):
        return False
    if (context.endswith(' operator++') or context.endswith(' operator--')):
        return False
    remainder = line[match.end(0):]
    if Match('^\\s*(?:;|const\\b|throw\\b|final\\b|override\\b|[=>{),]|->)', remainder):
        return False
    error(filename, linenum, 'readability/casting', 4, ('Using C-style cast.  Use %s<%s>(...) instead' % (cast_type, match.group(1))))
    return True

Checks for a C-style cast by looking for the pattern.

Args:
filename: The name of the current file.
clean_lines: A CleansedLines instance containing the file.
linenum: The number of the line to check.
cast_type: The string for the C++ cast to recommend.  This is either
reinterpret_cast, static_cast, or const_cast, depending.
pattern: The regular expression used to find C-style casts.
error: The function to call with any errors found.

Returns:
True if an error was emitted.
False otherwise.


def in_labelset(xmrs, nodeids, label=None):

    nodeids = set(nodeids)
    if label is None:
        label = xmrs.ep(next(iter(nodeids))).label
    return nodeids.issubset(xmrs._vars[label]['refs']['LBL'])

Test if all nodeids share a label.

Args:
nodeids: iterable of nodeids
label (str, optional): the label that all nodeids must share
Returns:
bool: `True` if all nodeids share a label, otherwise `False`


def to_schema(self, entry: dict, parents: dict={}) -> list:
    bigquery_schema = []
    for key, value in entry.items():
        if not isinstance(value, dict):
            continue
        if '$ref' in value:
            parents.setdefault(value['$ref'], 0)
            if parents[value['$ref']] < self.recursion_depth:
                parents[value['$ref']] += 1
                bigquery_schema.append({'name': key, 'type': 'RECORD', 'mode': 'NULLABLE', 'fields': self.to_schema(self.api_document['schemas'][value['$ref']]['properties'], parents)})
            parents[value['$ref']] -= 1
        elif 'items' in value:
            if '$ref' in value['items']:
                parents.setdefault(value['items']['$ref'], 0)
                if parents[value['items']['$ref']] < self.recursion_depth:
                    parents[value['items']['$ref']] += 1
                    bigquery_schema.append({'name': key, 'type': 'RECORD', 'mode': 'REPEATED', 'fields': self.to_schema(self.api_document['schemas'][value['items']['$ref']]['properties'], parents)})
                    parents[value['items']['$ref']] -= 1
            elif value['items']['type'] == 'object':
                bigquery_schema.append({'name': key, 'type': 'RECORD', 'mode': 'NULLABLE', 'fields': self.to_schema(value['items'], parents)})
            else:
                bigquery_schema.append({'description': ','.join(value['items'].get('enum', []))[:DESCRIPTION_LENGTH], 'name': key, 'type': self.to_type(value['items']), 'mode': 'REPEATED'})
        else:
            bigquery_schema.append({'description': ','.join(value.get('enum', []))[:DESCRIPTION_LENGTH], 'name': key, 'type': self.to_type(value), 'mode': 'NULLABLE'})
    return bigquery_schema

Convert a Discovery API Document schema to a BigQuery schema.

Recursively crawls the discovery document reference tree to build schema.
Leverages recursion depth passed in constructor to stop if necessary.

Args:
entry: a discovery document schema definition.
parents: used to track recursion depth for a specific schema branch

Returns:
A BigQuery schema object.


def _update_hasher(hasher, data, types=True):
    if isinstance(data, (tuple, list, zip)):
        needs_iteration = True
    else:
        needs_iteration = any((check(data) for check in _HASHABLE_EXTENSIONS.iterable_checks))
    if needs_iteration:
        SEP = b'_,_'
        ITER_PREFIX = b'_[_'
        ITER_SUFFIX = b'_]_'
        iter_ = iter(data)
        hasher.update(ITER_PREFIX)
        try:
            for item in iter_:
                (prefix, hashable) = _convert_to_hashable(item, types)
                binary_data = ((prefix + hashable) + SEP)
                hasher.update(binary_data)
        except TypeError:
            _update_hasher(hasher, item, types)
            for item in iter_:
                _update_hasher(hasher, item, types)
                hasher.update(SEP)
        hasher.update(ITER_SUFFIX)
    else:
        (prefix, hashable) = _convert_to_hashable(data, types)
        binary_data = (prefix + hashable)
        hasher.update(binary_data)

Converts `data` into a byte representation and calls update on the hasher
`hashlib.HASH` algorithm.

Args:
hasher (HASH): instance of a hashlib algorithm
data (object): ordered data with structure
types (bool): include type prefixes in the hash

Example:
>>> hasher = hashlib.sha512()
>>> data = [1, 2, ['a', 2, 'c']]
>>> _update_hasher(hasher, data)
>>> print(hasher.hexdigest()[0:8])
e2c67675

2ba8d82b


def _get_syslog_format(event_type):
    syslog_format_template = get_template('syslog_format.json')
    fmt = syslog_format_template.render(event_type=event_type, host=dbconfig.get('instance_name', default='local'))
    return json.dumps(json.loads(fmt))

Take an event type argument and return a python logging format

In order to properly format the syslog messages to current standard, load the template and perform necessary
replacements and return the string.

Args:
event_type (str): Event type name

Returns:
`str`


def _check_status(cls, response_json):
    status = response_json['status']
    msg = response_json['msg']
    if (status == 400):
        raise BadRequestException(msg)
    elif (status == 403):
        raise PermissionDeniedException(msg)
    elif (status == 404):
        raise FileNotFoundException(msg)
    elif (status == 451):
        raise UnavailableForLegalReasonsException(msg)
    elif (status == 509):
        raise BandwidthUsageExceeded(msg)
    elif (status >= 500):
        raise ServerErrorException(msg)

Check the status of the incoming response, raise exception if status is not 200.

Args:
response_json (dict): results of the response of the GET request.

Returns:
None


def _subscribe(tensor, side_effects, control_cache):
    if not tensor.dtype.is_numpy_compatible:
        logging.debug('Tensor {} has an un-supported {} type and cannot be subscribed.'.format(tensor.name, tensor.dtype))
        return tensor
    if _is_subscribed_identity(tensor):
        return _subscribe_extend(tensor, side_effects)
    name_scope = tensor.op.name + '/subscription/Identity'
    consumers = tensor.consumers()
    matching_ops = [op for op in consumers if op.name.startswith(name_scope)]
    assert len(matching_ops) <= 1, 'Op {} must only have one subscription op connected to it'.format(tensor.op.name)
    if len(matching_ops) == 1:
        candidate_tensor = matching_ops[0].outputs[0]
        if _is_subscribed_identity(candidate_tensor):
            return _subscribe_extend(candidate_tensor, side_effects)
    return _subscribe_new(tensor, side_effects, control_cache)

Helper method that subscribes a single tensor to a list of side_effects.

This method will check if the given tensor has already been subscribed or if
it's a tensor returned by a previous call to `subscribe()` and, if so, will
reuse the existing identity op, appending the given side effects to the list
of existing ones.

Args:
tensor: The `tf.Tensor` to be subscribed.
side_effects: List of side_effect functions, see subscribe for details.
control_cache: `_ControlOutputCache` helper to get control_outputs faster.

Returns:
The modified replacement to the passed in tensor which triggers the side
effects or the given tensor, if it was already been subscribed.


def load_model(
            self, the_metamodel, filename, is_main_model, encoding='utf-8',
            add_to_local_models=True):

        if not self.local_models.has_model(filename):
            if self.all_models.has_model(filename):
                new_model = self.all_models.filename_to_model[filename]
            else:

                new_model = the_metamodel.internal_model_from_file(
                    filename, pre_ref_resolution_callback=lambda
                    other_model: self.pre_ref_resolution_callback(other_model),
                    is_main_model=is_main_model, encoding=encoding)
                self.all_models.filename_to_model[filename] = new_model

            if add_to_local_models:
                self.local_models.filename_to_model[filename] = new_model
        assert self.all_models.has_model(filename)
        return self.all_models.filename_to_model[filename]

load a single model

Args:
the_metamodel: the metamodel used to load the model
filename: the model to be loaded (if not cached)

Returns:
the loaded/cached model


def save_optimizer_weights_to_hdf5_group(hdf5_group, optimizer):
    symbolic_weights = getattr(optimizer, 'weights')
    if symbolic_weights:
        weights_group = hdf5_group.create_group('optimizer_weights')
        weight_names = [str(w.name).encode('utf8') for w in symbolic_weights]
        save_attributes_to_hdf5_group(weights_group, 'weight_names', weight_names)
        weight_values = backend.batch_get_value(symbolic_weights)
        for name, val in zip(weight_names, weight_values):
            param_dset = weights_group.create_dataset(name, val.shape, dtype=val.dtype)
            if not val.shape:
                param_dset[()] = val
            else:
                param_dset[:] = val

Saves optimizer weights of a optimizer to a HDF5 group.

Args:
hdf5_group: HDF5 group.
optimizer: optimizer instance.


def link_asset_content_key(access_token, asset_id, encryptionkey_id, ams_redirected_rest_endpoint):

    path = '/Assets'
    full_path = ''.join([path, "('", asset_id, "')", "/$links/ContentKeys"])
    full_path_encoded = urllib.parse.quote(full_path, safe='')
    endpoint = ''.join([ams_rest_endpoint, full_path_encoded])
    uri = ''.join([ams_redirected_rest_endpoint, 'ContentKeys', "('", encryptionkey_id, "')"])
    body = '{"uri": "' + uri + '"}'
    return do_ams_post(endpoint, full_path_encoded, body, access_token)

Link Media Service Asset and Content Key.

Args:
access_token (str): A valid Azure authentication token.
asset_id (str): A Media Service Asset ID.
encryption_id (str): A Media Service Encryption ID.
ams_redirected_rest_endpoint (str): A Media Service Redirected Endpoint.

Returns:
HTTP response. JSON body.


def _run_in_hypothesis(self, mma, w_string, index):

        state = mma[0]
        for i in range(index):
            for arc in state:
                if mma.isyms.find(arc.ilabel) == w_string[i]:
                    state = mma[arc.nextstate]
                    s_index = arc.nextstate

        access_string = self.observation_table.sm_vector[s_index]
        logging.debug(
            'Access string for %d: %s - %d ',
            index,
            access_string,
            s_index)

        return access_string

Run the string in the hypothesis automaton for index steps and then
return the access string for the state reached concatanated with the
rest of the string w.
Args:
mma (DFA): The hypothesis automaton
w_string (str): The examined string to be consumed
index (int): The index value for selecting the prefix of w
Return:
str: The access string


def projection(self, variables):
    variables = set(variables)
    if (not variables.issubset(self.variables)):
        raise ValueError('Cannot project to variables not in the constraint.')
    idxs = [i for (i, v) in enumerate(self.variables) if (v in variables)]
    configurations = frozenset((tuple((config[i] for i in idxs)) for config in self.configurations))
    variables = tuple((self.variables[i] for i in idxs))
    return self.from_configurations(configurations, variables, self.vartype)

Create a new constraint that is the projection onto a subset of the variables.

Args:
variables (iterable):
Subset of the constraint's variables.

Returns:
:obj:`.Constraint`: A new constraint over a subset of the variables.

Examples:

>>> import dwavebinarycsp
...
>>> const = dwavebinarycsp.Constraint.from_configurations([(0, 0), (0, 1)],
...                                                       ['a', 'b'],
...                                                       dwavebinarycsp.BINARY)
>>> proj = const.projection(['a'])
>>> proj.variables
['a']
>>> proj.configurations
{(0,)}


def top_and_tail(a):

    if np.all(np.isnan(a)):
        return np.array([])
    nans = np.where(~np.isnan(a))[0]
    last = None if nans[-1]+1 == a.size else nans[-1]+1
    return a[nans[0]:last]

Remove the NaNs from the top and tail (only) of a well log.

Args:
a (ndarray): An array.
Returns:
ndarray: The top and tailed array.


def restore_op(self, filename_tensor, saveable, preferred_shard):
    tensors = []
    for spec in saveable.specs:
        tensors.append(io_ops.restore_v2(filename_tensor, [spec.name], [spec.slice_spec], [spec.dtype])[0])
    return tensors

Create ops to restore 'saveable'.

This is intended to be overridden by subclasses that want to generate
different Ops.

Args:
filename_tensor: String Tensor.
saveable: A BaseSaverBuilder.SaveableObject object.
preferred_shard: Int.  Shard to open first when loading a sharded file.

Returns:
A list of Tensors resulting from reading 'saveable' from
'filename'.


def transform(self, X, y=None):

        word_ids = [self._word_vocab.doc2id(doc) for doc in X]
        word_ids = pad_sequences(word_ids, padding='post')

        char_ids = [[self._char_vocab.doc2id(w) for w in doc] for doc in X]
        char_ids = pad_nested_sequences(char_ids)

        character_ids = batch_to_ids(X)
        elmo_embeddings = self._elmo(character_ids)['elmo_representations'][1]
        elmo_embeddings = elmo_embeddings.detach().numpy()

        features = [word_ids, char_ids, elmo_embeddings]

        if y is not None:
            y = [self._label_vocab.doc2id(doc) for doc in y]
            y = pad_sequences(y, padding='post')
            y = to_categorical(y, self.label_size).astype(int)

            y = y if len(y.shape) == 3 else np.expand_dims(y, axis=0)
            return features, y
        else:
            return features

Transform documents to document ids.

Uses the vocabulary learned by fit.

Args:
X : iterable
an iterable which yields either str, unicode or file objects.
y : iterabl, label strings.

Returns:
features: document id matrix.
y: label id matrix.


def add_to_screen(self, screen_width, screen):

        for lineno, fields in enumerate(self.line_fields):
            for left, field in self.compute_positions(screen_width, fields):
                logger.debug(
                    "Adding field %s to screen %s at x=%d->%d, y=%d",
                    field, screen.ref, left, left + field.width - 1, 1 + lineno,
                )

                self.widgets[field] = field.add_to_screen(screen, left, 1 + lineno)
                self.register_hooks(field)

Add the pattern to a screen.

Also fills self.widgets.

Args:
screen_width (int): the width of the screen
screen (lcdprod.Screen): the screen to fill.


def _ParseInfo2Record(
      self, parser_mediator, file_object, record_offset, record_size):

    record_data = self._ReadData(file_object, record_offset, record_size)

    record_map = self._GetDataTypeMap('recycler_info2_file_entry')

    try:
      record = self._ReadStructureFromByteStream(
          record_data, record_offset, record_map)
    except (ValueError, errors.ParseError) as exception:
      raise errors.ParseError((
          'Unable to map record data at offset: 0x{0:08x} with error: '
          '{1!s}').format(record_offset, exception))

    codepage = parser_mediator.codepage or 'ascii'

    ascii_filename = record.original_filename.split(b'\x00')[0]

    try:
      ascii_filename = ascii_filename.decode(codepage)
    except UnicodeDecodeError:
      ascii_filename = ascii_filename.decode(codepage, errors='replace')

      parser_mediator.ProduceExtractionWarning(
          'unable to decode original filename.')

    unicode_filename = None
    if record_size > 280:
      record_offset += 280
      utf16_string_map = self._GetDataTypeMap(
          'recycler_info2_file_entry_utf16le_string')

      try:
        unicode_filename = self._ReadStructureFromByteStream(
            record_data[280:], record_offset, utf16_string_map)
      except (ValueError, errors.ParseError) as exception:
        raise errors.ParseError((
            'Unable to map record data at offset: 0x{0:08x} with error: '
            '{1!s}').format(record_offset, exception))

      unicode_filename = unicode_filename.rstrip('\x00')

    if record.deletion_time == 0:
      date_time = dfdatetime_semantic_time.SemanticTime('Not set')
    else:
      date_time = dfdatetime_filetime.Filetime(
          timestamp=record.deletion_time)

    event_data = WinRecycleBinEventData()
    event_data.drive_number = record.drive_number
    event_data.original_filename = unicode_filename or ascii_filename
    event_data.file_size = record.original_file_size
    event_data.offset = record_offset
    event_data.record_index = record.index

    if ascii_filename != unicode_filename:
      event_data.short_filename = ascii_filename

    event = time_events.DateTimeValuesEvent(
        date_time, definitions.TIME_DESCRIPTION_DELETED)
    parser_mediator.ProduceEventWithEventData(event, event_data)

Parses an INFO-2 record.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
file_object (dfvfs.FileIO): file-like object.
record_offset (int): record offset.
record_size (int): record size.

Raises:
ParseError: if the record cannot be read.


def merge_styles(inline_style, new_styles, classes, remove_unset_properties=False):
    styles = OrderedDict([('', OrderedDict())])
    for pc in set(classes):
        styles[pc] = OrderedDict()
    for (i, style) in enumerate(new_styles):
        for (k, v) in style:
            styles[classes[i]][k] = v
    if inline_style:
        for (k, v) in csstext_to_pairs(inline_style):
            styles[''][k] = v
    normal_styles = []
    pseudo_styles = []
    for (pseudoclass, kv) in styles.items():
        if remove_unset_properties:
            kv = OrderedDict(((k, v) for (k, v) in kv.items() if (not (v.lower() == 'unset'))))
        if (not kv):
            continue
        if pseudoclass:
            pseudo_styles.append(('%s{%s}' % (pseudoclass, '; '.join((('%s:%s' % (k, v)) for (k, v) in kv.items())))))
        else:
            normal_styles.append('; '.join((('%s:%s' % (k, v)) for (k, v) in kv.items())))
    if pseudo_styles:
        all_styles = (([('{%s}' % ''.join(normal_styles))] + pseudo_styles) if normal_styles else pseudo_styles)
    else:
        all_styles = normal_styles
    return ' '.join(all_styles).strip()

This will merge all new styles where the order is important
The last one will override the first
When that is done it will apply old inline style again
The old inline style is always important and override
all new ones. The inline style must be valid.

Args:
inline_style(str): the old inline style of the element if there
is one
new_styles: a list of new styles, each element should be
a list of tuple
classes: a list of classes which maps new_styles, important!
remove_unset_properties(bool): Allow us to remove certain CSS
properties with rules that set their value to 'unset'

Returns:
str: the final style


def create_run(cmd, project, exp, grp):
    from benchbuild.utils import schema as s
    session = s.Session()
    run = s.Run(command=str(cmd), project_name=project.name, project_group=project.group, experiment_name=exp, run_group=str(grp), experiment_group=project.experiment.id)
    session.add(run)
    session.commit()
    return (run, session)

Create a new 'run' in the database.

This creates a new transaction in the database and creates a new
run in this transaction. Afterwards we return both the transaction as
well as the run itself. The user is responsible for committing it when
the time comes.

Args:
cmd: The command that has been executed.
prj: The project this run belongs to.
exp: The experiment this run belongs to.
grp: The run_group (uuid) we blong to.

Returns:
The inserted tuple representing the run and the session opened with
the new run. Don't forget to commit it at some point.


def mount(self, app=None):

        for endpoint in self._routes:
            endpoint.register_app(app)

        return self

Mounts all registered routes to a bottle.py application instance.

Args:
app (instance): A `bottle.Bottle()` application instance.

Returns:
The Router instance (for chaining purposes).


def __init__(self, text: str, sn: "DataNode"):

        super().__init__(text)
        self.schema_node = sn

Extend the superclass method.

Args:
sn: Schema node from which the path starts.


def plot_chmap(cube, kidid, ax=None, **kwargs):
    if (ax is None):
        ax = plt.gca()
    index = np.where((cube.kidid == kidid))[0]
    if (len(index) == 0):
        raise KeyError('Such a kidid does not exist.')
    index = int(index)
    im = ax.pcolormesh(cube.x, cube.y, cube[(:, :, index)].T, **kwargs)
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_title('intensity map ch
    return im

Plot an intensity map.

Args:
cube (xarray.DataArray): Cube which the spectrum information is included.
kidid (int): Kidid.
ax (matplotlib.axes): Axis the figure is plotted on.
kwargs (optional): Plot options passed to ax.imshow().


def _resource_apply_sparse_duplicate_indices(self, grad, handle, indices):
    summed_grad, unique_indices = _deduplicate_indexed_slices(values=grad, indices=indices)
    return self._resource_apply_sparse(summed_grad, handle, unique_indices)

Add ops to apply sparse gradients to `handle`, with repeated indices.

Optimizers which override this method must deal with repeated indices. See
the docstring of `_apply_sparse_duplicate_indices` for details. By default
the correct behavior, to sum non-unique indices and their associated
gradients, is enforced by first pre-processing `grad` and `indices` and
passing them on to `_resource_apply_sparse`. Optimizers which deal correctly
with duplicate indices may instead override this method to avoid the
overhead of summing.

Args:
grad: a `Tensor` representing the gradient for the affected indices.
handle: a `Tensor` of dtype `resource` which points to the variable
to be updated.
indices: a `Tensor` of integral type representing the indices for
which the gradient is nonzero. Indices may be repeated.

Returns:
An `Operation` which updates the value of the variable.


def calculate_embedding(self, batch_image_bytes):

    return self.tf_session.run(
        self.embedding, feed_dict={self.input_jpeg: batch_image_bytes})

Get the embeddings for a given JPEG image.

Args:
batch_image_bytes: As if returned from [ff.read() for ff in file_list].

Returns:
The Inception embeddings (bottleneck layer output)


def _convert_concrete_functions_to_saved_model(self, output_dir):
    if len(self._funcs) == 0:
        raise ValueError('No ConcreteFunction is specified.')
    if not self.experimental_lower_to_saved_model:
        return (None, None, None)
    if not self._trackable_obj or isinstance(self._trackable_obj, (_function.ConcreteFunction, _def_function.Function)):
        return (None, None, None)
    signatures = {}
    signature_keys = []
    try:
        if len(self._funcs) == 1:
            signatures[_signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY] = self._funcs[0]
            signature_keys = [_signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
        else:
            for func in self._funcs:
                signatures[func.graph.name] = func
                signature_keys.append(func.graph.name)
        _save.save(self._trackable_obj, output_dir, signatures=signatures, options=_save_options.SaveOptions(save_debug_info=True))
    except Exception:
        return (None, None, None)
    self.saved_model_dir = output_dir
    self._saved_model_tags = set([_tag_constants.SERVING])
    self._saved_model_exported_names = signature_keys
    self._parse_saved_model_args(always_enable_saved_model_import=True)
    if self.saved_model_dir:
        graph_def, input_tensors, output_tensors = self._load_saved_model(self.saved_model_dir, self._saved_model_tags)
        self._trackable_obj = _load(self.saved_model_dir, self._saved_model_tags)
        return (graph_def, input_tensors, output_tensors)
    return (None, None, None)

Save concrete functions to the SavedModel format.

Args:
output_dir: The output directory to save the SavedModel.

Returns:
graph_def: The frozen GraphDef.
input_tensors: List of input tensors.
output_tensors: List of output tensors.


def series_with_permutation(self, other):
    combined_permutation = tuple([self.permutation[p] for p in other.permutation])
    return CPermutation.create(combined_permutation)

Compute the series product with another channel permutation circuit

Args:
other (CPermutation):

Returns:
Circuit: The composite permutation circuit (could also be the
identity circuit for n channels)


def sheets_clear(config, auth, sheet_url_or_name, sheet_tab, sheet_range):
    if config.verbose:
        print('SHEETS CLEAR', sheet_url_or_name, sheet_tab, sheet_range)
    sheet_id = sheets_id(config, auth, sheet_url_or_name)
    if sheet_id:
        API_Sheets(config, auth).spreadsheets().values().clear(spreadsheetId=sheet_id, range=sheets_tab_range(sheet_tab, sheet_range), body={}).execute()
    else:
        raise ValueError('Sheet does not exist for %s: %s' % (config, auth, sheet_url_or_name))

Clear a sheet in the specified range.

Args:
config - see starthinker/util/configuration.py
auth - user or service
url_or_name - one of: URL, document title, or id
sheet_tab - name of tab to get id for
sheet_range - A1 notation or blank if whole sheet

No Return


def site_specific_nn_occupation( self ):

        to_return = { l : 0 for l in set( ( site.label for site in self.p_neighbours ) ) }
        for site in self.p_neighbours:
            if site.is_occupied:
             to_return[ site.label ] += 1
        return to_return

Returns the number of occupied nearest neighbour sites, classified by site type.

Args:
None

Returns:
(Dict(Str:Int)): Dictionary of nearest-neighbour occupied site numbers, classified by site label, e.g. { 'A' : 2, 'B' : 1 }.


def _parse_method_block_line(self, instrumentation_block, line):
    if line.startswith(_InstrumentationStructurePrefixes.STATUS):
        instrumentation_block.set_key(_InstrumentationStructurePrefixes.STATUS, line)
        return instrumentation_block
    elif line.startswith(_InstrumentationStructurePrefixes.STATUS_CODE):
        instrumentation_block.set_status_code(line)
        return self._transition_instrumentation_block(instrumentation_block)
    elif line.startswith(_InstrumentationStructurePrefixes.RESULT):
        instrumentation_block.set_key(_InstrumentationStructurePrefixes.RESULT, line)
        return self._parse_result_line(self._transition_instrumentation_block(instrumentation_block, new_state=_InstrumentationBlockStates.RESULT), line)
    else:
        instrumentation_block.add_value(line)
        return instrumentation_block

Parses the instrumnetation method block's line.

Args:
instrumentation_block: _InstrumentationBlock, the current
instrumentation method block.
line: string, the raw instrumentation output line to parse.

Returns:
The next instrumentation block, which should be used to continue
parsing instrumentation output.


def pie(self, key='wall_time', minfract=0.05, ax=None, **kwargs):
    (ax, fig, plt) = get_ax_fig_plt(ax=ax)
    ax.axis('equal')
    (labels, vals) = self.names_and_values(key, minfract=minfract)
    ax.pie(vals, explode=None, labels=labels, autopct='%1.1f%%', shadow=True)
    return fig

Plot pie chart for this timer.

Args:
key: Keyword used to extract data from the timer.
minfract: Don't show sections whose relative weight is less that minfract.
ax: matplotlib :class:`Axes` or None if a new figure should be created.

Returns:
`matplotlib` figure


def _process_event(self, event):

        if (not event.is_directory and
                not event.src_path.endswith(BATCH_EXTENSION)):
            self._logger.info('Detected file change: %s', event.src_path)
            self._batch.process_file(event.src_path)

Process received events.

Process events received, applying normalization for those
events referencing a new or changed file and only if it's
not the result of a previous normalization.

Args:
event: Event to process.


def get_nac_eigendisplacements_along_dir(self, direction):

        versor = [i / np.linalg.norm(direction) for i in direction]
        for d, e in self.nac_eigendisplacements:
            if np.allclose(versor, d):
                return e

        return None

Returns the nac_eigendisplacements for the given direction (not necessarily a versor).
None if the direction is not present or nac_eigendisplacements has not been calculated.

Args:
direction: the direction as a list of 3 elements
Returns:
the eigendisplacements as a numpy array of complex numbers with shape
(3*len(structure), len(structure), 3). None if not found.


def csv_to_num_matrix(csv_file_path):

    mtx = []
    with open(csv_file_path) as csv_data_file:
        for row in csv_data_file:
            mtx.append([float(val) for val in row.split(',')])
    return mtx

Load a CSV file consisting only of numbers into a Python matrix of floats.

Args:
csv_file_path: Full path to a valid CSV file (e.g. c:/ladybug/test.csv)


def get_storage(request):
    storage_model = oauth2_settings.storage_model
    user_property = oauth2_settings.storage_model_user_property
    credentials_property = oauth2_settings.storage_model_credentials_property
    if storage_model:
        (module_name, class_name) = storage_model.rsplit('.', 1)
        module = importlib.import_module(module_name)
        storage_model_class = getattr(module, class_name)
        return storage.DjangoORMStorage(storage_model_class, user_property, request.user, credentials_property)
    else:
        return dictionary_storage.DictionaryStorage(request.session, key=_CREDENTIALS_KEY)

Gets a Credentials storage object provided by the Django OAuth2 Helper
object.

Args:
request: Reference to the current request object.

Returns:
An :class:`oauth2.client.Storage` object.


def bitwise_left_shift(x, y):
    if any_symbolic_tensors((x, y)):
        return BitwiseLeftShift().symbolic_call(x, y)
    return backend.numpy.bitwise_left_shift(x, y)

Shift the bits of an integer to the left.

Bits are shifted to the left by appending `y` 0s at the right of `x`.
Since the internal representation of numbers is in binary format, this
operation is equivalent to multiplying `x` by `2**y`.

Args:
x: Input integer tensor.
y: Input integer tensor.

Returns:
Result tensor.


def showAddColumnDialog(self, triggered):

        if triggered:
            dialog = AddAttributesDialog(self)
            dialog.accepted.connect(self.addColumn)
            dialog.rejected.connect(self.uncheckButton)
            dialog.show()

Display the dialog to add a column to the model.

This method is also a slot.

Args:
triggered (bool): If the corresponding button was
activated, the dialog will be created and shown.


async def populate_jsone_context(chain, parent_link, decision_link, tasks_for):
    task_ids = {'default': parent_link.task_id, 'decision': decision_link.task_id}
    source_url = get_source_url(decision_link)
    project = get_and_check_project(chain.context.config['valid_vcs_rules'], source_url)
    log.debug('task_ids: {}'.format(task_ids))
    jsone_context = {'now': parent_link.task['created'], 'as_slugid': (lambda x: task_ids.get(x, task_ids['default'])), 'tasks_for': tasks_for, 'repository': {'url': get_repo(decision_link.task, decision_link.context.config['source_env_prefix']), 'project': project}, 'ownTaskId': parent_link.task_id, 'taskId': None}
    if (chain.context.config['cot_product'] in ('mobile', 'application-services')):
        if (tasks_for == 'github-release'):
            jsone_context.update((await _get_additional_github_releases_jsone_context(decision_link)))
        elif (tasks_for == 'cron'):
            jsone_context.update(_get_additional_git_cron_jsone_context(decision_link))
        elif (tasks_for == 'github-pull-request'):
            jsone_context.update((await _get_additional_github_pull_request_jsone_context(decision_link)))
        elif (tasks_for == 'github-push'):
            jsone_context.update((await _get_additional_github_push_jsone_context(decision_link)))
        else:
            raise CoTError('Unknown tasks_for "{}" for cot_product "mobile"!'.format(tasks_for))
    else:
        jsone_context['repository']['level'] = (await get_scm_level(chain.context, project))
        if (tasks_for == 'action'):
            jsone_context.update((await _get_additional_hg_action_jsone_context(parent_link, decision_link)))
        elif (tasks_for == 'hg-push'):
            jsone_context.update((await _get_additional_hg_push_jsone_context(parent_link, decision_link)))
        elif (tasks_for == 'cron'):
            jsone_context.update((await _get_additional_hg_cron_jsone_context(parent_link, decision_link)))
        else:
            raise CoTError('Unknown tasks_for {}!'.format(tasks_for))
    log.debug('{} json-e context:'.format(parent_link.name))
    log.debug(pprint.pformat(jsone_context))
    return jsone_context

Populate the json-e context to rebuild ``parent_link``'s task definition.

This defines the context that `.taskcluster.yml` expects to be rendered
with.  See comments at the top of that file for details.

Args:
chain (ChainOfTrust): the chain of trust to add to.
parent_link (LinkOfTrust): the parent link to test.
decision_link (LinkOfTrust): the parent link's decision task link.
tasks_for (str): the reason the parent link was created (cron,
hg-push, action)

Raises:
CoTError, KeyError, ValueError: on failure.

Returns:
dict: the json-e context.


def add_state_sensors(self, agent_name, sensors):
    if isinstance(sensors, list):
        for sensor in sensors:
            self.add_state_sensors(agent_name, sensor)
    else:
        if (agent_name not in self._sensor_map):
            self._sensor_map[agent_name] = dict()
        self._sensor_map[agent_name][sensors] = self._client.malloc(((agent_name + '_') + Sensors.name(sensors)), Sensors.shape(sensors), Sensors.dtype(sensors))

Adds a sensor to a particular agent. This only works if the world you are running also includes
that particular sensor on the agent.

Args:
agent_name (str): The name of the agent to add the sensor to.
sensors (:obj:`HolodeckSensor` or list of :obj:`HolodeckSensor`): Sensors to add to the agent.
Should be objects that inherit from :obj:`HolodeckSensor`.


def __init__(self, server):

        self.ready = False
        self.server = server

        self.requests_seen = 0
        self.bytes_read = 0
        self.bytes_written = 0
        self.start_time = None
        self.work_time = 0
        self.stats = {
            'Requests': lambda s: self.requests_seen + (
                self.start_time is None
                and trueyzero
                or self.conn.requests_seen
            ),
            'Bytes Read': lambda s: self.bytes_read + (
                self.start_time is None
                and trueyzero
                or self.conn.rfile.bytes_read
            ),
            'Bytes Written': lambda s: self.bytes_written + (
                self.start_time is None
                and trueyzero
                or self.conn.wfile.bytes_written
            ),
            'Work Time': lambda s: self.work_time + (
                self.start_time is None
                and trueyzero
                or time.time() - self.start_time
            ),
            'Read Throughput': lambda s: s['Bytes Read'](s) / (
                s['Work Time'](s) or 1e-6
            ),
            'Write Throughput': lambda s: s['Bytes Written'](s) / (
                s['Work Time'](s) or 1e-6
            ),
        }
        threading.Thread.__init__(self)

Initialize WorkerThread instance.

Args:
server (cheroot.server.HTTPServer): web server object
receiving this request


def wait_for_registration(self, processor_type):
    with self._condition:
        self._condition.wait_for((lambda : ((processor_type in self) or self._cancelled_event.is_set())))
        if self._cancelled_event.is_set():
            raise WaitCancelledException()

Waits for a particular processor type to register or until
is_cancelled is True. is_cancelled cannot be part of this class
since we aren't cancelling all waiting for a processor_type,
but just this particular wait.

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

Returns:
None


def set_member_roles(self, guild_id: int, member_id: int, roles: List[int]):
    self._query(f'guilds/{guild_id}/members/{member_id}', 'PATCH', {'roles': roles}, expected_status=204)

Set the member's roles

This method takes a list of **role ids** that you want the user to have. This
method will **overwrite** all of the user's current roles with the roles in
the passed list of roles.

When calling this method, be sure that the list of roles that you're setting
for this user is complete, not just the roles that you want to add or remove.
For assistance in just adding or just removing roles, set the ``add_member_roles``
and ``remove_member_roles`` methods.

Args:
guild_id: snowflake id of the guild
member_id: snowflake id of the member
roles: list of snowflake ids of roles to set


def _finalize_job(cls, mapreduce_spec, mapreduce_state):
    config = util.create_datastore_write_config(mapreduce_spec)
    queue_name = util.get_queue_name(mapreduce_spec.params.get(model.MapreduceSpec.PARAM_DONE_CALLBACK_QUEUE))
    done_callback = mapreduce_spec.params.get(model.MapreduceSpec.PARAM_DONE_CALLBACK)
    done_task = None
    if done_callback:
        done_task = taskqueue.Task(url=done_callback, headers=util._get_task_headers(mapreduce_spec.mapreduce_id, util.CALLBACK_MR_ID_TASK_HEADER), method=mapreduce_spec.params.get('done_callback_method', 'POST'))

    @db.transactional(retries=5)
    def _put_state():
        'Helper to store state.'
        fresh_state = model.MapreduceState.get_by_job_id(mapreduce_spec.mapreduce_id)
        if (not fresh_state.active):
            logging.warning('Job %s is not active. Looks like spurious task execution. Dropping task.', mapreduce_spec.mapreduce_id)
            return
        mapreduce_state.put(config=config)
        if (done_task and (not _run_task_hook(mapreduce_spec.get_hooks(), 'enqueue_done_task', done_task, queue_name))):
            done_task.add(queue_name, transactional=True)
    _put_state()
    logging.info("Final result for job '%s' is '%s'", mapreduce_spec.mapreduce_id, mapreduce_state.result_status)
    cls._clean_up_mr(mapreduce_spec)

Finalize job execution.

Invokes done callback and save mapreduce state in a transaction,
and schedule necessary clean ups. This method is idempotent.

Args:
mapreduce_spec: an instance of MapreduceSpec
mapreduce_state: an instance of MapreduceState


def absolute_name(self, depth=0):
    (node, node_depth) = (self, self.depth)
    if (depth < 1):
        depth = node_depth
    while ((node_depth > depth) and (node.package is not None)):
        node = node.package
        node_depth -= 1
    names = []
    while (node is not None):
        names.append(node.name)
        node = node.package
    return '.'.join(reversed(names))

Return the absolute name of the node.

Concatenate names from root to self within depth.

Args:
depth (int): maximum depth to go to.

Returns:
str: absolute name of the node (until given depth is reached).


def build(cls: Type[AN], node: ast.stmt) -> List[AN]:
    if node_is_result_assignment(node):
        return [cls(node, ActNodeType.result_assignment)]
    if node_is_pytest_raises(node):
        return [cls(node, ActNodeType.pytest_raises)]
    if node_is_unittest_raises(node):
        return [cls(node, ActNodeType.unittest_raises)]
    token = node.first_token
    if token.line.strip().endswith('
        return [cls(node, ActNodeType.marked_act)]
    if isinstance(node, ast.With):
        return cls.build_body(node.body)
    return []

Starting at this ``node``, check if it's an act node. If it's a context
manager, recurse into child nodes.

Returns:
List of all act nodes found.


def _set_read_only_resource_inputs_attr(op: ops.Operation, branch_graphs):
    read_only_indices = set(range(len(op.inputs)))
    for branch_graph in branch_graphs:
        if not read_only_indices:
            break
        branch_read_only_indices = acd.get_read_only_resource_input_indices_graph(branch_graph)
        read_only_indices = read_only_indices.intersection(branch_read_only_indices)
    ops.set_int_list_attr(op, acd.READ_ONLY_RESOURCE_INPUTS_ATTR, sorted(read_only_indices))

Sets the list of resource inputs which are read-only.

This is used by AutomaticControlDependencies.

Args:
op: While Operation.
branch_graphs: List of branch FuncGraphs.


def create_task(*args, **kwargs) -> asyncio.Task:
    tg = task_group()
    if tg is None:
        task = asyncio.create_task(*args, **kwargs)
        _without_context_background_tasks.add(task)
        task.add_done_callback(_without_context_background_tasks.discard)
        return task
    return tg.create_task(*args, **kwargs)

Creates a task that uses the context TaskGroup.

If no context is available then `asyncio.create_task` will be used.

Args:
*args: Positional arguments to pass to `asyncio.create_task`.
**kwargs: Keyword arguments to pass to `asyncio.create_task`.

Returns:
An asyncio task.


def divide_to_patches(image: Union[np.array, 'torch.Tensor'], patch_size: int) -> list[Union[np.array, 'torch.Tensor']]:
    patches = []
    height, width = get_image_size(image, channel_dim=ChannelDimension.FIRST)
    for i in range(0, height, patch_size):
        for j in range(0, width, patch_size):
            patch = image[:, i:i + patch_size, j:j + patch_size]
            patches.append(patch)
    return patches

Divides an image into patches of a specified size.

Args:
image (`Union[np.array, "torch.Tensor"]`):
The input image.
patch_size (`int`):
The size of each patch.
Returns:
list: A list of Union[np.array, "torch.Tensor"] representing the patches.


def _AddProvidesEdges(self, rdf_artifact):
    for attribute in rdf_artifact.provides:
        self._AddEdge(rdf_artifact.name, attribute)

Add an edge for every attribute the given artifact provides.

This method adds a directed edge from the artifact node to every attribute
this artifact provides.

Args:
rdf_artifact: The artifact object.


def cos(cls, x: 'TensorFluent') -> 'TensorFluent':
    return cls._unary_op(x, tf.cos, tf.float32)

Returns a TensorFluent for the cos function.

Args:
x: The input fluent.

Returns:
A TensorFluent wrapping the cos function.


def find_sorted_task_dependencies(task, task_name, task_id):

    log.info("find_sorted_task_dependencies {} {}".format(task_name, task_id))

    cot_input_dependencies = [
        _craft_dependency_tuple(task_name, task_type, task_id)
        for task_type, task_id in task['extra'].get('chainOfTrust', {}).get('inputs', {}).items()
    ]

    upstream_artifacts_dependencies = [
        _craft_dependency_tuple(task_name, artifact_dict['taskType'], artifact_dict['taskId'])
        for artifact_dict in task.get('payload', {}).get('upstreamArtifacts', [])
    ]

    dependencies = [*cot_input_dependencies, *upstream_artifacts_dependencies]
    dependencies = _sort_dependencies_by_name_then_task_id(dependencies)

    parent_task_id = get_parent_task_id(task) or get_decision_task_id(task)
    parent_task_type = 'parent'

    parent_tuple = _craft_dependency_tuple(task_name, parent_task_type, parent_task_id)
    dependencies.insert(0, parent_tuple)

    log.info('found dependencies: {}'.format(dependencies))
    return dependencies

Find the taskIds of the chain of trust dependencies of a given task.

Args:
task (dict): the task definition to inspect.
task_name (str): the name of the task, for logging and naming children.
task_id (str): the taskId of the task.

Returns:
list: tuples associating dependent task ``name`` to dependent task ``taskId``.


def resolve_class(classref):

    if classref is None:
        return None
    elif isinstance(classref, six.class_types):
        return classref
    elif isinstance(classref, six.string_types):
        return import_class(classref)
    else:
        raise ValueError("Unable to resolve class for '%s'" % classref)

Attempt to return a Python class for the input class reference.

If `classref` is a class or None, return it. If `classref` is a
python classpath (e.g., "foo.bar.MyClass") import the class and return
it.

Args:
classref: A fully-qualified Python path to class, or a Python class.

Returns:
A class.


def handle_one_of(schema, field, validator, parent_schema):
    if validator.choices:
        schema['enum'] = list(validator.choices)
        schema['enumNames'] = list(validator.labels)
    return schema

Adds the validation logic for ``marshmallow.validate.OneOf`` by setting
the JSONSchema `enum` property to the allowed choices in the validator.

Args:
schema (dict): The original JSON schema we generated. This is what we
want to post-process.
field (fields.Field): The field that generated the original schema and
who this post-processor belongs to.
validator (marshmallow.validate.OneOf): The validator attached to the
passed in field.
parent_schema (marshmallow.Schema): The Schema instance that the field
belongs to.

Returns:
dict: A, possibly, new JSON Schema that has been post processed and
altered.


def get_card(self, card_id, **query_params):
    card_json = self.fetch_json(uri_path=((self.base_uri + '/cards/') + card_id))
    return self.create_card(card_json)

Get a Card for a given card id. Returns a Card object.

Returns:
Card: The card with the given card_id


def report_list(config, auth):
    for query in API_DBM(config, auth, iterate=True).queries().list().execute():
        yield query

Lists all the DBM report configurations for the current credentials.

Args:
* auth: (string) Either user or service.

Returns:
* Iterator of JSONs.


def check_data_type(self):

        metadata_type = self.column_metadata.get('type')
        if self.type != metadata_type and metadata_type not in self.type:
            raise ValueError('Types of transformer don\'t match')

Check the type of the transformer and column match.

Args:
column_metadata(dict): Metadata of the column.

Raises a ValueError if the types don't match


def deserialize(self, stamp_token, serialized_proto):
    return gen_boosted_trees_ops.boosted_trees_deserialize_ensemble(self.resource_handle, stamp_token, serialized_proto)

Deserialize the input proto and resets the ensemble from it.

Args:
stamp_token: int64 scalar Tensor to denote the stamp of the resource.
serialized_proto: string scalar Tensor of the serialized proto.

Returns:
Operation (for dependencies).


def from_pb(cls, pb):
    obj = cls._from_pb(pb)
    obj._pb = pb
    return obj

Instantiate the object from a protocol buffer.

Args:
pb (protobuf)

Save a reference to the protocol buffer on the object.


def get_regularization_losses(scope=None):
    return ops.get_collection(ops.GraphKeys.REGULARIZATION_LOSSES, scope)

Gets the list of regularization losses.

Args:
scope: An optional scope name for filtering the losses to return.

Returns:
A list of regularization losses as Tensors.


def view_page(name=None):
    if (request.method == 'POST'):
        if (name is None):
            if (len(request.forms.filename) > 0):
                name = request.forms.filename
        if (name is not None):
            filename = '{0}.rst'.format(name)
            file_handle = open(filename, 'w')
            file_handle.write(request.forms.content.encode('utf-8'))
            file_handle.close()
            add_file_to_repo(filename)
            commit(filename)
    response.set_header('Cache-control', 'no-cache')
    response.set_header('Pragma', 'no-cache')
    if (name is None):
        index_files = glob.glob('./[Ii][Nn][Dd][Ee][Xx].rst')
        if (len(index_files) == 0):
            return view_meta_index()
        else:
            name = index_files[0][2:(- 4)]
    files = glob.glob('{0}.rst'.format(name))
    if (len(files) > 0):
        file_handle = open(files[0], 'r')
        html_body = publish_parts(file_handle.read(), writer=AttowikiWriter(), settings=None, settings_overrides=None)['html_body']
        history = commit_history('{0}.rst'.format(name))
        return template('page', type='view', name=name, extended_name=None, is_repo=check_repo(), history=history, gitref=None, content=html_body)
    else:
        return static_file(name, '')

Serve a page name.

.. note:: this is a bottle view

* if the view is called with the POST method, write the new page
content to the file, commit the modification and then display the
html rendering of the restructured text file

* if the view is called with the GET method, directly display the html
rendering of the restructured text file

Keyword Arguments:
:name: (str) -- name of the rest file (without the .rst extension)
OPTIONAL

if no filename is given, first try to find a "index.rst" file in the
directory and serve it. If not found, serve the meta page __index__

Returns:
bottle response object


def get_room_by_name(self, name):
    rooms = self.get_rooms()
    for room in (rooms or []):
        if (room['name'] == name):
            return self.get_room(room['id'])
    raise RoomNotFoundException(('Room %s not found' % name))

Get a room by name.

Returns:
:class:`Room`. Room

Raises:
RoomNotFoundException


def compatible_firmware_version(self):
    identifier = self.firmware_version.split('compiled')[0]
    buf_size = self.MAX_BUF_SIZE
    buf = (ctypes.c_char * buf_size)()
    res = self._dll.JLINKARM_GetEmbeddedFWString(identifier.encode(), buf, buf_size)
    if (res < 0):
        raise errors.JLinkException(res)
    return ctypes.string_at(buf).decode()

Returns the DLL's compatible J-Link firmware version.

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

Returns:
The firmware version of the J-Link that the DLL is compatible
with.

Raises:
JLinkException: on error.


def forward(self, hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool]=False) -> Tuple[torch.FloatTensor]:
    residual = hidden_states
    hidden_states = self.layer_norm1(hidden_states)
    hidden_states, attn_weights = self.self_attn(hidden_states=hidden_states, head_mask=attention_mask, output_attentions=output_attentions)
    hidden_states = hidden_states + residual
    residual = hidden_states
    hidden_states = self.layer_norm2(hidden_states)
    hidden_states = self.mlp(hidden_states)
    hidden_states = hidden_states + residual
    outputs = (hidden_states,)
    if output_attentions:
        outputs += (attn_weights,)
    return outputs

Args:
hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
attention_mask (`torch.FloatTensor`): attention mask of size
`(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
`(config.encoder_attention_heads,)`.
output_attentions (`bool`, *optional*):
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
returned tensors for more detail.


def _export_to_saved_model_graph(self, object_map, tensor_map, options, **kwargs):
    _, _, _ = (object_map, tensor_map, options)
    del kwargs
    return []

Creates a copy of this object's tensors onto SavedModel graph.

Needs to be overridden if the class contains tensors that must be saved
into the graph. This method should update the `object_map` and `tensor_map`
dictionaries.

This method is called on all nodes in the Trackable Graph (generated by
`_trackable_children`). The nodes are traversed in the order defined by
`_deserialization_dependencies`

All usages of _map_resources should be migrated to this method.

Args:
object_map: A dictionary that maps original Trackables to the copied
Trackables. This only needs to be updated if the object is a
tf.function, or if the copied tensors are necessary for checkpointing
this object.
tensor_map: Dictionary mapping original tensors to copied tensors.
options: A `tf.saved_model.SaveOptions` object.
**kwargs: Additional kwargs that may be added at a later time.

Returns:
Flat list of original tensors that have been copied.


def __init__(self, counters=None, distributions=None, gauges=None, string_sets=None, bounded_tries=None):
    self.counters = counters or {}
    self.distributions = distributions or {}
    self.gauges = gauges or {}
    self.string_sets = string_sets or {}
    self.bounded_tries = bounded_tries or {}

Create a MetricUpdates object.

Args:
counters: Dictionary of MetricKey:MetricUpdate updates.
distributions: Dictionary of MetricKey:MetricUpdate objects.
gauges: Dictionary of MetricKey:MetricUpdate objects.
string_sets: Dictionary of MetricKey:MetricUpdate objects.
bounded_tries: Dictionary of MetricKey:MetricUpdate objects.


def update(self, **kwargs):
    for key, value in kwargs.items():
        if hasattr(self, key):
            setattr(self, key, value)

Update the configuration attributes with new values.

Args:
**kwargs: Keyword arguments representing configuration attributes and their new values.


def StringEscape(self, string, match, **unused_kwargs):

    if match.group(1) in '\\\'"rnbt\\.ws':
      self.string += codecs.decode(string, 'unicode_escape')
    else:
      raise errors.ParseError('Invalid escape character {0:s}.'.format(string))

Escape backslashes found inside a string quote.

Backslashes followed by anything other than [\'"rnbt.ws] will raise
an Error.

Args:
string: The string that matched.
match: the match object (instance of re.MatchObject).
Where match.group(1) contains the escaped code.

Raises:
ParseError: When the escaped string is not one of [\'"rnbt]


def get(self, key, mem_map=True):
    self.raise_error_if_not_open()
    if (key in self._file):
        data = self._file[key]
        sampling_rate = data.attrs[SAMPLING_RATE_ATTR]
        if (not mem_map):
            data = data[()]
        data = (np.float32(data) / MAX_INT16_VALUE)
        return (data, sampling_rate)

Return the samples for the given key and the sampling-rate.

Args:
key (str): The key to read the data from.
mem_map (bool): If ``True`` returns the data as
memory-mapped array, otherwise a copy is returned.

Note:
The container has to be opened in advance.

Returns:
tuple: A tuple containing the samples as numpy array
with ``np.float32`` [-1.0,1.0] and the sampling-rate.


def expected_exercise_fn(design, continuation_value, exercise_value):
    batch_design = tf.broadcast_to(tf.expand_dims(design, -1), design.shape + [continuation_value.shape[-1]])
    mask = tf.cast(exercise_value > 0, design.dtype)
    masked = tf.transpose(batch_design * mask, perm=(2, 1, 0))
    lhs = tf.matmul(masked, masked, transpose_a=True)
    lhs_pinv = tf.linalg.pinv(lhs)
    rhs = tf.matmul(masked, tf.expand_dims(tf.transpose(continuation_value), -1), transpose_a=True)
    beta = tf.linalg.matmul(lhs_pinv, rhs)
    continuation = tf.matmul(tf.transpose(batch_design, perm=(2, 1, 0)), beta)
    return tf.maximum(tf.transpose(tf.squeeze(continuation, -1)), 0.0)

Returns the expected continuation value for each path.

Args:
design: A real `Tensor` of shape `[basis_size, num_samples]`.
continuation_value: A `Tensor` of shape `[num_samples, payoff_dim]` and of
the same dtype as `design`. The optimal value of the option conditional on
not exercising now or earlier, taking future information into account.
exercise_value: A `Tensor` of the same shape and dtype as
`continuation_value`. Value of the option if exercised immideately at
the current time

Returns:
A `Tensor` of the same shape and dtype as `continuation_value` whose
`(n, v)`-th entry represents the expected continuation value of sample path
`n` under the `v`-th payoff scheme.


def match_partial_against_complete(self, matcher, solver, partial, complete):
    assert is_partial(partial)
    assert is_complete(complete)
    subst = {p.type_param: pytd.AnythingType() for p in complete.template}
    formula = matcher.match_Class_against_Class(partial, complete, subst)
    if formula is booleq.FALSE:
        raise FlawedQuery(f'{partial.name} can never be {complete.name}')
    solver.always_true(formula)

Match a partial class (call record) against a complete class.

Args:
matcher: An instance of pytd.type_match.TypeMatch.
solver: An instance of pytd.booleq.Solver.
partial: The partial class to match. The class name needs to be prefixed
with "~" - the rest of the name is typically the same as complete.name.
complete: A complete class to match against. (E.g. a built-in or a user
defined class)

Returns:
An instance of pytd.booleq.BooleanTerm.
Raises:
FlawedQuery: If this call record is incompatible with the builtin.


def _init_init_op(self, init_op=USE_DEFAULT, init_feed_dict=None):
    if init_op is Supervisor.USE_DEFAULT:
        init_op = self._get_first_op_from_collection(ops.GraphKeys.INIT_OP)
        if init_op is None:
            init_op = variables.global_variables_initializer()
            ops.add_to_collection(ops.GraphKeys.INIT_OP, init_op)
    self._init_op = init_op
    self._init_feed_dict = init_feed_dict

Initializes init_op.

Args:
init_op: `Operation` to initialize the variables. If set to USE_DEFAULT,
create an op that initializes all variables and tables.
init_feed_dict: A dictionary that maps `Tensor` objects to feed values.
This feed dictionary will be used when `init_op` is evaluated.


def VerifyStructure(self, parser_mediator, lines):

    try:
      structure = self._SDF_HEADER.parseString(lines)
    except pyparsing.ParseException:
      logger.debug('Not a SkyDrive log file')
      return False

    try:
      dfdatetime_time_elements.TimeElementsInMilliseconds(
          time_elements_tuple=structure.header_date_time)
    except ValueError:
      logger.debug(
          'Not a SkyDrive log file, invalid date and time: {0!s}'.format(
              structure.header_date_time))
      return False

    return True

Verify that this file is a SkyDrive log file.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
lines (str): one or more lines from the text file.

Returns:
bool: True if this is the correct parser, False otherwise.


def convert_sbml_model(model):
    biomass_reactions = set()
    for reaction in model.reactions:
        if (reaction.id not in model.limits):
            (lower, upper) = parse_flux_bounds(reaction)
            if ((lower is not None) or (upper is not None)):
                model.limits[reaction.id] = (reaction.id, lower, upper)
        objective = parse_objective_coefficient(reaction)
        if ((objective is not None) and (objective != 0)):
            biomass_reactions.add(reaction.id)
    if (len(biomass_reactions) == 1):
        model.biomass_reaction = next(iter(biomass_reactions))
    convert_model_entries(model)
    if (model.extracellular_compartment is None):
        extracellular = detect_extracellular_compartment(model)
        model.extracellular_compartment = extracellular
    convert_exchange_to_compounds(model)

Convert raw SBML model to extended model.

Args:
model: :class:`NativeModel` obtained from :class:`SBMLReader`.


def ifilterfalse_items(item_iter, flag_iter):

    false_items = (item for (item, flag) in zip(item_iter, flag_iter) if not flag)
    return false_items

ifilterfalse_items

Args:
item_iter (list):
flag_iter (list): of bools

Example:
>>> # ENABLE_DOCTEST
>>> from utool.util_iter import *  # NOQA
>>> item_iter = [1, 2, 3, 4, 5]
>>> flag_iter = [False, True, True, False, True]
>>> false_items = ifilterfalse_items(item_iter, flag_iter)
>>> result = list(false_items)
>>> print(result)
[1, 4]


def GetGtfsClassByFileName(self, filename):
    if (filename not in self._file_mapping):
        return None
    mapping = self._file_mapping[filename]
    class_list = mapping['classes']
    if (len(class_list) > 1):
        raise problems.NonStandardMapping(filename)
    else:
        return self._class_mapping[class_list[0]]

Returns the transitfeed class corresponding to a GTFS file.

Args:
filename: The filename whose class is to be returned

Raises:
NonStandardMapping if the specified filename has more than one
corresponding class


def to_dict(self) -> Dict[str, Any]:
    output = copy.deepcopy(self.__dict__)
    output['feature_extractor_type'] = self.__class__.__name__
    if 'mel_filters' in output:
        del output['mel_filters']
    if 'mel_filters_slaney' in output:
        del output['mel_filters_slaney']
    return output

Serializes this instance to a Python dictionary.

Returns:
`Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance, except for the
mel filter banks, which do not need to be saved or printed as they are too long.


def _parse_name(self, config):

        value = NAME_RE.search(config).group('value')
        return dict(name=value)

_parse_name scans the provided configuration block and extracts
the vlan name.  The config block is expected to always return the
vlan name.  The return dict is intended to be merged into the response
dict.

Args:
config (str): The vlan configuration block from the nodes running
configuration

Returns:
dict: resource dict attribute


def executions(self, digest=False, begin=None, end=None):
    digests = self._execution_digests
    if begin is not None or end is not None:
        begin = begin or 0
        end = end or len(digests)
        digests = digests[begin:end]
    if digest:
        return digests
    else:
        return [self.read_execution(digest) for digest in digests]

Get `Execution`s or `ExecutionDigest`s this reader has read so far.

Args:
digest: Whether the results are returned in a digest form, i.e.,
`ExecutionDigest` format, instead of the more detailed `Execution`
format.
begin: Optional beginning index for the requested execution data objects
or their digests. Python-style negative indices are supported.
end: Optional ending index for the requested execution data objects or
their digests. Python-style negative indices are supported.

Returns:
If `digest`: a `list` of `ExecutionDigest` objects.
Else: a `list` of `Execution` objects.


def children_after_parents(self, piper1, piper2):

        if piper1 in self[piper2].deep_nodes():
            return 1
        elif piper2 in self[piper1].deep_nodes():
            return - 1
        else:
            return 0

Custom compare function. Returns ``1`` if the first ``Piper`` instance
is upstream of the second ``Piper`` instance, ``-1`` if the first
``Piper`` is downstream of the second ``Piper`` and ``0`` if the two
``Pipers`` are independent.

Arguments:

- piper1(``Piper``) ``Piper`` instance.
- piper2(``Piper``) ``Piper`` instance.


def _convert_dataset_to_list(dataset, dataset_type_spec, data_size_warning_flag=True, ensure_shape_similarity=True):
    dataset_iterator = _get_data_iterator_from_dataset(dataset, dataset_type_spec)
    dataset_as_list = []
    start_time = time.time()
    for sample in _get_next_sample(dataset_iterator, ensure_shape_similarity, data_size_warning_flag, start_time):
        dataset_as_list.append(sample)
    return dataset_as_list

Convert `dataset` object to a list of samples.

Args:
dataset: A `tf.data.Dataset`, a `torch.utils.data.Dataset` object,
or a list/tuple of arrays.
dataset_type_spec: the type of the dataset.
data_size_warning_flag: If set to `True`, a warning will
be issued if the dataset takes longer than 10 seconds to iterate.
Defaults to `True`.
ensure_shape_similarity: If set to `True`, the shape of
the first sample will be used to validate the shape of rest of the
samples. Defaults to `True`.

Returns:
List: A list of samples.


def commit(self, synchronized_processing_time):
    assert not self._committed
    self._committed = True
    self._elements = tuple(self._elements)
    self._synchronized_processing_time = synchronized_processing_time

Commits this bundle.

Uncommitted bundle will become committed (immutable) after this call.

Args:
synchronized_processing_time: the synchronized processing time at which
this bundle was committed


def save_dataset(self, out_file_name):

        for time_tag, script in self.data_sets.items():
            script.save(os.path.join(out_file_name, '{:s}.b26s'.format(time_tag)))

saves current dataset to out_file_name
Args:
out_file_name: name of file


def is_valid(self, field_name, value) -> (bool, object):
    if self.has_field(field_name):
        if (self.fields_dict[field_name] == FieldType.KG_ID):
            return (True, value)
        if (self.fields_dict[field_name] == FieldType.NUMBER):
            if isinstance(value, numbers.Number):
                return (True, value)
            else:
                converted_number = self.parse_number(value)
                return ((False, value) if (not converted_number) else (True, value))
        if (self.fields_dict[field_name] == FieldType.STRING):
            if isinstance(value, str):
                return (True, value.strip())
            else:
                return (True, str(value).strip())
        if (self.fields_dict[field_name] == FieldType.DATE):
            (valid, d) = self.is_date(value)
            if valid:
                return (True, d.isoformat())
            else:
                return (False, value)
        if (self.fields_dict[field_name] == FieldType.LOCATION):
            (valid, l) = self.is_location(value)
            if valid:
                return (True, l)
            else:
                return (False, value)
    else:
        print('{} not found in KG Schema'.format(field_name))
        return (False, value)

Return true if the value type matches or can be coerced to the defined type in schema, otherwise false.
If field not defined, return none

Args:
field_name: str
value:

Returns: bool, value, where the value may have been coerced to the required type.


def __init__(self, data_type_definition):

    super(UUIDMap, self).__init__(data_type_definition)
    self._byte_order = data_type_definition.byte_order

Initializes an UUID (or GUID) data type map.

Args:
data_type_definition (DataTypeDefinition): data type definition.


def _lookup_namespace(self, symbol, namespace):
    for namespace_part in symbol.parts:
        namespace = namespace.get(namespace_part)
        if (namespace is None):
            break
        if (not isinstance(namespace, dict)):
            return namespace
    raise Error(('%s not found' % symbol.name))

Helper for lookup_symbol that only looks up variables in a
namespace.

Args:
symbol: Symbol
namespace: pointer into self.namespaces


def chunk_sequence(sequence, chunk_length=200, padding_value=0):
    if ('length' in sequence):
        length = sequence.pop('length')
    else:
        length = tf.shape(tools.nested.flatten(sequence)[0])[0]
    num_chunks = (((length - 1)
    padding_length = ((chunk_length * num_chunks) - length)
    padded = tools.nested.map((lambda tensor: tf.concat([tensor, ((0 * tensor[:padding_length]) + padding_value)], 0)), sequence)
    chunks = tools.nested.map((lambda tensor: tf.reshape(tensor, ([num_chunks, chunk_length] + tensor.shape[1:].as_list()))), padded)
    chunks['length'] = tf.concat([(chunk_length * tf.ones(((num_chunks - 1),), dtype=tf.int32)), [(chunk_length - padding_length)]], 0)
    return chunks

Split a nested dict of sequence tensors into a batch of chunks.

This function does not expect a batch of sequences, but a single sequence. A
`length` key is added if it did not exist already.

Args:
sequence: Nested dict of tensors with time dimension.
chunk_length: Size of chunks the sequence will be split into.
padding_value: Value used for padding the last chunk after the sequence.

Returns:
Nested dict of sequence tensors with chunk dimension.


def get_or_create_direct_channel(cls, initiator_key, receiver_key):

        existing = cls.objects.OR().filter(
            code_name='%s_%s' % (initiator_key, receiver_key)).filter(
            code_name='%s_%s' % (receiver_key, initiator_key))
        receiver_name = UserModel.objects.get(receiver_key).full_name
        if existing:
            channel = existing[0]
        else:
            channel_name = '%s_%s' % (initiator_key, receiver_key)
            channel = cls(is_direct=True, code_name=channel_name, typ=10).blocking_save()
        with BlockSave(Subscriber):
            Subscriber.objects.get_or_create(channel=channel,
                                             user_id=initiator_key,
                                             name=receiver_name)
            Subscriber.objects.get_or_create(channel=channel,
                                             user_id=receiver_key,
                                             name=UserModel.objects.get(initiator_key).full_name)
        return channel, receiver_name

Creates a  direct messaging channel between two user

Args:
initiator: User, who want's to make first contact
receiver: User, other party

Returns:
(Channel, receiver_name)


def ParseMessage(descriptor, byte_str):

  result_class = MakeClass(descriptor)
  new_msg = result_class()
  new_msg.ParseFromString(byte_str)
  return new_msg

Generate a new Message instance from this Descriptor and a byte string.

Args:
descriptor: Protobuf Descriptor object
byte_str: Serialized protocol buffer byte string

Returns:
Newly created protobuf Message object.


def match(self, request):

        errors = []

        def match(matcher):
            try:
                return matcher.match(request)
            except Exception as err:
                err = '{}: {}'.format(type(matcher).__name__, err)
                errors.append(err)
                return False

        return all([match(matcher) for matcher in self]), errors

Match the given HTTP request instance against the registered
matcher functions in the current engine.

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

Returns:
tuple(bool, list[Exception]): ``True`` if all matcher tests
passes, otherwise ``False``. Also returns an optional list
of error exceptions.


def _process_image_files_batch(coder, thread_index, ranges, name, filenames, texts, labels, num_shards):
    num_threads = len(ranges)
    assert (not (num_shards % num_threads))
    num_shards_per_batch = int((num_shards / num_threads))
    shard_ranges = np.linspace(ranges[thread_index][0], ranges[thread_index][1], (num_shards_per_batch + 1)).astype(int)
    num_files_in_thread = (ranges[thread_index][1] - ranges[thread_index][0])
    counter = 0
    for s in range(num_shards_per_batch):
        shard = ((thread_index * num_shards_per_batch) + s)
        output_filename = ('%s-%.5d-of-%.5d' % (name, shard, num_shards))
        output_file = os.path.join(FLAGS.output_directory, output_filename)
        writer = tf.python_io.TFRecordWriter(output_file)
        shard_counter = 0
        files_in_shard = np.arange(shard_ranges[s], shard_ranges[(s + 1)], dtype=int)
        for i in files_in_shard:
            filename = filenames[i]
            label = labels[i]
            text = texts[i]
            (image_buffer, height, width) = _process_image(filename, coder)
            example = _convert_to_example(filename, image_buffer, label, text, height, width)
            writer.write(example.SerializeToString())
            shard_counter += 1
            counter += 1
            if (not (counter % 1000)):
                print(('%s [thread %d]: Processed %d of %d images in thread batch.' % (datetime.now(), thread_index, counter, num_files_in_thread)))
                sys.stdout.flush()
        writer.close()
        print(('%s [thread %d]: Wrote %d images to %s' % (datetime.now(), thread_index, shard_counter, output_file)))
        sys.stdout.flush()
        shard_counter = 0
    print(('%s [thread %d]: Wrote %d images to %d shards.' % (datetime.now(), thread_index, counter, num_files_in_thread)))
    sys.stdout.flush()

Processes and saves list of images as TFRecord in 1 thread.

Args:
coder: instance of ImageCoder to provide TensorFlow image coding utils.
thread_index: integer, unique batch to run index is within [0, len(ranges)).
ranges: list of pairs of integers specifying ranges of each batches to
analyze in parallel.
name: string, unique identifier specifying the data set
filenames: list of strings; each string is a path to an image file
texts: list of strings; each string is human readable, e.g. 'dog'
labels: list of integer; each integer identifies the ground truth
num_shards: integer number of shards for this data set.


def _rewrite_output_as_tensor(body_grad_graph, grad_output_slices):
    with body_grad_graph.as_default():
        new_output = tensor_conversion.convert_to_tensor_v2(grad_output_slices)
    idx = _get_tensor_index_in_iterable(body_grad_graph.structured_outputs, grad_output_slices)
    body_grad_graph.structured_outputs[idx] = new_output
    body_grad_graph.outputs = func_graph.flatten(body_grad_graph.structured_outputs)

Rewrites grad_output_slices to be a Tensor output.

Args:
body_grad_graph: _WhileBodyGradFuncGraph.
grad_output_slices: IndexedSlices output of body_grad_graph.


def _path_to_str(self, path):

        inp = ''
        for arc in path:
            i = self.isyms.find(arc.ilabel)

            if i != fst.EPSILON:
                inp += i
        return inp

Convert a path to the string representing the path
Args:
path (tuple): A tuple of arcs
Returns:
inp (str): The path concatenated as as string


def _get_table(name):
    item = google.datalab.utils.commands.get_notebook_item(name)
    if isinstance(item, bigquery.Table):
        return item
    try:
        return _existing_table_cache[name]
    except KeyError:
        table = bigquery.Table(name)
        if table.exists():
            _existing_table_cache[name] = table
            return table
    return None

Given a variable or table name, get a Table if it exists.

Args:
name: the name of the Table or a variable referencing the Table.
Returns:
The Table, if found.


def _SetSshHostKeys(self, host_key_types=None):

    section = 'Instance'
    instance_id = self._GetInstanceId()
    if instance_id != self.instance_config.GetOptionString(
        section, 'instance_id'):
      self.logger.info('Generating SSH host keys for instance %s.', instance_id)
      file_regex = re.compile(r'ssh_host_(?P<type>[a-z0-9]*)_key\Z')
      key_dir = '/etc/ssh'
      key_files = [f for f in os.listdir(key_dir) if file_regex.match(f)]
      key_types = host_key_types.split(',') if host_key_types else []
      key_types_files = ['ssh_host_%s_key' % key_type for key_type in key_types]
      for key_file in set(key_files) | set(key_types_files):
        key_type = file_regex.match(key_file).group('type')
        key_dest = os.path.join(key_dir, key_file)
        self._GenerateSshKey(key_type, key_dest)
      self._StartSshd()
      self.instance_config.SetOption(section, 'instance_id', str(instance_id))

Regenerates SSH host keys when the VM is restarted with a new IP address.

Booting a VM from an image with a known SSH key allows a number of attacks.
This function will regenerating the host key whenever the IP address
changes. This applies the first time the instance is booted, and each time
the disk is used to boot a new instance.

Args:
host_key_types: string, a comma separated list of host key types.


def on_message(self, message):

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

Runs on a create_message event from websocket connection

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


def append_paulis(self, paulis=None, pauli_labels=None):
    return self.insert_paulis(None, paulis=paulis, pauli_labels=pauli_labels)

Append pauli at the end.

Args:
paulis (Pauli): the to-be-inserted or appended pauli
pauli_labels (list[str]): the to-be-inserted or appended pauli label

Returns:
Pauli: self


def __recognize_union(self, node: yaml.Node,
                          expected_type: Type) -> RecResult:

        logger.debug('Recognizing as a union')
        recognized_types = []
        message = ''
        union_types = generic_type_args(expected_type)
        logger.debug('Union types {}'.format(union_types))
        for possible_type in union_types:
            recognized_type, msg = self.recognize(node, possible_type)
            if len(recognized_type) == 0:
                message += msg
            recognized_types.extend(recognized_type)
        recognized_types = list(set(recognized_types))
        if bool in recognized_types and bool_union_fix in recognized_types:
            recognized_types.remove(bool_union_fix)

        if len(recognized_types) == 0:
            return recognized_types, message
        elif len(recognized_types) > 1:
            message = ('{}{}Could not determine which of the following types'
                       ' this is: {}').format(node.start_mark, os.linesep,
                                              recognized_types)
            return recognized_types, message

        return recognized_types, ''

Recognize a node that we expect to be one of a union of types.

Args:
node: The node to recognize.
expected_type: Union[...something...]

Returns:
The specific type that was recognized, multiple, or none.


def _validate_chain_strength(sampler, chain_strength):
    properties = sampler.properties
    if ('extended_j_range' in properties):
        max_chain_strength = (- min(properties['extended_j_range']))
    elif ('j_range' in properties):
        max_chain_strength = (- min(properties['j_range']))
    else:
        raise ValueError("input sampler should have 'j_range' and/or 'extended_j_range' property.")
    if (chain_strength is None):
        chain_strength = max_chain_strength
    elif (chain_strength > max_chain_strength):
        raise ValueError('Provided chain strength exceedds the allowed range.')
    return chain_strength

Validate the provided chain strength, checking J-ranges of the sampler's children.

Args:
chain_strength (float) The provided chain strength.  Use None to use J-range.

Returns (float):
A valid chain strength, either provided or based on available J-range.  Positive finite float.


def _relative_position_to_absolute_position_masked(x):
    (batch, heads, length, _) = common_layers.shape_list(x)
    x = tf.pad(x, [[0, 0], [0, 0], [0, 0], [1, 0]])
    x = tf.reshape(x, [batch, heads, (1 + length), length])
    x = tf.slice(x, [0, 0, 1, 0], [(- 1), (- 1), (- 1), (- 1)])
    return x

Helper to dot_product_self_attention_relative_v2.

Rearrange an attention logits or weights Tensor.

The dimensions of the input represent:
[batch, heads, query_position, memory_position - query_position + length - 1]

The dimensions of the output represent:
[batch, heads, query_position, memory_position]

Only works with masked_attention.  Undefined behavior for regions of the
input where memory_position > query_position.

Args:
x: a Tensor with shape [batch, heads, length, length]

Returns:
a Tensor with shape [batch, heads, length, length]


def phone_text_subs():
    Small = {'zero': 0, 'zer0': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'fuor': 4, 'five': 5, 'fith': 5, 'six': 6, 'seven': 7, 'sven': 7, 'eight': 8, 'nine': 9, 'ten': 10, 'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourteen': 14, 'fifteen': 15, 'sixteen': 16, 'seventeen': 17, 'eighteen': 18, 'nineteen': 19, 'twenty': 20, 'thirty': 30, 'forty': 40, 'fifty': 50, 'sixty': 60, 'seventy': 70, 'eighty': 80, 'ninety': 90, 'oh': 0}
    Magnitude = {'thousand': 0, 'million': 0}
    Others = {'!': 1, 'o': 0, 'l': 1, 'i': 1}
    output = {}
    output['Small'] = Small
    output['Magnitude'] = Magnitude
    output['Others'] = Others
    return output

Gets a dictionary of dictionaries that each contain alphabetic number manifestations mapped to their actual
Number value.

Returns:
dictionary of dictionaries containing Strings mapped to Numbers


def get_internal_modules(key='exa'):
    key += '.'
    return [v for (k, v) in sys.modules.items() if k.startswith(key)]

Get a list of modules belonging to the given package.

Args:
key (str): Package or library name (e.g. "exa")


def mach60(msg):
    d = hex2bin(data(msg))
    if (d[23] == '0'):
        return None
    mach = ((bin2int(d[24:34]) * 2.048) / 512.0)
    return round(mach, 3)

Aircraft MACH number

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

Returns:
float: MACH number


def get_v2_names(symbol: Any) -> Sequence[str]:
    names_v2 = []
    tensorflow_api_attr = API_ATTRS[TENSORFLOW_API_NAME].names
    keras_api_attr = API_ATTRS[KERAS_API_NAME].names
    if not hasattr(symbol, '__dict__'):
        return names_v2
    if tensorflow_api_attr in symbol.__dict__:
        names_v2.extend(getattr(symbol, tensorflow_api_attr))
    if keras_api_attr in symbol.__dict__:
        names_v2.extend(getattr(symbol, keras_api_attr))
    return names_v2

Get a list of TF 2.0 names for this symbol.

Args:
symbol: symbol to get API names for.

Returns:
List of all API names for this symbol.


def get_reference_points(spatial_shapes, valid_ratios, device):
    reference_points_list = []
    for level, (height, width) in enumerate(spatial_shapes):
        ref_y, ref_x = meshgrid(torch.linspace(0.5, height - 0.5, height, dtype=valid_ratios.dtype, device=device), torch.linspace(0.5, width - 0.5, width, dtype=valid_ratios.dtype, device=device), indexing='ij')
        ref_y = ref_y.reshape(-1)[None] / (valid_ratios[:, None, level, 1] * height)
        ref_x = ref_x.reshape(-1)[None] / (valid_ratios[:, None, level, 0] * width)
        ref = torch.stack((ref_x, ref_y), -1)
        reference_points_list.append(ref)
    reference_points = torch.cat(reference_points_list, 1)
    reference_points = reference_points[:, :, None] * valid_ratios[:, None]
    return reference_points

Get reference points for each feature map. Used in decoder.

Args:
spatial_shapes (`torch.LongTensor` of shape `(num_feature_levels, 2)`):
Spatial shapes of each feature map.
valid_ratios (`torch.FloatTensor` of shape `(batch_size, num_feature_levels, 2)`):
Valid ratios of each feature map.
device (`torch.device`):
Device on which to create the tensors.
Returns:
`torch.FloatTensor` of shape `(batch_size, num_queries, num_feature_levels, 2)`


async def invoke(self, context):

        try:

            tasks = await self._run_cancellable(claim_work(context))
            if not tasks or not tasks.get('tasks', []):
                await self._run_cancellable(asyncio.sleep(context.config['poll_interval']))
                return None

            status = None
            for task_defn in tasks.get('tasks', []):
                prepare_to_run_task(context, task_defn)
                reclaim_fut = context.event_loop.create_task(reclaim_task(context, context.task))
                try:
                    status = await do_run_task(context, self._run_cancellable, self._to_cancellable_process)
                    artifacts_paths = filepaths_in_dir(context.config['artifact_dir'])
                except WorkerShutdownDuringTask:
                    shutdown_artifact_paths = [os.path.join('public', 'logs', log_file)
                                               for log_file in ['chain_of_trust.log', 'live_backing.log']]
                    artifacts_paths = [path for path in shutdown_artifact_paths
                                       if os.path.isfile(os.path.join(context.config['artifact_dir'], path))]
                    status = STATUSES['worker-shutdown']
                status = worst_level(status, await do_upload(context, artifacts_paths))
                await complete_task(context, status)
                reclaim_fut.cancel()
                cleanup(context)

            return status

        except asyncio.CancelledError:
            return None

Claims and processes Taskcluster work.

Args:
context (scriptworker.context.Context): context of worker

Returns: status code of build


def _sendline(self, line):

        self.lines = []
        try:
            self._read()
        except socket.error:
            logging.debug('Nothing cleared')

        logger.debug('sending [%s]', line)
        self._write(line + '\r\n')

        time.sleep(0.5)

Send exactly one line to the device

Args:
line str: data send to device