def repeat_op(repetitions, inputs, op, *args, **kwargs):
    scope = kwargs.pop('scope', None)
    with tf.variable_scope(scope, 'RepeatOp', [inputs]):
        tower = inputs
        for _ in range(repetitions):
            tower = op(tower, *args, **kwargs)
        return tower

Build a sequential Tower starting from inputs by using an op repeatedly.

It creates new scopes for each operation by increasing the counter.
Example: given repeat_op(3, _, ops.conv2d, 64, [3, 3], scope='conv1')
it will repeat the given op under the following variable_scopes:
conv1/Conv
conv1/Conv_1
conv1/Conv_2

Args:
repetitions: number or repetitions.
inputs: a tensor of size [batch_size, height, width, channels].
op: an operation.
*args: args for the op.
**kwargs: kwargs for the op.

Returns:
a tensor result of applying the operation op, num times.
Raises:
ValueError: if the op is unknown or wrong.


def chat_delete(self, *, channel: str, ts: str, **kwargs) -> SlackResponse:

        kwargs.update({"channel": channel, "ts": ts})
        return self.api_call("chat.delete", json=kwargs)

Deletes a message.

Args:
channel (str): Channel containing the message to be deleted. e.g. 'C1234567890'
ts (str): Timestamp of the message to be deleted. e.g. '1234567890.123456'


def AddEventTag(self, event_tag):

    self._RaiseIfNotWritable()

    event_identifier = event_tag.GetEventIdentifier()
    if not isinstance(event_identifier, identifiers.SQLTableIdentifier):
      raise IOError('Unsupported event identifier type: {0:s}'.format(
          type(event_identifier)))

    event_tag.event_row_identifier = event_identifier.row_identifier

    self._AddAttributeContainer(self._CONTAINER_TYPE_EVENT_TAG, event_tag)

Adds an event tag.

Args:
event_tag (EventTag): event tag.

Raises:
IOError: when the storage file is closed or read-only or
if the event identifier type is not supported.
OSError: when the storage file is closed or read-only or
if the event identifier type is not supported.


def __call__(self, shape, dtype=dtypes.float32, **kwargs):
    self._validate_kwargs(kwargs, support_partition=False)
    dtype = _assert_float_dtype(dtype)
    if len(shape) < 2:
        raise ValueError(f'The tensor to initialize, specified by argument `shape` must be at least two-dimensional. Received shape={shape}')
    num_rows = 1
    for dim in shape[:-1]:
        num_rows *= dim
    num_cols = shape[-1]
    flat_shape = (max(num_cols, num_rows), min(num_cols, num_rows))
    a = self._random_generator.random_normal(flat_shape, dtype=dtype)
    q, r = gen_linalg_ops.qr(a, full_matrices=False)
    d = array_ops.diag_part(r)
    q *= math_ops.sign(d)
    if num_rows < num_cols:
        q = array_ops.matrix_transpose(q)
    return self.gain * array_ops.reshape(q, shape)

Returns a tensor object initialized as specified by the initializer.

Args:
shape: Shape of the tensor.
dtype: Optional dtype of the tensor. Only floating point types are
supported.
**kwargs: Additional keyword arguments.

Raises:
ValueError: If the dtype is not floating point or the input shape is not
valid.


def _obtain_sampled_health_pills(self, run, node_names):
    runs_to_tags_to_content = self._event_multiplexer.PluginRunToTagToContent(constants.DEBUGGER_PLUGIN_NAME)
    if (run not in runs_to_tags_to_content):
        return {}
    tags_to_content = runs_to_tags_to_content[run]
    mapping = {}
    for node_name in node_names:
        if (node_name not in tags_to_content):
            continue
        health_pills = []
        for tensor_event in self._event_multiplexer.Tensors(run, node_name):
            json_string = tags_to_content[node_name]
            try:
                content_object = json.loads(tf.compat.as_text(json_string))
                device_name = content_object['device']
                output_slot = content_object['outputSlot']
                health_pills.append(self._tensor_proto_to_health_pill(tensor_event, node_name, device_name, output_slot))
            except (KeyError, ValueError) as e:
                logger.error('Could not determine device from JSON string %r: %r', json_string, e)
        mapping[node_name] = health_pills
    return mapping

Obtains the health pills for a run sampled by the event multiplexer.

This is much faster than the alternative path of reading health pills from
disk.

Args:
run: The run to fetch health pills for.
node_names: A list of node names for which to retrieve health pills.

Returns:
A dictionary mapping from node name to a list of
event_accumulator.HealthPillEvents.


def select_best_paths(examples):
    possible_paths = {}
    for example in examples:
        dom = _create_dom(example['html'])
        matching_elements = _match_elements(dom, example['vars'])
        for (key, match) in matching_elements.items():
            if (key not in possible_paths):
                possible_paths[key] = _collect_paths(match)
    for example in examples:
        dom = _create_dom(example['html'])
        matching_elements = _match_elements(dom, example['vars'])
        for (key, paths) in possible_paths.items():
            if (key not in matching_elements):
                continue
            possible_paths[key] = filter((lambda path: _is_working_path(dom, path, matching_elements[key])), paths)
    priorities = ['find', 'left_neighbour_tag', 'right_neighbour_tag', 'wfind', 'match', 'Chained']
    priorities = dict(map((lambda x: (x[1], x[0])), enumerate(priorities)))
    for key in possible_paths.keys():
        possible_paths[key] = list(sorted(possible_paths[key], key=(lambda x: priorities.get(x.call_type, 100))))
    return possible_paths

Process `examples`, select only paths that works for every example. Select
best paths with highest priority.

Args:
examples (dict): Output from :func:`.read_config`.

Returns:
list: List of :class:`.PathCall` and :class:`.Chained` objects.


def sni2route(self, sni: SchemaNodeId, sctx: SchemaContext) -> SchemaRoute:

        nlist = sni.split("/")
        res = []
        for qn in (nlist[1:] if sni[0] == "/" else nlist):
            res.append(self.translate_node_id(qn, sctx))
        return res

Translate schema node identifier to a schema route.

Args:
sni: Schema node identifier (absolute or relative).
sctx: Schema context.

Raises:
ModuleNotRegistered: If `mid` is not registered in the data model.
UnknownPrefix: If a prefix specified in `sni` is not declared.


def UpdateNumberOfWarnings(self, number_of_consumed_warnings, number_of_produced_warnings):
    consumed_warnings_delta = 0
    if (number_of_consumed_warnings is not None):
        if (number_of_consumed_warnings < self.number_of_consumed_warnings):
            raise ValueError('Number of consumed warnings smaller than previous update.')
        consumed_warnings_delta = (number_of_consumed_warnings - self.number_of_consumed_warnings)
        self.number_of_consumed_warnings = number_of_consumed_warnings
        self.number_of_consumed_warnings_delta = consumed_warnings_delta
    produced_warnings_delta = 0
    if (number_of_produced_warnings is not None):
        if (number_of_produced_warnings < self.number_of_produced_warnings):
            raise ValueError('Number of produced warnings smaller than previous update.')
        produced_warnings_delta = (number_of_produced_warnings - self.number_of_produced_warnings)
        self.number_of_produced_warnings = number_of_produced_warnings
        self.number_of_produced_warnings_delta = produced_warnings_delta
    return ((consumed_warnings_delta > 0) or (produced_warnings_delta > 0))

Updates the number of warnings.

Args:
number_of_consumed_warnings (int): total number of warnings consumed by
the process.
number_of_produced_warnings (int): total number of warnings produced by
the process.

Returns:
bool: True if either number of warnings has increased.

Raises:
ValueError: if the consumed or produced number of warnings is smaller
than the value of the previous update.


def get_input_batch_type(self, input_element_type) -> typing.Optional[typing.Union[TypeConstraint, type]]:
    if not self._process_batch_defined:
        return None
    input_type = list(inspect.signature(self.process_batch).parameters.values())[0].annotation
    if input_type == inspect.Signature.empty:
        raise TypeError(f'Either {self.__class__.__name__}.process_batch() must have a type annotation on its first parameter, or {self.__class__.__name__} must override get_input_batch_type.')
    return input_type

Determine the batch type expected as input to process_batch.

The default implementation of ``get_input_batch_type`` simply observes the
input typehint for the first parameter of ``process_batch``. A Batched DoFn
may override this method if a dynamic approach is required.

Args:
input_element_type: The **element type** of the input PCollection this
DoFn is being applied to.

Returns:
``None`` if this DoFn cannot accept batches, else a Beam typehint or
a native Python typehint.


def num_batches(self):
    if hasattr(self, '__len__'):
        return len(self)
    raise NotImplementedError('You need to implement the `num_batches` property:\n\n@property\ndef num_batches(self):\n  return ...')

Number of batches in the PyDataset.

Returns:
The number of batches in the PyDataset or `None` to indicate that
the dataset is infinite.


def csv_to_list_of_dicts(lines: List[str],
                         csvheader: str,
                         quotechar: str = '"') -> List[Dict[str, str]]:

    data = []

    csvlines = get_lines_from_to(lines, csvheader, [None])[1:]

    headerfields = csvheader.split(",")
    reader = csv.reader(csvlines, quotechar=quotechar)
    for fields in reader:
        row = {}
        for f in range(len(headerfields)):
            row[headerfields[f]] = fields[f]
        data.append(row)
    return data

Extracts data from a list of CSV lines (starting with a defined header
line) embedded in a longer text block but ending with a blank line.

Args:
lines: CSV lines
csvheader: CSV header line
quotechar: ``quotechar`` parameter passed to :func:`csv.reader`

Returns:
list of dictionaries mapping fieldnames (from the header) to values


def remove_interactions_from(self, interactions):
    for (u, v) in interactions:
        self.remove_interaction(u, v)

Remove all specified interactions from the binary quadratic model.

Args:
interactions (iterable[[variable, variable]]):
A collection of interactions. Each interaction should be a 2-tuple of variables
in the binary quadratic model.

Notes:
Any interaction not in the binary quadratic model is ignored.

Examples:
This example creates an Ising model with three variables that has interactions
between two, and then removes an interaction.

>>> import dimod
...
>>> bqm = dimod.BinaryQuadraticModel({}, {('a', 'b'): -1.0, ('b', 'c'): 1.0}, 0.0, dimod.SPIN)
>>> bqm.remove_interactions_from([('b', 'c'), ('a', 'c')])  # ('a', 'c') is not an interaction, so ignored
>>> len(bqm.quadratic)
1


def ttft(self) -> float | None:
    return self._ttft

Returns the TTFT of the wrapped processor.

Returns:
the TTFT of the wrapped processor or None if the processor has not been
called yet.


def start_txn(self, txn_name=None):

		if not txn_name:
			txn_name = uuid.uuid4().hex

		txn_response = self.api.http_request('POST','%s/fcr:tx' % self.root, data=None, headers=None)

		if txn_response.status_code == 201:

			txn_uri = txn_response.headers['Location']
			logger.debug("spawning transaction: %s" % txn_uri)

			txn = Transaction(
				self,
				txn_name,
				txn_uri,
				expires = txn_response.headers['Expires'])

			self.txns[txn_name] = txn

			return txn

Request new transaction from repository, init new Transaction,
store in self.txns

Args:
txn_name (str): human name for transaction

Return:
(Transaction): returns intance of newly created transaction


def files_delete(self, *, id: str, **kwargs) -> SlackResponse:

        kwargs.update({"id": id})
        return self.api_call("files.delete", json=kwargs)

Deletes a file.

