def add(self, op1, op2, operator_name, hints=None):
    updated_hints = _infer_hints_allowing_override(op1, op2, hints)
    if operator_name is None:
        operator_name = 'Add/' + op1.name + '__' + op2.name + '/'
    scope_name = self.name
    if scope_name.startswith('_'):
        scope_name = scope_name[1:]
    with ops.name_scope(scope_name):
        return self._add(op1, op2, operator_name, updated_hints)

Return new `LinearOperator` acting like `op1 + op2`.

Args:
op1:  `LinearOperator`
op2:  `LinearOperator`, with `shape` and `dtype` such that adding to
`op1` is allowed.
operator_name:  `String` name to give to returned `LinearOperator`
hints:  `_Hints` object.  Returned `LinearOperator` will be created with
these hints.

Returns:
`LinearOperator`


def tokenize(self, text):
    text = _replace_html_entities(text)
    if self.strip_handles:
        text = remove_handles(text)
    if self.reduce_len:
        text = reduce_lengthening(text)
    safe_text = HANG_RE.sub('\\1\\1\\1', text)
    words = WORD_RE.findall(safe_text)
    if not self.preserve_case:
        words = [x if EMOTICON_RE.search(x) else x.lower() for x in words]
    return words

Args:
text: str

Returns: list(str) A tokenized list of strings; concatenating this list returns the original string if
`preserve_case=False`


def __add__(self, other: Self | PartProcessor) -> _ChainProcessor:
    if isinstance(other, PartProcessor):
        return _ChainProcessor([self.call, other.to_processor().call])
    elif isinstance(other, _ChainProcessor):
        return _ChainProcessor([self.call] + other._processor_list)
    else:
        return _ChainProcessor([self.call, other.call])

Adds `other` to this processor: self + other.

Args:
other: a processor to add to `self`.

Returns:
The chain of this process with `other`.


def from_text_vision_configs(cls, text_config: GroupViTTextConfig, vision_config: GroupViTVisionConfig, **kwargs):
    return cls(text_config=text_config.to_dict(), vision_config=vision_config.to_dict(), **kwargs)

Instantiate a [`GroupViTConfig`] (or a derived class) from groupvit text model configuration and groupvit
vision model configuration.

Returns:
[`GroupViTConfig`]: An instance of a configuration object


def approve(self, access_level=gitlab.DEVELOPER_ACCESS, **kwargs):
    path = ('%s/%s/approve' % (self.manager.path, self.id))
    data = {'access_level': access_level}
    server_data = self.manager.gitlab.http_put(path, post_data=data, **kwargs)
    self._update_attrs(server_data)

Approve an access request.

Args:
access_level (int): The access level for the user
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabUpdateError: If the server fails to perform the request


def _SigSegvHandler(self, signal_number, stack_frame):
    self._OnCriticalError()
    if (self._original_sigsegv_handler is not None):
        signal.signal(signal.SIGSEGV, self._original_sigsegv_handler)
        os.kill(self._pid, signal.SIGSEGV)

Signal handler for the SIGSEGV signal.

Args:
signal_number (int): numeric representation of the signal.
stack_frame (frame): current stack frame or None.


def dequantize_higgs(model, current_key_name=None):
    with torch.no_grad():
        for name, module in model.named_children():
            if current_key_name is None:
                current_key_name = []
            current_key_name.append(name)
            if isinstance(module, HiggsLinear):
                in_features = module.in_features
                out_features = module.out_features
                model._modules[name] = torch.nn.Linear(in_features, out_features, bias=module.bias is not None, device=module.scales.device, dtype=module.scales.dtype)
                model._modules[name].weight.data = module(torch.eye(in_features, device=module.scales.device, dtype=module.scales.dtype)).T.contiguous()
            if len(list(module.children())) > 0:
                _ = dequantize_higgs(module, current_key_name=current_key_name)
            current_key_name.pop(-1)
        return model

Dequantizes the HiggsLinear layers in the given model by replacing them with standard torch.nn.Linear layers.
Args:
model (torch.nn.Module): The model containing HiggsLinear layers to be dequantized.
current_key_name (list, optional): A list to keep track of the current module names during recursion. Defaults to None.
Returns:
torch.nn.Module: The model with HiggsLinear layers replaced by torch.nn.Linear layers.


def md5sum( string ):

    h = hashlib.new( 'md5' )
    h.update( string.encode( 'utf-8' ) )
    return h.hexdigest()

Generate the md5 checksum for a string

Args:
string (Str): The string to be checksummed.

Returns:
(Str): The hex checksum.


def verify_sc_url(url: str) -> bool:
    parsed = urlsplit(url)
    scheme: str = parsed.scheme
    netloc: str = parsed.netloc
    path: str = parsed.path
    try:
        port = parsed.port
    except ValueError:
        port = None
    result = ((scheme.lower() == 'https') and (netloc.lower().split(':')[0] == 's3.amazonaws.com') and path.startswith('/echo.api/') and ((port == 443) or (port is None)))
    return result

Verify signature certificate URL against Amazon Alexa requirements.

Each call of Agent passes incoming utterances batch through skills filter,
agent skills, skills processor. Batch of dialog IDs can be provided, in
other case utterances indexes in incoming batch are used as dialog IDs.

Args:
url: Signature certificate URL from SignatureCertChainUrl HTTP header.

Returns:
result: True if verification was successful, False if not.


def __init__(self, pub_key=None):

        if pub_key is not None and type(pub_key) is not EllipticCurve.ECPoint:
            raise Exception("Pubkey must be ECPoint Instance")

        self.PublicKey = pub_key

Create an instance.

Args:
pub_key (EllipticCurve.ECPoint):

Raises:
Exception: if `pub_key` is not a valid ECPoint.


def experimental_set_strategy(strategy):
    old_scope = ops.get_default_graph()._global_distribute_strategy_scope
    if old_scope is not None:
        old_scope.__exit__(None, None, None)
        ops.get_default_graph()._global_distribute_strategy_scope = None
    if has_strategy():
        raise RuntimeError('Must not be called inside a `tf.distribute.Strategy` scope.')
    if strategy is not None:
        new_scope = strategy.scope()
        new_scope.__enter__()
        ops.get_default_graph()._global_distribute_strategy_scope = new_scope

Set a `tf.distribute.Strategy` as current without `with strategy.scope()`.

```
tf.distribute.experimental_set_strategy(strategy1)
f()
tf.distribute.experimental_set_strategy(strategy2)
g()
tf.distribute.experimental_set_strategy(None)
h()
```

is equivalent to:

```
with strategy1.scope():
f()
with strategy2.scope():
g()
h()
```

In general, you should use the `with strategy.scope():` API, but this
alternative may be convenient in notebooks where you would have to put
each cell in a `with strategy.scope():` block.

Note: This should only be called outside of any TensorFlow scope to
avoid improper nesting.

Args:
strategy: A `tf.distribute.Strategy` object or None.

Raises:
RuntimeError: If called inside a `with strategy.scope():`.


def as_dataframe(self, pattern='*', max_rows=None):

    data = []
    for i, metric in enumerate(self.list(pattern)):
      if max_rows is not None and i >= max_rows:
        break
      labels = ', '. join([l.key for l in metric.labels])
      data.append([
          metric.type, metric.display_name, metric.metric_kind,
          metric.value_type, metric.unit, labels])

    return pandas.DataFrame(data, columns=self._DISPLAY_HEADERS)

Creates a pandas dataframe from the descriptors that match the filters.

Args:
pattern: An optional pattern to further filter the descriptors. This can
include Unix shell-style wildcards. E.g. ``"compute*"``,
``"*/cpu/load_??m"``.
max_rows: The maximum number of descriptors to return. If None, return
all.

Returns:
A pandas dataframe containing matching metric descriptors.


def bulk_restore(self, filename_tensor, saveables, preferred_shard, restore_sequentially):
    del restore_sequentially
    all_tensors = []
    for saveable in saveables:
        if saveable.device:
            device = saveable_object_util.set_cpu0(saveable.device)
        else:
            device = None
        with ops.device(device):
            all_tensors.extend(self.restore_op(filename_tensor, saveable, preferred_shard))
    return all_tensors

Restore all tensors contained in saveables.

By default, this issues separate calls to `restore_op` for each saveable.
Subclasses may override to load multiple saveables in a single call.

Args:
filename_tensor: String Tensor.
saveables: List of BaseSaverBuilder.SaveableObject objects.
preferred_shard: Int.  Shard to open first when loading a sharded file.
restore_sequentially: Unused.  Bool.  If true, each restore is sequential.

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


def _remove_tree(self, tree, parent=None):

        for sub_tree in tree.sub_trees:
            self._remove_tree(sub_tree, parent=tree)

        for index in tree.indexes:
            if not getattr(tree, index):
                continue

            self._remove_from(
                getattr(self, index + "_db"),
                getattr(tree, index),
                tree,
            )

        if parent:
            self._remove_from(self.parent_db, tree.path, parent)

        self.zeo.pack()

Really remove the tree identified by `tree` instance from all indexes
from database.

Args:
tree (obj): :class:`.Tree` instance.
parent (obj, default None): Reference to parent.


def extract_random_video_patch(videos, num_frames=(- 1)):
    if (num_frames == (- 1)):
        return videos
    (batch_size, num_total_frames, h, w, c) = common_layers.shape_list(videos)
    if (num_total_frames < num_frames):
        raise ValueError(('Expected num_frames <= %d, got %d' % (num_total_frames, num_frames)))
    frame_start = tf.random_uniform(shape=(batch_size,), minval=0, maxval=((num_total_frames - num_frames) + 1), dtype=tf.int32)
    range_inds = tf.expand_dims(tf.range(num_frames), axis=0)
    frame_inds = (range_inds + tf.expand_dims(frame_start, axis=1))
    frame_inds = tf.reshape(frame_inds, [(- 1)])
    batch_inds = tf.expand_dims(tf.range(batch_size), axis=1)
    batch_inds = tf.tile(batch_inds, [1, num_frames])
    batch_inds = tf.reshape(batch_inds, [(- 1)])
    gather_inds = tf.stack((batch_inds, frame_inds), axis=1)
    video_patches = tf.gather_nd(videos, gather_inds)
    return tf.reshape(video_patches, (batch_size, num_frames, h, w, c))

For every video, extract a random consecutive patch of num_frames.

Args:
videos: 5-D Tensor, (NTHWC)
num_frames: Integer, if -1 then the entire video is returned.
Returns:
video_patch: 5-D Tensor, (NTHWC) with T = num_frames.
Raises:
ValueError: If num_frames is greater than the number of total frames in
the video.


def _expression_to_sql(expression, node, context):

    _expression_transformers = {
        expressions.LocalField: _transform_local_field_to_expression,
        expressions.Variable: _transform_variable_to_expression,
        expressions.Literal: _transform_literal_to_expression,
        expressions.BinaryComposition: _transform_binary_composition_to_expression,
    }
    expression_type = type(expression)
    if expression_type not in _expression_transformers:
        raise NotImplementedError(
            u'Unsupported compiler expression "{}" of type "{}" cannot be converted to SQL '
            u'expression.'.format(expression, type(expression)))
    return _expression_transformers[expression_type](expression, node, context)

Recursively transform a Filter block predicate to its SQLAlchemy expression representation.

Args:
expression: expression, the compiler expression to transform.
node: SqlNode, the SqlNode the expression applies to.
context: CompilationContext, global compilation state and metadata.

Returns:
Expression, SQLAlchemy Expression equivalent to the passed compiler expression.


