def labelset_heads(self, label):

        _eps = self._eps
        _vars = self._vars
        _hcons = self._hcons
        nodeids = {nodeid: _eps[nodeid][3].get(IVARG_ROLE, None)
                for nodeid in _vars[label]['refs']['LBL']}
        if len(nodeids) <= 1:
            return list(nodeids)

        scope_sets = {}
        for nid in nodeids:
            scope_sets[nid] = _ivs_in_scope(nid, _eps, _vars, _hcons)

        out = {}
        for n in nodeids:
            out[n] = 0
            for role, val in _eps[n][3].items():
                if role == IVARG_ROLE or role == CONSTARG_ROLE:
                    continue
                elif any(val in s for n2, s in scope_sets.items() if n2 != n):
                    out[n] += 1

        candidates = [n for n, out_deg in out.items() if out_deg == 0]
        rank = {}
        for n in candidates:
            iv = nodeids[n]
            pred = _eps[n][1]
            if iv in _vars and self.nodeid(iv, quantifier=True) is not None:
                rank[n] = 0
            elif pred.is_quantifier():
                rank[n] = 0
            elif pred.type == Pred.ABSTRACT:
                rank[n] = 2
            else:
                rank[n] = 1

        return sorted(candidates, key=lambda n: rank[n])

Return the heads of the labelset selected by *label*.

Args:
label: the label from which to find head nodes/EPs.
Returns:
An iterable of nodeids.


class ReadAllFromBigQuery(PTransform):
    COUNTER = 0

    def __init__(self, gcs_location: Union[str, ValueProvider]=None, validate: bool=False, kms_key: str=None, temp_dataset: Union[str, DatasetReference]=None, bigquery_job_labels: Dict[str, str]=None, query_priority: str=BigQueryQueryPriority.BATCH):
        if gcs_location:
            if not isinstance(gcs_location, (str, ValueProvider)):
                raise TypeError('%s: gcs_location must be of type string or ValueProvider; got %r instead' % (self.__class__.__name__, type(gcs_location)))
        self.gcs_location = gcs_location
        self.validate = validate
        self.kms_key = kms_key
        self.bigquery_job_labels = bigquery_job_labels
        self.temp_dataset = temp_dataset
        self.query_priority = query_priority

    def expand(self, pcoll):
        job_name = pcoll.pipeline.options.view_as(GoogleCloudOptions).job_name
        project = pcoll.pipeline.options.view_as(GoogleCloudOptions).project
        unique_id = str(uuid.uuid4())[0:10]
        try:
            step_name = self.label
        except AttributeError:
            step_name = 'ReadAllFromBigQuery_%d' % ReadAllFromBigQuery.COUNTER
            ReadAllFromBigQuery.COUNTER += 1
        sources_to_read, cleanup_locations = pcoll | beam.ParDo(_BigQueryReadSplit(options=pcoll.pipeline.options, gcs_location=self.gcs_location, validate=self.validate, bigquery_job_labels=self.bigquery_job_labels, job_name=job_name, step_name=step_name, unique_id=unique_id, kms_key=self.kms_key, project=project, temp_dataset=self.temp_dataset, query_priority=self.query_priority)).with_outputs('location_to_cleanup', main='files_to_read')
        return sources_to_read | SDFBoundedSourceReader(data_to_display=self.display_data()) | _PassThroughThenCleanup(beam.pvalue.AsIter(cleanup_locations))

Read data from BigQuery.

PTransform:ReadFromBigQueryRequest->Rows

This PTransform uses a BigQuery export job to take a snapshot of the table
on GCS, and then reads from each produced file. Data is exported into
a new subdirectory for each export using UUIDs generated in
`ReadFromBigQueryRequest` objects.

It is recommended not to use this PTransform for streaming jobs on
GlobalWindow, since it will not be able to cleanup snapshots.

Args:
gcs_location (str): The name of the Google Cloud Storage
bucket where the extracted table should be written as a string. If
:data:`None`, then the temp_location parameter is used.
validate (bool): If :data:`True`, various checks will be done when source
gets initialized (e.g., is table present?). Set this to :data:`False`
if the BigQuery export method is slow due to checking file existence.
kms_key (str): Experimental. Optional Cloud KMS key name for use when
creating new temporary tables.


def has_checked_field(self, locator, **kwargs):
    kwargs['checked'] = True
    return self.has_selector('field', locator, **kwargs)

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

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

Returns:
bool: Whether it exists.


def pick(self, connections):
    if (len(connections) == 1):
        return connections[0]

    def key(conn):
        return (datetime.min if (conn.backoff_time is None) else conn.backoff_time)
    return min(*connections, key=key)

Picks a connection with the earliest backoff time.

As a result, the first connection is picked
for as long as it has no backoff time.
Otherwise, the connections are tried in a round robin fashion.

Args:
connections (:obj:list): List of
:class:`~bigchaindb_driver.connection.Connection` instances.


def _prepare_np_fun_name_and_fun(np_fun_name, np_fun):
    if np_fun_name is not None:
        assert isinstance(np_fun_name, str)
    if np_fun is not None:
        assert not isinstance(np_fun, str)
    if np_fun is None:
        assert np_fun_name is not None
        try:
            np_fun = getattr(np, str(np_fun_name))
        except AttributeError:
            np_fun = None
    if np_fun_name is None:
        assert np_fun is not None
        np_fun_name = np_fun.__name__
    return (np_fun_name, np_fun)

Mutually propagates information between `np_fun_name` and `np_fun`.

If one is None and the other is not, we'll try to make the former not None in
a best effort.

Args:
np_fun_name: name for the np_fun symbol. At least one of np_fun or
np_fun_name shoud be set.
np_fun: the numpy function whose docstring will be used.

Returns:
Processed `np_fun_name` and `np_fun`.


def get_variables(self, include_nontrainable=False):
    if include_nontrainable:
        return [self.all_variables[key] for key in sorted(self.all_variables)]
    else:
        return [self.variables[key] for key in sorted(self.variables)]

Returns the TensorFlow variables used by the baseline.

Returns:
List of variables


def _add_partition(self, connection, partition):
    logger.debug('Creating foreign table for partition.\n    partition: {}'.format(partition.name))
    with connection.cursor() as cursor:
        postgres_med.add_partition(cursor, partition.datafile, partition.vid)

Creates FDW for the partition.

Args:
connection:
partition (orm.Partition):


def _Parse(self, template):

    if not template:
      raise TextFSMTemplateError('Null template.')

    self._ParseFSMVariables(template)

    while self._ParseFSMState(template):
      pass

    self._ValidateFSM()

Parses template file for FSM structure.

Args:
template: Valid template file.

Raises:
TextFSMTemplateError: If template file syntax is invalid.


def train(cluster_info, cluster_meta, feed_timeout=600, qname='input'):

    def _train(iter):
        mgr = _get_manager(cluster_info, util.get_ip_address(), util.read_executor_id())
        try:
            queue = mgr.get_queue(qname)
            equeue = mgr.get_queue('error')
        except (AttributeError, KeyError):
            msg = "Queue '{}' not found on this node, check for exceptions on other nodes.".format(qname)
            raise Exception(msg)
        state = str(mgr.get('state'))
        logging.info('mgr.state={0}'.format(state))
        terminating = (state == "'terminating'")
        if terminating:
            logging.info('mgr is terminating, skipping partition')
            count = sum((1 for item in iter))
            logging.info('Skipped {0} items from partition'.format(count))
        else:
            logging.info('Feeding partition {0} into {1} queue {2}'.format(iter, qname, queue))
            count = 0
            for item in iter:
                count += 1
                queue.put(item, block=True)
            joinThr = Thread(target=queue.join)
            joinThr.start()
            timeout = feed_timeout
            while joinThr.isAlive():
                if (not equeue.empty()):
                    e_str = equeue.get()
                    equeue.task_done()
                    raise Exception(('exception in worker:\n' + e_str))
                time.sleep(1)
                timeout -= 1
                if (timeout <= 0):
                    raise Exception('Timeout while feeding partition')
            logging.info('Processed {0} items in partition'.format(count))
        if (not terminating):
            state = str(mgr.get('state'))
            terminating = (state == "'terminating'")
            if terminating:
                try:
                    logging.info('TFSparkNode: requesting stop')
                    client = reservation.Client(cluster_meta['server_addr'])
                    client.request_stop()
                    client.close()
                except Exception as e:
                    logging.debug('Error while requesting stop: {0}'.format(e))
        return [terminating]
    return _train

Feeds Spark partitions into the shared multiprocessing.Queue.

Args:
:cluster_info: node reservation information for the cluster (e.g. host, executor_id, pid, ports, etc)
:cluster_meta: dictionary of cluster metadata (e.g. cluster_id, reservation.Server address, etc)
:feed_timeout: number of seconds after which data feeding times out (600 sec default)
:qname: *INTERNAL_USE*

Returns:
A dataRDD.mapPartitions() function


def check_graph_consistency(tensor=None, method='add_loss', force_raise=False):
    if force_raise or (ops.executing_eagerly_outside_functions() and hasattr(tensor, 'graph') and tensor.graph.is_control_flow_graph):
        if method == 'activity_regularizer':
            bad_example = "\n      class TestModel(tf.keras.Model):\n\n        def __init__(self):\n          super(TestModel, self).__init__(name='test_model')\n          self.dense = tf.keras.layers.Dense(2, activity_regularizer='l2')\n\n        def call(self, x, training=None):\n          if training:\n            return self.dense(x)\n          else:\n            return self.dense(x)\n      "
            correct_example = "\n      class TestModel(tf.keras.Model):\n\n        def __init__(self):\n          super(TestModel, self).__init__(name='test_model')\n          self.dense = tf.keras.layers.Dense(2, activity_regularizer='l2')\n\n        def call(self, x, training=None):\n          return self.dense(x)\n      "
            raise RuntimeError('You are using a layer with `activity_regularizer` in a control flow branch, e.g.:\n{bad_example}\nThis is currently not supported. Please move your call to the layer with `activity_regularizer` out of the control flow branch, e.g.:\n{correct_example}\nYou can also resolve this by marking your outer model/layer dynamic (eager-only) by passing `dynamic=True` to the layer constructor. Any kind of control flow is supported with dynamic layers. Note that using `dynamic=True` requires you to implement static shape inference in the `compute_output_shape(input_shape)` method.'.format(bad_example=bad_example, correct_example=correct_example))
        if method == 'add_metric':
            bad_example = "\n      def call(self, inputs, training=None):\n        if training:\n          metric = compute_metric(inputs)\n          self.add_metric(metric, name='my_metric', aggregation='mean')\n        return inputs\n      "
            correct_example = "\n      def call(self, inputs, training=None):\n        if training:\n          metric = compute_metric(inputs)\n        else:\n          metric = 0.\n        self.add_metric(metric, name='my_metric', aggregation='mean')\n        return inputs\n      "
        elif method == 'add_loss':
            bad_example = '\n      def call(self, inputs, training=None):\n        if training:\n          loss = compute_loss(inputs)\n          self.add_loss(loss)\n        return inputs\n      '
            correct_example = '\n      def call(self, inputs, training=None):\n        if training:\n          loss = compute_loss(inputs)\n        else:\n          loss = 0.\n        self.add_loss(loss)\n        return inputs\n      '
        else:
            bad_example = '\n      def call(self, inputs, training=None):\n        if training:\n          self.add_update(self.w.assign_add(1))\n        return inputs\n      '
            correct_example = '\n      def call(self, inputs, training=None):\n        if training:\n          increment = 1\n        else:\n          increment = 0\n        self.add_update(self.w.assign_add(increment))\n        return inputs\n      '
        raise RuntimeError('You are using the method `{method}` in a control flow branch in your layer, e.g.:\n{bad_example}\nThis is not currently supported. Please move your call to {method} out of the control flow branch, e.g.:\n{correct_example}\nYou can also resolve this by marking your layer as dynamic (eager-only) by passing `dynamic=True` to the layer constructor. Any kind of control flow is supported with dynamic layers. Note that using `dynamic=True` requires you to implement static shape inference in the `compute_output_shape(input_shape)` method.'.format(method=method, bad_example=bad_example, correct_example=correct_example))

