class FSMTEncoder(nn.Module):

    def __init__(self, config: FSMTConfig, embed_tokens):
        super().__init__()
        self.dropout = config.dropout
        self.layerdrop = config.encoder_layerdrop
        self.padding_idx = embed_tokens.padding_idx
        self.embed_tokens = embed_tokens
        embed_dim = embed_tokens.embedding_dim
        self.embed_scale = math.sqrt(embed_dim) if config.scale_embedding else 1.0
        self.embed_positions = SinusoidalPositionalEmbedding(config.max_position_embeddings + self.padding_idx + 1, embed_dim, self.padding_idx)
        self.layers = nn.ModuleList([EncoderLayer(config) for _ in range(config.encoder_layers)])

    def forward(self, input_ids: torch.Tensor, attention_mask: Optional[torch.Tensor]=None, inputs_embeds: Optional[torch.Tensor]=None, head_mask: Optional[torch.Tensor]=None, output_attentions: bool=False, output_hidden_states: bool=False, return_dict: bool=True):

        if attention_mask is not None:
            attention_mask = invert_mask(attention_mask)
        if input_ids is not None and inputs_embeds is not None:
            raise ValueError('You cannot specify both input_ids and inputs_embeds at the same time')
        elif input_ids is not None:
            inputs_embeds = self.embed_tokens(input_ids) * self.embed_scale
            embed_pos = self.embed_positions(input_ids)
        elif inputs_embeds is not None:
            inputs_embeds = inputs_embeds * self.embed_scale
            position_ids = inputs_embeds[:, :, 0].masked_fill(inputs_embeds[:, :, 0].eq(0), self.embed_positions.padding_idx)
            embed_pos = self.embed_positions(position_ids)
        else:
            raise ValueError('You have to specify either input_ids or inputs_embeds')
        x = inputs_embeds + embed_pos
        x = nn.functional.dropout(x, p=self.dropout, training=self.training)
        x = x.transpose(0, 1)
        encoder_states = () if output_hidden_states else None
        all_attentions = () if output_attentions else None
        if head_mask is not None:
            assert head_mask.size()[0] == len(self.layers), f'The head_mask should be specified for {len(self.layers)} layers, but it is for {head_mask.size()[0]}.'
        for idx, encoder_layer in enumerate(self.layers):
            if output_hidden_states:
                x = x.transpose(0, 1)
                encoder_states += (x,)
                x = x.transpose(0, 1)
            dropout_probability = torch.rand([])
            if self.training and dropout_probability < self.layerdrop:
                attn = None
            else:
                x, attn = encoder_layer(x, attention_mask, layer_head_mask=head_mask[idx] if head_mask is not None else None, output_attentions=output_attentions)
            if output_attentions:
                all_attentions = all_attentions + (attn,)
        x = x.transpose(0, 1)
        if output_hidden_states:
            encoder_states += (x,)
        if not return_dict:
            return tuple((v for v in [x, encoder_states, all_attentions] if v is not None))
        return BaseModelOutput(last_hidden_state=x, hidden_states=encoder_states, attentions=all_attentions)

Transformer encoder consisting of *config.encoder_layers* self attention layers. Each layer is a [`EncoderLayer`].

Args:
config: FSMTConfig


def getReportTable(reportzipfile, tablefilename, inReportsDir=True, verbose=False):
    import zipfile, io, re
    if (not reportzipfile.endswith('.zip')):
        reportzipfile += '.zip'
    if inReportsDir:
        reportzipfile = os.path.join(reportsDir, reportzipfile)
    with zipfile.ZipFile(reportzipfile) as z:
        if (not tablefilename):
            for f in z.filelist:
                if ('table' in f.filename):
                    print(f.filename)
            return
        if isinstance(tablefilename, list):
            tablelocation = tablefilename
            tablefilename = None
            location = re.compile('(s|table)(\\d+)_')
            for f in z.filelist:
                if (('table' not in f.filename) or (f.filename.count('/') != (len(tablelocation) - 1))):
                    continue
                if ([int(location.match(s).groups()[1]) for s in f.filename.split('/')] == tablelocation):
                    tablefilename = f.filename
                    if verbose:
                        print('Loading', tablefilename)
                    break
            if (tablefilename is None):
                raise FileNotFoundError('Table location not found in zip folder.')
        with z.open(tablefilename) as f:
            ft = io.TextIOWrapper(f)
            return pd.read_csv(ft, index_col=0, sep=csvsep, decimal=csvdec)

Get a pandas table from a previous report

Args:
reportzipfile (str): Zip folder location, '.zip' extension is optional.
tablefilename (str or list int): Table location within the zip folder.
Can be provided as the filename within the zip folder, or a list of integers
indicating its exact position (1-indexed). If you provide an empty string or
list, all available table filenames in the zip folder will be printed.
inReportsDir (bool): Search reportzipfile relative to reportsDir.

Returns:
pd.DataFrame


def _generate_async(self, generator):

        first_value_future = self._workers.submit(next, generator)

        def get_first_element(future=first_value_future):

            try:
                yield future.result()
            except StopIteration:
                return

        return chain(get_first_element(), generator)

Return the previous generator object after having run the first element
evaluation as a background task.

Args:
generator (iterable): A generator function.

Returns:
iterable: The generator function with first element evaluated
in background.


def _build_split_filenames(self, split_info_list):
    filenames = []
    for split_info in split_info_list:
        filenames.extend(naming.filepaths_for_dataset_split(dataset_name=self.name, split=split_info.name, num_shards=split_info.num_shards, data_dir=self._data_dir, filetype_suffix=self._file_format_adapter.filetype_suffix))
    return filenames

Construct the split filenames associated with the split info.

The filenames correspond to the pre-processed datasets files present in
the root directory of the dataset.

Args:
split_info_list: (list[SplitInfo]) List of split from which generate the
filenames

Returns:
filenames: (list[str]) The list of filenames path corresponding to the
split info object


def call(method: Method, *args: Any, **kwargs: Any) -> Any:

    return validate_args(method, *args, **kwargs)(*args, **kwargs)

Validates arguments and then calls the method.

Args:
method: The method to call.
*args, **kwargs: Arguments to the method.

Returns:
The "result" part of the JSON-RPC response (the return value from the method).

Raises:
TypeError: If arguments don't match function signature.


def fit(self, X):
    (U, V) = self.split_matrix(X)
    self.tau = stats.kendalltau(U, V)[0]
    self.theta = self.compute_theta()
    self.check_theta()

Fit a model to the data updating the parameters.

Args:
X: `np.ndarray` of shape (,2).

Return:
None


def _task_table(self, task_id):

        assert isinstance(task_id, ray.TaskID)
        message = self._execute_command(task_id, "RAY.TABLE_LOOKUP",
                                        ray.gcs_utils.TablePrefix.RAYLET_TASK,
                                        "", task_id.binary())
        if message is None:
            return {}
        gcs_entries = ray.gcs_utils.GcsTableEntry.GetRootAsGcsTableEntry(
            message, 0)

        assert gcs_entries.EntriesLength() == 1

        task_table_message = ray.gcs_utils.Task.GetRootAsTask(
            gcs_entries.Entries(0), 0)

        execution_spec = task_table_message.TaskExecutionSpec()
        task_spec = task_table_message.TaskSpecification()
        task = ray._raylet.Task.from_string(task_spec)
        function_descriptor_list = task.function_descriptor_list()
        function_descriptor = FunctionDescriptor.from_bytes_list(
            function_descriptor_list)

        task_spec_info = {
            "DriverID": task.driver_id().hex(),
            "TaskID": task.task_id().hex(),
            "ParentTaskID": task.parent_task_id().hex(),
            "ParentCounter": task.parent_counter(),
            "ActorID": (task.actor_id().hex()),
            "ActorCreationID": task.actor_creation_id().hex(),
            "ActorCreationDummyObjectID": (
                task.actor_creation_dummy_object_id().hex()),
            "ActorCounter": task.actor_counter(),
            "Args": task.arguments(),
            "ReturnObjectIDs": task.returns(),
            "RequiredResources": task.required_resources(),
            "FunctionID": function_descriptor.function_id.hex(),
            "FunctionHash": binary_to_hex(function_descriptor.function_hash),
            "ModuleName": function_descriptor.module_name,
            "ClassName": function_descriptor.class_name,
            "FunctionName": function_descriptor.function_name,
        }

        return {
            "ExecutionSpec": {
                "Dependencies": [
                    execution_spec.Dependencies(i)
                    for i in range(execution_spec.DependenciesLength())
                ],
                "LastTimestamp": execution_spec.LastTimestamp(),
                "NumForwards": execution_spec.NumForwards()
            },
            "TaskSpec": task_spec_info
        }

Fetch and parse the task table information for a single task ID.

Args:
task_id: A task ID to get information about.

Returns:
A dictionary with information about the task ID in question.


def write(self, output_buffer, kmip_version=enums.KMIPVersion.KMIP_1_3):
    if (kmip_version < enums.KMIPVersion.KMIP_1_3):
        raise exceptions.VersionNotSupported('KMIP {} does not support the ValidationInformation object.'.format(kmip_version.value))
    local_buffer = BytearrayStream()
    if self._validation_authority_type:
        self._validation_authority_type.write(local_buffer, kmip_version=kmip_version)
    else:
        raise exceptions.InvalidField('The ValidationInformation structure is missing the validation authority type field.')
    if self._validation_authority_country:
        self._validation_authority_country.write(local_buffer, kmip_version=kmip_version)
    if self._validation_authority_uri:
        self._validation_authority_uri.write(local_buffer, kmip_version=kmip_version)
    if self._validation_version_major:
        self._validation_version_major.write(local_buffer, kmip_version=kmip_version)
    else:
        raise exceptions.InvalidField('The ValidationInformation structure is missing the validation version major field.')
    if self._validation_version_minor:
        self._validation_version_minor.write(local_buffer, kmip_version=kmip_version)
    if self._validation_type:
        self._validation_type.write(local_buffer, kmip_version=kmip_version)
    else:
        raise exceptions.InvalidField('The ValidationInformation structure is missing the validation type field.')
    if self._validation_level:
        self._validation_level.write(local_buffer, kmip_version=kmip_version)
    else:
        raise exceptions.InvalidField('The ValidationInformation structure is missing the validation level field.')
    if self._validation_certificate_identifier:
        self._validation_certificate_identifier.write(local_buffer, kmip_version=kmip_version)
    if self._validation_certificate_uri:
        self._validation_certificate_uri.write(local_buffer, kmip_version=kmip_version)
    if self._validation_vendor_uri:
        self._validation_vendor_uri.write(local_buffer, kmip_version=kmip_version)
    if self._validation_profiles:
        for validation_profile in self._validation_profiles:
            validation_profile.write(local_buffer, kmip_version=kmip_version)
    self.length = local_buffer.length()
    super(ValidationInformation, self).write(output_buffer, kmip_version=kmip_version)
    output_buffer.write(local_buffer.buffer)

Write the ValidationInformation structure encoding to the data stream.

Args:
output_buffer (stream): A data stream in which to encode
ValidationInformation structure data, supporting a write
method.
kmip_version (enum): A KMIPVersion enumeration defining the KMIP
version with which the object will be encoded. Optional,
defaults to KMIP 2.0.

Raises:
InvalidField: Raised if the validation authority type, validation
version major, validation type, and/or validation level fields
are not defined.
VersionNotSupported: Raised when a KMIP version is provided that
does not support the ValidationInformation structure.