def lookup_prefix(self, prefix, n):
    commands = [cmd for cmd in self._commands if cmd.startswith(prefix)]
    return commands[-n:]

Look up the n most recent commands that starts with prefix.

Args:
prefix: The prefix to lookup.
n: Number of most recent commands to look up.

Returns:
A list of n most recent commands that have the specified prefix, or all
available most recent commands that have the prefix, if n exceeds the
number of history commands with the prefix.


def ping(self, endpoint=''):

        r = requests.get(self.url() + "/" + endpoint)
        return r.status_code

Ping the server to make sure that you can access the base URL.

Arguments:
None
Returns:
`boolean` Successful access of server (or status code)


def Gamma(cls,
            shape: 'TensorFluent',
            scale: 'TensorFluent',
            batch_size: Optional[int] = None) -> Tuple[Distribution, 'TensorFluent']:

        if shape.scope != scale.scope:
            raise ValueError('Gamma distribution: parameters must have same scope!')
        concentration = shape.tensor
        rate = 1 / scale.tensor
        dist = tf.distributions.Gamma(concentration, rate)
        batch = shape.batch or scale.batch
        if not batch and batch_size is not None:
            t = dist.sample(batch_size)
            batch = True
        else:
            t = dist.sample()
        scope = shape.scope.as_list()
        return (dist, TensorFluent(t, scope, batch=batch))

Returns a TensorFluent for the Gamma sampling op with given shape and scale parameters.

Args:
shape: The shape parameter of the Gamma distribution.
scale: The scale parameter of the Gamma distribution.
batch_size: The size of the batch (optional).

Returns:
The Gamma distribution and a TensorFluent sample drawn from the distribution.

Raises:
ValueError: If parameters do not have the same scope.


def _fluent_params(self, fluents, ordering) -> FluentParamsList:
    variables = []
    for fluent_id in ordering:
        fluent = fluents[fluent_id]
        param_types = fluent.param_types
        objects = ()
        names = []
        if (param_types is None):
            names = [fluent.name]
        else:
            objects = tuple((self.object_table[ptype]['objects'] for ptype in param_types))
            for values in itertools.product(*objects):
                values = ','.join(values)
                var_name = '{}({})'.format(fluent.name, values)
                names.append(var_name)
        variables.append((fluent_id, names))
    return tuple(variables)

Returns the instantiated `fluents` for the given `ordering`.

For each fluent in `fluents`, it instantiates each parameter
type w.r.t. the contents of the object table.

Returns:
Sequence[Tuple[str, List[str]]]: A tuple of pairs of fluent name
and a list of instantiated fluents represented as strings.


def rm(path):

    if path and os.path.exists(path):
        if os.path.isdir(path):
            shutil.rmtree(path)
        else:
            os.remove(path)

Equivalent to rm -rf.

Make sure ``path`` doesn't exist after this call.  If it's a dir,
shutil.rmtree(); if it's a file, os.remove(); if it doesn't exist,
ignore.

Args:
path (str): the path to nuke.


def check_local_install(ctx, version, ext, server='local'):
    here = Path(ctx.releaser.here).resolve()
    dist_dir = (here / 'dist')
    all_files = list(dist_dir.glob('*.{}'.format(ext)))
    the_file = all_files[0]
    for f in all_files[1:]:
        if (f.stat().st_mtime > the_file.stat().st_mtime):
            the_file = f
    environment = 'env-{}-{}-{}'.format(version, ext, server)
    if (server == 'local'):
        pass
    else:
        print('** Uploading to server **')
        cmd = 'twine upload {}'.format(the_file)
        if (server != 'pypi'):
            cmd = (cmd + ' -r {}'.format(server))
        result = invoke.run(cmd, warn=True)
        if result.failed:
            print(textwrap.fill("[{}ERROR{}] Something broke trying to upload your package. This will be the case if you have already uploaded it before. To upload again, use a different version number (or a different build by including a '+' suffix to your version number).".format(ERROR_COLOR, RESET_COLOR), width=(text.get_terminal_size().columns - 1), subsequent_indent=(' ' * 8)))
    if ((here / 'env') / environment).exists():
        shutil.rmtree((('env' + os.sep) + environment))
    invoke.run('python -m venv env{}{}'.format(os.sep, environment))
    other_dependencies(ctx, server, environment)
    if (server == 'local'):
        result = invoke.run('env{0}{1}{0}Scripts{0}pip{2} install {3} --no-cache'.format(os.sep, environment, '.exe', the_file), hide=True)
    else:
        result = invoke.run('env{0}{1}{0}Scripts{0}pip{2} install -i {3} {4}=={5} --no-cache'.format(os.sep, environment, '.exe', server_url(server), ctx.releaser.module_name, version), hide=True)
        if result.failed:
            print('[{}ERROR{}] Something broke trying to install your package.'.format(ERROR_COLOR, RESET_COLOR))
            print(result.stderr)
            sys.exit(1)
    print('** Test version of installed package **')
    result = invoke.run('env{0}{1}{0}Scripts{0}python{2} -c exec()'.format(os.sep, environment, '.exe', ctx.releaser.module_name.strip()))
    test_version = result.stdout.strip()
    if (Version(test_version) == version):
        results = '{}{} install {} works!{}'.format(GOOD_COLOR, server, ext, RESET_COLOR)
    else:
        results = '{}{} install {} broken{}'.format(ERROR_COLOR, server, ext, RESET_COLOR)
    print(results)
    return results

Upload and install works?

Uploads a distribution to PyPI, and then tests to see if I can download and
install it.

Returns:
str: string summazing operation


def run(self, data, max_epochs=1):
    self.state = State(dataloader=data, epoch=0, max_epochs=max_epochs, metrics={})
    try:
        self._logger.info('Engine run starting with max_epochs={}.'.format(max_epochs))
        start_time = time.time()
        self._fire_event(Events.STARTED)
        while ((self.state.epoch < max_epochs) and (not self.should_terminate)):
            self.state.epoch += 1
            self._fire_event(Events.EPOCH_STARTED)
            (hours, mins, secs) = self._run_once_on_dataset()
            self._logger.info('Epoch[%s] Complete. Time taken: %02d:%02d:%02d', self.state.epoch, hours, mins, secs)
            if self.should_terminate:
                break
            self._fire_event(Events.EPOCH_COMPLETED)
        self._fire_event(Events.COMPLETED)
        time_taken = (time.time() - start_time)
        (hours, mins, secs) = _to_hours_mins_secs(time_taken)
        self._logger.info(('Engine run complete. Time taken %02d:%02d:%02d' % (hours, mins, secs)))
    except BaseException as e:
        self._logger.error('Engine run is terminating due to exception: %s.', str(e))
        self._handle_exception(e)
    return self.state

Runs the process_function over the passed data.

Args:
data (Iterable): Collection of batches allowing repeated iteration (e.g., list or `DataLoader`).
max_epochs (int, optional): max epochs to run for (default: 1).

Returns:
State: output state.


def __init__(self, keras_model, trackable_obj=None):
    super(TFLiteKerasModelConverterV2, self).__init__()
    self._keras_model = keras_model
    self._trackable_obj = trackable_obj
    self.experimental_lower_to_saved_model = True

Constructor for TFLiteConverter.

Args:
keras_model: tf.Keras.Model.
trackable_obj: tf.AutoTrackable object associated with `funcs`. A
reference to this object needs to be maintained so that Variables do not
get garbage collected since functions have a weak reference to
Variables. This is only required when the tf.AutoTrackable object is not
maintained by the user (e.g. `from_saved_model`).


def git_branch_delete(branch_name):
    if (branch_name not in git.protected_branches()):
        log.info('Deleting branch <33>{}', branch_name)
        shell.run('git branch -d {}'.format(branch_name))

Delete the given branch.

Args:
branch_name (str):
Name of the branch to delete.


def __add__(self, other: 'TensorFluent') -> 'TensorFluent':

        return self._binary_op(self, other, tf.add, tf.float32)

Returns a TensorFluent for the addition arithmetic operator.

Args:
self: The first operand.
other: The second operand.

Returns:
A TensorFluent wrapping the operator's output.


def _UpdateUsers(self, update_users):

    for user, ssh_keys in update_users.items():
      if not user or user in self.invalid_users:
        continue
      configured_keys = self.user_ssh_keys.get(user, [])
      if set(ssh_keys) != set(configured_keys):
        if not self.utils.UpdateUser(user, ssh_keys):
          self.invalid_users.add(user)
        else:
          self.user_ssh_keys[user] = ssh_keys[:]

Provision and update Linux user accounts based on account metadata.

Args:
update_users: dict, authorized users mapped to their public SSH keys.


def is_on_curve(self, point):

        X, Y = point.X, point.Y
        return (
                pow(Y, 2, self.P) - pow(X, 3, self.P) - self.a * X - self.b
            ) % self.P == 0

Checks whether a point is on the curve.

Args:
point (AffinePoint): Point to be checked.

Returns:
bool: True if point is on the curve, False otherwise.


async def tag(self, name: str, repo: str, *, tag: str=None) -> bool:
    params = {'repo': repo}
    if tag:
        params['tag'] = tag
    (await self.docker._query('images/{name}/tag'.format(name=name), 'POST', params=params, headers={'content-type': 'application/json'}))
    return True

Tag the given image so that it becomes part of a repository.

Args:
repo: the repository to tag in
tag: the name for the new tag


def GetDefaultToken(token):

  if token is None:
    token = default_token

  if not isinstance(token, access_control.ACLToken):
    raise access_control.UnauthorizedAccess(
        "Token is not properly specified. It should be an "
        "instance of grr.lib.access_control.ACLToken()")

  return token

Returns the provided token or the default token.

Args:
token: A token or None.

Raises:
access_control.UnauthorizedAccess: no token was provided.


def is30(msg):

    if allzeros(msg):
        return False

    d = hex2bin(data(msg))

    if d[0:8] != '00110000':
        return False

    if d[28:30] == '11':
        return False

    if bin2int(d[15:22]) >= 48:
        return False

    return True

Check if a message is likely to be BDS code 2,0

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

Returns:
bool: True or False


def get_value_from_environment(
        section_name,
        key_name,
        envname_pad=ENVNAME_PAD,
        logger=logging.getLogger('ProsperCommon'),
):

    var_name = '{pad}_{section}__{key}'.format(
        pad=envname_pad,
        section=section_name,
        key=key_name
    )

    logger.debug('var_name=%s', var_name)
    value = getenv(var_name)
    logger.debug('env value=%s', value)

    return value

check environment for key/value pair

Args:
section_name (str): section name
key_name (str): key to look up
envname_pad (str): namespace padding
logger (:obj:`logging.logger`): logging handle

Returns:
str: value in environment


def filter_publication(publication, cache=_CACHE):
    if (cache is None):
        cache = load_cache()
    if (publication._get_hash() in cache):
        return None
    cache.update([publication._get_hash()])
    save_cache(cache)
    return publication

Deduplication function, which compares `publication` with samples stored in
`cache`. If the match NOT is found, `publication` is returned, else None.

Args:
publication (obj): :class:`.Publication` instance.
cache (obj): Cache which is used for lookups.

Returns:
obj/None: Depends whether the object is found in cache or not.


