def _filter_pb(field_or_unary):
    if isinstance(field_or_unary, query_pb2.StructuredQuery.FieldFilter):
        return query_pb2.StructuredQuery.Filter(field_filter=field_or_unary)
    elif isinstance(field_or_unary, query_pb2.StructuredQuery.UnaryFilter):
        return query_pb2.StructuredQuery.Filter(unary_filter=field_or_unary)
    else:
        raise ValueError('Unexpected filter type', type(field_or_unary), field_or_unary)

Convert a specific protobuf filter to the generic filter type.

Args:
field_or_unary (Union[google.cloud.proto.firestore.v1beta1.\
query_pb2.StructuredQuery.FieldFilter, google.cloud.proto.\
firestore.v1beta1.query_pb2.StructuredQuery.FieldFilter]): A
field or unary filter to convert to a generic filter.

Returns:
google.cloud.firestore_v1beta1.types.\
StructuredQuery.Filter: A "generic" filter.

Raises:
ValueError: If ``field_or_unary`` is not a field or unary filter.


def pivot_samples(self, values, index='ID_REF'):
    data = []
    for gsm in self.gsms.values():
        tmp_data = gsm.table.copy()
        tmp_data['name'] = gsm.name
        data.append(tmp_data)
    ndf = concat(data).pivot(index=index, values=values, columns='name')
    return ndf

Pivot samples by specified column.

Construct a table in which columns (names) are the samples, index
is a specified column eg. ID_REF and values in the columns are of one
specified type.

Args:
values (:obj:`str`): Column name present in all GSMs.
index (:obj:`str`, optional): Column name that will become an index in
pivoted table. Defaults to "ID_REF".

Returns:
:obj:`pandas.DataFrame`: Pivoted data


def listtransactions(self, user_id="", count=10, start_at=0):

        txlist = self.rpc.call("listtransactions", user_id, count, start_at)
        self.logger.debug("Got transaction list for " + str(user_id))
        return txlist

List all transactions associated with this account.

Args:
user_id (str): this user's unique identifier
count (int): number of transactions to return (default=10)
start_at (int): start the list at this transaction (default=0)

Returns:
list [dict]: transactions associated with this user's account


def split_metrics_by_namespace_and_name(metrics, namespace, name):
    matching_metrics = []
    not_matching_metrics = []
    for dist in metrics:
        if dist.key.metric.namespace == namespace and dist.key.metric.name == name:
            matching_metrics.append(dist)
        else:
            not_matching_metrics.append(dist)
    return (matching_metrics, not_matching_metrics)

Splits metrics list namespace and name.

Args:
metrics: list of metrics from pipeline result
namespace(str): filter metrics by namespace
name(str): filter metrics by name

Returns:
two lists - one of metrics which are matching filters
and second of not matching


def get_id(date=None, project: str = 'sip',
               instance_id: int = None) -> str:

        if date is None:
            date = datetime.datetime.utcnow()

        if isinstance(date, datetime.datetime):
            date = date.strftime('%Y%m%d')

        if instance_id is None:
            instance_id = randint(0, 9999)

        return 'SBI-{}-{}-{:04d}'.format(date, project, instance_id)

Get a SBI Identifier.

Args:
date (str or datetime.datetime, optional): UTC date of the SBI
project (str, optional ): Project Name
instance_id (int, optional): SBI instance identifier

Returns:
str, Scheduling Block Instance (SBI) ID.


def field_content_length(msg: message.Message, field: Union[descriptor.FieldDescriptor, str]) -> int:
    if isinstance(field, str):
        field = _field_descriptor_for_name(msg, field)
    if field_is_repeated(field):
        return len(getattr(msg, field.name))
    return 1 if msg.HasField(field.name) else 0

Returns the size of the field.

Args:
msg: The Message whose fields to examine.
field: The FieldDescriptor or name of the field to examine.

Returns:
The number of elements at the provided field. If field describes a singular
protobuf field, this will return 1. If the field is not set, returns 0.


def __write_error(self, status_code, error_message=None):
    if (error_message is None):
        error_message = httplib.responses[status_code]
    status = ('%d %s' % (status_code, httplib.responses[status_code]))
    message = EndpointsErrorMessage(state=EndpointsErrorMessage.State.APPLICATION_ERROR, error_message=error_message)
    return (status, self.__PROTOJSON.encode_message(message))

Return the HTTP status line and body for a given error code and message.

Args:
status_code: HTTP status code to be returned.
error_message: Error message to be returned.

Returns:
Tuple (http_status, body):
http_status: HTTP status line, e.g. 200 OK.
body: Body of the HTTP request.


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

    print('Converting transposed convolution ...')

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

    bias_name = '{0}.bias'.format(w_name)
    weights_name = '{0}.weight'.format(w_name)

    if len(weights[weights_name].numpy().shape) == 4:
        W = weights[weights_name].numpy().transpose(2, 3, 1, 0)
        height, width, n_filters, channels = W.shape

        n_groups = params['group']
        if n_groups > 1:
            raise AssertionError('Cannot convert conv1d with groups != 1')

        if params['dilations'][0] > 1:
            raise AssertionError('Cannot convert conv1d with dilation_rate != 1')

        if bias_name in weights:
            biases = weights[bias_name].numpy()
            has_bias = True
        else:
            biases = None
            has_bias = False

        input_name = inputs[0]

        if has_bias:
            weights = [W, biases]
        else:
            weights = [W]

        conv = keras.layers.Conv2DTranspose(
            filters=n_filters,
            kernel_size=(height, width),
            strides=(params['strides'][0], params['strides'][1]),
            padding='valid',
            output_padding=0,
            weights=weights,
            use_bias=has_bias,
            activation=None,
            dilation_rate=params['dilations'][0],
            bias_initializer='zeros', kernel_initializer='zeros',
            name=tf_name
        )

        layers[scope_name] = conv(layers[input_name])

        layers[scope_name].set_shape(layers[scope_name]._keras_shape)

        pads = params['pads']
        if pads[0] > 0:
            assert(len(pads) == 2 or (pads[2] == pads[0] and pads[3] == pads[1]))

            crop = keras.layers.Cropping2D(
                pads[:2],
                name=tf_name + '_crop'
            )
            layers[scope_name] = crop(layers[scope_name])
    else:
        raise AssertionError('Layer is not supported for now')

Convert transposed convolution layer.

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


def format_float(digit=0, is_pct=False):
    if is_pct:
        space = (' ' if (digit < 0) else '')
        fmt = f'{{:{space}.{abs(int(digit))}%}}'
        return (lambda vv: ('NaN' if np.isnan(vv) else fmt.format(vv)))
    else:
        return (lambda vv: ('NaN' if np.isnan(vv) else (f'{{:,.{digit}f}}'.format(vv) if vv else ('-' + (' ' * abs(digit))))))

Number display format for pandas

Args:
digit: number of digits to keep
if negative, add one space in front of positive pct
is_pct: % display

Returns:
lambda function to format floats

Examples:
>>> format_float(0)(1e5)
'100,000'
>>> format_float(1)(1e5)
'100,000.0'
>>> format_float(-1, True)(.2)
' 20.0%'
>>> format_float(-1, True)(-.2)
'-20.0%'
>>> pd.options.display.float_format = format_float(2)


def shutdown(self, vm_names=None, reboot=False):

        self.virt_env.shutdown(vm_names, reboot)

Shutdown this prefix

Args:
vm_names(list of str): List of the vms to shutdown
reboot(bool): If true, reboot the requested vms

Returns:
None


def _match_value_against_type(self, value: cfg.Binding, other_type: abstract.BaseValue, subst: _SubstType, view: _ViewType) -> _SubstType | None:
    left = value.data
    left = abstract_utils.unwrap_final(left)
    other_type = abstract_utils.unwrap_final(other_type)
    is_recursive = abstract_utils.is_recursive_annotation(other_type)
    if is_recursive:
        key = (left, other_type)
        if key in self._recursive_annots_cache:
            return subst if self._recursive_annots_cache[key] else None
        self._recursive_annots_cache[key] = True
    subst = self._match_nonfinal_value_against_type(left, value, other_type, subst, view)
    if is_recursive:
        self._recursive_annots_cache[key] = subst is not None
    return subst

One-way unify value into pytd type given a substitution.

Args:
value: A cfg.Binding.
other_type: A BaseValue instance.
subst: The current substitution. This dictionary is not modified.
view: A mapping of Variable to Value.

Returns:
A new (or unmodified original) substitution dict if the matching
succeeded, None otherwise.


def In(self, *values):

    self._awql = self._CreateMultipleValuesCondition(values, 'IN')
    return self._query_builder

Sets the type of the WHERE clause as "in".

Args:
*values: The values to be used in the WHERE condition.

Returns:
The query builder that this WHERE builder links to.


def get_field(self, key: str) -> Optional[Field]:
    if key in self._fields:
        return self._fields[key]
    if self._allow_nonconst_keys:
        for key_spec, field in self._fields.items():
            if key_spec.match(key):
                return field
    return None

Get field definition (Field) for a key.

Args:
key: string as input key.

Returns:
Matched field. A field is considered a match when:
* Its key spec is a ConstStrKey that equals to the input key.
* Or it's the first field whose key spec is a NonConstKey
which matches the input key.


def forward_loss(self, pixel_values, pred, mask, interpolate_pos_encoding: bool=False):
    target = self.patchify(pixel_values, interpolate_pos_encoding=interpolate_pos_encoding)
    if self.config.norm_pix_loss:
        mean = target.mean(dim=-1, keepdim=True)
        var = target.var(dim=-1, keepdim=True)
        target = (target - mean) / (var + 1e-06) ** 0.5
    loss = (pred - target) ** 2
    loss = loss.mean(dim=-1)
    loss = (loss * mask).sum() / mask.sum()
    return loss