Checks that tensors passed to `add_*` method match the Keras graph.

When one of the `add_*` method is called inside a V2 conditional branch,
the underlying tensor gets created in a FuncGraph managed by control_flow_v2.
We need to raise clear error messages in such cases.

Args:
tensor: Tensor to check, or `False` if it is known that an error
should be raised.
method: Caller method, one of {'add_metric', 'add_loss', 'add_update'}.
force_raise: If an error should be raised regardless of `tensor`.

Raises:
RuntimeError: In case of an out-of-graph tensor.


def _SoftmaxGrad(op: ops.Operation, grad_softmax):
    softmax = op.outputs[0]
    sum_channels = math_ops.reduce_sum(grad_softmax * softmax, -1, keepdims=True)
    return (grad_softmax - sum_channels) * softmax

The derivative of the softmax nonlinearity.

We assume that probs is of shape [batch_size * dim]
The formula for dsoftmax / dx = (diag(softmax) - softmax * softmax').
This matrix is diagonal minus a rank one matrix, so it is easy to implement
as follows:

grad_x = grad_softmax * softmax - sum(grad_softmax * softmax) * softmax

Args:
op: the Softmax op.
grad_softmax:  the tensor representing the gradient w.r.t. the softmax
output.

Returns:
gradient w.r.t the input to the softmax


def process_file(self, path):

        if self._config.verbose:
            self._logger.info('Processing file "%s"', path)

        output_path = '%s%s' % (path, BATCH_EXTENSION)

        with open(output_path, 'w') as file:
            for line in lines_generator(path):
                file.write('%s\n' % self._cucco.normalize(
                           line.encode().decode('utf-8')))

        self._logger.debug('Created file "%s"', output_path)

Process a file applying normalizations.

Get a file as input and generate a new file with the
result of applying normalizations to every single line
in the original file. The extension for the new file
will be the one defined in BATCH_EXTENSION.

Args:
path: Path to the file.


def ParseMessage(self, parser_mediator, key, date_time, tokens):
    if (key != 'task_run'):
        raise ValueError('Unknown grammar key: {0:s}'.format(key))
    event_data = CronTaskRunEventData()
    event_data.body = tokens.get('body', None)
    event_data.command = tokens.get('command', None)
    event_data.hostname = tokens.get('hostname', None)
    event_data.offset = 0
    event_data.pid = tokens.get('pid', None)
    event_data.reporter = tokens.get('reporter', None)
    event_data.severity = tokens.get('severity', None)
    event_data.username = tokens.get('username', None)
    event = time_events.DateTimeValuesEvent(date_time, definitions.TIME_DESCRIPTION_WRITTEN)
    parser_mediator.ProduceEventWithEventData(event, event_data)

Parses a syslog body that matched one of defined grammars.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
key (str): name of the matching grammar.
date_time (dfdatetime.DateTimeValues): date and time values.
tokens (dict[str, str]): tokens derived from a syslog message based on
the defined grammar.

Raises:
ValueError: If an unknown key is provided.


def set_slats_level(self, slatsLevel=0.0, shutterLevel=None):

        if shutterLevel is None:
            shutterLevel = self.shutterLevel
        data = {
            "channelIndex": 1,
            "deviceId": self.id,
            "slatsLevel": slatsLevel,
            "shutterLevel": shutterLevel,
        }
        return self._restCall("device/control/setSlatsLevel", json.dumps(data))

sets the slats and shutter level

Args:
slatsLevel(float): the new level of the slats. 0.0 = open, 1.0 = closed,
shutterLevel(float): the new level of the shutter. 0.0 = open, 1.0 = closed, None = use the current value
Returns:
the result of the _restCall


def AddService(self, new_service):

    for service in self._services:
      if new_service == service:

        service.sources.append(new_service.sources[0])
        return

    self._services.append(new_service)

Add a new service to the list of ones we know about.

Args:
new_service (WindowsService): the service to add.


def ParseGenericRow(self, parser_mediator, query, row, **unused_kwargs):
    query_hash = hash(query)
    event_data = WindowsTimelineGenericEventData()
    payload_json_bytes = bytes(self._GetRowValue(query_hash, row, 'Payload'))
    payload_json_string = payload_json_bytes.decode('utf-8')
    appid_entries_string = self._GetRowValue(query_hash, row, 'AppId')
    payload = json.loads(payload_json_string)
    appid_entries = json.loads(appid_entries_string)
    package_id_locations = ['packageId', 'x_exe_path', 'windows_win32', 'windows_universal', 'alternateId']
    for location in package_id_locations:
        for entry in appid_entries:
            if ((entry['platform'] == location) and (entry['application'] != '')):
                event_data.package_identifier = entry['application']
                break
        if (event_data.package_identifier is None):
            break
    if ('description' in payload):
        event_data.description = payload['description']
    else:
        event_data.description = ''
    if (('appDisplayName' in payload) and (payload['appDisplayName'] != '')):
        event_data.application_display_name = payload['appDisplayName']
    elif (('displayText' in payload) and (payload['displayText'] != '')):
        event_data.application_display_name = payload['displayText']
    timestamp = self._GetRowValue(query_hash, row, 'StartTime')
    date_time = dfdatetime_posix_time.PosixTime(timestamp=timestamp)
    event = time_events.DateTimeValuesEvent(date_time, definitions.TIME_DESCRIPTION_START)
    parser_mediator.ProduceEventWithEventData(event, event_data)

Parses a generic windows timeline row.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
query (str): query that created the row.
row (sqlite3.Row): row.


def plot_predictions_histogram(Y_ph, Y, title=None):
    labels = list(set(Y).union(set(Y_ph)))
    edges = [(x - 0.5) for x in range(min(labels), (max(labels) + 2))]
    plt.hist([Y_ph, Y], bins=edges, label=['Predicted', 'Gold'])
    ax = plt.gca()
    ax.set_xticks(labels)
    plt.xlabel('Label')
    plt.ylabel('
    plt.legend(loc='upper right')
    if isinstance(title, str):
        plt.title(title)
    plt.show()

Plot a histogram comparing int predictions vs true labels by class

Args:
Y_ph: An [n] or [n, 1] np.ndarray of predicted int labels
Y: An [n] or [n, 1] np.ndarray of gold labels


def download_file(self, remote_filename, local_filename=None):

        status = 'Failed'
        if local_filename is None:
            local_filename = remote_filename

        if not self.args.force and os.access(local_filename, os.F_OK):
            if not self._confirm_overwrite(local_filename):
                self._print_results(local_filename, 'Skipped')
                return

        url = '{}{}'.format(self.base_url, remote_filename)
        r = requests.get(url, allow_redirects=True)
        if r.ok:
            open(local_filename, 'wb').write(r.content)
            status = 'Success'
        else:
            self.handle_error('Error requesting: {}'.format(url), False)

        self._print_results(local_filename, status)

Download file from github.

Args:
remote_filename (str): The name of the file as defined in git repository.
local_filename (str, optional): Defaults to None. The name of the file as it should be
be written to local filesystem.


def _get_valid_formats():
    if NO_SOX:
        return []
    so = subprocess.check_output(['sox', '-h'])
    if (type(so) is not str):
        so = str(so, encoding='UTF-8')
    so = so.split('\n')
    idx = [i for i in range(len(so)) if ('AUDIO FILE FORMATS:' in so[i])][0]
    formats = so[idx].split(' ')[3:]
    return formats

Calls SoX help for a lists of audio formats available with the current
install of SoX.

Returns:
--------
formats : list
List of audio file extensions that SoX can process.


def _clean_query_string(q):

    q = q.replace("()", "").strip()
    if q.endswith("("):
        q = q[:-1].strip()

    if q[-3:] == "AND" or q[-3:] == "NOT":
        q = q[:-3]
    elif q[-2:] == "OR":
        q = q[:-2]

    while q.count("(") > q.count(")"):
        q += ")"
    while q.count(")") > q.count("("):
        q = "(" + q

    return q.strip()

Clean up a query string for searching.

Removes unmatched parentheses and joining operators.

Arguments:
q (str): Query string to be cleaned

Returns:
str: The clean query string.


def __cloudflare_list_zones(self, *, account, **kwargs):
    done = False
    zones = []
    page = 1
    while (not done):
        kwargs['page'] = page
        response = self.__cloudflare_request(account=account, path='/zones', args=kwargs)
        info = response['result_info']
        if (('total_pages' not in info) or (page == info['total_pages'])):
            done = True
        else:
            page += 1
        zones += response['result']
    return zones

Helper function to list all zones registered in the CloudFlare system. Returns a `list` of the zones

Args:
account (:obj:`CloudFlareAccount`): A CloudFlare Account object
**kwargs (`dict`): Extra arguments to pass to the API endpoint

Returns:
`list` of `dict`


def _call(callable_obj, arg_names, namespace):

    arguments = {arg_name: getattr(namespace, arg_name)
                 for arg_name in arg_names}
    return callable_obj(**arguments)

Actually calls the callable with the namespace parsed from the command
line.

Args:
callable_obj: a callable object
arg_names: name of the function arguments
namespace: the namespace object parsed from the command line


def ParseFileObject(self, parser_mediator, file_object):

    if not self.LINE_STRUCTURES:
      raise errors.UnableToParseFile('Missing line structures.')

    encoding = self._ENCODING or parser_mediator.codepage
    text_reader = EncodedTextReader(
        encoding, buffer_size=self.BUFFER_SIZE)

    text_reader.Reset()

    try:
      text_reader.ReadLines(file_object)
    except UnicodeDecodeError as exception:
      raise errors.UnableToParseFile(
          'Not a text file, with error: {0!s}'.format(exception))

    if not self.VerifyStructure(parser_mediator, text_reader.lines):
      raise errors.UnableToParseFile('Wrong file structure.')

    for key, structure in self.LINE_STRUCTURES:
      structure.parseWithTabs()

    consecutive_line_failures = 0

    while text_reader.lines:
      if parser_mediator.abort:
        break

      tokens = None
      start = 0
      end = 0

      key = None

      index = None

      for index, (key, structure) in enumerate(self._line_structures):
        try:
          structure_generator = structure.scanString(
              text_reader.lines, maxMatches=1)
          parsed_structure = next(structure_generator, None)
        except pyparsing.ParseException:
          parsed_structure = None

        if not parsed_structure:
          continue

        tokens, start, end = parsed_structure

        if start == 0:
          break

      if tokens and start == 0:

        if index is not None and index != 0:
          key_structure = self._line_structures.pop(index)
          self._line_structures.insert(0, key_structure)

        try:
          self.ParseRecord(parser_mediator, key, tokens)
          consecutive_line_failures = 0
        except (errors.ParseError, errors.TimestampError) as exception:
          parser_mediator.ProduceExtractionWarning(
              'unable to parse record: {0:s} with error: {1!s}'.format(
                  key, exception))

        text_reader.SkipAhead(file_object, end)

      else:
        odd_line = text_reader.ReadLine(file_object)
        if odd_line:
          if len(odd_line) > 80:
            odd_line = '{0:s}...'.format(odd_line[:77])
          parser_mediator.ProduceExtractionWarning(
              'unable to parse log line: {0:s}'.format(repr(odd_line)))
          consecutive_line_failures += 1
          if (consecutive_line_failures >
              self.MAXIMUM_CONSECUTIVE_LINE_FAILURES):
            raise errors.UnableToParseFile(
                'more than {0:d} consecutive failures to parse lines.'.format(
                    self.MAXIMUM_CONSECUTIVE_LINE_FAILURES))
      try:
        text_reader.ReadLines(file_object)
      except UnicodeDecodeError as exception:
        parser_mediator.ProduceExtractionWarning(
            'unable to read lines with error: {0!s}'.format(exception))

Parses a text file-like object using a pyparsing definition.

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

Raises:
UnableToParseFile: when the file cannot be parsed.


def validate(message, ssldir=None, **config):
    for field in ['signature', 'certificate']:
        if (field not in message):
            _log.warn('No %s field found.', field)
            return False
        if (not isinstance(message[field], six.text_type)):
            _log.error(('msg[%r] is not a unicode string' % field))
            try:
                message[field] = message[field].decode('utf-8')
            except UnicodeError as e:
                _log.error("Unable to decode the message '%s' field: %s", field, str(e))
                return False
    signature = base64.b64decode(message['signature'])
    certificate = base64.b64decode(message['certificate'])
    message = fedmsg.crypto.strip_credentials(message)
    ca_location = config.get('ca_cert_location', 'https:
    crl_location = config.get('crl_location', 'https:
    try:
        (ca_certificate, crl) = utils.load_certificates(ca_location, crl_location)
        _validate_signing_cert(ca_certificate, certificate, crl)
    except (IOError, RequestException, X509StoreContextError) as e:
        try:
            (ca_certificate, crl) = utils.load_certificates(ca_location, crl_location, invalidate_cache=True)
            _validate_signing_cert(ca_certificate, certificate, crl)
        except (IOError, RequestException, X509StoreContextError) as e:
            _log.error(str(e))
            return False
    try:
        crypto_certificate = x509.load_pem_x509_certificate(certificate, default_backend())
        crypto_certificate.public_key().verify(signature, fedmsg.encoding.dumps(message).encode('utf-8'), asymmetric.padding.PKCS1v15(), hashes.SHA1())
    except InvalidSignature as e:
        _log.error('message [{m}] has an invalid signature: {e}'.format(m=message, e=str(e)))
        return False
    common_name = crypto_certificate.subject.get_attributes_for_oid(x509.oid.NameOID.COMMON_NAME)
    common_name = common_name[0]
    routing_policy = config.get('routing_policy', {})
    nitpicky = config.get('routing_nitpicky', False)
    return utils.validate_policy(message.get('topic'), common_name.value, routing_policy, nitpicky=nitpicky)

Validate the signature on the given message.

Four things must be true for the signature to be valid:

1) The X.509 cert must be signed by our CA
2) The cert must not be in our CRL.
3) We must be able to verify the signature using the RSA public key
contained in the X.509 cert.
4) The topic of the message and the CN on the cert must appear in the
:ref:`conf-routing-policy` dict.