def get_forced_variation(self, experiment_key, user_id):

    if user_id not in self.forced_variation_map:
      self.logger.debug('User "%s" is not in the forced variation map.' % user_id)
      return None

    experiment = self.get_experiment_from_key(experiment_key)
    if not experiment:

      return None

    experiment_to_variation_map = self.forced_variation_map.get(user_id)

    if not experiment_to_variation_map:
      self.logger.debug('No experiment "%s" mapped to user "%s" in the forced variation map.' % (
        experiment_key,
        user_id
      ))
      return None

    variation_id = experiment_to_variation_map.get(experiment.id)
    if variation_id is None:
      self.logger.debug(
        'No variation mapped to experiment "%s" in the forced variation map.' % experiment_key
      )
      return None

    variation = self.get_variation_from_id(experiment_key, variation_id)

    self.logger.debug('Variation "%s" is mapped to experiment "%s" and user "%s" in the forced variation map' % (
      variation.key,
      experiment_key,
      user_id
    ))
    return variation

Gets the forced variation key for the given user and experiment.

Args:
experiment_key: Key for experiment.
user_id: The user ID.

Returns:
The variation which the given user and experiment should be forced into.


def _verify_required_claims_exist(jwt_claims):
    for claim_name in [u'aud', u'exp', u'iss', u'sub']:
        if (claim_name not in jwt_claims):
            raise suppliers.UnauthenticatedException((u'Missing "%s" claim' % claim_name))

Verifies that the required claims exist.

Args:
jwt_claims: the JWT claims to be verified.

Raises:
UnauthenticatedException: if some claim doesn't exist.


def get(cls, option, default_value=None):

        config = cls.__get_instance()
        for name in option.split(':'):
            if not name:
                raise Exception('Incorrect value in path (maybe double `:` or empty path)')
            if name not in config:
                return default_value
            config = config[name]
        return deepcopy(config)

Return value of given option.

If option isn't found - return default_value (None by default).

Args:
- option: string with path to option with `:` separator


def create_iam_resources(env='dev', app='', **_):

    session = boto3.session.Session(profile_name=env)
    client = session.client('iam')

    app_properties = get_properties(env='pipeline')

    generated = get_details(env=env, app=app)
    generated_iam = generated.iam()
    app_details = collections.namedtuple('AppDetails', generated_iam.keys())
    details = app_details(**generated_iam)

    LOG.debug('Application details: %s', details)

    deployment_type = app_properties['type']
    role_trust_template = get_template(
        'infrastructure/iam/trust/{0}_role.json.j2'.format(deployment_type), formats=generated)

    resource_action(
        client,
        action='create_role',
        log_format='Created Role: %(RoleName)s',
        RoleName=details.role,
        AssumeRolePolicyDocument=role_trust_template)
    resource_action(
        client,
        action='create_instance_profile',
        log_format='Created Instance Profile: %(InstanceProfileName)s',
        InstanceProfileName=details.profile)
    attach_profile_to_role(client, role_name=details.role, profile_name=details.profile)

    iam_policy = construct_policy(app=app, group=details.group, env=env, pipeline_settings=app_properties)
    if iam_policy:
        resource_action(
            client,
            action='put_role_policy',
            log_format='Added IAM Policy: %(PolicyName)s',
            RoleName=details.role,
            PolicyName=details.policy,
            PolicyDocument=iam_policy)

    resource_action(client, action='create_user', log_format='Created User: %(UserName)s', UserName=details.user)
    resource_action(client, action='create_group', log_format='Created Group: %(GroupName)s', GroupName=details.group)
    resource_action(
        client,
        action='add_user_to_group',
        log_format='Added User to Group: %(UserName)s -> %(GroupName)s',
        GroupName=details.group,
        UserName=details.user)

    return True

Create the IAM Resources for the application.

Args:
env (str): Deployment environment/account, i.e. dev, stage, prod.
app (str): Spinnaker Application name.

Returns:
True upon successful completion.


def paginate(db_query, items_per_page, offset=0, start_page=1):

    return Paginator(db_query, items_per_page, offset=offset, start_page=start_page)

Instantiates a Paginator instance for database queries.

Args:
db_query: The SQLAlchemy database query to paginate.
items_per_page: The desired number of items per page.
offset: The number of items to skip when paginating.
start_page: The number of the first page when reporting on page numbers.


def __init__(self, conf):
    super(S3FilesSource, self).__init__(conf)
    self._SetDefaults(conf)
    self.s3_client = None

Initialise the S3FilesSource object.

Args:
conf: A dictionary of key/value pairs.

Raises:
RuntimeError: object wasn't initialised with a dict


def assert_keys_exist(self, caller, *keys):

        assert keys, ("*keys parameter must be specified.")
        for key in keys:
            self.assert_key_exists(key, caller)

Assert that context contains keys.

Args:
keys: validates that these keys exists in context
caller: string. calling function or module name - this used to
construct error messages

Raises:
KeyNotInContextError: When key doesn't exist in context.


def message(self, tree, spins, subtheta, auxvars):
    energy_sources = set()
    for (v, children) in tree.items():
        aux = auxvars[v]
        assert all(((u in spins) for u in self._ancestors[v]))

        def energy_contributions():
            (yield subtheta.linear[v])
            for (u, bias) in subtheta.adj[v].items():
                if (u in spins):
                    (yield SpinTimes(spins[u], bias))
        plus_energy = Plus(energy_contributions())
        minus_energy = SpinTimes((- 1), plus_energy)
        if children:
            spins[v] = 1
            plus_energy = Plus(plus_energy, self.message(children, spins, subtheta, auxvars))
            spins[v] = (- 1)
            minus_energy = Plus(minus_energy, self.message(children, spins, subtheta, auxvars))
            del spins[v]
        m = FreshSymbol(REAL)
        ancestor_aux = {(auxvars[u] if (spins[u] > 0) else Not(auxvars[u])) for u in self._ancestors[v]}
        plus_aux = And({aux}.union(ancestor_aux))
        minus_aux = And({Not(aux)}.union(ancestor_aux))
        self.assertions.update({LE(m, plus_energy), LE(m, minus_energy), Implies(plus_aux, GE(m, plus_energy)), Implies(minus_aux, GE(m, minus_energy))})
        energy_sources.add(m)
    return Plus(energy_sources)

Determine the energy of the elimination tree.

Args:
tree (dict): The current elimination tree
spins (dict): The current fixed spins
subtheta (dict): Theta with spins fixed.
auxvars (dict): The auxiliary variables for the given spins.

Returns:
The formula for the energy of the tree.


def __init__(self, dhclient_script=None, dhcp_command=None, debug=False):

    self.dhclient_script = dhclient_script or '/sbin/google-dhclient-script'
    self.dhcp_command = dhcp_command
    facility = logging.handlers.SysLogHandler.LOG_DAEMON
    self.logger = logger.Logger(
        name='network-setup', debug=debug, facility=facility)
    self.distro_utils = distro_utils.Utils(debug=debug)

Constructor.

Args:
dhclient_script: string, the path to a dhclient script used by dhclient.
dhcp_command: string, a command to enable Ethernet interfaces.
debug: bool, True if debug output should write to the console.


def get_open_func(fn, return_fmt=False):
    bgzip = None
    with open(fn, 'rb') as i_file:
        bgzip = (i_file.read(3) == b'\x1f\x8b\x08')
    if (bgzip and (not HAS_BIOPYTHON)):
        raise ValueError('needs BioPython to index a bgzip file')
    open_func = open
    if bgzip:
        open_func = BgzfReader
    try:
        with open_func(fn, 'r') as i_file:
            if bgzip:
                if (not i_file.seekable()):
                    raise ValueError
            pass
    except ValueError:
        raise ValueError('{}: use bgzip for compression...'.format(fn))
    if return_fmt:
        return (bgzip, open_func)
    return open_func

Get the opening function.

Args:
fn (str): the name of the file.
return_fmt (bool): if the file format needs to be returned.

Returns:
tuple: either a tuple containing two elements: a boolean telling if the
format is bgzip, and the opening function.


def make(world_name, gl_version=GL_VERSION.OPENGL4, window_res=None, cam_res=None, verbose=False):
    holodeck_worlds = _get_worlds_map()
    if (world_name not in holodeck_worlds):
        raise HolodeckException('Invalid World Name')
    param_dict = copy(holodeck_worlds[world_name])
    param_dict['start_world'] = True
    param_dict['uuid'] = str(uuid.uuid4())
    param_dict['gl_version'] = gl_version
    param_dict['verbose'] = verbose
    if (window_res is not None):
        param_dict['window_width'] = window_res[0]
        param_dict['window_height'] = window_res[1]
    if (cam_res is not None):
        param_dict['camera_width'] = cam_res[0]
        param_dict['camera_height'] = cam_res[1]
    return HolodeckEnvironment(**param_dict)

Creates a holodeck environment using the supplied world name.

Args:
world_name (str): The name of the world to load as an environment. Must match the name of a world in an
installed package.
gl_version (int, optional): The OpenGL version to use (Linux only). Defaults to GL_VERSION.OPENGL4.
window_res ((int, int), optional): The resolution to load the game window at. Defaults to (512, 512).
cam_res ((int, int), optional): The resolution to load the pixel camera sensors at. Defaults to (256, 256).
verbose (bool): Whether to run in verbose mode. Defaults to False.

Returns:
HolodeckEnvironment: A holodeck environment instantiated with all the settings necessary for the specified
world, and other supplied arguments.


def terminate(self, nowait=False):
    logger.debug('Acquiring lock for service termination')
    with self.lock:
        logger.debug('Terminating service')
        if (not self.listener):
            logger.warning('Service already stopped.')
            return
        self.listener.stop(nowait)
        try:
            if (not nowait):
                self._post_log_batch()
        except Exception:
            if self.error_handler:
                self.error_handler(sys.exc_info())
            else:
                raise
        finally:
            self.queue = None
            self.listener = None

Finalize and stop service

Args:
nowait: set to True to terminate immediately and skip processing
messages still in the queue


def safejoin(base, *elements):
    base = os.path.abspath(base)
    path = os.path.join(base, *elements)
    path = os.path.normpath(path)
    if (not path_is_inside(path, base)):
        raise ValueError('target path is outside of the base path')
    return path

Safely joins paths together.

The result will always be a subdirectory under `base`, otherwise ValueError
is raised.

Args:
base (str): base path
elements (list of strings): path elements to join to base

Returns:
elements joined to base


def remove_perm(self, subj_str, perm_str):

        self._assert_valid_permission(perm_str)
        for perm_str in self._equal_or_higher_perm(perm_str):
            self._perm_dict.setdefault(perm_str, set()).discard(subj_str)

Remove permission from a subject.

Args:
subj_str : str
Subject for which to remove permission(s)

perm_str : str
Permission to remove. Implicitly removes all higher permissions. E.g., ``write``
will also remove ``changePermission`` if previously granted.


def _compute_default_rope_parameters(config: Optional[PretrainedConfig]=None, device: Optional['torch.device']=None, seq_len: Optional[int]=None, **rope_kwargs) -> tuple['torch.Tensor', float]:
    if config is not None and len(rope_kwargs) > 0:
        raise ValueError(f'Unexpected arguments: `**rope_kwargs` and `config` are mutually exclusive in `_compute_default_rope_parameters`, got `rope_kwargs`={rope_kwargs} and `config`={config}')
    if len(rope_kwargs) > 0:
        base = rope_kwargs['base']
        dim = rope_kwargs['dim']
    elif config is not None:
        base = config.rope_theta
        partial_rotary_factor = config.partial_rotary_factor if hasattr(config, 'partial_rotary_factor') else 1.0
        head_dim = getattr(config, 'head_dim', None) or config.hidden_size
        dim = int(head_dim * partial_rotary_factor)
    attention_factor = 1.0
    inv_freq = 1.0 / base ** (torch.arange(0, dim, 2, dtype=torch.int64).to(device=device, dtype=torch.float) / dim)
    return (inv_freq, attention_factor)

