async def do_run_task(context, run_cancellable, to_cancellable_process):

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

Run the task logic.

Returns the integer status of the task.

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

Raises:
Exception: on unexpected exception.

Returns:
int: exit status


class SGD(Optimizer):

    def __init__(self, lr=0.01, momentum=0.0, decay=0.0, nesterov=False, **kwargs):
        super(SGD, self).__init__(**kwargs)
        with backend.name_scope(self.__class__.__name__):
            self.iterations = backend.variable(0, dtype='int64', name='iterations')
            self.lr = backend.variable(lr, name='lr')
            self.momentum = backend.variable(momentum, name='momentum')
            self.decay = backend.variable(decay, name='decay')
        self.initial_decay = decay
        self.nesterov = nesterov

    def _create_all_weights(self, params):
        shapes = [backend.int_shape(p) for p in params]
        moments = [backend.zeros(shape) for shape in shapes]
        self.weights = [self.iterations] + moments
        return moments

    def get_updates(self, loss, params):
        grads = self.get_gradients(loss, params)
        self.updates = [state_ops.assign_add(self.iterations, 1)]
        lr = self.lr
        if self.initial_decay > 0:
            lr = lr * (1.0 / (1.0 + self.decay * math_ops.cast(self.iterations, backend.dtype(self.decay))))
        moments = self._create_all_weights(params)
        for p, g, m in zip(params, grads, moments):
            v = self.momentum * m - lr * g
            self.updates.append(state_ops.assign(m, v))
            if self.nesterov:
                new_p = p + self.momentum * v - lr * g
            else:
                new_p = p + v
            if getattr(p, 'constraint', None) is not None:
                new_p = p.constraint(new_p)
            self.updates.append(state_ops.assign(p, new_p))
        return self.updates

    def get_config(self):
        config = {'lr': float(backend.get_value(self.lr)), 'momentum': float(backend.get_value(self.momentum)), 'decay': float(backend.get_value(self.decay)), 'nesterov': self.nesterov}
        base_config = super(SGD, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

Stochastic gradient descent optimizer.

Includes support for momentum,
learning rate decay, and Nesterov momentum.

Args:
lr: float >= 0. Learning rate.
momentum: float >= 0. Parameter that accelerates SGD in the relevant
direction and dampens oscillations.
decay: float >= 0. Learning rate decay over each update.
nesterov: boolean. Whether to apply Nesterov momentum.


def GetTARInfoByPathSpec(self, path_spec):

    location = getattr(path_spec, 'location', None)
    if location is None:
      raise errors.PathSpecError('Path specification missing location.')

    if not location.startswith(self.LOCATION_ROOT):
      raise errors.PathSpecError('Invalid location in path specification.')

    if len(location) == 1:
      return None

    try:
      return self._tar_file.getmember(location[1:])
    except KeyError:
      pass

Retrieves the TAR info for a path specification.

Args:
path_spec (PathSpec): a path specification.

Returns:
tarfile.TARInfo: TAR info or None if it does not exist.

Raises:
PathSpecError: if the path specification is incorrect.


def update_team_name(self, name):
    request = self._get_request()
    return request.post(self.TEAM_UPDATE_URL, {'name': name})

Updates a Team's name

Args:

name (str): The new name of your team

Returns:
A Team object


def get_config_value(self, section_name, option, default_option="default"):

        if self.config is None:
            self.config = configparser.ConfigParser()
            self.config.read(self.ini_file_name)

        if option:
            try:
                return self.config.get(section_name, option)
            except configparser.NoOptionError:
                log.debug(
                    "Didn't find a configuration option for '%s' section and '%s' option",
                    section_name, option,
                )

        return self.config.get(section_name, default_option)

Read a value from the configuration, with a default.

Args:
section_name (str): name of the section in the configuration from which
the option should be found.
option (str): name of the configuration option.
default_option (str): name of the default configuration option whose
value should be returned if the requested option is not found.

Returns:
str: the value from the ini file.


def to_dense(tensor):
    if is_sparse(tensor):
        return sparse_ops.sparse_tensor_to_dense(tensor)
    else:
        return tensor

Converts a sparse tensor into a dense tensor and returns it.

Args:
tensor: A tensor instance (potentially sparse).

Returns:
A dense tensor.

Examples:

>>> b = tf.keras.backend.placeholder((2, 2), sparse=True)
>>> print(tf.keras.backend.is_sparse(b))
True
>>> c = tf.keras.backend.to_dense(b)
>>> print(tf.keras.backend.is_sparse(c))
False


def _continue_search(self, state):
    i = state[_StateKeys.CUR_INDEX]
    alive_log_probs = state[_StateKeys.ALIVE_LOG_PROBS]
    finished_scores = state[_StateKeys.FINISHED_SCORES]
    finished_flags = state[_StateKeys.FINISHED_FLAGS]
    not_at_max_decode_length = tf.less(i, self.max_decode_length)
    max_length_norm = _length_normalization(self.alpha, self.max_decode_length)
    best_alive_scores = (alive_log_probs[(:, 0)] / max_length_norm)
    finished_scores *= tf.to_float(finished_flags)
    lowest_finished_scores = tf.reduce_min(finished_scores, axis=1)
    finished_batches = tf.reduce_any(finished_flags, 1)
    lowest_finished_scores += ((1.0 - tf.to_float(finished_batches)) * (- INF))
    worst_finished_score_better_than_best_alive_score = tf.reduce_all(tf.greater(lowest_finished_scores, best_alive_scores))
    return tf.logical_and(not_at_max_decode_length, tf.logical_not(worst_finished_score_better_than_best_alive_score))

Return whether to continue the search loop.

The loops should terminate when
1) when decode length has been reached, or
2) when the worst score in the finished sequences is better than the best
score in the alive sequences (i.e. the finished sequences are provably
unchanging)

