def tf_initialize(self, x_init, base_value, target_value, estimated_improvement):

        self.base_value = base_value

        if estimated_improvement is None:
            estimated_improvement = tf.abs(x=base_value)

        first_step = super(LineSearch, self).tf_initialize(x_init)

        improvement = tf.divide(
            x=(target_value - self.base_value),
            y=tf.maximum(x=estimated_improvement, y=util.epsilon)
        )

        last_improvement = improvement - 1.0

        if self.mode == 'linear':
            deltas = [-t * self.parameter for t in x_init]
            self.estimated_incr = -estimated_improvement * self.parameter

        elif self.mode == 'exponential':
            deltas = [-t * self.parameter for t in x_init]

        return first_step + (deltas, improvement, last_improvement, estimated_improvement)

Initialization step preparing the arguments for the first iteration of the loop body.

Args:
x_init: Initial solution guess $x_0$.
base_value: Value $f(x')$ at $x = x'$.
target_value: Value $f(x_0)$ at $x = x_0$.
estimated_improvement: Estimated value at $x = x_0$, $f(x')$ if None.

Returns:
Initial arguments for tf_step.


def _extract_id(self) -> str:
    match = re.match(self._VALID_URL, self.url)
    if match:
        return match.group('video_id')
    else:
        raise VideoIdNotMatchedError

Get video_id needed to obtain the real_url of the video.

Raises:
VideoIdNotMatchedError: If video_id is not matched with regular expression.


def __add__(self, other):

        if not all(np.equal(self.energies, other.energies)):
            raise ValueError("Energies of both COHP are not compatible.")
        populations = {spin: self.populations[spin] + other.populations[spin]
                       for spin in self.cohp}
        if self.icohp is not None and other.icohp is not None:
            int_pop = {spin: self.icohp[spin] + other.icohp[spin]
                       for spin in self.icohp}
        else:
            if self.icohp is not None or other.icohp is not None:
                warnings.warn("One of the COHP objects does not contain "
                              "ICOHPs. Setting ICOHP to None.")
            int_pop = None
        return Cohp(self.efermi, self.energies, populations, icohp=int_pop)

Adds two COHP together. Checks that energy scales are the same.
Otherwise, it raises a ValueError. It also adds ICOHP if present.
If ICOHP is only present in one object, it displays a warning and
will not add ICOHP.

Args:
other: Another COHP object.

Returns:
Sum of the two COHPs as a COHP object.


def create_clusters(provider, context, **kwargs):
    conn = get_session(provider.region).client('ecs')
    try:
        clusters = kwargs['clusters']
    except KeyError:
        logger.error('setup_clusters hook missing "clusters" argument')
        return False
    if isinstance(clusters, basestring):
        clusters = [clusters]
    cluster_info = {}
    for cluster in clusters:
        logger.debug('Creating ECS cluster: %s', cluster)
        r = conn.create_cluster(clusterName=cluster)
        cluster_info[r['cluster']['clusterName']] = r
    return {'clusters': cluster_info}

Creates ECS clusters.

Expects a "clusters" argument, which should contain a list of cluster
names to create.

Args:
provider (:class:`stacker.providers.base.BaseProvider`): provider
instance
context (:class:`stacker.context.Context`): context instance

Returns: boolean for whether or not the hook succeeded.


def __to_plain_containers(self, container: Union[(CommentedSeq, CommentedMap)]) -> Union[(OrderedDict, list)]:
    if isinstance(container, CommentedMap):
        new_container = OrderedDict()
        for (key, value_obj) in container.items():
            if (isinstance(value_obj, CommentedMap) or isinstance(value_obj, CommentedSeq)):
                new_container[key] = self.__to_plain_containers(value_obj)
            else:
                new_container[key] = value_obj
    elif isinstance(container, CommentedSeq):
        new_container = list()
        for value_obj in container:
            if (isinstance(value_obj, CommentedMap) or isinstance(value_obj, CommentedSeq)):
                new_container.append(self.__to_plain_containers(value_obj))
            else:
                new_container.append(value_obj)
    return new_container

Converts any sequence or mapping to list or OrderedDict

Stops at anything that isn't a sequence or a mapping.

One day, we'll extract the comments and formatting and store \
them out-of-band.

Args:
mapping: The mapping of constructed subobjects to edit


def designPrimers(seq_args, global_args=None, misprime_lib=None, mishyb_lib=None, debug=False):
    if global_args:
        primerdesign.setGlobals(global_args, misprime_lib, mishyb_lib)
    primerdesign.setSeqArgs(seq_args)
    return primerdesign.runDesign(debug)

Run the Primer3 design process.

If the global args have been previously set (either by a pervious
`designPrimers` call or by a `setGlobals` call), `designPrimers` may be
called with seqArgs alone (as a means of optimization).

Args:
seq_args (dict)               : Primer3 sequence/design args as per
Primer3 docs

global_args (dict, optional)  : Primer3 global args as per Primer3 docs
misprime_lib (dict, optional) : `Sequence name: sequence` dictionary
for mispriming checks.
mishyb_lib (dict, optional)   : `Sequence name: sequence` dictionary
for mishybridization checks.

Returns:
A dictionary of Primer3 results (should be identical to the expected
BoulderIO output from primer3_main)


def from_tensor(cls, tensor):
    if isinstance(tensor, core.Value):
        return EagerWeakTensor(tensor)
    if isinstance(tensor, core.Symbol):
        return GraphWeakTensor(tensor)
    raise errors.InvalidArgumentError(None, None, f'WeakTensor can only be constructed from tf.Tensor or tf.WeakTensor, but {type(tensor)} was given.')

Converts a 'tf.Tensor' into a 'WeakTensor'.

This should be the standard way of creating a WeakTensor instead
of directly calling the WeakTensor constructor.

Args:
tensor: The `tf.Tensor` that should be converted into a 'WeakTensor'.

Returns:
A `EagerWeakTensor` or 'GraphWeakTensor' that holds the `tensor`.


def to_hashable_table_ref(table_ref_elem_kv: Tuple[Union[str, TableReference], V]) -> Tuple[str, V]:
    table_ref = table_ref_elem_kv[0]
    hashable_table_ref = get_hashable_destination(table_ref)
    return (hashable_table_ref, table_ref_elem_kv[1])

Turns the key of the input tuple to its string representation. The key
should be either a string or a TableReference.

Args:
table_ref_elem_kv: A tuple of table reference and element.

Returns:
A tuple of string representation of input table and input element.


def push(self, x):
    self._quantile_tracker.push(x)

Pushes a new value and updates the internal quantile tracker.

Args:
x: The new value to be pushed.


def _lookup_model(cls, kind, default_model=None):
    modelclass = cls._kind_map.get(kind, default_model)
    if (modelclass is None):
        raise KindError(("No model class found for kind '%s'. Did you forget to import it?" % kind))
    return modelclass

Get the model class for the kind.

Args:
kind: A string representing the name of the kind to lookup.
default_model: The model class to use if the kind can't be found.

Returns:
The model class for the requested kind.
Raises:
KindError: The kind was not found and no default_model was provided.


def _poll_once(self, timeout_ms, max_records):

        self._coordinator.poll()

        if not self._subscription.has_all_fetch_positions():
            self._update_fetch_positions(self._subscription.missing_fetch_positions())

        records, partial = self._fetcher.fetched_records(max_records)
        if records:

            if not partial:
                self._fetcher.send_fetches()
            return records

        self._fetcher.send_fetches()

        timeout_ms = min(timeout_ms, self._coordinator.time_to_next_poll() * 1000)
        self._client.poll(timeout_ms=timeout_ms)

        if self._coordinator.need_rejoin():
            return {}

        records, _ = self._fetcher.fetched_records(max_records)
        return records

Do one round of polling. In addition to checking for new data, this does
any needed heart-beating, auto-commits, and offset updates.

Arguments:
timeout_ms (int): The maximum time in milliseconds to block.

Returns:
dict: Map of topic to list of records (may be empty).


def settings(package, reload_=False):
    global packages
    if ((package not in packages) or reload_):
        from os import path
        result = CaseConfigParser()
        if (package != 'acorn'):
            confpath = _package_path(package)
            _read_single(result, confpath)
        _read_single(result, _package_path('acorn'))
        packages[package] = result
    return packages[package]

Returns the config settings for the specified package.

Args:
package (str): name of the python package to get settings for.


def refresh(self, request):

        try:
            self._retrieve_info(request)
            self.token, self.expiry = _metadata.get_service_account_token(
                request,
                service_account=self._service_account_email)
        except exceptions.TransportError as caught_exc:
            new_exc = exceptions.RefreshError(caught_exc)
            six.raise_from(new_exc, caught_exc)

Refresh the access token and scopes.

Args:
request (google.auth.transport.Request): The object used to make
HTTP requests.

Raises:
google.auth.exceptions.RefreshError: If the Compute Engine metadata
service can't be reached if if the instance has not
credentials.


def unpack(self, buff=None, offset=0):

        instruction_type = UBInt16(enum_ref=InstructionType)
        instruction_type.unpack(buff, offset)
        self.__class__ = InstructionType(instruction_type.value).find_class()

        length = UBInt16()
        length.unpack(buff, offset=offset+2)

        super().unpack(buff[:offset+length.value], offset)

Unpack *buff* into this object.

This method will convert a binary data into a readable value according
to the attribute format.

Args:
buff (bytes): Binary buffer.
offset (int): Where to begin unpacking.

Raises:
:exc:`~.exceptions.UnpackException`: If unpack fails.


def _MakeRequestNoRetry(http, http_request, redirections=5, check_response_func=CheckResponse):
    connection_type = None
    if getattr(http, 'connections', None):
        url_scheme = parse.urlsplit(http_request.url).scheme
        if (url_scheme and (url_scheme in http.connections)):
            connection_type = http.connections[url_scheme]
    new_debuglevel = (4 if (httplib2.debuglevel == 4) else 0)
    with _Httplib2Debuglevel(http_request, new_debuglevel, http=http):
        (info, content) = http.request(str(http_request.url), method=str(http_request.http_method), body=http_request.body, headers=http_request.headers, redirections=redirections, connection_type=connection_type)
    if (info is None):
        raise exceptions.RequestError()
    response = Response(info, content, http_request.url)
    check_response_func(response)
    return response

Send http_request via the given http.

This wrapper exists to handle translation between the plain httplib2
request/response types and the Request and Response types above.

Args:
http: An httplib2.Http instance, or a http multiplexer that delegates to
an underlying http, for example, HTTPMultiplexer.
http_request: A Request to send.
redirections: (int, default 5) Number of redirects to follow.
check_response_func: Function to validate the HTTP response.
Arguments are (Response, response content, url).

Returns:
A Response object.

Raises:
RequestError if no response could be parsed.


def controlled_by(self, *control_qubits: Qid) -> 'Operation':

        from cirq.ops import ControlledOperation
        if control_qubits is None or len(control_qubits) is 0:
            raise ValueError(
                "Can't get controlled operation without control qubit. Op: {}"
                .format(repr(self)))
        else:
            return ControlledOperation(control_qubits, self)

Returns a controlled version of this operation.

Args:
control_qubits: Qubits to control the operation by. Required.


def convert_reshape(params, w_name, scope_name, inputs, layers, weights, names):

    print('Converting reshape ...')
    if names == 'short':
        tf_name = 'RESH' + random_string(4)
    elif names == 'keep':
        tf_name = w_name
    else:
        tf_name = w_name + str(random.random())

    if len(inputs) > 1:
        if layers[inputs[1]][0] == -1:
            print('Cannot deduct batch size! It will be omitted, but result may be wrong.')

        reshape = keras.layers.Reshape(layers[inputs[1] + '_np'], name=tf_name)
        layers[scope_name] = reshape(layers[inputs[0]])
    else:
        if inputs[0] in layers:
            reshape = keras.layers.Reshape(params['shape'][1:], name=tf_name)
            layers[scope_name] = reshape(layers[inputs[0]])
        else:
            print('Skip weight matrix transpose, but result may be wrong.')

Convert reshape layer.

Args:
params: dictionary with layer parameters
w_name: name prefix in state_dict
scope_name: pytorch scope name
inputs: pytorch node inputs
layers: dictionary with keras tensors
weights: pytorch state_dict
names: use short names for keras layers


def _generate_assignments(splittable_dimensions, mesh_dimension_to_size):
    assignments = []
    for assignment_size in six.moves.xrange((1 + min(len(splittable_dimensions), len(mesh_dimension_to_size)))):
        for s_dims_chosen in itertools.combinations(splittable_dimensions, assignment_size):
            for m_dims_chosen in itertools.permutations(mesh_dimension_to_size, assignment_size):
                assignments.append(dict(zip(s_dims_chosen, m_dims_chosen)))
    return assignments

Generates all ways to map splittable dimensions to mesh dimensions.

Args:
splittable_dimensions: a frozenset of the names of splittable dimensions.
mesh_dimension_to_size: a dictionary from mesh dimension name to size.

Returns:
A list of the valid assignments. Each assignment is a dict keyed by every
splittable dimension, whose value is either a mesh dimension or None.


def _print_args(arguments, argument_type='Argument', indent=0):
    indent_str = '  ' * indent

    def _maybe_add_quotes(value):
        is_quotes = "'" * isinstance(value, str)
        return is_quotes + str(value) + is_quotes

    def in_print(s, end='\n'):
        print(indent_str + s, end=end)
    for index, element in enumerate(arguments, 1):
        if indent == 4:
            in_print('%s
        if isinstance(element, str):
            in_print('  %s' % element)
        elif isinstance(element, tensor_spec.TensorSpec):
            print((indent + 1) * '  ' + '%s: %s' % (element.name, repr(element)))
        elif isinstance(element, collections_abc.Iterable) and (not isinstance(element, dict)):
            in_print('  DType: %s' % type(element).__name__)
            in_print('  Value: [', end='')
            for value in element:
                print('%s' % _maybe_add_quotes(value), end=', ')
            print('\x08\x08]')
        elif isinstance(element, dict):
            in_print('  DType: %s' % type(element).__name__)
            in_print('  Value: {', end='')
            for key, value in element.items():
                print("'%s': %s" % (str(key), _maybe_add_quotes(value)), end=', ')
            print('\x08\x08}')
        else:
            in_print('  DType: %s' % type(element).__name__)
            in_print('  Value: %s' % str(element))

Formats and prints the argument of the concrete functions defined in the model.

Args:
arguments: Arguments to format print.
argument_type: Type of arguments.
indent: How far (in increments of 2 spaces) to indent each line of
output.


def ParseCodeToTree(code):
    if not code.endswith(os.linesep):
        code += os.linesep
    try:
        parser_driver = driver.Driver(_PYTHON_GRAMMAR, convert=pytree.convert)
        tree = parser_driver.parse_string(code, debug=False)
    except parse.ParseError:
        ast.parse(code)
        raise
    return _WrapEndMarker(tree)

Parse the given code to a lib2to3 pytree.

Arguments:
code: a string with the code to parse.

Raises:
SyntaxError if the code is invalid syntax.
parse.ParseError if some other parsing failure.

Returns:
The root node of the parsed tree.


def _build_all_reduce_ring(core_locations: List[_CoreLocation], rotate: bool=False) -> List[int]:
    permutation = list(range(len(core_locations)))
    if not permutation:
        return permutation
    logging.vlog(2, 'Core locations in: %s', core_locations)
    first_column = min([l.x for l in core_locations])
    first_row = min([l.y for l in core_locations])
    same_z = len(set([l.z for l in core_locations])) == 1
    logging.vlog(2, 'first_column: %d', first_column)
    logging.vlog(2, 'first_row: %d', first_row)
    logging.vlog(2, 'same_z: %s', same_z)

    def _cmp_2d(ia: int, ib: int) -> int:
        if not rotate:
            a = core_locations[ia]
            b = core_locations[ib]
            a_first = a.x == first_column and a.y != first_row
            b_first = b.x == first_column and b.y != first_row
            if a_first != b_first:
                return -1 if b_first else 1
            if a.y != b.y:
                return b.y - a.y if a_first else a.y - b.y
            if a.x != b.x:
                return a.x - b.x if a.y % 2 == 0 else b.x - a.x
            return a.core - b.core
        else:
            a = core_locations[ia]
            b = core_locations[ib]
            a_first = a.y == first_row and a.x != first_column
            b_first = b.y == first_row and b.x != first_column
            if a_first != b_first:
                return -1 if b_first else 1
            if a.x != b.x:
                return b.x - a.x if a_first else a.x - b.x
            if a.y != b.y:
                return a.y - b.y if a.x % 2 == 0 else b.y - a.y
            return a.core - b.core

    def _cmp_3d(ia: int, ib: int) -> int:
        a = core_locations[ia]
        b = core_locations[ib]
        a_corner = a.x == first_column and a.y == first_row
        b_corner = b.x == first_column and b.y == first_row
        if a_corner and b_corner:
            return b.z - a.z if a.z != b.z else a.core - b.core
        if a_corner != b_corner:
            return -1 if b_corner else 1
        if a.z == b.z:
            return _cmp_2d(ia, ib) if a.z % 2 == 0 else -_cmp_2d(ia, ib)
        return a.z - b.z
    if same_z:
        permutation.sort(key=functools.cmp_to_key(_cmp_2d))
    else:
        permutation.sort(key=functools.cmp_to_key(_cmp_3d))
    logging.vlog(2, 'Permutation out: %s', permutation)
    return permutation

Reorders a list of TPU cores to optimize for AllReduce performance.

This is ported from the C++ tensorflow::BuildAllReduceRing function,
mixed with some logic from TF TPU's device_assignment._ring_3d.

Args:
core_locations: A list of core locations expressed as [x, y, z, core].
rotate: If true, scan the cores in a column-major order. False by default.

Returns:
A permutation of the input list such that neighbors in the sequence are
nearby in the TPU topology.


def from_dict(cls, cls_dict, fallback_xsi_type=None):

        if not cls_dict:
            return None

        if isinstance(cls_dict, six.string_types):
            if not getattr(cls, "_convert_strings", False):
                return cls_dict

        try:
            typekey = cls.dictkey(cls_dict)
        except TypeError:
            typekey = fallback_xsi_type
        klass   = cls.entity_class(typekey)
        return klass.from_dict(cls_dict)

Parse the dictionary and return an Entity instance.

This will attempt to extract type information from the input
dictionary and pass it to entity_class to resolve the correct class
for the type.

Args:
cls_dict: A dictionary representation of an Entity object.
fallback_xsi_type: An xsi_type to use for string input, which
doesn't have properties

Returns:
An Entity instance.


def generate_link(flag, np_fun_name):
    if flag == 'dev':
        template = 'https:
    elif flag == 'stable':
        template = 'https:
    elif re.match('\\d+(\\.\\d+(\\.\\d+)?)?$', flag):
        template = f'https:
    else:
        return None
    return template % np_fun_name

Generates link from numpy function name.

Args:
flag: the flag to control link form. See `set_np_doc_form`.
np_fun_name: the numpy function name.

Returns:
A string.


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

Sets the access control policy on the specified resource. Replaces any existing policy. Can return `NOT_FOUND`, `INVALID_ARGUMENT`, and `PERMISSION_DENIED` errors.

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


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 find_usbserial(vendor, product):
    if (platform.system() == 'Linux'):
        (vendor, product) = [('%04x' % x).strip() for x in (vendor, product)]
        return linux_find_usbserial(vendor, product)
    elif (platform.system() == 'Darwin'):
        return osx_find_usbserial(vendor, product)
    else:
        raise NotImplementedError(('Cannot find serial ports on %s' % platform.system()))

Find the tty device for a given usbserial devices identifiers.

Args:
vendor: (int) something like 0x0000
product: (int) something like 0x0000

Returns:
String, like /dev/ttyACM0 or /dev/tty.usb...


def ParseOptions(cls, options, configuration_object):

    if not isinstance(configuration_object, tools.CLITool):
      raise errors.BadConfigObject(
          'Configuration object is not an instance of CLITool')

    artifacts_path = getattr(options, 'artifact_definitions_path', None)

    data_location = getattr(configuration_object, '_data_location', None)
    if ((not artifacts_path or not os.path.exists(artifacts_path)) and
        data_location):
      artifacts_path = os.path.dirname(data_location)
      artifacts_path = os.path.join(artifacts_path, 'artifacts')

      if not os.path.exists(artifacts_path) and 'VIRTUAL_ENV' in os.environ:
        artifacts_path = os.path.join(
            os.environ['VIRTUAL_ENV'], 'share', 'artifacts')

      if not os.path.exists(artifacts_path):
        artifacts_path = os.path.join(sys.prefix, 'share', 'artifacts')
      if not os.path.exists(artifacts_path):
        artifacts_path = os.path.join(sys.prefix, 'local', 'share', 'artifacts')

      if sys.prefix != '/usr':
        if not os.path.exists(artifacts_path):
          artifacts_path = os.path.join('/usr', 'share', 'artifacts')
        if not os.path.exists(artifacts_path):
          artifacts_path = os.path.join('/usr', 'local', 'share', 'artifacts')

      if not os.path.exists(artifacts_path):
        artifacts_path = None

    if not artifacts_path or not os.path.exists(artifacts_path):
      raise errors.BadConfigOption(
          'Unable to determine path to artifact definitions.')

    custom_artifacts_path = getattr(
        options, 'custom_artifact_definitions_path', None)

    if custom_artifacts_path and not os.path.isfile(custom_artifacts_path):
      raise errors.BadConfigOption(
          'No such artifacts filter file: {0:s}.'.format(custom_artifacts_path))

    if custom_artifacts_path:
      logger.info(
          'Custom artifact filter file: {0:s}'.format(custom_artifacts_path))

    registry = artifacts_registry.ArtifactDefinitionsRegistry()
    reader = artifacts_reader.YamlArtifactsReader()

    logger.info(
        'Determined artifact definitions path: {0:s}'.format(artifacts_path))

    try:
      registry.ReadFromDirectory(reader, artifacts_path)

    except (KeyError, artifacts_errors.FormatError) as exception:
      raise errors.BadConfigOption((
          'Unable to read artifact definitions from: {0:s} with error: '
          '{1!s}').format(artifacts_path, exception))

    for name in preprocessors_manager.PreprocessPluginsManager.GetNames():
      if not registry.GetDefinitionByName(name):
        raise errors.BadConfigOption(
            'Missing required artifact definition: {0:s}'.format(name))

    if custom_artifacts_path:
      try:
        registry.ReadFromFile(reader, custom_artifacts_path)

      except (KeyError, artifacts_errors.FormatError) as exception:
        raise errors.BadConfigOption((
            'Unable to read artifact definitions from: {0:s} with error: '
            '{1!s}').format(custom_artifacts_path, exception))

    setattr(configuration_object, '_artifact_definitions_path', artifacts_path)
    setattr(
        configuration_object, '_custom_artifacts_path', custom_artifacts_path)

Parses and validates options.

Args:
options (argparse.Namespace): parser options.
configuration_object (CLITool): object to be configured by the argument
helper.

Raises:
BadConfigObject: when the configuration object is of the wrong type.
BadConfigOption: if the required artifact definitions are not defined.


def download(self, file: Optional[IO]=None, rewind: bool=True,
                 duration_timeout: Optional[float]=None) -> Response:

        if self._session_state != SessionState.file_request_sent:
            raise RuntimeError('File request not sent')

        if rewind and file and hasattr(file, 'seek'):
            original_offset = file.tell()
        else:
            original_offset = None

        if not hasattr(file, 'drain'):
            self._response.body = file

            if not isinstance(file, Body):
                self._response.body = Body(file)

        read_future = self._commander.read_stream(file, self._data_stream)

        try:
            reply = yield from \
                asyncio.wait_for(read_future, timeout=duration_timeout)
        except asyncio.TimeoutError as error:
            raise DurationTimeout(
                'Did not finish reading after {} seconds.'
                .format(duration_timeout)
            ) from error

        self._response.reply = reply

        if original_offset is not None:
            file.seek(original_offset)

        self.event_dispatcher.notify(self.Event.end_transfer, self._response)

        self._session_state = SessionState.response_received

        return self._response

Read the response content into file.

Args:
file: A file object or asyncio stream.
rewind: Seek the given file back to its original offset after
reading is finished.
duration_timeout: Maximum time in seconds of which the
entire file must be read.

Returns:
A Response populated with the final data connection reply.

Be sure to call :meth:`start` first.

Coroutine.


def run_inference(self, batch: Sequence[ExampleT], model: ModelT, inference_args: Optional[dict[str, Any]]=None) -> Iterable[PredictionT]:
    raise NotImplementedError(type(self))

Runs inferences on a batch of examples.

Args:
batch: A sequence of examples or features.
model: The model used to make inferences.
inference_args: Extra arguments for models whose inference call requires
extra parameters.

Returns:
An Iterable of Predictions.


def apply(self, predictions: Iterable[AnomalyPrediction]) -> AnomalyPrediction:
    raise NotImplementedError

Applies the aggregation function to an iterable of predictions, either on
their outlier scores or labels.

Args:
predictions: An Iterable of `AnomalyPrediction` objects to aggregate.

Returns:
An `AnomalyPrediction` object containing the aggregated result.


def set_redirect(self, url, status=HttpStatusCodes.HTTP_303):

        self.set_status(status)
        self.set_content('')
        self.set_header(HttpResponseHeaders.LOCATION, url)

Helper method to set a redirect response.

Args:
url (:obj:`str`): URL to redirect to
status (:obj:`str`, optional): Status code of the response


def _PackArgumentsHelper(self, elem, data, set_type_attrs):
    if self._packer:
        data = self._packer.Pack(data, self._version)
    if isinstance(data, dict):
        type_override = data.get('xsi_type')
        if type_override:
            elem_type = self._DiscoverElementTypeFromLocalname(type_override)
        else:
            elem_type = elem.type
        data_formatted = data.iteritems()
        packed_result = self._CreateComplexTypeFromData(elem_type, (type_override is not None), data_formatted, set_type_attrs)
    elif isinstance(data, zeep.xsd.CompoundValue):
        elem_type = data._xsd_type
        data_formatted = zip(dir(data), [data[k] for k in dir(data)])
        packed_result = self._CreateComplexTypeFromData(elem_type, False, data_formatted, set_type_attrs)
    elif isinstance(data, (list, tuple)):
        packed_result = [self._PackArgumentsHelper(elem, item, set_type_attrs) for item in data]
    else:
        if ((elem.type.name == 'base64Binary') and self._IsBase64(data)):
            _logger.warn('Passing data to base64 field %s that may already be encoded. Do not pre-encode base64 fields with zeep.', elem.name)
        packed_result = data
    return packed_result

Recursive helper for PackArguments.

Args:
elem: The element type we are creating.
data: The data to instantiate it with.
set_type_attrs: A boolean indicating whether or not attributes that end
in .Type should be set. This is only necessary for batch job service.

Returns:
An instance of type 'elem'.


def _piecewise_learning_rate(step, boundaries, values):

  values = [1.0] + values
  boundaries = [float(x) for x in boundaries]
  return tf.train.piecewise_constant(
      step, boundaries, values, name="piecewise_lr")

Scale learning rate according to the given schedule.

Multipliers are not cumulative.

Args:
step: global step
boundaries: List of steps to transition on.
values: Multiplier to apply at each boundary transition.

Returns:
Scaled value for the learning rate.


def generate(self, information, timeout=-1):

        return self._client.create(information, timeout=timeout)

Generates a self signed certificate or an internal CA signed certificate for RabbitMQ clients.

Args:
information (dict): Information to generate the certificate for RabbitMQ clients.
timeout:
Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation
in OneView; it just stops waiting for its completion.

Returns:
dict: RabbitMQ certificate generated


def _validate_exp(claims, leeway=0):
    if ('exp' not in claims):
        return
    try:
        exp = int(claims['exp'])
    except ValueError:
        raise JWTClaimsError('Expiration Time claim (exp) must be an integer.')
    now = timegm(datetime.utcnow().utctimetuple())
    if (exp < (now - leeway)):
        raise ExpiredSignatureError('Signature has expired.')

Validates that the 'exp' claim is valid.

The "exp" (expiration time) claim identifies the expiration time on
or after which the JWT MUST NOT be accepted for processing.  The
processing of the "exp" claim requires that the current date/time
MUST be before the expiration date/time listed in the "exp" claim.
Implementers MAY provide for some small leeway, usually no more than
a few minutes, to account for clock skew.  Its value MUST be a number
containing a NumericDate value.  Use of this claim is OPTIONAL.

Args:
claims (dict): The claims dictionary to validate.
leeway (int): The number of seconds of skew that is allowed.


def list_tags(self, image_name):
    tags_url = (self.registry_url + '/v2/{}/tags/list')
    r = self.get(tags_url.format(image_name), auth=self.auth)
    data = r.json()
    if ('tags' in data):
        return reversed(sorted(data['tags']))
    return []

List all tags for the given image stored in the registry.

Args:
image_name (str):
The name of the image to query. The image must be present on the
registry for this call to return any values.
Returns:
list[str]: List of tags for that image.


def open_tunnel(self, serial_no, port=19020):

        return self.open(ip_addr='tunnel:' + str(serial_no) + ':' + str(port))

Connects to the J-Link emulator (over SEGGER tunnel).

Args:
self (JLink): the ``JLink`` instance
serial_no (int): serial number of the J-Link
port (int): optional port number (default to 19020).

Returns:
``None``


def find_element_by_id(self, id_, update=False) -> Elements:

        return self.find_element(by=By.ID, value=id_, update=update)

Finds an element by id.

Args:
id_: The id of the element to be found.
update: If the interface has changed, this option should be True.

Returns:
The element if it was found.

Raises:
NoSuchElementException - If the element wasn't found.

Usage:
element = driver.find_element_by_id('foo')


def update_mim_version(self, new_genes, new_panel, old_version):

        LOG.info('Updating versions for new genes')
        version = new_panel['version']
        for gene in new_panel['genes']:
            gene_symbol = gene['hgnc_id']

            if gene_symbol in new_genes:
                gene['database_entry_version'] = version
                continue

            gene['database_entry_version'] = old_version

        return

Set the correct version for each gene
Loop over the genes in the new panel

Args:
new_genes(set(str)): Set with the new gene symbols
new_panel(dict)


def chain_to_quadratic(chain, target_adjacency, chain_strength):
    quadratic = {}
    seen = set()
    try:
        next_level = {next(iter(chain))}
    except StopIteration:
        raise ValueError('chain must have at least one variable')
    while next_level:
        this_level = next_level
        next_level = set()
        for v in this_level:
            if (v not in seen):
                seen.add(v)
                for u in target_adjacency[v]:
                    if (u not in chain):
                        continue
                    next_level.add(u)
                    if ((u != v) and ((u, v) not in quadratic)):
                        quadratic[(v, u)] = (- chain_strength)
    if (len(chain) != len(seen)):
        raise ValueError('{} is not a connected chain'.format(chain))
    return quadratic

Determine the quadratic biases that induce the given chain.

Args:
chain (iterable):
The variables that make up a chain.

target_adjacency (dict/:class:`networkx.Graph`):
Should be a dict of the form {s: Ns, ...} where s is a variable
in the target graph and Ns is the set of neighbours of s.

chain_strength (float):
The magnitude of the quadratic bias that should be used to create chains.

Returns:
dict[edge, float]: The quadratic biases that induce the given chain.

Raises:
ValueError: If the variables in chain do not form a connected subgraph of target.

Examples:
>>> chain = {1, 2}
>>> target_adjacency = {0: {1, 2}, 1: {0, 2}, 2: {0, 1}}
>>> dimod.embedding.chain_to_quadratic(chain, target_adjacency, 1)
{(1, 2): -1}


def _CallMethod(self, srvc, method_descriptor, rpc_controller, request, callback):
    if (method_descriptor.containing_service != self.descriptor):
        raise RuntimeError('CallMethod() given method descriptor for wrong service type.')
    method = getattr(srvc, method_descriptor.name)
    return method(rpc_controller, request, callback)

Calls the method described by a given method descriptor.

Args:
srvc: Instance of the service for which this method is called.
method_descriptor: Descriptor that represent the method to call.
rpc_controller: RPC controller to use for this method's execution.
request: Request protocol message.
callback: A callback to invoke after the method has completed.


def length_squared(x, keep_dims=False, name=None, reduction_dim=None):

  with tf.name_scope(name, 'length_squared', [x]) as scope:
    x = tf.convert_to_tensor(x, name='x')
    if not reduction_dim:
      reduction_dim = _last_index(x, 1)
    return tf.reduce_sum(
        tf.square(x),
        reduction_dim,
        keep_dims=keep_dims,
        name=scope)

Computes the squared length of x.

Args:
x: A tensor.
keep_dims: If true, reduction does not change the rank of the input.
name: Optional name for this op.
reduction_dim: The dimension to reduce, by default choose the last one
and if no shape is specified guess 1.
Returns:
The squared length of x.


def combine_columns(columns):

    columns_zipped = itertools.zip_longest(*columns)
    return ''.join(x for zipped in columns_zipped for x in zipped if x)

Combine ``columns`` into a single string.

Example:
>>> combine_columns(['eape', 'xml'])
'example'

Args:
columns (iterable): ordered columns to combine

Returns:
String of combined columns


def _gather_saveables_for_checkpoint(self):

    def _saveable_factory(name=self._common_name):
        return _SyncOnReadSaveable(self, name)
    return {trackable.VARIABLE_VALUE_KEY: _saveable_factory}

Overrides Trackable method.

This allows both name-based and object-based save and restore of
`SyncOnReadVariable`s.

Returns:
A dictionary mapping attribute names to `SaveableObject` factories.


def GetBlockHash(self, height):

        if self._current_block_height < height:
            return

        if len(self._header_index) <= height:
            return

        return self._header_index[height]

Get the block hash by its block height
Args:
height(int): height of the block to retrieve hash from.

Returns:
bytes: a non-raw block hash (e.g. b'6dd83ed8a3fc02e322f91f30431bf3662a8c8e8ebe976c3565f0d21c70620991', but not b'\x6d\xd8...etc'


def _AddParentDirectories(self, path):
    path_segments = self.file_system.SplitPath(path)
    for segment_index in range(len(path_segments)):
        parent_path = self.file_system.JoinPath(path_segments[:segment_index])
        file_entry = self.file_system.GetFileEntryByPath(parent_path)
        if (file_entry and (not file_entry.IsDirectory())):
            raise ValueError('Non-directory parent file entry: {0:s} already exists.'.format(parent_path))
    for segment_index in range(len(path_segments)):
        parent_path = self.file_system.JoinPath(path_segments[:segment_index])
        if (not self.file_system.FileEntryExistsByPath(parent_path)):
            self.file_system.AddFileEntry(parent_path, file_entry_type=definitions.FILE_ENTRY_TYPE_DIRECTORY)

Adds the parent directories of a path to the fake file system.

Args:
path (str): path of the file within the fake file system.

Raises:
ValueError: if a parent directory is already set and is not a directory.


def parse_arguments(argv):
    parser = argparse.ArgumentParser(description='write-to-pubsub')
    parser.add_argument('-m', '--mode', help='Mode to run pipeline in.', choices=['local', 'cloud'], default='local')
    parser.add_argument('-p', '--project', help='GCP project to run pipeline on.', default=cfg.PROJECT_ID)
    args, _ = parser.parse_known_args(args=argv)
    return args

It parses the arguments passed to the command line and returns them as an object

Args:
argv: The arguments passed to the command line.

Returns:
The arguments that are being passed in.


def _read_hdf_columns(path_or_buf, columns, num_splits, kwargs):
    df = pandas.read_hdf(path_or_buf, columns=columns, **kwargs)
    return (_split_result_for_readers(0, num_splits, df) + [len(df.index)])

Use a Ray task to read columns from HDF5 into a Pandas DataFrame.

Note: Ray functions are not detected by codecov (thus pragma: no cover)

Args:
path_or_buf: The path of the HDF5 file.
columns: The list of column names to read.
num_splits: The number of partitions to split the column into.

Returns:
A list containing the split Pandas DataFrames and the Index as the last
element. If there is not `index_col` set, then we just return the length.
This is used to determine the total length of the DataFrame to build a
default Index.


def edge(self, tail_name, head_name, label=None, _attributes=None, **attrs):
    tail_name = self._quote_edge(tail_name)
    head_name = self._quote_edge(head_name)
    attr_list = self._attr_list(label, attrs, _attributes)
    line = (self._edge % (tail_name, head_name, attr_list))
    self.body.append(line)

Create an edge between two nodes.

Args:
tail_name: Start node identifier.
head_name: End node identifier.
label: Caption to be displayed near the edge.
attrs: Any additional edge attributes (must be strings).


def request_stop(self, ex=None):
    self._coord.request_stop(ex=ex)

Request that the coordinator stop the threads.

See `Coordinator.request_stop()`.

Args:
ex: Optional `Exception`, or Python `exc_info` tuple as returned by
`sys.exc_info()`.  If this is the first call to `request_stop()` the
corresponding exception is recorded and re-raised from `join()`.


def _get_bonds(self, mol):

        num_atoms = len(mol)

        if self.ignore_ionic_bond:
            covalent_atoms = [i for i in range(num_atoms) if mol.species[i].symbol not in self.ionic_element_list]
        else:
            covalent_atoms = list(range(num_atoms))
        all_pairs = list(itertools.combinations(covalent_atoms, 2))
        pair_dists = [mol.get_distance(*p) for p in all_pairs]
        elements = mol.composition.as_dict().keys()
        unavailable_elements = list(set(elements) -
                                    set(self.covalent_radius.keys()))
        if len(unavailable_elements) > 0:
            raise ValueError("The covalent radius for element {} is not "
                             "available".format(unavailable_elements))
        bond_13 = self.get_13_bonds(self.priority_bonds)
        max_length = [(self.covalent_radius[mol.sites[p[0]].specie.symbol] +
                       self.covalent_radius[mol.sites[p[1]].specie.symbol]) *
                      (1 + (self.priority_cap if p in self.priority_bonds
                            else (self.bond_length_cap if p not in bond_13
                                  else self.bond_13_cap))) *
                      (0.1 if (self.ignore_halogen_self_bond and p not in self.priority_bonds and
                               mol.sites[p[0]].specie.symbol in self.halogen_list and
                               mol.sites[p[1]].specie.symbol in self.halogen_list)
                       else 1.0)
                      for p in all_pairs]

        bonds = [bond
                 for bond, dist, cap in zip(all_pairs, pair_dists, max_length)
                 if dist <= cap]
        return bonds

Find all the bond in a molcule

Args:
mol: the molecule. pymatgen Molecule object

Returns:
List of tuple. Each tuple correspond to a bond represented by the
id of the two end atoms.


def classify_coupling(coupling):
    (lower, upper) = coupling
    if ((lower is None) and (upper is None)):
        return CouplingClass.Uncoupled
    elif ((lower is None) or (upper is None)):
        return CouplingClass.DirectionalReverse
    elif ((lower == 0.0) and (upper == 0.0)):
        return CouplingClass.Inconsistent
    elif ((lower <= 0.0) and (upper >= 0.0)):
        return CouplingClass.DirectionalForward
    elif (abs((lower - upper)) < 1e-06):
        return CouplingClass.Full
    else:
        return CouplingClass.Partial

Return a constant indicating the type of coupling.

Depending on the type of coupling, one of the constants from
:class:`.CouplingClass` is returned.

Args:
coupling: Tuple of minimum and maximum flux ratio


def __init__(self, option):

        self.option = option
        super().__init__('invalid option name: {}'.format(option))

Initialization of instances:

Args:
option (str): invalid option name.

Attributes:
option (str): invalid option name.


def extend(self, step):

        self.timesteps.extend(step.timesteps)
        self.masks.extend(step.masks)
        self.x.extend(step.x)
        self.y.extend(step.y)
        self.i.extend(step.i)
        self.j.extend(step.j)
        self.end_time = step.end_time
        self.times = np.arange(self.start_time, self.end_time + self.step, self.step)
        self.u = np.concatenate((self.u, step.u))
        self.v = np.concatenate((self.v, step.v))
        for attr in self.attributes.keys():
            if attr in step.attributes.keys():
                self.attributes[attr].extend(step.attributes[attr])

Adds the data from another STObject to this object.

Args:
step: another STObject being added after the current one in time.


def getFilepaths(self, filename):

        return (os.path.join(os.environ['HOME'], filename),
                os.path.join(self.mackup.mackup_folder, filename))

Get home and mackup filepaths for given file

Args:
filename (str)

Returns:
home_filepath, mackup_filepath (str, str)


def align_segmentation(beat_times, song):

    try:
        segment_times, segment_labels = msaf.io.read_references(song)
    except:
        return None, None, None
    segment_times = np.asarray(segment_times)

    segment_intervals = msaf.utils.times_to_intervals(segment_times)

    beat_intervals = np.asarray(zip(beat_times[:-1], beat_times[1:]))

    beat_segment_ids = librosa.util.match_intervals(beat_intervals,
                                                    segment_intervals)

    segment_beats = []
    segment_times_out = []
    segment_labels_out = []

    for i in range(segment_times.shape[0]):
        hits = np.argwhere(beat_segment_ids == i)
        if len(hits) > 0 and i < len(segment_intervals) and \
                i < len(segment_labels):
            segment_beats.extend(hits[0])
            segment_times_out.append(segment_intervals[i, :])
            segment_labels_out.append(segment_labels[i])

    segment_beats = list(segment_beats)

    segment_times_out = segment_times

    return segment_beats, segment_times_out, segment_labels_out

Load a ground-truth segmentation, and align times to the nearest
detected beats.

Arguments:
beat_times -- array
song -- path to the audio file

Returns:
segment_beats -- array
beat-aligned segment boundaries

segment_times -- array
true segment times

segment_labels -- array
list of segment labels


def get_capacity_vol(self, min_voltage=None, max_voltage=None, use_overall_normalization=True):
    pairs_in_range = self._select_in_voltage_range(min_voltage, max_voltage)
    normalization_vol = (self.normalization_volume if (use_overall_normalization or (len(pairs_in_range) == 0)) else pairs_in_range[(- 1)].vol_discharge)
    return (((sum([pair.mAh for pair in pairs_in_range]) / normalization_vol) * 1e+24) / N_A)

Get the volumetric capacity of the electrode.

Args:
min_voltage (float): The minimum allowable voltage for a given
step.
max_voltage (float): The maximum allowable voltage allowable for a
given step.
use_overall_normalization (booL): If False, normalize by the
discharged state of only the voltage pairs matching the voltage
criteria. if True, use default normalization of the full
electrode path.

Returns:
Volumetric capacity in mAh/cc across the insertion path (a subset
of the path can be chosen by the optional arguments)


def read(self, input_buffer, kmip_version=enums.KMIPVersion.KMIP_1_0):
    super(CreateKeyPairResponsePayload, self).read(input_buffer, kmip_version=kmip_version)
    local_buffer = utils.BytearrayStream(input_buffer.read(self.length))
    if self.is_tag_next(enums.Tags.PRIVATE_KEY_UNIQUE_IDENTIFIER, local_buffer):
        self._private_key_unique_identifier = primitives.TextString(tag=enums.Tags.PRIVATE_KEY_UNIQUE_IDENTIFIER)
        self._private_key_unique_identifier.read(local_buffer, kmip_version=kmip_version)
    else:
        raise exceptions.InvalidKmipEncoding('The CreateKeyPair response payload encoding is missing the private key unique identifier.')
    if self.is_tag_next(enums.Tags.PUBLIC_KEY_UNIQUE_IDENTIFIER, local_buffer):
        self._public_key_unique_identifier = primitives.TextString(tag=enums.Tags.PUBLIC_KEY_UNIQUE_IDENTIFIER)
        self._public_key_unique_identifier.read(local_buffer, kmip_version=kmip_version)
    else:
        raise exceptions.InvalidKmipEncoding('The CreateKeyPair response payload encoding is missing the public key unique identifier.')
    if (kmip_version < enums.KMIPVersion.KMIP_2_0):
        if self.is_tag_next(enums.Tags.PRIVATE_KEY_TEMPLATE_ATTRIBUTE, local_buffer):
            self._private_key_template_attribute = objects.TemplateAttribute(tag=enums.Tags.PRIVATE_KEY_TEMPLATE_ATTRIBUTE)
            self._private_key_template_attribute.read(local_buffer, kmip_version=kmip_version)
        if self.is_tag_next(enums.Tags.PUBLIC_KEY_TEMPLATE_ATTRIBUTE, local_buffer):
            self._public_key_template_attribute = objects.TemplateAttribute(tag=enums.Tags.PUBLIC_KEY_TEMPLATE_ATTRIBUTE)
            self._public_key_template_attribute.read(local_buffer, kmip_version=kmip_version)
    self.is_oversized(local_buffer)

Read the data encoding the CreateKeyPair response payload and decode it
into its constituent parts.

Args:
input_buffer (stream): A data buffer containing encoded object
data, supporting a read method.
kmip_version (KMIPVersion): An enumeration defining the KMIP
version with which the object will be decoded. Optional,
defaults to KMIP 1.0.

Raises:
InvalidKmipEncoding: Raised if the private key unique identifier or
the public key unique identifier is missing from the encoded
payload.


def PopEventSource(self):
    try:
        (_, _, event_source) = heapq.heappop(self._heap)
    except IndexError:
        return None
    return event_source

Pops an event source from the heap.

Returns:
EventSource: an event source or None on if no event source is available.


def _hexencode(bytestring, insert_spaces=False):
    _checkString(bytestring, description='byte string')
    separator = ('' if (not insert_spaces) else ' ')
    byte_representions = []
    for c in bytestring:
        byte_representions.append('{0:02X}'.format(ord(c)))
    return separator.join(byte_representions).strip()

Convert a byte string to a hex encoded string.

For example 'J' will return '4A', and ``'\\x04'`` will return '04'.

Args:
bytestring (str): Can be for example ``'A\\x01B\\x45'``.
insert_spaces (bool): Insert space characters between pair of characters to increase readability.

Returns:
A string of twice the length, with characters in the range '0' to '9' and 'A' to 'F'.
The string will be longer if spaces are inserted.

Raises:
TypeError, ValueError


def GetTopLevel(self, file_object):

    try:
      top_level_object = biplist.readPlist(file_object)

    except (biplist.InvalidPlistException,
            biplist.NotBinaryPlistException) as exception:
      raise errors.UnableToParseFile(
          'Unable to parse plist with error: {0!s}'.format(exception))

    return top_level_object

Returns the deserialized content of a plist as a dictionary object.

Args:
file_object (dfvfs.FileIO): a file-like object to parse.

Returns:
dict[str, object]: contents of the plist.

Raises:
UnableToParseFile: when the file cannot be parsed.


def retrieve_taf(station_icao) -> typing.Tuple[(typing.Union[(str, None)], typing.Union[(str, None)])]:
    url = _BASE_TAF_URL.format(station=station_icao)
    with requests.get(url) as resp:
        if (not resp.ok):
            return (f'''unable to obtain TAF for station {station_icao}
Got to "http:
        return (None, resp.content.decode().split('\n')[1])

Retrieves a TAF string from an online database

Args:
station_icao: ICAO of the station

Returns:
tuple of error, metar_str


def get_path_to_datafile(path):
    if runfiles:
        r = runfiles.Create()
        new_fpath = r.Rlocation(_os.path.abspath(_os.path.join('tensorflow', path)))
        if new_fpath is not None and _os.path.exists(new_fpath):
            return new_fpath
    old_filepath = _os.path.join(_os.path.dirname(_inspect.getfile(_sys._getframe(1))), path)
    return old_filepath

Get the path to the specified file in the data dependencies.

The path is relative to tensorflow/

Args:
path: a string resource path relative to tensorflow/

Returns:
The path to the specified file present in the data attribute of py_test
or py_binary.

Raises:
IOError: If the path is not found, or the resource can't be opened.


def publish(msg="checkpoint: publish package"):

    test = check()
    if test.succeeded:

        sdist = local("python setup.py sdist")
        if sdist.succeeded:
            build = local(
                'python setup.py build && python setup.py bdist_egg')
            if build.succeeded:
                upload = local("twine upload dist/*")
                if upload.succeeded:
                    tag()

Deploy the app to PYPI.

Args:
msg (str, optional): Description


def forward(self, hidden_states: torch.Tensor, position_embeddings: Optional[torch.Tensor]=None, reference_points=None, spatial_shapes=None, level_start_index=None, encoder_hidden_states: Optional[torch.Tensor]=None, encoder_attention_mask: Optional[torch.Tensor]=None, output_attentions: Optional[bool]=False):
    residual = hidden_states
    hidden_states, self_attn_weights = self.self_attn(hidden_states=hidden_states, position_embeddings=position_embeddings, output_attentions=output_attentions)
    hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
    hidden_states = residual + hidden_states
    hidden_states = self.self_attn_layer_norm(hidden_states)
    second_residual = hidden_states
    cross_attn_weights = None
    hidden_states, cross_attn_weights = self.encoder_attn(hidden_states=hidden_states, attention_mask=encoder_attention_mask, encoder_hidden_states=encoder_hidden_states, encoder_attention_mask=encoder_attention_mask, position_embeddings=position_embeddings, reference_points=reference_points, spatial_shapes=spatial_shapes, level_start_index=level_start_index, output_attentions=output_attentions)
    hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
    hidden_states = second_residual + hidden_states
    hidden_states = self.encoder_attn_layer_norm(hidden_states)
    residual = hidden_states
    hidden_states = self.activation_fn(self.fc1(hidden_states))
    hidden_states = nn.functional.dropout(hidden_states, p=self.activation_dropout, training=self.training)
    hidden_states = self.fc2(hidden_states)
    hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
    hidden_states = residual + hidden_states
    hidden_states = self.final_layer_norm(hidden_states)
    outputs = (hidden_states,)
    if output_attentions:
        outputs += (self_attn_weights, cross_attn_weights)
    return outputs

Args:
hidden_states (`torch.FloatTensor`):
Input to the layer of shape `(batch, seq_len, embed_dim)`.
position_embeddings (`torch.FloatTensor`, *optional*):
Position embeddings that are added to the queries and keys in the self-attention layer.
reference_points (`torch.FloatTensor`, *optional*):
Reference points.
spatial_shapes (`torch.LongTensor`, *optional*):
Spatial shapes.
level_start_index (`torch.LongTensor`, *optional*):
Level start index.
encoder_hidden_states (`torch.FloatTensor`):
cross attention input to the layer of shape `(batch, seq_len, embed_dim)`
encoder_attention_mask (`torch.FloatTensor`): encoder attention mask of size
`(batch, 1, target_len, source_len)` where padding elements are indicated by very large negative
values.
output_attentions (`bool`, *optional*):
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
returned tensors for more detail.


def get_model_indexes(model):
    indexes = []
    for index in get_index_names():
        for app_model in get_index_models(index):
            if (app_model == model):
                indexes.append(index)
    return indexes

Return list of all indexes in which a model is configured.

A model may be configured to appear in multiple indexes. This function
will return the names of the indexes as a list of strings. This is
useful if you want to know which indexes need updating when a model
is saved.

Args:
model: a Django model class.


def _add_row_partitions(self, flat_values, validate=False):
    if self.row_partitions:
        if validate:
            flat_values = self._validate_flat_values(flat_values)
        return ragged_tensor.RaggedTensor._from_nested_row_partitions(flat_values, self.row_partitions, validate=False)
    else:
        return flat_values

Add row partitions to flat_values, if necessary.

If the shape is truly ragged, then this adds the row_partitions.

The shape is dense, then this just returns flat_values.

Args:
flat_values: the flat_values of a ragged tensor with this shape, or a
dense tensor with this shape.
validate: validate the flat_values have the right first dimension.

Returns:
flat_values reshaped to have row_partitions.


def symbol_top(body_output, targets, model_hparams, vocab_size):

  del targets
  if model_hparams.shared_embedding_and_softmax_weights:
    scope_name = "shared"
    reuse = tf.AUTO_REUSE
  else:
    scope_name = "softmax"
    reuse = False
  with tf.variable_scope(scope_name, reuse=reuse):
    body_output_shape = common_layers.shape_list(body_output)
    var = get_weights(model_hparams, vocab_size, body_output_shape[-1])
    if (model_hparams.factored_logits and
        model_hparams.mode == tf.estimator.ModeKeys.TRAIN):

      body_output = tf.expand_dims(body_output, 3)
      return common_layers.FactoredTensor(body_output, var)
    else:
      body_output = tf.reshape(body_output, [-1, body_output_shape[-1]])
      logits = tf.matmul(body_output, var, transpose_b=True)
      return tf.reshape(logits,
                        body_output_shape[:-1] + [1, vocab_size])

Generate logits.

Args:
body_output: A Tensor with shape
[batch, p0, p1, model_hparams.hidden_size].
targets: Unused.
model_hparams: HParams, model hyperparmeters.
vocab_size: int, vocabulary size.

Returns:
logits: A Tensor with shape  [batch, p0, p1, ?, vocab_size].


def _get_event_id(object_type: str) -> str:

    key = _keys.event_counter(object_type)
    DB.watch(key, pipeline=True)
    count = DB.get_value(key)
    DB.increment(key)
    DB.execute()
    if count is None:
        count = 0
    return '{}_event_{:08d}'.format(object_type, int(count))

Return an event key for the event on the object type.

This must be a unique event id for the object.

Args:
object_type (str): Type of object

Returns:
str, event id


def convert(self, point):
    (x, y) = point
    (x1, y1) = ((x - self.x_offset), (y - self.y_offset))
    logger.debug('converted {} {} ==> {} {}'.format(x, y, x1, y1))
    return (x1, y1)

Convert a point from one coordinate system to another.

Args:
point: tuple(int x, int y)
The point in the original coordinate system.

Returns:
converted_point: tuple(int x, int y)
The point in the new coordinate system.

Example: convert coordinate from original image into a pixel location
within a cutout image.

@rtype: list(float,float)


def users_setPresence(self, *, presence: str, **kwargs) -> SlackResponse:

        kwargs.update({"presence": presence})
        return self.api_call("users.setPresence", json=kwargs)

Manually sets user presence.

Args:
presence (str): Either 'auto' or 'away'.


def convert_relu(params, w_name, scope_name, inputs, layers, weights, names):

    print('Converting relu ...')

    if names == 'short':
        tf_name = 'RELU' + random_string(4)
    elif names == 'keep':
        tf_name = w_name
    else:
        tf_name = w_name + str(random.random())

    relu = keras.layers.Activation('relu', name=tf_name)
    layers[scope_name] = relu(layers[inputs[0]])

Convert relu layer.

Args:
params: dictionary with layer parameters
w_name: name prefix in state_dict
scope_name: pytorch scope name
inputs: pytorch node inputs
layers: dictionary with keras tensors
weights: pytorch state_dict
names: use short names for keras layers


def aggregate_and_return_name_for_input(self, out_graphdef):
    flattened = self.flatten_nodes()
    if self.aggregation == OpHint.AGGREGATE_FIRST or self.aggregation == OpHint.AGGREGATE_LAST:
        assert len(flattened) == 1
    if len(flattened) == 1 and self.aggregation != OpHint.AGGREGATE_STACK:
        return _tensor_name_base(flattened[0].name)
    else:
        new_node = _node_def_pb2.NodeDef()
        new_node.op = 'Pack'
        new_node.name = 'OpHintStack-%s' % flattened[0].name
        new_node.attr['N'].i = len(flattened)
        new_node.attr['T'].type = flattened[0].attr['T'].type
        for discrete in flattened:
            new_node.input.append(_tensor_name_base(discrete.name))
        out_graphdef.node.extend([new_node])
        return new_node.name

This adds the nodes to out_graphdef and returns an aggregated output.

In particular, if you have 4 inputs to a hint stub, this will be the
node that you can use as an output. I.e. you have 4 timesteps from a
static rnn, then a fused UnidirectionalLSTM will expect 1 input with
all 4 time steps. So here we make a pack and return the output name of
that pack.

Args:
out_graphdef: A graphdef that is ready to have this input added.

Returns:
The name of a pack that aggregates this node.


def get(self, name):
    return self.prepare_model(self.client.api.inspect_image(name))

Gets an image.

Args:
name (str): The name of the image.

Returns:
(:py:class:`Image`): The image.

Raises:
:py:class:`docker.errors.ImageNotFound`
If the image does not exist.
:py:class:`docker.errors.APIError`
If the server returns an error.


def _parse_rd(self, config):

        match = RD_RE.search(config)
        if match:
            value = match.group('value')
        else:
            value = match
        return dict(rd=value)

_parse_rd scans the provided configuration block and extracts
the vrf rd. The return dict is intended to be merged into the response
dict.

Args:
config (str): The vrf configuration block from the nodes running
configuration

Returns:
dict: resource dict attribute


def _build_mac_signature_key_information(self, value):
    if (value is None):
        return None
    if (not isinstance(value, dict)):
        raise TypeError('MAC/signature key information must be a dictionary.')
    cryptographic_parameters = value.get('cryptographic_parameters')
    if cryptographic_parameters:
        cryptographic_parameters = self._build_cryptographic_parameters(cryptographic_parameters)
    mac_signature_key_information = cobjects.MACSignatureKeyInformation(unique_identifier=value.get('unique_identifier'), cryptographic_parameters=cryptographic_parameters)
    return mac_signature_key_information

Build an MACSignatureKeyInformation struct from a dictionary.

Args:
value (dict): A dictionary containing the key/value pairs for a
MACSignatureKeyInformation struct.

Returns:
MACSignatureInformation: a MACSignatureKeyInformation struct

Raises:
TypeError: if the input argument is invalid


def prefix(self, imod: YangIdentifier, mid: ModuleId) -> YangIdentifier:
    try:
        did = (imod, self.implement[imod])
    except KeyError:
        raise ModuleNotImplemented(imod) from None
    try:
        pmap = self.modules[mid].prefix_map
    except KeyError:
        raise ModuleNotRegistered(*mid) from None
    for p in pmap:
        if (pmap[p] == did):
            return p
    raise ModuleNotImported(imod, mid)

Return the prefix corresponding to an implemented module.

Args:
imod: Name of an implemented module.
mid: Identifier of the context module.

Raises:
ModuleNotImplemented: If `imod` is not implemented.
ModuleNotRegistered: If `mid` is not registered in YANG library.
ModuleNotImported: If `imod` is not imported in `mid`.


def delete(self, key):

    path = self.object_path(key)
    if os.path.exists(path):
      os.remove(path)

Removes the object named by `key`.

Args:
key: Key naming the object to remove.


def _import_and_bind(self, imp):

    with self.block.alloc_temp() as mod, \
        self.block.alloc_temp('[]*πg.Object') as mod_slice:
      self.writer.write_checked_call2(
          mod_slice, 'πg.ImportModule(πF, {})', util.go_str(imp.name))

      for binding in imp.bindings:
        if binding.bind_type == imputil.Import.MODULE:
          self.writer.write('{} = {}[{}]'.format(
              mod.name, mod_slice.expr, binding.value))
          self.block.bind_var(self.writer, binding.alias, mod.expr)
        else:
          self.writer.write('{} = {}[{}]'.format(
              mod.name, mod_slice.expr, imp.name.count('.')))

          with self.block.alloc_temp() as member:
            self.writer.write_checked_call2(
                member, 'πg.GetAttr(πF, {}, {}, nil)',
                mod.expr, self.block.root.intern(binding.value))
            self.block.bind_var(self.writer, binding.alias, member.expr)

Generates code that imports a module and binds it to a variable.

Args:
imp: Import object representing an import of the form "import x.y.z" or
"from x.y import z". Expects only a single binding.


def from_comm(cls, pub):

        filename = None
        if pub.b64_data:
            filename = cls._save_to_unique_filename(pub)

        return cls(
            title=pub.title,
            author=pub.author,
            pub_year=pub.pub_year,
            isbn=pub.isbn,
            urnnbn=pub.urnnbn,
            uuid=pub.uuid,
            aleph_id=pub.aleph_id,
            producent_id=pub.producent_id,
            is_public=pub.is_public,
            filename=pub.filename,
            is_periodical=pub.is_periodical,
            path=pub.path,

            file_pointer=filename
        )

Convert communication namedtuple to this class.

Args:
pub (obj): :class:`.Publication` instance which will be converted.

Returns:
obj: :class:`DBPublication` instance.


async def setup_round_robin_points(self, match_win: float = None, match_tie: float = None, game_win: float = None, game_tie: float = None):

        params = {}
        if match_win is not None:
            params['rr_pts_for_match_win'] = match_win
        if match_win is not None:
            params['rr_pts_for_match_tie'] = match_tie
        if match_win is not None:
            params['rr_pts_for_game_win'] = game_win
        if match_win is not None:
            params['rr_pts_for_game_tie'] = game_tie
        assert_or_raise(len(params) > 0, ValueError, 'At least one of the points must be given')
        await self.update(**params)

|methcoro|

Args:
match_win
match_tie
game_win
game_tie

Raises:
APIException


def value_instance_to_pytd_type(self, node, v, instance, seen, view):
    if abstract_utils.is_recursive_annotation(v):
        return pytd.LateType(v.unflatten_expr() if self._detailed else v.expr)
    elif isinstance(v, abstract.Union):
        return pytd.UnionType(tuple((self.value_instance_to_pytd_type(node, t, instance, seen, view) for t in v.options)))
    elif isinstance(v, abstract.AnnotationContainer):
        return self.value_instance_to_pytd_type(node, v.base_cls, instance, seen, view)
    elif isinstance(v, abstract.LiteralClass):
        if isinstance(v.value, abstract.Instance) and v.value.cls.is_enum:
            typ = pytd_utils.NamedTypeWithModule(v.value.cls.official_name or v.value.cls.name, v.value.cls.module)
            value = pytd.Constant(v.value.name, typ)
        elif isinstance(v.value.pyval, (str, bytes)):
            value = repr(v.value.pyval)
        elif isinstance(v.value.pyval, bool):
            value = self.ctx.loader.lookup_pytd('builtins', v.value.pyval)
        else:
            assert isinstance(v.value.pyval, int), v.value.pyval
            value = v.value.pyval
        return pytd.Literal(value)
    elif isinstance(v, typed_dict.TypedDictClass):
        return pytd.NamedType(v.name)
    elif isinstance(v, fiddle_overlay.BuildableType):
        param = self.value_instance_to_pytd_type(node, v.underlying, None, seen, view)
        return pytd.GenericType(base_type=pytd.NamedType(f'fiddle.{v.fiddle_type_name}'), parameters=(param,))
    elif isinstance(v, abstract.Class):
        if not self._detailed and v.official_name is None:
            return pytd.AnythingType()
        if seen is None:
            seen = frozenset()
        if instance in seen:
            type_params = ()
        else:
            type_params = tuple((t.name for t in v.template))
        if instance is not None:
            seen |= {instance}
        type_arguments = self._value_to_parameter_types(node, v, instance, type_params, seen, view)
        base = pytd_utils.NamedTypeWithModule(v.official_name or v.name, v.module)
        if self._is_tuple(v, instance):
            homogeneous = False
        elif v.full_name == 'typing.Callable':
            homogeneous = not isinstance(v, abstract.CallableClass)
        else:
            homogeneous = len(type_arguments) == 1
        return pytd_utils.MakeClassOrContainerType(base, type_arguments, homogeneous)
    elif isinstance(v, abstract.TYPE_VARIABLE_TYPES):
        return self._type_variable_to_def(node, v, v.name)
    elif isinstance(v, typing_overlay.Never):
        return pytd.NothingType()
    elif isinstance(v, abstract.Concatenate):
        params = tuple((self.value_instance_to_pytd_type(node, t, instance, seen, view) for t in v.args + [v.paramspec]))
        return pytd.Concatenate(pytd.NamedType('typing.Concatenate'), parameters=params)
    else:
        log.info('Using Any for instance of %s', v.name)
        return pytd.AnythingType()

Get the PyTD type an instance of this object would have.

Args:
node: The node.
v: The object.
instance: The instance.
seen: Already seen instances.
view: A Variable -> binding map.

Returns:
A PyTD type.


def bresenham(x1, y1, x2, y2):
    points = []
    issteep = (abs((y2 - y1)) > abs((x2 - x1)))
    if issteep:
        (x1, y1) = (y1, x1)
        (x2, y2) = (y2, x2)
    rev = False
    if (x1 > x2):
        (x1, x2) = (x2, x1)
        (y1, y2) = (y2, y1)
        rev = True
    deltax = (x2 - x1)
    deltay = abs((y2 - y1))
    error = int((deltax / 2))
    y = y1
    ystep = None
    if (y1 < y2):
        ystep = 1
    else:
        ystep = (- 1)
    for x in range(x1, (x2 + 1)):
        if issteep:
            points.append((y, x))
        else:
            points.append((x, y))
        error -= deltay
        if (error < 0):
            y += ystep
            error += deltax
    if rev:
        points.reverse()
    return points

Return a list of points in a bresenham line.

Implementation hastily copied from RogueBasin.

Returns:
List[Tuple[int, int]]: A list of (x, y) points,
including both the start and end-points.


def ping(self, url, endpoint=''):

        r = self.get_url(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 _AddDSATargeting(client, ad_group_id, label_name):

  ad_group_criterion_service = client.GetService('AdGroupCriterionService',
                                                 version='v201809')

  operation = {
      'operand': {
          'xsi_type': 'BiddableAdGroupCriterion',
          'adGroupId': ad_group_id,

          'criterion': {
              'xsi_type': 'Webpage',
              'parameter': {
                  'criterionName': 'Test criterion',

                  'conditions': [{
                      'operand': 'CUSTOM_LABEL',
                      'argument': label_name
                  }],
              }
          },

          'biddingStrategyConfiguration': {
              'bids': [{
                  'xsi_type': 'CpcBid',
                  'bid': {
                      'microAmount': 1500000
                  }
              }]
          }
      },
      'operator': 'ADD'
  }

  criterion = ad_group_criterion_service.mutate([operation])['value'][0]
  print 'Web page criterion with ID "%d" and status "%s" was created.' % (
      criterion['criterion']['id'], criterion['userStatus'])
  return criterion

Set custom targeting for the page feed URLs based on a list of labels.

Args:
client: an AdWordsClient instance.
ad_group_id: a str AdGroup ID.
label_name: a str label name.

Returns:
A suds.sudsobject.Object representing the newly created webpage criterion.


def DeregisterSourceType(cls, source_type_class):

    if source_type_class.TYPE_INDICATOR not in cls._source_type_classes:
      raise KeyError(
          'Source type not set for type: {0:s}.'.format(
              source_type_class.TYPE_INDICATOR))

    del cls._source_type_classes[source_type_class.TYPE_INDICATOR]

Deregisters a source type.

Source types are identified based on their type indicator.

Args:
source_type_class (type): source type.

Raises:
KeyError: if a source type is not set for the corresponding type
indicator.


def GetDataByPath(self, path):
    (_, path_data) = self._paths.get(path, (None, None))
    return path_data

Retrieves the data associated to a path.

Args:
path (str): path of the file entry.

Returns:
bytes: data or None if not available.


def on_modified(self, event):
    if (not self._event_error):
        self.logger.info(u'Change detected from an edit on: %s', event.src_path)
        self.compile_dependencies(event.src_path)

Called when a file or directory is modified.

Args:
event: Watchdog event, ``watchdog.events.DirModifiedEvent`` or
``watchdog.events.FileModifiedEvent``.


def skip(self, count=1):
    if self.closed():
        raise ValueError('Attempt to call skip() on a closed Queryable.')
    count = max(0, count)
    if (count == 0):
        return self
    if hasattr(self._iterable, '__getitem__'):
        try:
            stop = len(self._iterable)
            return self._create(self._generate_optimized_skip_result(count, stop))
        except TypeError:
            pass
    return self._create(self._generate_skip_result(count))

Skip the first count contiguous elements of the source sequence.

If the source sequence contains fewer than count elements returns an
empty sequence and does not raise an exception.

Note: This method uses deferred execution.

Args:
count: The number of elements to skip from the beginning of the
sequence. If omitted defaults to one. If count is less than one
the result sequence will be empty.

Returns:
A Queryable over the elements of source excluding the first count
elements.

Raises:
ValueError: If the Queryable is closed().


def enum(cls):

    assert cls.__bases__ == (object,)

    d = dict(cls.__dict__)
    new_type = type(cls.__name__, (int,), d)
    new_type.__module__ = cls.__module__

    map_ = {}
    for key, value in iteritems(d):
        if key.upper() == key and isinstance(value, integer_types):
            value_instance = new_type(value)
            setattr(new_type, key, value_instance)
            map_[value] = key

    def str_(self):
        if self in map_:
            return "%s.%s" % (type(self).__name__, map_[self])
        return "%d" % int(self)

    def repr_(self):
        if self in map_:
            return "<%s.%s: %d>" % (type(self).__name__, map_[self], int(self))
        return "%d" % int(self)

    setattr(new_type, "__repr__", repr_)
    setattr(new_type, "__str__", str_)

    return new_type

A decorator for creating an int enum class.

Makes the values a subclass of the type and implements repr/str.
The new class will be a subclass of int.

Args:
cls (type): The class to convert to an enum

Returns:
type: A new class

::

@enum
class Foo(object):
FOO = 1
BAR = 2


def _callable_func(self, func, axis, *args, **kwargs):

        def callable_apply_builder(df, axis=0):
            if not axis:
                df.index = index
                df.columns = pandas.RangeIndex(len(df.columns))
            else:
                df.columns = index
                df.index = pandas.RangeIndex(len(df.index))
            result = df.apply(func, axis=axis, *args, **kwargs)
            return result

        index = self.index if not axis else self.columns
        func_prepared = self._build_mapreduce_func(callable_apply_builder, axis=axis)
        result_data = self._map_across_full_axis(axis, func_prepared)
        return self._post_process_apply(result_data, axis)

Apply callable functions across given axis.

Args:
func: The functions to apply.
axis: Target axis to apply the function along.

Returns:
A new PandasQueryCompiler.


def _read_hopopt_options(self, length):
    counter = 0
    optkind = list()
    options = dict()
    while (counter < length):
        code = self._read_unpack(1)
        if (not code):
            break
        (abbr, desc) = _HOPOPT_OPT.get(code, ('none', 'Unassigned'))
        data = _HOPOPT_PROC(abbr)(self, code, desc=desc)
        enum = _OPT_TYPE.get(code)
        counter += data['length']
        if (enum in optkind):
            if isinstance(options[abbr], tuple):
                options[abbr] += (Info(data),)
            else:
                options[abbr] = (Info(options[abbr]), Info(data))
        else:
            optkind.append(enum)
            options[abbr] = data
    if (counter != length):
        raise ProtocolError(f'{self.alias}: invalid format')
    return (tuple(optkind), options)

Read HOPOPT options.

Positional arguments:
* length -- int, length of options

Returns:
* dict -- extracted HOPOPT options


def bench(image, thread_count):
    threads = [threading.Thread(target=(lambda : encoder.encode_png(image))) for _ in xrange(thread_count)]
    start_time = datetime.datetime.now()
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join()
    end_time = datetime.datetime.now()
    delta = (end_time - start_time).total_seconds()
    return delta

Encode `image` to PNG on `thread_count` threads in parallel.

Returns:
A `float` representing number of seconds that it takes all threads
to finish encoding `image`.


def _get_manager(cluster_info, host, executor_id):

  for node in cluster_info:
    if node['host'] == host and node['executor_id'] == executor_id:
      addr = node['addr']
      authkey = node['authkey']
      TFSparkNode.mgr = TFManager.connect(addr, authkey)
      break

  if TFSparkNode.mgr is None:
    msg = "No TFManager found on this node, please ensure that:\n" + \
          "1. Spark num_executors matches TensorFlow cluster_size\n" + \
          "2. Spark cores/tasks per executor is 1.\n" + \
          "3. Spark dynamic allocation is disabled."
    raise Exception(msg)

  logging.info("Connected to TFSparkNode.mgr on {0}, executor={1}, state={2}".format(host, executor_id, str(TFSparkNode.mgr.get('state'))))
  return TFSparkNode.mgr

Returns this executor's "singleton" instance of the multiprocessing.Manager, reconnecting per python-worker if needed.

Args:
:cluster_info: cluster node reservations
:host: host IP address
:executor_id: unique id per executor (created during initial call to run())

Returns:
TFManager instance for this executor/python-worker


def output_mask(self):
    output = self.output
    if isinstance(output, list):
        return [getattr(x, '_keras_mask', None) for x in output]
    else:
        return getattr(output, '_keras_mask', None)

Retrieves the output mask tensor(s) of a layer.

Only applicable if the layer has exactly one inbound node,
i.e. if it is connected to one incoming layer.

Returns:
Output mask tensor (potentially None) or list of output
mask tensors.

Raises:
AttributeError: if the layer is connected to
more than one incoming layers.


def _get_config_instance(group_or_term, session, **kwargs):

    path = group_or_term._get_path()
    cached = group_or_term._top._cached_configs.get(path)
    if cached:
        config = cached
        created = False
    else:

        config, created = get_or_create(session, Config, **kwargs)
    return config, created

Finds appropriate config instance and returns it.

Args:
group_or_term (Group or Term):
session (Sqlalchemy session):
kwargs (dict): kwargs to pass to get_or_create.

Returns:
tuple of (Config, bool):


def __init__(self, conf_path=ZEO_CLIENT_PATH, project_key=PROJECT_KEY):

        super(self.__class__, self).__init__(
            conf_path=conf_path,
            project_key=project_key
        )

        self.name_db_key = "name_db"
        self.name_db = self._get_key_or_create(self.name_db_key)

        self.aleph_id_db_key = "aleph_id_db"
        self.aleph_id_db = self._get_key_or_create(self.aleph_id_db_key)

        self.issn_db_key = "issn_db"
        self.issn_db = self._get_key_or_create(self.issn_db_key)

        self.path_db_key = "path_db"
        self.path_db = self._get_key_or_create(self.path_db_key)

        self.parent_db_key = "parent_db"
        self.parent_db = self._get_key_or_create(self.parent_db_key)

Constructor.

Args:
conf_path (str): Path to the ZEO configuration file. Default
:attr:`~storage.settings.ZEO_CLIENT_PATH`.
project_key (str): Project key, which is used for lookups into ZEO.
Default :attr:`~storage.settings.TREE_PROJECT_KEY`.


def _get_connection(self, conn_or_int_id):
    key = conn_or_int_id
    if isinstance(key, str):
        table = self._int_connections
    elif isinstance(key, int):
        table = self._connections
    else:
        return None
    try:
        data = table[key]
    except KeyError:
        return None
    return data

Get the data for a connection by either conn_id or internal_id

Args:
conn_or_int_id (int, string): The external integer connection id or
and internal string connection id

Returns:
dict: The context data associated with that connection or None if it cannot
be found.

Raises:
ArgumentError: When the key is not found in the list of active connections
or is invalid.


def WriteTimestamp(timestamp, filename):
    if timestamp is None:
        return True
    timestamp_dir = os.path.dirname(filename)
    filedesc, temp_filename = tempfile.mkstemp(prefix='nsscache-update-', dir=timestamp_dir)
    time_string = time.strftime('%Y-%m-%dT%H:%M:%SZ', timestamp)
    try:
        os.write(filedesc, b'%s\n' % time_string.encode())
        os.fsync(filedesc)
        os.close(filedesc)
    except OSError:
        os.unlink(temp_filename)
        logging.warning('writing timestamp failed!')
        return False
    os.chmod(temp_filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
    os.rename(temp_filename, filename)
    logging.debug('wrote timestamp %s to file %r', time_string, filename)
    return True

Write a given timestamp out to a file, converting to the ISO-8601
format.

We convert internal timestamp format (epoch) to ISO-8601 format, i.e.
YYYY-MM-DDThh:mm:ssZ which is basically UTC time, then write it out to a
file.

Args:
timestamp: A struct time.struct_time or time tuple.
filename: A String naming the file to write to.

Returns:
A boolean indicating success of write.


def finish(self, end='\n', dirty=False):

        if not dirty:
            self.end_time = datetime.now()
            self.update(self.max_value, force=True)

        StdRedirectMixin.finish(self, end=end)
        ResizableMixin.finish(self)
        ProgressBarBase.finish(self)

Puts the ProgressBar bar in the finished state.

Also flushes and disables output buffering if this was the last
progressbar running.

Args:
end (str): The string to end the progressbar with, defaults to a
newline
dirty (bool): When True the progressbar kept the current state and
won't be set to 100 percent


def _init_from_args(self, queue=None, enqueue_ops=None, close_op=None, cancel_op=None, queue_closed_exception_types=None):
    if not queue or not enqueue_ops:
        raise ValueError('Must provide queue and enqueue_ops.')
    self._queue = queue
    self._enqueue_ops = enqueue_ops
    self._close_op = close_op
    self._cancel_op = cancel_op
    if queue_closed_exception_types is not None:
        if not isinstance(queue_closed_exception_types, tuple) or not queue_closed_exception_types or (not all((issubclass(t, errors.OpError) for t in queue_closed_exception_types))):
            raise TypeError('queue_closed_exception_types, when provided, must be a tuple of tf.error types, but saw: %s' % queue_closed_exception_types)
    self._queue_closed_exception_types = queue_closed_exception_types
    if self._close_op is None:
        self._close_op = self._queue.close()
    if self._cancel_op is None:
        self._cancel_op = self._queue.close(cancel_pending_enqueues=True)
    if not self._queue_closed_exception_types:
        self._queue_closed_exception_types = (errors.OutOfRangeError,)
    else:
        self._queue_closed_exception_types = tuple(self._queue_closed_exception_types)

Create a QueueRunner from arguments.

Args:
queue: A `Queue`.
enqueue_ops: List of enqueue ops to run in threads later.
close_op: Op to close the queue. Pending enqueue ops are preserved.
cancel_op: Op to close the queue and cancel pending enqueue ops.
queue_closed_exception_types: Tuple of exception types, which indicate
the queue has been safely closed.

Raises:
ValueError: If `queue` or `enqueue_ops` are not provided when not
restoring from `queue_runner_def`.
TypeError: If `queue_closed_exception_types` is provided, but is not
a non-empty tuple of error types (subclasses of `tf.errors.OpError`).


def _get_python_exe_version(python_exe: list[str]):
    try:
        python_exe_version = subprocess.check_output(python_exe + ['-V'], stderr=subprocess.STDOUT).decode()
    except (subprocess.CalledProcessError, FileNotFoundError):
        return None
    return _parse_exe_version_string(python_exe_version)

Determine the major and minor version of given Python executable.

Arguments:
python_exe: absolute path to the Python executable

Returns:
Version as (major, minor) tuple, or None if it could not be determined.


def add(self, term):

        if isinstance(term, Conjunction):
            for term_ in term.terms:
                self.add(term_)
        elif isinstance(term, Term):
            self._terms.append(term)
        else:
            raise TypeError('Not a Term or Conjunction')

Add a term to the conjunction.

Args:
term (:class:`Term`, :class:`Conjunction`): term to add;
if a :class:`Conjunction`, all of its terms are added
to the current conjunction.
Raises:
:class:`TypeError`: when *term* is an invalid type


def delete(self, *names: str, pipeline=False):
    if pipeline:
        self._pipeline.delete(*names)
    else:
        self._db.delete(*names)

Delete one or more keys specified by names.

Args:
names (str): Names of keys to delete
pipeline (bool): True, start a transaction block. Default false.


def _extract_cell_info(self, structure, site_idx, sites, targets, voro, compute_adj_neighbors=False):
    all_vertices = voro.vertices
    center_coords = sites[site_idx].coords
    results = {}
    for (nn, vind) in voro.ridge_dict.items():
        if (site_idx in nn):
            other_site = (nn[0] if (nn[1] == site_idx) else nn[1])
            if ((- 1) in vind):
                if self.allow_pathological:
                    continue
                else:
                    raise RuntimeError('This structure is pathological, infinite vertex in the voronoi construction')
            facets = [all_vertices[i] for i in vind]
            angle = solid_angle(center_coords, facets)
            volume = 0
            for (j, k) in zip(vind[1:], vind[2:]):
                volume += vol_tetra(center_coords, all_vertices[vind[0]], all_vertices[j], all_vertices[k])
            face_dist = (np.linalg.norm((center_coords - sites[other_site].coords)) / 2)
            face_area = ((3 * volume) / face_dist)
            normal = np.subtract(sites[other_site].coords, center_coords)
            normal /= np.linalg.norm(normal)
            results[other_site] = {'site': sites[other_site], 'normal': normal, 'solid_angle': angle, 'volume': volume, 'face_dist': face_dist, 'area': face_area, 'n_verts': len(vind)}
            if compute_adj_neighbors:
                results[other_site]['verts'] = vind
    resultweighted = {}
    for (nn_index, nstats) in results.items():
        nn = nstats['site']
        if nn.is_ordered:
            if (nn.specie in targets):
                resultweighted[nn_index] = nstats
        else:
            for disordered_sp in nn.species.keys():
                if (disordered_sp in targets):
                    resultweighted[nn_index] = nstats
    if compute_adj_neighbors:
        adj_neighbors = dict(((i, []) for i in resultweighted.keys()))
        for (a_ind, a_nninfo) in resultweighted.items():
            a_verts = set(a_nninfo['verts'])
            for (b_ind, b_nninfo) in resultweighted.items():
                if (b_ind > a_ind):
                    continue
                if (len(a_verts.intersection(b_nninfo['verts'])) == 2):
                    adj_neighbors[a_ind].append(b_ind)
                    adj_neighbors[b_ind].append(a_ind)
        for (key, neighbors) in adj_neighbors.items():
            resultweighted[key]['adj_neighbors'] = neighbors
    return resultweighted

Get the information about a certain atom from the results of a tessellation

Args:
structure (Structure) - Structure being assessed
site_idx (int) - Index of the atom in question
sites ([Site]) - List of all sites in the tessellation
targets ([Element]) - Target elements
voro - Output of qvoronoi
compute_adj_neighbors (boolean) - Whether to compute which neighbors are adjacent
Returns:
A dict of sites sharing a common Voronoi facet. Key is facet id
(not useful) and values are dictionaries containing statistics
about the facet:
- site: Pymatgen site
- solid_angle - Solid angle subtended by face
- angle_normalized - Solid angle normalized such that the
faces with the largest
- area - Area of the facet
- face_dist - Distance between site n and the facet
- volume - Volume of Voronoi cell for this face
- n_verts - Number of vertices on the facet
- adj_neighbors - Facet id's for the adjacent neighbors


def is_http_running_on(port):
    try:
        conn = httplib.HTTPConnection(('127.0.0.1:' + str(port)))
        conn.connect()
        conn.close()
        return True
    except Exception:
        return False

Check if an http server runs on a given port.

Args:
The port to check.
Returns:
True if it is used by an http server. False otherwise.


def _to_dict(self, include=None, exclude=None):

    if (include is not None and
        not isinstance(include, (list, tuple, set, frozenset))):
      raise TypeError('include should be a list, tuple or set')
    if (exclude is not None and
        not isinstance(exclude, (list, tuple, set, frozenset))):
      raise TypeError('exclude should be a list, tuple or set')
    values = {}
    for prop in self._properties.itervalues():
      name = prop._code_name
      if include is not None and name not in include:
        continue
      if exclude is not None and name in exclude:
        continue
      try:
        values[name] = prop._get_for_dict(self)
      except UnprojectedPropertyError:
        pass
    return values

Return a dict containing the entity's property values.

Args:
include: Optional set of property names to include, default all.
exclude: Optional set of property names to skip, default none.
A name contained in both include and exclude is excluded.


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

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

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


async def peers(self):
    response = (await self._api.get('/v1/status/peers'))
    if (response.status == 200):
        return set(response.body)

Returns the current Raft peer set

Returns:
Collection: addresses of peers

This endpoint retrieves the Raft peers for the datacenter in which
the agent is running. It returns a collection of addresses, such as::

[
"10.1.10.12:8300",
"10.1.10.11:8300",
"10.1.10.10:8300"
]

This list of peers is strongly consistent and can be useful in
determining when a given server has successfully joined the cluster.


def ensemble_center(self, site_list, indices, cartesian=True):
    if cartesian:
        return np.average([site_list[i].coords for i in indices], axis=0)
    else:
        return np.average([site_list[i].frac_coords for i in indices], axis=0)

Finds the center of an ensemble of sites selected from
a list of sites.  Helper method for the find_adsorption_sites
algorithm.

Args:
site_list (list of sites): list of sites
indices (list of ints): list of ints from which to select
sites from site list
cartesian (bool): whether to get average fractional or
cartesian coordinate


def scale_vmss(access_token, subscription_id, resource_group, vmss_name, capacity):

    endpoint = ''.join([get_rm_endpoint(),
                        '/subscriptions/', subscription_id,
                        '/resourceGroups/', resource_group,
                        '/providers/Microsoft.Compute/virtualMachineScaleSets/', vmss_name,
                        '?api-version=', COMP_API])
    body = '{"sku":{"capacity":"' + str(capacity) + '"}}'
    return do_patch(endpoint, body, access_token)

Change the instance count of an existing VM Scale Set.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.
resource_group (str): Azure resource group name.
vmss_name (str): Name of the virtual machine scale set.
capacity (int): New number of VMs.
Returns:
HTTP response.


def _make_output_composite_tensors_match(op_type, branch_graphs):
    assert branch_graphs
    branch_outputs = [g.structured_outputs for g in branch_graphs]
    outputs_per_branch = list((len(outs) for outs in branch_outputs))
    assert len(set(outputs_per_branch)) == 1, outputs_per_branch
    for output_idx, branch_outs in enumerate(zip(*branch_outputs)):
        if len(set((type(out) for out in branch_outs))) == 1:
            continue
        if not any((isinstance(out, indexed_slices.IndexedSlices) for out in branch_outs)):
            continue
        for branch_idx, branch_out in enumerate(branch_outs):
            if isinstance(branch_out, indexed_slices.IndexedSlices):
                continue
            elif isinstance(branch_out, tensor_lib.Tensor):
                with branch_graphs[branch_idx].as_default():
                    branch_outputs[branch_idx][output_idx] = math_ops._as_indexed_slices(branch_out)
            else:
                raise TypeError('Cannot reconcile {op_name} {output_idx}-th outputs:\n  outputs from all branches: {outputs}'.format(op_name='tf.cond' if op_type == _COND else 'tf.switch_case', output_idx=output_idx, outputs=branch_outs))
    for branch_graph, branch_outs in zip(branch_graphs, branch_outputs):
        branch_graph.structured_outputs = branch_outs
        branch_graph.outputs = [t for t in func_graph_module.flatten(branch_outs) if t is not None]

Modifies each branch_graph's outputs to have the same output signature.

Currently the only transformation implemented is turning a Tensor into an
equivalent IndexedSlices if the other branch returns an IndexedSlices.
Updates branch_graph.{outputs,structured_outputs} for each branch_graph in
branch_graphs.

Args:
op_type: _COND or _CASE
branch_graphs: `list` of `FuncGraph`

Raises:
TypeError: if a set of outputs cannot be rewritten.


def get_arrays(self, type_img):
    if (type_img.lower() == 'lola'):
        return LolaMap(self.ppdlola, *self.window, path_pdsfile=self.path_pdsfiles).image()
    elif (type_img.lower() == 'wac'):
        return WacMap(self.ppdwac, *self.window, path_pdsfile=self.path_pdsfiles).image()
    else:
        raise ValueError('The img type has to be either "Lola" or "Wac"')

Return arrays the region of interest

Args:
type_img (str): Either lola or wac.

Returns:
A tupple of three arrays ``(X,Y,Z)`` with ``X`` contains the
longitudes, ``Y`` contains the latitude and ``Z`` the values
extracted for the region of interest.

Note:
The argument has to be either lola or wac. Note case sensitive.
All return arrays have the same size.

All coordinates are in degree.


def _validate_symbol_names(self) -> None:
    all_symbol_names = set(self._names) | set(self._names_v1)
    if self._api_name == TENSORFLOW_API_NAME:
        for subpackage in SUBPACKAGE_NAMESPACES:
            if any((n.startswith(subpackage) for n in all_symbol_names)):
                raise InvalidSymbolNameError('@tf_export is not allowed to export symbols under %s.*' % subpackage)
    elif not all((n.startswith(self._api_name) for n in all_symbol_names)):
        raise InvalidSymbolNameError('Can only export symbols under package name of component.')

Validate you are exporting symbols under an allowed package.

We need to ensure things exported by tf_export, etc.
export symbols under disjoint top-level package names.

For TensorFlow, we check that it does not export anything under subpackage
names used by components (keras, etc.).

For each component, we check that it exports everything under its own
subpackage.

Raises:
InvalidSymbolNameError: If you try to export symbol under disallowed name.


def add_resource(self, resource, *class_args, **class_kwargs):

        name = resource.__name__.lower()
        meta_resource = parse_docs(resource.__doc__, ["$shared"])
        self.meta[name] = meta_resource
        shared = self.meta["$shared"].copy()
        shared.update(meta_resource.get("$shared", {}))
        with MarkKey("%s.$shared" % resource.__name__):
            sp = SchemaParser(validators=self.validators, shared=shared)
        with MarkKey(resource.__name__):
            resource = resource(*class_args, **class_kwargs)

            actions = defaultdict(lambda: {})
            for action in dir(resource):
                find = PATTERN_ACTION.findall(action)
                if not find:
                    continue
                httpmethod, action_name = find[0]
                action_group = actions[action_name]
                fn = getattr(resource, action)
                meta_action = parse_docs(
                    fn.__doc__, ["$input", "$output", "$error"])
                meta_resource[action] = meta_action
                with MarkKey(fn.__name__):
                    action_group[httpmethod] = \
                        self.make_action(fn, sp, meta_action)

        for action_name in actions:
            if action_name == "":
                url = "/" + name
                endpoint = name
            else:
                url = "/{0}/{1}".format(name, action_name)
                endpoint = "{0}@{1}".format(name, action_name)
            action_group = actions[action_name]
            self.app.add_url_rule(
                url, endpoint=endpoint,
                view_func=self.make_view(action_group),
                methods=set(action_group)
            )

Add resource

Parse resource and it's actions, route actions by naming rule.

Args:
resource: resource class
class_args: class_args
class_kwargs: class_kwargs