Args:
message (dict): A signed message in need of validation. A signed message
contains the 'signature' and 'certificate' keys.
ssldir (str): The path to the directory containing PEM-encoded X.509
key pairs.

Returns:
bool: True of the message passes validation, False otherwise.


def Cleanse(obj, encoding='utf-8'):

  if isinstance(obj, int):
    return obj
  elif isinstance(obj, float):
    if obj == _INFINITY:
      return 'Infinity'
    elif obj == _NEGATIVE_INFINITY:
      return '-Infinity'
    elif math.isnan(obj):
      return 'NaN'
    else:
      return obj
  elif isinstance(obj, bytes):
    return tf.compat.as_text(obj, encoding)
  elif isinstance(obj, (list, tuple)):
    return [Cleanse(i, encoding) for i in obj]
  elif isinstance(obj, set):
    return [Cleanse(i, encoding) for i in sorted(obj)]
  elif isinstance(obj, dict):
    return {Cleanse(k, encoding): Cleanse(v, encoding) for k, v in obj.items()}
  else:
    return obj

Makes Python object appropriate for JSON serialization.

- Replaces instances of Infinity/-Infinity/NaN with strings.
- Turns byte strings into unicode strings.
- Turns sets into sorted lists.
- Turns tuples into lists.

Args:
obj: Python data structure.
encoding: Charset used to decode byte strings.

Returns:
Unicode JSON data structure.


def _one_body_mapping(a_i, a_j, threshold=0.000001):

        pauli_list = []
        for alpha in range(2):
            for beta in range(2):
                pauli_prod = Pauli.sgn_prod(a_i[alpha], a_j[beta])
                coeff = 1.0/4 * pauli_prod[1] * np.power(-1j, alpha) * np.power(1j, beta)
                pauli_term = [coeff, pauli_prod[0]]
                if np.absolute(pauli_term[0]) > threshold:
                    pauli_list.append(pauli_term)
        return Operator(paulis=pauli_list)

Subroutine for one body mapping.
Args:
a_i (Pauli): pauli at index i
a_j (Pauli): pauli at index j
threshold: (float): threshold to remove a pauli
Returns:
Operator: Operator for those paulis


def _Upgrade0To1(self, data):
    subgraph = {}
    for key_to_promote in ['tensors', 'operators', 'inputs', 'outputs']:
        subgraph[key_to_promote] = data[key_to_promote]
        del data[key_to_promote]
    data['subgraphs'] = [subgraph]

Upgrade data from Version 0 to Version 1.

Changes: Added subgraphs (which contains a subset of formally global
entries).

Args:
data: Dictionary representing the TensorFlow lite data to be upgraded.
This will be modified in-place to be an upgraded version.


def check_web_config(config_fname):
    print('Looking for config file at {0} ...'.format(config_fname))
    config = RawConfigParser()
    try:
        config.readfp(open(config_fname))
        return config
    except IOError:
        print("ERROR: Seems like the config file does not exist. Please call 'opensubmit-web configcreate' first, or specify a location with the '-c' option.")
        return None

Try to load the Django settings.
If this does not work, than settings file does not exist.

Returns:
Loaded configuration, or None.


def resolve_revision(self, dest, url, rev_options):
    rev = rev_options.arg_rev
    (sha, is_branch) = self.get_revision_sha(dest, rev)
    if (sha is not None):
        rev_options = rev_options.make_new(sha)
        rev_options.branch_name = (rev if is_branch else None)
        return rev_options
    if (not looks_like_hash(rev)):
        logger.warning("Did not find branch or tag '%s', assuming revision or ref.", rev)
    if (not rev.startswith('refs/')):
        return rev_options
    self.run_command((['fetch', '-q', url] + rev_options.to_args()), cwd=dest)
    sha = self.get_revision(dest, rev='FETCH_HEAD')
    rev_options = rev_options.make_new(sha)
    return rev_options

Resolve a revision to a new RevOptions object with the SHA1 of the
branch, tag, or ref if found.

Args:
rev_options: a RevOptions object.


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

Updates a `BuildTrigger` by its project ID and trigger ID. This API is experimental.

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


def save_libsvm(X, y, path):
    dump_svmlight_file(X, y, path, zero_based=False)

Save data as a LibSVM file.

Args:
X (numpy or scipy sparse matrix): Data matrix
y (numpy array): Target vector.
path (str): Path to the CSV file to save data.


def _FormatSizeInUnitsOf1024(self, size):

    magnitude_1024 = 0
    used_memory_1024 = float(size)
    while used_memory_1024 >= 1024:
      used_memory_1024 /= 1024
      magnitude_1024 += 1

    if 0 < magnitude_1024 <= 7:
      return '{0:.1f} {1:s}'.format(
          used_memory_1024, self._UNITS_1024[magnitude_1024])

    return '{0:d} B'.format(size)

Represents a number of bytes in units of 1024.

Args:
size (int): size in bytes.

Returns:
str: human readable string of the size.


def _add_to_schema(self, field_name, schema):
    super(ForeignKeyField, self)._add_to_schema(field_name, schema)
    if self.get_field_value('convert_fks', default=True):
        self.attribute = field_name.replace('_id', '')

Set the ``attribute`` attr to the field in question so this always
gets deserialzed into the field name without ``_id``.

Args:
field_name (str): The name of the field (the attribute name being
set in the schema).
schema (marshmallow.Schema): The actual parent schema this field
belongs to.


def hum44(msg):

    d = hex2bin(data(msg))

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

    hm = bin2int(d[50:56]) * 100.0 / 64

    return round(hm, 1)

humidity

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

Returns:
float: percentage of humidity, [0 - 100] %


def _CheckPythonModuleVersion(self, module_name, module_object, version_property, minimum_version, maximum_version):
    module_version = None
    if (not version_property.endswith('()')):
        module_version = getattr(module_object, version_property, None)
    else:
        version_method = getattr(module_object, version_property[:(- 2)], None)
        if version_method:
            module_version = version_method()
    if (not module_version):
        status_message = 'unable to determine version information for: {0:s}'.format(module_name)
        return (False, status_message)
    module_version = '{0!s}'.format(module_version)
    module_version = self._VERSION_NUMBERS_REGEX.findall(module_version)[0]
    if (module_version[(- 1)] == '.'):
        module_version = module_version[:(- 1)]
    try:
        module_version_map = list(map(int, self._VERSION_SPLIT_REGEX.split(module_version)))
    except ValueError:
        status_message = 'unable to parse module version: {0:s} {1:s}'.format(module_name, module_version)
        return (False, status_message)
    if minimum_version:
        try:
            minimum_version_map = list(map(int, self._VERSION_SPLIT_REGEX.split(minimum_version)))
        except ValueError:
            status_message = 'unable to parse minimum version: {0:s} {1:s}'.format(module_name, minimum_version)
            return (False, status_message)
        if (module_version_map < minimum_version_map):
            status_message = '{0:s} version: {1!s} is too old, {2!s} or later required'.format(module_name, module_version, minimum_version)
            return (False, status_message)
    if maximum_version:
        try:
            maximum_version_map = list(map(int, self._VERSION_SPLIT_REGEX.split(maximum_version)))
        except ValueError:
            status_message = 'unable to parse maximum version: {0:s} {1:s}'.format(module_name, maximum_version)
            return (False, status_message)
        if (module_version_map > maximum_version_map):
            status_message = '{0:s} version: {1!s} is too recent, {2!s} or earlier required'.format(module_name, module_version, maximum_version)
            return (False, status_message)
    status_message = '{0:s} version: {1!s}'.format(module_name, module_version)
    return (True, status_message)

Checks the version of a Python module.

Args:
module_object (module): Python module.
module_name (str): name of the Python module.
version_property (str): version attribute or function.
minimum_version (str): minimum version.
maximum_version (str): maximum version.

Returns:
tuple: consists:

bool: True if the Python module is available and conforms to
the minimum required version, False otherwise.
str: status message.


def load_disease_term(self, disease_obj):
    LOG.debug('Loading disease term %s into database', disease_obj['_id'])
    try:
        self.disease_term_collection.insert_one(disease_obj)
    except DuplicateKeyError as err:
        raise IntegrityError('Disease term %s already exists in database'.format(disease_obj['_id']))
    LOG.debug('Disease term saved')

Load a disease term into the database

Args:
disease_obj(dict)


def get_tag(self, name, params=None):

        return self.tag(name, action='GET', params=params)

Gets a tag from a Indicator/Group/Victim/Security Label
Args:
name: The name of the tag
params:


def from_features(cls, features, types):
    params = cls()
    if features:
        for key in sorted(features.keys()):
            feature = features[key]
            if not isinstance(feature, tuple(types)):
                raise ValueError(f"Unsupported {type(feature).__name__} {feature} for key '{key}'")
            params._add_feature(key, feature)
    params._validate()
    return params

Builds _ParseOpParams for a given set of features and allowed types.

Args:
features: A `dict` mapping feature keys to objects of a type in `types`.
types: Type of features to allow, among `FixedLenFeature`,
`VarLenFeature`, `SparseFeature`, and `FixedLenSequenceFeature`.

Returns:
A `_ParseOpParams` containing the raw parameters for `gen_parsing_ops`.

Raises:
ValueError: if `features` contains an item not in `types`, or an invalid
feature.
ValueError: if sparse and dense key sets intersect.
ValueError: if input lengths do not match up.


def download(self, folder=None):

        url = self.data.get('url', None)
        if not url:
            raise HDXError('No URL to download!')
        logger.debug('Downloading %s' % url)
        filename = self.data['name']
        format = '.%s' % self.data['format']
        if format not in filename:
            filename = '%s%s' % (filename, format)
        with Download(full_agent=self.configuration.get_user_agent()) as downloader:
            path = downloader.download_file(url, folder, filename)
            return url, path

Download resource store to provided folder or temporary folder if no folder supplied

Args:
folder (Optional[str]): Folder to download resource to. Defaults to None.

Returns:
Tuple[str, str]: (URL downloaded, Path to downloaded file)


def abort_collective_ops(self, code, message):
    self.ensure_initialized()
    pywrap_tfe.TFE_AbortCollectiveOps(self._handle, code, message)

Abort the collective ops.

This is intended to be used when a peer failure is detected, which allows
the user to handle the case instead of hanging. This aborts all on-going
collectives. After all subsequent collectives error immediately, and you
need to reset_context() to use collectives again.

Args:
code: a `tf.errors` error code.
message: a string. The error message.


def base256_encode(n, minwidth=0):

    if n > 0:
        arr = []
        while n:
            n, rem = divmod(n, 256)
            arr.append(rem)
        b = bytearray(reversed(arr))
    elif n == 0:
        b = bytearray(b'\x00')
    else:
        raise ValueError("Negative numbers not supported")

    if minwidth > 0 and len(b) < minwidth:
        padding = (minwidth - len(b)) * b'\x00'
        b = bytearray(padding) + b
    b.reverse()

    return b

Encode the input with base256.

Args:
n (int): input value.
minwidth: minimum return value length.

Raises:
ValueError: if a negative number is provided.

Returns:
bytearray:


def search_track(self, artist, album=None, track=None,
                     full_album_art_uri=False):

        subcategories = [artist]
        subcategories.append(album or '')

        result = self.get_album_artists(
            full_album_art_uri=full_album_art_uri,
            subcategories=subcategories, search_term=track,
            complete_result=True)
        result._metadata['search_type'] = 'search_track'
        return result

Search for an artist, an artist's albums, or specific track.

Args:
artist (str): an artist's name.
album (str, optional): an album name. Default `None`.
track (str, optional): a track name. Default `None`.
full_album_art_uri (bool): whether the album art URI should be
absolute (i.e. including the IP address). Default `False`.

Returns:
A `SearchResult` instance.


def diff_compute(self, text1, text2, checklines, deadline):
    if (not text1):
        return [(self.DIFF_INSERT, text2)]
    if (not text2):
        return [(self.DIFF_DELETE, text1)]
    if (len(text1) > len(text2)):
        (longtext, shorttext) = (text1, text2)
    else:
        (shorttext, longtext) = (text1, text2)
    i = longtext.find(shorttext)
    if (i != (- 1)):
        diffs = [(self.DIFF_INSERT, longtext[:i]), (self.DIFF_EQUAL, shorttext), (self.DIFF_INSERT, longtext[(i + len(shorttext)):])]
        if (len(text1) > len(text2)):
            diffs[0] = (self.DIFF_DELETE, diffs[0][1])
            diffs[2] = (self.DIFF_DELETE, diffs[2][1])
        return diffs
    if (len(shorttext) == 1):
        return [(self.DIFF_DELETE, text1), (self.DIFF_INSERT, text2)]
    hm = self.diff_halfMatch(text1, text2)
    if hm:
        (text1_a, text1_b, text2_a, text2_b, mid_common) = hm
        diffs_a = self.diff_main(text1_a, text2_a, checklines, deadline)
        diffs_b = self.diff_main(text1_b, text2_b, checklines, deadline)
        return ((diffs_a + [(self.DIFF_EQUAL, mid_common)]) + diffs_b)
    if (checklines and (len(text1) > 100) and (len(text2) > 100)):
        return self.diff_lineMode(text1, text2, deadline)
    return self.diff_bisect(text1, text2, deadline)

Find the differences between two texts.  Assumes that the texts do not
have any common prefix or suffix.

Args:
text1: Old string to be diffed.
text2: New string to be diffed.
checklines: Speedup flag.  If false, then don't run a line-level diff
first to identify the changed areas.
If true, then run a faster, slightly less optimal diff.
deadline: Time when the diff should be complete by.

Returns:
Array of changes.


def __init__(self, exclude_columns, coder=coders.registry.get_coder(Any)):
    self.coder = coder
    self.exclude_columns = exclude_columns

Encodes/decodes items of a dictionary into a single element.
Args:
exclude_columns: list of columns to exclude from the encoding.


def old_format(self, content: BeautifulSoup) -> List[str]:

        b = content.find('body')
        sender, date, nxt, rep_to = None, None, None, None
        strongs = b.findAll('strong', recursive=False)
        for s in strongs:
            field = str(s).split(">")[1].split("<")[0]
            if 'From' in field:
                sender = s.next_sibling.split("(")[0].strip()
            elif 'Date' in field:
                date_str = s.next_sibling.strip().replace("-","").replace("  "," ").strip()
                try:
                    date = parsedate_to_datetime(date_str).isoformat()[:19]
                except:
                    date = None
        sender = b.find('b').text if sender == None else sender
        sender = b.find('a').text if len(sender) == 0 else sender
        date = b.find('i').text[:19] if date == None else date

        try:
            nav = content.find('ul').findAll('li')
        except:
            nav = None
        if nav != None:
            for l in nav:
                s = l.text
                if 'Next in thread' in s:
                    nxt = '/'.join(self.email_url.split('/')[:-1]) + '/' + l.find('a')['href']
                    nxt = nxt[1:] if nxt[0] == '/' else nxt
                elif 'reply to' in s:
                    rep_to = '/'.join(self.email_url.split('/')[:-1]) + '/' + l.find('a')['href']
                    rep_to = rep_to[1:] if rep_to[0] == '/' else rep_to
        body = content.find('pre')
        body = body.text.strip() if body != None else None
        return [str(i) for i in [sender, date, body, nxt, rep_to]]

Extracts email message information if it uses the old Mailman format
Args:
content: BeautifulSoup

Returns: List[str]


def CompileReport(self, mediator):

    lines_of_text = ['Listing domains visited by all users']
    for domain in sorted(self._domains):
      lines_of_text.append(domain)

    lines_of_text.append('')
    report_text = '\n'.join(lines_of_text)
    return reports.AnalysisReport(plugin_name=self.NAME, text=report_text)

Compiles an analysis report.

Args:
mediator (AnalysisMediator): mediates interactions between
analysis plugins and other components, such as storage and dfvfs.

Returns:
AnalysisReport: the analysis report.


def subprogram_prototype(vo):

  plist = '; '.join(str(p) for p in vo.parameters)

  if isinstance(vo, VhdlFunction):
    if len(vo.parameters) > 0:
      proto = 'function {}({}) return {};'.format(vo.name, plist, vo.return_type)
    else:
      proto = 'function {} return {};'.format(vo.name, vo.return_type)

  else:
    proto = 'procedure {}({});'.format(vo.name, plist)

  return proto

Generate a canonical prototype string

Args:
vo (VhdlFunction, VhdlProcedure): Subprogram object
Returns:
Prototype string.


def _pretty_print(data_item, summarize):
    if isinstance(data_item, tensor_lib.Tensor):
        arr = data_item.numpy()
        if np.isscalar(arr):
            return str(arr)
        else:
            flat = arr.reshape((-1,))
            lst = [str(x) for x in flat[:summarize]]
            if len(lst) < flat.size:
                lst.append('...')
            return str(lst)
    else:
        return str(data_item)

Format a data item for use in an error message in eager mode.

Args:
data_item: One of the items in the "data" argument to an assert_* function.
Can be a Tensor or a scalar value.
summarize: How many elements to retain of each tensor-valued entry in data.

Returns:
An appropriate string representation of data_item


def transform_kernels(kernels, func, n_gates):
    return np.hstack([func(k) for k in np.hsplit(kernels, n_gates)])

Transforms kernel for each gate separately using given function.

Args:
kernels: Stacked array of kernels for individual gates.
func: Function applied to kernel of each gate.
n_gates: Number of gates (4 for LSTM, 3 for GRU).

Returns:
Stacked array of transformed kernels.


def create_asset_accesspolicy(access_token, name, duration, permission='1'):
    path = '/AccessPolicies'
    endpoint = ''.join([ams_rest_endpoint, path])
    body = (((((('{ \t\t"Name": "' + str(name)) + '", \t\t"DurationInMinutes": "') + duration) + '", \t\t"Permissions": "') + permission) + '" \t}')
    return do_ams_post(endpoint, path, body, access_token)

Create Media Service Asset Access Policy.

Args:
access_token (str): A valid Azure authentication token.
name (str): A Media Service Asset Access Policy Name.
duration (str): A Media Service duration.
permission (str): A Media Service permission.

Returns:
HTTP response. JSON body.


def _infer_hints_allowing_override(op1, op2, hints):
    hints = hints or _Hints()
    if hints.is_self_adjoint is None:
        is_self_adjoint = op1.is_self_adjoint and op2.is_self_adjoint
    else:
        is_self_adjoint = hints.is_self_adjoint
    if hints.is_positive_definite is None:
        is_positive_definite = op1.is_positive_definite and op2.is_positive_definite
    else:
        is_positive_definite = hints.is_positive_definite
    if is_positive_definite and hints.is_positive_definite is None:
        is_non_singular = True
    else:
        is_non_singular = hints.is_non_singular
    return _Hints(is_non_singular=is_non_singular, is_self_adjoint=is_self_adjoint, is_positive_definite=is_positive_definite)

Infer hints from op1 and op2.  hints argument is an override.