Computes the inverse frequencies according to the original RoPE implementation
Args:
config ([`~transformers.PretrainedConfig`]):
The model configuration.
device (`torch.device`):
The device to use for initialization of the inverse frequencies.
seq_len (`int`, *optional*):
The current sequence length. Unused for this type of RoPE.
rope_kwargs (`Dict`, *optional*):
BC compatibility with the previous RoPE class instantiation, will be removed in v4.45.
Returns:
Tuple of (`torch.Tensor`, `float`), containing the inverse frequencies for the RoPE embeddings and the
post-processing scaling factor applied to the computed cos/sin (unused in this type of RoPE).


def from_config(cls, config):
    return cls(**config)

Instantiates an initializer from a configuration dictionary.

Example:

```python
initializer = RandomUniform(-1, 1)
config = initializer.get_config()
initializer = RandomUniform.from_config(config)
```

Args:
config: A Python dictionary. It will typically be the output of
`get_config`.

Returns:
An Initializer instance.


def create_handler(Model, name=None, **kwds):

    async def action_handler(service, action_type, payload, props, notify=True, **kwds):

        if action_type == get_crud_action('create', name or Model):

            try:

                message_props = {}

                if 'correlation_id' in props:

                    message_props['correlation_id'] = props['correlation_id']

                for requirement in Model.required_fields():

                    field_name = requirement.name

                    if not field_name in payload and field_name != 'id':

                        raise ValueError(
                            "Required field not found in payload: %s" %field_name
                        )

                new_model = Model(**payload)

                new_model.save()

                if notify:

                    await service.event_broker.send(
                        payload=ModelSerializer().serialize(new_model),
                        action_type=change_action_status(action_type, success_status()),
                        **message_props
                    )

            except Exception as err:

                if notify:

                    await service.event_broker.send(
                        payload=str(err),
                        action_type=change_action_status(action_type, error_status()),
                        **message_props
                    )

                else:

                    raise err

    return action_handler

This factory returns an action handler that creates a new instance of
the specified model when a create action is recieved, assuming the
action follows nautilus convetions.

Args:
Model (nautilus.BaseModel): The model to create when the action
received.

Returns:
function(action_type, payload): The action handler for this model


def compute_sub_structure(self, sub_structure, tol=1e-3):

        total_energy_matrix = self.total_energy_matrix.copy()

        def find_match(site):
            for test_site in sub_structure:
                frac_diff = abs(np.array(site.frac_coords)
                                - np.array(test_site.frac_coords)) % 1
                frac_diff = [abs(a) < tol or abs(a) > 1 - tol
                             for a in frac_diff]
                if all(frac_diff):
                    return test_site
            return None

        matches = []
        for i, site in enumerate(self._s):
            matching_site = find_match(site)
            if matching_site:
                new_charge = compute_average_oxidation_state(matching_site)
                old_charge = self._oxi_states[i]
                scaling_factor = new_charge / old_charge
                matches.append(matching_site)
            else:
                scaling_factor = 0
            total_energy_matrix[i, :] *= scaling_factor
            total_energy_matrix[:, i] *= scaling_factor

        if len(matches) != len(sub_structure):
            output = ["Missing sites."]
            for site in sub_structure:
                if site not in matches:
                    output.append("unmatched = {}".format(site))
            raise ValueError("\n".join(output))

        return sum(sum(total_energy_matrix))

Gives total ewald energy for an sub structure in the same
lattice. The sub_structure must be a subset of the original
structure, with possible different charges.

Args:
substructure (Structure): Substructure to compute Ewald sum for.
tol (float): Tolerance for site matching in fractional coordinates.

Returns:
Ewald sum of substructure.


def get_backup(self, id_or_uri):
    uri = ((self.BACKUPS_PATH + '/') + extract_id_from_uri(id_or_uri))
    return self._client.get(id_or_uri=uri)

Get the details for the backup from an Artifact Bundle.

Args:
id_or_uri: ID or URI of the Artifact Bundle.

Returns:
Dict: Backup for an Artifacts Bundle.


def _create_min_max_boundaries(
    max_length, min_boundary=_MIN_BOUNDARY, boundary_scale=_BOUNDARY_SCALE):

  bucket_boundaries = []
  x = min_boundary
  while x < max_length:
    bucket_boundaries.append(x)
    x = max(x + 1, int(x * boundary_scale))

  buckets_min = [0] + bucket_boundaries
  buckets_max = bucket_boundaries + [max_length + 1]
  return buckets_min, buckets_max

Create min and max boundary lists up to max_length.

For example, when max_length=24, min_boundary=4 and boundary_scale=2, the
returned values will be:
buckets_min = [0, 4, 8, 16, 24]
buckets_max = [4, 8, 16, 24, 25]

Args:
max_length: The maximum length of example in dataset.
min_boundary: Minimum length in boundary.
boundary_scale: Amount to scale consecutive boundaries in the list.

Returns:
min and max boundary lists


def run(argv=None, save_main_session=True, test_pipeline=None) -> PipelineResult:
    known_args, pipeline_args = parse_known_args(argv)
    pipeline_options = PipelineOptions(pipeline_args)
    pipeline_options.view_as(SetupOptions).save_main_session = save_main_session
    pipeline = test_pipeline
    if not test_pipeline:
        pipeline = beam.Pipeline(options=pipeline_options)
    model_handler = HuggingFacePipelineModelHandler(task=PipelineTask.QuestionAnswering, model=known_args.model_name, load_model_args={'framework': 'pt', 'revision': known_args.revision})
    if not known_args.input:
        text = pipeline | 'CreateSentences' >> beam.Create(['What does Apache Beam do?;Apache Beam enables batch and streaming data processing.', 'What is the capital of France?;The capital of France is Paris .', 'Where was beam summit?;Apache Beam Summit 2023 was in NYC.'])
    else:
        text = pipeline | 'ReadSentences' >> beam.io.ReadFromText(known_args.input)
    processed_text = text | 'PreProcess' >> beam.ParDo(preprocess) | 'SquadExample' >> beam.ParDo(create_squad_example)
    output = processed_text | 'RunInference' >> RunInference(KeyedModelHandler(model_handler)) | 'ProcessOutput' >> beam.ParDo(PostProcessor())
    _ = output | 'WriteOutput' >> beam.io.WriteToText(known_args.output, shard_name_template='', append_trailing_newlines=True)
    result = pipeline.run()
    result.wait_until_finish()
    return result

Args:
argv: Command line arguments defined for this example.
save_main_session: Used for internal testing.
test_pipeline: Used for internal testing.


def cloud_predict(model_name, model_version, data):
    import google.datalab.ml as ml
    if isinstance(data, pd.DataFrame):
        string_buffer = io.StringIO()
        data.to_csv(string_buffer, header=None, index=False)
        input_data = string_buffer.getvalue().split('\n')
        input_data = [line for line in input_data if line]
    else:
        input_data = data
    predictions = ml.ModelVersions(model_name).predict(model_version, input_data)
    df = pd.DataFrame(columns=sorted(predictions[0].keys()))
    for i in range(len(predictions)):
        for (k, v) in predictions[i].iteritems():
            df.loc[(i, k)] = v
    return df

Use Online prediction.

Runs online prediction in the cloud and prints the results to the screen. For
running prediction on a large dataset or saving the results, run
local_batch_prediction or batch_prediction.

Args:
model_name: deployed model name
model_version: depoyed model version
data: List of csv strings or a Pandas DataFrame that match the model schema.

Before using this, the model must be created. This can be done by running
two gcloud commands:
1) gcloud beta ml models create NAME
2) gcloud beta ml versions create VERSION --model NAME \
--origin gs://BUCKET/training_dir/model
or these datalab commands:
1) import google.datalab as datalab
model = datalab.ml.ModelVersions(MODEL_NAME)
model.deploy(version_name=VERSION,
path='gs://BUCKET/training_dir/model')
Note that the model must be on GCS.


def __init__(self,
               skewness=None,
               tailweight=None,
               validate_args=False,
               name="SinhArcsinh"):

    self._graph_parents = []
    self._name = name
    self._validate_args = validate_args
    with self._name_scope("init"):
      tailweight = 1. if tailweight is None else tailweight
      skewness = 0. if skewness is None else skewness
      self._skewness = tf.convert_to_tensor(value=skewness, name="skewness")
      self._tailweight = tf.convert_to_tensor(
          value=tailweight, name="tailweight", dtype=self._skewness.dtype)
      dtype_util.assert_same_float_dtype([self._skewness, self._tailweight])
      if validate_args:
        self._tailweight = distribution_util.with_dependencies([
            assert_util.assert_positive(
                self._tailweight,
                message="Argument tailweight was not positive")
        ], self._tailweight)
    super(SinhArcsinh, self).__init__(
        forward_min_event_ndims=0,
        validate_args=validate_args,
        name=name)

Instantiates the `SinhArcsinh` bijector.

Args:
skewness:  Skewness parameter.  Float-type `Tensor`.  Default is `0`
of type `float32`.
tailweight:  Tailweight parameter.  Positive `Tensor` of same `dtype` as
`skewness` and broadcastable `shape`.  Default is `1` of type `float32`.
validate_args: Python `bool` indicating whether arguments should be
checked for correctness.
name: Python `str` name given to ops managed by this object.


def upload_benchmark_run(self, dataset_name, table_name, run_id):

    expected_file = os.path.join(
        self._logging_dir, logger.BENCHMARK_RUN_LOG_FILE_NAME)
    with tf.gfile.GFile(expected_file) as f:
      benchmark_json = json.load(f)
      benchmark_json["model_id"] = run_id
      table_ref = self._bq_client.dataset(dataset_name).table(table_name)
      errors = self._bq_client.insert_rows_json(table_ref, [benchmark_json])
      if errors:
        tf.logging.error(
            "Failed to upload benchmark info to bigquery: {}".format(errors))

Upload benchmark run information to Bigquery.

Args:
dataset_name: string, the name of bigquery dataset where the data will be
uploaded.
table_name: string, the name of bigquery table under the dataset where
the data will be uploaded.
run_id: string, a unique ID that will be attached to the data, usually
this is a UUID4 format.


def remove_device(self, device, id_override=None, type_override=None):
    object_id = (id_override or device.object_id())
    object_type = (type_override or device.object_type())
    url_string = '{}/{}s/{}'.format(self.BASE_URL, object_type, object_id)
    try:
        arequest = requests.delete(url_string, headers=API_HEADERS)
        if (arequest.status_code == 204):
            return True
        _LOGGER.error('Failed to remove device. Status code: %s', arequest.status_code)
        return False
    except requests.exceptions.RequestException:
        _LOGGER.error('Failed to remove device.')
        return False

Remove a device.

Args:
device (WinkDevice): The device the change is being requested for.
id_override (String, optional): A device ID used to override the
passed in device's ID. Used to make changes on sub-devices.
i.e. Outlet in a Powerstrip. The Parent device's ID.
type_override (String, optional): Used to override the device type
when a device inherits from a device other than WinkDevice.
Returns:
(boolean): True if the device was removed.


def _CheckGitkitError(self, raw_response):
    try:
        response = simplejson.loads(raw_response)
        if ('error' not in response):
            return response
        else:
            error = response['error']
            if ('code' in error):
                code = error['code']
                if str(code).startswith('4'):
                    raise errors.GitkitClientError(error['message'])
                else:
                    raise errors.GitkitServerError(error['message'])
    except simplejson.JSONDecodeError:
        pass
    raise errors.GitkitServerError('null error code from Gitkit server')