Args:
state: A dictionary with the current loop state.

Returns:
Bool tensor with value True if loop should continue, False if loop should
terminate.


def connect(self, client_id):
    if (self.client is not None):
        raise InternalError('Connect called on an alreaded connected MQTT client')
    client = AWSIoTPythonSDK.MQTTLib.AWSIoTMQTTClient(client_id, useWebsocket=self.websockets)
    if self.websockets:
        client.configureEndpoint(self.endpoint, 443)
        client.configureCredentials(self.root)
        if (self.iam_session is None):
            client.configureIAMCredentials(self.iam_key, self.iam_secret)
        else:
            client.configureIAMCredentials(self.iam_key, self.iam_secret, self.iam_session)
    else:
        client.configureEndpoint(self.endpoint, 8883)
        client.configureCredentials(self.root, self.key, self.cert)
    client.configureOfflinePublishQueueing(0)
    try:
        client.connect()
        self.client = client
    except operationError as exc:
        raise InternalError('Could not connect to AWS IOT', message=exc.message)
    self.sequencer.reset()

Connect to AWS IOT with the given client_id

Args:
client_id (string): The client ID passed to the MQTT message broker


def run_commands(self, program_language, program_main):
    if (program_language == 'python'):
        python_exe = sys.executable
        ptvsd_host = 'localhost'
        if self.args.docker:
            python_exe = 'python'
            ptvsd_host = '0.0.0.0'
        if self.args.vscd:
            self.update_environment()
            command = [python_exe, '-m', 'ptvsd', '--host', ptvsd_host, '--port', self.args.vscd_port, '--wait', '{}.py'.format(program_main)]
        else:
            command = [python_exe, '.', program_main]
        cli_command = [str(s) for s in (command + self.profile.get('profile_args').standard)]
        print_command = ' '.join((str(s) for s in (command + self.profile.get('profile_args').masked)))
        if self.args.unmask:
            print_command = ' '.join((str(s) for s in (command + self.profile.get('profile_args').quoted)))
    elif (program_language == 'java'):
        if self.args.docker:
            command = ['java', '-cp', self.tcex_json.get('class_path', './target/*')]
        else:
            command = [self.tcex_json.get('java_path', program_language), '-cp', self.tcex_json.get('class_path', './target/*')]
        cli_command = ((command + self.profile.get('profile_args').standard) + [program_main])
        print_command = ' '.join(((command + self.profile.get('profile_args').masked) + [program_main]))
        if self.args.unmask:
            print_command = ' '.join(((command + self.profile.get('profile_args').quoted) + [program_main]))
    return {'cli_command': cli_command, 'print_command': print_command}

Return the run Print Command.

Args:
program_language (str): The language of the current App/Project.
program_main (str): The executable name.

Returns:
dict: A dictionary containing the run command and a printable version of the command.


def __add_scraped_requests_to_queue(self, queue_item, scraped_requests):

        new_queue_items = []

        for scraped_request in scraped_requests:
            HTTPRequestHelper.patch_with_options(scraped_request, self.__options, queue_item)

            if not HTTPRequestHelper.complies_with_scope(queue_item, scraped_request, self.__options.scope):
                continue

            if self.queue.has_request(scraped_request):
                continue

            scraped_request.depth = queue_item.request.depth + 1
            if self.__options.scope.max_depth is not None:
                if scraped_request.depth > self.__options.scope.max_depth:
                    continue

            new_queue_item = self.queue.add_request(scraped_request)
            new_queue_items.append(new_queue_item)

        return new_queue_items

Convert the scraped requests to queue items, return them and also add them to the queue.

Args:
queue_item (:class:`nyawc.QueueItem`): The request/response pair that finished.
new_requests list(:class:`nyawc.http.Request`): All the requests that were found during this request.

Returns:
list(:class:`nyawc.QueueItem`): The new queue items.


def launch_simulation(self, parameter):
    return next(SimulationRunner.run_simulations(self, [parameter], self.data_folder))

Launch a single simulation, using SimulationRunner's facilities.

This function is used by ParallelRunner's run_simulations to map
simulation running over the parameter list.

Args:
parameter (dict): the parameter combination to simulate.