Args:
op1:  LinearOperator
op2:  LinearOperator
hints:  _Hints object holding "is_X" boolean hints to use for returned
operator.
If some hint is None, try to set using op1 and op2.  If the
hint is provided, ignore op1 and op2 hints.  This allows an override
of previous hints, but does not allow forbidden hints (e.g. you still
cannot say a real diagonal operator is not self-adjoint.

Returns:
_Hints object.


def visible(self):
    query_results = self.map((lambda el: el.is_displayed()), 'visible').results
    if query_results:
        return all(query_results)
    return False

Check whether all matched elements are visible.

Returns:
bool


def export_json(data, status, headers):
    dumped = json.dumps(data, ensure_ascii=False)
    resp = current_app.response_class(dumped, status=status, headers=headers, content_type='application/json; charset=utf-8')
    return resp

Creates a JSON response

JSON content is encoded by utf-8, not unicode escape.

Args:
data: any type object that can dump to json
status (int): http status code
headers (dict): http headers


def handle_enterprise_logistration(backend, user, **kwargs):
    request = backend.strategy.request
    enterprise_customer = get_enterprise_customer_for_running_pipeline(request, {'backend': backend.name, 'kwargs': kwargs})
    if (enterprise_customer is None):
        return
    (enterprise_customer_user, _) = EnterpriseCustomerUser.objects.update_or_create(enterprise_customer=enterprise_customer, user_id=user.id)
    enterprise_customer_user.update_session(request)

Perform the linking of user in the process of logging to the Enterprise Customer.

Args:
backend: The class handling the SSO interaction (SAML, OAuth, etc)
user: The user object in the process of being logged in with
**kwargs: Any remaining pipeline variables


def add(self, text, checked=False, sort=None):

        node = ListItem(parent_id=self.id, parent_server_id=self.server_id)
        node.checked = checked
        node.text = text
        if sort is not None:
            node.sort = sort
        self.append(node, True)
        self.touch(True)
        return node

Add a new item to the list.

Args:
text (str): The text.
checked (bool): Whether this item is checked.
sort (int): Item id for sorting.


def discount_bond_price(self, state: types.RealTensor, times: types.RealTensor, maturities: types.RealTensor, name: str=None) -> types.RealTensor:
    name = name or self._name + '_discount_bond_prices'
    with tf.name_scope(name):
        x_t = tf.convert_to_tensor(state, self._dtype)
        times = tf.convert_to_tensor(times, self._dtype)
        maturities = tf.convert_to_tensor(maturities, self._dtype)
        input_shape_times = tf.shape(times)
        mean_reversion = self._mean_reversion
        y_t = self.state_y(times)
        y_t = tf.reshape(tf.transpose(y_t), tf.concat([input_shape_times, [self._dim, self._dim]], axis=0))
        values = self._bond_reconstitution(times, maturities, mean_reversion, x_t, y_t, 1, tf.shape(times)[0])
        return values[0][0]

Returns zero-coupon bond prices `P(t,T)` conditional on `x(t)`.

Args:
state: A `Tensor` of real dtype and shape compatible with
`(num_times, dim)` specifying the state `x(t)`.
times: A `Tensor` of real dtype and shape `(num_times,)`. The time `t`
at which discount bond prices are computed.
maturities: A `Tensor` of real dtype and shape `(num_times,)`. The time
to maturity of the discount bonds.
name: Str. The name to give this op.
Default value: `discount_bond_prices`.

Returns:
A `Tensor` of real dtype and the same shape as `(num_times,)`
containing the price of zero-coupon bonds.


def get_summary(result):
    summary = {'success': result.wasSuccessful(), 'stat': {'total': result.testsRun, 'failures': len(result.failures), 'errors': len(result.errors), 'skipped': len(result.skipped), 'expectedFailures': len(result.expectedFailures), 'unexpectedSuccesses': len(result.unexpectedSuccesses)}}
    summary['stat']['successes'] = (((((summary['stat']['total'] - summary['stat']['failures']) - summary['stat']['errors']) - summary['stat']['skipped']) - summary['stat']['expectedFailures']) - summary['stat']['unexpectedSuccesses'])
    summary['time'] = {'start_at': result.start_at, 'duration': result.duration}
    summary['records'] = result.records
    return summary

get summary from test result

Args:
result (instance): HtmlTestResult() instance

Returns:
dict: summary extracted from result.

{
"success": True,
"stat": {},
"time": {},
"records": []
}


def encode(self, input_audio, start_level=0, end_level=None, bs_chunks=1):
    audio_chunks = torch.chunk(input_audio, bs_chunks, dim=0)
    music_tokens_list = []
    for chunk_i in audio_chunks:
        music_tokens_i = self._encode(chunk_i, start_level=start_level, end_level=end_level)
        music_tokens_list.append(music_tokens_i)
    music_tokens = [torch.cat(music_tokens_level, dim=0) for music_tokens_level in zip(*music_tokens_list)]
    return music_tokens

Transforms the `input_audio` to a discrete representation made out of `music_tokens`.

Args:
input_audio (`torch.Tensor`):
Raw audio which will be encoded to its discrete representation using the codebook. The closest `code`
form the codebook will be computed for each sequence of samples.
start_level (`int`, *optional*, defaults to 0):
Level at which the encoding process will start. Default to 0.
end_level (`int`, *optional*):
Level at which the encoding process will start. Default to None.
bs_chunks (int, *optional*, defaults to 1):
Number of chunks of raw audio to process at the same time.


def __init__(self, time_elements_tuple=None):

    fraction_of_second = None
    if time_elements_tuple:
      if len(time_elements_tuple) < 7:
        raise ValueError((
            'Invalid time elements tuple at least 7 elements required,'
            'got: {0:d}').format(len(time_elements_tuple)))

      milliseconds = time_elements_tuple[6]
      time_elements_tuple = time_elements_tuple[:6]

      if (milliseconds < 0 or
          milliseconds >= definitions.MILLISECONDS_PER_SECOND):
        raise ValueError('Invalid number of milliseconds.')

      fraction_of_second = (
          decimal.Decimal(milliseconds) / definitions.MILLISECONDS_PER_SECOND)

    super(TimeElementsInMilliseconds, self).__init__(
        fraction_of_second=fraction_of_second,
        time_elements_tuple=time_elements_tuple)
    self._precision = definitions.PRECISION_1_MILLISECOND

Initializes time elements.

Args:
time_elements_tuple (Optional[tuple[int, int, int, int, int, int, int]]):
time elements, contains year, month, day of month, hours, minutes,
seconds and milliseconds.

Raises:
ValueError: if the time elements tuple is invalid.


def useQt(qtLib: str = 'PyQt5', period: float = 0.01):

    def qt_step():
        loop.call_later(period, qt_step)
        if not stack:
            qloop = QEventLoop()
            timer = QTimer()
            timer.timeout.connect(qloop.quit)
            stack.append((qloop, timer))
        qloop, timer = stack.pop()
        timer.start(0)
        qloop.exec_()
        timer.stop()
        stack.append((qloop, timer))

    if qtLib not in ('PyQt5', 'PySide2'):
        raise RuntimeError(f'Unknown Qt library: {qtLib}')
    if qtLib == 'PyQt5':
        from PyQt5.Qt import QApplication, QTimer, QEventLoop
    else:
        from PySide2.QtWidgets import QApplication
        from PySide2.QtCore import QTimer, QEventLoop
    global qApp
    qApp = QApplication.instance() or QApplication(sys.argv)
    loop = asyncio.get_event_loop()
    stack: list = []
    qt_step()

Run combined Qt5/asyncio event loop.

Args:
qtLib: Name of Qt library to use, can be 'PyQt5' or 'PySide2'.
period: Period in seconds to poll Qt.


def percentile(self, percent):

        if percent >= 100:
            percent = 100

        target = len(self) - len(self) * (percent / 100)

        for k in reversed(sorted(self._data.keys())):
            target -= self._data[k]
            if target < 0:
                return k

        return 10

Return the value that is the Nth precentile in the histogram.

Args:
percent (Union[int, float]): The precentile being sought. The
default consumer implementations use consistently use ``99``.

Returns:
int: The value corresponding to the requested percentile.


def _new_population_genalg(population, fitnesses, mutation_chance=0.02, crossover_chance=0.7, selection_function=gaoperators.tournament_selection, crossover_function=gaoperators.one_point_crossover):
    intermediate_population = selection_function(population, fitnesses)
    new_population = _crossover(intermediate_population, crossover_chance, crossover_function)
    gaoperators.random_flip_mutate(new_population, mutation_chance)
    return new_population

Perform all genetic algorithm operations on a population, and return a new population.

population must have an even number of chromosomes.

Args:
population: A list of binary lists, ex. [[0,1,1,0], [1,0,1,0]]
fitness: A list of fitnesses that correspond with chromosomes in the population,
ex. [1.2, 10.8]
mutation_chance: the chance that a bit will be flipped during mutation
crossover_chance: the chance that two parents will be crossed during crossover
selection_function: A function that will select parents for crossover and mutation
crossover_function: A function that will cross two parents

Returns:
list; A new population of chromosomes, that should be more fit.


def Unregister(self, name):
    precondition.AssertType(name, Text)
    try:
        del self._constructors[name]
    except KeyError:
        raise ValueError(("Constructor with name '%s' is not registered" % name))

Unregisters a constructor.

Args:
name: A name of the constructor to unregister.

Raises:
ValueError: If constructor with specified name has never been registered.


class MaxTimeCriteria(StoppingCriteria):

    def __init__(self, max_time: float, initial_timestamp: Optional[float]=None):
        self.max_time = max_time
        self.initial_timestamp = time.time() if initial_timestamp is None else initial_timestamp

    @add_start_docstrings(STOPPING_CRITERIA_INPUTS_DOCSTRING)
    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> torch.BoolTensor:
        is_done = time.time() - self.initial_timestamp > self.max_time
        return torch.full((input_ids.shape[0],), is_done, device=input_ids.device, dtype=torch.bool)

This class can be used to stop generation whenever the full generation exceeds some amount of time. By default, the
time will start being counted when you initialize this function. You can override this by passing an
`initial_time`.

Args:
max_time (`float`):
The maximum allowed time in seconds for the generation.
initial_time (`float`, *optional*, defaults to `time.time()`):
The start of the generation allowed time.


def ch_duration(self, *channels: List[Channel]) -> int:

        return self.timeslots.ch_duration(*channels)

Return duration of supplied channels.

Args:
*channels: Supplied channels


def add_arguments(cls, parser):

        parser.add_argument(
            '-c', '--create-missing-tasks',
            action='store_true',
            dest='create_missing_tasks',
            help="[sync] create asana tasks for issues without tasks"
            )

        parser.add_argument(
            '-l', '--sync-labels',
            action='store_true',
            dest='sync_labels',
            help="[sync] sync labels and milestones for each issue"
            )

Add arguments to the parser for collection in app.args.

Args:
parser:
`argparse.ArgumentParser`. Parser.
Arguments added here are server on
self.args.


def quick_execute(op_name, num_outputs, inputs, attrs, ctx, name=None):
    device_name = ctx.device_name
    try:
        ctx.ensure_initialized()
        tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, inputs, attrs, num_outputs)
    except core._NotOkStatusException as e:
        if name is not None:
            e.message += ' name: ' + name
        raise core._status_to_exception(e) from None
    except TypeError as e:
        keras_symbolic_tensors = [x for x in inputs if _is_keras_symbolic_tensor(x)]
        if keras_symbolic_tensors:
            raise core._SymbolicException('Inputs to eager execution function cannot be Keras symbolic tensors, but found {}'.format(keras_symbolic_tensors))
        raise e
    return tensors