Raises error if API invocation failed.

Args:
raw_response: string, the http response.

Raises:
GitkitClientError: if the error code is 4xx.
GitkitServerError: if the response if malformed.

Returns:
Successful response as dict.


def prepare_http_request(self, method_type, params, **kwargs):
    prepared_request = self.session.prepare_request(requests.Request(method=method_type, **params))
    return prepared_request

Prepares the HTTP REQUEST and returns it.

Args:
method_type: The HTTP method type
params: Additional parameters for the HTTP request.
kwargs: Any extra keyword arguements passed into a client method.

returns:
prepared_request: An HTTP request object.


def render_html_report(summary, report_template=None, report_dir=None):

    if not report_template:
        report_template = os.path.join(
            os.path.abspath(os.path.dirname(__file__)),
            "templates",
            "report_template.html"
        )
        logger.log_debug("No html report template specified, use default.")
    else:
        logger.log_info("render with html report template: {}".format(report_template))

    logger.log_info("Start to render Html report ...")

    report_dir = report_dir or os.path.join(os.getcwd(), "reports")
    if not os.path.isdir(report_dir):
        os.makedirs(report_dir)

    start_at_timestamp = int(summary["time"]["start_at"])
    summary["time"]["start_datetime"] = datetime.fromtimestamp(start_at_timestamp).strftime('%Y-%m-%d %H:%M:%S')

    report_path = os.path.join(report_dir, "{}.html".format(start_at_timestamp))

    with io.open(report_template, "r", encoding='utf-8') as fp_r:
        template_content = fp_r.read()
        with io.open(report_path, 'w', encoding='utf-8') as fp_w:
            rendered_content = Template(
                template_content,
                extensions=["jinja2.ext.loopcontrols"]
            ).render(summary)
            fp_w.write(rendered_content)

    logger.log_info("Generated Html report: {}".format(report_path))

    return report_path

render html report with specified report name and template

Args:
report_template (str): specify html report template path
report_dir (str): specify html report save directory


def tf_loss(self, states, internals, reward, update, reference=None):

        prediction = self.predict(states=states, internals=internals, update=update)
        return tf.nn.l2_loss(t=(prediction - reward))

Creates the TensorFlow operations for calculating the L2 loss between predicted
state values and actual rewards.

Args:
states: Dict of state tensors.
internals: List of prior internal state tensors.
reward: Reward tensor.
update: Boolean tensor indicating whether this call happens during an update.
reference: Optional reference tensor(s), in case of a comparative loss.

Returns:
Loss tensor


def take_quality_screenshot(self, screenshot_name):

        self.info_log("Taking a quality screenshot...")

        if self.test_instance._runner_dir:
            _screenshot_name = '%s.png' % string_to_filename(screenshot_name)
            relative_path = os.path.join(
                    self.test_instance._quality_screenshot_relative_dir,
                    _screenshot_name
                )

            full_path = os.path.join(
                    self.test_instance._quality_screenshot_dir,
                    _screenshot_name
                )
            self._driver.save_screenshot(
                full_path
            )

            with DbSessionContext(BROME_CONFIG['database']['mongo_database_name']) as session:
                capabilities = {
                    'browserName': self.capabilities['browserName'],
                    'platform': self.capabilities['platform'],
                    'version': self.capabilities['version']
                }
                quality_screenshot = Testqualityscreenshot()
                quality_screenshot.timestamp = utcnow()
                quality_screenshot.browser_capabilities = capabilities
                quality_screenshot.browser_id = self.get_id()
                quality_screenshot.file_path = relative_path

                quality_screenshot.location = 'local_file_system'
                quality_screenshot.root_path = self.test_instance._runner.root_test_result_dir
                quality_screenshot.extra_data = {}
                quality_screenshot.title = screenshot_name
                quality_screenshot.test_instance_id = self.test_instance._test_instance_id
                quality_screenshot.test_batch_id = self.test_instance._test_batch_id

                session.save(quality_screenshot, safe=True)

            self.debug_log("Quality screenshot taken (%s)" % full_path)

Take a quality screenshot

Use the screenshot_name args when you want to take a
screenshot for reference

Args:
screenshot_name (str) the name of the screenshot


def joinCommissioned(self, strPSKd='threadjpaketest', waitTime=20):

        print '%s call joinCommissioned' % self.port
        cmd = WPANCTL_CMD + 'joiner --start %s %s' %(strPSKd, self.provisioningUrl)
        print cmd
        if self.__sendCommand(cmd)[0] != "Fail":
            if self.__getJoinerState():
                self.__sendCommand(WPANCTL_CMD + 'joiner --attach')
                time.sleep(30)
                return True
            else:
                return False
        else:
            return False

start joiner

Args:
strPSKd: Joiner's PSKd

Returns:
True: successful to start joiner
False: fail to start joiner


def _validate_netconfig(self, conf):

        nets = conf.get('nets', {})
        if len(nets) == 0:

            raise LagoInitException('No networks configured.')

        no_mgmt_dns = [
            name for name, net in nets.iteritems()
            if net.get('management', None) is None and
            (net.get('main_dns') or net.get('dns_domain_name'))
        ]
        if len(no_mgmt_dns) > 0 and len(nets.keys()) > 1:
            raise LagoInitException(
                (
                    'Networks: {0}, misconfigured, they '
                    'are not marked as management, but have '
                    'DNS attributes. DNS is supported '
                    'only in management networks.'
                ).format(','.join(no_mgmt_dns))
            )

        for dom_name, dom_spec in conf['domains'].items():
            mgmts = []
            for nic in dom_spec['nics']:
                net = self._get_net(conf, dom_name, nic)
                if net.get('management', False) is True:
                    mgmts.append(nic['net'])
            if len(mgmts) == 0:
                raise LagoInitException(
                    (
                        'VM {0} has no management network, '
                        'please connect it to '
                        'one.'
                    ).format(dom_name)
                )

            if len(mgmts) > 1:
                raise LagoInitException(
                    (
                        'VM {0} has more than one management '
                        'network: {1}. It should have exactly '
                        'one.'
                    ).format(dom_name, ','.join(mgmts))
                )

Validate network configuration

Args:
conf(dict): spec

Returns:
None

Raises:
:exc:`~lago.utils.LagoInitException`: If a VM has more than
one management network configured, or a network which is not
management has DNS attributes, or a VM is configured with a
none-existence NIC, or a VM has no management network.


def get_calibration_min_max_value(self, calibration_statistics_serialized: bytes, calibration_options_serialized: bytes) -> Optional[tuple[float, float]]:
    statistics = calibration_statistics_pb2.CalibrationStatistics.FromString(calibration_statistics_serialized)
    options = stablehlo_quant_config_pb2.CalibrationOptions.FromString(calibration_options_serialized)
    return _call_and_return_none_on_error(functools.partial(calibration_algorithm.get_min_max_value, statistics, options), error_msg=f'Retrieving calibrated min / max failed. Options: {options}.')

Calculates min and max values from statistics.

Args:
calibration_statistics_serialized: Serialized `CalibrationStatistics`.
This will be the source to calculate min and max values from.
calibration_options_serialized: Serialized `CalibrationOptions`. Specifies
how the min / max should be calculated.

Returns:
(min_value, max_value): Min and max calculated using calib_opts. `None`
upon error.


def final_bearing(self, format='numeric'):

        bearings = []
        for segment in self:
            if len(segment) < 2:
                bearings.append([])
            else:
                bearings.append(segment.final_bearing(format))
        return bearings

Calculate final bearing between locations in segments.

Args:
format (str): Format of the bearing string to return

Returns:
list of list of float: Groups of bearings between points in
segments


def ReleaseObject(self, identifier):
    if (identifier not in self._values):
        raise KeyError('Missing cached object for identifier: {0:s}'.format(identifier))
    cache_value = self._values[identifier]
    if (not cache_value):
        raise RuntimeError('Missing cache value for identifier: {0:s}'.format(identifier))
    cache_value.DecrementReferenceCount()

Releases a cached object based on the identifier.

This method decrements the cache value reference count.

Args:
identifier (str): VFS object identifier.

Raises:
KeyError: if the VFS object is not found in the cache.
RuntimeError: if the cache value is missing.


def new(cls, access_token, environment='prod'):

        request = RequestBuilder \
            .request(environment) \
            .to_service(cls.SERVICE_NAME, cls.SERVICE_VERSION) \
            .throw(
                StorageForbiddenException,
                lambda resp: 'You are forbidden to do this.'
                if resp.status_code == 403 else None
            ) \
            .throw(
                StorageNotFoundException,
                lambda resp: 'The entity is not found'
                if resp.status_code == 404 else None
            ) \
            .throw(
                StorageException,
                lambda resp: 'Server response: {0} - {1}'.format(resp.status_code, resp.text)
                if not resp.ok else None
            )

        authenticated_request = request.with_token(access_token)

        return cls(request, authenticated_request)

Create a new storage service REST client.

Arguments:
environment: The service environment to be used for the client
access_token: The access token used to authenticate with the
service

Returns:
A storage_service.api.ApiClient instance

Example:
>>> storage_client = ApiClient.new(my_access_token)


def serialize_to_transport(self, doc_format='xml', *args, **kwargs):
    return super(ResourceMap, self).serialize(*args, format=doc_format, encoding='utf-8', **kwargs)

Serialize ResourceMap to UTF-8 encoded XML document.

Args:
doc_format: str
One of: ``xml``, ``n3``, ``turtle``, ``nt``, ``pretty-xml``, ``trix``,
``trig`` and ``nquads``.

args and kwargs:
Optional arguments forwarded to rdflib.ConjunctiveGraph.serialize().

Returns:
bytes: UTF-8 encoded XML doc.

Note:
Only the default, "xml", is automatically indexed by DataONE.


def merge_accumulators(self, accumulators, *args, **kwargs):
    raise NotImplementedError(str(self))

Returns the result of merging several accumulators
to a single accumulator value.

Args:
accumulators: the accumulators to merge.
Only the first accumulator may be modified and returned for efficiency;
the other accumulators should not be mutated, because they may be
shared with other code and mutating them could lead to incorrect
results or data corruption.
*args: Additional arguments and side inputs.
**kwargs: Additional arguments and side inputs.


def __init__(self, jsonstr=None, key=None, warnings=None):

        super(Resource, self).__init__()
        self.warnings = warnings
        if jsonstr is not None:
            data = json.loads(json.dumps(jsonstr))
            if key is not None:
                object.__setattr__(self, 'json_data', data[key])
            else:
                object.__setattr__(self, 'json_data', data)

Initialization of the object

Args:
jsonstr (str): a raw JSON string that is returned by a request.
We store all the data in `self.json_data` and use `__getattr__`
and `__setattr__` to make the data accessible like attributes
of the object
key (str): Optional key to use with jsonstr. If `key` exists, we'll
load the data of `jsonstr[key]` instead of the whole `jsonstr`


def get_direct_band_gap(self):
    if self.is_metal():
        return 0.0
    dg = self.get_direct_band_gap_dict()
    return min((v['value'] for v in dg.values()))

Returns the direct band gap.

Returns:
the value of the direct band gap


def resource(self, resource_type):

        try:
            resource = getattr(self.resources, self.safe_rt(resource_type))(self)
        except AttributeError:
            self._resources(True)
            resource = getattr(self.resources, self.safe_rt(resource_type))(self)
        return resource

Get instance of Resource Class with dynamic type.

Args:
resource_type: The resource type name (e.g Adversary, User Agent, etc).

Returns:
(object): Instance of Resource Object child class.