Args:
pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
Pixel values.
pred (`torch.FloatTensor` of shape `(batch_size, num_patches, patch_size**2 * num_channels)`:
Predicted pixel values.
mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`):
Tensor indicating which patches are masked (1) and which are not (0).
interpolate_pos_encoding (`bool`, *optional*, default `False`):
interpolation flag passed during the forward pass.

Returns:
`torch.FloatTensor`: Pixel reconstruction loss.


def monitor(self, job, event_monitor, result_monitor):
    logging.info('starting to monitor the job')
    last_active_ms = -1
    perf = None
    cancel_job = False
    waiting_for_shutdown = False
    while True:
        now = int(time.time() * 1000)
        logging.debug('now is %d', now)
        curr_perf = NexmarkLauncher.get_performance(job, event_monitor, result_monitor)
        if perf is None or curr_perf.has_progress(perf):
            last_active_ms = now
        if self.streaming and (not waiting_for_shutdown):
            quiet_duration = (now - last_active_ms)
            if curr_perf.event_count >= self.args.num_events and curr_perf.result_count >= 0 and (quiet_duration > self.DONE_DELAY):
                logging.info('streaming query appears to have finished executing')
                waiting_for_shutdown = True
                cancel_job = True
            elif quiet_duration > self.TERMINATE_DELAY:
                logging.error('streaming query have been stuck for %d seconds', quiet_duration)
                logging.error('canceling streaming job')
                waiting_for_shutdown = True
                cancel_job = True
            elif quiet_duration > self.WARNING_DELAY:
                logging.warning('streaming query have been stuck for %d seconds', quiet_duration)
            if cancel_job:
                job.cancel()
        perf = curr_perf
        stopped = PipelineState.is_terminal(job.state)
        if stopped:
            break
        if not waiting_for_shutdown:
            if last_active_ms == now:
                logging.info('activity seen, new performance data extracted')
            else:
                logging.info('no activity seen')
        else:
            logging.info('waiting for shutdown')
        time.sleep(self.PERF_DELAY)
    return perf

keep monitoring the performance and progress of running job and cancel
the job if the job is stuck or seems to have finished running

Returns:
the final performance if it is measured


def _set_shape(self, shape):
    shape = tensor_shape.as_shape(shape)
    if shape.rank is None:
        return
    shape = shape.as_list()
    if shape[0] is not None:
        self._row_partition._row_splits.set_shape(shape[0] + 1)
    dtype = self._row_partition.dtype
    for i, partition in enumerate(self._nested_row_partitions):
        size = shape[i + 1]
        if size is not None:
            if partition._uniform_row_length is not None:
                old_row_length = tensor_util.constant_value(partition._uniform_row_length)
                if old_row_length is not None:
                    if size == old_row_length:
                        continue
                    else:
                        raise ValueError(f'Inconsistent size for axis {i + 1}: {old_row_length} vs. {size}.')
            partition._uniform_row_length = ops.convert_to_tensor(size, dtype)
            if partition._nrows is None:
                partition._nrows = array_ops.size(partition._row_splits, out_type=dtype) - 1
    if hasattr(self.flat_values, 'set_shape'):
        flat_shape = tensor_shape.as_shape([None] + shape[self.ragged_rank + 1:])
        self.flat_values.set_shape(flat_shape)

Updates the static shape of `self` to be `shape`.

* If a dimension of `shape` has known rank, and is encoded via
partitioning, then this will update the corresponding partition to
define `_uniform_row_length` and `nrows`.
* If a dimension of `shape` has a known rank, and is encoded as one
of the `flat_values` dimensions, then `flat_values.set_shape()` will
be used to update its shape.

Warning: Using this method to assert an incorrect shape for a RaggedTensor
(i.e., one that's not consistent with its actual shape) can cause
segmentation faults and very difficult-to-diagnose behavior.  Only use this
method if you are certain that the shape is correct.

Args:
shape: `tf.TensorShape` specifying the shape for this `RaggedTensor`.


def key_periods(ciphertext, max_key_period):

    if max_key_period <= 0:
        raise ValueError("max_key_period must be a positive integer")

    key_scores = []
    for period in range(1, min(max_key_period, len(ciphertext)) + 1):
        score = abs(ENGLISH_IC - index_of_coincidence(*split_columns(ciphertext, period)))
        key_scores.append((period, score))

    return [p[0] for p in sorted(key_scores, key=lambda x: x[1])]

Rank all key periods for ``ciphertext`` up to and including ``max_key_period``

Example:
>>> key_periods(ciphertext, 30)
[2, 4, 8, 3, ...]

Args:
ciphertext (str): The text to analyze
max_key_period (int): The maximum period the key could be

Returns:
Sorted list of keys

Raises:
ValueError: If max_key_period is less than or equal to 0


def to_matrix( xx, yy, zz, xy, yz, xz ):

    matrix = np.array( [[xx, xy, xz], [xy, yy, yz], [xz, yz, zz]] )
    return matrix

Convert a list of matrix components to a symmetric 3x3 matrix.
Inputs should be in the order xx, yy, zz, xy, yz, xz.

Args:
xx (float): xx component of the matrix.
yy (float): yy component of the matrix.
zz (float): zz component of the matrix.
xy (float): xy component of the matrix.
yz (float): yz component of the matrix.
xz (float): xz component of the matrix.

Returns:
(np.array): The matrix, as a 3x3 numpy array.


def _find_hstreaming():
    global WARNED_HADOOP_HOME, HADOOP_STREAMING_PATH_CACHE
    if HADOOP_STREAMING_PATH_CACHE:
        return HADOOP_STREAMING_PATH_CACHE
    try:
        search_root = os.environ['HADOOP_HOME']
    except KeyError:
        search_root = '/'
    cmd = ('find %s -name hadoop*streaming*.jar' % search_root)
    p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    HADOOP_STREAMING_PATH_CACHE = p.communicate()[0].split('\n')[0]
    if ((search_root == '/') and (not WARNED_HADOOP_HOME)):
        WARNED_HADOOP_HOME = True
        hadoop_home = HADOOP_STREAMING_PATH_CACHE[:HADOOP_STREAMING_PATH_CACHE.rfind('/contrib/')]
        logging.warn(('Set the HADOOP_HOME environmental variable to your hadoop path to improve performance. Put the following [export HADOOP_HOME="%s"] in ~/.bashrc' % hadoop_home))
    return HADOOP_STREAMING_PATH_CACHE

Finds the whole path to the hadoop streaming jar.

If the environmental var HADOOP_HOME is specified, then start the search
from there.

Returns:
Full path to the hadoop streaming jar if found, else return an empty
string.


def import_subview(self, idx, subview):
    subview.corpus = self
    self._subviews[idx] = subview

Add the given subview to the corpus.

Args:
idx (str): An idx that is unique in the corpus for identifying the subview.
If already a subview exists with the given id it will be overridden.
subview (Subview): The subview to add.


def batch_shape_tensor(self, name='batch_shape_tensor'):
    with self._name_scope(name):
        if self.batch_shape.is_fully_defined():
            return ops.convert_to_tensor(self.batch_shape.as_list(), dtype=dtypes.int32, name='batch_shape')
        return self._batch_shape_tensor()

Shape of a single sample from a single event index as a 1-D `Tensor`.

The batch dimensions are indexes into independent, non-identical
parameterizations of this distribution.

Args:
name: name to give to the op

Returns:
batch_shape: `Tensor`.


def add_derivatives(self, path, **kwargs):
    paths = listify(path)
    deriv_dirs = []

    def check_for_description(dir):
        dd = os.path.join(dir, 'dataset_description.json')
        return os.path.exists(dd)
    for p in paths:
        p = os.path.abspath(p)
        if os.path.exists(p):
            if check_for_description(p):
                deriv_dirs.append(p)
            else:
                subdirs = [d for d in os.listdir(p) if os.path.isdir(os.path.join(p, d))]
                for sd in subdirs:
                    sd = os.path.join(p, sd)
                    if check_for_description(sd):
                        deriv_dirs.append(sd)
    if (not deriv_dirs):
        warnings.warn("Derivative indexing was enabled, but no valid derivatives datasets were found in any of the provided or default locations. Please make sure all derivatives datasets you intend to index contain a 'dataset_description.json' file, as described in the BIDS-derivatives specification.")
    for deriv in deriv_dirs:
        dd = os.path.join(deriv, 'dataset_description.json')
        with open(dd, 'r', encoding='utf-8') as ddfd:
            description = json.load(ddfd)
        pipeline_name = description.get('PipelineDescription', {}).get('Name')
        if (pipeline_name is None):
            raise ValueError('Every valid BIDS-derivatives dataset must have a PipelineDescription.Name field set inside dataset_description.json.')
        if (pipeline_name in self.derivatives):
            raise ValueError("Pipeline name '%s' has already been added to this BIDSLayout. Every added pipeline must have a unique name!")
        kwargs['config'] = (kwargs.get('config') or ['bids', 'derivatives'])
        kwargs['sources'] = (kwargs.get('sources') or self)
        self.derivatives[pipeline_name] = BIDSLayout(deriv, **kwargs)
    for deriv in self.derivatives.values():
        self.entities.update(deriv.entities)

Add BIDS-Derivatives datasets to tracking.

Args:
path (str, list): One or more paths to BIDS-Derivatives datasets.
Each path can point to either a derivatives/ directory
containing one more more pipeline directories, or to a single
pipeline directory (e.g., derivatives/fmriprep).
kwargs (dict): Optional keyword arguments to pass on to
BIDSLayout() when initializing each of the derivative datasets.

Note: Every derivatives directory intended for indexing MUST contain a
valid dataset_description.json file. See the BIDS-Derivatives
specification for details.


def connect(self,
                fedora_url,
                data=None,
                method='Get'):

        if data is None:
            data = {}
        if not fedora_url.startswith("http"):
            fedora_url = urllib.parse.urljoin(self.base_url, fedora_url)
        request = urllib.request.Request(fedora_url,
                                         method=method)
        request.add_header('Accept', 'text/turtle')
        request.add_header('Content-Type', 'text/turtle')
        if len(data) > 0:
            request.data = data
        try:
            response = urllib.request.urlopen(request)
        except urllib.error.URLError as err:
            if hasattr(err, 'reason'):
                print("failed to reach server at {} with {} method".format(
                    fedora_url,
                    request.method))
                print("Reason: ", err.reason)
                print("Data: ", data)
            elif hasattr(err, 'code'):
                print("Server error {}".format(err.code))
            raise err
        return response

Method attempts to connect to REST servers of the Fedora
Commons repository using optional data parameter.

Args:
fedora_url(string): Fedora URL
data(dict): Data to through to REST endpoint
method(str): REST Method, defaults to GET

Returns:
result(string): Response string from Fedora


def _inchi_labels(mol):

        obconv = ob.OBConversion()
        obconv.SetOutFormat(str("inchi"))
        obconv.AddOption(str("a"), ob.OBConversion.OUTOPTIONS)
        obconv.AddOption(str("X"), ob.OBConversion.OUTOPTIONS, str("DoNotAddH"))
        inchi_text = obconv.WriteString(mol)
        match = re.search(r"InChI=(?P<inchi>.+)\nAuxInfo=.+"
                          r"/N:(?P<labels>[0-9,;]+)/(E:(?P<eq_atoms>[0-9,"
                          r";\(\)]*)/)?", inchi_text)
        inchi = match.group("inchi")
        label_text = match.group("labels")
        eq_atom_text = match.group("eq_atoms")
        heavy_atom_labels = tuple([int(i) for i in label_text.replace(
            ';', ',').split(',')])
        eq_atoms = []
        if eq_atom_text is not None:
            eq_tokens = re.findall(r'\(((?:[0-9]+,)+[0-9]+)\)', eq_atom_text
                                   .replace(';', ','))
            eq_atoms = tuple([tuple([int(i) for i in t.split(',')])
                              for t in eq_tokens])
        return heavy_atom_labels, eq_atoms, inchi

Get the inchi canonical labels of the heavy atoms in the molecule

Args:
mol: The molecule. OpenBabel OBMol object

Returns:
The label mappings. List of tuple of canonical label,
original label
List of equivalent atoms.


def preprocessing_fn(inputs):
    result = {'clicked': inputs['clicked']}
    for name in _INTEGER_COLUMN_NAMES:
        feature = inputs[name]
        feature = tft.sparse_tensor_to_dense_with_shape(feature, [None, 1], default_value=-1)
        feature = tf.squeeze(feature, axis=1)
        result[name] = feature
        result[name + '_bucketized'] = tft.bucketize(feature, _NUM_BUCKETS)
    for name in _CATEGORICAL_COLUMN_NAMES:
        feature = inputs[name]
        feature = tft.sparse_tensor_to_dense_with_shape(feature, [None, 1], default_value='')
        feature = tf.squeeze(feature, axis=1)
        result[get_transformed_categorical_column_name(name)] = tft.compute_and_apply_vocabulary(feature, frequency_threshold=frequency_threshold)
    return result

User defined preprocessing function for criteo columns.

Args:
inputs: dictionary of input `tensorflow_transform.Column`.
Returns:
A dictionary of `tensorflow_transform.Column` representing the transformed
columns.


def _update_graph_variables(self, learning_rate: float = None, momentum: float = None):

        if learning_rate is not None:
            K.set_value(self.get_learning_rate_variable(), learning_rate)

        if momentum is not None:
            K.set_value(self.get_momentum_variable(), momentum)

Update graph variables setting giving `learning_rate` and `momentum`

Args:
learning_rate: learning rate value to be set in graph (set if not None)
momentum: momentum value to be set in graph (set if not None)

Returns:
None


def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):

        super(DerivationParameters, self).read(
            input_stream,
            kmip_version=kmip_version
        )
        local_stream = BytearrayStream(input_stream.read(self.length))

        if self.is_tag_next(
                enums.Tags.CRYPTOGRAPHIC_PARAMETERS,
                local_stream
        ):
            self._cryptographic_parameters = CryptographicParameters()
            self._cryptographic_parameters.read(
                local_stream,
                kmip_version=kmip_version
            )

        if self.is_tag_next(enums.Tags.INITIALIZATION_VECTOR, local_stream):
            self._initialization_vector = ByteString(
                tag=enums.Tags.INITIALIZATION_VECTOR
            )
            self._initialization_vector.read(
                local_stream,
                kmip_version=kmip_version
            )

        if self.is_tag_next(enums.Tags.DERIVATION_DATA, local_stream):
            self._derivation_data = ByteString(tag=enums.Tags.DERIVATION_DATA)
            self._derivation_data.read(local_stream, kmip_version=kmip_version)

        if self.is_tag_next(enums.Tags.SALT, local_stream):
            self._salt = ByteString(tag=enums.Tags.SALT)
            self._salt.read(local_stream, kmip_version=kmip_version)

        if self.is_tag_next(Tags.ITERATION_COUNT, local_stream):
            self._iteration_count = Integer(tag=Tags.ITERATION_COUNT)
            self._iteration_count.read(local_stream, kmip_version=kmip_version)

        self.is_oversized(local_stream)

Read the data encoding the DerivationParameters struct and decode it
into its constituent parts.

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


def save_data(X, y, path):
    catalog = {'.csv': save_csv, '.sps': save_libsvm, '.h5': save_hdf5}
    ext = os.path.splitext(path)[1]
    func = catalog[ext]
    if (y is None):
        y = np.zeros((X.shape[0],))
    func(X, y, path)

Save data as a CSV, LibSVM or HDF5 file based on the file extension.

Args:
X (numpy or scipy sparse matrix): Data matrix
y (numpy array): Target vector. If None, all zero vector will be saved.
path (str): Path to the CSV, LibSVM or HDF5 file to save data.


def _CreateImage(media_service, opener, url):
    image_data = opener.open(url).read().decode('utf-8')
    image = {'type': 'IMAGE', 'data': image_data, 'xsi_type': 'Image'}
    return media_service.upload(image)[0]

Creates an image and uploads it to the server.

Args:
media_service: a SudsServiceProxy instance for AdWords's MediaService.
opener: an OpenerDirector instance.
url: a str URL used to load image data.

Returns:
The image that was successfully uploaded.


def convert(self, vroot, entry_variables):
    for converter in self.converters:
        vroot = converter.convert(vroot, entry_variables)
    return vroot

Convert a given graph.

Convert a given graph using the `converters` in the order of the registeration, i.e., sequentially.

Args:
vroot (:obj:`Variable`): NNabla Variable
entry_variables (:obj:`Variable`): Entry variable from which the conversion starts.


def BatchConvert(self, metadata_value_pairs, token=None):

    msg_dict = {}
    for metadata, msg in metadata_value_pairs:
      msg_dict.setdefault(msg.source, []).append((metadata, msg))

    metadata_objects = []
    metadata_to_fetch = []

    for client_urn in msg_dict:
      try:
        metadata_objects.append(self.cached_metadata[client_urn])
      except KeyError:
        metadata_to_fetch.append(client_urn)

    if metadata_to_fetch:
      if data_store.RelationalDBEnabled():
        client_ids = set(urn.Basename() for urn in metadata_to_fetch)
        infos = data_store.REL_DB.MultiReadClientFullInfo(client_ids)

        fetched_metadata = [
            GetMetadata(client_id, info) for client_id, info in infos.items()
        ]
      else:
        client_fds = aff4.FACTORY.MultiOpen(
            metadata_to_fetch, mode="r", token=token)

        fetched_metadata = [
            GetMetadataLegacy(client_fd, token=token)
            for client_fd in client_fds
        ]

      for metadata in fetched_metadata:
        self.cached_metadata[metadata.client_urn] = metadata
      metadata_objects.extend(fetched_metadata)

    data_by_type = {}
    for metadata in metadata_objects:
      try:
        for original_metadata, message in msg_dict[metadata.client_urn]:

          new_metadata = ExportedMetadata(metadata)
          new_metadata.source_urn = original_metadata.source_urn
          new_metadata.annotations = original_metadata.annotations
          new_metadata.original_timestamp = message.payload.age
          cls_name = message.payload.__class__.__name__

          if cls_name not in data_by_type:
            converters_classes = ExportConverter.GetConvertersByValue(
                message.payload)
            data_by_type[cls_name] = {
                "converters": [cls(self.options) for cls in converters_classes],
                "batch_data": [(new_metadata, message.payload)]
            }
          else:
            data_by_type[cls_name]["batch_data"].append(
                (new_metadata, message.payload))

      except KeyError:
        pass

    converted_batch = []
    for dataset in itervalues(data_by_type):
      for converter in dataset["converters"]:
        converted_batch.extend(
            converter.BatchConvert(dataset["batch_data"], token=token))

    return converted_batch

Converts a batch of GrrMessages into a set of RDFValues at once.

Args:
metadata_value_pairs: a list or a generator of tuples (metadata, value),
where metadata is ExportedMetadata to be used for conversion and value
is a GrrMessage to be converted.
token: Security token.

Returns:
Resulting RDFValues. Empty list is a valid result and means that
conversion wasn't possible.


def business_days_in_period(self, date_tensor, period_tensor):
    return self.business_days_between(date_tensor, date_tensor + period_tensor)

Calculates number of business days in a period.

Includes the dates in `date_tensor`, but excludes final dates resulting from
addition of `period_tensor`.

Args:
date_tensor: DateTensor of starting dates.
period_tensor: PeriodTensor, should be broadcastable to `date_tensor`.

Returns:
An int32 Tensor with the number of business days in given periods that
start at given dates.


def check_num_tasks(chain, task_count):

    errors = []

    min_decision_tasks = 1
    if task_count['decision'] < min_decision_tasks:
        errors.append("{} decision tasks; we must have at least {}!".format(
            task_count['decision'], min_decision_tasks
        ))
    raise_on_errors(errors)

Make sure there are a specific number of specific task types.

Currently we only check decision tasks.

Args:
chain (ChainOfTrust): the chain we're operating on
task_count (dict): mapping task type to the number of links.

Raises:
CoTError: on failure.


def _rank(x):
    rank = ops.convert_to_tensor(x).get_shape().ndims
    if rank:
        return (rank, True)
    else:
        return (array_ops.rank(x), False)

Helper function to retrieve the rank of a tensor.

Args:
x: Something convertible to `Tensor`.

Returns:
Either a pair `(rank, True)` where `rank` is an integer or a pair
`(rank, False)` where `rank` is an integer `Tensor`. In either case,
`rank` is the rank of `x`.


def get_nodes_lines(self, **kwargs):

        params = {'Nodes': util.ints_to_string(kwargs.get('nodes', []))}

        result = self.make_request('bus', 'get_nodes_lines', **params)

        if not util.check_result(result):
            return False, result.get('resultDescription', 'UNKNOWN ERROR')

        values = util.response_list(result, 'resultValues')
        return True, [emtype.NodeLinesItem(**a) for a in values]

Obtain stop IDs, coordinates and line information.

Args:
nodes (list[int] | int): nodes to query, may be empty to get
all nodes.

Returns:
Status boolean and parsed response (list[NodeLinesItem]), or message
string in case of error.


def _CanProcessKeyWithPlugin(self, registry_key, plugin):

    for registry_key_filter in plugin.FILTERS:

      if getattr(registry_key_filter, 'key_paths', []):
        continue

      if registry_key_filter.Match(registry_key):
        return True

    return False

Determines if a plugin can process a Windows Registry key or its values.

Args:
registry_key (dfwinreg.WinRegistryKey): Windows Registry key.
plugin (WindowsRegistryPlugin): Windows Registry plugin.

Returns:
bool: True if the Registry key can be processed with the plugin.


def fetch_credential(self, credential=None, profile=None):

        q = self.db.get(self.query.profile == profile)
        if q is not None:
            return q.get(credential)

Fetch credential from credentials file.

Args:
credential (str): Credential to fetch.
profile (str): Credentials profile. Defaults to ``'default'``.

Returns:
str, None: Fetched credential or ``None``.


def fulfill_transaction(transaction, *, private_keys):
    if (not isinstance(private_keys, (list, tuple))):
        private_keys = [private_keys]
    if isinstance(private_keys, tuple):
        private_keys = list(private_keys)
    transaction_obj = Transaction.from_dict(transaction)
    try:
        signed_transaction = transaction_obj.sign(private_keys)
    except KeypairMismatchException as exc:
        raise MissingPrivateKeyError('A private key is missing!') from exc
    return signed_transaction.to_dict()

Fulfills the given transaction.

Args:
transaction (dict): The transaction to be fulfilled.
private_keys (:obj:`str` | :obj:`list` | :obj:`tuple`): One or
more private keys to be used for fulfilling the
transaction.

Returns:
dict: The fulfilled transaction payload, ready to be sent to a
BigchainDB federation.

Raises:
:exc:`~.exceptions.MissingPrivateKeyError`: If a private
key is missing.


def __init__(self, app):

        super(SendgridEmailAdapter, self).__init__(app)

        sendgrid_api_key = app.config.get('SENDGRID_API_KEY')
        if not sendgrid_api_key:
            raise ConfigError(
                "The SENDGRID_API_KEY setting is missing. Set SENDGRID_API_KEY in your app config.")

        try:
            from sendgrid import SendGridAPIClient
            self.sg = SendGridAPIClient(apikey=sendgrid_api_key)
        except ImportError:
            raise ConfigError(SENDGRID_IMPORT_ERROR_MESSAGE)

Check config settings and setup SendGrid Web API v3.

Args:
app(Flask): The Flask application instance.


def doc2id(self, doc):
    doc = map(self.process_token, doc)
    return [self.token_to_id(token) for token in doc]

Get the list of token_id given doc.

Args:
doc (list): document.

Returns:
list: int id of doc.


def call(self, func, key, timeout=None):
    result = self.get(key)
    if (result == NONE_RESULT):
        return None
    if (result is None):
        result = func()
        self.set(key, (result if (result is not None) else NONE_RESULT), timeout)
    return result

Wraps a function call with cache.

Args:
func (function): the function to call.
key (str): the cache key for this call.
timeout (int): the cache timeout for the key (the
unit of this parameter depends on
the cache class you use, for example,
if you use the classes from werkzeug,
then timeout is in seconds.)

Returns:
The return value of calling func


def compute_f(match_num, test_num, gold_num):

    if test_num == 0 or gold_num == 0:
        return 0.00, 0.00, 0.00
    precision = float(match_num) / float(test_num)
    recall = float(match_num) / float(gold_num)
    if (precision + recall) != 0:
        f_score = 2 * precision * recall / (precision + recall)
        if veryVerbose:
            print("F-score:", f_score, file=DEBUG_LOG)
        return precision, recall, f_score
    else:
        if veryVerbose:
            print("F-score:", "0.0", file=DEBUG_LOG)
        return precision, recall, 0.00

Compute the f-score based on the matching triple number,
triple number of AMR set 1,
triple number of AMR set 2
Args:
match_num: matching triple number
test_num:  triple number of AMR 1 (test file)
gold_num:  triple number of AMR 2 (gold file)
Returns:
precision: match_num/test_num
recall: match_num/gold_num
f_score: 2*precision*recall/(precision+recall)


def assert_split_at_fraction_fails(source, num_items_to_read_before_split, split_fraction):
    assert_split_at_fraction_behavior(source, num_items_to_read_before_split, split_fraction, ExpectedSplitOutcome.MUST_FAIL)

Asserts that dynamic work rebalancing at a given fraction fails.

Asserts that trying to perform dynamic splitting after reading
'num_items_to_read_before_split' items from the source fails.

Args:
source: source to perform dynamic splitting on.
num_items_to_read_before_split: number of items to read before splitting.
split_fraction: fraction to split at.


def NewFromJSON(data):
    return Comment(body=data.get('body', None), posted_at=data.get('posted_at', None), user=User.NewFromJSON(data.get('user', None)))

Create a new Comment instance from a JSON dict.

Args:
data (dict): JSON dictionary representing a Comment.

Returns:
A Comment instance.


def make_grid_texture(num_h_lines=10, num_v_lines=10, resolution=50):
    (x_h, y_h) = make_lines_texture(num_h_lines, resolution)
    (y_v, x_v) = make_lines_texture(num_v_lines, resolution)
    return (np.concatenate([x_h, x_v]), np.concatenate([y_h, y_v]))

Makes a texture consisting of a grid of vertical and horizontal lines.

Args:
num_h_lines (int): the number of horizontal lines to draw
num_v_lines (int): the number of vertical lines to draw
resolution (int): the number of midpoints to draw on each line

Returns:
A texture.


def format_cert_name(env='', account='', region='', certificate=None):
    cert_name = None
    if certificate:
        if certificate.startswith('arn'):
            LOG.info('Full ARN provided...skipping lookup.')
            cert_name = certificate
        else:
            generated_cert_name = generate_custom_cert_name(env, region, account, certificate)
            if generated_cert_name:
                LOG.info('Found generated certificate %s from template', generated_cert_name)
                cert_name = generated_cert_name
            else:
                LOG.info('Using default certificate name logic')
                cert_name = 'arn:aws:iam::{account}:server-certificate/{name}'.format(account=account, name=certificate)
    LOG.debug('Certificate name: %s', cert_name)
    return cert_name

Format the SSL certificate name into ARN for ELB.

Args:
env (str): Account environment name
account (str): Account number for ARN
region (str): AWS Region.
certificate (str): Name of SSL certificate

Returns:
str: Fully qualified ARN for SSL certificate
None: Certificate is not desired


def run_display_app_errors(self, err):

        if err is not None and err:
            for e_ in err.decode('utf-8').split('\n'):
                print('{}{}{}'.format(c.Style.BRIGHT, c.Fore.RED, e_))
                self.log.error('[tcrun] App error: {}'.format(e_))

Handle the exit code for the current run.

Args:
err (str): One or more lines of errors messages.


def convertTime(self, time):
    m_format = ''
    if time.minute:
        m_format = ':%M'
    timeString = time.strftime((('%I' + m_format) + ' %p'))
    if (not int(timeString[0])):
        timeString = timeString[1:]
    return timeString

Convert a datetime object representing a time into a human-ready
string that can be read, spoken aloud, etc.

Args:
time (datetime.date): A datetime object to be converted into text.

Returns:
A string representation of the input time, ignoring any day-related
information.


def _decorate_ast_reference_data_types(node: _ast.AbstractSyntaxTree) -> None:
    if isinstance(node, _ast.Identifier) and node.value == 'reference':
        node.data_type = unittest.mock.Mock(spec=_fhir_path_data_types.StructureDataType, element_type='Reference')
    for child in node.children or ():
        _decorate_ast_reference_data_types(child)

Adds data types for reference nodes.

Sets the data_type for any identifier node named 'reference' to that of a
Reference type.

Args:
node: The root node of the AST to modify.


def _use_cache(self, key, options=None):
    flag = ContextOptions.use_cache(options)
    if (flag is None):
        flag = self._cache_policy(key)
    if (flag is None):
        flag = ContextOptions.use_cache(self._conn.config)
    if (flag is None):
        flag = True
    return flag

Return whether to use the context cache for this key.

Args:
key: Key instance.
options: ContextOptions instance, or None.

Returns:
True if the key should be cached, False otherwise.


def less(x1, x2):
    if any_symbolic_tensors((x1, x2)):
        return Less().symbolic_call(x1, x2)
    return backend.numpy.less(x1, x2)

Return the truth value of `x1 < x2` element-wise.

Args:
x1: First input tensor.
x2: Second input tensor.

Returns:
Output tensor, element-wise comparison of `x1` and `x2`.


def gather(values, index, name='segmented_gather'):
    return tf.gather(values, index.indices, batch_dims=index.batch_dims, name=name)

Gathers from `values` using the index map. For each element in the domain of the index map this operation looks up
a value for that index in `values`. Two elements from the same segment always get assigned the same value.

Args:
values: [B1, ..., Bn, num_segments, V1, ...] Tensor with segment values.
index: [B1, ..., Bn, I1, ..., Ik] IndexMap.
name: Name for the TensorFlow operation.

Returns:
[B1, ..., Bn, I1, ..., Ik, V1, ...] Tensor with the gathered values.


def keep_file(self, task, response, min_size=None, max_size=None):
    try:
        img = Image.open(BytesIO(response.content))
    except (IOError, OSError):
        return False
    task['img_size'] = img.size
    if (min_size and (not self._size_gt(img.size, min_size))):
        return False
    if (max_size and (not self._size_lt(img.size, max_size))):
        return False
    return True

Decide whether to keep the image

Compare image size with ``min_size`` and ``max_size`` to decide.

Args:
response (Response): response of requests.
min_size (tuple or None): minimum size of required images.
max_size (tuple or None): maximum size of required images.
Returns:
bool: whether to keep the image.


def MapByteStream(
      self, byte_stream, byte_offset=0, context=None, **unused_kwargs):

    context_state = getattr(context, 'state', {})

    size_hints = context_state.get('size_hints', {})

    elements_data_size = self._CalculateElementsDataSize(context)
    if elements_data_size is not None:
      self._CheckByteStreamSize(byte_stream, byte_offset, elements_data_size)

    elif not self._HasElementsTerminator():
      raise errors.MappingError(
          'Unable to determine elements data size and missing elements '
          'terminator')

    else:
      byte_stream_size = len(byte_stream)

      element_byte_size = self._element_data_type_definition.GetByteSize()
      elements_data_offset = byte_offset
      next_elements_data_offset = elements_data_offset + element_byte_size

      elements_terminator = self._data_type_definition.elements_terminator
      element_value = byte_stream[
          elements_data_offset:next_elements_data_offset]

      while byte_stream[elements_data_offset:]:
        elements_data_offset = next_elements_data_offset
        if element_value == elements_terminator:
          elements_data_size = elements_data_offset - byte_offset
          break

        next_elements_data_offset += element_byte_size
        element_value = byte_stream[
            elements_data_offset:next_elements_data_offset]

      if element_value != elements_terminator:
        size_hints[self._data_type_definition.name] = DataTypeMapSizeHint(
            byte_stream_size - byte_offset)

        context_state['size_hints'] = size_hints

        error_string = (
            'Unable to read: {0:s} from byte stream at offset: {1:d} '
            'with error: unable to find elements terminator').format(
                self._data_type_definition.name, byte_offset)
        raise errors.ByteStreamTooSmallError(error_string)

    if context:
      context.byte_size = elements_data_size

      size_hints[self._data_type_definition.name] = DataTypeMapSizeHint(
          elements_data_size, is_complete=True)

      context_state['size_hints'] = size_hints

    return byte_stream[byte_offset:byte_offset + elements_data_size]

Maps the data type on a byte stream.

Args:
byte_stream (bytes): byte stream.
byte_offset (Optional[int]): offset into the byte stream where to start.
context (Optional[DataTypeMapContext]): data type map context.

Returns:
tuple[object, ...]: mapped values.

Raises:
MappingError: if the data type definition cannot be mapped on
the byte stream.


def _fqdn(o, oset=True, recheck=False, pmodule=None):

    if id(o) in _set_failures or o is None:
        return None

    if recheck or not _safe_hasattr(o, "__fqdn__"):
        import inspect
        if not hasattr(o, "__name__"):
            msg.warn("Skipped object {}: no __name__ attribute.".format(o), 3)
            return

        result = None
        if hasattr(o, "__acornext__") and o.__acornext__ is not None:
            otarget = o.__acornext__
        else:
            otarget = o

        omod = _safe_getmodule(otarget) or pmodule
        if (omod is None and hasattr(otarget, "__objclass__") and
            otarget.__objclass__ is not None):
            omod = _safe_getmodule(otarget.__objclass__)
            parts = ("<unknown>" if omod is None else omod.__name__,
                     otarget.__objclass__.__name__,
                     otarget.__name__)

            result = "{}.{}.{}".format(*parts)
        elif (omod is None and hasattr(otarget, "__class__") and
              otarget.__class__ is not None):
            omod = _safe_getmodule(otarget.__class__)
            parts = ("<unknown>" if omod is None else omod.__name__,
                     otarget.__class__.__name__,
                     otarget.__name__)

            result = "{}.{}.{}".format(*parts)
        elif omod is not otarget:
            parts = (_fqdn(omod, False), otarget.__name__)

            result = "{}.{}".format(*parts)
        else:
            result = otarget.__name__

        if oset:
            _safe_setattr(o, "__fqdn__", result)
        return result

    if _safe_hasattr(o, "__fqdn__"):
        return o.__fqdn__

Returns the fully qualified name of the object.

Args:
o (type): instance of the object's type.
oset (bool): when True, the fqdn will also be set on the object as attribute
`__fqdn__`.
recheck (bool): for sub-classes, sometimes the super class has already had
its __fqdn__ attribute set; in that case, we want to recheck the
object's name. This usually only gets used during object extension.


def __init__(self, locations=None, separation_char=os.sep):

        super(LocationDescriptor, self).__init__()

        self._separation_char = separation_char

        if isinstance(locations, list):
            self._locations_list = list(locations)
        elif isinstance(locations, str) or isinstance(locations, unicode):
            self._locations_list = locations.split(self._separation_char)
        elif locations is None:
            self._locations_list = list()
        else:
            raise TypeError("Argument in constructor not recognized.")

Constructor.

Args:
locations: Can be either a string with sub-strings joined by the separation character or a list of strings,
each giving a location.
separation_char: Separation character in the location string.

Raises:
TypeError: if argument is not recognized as either a string, a list of strings or ``None``.

Notes:
Empty :class:`LocationDescriptor`s **are** allowed and empty locations are also allowed.


def failure_message(description, options):
    message = 'expected to find {}'.format(description)
    if (options['count'] is not None):
        message += ' {count} {times}'.format(count=options['count'], times=declension('time', 'times', options['count']))
    elif (options['between'] is not None):
        between = options['between']
        if between:
            (first, last) = (between[0], between[(- 1)])
        else:
            (first, last) = (None, None)
        message += ' between {first} and {last} times'.format(first=first, last=last)
    elif (options['maximum'] is not None):
        message += ' at most {maximum} {times}'.format(maximum=options['maximum'], times=declension('time', 'times', options['maximum']))
    elif (options['minimum'] is not None):
        message += ' at least {minimum} {times}'.format(minimum=options['minimum'], times=declension('time', 'times', options['minimum']))
    return message

Returns a expectation failure message for the given query description.

Args:
description (str): A description of the failed query.
options (Dict[str, Any]): The query options.

Returns:
str: A message describing the failure.


def find_response_component(self, api_id=None, signature_id=None):

        if not api_id and not signature_id:
            raise ValueError('At least one of api_id and signature_id is required')

        components = list()

        if self.response_data:
            for component in self.response_data:
                if (api_id and component['api_id']) == api_id or (signature_id and component['signature_id'] == signature_id):
                    components.append(component)

        return components

Find one or many repsonse components.

Args:

api_id (str):           Api id associated with the component(s) to be retrieved.

signature_id (str):     Signature id associated with the component(s) to be retrieved.

Returns:
A list of dictionaries containing component data


class ZoeDepthFeatureFusionLayer(nn.Module):

    def __init__(self, config, align_corners=True):
        super().__init__()
        self.align_corners = align_corners
        self.projection = nn.Conv2d(config.fusion_hidden_size, config.fusion_hidden_size, kernel_size=1, bias=True)
        self.residual_layer1 = ZoeDepthPreActResidualLayer(config)
        self.residual_layer2 = ZoeDepthPreActResidualLayer(config)

    def forward(self, hidden_state, residual=None):
        if residual is not None:
            if hidden_state.shape != residual.shape:
                residual = nn.functional.interpolate(residual, size=(hidden_state.shape[2], hidden_state.shape[3]), mode='bilinear', align_corners=False)
            hidden_state = hidden_state + self.residual_layer1(residual)
        hidden_state = self.residual_layer2(hidden_state)
        hidden_state = nn.functional.interpolate(hidden_state, scale_factor=2, mode='bilinear', align_corners=self.align_corners)
        hidden_state = self.projection(hidden_state)
        return hidden_state

Feature fusion layer, merges feature maps from different stages.

Args:
config (`[ZoeDepthConfig]`):
Model configuration class defining the model architecture.
align_corners (`bool`, *optional*, defaults to `True`):
The align_corner setting for bilinear upsample.


def call_backend(self, orig_request, start_response):

    method_config, params = self.lookup_rest_method(orig_request)
    if not method_config:
      cors_handler = self._create_cors_handler(orig_request)
      return util.send_wsgi_not_found_response(start_response,
                                               cors_handler=cors_handler)

    transformed_request = self.transform_request(
        orig_request, params, method_config)

    discovery = discovery_service.DiscoveryService(
        self.config_manager, self._backend)
    discovery_response = discovery.handle_discovery_request(
        transformed_request.path, transformed_request, start_response)
    if discovery_response:
      return discovery_response

    url = transformed_request.base_path + transformed_request.path
    transformed_request.headers['Content-Type'] = 'application/json'
    transformed_environ = self.prepare_backend_environ(
        orig_request.server, 'POST', url, transformed_request.headers.items(),
        transformed_request.body, transformed_request.source_ip,
        orig_request.port)

    with util.StartResponseProxy() as start_response_proxy:
      body_iter = self._backend(transformed_environ, start_response_proxy.Proxy)
      status = start_response_proxy.response_status
      headers = start_response_proxy.response_headers

      body = start_response_proxy.response_body

      if not body:
        body = ''.join(body_iter)

    return self.handle_backend_response(orig_request, transformed_request,
                                        status, headers, body, method_config,
                                        start_response)

Generate API call (from earlier-saved request).

This calls start_response and returns the response body.

Args:
orig_request: An ApiRequest, the original request from the user.
start_response: A function with semantics defined in PEP-333.

Returns:
A string containing the response body.


def wrap_with_monitor(env, video_dir):

  env = ExtendToEvenDimentions(env)
  env = RenderObservations(env)
  env = gym.wrappers.Monitor(env, video_dir, force=True,
                             video_callable=lambda idx: True,
                             write_upon_reset=True)
  return env

Wrap environment with gym.Monitor.

Video recording provided by Monitor requires
1) both height and width of observation to be even numbers.
2) rendering of environment

Args:
env: environment.
video_dir: video directory.

Returns:
wrapped environment.


def getQueryEngineDescription(self, queryEngine, **kwargs):

        response = self.getQueryEngineDescriptionResponse(queryEngine, **kwargs)
        return self._read_dataone_type_response(response, 'QueryEngineDescription')

See Also: getQueryEngineDescriptionResponse()

Args:
queryEngine:
**kwargs:

Returns:


def run_suite(test_classes, argv=None):

    parser = argparse.ArgumentParser(description='Mobly Suite Executable.')
    parser.add_argument(
        '-c',
        '--config',
        nargs=1,
        type=str,
        required=True,
        metavar='<PATH>',
        help='Path to the test configuration file.')
    parser.add_argument(
        '--tests',
        '--test_case',
        nargs='+',
        type=str,
        metavar='[ClassA[.test_a] ClassB[.test_b] ...]',
        help='A list of test classes and optional tests to execute.')
    if not argv:
        argv = sys.argv[1:]
    args = parser.parse_args(argv)

    test_configs = config_parser.load_test_config_file(args.config[0])

    for test_class in test_classes:
        if not issubclass(test_class, base_test.BaseTestClass):
            logging.error('Test class %s does not extend '
                          'mobly.base_test.BaseTestClass', test_class)
            sys.exit(1)

    selected_tests = compute_selected_tests(test_classes, args.tests)

    ok = True
    for config in test_configs:
        runner = test_runner.TestRunner(config.log_path, config.test_bed_name)
        for (test_class, tests) in selected_tests.items():
            runner.add_test_class(config, test_class, tests)
        try:
            runner.run()
            ok = runner.results.is_all_pass and ok
        except signals.TestAbortAll:
            pass
        except:
            logging.exception('Exception when executing %s.',
                              config.test_bed_name)
            ok = False
    if not ok:
        sys.exit(1)

Executes multiple test classes as a suite.

This is the default entry point for running a test suite script file
directly.

Args:
test_classes: List of python classes containing Mobly tests.
argv: A list that is then parsed as cli args. If None, defaults to cli
input.


def update(self, value):
    with tf.name_scope((self._name + '/update')):
        if (value.shape.ndims == self._mean.shape.ndims):
            value = value[(None, ...)]
        count = tf.shape(value)[0]
        with tf.control_dependencies([self._count.assign_add(count)]):
            step = tf.cast(self._count, tf.float32)
            mean_delta = tf.reduce_sum((value - self._mean[(None, ...)]), 0)
            new_mean = (self._mean + (mean_delta / step))
            new_mean = tf.cond((self._count > 1), (lambda : new_mean), (lambda : value[0]))
            var_delta = ((value - self._mean[(None, ...)]) * (value - new_mean[(None, ...)]))
            new_var_sum = (self._var_sum + tf.reduce_sum(var_delta, 0))
        with tf.control_dependencies([new_mean, new_var_sum]):
            update = (self._mean.assign(new_mean), self._var_sum.assign(new_var_sum))
        with tf.control_dependencies(update):
            if (value.shape.ndims == 1):
                value = tf.reduce_mean(value)
            return self._summary('value', tf.reduce_mean(value))

Update the mean and variance estimates.

Args:
value: Batch or single value tensor.

Returns:
Summary tensor.


def process(self, element):

    import apache_beam as beam
    import six
    import tensorflow as tf

    tf.logging.set_verbosity(tf.logging.ERROR)
    try:
      clean_element = []
      for line in element:
        clean_element.append(line.rstrip())

      batch_result = self._session.run(
          fetches=self._transformed_features,
          feed_dict={self._input_placeholder_tensor: clean_element})

      for i in range(len(clean_element)):
        transformed_features = {}
        for name, value in six.iteritems(batch_result):
          if isinstance(value, tf.SparseTensorValue):
            batch_i_indices = value.indices[:, 0] == i
            batch_i_values = value.values[batch_i_indices]
            transformed_features[name] = batch_i_values.tolist()
          else:
            transformed_features[name] = value[i].tolist()

        yield transformed_features

    except Exception as e:
      yield beam.pvalue.TaggedOutput('errors', (str(e), element))

Run the transformation graph on batched input data

Args:
element: list of csv strings, representing one batch input to the TF graph.

Returns:
dict containing the transformed data. Results are un-batched. Sparse
tensors are converted to lists.


def GetDecompressor(cls, compression_method):
    compression_method = compression_method.lower()
    decompressor = cls._decompressors.get(compression_method, None)
    if (not decompressor):
        return None
    return decompressor()

Retrieves the decompressor object for a specific compression method.

Args:
compression_method (str): compression method identifier.

Returns:
Decompressor: decompressor or None if the compression method does
not exists.


def save_data(self, filename):
    with zopen(filename, 'wt') as f:
        json.dump(list(self._data), f, cls=MontyEncoder)

Save the assimilated data to a file.

Args:
filename (str): filename to save the assimilated data to. Note
that if the filename ends with gz or bz2, the relevant gzip
or bz2 compression will be applied.


def SetInputSourceConfiguration(self, configuration):
    mount_path = configuration.mount_path
    if (mount_path and mount_path.endswith(os.sep)):
        mount_path = mount_path[:(- 1)]
    self._mount_path = mount_path

Sets the input source configuration settings.

Args:
configuration (InputSourceConfiguration): input source configuration.


def init_from_adversarial_batches_write_to_datastore(self, submissions,
                                                       adv_batches):

    idx = 0
    for s_id in iterkeys(submissions.defenses):
      for adv_id in iterkeys(adv_batches.data):
        class_batch_id = CLASSIFICATION_BATCH_ID_PATTERN.format(idx)
        idx += 1
        self.data[class_batch_id] = {
            'adversarial_batch_id': adv_id,
            'submission_id': s_id,
            'result_path': os.path.join(
                self._round_name,
                CLASSIFICATION_BATCHES_SUBDIR,
                s_id + '_' + adv_id + '.csv')
        }

    client = self._datastore_client
    with client.no_transact_batch() as batch:
      for key, value in iteritems(self.data):
        entity = client.entity(client.key(KIND_CLASSIFICATION_BATCH, key))
        entity.update(value)
        batch.put(entity)

Populates data from adversarial batches and writes to datastore.

Args:
submissions: instance of CompetitionSubmissions
adv_batches: instance of AversarialBatches


def _zeo_key(self, key, new_type=OOBTree):

        zeo_key = self.zeo.get(key, None)

        if zeo_key is None:
            zeo_key = new_type()
            self.zeo[key] = zeo_key

        return zeo_key

Get key from the :attr:`zeo` database root. If the key doesn't exist,
create it by calling `new_type` argument.

Args:
key (str): Key in the root dict.
new_type (func/obj): Object/function returning the new instance.

Returns:
obj: Stored object, or `new_type`.


def reindex_similar(self, other, n_sphere=4):

    def make_subset_similar(m1, subset1, m2, subset2, index_dct):
        'Changes index_dct INPLACE'
        coords = ['x', 'y', 'z']
        index1 = list(subset1)
        for m1_i in index1:
            dist_m2_to_m1_i = m2.get_distance_to(m1.loc[(m1_i, coords)], subset2, sort=True)
            m2_i = dist_m2_to_m1_i.index[0]
            dist_new = dist_m2_to_m1_i.loc[(m2_i, 'distance')]
            m2_pos_i = dist_m2_to_m1_i.loc[(m2_i, coords)]
            counter = itertools.count()
            found = False
            while (not found):
                if (m2_i in index_dct.keys()):
                    old_m1_pos = m1.loc[(index_dct[m2_i], coords)]
                    if (dist_new < np.linalg.norm((m2_pos_i - old_m1_pos))):
                        index1.append(index_dct[m2_i])
                        index_dct[m2_i] = m1_i
                        found = True
                    else:
                        m2_i = dist_m2_to_m1_i.index[next(counter)]
                        dist_new = dist_m2_to_m1_i.loc[(m2_i, 'distance')]
                        m2_pos_i = dist_m2_to_m1_i.loc[(m2_i, coords)]
                else:
                    index_dct[m2_i] = m1_i
                    found = True
        return index_dct
    molecule1 = self.copy()
    molecule2 = other.copy()
    partition1 = molecule1.partition_chem_env(n_sphere)
    partition2 = molecule2.partition_chem_env(n_sphere)
    index_dct = {}
    for key in partition1:
        message = 'You have chemically different molecules, regarding the topology of their connectivity.'
        assert (len(partition1[key]) == len(partition2[key])), message
        index_dct = make_subset_similar(molecule1, partition1[key], molecule2, partition2[key], index_dct)
    molecule2.index = [index_dct[i] for i in molecule2.index]
    return molecule2.loc[molecule1.index]

Reindex ``other`` to be similarly indexed as ``self``.

Returns a reindexed copy of ``other`` that minimizes the
distance for each atom to itself in the same chemical environemt
from ``self`` to ``other``.
Read more about the definition of the chemical environment in
:func:`Cartesian.partition_chem_env`

.. note:: It is necessary to align ``self`` and other before
applying this method.
This can be done via :meth:`~Cartesian.align`.

.. note:: It is probably necessary to improve the result using
:meth:`~Cartesian.change_numbering()`.

Args:
other (Cartesian):
n_sphere (int): Wrapper around the argument for
:meth:`~Cartesian.partition_chem_env`.

Returns:
Cartesian: Reindexed version of other


def exportGurobiModel(self, gurobiDriver='gurobi', verbose=False):

        from gurobipy import GRB, read
        from tempfile import mkdtemp
        from shutil import rmtree
        from os import path
        import sys
        if (sys.version_info > (3, 0)):
            from io import StringIO
        else:
            from io import BytesIO as StringIO
        tmp_dir = mkdtemp()
        model_file = path.join(tmp_dir, 'model.mps')

        previous = {
            'solver': self.getOption('solver') or '',
            'gurobi_auxfiles': self.getOption('auxfiles') or '',
            'gurobi_options': self.getOption('gurobi_options') or '',
        }
        temporary = {
            'solver': gurobiDriver,
            'gurobi_auxfiles': 'rc',
            'gurobi_options': .format(model_file)
        }

        for option in temporary:
            self.setOption(option, temporary[option])

        output = self.getOutput('solve;')
        if not path.isfile(model_file):
            raise RuntimeError(output)

        for option in previous:
            self.setOption(option, previous[option])

        text_trap = StringIO()
        stdout = sys.stdout
        sys.stdout = text_trap
        model = read(model_file)
        sys.stdout = stdout
        if verbose:
            print(text_trap.getvalue())
        if model_file.endswith('.mps'):
            if not self.getCurrentObjective().minimization():
                model.ModelSense = GRB.MAXIMIZE
                model.setObjective(- model.getObjective())
        model.update()
        rmtree(tmp_dir)
        return model

Export the model to Gurobi as a gurobipy.Model object.

Args:
gurobiDriver: The name or the path of the Gurobi solver driver.
verbose: Whether should generate verbose output.

Returns:
A :class:`gurobipy.Model` object with the model loaded.


def write_config_file(config_instance, appdirs=DEFAULT_APPDIRS, file_name=DEFAULT_CONFIG_FILENAME):
    path = get_config_path(appdirs, file_name)
    with open(path, 'w') as fobj:
        config_instance.write(fobj)
    return config_instance

Write a ConfigParser instance to file at the correct location.

Args:
config_instance: Config instance to safe to file.
appdirs (HamsterAppDirs, optional): ``HamsterAppDirs`` instance storing app/user specific
path information.
file_name (text_type, optional): Name of the config file. Defaults to
``DEFAULT_CONFIG_FILENAME``.

Returns:
SafeConfigParser: Instance written to file.


def url_assembler(query_string, no_redirect=0, no_html=0, skip_disambig=0):
    params = [('q', query_string.encode('utf-8')), ('format', 'json')]
    if no_redirect:
        params.append(('no_redirect', 1))
    if no_html:
        params.append(('no_html', 1))
    if skip_disambig:
        params.append(('skip_disambig', 1))
    return ('/?' + urlencode(params))

Assembler of parameters for building request query.

Args:
query_string: Query to be passed to DuckDuckGo API.
no_redirect: Skip HTTP redirects (for !bang commands). Default - False.
no_html: Remove HTML from text, e.g. bold and italics. Default - False.
skip_disambig: Skip disambiguation (D) Type. Default - False.

Returns:
A “percent-encoded” string which is used as a part of the query.


def submit_files(self, halt_on_error=True):

        if self.halt_on_file_error is not None:
            halt_on_error = self.halt_on_file_error

        upload_status = []
        for xid, content_data in self._files.items():
            del self._files[xid]
            status = True

            if self.debug and xid in self.saved_xids:
                self.tcex.log.debug('skipping previously saved file {}.'.format(xid))
                continue

            content = content_data.get('fileContent')
            if callable(content):
                content = content_data.get('fileContent')(xid)
            if content is None:
                upload_status.append({'uploaded': False, 'xid': xid})
                self.tcex.log.warning('File content was null for xid {}.'.format(xid))
                continue
            if content_data.get('type') == 'Document':
                api_branch = 'documents'
            elif content_data.get('type') == 'Report':
                api_branch = 'reports'

            url = '/v2/groups/{}/{}/upload'.format(api_branch, xid)
            headers = {'Content-Type': 'application/octet-stream'}
            params = {'owner': self._owner}
            r = self.submit_file_content('POST', url, content, headers, params, halt_on_error)
            if r.status_code == 401:

                self.tcex.log.info('Received 401 status code using POST. Trying PUT to update.')
                r = self.submit_file_content('PUT', url, content, headers, params, halt_on_error)
            self.tcex.log.debug('{} Upload URL: {}.'.format(content_data.get('type'), r.url))
            if not r.ok:
                status = False
                self.tcex.handle_error(585, [r.status_code, r.text], halt_on_error)
            elif self.debug:
                self.saved_xids.append(xid)
            self.tcex.log.info('Status {} for file upload with xid {}.'.format(r.status_code, xid))
            upload_status.append({'uploaded': status, 'xid': xid})
        return upload_status

Submit Files for Documents and Reports to ThreatConnect API.

Critical Errors

* There is insufficient document storage allocated to this account.

Args:
halt_on_error (bool, default:True): If True any exception will raise an error.

Returns:
dict: The upload status for each xid.


def _ParseFieldsMetadata(self, structure):
    fields = structure.fields.split(' ')
    log_line_structure = pyparsing.Empty()
    if ((fields[0] == 'date') and (fields[1] == 'time')):
        log_line_structure += self.DATE_TIME.setResultsName('date_time')
        fields = fields[2:]
    for member in fields:
        log_line_structure += self._LOG_LINE_STRUCTURES.get(member, self.URI)
    updated_structures = []
    for line_structure in self._line_structures:
        if (line_structure[0] != 'logline'):
            updated_structures.append(line_structure)
    updated_structures.append(('logline', log_line_structure))
    self._line_structures = updated_structures

Parses the fields metadata and updates the log line definition to match.

Args:
structure (pyparsing.ParseResults): structure parsed from the log file.


def str_to_inet(address):

    try:
        return socket.inet_pton(socket.AF_INET, address)
    except socket.error:
        return socket.inet_pton(socket.AF_INET6, address)

Convert an a string IP address to a inet struct

Args:
address (str): String representation of address
Returns:
inet: Inet network address


def torch_equals_ignore_index(tensor, tensor_other, ignore_index=None):

    if ignore_index is not None:
        assert tensor.size() == tensor_other.size()
        mask_arr = tensor.ne(ignore_index)
        tensor = tensor.masked_select(mask_arr)
        tensor_other = tensor_other.masked_select(mask_arr)

    return torch.equal(tensor, tensor_other)

Compute ``torch.equal`` with the optional mask parameter.

Args:
ignore_index (int, optional): Specifies a ``tensor`` index that is ignored.

Returns:
(bool) Returns ``True`` if target and prediction are equal.


def read(cls, five9, external_id):

        results = cls.search(five9, {cls.__uid_field__: external_id})
        if not results:
            return None
        return results[0]

Return a record singleton for the ID.

Args:
five9 (five9.Five9): The authenticated Five9 remote.
external_id (mixed): The identified on Five9. This should be the
value that is in the ``__uid_field__`` field on the record.

Returns:
BaseModel: The record, if found. Otherwise ``None``


def mach53(msg):
    d = hex2bin(data(msg))
    if (d[23] == '0'):
        return None
    mach = (bin2int(d[24:33]) * 0.008)
    return round(mach, 3)

MACH number, DBS 5,3 message

Args:
msg (String): 28 bytes hexadecimal message

Returns:
float: MACH number


def build_avatar_url(jid):

        digest = md5(str(jid).encode("utf-8")).hexdigest()
        return "http:

Static method to build a gravatar url with the agent's JID

Args:
jid (aioxmpp.JID): an XMPP identifier

Returns:
str: an URL for the gravatar


def shift_and_pad(tensor, shift, axis=0):

  shape = tensor.shape
  rank = len(shape)
  assert 0 <= abs(axis) < rank

  length = int(shape[axis])
  assert 0 <= abs(shift) < length

  paddings = [(0, 0)] * rank
  begin = [0] * rank
  size = [-1] * rank

  if shift > 0:
    paddings[axis] = (shift, 0)
    size[axis] = length - shift
  elif shift < 0:
    paddings[axis] = (0, -shift)
    begin[axis] = -shift

  ret = tf.pad(tf.slice(tensor, begin, size), paddings)

  return ret

Shifts and pads with zero along an axis.

Example:
shift_and_pad([1, 2, 3, 4], 2)  --> [0, 0, 1, 2]
shift_and_pad([1, 2, 3, 4], -2) --> [3, 4, 0, 0]

Args:
tensor: Tensor; to be shifted and padded.
shift: int; number of positions to shift by.
axis: int; along which axis to shift and pad.

Returns:
A Tensor with the same shape as the input tensor.


def _validate_paths(self, settings, name, value):

        return [self._validate_path(settings, name, item)
                for item in value]

Apply ``SettingsPostProcessor._validate_path`` to each element in
list.

Args:
settings (dict): Current settings.
name (str): Setting name.
value (list): List of paths to patch.

Raises:
boussole.exceptions.SettingsInvalidError: Once a path does not
exists.

Returns:
list: Validated paths.


def _unknown_args(self, args):

        for u in args:
            self.tcex.log.warning(u'Unsupported arg found ({}).'.format(u))

Log argparser unknown arguments.

Args:
args (list): List of unknown arguments


def InitPathInfos(self, client_id, path_infos):

    self.ClearPathHistory(client_id, path_infos)
    self.WritePathInfos(client_id, path_infos)

Initializes a collection of path info records for a client.

Unlike `WritePathInfo`, this method clears stat and hash histories of paths
associated with path info records. This method is intended to be used only
in the data migration scripts.

Args:
client_id: A client identifier for which the paths are to be initialized.
path_infos: A list of `rdf_objects.PathInfo` objects to write.


async def find_user(cls, config: Config, user: str) -> Tuple[(str, str)]:
    with open(config.users_file, 'r') as users_file:
        for line in users_file:
            (this_user, user_dir, password) = line.split(':', 2)
            if (user == this_user):
                return (password.rstrip('\r\n'), (user_dir or user))
    raise InvalidAuth()

If the given user ID exists, return its expected password and
mailbox path. Override this method to implement custom login logic.

Args:
config: The maildir config object.
user: The expected user ID.

Raises:
InvalidAuth: The user ID was not valid.


def write_to_fil(self, filename_out, *args, **kwargs):

        t0 = time.time()

        self.__update_header()

        if self.container.isheavy():
            self.__write_to_fil_heavy(filename_out)
        else:
            self.__write_to_fil_light(filename_out)

        t1 = time.time()
        logger.info('Conversion time: %2.2fsec' % (t1- t0))

Write data to .fil file.
It check the file size then decides how to write the file.

Args:
filename_out (str): Name of output file


def AsDict(self, dt=True):

        data = {}
        if self.name:
            data['name'] = self.name
            data['mlkshk_url'] = self.mlkshk_url
        if self.profile_image_url:
            data['profile_image_url'] = self.profile_image_url
        if self.id:
            data['id'] = self.id
        if self.about:
            data['about'] = self.about
        if self.website:
            data['website'] = self.website
        if self.shakes:
            data['shakes'] = [shk.AsDict(dt=dt) for shk in self.shakes]
        data['shake_count'] = self.shake_count
        return data

A dict representation of this User instance.

The return value uses the same key names as the JSON representation.

Args:
dt (bool): If True, return dates as python datetime objects. If
False, return dates as ISO strings.

Return:
A dict representing this User instance


def _use_cache(self, key, options=None):

    flag = ContextOptions.use_cache(options)
    if flag is None:
      flag = self._cache_policy(key)
    if flag is None:
      flag = ContextOptions.use_cache(self._conn.config)
    if flag is None:
      flag = True
    return flag

Return whether to use the context cache for this key.

Args:
key: Key instance.
options: ContextOptions instance, or None.

Returns:
True if the key should be cached, False otherwise.


def run_suite(test_classes, argv=None):
    parser = argparse.ArgumentParser(description='Mobly Suite Executable.')
    parser.add_argument('-c', '--config', nargs=1, type=str, required=True, metavar='<PATH>', help='Path to the test configuration file.')
    parser.add_argument('--tests', '--test_case', nargs='+', type=str, metavar='[ClassA[.test_a] ClassB[.test_b] ...]', help='A list of test classes and optional tests to execute.')
    if (not argv):
        argv = sys.argv[1:]
    args = parser.parse_args(argv)
    test_configs = config_parser.load_test_config_file(args.config[0])
    for test_class in test_classes:
        if (not issubclass(test_class, base_test.BaseTestClass)):
            logging.error('Test class %s does not extend mobly.base_test.BaseTestClass', test_class)
            sys.exit(1)
    selected_tests = compute_selected_tests(test_classes, args.tests)
    ok = True
    for config in test_configs:
        runner = test_runner.TestRunner(config.log_path, config.test_bed_name)
        for (test_class, tests) in selected_tests.items():
            runner.add_test_class(config, test_class, tests)
        try:
            runner.run()
            ok = (runner.results.is_all_pass and ok)
        except signals.TestAbortAll:
            pass
        except:
            logging.exception('Exception when executing %s.', config.test_bed_name)
            ok = False
    if (not ok):
        sys.exit(1)

Executes multiple test classes as a suite.

This is the default entry point for running a test suite script file
directly.

Args:
test_classes: List of python classes containing Mobly tests.
argv: A list that is then parsed as cli args. If None, defaults to cli
input.


def __init__(self, export_dir):
    self._export_dir = export_dir
    self._saved_model = loader.parse_saved_model(export_dir)

Creates an MethodNameUpdater object.

Args:
export_dir: Directory containing the SavedModel files.

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


def assert_cardinality(expected_cardinality):

    def _apply_fn(dataset):
        return _AssertCardinalityDataset(dataset, expected_cardinality)
    return _apply_fn

Asserts the cardinality of the input dataset.

NOTE: The following assumes that "examples.tfrecord" contains 42 records.

>>> dataset = tf.data.TFRecordDataset("examples.tfrecord")
>>> cardinality = tf.data.experimental.cardinality(dataset)
>>> print((cardinality == tf.data.experimental.UNKNOWN_CARDINALITY).numpy())
True
>>> dataset = dataset.apply(tf.data.experimental.assert_cardinality(42))
>>> print(tf.data.experimental.cardinality(dataset).numpy())
42

Args:
expected_cardinality: The expected cardinality of the input dataset.

Returns:
A `Dataset` transformation function, which can be passed to
`tf.data.Dataset.apply`.

Raises:
FailedPreconditionError: The assertion is checked at runtime (when iterating
the dataset) and an error is raised if the actual and expected cardinality
differ.


def __init__(self, app_or_name, registry=None):

        if isinstance(app_or_name, flask.Flask):
            self.app = app_or_name
        else:

            self.app = flask.Flask(app_or_name)

            self.app.wsgi_app = ProxyFix(self.app.wsgi_app)

        self.blueprint = flask.Blueprint(
            "gourde", __name__, template_folder="templates"
        )

        self.host = "0.0.0.0"
        self.port = 8080
        self.debug = False
        self.log_level = None
        self.twisted = False
        self.gunicorn = False
        self.threads = None
        self.metrics = None
        self.is_setup = False

        self.setup_blueprint()
        self.setup_prometheus(registry)
        self.setup_sentry(sentry_dsn=None)

Build a new Gourde.

Args:
Either a flask.Flask or the name of the calling module.


def dbmin05years(self, value=None):

        if value is not None:
            try:
                value = float(value)
            except ValueError:
                raise ValueError('value {} need to be of type float '
                                 'for field `dbmin05years`'.format(value))

        self._dbmin05years = value

Corresponds to IDD Field `dbmin05years`
5-year return period values for minimum extreme dry-bulb temperature

Args:
value (float): value for IDD Field `dbmin05years`
Unit: C
if `value` is None it will not be checked against the
specification and is assumed to be a missing value

Raises:
ValueError: if `value` is not a valid value


def write_hex(fout, buf, offset, width=16):

    skipped_zeroes = 0
    for i, chunk in enumerate(chunk_iter(buf, width)):

        if chunk == (b"\x00" * width):
            skipped_zeroes += 1
            continue
        elif skipped_zeroes != 0:
            fout.write("  -- skipped zeroes: {}\n".format(skipped_zeroes))
            skipped_zeroes = 0

        fout.write("{:016x}  ".format(i * width + offset))

        column = "  ".join([" ".join(["{:02x}".format(c) for c in subchunk])
                            for subchunk in chunk_iter(chunk, 8)])
        w = width * 2 + (width - 1) + ((width
        if len(column) != w:
            column += " " * (w - len(column))
        fout.write(column)

        fout.write("  |")
        for c in chunk:
            if c in PRINTABLE_CHARS:
                fout.write(chr(c))
            else:
                fout.write(".")
        if len(chunk) < width:
            fout.write(" " * (width - len(chunk)))
        fout.write("|")

        fout.write("\n")

Write the content of 'buf' out in a hexdump style

Args:
fout: file object to write to
buf: the buffer to be pretty printed
offset: the starting offset of the buffer
width: how many bytes should be displayed per row


def parse_headers(obj):

    if isinstance(obj, basestring):
        obj = cStringIO.StringIO(obj)
    hdrs = []
    for line in obj:
        hdr = parse_header(line)
        if not hdr:
            break
        if isinstance(hdr, basestring):
            if not hdrs:
                raise ValueError("First header is a continuation")
            hdrs[-1] = (hdrs[-1][0], hdrs[-1][1] + hdr)
            continue
        hdrs.append(hdr)
    return iodict.IODict(hdrs)

Parse a string a iterable object (including file like objects) to a
python dictionary.

Args:
obj: An iterable object including file-like objects.

Returns:
An dictionary of headers. If a header is repeated then the last value
for that header is given.

Raises:
ValueError: If the first line is a continuation line or the headers
cannot be parsed.


def endswith(self, search_str):
    for entry in reversed(list(open(self._jrnl_file, 'r'))[(- 5):]):
        if (search_str in entry):
            return True
    return False

Check whether the provided string exists in Journal file.

Only checks the last 5 lines of the journal file. This method is
usually used when tracking a journal from an active Revit session.

Args:
search_str (str): string to search for

Returns:
bool: if True the search string is found


def ParseFileObject(self, parser_mediator, file_object):
    file_offset = file_object.get_offset()
    file_size = file_object.get_size()
    while (file_offset < file_size):
        try:
            self._ParseRecord(parser_mediator, file_object)
        except errors.ParseError as exception:
            if (file_offset == 0):
                raise errors.UnableToParseFile('Unable to parse first event record with error: {0!s}'.format(exception))
        file_offset = file_object.get_offset()

Parses a BSM file-like object.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
file_object (dfvfs.FileIO): a file-like object.

Raises:
UnableToParseFile: when the file cannot be parsed.


def calculate_weights(correlation_matrix, min_wt):
    np.fill_diagonal(correlation_matrix.values, np.nan)
    correlation_matrix = correlation_matrix.clip(lower=0)
    raw_weights = correlation_matrix.mean(axis=1)
    raw_weights = raw_weights.clip(lower=min_wt)
    weights = (raw_weights / sum(raw_weights))
    return (raw_weights.round(rounding_precision), weights.round(rounding_precision))

Calculate a weight for each profile based on its correlation to other
replicates. Negative correlations are clipped to 0, and weights are clipped
to be min_wt at the least.

Args:
correlation_matrix (pandas df): Correlations between all replicates
min_wt (float): Minimum raw weight when calculating weighted average

Returns:
raw weights (pandas series):  Mean correlation to other replicates
weights (pandas series): raw_weights normalized such that they add to 1


def wrap_or_unwrap(value):
    if isinstance(value, NoDependency):
        return value.value
    if isinstance(value, base.Trackable):
        return value
    elif type(value) == dict:
        return _DictWrapper(value)
    elif type(value) == collections.OrderedDict:
        return _DictWrapper(value)
    elif type(value) == list:
        return ListWrapper(value)
    elif isinstance(value, tuple) and _should_wrap_tuple(value):
        return _TupleWrapper(value)
    else:
        return value

Wraps input value into trackable data structures.

This is mostly useful for containers like list, dict, etc, which could contain
trackable objects in it. Wrapped data structure will be tracked when
associated with a `tf.Module`, so that save model/checkpoint can properly
track the dependency.

It will also unwrap NoDependency objects.

Args:
value: the input object to be wrapped.

Returns:
Wrapped trackable data structure.


def load(self, filepath, file_encoding=None):
    with open(filepath, encoding=file_encoding) as inf:
        for line in inf:
            current_line = str(line).strip()
            if current_line.startswith('@prefix'):
                self._add_ttl_ns(current_line.replace('\n', ''))
            elif (len(current_line) > 10):
                break
    self.__make_dicts__

Reads the the beginning of a turtle file and sets the prefix's used
in that file and sets the prefix attribute

Args:
filepath: the path to the turtle file
file_encoding: specify a specific encoding if necessary


def load(path):

        with open(path, "r") as fobj:
            analytics = Analytics(info=json.load(fobj))
        os.unlink(path)
        return analytics

Loads analytics report from json file specified by path.

Args:
path (str): path to json file with analytics report.


def set_metadata(self, key: str, value: Any, per_trial: bool=True) -> None:

Sets metadata for current trial or current sampling.

Metadata can be used in two use cases:

* Worker processes that co-work on the same trial can use meta-data to
communicate with each other.
* Worker use metadata as a persistent store to save information for
current trial, which can be retrieved via `poll_result` method later.

Args:
key: A string as key to metadata.
value: A value that can be serialized by `pg.to_json_str`.
per_trial: If True, the key is set per current trial. Otherwise, it
is set per current sampling loop.


def _TerminateProcess(self, process):
    pid = process.pid
    logger.warning('Terminating process: (PID: {0:d}).'.format(pid))
    process.terminate()
    process.join(timeout=self._PROCESS_JOIN_TIMEOUT)
    if process.is_alive():
        logger.warning('Killing process: (PID: {0:d}).'.format(pid))
        self._KillProcess(pid)

Terminate a process.

Args:
process (MultiProcessBaseProcess): process to terminate.


def update_state(world):
    world_size = len(world)

    def wrap(index):
        'Wrap an index around the other end of the array'
        return (index % world_size)
    for x in range(world_size):
        for y in range(world_size):
            if (not world[x][y].allow_change.get()):
                continue
            live_neighbor_count = sum([world[wrap(x)][wrap((y + 1))].value, world[wrap((x + 1))][wrap((y + 1))].value, world[wrap((x + 1))][wrap(y)].value, world[wrap((x + 1))][wrap((y - 1))].value, world[wrap(x)][wrap((y - 1))].value, world[wrap((x - 1))][wrap((y - 1))].value, world[wrap((x - 1))][wrap(y)].value, world[wrap((x - 1))][wrap((y + 1))].value])
            if world[x][y].value:
                if (not ((live_neighbor_count == 2) or (live_neighbor_count == 3))):
                    world[x][y].value = False
            elif (live_neighbor_count == 3):
                world[x][y].value = True

Increment the world state, determining which cells live, die, or appear.

Args:
world (list[list]): A square matrix of cells

Returns: None


def delete(self, record_id):

        record_url = self.record_url(record_id)
        return self._delete(record_url)

Deletes a record by its id

>>> record = airtable.match('Employee Id', 'DD13332454')
>>> airtable.delete(record['id'])

Args:
record_id(``str``): Airtable record id

Returns:
record (``dict``): Deleted Record


def _resolve_subkeys(key, separator='.'):

    subkey = None
    if separator in key:
        index = key.index(separator)
        subkey = key[index + 1:]
        key = key[:index]
    return key, subkey

Given a key which may actually be a nested key, return the top level
key and any nested subkeys as separate values.

Args:
key (str): A string that may or may not contain the separator.
separator (str): The namespace separator. Defaults to `.`.

Returns:
Tuple[str, str]: The key and subkey(s).


def add_evaluation_step(result_tensor, ground_truth_tensor):
    with tf.name_scope('accuracy'):
        with tf.name_scope('correct_prediction'):
            prediction = tf.argmax(result_tensor, 1)
            correct_prediction = tf.equal(prediction, ground_truth_tensor)
        with tf.name_scope('accuracy'):
            evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar('accuracy', evaluation_step)
    return (evaluation_step, prediction)

Inserts the operations we need to evaluate the accuracy of our results.

Args:
result_tensor: The new final node that produces results.
ground_truth_tensor: The node we feed ground truth data
into.

Returns:
Tuple of (evaluation step, prediction).


def AddWeight(self, path_segment_index, weight):

    if path_segment_index not in self._weight_per_index:
      raise ValueError('Path segment index not set.')

    self._weight_per_index[path_segment_index] += weight

    if weight not in self._indexes_per_weight:
      self._indexes_per_weight[weight] = []

    self._indexes_per_weight[weight].append(path_segment_index)

Adds a weight for a specific path segment index.

Args:
path_segment_index: an integer containing the path segment index.
weight: an integer containing the weight.

Raises:
ValueError: if the path segment weights do not contain
the path segment index.


def softmax_cross_entropy_one_hot(logits, labels, weights_fn=None):
    with tf.variable_scope('softmax_cross_entropy_one_hot', values=[logits, labels]):
        del weights_fn
        cross_entropy = tf.losses.softmax_cross_entropy(onehot_labels=labels, logits=logits)
        return (cross_entropy, tf.constant(1.0))

Calculate softmax cross entropy given one-hot labels and logits.

Args:
logits: Tensor of size [batch-size, o=1, p=1, num-classes]
labels: Tensor of size [batch-size, o=1, p=1, num-classes]
weights_fn: Function that takes in labels and weighs examples (unused)
Returns:
cross-entropy (scalar), weights


def var(
        self, axis=None, skipna=None, level=None, ddof=1, numeric_only=None, **kwargs
    ):

        axis = self._get_axis_number(axis) if axis is not None else 0
        if numeric_only is not None and not numeric_only:
            self._validate_dtypes(numeric_only=True)
        return self._reduce_dimension(
            self._query_compiler.var(
                axis=axis,
                skipna=skipna,
                level=level,
                ddof=ddof,
                numeric_only=numeric_only,
                **kwargs
            )
        )

Computes variance across the DataFrame.

Args:
axis (int): The axis to take the variance on.
skipna (bool): True to skip NA values, false otherwise.
ddof (int): degrees of freedom

Returns:
The variance of the DataFrame.


def first_seen(self, first_seen):
    if (not self.can_update()):
        self._tcex.handle_error(910, [self.type])
    first_seen = self._utils.format_datetime(first_seen, date_format='%Y-%m-%dT%H:%M:%SZ')
    self._data['firstSeen'] = first_seen
    request = {'firstSeen': first_seen}
    return self.tc_requests.update(self.api_type, self.api_sub_type, self.unique_id, request)

Updates the campaign with the new first_seen date.

Args:
first_seen: The first_seen date. Converted to %Y-%m-%dT%H:%M:%SZ date format

Returns:


def fstat(self, file_des):
    file_object = self.filesystem.get_open_file(file_des).get_object()
    return file_object.stat_result.copy()

Return the os.stat-like tuple for the FakeFile object of file_des.

Args:
file_des: The file descriptor of filesystem object to retrieve.

Returns:
The FakeStatResult object corresponding to entry_path.

Raises:
OSError: if the filesystem object doesn't exist.


def sparsemax(x, axis=-1):
    if any_symbolic_tensors((x,)):
        return Sparsemax(axis).symbolic_call(x)
    return backend.nn.sparsemax(x, axis=axis)

Sparsemax activation function.

For each batch `i`, and class `j`,
sparsemax activation function is defined as:

`sparsemax(x)[i, j] = max(x[i, j] - τ(x[i, :]), 0).`

Args:
x: Input tensor.
axis: `int`, axis along which the sparsemax operation is applied.

Returns:
A tensor, output of sparsemax transformation. Has the same type and
shape as `x`.

Example:

>>> x = np.array([-1., 0., 1.])
>>> x_sparsemax = keras.ops.sparsemax(x)
>>> print(x_sparsemax)
array([0., 0., 1.], shape=(3,), dtype=float64)


def post_op(self, id: str, path_data: Union[(dict, None)], post_data: Any) -> dict:
    path = self._get_path_for_op_id(id)
    return self.post_path(path, path_data, post_data)

Modifies the ESI by looking up an operation id.

Args:
path: raw ESI URL path
path_data: data to format the path with (can be None)
post_data: data to send to ESI

Returns:
ESI data


def rotate(p, rad, o=(0, 0)):

    v = vector(o, p)
    fx = lambda x, y, d: x * cos(d) - y * sin(d)
    fy = lambda x, y, d: x * sin(d) + y * cos(d)
    rv = fx(v[0], v[1], rad), fy(v[0], v[1], rad)
    return translate(rv, o)

rotate vector
Args:
p: point (x, y)
rad: angle(radian)
o: origin (x, y)


async def process_message(self, message, wait=True):
    to_check = deque([self._waiters])
    ignored = True
    while (len(to_check) > 0):
        context = to_check.popleft()
        waiters = context.get(OperationManager._LEAF, [])
        for waiter in waiters:
            if isinstance(waiter, asyncio.Future):
                waiter.set_result(message)
            else:
                try:
                    (await _wait_or_launch(self._loop, waiter, message, wait))
                except:
                    self._logger.warning('Error calling every_match callback, callback=%s, message=%s', waiter, message, exc_info=True)
            ignored = False
        for key in context:
            if (key is OperationManager._LEAF):
                continue
            message_val = _get_key(message, key)
            if (message_val is _MISSING):
                continue
            next_level = context[key]
            if (message_val in next_level):
                to_check.append(next_level[message_val])
    return (not ignored)

Process a message to see if it wakes any waiters.

This will check waiters registered to see if they match the given
message.  If so, they are awoken and passed the message.  All matching
waiters will be woken.

This method returns False if the message matched no waiters so it was
ignored.

Normally you want to use wait=True (the default behavior) to guarantee
that all callbacks have finished before this method returns.  However,
sometimes that can cause a deadlock if those callbacks would
themselves invoke behavior that requires whatever is waiting for this
method to be alive.  In that case you can pass wait=False to ensure
that the caller of this method does not block.

Args:
message (dict or object): The message that we should process
wait (bool): Whether to block until all callbacks have finished
or to return once the callbacks have been launched.

Returns:
bool: True if at least one waiter matched, otherwise False.


def get_statuses(self, batch_ids):
    with self._lock:
        return {b: self.get_status(b) for b in batch_ids}

Returns a statuses dict for the requested batches.

Args:
batch_ids (list of str): The ids of the batches to get statuses for

Returns:
dict: A dict with keys of batch ids, and values of status enums


def supported_cache_type(types):

    if isinstance(types, str):
        types = [typ.strip() for typ in types.split(",")]
    for typ in types:
        if typ not in ["reflink", "hardlink", "symlink", "copy"]:
            return False
    return True

Checks if link type config option has a valid value.

Args:
types (list/string): type(s) of links that dvc should try out.


def _gen_sentence(self, assetid_body_tuple):

        asset_id, body = assetid_body_tuple
        text = self._process(body)
        sentence = LabeledSentence(text, labels=['DOC_%s' % str(asset_id)])
        return sentence

Takes an assetid_body_tuple and returns a Doc2Vec LabeledSentence

Args:
assetid_body_tuple (tuple): (assetid, bodytext) pair