def request(self, method, url, erc, **kwargs):
    abs_url = self.abs_url(url)
    kwargs.setdefault('timeout', self.single_request_timeout)
    while True:
        response = self._req_session.request(method, abs_url, **kwargs)
        try:
            check_response_code(response, erc)
        except RateLimitError as e:
            if self.wait_on_rate_limit:
                warnings.warn(RateLimitWarning(response))
                time.sleep(e.retry_after)
                continue
            else:
                raise
        else:
            return response

Abstract base method for making requests to the Webex Teams APIs.

This base method:
* Expands the API endpoint URL to an absolute URL
* Makes the actual HTTP request to the API endpoint
* Provides support for Webex Teams rate-limiting
* Inspects response codes and raises exceptions as appropriate

Args:
method(basestring): The request-method type ('GET', 'POST', etc.).
url(basestring): The URL of the API endpoint to be called.
erc(int): The expected response code that should be returned by the
Webex Teams API endpoint to indicate success.
**kwargs: Passed on to the requests package.

Raises:
ApiError: If anything other than the expected response code is
returned by the Webex Teams API endpoint.


def from_sasl_name(cls, name=None):

        if rfc5801 is None:
            raise NotImplementedError("Your GSSAPI implementation does not "
                                      "have support for RFC 5801")
        if isinstance(name, six.text_type):
            name = name.encode(_utils._get_encoding())

        m = rfc5801.inquire_mech_for_saslname(name)

        return cls(m)

Create a Mechanism from its SASL name

Args:
name (str): SASL name of the desired mechanism

Returns:
Mechanism: the desired mechanism

Raises:
GSSError

:requires-ext:`rfc5801`


def parse_saved_model(export_dir):
    path_to_pbtxt = file_io.join(compat.as_bytes(compat.path_to_str(export_dir)), compat.as_bytes(constants.SAVED_MODEL_FILENAME_PBTXT))
    path_to_pb = file_io.join(compat.as_bytes(compat.path_to_str(export_dir)), compat.as_bytes(constants.SAVED_MODEL_FILENAME_PB))
    path_to_cpb = file_io.join(compat.as_bytes(compat.path_to_str(export_dir)), compat.as_bytes(constants.SAVED_MODEL_FILENAME_CPB))
    saved_model = saved_model_pb2.SavedModel()
    if file_io.file_exists(path_to_pb):
        with file_io.FileIO(path_to_pb, 'rb') as f:
            file_content = f.read()
        try:
            saved_model.ParseFromString(file_content)
        except message.DecodeError as e:
            raise IOError(f'Cannot parse file {path_to_pb}: {str(e)}.') from e
    elif file_io.file_exists(path_to_pbtxt):
        with file_io.FileIO(path_to_pbtxt, 'rb') as f:
            file_content = f.read()
        try:
            text_format.Parse(file_content.decode('utf-8'), saved_model)
        except text_format.ParseError as e:
            raise IOError(f'Cannot parse file {path_to_pbtxt}: {str(e)}.') from e
    else:
        raise IOError(f'SavedModel file does not exist at: {export_dir}{os.path.sep}{{{constants.SAVED_MODEL_FILENAME_PBTXT}|{constants.SAVED_MODEL_FILENAME_PB}}}')
    return saved_model

Reads the savedmodel.pb or savedmodel.pbtxt file containing `SavedModel`.

Args:
export_dir: String or Pathlike, path to the directory containing the
SavedModel file.

Returns:
A `SavedModel` protocol buffer.

Raises:
IOError: If the file does not exist, or cannot be successfully parsed.


def guess_dir_structure(dir):
    subdir = os.listdir(dir)[0]
    if (subdir.startswith('n') and os.path.isdir(os.path.join(dir, subdir))):
        dir_structure = 'train'
    else:
        dir_structure = 'original'
    logger.info("[ILSVRC12] Assuming directory {} has '{}' structure.".format(dir, dir_structure))
    return dir_structure

Return the directory structure of "dir".

Args:
dir(str): something like '/path/to/imagenet/val'

Returns:
either 'train' or 'original'


def get_profiles(adapter, vcf_file):

    vcf = get_file_handle(vcf_file)
    individuals = vcf.samples
    profiles = {individual: [] for individual in individuals}

    for profile_variant in adapter.profile_variants():

        ref = profile_variant['ref']
        alt = profile_variant['alt']

        pos = profile_variant['pos']
        end = pos + 1
        chrom = profile_variant['chrom']

        region = f"{chrom}:{pos}-{end}"

        found_variant = False
        for variant in vcf(region):

            variant_id = get_variant_id(variant)

            if variant_id == profile_variant['_id']:
                found_variant = True

                for i, individual in enumerate(individuals):

                    genotype = GENOTYPE_MAP[variant.gt_types[i]]
                    if genotype == 'hom_alt':
                        gt_str = f"{alt}{alt}"
                    elif genotype == 'het':
                        gt_str = f"{ref}{alt}"
                    else:
                        gt_str = f"{ref}{ref}"

                    profiles[individual].append(gt_str)

                break

        if not found_variant:
            for individual in individuals: profiles[individual].append(f"{ref}{ref}")

    return profiles

Given a vcf, get a profile string for each sample in the vcf
based on the profile variants in the database

Args:
adapter(MongoAdapter): Adapter to mongodb
vcf_file(str): Path to vcf file

Returns:
profiles (dict(str)): The profiles (given as strings) for each sample
in vcf.


def _output_ast(ast: pytd.TypeDeclUnit, options: config.Options | None=None) -> str:
    options = options or config.Options.create()
    with config.verbosity_from(options):
        result = pytd_utils.Print(ast)
        log.info('=========== pyi optimized =============')
        log.info('\n%s', result)
        log.info('========================================')
    result += '\n'
    if options.quick:
        result = '
    return result

Transforms the given analysis result into a pyi representation.

Args:
ast: pytd.TypeDeclUnit to output in pyi format.
options: config.Options object.

Returns:
A pyi representation of the given AST as a string.


def quadrature_scheme_lognormal_quantiles(loc, scale, quadrature_size, validate_args=False, name=None):
    with tf.name_scope((name or 'quadrature_scheme_lognormal_quantiles')):
        dist = transformed_distribution.TransformedDistribution(distribution=normal.Normal(loc=loc, scale=scale), bijector=exp_bijector.Exp(), validate_args=validate_args)
        batch_ndims = tensorshape_util.rank(dist.batch_shape)
        if (batch_ndims is None):
            batch_ndims = tf.shape(input=dist.batch_shape_tensor())[0]

        def _compute_quantiles():
            'Helper to build quantiles.'
            zero = tf.zeros([], dtype=dist.dtype)
            edges = tf.linspace(zero, 1.0, (quadrature_size + 3))[1:(- 1)]
            edges = tf.reshape(edges, shape=tf.concat([[(- 1)], tf.ones([batch_ndims], dtype=tf.int32)], axis=0))
            quantiles = dist.quantile(edges)
            perm = tf.concat([tf.range(1, (1 + batch_ndims)), [0]], axis=0)
            quantiles = tf.transpose(a=quantiles, perm=perm)
            return quantiles
        quantiles = _compute_quantiles()
        grid = ((quantiles[(..., :(- 1))] + quantiles[(..., 1:)]) / 2.0)
        new_shape = tensorshape_util.concatenate(dist.batch_shape, [quadrature_size])
        tensorshape_util.set_shape(grid, new_shape)
        probs = tf.fill(dims=[quadrature_size], value=(1.0 / tf.cast(quadrature_size, dist.dtype)))
        return (grid, probs)

Use LogNormal quantiles to form quadrature on positive-reals.

Args:
loc: `float`-like (batch of) scalar `Tensor`; the location parameter of
the LogNormal prior.
scale: `float`-like (batch of) scalar `Tensor`; the scale parameter of
the LogNormal prior.
quadrature_size: Python `int` scalar representing the number of quadrature
points.
validate_args: Python `bool`, default `False`. When `True` distribution
parameters are checked for validity despite possibly degrading runtime
performance. When `False` invalid inputs may silently render incorrect
outputs.
name: Python `str` name prefixed to Ops created by this class.

Returns:
grid: (Batch of) length-`quadrature_size` vectors representing the
`log_rate` parameters of a `Poisson`.
probs: (Batch of) length-`quadrature_size` vectors representing the
weight associate with each `grid` value.


def pauli_expansion(val: Any, *, default: Union[(value.LinearDict[str], TDefault)]=RaiseTypeErrorIfNotProvided, atol: float=1e-09) -> Union[(value.LinearDict[str], TDefault)]:
    method = getattr(val, '_pauli_expansion_', None)
    expansion = (NotImplemented if (method is None) else method())
    if (expansion is not NotImplemented):
        return expansion.clean(atol=atol)
    matrix = unitary(val, default=None)
    if (matrix is None):
        if (default is RaiseTypeErrorIfNotProvided):
            raise TypeError('No Pauli expansion for object {} of type {}'.format(val, type(val)))
        return default
    num_qubits = (matrix.shape[0].bit_length() - 1)
    basis = operator_spaces.kron_bases(operator_spaces.PAULI_BASIS, repeat=num_qubits)
    expansion = operator_spaces.expand_matrix_in_orthogonal_basis(matrix, basis)
    return expansion.clean(atol=atol)

Returns coefficients of the expansion of val in the Pauli basis.

Args:
val: The value whose Pauli expansion is to returned.
default: Determines what happens when `val` does not have methods that
allow Pauli expansion to be obtained (see below). If set, the value
is returned in that case. Otherwise, TypeError is raised.
atol: Ignore coefficients whose absolute value is smaller than this.

Returns:
If `val` has a _pauli_expansion_ method, then its result is returned.
Otherwise, if `val` has a small unitary then that unitary is expanded
in the Pauli basis and coefficients are returned. Otherwise, if default
is set to None or other value then default is returned. Otherwise,
TypeError is raised.

Raises:
TypeError if `val` has none of the methods necessary to obtain its Pauli
expansion and no default value has been provided.


def plugins(self):
    url = self._url('/plugins')
    return self._result(self._get(url), True)

Retrieve a list of installed plugins.

Returns:
A list of dicts, one per plugin