Execute a TensorFlow operation.

Args:
op_name: Name of the TensorFlow operation (see REGISTER_OP in C++ code) to
execute.
num_outputs: The number of outputs of the operation to fetch. (Explicitly
provided instead of being inferred for performance reasons).
inputs: A list of inputs to the operation. Each entry should be a Tensor, or
a value which can be passed to the Tensor constructor to create one.
attrs: A tuple with alternating string attr names and attr values for this
operation.
ctx: The value of context.context().
name: Customized name for the operation.

Returns:
List of output Tensor objects. The list is empty if there are no outputs

Raises:
An exception on error.


def load_object(obj) -> object:
    if isinstance(obj, str):
        if (':' in obj):
            (module_name, obj_name) = obj.split(':')
            if (not module_name):
                module_name = '.'
        else:
            module_name = obj
        obj = importlib.import_module(module_name)
        if obj_name:
            attrs = obj_name.split('.')
            for attr in attrs:
                obj = getattr(obj, attr)
    return obj

Load an object.

Args:
obj (str|object): Load the indicated object if this is a string;
otherwise, return the object as is.

To load a module, pass a dotted path like 'package.module';
to load an an object from a module pass a path like
'package.module:name'.

Returns:
object


def map_err(self, op: Callable[[E], F]) -> 'Union[Result[T, F], Result[T, E]]':

        return self if self._is_ok else cast(
            'Result[T, F]',
            self._type.Err(op(cast(E, self._val)))
        )

Applies a function to the contained :meth:`Result.Err` value.

Args:
op: The function to apply to the :meth:`Result.Err` value.

Returns:
A :class:`Result` with its error value as the function result
if `self` is a :meth:`Result.Err` value, otherwise returns
`self`.

Examples:
>>> Ok(1).map_err(lambda x: x * 2)
Ok(1)
>>> Err(1).map_err(lambda x: x * 2)
Err(2)


def cv_squared(x):
    epsilon = 1e-10
    float_size = (tf.to_float(tf.size(x)) + epsilon)
    mean = (tf.reduce_sum(x) / float_size)
    variance = (tf.reduce_sum(tf.squared_difference(x, mean)) / float_size)
    return (variance / (tf.square(mean) + epsilon))

The squared coefficient of variation of a sample.

Useful as a loss to encourage a positive distribution to be more uniform.
Epsilons added for numerical stability.
Returns 0 for an empty Tensor.

Args:
x: a `Tensor`.

Returns:
a `Scalar`.


def load_module(self, namespace, module_name):
    try:
        filename, src = self.typeshed.get_module_file(namespace, module_name, self.options.python_version)
    except OSError:
        return (None, None)
    ast = parser.parse_string(src, filename=filename, name=module_name, options=self.options)
    return (filename, ast)

Load and parse a *.pyi from typeshed.

Args:
namespace: one of "stdlib" or "third_party"
module_name: the module name (without any file extension or "__init__"
suffix).

Returns:
(None, None) if the module doesn't have a definition.
Else a tuple of the filename and the AST of the module.


def add_spin_by_site(self, spins):
    if (len(spins) != len(self.sites)):
        raise ValueError('Spin of all sites must be specified in the dictionary.')
    for (site, spin) in zip(self.sites, spins):
        new_sp = {}
        for (sp, occu) in site.species.items():
            sym = sp.symbol
            oxi_state = getattr(sp, 'oxi_state', None)
            new_sp[Specie(sym, oxidation_state=oxi_state, properties={'spin': spin})] = occu
        site.species = new_sp

Add spin states to a structure by site.

Args:
spins (list): List of spins
E.g., [+5, -5, 0, 0]


def get_flights(self, search_key):
    url = AIRLINE_FLT_BASE.format(search_key, 100)
    return self._fr24.get_airline_flight_data(url)

Get the flights for a particular airline.

Given a full or partial flight number string, this method returns the first 100 flights matching that string.

Please note this method was different in earlier versions. The older versions took an airline code and returned all scheduled flights for that airline

Args:
search_key (str): Full or partial flight number for any airline e.g. MI47 to get all SilkAir flights starting with MI47

Returns:
A list of dicts, one for each scheduled flight in the airlines network

Example::
from pyflightdata import FlightData
f=FlightData()
#optional login
f.login(myemail,mypassword)
f.get_flights('MI47')


def _beam_decode(self, features, decode_length, beam_size, top_beams, alpha, use_tpu=False):
    return self._beam_decode_slow(features, decode_length, beam_size, top_beams, alpha, use_tpu)

Beam search decoding.

Models should ideally implement a more efficient version of this function.

Args:
features: an map of string to `Tensor`
decode_length: an integer.  How many additional timesteps to decode.
beam_size: number of beams.
top_beams: an integer. How many of the beams to return.
alpha: Float that controls the length penalty. larger the alpha, stronger
the preference for longer translations.
use_tpu: A bool, whether to do beam decode on TPU.

Returns:
samples: an integer `Tensor`. Top samples from the beam search


def _verify_setup(self):
    if not self._is_chief:
        for op in self._graph.get_operations():
            if op.type in ['Variable', 'VariableV2'] and (not op.device):
                raise ValueError('When using replicas, all Variables must have their device set: %s' % op)

Check that all is good.

Raises:
ValueError: If something is not good.


def delete_customer(self, customer_id):

        return self.client._delete(self.url + 'customers/{}'.format(customer_id), headers=self.get_headers())

Removes a user from the system.

Args:
customer_id: Identifier of the client to be deleted.

Returns:


def __init__(self, latitude, longitude, comment=None):

        super(Xearth, self).__init__(latitude, longitude)
        self.comment = comment

Initialise a new ``Xearth`` object.

Args:
latitude (float): Location's latitude
longitude (float): Location's longitude
comment (str): Comment for location


def list_files(file_directory,
               file_extensions=None,
               include_subfolders=True,
               include_root=True,
               root_dir=None):

    log = logging.getLogger("%s" % (inspect.stack()[0][3]))
    log.setLevel(__LOG_LEVEL__)

    rtn_list = []
    if not root_dir:
        root_dir = file_directory
    root_dir = root_dir.strip()
    if root_dir.endswith(os.path.sep):
        root_dir = root_dir.strip()[:-1]
    dir_parts_len = len(root_dir.split(os.path.sep))
    level = 0
    for root, dirnames, filenames in os.walk(file_directory):
        root_str = root
        if level > 0 and not include_subfolders:
            break
        if not include_root:
            root_str = os.path.sep.join(root.split(os.path.sep)[dir_parts_len:])
        if file_extensions:
            files = [(x,
                      os.path.join(root_str, x),
                      os.path.getmtime(os.path.join(root, x)),
                      os.path.join(root, x))
                     for x in filenames \
                     if "." in x \
                     and x.split(".")[len(x.split("."))-1] in file_extensions]
        else:
            files = [(x,
                      os.path.join(root_str, x),
                      os.path.getmtime(os.path.join(root, x)),
                      os.path.join(root, x))
                     for x in filenames]
        rtn_list += files
        level += 1
    rtn_list.sort(key=lambda tup: tup[0], reverse=True)
    return rtn_list

Returns a list of files

args:
file_directory: a sting path to the file directory
file_extensions: a list of file extensions to filter example
['xml', 'rdf']. If none include all files
include_subfolders: as implied
include_root: whether to include the root in the path
root_dir: the root directory to remove if include_root is False

returns:
(tuple) (file_name, file_path_with_root_mod, modified_time, full_path)


def dict_load(self, ns_dict):
    for (prefix, uri) in ns_dict.items():
        self.bind(prefix, uri, override=False, calc=False)
    self.__make_dicts__

Reads a dictionary of namespaces and binds them to the manager

Args:
ns_dict: dictionary with the key as the prefix and the value
as the uri


def nth(series, n, order_by=None):
    if (order_by is not None):
        series = order_series_by(series, order_by)
    try:
        return series.iloc[n]
    except:
        return np.nan

Returns the nth value of a series.

Args:
series (pandas.Series): column to summarize.
n (integer): position of desired value. Returns `NaN` if out of range.

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


def get_log_file_timestamp(delta=None):
    return _get_timestamp('%m-%d-%Y_%H-%M-%S-%f', delta)

Returns a timestamp in the format used for log file names.

Default is current time. If a delta is set, the return value will be
the current time offset by delta seconds.

Args:
delta: Number of seconds to offset from current time; can be negative.

Returns:
A timestamp in log filen name format with an offset.


def Decompress(self, compressed_data):

    try:
      if hasattr(lzma, 'LZMA_VERSION'):

        uncompressed_data = self._lzma_decompressor.decompress(
            compressed_data, 0)
      else:
        uncompressed_data = self._lzma_decompressor.decompress(compressed_data)

      remaining_compressed_data = getattr(
          self._lzma_decompressor, 'unused_data', b'')

    except (EOFError, IOError, LZMAError) as exception:
      raise errors.BackEndError((
          'Unable to decompress XZ compressed stream with error: '
          '{0!s}.').format(exception))

    return uncompressed_data, remaining_compressed_data

Decompresses the compressed data.

Args:
compressed_data (bytes): compressed data.

Returns:
tuple(bytes, bytes): uncompressed data and remaining compressed data.

Raises:
BackEndError: if the XZ compressed stream cannot be decompressed.


def map_kegg_all_genes(organism_code, target_db):
    mapping = bs_kegg.conv(target_db, organism_code)
    new_mapping = {}
    for (k, v) in mapping.items():
        new_mapping[k.replace((organism_code + ':'), '')] = str(v.split(':')[1])
    return new_mapping

Map all of an organism's gene IDs to the target database.

This is faster than supplying a specific list of genes to map,
plus there seems to be a limit on the number you can map with a manual REST query anyway.

Args:
organism_code: the three letter KEGG code of your organism
target_db: ncbi-proteinid | ncbi-geneid | uniprot

Returns:
Dictionary of ID mapping


def __init__(self, option=None, default=None, *args, **kwargs):

        super(ListOption, self).__init__(*args, **kwargs)
        if not isinstance(option, opt.Option):

            raise TypeError("Option must be an option type.")

        self._option = option
        self._default = default

        if default is not None:

            self._value = self.coerce(default)

Initialize the option with an option type.

Args:
option (option.Option): The option which is used to validate all
list options.

Raises:
TypeError: If the given option is not an instance of option.Option.
TypeError: If the default value is set but not an iterable.


def copy(self, dest):
    if os.path.isfile(self.path):
        shutil.copy2(self.path, dest)
    else:
        shutil.copytree(self.path, dest, symlinks=False, ignore=None)

Copy item to the given `dest` path.

Args:
* dest: destination path to copy.


def resize(self, image: np.ndarray, size: Dict[str, int], resample: PILImageResampling=PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]]=None, input_data_format: Optional[Union[str, ChannelDimension]]=None, **kwargs) -> np.ndarray:
    size = get_size_dict(size, default_to_square=False)
    if 'shortest_edge' in size:
        output_size = get_resize_output_image_size(image, size['shortest_edge'], default_to_square=False, input_data_format=input_data_format)
    elif 'height' in size and 'width' in size:
        output_size = (size['height'], size['width'])
    else:
        raise ValueError(f"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}")
    return resize(image, size=output_size, resample=resample, data_format=data_format, input_data_format=input_data_format, **kwargs)

