def workspace_from_url(self, mets_url, dst_dir=None, clobber_mets=False, mets_basename=None, download=False, baseurl=None):
    if (dst_dir and (not dst_dir.startswith('/'))):
        dst_dir = abspath(dst_dir)
    if (mets_url is None):
        if (baseurl is None):
            raise Exception('Must pass mets_url and/or baseurl to workspace_from_url')
        else:
            mets_url = ('file:
    if (baseurl is None):
        baseurl = mets_url.rsplit('/', 1)[0]
    log.debug("workspace_from_url\nmets_url='%s'\nbaseurl='%s'\ndst_dir='%s'", mets_url, baseurl, dst_dir)
    if (':
        mets_url = ('file:
    if (dst_dir is None):
        if mets_url.startswith('file:
            dst_dir = dirname(mets_url[len('file:
        else:
            dst_dir = tempfile.mkdtemp(prefix=TMP_PREFIX)
            log.debug("Creating workspace '%s' for METS @ <%s>", dst_dir, mets_url)
    if (mets_basename is None):
        mets_basename = mets_url.rsplit('/', 1)[(- 1)].split('?')[0].split('
    dst_mets = join(dst_dir, mets_basename)
    log.debug("Copying mets url '%s' to '%s'", mets_url, dst_mets)
    if (('file:
        log.debug('Target and source mets are identical')
    elif (exists(dst_mets) and (not clobber_mets)):
        raise Exception(("File '%s' already exists but clobber_mets is false" % dst_mets))
    else:
        self.download_to_directory(dst_dir, mets_url, basename=mets_basename)
    workspace = Workspace(self, dst_dir, mets_basename=mets_basename, baseurl=baseurl)
    if download:
        for f in workspace.mets.find_files():
            workspace.download_file(f)
    return workspace

Create a workspace from a METS by URL.

Sets the mets.xml file

Arguments:
mets_url (string): Source mets URL
dst_dir (string, None): Target directory for the workspace
clobber_mets (boolean, False): Whether to overwrite existing mets.xml. By default existing mets.xml will raise an exception.
download (boolean, False): Whether to download all the files
baseurl (string, None): Base URL for resolving relative file locations

Returns:
Workspace


def message_index(index_url):
    idx = csv.reader(urllib2.urlopen(index_url), delimiter=':')
    messages = []
    for line in idx:
        messages.append(line)
    return messages

get message index of components for urllib2.

Args:
url(string):

Returns:
list: messages


def batch_predict_async(training_dir, prediction_input_file, output_dir, mode, batch_size=16, shard_files=True, output_format='csv', cloud=False):
    import google.datalab.utils as du
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        if cloud:
            runner_results = cloud_batch_predict(training_dir, prediction_input_file, output_dir, mode, batch_size, shard_files, output_format)
            job = du.DataflowJob(runner_results)
        else:
            runner_results = local_batch_predict(training_dir, prediction_input_file, output_dir, mode, batch_size, shard_files, output_format)
            job = du.LambdaJob((lambda : runner_results.wait_until_finish()), job_id=None)
    return job

Local and cloud batch prediction.

Args:
training_dir: The output folder of training.
prediction_input_file: csv file pattern to a file. File must be on GCS if
running cloud prediction
output_dir: output location to save the results. Must be a GSC path if
running cloud prediction.
mode: 'evaluation' or 'prediction'. If 'evaluation', the input data must
contain a target column. If 'prediction', the input data must not
contain a target column.
batch_size: Int. How many instances to run in memory at once. Larger values
mean better performace but more memeory consumed.
shard_files: If False, the output files are not shardded.
output_format: csv or json. Json file are json-newlined.
cloud: If ture, does cloud batch prediction. If False, runs batch prediction
locally.

Returns:
A google.datalab.utils.Job object that can be used to query state from or wait.


def get_q2(self, thetas=None, phis=None):
    if ((thetas is not None) and (phis is not None)):
        self.compute_trigonometric_terms(thetas, phis)
    nnn = len(self._pow_sin_t[1])
    nnn_range = range(nnn)
    sqrt_15_2pi = sqrt((15.0 / (2.0 * pi)))
    sqrt_5_pi = sqrt((5.0 / pi))
    pre_y_2_2 = [((0.25 * sqrt_15_2pi) * val) for val in self._pow_sin_t[2]]
    pre_y_2_1 = [(((0.5 * sqrt_15_2pi) * val[0]) * val[1]) for val in zip(self._pow_sin_t[1], self._pow_cos_t[1])]
    acc = 0.0
    real = imag = 0.0
    for i in nnn_range:
        real += (pre_y_2_2[i] * self._cos_n_p[2][i])
        imag -= (pre_y_2_2[i] * self._sin_n_p[2][i])
    acc += ((real * real) + (imag * imag))
    real = imag = 0.0
    for i in nnn_range:
        real += (pre_y_2_1[i] * self._cos_n_p[1][i])
        imag -= (pre_y_2_1[i] * self._sin_n_p[1][i])
    acc += ((real * real) + (imag * imag))
    real = imag = 0.0
    for i in nnn_range:
        real += ((0.25 * sqrt_5_pi) * ((3.0 * self._pow_cos_t[2][i]) - 1.0))
    acc += (real * real)
    real = imag = 0.0
    for i in nnn_range:
        real -= (pre_y_2_1[i] * self._cos_n_p[1][i])
        imag -= (pre_y_2_1[i] * self._sin_n_p[1][i])
    acc += ((real * real) + (imag * imag))
    real = imag = 0.0
    for i in nnn_range:
        real += (pre_y_2_2[i] * self._cos_n_p[2][i])
        imag += (pre_y_2_2[i] * self._sin_n_p[2][i])
    acc += ((real * real) + (imag * imag))
    q2 = sqrt((((4.0 * pi) * acc) / (5.0 * float((nnn * nnn)))))
    return q2

Calculates the value of the bond orientational order parameter of
weight l=2.  If the function is called with non-empty lists of
polar and azimuthal angles the corresponding trigonometric terms
are computed afresh.  Otherwise, it is expected that the
compute_trigonometric_terms function has been just called.

Args:
thetas ([float]): polar angles of all neighbors in radians.
phis ([float]): azimuth angles of all neighbors in radians.

Returns:
float: bond orientational order parameter of weight l=2
corresponding to the input angles thetas and phis.


def cast_vdata(vdata=None, vtype='REG_SZ'):
    registry = Registry()
    vtype_value = registry.vtype[vtype]
    if (vtype_value in [win32con.REG_SZ, win32con.REG_EXPAND_SZ]):
        return _to_unicode(vdata)
    elif (vtype_value == win32con.REG_BINARY):
        if isinstance(vdata, six.text_type):
            return vdata.encode('utf-8')
        return vdata
    elif (vtype_value == win32con.REG_MULTI_SZ):
        return [_to_unicode(i) for i in vdata]
    elif (vtype_value == win32con.REG_QWORD):
        return (vdata if six.PY3 else long(vdata))
    else:
        return int(vdata)

Cast the ``vdata` value to the appropriate data type for the registry type
specified in ``vtype``

Args:

vdata (str, int, list, bytes): The data to cast

vtype (str):
The type of data to be written to the registry. Must be one of the
following:

- REG_BINARY
- REG_DWORD
- REG_EXPAND_SZ
- REG_MULTI_SZ
- REG_QWORD
- REG_SZ

Returns:
The vdata cast to the appropriate type. Will be unicode string, binary,
list of unicode strings, or int

Usage:

.. code-block:: python

import salt.utils.win_reg
winreg.cast_vdata(vdata='This is the string', vtype='REG_SZ')


def _get_resource(self, label: str, source: dict, resource_type: str):

        try:
            return source[label]
        except KeyError:
            raise ValueError("Cannot find {0} with label '{1}'.\nExisting {0} labels: {2}".format(
                resource_type, label, list(source.keys())))

Generic resoure fetcher handling errors.

Args:
label (str): The label to fetch
source (dict): The dictionary to look up the label
resource_type str: The display name of the resource type (used in errors)


def __init__(
      self, resolver_context, file_system, path_spec, is_root=False,
      is_virtual=False):

    compressed_stream = resolver.Resolver.OpenFileObject(
        path_spec, resolver_context=resolver_context)
    if not compressed_stream:
      raise errors.BackEndError(
          'Unable to open compressed stream: {0:s}.'.format(
              self.path_spec.comparable))

    super(CompressedStreamFileEntry, self).__init__(
        resolver_context, file_system, path_spec, is_root=is_root,
        is_virtual=is_virtual)
    self._compressed_stream = compressed_stream
    self.entry_type = definitions.FILE_ENTRY_TYPE_FILE

Initializes a file entry.

Args:
resolver_context (Context): resolver context.
file_system (FileSystem): file system.
path_spec (PathSpec): path specification.
is_root (Optional[bool]): True if the file entry is the root file entry
of the corresponding file system.
is_virtual (Optional[bool]): True if the file entry is a virtual file

Raises:
BackEndError: when the compressed stream is missing.


def get_aws_session(account):
    from cloud_inquisitor.config import dbconfig
    from cloud_inquisitor.plugins.types.accounts import AWSAccount
    if (not isinstance(account, AWSAccount)):
        raise InquisitorError('Non AWSAccount passed to get_aws_session, got {}'.format(account.__class__.__name__))
    session = get_local_aws_session()
    if (session.get_credentials().method in ['iam-role', 'env', 'explicit']):
        sts = session.client('sts')
    else:
        temp_sts = session.client('sts')
        audit_sts_role = temp_sts.assume_role(RoleArn=app_config.aws_api.instance_role_arn, RoleSessionName='inquisitor')
        sts = boto3.session.Session(audit_sts_role['Credentials']['AccessKeyId'], audit_sts_role['Credentials']['SecretAccessKey'], audit_sts_role['Credentials']['SessionToken']).client('sts')
    role = sts.assume_role(RoleArn='arn:aws:iam::{}:role/{}'.format(account.account_number, dbconfig.get('role_name', default='cinq_role')), RoleSessionName='inquisitor')
    sess = boto3.session.Session(role['Credentials']['AccessKeyId'], role['Credentials']['SecretAccessKey'], role['Credentials']['SessionToken'])
    return sess

Function to return a boto3 Session based on the account passed in the first argument.

Args:
account (:obj:`Account`): Account to create the session object for

Returns:
:obj:`boto3:boto3.session.Session`


def join(self):
    c_api.TF_ServerJoin(self._server)

Blocks until the server has shut down.

This method currently blocks forever.

Raises:
tf.errors.OpError: Or one of its subclasses if an error occurs while
joining the TensorFlow server.


def response_list(data, key):
    if (key not in data):
        return None
    if isinstance(data[key], list):
        return data[key]
    else:
        return [data[key]]

Obtain the relevant response data in a list.

If the response does not already contain the result in a list, a new one
will be created to ease iteration in the parser methods.

Args:
data (dict): API response.
key (str): Attribute of the response that contains the result values.

Returns:
List of response items (usually dict) or None if the key is not present.


def initialize_plugs(self, plug_types=None):

    types = plug_types if plug_types is not None else self._plug_types
    for plug_type in types:

      plug_logger = self.logger.getChild(plug_type.__name__)
      if plug_type in self._plugs_by_type:
        continue
      try:
        if not issubclass(plug_type, BasePlug):
          raise InvalidPlugError(
              'Plug type "%s" is not an instance of BasePlug' % plug_type)
        if plug_type.logger != _LOG:

          raise InvalidPlugError(
              'Do not override "logger" in your plugs.', plug_type)

        plug_type.logger = plug_logger
        try:
          plug_instance = plug_type()
        finally:

          plug_type.logger = _LOG

        if plug_instance.logger != _LOG:
          raise InvalidPlugError(
              'Do not set "self.logger" in __init__ in your plugs', plug_type)
        else:

          plug_instance.logger = plug_logger
      except Exception:
        plug_logger.exception('Exception instantiating plug type %s', plug_type)
        self.tear_down_plugs()
        raise
      self.update_plug(plug_type, plug_instance)

Instantiate required plugs.

Instantiates plug types and saves the instances in self._plugs_by_type for
use in provide_plugs().

Args:
plug_types: Plug types may be specified here rather than passed
into the constructor (this is used primarily for unit testing
phases).


def AddFiles(self, hash_id_metadatas):

    for hash_id, metadata in iteritems(hash_id_metadatas):
      self.AddFile(hash_id, metadata)

Adds multiple files to the file store.

Args:
hash_id_metadatas: A dictionary mapping hash ids to file metadata (a tuple
of hash client path and blob references).


def create_cloudwatch_event(app_name, env, region, rules):
    session = boto3.Session(profile_name=env, region_name=region)
    cloudwatch_client = session.client('events')
    rule_name = rules.get('rule_name')
    schedule = rules.get('schedule')
    rule_description = rules.get('rule_description')
    json_input = rules.get('json_input', {})
    if (schedule is None):
        LOG.critical('Schedule is required and no schedule is defined!')
        raise InvalidEventConfiguration('Schedule is required and no schedule is defined!')
    if (rule_name is None):
        LOG.critical('Rule name is required and no rule_name is defined!')
        raise InvalidEventConfiguration('Rule name is required and no rule_name is defined!')
    else:
        LOG.info('%s and %s', app_name, rule_name)
        rule_name = '{}_{}'.format(app_name, rule_name.replace(' ', '_'))
    if (rule_description is None):
        rule_description = '{} - {}'.format(app_name, rule_name)
    lambda_arn = get_lambda_arn(app=app_name, account=env, region=region)
    account_id = get_env_credential(env=env)['accountId']
    principal = 'events.amazonaws.com'
    statement_id = '{}_cloudwatch_{}'.format(app_name, rule_name)
    source_arn = 'arn:aws:events:{}:{}:rule/{}'.format(region, account_id, rule_name)
    add_lambda_permissions(function=lambda_arn, statement_id=statement_id, action='lambda:InvokeFunction', principal=principal, source_arn=source_arn, env=env, region=region)
    cloudwatch_client.put_rule(Name=rule_name, ScheduleExpression=schedule, State='ENABLED', Description=rule_description)
    targets = []
    json_payload = '{}'.format(json.dumps(json_input))
    target = {'Id': app_name, 'Arn': lambda_arn, 'Input': json_payload}
    targets.append(target)
    put_targets_response = cloudwatch_client.put_targets(Rule=rule_name, Targets=targets)
    LOG.debug('Cloudwatch put targets response: %s', put_targets_response)
    LOG.info('Created Cloudwatch event "%s" with schedule: %s', rule_name, schedule)

Create cloudwatch event for lambda from rules.

Args:
app_name (str): name of the lambda function
env (str): Environment/Account for lambda function
region (str): AWS region of the lambda function
rules (dict): Trigger rules from the settings


def __init__(self, _args):

        super(TcExRun, self).__init__(_args)

        self._signal_handler_init()
        self._config = None
        self._profile = {}
        self._staging_data = None
        self.container = None
        self.reports = Reports()
        self.tcex = None
        self.docker_image = 'tcintegrations/tci-dev:latest'

        self.log = self._logger()

        self._clear_redis_tracker = []
        self.json_report = {}
        self.max_diff = 10
        self.sleep = 0

        self.display_name = None
        self.program_main = None
        self.program_version = None
        self.runtime_level = None

        self.shell = False

Initialize Class properties.

Args:
_args (namespace): The argparser args Namespace.


def reschedule(cls,
                 mapreduce_state,
                 mapreduce_spec,
                 serial_id,
                 queue_name=None):

    task_name = ControllerCallbackHandler.get_task_name(
        mapreduce_spec, serial_id)
    task_params = ControllerCallbackHandler.controller_parameters(
        mapreduce_spec, serial_id)
    if not queue_name:
      queue_name = os.environ.get("HTTP_X_APPENGINE_QUEUENAME", "default")

    controller_callback_task = model.HugeTask(
        url=(mapreduce_spec.params["base_path"] + "/controller_callback/" +
             mapreduce_spec.mapreduce_id),
        name=task_name, params=task_params,
        countdown=parameters.config._CONTROLLER_PERIOD_SEC,
        parent=mapreduce_state,
        headers=util._get_task_headers(mapreduce_spec.mapreduce_id))

    if not _run_task_hook(mapreduce_spec.get_hooks(),
                          "enqueue_controller_task",
                          controller_callback_task,
                          queue_name):
      try:
        controller_callback_task.add(queue_name)
      except (taskqueue.TombstonedTaskError,
              taskqueue.TaskAlreadyExistsError), e:
        logging.warning("Task %r with params %r already exists. %s: %s",
                        task_name, task_params, e.__class__, e)

Schedule new update status callback task.

Args:
mapreduce_state: mapreduce state as model.MapreduceState
mapreduce_spec: mapreduce specification as MapreduceSpec.
serial_id: id of the invocation as int.
queue_name: The queue to schedule this task on. Will use the current
queue of execution if not supplied.


def apply(self, func, *args, **kwargs):
    ret = func(self._t, *args, **kwargs)
    return LinearWrap(ret)

Apply a function on the wrapped tensor.

Returns:
LinearWrap: ``LinearWrap(func(self.tensor(), *args, **kwargs))``.


def collect_trajectories(env, policy_fun, num_trajectories=1, policy='greedy', max_timestep=None, epsilon=0.1):
    trajectories = []
    for t in range(num_trajectories):
        t_start = time.time()
        rewards = []
        actions = []
        done = False
        observation = env.reset()
        observation_history = observation[(np.newaxis, np.newaxis, :)]
        ts = 0
        while ((not done) and ((not max_timestep) or (observation_history.shape[1] < max_timestep))):
            ts_start = time.time()
            predictions = policy_fun(observation_history)
            predictions = np.squeeze(predictions, axis=0)[(- 1)]
            action = None
            if (policy == 'greedy'):
                action = np.argmax(predictions)
            elif (policy == 'epsilon-greedy'):
                if (onp.random.random() < epsilon):
                    action = onp.random.randint(0, high=len(predictions))
                else:
                    action = np.argmax(predictions)
            elif (policy == 'categorical-sampling'):
                predictions = np.exp(predictions)
                action = onp.argwhere((onp.random.multinomial(1, predictions) == 1))
            else:
                raise ValueError(('Unknown policy: %s' % policy))
            try:
                action = int(action)
            except TypeError as err:
                logging.error('Cannot convert action into an integer: [%s]', err)
                logging.error('action.shape: [%s]', action.shape)
                logging.error('action: [%s]', action)
                logging.error('predictions.shape: [%s]', predictions.shape)
                logging.error('predictions: [%s]', predictions)
                logging.error('observation_history: [%s]', observation_history)
                raise err
            (observation, reward, done, _) = env.step(action)
            observation_history = np.concatenate([observation_history, observation[(np.newaxis, np.newaxis, :)]], axis=1)
            rewards.append(reward)
            actions.append(action)
            ts += 1
            logging.vlog(2, '  Collected time-step[ %5d] of trajectory[ %5d] in [%0.2f] msec.', ts, t, get_time(ts_start))
        logging.vlog(2, ' Collected trajectory[ %5d] in [%0.2f] msec.', t, get_time(t_start))
        assert (done or (max_timestep and (max_timestep >= observation_history.shape[1])))
        observation_history = np.squeeze(observation_history, axis=0)
        trajectories.append((observation_history, np.stack(actions), np.stack(rewards)))
    return trajectories

Collect trajectories with the given policy net and behaviour.

Args:
env: A gym env interface, for now this is not-batched.
policy_fun: observations(B,T+1) -> log-probabs(B,T+1, A) callable.
num_trajectories: int, number of trajectories.
policy: string, "greedy", "epsilon-greedy", or "categorical-sampling" i.e.
how to use the policy_fun to return an action.
max_timestep: int or None, the index of the maximum time-step at which we
return the trajectory, None for ending a trajectory only when env
returns done.
epsilon: float, the epsilon for `epsilon-greedy` policy.

Returns:
trajectory: list of (observation, action, reward) tuples, where each element
`i` is a tuple of numpy arrays with shapes as follows:
observation[i] = (B, T_i + 1)
action[i] = (B, T_i)
reward[i] = (B, T_i)


def __init__(self, resolver_context):

    super(APFSFileSystem, self).__init__(resolver_context)
    self._fsapfs_volume = None

Initializes an APFS file system.

Args:
resolver_context (Context): resolver context.


def validate_signature(self, signature, data, encoding='utf8'):
    if isinstance(data, string_types):
        data = bytearray(data, encoding)
    if isinstance(signature, string_types):
        signature = bytearray(signature, encoding)
    secret_key = bytearray(self.secret_key, 'utf8')
    hashed = hmac.new(secret_key, data, sha1)
    encoded = b64encode(hashed.digest())
    return (encoded.strip() == signature.strip())

Validate the signature for the provided data.

Args:
signature (str or bytes or bytearray): Signature that was provided
for the request.
data (str or bytes or bytearray): Data string to validate against
the signature.
encoding (str, optional): If a string was provided for ``data`` or
``signature``, this is the character encoding.

Returns:
bool: Whether the signature is valid for the provided data.


def add_nodes(self, root_id, current_node, indent=1):

        if not current_node.children:
            return

        config.LOGGER.info("({count} of {total} uploaded) {indent}Processing {title} ({kind})".format(
            count=self.node_count_dict['upload_count'],
            total=self.node_count_dict['total_count'],
            indent="   " * indent,
            title=current_node.title,
            kind=current_node.__class__.__name__)
        )

        try:
            chunks = [current_node.children[x:x+10] for x in range(0, len(current_node.children), 10)]
            for chunk in chunks:
                payload_children = []

                for child in chunk:
                    failed = [f for f in child.files if f.is_primary and (not f.filename or self.failed_uploads.get(f.filename))]
                    if any(failed):
                        if not self.failed_node_builds.get(root_id):
                            error_message = ""
                            for fail in failed:
                                reason = fail.filename + ": " + self.failed_uploads.get(fail.filename) if fail.filename else "File failed to download"
                                error_message = error_message + reason + ", "
                            self.failed_node_builds[root_id] = {'node': current_node, 'error': error_message[:-2]}
                    else:
                        payload_children.append(child.to_dict())
                payload = {
                    'root_id': root_id,
                    'content_data': payload_children
                }

                response = config.SESSION.post(config.add_nodes_url(), data=json.dumps(payload))
                if response.status_code != 200:
                    self.failed_node_builds[root_id] = {'node': current_node, 'error': response.reason}
                else:
                    response_json = json.loads(response._content.decode("utf-8"))
                    self.node_count_dict['upload_count'] += len(chunk)

                    if response_json['root_ids'].get(child.get_node_id().hex):
                        for child in chunk:
                            self.add_nodes(response_json['root_ids'].get(child.get_node_id().hex), child, indent + 1)
        except ConnectionError as ce:
            self.failed_node_builds[root_id] = {'node': current_node, 'error': ce}

add_nodes: adds processed nodes to tree
Args:
root_id (str): id of parent node on Kolibri Studio
current_node (Node): node to publish children
indent (int): level of indentation for printing
Returns: link to uploadedchannel


def run(self, dag):

        num_dag_qubits = sum([qreg.size for qreg in dag.qregs.values()])
        if num_dag_qubits > self.coupling_map.size():
            raise TranspilerError('Number of qubits greater than device.')
        best_sub = self._best_subset(num_dag_qubits)
        layout = Layout()
        map_iter = 0
        for qreg in dag.qregs.values():
            for i in range(qreg.size):
                layout[(qreg, i)] = int(best_sub[map_iter])
                map_iter += 1
        self.property_set['layout'] = layout

Pick a convenient layout depending on the best matching
qubit connectivity, and set the property `layout`.

Args:
dag (DAGCircuit): DAG to find layout for.

Raises:
TranspilerError: if dag wider than self.coupling_map


def flatten(structure):
    return tree_impl.flatten(structure)

Flattens a possibly nested structure into a list.

In the case of dict instances, the sequence consists of the values,
sorted by key to ensure deterministic behavior. However, instances of
`collections.OrderedDict` are handled differently: their sequence order is
used instead of the sorted keys. The same convention is followed in
`pack_sequence_as`. This correctly unflattens dicts and `OrderedDict` after
they have been flattened, or vice-versa.

Dictionaries with non-sortable keys are not supported.

Examples:

>>> keras.tree.flatten([[1, 2, 3], [4, [5], [[6]]]])
[1, 2, 3, 4, 5, 6]
>>> keras.tree.flatten(None)
[None]
>>> keras.tree.flatten(1)
[1]
>>> keras.tree.flatten({100: 'world!', 6: 'Hello'})
['Hello', 'world!']

Args:
structure: An arbitrarily nested structure.

Returns:
A list, the flattened version of the input `structure`.


def _read_messages_until_true(self, predicate, timeout):
    while (not predicate()):
        self._message_received.acquire()
        if self._reader_lock.acquire(False):
            try:
                self._message_received.release()
                if predicate():
                    return
                self._handle_message(self.adb_connection.read_for_stream(self, timeout))
                with self._message_received:
                    self._message_received.notify_all()
            finally:
                self._reader_lock.release()
        else:
            try:
                self._message_received.wait(timeout.remaining)
                if timeout.has_expired():
                    raise usb_exceptions.AdbTimeoutError('%s timed out reading messages.', self)
            finally:
                self._message_received.release()

Read a message from this stream and handle it.

This method tries to read a message from this stream, blocking until a
message is read.  Once read, it will handle it accordingly by calling
self._handle_message().

This is repeated as long as predicate() returns False.  There is some
locking used internally here so that we don't end up with multiple threads
blocked on a call to read_for_stream when another thread has read the
message that caused predicate() to become True.

Args:
predicate: Callable, keep reading messages until it returns true.  Note
that predicate() should not block, as doing so may cause this method to
hang beyond its timeout.
timeout: Timeout to use for this call.

Raises:
AdbStreamClosedError: If this stream is already closed.


def assert_is_compatible_with(self, other):
    if not self.is_compatible_with(other):
        raise ValueError('Dimensions %s and %s are not compatible' % (self, other))

Raises an exception if `other` is not compatible with this Dimension.

Args:
other: Another Dimension.

Raises:
ValueError: If `self` and `other` are not compatible (see
is_compatible_with).


def _IsBase64(cls, s):

    try:
      if base64.b64encode(base64.b64decode(s)).decode('utf-8') == s:
        return True
    except (TypeError, binascii.Error):
      pass
    return False

An imperfect but decent method for determining if a string is base64.

Args:
s: A string with the data to test.

Returns:
True if s is base64, else False.


def loss_l2(self, l2=0):
    if isinstance(l2, (int, float)):
        D = (l2 * torch.eye(self.d))
    else:
        D = torch.diag(torch.from_numpy(l2))
    return (torch.norm((D @ (self.mu - self.mu_init))) ** 2)

L2 loss centered around mu_init, scaled optionally per-source.

In other words, diagonal Tikhonov regularization,
||D(\mu-\mu_{init})||_2^2
where D is diagonal.

Args:
- l2: A float or np.array representing the per-source regularization
strengths to use


def get_table(e: exp.Expression) -> str:
    table = e.find(exp.Table).args['this'].args['this']
    if table in table_dataset_map:
        table = table_dataset_map[table]
    return table

Get the table name from an expression.

Args:
e (Expression): The expression containing table information.

Returns:
str: The table name.


def save_variation_for_experiment(self, experiment_id, variation_id):

    self.experiment_bucket_map.update({
      experiment_id: {
        self.VARIATION_ID_KEY: variation_id
      }
    })

Helper method to save new experiment/variation as part of the user's profile.

Args:
experiment_id: ID for experiment for which the decision is to be stored.
variation_id: ID for variation that the user saw.


def callback_handler(self):
    decorator = self

    class OAuth2Handler(webapp.RequestHandler):
        'Handler for the redirect_uri of the OAuth 2.0 dance.'

        @login_required
        def get(self):
            error = self.request.get('error')
            if error:
                errormsg = self.request.get('error_description', error)
                self.response.out.write('The authorization request failed: {0}'.format(_safe_html(errormsg)))
            else:
                user = users.get_current_user()
                decorator._create_flow(self)
                credentials = decorator.flow.step2_exchange(self.request.params)
                decorator._storage_class(decorator._credentials_class, None, decorator._credentials_property_name, user=user).put(credentials)
                redirect_uri = _parse_state_value(str(self.request.get('state')), user)
                if (redirect_uri is None):
                    self.response.out.write('The authorization request failed')
                    return
                if (decorator._token_response_param and credentials.token_response):
                    resp_json = json.dumps(credentials.token_response)
                    redirect_uri = _helpers._add_query_parameter(redirect_uri, decorator._token_response_param, resp_json)
                self.redirect(redirect_uri)
    return OAuth2Handler

RequestHandler for the OAuth 2.0 redirect callback.

Usage::

app = webapp.WSGIApplication([
('/index', MyIndexHandler),
...,
(decorator.callback_path, decorator.callback_handler())
])

Returns:
A webapp.RequestHandler that handles the redirect back from the
server during the OAuth 2.0 dance.


def get_uri(dir_name):

    fullpath = os.path.abspath(dir_name)
    try:
        hostname = socket.gethostbyaddr(socket.gethostname())[0]
    except:
        hostname = socket.gethostname()
    return "{}:{}".format(hostname, fullpath)

Returns the URI path for a directory. This allows files hosted on
different file servers to have distinct locations.

Args:
dir_name:
A directory name.

Returns:
Full URI path, e.g., fileserver.host.com:/full/path/of/dir_name.


def load_model(file_path: str) -> NormalizedModel:
    with open(file_path) as f:
        model = json.load(f)
    model_flat = OrderedDict()
    for category in model:
        for item in model[category]:
            model_flat['%s:%s' % (category, item)] = model[category][item]
    weights = jnp.array(list(model_flat.values()))
    weights = weights / weights.std()
    weights = weights - weights.mean()
    keys = list(model_flat.keys())
    return NormalizedModel(keys, weights)

Loads a model as a pair of a features list and a normalized weight vector.

Args:
file_path: A file path for the model JSON file.

Returns:
A normalized model, which is a pair of a list of feature identifiers and a
normalized weight vector.


def parse_meta(filename, data):

    if "." not in filename:
        raise MetaParsingException(
            "Can't recognize type of your metadata ('%s')!" % filename
        )

    suffix = filename.rsplit(".", 1)[1].lower()

    if suffix not in SUPPORTED_FILES:
        raise MetaParsingException("Can't parse file of type '%s'!" % suffix)

    fp = validator.FieldParser()
    for key, val in SUPPORTED_FILES[suffix](data).items():
        fp.process(key, val)

    return fp.get_epublication()

Parse `data` to EPublication.

Args:
filename (str): Used to choose right parser based at suffix.
data (str): Content of the metadata file.

Returns:
EPublication: object.


def CheckTaskReadyForMerge(self, task):
    if (self._storage_type != definitions.STORAGE_TYPE_SESSION):
        raise IOError('Unsupported storage type.')
    if (not self._processed_task_storage_path):
        raise IOError('Missing processed task storage path.')
    processed_storage_file_path = self._GetProcessedStorageFilePath(task)
    try:
        stat_info = os.stat(processed_storage_file_path)
    except (IOError, OSError):
        return False
    task.storage_file_size = stat_info.st_size
    return True

Checks if a task is ready for merging with this session storage.

If the task is ready to be merged, this method also sets the task's
storage file size.

Args:
task (Task): task.

Returns:
bool: True if the task is ready to be merged.

Raises:
IOError: if the storage type is not supported or
OSError: if the storage type is not supported or
if the temporary path for the task storage does not exist.


def AddRow(self, values):

    super(CLITableView, self).AddRow(values)

    value_length = len(values[0])
    if value_length > self._column_width:
      self._column_width = value_length

Adds a row of values.

Args:
values (list[object]): values.

Raises:
ValueError: if the number of values is out of bounds.


def _register_and_parse_flags_with_usage(argv=None, flags_parser=parse_flags_with_usage):
    if _register_and_parse_flags_with_usage.done:
        raise SystemError('Flag registration can be done only once.')
    define_help_flags()
    original_argv = (sys.argv if (argv is None) else argv)
    args_to_main = flags_parser(original_argv)
    if (not FLAGS.is_parsed()):
        raise Error('FLAGS must be parsed after flags_parser is called.')
    if FLAGS.only_check_args:
        sys.exit(0)
    if FLAGS['verbosity'].using_default_value:
        FLAGS.verbosity = 0
    _register_and_parse_flags_with_usage.done = True
    return args_to_main

Registers help flags, parses arguments and shows usage if appropriate.

This also calls sys.exit(0) if flag --only_check_args is True.

Args:
argv: [str], a non-empty list of the command line arguments including
program name, sys.argv is used if None.
flags_parser: Callable[[List[Text]], Any], the function used to parse flags.
The return value of this function is passed to `main` untouched.
It must guarantee FLAGS is parsed after this function is called.

Returns:
The return value of `flags_parser`. When using the default `flags_parser`,
it returns the following:
[str], a non-empty list of remaining command line arguments after parsing
flags, including program name.

Raises:
Error: Raised when flags_parser is called, but FLAGS is not parsed.
SystemError: Raised when it's called more than once.


def _policy_equivalent_to_dtype(policy):
    return type(policy) == Policy and list(policy.get_config().keys()) == ['name'] and (policy.name == '_infer' or _is_convertible_to_dtype(policy.name))

Returns True if the Policy is equivalent to a single dtype.

A policy is equivalent to a single dtype if the policy's compute and variable
dtypes are the same and the policy's type is Policy and not a subclass of
Policy (such as PolicyV1).

The "_infer" policy is considered equivalent to a single dtype.

Args:
policy: A Policy.

Returns:
True, if the policy is equivalent to a single dtype.


async def retry_async(func, attempts=5, sleeptime_callback=calculate_sleep_time, retry_exceptions=Exception, args=(), kwargs=None, sleeptime_kwargs=None):
    kwargs = (kwargs or {})
    attempt = 1
    while True:
        try:
            return (await func(*args, **kwargs))
        except retry_exceptions:
            attempt += 1
            if (attempt > attempts):
                log.warning('retry_async: {}: too many retries!'.format(func.__name__))
                raise
            sleeptime_kwargs = (sleeptime_kwargs or {})
            sleep_time = sleeptime_callback(attempt, **sleeptime_kwargs)
            log.debug('retry_async: {}: sleeping {} seconds before retry'.format(func.__name__, sleep_time))
            (await asyncio.sleep(sleep_time))

Retry ``func``, where ``func`` is an awaitable.

Args:
func (function): an awaitable function.
attempts (int, optional): the number of attempts to make.  Default is 5.
sleeptime_callback (function, optional): the function to use to determine
how long to sleep after each attempt.  Defaults to ``calculateSleepTime``.
retry_exceptions (list or exception, optional): the exception(s) to retry on.
Defaults to ``Exception``.
args (list, optional): the args to pass to ``function``.  Defaults to ()
kwargs (dict, optional): the kwargs to pass to ``function``.  Defaults to
{}.
sleeptime_kwargs (dict, optional): the kwargs to pass to ``sleeptime_callback``.
If None, use {}.  Defaults to None.

Returns:
object: the value from a successful ``function`` call

Raises:
Exception: the exception from a failed ``function`` call, either outside
of the retry_exceptions, or one of those if we pass the max
``attempts``.


def GetSubkeyByPath(self, key_path):

    pyregf_key = self._pyregf_key.get_sub_key_by_path(key_path)
    if not pyregf_key:
      return None

    key_path = key_paths.JoinKeyPath([self._key_path, key_path])
    return REGFWinRegistryKey(pyregf_key, key_path=key_path)

Retrieves a subkey by path.

Args:
key_path (str): path of the subkey.

Returns:
WinRegistryKey: Windows Registry subkey or None if not found.


def __init__(self, dataFrame=None, editable=False):

        super(ColumnDtypeModel, self).__init__()
        self.headers = ['column', 'data type']

        self._editable = editable

        self._dataFrame = pandas.DataFrame()
        if dataFrame is not None:
            self.setDataFrame(dataFrame)

the __init__ method.

Args:
dataFrame (pandas.core.frame.DataFrame, optional): initializes the model with given DataFrame.
If none is given an empty DataFrame will be set. defaults to None.
editable (bool, optional): apply changes while changing dtype. defaults to True.


def set_tick(self, index, interval):

        name = self.tick_name(index)
        if name is None:
            return pack_error(ControllerSubsystem.SENSOR_GRAPH, Error.INVALID_ARRAY_KEY)

        self.ticks[name] = interval
        return Error.NO_ERROR

Update the a tick's interval.

Args:
index (int): The index of the tick that you want to fetch.
interval (int): The number of seconds between ticks.
Setting this to 0 will disable the tick.

Returns:
int: An error code.


def save(self, data):

        if self.__nested:
            raise ConfigLoaderException("Cannot save the config if the 'nested' paramter is True!")

        if self.__loaded_config_file is None:
            raise ConfigLoaderException("Load not called yet!")

        try:
            with open(self.__loaded_config_file, 'w') as f:
                f.write(self.__formatter.encode(data))
        except Exception as e:
            raise ConfigLoaderException("Config data is not serializable: %s" % e)

Save the config data

Args:
data: any serializable config data

Raises:
ConfigLoaderException: if the ConfigLoader.load not called, so there is no config file name,
or the data is not serializable or the loader is nested


def _handle_location(self, location):

        if not isinstance(location, ElementTree.Element):
            element = self.find(location)
            if element is None:
                raise ValueError("Invalid path!")
        else:
            element = location
        return element

Return an element located at location with flexible args.

Args:
location: String xpath to use in an Element.find search OR
an Element (which is simply returned).

Returns:
The found Element.

Raises:
ValueError if the location is a string that results in a
find of None.


def do_post(self, uri, resource, timeout, custom_headers):

        self.validate_resource_uri(uri)

        task, entity = self._connection.post(uri, resource, custom_headers=custom_headers)

        if not task:
            return entity

        return self._task_monitor.wait_for_task(task, timeout)

Helps to make post requests.

Args:
uri: URI of  the resource.
resource: Resource data to post.
timeout: Time out for the request in seconds.
cutom_headers: Allows to add custom http headers.

Returns:
Retunrs Task object.


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

        tstream = BytearrayStream()

        self.extension_name.write(tstream, kmip_version=kmip_version)

        if self.extension_tag is not None:
            self.extension_tag.write(tstream, kmip_version=kmip_version)
        if self.extension_type is not None:
            self.extension_type.write(tstream, kmip_version=kmip_version)

        self.length = tstream.length()
        super(ExtensionInformation, self).write(
            ostream,
            kmip_version=kmip_version
        )
        ostream.write(tstream.buffer)

Write the data encoding the ExtensionInformation object to a stream.

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


def replace_by_etree(self, root_el, el_idx=0):

        el = self.get_element_by_name(root_el.tag, el_idx)
        el[:] = list(root_el)
        el.attrib = root_el.attrib

Replace element.

Select element that has the same name as ``root_el``, then replace the selected
element with ``root_el``

``root_el`` can be a single element or the root of an element tree.

Args:
root_el : element
New element that will replace the existing element.


def label_sequential_regions(inlist):
    import more_itertools as mit
    df = pd.DataFrame(inlist).set_index(0)
    labeled = {}
    for label in df[1].unique():
        iterable = df[(df[1] == label)].index.tolist()
        labeled.update({'{}{}'.format(label, (i + 1)): items for (i, items) in enumerate([list(group) for group in mit.consecutive_groups(iterable)])})
    return labeled

Input a list of labeled tuples and return a dictionary of sequentially labeled regions.

Args:
inlist (list): A list of tuples with the first number representing the index and the second the index label.

Returns:
dict: Dictionary of labeled regions.

Examples:

>>> label_sequential_regions([(1, 'O'), (2, 'O'), (3, 'O'), (4, 'M'), (5, 'M'), (6, 'I'), (7, 'M'), (8, 'O'), (9, 'O')])
{'O1': [1, 2, 3], 'M1': [4, 5], 'I1': [6], 'M2': [7], 'O2': [8, 9]}


def aggregate_periods(self, periods):

        try:
            fieldname = self.raster_field.name
        except TypeError:
            raise exceptions.FieldDoesNotExist('Raster field not found')
        arrays = self.arrays(fieldname)
        arr = arrays[0]
        if len(arrays) > 1:
            if getattr(arr, 'ndim', 0) > 2:
                arrays = np.vstack(arrays)
            fill = getattr(arr, 'fill_value', None)
            arr = np.ma.masked_values(arrays, fill, copy=False)

        try:
            means = arr.reshape((periods, -1)).mean(axis=1)
        except ValueError:
            means = np.array([a.mean() for a in np.array_split(arr, periods)])
        obj = self[0]
        setattr(obj, fieldname, means)
        return [obj]

Returns list of ndarrays averaged to a given number of periods.

Arguments:
periods -- desired number of periods as int


def log_warning(self, msg):
    if self.__logger:
        self.__logger.warning(msg)
    if self.__raise_exception_on_warning:
        raise RuntimeError(msg)

Log a warning if ``logger`` exists.

Args:
msg: Warning to log.

Warning:
Can raise a ``RuntimeError`` if this was asked in the constructor.


def hgnc_genes(self, hgnc_symbol, build='37', search=False):
    LOG.debug(('Fetching genes with symbol %s' % hgnc_symbol))
    if search:
        full_query = self.hgnc_collection.find({'$or': [{'aliases': hgnc_symbol}, {'hgnc_id': (int(hgnc_symbol) if hgnc_symbol.isdigit() else None)}], 'build': build})
        if (full_query.count() != 0):
            return full_query
        return self.hgnc_collection.find({'aliases': {'$regex': hgnc_symbol, '$options': 'i'}, 'build': build})
    return self.hgnc_collection.find({'build': build, 'aliases': hgnc_symbol})

Fetch all hgnc genes that match a hgnc symbol

Check both hgnc_symbol and aliases

Args:
hgnc_symbol(str)
build(str): The build in which to search
search(bool): if partial searching should be used

Returns:
result()


def get_index(uid, i):
    return _SHARED_SEQUENCES[uid][i]

Get the value from the Sequence `uid` at index `i`.

To allow multiple Sequences to be used at the same time, we use `uid` to
get a specific one. A single Sequence would cause the validation to
overwrite the training Sequence.

Args:
uid: int, Sequence identifier
i: index

Returns:
The value at index `i`.


def bfs(self, graph, start):

        newstatediag = {}

        queue = []
        visited = []

        queue.append(start)
        while queue:

            state = queue.pop(0)

            visited.append(state.id)

            for key in state.trans:
                if state.trans[key] != []:
                    if key not in visited:
                        for nextstate in graph:
                            if graph[nextstate].id == key:
                                queue.append(graph[nextstate])
                                break
        i = 0
        for state in graph:
            if graph[state].id in visited:
                newstatediag[i] = graph[state]
                i = i + 1

        return newstatediag

Performs BFS operation for eliminating useless loop transitions
Args:
graph (PDA): the PDA object
start (PDA state): The PDA initial state
Returns:
list: A cleaned, smaller list of DFA states


def perspective(img, startpoints, endpoints, interpolation=Image.BICUBIC):

    if not _is_pil_image(img):
        raise TypeError('img should be PIL Image. Got {}'.format(type(img)))

    coeffs = _get_perspective_coeffs(startpoints, endpoints)
    return img.transform(img.size, Image.PERSPECTIVE, coeffs, interpolation)

Perform perspective transform of the given PIL Image.

Args:
img (PIL Image): Image to be transformed.
coeffs (tuple) : 8-tuple (a, b, c, d, e, f, g, h) which contains the coefficients.
for a perspective transform.
interpolation: Default- Image.BICUBIC
Returns:
PIL Image:  Perspectively transformed Image.


def parse_options(cls, options):
    d = {}
    for (filename_check, dictionary) in cls.filename_checks.items():
        filename_data = getattr(options, filename_check)
        if (len(filename_data) != 0):
            parsed_params = {}
            for single_line in filename_data:
                a = [s.strip() for s in single_line.split('=')]
                if (a[0] in ['filter_regex', 'filename_regex']):
                    parsed_params[a[0]] = a[1]
            d[filename_check] = parsed_params
    cls.filename_checks.update(d)
    cls.filename_checks = {x: y for (x, y) in cls.filename_checks.items() if (len(y) > 0)}

Required by flake8
parse the options, called after add_options

Args:
options (dict): options to be parsed


def predict_undirected_graph(self, data):

        graph = Graph()

        for idx_i, i in enumerate(data.columns):
            for idx_j, j in enumerate(data.columns[idx_i+1:]):
                score = self.predict(data[i].values, data[j].values)
                if abs(score) > 0.001:
                    graph.add_edge(i, j, weight=score)

        return graph

Build a skeleton using a pairwise independence criterion.

Args:
data (pandas.DataFrame): Raw data table

Returns:
networkx.Graph: Undirected graph representing the skeleton.


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

        logger.info('Processing node {} expecting type {}'.format(
            node, expected_type))

        recognized_types, message = self.__recognizer.recognize(
            node, expected_type)

        if len(recognized_types) != 1:
            raise RecognitionError(message)

        recognized_type = recognized_types[0]

        logger.debug('Savorizing node {}'.format(node))
        if recognized_type in self._registered_classes.values():
            node = self.__savorize(node, recognized_type)
        logger.debug('Savorized, now {}'.format(node))

        logger.debug('Recursing into subnodes')
        if is_generic_list(recognized_type):
            if node.tag != 'tag:yaml.org,2002:seq':
                raise RecognitionError('{}{}Expected a {} here'.format(
                    node.start_mark, os.linesep,
                    type_to_desc(expected_type)))
            for item in node.value:
                self.__process_node(item,
                                    generic_type_args(recognized_type)[0])
        elif is_generic_dict(recognized_type):
            if node.tag != 'tag:yaml.org,2002:map':
                raise RecognitionError('{}{}Expected a {} here'.format(
                    node.start_mark, os.linesep,
                    type_to_desc(expected_type)))
            for _, value_node in node.value:
                self.__process_node(value_node,
                                    generic_type_args(recognized_type)[1])

        elif recognized_type in self._registered_classes.values():
            if (not issubclass(recognized_type, enum.Enum)
                    and not issubclass(recognized_type, str)
                    and not issubclass(recognized_type, UserString)):
                for attr_name, type_, _ in class_subobjects(recognized_type):
                    cnode = Node(node)
                    if cnode.has_attribute(attr_name):
                        subnode = cnode.get_attribute(attr_name)
                        new_subnode = self.__process_node(
                            subnode.yaml_node, type_)
                        cnode.set_attribute(attr_name, new_subnode)
        else:
            logger.debug('Not a generic class or a user-defined class, not'
                         ' recursing')

        node.tag = self.__type_to_tag(recognized_type)
        logger.debug('Finished processing node {}'.format(node))
        return node

Processes a node.

This is the main function that implements yatiml's \
functionality. It figures out how to interpret this node \
(recognition), then applies syntactic sugar, and finally \
recurses to the subnodes, if any.

Args:
node: The node to process.
expected_type: The type we expect this node to be.

Returns:
The transformed node, or a transformed copy.


def line_line_collide(line1, line2):

    s, t, success = segment_intersection(
        line1[:, 0], line1[:, 1], line2[:, 0], line2[:, 1]
    )
    if success:
        return _helpers.in_interval(s, 0.0, 1.0) and _helpers.in_interval(
            t, 0.0, 1.0
        )

    else:
        disjoint, _ = parallel_lines_parameters(
            line1[:, 0], line1[:, 1], line2[:, 0], line2[:, 1]
        )
        return not disjoint

Determine if two line segments meet.

This is a helper for :func:`convex_hull_collide` in the
special case that the two convex hulls are actually
just line segments. (Even in this case, this is only
problematic if both segments are on a single line.)

Args:
line1 (numpy.ndarray): ``2 x 2`` array of start and end nodes.
line2 (numpy.ndarray): ``2 x 2`` array of start and end nodes.

Returns:
bool: Indicating if the line segments collide.


def _convert_to_hashable(data, types=True):
    if (data is None):
        hashable = b'NONE'
        prefix = b'NULL'
    elif isinstance(data, six.binary_type):
        hashable = data
        prefix = b'TXT'
    elif isinstance(data, six.text_type):
        hashable = data.encode('utf-8')
        prefix = b'TXT'
    elif isinstance(data, _intlike):
        hashable = _int_to_bytes(data)
        prefix = b'INT'
    elif isinstance(data, float):
        (a, b) = float(data).as_integer_ratio()
        hashable = ((_int_to_bytes(a) + b'/') + _int_to_bytes(b))
        prefix = b'FLT'
    else:
        hash_func = _HASHABLE_EXTENSIONS.lookup(data)
        (prefix, hashable) = hash_func(data)
    if types:
        return (prefix, hashable)
    else:
        return (b'', hashable)

r"""
Converts `data` into a hashable byte representation if an appropriate
hashing function is known.

Args:
data (object): ordered data with structure
types (bool): include type prefixes in the hash

Returns:
tuple(bytes, bytes): prefix, hashable:
a prefix hinting the original data type and the byte representation
of `data`.

Raises:
TypeError : if data has no registered hash methods

Example:
>>> assert _convert_to_hashable(None) == (b'NULL', b'NONE')
>>> assert _convert_to_hashable('string') == (b'TXT', b'string')
>>> assert _convert_to_hashable(1) == (b'INT', b'\x01')
>>> assert _convert_to_hashable(1.0) == (b'FLT', b'\x01/\x01')
>>> assert _convert_to_hashable(_intlike[-1](1)) == (b'INT', b'\x01')


def register(self, name, option):

        if name in self._options:

            raise ValueError("Option {0} already exists.".format(name))

        if not isinstance(option, opt.Option):

            raise TypeError("Options must be of type Option.")

        self._options[name] = option

Register a new option with the namespace.

Args:
name (str): The name to register the option under.
option (option.Option): The option object to register.

Raises:
TypeError: If the option is not an option.Option object.
ValueError: If the name is already registered.


def _batch_static_inner_shape(old_shape: tensor_shape.TensorShape, batch_size: Optional[int]) -> tensor_shape.TensorShape:
    head_dim = tensor_shape.dimension_at_index(old_shape, 0) * batch_size
    return head_dim + old_shape[1:]

Returns a copy of old_shape with axis=0 multiplied by batch_size.

Only use if this is the inner_shape of a DynamicRaggedShape.Spec with one
or more row partitions.

Args:
old_shape: the original inner_shape.
batch_size: the batch size.

Returns:
a new shape.


def _get_subclass_names(self, classname, namespace, deep_inheritance):
    assert ((classname is None) or isinstance(classname, (six.string_types, CIMClassName)))
    if isinstance(classname, CIMClassName):
        classname = classname.classname
    try:
        classes = self.classes[namespace]
    except KeyError:
        classes = NocaseDict()
    if (classname is None):
        rtn_classnames = [cl.classname for cl in six.itervalues(classes) if (cl.superclass is None)]
    else:
        rtn_classnames = [cl.classname for cl in six.itervalues(classes) if (cl.superclass and (cl.superclass.lower() == classname.lower()))]
    if deep_inheritance:
        subclass_names = []
        if rtn_classnames:
            for cn in rtn_classnames:
                subclass_names.extend(self._get_subclass_names(cn, namespace, deep_inheritance))
        rtn_classnames.extend(subclass_names)
    return rtn_classnames

Get class names that are subclasses of the
classname input parameter from the repository.

If DeepInheritance is False, get only classes in the
repository for the defined namespace for which this class is a
direct super class.

If deep_inheritance is `True`, get all direct and indirect
subclasses.  If false, get only a the next level of the
hiearchy.

Returns:
list of strings with the names of all subclasses of `classname`.


def get_generated_cols(X_original, X_transformed, to_transform):

    original_cols = list(X_original.columns)

    if len(to_transform) > 0:
        [original_cols.remove(c) for c in to_transform]

    current_cols = list(X_transformed.columns)
    if len(original_cols) > 0:
        [current_cols.remove(c) for c in original_cols]

    return current_cols

Returns a list of the generated/transformed columns.

Arguments:
X_original: df
the original (input) DataFrame.
X_transformed: df
the transformed (current) DataFrame.
to_transform: [str]
a list of columns that were transformed (as in the original DataFrame), commonly self.cols.

Output:
a list of columns that were transformed (as in the current DataFrame).


def _attach_files(filepaths, email_):
    for filepath in filepaths:
        base = os.path.basename(filepath)
        with open(filepath, 'rb') as file:
            part = MIMEApplication(file.read(), Name=base)
            part['Content-Disposition'] = ('attachment; filename="%s"' % base)
            email_.attach(part)

Take a list of filepaths and attach the files to a MIMEMultipart.

Args:
filepaths (list(str)): A list of filepaths.
email_ (email.MIMEMultipart): A MIMEMultipart email_.


def _ContainsExactlyElementsIn(self, expected, warn_elements_in=False):
    if not expected:
        if self._actual:
            self._FailWithProposition('is empty')
        return _InOrder()
    missing = _DuplicateCounter()
    extra = _DuplicateCounter()
    actual_iter = iter(self._actual)
    expected_iter = iter(expected)
    warning = ''
    if warn_elements_in:
        warning = ' Passing a single iterable to ContainsExactly(*expected) is often not the correct thing to do. Did you mean to call ContainsExactlyElementsIn(Iterable) instead?'
    while True:
        try:
            actual_element = next(actual_iter)
        except StopIteration:
            break
        try:
            expected_element = next(expected_iter)
        except StopIteration:
            extra.Increment(actual_element)
            break
        if actual_element != expected_element:
            missing.Increment(expected_element)
            for m in expected_iter:
                missing.Increment(m)
            if actual_element in missing:
                missing.Decrement(actual_element)
            else:
                extra.Increment(actual_element)
            for e in actual_iter:
                if e in missing:
                    missing.Decrement(e)
                else:
                    extra.Increment(e)
            if missing:
                if extra:
                    self._FailWithProposition('contains exactly <{0!r}>. It is missing <{1}> and has unexpected items <{2}>'.format(expected, missing, extra), suffix=warning)
                else:
                    self._FailWithBadResults('contains exactly', expected, 'is missing', missing, suffix=warning)
            if extra:
                self._FailWithBadResults('contains exactly', expected, 'has unexpected items', extra, suffix=warning)
            return _NotInOrder(self._actual, 'contains exactly these elements in order', expected)
    for e in actual_iter:
        extra.Increment(e)
    if extra:
        self._FailWithBadResults('contains exactly', expected, 'has unexpected items', extra, suffix=warning)
    for m in expected_iter:
        missing.Increment(m)
    if missing:
        self._FailWithBadResults('contains exactly', expected, 'is missing', missing, suffix=warning)
    return _InOrder()

Determines if the subject contains exactly the expected elements.

Helper function for ContainsExactly() and ContainsExactlyElementsIn().

Args:
expected: iterable of objects that should be contained in the subject.
warn_elements_in: boolean, default False. If True, and the assertion
fails, and the developer invoked ContainsExactly() with a single
iterable, warn that this usage is error-prone.

Returns:
If the subject does contain exactly the expected elements, returns an
_Ordered predicate on which .InOrder() can be subsequently called.

Raises:
TruthAssertionError: the subject is missing any of the expected elements,
or the subject contains any element not in the expected elements.


def problem_id(self, value):

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

The problem_id property.

Args:
value (string). the property value.


def get_inspection_units(logdir='', event_file='', tag=''):
    if logdir:
        subdirs = io_wrapper.GetLogdirSubdirectories(logdir)
        inspection_units = []
        for subdir in subdirs:
            generator = itertools.chain(*[generator_from_event_file(os.path.join(subdir, f)) for f in tf.io.gfile.listdir(subdir) if io_wrapper.IsTensorFlowEventsFile(os.path.join(subdir, f))])
            inspection_units.append(InspectionUnit(name=subdir, generator=generator, field_to_obs=get_field_to_observations_map(generator, tag)))
        if inspection_units:
            print('Found event files in:\n{}\n'.format('\n'.join([u.name for u in inspection_units])))
        elif io_wrapper.IsTensorFlowEventsFile(logdir):
            print('It seems that {} may be an event file instead of a logdir. If this is the case, use --event_file instead of --logdir to pass it in.'.format(logdir))
        else:
            print('No event files found within logdir {}'.format(logdir))
        return inspection_units
    elif event_file:
        generator = generator_from_event_file(event_file)
        return [InspectionUnit(name=event_file, generator=generator, field_to_obs=get_field_to_observations_map(generator, tag))]
    return []

Returns a list of InspectionUnit objects given either logdir or event_file.

If logdir is given, the number of InspectionUnits should equal the
number of directories or subdirectories that contain event files.

If event_file is given, the number of InspectionUnits should be 1.

Args:
logdir: A log directory that contains event files.
event_file: Or, a particular event file path.
tag: An optional tag name to query for.

Returns:
A list of InspectionUnit objects.


def uniform_binning_correction(x, n_bits=8):

  n_bins = 2**n_bits
  batch_size, height, width, n_channels = common_layers.shape_list(x)
  hwc = float(height * width * n_channels)

  x = x + tf.random_uniform(
      shape=(batch_size, height, width, n_channels),
      minval=0.0, maxval=1.0/n_bins)
  objective = -np.log(n_bins) * hwc * tf.ones(batch_size)
  return x, objective

Replaces x^i with q^i(x) = U(x, x + 1.0 / 256.0).

Args:
x: 4-D Tensor of shape (NHWC)
n_bits: optional.
Returns:
x: x ~ U(x, x + 1.0 / 256)
objective: Equivalent to -q(x)*log(q(x)).


def _ParseSourcePathOption(self, options):
    self._source_path = self.ParseStringOption(options, self._SOURCE_OPTION)
    if (not self._source_path):
        raise errors.BadConfigOption('Missing source path.')
    self._source_path = os.path.abspath(self._source_path)

Parses the source path option.

Args:
options (argparse.Namespace): command line arguments.

Raises:
BadConfigOption: if the options are invalid.


def provide(self, cls):
    support.verify_class_type(cls, 'cls')
    if (not self._is_injectable_fn(cls)):
        provide_loc = locations.get_back_frame_loc()
        raise errors.NonExplicitlyBoundClassError(provide_loc, cls)
    try:
        return self._obj_provider.provide_class(cls, self._injection_context_factory.new(cls.__init__), direct_init_pargs=[], direct_init_kwargs={})
    except errors.Error as e:
        if self._use_short_stack_traces:
            raise e
        else:
            raise

Provides an instance of the given class.

Args:
cls: a class (not an instance)
Returns:
an instance of cls
Raises:
Error: an instance of cls is not providable


def transform_function(self, fn, user_context):
    cache_subkey = self.get_caching_key(user_context)
    if self._cache.has(fn, cache_subkey):
        factory = self._cached_factory(fn, cache_subkey)
    else:
        with self._cache_lock:
            if self._cache.has(fn, cache_subkey):
                factory = self._cached_factory(fn, cache_subkey)
            else:
                logging.log(1, '%s is not cached for subkey %s', fn, cache_subkey)
                nodes, ctx = super(PyToPy, self).transform_function(fn, user_context)
                if isinstance(nodes, gast.Lambda):
                    nodes = gast.Assign(targets=[gast.Name(ctx.info.name, ctx=gast.Store(), annotation=None, type_comment=None)], value=nodes)
                else:
                    nodes.name = ctx.info.name
                if logging.has_verbosity(2):
                    logging.log(2, 'Transformed %s:\n\n%s\n', fn, parser.unparse(nodes))
                factory = _PythonFnFactory(ctx.info.name, fn.__code__.co_freevars, self.get_extra_locals())
                factory.create(nodes, ctx.namer, future_features=ctx.info.future_features)
                self._cache[fn][cache_subkey] = factory
    transformed_fn = factory.instantiate(globals_=fn.__globals__, closure=fn.__closure__ or (), defaults=fn.__defaults__, kwdefaults=getattr(fn, '__kwdefaults__', None))
    return (transformed_fn, factory.module, factory.source_map)

Transforms a function. See GenericTranspiler.transform_function.

This overload wraps the parent's `transform_function`, adding caching and
facilities to instantiate the output as a Python object. It also
adds facilities to make new symbols available to the generated Python code,
visible as local variables - see `get_extra_locals`.

Args:
fn: A function or lambda.
user_context: An opaque object (may be None) that is forwarded to
transform_ast, through the ctx.user attribute.

Returns:
A tuple:
* A function or lambda with the same signature and closure as `fn`
* The temporary module into which the transformed function was loaded
* The source map as a
Dict[origin_info.LineLocation, origin_info.OriginInfo]


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

        local_stream = BytearrayStream()

        if self._wrapping_method:
            self._wrapping_method.write(
                local_stream,
                kmip_version=kmip_version
            )
        else:
            raise ValueError(
                "Invalid struct missing the wrapping method attribute."
            )

        if self._encryption_key_information:
            self._encryption_key_information.write(
                local_stream,
                kmip_version=kmip_version
            )
        if self._mac_signature_key_information:
            self._mac_signature_key_information.write(
                local_stream,
                kmip_version=kmip_version
            )
        if self._mac_signature:
            self._mac_signature.write(
                local_stream,
                kmip_version=kmip_version
            )
        if self._iv_counter_nonce:
            self._iv_counter_nonce.write(
                local_stream,
                kmip_version=kmip_version
            )
        if self._encoding_option:
            self._encoding_option.write(
                local_stream,
                kmip_version=kmip_version
            )

        self.length = local_stream.length()
        super(KeyWrappingData, self).write(
            output_stream,
            kmip_version=kmip_version
        )
        output_stream.write(local_stream.buffer)

Write the data encoding the KeyWrappingData struct to a stream.

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


def _find_penultimate_layer(model, layer_idx, penultimate_layer_idx):
    if (penultimate_layer_idx is None):
        for (idx, layer) in utils.reverse_enumerate(model.layers[:(layer_idx - 1)]):
            if isinstance(layer, Wrapper):
                layer = layer.layer
            if isinstance(layer, (_Conv, _Pooling1D, _Pooling2D, _Pooling3D)):
                penultimate_layer_idx = idx
                break
    if (penultimate_layer_idx is None):
        raise ValueError('Unable to determine penultimate `Conv` or `Pooling` layer for layer_idx: {}'.format(layer_idx))
    if (layer_idx < 0):
        layer_idx = (len(model.layers) + layer_idx)
    if (penultimate_layer_idx > layer_idx):
        raise ValueError('`penultimate_layer_idx` needs to be before `layer_idx`')
    return model.layers[penultimate_layer_idx]

Searches for the nearest penultimate `Conv` or `Pooling` layer.

Args:
model: The `keras.models.Model` instance.
layer_idx: The layer index within `model.layers`.
penultimate_layer_idx: The pre-layer to `layer_idx`. If set to None, the nearest penultimate
`Conv` or `Pooling` layer is used.

Returns:
The penultimate layer.


def Close(self, abort=False):

    if abort:

      self._queue.cancel_join_thread()

    self._queue.close()
    self._queue.join_thread()

Closes the queue.

This needs to be called from any process or thread putting items onto
the queue.

Args:
abort (Optional[bool]): True if the close was issued on abort.


def poll(self, transaction_hash: bytes):
    if (len(transaction_hash) != 32):
        raise ValueError('transaction_hash must be a 32 byte hash')
    transaction_hash = encode_hex(transaction_hash)
    last_result = None
    while True:
        transaction = self.web3.eth.getTransaction(transaction_hash)
        if ((transaction is None) and (last_result is not None)):
            raise Exception('invalid transaction, check gas price')
        if (transaction and (transaction['blockNumber'] is not None)):
            last_result = transaction
            transaction_block = transaction['blockNumber']
            confirmation_block = (transaction_block + self.default_block_num_confirmations)
            block_number = self.block_number()
            if (block_number >= confirmation_block):
                return transaction
        gevent.sleep(1.0)

Wait until the `transaction_hash` is applied or rejected.

Args:
transaction_hash: Transaction hash that we are waiting for.


def relevant_connections(n, _from, to):
    cm = np.zeros((n, n))
    if ((not _from) or (not to)):
        return cm
    cm[np.ix_(_from, to)] = 1
    return cm

Construct a connectivity matrix.

Args:
n (int): The dimensions of the matrix
_from (tuple[int]): Nodes with outgoing connections to ``to``
to (tuple[int]): Nodes with incoming connections from ``_from``

Returns:
np.ndarray: An |n x n| connectivity matrix with the |i,jth| entry is
``1`` if |i| is in ``_from`` and |j| is in ``to``, and 0 otherwise.


def _export_work_errors(self, work, output_file):
    errors = set()
    for v in itervalues(work.work):
        if (v['is_completed'] and (v['error'] is not None)):
            errors.add(v['error'])
    with open(output_file, 'w') as f:
        for e in sorted(errors):
            f.write(e)
            f.write('\n')

Saves errors for given work pieces into file.

Args:
work: instance of either AttackWorkPieces or DefenseWorkPieces
output_file: name of the output file


async def do_run_task(context, run_cancellable, to_cancellable_process):
    status = 0
    try:
        if context.config['verify_chain_of_trust']:
            chain = ChainOfTrust(context, context.config['cot_job_type'])
            (await run_cancellable(verify_chain_of_trust(chain)))
        status = (await run_task(context, to_cancellable_process))
        generate_cot(context)
    except asyncio.CancelledError:
        log.info('CoT cancelled asynchronously')
        raise WorkerShutdownDuringTask
    except ScriptWorkerException as e:
        status = worst_level(status, e.exit_code)
        log.error('Hit ScriptWorkerException: {}'.format(e))
    except Exception as e:
        log.exception('SCRIPTWORKER_UNEXPECTED_EXCEPTION task {}'.format(e))
        raise
    return status

Run the task logic.

Returns the integer status of the task.

args:
context (scriptworker.context.Context): the scriptworker context.
run_cancellable (typing.Callable): wraps future such that it'll cancel upon worker shutdown
to_cancellable_process (typing.Callable): wraps ``TaskProcess`` such that it will stop if the worker is shutting
down

Raises:
Exception: on unexpected exception.

Returns:
int: exit status


def setNetworkIDTimeout(self, iNwkIDTimeOut):

        print '%s call setNetworkIDTimeout' % self.port
        print iNwkIDTimeOut
        iNwkIDTimeOut /= 1000
        try:
            cmd = 'networkidtimeout %s' % str(iNwkIDTimeOut)
            print cmd
            return self.__sendCommand(cmd)[0] == 'Done'
        except Exception, e:
            ModuleHelper.WriteIntoDebugLogger("setNetworkIDTimeout() Error: " + str(e))

set networkid timeout for Thread device

Args:
iNwkIDTimeOut: a given NETWORK_ID_TIMEOUT

Returns:
True: successful to set NETWORK_ID_TIMEOUT
False: fail to set NETWORK_ID_TIMEOUT


def present(name, save=False, **kwargs):
    ret = {'name': name, 'result': True, 'changes': {}, 'comment': []}
    current_beacons = __salt__['beacons.list'](return_yaml=False, **kwargs)
    beacon_data = [{k: v} for (k, v) in six.iteritems(kwargs)]
    if (name in current_beacons):
        if (beacon_data == current_beacons[name]):
            ret['comment'].append('Job {0} in correct state'.format(name))
        elif (('test' in __opts__) and __opts__['test']):
            kwargs['test'] = True
            result = __salt__['beacons.modify'](name, beacon_data, **kwargs)
            ret['comment'].append(result['comment'])
            ret['changes'] = result['changes']
        else:
            result = __salt__['beacons.modify'](name, beacon_data, **kwargs)
            if (not result['result']):
                ret['result'] = result['result']
                ret['comment'] = result['comment']
                return ret
            elif ('changes' in result):
                ret['comment'].append('Modifying {0} in beacons'.format(name))
                ret['changes'] = result['changes']
            else:
                ret['comment'].append(result['comment'])
    elif (('test' in __opts__) and __opts__['test']):
        kwargs['test'] = True
        result = __salt__['beacons.add'](name, beacon_data, **kwargs)
        ret['comment'].append(result['comment'])
    else:
        result = __salt__['beacons.add'](name, beacon_data, **kwargs)
        if (not result['result']):
            ret['result'] = result['result']
            ret['comment'] = result['comment']
            return ret
        else:
            ret['comment'].append('Adding {0} to beacons'.format(name))
    if save:
        __salt__['beacons.save'](**kwargs)
        ret['comment'].append('Beacon {0} saved'.format(name))
    ret['comment'] = '\n'.join(ret['comment'])
    return ret

Ensure beacon is configured with the included beacon data.

Args:

name (str):
The name of the beacon ensure is configured.

save (bool):
``True`` updates the beacons.conf. Default is ``False``.

Returns:
dict: A dictionary of information about the results of the state

Example:

.. code-block:: yaml

ps_beacon:
beacon.present:
- name: ps
- save: True
- enable: False
- services:
salt-master: running
apache2: stopped


def __ge__(self, other):

        if other.__class__ is not self.__class__:
            return NotImplemented
        return not self < other

Test if self is greater than or equal an object of the same class.

Args:
other: The object to compare against.

Returns:
True if self is greater than or equal to other; else False.

Raises:
TypeError: Raised if the objects are not of the same class.


def get_next_of_type(self, processor_type):
    with self._condition:
        if (processor_type not in self):
            self.wait_for_registration(processor_type)
        try:
            processor = self[processor_type].next_processor()
        except NoProcessorVacancyError:
            processor = self.wait_for_vacancy(processor_type)
        processor.inc_occupancy()
        return processor

Get the next available processor of a particular type and increment
its occupancy counter.

Args:
processor_type (ProcessorType): The processor type associated with
a zmq identity.

Returns:
(Processor): Information about the transaction processor


def prop(pode, prop):

    form = pode[0][0]
    if prop.startswith(form):
        prop = prop[len(form):]
    if prop[0] == ':':
        prop = prop[1:]
    return pode[1]['props'].get(prop)

Return the valu of a given property on the node.

Args:
pode (tuple): A packed node.
prop (str): Property to retrieve.

Notes:
The prop argument may be the full property name (foo:bar:baz), relative property name (:baz) , or the unadorned
property name (baz).

Returns:


def initialize_write(self):
    raise NotImplementedError

Initializes the sink before writing begins.

Invoked before any data is written to the sink.

Please see documentation in ``iobase.Sink`` for an example.

Returns:
An object that contains any sink specific state generated by
initialization. This object will be passed to open_writer() and
finalize_write() methods.


def create_graph_from_data(self, data, **kwargs):
    self.arguments['{SCORE}'] = self.scores[self.score]
    self.arguments['{CUTOFF}'] = str(self.cutoff)
    self.arguments['{VARSEL}'] = str(self.variablesel).upper()
    self.arguments['{SELMETHOD}'] = self.var_selection[self.selmethod]
    self.arguments['{PRUNING}'] = str(self.pruning).upper()
    self.arguments['{PRUNMETHOD}'] = self.var_selection[self.prunmethod]
    self.arguments['{NJOBS}'] = str(self.nb_jobs)
    self.arguments['{VERBOSE}'] = str(self.verbose).upper()
    results = self._run_cam(data, verbose=self.verbose)
    return nx.relabel_nodes(nx.DiGraph(results), {idx: i for (idx, i) in enumerate(data.columns)})

Apply causal discovery on observational data using CAM.

Args:
data (pandas.DataFrame): DataFrame containing the data

Returns:
networkx.DiGraph: Solution given by the CAM algorithm.


def set_db_row(db, start, size, _bytearray):

    client.db_write(db, start, size, _bytearray)

Here we replace a piece of data in a db block with new data

Args:
db (int): The db to use
start(int): The start within the db
size(int): The size of the data in bytes
_butearray (enumerable): The data to put in the db


def BuildTypeDescriptor(self, value_cls):

    result = ApiRDFValueDescriptor(
        name=value_cls.__name__,
        parents=[klass.__name__ for klass in value_cls.__mro__],
        doc=value_cls.__doc__ or "",
        kind="PRIMITIVE")

    result.default = self.BuildDefaultValue(value_cls)

    return result

Renders metadata of a given value class.

Args:
value_cls: Metadata of this class will be rendered. This class has to be
(or to be a subclass of) a self.value_class (i.e. a class that this
renderer is capable of rendering).

Returns:
Dictionary with class metadata.


def GetEntries(self, parser_mediator, match=None, **unused_kwargs):

    shortcuts = match.get('UserShortcuts', {})
    for search_text, data in iter(shortcuts.items()):
      datetime_value = data.get('LAST_USED', None)
      if not datetime_value:
        continue

      display_name = data.get('DISPLAY_NAME', '<DISPLAY_NAME>')
      path = data.get('PATH', '<PATH>')

      event_data = plist_event.PlistTimeEventData()
      event_data.desc = (
          'Spotlight term searched "{0:s}" associate to {1:s} ({2:s})').format(
              search_text, display_name, path)
      event_data.key = search_text
      event_data.root = '/UserShortcuts'

      event = time_events.PythonDatetimeEvent(
          datetime_value, definitions.TIME_DESCRIPTION_WRITTEN)
      parser_mediator.ProduceEventWithEventData(event, event_data)

Extracts relevant Spotlight entries.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
match (Optional[dict[str: object]]): keys extracted from PLIST_KEYS.


def switch(data, pred, dtype=None, name=None):
    with ops.name_scope(name, 'Switch', [data, pred]) as name:
        data = ops.internal_convert_to_tensor_or_composite(data, dtype=dtype, name='data', as_ref=True)
        pred = ops.convert_to_tensor(pred, name='pred')
        if isinstance(data, tensor_lib.Tensor):
            return gen_control_flow_ops.switch(data, pred, name=name)
        else:
            if not isinstance(data, composite_tensor.CompositeTensor):
                raise TypeError(f"'data' must be a Tensor or CompositeTensor. Received: {type(data)}.")
            tensors = nest.flatten(data, expand_composites=True)
            mapped = [gen_control_flow_ops.switch(tensor, pred) for tensor in tensors]
            mapped_f, mapped_t = zip(*mapped)
            return (nest.pack_sequence_as(data, mapped_f, expand_composites=True), nest.pack_sequence_as(data, mapped_t, expand_composites=True))

Forwards `data` to an output determined by `pred`.

If `pred` is false, the `data` input is forwarded to the first output.
Otherwise, the data goes to the second output.

This op handles `Tensor`s and `IndexedSlices`.

Args:
data: The tensor to be forwarded to the appropriate output.
pred: A scalar that specifies which output port will receive data.
dtype: Optional element type for the returned tensor. If missing, the type
is inferred from the type of `value`.
name: A name for this operation (optional).

Returns:
`(output_false, output_true)`: If `pred` is true, data will be forwarded
to `output_true`, otherwise it goes to `output_false`.


def __init__(self, latent_size, hidden_size):

    super(EncoderStatic, self).__init__()
    self.latent_size = latent_size
    self.hidden_size = hidden_size
    self.bilstm = tf.keras.layers.Bidirectional(
        tf.keras.layers.LSTM(hidden_size),
        merge_mode="sum")
    self.output_layer = tf.keras.layers.Dense(2*latent_size)

Constructs an encoder for `f`.

Args:
latent_size: An integer corresponding to the dimensionality of the
distribution.
hidden_size: Dimensionality of the LSTM, RNN, and affine function
parameters.


def show_rules(cls, *names, attr=None):

        from qnet.printing import srepr
        try:
            if attr is None:
                attr = cls._rules_attr()
            rules = getattr(cls, attr)
        except TypeError:
            rules = {}
        for (name, rule) in rules.items():
            if len(names) > 0 and name not in names:
                continue
            pat, repl = rule
            print(name)
            print("    PATTERN:")
            print(textwrap.indent(
                textwrap.dedent(srepr(pat, indented=True)),
                prefix=" "*8))
            print("    REPLACEMENT:")
            print(textwrap.indent(
                textwrap.dedent(inspect.getsource(repl).rstrip()),
                prefix=" "*8))

Print algebraic rules used by :class:`create`

Print a summary of the algebraic rules with the given names, or all
rules if not names a given.

Args:
names (str): Names of rules to show
attr (None or str): Name of the class attribute from which to get
the rules. Cf. :meth:`add_rule`.

Raises:
AttributeError: If invalid `attr`


class Wav2Vec2DecoderWithLMOutput(ModelOutput):
    text: Union[List[List[str]], List[str], str]
    logit_score: Union[List[List[float]], List[float], float] = None
    lm_score: Union[List[List[float]], List[float], float] = None
    word_offsets: Union[List[List[ListOfDict]], List[ListOfDict], ListOfDict] = None

Output type of [`Wav2Vec2DecoderWithLM`], with transcription.

Args:
text (list of `str` or `str`):
Decoded logits in text from. Usually the speech transcription.
logit_score (list of `float` or `float`):
Total logit score of the beams associated with produced text.
lm_score (list of `float`):
Fused lm_score of the beams associated with produced text.
word_offsets (list of `List[Dict[str, Union[int, str]]]` or `List[Dict[str, Union[int, str]]]`):
Offsets of the decoded words. In combination with sampling rate and model downsampling rate word offsets
can be used to compute time stamps for each word.


def start_tpot(automated_run, session, path):
    module = functions.import_string_code_as_module(automated_run.source)
    extraction = session.query(models.Extraction).first()
    (X, y) = extraction.return_train_dataset()
    tpot_learner = module.tpot_learner
    tpot_learner.fit(X, y)
    temp_filename = os.path.join(path, 'tpot-temp-export-{}'.format(os.getpid()))
    tpot_learner.export(temp_filename)
    with open(temp_filename) as f:
        base_learner_source = f.read()
    base_learner_source = (constants.tpot_learner_docstring + base_learner_source)
    try:
        os.remove(temp_filename)
    except OSError:
        pass
    blo = models.BaseLearnerOrigin(source=base_learner_source, name='TPOT Learner', meta_feature_generator='predict')
    session.add(blo)
    session.commit()

Starts a TPOT automated run that exports directly to base learner setup

Args:
automated_run (xcessiv.models.AutomatedRun): Automated run object

session: Valid SQLAlchemy session

path (str, unicode): Path to project folder


def set_style(self, style):

        if style is not None:
            try:
                self.style.update(style)
            except ValueError:
                for s in style.split(';'):
                    k, v = s.split(':', 1)
                    self.style[k.strip()] = v.strip()

Allows to set style properties for the widget.
Args:
style (str or dict): The style property dictionary or json string.


def HasBalance(self, assetId):
    for (key, fixed8) in self.Balances.items():
        if (key == assetId):
            return True
    return False

Flag indicating if the asset has a balance.

Args:
assetId (UInt256):

Returns:
bool: True if a balance is present. False otherwise.


def iter_variants_by_names(self, names):

        for name in names:
            for result in self.get_variant_by_name(name):
                yield result

Iterates over the genotypes for variants using a list of names.

Args:
names (list): The list of names for variant extraction.


def authorize(self, scheme, **params):

        if scheme not in self.schemes:
            return False

        for field, value in iteritems(params):
            setattr(self, field, value)
            if field in self.schemes[scheme][u'params'].keys() and value:
                self.schemes[scheme][u'params'][field] = value

        return True

Store credentials required to satisfy a given auth scheme.

Args:
scheme (str): The name of the Authentication scheme.
**params: parameters for the specified scheme.

Returns:
True if parameters are set successfully (note that this doesn't mean
the credentials are valid)
False if the scheme specified is not supported


def separate_words(text, acronyms=None):

    words, _case, _sep = case_parse.parse_case(text, acronyms, preserve_case=True)
    return ' '.join(words)

Return text in "seperate words" style.

Args:
text: input string to convert case
detect_acronyms: should attempt to detect acronyms
acronyms: a list of acronyms to detect

>>> separate_words("HELLO_WORLD")
'HELLO WORLD'
>>> separate_words("helloHTMLWorld", True, ["HTML"])
'hello HTML World'


async def _sync_all_conversations(client):
    conv_states = []
    sync_timestamp = None
    request = hangouts_pb2.SyncRecentConversationsRequest(request_header=client.get_request_header(), max_conversations=CONVERSATIONS_PER_REQUEST, max_events_per_conversation=1, sync_filter=[hangouts_pb2.SYNC_FILTER_INBOX, hangouts_pb2.SYNC_FILTER_ARCHIVED])
    for _ in range(MAX_CONVERSATION_PAGES):
        logger.info('Requesting conversations page %s', request.last_event_timestamp)
        response = (await client.sync_recent_conversations(request))
        conv_states = (list(response.conversation_state) + conv_states)
        sync_timestamp = parsers.from_timestamp(response.response_header.current_server_time)
        if (response.continuation_end_timestamp == 0):
            logger.info('Reached final conversations page')
            break
        else:
            request.last_event_timestamp = response.continuation_end_timestamp
    else:
        logger.warning('Exceeded maximum number of conversation pages')
    logger.info('Synced %s total conversations', len(conv_states))
    return (conv_states, sync_timestamp)

Sync all conversations by making paginated requests.

Conversations are ordered by ascending sort timestamp.

Args:
client (Client): Connected client.

Raises:
NetworkError: If the requests fail.

Returns:
tuple of list of ``ConversationState`` messages and sync timestamp


def from_backbone_config(cls, backbone_config: PretrainedConfig, **kwargs):
    return cls(backbone_config=backbone_config, **kwargs)

Instantiate a [`DetrConfig`] (or a derived class) from a pre-trained backbone model configuration.

Args:
backbone_config ([`PretrainedConfig`]):
The backbone configuration.
Returns:
[`DetrConfig`]: An instance of a configuration object


def gen_subject_info_tree(subject_info_pyxb, authn_subj, include_duplicates=False):

    class State():
        'self.'
        pass
    state = State()
    state.subject_info_pyxb = subject_info_pyxb
    state.include_duplicates = include_duplicates
    state.visited_set = set()
    state.tree = SubjectInfoNode('Root', TYPE_NODE_TAG)
    _add_subject(state, state.tree, authn_subj)
    symbolic_node = state.tree.add_child('Symbolic', TYPE_NODE_TAG)
    _add_subject(state, symbolic_node, d1_common.const.SUBJECT_AUTHENTICATED)
    _trim_tree(state)
    return state.tree

Convert the flat, self referential lists in the SubjectInfo to a tree structure.

Args:
subject_info_pyxb: SubjectInfo PyXB object

authn_subj: str
The authenticated subject that becomes the root subject in the tree of
subjects built from the SubjectInfo.

Only subjects that are authenticated by a direct or indirect connection to
this subject are included in the tree.

include_duplicates:
Include branches of the tree that contain subjects that have already been
included via other branches.

If the tree is intended for rendering, including the duplicates will
provide a more complete view of the SubjectInfo.

Returns:
SubjectInfoNode : Tree of nodes holding information about subjects that are
directly or indirectly connected to the authenticated subject in the root.


def _apply_conv(self, inputs, w):
    outputs = tf.nn.convolution(inputs, w, strides=self._stride, padding=self._conv_op_padding, dilation_rate=self._rate, data_format=self._data_format)
    return outputs

Apply a convolution operation on `inputs` using variable `w`.

Args:
inputs: A Tensor of shape `data_format` and of type `tf.float16`,
`tf.bfloat16` or `tf.float32`.
w: A weight matrix of the same type as `inputs`.

Returns:
outputs: The result of the convolution operation on `inputs`.


def __init__(self, embedding_shape, initializer, weight_collections=None, trainable=True, name=None, **kwargs):
    super(_EmbeddingColumnLayer, self).__init__(trainable=trainable, name=name, **kwargs)
    self._embedding_shape = embedding_shape
    self._initializer = initializer
    self._weight_collections = weight_collections

Constructor.

Args:
embedding_shape: Shape of the embedding variable used for lookup.
initializer: A variable initializer function to be used in embedding
variable initialization.
weight_collections: A list of collection names to which the Variable will
be added. Note that, variables will also be added to collections
`tf.GraphKeys.GLOBAL_VARIABLES` and `ops.GraphKeys.MODEL_VARIABLES`.
trainable: If `True` also add the variable to the graph collection
`GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`).
name: Name of the layer
**kwargs: keyword named properties.


def gene_filter(self, query, mongo_query):

        LOG.debug('Adding panel and genes-related parameters to the query')

        gene_query = []

        if query.get('hgnc_symbols') and query.get('gene_panels'):
            gene_query.append({'hgnc_symbols': {'$in': query['hgnc_symbols']}})
            gene_query.append({'panels': {'$in': query['gene_panels']}})
            mongo_query['$or']=gene_query
        else:
            if query.get('hgnc_symbols'):
                hgnc_symbols = query['hgnc_symbols']
                mongo_query['hgnc_symbols'] = {'$in': hgnc_symbols}
                LOG.debug("Adding hgnc_symbols: %s to query" %
                             ', '.join(hgnc_symbols))

            if query.get('gene_panels'):
                gene_panels = query['gene_panels']
                mongo_query['panels'] = {'$in': gene_panels}

        return gene_query

Adds gene-related filters to the query object

Args:
query(dict): a dictionary of query filters specified by the users
mongo_query(dict): the query that is going to be submitted to the database

Returns:
mongo_query(dict): returned object contains gene and panel-related filters


async def shuffle_participants(self):
    res = (await self.connection('POST', 'tournaments/{}/participants/randomize'.format(self._id)))
    self._refresh_participants_from_json(res)

Shuffle participants' seeds

|methcoro|

Note:
|from_api| Randomize seeds among participants. Only applicable before a tournament has started.

Raises:
APIException


def get_available_host_port():
    logging.warning('The method mobly.utils.get_available_host_port is deprecated because it is unreliable. Pass "tcp:0" to adb forward instead.')
    from mobly.controllers.android_device_lib import adb
    port = portpicker.pick_unused_port()
    if not adb.is_adb_available():
        return port
    for _ in range(MAX_PORT_ALLOCATION_RETRY):
        if port not in adb.list_occupied_adb_ports():
            return port
        port = portpicker.pick_unused_port()
    raise Error('Failed to find available port after {} retries'.format(MAX_PORT_ALLOCATION_RETRY))

Gets a host port number available for adb forward.

DEPRECATED: This method is unreliable. Pass `tcp:0` to adb forward instead.

Returns:
An integer representing a port number on the host available for adb
forward.

Raises:
Error: when no port is found after MAX_PORT_ALLOCATION_RETRY times.


def add_args(self, args):
    for (key, value) in vars(args).items():
        if (value is not None):
            setattr(self, key.upper(), value)

Add the args

Args:
args (namespace): The commandline args


def get_message(self, block=False, timeout=None):

        try:
            message = self._inbox.get(block=block, timeout=timeout)
            return message
        except Exception:
            return None

Removes and returns a RTMMessage from self._inbox

Args:
block(bool): if True block until a RTMMessage is available,
else it will return None when self._inbox is empty
timeout(int): it blocks at most timeout seconds

Returns:
RTMMessage if self._inbox is not empty, else None


def get_hash(self, handle):

        response = self.open_url(url=handle, suffix='.hash')
        try:
            return response.read()
        finally:
            response.close()

Get the associated hash for the given handle, the hash file must
exist (``handle + '.hash'``).

Args:
handle (str): Path to the template to get the hash from

Returns:
str: Hash for the given handle


def top_1(x, reduced_dim, dtype=tf.int32, name=None):
    reduced_dim = convert_to_dimension(reduced_dim)
    with tf.name_scope(name, default_name='top_1'):
        max_val = reduce_max(x, reduced_dim=reduced_dim)
        is_max = to_float(equal(x, max_val))
        pos = mtf_range(x.mesh, reduced_dim, tf.float32)
        ret = reduce_max((is_max * pos), reduced_dim=reduced_dim)
        ret = cast(ret, dtype)
        return (ret, max_val)

Argmax and Max.

Args:
x: a Tensor
reduced_dim: a Dimension in x.shape.dims
dtype: a tf.dtype (for the output)
name: an optional string
Returns:
indices: a Tensor with given dtype
values: optional Tensor equal to mtf.reduce_max(x, reduced_dim=reduced_dim)


def from_json(cls, json_data):

    return cls(user=json_data['client_email'],
               keydata=json_data['private_key'],
               token_uri=json_data['token_uri'])

Create an uploader given (parsed) JSON data.

Note that this is a JSON-formatted key file downloaded from Google when
the service account key is created, *NOT* a json-encoded
oauth2client.client.SignedJwtAssertionCredentials object.

Args:
json_data: Dict containing the loaded JSON key data.

Returns:
a MfgInspectorCallback with credentials.


def _step(self, actions):
    self.assert_common_preconditions()
    assert (len(actions) == len(self._envs))
    observations = []
    rewards = []
    dones = []
    infos = []
    for (env, action) in zip(self._envs, actions):
        (observation, reward, done, info) = env.step(action)
        observations.append(observation)
        rewards.append(reward)
        dones.append(done)
        infos.append(info)
    return tuple(map(np.stack, [observations, rewards, dones, infos]))

Takes a step in all environments, shouldn't pre-process or record.

Subclasses should override this to do the actual step if something other
than the default implementation is desired.

Args:
actions: (np.ndarray) with first dimension equal to the batch size.

Returns:
a tuple of stacked raw observations, raw rewards, dones and infos.


def make_prior(num_topics, initial_value):

  def _softplus_inverse(x):
    return np.log(np.expm1(x))

  logit_concentration = tf.compat.v1.get_variable(
      "logit_concentration",
      shape=[1, num_topics],
      initializer=tf.compat.v1.initializers.constant(
          _softplus_inverse(initial_value)))
  concentration = _clip_dirichlet_parameters(
      tf.nn.softplus(logit_concentration))

  def prior():
    return tfd.Dirichlet(concentration=concentration,
                         name="topics_prior")

  prior_variables = [logit_concentration]

  return prior, prior_variables

Create the prior distribution.

Args:
num_topics: Number of topics.
initial_value: The starting value for the prior parameters.

Returns:
prior: A `callable` that returns a `tf.distribution.Distribution`
instance, the prior distribution.
prior_variables: A `list` of `Variable` objects, the trainable parameters
of the prior.


def add_messages(self, validation):
    if (not isinstance(validation, Validation)):
        raise TypeError('Argument must be of type Validation')
    self.messages.extend(validation.messages)

Adds all the messages in the specified `Validation` object to this instance's
messages array.

Args:
validation (Validation): An object containing the messages to add to this instance's messages.


def get_public_tokens(self):

        r = self.remote_utils.get_url(self.url() + "public_tokens/")
        return r.json()

Get a list of public tokens available on this server.

Arguments:
None

Returns:
str[]: list of public tokens