def GetUserInfo(knowledge_base, user):

  if "\\" in user:
    domain, user = user.split("\\", 1)
    users = [
        u for u in knowledge_base.users
        if u.username == user and u.userdomain == domain
    ]
  else:
    users = [u for u in knowledge_base.users if u.username == user]

  if not users:
    return
  else:
    return users[0]

Get a User protobuf for a specific user.

Args:
knowledge_base: An rdf_client.KnowledgeBase object.
user: Username as string. May contain domain like DOMAIN\\user.

Returns:
A User rdfvalue or None


def group_associations_types(
        self,
        main_type,
        sub_type,
        unique_id,
        target,
        api_branch=None,
        api_entity=None,
        owner=None,
        params=None,
    ):

        params = params or {}
        if owner:
            params['owner'] = owner

        api_branch = api_branch or target.api_sub_type
        api_entity = api_entity or target.api_entity

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

        for gat in self._iterate(url, params, api_entity):
            yield gat

Args:
owner:
main_type:
sub_type:
unique_id:
target:
api_branch:
api_entity:
params:

Return:


def ragged_binary_elementwise_assert_op_impl(op, x, y):
    x_is_ragged = ragged_tensor.is_ragged(x)
    y_is_ragged = ragged_tensor.is_ragged(y)
    x = ragged_tensor.convert_to_tensor_or_ragged_tensor(x, preferred_dtype=y.dtype if y_is_ragged else None)
    y = ragged_tensor.convert_to_tensor_or_ragged_tensor(y, preferred_dtype=x.dtype)
    if x_is_ragged and y_is_ragged:
        x, y = ragged_tensor.match_row_splits_dtypes(x, y)
    if x_is_ragged and y_is_ragged or (x_is_ragged and x.flat_values.shape.ndims <= y.shape.ndims) or (y_is_ragged and y.flat_values.shape.ndims <= x.shape.ndims):
        shape_x = DynamicRaggedShape.from_tensor(x)
        shape_y = DynamicRaggedShape.from_tensor(y)
        if shape_x.dtype != shape_y.dtype:
            if not x_is_ragged:
                shape_x = shape_x.with_dtype(shape_y.dtype)
            elif not y_is_ragged:
                shape_y = shape_y.with_dtype(shape_x.dtype)
        if _row_partitions_identical(shape_x, shape_y):
            return op(x.flat_values, y.flat_values)
        _, bcast_xz, bcast_yz = broadcast_dynamic_shape_extended(shape_x, shape_y)
        x_new_flat = bcast_xz.broadcast_flat_values(x, inner_dimensions=False)
        y_new_flat = bcast_yz.broadcast_flat_values(y, inner_dimensions=False)
        return op(x_new_flat, y_new_flat)
    x_values = x.flat_values if ragged_tensor.is_ragged(x) else x
    y_values = y.flat_values if ragged_tensor.is_ragged(y) else y
    return op(x_values, y_values)

Binary elementwise assert api handler for RaggedTensors.

This handles binary assert operations for ragged tensors. Compared with
`ragged_binary_elementwise_op_impl`, this handler does not compute a ragged
tensor as output. Instead, it applies the assert operation `op` to input
tensors based on their ragged shapes and flat_values, and returns the result
of the assertion operation.

Args:
op: a binary assert operation on Tensors.
x: something that can be coerced to a Tensor or RaggedTensor.
y: something that can be coerced to a Tensor or RaggedTensor.

Returns:
the result of the assertion operation.


def init_cache(self, batch_size, max_length, encoder_outputs):
    decoder_input_ids = jnp.ones((batch_size, max_length), dtype='i4')
    decoder_attention_mask = jnp.ones_like(decoder_input_ids)
    decoder_position_ids = jnp.broadcast_to(jnp.arange(jnp.atleast_2d(decoder_input_ids).shape[-1]), decoder_input_ids.shape)

    def _decoder_forward(module, decoder_input_ids, decoder_attention_mask, decoder_position_ids, **kwargs):
        decoder_module = module._get_decoder_module()
        return decoder_module(decoder_input_ids, decoder_attention_mask, decoder_position_ids, **kwargs)
    init_variables = self.module.init(jax.random.PRNGKey(0), decoder_input_ids=decoder_input_ids, decoder_attention_mask=decoder_attention_mask, decoder_position_ids=decoder_position_ids, encoder_hidden_states=encoder_outputs[0], init_cache=True, method=_decoder_forward)
    return unfreeze(init_variables['cache'])

Args:
batch_size (`int`):
batch_size used for fast auto-regressive decoding. Defines the batch size of the initialized cache.
max_length (`int`):
maximum possible length for auto-regressive decoding. Defines the sequence length of the initialized
cache.
encoder_outputs (`Union[FlaxBaseModelOutput, tuple(tuple(jnp.ndarray)]`):
`encoder_outputs` consists of (`last_hidden_state`, *optional*: `hidden_states`, *optional*:
`attentions`). `last_hidden_state` of shape `(batch_size, sequence_length, hidden_size)`, *optional*)
is a sequence of hidden-states at the output of the last layer of the encoder. Used in the
cross-attention of the decoder.


def _calc_min_size(self, conv_layers):
    input_size = 1
    for (_, conv_params, max_pooling) in reversed(conv_layers):
        if (max_pooling is not None):
            (kernel_size, stride) = max_pooling
            input_size = ((input_size * stride) + (kernel_size - stride))
        if (conv_params is not None):
            (kernel_size, stride) = conv_params
            input_size = ((input_size * stride) + (kernel_size - stride))
    return input_size

Calculates the minimum size of the input layer.

Given a set of convolutional layers, calculate the minimum value of
the `input_height` and `input_width`, i.e. such that the output has
size 1x1. Assumes snt.VALID padding.

Args:
conv_layers: List of tuples `(output_channels, (kernel_size, stride),
(pooling_size, pooling_stride))`

Returns:
Minimum value of input height and width.


def save(self, save_path, options=None):
    save_start_time = time.time()
    if not self._initialized:
        self._ensure_initialized()
    else:
        self._queue.join()
        self._copy_to_cpu()
    self._check_async_thread_error()
    save_counter = self.checkpointer().save_counter.numpy() + 1
    full_path = '{}-{}'.format(save_path, save_counter)
    context.async_wait()
    self._save_file_prefix = save_path
    self._use_checkpoint_save = True
    self._checkpoint_options = copy.copy(options) if options else None
    if self._checkpoint_options:
        self._checkpoint_options.experimental_enable_async_checkpoint = False
    self._queue.put(True)
    save_end_time = time.time()
    metrics.AddCheckpointWriteDuration(api_label=_ASYNC_CHECKPOINT, microseconds=_get_duration_microseconds(save_start_time, save_end_time))
    return full_path

Save the checkpointed variables.

Args:
save_path: The file prefix of the checkpoint file.
options: Optional CheckpointOption instance.

Returns:
The full path of the checkpoint file.


def _AlignDecodedDataOffset(self, decoded_data_offset):
    self._file_object.seek(0, os.SEEK_SET)
    self._decoder = self._GetDecoder()
    self._decoded_data = b''
    encoded_data_offset = 0
    encoded_data_size = self._file_object.get_size()
    while (encoded_data_offset < encoded_data_size):
        read_count = self._ReadEncodedData(self._ENCODED_DATA_BUFFER_SIZE)
        if (read_count == 0):
            break
        encoded_data_offset += read_count
        if (decoded_data_offset < self._decoded_data_size):
            self._decoded_data_offset = decoded_data_offset
            break
        decoded_data_offset -= self._decoded_data_size

Aligns the encoded file with the decoded data offset.

Args:
decoded_data_offset (int): decoded data offset.


def generate_message_doc(message_descriptor, locations, path, name_prefix=''):
    prefixed_name = (name_prefix + message_descriptor.name)
    print(make_subsection(prefixed_name))
    location = locations[path]
    if location.HasField('leading_comments'):
        print(textwrap.dedent(location.leading_comments))
    row_tuples = []
    for (field_index, field) in enumerate(message_descriptor.field):
        field_location = locations[(path + (2, field_index))]
        if (field.type not in [11, 14]):
            type_str = TYPE_TO_STR[field.type]
        else:
            type_str = make_link(field.type_name.lstrip('.'))
        row_tuples.append((make_code(field.name), field.number, type_str, LABEL_TO_STR[field.label], textwrap.fill(get_comment_from_location(field_location), INFINITY)))
    print_table(('Field', 'Number', 'Type', 'Label', 'Description'), row_tuples)
    nested_types = enumerate(message_descriptor.nested_type)
    for (index, nested_message_desc) in nested_types:
        generate_message_doc(nested_message_desc, locations, (path + (3, index)), name_prefix=(prefixed_name + '.'))
    for (index, nested_enum_desc) in enumerate(message_descriptor.enum_type):
        generate_enum_doc(nested_enum_desc, locations, (path + (4, index)), name_prefix=(prefixed_name + '.'))

Generate docs for message and nested messages and enums.

Args:
message_descriptor: descriptor_pb2.DescriptorProto instance for message
to generate docs for.
locations: Dictionary of location paths tuples to
descriptor_pb2.SourceCodeInfo.Location instances.
path: Path tuple to the message definition.
name_prefix: Optional prefix for this message's name.


def concrete(self, other=None):
    new_system = self.clone()
    if other:
        new_system.applyFeatures(other, missing='other')
    soft_features = self.getValue(SoftFeatures.SOFT, [])
    score = 0
    for f in sorted(soft_features, key=(lambda f: f.soft), reverse=True):
        try:
            new_system.applyFeatures(f, missing='other')
            score += f.soft
        except:
            pass
    new_system.delValue(SoftFeatures.SOFT)
    return (new_system, score)

Return copy and score after being applied other system and soft features.

Args:

- other(system, optional): system to apply just before soft features.

Return(tuple): tuple of the resulting system and its score.


def readlink(path):

    if sys.getwindowsversion().major < 6:
        raise SaltInvocationError('Symlinks are only supported on Windows Vista or later.')

    try:
        return salt.utils.path.readlink(path)
    except OSError as exc:
        if exc.errno == errno.EINVAL:
            raise CommandExecutionError('{0} is not a symbolic link'.format(path))
        raise CommandExecutionError(exc.__str__())
    except Exception as exc:
        raise CommandExecutionError(exc)

Return the path that a symlink points to

This is only supported on Windows Vista or later.

Inline with Unix behavior, this function will raise an error if the path is
not a symlink, however, the error raised will be a SaltInvocationError, not
an OSError.

Args:
path (str): The path to the symlink

Returns:
str: The path that the symlink points to

CLI Example:

.. code-block:: bash

salt '*' file.readlink /path/to/link


def write_temp_file(self, content, filename=None, mode='w'):

        if filename is None:
            filename = str(uuid.uuid4())
        fqpn = os.path.join(self.tcex.default_args.tc_temp_path, filename)
        with open(fqpn, mode) as fh:
            fh.write(content)
        return fqpn

Write content to a temporary file.

Args:
content (bytes|str): The file content. If passing binary data the mode needs to be set
to 'wb'.
filename (str, optional): The filename to use when writing the file.
mode (str, optional): The file write mode which could be either 'w' or 'wb'.

Returns:
str: Fully qualified path name for the file.


def _parse_authors(html_chunk):

    authors = html_chunk.match(
        ["div", {"class": "comment"}],
        "h3",
        "a",
    )

    if not authors:
        return []

    authors = map(
        lambda x: Author(
            x.getContent().strip(),
            normalize_url(BASE_URL, x.params.get("href", None))
        ),
        authors
    )

    return filter(lambda x: x.name.strip(), authors)