def reduce_formula(sym_amt, iupac_ordering=False):
    syms = sorted(sym_amt.keys(), key=(lambda x: [get_el_sp(x).X, x]))
    syms = list(filter((lambda x: (abs(sym_amt[x]) > Composition.amount_tolerance)), syms))
    factor = 1
    if all(((int(i) == i) for i in sym_amt.values())):
        factor = abs(gcd(*(int(i) for i in sym_amt.values())))
    polyanion = []
    if ((len(syms) >= 3) and ((get_el_sp(syms[(- 1)]).X - get_el_sp(syms[(- 2)]).X) < 1.65)):
        poly_sym_amt = {syms[i]: (sym_amt[syms[i]] / factor) for i in [(- 2), (- 1)]}
        (poly_form, poly_factor) = reduce_formula(poly_sym_amt, iupac_ordering=iupac_ordering)
        if (poly_factor != 1):
            polyanion.append('({}){}'.format(poly_form, int(poly_factor)))
    syms = syms[:((len(syms) - 2) if polyanion else len(syms))]
    if iupac_ordering:
        syms = sorted(syms, key=(lambda x: [get_el_sp(x).iupac_ordering, x]))
    reduced_form = []
    for s in syms:
        normamt = ((sym_amt[s] * 1.0) / factor)
        reduced_form.append(s)
        reduced_form.append(formula_double_format(normamt))
    reduced_form = ''.join((reduced_form + polyanion))
    return (reduced_form, factor)

Helper method to reduce a sym_amt dict to a reduced formula and factor.

Args:
sym_amt (dict): {symbol: amount}.
iupac_ordering (bool, optional): Whether to order the
formula by the iupac "electronegativity" series, defined in
Table VI of "Nomenclature of Inorganic Chemistry (IUPAC
Recommendations 2005)". This ordering effectively follows
the groups and rows of the periodic table, except the
Lanthanides, Actanides and hydrogen. Note that polyanions
will still be determined based on the true electronegativity of
the elements.

Returns:
(reduced_formula, factor).


def update_from_json(self, json_attributes, models=None, setter=None):
    for (k, v) in json_attributes.items():
        self.set_from_json(k, v, models, setter)

Updates the object's properties from a JSON attributes dictionary.

Args:
json_attributes: (JSON-dict) : attributes and values to update

models (dict or None, optional) :
Mapping of model ids to models (default: None)

This is needed in cases where the attributes to update also
have values that have references.

setter(ClientSession or ServerSession or None, optional) :
This is used to prevent "boomerang" updates to Bokeh apps.

In the context of a Bokeh server application, incoming updates
to properties will be annotated with the session that is
doing the updating. This value is propagated through any
subsequent change notifications that the update triggers.
The session can compare the event setter to itself, and
suppress any updates that originate from itself.

Returns:
None


def add_run_metadata(self, run_metadata, tag, global_step=None):
    if tag in self._session_run_tags:
        raise ValueError('The provided tag was already used for this event type')
    self._session_run_tags[tag] = True
    tagged_metadata = event_pb2.TaggedRunMetadata()
    tagged_metadata.tag = tag
    tagged_metadata.run_metadata = run_metadata.SerializeToString()
    event = event_pb2.Event(tagged_run_metadata=tagged_metadata)
    self._add_event(event, global_step)

Adds a metadata information for a single session.run() call.

Args:
run_metadata: A `RunMetadata` protobuf object.
tag: The tag name for this metadata.
global_step: Number. Optional global step counter to record with the
StepStats.

Raises:
ValueError: If the provided tag was already used for this type of event.


class TimmWrapperModelOutput(ModelOutput):
    last_hidden_state: torch.FloatTensor
    pooler_output: Optional[torch.FloatTensor] = None
    hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None
    attentions: Optional[Tuple[torch.FloatTensor, ...]] = None

Output class for models TimmWrapperModel, containing the last hidden states, an optional pooled output,
and optional hidden states.

Args:
last_hidden_state (`torch.FloatTensor`):
The last hidden state of the model, output before applying the classification head.
pooler_output (`torch.FloatTensor`, *optional*):
The pooled output derived from the last hidden state, if applicable.
hidden_states (`tuple(torch.FloatTensor)`, *optional*):
A tuple containing the intermediate hidden states of the model at the output of each layer or specified layers.
Returned if `output_hidden_states=True` is set or if `config.output_hidden_states=True`.
attentions (`tuple(torch.FloatTensor)`, *optional*):
A tuple containing the intermediate attention weights of the model at the output of each layer.
Returned if `output_attentions=True` is set or if `config.output_attentions=True`.
Note: Currently, Timm models do not support attentions output.


def getCaCerts(self):
    retn = []
    path = s_common.genpath(self.certdir, 'cas')
    for name in os.listdir(path):
        if (not name.endswith('.crt')):
            continue
        full = s_common.genpath(self.certdir, 'cas', name)
        retn.append(self._loadCertPath(full))
    return retn

Return a list of CA certs from the CertDir.

Returns:
[OpenSSL.crypto.X509]: List of CA certificates.