Args:
id (str): The file id. e.g. 'F1234467890'


def size_str(size_in_bytes):
    if (not size_in_bytes):
        return '?? GiB'
    size_in_bytes = float(size_in_bytes)
    for (name, size_bytes) in _NAME_LIST:
        value = (size_in_bytes / size_bytes)
        if (value >= 1.0):
            return '{:.2f} {}'.format(value, name)
    return '{} {}'.format(int(size_in_bytes), 'bytes')

Returns a human readable size string.

If size_in_bytes is None, then returns "?? GiB".

For example `size_str(1.5 * tfds.units.GiB) == "1.50 GiB"`.

Args:
size_in_bytes: `int` or `None`, the size, in bytes, that we want to
format as a human-readable size string.


def _SkipFieldValue(tokenizer):
    if tokenizer.TryConsumeByteString():
        while tokenizer.TryConsumeByteString():
            pass
        return
    if ((not tokenizer.TryConsumeIdentifier()) and (not tokenizer.TryConsumeInt64()) and (not tokenizer.TryConsumeUint64()) and (not tokenizer.TryConsumeFloat())):
        raise ParseError(('Invalid field value: ' + tokenizer.token))

Skips over a field value.

Args:
tokenizer: A tokenizer to parse the field name and values.

Raises:
ParseError: In case an invalid field value is found.


def add_ephemeral_listener(self, callback, event_type=None):
    listener_id = uuid4()
    self.ephemeral_listeners.append({'uid': listener_id, 'callback': callback, 'event_type': event_type})
    return listener_id

Add a callback handler for ephemeral events going to this room.

Args:
callback (func(room, event)): Callback called when an ephemeral event arrives.
event_type (str): The event_type to filter for.
Returns:
uuid.UUID: Unique id of the listener, can be used to identify the listener.


def write(self, ostream, kmip_version=enums.KMIPVersion.KMIP_1_0):

        super(LongInteger, self).write(ostream, kmip_version=kmip_version)
        ostream.write(pack('!q', self.value))

Write the encoding of the LongInteger to the output stream.

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


def next_batch(self, batch_size):
    logging.debug('next_batch() invoked')
    queue = self.mgr.get_queue(self.qname_in)
    tensors = ([] if (self.input_tensors is None) else {tensor: [] for tensor in self.input_tensors})
    count = 0
    while (count < batch_size):
        item = queue.get(block=True)
        if (item is None):
            logging.info('next_batch() got None')
            queue.task_done()
            self.done_feeding = True
            break
        elif (type(item) is marker.EndPartition):
            logging.info('next_batch() got EndPartition')
            queue.task_done()
            if ((not self.train_mode) and (count > 0)):
                break
        else:
            if (self.input_tensors is None):
                tensors.append(item)
            else:
                for i in range(len(self.input_tensors)):
                    tensors[self.input_tensors[i]].append(item[i])
            count += 1
            queue.task_done()
    logging.debug('next_batch() returning {0} items'.format(count))
    return tensors

Gets a batch of items from the input RDD.

If multiple tensors are provided per row in the input RDD, e.g. tuple of (tensor1, tensor2, ..., tensorN) and:

* no ``input_mapping`` was provided to the DataFeed constructor, this will return an array of ``batch_size`` tuples,
and the caller is responsible for separating the tensors.
* an ``input_mapping`` was provided to the DataFeed constructor, this will return a dictionary of N tensors,
with tensor names as keys and arrays of length ``batch_size`` as values.

Note: if the end of the data is reached, this may return with fewer than ``batch_size`` items.

Args:
:batch_size: number of items to retrieve.

Returns:
A batch of items or a dictionary of tensors.


def bottleneck_block(cnn, depth, depth_bottleneck, stride, pre_activation):
    if pre_activation:
        bottleneck_block_v2(cnn, depth, depth_bottleneck, stride)
    else:
        bottleneck_block_v1(cnn, depth, depth_bottleneck, stride)

Bottleneck block with identity short-cut.

Args:
cnn: the network to append bottleneck blocks.
depth: the number of output filters for this bottleneck block.
depth_bottleneck: the number of bottleneck filters for this block.
stride: Stride used in the first layer of the bottleneck block.
pre_activation: use pre_activation structure used in v2 or not.


def __new__(cls, deformation_gradient):

        obj = super().__new__(cls, deformation_gradient)
        return obj.view(cls)

Create a Deformation object.  Note that the constructor uses __new__
rather than __init__ according to the standard method of subclassing
numpy ndarrays.

Args:
deformation_gradient (3x3 array-like): the 3x3 array-like
representing the deformation gradient


def profile_settings_args_install_json(self, ij, required):
    profile_args = {}
    for p in (ij.get('params') or []):
        if ((p.get('required', False) != required) and (required is not None)):
            continue
        if (p.get('type').lower() == 'boolean'):
            profile_args[p.get('name')] = self._to_bool(p.get('default', False))
        elif (p.get('type').lower() == 'choice'):
            valid_values = '|'.join(self.expand_valid_values(p.get('validValues', [])))
            profile_args[p.get('name')] = '[{}]'.format(valid_values)
        elif (p.get('type').lower() == 'multichoice'):
            profile_args[p.get('name')] = p.get('validValues', [])
        elif (p.get('name') in ['api_access_id', 'api_secret_key']):
            pass
        else:
            types = '|'.join(p.get('playbookDataType', []))
            if types:
                profile_args[p.get('name')] = p.get('default', '<{}>'.format(types))
            else:
                profile_args[p.get('name')] = p.get('default', '')
    return profile_args

Return args based on install.json params.

Args:
ij (dict): The install.json contents.
required (bool): If True only required args will be returned.

Returns:
dict: Dictionary of required or optional App args.


def get_discovery_doc(self, services, hostname=None):

    if not isinstance(services, (tuple, list)):
      services = [services]

    util.check_list_type(services, remote._ServiceClass, 'services',
                         allow_none=False)

    return self.__discovery_doc_descriptor(services, hostname=hostname)

JSON dict description of a protorpc.remote.Service in discovery format.

Args:
services: Either a single protorpc.remote.Service or a list of them
that implements an api/version.
hostname: string, Hostname of the API, to override the value set on the
current service. Defaults to None.

Returns:
dict, The discovery document as a JSON dict.


def furthest_from_root(self):
    best = (self.root, 0)
    d = dict()
    for node in self.traverse_preorder():
        if (node.edge_length is None):
            d[node] = 0
        else:
            d[node] = node.edge_length
        if (not node.is_root()):
            d[node] += d[node.parent]
        if (d[node] > best[1]):
            best = (node, d[node])
    return best

Return the ``Node`` that is furthest from the root and the corresponding distance. Edges with no length will be considered to have a length of 0

Returns:
``tuple``: First value is the furthest ``Node`` from the root, and second value is the corresponding distance


def Read(self, path, length=None, offset=0, fh=None):
    del fh
    if self._IsDir(path):
        raise fuse.FuseOSError(errno.EISDIR)
    fd = aff4.FACTORY.Open(self.root.Add(path), token=self.token)
    if all((hasattr(fd, 'Read'), hasattr(fd, 'Seek'), callable(fd.Read), callable(fd.Seek))):
        if (length is None):
            length = fd.Get(fd.Schema.SIZE)
        fd.Seek(offset)
        return fd.Read(length)
    else:
        raise fuse.FuseOSError(errno.EIO)

Reads data from a file.

Args:
path: The path to the file to read.
length: How many bytes to read.
offset: Offset in bytes from which reading should start.
fh: A file handler. Not used.

Returns:
A string containing the file contents requested.

Raises:
FuseOSError: If we try and read a directory or if we try and read an
object that doesn't support reading.


def init_from_adversarial_batches(self, adv_batches):
    for (idx, (adv_batch_id, adv_batch_val)) in enumerate(iteritems(adv_batches)):
        work_id = ATTACK_WORK_ID_PATTERN.format(idx)
        self.work[work_id] = {'claimed_worker_id': None, 'claimed_worker_start_time': None, 'is_completed': False, 'error': None, 'elapsed_time': None, 'submission_id': adv_batch_val['submission_id'], 'shard_id': None, 'output_adversarial_batch_id': adv_batch_id}

Initializes work pieces from adversarial batches.

Args:
adv_batches: dict with adversarial batches,
could be obtained as AversarialBatches.data


class DacEncoderOutput(ModelOutput):
    loss: Optional[torch.FloatTensor] = None
    quantized_representation: Optional[torch.FloatTensor] = None
    audio_codes: Optional[torch.FloatTensor] = None
    projected_latents: Optional[torch.FloatTensor] = None

Args:
loss (`torch.Tensor`):
Loss from the encoder model, comprising the weighted combination of the commitment and codebook losses.
quantized_representation (`torch.Tensor` of shape `(batch_size, dimension, time_steps)`, *optional*):
Quantized continuous representation of input.
audio_codes (`torch.Tensor` of shape `(batch_size, num_codebooks, time_steps)`, *optional*):
Codebook indices for each codebook (quantized discrete representation of input).
projected_latents (`torch.Tensor` of shape `(batch_size, num_codebooks * dimension, time_steps)`, *optional*):
Projected latents (continuous representation of input before quantization).


def bipartition(seq):
    return [(tuple((seq[i] for i in part0_idx)), tuple((seq[j] for j in part1_idx))) for (part0_idx, part1_idx) in bipartition_indices(len(seq))]

Return a list of bipartitions for a sequence.

Args:
a (Iterable): The sequence to partition.

Returns:
list[tuple[tuple]]: A list of tuples containing each of the two
partitions.

Example:
>>> bipartition((1,2,3))
[((), (1, 2, 3)), ((1,), (2, 3)), ((2,), (1, 3)), ((1, 2), (3,))]


def _shape_as_tensor(shape, dtype):
    if dtype != dtypes.int64 and dtype != dtypes.int32:
        raise ValueError(f'Expected int64 or int32 for dtype: got {dtype}.')
    if isinstance(shape, tensor_lib.Tensor):
        if shape.dtype != dtypes.int64 and shape.dtype != dtypes.int32:
            return math_ops.cast(shape, dtype)
        return shape
    shape = tensor_shape.as_shape(shape)
    if not shape:
        return constant_op.constant(-1, dtype=dtype)
    shape = [-1 if x is None else x for x in shape.as_list()]
    return constant_op.constant(shape, dtype=dtype)

Takes shape and coerces it to a shape as a tensor.

If the object is already a tensor, simply passes it on (result is guaranteed
to be int64 or int32, but not necessarily dtype).
If not, creates a tensor of type dtype.

Result is either a scalar equal to -1 if the shape is unknown_rank.
Otherwise, it is a vector, where unknown dimensions are represented with a
value of -1.

In C++, see TensorShapeFromTensor for parsing shapes in kernels, and
InferenceContext::MakeShapeFromShapeTensorTreatScalarAsUnknownShape, for
use in the shape inference function.

Args:
shape: input to coerce from TensorShape, Tensor, None, List[Optional[Int]],
Tuple[Optional[Int]].
dtype: tf.int64 or tf.int32

Returns:
a scalar or vector tensor of dtype tf.int32 or tf.int64.


def random_bytes(n):

    n = min(n, 100 * 1024)

    params = CaseInsensitiveDict(request.args.items())
    if "seed" in params:
        random.seed(int(params["seed"]))

    response = make_response()

    response.data = bytearray(random.randint(0, 255) for i in range(n))
    response.content_type = "application/octet-stream"
    return response