Resize an image.

Args:
image (`np.ndarray`):
Image to resize.
size (`Dict[str, int]`):
Size of the output image. If `size` is of the form `{"height": h, "width": w}`, the output image will
have the size `(h, w)`. If `size` is of the form `{"shortest_edge": s}`, the output image will have its
shortest edge of length `s` while keeping the aspect ratio of the original image.
resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`):
Resampling filter to use when resiizing the image.
data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the image. If not provided, it will be the same as the input image.
input_data_format (`str` or `ChannelDimension`, *optional*):
The channel dimension format of the input image. If not provided, it will be inferred.


def _group_chunks_by_entities(self, chunks, entities):

    for entity in entities:
      chunks_to_concat = chunks.get_overlaps(
          entity['beginOffset'], len(entity['content']))
      if not chunks_to_concat:
        continue
      new_chunk_word = u''.join([chunk.word for chunk in chunks_to_concat])
      new_chunk = Chunk(new_chunk_word)
      chunks.swap(chunks_to_concat, new_chunk)
    return chunks

Groups chunks by entities retrieved from NL API Entity Analysis.

Args:
chunks (:obj:`budou.chunk.ChunkList`): List of chunks to be processed.
entities (:obj:`list` of :obj:`dict`): List of entities.

Returns:
A chunk list. (:obj:`budou.chunk.ChunkList`)


def get_dropout_mask_for_cell(self, inputs, training, count=1):
    if self.dropout == 0:
        return None
    init_kwargs = dict(inputs=inputs, training=training, count=count)
    return self._dropout_mask_cache.setdefault(kwargs=init_kwargs)

Get the dropout mask for RNN cell's input.

It will create mask based on context if there isn't any existing cached
mask. If a new mask is generated, it will update the cache in the cell.

Args:
inputs: The input tensor whose shape will be used to generate dropout
mask.
training: Boolean tensor, whether its in training mode, dropout will be
ignored in non-training mode.
count: Int, how many dropout mask will be generated. It is useful for cell
that has internal weights fused together.
Returns:
List of mask tensor, generated or cached mask based on context.


def _SanitizedMRO(obj):
    return_list = []
    for cls in tf_inspect.getmro(obj):
        if cls.__name__ == '_NewClass':
            continue
        str_repr = _NormalizeType(str(cls))
        return_list.append(str_repr)
        if 'tensorflow' not in str_repr and 'keras' not in str_repr:
            break
        if 'StubOutForTesting' in str_repr:
            break
    return return_list

Get a list of superclasses with minimal amount of non-TF classes.

Based on many parameters like python version, OS, protobuf implementation
or changes in google core libraries the list of superclasses of a class
can change. We only return the first non-TF class to be robust to non API
affecting changes. The Method Resolution Order returned by `tf_inspect.getmro`
is still maintained in the return value.

Args:
obj: A python routine for us the create the sanitized arspec of.

Returns:
list of strings, string representation of the class names.


def SetTimelineOwner(self, username):
    self._timeline_owner = username
    logger.info('Owner of the timeline: {0!s}'.format(self._timeline_owner))

Sets the username of the user that should own the timeline.

Args:
username (str): username.


def add_property(self, set_property, name, starting_value, tag_name=None):

    def del_property(self, tag_name):
        try:
            del self._content[tag_name]
        except KeyError:
            pass

    def get_property(self, tag_name):
        try:
            return self._content[tag_name]
        except KeyError:
            return None
    tag_name = (name if (tag_name is None) else tag_name)
    fget = (lambda self: get_property(self, tag_name))
    fdel = (lambda self: del_property(self, tag_name))
    fset = (lambda self, value: set_property(value))
    setattr(self.__class__, name, property(fget, fset, fdel))
    set_property(starting_value)

Set properies of atributes stored in content using stored common fdel and fget and given fset.

Args:
set_property -- Function that sets given property.
name -- Name of the atribute this property must simulate. Used as key in content dict by default.
starting_value -- Starting value of given property.

Keyword args:
tag_name -- The tag name stored in conted dict as a key if different to name.


def setup_prefix_logging(logdir):

    if not os.path.exists(logdir):
        os.mkdir(logdir)

    file_handler = logging.FileHandler(
        filename=os.path.join(logdir, 'lago.log'),
    )
    file_formatter = get_default_log_formatter()
    file_handler.setFormatter(file_formatter)
    logging.root.addHandler(file_handler)
    hide_paramiko_logs()
    hide_stevedore_logs()

Sets up a file logger that will create a log in the given logdir (usually a
lago prefix)

Args:
logdir (str): path to create the log into, will be created if it does
not exist

Returns:
None


def query(
        self,
        url: Union[str, methods],
        data: Optional[MutableMapping] = None,
        headers: Optional[MutableMapping] = None,
        as_json: Optional[bool] = None,
    ) -> dict:

        url, body, headers = sansio.prepare_request(
            url=url,
            data=data,
            headers=headers,
            global_headers=self._headers,
            token=self._token,
        )
        return self._make_query(url, body, headers)

Query the slack API

When using :class:`slack.methods` the request is made `as_json` if available

Args:
url: :class:`slack.methods` or url string
data: JSON encodable MutableMapping
headers: Custom headers
as_json: Post JSON to the slack API
Returns:
dictionary of slack API response data


async def msetup(self, text_channel):
    if self.mready:
        logger.warning('Attempt to init music when already initialised')
        return
    if (self.state != 'starting'):
        logger.error("Attempt to init from wrong state ('{}'), must be 'starting'.".format(self.state))
        return
    self.logger.debug('Setting up gui')
    self.mchannel = text_channel
    self.new_embed_ui()
    (await self.embed.send())
    (await self.embed.usend())
    (await self.add_reactions())
    self.mready = True

Creates the gui

Args:
text_channel (discord.Channel): The channel for the embed ui to run in


async def on_message(message):

    server = message.server
    author = message.author
    channel = message.channel
    content = message.content

    data = datatools.get_data()

    if not data["discord"]["servers"][server.id][_data.modulename]["activated"]:
        return

    if server is not None and author != channel.server.me:

        prefix = data["discord"]["servers"][server.id]["prefix"]
        if content.startswith(prefix):

            package = content.split(" ")
            command = package[0][len(prefix):]
            args = package[1:]

            alias_steam = ["steam", "pc"]
            alias_ps = ["ps", "psn", "playstation", "ps4", "playstation 4"]
            alias_xbox = ["xbox", "xb", "xb1", "xbone", "xbox one", "xbox one"]

            platform = "steam"
            if len(args) > 0:
                player_name = args[0]
            else:
                return

            if len(args) > 1:
                platform = ' '.join(args[1:]).lower()

            if platform in alias_steam:
                platform = "steam"
            elif platform in alias_ps:
                platform = "ps"
            elif platform in alias_xbox:
                platform = "xbox"

            if command == 'rlstats':
                await client.send_typing(channel)

                success, rldata = api_rocketleaguestats.check_rank(player_name, platform)

                if success:
                    embed = ui_embed.success(channel, rldata[0], rldata[1], rldata[2], rldata[3])
                else:
                    embed = ui_embed.fail_api(channel)

                await embed.send()

The on_message event handler for this module

Args:
message (discord.Message): Input message


def Matches(self, registry_key, search_depth):
    if (self._key_path_segments is None):
        key_path_match = None
    else:
        key_path_match = self._CheckKeyPath(registry_key, search_depth)
        if (not key_path_match):
            return (False, key_path_match)
        if (search_depth != self._number_of_key_path_segments):
            return (False, key_path_match)
    return (True, key_path_match)

Determines if the Windows Registry key matches the find specification.

Args:
registry_key (WinRegistryKey): Windows Registry key.
search_depth (int): number of key path segments to compare.

Returns:
tuple: contains:

bool: True if the Windows Registry key matches the find specification,
False otherwise.
bool: True if the key path matches, False if not or None if no key path
specified.


def get_data_dirs(__pkg: str) -> List[str]:
    dirs = [user_data(__pkg)]
    dirs.extend((path.expanduser(path.sep.join([d, __pkg])) for d in getenv('XDG_DATA_DIRS', '/usr/local/share/:/usr/share/').split(':')))
    return [d for d in dirs if path.isdir(d)]

Return all data directories for given package.

Args:
__pkg: Package name


def _init_pfor(self, parent_pfor, indices, cond_stacked, inputs, inputs_stacked):
    num_outputs = len(self._outputs)
    assert len(inputs) == len(self._enters)
    assert len(inputs_stacked) == len(self._enters)
    loop_var = parent_pfor.loop_var
    loop_len = array_ops.size(indices)
    pfor = PFor(loop_var, loop_len, pfor_ops=self._pfor_ops, all_indices=indices, all_indices_partitioned=cond_stacked, fallback_to_while_loop=self._fallback_to_while_loop, pfor_config=self._pfor_config)
    for enter in self._direct_enters:
        enter_input = enter.op.inputs[0]
        converted_enter, stacked, is_sparse_stacked = parent_pfor._convert_helper(enter_input)
        assert not stacked and (not is_sparse_stacked), (enter, converted_enter)
        pfor._add_conversion(enter, wrap(converted_enter, False))
    for enter, inp, stacked in zip(self._enters, inputs, inputs_stacked):
        pfor._add_conversion(enter, wrap(inp, stacked))
    for i in range(num_outputs):
        wrapped_inp = wrap(inputs[i], inputs_stacked[i])
        merge = self._enter_merges[i]
        pfor._add_conversion(merge.outputs[0], wrapped_inp)
        pfor._add_conversion(merge.outputs[1], wrap(constant_op.constant(-1.0), False))
        switch = self._exit_switches[i]
        pfor._add_conversion(switch.outputs[1], wrapped_inp)
    return pfor

Create a PFor object for converting parts of the while_loop.

Args:
parent_pfor: PFor object being used for converting the while_loop.
indices: int32 Tensor of ids for the iterations that are still active
(i.e. did not exit the while_loop).
cond_stacked: True if the while_loop condition is stacked.
inputs: list of input Tensors corresponding 1-to-1 with self._enters. Note
that these Tensors are a subset of the loop variables for the generated
while_loop.
inputs_stacked: List of booleans corresponding 1-to-1 with `inputs`,
indicating if the value is stacked or not.

Returns:
A PFor instance. The instance is initialized by adding conversion mappings
of nodes that will be external to the conversion that the returned
instance will be used for. e.g. Enter nodes as well as Merge and Switch
outputs are mapped to converted values.


def stop_on_exception(self):
    return self._coord.stop_on_exception()

Context handler to stop the supervisor when an exception is raised.

See `Coordinator.stop_on_exception()`.

Returns:
A context handler.


def observations(self, main_type, sub_type, unique_id, owner=None, params=None):

        params = params or {}

        if owner:
            params['owner'] = owner

        if not sub_type:
            url = '/v2/{}/{}/observations'.format(main_type, unique_id)
        else:
            url = '/v2/{}/{}/{}/observations'.format(type, sub_type, unique_id)

        return self.tcex.session.get(url, json=params)

Args:
main_type:
sub_type:
unique_id:
owner:
params:

Return:


def _pick_unused_port_without_server():
    rng = random.Random()
    for _ in range(10):
        port = int(rng.randrange(15000, 25000))
        if is_port_free(port):
            _random_ports.add(port)
            return port
    for _ in range(10):
        port = bind(0, _PROTOS[0][0], _PROTOS[0][1])
        if (port and bind(port, _PROTOS[1][0], _PROTOS[1][1])):
            _random_ports.add(port)
            return port
    raise NoFreePortFoundError()

Pick an available network port without the help of a port server.

This code ensures that the port is available on both TCP and UDP.

This function is an implementation detail of PickUnusedPort(), and
should not be called by code outside of this module.

Returns:
A port number that is unused on both TCP and UDP.

Raises:
NoFreePortFoundError: No free port could be found.


def send_log_message(self, message: LogMessage) -> None:
    print(message)

Prints the log message to be captured by cloud logging.

Args:
* message: LogMessage dictionary

Returns:
* None


def write_worksheets(workbook, data_list, result_info_key, identifier_keys):
    worksheet_keys = get_worksheet_keys(data_list[0], result_info_key)
    for key in worksheet_keys:
        title = key.split('/')[1]
        title = utilities.convert_snake_to_title_case(title)
        title = KEY_TO_WORKSHEET_MAP.get(title, title)
        if (key == 'property/nod'):
            create_property_nod_worksheets(workbook, data_list, result_info_key, identifier_keys)
        else:
            worksheet = workbook.create_sheet(title=title[:31])
            processed_data = process_data(key, data_list, result_info_key, identifier_keys)
            write_data(worksheet, processed_data)
    workbook.remove_sheet(workbook.active)

Writes rest of the worksheets to workbook.

Args:
workbook: workbook to write into
data_list: Analytics API data as a list of dicts
result_info_key: the key in api_data dicts that contains the data results
identifier_keys: the list of keys used as requested identifiers
(address, zipcode, block_id, etc)


def _ParseFileData(self, knowledge_base, file_object):

    text_file_object = dfvfs_text_file.TextFile(file_object, encoding='utf-8')

    system_product = text_file_object.readline()

    if system_product.startswith('Debian GNU/Linux '):
      system_product, _, _ = system_product.partition('\\')
      system_product = system_product.rstrip()

    else:
      system_product = None

    if not knowledge_base.GetValue('operating_system_product'):
      if system_product:
        knowledge_base.SetValue('operating_system_product', system_product)

Parses file content (data) for system product preprocessing attribute.

Args:
knowledge_base (KnowledgeBase): to fill with preprocessing information.
file_object (dfvfs.FileIO): file-like object that contains the artifact
value data.

Raises:
errors.PreProcessFail: if the preprocessing fails.


def read_binary_array(self, key, b64decode=True, decode=False):
    data = None
    if (key is not None):
        data = self.db.read(key.strip())
        if (data is not None):
            data_decoded = []
            for d in json.loads(data, object_pairs_hook=OrderedDict):
                if b64decode:
                    dd = base64.b64decode(d)
                    if decode:
                        try:
                            dd = dd.decode('utf-8')
                        except UnicodeDecodeError:
                            dd = dd.decode('latin-1')
                    data_decoded.append(dd)
                else:
                    data_decoded.append(d)
            data = data_decoded
    else:
        self.tcex.log.warning(u'The key field was None.')
    return data

Read method of CRUD operation for binary array data.

Args:
key (string): The variable to read from the DB.
b64decode (bool): If true the data will be base64 decoded.
decode (bool): If true the data will be decoded to a String.

Returns:
(list): Results retrieved from DB.


def js_adaptor(buffer):

    buffer = re.sub('true', 'True', buffer)
    buffer = re.sub('false', 'False', buffer)
    buffer = re.sub('none', 'None', buffer)
    buffer = re.sub('NaN', '"NaN"', buffer)
    return buffer

convert javascript objects like true, none, NaN etc. to
quoted word.

Arguments:
buffer: string to be converted

Returns:
string after conversion


def extraterrestrial_horizontal_radiation(self, value=9999.0):

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

        self._extraterrestrial_horizontal_radiation = value

Corresponds to IDD Field `extraterrestrial_horizontal_radiation`

Args:
value (float): value for IDD Field `extraterrestrial_horizontal_radiation`
Unit: Wh/m2
value >= 0.0
Missing value: 9999.0
if `value` is None it will not be checked against the
specification and is assumed to be a missing value

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


def get_elements_iterable(self, make_copy: bool=False) -> Iterable[WindowedValue]:
    if not self._stacked:
        elements = cast('List[WindowedValue]', self._elements)
        if self._committed and (not make_copy):
            return elements
        return list(elements)

    def iterable_stacked_or_elements(elements):
        for e in elements:
            if isinstance(e, _Bundle._StackedWindowedValues):
                for w in e.windowed_values():
                    yield w
            else:
                yield e
    if self._committed and (not make_copy):
        return iterable_stacked_or_elements(self._elements)
    return [e for e in iterable_stacked_or_elements(self._elements)]

Returns iterable elements.

Args:
make_copy: whether to force returning copy or yielded iterable.

Returns:
unstacked elements,
in the form of iterable if committed and make_copy is not True,
or as a list of copied WindowedValues.


def find_labels(model_class):
    model_name = model_class.__name__
    framework = infer_framework(model_class)
    if framework == 'tf':
        signature = inspect.signature(model_class.call)
    elif framework == 'pt':
        signature = inspect.signature(model_class.forward)
    else:
        signature = inspect.signature(model_class.__call__)
    if 'QuestionAnswering' in model_name:
        return [p for p in signature.parameters if 'label' in p or p in ('start_positions', 'end_positions')]
    else:
        return [p for p in signature.parameters if 'label' in p]

Find the labels used by a given model.

Args:
model_class (`type`): The class of the model.


def __call__(self, shape, dtype=dtypes.float32, **kwargs):
    self._validate_kwargs(kwargs)
    dtype = _assert_float_dtype(dtype)
    if _PARTITION_SHAPE in kwargs:
        shape = kwargs[_PARTITION_SHAPE]
    return self._random_generator.truncated_normal(shape, self.mean, self.stddev, dtype)

Returns a tensor object initialized as specified by the initializer.

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

Raises:
ValueError: If the dtype is not floating point


def get_form_energy(self, entry):

        c = entry.composition
        return entry.energy - sum([c[el] * self.el_refs[el].energy_per_atom
                                   for el in c.elements])

Returns the formation energy for an entry (NOT normalized) from the
elemental references.

Args:
entry: A PDEntry-like object.

Returns:
Formation energy from the elemental references.


def files_upload(self, *, file: Union[(str, IOBase)]=None, content: str=None, **kwargs) -> SlackResponse:
    if ((file is None) and (content is None)):
        raise e.SlackRequestError('The file or content argument must be specified.')
    if ((file is not None) and (content is not None)):
        raise e.SlackRequestError('You cannot specify both the file and the content argument.')
    if file:
        return self.api_call('files.upload', files={'file': file}, data=kwargs)
    elif content:
        data = kwargs.copy()
        data.update({'content': content})
        return self.api_call('files.upload', data=data)

Uploads or creates a file.

Args:
file (str): Supply a file path.
when you'd like to upload a specific file. e.g. 'dramacat.gif'
content (str): Supply content when you'd like to create an
editable text file containing the specified text. e.g. 'launch plan'
Raises:
SlackRequestError: If niether or both the `file` and `content` args are specified.


def _build(self, inputs, is_training=True, dropout_keep_prob=0.5):
    self._input_shape = tuple(inputs.get_shape().as_list())
    net = inputs
    final_index = (self._num_layers - 1)
    for layer_id in xrange(self._num_layers):
        net = self._layers[layer_id](net)
        if ((final_index != layer_id) or self._activate_final):
            if self._use_dropout:
                keep_prob = utils.smart_cond(is_training, true_fn=(lambda : dropout_keep_prob), false_fn=(lambda : tf.constant(1.0)))
                net = tf.nn.dropout(net, keep_prob=keep_prob)
            net = self._activation(net)
    return net

Assembles the `MLP` and connects it to the graph.

Args:
inputs: A 2D Tensor of size `[batch_size, input_size]`.
is_training: A bool or tf.Bool Tensor. Indicates whether we are
currently training. Defaults to `True`.
dropout_keep_prob: The probability that each element is kept when
both `use_dropout` and `is_training` are True. Defaults to 0.5.
Returns:
A 2D Tensor of size `[batch_size, output_sizes[-1]]`.


def insert(cls, cur, table: str, values: dict):

        keys = cls._COMMA.join(values.keys())
        value_place_holder = cls._PLACEHOLDER * len(values)
        query = cls._insert_string.format(table, keys, value_place_holder[:-1])
        yield from cur.execute(query, tuple(values.values()))
        return (yield from cur.fetchone())

Creates an insert statement with only chosen fields

Args:
table: a string indicating the name of the table
values: a dict of fields and values to be inserted

Returns:
A 'Record' object with table columns as properties


def _get_corrupted_example(self, x):

    corruption_type = self.builder_config.corruption_type
    severity = self.builder_config.severity

    return {
        'gaussian_noise': corruptions.gaussian_noise,
        'shot_noise': corruptions.shot_noise,
        'impulse_noise': corruptions.impulse_noise,
        'defocus_blur': corruptions.defocus_blur,
        'frosted_glass_blur': corruptions.frosted_glass_blur,
        'zoom_blur': corruptions.zoom_blur,
        'fog': corruptions.fog,
        'brightness': corruptions.brightness,
        'contrast': corruptions.contrast,
        'elastic': corruptions.elastic,
        'pixelate': corruptions.pixelate,
        'jpeg_compression': corruptions.jpeg_compression,
    }[corruption_type](x, severity)

Return corrupted images.

Args:
x: numpy array, uncorrupted image.

Returns:
numpy array, corrupted images.


def mme_nodes(mme_base_url, token):

    nodes = []
    if not mme_base_url or not token:
        return nodes
    url = ''.join([mme_base_url, '/nodes'])
    nodes = matchmaker_request(url=url, token=token, method='GET')
    LOG.info('Matchmaker has the following connected nodes:{}'.format(nodes))
    return nodes

Return the available MatchMaker nodes

Args:
mme_base_url(str): base URL of MME service
token(str): MME server authorization token

Returns:
nodes(list): a list of node disctionaries


def get_vcs_root():
    for vcs in (git, hg):
        repo_root = vcs.repository_root()
        if repo_root:
            return (vcs, repo_root)
    return (None, None)

Returns the vcs module and the root of the repo.

Returns:
A tuple containing the vcs module to use (git, hg) and the root of the
repository. If no repository exisits then (None, None) is returned.


class GraniteMoeSharedMLP(nn.Module):

    def __init__(self, config: GraniteMoeSharedConfig):
        super(GraniteMoeSharedMLP, self).__init__()
        self.input_size = config.hidden_size
        self.hidden_size = config.shared_intermediate_size
        self.activation = ACT2FN[config.hidden_act]
        self.input_linear = nn.Linear(self.input_size, self.hidden_size * 2, bias=False)
        self.output_linear = nn.Linear(self.hidden_size, self.input_size, bias=False)

    def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
        hidden_states = self.input_linear(hidden_states)
        chunked_hidden_states = hidden_states.chunk(2, dim=-1)
        hidden_states = self.activation(chunked_hidden_states[0]) * chunked_hidden_states[1]
        hidden_states = self.output_linear(hidden_states)
        return hidden_states

MLP layer for shared experts

Args:
config:
Configuration object with model hyperparameters.