Parse authors of the book.

Args:
html_chunk (obj): HTMLElement containing slice of the page with details.

Returns:
list: List of :class:`structures.Author` objects. Blank if no author \
found.


def from_schema(cls, schema, id_of=_id_of, *args, **kwargs):

        return cls(base_uri=id_of(schema), referrer=schema, *args, **kwargs)

Construct a resolver from a JSON schema object.

Arguments:

schema:

the referring schema

Returns:

`RefResolver`


def yield_batch(iterable, batch_size, num_tensors=1):
    tensors = [[] for i in range(num_tensors)]
    for item in iterable:
        if (item is None):
            break
        for i in range(num_tensors):
            tmp = (str(item[i]) if (type(item[i]) is bytearray) else item[i])
            tensors[i].append(tmp)
        if (len(tensors[0]) >= batch_size):
            (yield tensors)
            tensors = [[] for i in range(num_tensors)]
    if (len(tensors[0]) > 0):
        (yield tensors)

Generator that yields batches of a DataFrame iterator.

Args:
:iterable: Spark partition iterator.
:batch_size: number of items to retrieve per invocation.
:num_tensors: number of tensors (columns) expected in each item.

Returns:
An array of ``num_tensors`` arrays, each of length `batch_size`


def get_cohp(self, spin=None, integrated=False):

        if not integrated:
            populations = self.cohp
        else:
            populations = self.icohp

        if populations is None:
            return None
        elif spin is None:
            return populations
        else:
            if isinstance(spin, int):
                spin = Spin(spin)
            elif isinstance(spin, str):
                s = {"up": 1, "down": -1}[spin.lower()]
                spin = Spin(s)
            return {spin: populations[spin]}

Returns the COHP or ICOHP for a particular spin.

Args:
spin: Spin. Can be parsed as spin object, integer (-1/1)
or str ("up"/"down")
integrated: Return COHP (False) or ICOHP (True)

Returns:
Returns the CHOP or ICOHP for the input spin. If Spin is
None and both spins are present, both spins will be returned
as a dictionary.


def get_leaf_node_path_list(self, sep=os.path.sep, type_str=None):
    return [v.get_path_str(sep, type_str) for v in self.leaf_node_gen]

Get paths for all leaf nodes for the tree rooted at this node.

Args:
sep: str
One or more characters to insert between each element in the path.
Defaults to "/" on Unix and "\" on Windows.

type_str:
SUBJECT_NODE_TAG, TYPE_NODE_TAG or None. If set, only include
information from nodes of that type.

Returns:
list of str: The paths to the leaf nodes for the tree rooted at this node.


def _clean(self, item):
    null_keys = []
    for key in item:
        if item[key] == None:
            null_keys.append(key)
    for key in null_keys:
        del item[key]
    if 'adBlockingOptOut' in item and (not item['adBlockingOptOut']):
        item['adBlockingOptOut'] = False
    return item

Removes null keys from the item.

An absent key and a null key mean different things in certain contexts for
CM, this method cleans up objects before sending to the CM API by removing
any null keys.

Args:
item: The CM object to clean.


def collect_constant_renames():
    renames = set()
    for module in sys.modules.copy().values():
        try:
            constants_v1_list = tf_export.get_v1_constants(module)
            constants_v2_list = tf_export.get_v2_constants(module)
        except:
            pass
        constants_v1 = {constant_name: api_names for api_names, constant_name in constants_v1_list}
        constants_v2 = {constant_name: api_names for api_names, constant_name in constants_v2_list}
        for constant_name, api_names_v1 in constants_v1.items():
            api_names_v2 = constants_v2[constant_name]
            for name in api_names_v1:
                if name not in api_names_v2:
                    renames.add((name, get_canonical_name(api_names_v2, name)))
    return renames

Looks for constants that need to be renamed in TF 2.0.

Returns:
Set of tuples of the form (current name, new name).


def _kl_blockwise_blockwise(b0, b1, name=None):

  if len(b0.distributions) != len(b1.distributions):
    raise ValueError(
        'Can only compute KL divergence between Blockwise distributions with '
        'the same number of component distributions.')

  b0_event_sizes = [_event_size(d) for d in b0.distributions]
  b1_event_sizes = [_event_size(d) for d in b1.distributions]

  assertions = []
  message = ('Can only compute KL divergence between Blockwise distributions '
             'with the same pairwise event shapes.')

  if (all(isinstance(event_size, int) for event_size in b0_event_sizes) and
      all(isinstance(event_size, int) for event_size in b1_event_sizes)):
    if b0_event_sizes != b1_event_sizes:
      raise ValueError(message)
  else:
    if b0.validate_args or b1.validate_args:
      assertions.extend(
          assert_util.assert_equal(
              e1, e2, message=message)
          for e1, e2 in zip(b0_event_sizes, b1_event_sizes))

  with tf.name_scope(name or 'kl_blockwise_blockwise'):
    with tf.control_dependencies(assertions):
      return sum([
          kullback_leibler.kl_divergence(d1, d2) for d1, d2 in zip(
              b0.distributions, b1.distributions)])

Calculate the batched KL divergence KL(b0 || b1) with b0 and b1 Blockwise distributions.

Args:
b0: instance of a Blockwise distribution object.
b1: instance of a Blockwise distribution object.
name: (optional) Name to use for created operations. Default is
"kl_blockwise_blockwise".

Returns:
kl_blockwise_blockwise: `Tensor`. The batchwise KL(b0 || b1).


def copy_function_def_to_graph_def_recursively(func_name, graph_def, copied_functions, default_graph=None):
    if func_name and (not is_function(func_name, default_graph)):
        raise ValueError(f'Function {func_name} was not found. Please make sure the FunctionDef `fdef` is correct.')
    if func_name in copied_functions:
        return
    copied_functions.add(func_name)
    func_def = get_function_def(func_name, default_graph)
    graph_def.library.function.add().CopyFrom(func_def)
    for node_def in func_def.node_def:
        op_def = default_graph.op_def_for_type(node_def.op)
        for attr in op_def.attr:
            if attr.type == 'func':
                func_name = node_def.attr[attr.name].func.name
                copy_function_def_to_graph_def_recursively(func_name, graph_def, copied_functions, default_graph)
            elif attr.type == 'list(func)':
                for fn in node_def.attr[attr.name].list.func:
                    func_name = fn.name
                    copy_function_def_to_graph_def_recursively(func_name, graph_def, copied_functions, default_graph)

Recursively copies `FunctionDef`s to `GraphDef`.

It copies the outermost `FunctionDef` and all nested `FunctionDef`s to
`graph_def`. The `copied_function` enforces that every `FunctionDef` will be
copied at most once. The `FunctionDef`s will be found from `default_graph` if
this function was called in graph mode or from eager context if this function
was called in eager mode.

Args:
func_name: The signature name of FunctionDef to be copied to `graph_def`.
graph_def: The GraphDef that will contain all `FunctionDef`s in its library.
copied_functions: A set contains all copied function names.
default_graph: The `tf.Graph` where all `FunctionDef`s will be found
in graph mode. Not used in eager mode.


def find_distinct(self, collection, key):
    obj = getattr(self.db, collection)
    result = obj.distinct(key)
    return result

Search a collection for the distinct key values provided.

Args:
collection: The db collection. See main class documentation.
key: The name of the key to find distinct values. For example with
the indicators collection, the key could be "type".
Returns:
List of distinct values.


def __init__(self, function_type, default_values, is_pure=False, name=None, jit_compile=None):
    self._function_type = function_type
    self._default_values = default_values
    self._fullargspec = to_fullargspec(function_type, default_values)
    self._is_pure = is_pure
    self._jit_compile = jit_compile
    self._name = name or 'f'
    self._input_signature = to_input_signature(function_type)

Constructs a FunctionSpec describing a python function.

Args:
function_type: A FunctionType describing the python function signature.
default_values: Dictionary mapping parameter names to default values.
is_pure: if True all input arguments (including variables and constants)
will be converted to tensors and no variable changes allowed.
name: Name of the function
jit_compile: see `tf.function`.


def save(thing, url_or_handle, **kwargs):

    is_handle = hasattr(url_or_handle, "write") and hasattr(url_or_handle, "name")
    if is_handle:
        _, ext = os.path.splitext(url_or_handle.name)
    else:
        _, ext = os.path.splitext(url_or_handle)
    if not ext:
        raise RuntimeError("No extension in URL: " + url_or_handle)

    if ext in savers:
        saver = savers[ext]
        if is_handle:
            saver(thing, url_or_handle, **kwargs)
        else:
            with write_handle(url_or_handle) as handle:
                saver(thing, handle, **kwargs)
    else:
        saver_names = [(key, fn.__name__) for (key, fn) in savers.items()]
        message = "Unknown extension '{}', supports {}."
        raise ValueError(message.format(ext, saver_names))

Save object to file on CNS.

File format is inferred from path. Use save_img(), save_npy(), or save_json()
if you need to force a particular format.

Args:
obj: object to save.
path: CNS path.

Raises:
RuntimeError: If file extension not supported.