Returns n random bytes generated with given seed
---
tags:
- Dynamic data
parameters:
- in: path
name: n
type: int
produces:
- application/octet-stream
responses:
200:
description: Bytes.


def tokens(self, batch_index: int=0) -> List[str]:
    if not self._encodings:
        raise ValueError('tokens() is not available when using non-fast tokenizers (e.g. instance of a `XxxTokenizerFast` class).')
    return self._encodings[batch_index].tokens

Return the list of tokens (sub-parts of the input strings after word/subword splitting and before conversion to
integer indices) at a given batch index (only works for the output of a fast tokenizer).

Args:
batch_index (`int`, *optional*, defaults to 0): The index to access in the batch.

Returns:
`List[str]`: The list of tokens at that index.


def __init__(self, cluster):
    if isinstance(cluster, dict):
        self._cluster_spec = {}
        for job_name, tasks in cluster.items():
            if isinstance(tasks, (list, tuple)):
                job_tasks = {i: task for i, task in enumerate(tasks)}
            elif isinstance(tasks, dict):
                job_tasks = {int(i): task for i, task in tasks.items()}
            else:
                raise TypeError('The tasks for job %r must be a list or a dictionary from integers to strings.' % job_name)
            self._cluster_spec[job_name] = job_tasks
        self._make_cluster_def()
    elif isinstance(cluster, cluster_pb2.ClusterDef):
        self._cluster_def = cluster
        self._cluster_spec = {}
        for job_def in self._cluster_def.job:
            self._cluster_spec[job_def.name] = {i: t for i, t in job_def.tasks.items()}
    elif isinstance(cluster, ClusterSpec):
        self._cluster_def = cluster_pb2.ClusterDef()
        self._cluster_def.MergeFrom(cluster.as_cluster_def())
        self._cluster_spec = {}
        for job_def in self._cluster_def.job:
            self._cluster_spec[job_def.name] = {i: t for i, t in job_def.tasks.items()}
    else:
        raise TypeError('`cluster` must be a dictionary mapping one or more job names to lists of network addresses, or a `ClusterDef` protocol buffer')

Creates a `ClusterSpec`.

Args:
cluster: A dictionary mapping one or more job names to (i) a list of
network addresses, or (ii) a dictionary mapping integer task indices to
network addresses; or a `tf.train.ClusterDef` protocol buffer.

Raises:
TypeError: If `cluster` is not a dictionary mapping strings to lists
of strings, and not a `tf.train.ClusterDef` protobuf.


def add_frequency(self, name, value):

        logger.debug("Adding frequency {0} with value {1} to variant {2}".format(
            name, value, self['variant_id']))
        self['frequencies'].append({'label': name, 'value': value})

Add a frequency that will be displayed on the variant level

Args:
name (str): The name of the frequency field


def run_tag_processor(self, tag_proc_name):
    tag_processor = self.tag_procs[tag_proc_name]
    for tag in tag_processor.find(self.soup):
        self.process_tag(tag_proc_name, tag)

Run a tag processor.

Args:
tag_proc_name: A string key that maps to the TagProcessor to run.


