async def get_events(self, event_id=None, max_events=50):
    if (event_id is None):
        conv_events = self._events[((- 1) * max_events):]
    else:
        conv_event = self.get_event(event_id)
        if (self._events[0].id_ != event_id):
            conv_events = self._events[(self._events.index(conv_event) + 1):]
        else:
            logger.info('Loading events for conversation {} before {}'.format(self.id_, conv_event.timestamp))
            res = (await self._client.get_conversation(hangouts_pb2.GetConversationRequest(request_header=self._client.get_request_header(), conversation_spec=hangouts_pb2.ConversationSpec(conversation_id=hangouts_pb2.ConversationId(id=self.id_)), include_event=True, max_events_per_conversation=max_events, event_continuation_token=self._event_cont_token)))
            if res.conversation_state.HasField('conversation'):
                self.update_conversation(res.conversation_state.conversation)
            self._event_cont_token = res.conversation_state.event_continuation_token
            conv_events = [self._wrap_event(event) for event in res.conversation_state.event]
            logger.info('Loaded {} events for conversation {}'.format(len(conv_events), self.id_))
            for conv_event in reversed(conv_events):
                if (conv_event.id_ not in self._events_dict):
                    self._events.insert(0, conv_event)
                    self._events_dict[conv_event.id_] = conv_event
                else:
                    logger.info('Conversation %s ignoring duplicate event %s', self.id_, conv_event.id_)
    return conv_events

Get events from this conversation.

Makes a request to load historical events if necessary.

Args:
event_id (str): (optional) If provided, return events preceding
this event, otherwise return the newest events.
max_events (int): Maximum number of events to return. Defaults to
50.

Returns:
List of :class:`.ConversationEvent` instances, ordered
newest-first.

Raises:
KeyError: If ``event_id`` does not correspond to a known event.
.NetworkError: If the events could not be requested.


def same_dynamic_shape(a, b):

  a = tf.convert_to_tensor(value=a, name="a")
  b = tf.convert_to_tensor(value=b, name="b")

  def all_shapes_equal():
    return tf.reduce_all(
        input_tensor=tf.equal(
            tf.concat([tf.shape(input=a), tf.shape(input=b)], 0),
            tf.concat([tf.shape(input=b), tf.shape(input=a)], 0)))

  return tf.cond(
      pred=tf.equal(tf.rank(a), tf.rank(b)),
      true_fn=all_shapes_equal,
      false_fn=lambda: tf.constant(False))

Returns whether a and b have the same dynamic shape.

Args:
a: `Tensor`
b: `Tensor`

Returns:
`bool` `Tensor` representing if both tensors have the same shape.


def weighted_average(input_tensor: torch.Tensor, weights: Optional[torch.Tensor]=None, dim=None) -> torch.Tensor:
    if weights is not None:
        weighted_tensor = torch.where(weights != 0, input_tensor * weights, torch.zeros_like(input_tensor))
        sum_weights = torch.clamp(weights.sum(dim=dim) if dim else weights.sum(), min=1.0)
        return (weighted_tensor.sum(dim=dim) if dim else weighted_tensor.sum()) / sum_weights
    else:
        return input_tensor.mean(dim=dim)

Computes the weighted average of a given tensor across a given `dim`, masking values associated with weight zero,
meaning instead of `nan * 0 = nan` you will get `0 * 0 = 0`.

Args:
input_tensor (`torch.FloatTensor`):
Input tensor, of which the average must be computed.
weights (`torch.FloatTensor`, *optional*):
Weights tensor, of the same shape as `input_tensor`.
dim (`int`, *optional*):
The dim along which to average `input_tensor`.

Returns:
`torch.FloatTensor`: The tensor with values averaged along the specified `dim`.