def _find_control_structure(self, start_address, search_length):
    words = self._read_memory(start_address, search_length, chunk_size=4, join=False)
    found_offset = None
    for (i, word) in enumerate(words):
        if (word == ControlStructure.CONTROL_MAGIC_1):
            if ((len(words) - i) < 4):
                continue
            if ((words[(i + 1)] == ControlStructure.CONTROL_MAGIC_2) and (words[(i + 2)] == ControlStructure.CONTROL_MAGIC_3) and (words[(i + 3)] == ControlStructure.CONTROL_MAGIC_4)):
                found_offset = i
                break
    if (found_offset is None):
        raise HardwareError('Could not find control structure magic value in search area')
    struct_info = words[(found_offset + 4)]
    (_version, _flags, length) = struct.unpack('<BBH', struct.pack('<L', struct_info))
    if ((length % 4) != 0):
        raise HardwareError('Invalid control structure length that was not a multiple of 4', length=length)
    word_length = (length
    control_data = struct.pack(('<%dL' % word_length), *words[found_offset:(found_offset + word_length)])
    logger.info('Found control stucture at address 0x%08X, word_length=%d', (start_address + (4 * found_offset)), word_length)
    return ControlStructure((start_address + (4 * found_offset)), control_data)

Find the control structure in RAM for this device.

Returns:
ControlStructure: The decoded contents of the shared memory control structure
used for communication with this IOTile device.


def get_description(expression, options=None):

    descripter = ExpressionDescriptor(expression, options)
    return descripter.get_description(DescriptionTypeEnum.FULL)

Generates a human readable string for the Cron Expression
Args:
expression: The cron expression string
options: Options to control the output description
Returns:
The cron expression description


def call(self, input_ids: Optional[tf.Tensor]=None, position_ids: Optional[tf.Tensor]=None, token_type_ids: Optional[tf.Tensor]=None, inputs_embeds: Optional[tf.Tensor]=None, past_key_values_length=0, training: bool=False) -> tf.Tensor:
    if input_ids is None and inputs_embeds is None:
        raise ValueError('Need to provide either `input_ids` or `input_embeds`.')
    if input_ids is not None:
        check_embeddings_within_bounds(input_ids, self.config.vocab_size)
        inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
    input_shape = shape_list(inputs_embeds)[:-1]
    if token_type_ids is None:
        token_type_ids = tf.fill(dims=input_shape, value=0)
    if position_ids is None:
        position_ids = tf.expand_dims(tf.range(start=past_key_values_length, limit=input_shape[1] + past_key_values_length), axis=0)
    position_embeds = tf.gather(params=self.position_embeddings, indices=position_ids)
    token_type_embeds = tf.gather(params=self.token_type_embeddings, indices=token_type_ids)
    final_embeddings = inputs_embeds + position_embeds + token_type_embeds
    final_embeddings = self.LayerNorm(inputs=final_embeddings)
    final_embeddings = self.dropout(inputs=final_embeddings, training=training)
    return final_embeddings

Applies embedding based on inputs tensor.

Returns:
final_embeddings (`tf.Tensor`): output embedding tensor.


def _check_property(self, rest=None, require_indexed=True):
    if (require_indexed and (not self._indexed)):
        raise InvalidPropertyError(('Property is unindexed %s' % self._name))
    if rest:
        raise InvalidPropertyError(('Referencing subproperty %s.%s but %s is not a structured property' % (self._name, rest, self._name)))

Internal helper to check this property for specific requirements.

Called by Model._check_properties().

Args:
rest: Optional subproperty to check, of the form 'name1.name2...nameN'.

Raises:
InvalidPropertyError if this property does not meet the given
requirements or if a subproperty is specified.  (StructuredProperty
overrides this method to handle subproperties.)


def _check_obj_properties(self, pub, name="pub"):

        if not hasattr(pub, "indexes"):
            raise InvalidType("`%s` doesn't have .indexes property!" % name)

        if not pub.indexes:
            raise InvalidType("`%s.indexes` is not set!" % name)

        if not hasattr(pub, "project_key"):
            raise InvalidType(
                "`%s` doesn't have .project_key property!" % name
            )

        if not pub.project_key:
            raise InvalidType("`%s.project_key` is not set!" % name)

Make sure, that `pub` has the right interface.

Args:
pub (obj): Instance which will be checked.
name (str): Name of the instance. Used in exception. Default `pub`.

Raises:
InvalidType: When the `pub` is not instance of `obj_type`.


def get_user(path, follow_symlinks=True):
    if (not os.path.exists(path)):
        raise CommandExecutionError('Path not found: {0}'.format(path))
    if (follow_symlinks and (sys.getwindowsversion().major >= 6)):
        path = _resolve_symlink(path)
    return salt.utils.win_dacl.get_owner(path)

Return the user that owns a given file

Symlinks are followed by default to mimic Unix behavior. Specify
`follow_symlinks=False` to turn off this behavior.

Args:
path (str): The path to the file or directory

follow_symlinks (bool):
If the object specified by ``path`` is a symlink, get attributes of
the linked file instead of the symlink itself. Default is True

Returns:
str: The name of the owner

CLI Example:

.. code-block:: bash

salt '*' file.get_user c:\\temp\\test.txt
salt '*' file.get_user c:\\temp\\test.txt follow_symlinks=False


def set_user_info(self, nick, user='*', real='*'):

        if self.connected:
            raise Exception("Can't set user info now, we're already connected!")

        if not self.connected:
            self.nick = nick

        self.connect_info['user'] = {
            'nick': nick,
            'user': user,
            'real': real,
        }

Sets user info for this server, to be used before connection.

Args:
nick (str): Nickname to use.
user (str): Username to use.
real (str): Realname to use.


def is_module(obj):

    return True if obj and isinstance(obj, ModuleType) or inspect.isclass(obj) else False

Checking and setting type to MODULE
Args:
obj: ModuleType / class
Note: An instance will be treated as a Class
Return:
Boolean


def start(self, channel=None):

        super(ReferenceDevice, self).start(channel)

        try:
            self.controller.start(channel)

            for address, tile in sorted(self._tiles.items()):
                if address == 8:
                    continue

                if not isinstance(tile, EmulatedPeripheralTile):
                    raise DataError("An emulated ReferenceDevice can only have a single controller and all other tiles must inherit from EmulatedPeripheralTile",
                                    address=address)

                tile.start(channel)

            async def _launch_tiles():
                await self.controller.reset()
                await asyncio.wait_for(self.controller.initialized.wait(), 2.0)

                for address, tile in sorted(self._tiles.items()):
                    if address == 8:
                        continue

                    await asyncio.wait_for(tile.initialized.wait(), 2.0)

            self.emulator.run_task_external(_launch_tiles())

            if self._simulating_time:
                self.emulator.add_task(None, self._time_ticker())
        except:
            self.stop()
            raise

Start this emulated device.

This triggers the controller to call start on all peripheral tiles in
the device to make sure they start after the controller does and then
it waits on each one to make sure they have finished initializing
before returning.

Args:
channel (IOTilePushChannel): the channel with a stream and trace
routine for streaming and tracing data through a VirtualInterface


def get_most_recent_images(self, results, types=[], sensors=[], N=1):
    if (not len(results)):
        return None
    if types:
        results = [r for r in results if (r['type'] in types)]
    if sensors:
        results = [r for r in results if (r['properties'].get('sensorPlatformName') in sensors)]
    newlist = sorted(results, key=(lambda k: k['properties'].get('timestamp')), reverse=True)
    return newlist[:N]

Return the most recent image

Args:
results: a catalog resultset, as returned from a search
types: array of types you want. optional.
sensors: array of sensornames. optional.
N: number of recent images to return.  defaults to 1.

Returns:
single catalog item, or none if not found


def create_script(target_test):
    script = f'\nimport os\nimport subprocess\n\nresult = subprocess.run(\n    ["python3", "-m", "pytest", "-v", "-rfEp", f"{target_test}"],\n    capture_output = True,\n    text=True,\n)\nprint(result.stdout)\n\nif f"PASSED {target_test}" in result.stdout:\n    print("test passed")\n    exit(0)\nelif len(result.stderr) > 0:\n    if "ERROR: file or directory not found: " in result.stderr:\n        print("test file or directory not found in this commit")\n        exit(0)\n    elif "ERROR: not found: " in result.stderr:\n        print("test not found in this commit")\n        exit(0)\n    else:\n        print(f"pytest failed to run: {{result.stderr}}")\n        exit(-1)\nelif f"FAILED {target_test}" in result.stdout:\n    print("test failed")\n    exit(2)\n\nexit(0)\n'
    with open('target_script.py', 'w') as fp:
        fp.write(script.strip())

Create a python script to be run by `git bisect run` to determine if `target_test` passes or fails.
If a test is not found in a commit, the script with exit code `0` (i.e. `Success`).

Args:
target_test (`str`): The test to check.

Returns:
`str`: The script to be run by `git bisect run`.


def take_reference_screenshot(webdriver, file_name):
    folder_location = os.path.join(ProjectUtils.get_project_root(), WebScreenShotUtil.REFERENCE_SCREEN_SHOT_LOCATION)
    WebScreenShotUtil.__capture_screenshot(webdriver, folder_location, (file_name + '.png'))

Captures a screenshot as a reference screenshot.

Args:
webdriver (WebDriver) - Selenium webdriver.
file_name (str) - File name to save screenshot as.


def operates_on(self, qubits: Iterable[raw_types.Qid]) -> bool:
    return any(((q in qubits) for q in self.qubits))

Determines if the moment has operations touching the given qubits.

Args:
qubits: The qubits that may or may not be touched by operations.

Returns:
Whether this moment has operations involving the qubits.


def fill(self, text):

    def _fill(elem):
        elem.clear()
        elem.send_keys(text)
    self.map(_fill, u'fill({!r})'.format(text)).execute()

Set the text value of each matched element to `text`.

Example usage:

.. code:: python

# Set the text of the first element matched by the query to "Foo"
q.first.fill('Foo')

Args:
text (str): The text used to fill the element (usually a text field or text area).

Returns:
None


def replace_multiple_in_file(filename: str,
                             replacements: List[Tuple[str, str]]) -> None:

    with open(filename) as infile:
        contents = infile.read()
    for text_from, text_to in replacements:
        log.info("Amending {}: {} -> {}",
                 filename, repr(text_from), repr(text_to))
        contents = contents.replace(text_from, text_to)
    with open(filename, 'w') as outfile:
        outfile.write(contents)

Replaces multiple from/to string pairs within a single file.

Args:
filename: filename to process (modifying it in place)
replacements: list of ``(from_text, to_text)`` tuples


def sanity_check_ir_blocks_from_frontend(ir_blocks, query_metadata_table):

    if not ir_blocks:
        raise AssertionError(u'Received no ir_blocks: {}'.format(ir_blocks))

    _sanity_check_fold_scope_locations_are_unique(ir_blocks)
    _sanity_check_no_nested_folds(ir_blocks)
    _sanity_check_query_root_block(ir_blocks)
    _sanity_check_output_source_follower_blocks(ir_blocks)
    _sanity_check_block_pairwise_constraints(ir_blocks)
    _sanity_check_mark_location_preceding_optional_traverse(ir_blocks)
    _sanity_check_every_location_is_marked(ir_blocks)
    _sanity_check_coerce_type_outside_of_fold(ir_blocks)
    _sanity_check_all_marked_locations_are_registered(ir_blocks, query_metadata_table)
    _sanity_check_registered_locations_parent_locations(query_metadata_table)

Assert that IR blocks originating from the frontend do not have nonsensical structure.

Args:
ir_blocks: list of BasicBlocks representing the IR to sanity-check

Raises:
AssertionError, if the IR has unexpected structure. If the IR produced by the front-end
cannot be successfully and correctly used to generate MATCH or Gremlin due to a bug,
this is the method that should catch the problem.


def get_min_max_value(self) -> tuple[float, float]:
    average_min_max_statistics = self._statistics.average_min_max_statistics
    num_samples = average_min_max_statistics.num_samples
    if num_samples == 0:
        raise ValueError(f'num_samples must not be 0 when calibration method is AverageMinMax: {self._calib_opts}')
    min_value, max_value = (average_min_max_statistics.min_sum / num_samples, average_min_max_statistics.max_sum / num_samples)
    return (min_value, max_value)

Calculates the average of min and max values.

Returns:
(min_value, max_value): Min and max calculated using AverageMinMax

Raises:
ValueError: num_samples is 0.


def CheckGlobalStatic(filename, clean_lines, linenum, error):
    line = clean_lines.elided[linenum]
    if (((linenum + 1) < clean_lines.NumLines()) and (not Search('[;({]', line))):
        line += clean_lines.elided[(linenum + 1)].strip()
    match = Match('((?:|static +)(?:|const +))(?::*std::)?string( +const)? +([a-zA-Z0-9_:]+)\\b(.*)', line)
    if (match and (not Search('\\bstring\\b(\\s+const)?\\s*[\\*\\&]\\s*(const\\s+)?\\w', line)) and (not Search('\\boperator\\W', line)) and (not Match('\\s*(<.*>)?(::[a-zA-Z0-9_]+)*\\s*\\(([^"]|$)', match.group(4)))):
        if Search('\\bconst\\b', line):
            error(filename, linenum, 'runtime/string', 4, ('For a static/global string constant, use a C style string instead: "%schar%s %s[]".' % (match.group(1), (match.group(2) or ''), match.group(3))))
        else:
            error(filename, linenum, 'runtime/string', 4, 'Static/global string variables are not permitted.')
    if (Search('\\b([A-Za-z0-9_]*_)\\(\\1\\)', line) or Search('\\b([A-Za-z0-9_]*_)\\(CHECK_NOTNULL\\(\\1\\)\\)', line)):
        error(filename, linenum, 'runtime/init', 4, 'You seem to be initializing a member variable with itself.')

Check for unsafe global or static objects.

Args:
filename: The name of the current file.
clean_lines: A CleansedLines instance containing the file.
linenum: The number of the line to check.
error: The function to call with any errors found.