def write(self, output_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
    local_buffer = utils.BytearrayStream()
    if self._maximum_items:
        self._maximum_items.write(local_buffer, kmip_version=kmip_version)
    if self._offset_items:
        self._offset_items.write(local_buffer, kmip_version=kmip_version)
    if self._storage_status_mask:
        self._storage_status_mask.write(local_buffer, kmip_version=kmip_version)
    if self._object_group_member:
        self._object_group_member.write(local_buffer, kmip_version=kmip_version)
    if (kmip_version < enums.KMIPVersion.KMIP_2_0):
        if self._attributes:
            for attribute in self.attributes:
                attribute.write(local_buffer, kmip_version=kmip_version)
    elif self._attributes:
        template_attribute = objects.TemplateAttribute(attributes=self.attributes)
        attributes = objects.convert_template_attribute_to_attributes(template_attribute)
        attributes.write(local_buffer, kmip_version=kmip_version)
    else:
        raise exceptions.InvalidField('The Locate request payload is missing the attributes list.')
    self.length = local_buffer.length()
    super(LocateRequestPayload, self).write(output_buffer, kmip_version=kmip_version)
    output_buffer.write(local_buffer.buffer)

Write the data encoding the Locate request payload to a buffer.

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


def SetTimezone(self, timezone):

    if not timezone:
      return

    try:
      self._timezone = pytz.timezone(timezone)
    except pytz.UnknownTimeZoneError:
      raise ValueError('Unsupported timezone: {0:s}'.format(timezone))

Sets the timezone.

Args:
timezone (str): timezone.

Raises:
ValueError: if the timezone is not supported.


def find_resistance(record):
    for feature in record.features:
        labels = set(feature.qualifiers.get('label', []))
        cassettes = labels.intersection(_ANTIBIOTICS)
        if (len(cassettes) > 1):
            raise RuntimeError('multiple resistance cassettes detected')
        elif (len(cassettes) == 1):
            return _ANTIBIOTICS.get(cassettes.pop())
    raise RuntimeError("could not find the resistance of '{}'".format(record.id))

Infer the antibiotics resistance of the given record.

Arguments:
record (`~Bio.SeqRecord.SeqRecord`): an annotated sequence.

Raises:
RuntimeError: when there's not exactly one resistance cassette.


def floor_divide(x1, x2):
    if any_symbolic_tensors((x1, x2)):
        return FloorDivide().symbolic_call(x1, x2)
    return backend.numpy.floor_divide(x1, x2)

Returns the largest integer smaller or equal to the division of inputs.

Args:
x1: Numerator.
x2: Denominator.

Returns:
Output tensor, `y = floor(x1/x2)`


def Verify(self, public_key):

    now = rdfvalue.RDFDatetime.Now().AsDatetime()
    if now > self._value.not_valid_after:
      raise VerificationError("Certificate expired!")
    if now < self._value.not_valid_before:
      raise VerificationError("Certificate not yet valid!")

    public_key.Verify(
        self._value.tbs_certificate_bytes,
        self._value.signature,
        hash_algorithm=self._value.signature_hash_algorithm)
    return True

Verifies the certificate using the given key.

Args:
public_key: The public key to use.

Returns:
True: Everything went well.

Raises:
VerificationError: The certificate did not verify.


def _rewrite_insert(self, sql, params, return_id=False):

        returning = self.qn(self.query.model._meta.pk.attname) if return_id else '*'

        if self.query.conflict_action.value == 'UPDATE':
            return self._rewrite_insert_update(sql, params, returning)
        elif self.query.conflict_action.value == 'NOTHING':
            return self._rewrite_insert_nothing(sql, params, returning)

        raise SuspiciousOperation((
            '%s is not a valid conflict action, specify '
            'ConflictAction.UPDATE or ConflictAction.NOTHING.'
        ) % str(self.query.conflict_action))

Rewrites a formed SQL INSERT query to include
the ON CONFLICT clause.

Arguments:
sql:
The SQL INSERT query to rewrite.

params:
The parameters passed to the query.

returning:
What to put in the `RETURNING` clause
of the resulting query.

Returns:
A tuple of the rewritten SQL query and new params.


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

Send encoded debug capture data for component.

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


def GetReportDownloader(self, version=sorted(_SERVICE_MAP.keys())[(- 1)], server=None):
    if (not server):
        server = _DEFAULT_ENDPOINT
    return ReportDownloader(self, version, server)

Creates a downloader for AdWords reports.

This is a convenience method. It is functionally identical to calling
ReportDownloader(adwords_client, version, server).

Args:
[optional]
version: A string identifying the AdWords version to connect to. This
defaults to what is currently the latest version. This will be updated
in future releases to point to what is then the latest version.
server: A string identifying the webserver hosting the AdWords API.

Returns:
A ReportDownloader tied to this AdWordsClient, ready to download reports.


def get_gutter_client(
        alias='default',
        cache=CLIENT_CACHE,
        **kwargs
):

    from gutter.client.models import Manager

    if not alias:
        return Manager(**kwargs)
    elif alias not in cache:
        cache[alias] = Manager(**kwargs)

    return cache[alias]

Creates gutter clients and memoizes them in a registry for future quick access.

Args:
alias (str or None): Name of the client. Used for caching.
If name is falsy then do not use the cache.
cache (dict): cache to store gutter managers in.
**kwargs: kwargs to be passed the Manger class.

Returns (Manager):
A gutter client.


def sil(msg, version):

    tc = typecode(msg)

    if tc not in [29, 31]:
        raise RuntimeError("%s: Not a target state and status messag, \
                           or operation status message, expecting TC = 29 or 31" % msg)

    msgbin = common.hex2bin(msg)

    if tc == 29:
        SIL = common.bin2int(msgbin[76:78])
    elif tc == 31:
        SIL = common.bin2int(msgbin[82:84])

    try:
        PE_RCu = uncertainty.SIL[SIL]['PE_RCu']
        PE_VPL = uncertainty.SIL[SIL]['PE_VPL']
    except KeyError:
        PE_RCu, PE_VPL = uncertainty.NA, uncertainty.NA

    base = 'unknown'

    if version == 2:
        if tc == 29:
            SIL_SUP = common.bin2int(msgbin[39])
        elif tc == 31:
            SIL_SUP = common.bin2int(msgbin[86])

        if SIL_SUP == 0:
            base = "hour"
        elif SIL_SUP == 1:
            base = "sample"

    return PE_RCu, PE_VPL, base

Calculate SIL, Surveillance Integrity Level

Args:
msg (string): 28 bytes hexadecimal message string with TC = 29, 31

Returns:
int or string: Probability of exceeding Horizontal Radius of Containment RCu
int or string: Probability of exceeding Vertical Integrity Containment Region VPL
string: SIL supplement based on per "hour" or "sample", or 'unknown'


def sg_gather_associated_ports(context, group):
    if (not group):
        return None
    if ((not hasattr(group, 'ports')) or (len(group.ports) <= 0)):
        return []
    return group.ports

Gather all ports associated to security group.

Returns:
* list, or None


def evaluate(self, test_events):

        for i, e in enumerate(test_events):
            self.__validate(e)

            unobserved = set(self.item_buffer)
            if not self.repeat:
                unobserved -= self.rec.users[e.user.index]['known_items']

            unobserved.add(e.item.index)

            candidates = np.asarray(list(unobserved))

            start = time.clock()
            recos, scores = self.__recommend(e, candidates)
            recommend_time = (time.clock() - start)

            rank = np.where(recos == e.item.index)[0][0]

            self.rec.users[e.user.index]['known_items'].add(e.item.index)
            start = time.clock()
            self.rec.update(e)
            update_time = (time.clock() - start)

            self.item_buffer.append(e.item.index)

            yield scores[0], rank, recommend_time, update_time

Iterate recommend/update procedure and compute incremental recall.

Args:
test_events (list of Event): Positive test events.

Returns:
list of tuples: (rank, recommend time, update time)


def _resize_output_size_rescale_to_max_len(height: int, width: int, min_len: Optional[int]=1, max_len: Optional[int]=None) -> Tuple[int, int]:
    max_len = max(height, width) if max_len is None else max_len
    aspect_ratio = width / height
    if width >= height:
        width = max_len
        height = int(width / aspect_ratio)
        if height % 2 != 0:
            height += 1
    elif height > width:
        height = max_len
        width = int(height * aspect_ratio)
        if width % 2 != 0:
            width += 1
    height = max(height, min_len)
    width = max(width, min_len)
    return (height, width)

Get the output size of the image after resizing given a dictionary specifying the max and min sizes.
Args:
height (`int`):
Height of the input image.
width (`int`):
Width of the input image.
min_len (`int`, *optional*, defaults to 1):
Minimum size of the output image.
max_len (`int`, *optional*, defaults to the maximum size of the image):
Maximum size of the output image.
Returns:
The output size of the image after resizing.


def _prepare_resource_chunks(self, resources, resource_delim=','):

        return [self._prepare_resource_chunk(resources, resource_delim, pos)
                for pos in range(0, len(resources), self._resources_per_req)]

As in some VirusTotal API methods the call can be made for multiple
resources at once this method prepares a list of concatenated resources
according to the maximum number of resources per requests.

Args:
resources: a list of the resources.
resource_delim: a string used to separate the resources.
Default value is a comma.
Returns:
A list of the concatenated resources.


def _ready(op: ops.Operation, sess: session.Session, msg) -> Tuple[bool, Optional[str]]:
    if op is None:
        return (True, None)
    else:
        try:
            ready_value = sess.run(op)
            if ready_value is None or ready_value.dtype == np.int32 or ready_value.size == 0:
                return (True, None)
            else:
                non_initialized_varnames = ', '.join([i.decode('utf-8') for i in ready_value])
                return (False, 'Variables not initialized: ' + non_initialized_varnames)
        except errors.FailedPreconditionError as e:
            if 'uninitialized' not in str(e):
                logging.warning('%s : error [%s]', msg, str(e))
                raise e
            return (False, str(e))

Checks if the model is ready or not, as determined by op.

Args:
op: An op, either _ready_op or _ready_for_local_init_op, which defines the
readiness of the model.
sess: A `Session`.
msg: A message to log to warning if not ready

Returns:
A tuple (is_ready, msg), where is_ready is True if ready and False
otherwise, and msg is `None` if the model is ready, a `String` with the
reason why it is not ready otherwise.


def indent(text, n=2, ch=" "):

    padding = ch * n
    return "\n".join(padding+line for line in text.split("\n"))

Indent all the lines in a given block of text by a specified amount.

Args:
text (str) :
The text to indent

n (int, optional) :
The amount to indent each line by (default: 2)

ch (char, optional) :
What character to fill the indentation with (default: " ")


def save(self, fname, mode=None, validate=True, encoding='utf-8',
             wd=False, inline=False, relative=False, pack=False):

        self._closed()

        if mode is None:
            mode = 'abs'
            if pack:
                mode = 'pack'
            elif wd:
                mode = 'wd'
            elif relative:
                mode = 'rel'

            msg = 'Using deprecated save method. Please save the workflow ' \
                  'with: wf.save(\'{}\', mode=\'{}\'). Redirecting to new ' \
                  'save method.'.format(fname, mode)
            warnings.warn(msg, DeprecationWarning)

        modes = ('rel', 'abs', 'wd', 'inline', 'pack')
        if mode not in modes:
            msg = 'Illegal mode "{}". Choose one of ({}).'\
                  .format(mode, ','.join(modes))
            raise ValueError(msg)

        if validate:
            self.validate()

        dirname = os.path.dirname(os.path.abspath(fname))
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        if mode == 'inline':
            msg = ('Inline saving is deprecated. Please save the workflow '
                   'using mode=\'pack\'. Setting mode to pack.')
            warnings.warn(msg, DeprecationWarning)
            mode = 'pack'

        if mode == 'rel':
            relpath = dirname
            save_yaml(fname=fname, wf=self, pack=False, relpath=relpath,
                      wd=False)

        if mode == 'abs':
            save_yaml(fname=fname, wf=self, pack=False, relpath=None,
                      wd=False)

        if mode == 'pack':
            self._pack(fname, encoding)

        if mode == 'wd':
            if self.get_working_dir() is None:
                raise ValueError('Working directory not set.')
            else:

                bn = os.path.basename(fname)
                wd_file = os.path.join(self.working_dir, bn)
                save_yaml(fname=wd_file, wf=self, pack=False, relpath=None,
                          wd=True)

                try:
                    shutil.copy2(wd_file, fname)
                except shutil.Error:
                    pass

Save the workflow to file.

Save the workflow to a CWL file that can be run with a CWL runner.

Args:
fname (str): file to save the workflow to.
mode (str): one of  (rel, abs, wd, inline, pack)
encoding (str): file encoding to use (default: ``utf-8``).


def _pad_input(incoming):
    incoming_expanded = incoming.replace('{', '{{').replace('}', '}}')
    for key in _BASE_CODES:
        (before, after) = (('{{%s}}' % key), ('{%s}' % key))
        if (before in incoming_expanded):
            incoming_expanded = incoming_expanded.replace(before, after)
    return incoming_expanded

Avoid IndexError and KeyError by ignoring un-related fields.

Example: '{0}{autored}' becomes '{{0}}{autored}'.

Positional arguments:
incoming -- the input unicode value.

Returns:
Padded unicode value.


def with_rows(self, rows):
    self = self.copy()
    self.append(self._with_columns(zip(*rows)))
    return self

Return a table with additional rows.

Args:
``rows`` (sequence of sequences): Each row has a value per column.

If ``rows`` is a 2-d array, its shape must be (_, n) for n columns.

Raises:
``ValueError``: If a row length differs from the column count.

>>> tiles = Table(make_array('letter', 'count', 'points'))
>>> tiles.with_rows(make_array(make_array('c', 2, 3),
...     make_array('d', 4, 2)))
letter | count | points
c      | 2     | 3
d      | 4     | 2


def create_temp_file(self, suffix='', lines=None):
    with tempfile.NamedTemporaryFile(delete=False, dir=self._tempdir, suffix=suffix) as f:
        if lines:
            for line in lines:
                f.write(line)
        return f.name

Creates a temporary file in the temporary directory.

Args:
suffix (str): The filename suffix of the temporary file (e.g. '.txt')
lines (List[str]): A list of lines that will be written to the temporary
file.
Returns:
The name of the temporary file created.


def get_job(self, jobid):

        import shlex
        from pyccc.job import Job

        job = Job(engine=self)
        job.jobid = job.rundata.containerid = jobid
        try:
            jobdata = self.client.inspect_container(job.jobid)
        except docker.errors.NotFound:
            raise exceptions.JobNotFound(
                    'The daemon could not find containter "%s"' % job.jobid)

        cmd = jobdata['Config']['Cmd']
        entrypoint = jobdata['Config']['Entrypoint']

        if len(cmd) == 3 and cmd[0:2] == ['sh', '-c']:
            cmd = cmd[2]
        elif entrypoint is not None:
            cmd = entrypoint + cmd

        if isinstance(cmd, list):
            cmd = ' '.join(shlex.quote(x) for x in cmd)

        job.command = cmd
        job.env = jobdata['Config']['Env']
        job.workingdir = jobdata['Config']['WorkingDir']
        job.rundata.container = jobdata

        return job

Return a Job object for the requested job id.

The returned object will be suitable for retrieving output, but depending on the engine,
may not populate all fields used at launch time (such as `job.inputs`, `job.commands`, etc.)

Args:
jobid (str): container id

Returns:
pyccc.job.Job: job object for this container

Raises:
pyccc.exceptions.JobNotFound: if no job could be located for this jobid


def list(self, **request_parameters):
    items = self._session.get_items(API_ENDPOINT, params=request_parameters)
    for item in items:
        (yield self._object_factory(OBJECT_TYPE, item))

List all roles.

Args:
**request_parameters: Additional request parameters (provides
support for parameters that may be added in the future).

Returns:
GeneratorContainer: A GeneratorContainer which, when iterated,
yields the roles returned by the Webex Teams query.

Raises:
TypeError: If the parameter types are incorrect.
ApiError: If the Webex Teams cloud returns an error.


def __init__(self, input_dataset, row_splits_dtype, name=None):

    def to_ragged_spec(spec):

        if not isinstance(spec, tensor.TensorSpec) or spec.shape.rank is None or spec.shape.is_fully_defined():
            return spec
        else:
            ragged_rank = max([axis for axis, size in enumerate(spec.shape.as_list()) if size is None])
            return ragged_tensor.RaggedTensorSpec(shape=spec.shape, dtype=spec.dtype, ragged_rank=ragged_rank, row_splits_dtype=row_splits_dtype)
    self._structure = nest.map_structure(to_ragged_spec, input_dataset.element_spec)

    def to_ragged_variant(value):

        if not isinstance(value, tensor.Tensor) or value.shape.rank is None or value.shape.is_fully_defined():
            return value
        else:
            spec = to_ragged_spec(tensor.TensorSpec.from_tensor(value))
            if spec._ragged_rank > 0:
                value = ragged_tensor.RaggedTensor.from_tensor(value, ragged_rank=spec._ragged_rank)
            return spec._to_tensor_list(value)[0]
    if structured_function._should_unpack(input_dataset.element_spec):
        map_fn = lambda *value: nest.map_structure(to_ragged_variant, value)
    else:
        map_fn = lambda value: nest.map_structure(to_ragged_variant, value)
    self._mapped_dataset = input_dataset.map(map_fn)
    self._name = name
    variant = self._mapped_dataset._variant_tensor
    super().__init__(input_dataset, variant)

Constructs a new _DenseToRaggedDataset.

Args:
input_dataset: The dataset whose tf.Tensor elements should be made ragged.
row_splits_dtype: The dtype that should be used for the `row_splits` of
any new ragged tensors.  Existing `tf.RaggedTensor` elements do *not*
have their row_splits dtype changed.
name: (Optional.) A string indicating a name for the `tf.data` operation.


def number_to_day(self, day_number):

        return [
            calendar.day_name[6],
            calendar.day_name[0],
            calendar.day_name[1],
            calendar.day_name[2],
            calendar.day_name[3],
            calendar.day_name[4],
            calendar.day_name[5]
        ][day_number]

Returns localized day name by its CRON number

Args:
day_number: Number of a day
Returns:
Day corresponding to day_number
Raises:
IndexError: When day_number is not found


def missing_value(self) -> Union[float, int, bytes, bool]:
    if self.is_float:
        return math.nan
    if self.is_integer:
        return 0
    if self == DType.STRING:
        return b''
    if self == DType.BOOLEAN:
        return False
    raise ValueError(f'Non-implemented type {self}')

Returns missing value for specific dtype.

Returns:
The default missing value for the given data type.


def benchmark(optimizer, problem, runs=20, **kwargs):

    stats = {'runs': []}

    kwargs = copy.copy(kwargs)
    kwargs['logging_func'] = None

    for _ in range(runs):
        optimizer.optimize(problem, **kwargs)

        if optimizer.solution_found:
            finished_num = 1.0
        else:
            finished_num = 0.0

        stats_ = {
            'fitness': optimizer.best_fitness,
            'fitness_runs': optimizer.fitness_runs,
            'solution_found': finished_num
        }
        stats['runs'].append(stats_)

        print '.',

    _add_mean_sd_to_stats(stats)

    return stats

Run an optimizer through a problem multiple times.

Args:
optimizer: Optimizer; The optimizer to benchmark.
problem: Problem; The problem to benchmark on.
runs: int > 0; Number of times that optimize is called on problem.

Returns:
dict; A dictionary of various statistics.


def get(self, name=None):
    if name is None:
        name = '%s_get' % self._name
    fn = lambda: gen_data_flow_ops.unstage(dtypes=self._dtypes, shared_name=self._name, name=name, capacity=self._capacity, memory_limit=self._memory_limit)
    return self.__internal_get(fn, name)

Gets one element from this staging area.

If the staging area is empty when this operation executes, it will block
until there is an element to dequeue.

Note that unlike others ops that can block, like the queue Dequeue
operations, this can stop other work from happening.  To avoid this, the
intended use is for this to be called only when there will be an element
already available.  One method for doing this in a training loop would be to
run a `put()` call during a warmup session.run call, and then call both
`get()` and `put()` in each subsequent step.

The placement of the returned tensor will be determined by the current
device scope when this function is called.

Args:
name: A name for the operation (optional).

Returns:
The tuple of tensors that was gotten.


def save_config(self, filepath):

        def get_hidden_parameter(item):

            num_sub_elements = item.childCount()

            if num_sub_elements == 0:
                dictator = {item.name : item.visible}
            else:
                dictator = {item.name:{}}
                for child_id in range(num_sub_elements):
                    dictator[item.name].update(get_hidden_parameter(item.child(child_id)))
            return dictator

        try:
            filepath = str(filepath)
            if not os.path.exists(os.path.dirname(filepath)):
                os.makedirs(os.path.dirname(filepath))

            dictator = {}
            for index in range(self.tree_scripts.topLevelItemCount()):
                script_item = self.tree_scripts.topLevelItem(index)
                dictator.update(get_hidden_parameter(script_item))

            dictator = {"gui_settings": self.gui_settings, "gui_settings_hidden": self.gui_settings_hidden, "scripts_hidden_parameters":dictator}

            for index in range(self.tree_scripts.topLevelItemCount()):
                script_item = self.tree_scripts.topLevelItem(index)
                self.update_script_from_item(script_item)

            dictator.update({'instruments': {}, 'scripts': {}, 'probes': {}})

            for instrument in self.instruments.values():
                dictator['instruments'].update(instrument.to_dict())
            for script in self.scripts.values():
                dictator['scripts'].update(script.to_dict())

            for instrument, probe_dict in self.probes.items():
                dictator['probes'].update({instrument: ','.join(list(probe_dict.keys()))})

            with open(filepath, 'w') as outfile:
                json.dump(dictator, outfile, indent=4)

            save_config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'save_config.json'))
            if os.path.isfile(save_config_path) and os.access(save_config_path, os.R_OK):
                with open(save_config_path, 'w') as outfile:
                    json.dump({'last_save_path': filepath}, outfile, indent=4)
            else:
                with io.open(save_config_path, 'w') as save_config_file:
                    save_config_file.write(json.dumps({'last_save_path': filepath}))

            self.log('Saved GUI configuration (location: {0}'.format(filepath))
        except Exception:
            msg = QtWidgets.QMessageBox()
            msg.setText("Saving failed. Please use 'save as' to define a valid path for the gui.")
            msg.exec_()