def check_mailfy(self, query, kwargs={}):

        import requests

        s = requests.Session()

        r1 = s.get('https:

        r2 = s.post(
            'https:
            data={"email": query},
        )

        if '{"email_is_secure":true,"email":true}' in r2.text:
            return r2.text
        return None

Verifying a mailfy query in this platform.

This might be redefined in any class inheriting from Platform. The only
condition is that any of this should return a dictionary as defined.

Args:
-----
query: The element to be searched.
kwargs: Dictionary with extra parameters. Just in case.

Return:
-------
Returns the collected data if exists or None if not.


def serialize(learning_rate_schedule):
    return serialization_lib.serialize_keras_object(learning_rate_schedule)

Serializes a `LearningRateSchedule` into a JSON-compatible dict.

Args:
learning_rate_schedule: The `LearningRateSchedule` object to serialize.

Returns:
A JSON-serializable dict representing the object's config.

Example:

>>> lr_schedule = keras.optimizers.schedules.ExponentialDecay(
...     0.1, decay_steps=100000, decay_rate=0.96, staircase=True)
>>> keras.optimizers.schedules.serialize(lr_schedule)
{'module': 'keras.optimizers.schedules',
'class_name': 'ExponentialDecay', 'config': {...},
'registered_name': None}


def flatten_with_joined_string_paths(structure, separator='/', expand_composites=False):
    flat_paths = yield_flat_paths(structure, expand_composites=expand_composites)

    def stringify_and_join(path_elements):
        return separator.join((str(path_element) for path_element in path_elements))
    flat_string_paths = (stringify_and_join(path) for path in flat_paths)
    return list(zip(flat_string_paths, flatten(structure, expand_composites=expand_composites)))

Returns a list of (string path, atom) tuples.

The order of tuples produced matches that of `nest.flatten`. This allows you
to flatten a nested structure while keeping information about where in the
structure each atom was located. See `nest.yield_flat_paths`
for more information.

Args:
structure: the nested structure to flatten.
separator: string to separate levels of hierarchy in the results, defaults
to '/'.
expand_composites: If true, then composite tensors such as
`tf.sparse.SparseTensor` and `tf.RaggedTensor` are expanded into their
component tensors.

Returns:
A list of (string, atom) tuples.


def CompileReport(self, mediator):

    lines_of_text = []
    for user, extensions in sorted(self._results.items()):
      lines_of_text.append(' == USER: {0:s} =='.format(user))
      for extension, extension_identifier in sorted(extensions):
        lines_of_text.append('  {0:s} [{1:s}]'.format(
            extension, extension_identifier))
      lines_of_text.append('')

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

Compiles an analysis report.

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

Returns:
AnalysisReport: analysis report.


def onchange(self, new_value):
    self.disable_refresh()
    self.set_value(new_value)
    self.enable_refresh()
    return (new_value,)

Called when the user changes the TextInput content.
With single_line=True it fires in case of focus lost and Enter key pressed.
With single_line=False it fires at each key released.

Args:
new_value (str): the new string content of the TextInput.


def annotate(self, sent):

    preds = []
    words = []
    for word, fv in self.sent2examples(sent):
      probs = self.predictor(fv)
      tags = probs.argsort()
      tag = self.ID_TAG[tags[-1]]

      words.append(word)
      preds.append(tag)

    annotations = zip(words, preds)
    return annotations

Annotate a squence of words with entity tags.

Args:
sent: sequence of strings/words.


def put(self, entity):
    actual_entity = _normalize_entity(entity)
    if (actual_entity is None):
        return self.ndb_put(entity)
    self.puts.append(actual_entity)

Registers entity to put to datastore.

Args:
entity: an entity or model instance to put.


def get_energy_relax_structure_buckingham(structure,
                                          gulp_cmd='gulp',
                                          keywords=('optimise', 'conp'),
                                          valence_dict=None):

    gio = GulpIO()
    gc = GulpCaller(gulp_cmd)
    gin = gio.buckingham_input(
        structure, keywords, valence_dict=valence_dict
    )
    gout = gc.run(gin)
    energy = gio.get_energy(gout)
    relax_structure = gio.get_relaxed_structure(gout)
    return energy, relax_structure

Relax a structure and compute the energy using Buckingham potential.

Args:
structure: pymatgen.core.structure.Structure
gulp_cmd: GULP command if not in standard place
keywords: GULP first line keywords
valence_dict: {El: valence}. Needed if the structure is not charge
neutral.


def _unescape_token(token):

    def match(m):
        'Returns replacement string for matched object.\n\n    Matched objects contain one of the strings that matches the regex pattern:\n      r"\\\\u|\\\\\\\\|\\\\([0-9]+);"\n    The strings can be \'\\u\', \'\\\\\', or \'\\
        if (m.group(1) is None):
            return (u'_' if (m.group(0) == u'\\u') else u'\\')
        try:
            return six.unichr(int(m.group(1)))
        except (ValueError, OverflowError) as _:
            return _UNDEFINED_UNICODE
    return _UNESCAPE_REGEX.sub(match, token)

r"""Replaces escaped characters in the token with their unescaped versions.

Applies inverse transformations as _escape_token():
1. Replace "\u" with "_", and "\\" with "\".
2. Replace "\###;" with the unicode character the ### refers to.

Args:
token: escaped string

Returns:
unescaped string


def get_training_arg_index(call_fn):
    argspec = tf_inspect.getfullargspec(call_fn)
    if argspec.varargs:
        if 'training' in argspec.kwonlyargs or argspec.varkw:
            return -1
        return None
    else:
        arg_list = argspec.args
        if tf_inspect.ismethod(call_fn):
            arg_list = arg_list[1:]
        if 'training' in arg_list:
            return arg_list.index('training')
        elif 'training' in argspec.kwonlyargs or argspec.varkw:
            return -1
        return None

Returns the index of 'training' in the layer call function arguments.

Args:
call_fn: Call function.

Returns:
- n: index of 'training' in the call function arguments.
- -1: if 'training' is not found in the arguments, but layer.call accepts
variable keyword arguments
- None: if layer doesn't expect a training argument.


def _exec_cmd(self, args, shell, timeout, stderr):
    if (timeout and (timeout <= 0)):
        raise ValueError(('Timeout is not a positive value: %s' % timeout))
    try:
        (ret, out, err) = utils.run_command(args, shell=shell, timeout=timeout)
    except psutil.TimeoutExpired:
        raise AdbTimeoutError(cmd=args, timeout=timeout, serial=self.serial)
    if stderr:
        stderr.write(err)
    logging.debug('cmd: %s, stdout: %s, stderr: %s, ret: %s', utils.cli_cmd_to_string(args), out, err, ret)
    if (ret == 0):
        return out
    else:
        raise AdbError(cmd=args, stdout=out, stderr=err, ret_code=ret, serial=self.serial)

Executes adb commands.

Args:
args: string or list of strings, program arguments.
See subprocess.Popen() documentation.
shell: bool, True to run this command through the system shell,
False to invoke it directly. See subprocess.Popen() docs.
timeout: float, the number of seconds to wait before timing out.
If not specified, no timeout takes effect.
stderr: a Byte stream, like io.BytesIO, stderr of the command will
be written to this object if provided.

Returns:
The output of the adb command run if exit code is 0.

Raises:
ValueError: timeout value is invalid.
AdbError: The adb command exit code is not 0.
AdbTimeoutError: The adb command timed out.


def fog(x, severity=1):
    c = [(1.5, 2), (2.0, 2), (2.5, 1.7), (2.5, 1.5), (3.0, 1.4)][(severity - 1)]
    x = (np.array(x) / 255.0)
    max_val = x.max()
    mapsize = 512
    shape = x.shape
    max_length = max(shape[0], shape[1])
    if (max_length > mapsize):
        mapsize = (2 ** int(np.ceil(np.log2(float(max_length)))))
    tmp = plasma_fractal(mapsize=mapsize, wibbledecay=c[1])
    tmp = tmp[(:x.shape[0], :x.shape[1])]
    tmp = tmp[(..., np.newaxis)]
    x += (c[0] * tmp)
    x_clip = (np.clip(((x * max_val) / (max_val + c[0])), 0, 1) * 255)
    return around_and_astype(x_clip)

Fog corruption to images.

Adding fog to images. Fog is generated by diamond-square algorithm.

Args:
x: numpy array, uncorrupted image, assumed to have uint8 pixel in [0,255].
severity: integer, severity of corruption.

Returns:
numpy array, image with uint8 pixels in [0,255]. Added fog.


def load_config(self, config):
    for k, v in config.items():
        if hasattr(self, k) and k not in _ANDROID_DEVICE_SETTABLE_PROPS:
            raise DeviceError(self, 'Attribute %s already exists with value %s, cannot set again.' % (k, getattr(self, k)))
        setattr(self, k, v)
        self.add_device_info(k, v)

Add attributes to the AndroidDevice object based on config.

Args:
config: A dictionary representing the configs.

Raises:
Error: The config is trying to overwrite an existing attribute.


def validate_config(config, required_keys, optional_keys=None):

  if optional_keys is None:
    optional_keys = []
  if not isinstance(config, dict):
    raise Exception('config is not dict type')
  invalid_keys = set(config) - set(required_keys + optional_keys)
  if len(invalid_keys) > 0:
    raise Exception('Invalid config with unexpected keys "%s"' % ', '.join(e for e in invalid_keys))
  missing_keys = set(required_keys) - set(config)
  if len(missing_keys) > 0:
    raise Exception('Invalid config with missing keys "%s"' % ', '.join(missing_keys))

Validate a config dictionary to make sure it includes all required keys
and does not include any unexpected keys.

Args:
config: the config to validate.
required_keys: the names of the keys that the config must have.
optional_keys: the names of the keys that the config can have.

Raises:
Exception if the config is not a dict or invalid.


def sum_mvns(distributions):
    graph_parents = [tensor for distribution in distributions for tensor in distribution._graph_parents]
    with tf.compat.v1.name_scope('sum_mvns', values=graph_parents):
        if all([isinstance(mvn, tfd.MultivariateNormalDiag) for mvn in distributions]):
            return tfd.MultivariateNormalDiag(loc=sum([mvn.mean() for mvn in distributions]), scale_diag=tf.sqrt(sum([(mvn.scale.diag ** 2) for mvn in distributions])))
        else:
            raise NotImplementedError('Sums of distributions other than MultivariateNormalDiag are not currently implemented. (given: {})'.format(distributions))

Attempt to sum MultivariateNormal distributions.

The sum of (multivariate) normal random variables is itself (multivariate)
normal, with mean given by the sum of means and (co)variance given by the
sum of (co)variances. This method exploits this fact to compute the
sum of a list of `tfd.MultivariateNormalDiag` objects.

It may in the future be extended to support summation of other forms of
(Multivariate)Normal distributions.

Args:
distributions: Python `iterable` of `tfd.MultivariateNormalDiag`
distribution instances. These must all have the same event
shape, and broadcast to a consistent batch shape.

Returns:
sum_distribution: A `tfd.MultivariateNormalDiag` instance with mean
equal to the sum of input means and covariance equal to the sum of
input covariances.


def error(self, error):

        self._error = RuntimeError(error) if isinstance(error, str) else error

Defines a simulated exception error that will be raised.

Arguments:
error (str|Exception): error to raise.

Returns:
self: current Mock instance.


def post(self, url, data=None, files=None, headers=None, get_json=True):

        if self.debug:
            print("POST: %s, headers=%s" % (url, headers))

        self.headers = self._get_default_headers()
        if headers is not None:
            self.headers.update(headers)

        response = requests.post(url, headers=self.headers, data=data, auth=self.auth, files=files, verify=self.verify_ssl)
        json_response = self._process_json_response(response)

        return json_response if get_json is True else response

Make POST request to a url

Args:
url (str): URL to send the request to
data (dict, optional): Data to send
files (dict, optional): Files to send with the request
headers (str, optional): custom headers

Returns:
A JSON object of the returned response if `get_json` is True,
Requests' response object otherwise


def annotate_arg(arg_name, with_annotation):
    arg_binding_key = arg_binding_keys.new(arg_name, with_annotation)
    return _get_pinject_wrapper(locations.get_back_frame_loc(), arg_binding_key=arg_binding_key)

Adds an annotation to an injected arg.

arg_name must be one of the named args of the decorated function, i.e.,
@annotate_arg('foo', with_annotation='something')
def a_function(foo):  # ...
is OK, but
@annotate_arg('foo', with_annotation='something')
def a_function(bar, **kwargs):  # ...
is not.

The same arg (on the same function) may not be annotated twice.

Args:
arg_name: the name of the arg to annotate on the decorated function
with_annotation: an annotation object
Returns:
a function that will decorate functions passed to it


def get_page_artid_for_publication_info(publication_info, separator):
    if ('artid' in publication_info):
        return publication_info['artid']
    elif (('page_start' in publication_info) and ('page_end' in publication_info)):
        page_start = publication_info['page_start']
        page_end = publication_info['page_end']
        return text_type('{}{}{}').format(page_start, text_type(separator), page_end)
    return ''

Return the page range or the article id of a publication_info entry.

Args:
publication_info(dict): a publication_info field entry of a record
separator(basestring): optional page range symbol, defaults to a single dash

Returns:
string: the page range or the article id of the record.

Examples:
>>> publication_info = {'artid': '054021'}
>>> get_page_artid(publication_info)
'054021'


def connectionReady(self, res=None):

        self._channel = yield self._allocate_channel()
        if _pika_version < pkg_resources.parse_version("1.0.0b1"):
            extra_args = dict(all_channels=True)
        else:
            extra_args = dict(global_qos=True)
        yield self._channel.basic_qos(
            prefetch_count=config.conf["qos"]["prefetch_count"],
            prefetch_size=config.conf["qos"]["prefetch_size"],
            **extra_args
        )
        if _pika_version < pkg_resources.parse_version("1.0.0b1"):
            TwistedProtocolConnection.connectionReady(self, res)

Callback invoked when the AMQP connection is ready (when self.ready fires).

This API is not meant for users.

Args:
res: This is an unused argument that provides compatibility with Pika
versions lower than 1.0.0.


def _get_entry_energy(pd, composition):
    candidate = [i.energy_per_atom for i in pd.qhull_entries if (i.composition.fractional_composition == composition.fractional_composition)]
    if (not candidate):
        warnings.warn((('The reactant ' + composition.reduced_formula) + ' has no matching entry with negative formation energy, instead convex hull energy for this composition will be used for reaction energy calculation. '))
        return pd.get_hull_energy(composition)
    else:
        min_entry_energy = min(candidate)
        return (min_entry_energy * composition.num_atoms)

Finds the lowest entry energy for entries matching the composition.
Entries with non-negative formation energies are excluded. If no
entry is found, use the convex hull energy for the composition.

Args:
pd (PhaseDiagram): PhaseDiagram object.
composition (Composition): Composition object that the target
entry should match.

Returns:
The lowest entry energy among entries matching the composition.


def _URange(s):
    a = s.split('..')
    if (len(a) == 1):
        return [_UInt(a[0])]
    if (len(a) == 2):
        lo = _UInt(a[0])
        hi = _UInt(a[1])
        if (lo < hi):
            return range(lo, (hi + 1))
    raise InputError(('invalid Unicode range %s' % (s,)))

Converts string to Unicode range.

'0001..0003' => [1, 2, 3].
'0001' => [1].

Args:
s: string to convert

Returns:
Unicode range

Raises:
InputError: the string is not a valid Unicode range.


def youtube(keyword=None):
    if (keyword is None):
        web.open('https:
    else:
        web.open(quote('https:

Open youtube.

Args:
keyword (optional): Search word.


def build_all_reduce_device_prefixes(job_name, num_tasks):

    if job_name != "localhost":
        return ["/job:%s/task:%d" % (job_name, d) for d in range(0, num_tasks)]
    else:
        assert num_tasks == 1
        return ["/job:%s" % job_name]

Build list of device prefix names for all_reduce.

Args:
job_name: "worker", "ps" or "localhost".
num_tasks: number of jobs across which device names should be generated.

Returns:
A list of device name prefix strings. Each element spells out the full
host name without adding the device.
e.g. "/job:worker/task:0"


def setup_data(X, y, tokenizer, proc_data_path, **kwargs):

    train = not tokenizer.has_vocab
    if train:
        tokenizer.build_vocab(X)

    process_save(X, y, tokenizer, proc_data_path,
                 train=train, **kwargs)
    return tokenizer

Setup data

Args:
X: text data,
y: data labels,
tokenizer: A Tokenizer instance
proc_data_path: Path for the processed data


def get_shared_memory_bytes():
    assert ((sys.platform == 'linux') or (sys.platform == 'linux2'))
    shm_fd = os.open('/dev/shm', os.O_RDONLY)
    try:
        shm_fs_stats = os.fstatvfs(shm_fd)
        shm_avail = (shm_fs_stats.f_bsize * shm_fs_stats.f_bavail)
    finally:
        os.close(shm_fd)
    return shm_avail

Get the size of the shared memory file system.

Returns:
The size of the shared memory file system in bytes.


def SplitLine(self, line, width):
    lines = []
    chunk = ''
    w = 0
    keep = False
    for normal, control in self.SplitIntoNormalAndControl(line):
        keep = True
        while True:
            n = width - w
            w += len(normal)
            if w <= width:
                break
            lines.append(chunk + normal[:n])
            chunk = ''
            keep = False
            w = 0
            normal = normal[n:]
        chunk += normal + control
    if chunk or keep:
        lines.append(chunk)
    return lines

Splits line into width length chunks.

Args:
line: The line to split.
width: The width of each chunk except the last which could be smaller than
width.

Returns:
A list of chunks, all but the last with display width == width.


class TFBaseModelOutputWithCLSToken(ModelOutput):
    last_hidden_state: Optional[tf.Tensor] = None
    cls_token_value: Optional[tf.Tensor] = None
    hidden_states: Tuple[tf.Tensor, ...] | None = None

Base class for model's outputs.

Args:
last_hidden_state (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`):
Sequence of hidden-states at the output of the last layer of the model.
cls_token_value (`tf.Tensor` of shape `(batch_size, 1, hidden_size)`):
Classification token at the output of the last layer of the model.
hidden_states (`tuple(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
Tuple of `tf.Tensor` (one for the output of the embeddings + one for the output of each layer) of shape
`(batch_size, sequence_length, hidden_size)`. Hidden-states of the model at the output of each layer plus
the initial embedding outputs.


def InsertNodesBefore(new_nodes, target):
    for node in new_nodes:
        _InsertNodeAt(node, target, after=False)

Insert new_nodes before the given target location in the tree.

Arguments:
new_nodes: a sequence of new nodes to insert (the nodes should not be in the
tree).
target: the target node before which the new node node will be inserted.

Raises:
RuntimeError: if the tree is corrupted, or the insertion would corrupt it.


def on_graph_execution_trace(self, graph_execution_trace_index, graph_execution_trace):

Monitor method for intra-graph execution events.

Return values (if any) are ignored by the associated DebugDataReader.

Args:
graph_execution_trace_index: The index of the intra-graph execution
event, as an int.
graph_execution_trace: A GraphExecutionTrace data object, for an
intra-graph tensor event.


def ip_geoloc(ip, hit_api=True):

    from ..logs.models import IPInfoCheck
    try:
        obj = IPInfoCheck.objects.get(ip_address=ip).ip_info
    except IPInfoCheck.DoesNotExist:
        if hit_api:
            try:
                obj = IPInfoCheck.check_ip(ip)
            except RateExceededError:
                return None
        else:
            return None
    return obj.latitude, obj.longitude

Get IP geolocation.

Args:
ip (str): IP address to use if no data provided.
hit_api (bool): whether to hit api if info not found.

Returns:
str: latitude and longitude, comma-separated.


def write_file(filepath, encoding, gzipped, data):

    if gzipped:
        with gzip.open(filepath, "wt") as fobj:
            fobj.write(data)
    else:
        with OPEN(filepath, "w", encoding=encoding) as fobj:
            if sys.version_info < (3, 0):
                data = data.decode(encoding)
            fobj.write(data)

Write the data to file either as a gzip file or text based on the
gzipped parameter

Args:
filepath (str): The filename to open
encoding (str): The file encoding to use
gzipped (bool): Whether the file should be gzipped or not
data (str): The data to be written out


def from_keras_model_file(cls, model_file, input_arrays=None, input_shapes=None, output_arrays=None, custom_objects=None):
    TFLiteConverterBase._set_original_model_type(conversion_metadata_fb.ModelType.KERAS_MODEL)
    return TFLiteKerasModelConverter(model_file, input_arrays, input_shapes, output_arrays, custom_objects)

Creates a TFLiteConverter class from a tf.keras model file.

Args:
model_file: Full filepath of HDF5 file containing the tf.keras model.
input_arrays: List of input tensors to freeze graph with. Uses input
arrays from SignatureDef when none are provided. (default None)
input_shapes: Dict of strings representing input tensor names to list of
integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}).
Automatically determined when input shapes is None (e.g., {"foo" :
None}). (default None)
output_arrays: List of output tensors to freeze graph with. Uses output
arrays from SignatureDef when none are provided. (default None)
custom_objects: Dict mapping names (strings) to custom classes or
functions to be considered during model deserialization. (default None)

Returns:
TFLiteConverter class.


def ParseFileObject(self, parser_mediator, file_object):

    file_header_map = self._GetDataTypeMap('binarycookies_file_header')

    try:
      file_header, file_header_data_size = self._ReadStructureFromFileObject(
          file_object, 0, file_header_map)
    except (ValueError, errors.ParseError) as exception:
      raise errors.UnableToParseFile(
          'Unable to read file header with error: {0!s}.'.format(exception))

    if file_header.signature != self._SIGNATURE:
      raise errors.UnableToParseFile('Unsupported file signature.')

    file_offset = file_header_data_size

    page_sizes_data_size = file_header.number_of_pages * 4

    page_sizes_data = file_object.read(page_sizes_data_size)

    context = dtfabric_data_maps.DataTypeMapContext(values={
        'binarycookies_file_header': file_header})

    page_sizes_map = self._GetDataTypeMap('binarycookies_page_sizes')

    try:
      page_sizes_array = self._ReadStructureFromByteStream(
          page_sizes_data, file_offset, page_sizes_map, context=context)
    except (ValueError, errors.ParseError) as exception:
      raise errors.ParseError((
          'Unable to map page sizes data at offset: 0x{0:08x} with error: '
          '{1!s}').format(file_offset, exception))

    file_offset += page_sizes_data_size

    for page_number, page_size in enumerate(page_sizes_array):
      if parser_mediator.abort:
        break

      page_data = file_object.read(page_size)
      if len(page_data) != page_size:
        parser_mediator.ProduceExtractionWarning(
            'unable to read page: {0:d}'.format(page_number))
        break

      self._ParsePage(parser_mediator, file_offset, page_data)

      file_offset += page_size

Parses a Safari binary cookie file-like object.

Args:
parser_mediator (ParserMediator): parser mediator.
file_object (dfvfs.FileIO): file-like object to be parsed.

Raises:
UnableToParseFile: when the file cannot be parsed, this will signal
the event extractor to apply other parsers.


def _NewMatchSection(self, val):

    section = {"criterion": val, "config": {}}
    self.matches.append(section)

    self.section = section["config"]

    self.processor = self._ParseMatchGrp

Create a new configuration section for each match clause.

Each match clause is added to the main config, and the criterion that will
trigger the match is recorded, as is the configuration.

Args:
val: The value following the 'match' keyword.


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

        def _int2hex(number):
            return "{0:0{1}x}".format(number, 2)

        try:
            unpacked_data = struct.unpack('!6B', buff[offset:offset+6])
        except struct.error as exception:
            raise exceptions.UnpackException('%s; %s: %s' % (exception,
                                                             offset, buff))

        transformed_data = ':'.join([_int2hex(x) for x in unpacked_data])
        self._value = transformed_data

Unpack a binary message into this object's attributes.

Unpack the binary value *buff* and update this object attributes based
on the results.

Args:
buff (bytes): Binary data package to be unpacked.
offset (int): Where to begin unpacking.

Raises:
Exception: If there is a struct unpacking error.


def upload_metric(self, dataset_name, table_name, run_id):
    expected_file = os.path.join(self._logging_dir, logger.METRIC_LOG_FILE_NAME)
    with tf.gfile.GFile(expected_file) as f:
        lines = f.readlines()
        metrics = []
        for line in filter((lambda l: l.strip()), lines):
            metric = json.loads(line)
            metric['run_id'] = run_id
            metrics.append(metric)
        table_ref = self._bq_client.dataset(dataset_name).table(table_name)
        errors = self._bq_client.insert_rows_json(table_ref, metrics)
        if errors:
            tf.logging.error('Failed to upload benchmark info to bigquery: {}'.format(errors))

Upload metric information to Bigquery.

Args:
dataset_name: string, the name of bigquery dataset where the data will be
uploaded.
table_name: string, the name of bigquery table under the dataset where
the metric data will be uploaded. This is different from the
benchmark_run table.
run_id: string, a unique ID that will be attached to the data, usually
this is a UUID4 format. This should be the same as the benchmark run_id.


def visit_value(self, value, producer_node):
    pass

Callback for visiting a PValue in the pipeline DAG.

Args:
value: PValue visited (typically a PCollection instance).
producer_node: AppliedPTransform object whose transform produced the
pvalue.


def set_memcache_timeout_policy(self, func):

    if func is None:
      func = self.default_memcache_timeout_policy
    elif isinstance(func, (int, long)):
      func = lambda unused_key, flag=func: flag
    self._memcache_timeout_policy = func

Set the policy function for memcache timeout (expiration).

Args:
func: A function that accepts a key instance as argument and returns
an integer indicating the desired memcache timeout.  May be None.

If the function returns 0 it implies the default timeout.


def __init__(self, data: dict, expires_in: float) -> None:

        self.data = data
        self.expires_in = expires_in
        self.expires_after = time.time() + expires_in

SavedEndpoint class.

A wrapper around a page from ESI that also includes the expiration time
in seconds and the time after which the wrapped data expires.

Args:
data: page data from ESI
expires_in: number of seconds from now that the data expires

Returns:
None


def iplot_state(quantum_state, method='city', figsize=None):

    warnings.warn("iplot_state is deprecated, and will be removed in \
                  the 0.9 release. Use the iplot_state_ * functions \
                  instead.",
                  DeprecationWarning)
    rho = _validate_input_state(quantum_state)
    if method == "city":
        iplot_state_city(rho, figsize=figsize)
    elif method == "paulivec":
        iplot_state_paulivec(rho, figsize=figsize)
    elif method == "qsphere":
        iplot_state_qsphere(rho, figsize=figsize)
    elif method == "bloch":
        iplot_bloch_multivector(rho, figsize=figsize)
    elif method == "hinton":
        iplot_state_hinton(rho, figsize=figsize)
    else:
        raise VisualizationError('Invalid plot state method.')

Plot the quantum state.

Args:
quantum_state (ndarray): statevector or density matrix
representation of a quantum state.
method (str): Plotting method to use.
figsize (tuple): Figure size in pixels.

Raises:
VisualizationError: if the input is not a statevector or density
matrix, or if the state is not an multi-qubit quantum state.


def read_value_no_copy(self):
    with ops.name_scope('Read'):
        value = self._read_variable_op(no_copy=True)
    return array_ops.identity(value)

Constructs an op which reads the value of this variable without copy.

The variable is read without making a copy even when it has been sparsely
accessed. Variables in copy-on-read mode will be converted to copy-on-write
mode.

Returns:
The value of the variable.


def timestamp_a__d_b_Y_H_M_S_z(value):

    a, d, b, Y, t, z = value.split()
    H, M, S = t.split(":")
    return int(calendar.timegm((
        int(Y), _months[b.lower()], int(d), int(H), int(M), int(S), 0, 0, 0
    ))) - _offset(z)

Convert timestamp string to time in seconds since epoch.

Timestamps strings like 'Tue, 18 Jun 2013 22:00:00 +1000' are able to be
converted by this function.

Args:
value: A timestamp string in the format '%a, %d %b %Y %H:%M:%S %z'.

Returns:
The time in seconds since epoch as an integer.

Raises:
ValueError: If timestamp is invalid.
KeyError: If the abbrieviated month is invalid.


def GetKey(self, public_key_hash):

        if public_key_hash.ToBytes() in self._keys.keys():
            return self._keys[public_key_hash.ToBytes()]
        return None

Get the KeyPair belonging to the public key hash.

Args:
public_key_hash (UInt160): a public key hash to get the KeyPair for.

Returns:
KeyPair: If successful, the KeyPair belonging to the public key hash, otherwise None


def create_build_job(user, project, config, code_reference, configmap_refs=None, secret_refs=None):
    (build_job, rebuild) = BuildJob.create(user=user, project=project, config=config, code_reference=code_reference, configmap_refs=configmap_refs, secret_refs=secret_refs)
    if (build_job.succeeded and (not rebuild)):
        return (build_job, True, False)
    if build_job.is_done:
        (build_job, _) = BuildJob.create(user=user, project=project, config=config, code_reference=code_reference, configmap_refs=configmap_refs, secret_refs=secret_refs, nocache=True)
    if (not build_job.is_running):
        auditor.record(event_type=BUILD_JOB_STARTED_TRIGGERED, instance=build_job, actor_id=user.id, actor_name=user.username)
        build_status = start_dockerizer(build_job=build_job)
    else:
        build_status = True
    return (build_job, False, build_status)

Get or Create a build job based on the params.

If a build job already exists, then we check if the build has already an image created.
If the image does not exists, and the job is already done we force create a new job.

Returns:
tuple: (build_job, image_exists[bool], build_status[bool])


def exec_iteration(self, counter, context, step_method):
    logger.debug('starting')
    context['retryCounter'] = counter
    logger.info(f'retry: running step with counter {counter}')
    try:
        step_method(context)
        result = True
    except Exception as ex_info:
        if self.max:
            if (counter == self.max):
                logger.debug(f'retry: max {counter} retries exhausted. raising error.')
                raise
        if (self.stop_on or self.retry_on):
            error_name = get_error_name(ex_info)
            if self.stop_on:
                formatted_stop_list = context.get_formatted_iterable(self.stop_on)
                if (error_name in formatted_stop_list):
                    logger.error(f'{error_name} in stopOn. Raising error and exiting retry.')
                    raise
                else:
                    logger.debug(f'{error_name} not in stopOn. Continue.')
            if self.retry_on:
                formatted_retry_list = context.get_formatted_iterable(self.retry_on)
                if (error_name not in formatted_retry_list):
                    logger.error(f'{error_name} not in retryOn. Raising error and exiting retry.')
                    raise
                else:
                    logger.debug(f'{error_name} in retryOn. Retry again.')
        result = False
        logger.error(f)
    logger.debug(f'retry: done step with counter {counter}')
    logger.debug('done')
    return result

Run a single retry iteration.

This method abides by the signature invoked by poll.while_until_true,
which is to say (counter, *args, **kwargs). In a normal execution
chain, this method's args passed by self.retry_loop where context
and step_method set. while_until_true injects counter as a 1st arg.

Args:
counter. int. loop counter, which number of iteration is this.
context: (pypyr.context.Context) The pypyr context. This arg will
mutate - after method execution will contain the new
updated context.
step_method: (method/function) This is the method/function that
will execute on every loop iteration. Signature is:
function(context)

Returns:
bool. True if step execution completed without error.
False if error occured during step execution.


def _validate_body_parameters(self, body, action_spec):

        processed_params = []
        for param_name, param_spec in action_spec['parameters'].items():
            if param_spec['in'] == 'body':
                processed_params.append(param_name)

                if 'type' in param_spec.keys() and not self.check_type(body, param_spec['type']):
                    msg = "Check type did not validate for {0} and {1}".format(param_spec['type'], body)
                    return False, msg

                elif 'schema' in param_spec.keys():
                    if 'type' in param_spec['schema'].keys() and param_spec['schema']['type'] == 'array':

                        definition_name = self.get_definition_name_from_ref(param_spec['schema']['items']['$ref'])
                        if len(body) > 0 and not self.validate_definition(definition_name, body[0]):
                            msg = "The body did not validate against its definition"
                            return False, msg
                    elif ('type' in param_spec['schema'].keys() and not
                          self.check_type(body, param_spec['schema']['type'])):

                        msg = "Check type did not validate for {0} and {1}".format(param_spec['schema']['type'], body)
                        return False, msg
                    else:
                        definition_name = self.get_definition_name_from_ref(param_spec['schema']['$ref'])
                        if not self.validate_definition(definition_name, body):
                            msg = "The body did not validate against its definition"
                            return False, msg

        if not all(param in processed_params for param, spec in action_spec['parameters'].items()
                   if spec['in'] == 'body' and 'required' in spec and spec['required']):
            msg = "Not all required parameters were present"
            return False, msg

        return True, ""

Check the body parameter for the action specification.

Args:
body: body parameter to check.
action_spec: specification of the action.

Returns:
True if the body is valid.
A string containing an error msg in case the body did not validate,
otherwise the string is empty


def register_func(self, func_name, func_kwargs):

        self.scan_funcs.append(func_name)
        self.scan_kwargs.append(func_kwargs)

Register a scan function

Args:
func_name: The function name of a scan function.
func_kwargs: A dict containing arguments of the scan function.


def merge_context(self, tag, metadata):

        self.entities.append(tag)
        for k in metadata.keys():
            if k not in self.metadata:
                self.metadata[k] = k

merge into contextManagerFrame new entity and metadata.

Appends tag as new entity and adds keys in metadata to keys in
self.metadata.

Args:
tag(str): entity to be added to self.entities
metadata(object): metadata containes keys to be added to self.metadata


def _create_and_save_tf1_conv_model(self, saved_model_path: str, signature_key: str, tags: Collection[str], input_key: str, output_key: str, *, input_shape: Sequence[int]=(1, 3, 4, 3), filter_shape: Sequence[int]=(2, 3, 3, 2), use_variable: bool=False) -> core.Tensor:
    with ops.Graph().as_default(), session.Session() as sess:
        in_placeholder, output_tensor = self._create_simple_tf1_conv_model(input_shape=input_shape, filter_shape=filter_shape, use_variable_for_filter=use_variable)
        if use_variable:
            sess.run(variables.global_variables_initializer())
        self._save_tf1_model(sess, saved_model_path, signature_key, tags, inputs={input_key: in_placeholder}, outputs={output_key: output_tensor})
    return in_placeholder

Creates and saves a simple convolution model.

This is intended to be used for TF1 (graph mode) tests.

Args:
saved_model_path: Directory to save the model.
signature_key: The key to the SignatureDef that inputs & outputs
correspond to.
tags: Set of tags associated with the model.
input_key: The key to the input tensor.
output_key: The key to the output tensor.
input_shape: Shape of the input tensor.
filter_shape: Shape of the filter.
use_variable: Setting this to `True` makes the filter for the conv
operation a `tf.Variable`.

Returns:
in_placeholder: The placeholder tensor used as an input to the model.


def _try_parse_datetime(time_str, fmts):
    result = None
    for fmt in fmts:
        try:
            result = datetime.strptime(time_str, fmt)
            break
        except ValueError:
            pass
    return result

A helper function that attempts to parse the input time_str as a date.

Args:

time_str (str): A string representing the time

fmts (list): A list of date format strings

Returns:
datetime: Returns a datetime object if parsed properly, otherwise None


def get_linenumbers(functions, module, searchstr='def {}(image):\n'):

    lines = inspect.getsourcelines(module)[0]
    line_numbers = {}
    for function in functions:
        try:
            line_numbers[function] = lines.index(
                    searchstr.format(function)) + 1
        except ValueError:
            print(r'Can not find `{}`'.format(searchstr.format(function)))
            line_numbers[function] = 0
    return line_numbers

Returns a dictionary which maps function names to line numbers.

Args:
functions: a list of function names
module:    the module to look the functions up
searchstr: the string to search for
Returns:
A dictionary with functions as keys and their line numbers as values.


def __init__(self, attribute_values_hash):

    super(FakeIdentifier, self).__init__()
    self.attribute_values_hash = attribute_values_hash

Initializes a fake attribute container identifier.

Args:
attribute_values_hash (int): hash value of the attribute values.


def __init__(self, ui_type='readline', dump_root=None, thread_name_filter=None, config_file_path=None):
    self._ui_type = ui_type
    self._dump_root = dump_root
    self._thread_name_filter = thread_name_filter
    self._session_wrapper = None
    self._pending_tensor_filters = {}
    self._config_file_path = config_file_path

Create a local debugger command-line interface (CLI) hook.

Args:
ui_type: (`str`) requested user-interface type. Currently supported:
(readline).
dump_root: (`str`) optional path to the dump root directory. Must be a
directory that does not exist or an empty directory. If the directory
does not exist, it will be created by the debugger core during debug
`run()` calls and removed afterwards.
thread_name_filter: Regular-expression white list for threads on which the
wrapper session will be active. See doc of `BaseDebugWrapperSession` for
more details.
config_file_path: Optional override to the default configuration file
path, which is at `${HOME}/.tfdbg_config`.


def get_image_features(self, pixel_values: torch.FloatTensor, image_grid_thw: Optional[torch.LongTensor]=None):
    pixel_values = pixel_values.type(self.visual.dtype)
    image_embeds = self.visual(pixel_values, grid_thw=image_grid_thw)
    split_sizes = (image_grid_thw.prod(-1)
    image_embeds = torch.split(image_embeds, split_sizes)
    return image_embeds

Encodes images into continuous embeddings that can be forwarded to the language model.

Args:
pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`):
The tensors corresponding to the input images.
image_grid_thw (`torch.LongTensor` of shape `(num_images, 3)`, *optional*):
The temporal, height and width of feature shape of each image in LLM.


def get_by_index(self, index):
    if (index >= len(self._datasets)):
        raise DataInvalidIndex('A dataset with index {} does not exist'.format(index))
    return self._datasets[index]

Return a dataset by its index.

Args:
index (int): The index of the dataset that should be returned.

Raises:
DataInvalidIndex: If the index does not represent a valid dataset.


def ioa(boxes1, boxes2):
    intersect = intersection(boxes1, boxes2)
    inv_areas = np.expand_dims((1.0 / area(boxes2)), axis=0)
    return (intersect * inv_areas)

Computes pairwise intersection-over-area between box collections.

Intersection-over-area (ioa) between two boxes box1 and box2 is defined as
their intersection area over box2's area. Note that ioa is not symmetric,
that is, IOA(box1, box2) != IOA(box2, box1).

Args:
boxes1: a numpy array with shape [N, 4] holding N boxes.
boxes2: a numpy array with shape [M, 4] holding N boxes.

Returns:
a numpy array with shape [N, M] representing pairwise ioa scores.


def _maybe_define_function(args, kwargs, tracing_options):
    bound_args = function_type_utils.canonicalize_function_inputs(args, kwargs, tracing_options.polymorphic_type, tracing_options.default_values, tracing_options.is_pure)
    args, kwargs = (bound_args.args, bound_args.kwargs)
    if tracing_options.input_signature is not None:
        args = (*tracing_options.input_signature, *args[len(tracing_options.input_signature):])
    current_func_context = function_context.make_function_context(tracing_options.scope_type)
    capture_types = tracing_options.function_captures.capture_types if tracing_options.function_captures else {}
    lookup_func_type, lookup_func_context = function_type_utils.make_canonicalized_monomorphic_type(args, kwargs, capture_types, tracing_options.polymorphic_type)
    if tracing_options.function_cache is not None:
        concrete_function = tracing_options.function_cache.lookup(lookup_func_type, current_func_context)
    else:
        concrete_function = None
    if concrete_function is not None:
        return concrete_function
    with monitoring.MonitoredTimer(_graph_building_time_counter.get_cell()) if not ops.inside_function() else contextlib.nullcontext():
        with trace.Trace('tf.function-graph_building'):
            logging.vlog(1, 'Creating new FuncGraph for Python function %r (key: %r, %r)', tracing_options.python_function, current_func_context, lookup_func_type)
            logging.vlog(2, 'Python function signature [args: %s] [kwargs: %s]', args, kwargs)
            ag_status = ag_ctx.Status.ENABLED if tracing_options.autograph else ag_ctx.Status.DISABLED
            with ag_ctx.ControlStatusCtx(status=ag_status, options=tracing_options.autograph_options):
                func_graph = func_graph_module.FuncGraph(tracing_options.name)
                if tracing_options.input_signature is None and tracing_options.reduce_retracing and tracing_options.function_cache:
                    target_func_type = tracing_options.function_cache.generalize(current_func_context, lookup_func_type)
                else:
                    target_func_type = lookup_func_type
                concrete_function = _create_concrete_function(target_func_type, lookup_func_context, func_graph, tracing_options)
                if tracing_options.function_cache is not None:
                    tracing_options.function_cache.add(concrete_function, current_func_context)
                return concrete_function

Gets a function for these inputs, defining it if necessary.

Args:
args: The varargs for the Python function.
kwargs: The keyword args for the Python function.
tracing_options: TracingOptions for the tracing process.

Returns:
A ConcreteFunction generated based on args, kwargs and tracing_options.

Raises:
ValueError: If inputs are incompatible with the input signature.
TypeError: If the function inputs include non-hashable objects
RuntimeError: If there's an internal bug (inconsistency) in handling
shape relaxation retracing.


def delete(self):
    session = self._session
    url = session._build_url(self._resource_path(), self.id)
    return session.delete(url, CB.boolean(204))

Delete the resource.

Returns:

True if the delete is successful. Will throw an error if
other errors occur


def save(self, file_path, note_df):

        chord = pretty_midi.PrettyMIDI()
        for program in note_df.program.drop_duplicates().values.tolist():
            df = note_df[note_df.program == program]
            midi_obj = pretty_midi.Instrument(program=program)
            for i in range(df.shape[0]):
                note = pretty_midi.Note(
                    velocity=int(df.iloc[i, :]["velocity"]),
                    pitch=int(df.iloc[i, :]["pitch"]),
                    start=float(df.iloc[i, :]["start"]),
                    end=float(df.iloc[i, :]["end"])
                )

                midi_obj.notes.append(note)

            chord.instruments.append(midi_obj)

        chord.write(file_path)

Save MIDI file.

Args:
file_path:    File path of MIDI.
note_df:      `pd.DataFrame` of note data.


def _span_attrs_to_pb(span_attr, proto_type):
    attr_pb = getattr(trace_pb2.Span, proto_type)()
    ParseDict(span_attr, attr_pb)
    return attr_pb

Convert a span attribute dict to protobuf, including Links, Attributes,
TimeEvents.

Args:
span_attr (dict): A dict that needs to be converted to protobuf.
proto_type (str): The type of the Protobuf.

Returns:
An instance of the specified protobuf.


def _apply_unitary_circuit(circuit: Circuit, state: np.ndarray, qubits: Tuple[(ops.Qid, ...)], dtype: Type[np.number]) -> np.ndarray:
    qubit_map = {q: i for (i, q) in enumerate(qubits)}
    buffer = np.zeros(state.shape, dtype=dtype)

    def on_stuck(bad_op):
        return TypeError('Operation without a known matrix or decomposition: {!r}'.format(bad_op))
    unitary_ops = protocols.decompose(circuit.all_operations(), keep=protocols.has_unitary, intercepting_decomposer=_decompose_measurement_inversions, on_stuck_raise=on_stuck)
    for op in unitary_ops:
        indices = [qubit_map[q] for q in op.qubits]
        result = protocols.apply_unitary(unitary_value=op, args=protocols.ApplyUnitaryArgs(state, buffer, indices))
        if (result is buffer):
            buffer = state
        state = result
    return state

Applies a circuit's unitary effect to the given vector or matrix.

This method assumes that the caller wants to ignore measurements.

Args:
circuit: The circuit to simulate. All operations must have a known
matrix or decompositions leading to known matrices. Measurements
are allowed to be in the circuit, but they will be ignored.
state: The initial state tensor (i.e. superposition or unitary matrix).
This is what will be left-multiplied by the circuit's effective
unitary. If this is a state vector, it must have shape
(2,) * num_qubits. If it is a unitary matrix it should have shape
(2,) * (2*num_qubits).
qubits: The qubits in the state tensor. Determines which axes operations
apply to. An operation targeting the k'th qubit in this list will
operate on the k'th axis of the state tensor.
dtype: The numpy dtype to use for applying the unitary. Must be a
complex dtype.

Returns:
The left-multiplied state tensor.


def tokenize(self, docs):

        if self.n_jobs == 1:
            return [self._tokenize(doc) for doc in docs]
        else:
            return parallel(self._tokenize, docs, self.n_jobs)

Tokenizes a document, using a lemmatizer.

Args:
| doc (str)                 -- the text document to process.

Returns:
| list                      -- the list of tokens.


def accumulate_hash(self, current_hash: torch.LongTensor, data: torch.LongTensor, multiplier: int=6364136223846793005, increment: int=1) -> torch.LongTensor:
    for i in range(data.shape[-1]):
        current_hash = torch.add(current_hash, data[..., i])
        current_hash = torch.mul(current_hash, multiplier)
        current_hash = torch.add(current_hash, increment)
    return current_hash

Accumulate hash of data on current hash.

Method uses adapted linear congruential generator with newlib/musl parameters.

This function has following property -
f(x, data[T]) = f(f(x, data[:T - 1]), data[T])

This function expects current_hash.shape and data.shape[:-1] to
match/broadcastable.

Args:
current_hash (`torch.LongTensor`):
(shape,)
data (`torch.LongTensor`):
(shape, tensor_len)
multiplier (`int`, optional, *optional*, defaults to 6364136223846793005):
multiplier of linear congruential generator
increment (`int`, optional, *optional*, defaults to 1):
increment of linear congruential generator

Returns:
updated hash (shape,)


def pool_direct(input, window_shape, pooling_type, padding, dilation_rate, strides, data_format=None):
    if data_format is None or not data_format.startswith('NC'):
        spatial_start_dim = 1
    else:
        spatial_start_dim = 2
    output = input
    for i in range(len(window_shape)):
        output = pool_direct_single_axis(input=output, axis=i + spatial_start_dim, window_size=window_shape[i], pooling_type=pooling_type, padding=padding, dilation_rate=dilation_rate[i], stride=strides[i])
    return output

Numpy implementation of pooling.

This is intended for testing only, and therefore isn't particularly efficient.

See tensorflow.nn.pool.

Args:
input: numpy array of rank N+2.
window_shape: Sequence of N ints >= 1.
pooling_type: either "MAX" or "AVG".
padding: either "SAME" or "VALID".
dilation_rate: Sequence of N ints >= 1.
strides: Sequence of N ints >= 1.
data_format: If specified and starts with "NC", indicates that second
dimension, rather than the last dimension, specifies the channel.

Returns:
pooling output array of rank N+2.

Raises:
ValueError: if arguments are invalid.


def filter(stream_spec, filter_name, *args, **kwargs):
    return filter_multi_output(stream_spec, filter_name, *args, **kwargs).stream()

Apply custom filter.

``filter_`` is normally used by higher-level filter functions such as ``hflip``, but if a filter implementation
is missing from ``fmpeg-python``, you can call ``filter_`` directly to have ``fmpeg-python`` pass the filter name
and arguments to ffmpeg verbatim.

Args:
stream_spec: a Stream, list of Streams, or label-to-Stream dictionary mapping
filter_name: ffmpeg filter name, e.g. `colorchannelmixer`
*args: list of args to pass to ffmpeg verbatim
**kwargs: list of keyword-args to pass to ffmpeg verbatim

The function name is suffixed with ``_`` in order avoid confusion with the standard python ``filter`` function.

Example:

``ffmpeg.input('in.mp4').filter('hflip').output('out.mp4').run()``


def _pyval_empty_list_depth(pyval):
    if isinstance(pyval, list):
        if not pyval:
            return 1
        depths = [_pyval_empty_list_depth(v) for v in pyval]
        if any((depth is None for depth in depths)):
            return None
        else:
            return max(depths) + 1
    else:
        return None

Find the max depth for nested empty lists.

Args:
pyval: A nested python list.

Returns:
The maximum depth of empty lists in `pyval`, or None if `pyval` contains
anything other than nested empty lists.


def reqTickers(
            self, *contracts: List[Contract],
            regulatorySnapshot: bool = False) -> List[Ticker]:

        return self._run(
            self.reqTickersAsync(
                *contracts, regulatorySnapshot=regulatorySnapshot))

Request and return a list of snapshot tickers.
The list is returned when all tickers are ready.

This method is blocking.

Args:
contracts: Contracts to get tickers for.
regulatorySnapshot: Request NBBO snapshots (may incur a fee).


def hwvtep_activate_hwvtep(self, **kwargs):
    name = kwargs.pop('name')
    name_args = dict(name=name)
    method_name = 'overlay_gateway_activate'
    method_class = self._brocade_tunnels
    gw_attr = getattr(method_class, method_name)
    config = gw_attr(**name_args)
    output = self._callback(config)
    return output

Activate the hwvtep

Args:
name (str): overlay_gateway name
callback (function): A function executed upon completion of the
method.

Returns:
Return value of `callback`.

Raises:
None


def last_updated(self, path):
    try:
        return self._blobstorageIO().last_updated(path)
    except Exception as e:
        raise BeamIOError('Last updated operation failed', {path: e})

Get UNIX Epoch time in seconds on the FileSystem.

Args:
path: string path of file.

Returns: float UNIX Epoch time

Raises:
``BeamIOError``: if path doesn't exist.


def detect_phantomjs(version='2.1'):
    if (settings.phantomjs_path() is not None):
        phantomjs_path = settings.phantomjs_path()
    elif hasattr(shutil, 'which'):
        phantomjs_path = (shutil.which('phantomjs') or 'phantomjs')
    else:
        phantomjs_path = 'phantomjs'
    try:
        proc = Popen([phantomjs_path, '--version'], stdout=PIPE, stderr=PIPE)
        proc.wait()
        out = proc.communicate()
        if (len(out[1]) > 0):
            raise RuntimeError(('Error encountered in PhantomJS detection: %r' % out[1].decode('utf8')))
        required = V(version)
        installed = V(out[0].decode('utf8'))
        if (installed < required):
            raise RuntimeError(('PhantomJS version to old. Version>=%s required, installed: %s' % (required, installed)))
    except OSError:
        raise RuntimeError('PhantomJS is not present in PATH or BOKEH_PHANTOMJS_PATH. Try "conda install phantomjs" or             "npm install -g phantomjs-prebuilt"')
    return phantomjs_path

Detect if PhantomJS is avaiable in PATH, at a minimum version.

Args:
version (str, optional) :
Required minimum version for PhantomJS (mostly for testing)

Returns:
str, path to PhantomJS


def check_tags_contain(actual, expected):
    actual_set = set(((item['Key'], item['Value']) for item in actual))
    expected_set = set(((item['Key'], item['Value']) for item in expected))
    return (actual_set >= expected_set)

Check if a set of AWS resource tags is contained in another

Every tag key in `expected` must be present in `actual`, and have the same
value. Extra keys in `actual` but not in `expected` are ignored.

Args:
actual (list): Set of tags to be verified, usually from the description
of a resource. Each item must be a `dict` containing `Key` and
`Value` items.
expected (list): Set of tags that must be present in `actual` (in the
same format).


def make_preprocessing_fn(output_dir, features, keep_target):

  def preprocessing_fn(inputs):

    stats = json.loads(
      file_io.read_file_to_string(
          os.path.join(output_dir, STATS_FILE)).decode())

    result = {}
    for name, transform in six.iteritems(features):
      transform_name = transform['transform']
      source_column = transform['source_column']

      if transform_name == TARGET_TRANSFORM:
        if not keep_target:
          continue
        if file_io.file_exists(os.path.join(output_dir, VOCAB_ANALYSIS_FILE % source_column)):
          transform_name = 'one_hot'
        else:
          transform_name = 'identity'

      if transform_name == 'identity':
        result[name] = inputs[source_column]
      elif transform_name == 'scale':
        result[name] = _scale(
            inputs[name],
            min_x_value=stats['column_stats'][source_column]['min'],
            max_x_value=stats['column_stats'][source_column]['max'],
            output_min=transform.get('value', 1) * (-1),
            output_max=transform.get('value', 1))
      elif transform_name in [ONE_HOT_TRANSFORM, MULTI_HOT_TRANSFORM]:
        vocab, ex_count = read_vocab_file(
            os.path.join(output_dir, VOCAB_ANALYSIS_FILE % source_column))
        if transform_name == MULTI_HOT_TRANSFORM:
          separator = transform.get('separator', ' ')
          tokens = tf.string_split(inputs[source_column], separator)
          result[name] = _string_to_int(tokens, vocab)
        else:
          result[name] = _string_to_int(inputs[source_column], vocab)
      elif transform_name == IMAGE_TRANSFORM:
        make_image_to_vec_fn = _make_image_to_vec_tito(
            name, checkpoint=transform.get('checkpoint', None))
        result[name] = make_image_to_vec_fn(inputs[source_column])
      else:
        raise ValueError('unknown transform %s' % transform_name)
    return result

  return preprocessing_fn

Makes a preprocessing function.

Args:
output_dir: folder path that contains the vocab and stats files.
features: the features dict

Returns:
a function that takes a dict of input tensors


def _two_element_tuple(int_or_tuple):

  if isinstance(int_or_tuple, (list, tuple)):
    if len(int_or_tuple) != 2:
      raise ValueError('Must be a list with 2 elements: %s' % int_or_tuple)
    return int(int_or_tuple[0]), int(int_or_tuple[1])
  if isinstance(int_or_tuple, int):
    return int(int_or_tuple), int(int_or_tuple)
  if isinstance(int_or_tuple, tf.TensorShape):
    if len(int_or_tuple) == 2:
      return int_or_tuple[0], int_or_tuple[1]
  raise ValueError('Must be an int, a list with 2 elements or a TensorShape of '
                   'length 2')

Converts `int_or_tuple` to height, width.

Several of the functions that follow accept arguments as either
a tuple of 2 integers or a single integer.  A single integer
indicates that the 2 values of the tuple are the same.

This functions normalizes the input value by always returning a tuple.

Args:
int_or_tuple: A list of 2 ints, a single int or a tf.TensorShape.

Returns:
A tuple with 2 values.

Raises:
ValueError: If `int_or_tuple` it not well formed.


def getfullargspec(obj):
    decorators, target = tf_decorator.unwrap(obj)
    for d in decorators:
        if d.decorator_argspec is not None:
            return _convert_maybe_argspec_to_fullargspec(d.decorator_argspec)
    return _getfullargspec(target)

TFDecorator-aware replacement for `inspect.getfullargspec`.

This wrapper emulates `inspect.getfullargspec` in[^)]* Python2.

Args:
obj: A callable, possibly decorated.

Returns:
The `FullArgSpec` that describes the signature of
the outermost decorator that changes the callable's signature. If the
callable is not decorated, `inspect.getfullargspec()` will be called
directly on the callable.


def run_ninja(targets, fail_collector=None, fail_fast=False, verbose=False):
    cmd = ['ninja', '-k', '1' if fail_fast else '100000'] + targets
    with subprocess.Popen(cmd, cwd=OUT_DIR, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) as process:
        failed_targets = []
        print_if_verbose = False
        with open(NINJA_LOG, 'w') as ninja_log:
            while True:
                line = process.stdout.readline()
                if not line:
                    break
                ninja_log.write(line)
                msg_type, modname, logfile = parse_ninja_output_line(line)
                if msg_type == _NINJA_FAILURE_MSG:
                    failed_targets.append(line[len(NINJA_FAILURE_PREFIX):].strip())
                    print_if_verbose = True
                if msg_type == _TEST_MODULE_PASS_MSG or msg_type == _TEST_MODULE_FAIL_MSG:
                    print(line)
                    if msg_type == _TEST_MODULE_FAIL_MSG:
                        fail_collector.add_failure(modname, logfile)
                    print_if_verbose = False
                if verbose and print_if_verbose:
                    print(line.rstrip())
            if failed_targets:
                summary_hdr = '>>> Found Ninja target failures (includes test failures):'
                print('\n' + summary_hdr)
                ninja_log.write('\n' + summary_hdr + '\n')
                for t in failed_targets:
                    target = f'    - {t}'
                    print(target)
                    ninja_log.write(target + '\n')
        process.wait()
        if process.returncode == 0:
            return True
        else:
            print(f">>> FAILED: Ninja command '{shlex.join(cmd)}'.")
            print(">>>         Run it in the 'out' directory to reproduce.")
            print(f">>>         Full Ninja output is available in '{NINJA_LOG}'.")
            print('>>>         Failing test modules (if any) will be reported below.')
            return False

Run ninja over the list of specified targets.

Arguments:
targets: The list of targets to run.
fail_collector: A FailCollector object to collect failures.
fail_fast: If True, abort at the first target failure.
verbose: If True, print verbose output.

Returns:
True if no target fails. False, otherwise.


def delete(self, uri, force=False, timeout=-1, custom_headers=None):

        if force:
            uri += '?force=True'

        logger.debug("Delete resource (uri = %s)" % (str(uri)))

        task, body = self._connection.delete(uri, custom_headers=custom_headers)

        if not task:

            return True

        task = self._task_monitor.wait_for_task(task, timeout=timeout)

        return task

Deletes current resource.

Args:
force: Flag to delete the resource forcefully, default is False.
timeout: Timeout in seconds.
custom_headers: Allows to set custom http headers.


def register_targets(name, targets, region=None, key=None, keyid=None, profile=None):
    targetsdict = []
    if (isinstance(targets, six.string_types) or isinstance(targets, six.text_type)):
        targetsdict.append({'Id': targets})
    else:
        for target in targets:
            targetsdict.append({'Id': target})
    conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
    try:
        registered_targets = conn.register_targets(TargetGroupArn=name, Targets=targetsdict)
        if registered_targets:
            return True
        return False
    except ClientError as error:
        log.warning(error)
        return False

Register targets to a target froup of an ALB. ``targets`` is either a
instance id string or a list of instance id's.

Returns:

- ``True``: instance(s) registered successfully
- ``False``: instance(s) failed to be registered

CLI example:

.. code-block:: bash

salt myminion boto_elbv2.register_targets myelb instance_id
salt myminion boto_elbv2.register_targets myelb "[instance_id,instance_id]"


def port_create_vlan(br, port, id, internal=False):

    interfaces = __salt__['network.interfaces']()
    if not 0 <= id <= 4095:
        return False
    elif not bridge_exists(br):
        return False
    elif not internal and port not in interfaces:
        return False
    elif port in port_list(br):
        cmd = 'ovs-vsctl set port {0} tag={1}'.format(port, id)
        if internal:
            cmd += ' -- set interface {0} type=internal'.format(port)
        result = __salt__['cmd.run_all'](cmd)
        return _retcode_to_bool(result['retcode'])
    else:
        cmd = 'ovs-vsctl add-port {0} {1} tag={2}'.format(br, port, id)
        if internal:
            cmd += ' -- set interface {0} type=internal'.format(port)
        result = __salt__['cmd.run_all'](cmd)
        return _retcode_to_bool(result['retcode'])

Isolate VM traffic using VLANs.

Args:
br: A string - bridge name.
port: A string - port name.
id: An integer in the valid range 0 to 4095 (inclusive), name of VLAN.
internal: A boolean to create an internal interface if one does not exist.

Returns:
True on success, else False.

.. versionadded:: 2016.3.0

CLI Example:
.. code-block:: bash

salt '*' openvswitch.port_create_vlan br0 tap0 100


def recipe_trends_places_to_sheets_via_query(config, auth_write, secret, key, places_dataset, places_query, places_legacy, destination_sheet, destination_tab):
    twitter(config, {'auth': auth_write, 'secret': secret, 'key': key, 'trends': {'places': {'single_cell': True, 'bigquery': {'dataset': places_dataset, 'query': places_query, 'legacy': places_legacy}}}, 'out': {'sheets': {'sheet': destination_sheet, 'tab': destination_tab, 'range': 'A1'}}})

Move using a WOEID query.

Args:
auth_write (authentication) - Credentials used for writing data.
secret (string) - NA
key (string) - NA
places_dataset (string) - NA
places_query (string) - NA
places_legacy (boolean) - NA
destination_sheet (string) - NA
destination_tab (string) - NA


def when(self, key):

        ctx = Context(key, self)
        self.context.append(ctx)
        return ctx

Specify context, i.e. condition that must be met.

Arguments:
key (str): Name of the context whose value you want to query.
Returns:
Context:


def intrusion_set(self, name, **kwargs):
    group_obj = IntrusionSet(name, **kwargs)
    return self._group(group_obj)

Add Intrusion Set data to Batch object.

Args:
name (str): The name for this Group.
date_added (str, kwargs): The date timestamp the Indicator was created.
xid (str, kwargs): The external id for this Group.

Returns:
obj: An instance of IntrusionSet.


def is_traceback_filtering_enabled():
    value = getattr(_ENABLE_TRACEBACK_FILTERING, 'value', True)
    return value

Check whether traceback filtering is currently enabled.

See also `tf.debugging.enable_traceback_filtering()` and
`tf.debugging.disable_traceback_filtering()`. Note that filtering out
internal frames from the tracebacks of exceptions raised by TensorFlow code
is the default behavior.

Returns:
True if traceback filtering is enabled
(e.g. if `tf.debugging.enable_traceback_filtering()` was called),
and False otherwise (e.g. if `tf.debugging.disable_traceback_filtering()`
was called).


def get_structure(atoms, cls=None):
    symbols = atoms.get_chemical_symbols()
    positions = atoms.get_positions()
    lattice = atoms.get_cell()
    cls = (Structure if (cls is None) else cls)
    return cls(lattice, symbols, positions, coords_are_cartesian=True)

Returns pymatgen structure from ASE Atoms.

Args:
atoms: ASE Atoms object
cls: The Structure class to instantiate (defaults to pymatgen structure)

Returns:
Equivalent pymatgen.core.structure.Structure


def add_section(self, id_, parent_id, section_type, points):

        assert id_ not in self.sections, 'id %s already exists in sections' % id_
        self.sections[id_] = BlockNeuronBuilder.BlockSection(parent_id, section_type, points)

add a section

Args:
id_(int): identifying number of the section
parent_id(int): identifying number of the parent of this section
section_type(int): the section type as defined by POINT_TYPE
points is an array of [X, Y, Z, R]


def match_filename( filename ):

    f = next( ( '{}{}'.format( filename, extension ) for extension in [ '', '.gz' ]
        if Path( '{}{}'.format( filename, extension ) ).is_file() ), None )
    return f

Checks whether a file exists, either as named, or as a a gzippped file (filename.gz)

Args:
(Str): The root filename.

Returns:
(Str|None): if the file exists (either as the root filename, or gzipped), the return
value will be the actual filename. If no matching filename is found the return
value is set to None


def findall(self, title=None):
    if (title is None):
        return list(self._sheets)
    if (title not in self._titles):
        return []
    return list(self._titles[title])

Return a list of worksheets with the given title.

Args:
title(str): title/name of the worksheets to return, or ``None`` for all
Returns:
list: list of contained worksheet instances (possibly empty)


def _ExtractRequestSummaryFields(self, request, error=None):

    headers = request.headers
    summary_fields = {
        'server': request.get_full_url(),
        'contentRange': headers['Content-range'],
        'contentLength': headers['Content-length']
    }

    if error:
      summary_fields['isError'] = True
      summary_fields['errorMessage'] = error.reason
    else:
      summary_fields['isError'] = False

    return summary_fields

Extract fields used in the summary logs.

Args:
request:  a urllib2.Request instance configured to make the request.
[optional]
error: a urllib2.HttpError instance used to retrieve error details.

Returns:
A dict containing the fields to be output in the summary logs.


def evaluate(self, instance, step, extra):
    chain = step.chain[1:]
    if (self.strict and (not chain)):
        raise TypeError("A ContainerAttribute in 'strict' mode can only be used within a SubFactory.")
    return self.function(instance, chain)

Evaluate the current ContainerAttribute.

Args:
obj (LazyStub): a lazy stub of the object being constructed, if
needed.
containers (list of LazyStub): a list of lazy stubs of factories
being evaluated in a chain, each item being a future field of
next one.


async def search_raw(self, term: str, limit: int = 3) -> List[dict]:

        return (await self._get(term=term))['list'][:limit]

Performs a search for a term and returns the raw response.

Args:
term: The term to be defined.

limit: The maximum amount of results you'd like.
Defaults to 3.

Returns:
A list of :class:`dict`\s which contain word information.


def __init__(self, config):

        self.event = Event.create(__name__)
        self.logger = Logger.get_logger(__name__)
        self.config = config
        self.success = True
        self.env = {}
        self.env.update(config.env)

        self.stdout = subprocess.PIPE
        self.stderr = subprocess.STDOUT
        self.shell = False
        self.exit_code = 0

Initialize with Bash code and optional environment variables.

Args:
config(ShellConfig): options for configuring Bash environment and behavior


def get_most_recent_images(self, results, types=[], sensors=[], N=1):

        if not len(results):
            return None

        if types:
            results = [r for r in results if r['type'] in types]

        if sensors:
            results = [r for r in results if r['properties'].get('sensorPlatformName') in sensors]

        newlist = sorted(results, key=lambda k: k['properties'].get('timestamp'), reverse=True)
        return newlist[:N]

Return the most recent image

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

Returns:
single catalog item, or none if not found


def done(self):
    return self._cluster.done()

Returns whether all the scheduled functions have finished execution.

If any previously scheduled function raises an error, `done` will fail by
raising any one of those errors.

When `done` returns True or raises, it guarantees that there is no function
that is still being executed.

Returns:
Whether all the scheduled functions have finished execution.
Raises:
Exception: one of the exceptions caught by the coordinator by any
previously scheduled function since the last time an error was thrown or
since the beginning of the program.


def sobol_sample(dim, num_results, skip=0, dtype=dtypes.float32, name=None):
    with ops.name_scope(name, 'sobol', [dim, num_results, skip]):
        return gen_math_ops.sobol_sample(dim, num_results, skip, dtype=dtype)

Generates points from the Sobol sequence.

Creates a Sobol sequence with `num_results` samples. Each sample has dimension
`dim`. Skips the first `skip` samples.

Args:
dim: Positive scalar `Tensor` representing each sample's dimension.
num_results: Positive scalar `Tensor` of dtype int32. The number of Sobol
points to return in the output.
skip: (Optional) Positive scalar `Tensor` of dtype int32. The number of
initial points of the Sobol sequence to skip. Default value is 0.
dtype: (Optional) The `tf.Dtype` of the sample. One of: `tf.float32` or
`tf.float64`. Defaults to `tf.float32`.
name: (Optional) Python `str` name prefixed to ops created by this function.

Returns:
`Tensor` of samples from Sobol sequence with `shape` [num_results, dim].


def set_clang_compiler_path(environ_cp):
    default_clang_path = '/usr/lib/llvm-18/bin/clang'
    if not os.path.exists(default_clang_path):
        default_clang_path = '/usr/lib/llvm-17/bin/clang'
        if not os.path.exists(default_clang_path):
            default_clang_path = '/usr/lib/llvm-16/bin/clang'
        if not os.path.exists(default_clang_path):
            default_clang_path = shutil.which('clang') or ''
    clang_compiler_path = prompt_loop_or_load_from_env(environ_cp, var_name='CLANG_COMPILER_PATH', var_default=default_clang_path, ask_for_var='Please specify the path to clang executable.', check_success=os.path.exists, resolve_symlinks=True, error_msg='Invalid clang path. %s cannot be found. Note that TensorFlow now requires clang to compile. You may override this behavior by setting TF_NEED_CLANG=0')
    write_action_env_to_bazelrc('CLANG_COMPILER_PATH', clang_compiler_path)
    write_to_bazelrc('build --repo_env=CC=%s' % clang_compiler_path)
    write_to_bazelrc('build --repo_env=BAZEL_COMPILER=%s' % clang_compiler_path)
    return clang_compiler_path

Set CLANG_COMPILER_PATH and environment variables.

Loop over user prompts for clang path until receiving a valid response.
Default is used if no input is given. Set CLANG_COMPILER_PATH and write
environment variables CC and BAZEL_COMPILER to .bazelrc.

Args:
environ_cp: (Dict) copy of the os.environ.

Returns:
string value for clang_compiler_path.


def url(self, url, owner=None, **kwargs):
    return URL(self.tcex, url, owner=owner, **kwargs)

Create the URL TI object.

Args:
owner:
url:
**kwargs:

Return:


def flush(self, force: bool=False) -> bool | Any:
    if force or len(self.queue) > self.max_size:
        result = self.flusher(self.queue)
        self.queue.clear()
        return result or True
    else:
        return False

Empty and consume queue items, if force or max_size reached

Args:
* force: If True, force queue to flush

Returns:
* True, if flushed with no errors
* False, if not flushed
* Error value from consumer, if flushed with errors