def parse_vhdl(text):

  lex = VhdlLexer

  name = None
  kind = None
  saved_type = None
  end_param_group = False
  cur_package = None

  metacomments = []
  parameters = []
  param_items = []

  generics = []
  ports = []
  sections = []
  port_param_index = 0
  last_item = None
  array_range_start_pos = 0

  objects = []

  for pos, action, groups in lex.run(text):
    if action == 'metacomment':
      realigned = re.sub(r'^
      if last_item is None:
        metacomments.append(realigned)
      else:
        last_item.desc = realigned
    if action == 'section_meta':
      sections.append((port_param_index, groups[0]))

    elif action == 'function':
      kind = 'function'
      name = groups[0]
      param_items = []
      parameters = []
    elif action == 'procedure':
      kind = 'procedure'
      name = groups[0]
      param_items = []
      parameters = []
    elif action == 'param':
      if end_param_group:

        for i in param_items:
          parameters.append(i)
        param_items = []
        end_param_group = False

      param_items.append(VhdlParameter(groups[1]))
    elif action == 'param_type':
      mode, ptype = groups

      if mode is not None:
        mode = mode.strip()

      for i in param_items:
        i.mode = mode
        i.data_type = ptype

      end_param_group = True

    elif action == 'param_default':
      for i in param_items:
        i.default_value = groups[0]

    elif action == 'end_subprogram':

      for i in param_items:
        parameters.append(i)

      if kind == 'function':
        vobj = VhdlFunction(name, cur_package, parameters, groups[0], metacomments)
      else:
        vobj = VhdlProcedure(name, cur_package, parameters, metacomments)

      objects.append(vobj)

      metacomments = []
      parameters = []
      param_items = []
      kind = None
      name = None

    elif action == 'component':
      kind = 'component'
      name = groups[0]
      generics = []
      ports = []
      param_items = []
      sections = []
      port_param_index = 0

    elif action == 'generic_param':
      param_items.append(groups[0])

    elif action == 'generic_param_type':
      ptype = groups[0]

      for i in param_items:
        generics.append(VhdlParameter(i, 'in', ptype))
      param_items = []
      last_item = generics[-1]

    elif action == 'port_param':
      param_items.append(groups[0])
      port_param_index += 1

    elif action == 'port_param_type':
      mode, ptype = groups

      for i in param_items:
        ports.append(VhdlParameter(i, mode, ptype))

      param_items = []
      last_item = ports[-1]

    elif action == 'port_array_param_type':
      mode, ptype = groups
      array_range_start_pos = pos[1]

    elif action == 'array_range_end':
      arange = text[array_range_start_pos:pos[0]+1]

      for i in param_items:
        ports.append(VhdlParameter(i, mode, ptype + arange))

      param_items = []
      last_item = ports[-1]

    elif action == 'end_component':
      vobj = VhdlComponent(name, cur_package, ports, generics, dict(sections), metacomments)
      objects.append(vobj)
      last_item = None
      metacomments = []

    elif action == 'package':
      objects.append(VhdlPackage(groups[0]))
      cur_package = groups[0]
      kind = None
      name = None

    elif action == 'type':
      saved_type = groups[0]

    elif action in ('array_type', 'file_type', 'access_type', 'record_type', 'range_type', 'enum_type', 'incomplete_type'):
      vobj = VhdlType(saved_type, cur_package, action, metacomments)
      objects.append(vobj)
      kind = None
      name = None
      metacomments = []

    elif action == 'subtype':
      vobj = VhdlSubtype(groups[0], cur_package, groups[1], metacomments)
      objects.append(vobj)
      kind = None
      name = None
      metacomments = []

    elif action == 'constant':
      vobj = VhdlConstant(groups[0], cur_package, groups[1], metacomments)
      objects.append(vobj)
      kind = None
      name = None
      metacomments = []

  return objects

Parse a text buffer of VHDL code

Args:
text(str): Source code to parse
Returns:
Parsed objects.


def indicator_body(indicators):

        hash_patterns = {
            'md5': re.compile(r'^([a-fA-F\d]{32})$'),
            'sha1': re.compile(r'^([a-fA-F\d]{40})$'),
            'sha256': re.compile(r'^([a-fA-F\d]{64})$'),
        }
        body = {}
        for indicator in indicators:
            if indicator is None:
                continue

            if hash_patterns['md5'].match(indicator):
                body['md5'] = indicator
            elif hash_patterns['sha1'].match(indicator):
                body['sha1'] = indicator
            elif hash_patterns['sha256'].match(indicator):
                body['sha256'] = indicator

        return body

Generate the appropriate dictionary content for POST of an File indicator

Args:
indicators (list): A list of one or more hash value(s).


def get_modified_python_files(diff_with_last_commit: bool=False) -> List[str]:
    repo = Repo(PATH_TO_REPO)
    if not diff_with_last_commit:
        print(f'main is at {repo.refs.main.commit}')
        print(f'Current head is at {repo.head.commit}')
        branching_commits = repo.merge_base(repo.refs.main, repo.head)
        for commit in branching_commits:
            print(f'Branching commit: {commit}')
        return get_diff(repo, repo.head.commit, branching_commits)
    else:
        print(f'main is at {repo.head.commit}')
        parent_commits = repo.head.commit.parents
        for commit in parent_commits:
            print(f'Parent commit: {commit}')
        return get_diff(repo, repo.head.commit, parent_commits)

Return a list of python files that have been modified between:

- the current head and the main branch if `diff_with_last_commit=False` (default)
- the current head and its parent commit otherwise.

Returns:
`List[str]`: The list of Python files with a diff (files added, renamed or deleted are always returned, files
modified are returned if the diff in the file is not only in docstrings or comments, see
`diff_is_docstring_only`).


def compute(i, a_flat, tas):
    packed_elems = input_pack([elem_ta.read(i) for elem_ta in elems_ta])
    packed_a = output_pack(a_flat)
    a_out = fn(packed_a, packed_elems)
    nest.assert_same_structure(elems if initializer is None else initializer, a_out)
    flat_a_out = output_flatten(a_out)
    tas = [ta.write(i, value) for ta, value in zip(tas, flat_a_out)]
    if reverse:
        next_i = i - 1
    else:
        next_i = i + 1
    return (next_i, flat_a_out, tas)

The loop body of scan.

Args:
i: the loop counter.
a_flat: the accumulator value(s), flattened.
tas: the output accumulator TensorArray(s), flattened.

Returns:
[i + 1, a_flat, tas]: the updated counter + new accumulator values +
updated TensorArrays

Raises:
TypeError: if initializer and fn() output structure do not match
ValueType: if initializer and fn() output lengths do not match


def _parse_metadata(self, message):
    metadata = Metadata(source=self.actor_urn).__dict__
    metadata['thread_ts'] = message.get('thread_ts')
    if ('presence' in message):
        metadata['presence'] = message['presence']
    if ('text' in message):
        metadata['text'] = message['text']
    elif ('previous_message' in message):
        if ('text' in message['previous_message']):
            metadata['text'] = message['previous_message']['text']
        else:
            metadata['text'] = None
    else:
        metadata['text'] = None
    if ('user' in message):
        metadata['source_user'] = message['user']
    elif ('bot_id' in message):
        metadata['source_user'] = self.get_userid_from_botid(message['bot_id'])
    elif (('message' in message) and ('user' in message['message'])):
        metadata['source_user'] = message['message']['user']
    else:
        metadata['source_user'] = None
    metadata['user_id'] = metadata['source_user']
    metadata['display_name'] = self.get_username(metadata['source_user'])
    if ('channel' in message):
        metadata['source_channel'] = message['channel']
        if message['channel'].startswith('D'):
            metadata['is_private_message'] = True
        else:
            metadata['is_private_message'] = False
    metadata['source_connector'] = 'slack'
    return metadata

Parse incoming messages to build metadata dict
Lots of 'if' statements. It sucks, I know.

Args:
message (dict): JSON dump of message sent from Slack

Returns:
Legobot.Metadata


def cos(duration: int, amp: complex, freq: float=None, phase: float=0, name: str=None) -> SamplePulse:
    if (freq is None):
        freq = (1 / duration)
    return _sampled_cos_pulse(duration, amp, freq, phase=phase, name=name)

Generates cosine wave `SamplePulse`.

Applies `left` sampling strategy to generate discrete pulse from continuous function.

Args:
duration: Duration of pulse. Must be greater than zero.
amp: Pulse amplitude.
freq: Pulse frequency, units of 1/dt. If `None` defaults to single cycle.
phase: Pulse phase.
name: Name of pulse.


def _init_table_from_dfa(self, mma):

        observation_table_init = ObservationTableInit(self.epsilon, self.alphabet)
        sm_vector, smi_vector, em_vector = observation_table_init.initialize(mma, True)
        self.observation_table.sm_vector = sm_vector
        self.observation_table.smi_vector = smi_vector
        self.observation_table.em_vector = em_vector

        logging.info('Initialized from DFA em_vector table is the following:')
        logging.info(em_vector)

        self._fill_table_entry(self.epsilon, self.epsilon)

        for row in sorted(list(set(sm_vector + smi_vector)), key=len)[1:]:
            for column in em_vector:
                self._fill_table_entry(str(row), str(column))

Initializes table form a DFA
Args:
mma: The input automaton
Returns:
None


def _insert_back_keep_dims(x, axis):
    for i in sorted(axis):
        x = tf.expand_dims(x, axis=i)
    return x

Insert the dims in `axis` back as singletons after being removed.

Args:
x:  `Tensor`.
axis:  Python list of integers.

Returns:
`Tensor` with same values as `x`, but additional singleton dimensions.


def build_inputs_with_special_tokens(self, token_ids_0: List[int], token_ids_1: Optional[List[int]]=None) -> List[int]:
    sep = [self.sep_token_id]
    cls = [self.cls_token_id]
    if token_ids_1 is None:
        return cls + token_ids_0 + sep
    return cls + token_ids_0 + sep + token_ids_1 + sep

Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
adding special tokens. A REMBERT sequence has the following format:

- single sequence: `[CLS] X [SEP]`
- pair of sequences: `[CLS] A [SEP] B [SEP]`

Args:
token_ids_0 (`List[int]`):
List of IDs to which the special tokens will be added.
token_ids_1 (`List[int]`, *optional*):
Optional second list of IDs for sequence pairs.

Returns:
`List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.


def fetch_metric_data(self, *, test_config: TestConfigContainer) -> MetricContainer:
    project = test_config.project
    metrics_dataset = test_config.metrics_dataset
    metrics_table = test_config.metrics_table
    metric_name = test_config.metric_name
    query = f"\n          SELECT *\n          FROM {project}.{metrics_dataset}.{metrics_table}\n          WHERE CONTAINS_SUBSTR(({load_test_metrics_utils.METRICS_TYPE_LABEL}), '{metric_name}')\n          ORDER BY {load_test_metrics_utils.SUBMIT_TIMESTAMP_LABEL} DESC\n          LIMIT {constants._NUM_DATA_POINTS_TO_RUN_CHANGE_POINT_ANALYSIS}\n        "
    if bigquery is None:
        raise ImportError('Bigquery dependencies are not installed.')
    client = bigquery.Client()
    query_job = client.query(query=query)
    metric_data = query_job.result().to_dataframe()
    return MetricContainer(values=metric_data[load_test_metrics_utils.VALUE_LABEL].tolist(), timestamps=metric_data[load_test_metrics_utils.SUBMIT_TIMESTAMP_LABEL].tolist())

Args:
test_config: TestConfigContainer containing metadata required to fetch
metric data from BigQuery.
Returns:
MetricContainer containing metric values and timestamps.


def sample_shape_tensor(self, name='sample_shape_tensor'):
    with tf.compat.v1.name_scope(name):
        if isinstance(self._sample_shape, tf.Tensor):
            return self._sample_shape
        return tf.convert_to_tensor(value=self.sample_shape.as_list(), dtype=tf.int32)

Sample shape of random variable as a 1-D `Tensor`.

Args:
name: name to give to the op

Returns:
sample_shape: `Tensor`.


def __init__(self, chunk_size: int, chunk_overlap: int, chunking_strategy: ChunkingStrategy):
    self.chunk_size = chunk_size
    self.chunk_overlap = chunk_overlap
    self.chunking_strategy = chunking_strategy

Args:
chunk_size : Chunk size is the maximum number of characters that a chunk can contain
chunk_overlap : the number of characters that should overlap between two adjacent chunks
chunking_strategy : Defines the way to split text

Returns:
:class:`~apache_beam.transforms.ptransform.PTransform`


def __init__(self, queue, record_writer, flush_secs):

        threading.Thread.__init__(self)
        self.daemon = True
        self._queue = queue
        self._record_writer = record_writer
        self._flush_secs = flush_secs

        self._next_flush_time = 0
        self._has_pending_data = False
        self._shutdown_signal = object()

Creates an _AsyncWriterThread.
Args:
queue: A Queue from which to dequeue data.
record_writer: An instance of record_writer writer.
flush_secs: How often, in seconds, to flush the
pending file to disk.


def __init__(self, __task_type, **kwargs):

        self.name = __task_type + '_' + str(uuid.uuid4())[:8]
        self.name = self.name.replace(':','_')

        self.type = __task_type
        task_registry = TaskRegistry()
        self.definition = task_registry.get_definition(__task_type)
        self.domain = self.definition.get('containerDescriptors', [{'properties': {}}])[0]['properties'].get('domain', 'default')
        self._timeout = self.definition['properties'].get('timeout')

        self.inputs = Inputs(self.input_ports, task=self)
        self.outputs = Outputs(self.output_ports, self.name)
        self.batch_values = None
        self._impersonation_allowed = None

        self.set(**kwargs)

Construct an instance of GBDX Task

Args:
__task_type: name of the task
**kwargs: key=value pairs for inputs to set on the task

Returns:
An instance of Task.


def write(self, output_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):
    local_stream = BytearrayStream()
    if self._wrapping_method:
        self._wrapping_method.write(local_stream, kmip_version=kmip_version)
    else:
        raise ValueError('Invalid struct missing the wrapping method attribute.')
    if self._encryption_key_information:
        self._encryption_key_information.write(local_stream, kmip_version=kmip_version)
    if self._mac_signature_key_information:
        self._mac_signature_key_information.write(local_stream, kmip_version=kmip_version)
    if self._mac_signature:
        self._mac_signature.write(local_stream, kmip_version=kmip_version)
    if self._iv_counter_nonce:
        self._iv_counter_nonce.write(local_stream, kmip_version=kmip_version)
    if self._encoding_option:
        self._encoding_option.write(local_stream, kmip_version=kmip_version)
    self.length = local_stream.length()
    super(KeyWrappingData, self).write(output_stream, kmip_version=kmip_version)
    output_stream.write(local_stream.buffer)

Write the data encoding the KeyWrappingData struct to a stream.

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


def add_get(self, path, controller, template, raw=False):
    if raw:
        fn = controller
    else:
        fn = self._prepare_controller(controller, template)
    self.app.router.add_get(path, fn)

Setup a route of type GET

Args:
path (str): URL to listen to
controller (coroutine): the coroutine to handle the request
template (str): the template to render the response or None if it is a JSON response
raw (bool): indicates if post-processing (jinja, json, etc) is needed or not


def calc_digest(origin, algorithm="sha1", block_size=None):

    try:
        hashM = hashlib.new(algorithm)
    except ValueError:
        raise ValueError('hash algorithm not supported by the underlying platform: "{0}"'.format(algorithm))

    while True:
        chunk = origin.read(block_size) if block_size else origin.read()
        if not chunk:
            break
        hashM.update(chunk)
    return hashM.hexdigest()

Calculate digest of a readable object

Args:
origin -- a readable object for which calculate digest
algorithn -- the algorithm to use. See ``hashlib.algorithms_available`` for supported algorithms.
block_size -- the size of the block to read at each iteration


def typing(self, *, channel: str):
    payload = {'id': self._next_msg_id(), 'type': 'typing', 'channel': channel}
    self.send_over_websocket(payload=payload)

Sends a typing indicator to the specified channel.

This indicates that this app is currently
writing a message to send to a channel.

Args:
channel (str): The channel id. e.g. 'C024BE91L'

Raises:
SlackClientNotConnectedError: Websocket connection is closed.


def read(self, size=None):
    if (size is None):
        size = self.__size
    ret_list = []
    while ((size > 0) and self.__buf):
        data = self.__buf.popleft()
        size -= len(data)
        ret_list.append(data)
    if (size < 0):
        (ret_list[(- 1)], remainder) = (ret_list[(- 1)][:size], ret_list[(- 1)][size:])
        self.__buf.appendleft(remainder)
    ret = b''.join(ret_list)
    self.__size -= len(ret)
    return ret

Read at most size bytes from this buffer.

Bytes read from this buffer are consumed and are permanently removed.

Args:
size: If provided, read no more than size bytes from the buffer.
Otherwise, this reads the entire buffer.

Returns:
The bytes read from this buffer.


def Hash(self):
    if (not self.__hash):
        hashdata = self.RawData()
        ba = bytearray(binascii.unhexlify(hashdata))
        hash = bin_dbl_sha256(ba)
        self.__hash = UInt256(data=hash)
    return self.__hash

Get the hash value of the Blockbase.

Returns:
UInt256: containing the hash of the data.


def _flatten_summand_list(kernels):

  flattened = []
  for k in kernels:
    if isinstance(k, _SumKernel):
      flattened += k.kernels
    else:
      flattened.append(k)
  return flattened

Flatten a list of kernels which may contain _SumKernel instances.

Args:
kernels: Python list of `PositiveSemidefiniteKernel` instances

Returns:
Python list containing the elements of kernels, with any _SumKernel
instances replaced by their `kernels` property contents.


def ObjectFactory(value_type: Type[symbolic.Symbolic], base_value: Union[symbolic.Symbolic, Callable[[], symbolic.Symbolic], str], patches: Optional[rule_based.PatchType]=None, params_override: Optional[Union[Dict[str, Any], str]]=None) -> Any:
    if not isinstance(base_value, value_type) and callable(base_value):
        value = base_value()
    elif isinstance(base_value, str):
        value = symbolic.load(base_value)
    else:
        value = base_value
    if not isinstance(value, value_type):
        raise TypeError(f'{base_value!r} is neither an instance of {value_type!r}, nor a factory or a path of JSON file that produces an instance of {value_type!r}.')
    if patches is not None:
        value = rule_based.patch(value, patches)
    if params_override:
        value = value.rebind(utils.flatten(from_maybe_serialized(params_override, dict)), raise_on_no_change=False)
    return value

A factory to create symbolic object from a base value and patches.

Args:
value_type: Type of return value.
base_value: An instance of `value_type`,
or a callable object that produces an instance of `value_type`,
or a string as the path to the serialized value.
patches: Optional patching rules. See :func:`patch` for details.
params_override: A rebind dict (or a JSON string as serialized rebind dict)
as an additional patch to the value,

Returns:
Value after applying `patchers` and `params_override` based on `base_value`.


def __init__(self, dir=dir, size=10000, upstream=None, **kwargs):

        from ambry.dbexceptions import ConfigurationError

        super(FsLimitedCache, self).__init__(dir, upstream=upstream, **kwargs)

        self._size = size
        self.maxsize = int(size) * 1048578

        self.readonly = False
        self.usreadonly = False
        self._database = None

        self.use_db = True

        if not os.path.isdir(self.cache_dir):
            os.makedirs(self.cache_dir)

        if not os.path.isdir(self.cache_dir):
            raise ConfigurationError(
                "Cache dir '{}' is not valid".format(
                    self.cache_dir))

Init a new FileSystem Cache

Args:
cache_dir
maxsize. Maximum size of the cache, in GB


def from_service_account_file(cls, filename, *args, **kwargs):

        credentials = service_account.Credentials.from_service_account_file(
            filename)
        kwargs['credentials'] = credentials
        return cls(*args, **kwargs)

Creates an instance of this client using the provided credentials
file.

Args:
filename (str): The path to the service account private key json
file.
args: Additional arguments to pass to the constructor.
kwargs: Additional arguments to pass to the constructor.

Returns:
dialogflow_v2.SessionEntityTypesClient: The constructed client.


def _get_task_id(source):
    if (type(source) is ray.actor.ActorHandle):
        return source._ray_actor_id
    elif (type(source) is ray.TaskID):
        return source
    else:
        return ray._raylet.compute_task_id(source)

Return the task id associated to the generic source of the signal.

Args:
source: source of the signal, it can be either an object id returned
by a task, a task id, or an actor handle.

Returns:
- If source is an object id, return id of task which creted object.
- If source is an actor handle, return id of actor's task creator.
- If source is a task id, return same task id.


class FlaubertPoolerStartLogits(nn.Module):

    def __init__(self, config: FlaubertConfig):
        super().__init__()
        self.dense = nn.Linear(config.hidden_size, 1)

    def forward(self, hidden_states: torch.FloatTensor, p_mask: Optional[torch.FloatTensor]=None) -> torch.FloatTensor:

        x = self.dense(hidden_states).squeeze(-1)
        if p_mask is not None:
            if p_mask.dtype == torch.float16:
                x = x * (1 - p_mask) - 65500 * p_mask
            else:
                x = x * (1 - p_mask) - 1e+30 * p_mask
        return x

Compute SQuAD start logits from sequence hidden states.

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


def device_function(self, var):
    if (var.type not in ('Variable', 'VariableV2', 'VarHandleOp')):
        tf.logging.debug('Place {} on last device: {}.'.format(var.name, self._last_device))
        return self._last_device
    shape = tf.TensorShape(var.get_attr('shape'))
    assert (shape.num_elements() is not None)
    size = var.get_attr('dtype').size
    (mem, device) = heapq.heappop(self._mem_device_heap)
    mem += (shape.num_elements() * size)
    heapq.heappush(self._mem_device_heap, (mem, device))
    tf.logging.debug('Place variable {} on {} and consumes {} Bytes.'.format(var.name, device, mem))
    self._last_device = device
    return device

Choose a device for the input variable.

Args:
var: an Variable.

Returns:
The device for placing the var.


def decode(self, ids, strip_extraneous=False):

    del strip_extraneous
    return " ".join([str(i) for i in ids])

Transform sequence of float values into string (float values).

Args:
ids: array of floats to be converted.
strip_extraneous: unused

Returns:
String having space separated float values.

Raises:
ValueError: if the ids are not of the appropriate size.


def add_recipe_folder(self, recipe_folder, whitelist=None):

        if whitelist is not None:
            whitelist = set(whitelist)

        if recipe_folder == '':
            recipe_folder = '.'

        for yaml_file in [x for x in os.listdir(recipe_folder) if x.endswith('.yaml')]:
            if whitelist is not None and yaml_file not in whitelist:
                continue

            recipe = RecipeObject.FromFile(os.path.join(recipe_folder, yaml_file), self._recipe_actions, self._recipe_resources)
            self._recipes[recipe.name] = recipe

        for ship_file in [x for x in os.listdir(recipe_folder) if x.endswith('.ship')]:
            if whitelist is not None and ship_file not in whitelist:
                continue

            recipe = RecipeObject.FromArchive(os.path.join(recipe_folder, ship_file), self._recipe_actions, self._recipe_resources)
            self._recipes[recipe.name] = recipe

Add all recipes inside a folder to this RecipeManager with an optional whitelist.

Args:
recipe_folder (str): The path to the folder of recipes to add.
whitelist (list): Only include files whose os.basename() matches something
on the whitelist


def _load_config_file(path):
    with io.open(utils.abs_path(path), 'r', encoding='utf-8') as f:
        conf = yaml.load(f)
        return conf

Loads a test config file.

The test config file has to be in YAML format.

Args:
path: A string that is the full path to the config file, including the
file name.

Returns:
A dict that represents info in the config file.


def get_debug_quantized_model(self) -> bytes:
    return self._get_quantized_model(is_debug=True)

Returns an instrumented quantized model.

Convert the quantized model with the initialized converter and
return bytes for model. The model will be instrumented with numeric
verification operations and should only be used for debugging.

Returns:
Model bytes corresponding to the model.
Raises:
ValueError: if converter is not passed to the debugger.


def serialize(loss):
    return serialize_keras_object(loss)

Serializes loss function or `Loss` instance.

Args:
loss: A Keras `Loss` instance or a loss function.

Returns:
Loss configuration dictionary.


def add_permission_by_name(self, code, save=False):

        if not save:
            return ["%s | %s" % (p.name, p.code) for p in
                    Permission.objects.filter(code__contains=code)]
        for p in Permission.objects.filter(code__contains=code):
            if p not in self.Permissions:
                self.Permissions(permission=p)
        if p:
            self.save()

Adds a permission with given name.

Args:
code (str): Code name of the permission.
save (bool): If False, does nothing.


def add_new_fields(data, data_to_add):

        for key, value in data_to_add.items():
            if not data.get(key):
                data[key] = value

        return data

Update resource data with new fields.

Args:
data: resource data
data_to_update: dict of data to update resource data

Returnes:
Returnes dict


def factor_mark(field_name, markers, factors, start=0, end=None):
    return field(field_name, CategoricalMarkerMapper(markers=markers, factors=factors, start=start, end=end))

Create a ``DataSpec`` dict that applies a client-side
``CategoricalMarkerMapper`` transformation to a ``ColumnDataSource``
column.

.. note::
This transform is primarily only useful with ``scatter``, which
can be parameterized by glyph type.

Args:
field_name (str) : a field name to configure ``DataSpec`` with

markers (seq[string]) : a list of markers to use to map to

factors (seq) : a sequences of categorical factors corresponding to
the palette

start (int, optional) : a start slice index to apply when the column
data has factors with multiple levels. (default: 0)

end (int, optional) : an end slice index to apply when the column
data has factors with multiple levels. (default: None)

Returns:
dict


def binary_crossentropy(target, output, from_logits=False):
    target = tensor_conversion.convert_to_tensor_v2_with_dispatch(target)
    output = tensor_conversion.convert_to_tensor_v2_with_dispatch(output)
    if hasattr(output, '_keras_logits'):
        output = output._keras_logits
        if from_logits:
            warnings.warn('"`binary_crossentropy` received `from_logits=True`, but the `output` argument was produced by a sigmoid or softmax activation and thus does not represent logits. Was this intended?"')
        from_logits = True
    if from_logits:
        return nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
    if (not isinstance(output, (ops.EagerTensor, variables_module.Variable)) and output.op.type == 'Sigmoid') and (not hasattr(output, '_keras_history')):
        assert len(output.op.inputs) == 1
        output = output.op.inputs[0]
        return nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
    epsilon_ = _constant_to_tensor(epsilon(), output.dtype.base_dtype)
    output = clip_ops.clip_by_value(output, epsilon_, 1.0 - epsilon_)
    bce = target * math_ops.log(output + epsilon())
    bce += (1 - target) * math_ops.log(1 - output + epsilon())
    return -bce

Binary crossentropy between an output tensor and a target tensor.

Args:
target: A tensor with the same shape as `output`.
output: A tensor.
from_logits: Whether `output` is expected to be a logits tensor.
By default, we consider that `output`
encodes a probability distribution.

Returns:
A tensor.


def append_transformed_structures(self, tstructs_or_transmuter):

        if isinstance(tstructs_or_transmuter, self.__class__):
            self.transformed_structures.extend(tstructs_or_transmuter
                                               .transformed_structures)
        else:
            for ts in tstructs_or_transmuter:
                assert isinstance(ts, TransformedStructure)
            self.transformed_structures.extend(tstructs_or_transmuter)

Method is overloaded to accept either a list of transformed structures
or transmuter, it which case it appends the second transmuter"s
structures.

Args:
tstructs_or_transmuter: A list of transformed structures or a
transmuter.


def connect_to_websocket(self):
    self.logger.info('Making websocket connection')
    try:
        if hasattr(self, '_ws'):
            self._ws.close()
    except:
        self.logger.debug("Couldn't terminate previous websocket connection")
    self._ws = websocket.WebSocketApp((self._get_websocket_address() + '?v=6&encoding=json'), on_message=self._ws_on_message, on_error=self._ws_on_error, on_close=self._ws_on_close)
    self._ws.on_open = self._ws_on_open
    self._ws_run_forever_wrapper = WebSocketRunForeverWrapper(self.logger, self._ws)
    self._ws_run_forever_wrapper.start()

Call this method to make the connection to the Discord websocket

This method is not blocking, so you'll probably want to call it after
initializating your Pycord object, and then move on with your code. When
you want to block on just maintaining the websocket connection, then call
``keep_running``, and it'll block until your application is interrupted.

Args:
None


def __init__(
            self, server, min=10, max=-1, accepted_queue_size=-1,
            accepted_queue_timeout=10,
    ):

        self.server = server
        self.min = min
        self.max = max
        self._threads = []
        self._queue = queue.Queue(maxsize=accepted_queue_size)
        self._queue_put_timeout = accepted_queue_timeout
        self.get = self._queue.get

Initialize HTTP requests queue instance.

Args:
server (cheroot.server.HTTPServer): web server object
receiving this request
min (int): minimum number of worker threads
max (int): maximum number of worker threads
accepted_queue_size (int): maximum number of active
requests in queue
accepted_queue_timeout (int): timeout for putting request
into queue


def call(self, url, method=None, args=None):

        if not args:
            args = {}

        if sys.version_info.major == 3:
            data = urllib.parse.urlparse(url)
            path = data.path.rstrip('/') + '/'
            _args = dict(urllib.parse.parse_qs(data.query,
                                               keep_blank_values=True))
        elif sys.version_info.major == 2:
            data = urlparse.urlparse(url)
            path = data.path.rstrip('/') + '/'
            _args = dict(urlparse.parse_qs(data.query,
                                           keep_blank_values=True))

        for elem in self._data_store:
            pattern = elem['pattern']
            function = elem['function']
            _method = elem['method']
            type_cast = elem['type_cast']

            result = re.match(pattern, path)

            if result and _method == method:
                _args = dict(_args, **result.groupdict())

                for key, val in _args.items():
                    if isinstance(_args[key], list) and len(_args[key]) == 1:
                        _args[key] = _args[key][0]

                for key, val in type_cast.items():

                    if key not in _args:
                        continue

                    if not _args[key]:
                        continue

                    if isinstance(_args[key], list):
                        for i, _val in enumerate(_args[key]):
                            _args[key][i] = self._cast(_val, val)
                    else:
                        _args[key] = self._cast(_args[key], val)

                requiered_args = self._get_function_args(function)
                for key, val in args.items():
                    if key in requiered_args:
                        _args[key] = val

                return function(**_args)

        return None

Calls the first function matching the urls pattern and method.

Args:
url (str): Url for which to call a matching function.
method (str, optional): The method used while registering a
function.
Defaults to None
args (dict, optional): Additional args to be passed to the
matching function.

Returns:
The functions return value or `None` if no function was called.


def create_tar_file(source_files, target=None):

    if target:
        filename = target
    else:
        _, filename = tempfile.mkstemp()

    with tarfile.open(filename, mode='w:gz') as t:
        for sf in source_files:

            t.add(sf, arcname=os.path.basename(sf))
    return filename

Create a tar file containing all the source_files

Args:
source_files (List[str]): List of file paths that will be contained in the tar file

Returns:
(str): path to created tar file


def compact_interval_string(value_list):
    if (not value_list):
        return ''
    value_list.sort()
    interval_list = []
    curr = []
    for val in value_list:
        if (curr and (val > (curr[(- 1)] + 1))):
            interval_list.append((curr[0], curr[(- 1)]))
            curr = [val]
        else:
            curr.append(val)
    if curr:
        interval_list.append((curr[0], curr[(- 1)]))
    return ','.join([('{}-{}'.format(pair[0], pair[1]) if (pair[0] != pair[1]) else str(pair[0])) for pair in interval_list])

Compact a list of integers into a comma-separated string of intervals.

Args:
value_list: A list of sortable integers such as a list of numbers

Returns:
A compact string representation, such as "1-5,8,12-15"


def normalize(rp_pyxb):

    def sort(r, a):
        d1_common.xml.sort_value_list_pyxb(_get_attr_or_list(r, a))
    rp_pyxb.preferredMemberNode = (set(_get_attr_or_list(rp_pyxb, 'pref')) - set(_get_attr_or_list(rp_pyxb, 'block')))
    sort(rp_pyxb, 'block')
    sort(rp_pyxb, 'pref')

Normalize a ReplicationPolicy PyXB type in place.

The preferred and blocked lists are sorted alphabetically. As blocked nodes
override preferred nodes, and any node present in both lists is removed from the
preferred list.

Args:
rp_pyxb : ReplicationPolicy PyXB object
The object will be normalized in place.


def calc_shape_step(self, stat_names, time):
    ti = np.where((self.times == time))[0][0]
    props = regionprops(self.masks[ti], self.timesteps[ti])[0]
    shape_stats = []
    for stat_name in stat_names:
        if ('moments_hu' in stat_name):
            hu_index = int(stat_name.split('_')[(- 1)])
            hu_name = '_'.join(stat_name.split('_')[:(- 1)])
            hu_val = np.log(props[hu_name][hu_index])
            if np.isnan(hu_val):
                shape_stats.append(0)
            else:
                shape_stats.append(hu_val)
        else:
            shape_stats.append(props[stat_name])
    return shape_stats

Calculate shape statistics for a single time step

Args:
stat_names: List of shape statistics calculated from region props
time: Time being investigated

Returns:
List of shape statistics


def read_execution_event(self, offset):
    with self._reader_read_locks[self._execution_path]:
        proto_string = self._get_reader(self._execution_path).read(offset)[0]
    return debug_event_pb2.DebugEvent.FromString(proto_string)

Read a DebugEvent proto at a given offset from the .execution file.

Args:
offset: Offset to read the DebugEvent proto from.

Returns:
A DebugEventProto.

Raises:
`errors.DataLossError` if offset is at a wrong location.
`IndexError` if offset is out of range of the file.


def gates_tab(backend):

    config = backend.configuration().to_dict()
    props = backend.properties().to_dict()

    multi_qubit_gates = props['gates'][3*config['n_qubits']:]

    header_html = "
{key}: {value}
"
    header_html = header_html.format(key='last_update_date',
                                     value=props['last_update_date'])

    update_date_widget = widgets.HTML(value=header_html,
                                      layout=widgets.Layout(grid_area='top'))

    gate_html = "
"
    gate_html +=

    gate_html += "
	Type	Gate error
"
    gate_footer = "
"

    left_num = math.ceil(len(multi_qubit_gates)/3)
    mid_num = math.ceil((len(multi_qubit_gates)-left_num)/2)

    left_table = gate_html

    for qub in range(left_num):
        gate = multi_qubit_gates[qub]
        name = gate['name']
        ttype = gate['gate']
        error = round(gate['parameters'][0]['value'], 5)

        left_table += "
%s"
        left_table += "	%s	%s
"
        left_table = left_table % (name, ttype, error)
    left_table += gate_footer

    middle_table = gate_html

    for qub in range(left_num, left_num+mid_num):
        gate = multi_qubit_gates[qub]
        name = gate['name']
        ttype = gate['gate']
        error = round(gate['parameters'][0]['value'], 5)

        middle_table += "
%s"
        middle_table += "	%s	%s
"
        middle_table = middle_table % (name, ttype, error)
    middle_table += gate_footer

    right_table = gate_html

    for qub in range(left_num+mid_num, len(multi_qubit_gates)):
        gate = multi_qubit_gates[qub]
        name = gate['name']
        ttype = gate['gate']
        error = round(gate['parameters'][0]['value'], 5)

        right_table += "
%s"
        right_table += "	%s	%s
"
        right_table = right_table % (name, ttype, error)
    right_table += gate_footer

    left_table_widget = widgets.HTML(value=left_table,
                                     layout=widgets.Layout(grid_area='left'))
    middle_table_widget = widgets.HTML(value=middle_table,
                                       layout=widgets.Layout(grid_area='middle'))
    right_table_widget = widgets.HTML(value=right_table,
                                      layout=widgets.Layout(grid_area='right'))

    grid = widgets.GridBox(children=[update_date_widget,
                                     left_table_widget,
                                     middle_table_widget,
                                     right_table_widget],
                           layout=widgets.Layout(
                               grid_template_rows='auto auto',
                               grid_template_columns='33% 33% 33%',
                               grid_template_areas=,
                               grid_gap='0px 0px'))

    return grid

The multiple qubit gate error widget.

Args:
backend (IBMQbackend): The backend.

Returns:
VBox: A VBox widget.


def validate_policy(topic, signer, routing_policy, nitpicky=False):
    if (topic in routing_policy):
        if (signer in routing_policy[topic]):
            return True
        else:
            _log.error(('Authorization/routing_policy error.  Topic %r.  Signer %r.' % (topic, signer)))
            return False
    elif nitpicky:
        _log.error('Authorization/routing_policy underspecified.')
        return False
    else:
        _log.warning('No routing policy defined for "{t}" but routing_nitpicky is False so the message is being treated as authorized.'.format(t=topic))
        return True

Checks that the sender is allowed to emit messages for the given topic.

Args:
topic (str): The message topic the ``signer`` used when sending the message.
signer (str): The Common Name of the certificate used to sign the message.

Returns:
bool: True if the policy defined in the settings allows the signer to send
messages on ``topic``.


def request(self, method, path, query={}, headers={}, body={}, base_url=None):
    req_headers = self._make_headers(method, path, query, headers)
    if (base_url is None):
        base_url = self._url
    url = (((base_url + path) + '?') + urlencode(query))
    if self._logging:
        utils.log(body)
        utils.log(req_headers)
        utils.log(('request url: ' + url))
    body = (json.dumps(body) if (type(body) == dict) else body)
    res = requests.request(method, url, headers=req_headers, data=body, allow_redirects=False, stream=True)
    if (res.status_code == 307):
        location = urlparse(res.headers['Location'])
        querystring = parse_qs(location.query)
        if self._logging:
            utils.log(('request redirected to: ' + location.geturl()))
        new_query = {}
        new_base_url = ((location.scheme + ':
        for key in querystring:
            new_query[key] = querystring[key][0]
        return self.request(method, location.path, query=new_query, headers=headers, base_url=new_base_url)
    elif (not (200 <= res.status_code <= 206)):
        if self._logging:
            utils.log(('request failed, details: ' + res.text), level=1)
    elif self._logging:
        utils.log(('request succeeded, details: ' + res.text))
    return res

Issues a request to Onshape

Args:
- method (str): HTTP method
- path (str): Path  e.g. /api/documents/:id
- query (dict, default={}): Query params in key-value pairs
- headers (dict, default={}): Key-value pairs of headers
- body (dict, default={}): Body for POST request
- base_url (str, default=None): Host, including scheme and port (if different from creds file)

Returns:
- requests.Response: Object containing the response from Onshape


def write_event(self, event):
    self._lock.acquire()
    try:
        self._events_writer.WriteEvent(event)
        self._event_count += 1
        if self._always_flush:
            self._events_writer.Flush()
        if (self._event_count == self._check_this_often):
            self._event_count = 0
            self._events_writer.Flush()
            file_path = os.path.join(self._events_directory, self.get_current_file_name())
            if (not tf.io.gfile.exists(file_path)):
                self._events_writer.Close()
                self._events_writer = self._create_events_writer(self._events_directory)
            elif (tf.io.gfile.stat(file_path).length > self._single_file_size_cap_bytes):
                self._events_writer.Close()
                self._events_writer = self._create_events_writer(self._events_directory)
    except IOError as err:
        logger.error('Writing to %s failed: %s', self.get_current_file_name(), err)
    self._lock.release()

Writes an event proto to disk.

This method is threadsafe with respect to invocations of itself.

Args:
event: The event proto.

Raises:
IOError: If writing the event proto to disk fails.


def _make_new_nodes(nodes_by_depth, layer_fn, layer_map, tensor_map):
    new_nodes = set()
    depth_keys = list(nodes_by_depth.keys())
    depth_keys.sort(reverse=True)
    for depth in depth_keys:
        nodes = nodes_by_depth[depth]
        for node in nodes:
            layer = node.outbound_layer
            if layer not in layer_map:
                new_layer = layer_fn(layer)
                layer_map[layer] = new_layer
                layer = new_layer
            else:
                layer = layer_map[layer]
                if isinstance(layer, InputLayer):
                    continue
            if all((tensor in tensor_map for tensor in nest.flatten(node.input_tensors))):
                args = nest.map_structure(lambda t: tensor_map.get(t, t), node.call_args)
                kwargs = nest.map_structure(lambda t: tensor_map.get(t, t), node.call_kwargs)
                output_tensors = layer(*args, **kwargs)
                first_output_tensor = nest.flatten(output_tensors)[0]
                new_nodes.add(layer._inbound_nodes[first_output_tensor._keras_history.node_index])
                for x, y in zip(nest.flatten(node.output_tensors), nest.flatten(output_tensors)):
                    tensor_map[x] = y
    return new_nodes

Uses the layers in `layer_map` to make new nodes based on `nodes_by_depth`.

Args:
nodes_by_depth: Provides structure information to create new nodes.
layer_fn: Function to clone layers.
layer_map: Map from layers in `model` to new layers.
tensor_map: Map from tensors in `model` to newly compute tensors.

Returns:
A set of new nodes. `layer_map` and `tensor_map` are updated.


def __init__(self, resolver_context):

    super(LVMFileSystem, self).__init__(resolver_context)
    self._file_object = None
    self._vslvm_volume_group = None
    self._vslvm_handle = None

Initializes a file system.

Args:
resolver_context (Context): resolver context.


def __init__(self, path, options=None):
    if not isinstance(options, TFRecordOptions):
        options = TFRecordOptions(compression_type=options)
    super(TFRecordWriter, self).__init__(compat.as_bytes(path), options._as_record_writer_options())

Opens file `path` and creates a `TFRecordWriter` writing to it.

Args:
path: The path to the TFRecords file.
options: (optional) String specifying compression type,
`TFRecordCompressionType`, or `TFRecordOptions` object.

Raises:
IOError: If `path` cannot be opened for writing.
ValueError: If valid compression_type can't be determined from `options`.


def _check_subgraph_closed(n, reachable_by_input, input_nodes_set, name_to_input_name):
    next_to_visit = [n]
    visited = set()
    while next_to_visit:
        current_node = next_to_visit.pop()
        visited.add(current_node)
        if current_node in reachable_by_input and current_node not in input_nodes_set:
            raise TypeError('Node %s uses input %s not in input_nodes.' % (n, current_node))
        if current_node not in input_nodes_set:
            next_to_visit += [input_node for input_node in name_to_input_name[current_node] if input_node not in visited]

Checks to make sure node only connects to predecessor graph through inputs.

Args:
n: Node to check
reachable_by_input: Nodes that are reachable by all inputs of subgraph
input_nodes_set: The set of nodes that are "inputs".
name_to_input_name: Maps from name to the list of inputs.

Raises:
TypeError: If the given node uses items past inputs directly.


def _publish_internal(self, push_messages):

        import requests

        response = requests.post(
            self.host + self.api_url + '/push/send',
            data=json.dumps([pm.get_payload() for pm in push_messages]),
            headers={
                'accept': 'application/json',
                'accept-encoding': 'gzip, deflate',
                'content-type': 'application/json',
            }
        )

        try:
            response_data = response.json()
        except ValueError:

            response.raise_for_status()

            raise PushServerError('Invalid server response', response)

        if 'errors' in response_data:
            raise PushServerError(
                'Request failed',
                response,
                response_data=response_data,
                errors=response_data['errors'])

        if 'data' not in response_data:
            raise PushServerError(
                'Invalid server response',
                response,
                response_data=response_data)

        response.raise_for_status()

        if len(push_messages) != len(response_data['data']):
            raise PushServerError(
                ('Mismatched response length. Expected %d %s but only '
                 'received %d' % (
                     len(push_messages),
                     'receipt' if len(push_messages) == 1 else 'receipts',
                     len(response_data['data']))),
                response,
                response_data=response_data)

        receipts = []
        for i, receipt in enumerate(response_data['data']):
            receipts.append(PushResponse(
                push_message=push_messages[i],

                status=receipt.get('status', PushResponse.ERROR_STATUS),
                message=receipt.get('message', ''),
                details=receipt.get('details', None)))

        return receipts

Send push notifications

The server will validate any type of syntax errors and the client will
raise the proper exceptions for the user to handle.

Each notification is of the form:
{
'to': 'ExponentPushToken[xxx]',
'body': 'This text gets display in the notification',
'badge': 1,
'data': {'any': 'json object'},
}

Args:
push_messages: An array of PushMessage objects.


def modify_required_power_levels(self, events=None, **kwargs):
    try:
        content = self.client.api.get_power_levels(self.room_id)
        content.update(kwargs)
        for (key, value) in list(content.items()):
            if (value is None):
                del content[key]
        if events:
            if ('events' in content):
                content['events'].update(events)
            else:
                content['events'] = events
            for (event, power_level) in list(content['events'].items()):
                if (power_level is None):
                    del content['events'][event]
        self.client.api.set_power_levels(self.room_id, content)
        return True
    except MatrixRequestError:
        return False

Modifies room power level requirements.

Args:
events(dict): Power levels required for sending specific event types,
in the form {"m.room.whatever0": 60, "m.room.whatever2": None}.
Overrides events_default and state_default for the specified
events. A level of None causes the target event to revert to the
default level as specified by events_default or state_default.
**kwargs: Key/value pairs specifying the power levels required for
various actions:

- events_default(int): Default level for sending message events
- state_default(int): Default level for sending state events
- invite(int): Inviting a user
- redact(int): Redacting an event
- ban(int): Banning a user
- kick(int): Kicking a user

Returns:
True if successful, False if not


def psq2(d1, d2):
    (d1, d2) = (flatten(d1), flatten(d2))

    def f(p):
        return sum(((p ** 2) * np.nan_to_num(np.log((p * len(p))))))
    return abs((f(d1) - f(d2)))

Compute the PSQ2 measure.

Args:
d1 (np.ndarray): The first distribution.
d2 (np.ndarray): The second distribution.


def _compute_fans(shape):

  if len(shape) < 1:
    fan_in = fan_out = 1
  elif len(shape) == 1:
    fan_in = fan_out = shape[0]
  elif len(shape) == 2:
    fan_in = shape[0]
    fan_out = shape[1]
  else:

    receptive_field_size = 1.
    for dim in shape[:-2]:
      receptive_field_size *= dim
    fan_in = shape[-2] * receptive_field_size
    fan_out = shape[-1] * receptive_field_size
  if isinstance(fan_in, tf.Dimension):
    fan_in = fan_in.value
  if isinstance(fan_out, tf.Dimension):
    fan_out = fan_out.value
  return fan_in, fan_out

Computes the number of input and output units for a weight shape.

Args:
shape: Integer shape tuple or TF tensor shape.

Returns:
A tuple of scalars (fan_in, fan_out).


def __init__(self, identifier, **kwargs):

    if not identifier:
      raise ValueError('Missing identifier value.')

    super(MountPathSpec, self).__init__(parent=None, **kwargs)
    self.identifier = identifier

Initializes a path specification.

Note that the mount path specification cannot have a parent.

Args:
identifier (str): identifier of the mount point.

Raises:
ValueError: when identifier is not set.


def _get_win_argv():
    assert is_win
    argc = ctypes.c_int()
    try:
        argv = winapi.CommandLineToArgvW(winapi.GetCommandLineW(), ctypes.byref(argc))
    except WindowsError:
        return []
    if (not argv):
        return []
    res = argv[max(0, (argc.value - len(sys.argv))):argc.value]
    winapi.LocalFree(argv)
    return res

Returns a unicode argv under Windows and standard sys.argv otherwise

Returns:
List[`fsnative`]


def implicit_step():

    def step_fn(time, next_time, coord_grid, value_grid, boundary_conditions, second_order_coeff_fn, first_order_coeff_fn, zeroth_order_coeff_fn, inner_second_order_coeff_fn, inner_first_order_coeff_fn, num_steps_performed, dtype=None, name=None):

        del num_steps_performed
        name = name or 'implicit_step'
        return parabolic_equation_step(time, next_time, coord_grid, value_grid, boundary_conditions, second_order_coeff_fn, first_order_coeff_fn, zeroth_order_coeff_fn, inner_second_order_coeff_fn, inner_first_order_coeff_fn, time_marching_scheme=implicit_scheme, dtype=dtype, name=name)
    return step_fn

Creates a stepper function with implicit time marching scheme.

Given a space-discretized equation

```
du/dt = A(t) u(t) + b(t)
```
(here `u` is a value vector, `A` and `b` are the matrix and the vector defined
by the PDE), the implicit time marching scheme approximates the right-hand
side with its value after the time step:

```
(u(t2) - u(t1)) / (t2 - t1) = A(t2) u(t2) + b(t2)
```
This scheme is stable, but is only first order accurate.
Usually, Crank-Nicolson or Extrapolation schemes are preferable.

More details can be found in `weighted_implicit_explicit.py` describing the
weighted implicit-explicit scheme - implicit scheme is a special case
with `theta = 0`.

Returns:
Callable to be used in finite-difference PDE solvers (see fd_solvers.py).


def find_by_id(self, submission_id):
    return self._attacks.get(submission_id, self._defenses.get(submission_id, self._targeted_attacks.get(submission_id, None)))

Finds submission by ID.

Args:
submission_id: ID of the submission

Returns:
SubmissionDescriptor with information about submission or None if
submission is not found.


def forward(self, input_ids: Optional[torch.Tensor]=None, attention_mask: Optional[torch.Tensor]=None, head_mask: Optional[torch.Tensor]=None, inputs_embeds: Optional[torch.Tensor]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None) -> Union[Tuple, BaseModelOutput]:
    output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
    output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    if input_ids is None and inputs_embeds is None:
        raise ValueError('Either input_ids or inputs_embeds has to be passed.')
    elif input_ids is not None and inputs_embeds is not None:
        raise ValueError('Make sure to only pass input_ids or inputs_embeds.')
    elif input_ids is not None and inputs_embeds is None:
        inputs_embeds = self.word_embeddings(input_ids)
    if attention_mask is not None:
        extended_attention_mask = (1.0 - attention_mask[:, None, None, :].repeat(1, self.config.num_encoder_attention_heads, 1, 1)) * torch.finfo(self.dtype).min
        extended_attention_mask = extended_attention_mask.to(inputs_embeds.dtype)
    else:
        extended_attention_mask = None
    position_embeddings, position_ids = self.position_embeddings(inputs_embeds.shape[:2], inputs_embeds.device)
    hidden_states = inputs_embeds + position_embeddings
    hidden_states = self.embeddings_layer_norm(hidden_states)
    hidden_states = nn.functional.dropout(hidden_states, p=self.config.dropout, training=self.training)
    encoder_hidden_states = () if output_hidden_states else None
    all_attentions = () if output_attentions else None
    if head_mask is not None:
        assert head_mask.size()[0] == len(self.layers), f'The head_mask should be specified for {len(self.layers)} layers, but it is for {head_mask.size()[0]}.'
    for idx, encoder_layer in enumerate(self.layers):
        if output_hidden_states:
            encoder_hidden_states = encoder_hidden_states + (hidden_states,)
        if self.gradient_checkpointing and self.training:
            layer_outputs = self._gradient_checkpointing_func(encoder_layer.__call__, hidden_states, extended_attention_mask, head_mask[idx] if head_mask is not None else None, output_attentions)
        else:
            layer_outputs = encoder_layer(hidden_states, attention_mask=extended_attention_mask, layer_head_mask=head_mask[idx] if head_mask is not None else None, output_attentions=output_attentions)
        hidden_states = layer_outputs[0]
        if output_attentions:
            all_attentions = all_attentions + (layer_outputs[1],)
    if output_hidden_states:
        encoder_hidden_states = encoder_hidden_states + (hidden_states,)
    if not return_dict:
        return tuple((v for v in [hidden_states, encoder_hidden_states, all_attentions] if v is not None))
    return BaseModelOutput(last_hidden_state=hidden_states, hidden_states=encoder_hidden_states, attentions=all_attentions)

Returns:

Example:

```python
>>> from transformers import AutoTokenizer, XLMProphetNetEncoder
>>> import torch

>>> tokenizer = AutoTokenizer.from_pretrained("patrickvonplaten/xprophetnet-large-uncased-standalone")
>>> model = XLMProphetNetEncoder.from_pretrained("patrickvonplaten/prophetnet-large-uncased-standalone")
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
>>> outputs = model(**inputs)

>>> last_hidden_states = outputs.last_hidden_state
```


def list_container_instance_groups(access_token, subscription_id, resource_group):

    endpoint = ''.join([get_rm_endpoint(),
                        '/subscriptions/', subscription_id,
                        '/resourcegroups/', resource_group,
                        '/providers/Microsoft.ContainerInstance/ContainerGroups',
                        '?api-version=', CONTAINER_API])
    return do_get(endpoint, access_token)

List the container groups in a resource group.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.
resource_group (str): Azure resource group name.

Returns:
HTTP response. JSON list of container groups and their properties.


def watch(self, selector, callback):
    if (selector not in self._monitors):
        self._monitors[selector] = set()
    self._monitors[selector].add(callback)

Call a function whenever a stream changes.

Args:
selector (DataStreamSelector): The selector to watch.
If this is None, it is treated as a wildcard selector
that matches every stream.
callback (callable): The function to call when a new
reading is pushed.  Callback is called as:
callback(stream, value)


def get_params(degrees, translate, scale_ranges, shears, img_size):
    angle = random.uniform(degrees[0], degrees[1])
    if (translate is not None):
        max_dx = (translate[0] * img_size[0])
        max_dy = (translate[1] * img_size[1])
        translations = (np.round(random.uniform((- max_dx), max_dx)), np.round(random.uniform((- max_dy), max_dy)))
    else:
        translations = (0, 0)
    if (scale_ranges is not None):
        scale = random.uniform(scale_ranges[0], scale_ranges[1])
    else:
        scale = 1.0
    if (shears is not None):
        shear = random.uniform(shears[0], shears[1])
    else:
        shear = 0.0
    return (angle, translations, scale, shear)

Get parameters for affine transformation

Returns:
sequence: params to be passed to the affine transformation


def Random(self):
    if (len(self.d) == 0):
        raise ValueError('Pmf contains no values.')
    target = random.random()
    total = 0.0
    for (x, p) in self.d.iteritems():
        total += p
        if (total >= target):
            return x
    assert False

Chooses a random element from this PMF.

Returns:
float value from the Pmf


def get_flattened_names(feeds_or_fetches):
    lines = []
    if isinstance(feeds_or_fetches, (list, tuple)):
        for item in feeds_or_fetches:
            lines.extend(get_flattened_names(item))
    elif isinstance(feeds_or_fetches, dict):
        for key in feeds_or_fetches:
            lines.extend(get_flattened_names(feeds_or_fetches[key]))
    else:
        lines.append(get_graph_element_name(feeds_or_fetches))
    return lines

Get a flattened list of the names in run() call feeds or fetches.

Args:
feeds_or_fetches: Feeds or fetches of the `Session.run()` call. It maybe
a Tensor, an Operation or a Variable. It may also be nested lists, tuples
or dicts. See doc of `Session.run()` for more details.

Returns:
(list of str) A flattened list of fetch names from `feeds_or_fetches`.


def kill(self):
    if self.block:
        raise TypeError(NON_BLOCKING_ERROR_MESSAGE)
    try:
        self.process.kill()
    except ProcessLookupError as exc:
        self.logger.debug(exc)

Kill the current non blocking command

Raises:
TypeError: If command is blocking


def get_all_instances(include_fastboot=False):

    if include_fastboot:
        serial_list = list_adb_devices() + list_fastboot_devices()
        return get_instances(serial_list)
    return get_instances(list_adb_devices())

Create AndroidDevice instances for all attached android devices.

Args:
include_fastboot: Whether to include devices in bootloader mode or not.

Returns:
A list of AndroidDevice objects each representing an android device
attached to the computer.


def new(self, injection_site_fn):

        return _InjectionContext(
            injection_site_fn, binding_stack=[], scope_id=scoping.UNSCOPED,
            is_scope_usable_from_scope_fn=self._is_scope_usable_from_scope_fn)

Creates a _InjectionContext.

Args:
injection_site_fn: the initial function being injected into
Returns:
a new empty _InjectionContext in the default scope


def _ensure_tf_install():
    try:
        import tensorflow as tf
    except ImportError:
        print('\n\nFailed to import TensorFlow. Please note that TensorFlow is not installed by default when you install TensorFlow Probability. This is so that users can decide whether to install the GPU-enabled TensorFlow package. To use TensorFlow Probability, please install the most recent version of TensorFlow, by following instructions at https:
        raise
    import distutils.version
    required_tensorflow_version = '1.13'
    if (distutils.version.LooseVersion(tf.__version__) < distutils.version.LooseVersion(required_tensorflow_version)):
        raise ImportError('This version of TensorFlow Probability requires TensorFlow version >= {required}; Detected an installation of version {present}. Please upgrade TensorFlow to proceed.'.format(required=required_tensorflow_version, present=tf.__version__))

Attempt to import tensorflow, and ensure its version is sufficient.

Raises:
ImportError: if either tensorflow is not importable or its version is
inadequate.


def correct_pad(inputs, kernel_size):
    img_dim = 2 if backend.image_data_format() == 'channels_first' else 1
    input_size = inputs.shape[img_dim:img_dim + 2]
    if isinstance(kernel_size, int):
        kernel_size = (kernel_size, kernel_size)
    if input_size[0] is None:
        adjust = (1, 1)
    else:
        adjust = (1 - input_size[0] % 2, 1 - input_size[1] % 2)
    correct = (kernel_size[0]
    return ((correct[0] - adjust[0], correct[0]), (correct[1] - adjust[1], correct[1]))

Returns a tuple for zero-padding for 2D convolution with downsampling.

Args:
inputs: Input tensor.
kernel_size: An integer or tuple/list of 2 integers.

Returns:
A tuple.


def sample_points_using_uncertainty(self, logits: torch.Tensor, uncertainty_function, num_points: int, oversample_ratio: int, importance_sample_ratio: float) -> torch.Tensor:
    num_boxes = logits.shape[0]
    num_points_sampled = int(num_points * oversample_ratio)
    point_coordinates = torch.rand(num_boxes, num_points_sampled, 2, device=logits.device)
    point_logits = sample_point(logits, point_coordinates, align_corners=False)
    point_uncertainties = uncertainty_function(point_logits)
    num_uncertain_points = int(importance_sample_ratio * num_points)
    num_random_points = num_points - num_uncertain_points
    idx = torch.topk(point_uncertainties[:, 0, :], k=num_uncertain_points, dim=1)[1]
    shift = num_points_sampled * torch.arange(num_boxes, dtype=torch.long, device=logits.device)
    idx += shift[:, None]
    point_coordinates = point_coordinates.view(-1, 2)[idx.view(-1), :].view(num_boxes, num_uncertain_points, 2)
    if num_random_points > 0:
        point_coordinates = torch.cat([point_coordinates, torch.rand(num_boxes, num_random_points, 2, device=logits.device)], dim=1)
    return point_coordinates

This function is meant for sampling points in [0, 1] * [0, 1] coordinate space based on their uncertainty. The
uncertainty is calculated for each point using the passed `uncertainty function` that takes points logit
prediction as input.

Args:
logits (`float`):
Logit predictions for P points.
uncertainty_function:
A function that takes logit predictions for P points and returns their uncertainties.
num_points (`int`):
The number of points P to sample.
oversample_ratio (`int`):
Oversampling parameter.
importance_sample_ratio (`float`):
Ratio of points that are sampled via importance sampling.

Returns:
point_coordinates (`torch.Tensor`):
Coordinates for P sampled points.


def render_to_terminal(self, array, cursor_pos=(0, 0)):

        height, width = self.height, self.width

        for_stdout = self.fmtstr_to_stdout_xform()
        if not self.hide_cursor:
            self.write(self.t.hide_cursor)
        if (height != self._last_rendered_height or
                width != self._last_rendered_width):
            self.on_terminal_size_change(height, width)

        current_lines_by_row = {}
        rows = list(range(height))
        rows_for_use = rows[:len(array)]
        rest_of_rows = rows[len(array):]

        for row, line in zip(rows_for_use, array):
            current_lines_by_row[row] = line
            if line == self._last_lines_by_row.get(row, None):
                continue
            self.write(self.t.move(row, 0))
            self.write(for_stdout(line))
            if len(line) < width:
                self.write(self.t.clear_eol)

        for row in rest_of_rows:
            if self._last_lines_by_row and row not in self._last_lines_by_row:
                continue
            self.write(self.t.move(row, 0))
            self.write(self.t.clear_eol)
            self.write(self.t.clear_bol)
            current_lines_by_row[row] = None

        logger.debug(
            'lines in last lines by row: %r' % self._last_lines_by_row.keys()
        )
        logger.debug(
            'lines in current lines by row: %r' % current_lines_by_row.keys()
        )
        self.write(self.t.move(*cursor_pos))
        self._last_lines_by_row = current_lines_by_row
        if not self.hide_cursor:
            self.write(self.t.normal_cursor)

Renders array to terminal and places (0-indexed) cursor

Args:
array (FSArray): Grid of styled characters to be rendered.

* If array received is of width too small, render it anyway
* If array received is of width too large,
* render the renderable portion
* If array received is of height too small, render it anyway
* If array received is of height too large,
* render the renderable portion (no scroll)


def throttle_request(self, now):
    throttling_probability = self._throttling_probability(now)
    self._all_requests.add(now, 1)
    return self._random.uniform(0, 1) < throttling_probability

Determines whether one RPC attempt should be throttled.

This should be called once each time the caller intends to send an RPC; if
it returns true, drop or delay that request (calling this function again
after the delay).

Args:
now: int, time in ms since the epoch
Returns:
bool, True if the caller should throttle or delay the request.


def exec_function(ast, globals_map):

    locals_map = globals_map
    exec ast in globals_map, locals_map
    return locals_map

Execute a python code object in the given environment.

Args:
globals_map: Dictionary to use as the globals context.
Returns:
locals_map: Dictionary of locals from the environment after execution.


def Create(path, password, generate_default_key=True):
    wallet = UserWallet(path=path, passwordKey=password, create=True)
    if generate_default_key:
        wallet.CreateKey()
    return wallet

Create a new user wallet.

Args:
path (str): A path indicating where to create or open the wallet e.g. "/Wallets/mywallet".
password (str): a 10 characters minimum password to secure the wallet with.

Returns:
UserWallet: a UserWallet instance.


def RemoveTask(self, task):

    with self._lock:
      if task.identifier not in self._tasks_abandoned:
        raise KeyError('Task {0:s} was not abandoned.'.format(task.identifier))

      if not task.has_retry:
        raise KeyError(
            'Will not remove a task {0:s} without retry task.'.format(
                task.identifier))

      del self._tasks_abandoned[task.identifier]

      logger.debug('Removed task {0:s}.'.format(task.identifier))

Removes an abandoned task.

Args:
task (Task): task.

Raises:
KeyError: if the task was not abandoned or the task was abandoned and
was not retried.


def resolve_image_as_pil(self, image_url, coords=None):
    files = self.mets.find_files(url=image_url)
    if files:
        image_filename = self.download_file(files[0]).local_filename
    else:
        image_filename = self.download_url(image_url)
    if (image_url not in self.image_cache['pil']):
        self.image_cache['pil'][image_url] = Image.open(image_filename)
    pil_image = self.image_cache['pil'][image_url]
    if (coords is None):
        return pil_image
    if (image_url not in self.image_cache['cv2']):
        log.debug('Converting PIL to OpenCV: %s', image_url)
        color_conversion = (cv2.COLOR_GRAY2BGR if (pil_image.mode in ('1', 'L')) else cv2.COLOR_RGB2BGR)
        pil_as_np_array = (np.array(pil_image).astype('uint8') if (pil_image.mode == '1') else np.array(pil_image))
        self.image_cache['cv2'][image_url] = cv2.cvtColor(pil_as_np_array, color_conversion)
    cv2_image = self.image_cache['cv2'][image_url]
    poly = np.array(coords, np.int32)
    log.debug('Cutting region %s from %s', coords, image_url)
    region_cut = cv2_image[(np.min(poly[(:, 1)]):np.max(poly[(:, 1)]), np.min(poly[(:, 0)]):np.max(poly[(:, 0)]))]
    return Image.fromarray(region_cut)

Resolve an image URL to a PIL image.

Args:
coords (list) : Coordinates of the bounding box to cut from the image

Returns:
Image or region in image as PIL.Image


def forward(self, hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor]=None, pos_emb: Optional[torch.Tensor]=None, output_attentions: Optional[torch.Tensor]=False) -> Tuple[torch.Tensor, torch.Tensor]:
    bsz, q_len, _ = hidden_states.size()
    query_states = self.linear_q(hidden_states).view(bsz, -1, self.num_heads, self.head_dim)
    key_states = self.linear_k(hidden_states).view(bsz, -1, self.num_heads, self.head_dim)
    value_states = self.linear_v(hidden_states).view(bsz, -1, self.num_heads, self.head_dim)
    bsz_pos = pos_emb.size(0)
    pos_encoding = self.linear_pos(pos_emb).view(bsz_pos, -1, self.num_heads, self.head_dim)
    query_with_bias_u = (query_states + self.pos_bias_u).transpose(1, 2)
    query_with_bias_v = (query_states + self.pos_bias_v).transpose(1, 2)
    matrix_ac = torch.matmul(query_with_bias_u, key_states.permute(0, 2, 3, 1))
    matrix_bd = torch.matmul(query_with_bias_v, pos_encoding.permute(0, 2, 3, 1))
    matrix_bd = self.shift_relative_position_tensor(matrix_bd)
    scores = (matrix_ac + matrix_bd) / math.sqrt(self.dim_key)
    if attention_mask is not None:
        expected_size = (bsz, 1, q_len)
        if attention_mask.size() != expected_size:
            raise ValueError(f'Attention mask should be of size {expected_size}, but is {attention_mask.size()}')
        attention_mask = attention_mask.unsqueeze(1).eq(0)
        min_value = float(torch.finfo(scores.dtype).min)
        scores = scores.masked_fill(attention_mask, min_value)
        attn_weights = torch.softmax(scores, dim=-1).masked_fill(attention_mask, 0.0)
    else:
        attn_weights = torch.softmax(scores, dim=-1)
    attn_weights = self.dropout(attn_weights)
    attn_output = torch.matmul(attn_weights, value_states.transpose(1, 2))
    attn_output = attn_output.transpose(1, 2).contiguous().view(bsz, q_len, -1)
    attn_output = self.linear_out(attn_output)
    if not output_attentions:
        attn_weights = None
    return (attn_output, attn_weights)

Compute 'Scaled Dot Product Attention' with rel. positional encoding.

Args:
hidden_states (`torch.Tensor` of shape `(batch, time2, size)`): Values of the hidden states
attention_mask (`torch.Tensor` of shape `(batch, time1, time2)`): Mask tensor.
pos_emb (`torch.Tensor` of shape `(batch, 2*time1-1, size)`): Positional embedding tensor.
output_attentions (`bool`, *optional*):
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
returned tensors for more detail.
Returns:
`torch.Tensor`: Output tensor of shape `(batch, time1, d_model)`.


def console_set_alignment(con: tcod.console.Console, alignment: int) -> None:
    lib.TCOD_console_set_alignment(_console(con), alignment)

Change this consoles current alignment mode.

* tcod.LEFT
* tcod.CENTER
* tcod.RIGHT

Args:
con (Console): Any Console instance.
alignment (int):

.. deprecated:: 8.5
Set :any:`Console.default_alignment` instead.


def query_icao(icao: str):
    params = {'dataSource': 'metars', 'requestType': 'retrieve', 'format': 'csv', 'hoursBeforeNow': 24}
    AWC._validate_icao(icao)
    params['stationString'] = icao
    try:
        return AWC._query(params)
    except RequestsConnectionError:
        raise AWCRequestFailed('failed to obtain requested data from AWC')

Queries AWC for the METAR of a given station

Args:
icao: station ID as a four letters-digits ICAO code

Returns: AWC result for the station


def GetHasherNamesFromString(cls, hasher_names_string):

    hasher_names = []

    if not hasher_names_string or hasher_names_string.strip() == 'none':
      return hasher_names

    if hasher_names_string.strip() == 'all':
      return cls.GetHasherNames()

    for hasher_name in hasher_names_string.split(','):
      hasher_name = hasher_name.strip()
      if not hasher_name:
        continue

      hasher_name = hasher_name.lower()
      if hasher_name in cls._hasher_classes:
        hasher_names.append(hasher_name)

    return hasher_names

Retrieves a list of a hasher names from a comma separated string.

Takes a string of comma separated hasher names transforms it to a list of
hasher names.

Args:
hasher_names_string (str): comma separated names of hashers to enable,
the string 'all' to enable all hashers or 'none' to disable all
hashers.

Returns:
list[str]: names of valid hashers from the string, or an empty list if no
valid names are found.


def micro_pyawk(filename, search, results=None, debug=None, postdebug=None):
    if (results is None):
        results = {}
    for entry in search:
        entry[0] = re.compile(entry[0])
    with zopen(filename, 'rt') as f:
        for line in f:
            for entry in search:
                match = re.search(entry[0], line)
                if (match and ((entry[1] is None) or entry[1](results, line))):
                    if (debug is not None):
                        debug(results, match)
                    entry[2](results, match)
                    if (postdebug is not None):
                        postdebug(results, match)
    return results

Small awk-mimicking search routine.

'file' is file to search through.
'search' is the "search program", a list of lists/tuples with 3 elements;
i.e. [[regex,test,run],[regex,test,run],...]
'results' is a an object that your search program will have access to for
storing results.

Here regex is either as a Regex object, or a string that we compile into a
Regex. test and run are callable objects.

This function goes through each line in filename, and if regex matches that
line *and* test(results,line)==True (or test is None) we execute
run(results,match),where match is the match object from running
Regex.match.

The default results is an empty dictionary. Passing a results object let
you interact with it in run() and test(). Hence, in many occasions it is
thus clever to use results=self.

Author: Rickard Armiento, Ioannis Petousis

Returns:
results


def _create_or_get_tensor_values_cache(self, cache_name, graph, shape=None, dtype=dtypes.float32):
    if graph is None:
        raise ValueError('Invalid graph.')
    graph_cache_var = self._cache_variable_for_graph(graph)
    if cache_name not in graph_cache_var:
        if shape is None:
            raise ValueError('shape must be provided at cache creation.')
        if dtype.is_integer:
            init_val = int(_COMPACT_TRACE_ENTRY_INIT_VALUE)
        else:
            init_val = _COMPACT_TRACE_ENTRY_INIT_VALUE
        with graph.as_default() as g, g.name_scope(None):
            graph_cache_var[cache_name] = variable_scope.get_variable(_TT_SNAPSHOT + '_' + self._escape_namescopes(cache_name), shape=shape, dtype=dtype, initializer=init_ops.constant_initializer(init_val), trainable=False, use_resource=True, collections=[_TENSOR_TRACER_STORAGE, ops.GraphKeys.LOCAL_VARIABLES])
    return graph_cache_var[cache_name]

Creates a variable as the cache to store intermediate tensor values.

Args:
cache_name: Name to be given to the cache (an instance of tf.variable).
graph: Tensorflow graph.
shape: A list of dimensions.
dtype: Data type of created cache.
Returns:
A ref to newly created or existing cache with the given dimensions.
Raises:
ValueError:
(1) If graph is None, or
(2) shape is None when a new cache needs to be created.


def sk_log_loss(y_true: Union[List[List[float]], List[List[int]], np.ndarray],
                y_predicted: Union[List[List[float]], List[List[int]], np.ndarray]) -> float:

    return log_loss(y_true, y_predicted)

Calculates log loss.

Args:
y_true: list or array of true values
y_predicted: list or array of predicted values

Returns:
Log loss


def __init__(self, fiql_op_str):

        if not fiql_op_str in OPERATOR_MAP:
            raise FiqlObjectException(
                "'%s' is not a valid FIQL operator" % fiql_op_str)
        self.value = fiql_op_str

Initialize instance of ``Operator``.

Args:
fiql_op_str (string): The FIQL operator (e.g., ";").

Raises:
FiqlObjectException: Invalid FIQL operator.


def read_graphs_event(self, offset):
    return debug_event_pb2.DebugEvent.FromString(self._get_reader(self._graphs_path).read(offset)[0])

Read a DebugEvent proto at a given offset from the .graphs file.

Args:
offset: Offset to read the DebugEvent proto from.

Returns:
A DebugEventProto.

Raises:
`errors.DataLossError` if offset is at a wrong location.
`IndexError` if offset is out of range of the file.


def _update_resource_view(self, log=False):
    update = False
    if (('id' in self.data) and self._load_from_hdx('resource view', self.data['id'])):
        update = True
    elif ('resource_id' in self.data):
        resource_views = self.get_all_for_resource(self.data['resource_id'])
        for resource_view in resource_views:
            if (self.data['title'] == resource_view['title']):
                self.old_data = self.data
                self.data = resource_view.data
                update = True
                break
    if update:
        if log:
            logger.warning(('resource view exists. Updating %s' % self.data['id']))
        self._merge_hdx_update('resource view', 'id')
    return update

Check if resource view exists in HDX and if so, update resource view

Returns:
bool: True if updated and False if not


def load_data_table(table_name, meta_file, meta):
    for table in meta['tables']:
        if (table['name'] == table_name):
            prefix = os.path.dirname(meta_file)
            relative_path = os.path.join(prefix, meta['path'], table['path'])
            return (pd.read_csv(relative_path), table)

Return the contents and metadata of a given table.

Args:
table_name(str): Name of the table.
meta_file(str): Path to the meta.json file.
meta(dict): Contents of meta.json.

Returns:
tuple(pandas.DataFrame, dict)


def GetEnvironmentVariable(self, name):

    name = name.upper()
    return self._environment_variables.get(name, None)

Retrieves an environment variable.

Args:
name (str): name of the environment variable.

Returns:
EnvironmentVariableArtifact: environment variable artifact or None
if there was no value set for the given name.


def onehot_encode(dataset, char_indices, maxlen):

    X = np.zeros((len(dataset), maxlen, len(char_indices.keys())))
    for i, sentence in enumerate(dataset):
        for t, char in enumerate(sentence):
            X[i, t, char_indices[char]] = 1
    return X

One hot encode the tokens

Args:
dataset  list of lists of tokens
char_indices  dictionary of {key=character, value=index to use encoding vector}
maxlen  int  Length of each sample
Return:
np array of shape (samples, tokens, encoding length)


def clean(self, settings):

        return {k: v for k, v in settings.items() if k in DEFAULT_SETTINGS}

Filter given settings to keep only key names available in
``DEFAULT_SETTINGS``.

Args:
settings (dict): Loaded settings.

Returns:
dict: Settings object filtered.


def Git(repository, directory, rev=None, prefix=None, shallow_clone=True):
    repository_loc = str(prefix)
    if (prefix is None):
        repository_loc = str(CFG['tmp_dir'])
    from benchbuild.utils.cmd import git
    src_dir = (local.path(repository_loc) / directory)
    if (not source_required(src_dir)):
        Copy(src_dir, '.')
        return
    extra_param = []
    if shallow_clone:
        extra_param.append('--depth')
        extra_param.append('1')
    git('clone', extra_param, repository, src_dir)
    if rev:
        with local.cwd(src_dir):
            git('checkout', rev)
    update_hash(src_dir)
    Copy(src_dir, '.')
    return repository_loc

Get a clone of the given repo

Args:
repository (str): Git URL of the SOURCE repo.
directory (str): Name of the repo folder on disk.
tgt_root (str): TARGET folder for the git repo.
Defaults to ``CFG["tmpdir"]``
shallow_clone (bool): Only clone the repository shallow
Defaults to true


def cleanup_mprocess(config_path, cfg):

    for key in ('keyFile', 'logPath', 'dbpath'):
        remove_path(cfg.get(key, None))
    isinstance(config_path, str) and os.path.exists(config_path) and remove_path(config_path)

remove all process's stuff
Args:
config_path - process's options file
cfg - process's config


def start_app_and_connect(self):
    try:
        self._start_app_and_connect()
    except AppStartPreCheckError:
        raise
    except Exception as e:
        self._ad.log.exception('Failed to start app and connect.')
        try:
            self.stop_app()
        except Exception:
            self._ad.log.exception('Failed to stop app after failure to start and connect.')
        raise e

Starts snippet apk on the device and connects to it.

This wraps the main logic with safe handling

Raises:
AppStartPreCheckError, when pre-launch checks fail.


def get_total_loss(add_regularization_losses=True, name='total_loss', scope=None):
    losses = get_losses(scope=scope)
    if add_regularization_losses:
        losses += get_regularization_losses(scope=scope)
    return math_ops.add_n(losses, name=name)

Returns a tensor whose value represents the total loss.

In particular, this adds any losses you have added with `tf.add_loss()` to
any regularization losses that have been added by regularization parameters
on layers constructors e.g. `tf.layers`. Be very sure to use this if you
are constructing a loss_op manually. Otherwise regularization arguments
on `tf.layers` methods will not function.

Args:
add_regularization_losses: A boolean indicating whether or not to use the
regularization losses in the sum.
name: The name of the returned tensor.
scope: An optional scope name for filtering the losses to return. Note that
this filters the losses added with `tf.add_loss()` as well as the
regularization losses to that scope.

Returns:
A `Tensor` whose value represents the total loss.

Raises:
ValueError: if `losses` is not iterable.


def _ConvertFileProtoToFileDescriptor(self, file_proto):

    if file_proto.name not in self._file_descriptors:
      built_deps = list(self._GetDeps(file_proto.dependency))
      direct_deps = [self.FindFileByName(n) for n in file_proto.dependency]
      public_deps = [direct_deps[i] for i in file_proto.public_dependency]

      file_descriptor = descriptor.FileDescriptor(
          pool=self,
          name=file_proto.name,
          package=file_proto.package,
          syntax=file_proto.syntax,
          options=_OptionsOrNone(file_proto),
          serialized_pb=file_proto.SerializeToString(),
          dependencies=direct_deps,
          public_dependencies=public_deps)
      scope = {}

      for dependency in built_deps:
        scope.update(self._ExtractSymbols(
            dependency.message_types_by_name.values()))
        scope.update((_PrefixWithDot(enum.full_name), enum)
                     for enum in dependency.enum_types_by_name.values())

      for message_type in file_proto.message_type:
        message_desc = self._ConvertMessageDescriptor(
            message_type, file_proto.package, file_descriptor, scope,
            file_proto.syntax)
        file_descriptor.message_types_by_name[message_desc.name] = (
            message_desc)

      for enum_type in file_proto.enum_type:
        file_descriptor.enum_types_by_name[enum_type.name] = (
            self._ConvertEnumDescriptor(enum_type, file_proto.package,
                                        file_descriptor, None, scope))

      for index, extension_proto in enumerate(file_proto.extension):
        extension_desc = self._MakeFieldDescriptor(
            extension_proto, file_proto.package, index, is_extension=True)
        extension_desc.containing_type = self._GetTypeFromScope(
            file_descriptor.package, extension_proto.extendee, scope)
        self._SetFieldType(extension_proto, extension_desc,
                           file_descriptor.package, scope)
        file_descriptor.extensions_by_name[extension_desc.name] = (
            extension_desc)

      for desc_proto in file_proto.message_type:
        self._SetAllFieldTypes(file_proto.package, desc_proto, scope)

      if file_proto.package:
        desc_proto_prefix = _PrefixWithDot(file_proto.package)
      else:
        desc_proto_prefix = ''

      for desc_proto in file_proto.message_type:
        desc = self._GetTypeFromScope(
            desc_proto_prefix, desc_proto.name, scope)
        file_descriptor.message_types_by_name[desc_proto.name] = desc

      for index, service_proto in enumerate(file_proto.service):
        file_descriptor.services_by_name[service_proto.name] = (
            self._MakeServiceDescriptor(service_proto, index, scope,
                                        file_proto.package, file_descriptor))

      self.Add(file_proto)
      self._file_descriptors[file_proto.name] = file_descriptor

    return self._file_descriptors[file_proto.name]

Creates a FileDescriptor from a proto or returns a cached copy.

This method also has the side effect of loading all the symbols found in
the file into the appropriate dictionaries in the pool.

Args:
file_proto: The proto to convert.

Returns:
A FileDescriptor matching the passed in proto.


def add_subtask(self, subtask):

        if self.stopped:
            raise InternalError("Cannot add a subtask to a parent that is already stopped")

        if not isinstance(subtask, BackgroundTask):
            raise ArgumentError("Subtasks must inherit from BackgroundTask, task={}".format(subtask))

        if subtask._loop != self._loop:
            raise ArgumentError("Subtasks must run in the same BackgroundEventLoop as their parent",
                                subtask=subtask, parent=self)

        self.subtasks.append(subtask)

Link a subtask to this parent task.

This will cause stop() to block until the subtask has also
finished.  Calling stop will not directly cancel the subtask.
It is expected that your finalizer for this parent task will
cancel or otherwise stop the subtask.

Args:
subtask (BackgroundTask): Another task that will be stopped
when this task is stopped.


def is_match(subject: Expression, pattern: Expression) -> bool:

    return any(True for _ in match(subject, pattern))

Check whether the given *subject* matches given *pattern*.

Args:
subject:
The subject.
pattern:
The pattern.

Returns:
True iff the subject matches the pattern.


def _ExtractDataStream(self, file_entry, data_stream_name, destination_path, output_writer, skip_duplicates=True):
    if ((not data_stream_name) and (not file_entry.IsFile())):
        return
    display_name = path_helper.PathHelper.GetDisplayNameForPathSpec(file_entry.path_spec)
    if skip_duplicates:
        try:
            digest = self._CalculateDigestHash(file_entry, data_stream_name)
        except (IOError, dfvfs_errors.BackEndError) as exception:
            output_writer.Write('[skipping] unable to read content of file entry: {0:s} with error: {1!s}\n'.format(display_name, exception))
            return
        if (not digest):
            output_writer.Write('[skipping] unable to read content of file entry: {0:s}\n'.format(display_name))
            return
        duplicate_display_name = self._digests.get(digest, None)
        if duplicate_display_name:
            output_writer.Write('[skipping] file entry: {0:s} is a duplicate of: {1:s} with digest: {2:s}\n'.format(display_name, duplicate_display_name, digest))
            return
        self._digests[digest] = display_name
    (target_directory, target_filename) = self._CreateSanitizedDestination(file_entry, file_entry.path_spec, data_stream_name, destination_path)
    if (not os.path.isdir(target_directory)):
        os.makedirs(target_directory)
    target_path = os.path.join(target_directory, target_filename)
    if os.path.exists(target_path):
        output_writer.Write('[skipping] unable to export contents of file entry: {0:s} because exported file: {1:s} already exists.\n'.format(display_name, target_path))
        return
    try:
        self._WriteFileEntry(file_entry, data_stream_name, target_path)
    except (IOError, dfvfs_errors.BackEndError) as exception:
        output_writer.Write('[skipping] unable to export contents of file entry: {0:s} with error: {1!s}\n'.format(display_name, exception))
        try:
            os.remove(target_path)
        except (IOError, OSError):
            pass

Extracts a data stream.

Args:
file_entry (dfvfs.FileEntry): file entry containing the data stream.
data_stream_name (str): name of the data stream.
destination_path (str): path where the extracted files should be stored.
output_writer (CLIOutputWriter): output writer.
skip_duplicates (Optional[bool]): True if files with duplicate content
should be skipped.


def get_user_field(question: str, default_value: Optional[str]=None, is_valid_answer: Optional[Callable]=None, convert_to: Optional[Callable]=None, fallback_message: Optional[str]=None) -> Any:
    if not question.endswith(' '):
        question = question + ' '
    if default_value is not None:
        question = f'{question} [{default_value}] '
    valid_answer = False
    while not valid_answer:
        answer = input(question)
        if default_value is not None and len(answer) == 0:
            answer = default_value
        if is_valid_answer is not None:
            valid_answer = is_valid_answer(answer)
        elif convert_to is not None:
            try:
                answer = convert_to(answer)
                valid_answer = True
            except Exception:
                valid_answer = False
        else:
            valid_answer = True
        if not valid_answer:
            print(fallback_message)
    return answer

A utility function that asks a question to the user to get an answer, potentially looping until it gets a valid
answer.

Args:
question (`str`): The question to ask the user.
default_value (`str`, *optional*): A potential default value that will be used when the answer is empty.
is_valid_answer (`Callable`, *optional*):
If set, the question will be asked until this function returns `True` on the provided answer.
convert_to (`Callable`, *optional*):
If set, the answer will be passed to this function. If this function raises an error on the provided
answer, the question will be asked again.
fallback_message (`str`, *optional*):
A message that will be displayed each time the question is asked again to the user.

Returns:
`Any`: The answer provided by the user (or the default), passed through the potential conversion function.


def ceil(x):
    if any_symbolic_tensors((x,)):
        return Ceil().symbolic_call(x)
    return backend.numpy.ceil(x)

Return the ceiling of the input, element-wise.

The ceil of the scalar `x` is the smallest integer `i`, such that
`i >= x`.

Args:
x: Input tensor.

Returns:
The ceiling of each element in `x`, with float dtype.


def sections_list(self, cmd=None):
    sections = list(self.common.sections)
    if (not cmd):
        if (self.bare is not None):
            sections.extend(self.bare.sections)
            return sections
        return []
    sections.extend(self.subcmds[cmd].sections)
    if (cmd in self._conf):
        sections.append(cmd)
    return sections

List of config sections used by a command.

Args:
cmd (str): command name, set to ``None`` or ``''`` for the bare
command.

Returns:
list of str: list of configuration sections used by that command.


def tile(cls, tile_assignment):
    if not isinstance(tile_assignment, _np.ndarray):
        raise TypeError('Tile assignment must be of type np.ndarray')
    dims = list(tile_assignment.shape)
    flattened_devices = tile_assignment.reshape(-1, order='C')
    return Sharding(proto=xla_data_pb2.OpSharding(type=xla_data_pb2.OpSharding.OTHER, tile_assignment_dimensions=dims, tile_assignment_devices=list(flattened_devices)))

Returns a Tiled sharding attribute.

This causes an op to be partially computed on multiple cores in the
XLA device.

Args:
tile_assignment: An np.ndarray describing the topology of the tiling and
which device will compute which part of the topology.

Raises:
TypeError: tile_assignment was not of np.array type.

TODO(jmolloy): This concept is nefarious and is not
something we really want to expose to users (especially as the
contract for tile_assignment is very strict).