saves gui configuration to out_file_name
Args:
filepath: name of file


def sort_key_val(keys, values, dimension=-1):
    keys = tf_np.asarray(keys)
    values = tf_np.asarray(values)
    rank = keys.shape.ndims
    if rank is None:
        rank = values.shape.ndims
    if rank is None:
        raise ValueError('The rank of either keys or values must be known, but both are unknown (i.e. their shapes are both None).')
    if dimension in (-1, rank - 1):

        def maybe_swapaxes(a):
            return a
    else:

        def maybe_swapaxes(a):
            return tf_np.swapaxes(a, dimension, -1)
    keys = maybe_swapaxes(keys)
    values = maybe_swapaxes(values)
    idxs = tf_np.argsort(keys)

    def gather(a):
        return tf_np.asarray(array_ops.gather_v2(a, idxs, batch_dims=rank - 1))
    keys = gather(keys)
    values = gather(values)
    keys = maybe_swapaxes(keys)
    values = maybe_swapaxes(values)
    return (keys, values)

Sorts keys along a dimension and applies same permutation to values.

Args:
keys: an array. The dtype must be comparable numbers (integers and reals).
values: an array, with the same shape of `keys`.
dimension: an `int`. The dimension along which to sort.

Returns:
Permuted keys and values.


def compute_classification_results(self, adv_batches, dataset_batches,
                                     dataset_meta, defense_work=None):

    class_batch_to_work = {}
    if defense_work:
      for v in itervalues(defense_work.work):
        class_batch_to_work[v['output_classification_batch_id']] = v

    accuracy_matrix = ResultMatrix()

    error_matrix = ResultMatrix()

    hit_target_class_matrix = ResultMatrix()

    processed_images_count = {}

    total_count = len(self.data)
    processed_count = 0
    logging.info('Processing %d files with classification results',
                 len(self.data))
    for k, v in iteritems(self.data):
      if processed_count % 100 == 0:
        logging.info('Processed %d out of %d classification results',
                     processed_count, total_count)
      processed_count += 1
      defense_id = v['submission_id']
      adv_batch = adv_batches.data[v['adversarial_batch_id']]
      attack_id = adv_batch['submission_id']

      work_item = class_batch_to_work.get(k)
      required_work_stats = ['stat_correct', 'stat_error', 'stat_target_class',
                             'stat_num_images']
      if work_item and work_item['error']:

        continue
      if work_item and all(work_item.get(i) is not None
                           for i in required_work_stats):
        count_correctly_classified = work_item['stat_correct']
        count_errors = work_item['stat_error']
        count_hit_target_class = work_item['stat_target_class']
        num_images = work_item['stat_num_images']
      else:
        logging.warning('Recomputing accuracy for classification batch %s', k)
        (count_correctly_classified, count_errors, count_hit_target_class,
         num_images) = analyze_one_classification_result(
             self._storage_client, v['result_path'], adv_batch, dataset_batches,
             dataset_meta)

      accuracy_matrix[defense_id, attack_id] += count_correctly_classified
      error_matrix[defense_id, attack_id] += count_errors
      hit_target_class_matrix[defense_id, attack_id] += count_hit_target_class

      processed_images_count[defense_id] = (
          processed_images_count.get(defense_id, 0) + num_images)
    return (accuracy_matrix, error_matrix, hit_target_class_matrix,
            processed_images_count)

Computes classification results.

Args:
adv_batches: instance of AversarialBatches
dataset_batches: instance of DatasetBatches
dataset_meta: instance of DatasetMetadata
defense_work: instance of DefenseWorkPieces

Returns:
accuracy_matrix, error_matrix, hit_target_class_matrix,
processed_images_count


def list_vm_images_sub(access_token, subscription_id):
    endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/providers/Microsoft.Compute/images', '?api-version=', COMP_API])
    return do_get_next(endpoint, access_token)

List VM images in a subscription.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.

Returns:
HTTP response. JSON body of a list of VM images.


def install(self, apk_path, destination_dir=None, timeout_ms=None):
    if (not destination_dir):
        destination_dir = '/data/local/tmp/'
    basename = os.path.basename(apk_path)
    destination_path = (destination_dir + basename)
    self.push(apk_path, destination_path, timeout_ms=timeout_ms)
    return self.Shell(('pm install -r "%s"' % destination_path), timeout_ms=timeout_ms)

Install apk to device.

Doesn't support verifier file, instead allows destination directory to be
overridden.

Arguments:
apk_path: Local path to apk to install.
destination_dir: Optional destination directory. Use /system/app/ for
persistent applications.
timeout_ms: Expected timeout for pushing and installing.

Returns:
The pm install output.


def ast_to_html(self, ast, link_resolver):
    (out, _) = cmark.ast_to_html(ast, link_resolver)
    return out

See the documentation of `to_ast` for
more information.

Args:
ast: PyCapsule, a capsule as returned by `to_ast`
link_resolver: hotdoc.core.links.LinkResolver, a link
resolver instance.


def select_action(self, state_key, next_action_list):

        epsilon_greedy_flag = bool(np.random.binomial(n=1, p=self.epsilon_greedy_rate))

        if epsilon_greedy_flag is False:
            action_key = random.choice(next_action_list)
        else:
            action_key = self.predict_next_action(state_key, next_action_list)
        return action_key

Select action by Q(state, action).

Concreat method.

ε-greedy.

Args:
state_key:              The key of state.
next_action_list:       The possible action in `self.t+1`.
If the length of this list is 0, all action should be possible.

Returns:
The key of action.


def get_accounts(self, provider='aws'):

        url = '{gate}/credentials'.format(gate=API_URL)
        response = requests.get(url, verify=GATE_CA_BUNDLE, cert=GATE_CLIENT_CERT)
        assert response.ok, 'Failed to get accounts: {0}'.format(response.text)

        all_accounts = response.json()
        self.log.debug('Accounts in Spinnaker:\n%s', all_accounts)

        filtered_accounts = []
        for account in all_accounts:
            if account['type'] == provider:
                filtered_accounts.append(account)

        if not filtered_accounts:
            raise ForemastError('No Accounts matching {0}.'.format(provider))

        return filtered_accounts

Get Accounts added to Spinnaker.

Args:
provider (str): What provider to find accounts for.

Returns:
list: list of dicts of Spinnaker credentials matching _provider_.

Raises:
AssertionError: Failure getting accounts from Spinnaker.


def _get_help_for_modules(self, modules, prefix, include_special_flags):

    output_lines = []
    for module in modules:
      self._render_our_module_flags(module, output_lines, prefix)
    if include_special_flags:
      self._render_module_flags(
          'absl.flags',
          six.itervalues(_helpers.SPECIAL_FLAGS._flags()),
          output_lines,
          prefix)
    return '\n'.join(output_lines)

Returns the help string for a list of modules.

Private to absl.flags package.

Args:
modules: List[str], a list of modules to get the help string for.
prefix: str, a string that is prepended to each generated help line.
include_special_flags: bool, whether to include description of
SPECIAL_FLAGS, i.e. --flagfile and --undefok.


def Search(self, artifact, os_name=None, cpe=None, label=None):

    hit = lambda x: x[0] == x[1] or not x[0]
    seq = [(artifact, self.artifact), (os_name, self.os_name), (cpe, self.cpe),
           (label, self.label)]
    return all(map(hit, seq))

Whether the condition contains the specified values.

Args:
artifact: A string identifier for the artifact.
os_name: An OS string.
cpe: A CPE string.
label: A label string.

Returns:
True if the values match the non-empty query attributes.
Empty query attributes are ignored in the comparison.


def connected_sites(self, site_labels=None):
    if site_labels:
        selected_sites = self.select_sites(site_labels)
    else:
        selected_sites = self.sites
    initial_clusters = [cluster.Cluster([site]) for site in selected_sites]
    if site_labels:
        blocking_sites = (self.site_labels - set(site_labels))
        for c in initial_clusters:
            c.remove_sites_from_neighbours(blocking_sites)
    final_clusters = []
    while initial_clusters:
        this_cluster = initial_clusters.pop(0)
        while this_cluster.neighbours:
            neighbouring_clusters = [c for c in initial_clusters if this_cluster.is_neighbouring(c)]
            for nc in neighbouring_clusters:
                initial_clusters.remove(nc)
                this_cluster = this_cluster.merge(nc)
        final_clusters.append(this_cluster)
    return final_clusters

Searches the lattice to find sets of sites that are contiguously neighbouring.
Mutually exclusive sets of contiguous sites are returned as Cluster objects.

Args:
site_labels (:obj:(List(Str)|Set(Str)|Str), optional): Labels for sites to be considered in the search.
This can be a list::

[ 'A', 'B' ]

a set::

( 'A', 'B' )

or a string::

'A'.

Returns:
(List(Cluster)): List of Cluster objects for groups of contiguous sites.


def convert_date_to_duration(date: Timestamp) -> NormalizedDuration:
    if isinstance(date, float):
        return date
    if isinstance(date, int):
        return float(date)
    if isinstance(date, np.datetime64):
        return convert_numpy_datetime64_to_duration(date)
    if isinstance(date, datetime.datetime):
        return convert_datetime_to_duration(date)
    if isinstance(date, datetime.date):
        return convert_datetime_date_to_duration(date)
    raise TypeError(f'Unsupported type: {type(date)}')

Converts date value to a number representing the Unix timestamp.

If a float or int, it is returned as float.
If a date, it is converted to a Unix timestamp (number of seconds from Unix
epoch).

Args:
date: Date to convert.

Returns:
Unix timestamp (seconds elapsed from unix epoch).

Raises:
TypeError: unsupported type. Supported types are:
- np.datetime64
- datetime.datetime


def downsampled_mesh(self, step):
    from lace.mesh import Mesh
    if (self.f is not None):
        raise ValueError('Function `downsampled_mesh` does not support faces.')
    low = Mesh()
    if (self.v is not None):
        low.v = self.v[::step]
    if (self.vc is not None):
        low.vc = self.vc[::step]
    return low

Returns a downsampled copy of this mesh.

Args:
step: the step size for the sampling

Returns:
a new, downsampled Mesh object.

Raises:
ValueError if this Mesh has faces.


def get_soap_header(self):
    if (self._cached_soap_header is not None):
        return self._cached_soap_header
    music_service = self.music_service
    credentials_header = XML.Element('credentials', {'xmlns': 'http:
    device_id = XML.SubElement(credentials_header, 'deviceId')
    device_id.text = self._device_id
    device_provider = XML.SubElement(credentials_header, 'deviceProvider')
    device_provider.text = 'Sonos'
    if music_service.account.oa_device_id:
        login_token = XML.Element('loginToken')
        token = XML.SubElement(login_token, 'token')
        token.text = music_service.account.oa_device_id
        key = XML.SubElement(login_token, 'key')
        key.text = music_service.account.key
        household_id = XML.SubElement(login_token, 'householdId')
        household_id.text = self._device.household_id
        credentials_header.append(login_token)
    elif (music_service.auth_type in ['DeviceLink', 'UserId']):
        session_id = self._device.musicServices.GetSessionId([('ServiceId', music_service.service_id), ('Username', music_service.account.username)])['SessionId']
        session_elt = XML.Element('sessionId')
        session_elt.text = session_id
        credentials_header.append(session_elt)
    self._cached_soap_header = XML.tostring(credentials_header, encoding='utf-8').decode(encoding='utf-8')
    return self._cached_soap_header

Generate the SOAP authentication header for the related service.

This header contains all the necessary authentication details.

Returns:
str: A string representation of the XML content of the SOAP
header.


def _on_connected(self, device):

        self._logger.debug("Device connected event: {}".format(device))

        self.connected = True
        self._connection_handle = device['connection_handle']
        self.device.connected = True
        self._audit('ClientConnected')

Callback function called when a connected event has been received.
It is executed in the baBLE working thread: should not be blocking.

Args:
device (dict): Information about the newly connected device


def get_symmetric_site(self, point, cartesian=False):
    sg = SpacegroupAnalyzer(self)
    ops = sg.get_symmetry_operations(cartesian=cartesian)
    for op in ops:
        slab = self.copy()
        site2 = op.operate(point)
        if (('%.6f' % site2[2]) == ('%.6f' % point[2])):
            continue
        slab.append('O', point, coords_are_cartesian=cartesian)
        slab.append('O', site2, coords_are_cartesian=cartesian)
        sg = SpacegroupAnalyzer(slab)
        if sg.is_laue():
            break
        else:
            slab.remove_sites([(len(slab) - 1)])
            slab.remove_sites([(len(slab) - 1)])
    return site2

This method uses symmetry operations to find equivalent sites on
both sides of the slab. Works mainly for slabs with Laue
symmetry. This is useful for retaining the non-polar and
symmetric properties of a slab when creating adsorbed
structures or symmetric reconstructions.

Arg:
point: Fractional coordinate.

Returns:
point: Fractional coordinate. A point equivalent to the
parameter point, but on the other side of the slab


def EnrolFleetspeakClient(self, client_id):
    client_urn = rdf_client.ClientURN(client_id)
    if data_store.RelationalDBEnabled():
        try:
            data_store.REL_DB.ReadClientMetadata(client_id)
            return False
        except db.UnknownClientError:
            pass
    elif aff4.FACTORY.ExistsWithType(client_urn, aff4_type=aff4_grr.VFSGRRClient, token=self.token):
        return False
    logging.info('Enrolling a new Fleetspeak client: %r', client_id)
    if data_store.RelationalDBEnabled():
        now = rdfvalue.RDFDatetime.Now()
        data_store.REL_DB.WriteClientMetadata(client_id, first_seen=now, fleetspeak_enabled=True, last_ping=now)
    if data_store.AFF4Enabled():
        with aff4.FACTORY.Create(client_urn, aff4_type=aff4_grr.VFSGRRClient, mode='rw', token=self.token) as client:
            client.Set(client.Schema.FLEETSPEAK_ENABLED, rdfvalue.RDFBool(True))
            index = client_index.CreateClientIndex(token=self.token)
            index.AddClient(client)
            if data_store.RelationalDBEnabled():
                client_obj = rdf_objects.ClientSnapshot(client_id=client_urn.Basename())
                index = client_index.ClientIndex()
                index.AddClient(client_obj)
    events.Events.PublishEvent('ClientEnrollment', client_urn, token=self.token)
    return True

Enrols a Fleetspeak-enabled client for use with GRR.

Args:
client_id: GRR client-id for the client.

Returns:
True if the client is new, and actually got enrolled. This method
is a no-op if the client already exists (in which case False is returned).


def short(self, url):

        self.clean_url(url)
        shorten_url = f'{self.api_url}v3/shorten'
        params = {
            'uri': url,
            'access_token': self.api_key,
            'format': 'txt',
        }
        response = self._get(shorten_url, params=params)
        if response.ok:
            return response.text.strip()
        raise ShorteningErrorException(response.content)

Short implementation for Bit.ly
Args:
url: the URL you want to shorten

Returns:
A string containing the shortened URL

Raises:
BadAPIResponseException: If the data is malformed or we got a bad
status code on API response
ShorteningErrorException: If the API Returns an error as response


def serialize_many_sparse_tensors(tensors):
    ret = nest.pack_sequence_as(tensors, [sparse_ops.serialize_many_sparse(tensor, out_type=dtypes.variant) if sparse_tensor.is_sparse(tensor) else tensor for tensor in nest.flatten(tensors)])
    return ret

Serializes many sparse tensors into a batch.

Args:
tensors: a tensor structure to serialize.

Returns:
`tensors` with any sparse tensors replaced by the serialized batch.


def get_const_dim_count(node_def):
    const_value = values_from_const(node_def)
    return const_value.ndim

Get the number of dimensions for a Const node.

Args:
node_def: Const NodeDef.

Returns:
Number of dimensions for the Const node.


def no_automatic_dependency_tracking(method):

    def _method_wrapper(self, *args, **kwargs):
        previous_value = getattr(self, '_self_setattr_tracking', True)
        self._self_setattr_tracking = False
        try:
            result = method(self, *args, **kwargs)
        finally:
            self._self_setattr_tracking = previous_value
        return result
    return tf_decorator.make_decorator(target=method, decorator_func=_method_wrapper)

Disables automatic dependency tracking on attribute assignment.

Use to decorate any method of a Trackable object. Attribute assignment in
that method will not add dependencies (also respected in Model). Harmless if
used in a class which does not do automatic dependency tracking (which means
it's safe to use in base classes which may have subclasses which also inherit
from Trackable).

Args:
method: The method to decorate.

Returns:
A decorated method which sets and un-sets automatic dependency tracking for
the object the method is called on (not thread safe).


def sendResponse(self, message, UUID, routing_key):
    self.sendMessage(exchange=self.output_exchange, routing_key=routing_key, message=message, UUID=UUID)

Send `message` to ``self.output_exchange`` with routing key
``self.output_key``, ``self.content_type`` in ``delivery_mode=2``.

Args:
message (str): message which will be sent
UUID: unique identification of message
routing_key (str): which routing key to use to send message back


def load_manual_sequence_file(self, ident, seq_file, copy_file=False, outdir=None, set_as_representative=False):
    if copy_file:
        if (not outdir):
            outdir = self.sequence_dir
            if (not outdir):
                raise ValueError('Output directory must be specified')
        shutil.copy(seq_file, outdir)
        seq_file = op.join(outdir, seq_file)
    manual_sequence = SeqProp(id=ident, sequence_path=seq_file, seq=None)
    self.sequences.append(manual_sequence)
    if set_as_representative:
        self.representative_sequence = manual_sequence
    return self.sequences.get_by_id(ident)

Load a manual sequence, given as a FASTA file and optionally set it as the representative sequence.
Also store it in the sequences attribute.

Args:
ident (str): Sequence ID
seq_file (str): Path to sequence FASTA file
copy_file (bool): If the FASTA file should be copied to the protein's sequences folder or the ``outdir``, if
protein folder has not been set
outdir (str): Path to output directory
set_as_representative (bool): If this sequence should be set as the representative one

Returns:
SeqProp: Sequence that was loaded into the ``sequences`` attribute


def key_for_property(cls, kind, property):

    return model.Key(Kind.KIND_NAME, kind, Property.KIND_NAME, property)

Return the __property__ key for property of kind.

Args:
kind: kind whose key is requested.
property: property whose key is requested.

Returns:
The key for property of kind.


def showPDFpage(
        page,
        rect,
        src,
        pno=0,
        overlay=True,
        keep_proportion=True,
        rotate=0,
        reuse_xref=0,
        clip = None,
    ):

    def calc_matrix(sr, tr, keep=True, rotate=0):

        smp = Point((sr.x1 + sr.x0) / 2., (sr.y1 + sr.y0) / 2.)

        tmp = Point((tr.x1 + tr.x0) / 2., (tr.y1 + tr.y0) / 2.)

        rot = Matrix(rotate)

        m = Matrix(1, 0, 0, 1, -smp.x, -smp.y) * rot

        sr1 = sr * m

        fw = tr.width / sr1.width
        fh = tr.height / sr1.height
        if keep:
            fw = fh = min(fw, fh)

        m *= Matrix(fw, fh)
        m *= Matrix(1, 0, 0, 1, tmp.x, tmp.y)
        return m

    CheckParent(page)
    doc = page.parent

    if not doc.isPDF or not src.isPDF:
        raise ValueError("not a PDF")

    rect = page.rect & rect
    if rect.isEmpty or rect.isInfinite:
        raise ValueError("rect must be finite and not empty")

    if reuse_xref > 0:
        warnings.warn("ignoring 'reuse_xref'", DeprecationWarning)

    while pno < 0:
        pno += len(src)
    src_page = src[pno]
    if len(src_page._getContents()) == 0:
        raise ValueError("nothing to show - source page empty")

    tar_rect = rect * ~page._getTransformation()

    src_rect = src_page.rect if not clip else src_page.rect & clip
    if src_rect.isEmpty or src_rect.isInfinite:
        raise ValueError("clip must be finite and not empty")
    src_rect = src_rect * ~src_page._getTransformation()

    matrix = calc_matrix(src_rect, tar_rect, keep=keep_proportion, rotate=rotate)

    ilst = [i[1] for i in doc._getPageInfo(page.number, 3)]

    n = "fzFrm"
    i = 0
    _imgname = n + "0"
    while _imgname in ilst:
        i += 1
        _imgname = n + str(i)

    isrc = src._graft_id
    if doc._graft_id == isrc:
        raise ValueError("source document must not equal target")

    if isrc in doc.Graftmaps:
        gmap = doc.Graftmaps[isrc]
    else:
        gmap = Graftmap(doc)
        doc.Graftmaps[isrc] = gmap

    pno_id = (isrc, pno)
    xref = doc.ShownPages.get(pno_id, 0)

    xref = page._showPDFpage(
                src_page,
                overlay=overlay,
                matrix=matrix,
                xref=xref,
                clip=src_rect,
                graftmap=gmap,
                _imgname=_imgname,
            )
    doc.ShownPages[pno_id] = xref

    return xref

Show page number 'pno' of PDF 'src' in rectangle 'rect'.

Args:
rect: (rect-like) where to place the source image
src: (document) source PDF
pno: (int) source page number
overlay: (bool) put in foreground
keep_proportion: (bool) do not change width-height-ratio
rotate: (int) degrees (multiple of 90)
clip: (rect-like) part of source page rectangle
Returns:
xref of inserted object (for reuse)


def _discover_ideal_backend(orm_backend):
    if orm_backend:
        return orm_backend
    if ((peewee is not MISSING) and (sqlalchemy is not MISSING)):
        raise RuntimeError('Both PeeWee and SQLAlchemy detected as installed, but no explicit backend provided! Please specify one!')
    if (peewee is not MISSING):
        return _PEEWEE_BACKEND
    elif (sqlalchemy is not MISSING):
        return _SQLALCHEMY_BACKEND
    else:
        return MISSING

Auto-discover the ideal backend based on what is installed.

Right now, handles discovery of:
* PeeWee
* SQLAlchemy

Args:
orm_backend (str): The ``orm_backend`` value that was passed to the
``create_app`` function. That is, the ORM Backend the User
indicated they wanted to use.

Returns:
str|fleaker.missing.MissingSentinel: Returns a string for the ideal
backend if it found one, or :obj:`fleaker.MISSING` if we couldn't
find one.

Raises:
RuntimeError: Raised if no user provided ORM Backend is given and BOTH
PeeWee and SQLAlchemy are installed.


def ParseOptions(cls, options, analysis_plugin):

    if not isinstance(analysis_plugin, viper.ViperAnalysisPlugin):
      raise errors.BadConfigObject(
          'Analysis plugin is not an instance of ViperAnalysisPlugin')

    lookup_hash = cls._ParseStringOption(
        options, 'viper_hash', default_value=cls._DEFAULT_HASH)
    analysis_plugin.SetLookupHash(lookup_hash)

    host = cls._ParseStringOption(
        options, 'viper_host', default_value=cls._DEFAULT_HOST)
    analysis_plugin.SetHost(host)

    port = cls._ParseNumericOption(
        options, 'viper_port', default_value=cls._DEFAULT_PORT)
    analysis_plugin.SetPort(port)

    protocol = cls._ParseStringOption(
        options, 'viper_protocol', default_value=cls._DEFAULT_PROTOCOL)
    protocol = protocol.lower().strip()
    analysis_plugin.SetProtocol(protocol)

    if not analysis_plugin.TestConnection():
      raise errors.BadConfigOption(
          'Unable to connect to Viper {0:s}:{1:d}'.format(host, port))

Parses and validates options.

Args:
options (argparse.Namespace): parser options.
analysis_plugin (ViperAnalysisPlugin): analysis plugin to configure.

Raises:
BadConfigObject: when the output module object is of the wrong type.
BadConfigOption: when unable to connect to Viper instance.


def shift(x, offsets):

    def to_slice(offset):
        return slice(offset, None) if offset >= 0 else slice(None, offset)
    out = np.empty_like(x)
    out.fill(np.nan)
    ind_src = tuple((to_slice(-o) for o in offsets))
    ind_dst = tuple((to_slice(o) for o in offsets))
    out[ind_dst] = x[ind_src]
    return out

Similar to np.roll, but fills with nan instead of rolling values over.

Also shifts along multiple axes at the same time.

Args:
x: The input array to shift.
offsets: How much to shift each axis, offsets[i] is the offset for the i-th
axis.

Returns:
An array with same shape as the input, with specified shifts applied.


def events_from_logdir(logdir):
    assert tf.io.gfile.exists(logdir)
    files = tf.io.gfile.listdir(logdir)
    assert len(files) == 1, 'Found not exactly one file in logdir: %s' % files
    return _events_from_file(os.path.join(logdir, files[0]))

Returns all events in the single eventfile in logdir.

Args:
logdir: The directory in which the single event file is sought.

Returns:
A list of all tf.compat.v1.Event protos from the single event file.

Raises:
AssertionError: If logdir does not contain exactly one file.


def _initialise(self, initial_state: str = 'unknown') -> dict:

        initial_state = initial_state.lower()
        if initial_state != 'unknown' and \
                initial_state not in self._allowed_states:
            raise ValueError('Invalid initial state: {}'.format(initial_state))
        _initial_state = dict(
            current_state=initial_state,
            target_state=initial_state,
            current_timestamp=datetime.utcnow().isoformat(),
            target_timestamp=datetime.utcnow().isoformat())
        return _initial_state

Return a dictionary used to initialise a state object.

This method is used to obtain a dictionary/hash describing the initial
state of SDP or a service in SDP.

Args:
initial_state (str): Initial state.

Returns:
dict, Initial state configuration


def commit_signature(vcs, user_config, signature):
    if (signature not in get_staged_signatures(vcs)):
        raise NotStagedError
    evidence_path = _get_committed_history_path(vcs)
    committed_signatures = get_committed_signatures(vcs)
    if (signature in committed_signatures):
        raise AlreadyCommittedError
    committed_signatures.append(signature)
    string = '\n'.join(committed_signatures[(- user_config['history_limit']):])
    with open(evidence_path, 'w') as f:
        f.write(string)
    unstage_signature(vcs, signature)

Add `signature` to the list of committed signatures

The signature must already be staged

Args:
vcs (easyci.vcs.base.Vcs)
user_config (dict)
signature (basestring)

Raises:
NotStagedError
AlreadyCommittedError


def get_model_field(model, field_name):
    meta = model._meta
    try:
        if DJANGO19:
            field = meta.get_field(field_name)
        else:
            field = meta.get_field_by_name(field_name)[0]
        return field
    except:
        if DJANGO19:
            related_objs = (f for f in meta.get_fields() if ((f.one_to_many or f.one_to_one) and f.auto_created and (not f.concrete)))
            related_m2m_objs = (f for f in meta.get_fields(include_hidden=True) if (f.many_to_many and f.auto_created))
        else:
            related_objs = meta.get_all_related_objects()
            related_m2m_objs = meta.get_all_related_many_to_many_objects()
        related_objects = {o.get_accessor_name(): o for o in chain(related_objs, related_m2m_objs)}
        if (field_name in related_objects):
            return related_objects[field_name]
        else:
            if hasattr(meta, 'virtual_fields'):
                for field in meta.virtual_fields:
                    if (field.name == field_name):
                        return field
            raise AttributeError(('%s is not a valid field for %s' % (field_name, model)))

Return a field given a model and field name.

Arguments:
model: a Django model
field_name: the name of a field

Returns:
A Django field if `field_name` is a valid field for `model`,
None otherwise.


def convert_http_request(request, referrer_host=None):

    new_request = urllib.request.Request(
        request.url_info.url,
        origin_req_host=referrer_host,
    )

    for name, value in request.fields.get_all():
        new_request.add_header(name, value)

    return new_request

Convert a HTTP request.

Args:
request: An instance of :class:`.http.request.Request`.
referrer_host (str): The referrering hostname or IP address.

Returns:
Request: An instance of :class:`urllib.request.Request`


def _decode_POST_value(request, field_name, default=None):
    if (default is None):
        value = request.POST[field_name]
    else:
        value = request.POST.get(field_name, default)
    charsets = json.loads(request.POST.get('charsets', '{}'))
    charset = charsets.get(field_name, 'utf-8')
    if (charset.lower() != 'utf-8'):
        logger.debug("Incoming email field '%s' has %s encoding.", field_name, charset)
    return smart_text(value, encoding=charset)

Helper to decode a request field into unicode based on charsets encoding.

Args:
request: the HttpRequest object.
field_name: the field expected in the request.POST

Kwargs:
default: if passed in then field is optional and default is used if not
found; if None, then assume field exists, which will raise an error
if it does not.

Returns: the contents of the string encoded using the related charset from
the requests.POST['charsets'] dictionary (or 'utf-8' if none specified).


def start_daemon():
    if (RequestLog.daemon is None):
        parser = get_nginx_parser()
        RequestLog.daemon = RequestLog.ParseToDBThread(parser, daemon=True)
    RequestLog.daemon.start()
    return RequestLog.daemon

Start a thread to continuously read log files and append lines in DB.

Work in progress. Currently the thread doesn't append anything,
it only print the information parsed from each line read.

Returns:
thread: the started thread.


def optional(name, default) -> 'Wildcard':

        return Wildcard(min_count=1, fixed_size=True, variable_name=name, optional=default)

Create a `Wildcard` that matches a single argument with a default value.

If the wildcard does not match, the substitution will contain the
default value instead.

Args:
name:
The name for the wildcard.
default:
The default value of the wildcard.

Returns:
A n optional wildcard.


def get_decorated_names(path,decorator_module, decorator_name):

    with open(path) as f:
        module_source   = f.read()

    expression          = '\s*@(?:{}\.)?{}[\s\S]+?(?:instance_creator\s*=\s*([\w.]+)[\s\S]+?)?\s*def\s+(\w+)\s*\(\s*(self)?'.format(decorator_module, decorator_name)
    methods             = re.compile(expression).findall(module_source)
    decorateds          = [x[1] for x in methods]
    instance_creators   = [x[0] if x[0] != '' else None for x in methods]
    is_methods          = [x[2]!='' for x in methods]
    result                      = []
    class_name_expression       = '\s*(?:class\s+(\w+)\s*\(.*\:\n[\s\S]+?)+?\s+def\s+{}\s*\('

    for i in range(len(decorateds)) :
        decorated   = decorateds[i]

        if is_methods[i] :

            index           = decorateds[:i].count(decorated)
            class_name      = re.compile(class_name_expression.format(decorated)).findall(module_source)[index]
            original_name   = decorated
            instance_creator= instance_creators[i]
            if decorated[:2] =='__' :
                decorated = '_'+ class_name+ decorated
            result.append({'name': decorated, 'class_name': class_name, 'instance_creator': instance_creator})
        else :
            result.append({'name': decorated})
    if DEBUG :
        print(
            decorateds          ,
            instance_creators   ,
            is_methods          ,
            result
            )
    return result

Get the name of fumctions or methods decorated with the specified decorator.
If a method, the name will be as class_name.method_name.
Args :
path                : The path to the module.
decorator_module    : The name of the module defining the decorator.
decorator_name      : The name of the decorator.


def monkhorst_automatic(cls, structure, ngkpt, use_symmetries=True, use_time_reversal=True, chksymbreak=None, comment=None):
    sg = SpacegroupAnalyzer(structure)
    nshiftk = 1
    shiftk = (3 * (0.5,))
    return cls.monkhorst(ngkpt, shiftk=shiftk, use_symmetries=use_symmetries, use_time_reversal=use_time_reversal, chksymbreak=chksymbreak, comment=(comment if comment else 'Automatic Monkhorst-Pack scheme'))

Convenient static constructor for an automatic Monkhorst-Pack mesh.

Args:
structure: :class:`Structure` object.
ngkpt: Subdivisions N_1, N_2 and N_3 along reciprocal lattice vectors.
use_symmetries: Use spatial symmetries to reduce the number of k-points.
use_time_reversal: Use time-reversal symmetry to reduce the number of k-points.

Returns:
:class:`KSampling` object.


def _get_cached_certs(cert_uri, cache):

  certs = cache.get(cert_uri, namespace=_CERT_NAMESPACE)
  if certs is None:
    _logger.debug('Cert cache miss for %s', cert_uri)
    try:
      result = urlfetch.fetch(cert_uri)
    except AssertionError:

      return None

    if result.status_code == 200:
      certs = json.loads(result.content)
      expiration_time_seconds = _get_cert_expiration_time(result.headers)
      if expiration_time_seconds:
        cache.set(cert_uri, certs, time=expiration_time_seconds,
                  namespace=_CERT_NAMESPACE)
    else:
      _logger.error(
          'Certs not available, HTTP request returned %d', result.status_code)

  return certs

Get certs from cache if present; otherwise, gets from URI and caches them.

Args:
cert_uri: URI from which to retrieve certs if cache is stale or empty.
cache: Cache of pre-fetched certs.

Returns:
The retrieved certs.


def apply_operation(self, symmop):

    def operate_site(site):
        new_cart = symmop.operate(site.coords)
        return Site(site.species, new_cart, properties=site.properties)
    self._sites = [operate_site(s) for s in self._sites]

Apply a symmetry operation to the molecule.

Args:
symmop (SymmOp): Symmetry operation to apply.


def sync(self, option: str = 'all') -> None:

        if option in ['system', 'vendor', 'oem', 'data', 'all']:
            self._execute('-s', self.device_sn, 'sync', option)
        else:
            raise ValueError(f'There is no option named: {option!r}.')

Sync a local build from $ANDROID_PRODUCT_OUT to the device (default all).

Args:
option: 'system', 'vendor', 'oem', 'data', 'all'


def invertible_1x1_conv(name, x, reverse=False):
    (_, height, width, channels) = common_layers.shape_list(x)
    w_shape = [channels, channels]
    random_matrix = np.random.rand(channels, channels)
    np_w = scipy.linalg.qr(random_matrix)[0].astype('float32')
    (np_p, np_l, np_u) = scipy.linalg.lu(np_w)
    np_s = np.diag(np_u)
    np_sign_s = np.sign(np_s)
    np_log_s = np.log(np.abs(np_s))
    np_u = np.triu(np_u, k=1)
    with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
        p = tf.get_variable('P', initializer=np_p, trainable=False)
        l = tf.get_variable('L', initializer=np_l)
        sign_s = tf.get_variable('sign_S', initializer=np_sign_s, trainable=False)
        log_s = tf.get_variable('log_S', initializer=np_log_s)
        u = tf.get_variable('U', initializer=np_u)
        l_mask = np.tril(np.ones([channels, channels], dtype=np.float32), (- 1))
        l = ((l * l_mask) + tf.eye(channels, channels))
        u = ((u * np.transpose(l_mask)) + tf.diag((sign_s * tf.exp(log_s))))
        w = tf.matmul(p, tf.matmul(l, u))
        objective = (tf.reduce_sum(log_s) * tf.cast((height * width), log_s.dtype))
        if (not reverse):
            w = tf.reshape(w, ([1, 1] + w_shape))
            x = tf.nn.conv2d(x, w, [1, 1, 1, 1], 'SAME', data_format='NHWC')
        else:

            def tpu_inv(m):
                'tf.linalg.inv workaround until it is supported on TPU.'
                (q, r) = tf.linalg.qr(m)
                return tf.linalg.triangular_solve(r, tf.transpose(q), lower=False)
            w_inv = tf.reshape(tpu_inv(w), ([1, 1] + w_shape))
            x = tf.nn.conv2d(x, w_inv, [1, 1, 1, 1], 'SAME', data_format='NHWC')
            objective *= (- 1)
    return (x, objective)

1X1 convolution on x.

The 1X1 convolution is parametrized as P*L*(U + sign(s)*exp(log(s))) where
1. P is a permutation matrix.
2. L is a lower triangular matrix with diagonal entries unity.
3. U is a upper triangular matrix where the diagonal entries zero.
4. s is a vector.

sign(s) and P are fixed and the remaining are optimized. P, L, U and s are
initialized by the PLU decomposition of a random rotation matrix.

Args:
name: scope
x: Input Tensor.
reverse: whether the pass is from z -> x or x -> z.

Returns:
x_conv: x after a 1X1 convolution is applied on x.
objective: sum(log(s))


def _validate_body_parameters(self, body, action_spec):
    processed_params = []
    for (param_name, param_spec) in action_spec['parameters'].items():
        if (param_spec['in'] == 'body'):
            processed_params.append(param_name)
            if (('type' in param_spec.keys()) and (not self.check_type(body, param_spec['type']))):
                msg = 'Check type did not validate for {0} and {1}'.format(param_spec['type'], body)
                return (False, msg)
            elif ('schema' in param_spec.keys()):
                if (('type' in param_spec['schema'].keys()) and (param_spec['schema']['type'] == 'array')):
                    definition_name = self.get_definition_name_from_ref(param_spec['schema']['items']['$ref'])
                    if ((len(body) > 0) and (not self.validate_definition(definition_name, body[0]))):
                        msg = 'The body did not validate against its definition'
                        return (False, msg)
                elif (('type' in param_spec['schema'].keys()) and (not self.check_type(body, param_spec['schema']['type']))):
                    msg = 'Check type did not validate for {0} and {1}'.format(param_spec['schema']['type'], body)
                    return (False, msg)
                else:
                    definition_name = self.get_definition_name_from_ref(param_spec['schema']['$ref'])
                    if (not self.validate_definition(definition_name, body)):
                        msg = 'The body did not validate against its definition'
                        return (False, msg)
    if (not all(((param in processed_params) for (param, spec) in action_spec['parameters'].items() if ((spec['in'] == 'body') and ('required' in spec) and spec['required'])))):
        msg = 'Not all required parameters were present'
        return (False, msg)
    return (True, '')

Check the body parameter for the action specification.

Args:
body: body parameter to check.
action_spec: specification of the action.

Returns:
True if the body is valid.
A string containing an error msg in case the body did not validate,
otherwise the string is empty


def add_country_location(self, country, exact=True, locations=None, use_live=True):
    (iso3, match) = Country.get_iso3_country_code_fuzzy(country, use_live=use_live)
    if (iso3 is None):
        raise HDXError(('Country: %s - cannot find iso3 code!' % country))
    return self.add_other_location(iso3, exact=exact, alterror=('Country: %s with iso3: %s could not be found in HDX list!' % (country, iso3)), locations=locations)

Add a country. If an iso 3 code is not provided, value is parsed and if it is a valid country name,
converted to an iso 3 code. If the country is already added, it is ignored.

Args:
country (str): Country to add
exact (bool): True for exact matching or False to allow fuzzy matching. Defaults to True.
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.

Returns:
bool: True if country added or False if country already present


def memory_read64(self, addr, num_long_words):

        buf_size = num_long_words
        buf = (ctypes.c_ulonglong * buf_size)()
        units_read = self._dll.JLINKARM_ReadMemU64(addr, buf_size, buf, 0)
        if units_read < 0:
            raise errors.JLinkException(units_read)

        return buf[:units_read]

Reads memory from the target system in units of 64-bits.

Args:
self (JLink): the ``JLink`` instance
addr (int): start address to read from
num_long_words (int): number of long words to read

Returns:
List of long words read from the target system.

Raises:
JLinkException: if memory could not be read


def predict_proba(self, a, b, idx=0, **kwargs):

        return self.predict_dataset(DataFrame([[a, b]],
                                              columns=['A', 'B']))

Use Jarfo to predict the causal direction of a pair of vars.

Args:
a (numpy.ndarray): Variable 1
b (numpy.ndarray): Variable 2
idx (int): (optional) index number for printing purposes

Returns:
float: Causation score (Value : 1 if a->b and -1 if b->a)


def predict_on_batch(self, data: Union[list, tuple],
                         return_indexes: bool = False) -> List[List[str]]:

        X = self._transform_batch(data)
        objects_number, lengths = len(X[0]), [len(elem) for elem in data[0]]
        Y = self.model_.predict_on_batch(X)
        labels = np.argmax(Y, axis=-1)
        answer: List[List[str]] = [None] * objects_number
        for i, (elem, length) in enumerate(zip(labels, lengths)):
            elem = elem[:length]
            answer[i] = elem if return_indexes else self.tags.idxs2toks(elem)
        return answer

Makes predictions on a single batch

Args:
data: a batch of word sequences together with additional inputs
return_indexes: whether to return tag indexes in vocabulary or tags themselves

Returns:
a batch of label sequences


def counter(namespace: Union[Type, str], name: str) -> 'Metrics.DelegatingCounter':
    namespace = Metrics.get_namespace(namespace)
    return Metrics.DelegatingCounter(MetricName(namespace, name))

Obtains or creates a Counter metric.

Args:
namespace: A class or string that gives the namespace to a metric
name: A string that gives a unique name to a metric

Returns:
A Counter object.


def read_geom_h5(xdmf_file, snapshot):
    header = {}
    xdmf_root = xmlET.parse(str(xdmf_file)).getroot()
    if (snapshot is None):
        return (None, xdmf_root)
    elt_snap = xdmf_root[0][0][snapshot]
    header['ti_ad'] = float(elt_snap.find('Time').get('Value'))
    header['mo_lambda'] = _maybe_get(elt_snap, 'mo_lambda', 'Value', float)
    header['mo_thick_sol'] = _maybe_get(elt_snap, 'mo_thick_sol', 'Value', float)
    header['ntb'] = 1
    coord_h5 = []
    coord_shape = []
    twod = None
    for elt_subdomain in elt_snap.findall('Grid'):
        if elt_subdomain.get('Name').startswith('meshYang'):
            header['ntb'] = 2
            break
        elt_geom = elt_subdomain.find('Geometry')
        if ((elt_geom.get('Type') == 'X_Y') and (twod is None)):
            twod = ''
            for data_item in elt_geom.findall('DataItem'):
                coord = data_item.text.strip()[(- 1)]
                if (coord in 'XYZ'):
                    twod += coord
        data_item = elt_geom.find('DataItem')
        coord_shape.append(_get_dim(data_item))
        coord_h5.append((xdmf_file.parent / data_item.text.strip().split(':/', 1)[0]))
    _read_coord_h5(coord_h5, coord_shape, header, twod)
    return (header, xdmf_root)

Extract geometry information from hdf5 files.

Args:
xdmf_file (:class:`pathlib.Path`): path of the xdmf file.
snapshot (int): snapshot number.
Returns:
(dict, root): geometry information and root of xdmf document.


def find_pad_index(self, array):

        try:
            return list(array).index(self.pad_value)
        except ValueError:
            return len(array)

Find padding index.

Args:
array (list): integer list.

Returns:
idx: padding index.

Examples:
>>> array = [1, 2, 0]
>>> self.find_pad_index(array)
2