def _redirect_with_params(url_name, *args, **kwargs):

    url = urlresolvers.reverse(url_name, args=args)
    params = parse.urlencode(kwargs, True)
    return "{0}?{1}".format(url, params)

Helper method to create a redirect response with URL params.

This builds a redirect string that converts kwargs into a
query string.

Args:
url_name: The name of the url to redirect to.
kwargs: the query string param and their values to build.

Returns:
A properly formatted redirect string.


def slice_shape(self, tensor_shape):
    tensor_layout = self.tensor_layout(tensor_shape)
    ret = []
    for (tensor_dim, mesh_axis) in zip(tensor_shape, tensor_layout.tensor_axis_to_mesh_axis):
        if (mesh_axis is None):
            ret.append(tensor_dim.size)
        else:
            mesh_dim = self.shape[mesh_axis]
            if ((tensor_dim.size % mesh_dim.size) != 0):
                raise ValueError(('Tensor dimension size not divisible by mesh dimension size: tensor_shape=%s tensor_layout=%s' % (tensor_shape, tensor_layout)))
            ret.append((tensor_dim.size
    return ret

Shape of each slice of the Tensor.

Args:
tensor_shape: Shape.

Returns:
list of integers with length tensor_shape.ndims.

Raises:
ValueError: If a Tensor dimension is not divisible by the corresponding
Mesh dimension.


def stack_call(self, *args):

        self.pipelined_args.append(args)
        self.number_of_stacked_calls = self.number_of_stacked_calls + 1

Stacks a redis command inside the object.

The syntax is the same than the call() method a Client class.

Args:
*args: full redis command as variable length argument list.

Examples:
>>> pipeline = Pipeline()
>>> pipeline.stack_call("HSET", "key", "field", "value")
>>> pipeline.stack_call("PING")
>>> pipeline.stack_call("INCR", "key2")


def _build_job_meta(cls, job_dir):
    meta_file = os.path.join(job_dir, JOB_META_FILE)
    meta = parse_json(meta_file)
    if (not meta):
        job_name = job_dir.split('/')[(- 1)]
        user = os.environ.get('USER', None)
        meta = {'job_id': job_name, 'job_name': job_name, 'user': user, 'type': 'ray', 'start_time': os.path.getctime(job_dir), 'end_time': None, 'best_trial_id': None}
    if meta.get('start_time', None):
        meta['start_time'] = timestamp2date(meta['start_time'])
    return meta

Build meta file for job.

Args:
job_dir (str): Directory path of the job.

Return:
A dict of job meta info.


def CaffeLMDB(lmdb_path, shuffle=True, keys=None):

    cpb = get_caffe_pb()
    lmdb_data = LMDBData(lmdb_path, shuffle, keys)

    def decoder(k, v):
        try:
            datum = cpb.Datum()
            datum.ParseFromString(v)
            img = np.fromstring(datum.data, dtype=np.uint8)
            img = img.reshape(datum.channels, datum.height, datum.width)
        except Exception:
            log_once("Cannot read key {}".format(k), 'warn')
            return None
        return [img.transpose(1, 2, 0), datum.label]
    logger.warn("Caffe LMDB format doesn't store jpeg-compressed images, \
        it's not recommended due to its inferior performance.")
    return LMDBDataDecoder(lmdb_data, decoder)

Read a Caffe LMDB file where each value contains a ``caffe.Datum`` protobuf.
Produces datapoints of the format: [HWC image, label].

Note that Caffe LMDB format is not efficient: it stores serialized raw
arrays rather than JPEG images.

Args:
lmdb_path, shuffle, keys: same as :class:`LMDBData`.

Returns:
a :class:`LMDBDataDecoder` instance.

Example:
.. code-block:: python

ds = CaffeLMDB("/tmp/validation", keys='{:0>8d}')


def save(self, path=None, complevel=1, complib='zlib'):

        if path is None:
            path = self.hexuid + '.hdf5'
        elif os.path.isdir(path):
            path += os.sep + self.hexuid + '.hdf5'
        elif not (path.endswith('.hdf5') or path.endswith('.hdf')):
            raise ValueError('File path must have a ".hdf5" or ".hdf" extension.')
        with pd.HDFStore(path, 'w', complevel=complevel, complib=complib) as store:
            store['kwargs'] = pd.Series()
            store.get_storer('kwargs').attrs.metadata = self._rel()
            fc = 0
            for name, data in self._data().items():
                if hasattr(data, '_revert_categories'):
                    data._revert_categories()
                name = name[1:] if name.startswith('_') else name
                if isinstance(data, Field):
                    fname = 'FIELD{}_'.format(fc) + name + '/'
                    store[fname + 'data'] = pd.DataFrame(data)
                    for i, field in enumerate(data.field_values):
                        ffname = fname + 'values' + str(i)
                        if isinstance(field, pd.Series):
                            store[ffname] = pd.Series(field)
                        else:
                            store[ffname] = pd.DataFrame(field)
                    fc += 1
                elif isinstance(data, Series):
                    s = pd.Series(data)
                    if isinstance(data.dtype, pd.types.dtypes.CategoricalDtype):
                        s = s.astype('O')
                    store[name] = s
                elif isinstance(data, DataFrame):
                    store[name] = pd.DataFrame(data)
                elif isinstance(data, SparseSeries):
                    s = pd.SparseSeries(data)
                    if isinstance(data.dtype, pd.types.dtypes.CategoricalDtype):
                        s = s.astype('O')
                    store[name] = s
                elif isinstance(data, SparseDataFrame):
                    store[name] = pd.SparseDataFrame(data)
                else:
                    if hasattr(data, 'dtype') and isinstance(data.dtype, pd.types.dtypes.CategoricalDtype):
                        data = data.astype('O')
                    else:
                        for col in data:
                            if isinstance(data[col].dtype, pd.types.dtypes.CategoricalDtype):
                                data[col] = data[col].astype('O')
                    store[name] = data
                if hasattr(data, '_set_categories'):
                    data._set_categories()

Save the container as an HDF5 archive.

Args:
path (str): Path where to save the container


def read_file_to_string(filename, binary_mode=False):
    if binary_mode:
        f = FileIO(filename, mode='rb')
    else:
        f = FileIO(filename, mode='r')
    return f.read()

Reads the entire contents of a file to a string.

Args:
filename: string, path to a file
binary_mode: whether to open the file in binary mode or not. This changes
the type of the object returned.

Returns:
contents of the file as a string or bytes.

Raises:
errors.OpError: Raises variety of errors that are subtypes e.g.
`NotFoundError` etc.


def has_option(self, section, option):
    if (section not in self.sections()):
        return False
    else:
        option = self.optionxform(option)
        return (option in self[section])

Checks for the existence of a given option in a given section.

Args:
section (str): name of section
option (str): name of option

Returns:
bool: whether the option exists in the given section


def _get_read_preference(read_preference):

        read_preference = getattr(pymongo.ReadPreference, read_preference, None)
        if read_preference is None:
            raise ValueError('Invalid read preference: %s' % read_preference)
        return read_preference

Converts read_preference from string to pymongo.ReadPreference value.

Args:
read_preference: string containig the read_preference from the
config file
Returns:
A value from the pymongo.ReadPreference enum

Raises:
Exception: Invalid read preference


def active_qubits(linear, quadratic):

    active = {idx for idx,bias in uniform_iterator(linear)}
    for edge, _ in six.iteritems(quadratic):
        active.update(edge)
    return active

Calculate a set of all active qubits. Qubit is "active" if it has
bias or coupling attached.

Args:
linear (dict[variable, bias]/list[variable, bias]):
Linear terms of the model.

quadratic (dict[(variable, variable), bias]):
Quadratic terms of the model.

Returns:
set:
Active qubits' indices.


def _is_guaranteed_const(tensor):
    if isinstance(tensor, ops.EagerTensor):
        return False

    class Work(object):

        def __init__(self, op: ops.Operation, leaving):
            self.op = op
            self.leaving = leaving
    is_guaranteed_const = lambda op: op.node_def.op == 'GuaranteeConst'
    constants = set([])

    def all_inputs_const(op: ops.Operation):
        return op.inputs and all((inp.op in constants for inp in op.inputs))
    visited = set([])
    stack = [Work(tensor.op, leaving=False)]
    while stack:
        work = stack.pop()
        if work.leaving:
            if all_inputs_const(work.op):
                constants.add(work.op)
            continue
        visited.add(work.op)
        if is_guaranteed_const(work.op):
            constants.add(work.op)
            continue
        stack.append(Work(work.op, leaving=True))
        for inp in work.op.inputs:
            if inp.op not in visited:
                stack.append(Work(inp.op, leaving=False))
    return tensor.op in constants

Determines whether `tensor` is guaranteed to be a constant.

A tensor is guaranteed to be a constant if either it was produced by
a `GuaranteeConst` op or if all of its children are guaranteed to be
constants.

Args:
tensor: The tensor for which to determine const-ness.

Returns:
True if `tensor` is guaranteed to be a constant, False otherwise.


def set_display_name(self, display_name):

        self.displayname = display_name
        return self.api.set_display_name(self.user_id, display_name)

Set this users display name.

Args:
display_name (str): Display Name


def month(self, value=None):

        if value is not None:
            try:
                value = int(value)
            except ValueError:
                raise ValueError('value {} need to be of type int '
                                 'for field `month`'.format(value))
            if value < 1:
                raise ValueError('value need to be greater or equal 1 '
                                 'for field `month`')
            if value > 12:
                raise ValueError('value need to be smaller 12 '
                                 'for field `month`')

        self._month = value

Corresponds to IDD Field `month`

Args:
value (int): value for IDD Field `month`
value >= 1
value <= 12
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 decode_event(abi: Dict, log: Dict):

    if isinstance(log['topics'][0], str):
        log['topics'][0] = decode_hex(log['topics'][0])
    elif isinstance(log['topics'][0], int):
        log['topics'][0] = decode_hex(hex(log['topics'][0]))
    event_id = log['topics'][0]
    events = filter_by_type('event', abi)
    topic_to_event_abi = {
        event_abi_to_log_topic(event_abi): event_abi
        for event_abi in events
    }
    event_abi = topic_to_event_abi[event_id]
    return get_event_data(event_abi, log)

Helper function to unpack event data using a provided ABI

Args:
abi: The ABI of the contract, not the ABI of the event
log: The raw event data

Returns:
The decoded event


def forward(self, spectrogram: torch.FloatTensor) -> torch.FloatTensor:
    if self.config.normalize_before:
        spectrogram = (spectrogram - self.mean) / self.scale
    is_batched = spectrogram.dim() == 3
    if not is_batched:
        spectrogram = spectrogram.unsqueeze(0)
    hidden_states = spectrogram.transpose(2, 1)
    hidden_states = self.conv_pre(hidden_states)
    for i in range(self.num_upsamples):
        hidden_states = nn.functional.leaky_relu(hidden_states, self.config.leaky_relu_slope)
        hidden_states = self.upsampler[i](hidden_states)
        res_state = self.resblocks[i * self.num_kernels](hidden_states)
        for j in range(1, self.num_kernels):
            res_state += self.resblocks[i * self.num_kernels + j](hidden_states)
        hidden_states = res_state / self.num_kernels
    hidden_states = nn.functional.leaky_relu(hidden_states)
    hidden_states = self.conv_post(hidden_states)
    hidden_states = torch.tanh(hidden_states)
    if not is_batched:
        waveform = hidden_states.squeeze(0).transpose(1, 0).view(-1)
    else:
        waveform = hidden_states.squeeze(1)
    return waveform

spectrogram (`torch.FloatTensor`):
Tensor containing the log-mel spectrograms. Can be batched and of shape `(batch_size, sequence_length,
config.model_in_dim)`, or un-batched and of shape `(sequence_length, config.model_in_dim)`.

Returns:
`torch.FloatTensor`: Tensor containing the speech waveform. If the input spectrogram is batched, will be of
shape `(batch_size, num_frames,)`. If un-batched, will be of shape `(num_frames,)`.


def get_min_eig_vec_proxy(self, use_tf_eig=False):

    if use_tf_eig:

      return tf.cond(self.smooth_placeholder < 1E-8,
                     self.tf_min_eig_vec,
                     self.tf_smooth_eig_vec)

    min_eigen_tf = autograph.to_graph(utils.minimum_eigen_vector)

    def _vector_prod_fn(x):
      return self.dual_object.get_psd_product(x)

    estimated_eigen_vector = min_eigen_tf(
        x=self.eig_init_vec_placeholder,
        num_steps=self.eig_num_iter_placeholder,
        learning_rate=self.params['eig_learning_rate'],
        vector_prod_fn=_vector_prod_fn)
    return estimated_eigen_vector

Computes the min eigen value and corresponding vector of matrix M.

Args:
use_tf_eig: Whether to use tf's default full eigen decomposition
Returns:
eig_vec: Minimum absolute eigen value
eig_val: Corresponding eigen vector


def set_integer(self, option, value):

        try:
            int_value = int(value)
        except ValueError as err:
            print(err.args)

        self.options[option] = value

Set an integer option.

Args:
option (str): name of option.
value (int): value of the option.

Raises:
ValueError: Value must be an integer.


def __init__(self, iter_type, item_type=None, min_length=None, max_length=None, empty=True):

        super(IterableTypeChecker, self).__init__(base_type=iter_type)
        self.item_type = item_type
        self.min_length = min_length
        self.max_length = max_length
        self.empty = empty

Initialization method.

Args:
iter_type (type): the type of the iterable object.
item_type (type): the type of the items inside the object.
min_length (int): a minimum length (included).
max_length (int): a maximum length (included).
empty (bool): whether emptiness is allowed.


def log_for_testing(pcoll, *, level: Optional[str]='INFO', prefix: Optional[str]=''):
    log_levels = {'ERROR': logging.error, 'INFO': logging.info, 'DEBUG': logging.debug}
    if level not in log_levels:
        raise ValueError(f'Unknown log level {level} not in {list(log_levels.keys())}')
    logger = log_levels[level]

    def to_loggable_json_recursive(o):
        if isinstance(o, (str, bytes)):
            return str(o)
        elif callable(getattr(o, '_asdict', None)):
            return to_loggable_json_recursive(o._asdict())
        elif isinstance(o, Mapping) and callable(getattr(o, 'items', None)):
            return {str(k): to_loggable_json_recursive(v) for k, v in o.items()}
        elif isinstance(o, Iterable):
            return [to_loggable_json_recursive(x) for x in o]
        else:
            return o

    def log_and_return(x):
        logger(prefix + json.dumps(to_loggable_json_recursive(x)))
        return x
    return pcoll | 'LogForTesting' >> beam.Map(log_and_return)

Logs each element of its input PCollection.

The output of this transform is a copy of its input for ease of use in
chain-style pipelines.

Args:
level: one of ERROR, INFO, or DEBUG, mapped to a corresponding
language-specific logging level
prefix: an optional identifier that will get prepended to the element
being logged


def _ReadFloatingPointDataTypeDefinition(self, definitions_registry, definition_values, definition_name, is_member=False):
    return self._ReadFixedSizeDataTypeDefinition(definitions_registry, definition_values, data_types.FloatingPointDefinition, definition_name, self._SUPPORTED_ATTRIBUTES_FIXED_SIZE_DATA_TYPE, is_member=is_member, supported_size_values=(4, 8))

Reads a floating-point data type definition.

Args:
definitions_registry (DataTypeDefinitionsRegistry): data type definitions
registry.
definition_values (dict[str, object]): definition values.
definition_name (str): name of the definition.
is_member (Optional[bool]): True if the data type definition is a member
data type definition.

Returns:
FloatingPointDefinition: floating-point data type definition.


def fit(self, train_events, test_events, n_epoch=1):
    for e in train_events:
        self.__validate(e)
        self.rec.users[e.user.index]['known_items'].add(e.item.index)
        self.item_buffer.append(e.item.index)
    for e in test_events:
        self.__validate(e)
        self.item_buffer.append(e.item.index)
    self.__batch_update(train_events, test_events, n_epoch)
    for e in test_events:
        self.rec.users[e.user.index]['known_items'].add(e.item.index)
        self.rec.update(e)

Train a model using the first 30% positive events to avoid cold-start.

Evaluation of this batch training is done by using the next 20% positive events.
After the batch SGD training, the models are incrementally updated by using the 20% test events.

Args:
train_events (list of Event): Positive training events (0-30%).
test_events (list of Event): Test events (30-50%).
n_epoch (int): Number of epochs for the batch training.


def start_with(self, request):
    HTTPRequestHelper.patch_with_options(request, self.__options)
    self.queue.add_request(request)
    self.__crawler_start()

Start the crawler using the given request.

Args:
request (:class:`nyawc.http.Request`): The startpoint for the crawler.


def load_videos(template, video_length, frame_shape):

  filenames = tf.gfile.Glob(template)
  if not filenames:
    raise ValueError("no files found.")
  filenames = sorted(filenames)
  dataset_len = len(filenames)
  filenames = tf.constant(filenames)
  dataset = tf.data.Dataset.from_tensor_slices(filenames)
  dataset = dataset.apply(tf.data.experimental.map_and_batch(
      lambda filename: load_image_map_function(filename, frame_shape),
      video_length, drop_remainder=True))
  return dataset, dataset_len

Loads videos from files.

Args:
template: template string for listing the image files.
video_length: length of the video.
frame_shape: shape of each frame.

Returns:
dataset: the tf dataset frame by frame.
dataset_len: number of the items which is the number of image files.

Raises:
ValueError: if no files found.


def schema(cls_or_fn: Callable[..., Any], args: Union[List[Union[class_schema.Field, class_schema.FieldDef]], Dict[class_schema.FieldKeyDef, class_schema.FieldValueDef], None]=None, returns: Optional[class_schema.ValueSpec]=None, *, auto_typing: bool=True, auto_doc: bool=True, remove_self: bool=True, include_return: bool=False) -> class_schema.Schema:
    s = getattr(cls_or_fn, '__schema__', None)
    if isinstance(s, class_schema.Schema):
        return s
    return signature(cls_or_fn, auto_typing=auto_typing, auto_doc=auto_doc).annotate(args, return_value=returns).to_schema(remove_self=remove_self, include_return=include_return)

Returns the schema from the signature of a class or a function.

Args:
cls_or_fn: A class or a function.
args: (Optional) additional annotations for arguments.
returns: (Optional) additional annotation for return value.
auto_typing: If True, enable type inference from annotations.
auto_doc: If True, extract schema/field description form docstrs.
remove_self: If True, remove the first `self` argument if it appears in the
signature.
include_return: If True, include the return value spec in the schema with
key 'return_value'.

Returns:
A pg.typing.Schema object.


def update_telemetry_configurations(self, configuration, timeout=(- 1)):
    telemetry_conf_uri = self._get_telemetry_configuration_uri()
    default_values = self._get_default_values(self.SETTINGS_TELEMETRY_CONFIG_DEFAULT_VALUES)
    configuration = self._helper.update_resource_fields(configuration, default_values)
    return self._helper.update(configuration, uri=telemetry_conf_uri, timeout=timeout)

Updates the telemetry configuration of a logical interconnect. Changes to the telemetry configuration are
asynchronously applied to all managed interconnects.

Args:
configuration:
The telemetry configuration for the logical interconnect.
timeout:
Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation in
OneView, just stops waiting for its completion.

Returns:
dict: The Logical Interconnect.


def padding(self):
    if all(((p == self._padding[0]) for p in self._padding)):
        return self._padding[0]
    else:
        raise ValueError('This layer uses different paddings for different dimensions. Use .paddings if you want a tuple of per-dimension padding settings.')

Returns the padding algorithm used, if this is the same for all dims.

Use `.paddings` if you want a tuple with the padding algorithm used for each
dimension.

Returns:
The padding algorithm used, if this is the same for all dimensions.

Raises:
ValueError: If different padding algorithms are used for different
dimensions.


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

Create a new storage service REST client.

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

Returns:
A storage_service.api.ApiClient instance

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


def _assert_concat_compatible_structured_tensors(values):
    if not isinstance(values, Sequence):
        raise ValueError('values must be a list of StructuredTensors (not a list)')
    if not values:
        raise ValueError('values must not be an empty list')
    for st in values:
        if not isinstance(st, StructuredTensor):
            raise ValueError('values must be a list of StructuredTensors')
    _assert_all_paths_match(values)
    _assert_all_ranks_match(values)

Sometimes raises an error if concat doesn't make sense statically on values.

values must be a sequence, and each element in values must be a structured
tensor, and must have the same paths. Additionally, each path that is a
submessage must have the same rank.

These constraints are sufficient for concat on the fields to be the same
as concat on structured tensors. This is meant to capture scenarios like
paths that are not in the first structured tensor, but are in later
structured tensors, which will just be ignored by the recursive algorithm.

If the rank of a submessage was different for two structured tensors,
then that is also a non-sensical merge.

Note that all of these checks are static, as paths and submessage ranks
are known.

Args:
values: a Sequence of StructuredTensors.

Raises:
ValueError: if there is any inconsistency as described above.


def validate_json_schema(data, schema, name='task'):
    try:
        jsonschema.validate(data, schema)
    except jsonschema.exceptions.ValidationError as exc:
        raise ScriptWorkerTaskException("Can't validate {} schema!\n{}".format(name, str(exc)), exit_code=STATUSES['malformed-payload'])

Given data and a jsonschema, let's validate it.

This happens for tasks and chain of trust artifacts.

Args:
data (dict): the json to validate.
schema (dict): the jsonschema to validate against.
name (str, optional): the name of the json, for exception messages.
Defaults to "task".

Raises:
ScriptWorkerTaskException: on failure


def __call__(self, environ, start_response):

        path = environ['PATH_INFO'] or '/'
        for p, app in self.apps:

            if path.startswith(p + '/') or path == p:
                environ = environ.copy()
                environ['SCRIPT_NAME'] = environ.get('SCRIPT_NAME', '') + p
                environ['PATH_INFO'] = path[len(p):]
                return app(environ, start_response)

        start_response(
            '404 Not Found', [
                ('Content-Type', 'text/plain'),
                ('Content-Length', '0'),
            ],
        )
        return ['']

Process incoming WSGI request.

Ref: :pep:`3333`

Args:
environ (Mapping): a dict containing WSGI environment variables
start_response (callable): function, which sets response
status and headers

Returns:
list[bytes]: iterable containing bytes to be returned in
HTTP response body


def get_signature_request(self, signature_request_id, ux_version=None):
    request = self._get_request()
    parameters = None
    if (ux_version is not None):
        parameters = {'ux_version': ux_version}
    return request.get((self.SIGNATURE_REQUEST_INFO_URL + signature_request_id), parameters=parameters)

Get a signature request by its ID

Args:

signature_request_id (str):     The id of the SignatureRequest to retrieve

ux_version (int):               UX version, either 1 (default) or 2.

Returns:
A SignatureRequest object


def _validate_measure_sampling(self, experiment):

        if self._shots <= 1:
            self._sample_measure = False
            return

        if hasattr(experiment.config, 'allows_measure_sampling'):
            self._sample_measure = experiment.config.allows_measure_sampling

        else:
            measure_flag = False
            for instruction in experiment.instructions:

                if instruction.name == "reset":
                    self._sample_measure = False
                    return

                if measure_flag:

                    if instruction.name not in ["measure", "barrier", "id", "u0"]:
                        self._sample_measure = False
                        return
                elif instruction.name == "measure":
                    measure_flag = True

            self._sample_measure = True

Determine if measure sampling is allowed for an experiment

Args:
experiment (QobjExperiment): a qobj experiment.


def dump_ddl(metadata: MetaData, dialect_name: str, fileobj: TextIO=sys.stdout, checkfirst: bool=True) -> None:

    def dump(querysql, *multiparams, **params):
        compsql = querysql.compile(dialect=engine.dialect)
        writeline_nl(fileobj, '{sql};'.format(sql=compsql))
    writeline_nl(fileobj, sql_comment('Schema (for dialect {}):'.format(dialect_name)))
    engine = create_engine('{dialect}:
    metadata.create_all(engine, checkfirst=checkfirst)

Sends schema-creating DDL from the metadata to the dump engine.
This makes ``CREATE TABLE`` statements.

Args:
metadata: SQLAlchemy :class:`MetaData`
dialect_name: string name of SQL dialect to generate DDL in
fileobj: file-like object to send DDL to
checkfirst: if ``True``, use ``CREATE TABLE IF NOT EXISTS`` or
equivalent.


def json_integrity(baseline, suspect):

    try:
        for k,v in baseline.items():
            for ks, vs in suspect.items():
                keys_baseline = set(v.keys())
                keys_suspect = set(vs.keys())
                intersect_keys = keys_baseline.intersection(keys_suspect)
                added = keys_baseline - keys_suspect
                rm = keys_suspect - keys_baseline
                logger.info('keys added: %s, keys removed %s' % (str(added), str(rm)))
                if keys_baseline != keys_suspect:
                    return False
    except KeyError as e:
        logger.info(
            'KeyError parsing pre-existing config (%s). Replacing config file' %
            str(e))
    return True

Summary:
Validates baseline dict against suspect dict to ensure contain USERNAME
k,v parameters.
Args:
baseline (dict): baseline json structure
suspect (dict): json object validated against baseline structure
Returns:
Success (matches baseline) | Failure (no match), TYPE: bool


def check_dups(iterable, debug_limit=1000):
    if (not iterable):
        return
    unique = set(iterable)
    if (len(unique) != len(iterable)):
        if (len(iterable) < debug_limit):
            dups = [x for x in unique if (iterable.count(x) > 1)]
            msg = 'Duplicate values: {}'.format(dups)
        else:
            msg = 'Duplicate values, not generating list because len(iterable)={} is greater than debug_limit={}'.format(len(iterable), debug_limit)
        raise ValueError(msg)

Checks an iterable for duplicates

Note that it does not make sense to call this on a set()

If calling on a collection without a .count method,
set @debug_limit to -1.

For custom equality comparisons, create a custom
__eq__ and __hash__ method in the class contained in @iterable

Args:
iterable:    An iterable to check for duplicates
debug_limit: Don't generate a list of duplicates for the
Exception message if the length of @iterable
is >= this value.  This exists because generating
the list does not scale efficiently.
Raises:
ValueError: If @iterable contains duplicates


def connection(self, hostname=None, read_preference=pymongo.ReadPreference.PRIMARY, timeout=300):

        logger.debug("connection({hostname}, {read_preference}, {timeout})".format(**locals()))
        t_start = time.time()
        servers = hostname or ",".join(self.server_map.values())
        while True:
            try:
                if hostname is None:
                    c = pymongo.MongoReplicaSetClient(
                        servers, replicaSet=self.repl_id,
                        read_preference=read_preference,
                        socketTimeoutMS=self.socket_timeout,
                        w=self._write_concern, fsync=True, **self.kwargs)
                    connected(c)
                    if c.primary:
                        self._authenticate_client(c)
                        return c
                    raise pymongo.errors.AutoReconnect("No replica set primary available")
                else:
                    logger.debug("connection to the {servers}".format(**locals()))
                    c = pymongo.MongoClient(
                        servers, socketTimeoutMS=self.socket_timeout,
                        w=self._write_concern, fsync=True, **self.kwargs)
                    connected(c)
                    self._authenticate_client(c)
                    return c
            except (pymongo.errors.PyMongoError):
                exc_type, exc_value, exc_tb = sys.exc_info()
                err_message = traceback.format_exception(exc_type, exc_value, exc_tb)
                logger.error("Exception {exc_type} {exc_value}".format(**locals()))
                logger.error(err_message)
                if time.time() - t_start > timeout:
                    raise pymongo.errors.AutoReconnect("Couldn't connect while timeout {timeout} second".format(**locals()))
                time.sleep(1)

return MongoReplicaSetClient object if hostname specified
return MongoClient object if hostname doesn't specified
Args:
hostname - connection uri
read_preference - default PRIMARY
timeout - specify how long, in seconds, a command can take before server times out.


def assert_not_present(self, selector, testid=None, **kwargs):

        self.info_log(
            "Assert not present selector(%s) testid(%s)" %
            (selector, testid)
        )

        wait_until_not_present = kwargs.get(
            'wait_until_not_present',
            BROME_CONFIG['proxy_driver']['wait_until_not_present_before_assert_not_present']
        )
        self.debug_log(
            "effective wait_until_not_present: %s" % wait_until_not_present
        )

        if wait_until_not_present:
            ret = self.wait_until_not_present(selector, raise_exception=False)
        else:
            ret = not self.is_present(selector)

        if ret:
            if testid is not None:
                self.create_test_result(testid, True)

            return True
        else:
            if testid is not None:
                self.create_test_result(testid, False)

            return False

Assert that the element is not present in the dom

Args:
selector (str): the selector used to find the element
test_id (str): the test_id or a str

Kwargs:
wait_until_not_present (bool)

Returns:
bool: True is the assertion succeed; False otherwise.


def GetEntries(self, parser_mediator, match=None, **unused_kwargs):
    accounts = match.get('Accounts', {})
    for (name_account, account) in iter(accounts.items()):
        first_name = account.get('FirstName', '<FirstName>')
        last_name = account.get('LastName', '<LastName>')
        general_description = '{0:s} ({1:s} {2:s})'.format(name_account, first_name, last_name)
        event_data = plist_event.PlistTimeEventData()
        event_data.key = name_account
        event_data.root = '/Accounts'
        datetime_value = account.get('CreationDate', None)
        if datetime_value:
            event_data.desc = 'Configured Apple account {0:s}'.format(general_description)
            event = time_events.PythonDatetimeEvent(datetime_value, definitions.TIME_DESCRIPTION_WRITTEN)
            parser_mediator.ProduceEventWithEventData(event, event_data)
        datetime_value = account.get('LastSuccessfulConnect', None)
        if datetime_value:
            event_data.desc = 'Connected Apple account {0:s}'.format(general_description)
            event = time_events.PythonDatetimeEvent(datetime_value, definitions.TIME_DESCRIPTION_WRITTEN)
            parser_mediator.ProduceEventWithEventData(event, event_data)
        datetime_value = account.get('ValidationDate', None)
        if datetime_value:
            event_data.desc = 'Last validation Apple account {0:s}'.format(general_description)
            event = time_events.PythonDatetimeEvent(datetime_value, definitions.TIME_DESCRIPTION_WRITTEN)
            parser_mediator.ProduceEventWithEventData(event, event_data)

Extracts relevant Apple Account entries.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
match (Optional[dict[str: object]]): keys extracted from PLIST_KEYS.


def _make_1d_state(state_size, seed):
    if isinstance(seed, int):
        ls = []
        for _ in range(state_size):
            ls.append(seed & SEED_BIT_MASK)
            seed >>= SEED_TYPE_BITS
        seed = ls
    seed = nest.map_structure(_uint_to_int, seed)
    seed = math_ops.cast(seed, STATE_TYPE)
    seed = array_ops.reshape(seed, [-1])
    seed = seed[0:state_size]
    seed_size = seed.shape[0]
    if seed_size is None:
        seed_size = array_ops.shape(seed)[0]
    padding_size = math_ops.maximum(state_size - seed_size, 0)
    padding = array_ops.zeros([padding_size], seed.dtype)
    seed = array_ops.concat([padding, seed], axis=0)
    seed.set_shape([state_size])
    return seed

Makes a 1-D RNG state.

Args:
state_size: an integer.
seed: an integer or 1-D tensor.

Returns:
a 1-D tensor of shape [state_size] and dtype STATE_TYPE.


def json_expand_queries(recipe, nodes=['query']):
    if isinstance(recipe, dict):
        for key, value in recipe.items():
            if isinstance(value, str) and key in nodes:
                spaces = re.search('  +', value)
                if spaces:
                    offset = spaces.end() - spaces.start()
                    if offset > 1:
                        recipe[key] = re.sub('\\s{%d}' % offset, '\n' + ' ' * (offset - 2), value)
            else:
                json_expand_queries(value, nodes)
    elif type(recipe) in (list, tuple):
        for value in recipe:
            json_expand_queries(value, nodes)
    return recipe

Attempts to expand newlines in fields containing queries.

Works only on fields, not lists.
Uses first whitespace > 1 as indent indicator.
Appends newlines to start of each indent indicator.

Args:
recipe - dictionary or list containing a recipe.
nodes - the list of fields which contain queries.

Returns:
Dictionary modified in place with newlines for each query field.


def find_stream(cls, fileobj, max_bytes):
    r = BitReader(fileobj)
    stream = cls(r)
    if stream.sync(max_bytes):
        stream.offset = ((r.get_position() - 12)
        return stream

Returns a possibly valid _ADTSStream or None.

Args:
max_bytes (int): maximum bytes to read


def set_ylim(self, xlims, dx, xscale, reverse=False):
    self._set_axis_limits('y', xlims, dx, xscale, reverse)
    return

Set y limits for plot.

This will set the limits for the y axis
for the specific plot.

Args:
ylims (len-2 list of floats): The limits for the axis.
dy (float): Amount to increment by between the limits.
yscale (str): Scale of the axis. Either `log` or `lin`.
reverse (bool, optional): If True, reverse the axis tick marks. Default is False.


def _bundleable(desc):

    def inner(a_func, settings, request, **kwargs):
        'Schedules execution of a bundling task.'
        if (not settings.bundler):
            return a_func(request, **kwargs)
        the_id = bundling.compute_bundle_id(request, desc.request_discriminator_fields)
        return settings.bundler.schedule(a_func, the_id, desc, request, kwargs)
    return inner

Creates a function that transforms an API call into a bundling call.

It transform a_func from an API call that receives the requests and returns
the response into a callable that receives the same request, and
returns a :class:`bundling.Event`.

The returned Event object can be used to obtain the eventual result of the
bundled call.

Args:
desc (gax.BundleDescriptor): describes the bundling that a_func
supports.

Returns:
Callable: takes the API call's request and keyword args and returns a
bundling.Event object.


def WriteConfig(self, config_file=None):

    config_file = config_file or self.config_file
    config_name = os.path.splitext(os.path.basename(config_file))[0]
    config_lock = (
        '%s/lock/google_%s.lock' % (constants.LOCALSTATEDIR, config_name))
    with file_utils.LockFile(config_lock):
      with open(config_file, 'w') as config_fp:
        if self.config_header:
          self._AddHeader(config_fp)
        self.config.write(config_fp)

Write the config values to a given file.

Args:
config_file: string, the file location of the config file to write.


def like_shared_file(self, sharekey=None):
    if (not sharekey):
        raise Exception("You must specify a sharekey of the file youwant to 'like'.")
    endpoint = '/api/sharedfile/{sharekey}/like'.format(sharekey=sharekey)
    data = self._make_request('POST', endpoint=endpoint, data=None)
    try:
        sf = SharedFile.NewFromJSON(data)
        sf.liked = True
        return sf
    except:
        raise Exception('{0}'.format(data['error']))

'Like' a SharedFile. mlkshk doesn't allow you to unlike a
sharedfile, so this is ~~permanent~~.

Args:
sharekey (str): Sharekey for the file you want to 'like'.

Returns:
Either a SharedFile on success, or an exception on error.


def pack_aspect_ratios(aspect_ratios: List[List[Tuple[int, int]]], pad_value: int=1) -> np.ndarray:
    batch_size = len(aspect_ratios)
    max_num_images = max([len(row) for row in aspect_ratios])
    aspect_ratios_stacked = np.full((batch_size, max_num_images, 2), pad_value, dtype=np.int64)
    for i, row in enumerate(aspect_ratios):
        if len(row) > 0:
            aspect_ratios_stacked[i, :len(row)] = np.array(row)
    return aspect_ratios_stacked

Stack a list of aspect ratios into a numpy array.

Args:
aspect_ratios (`List[List[Tuple[int, int]]]`):
A list of aspect ratios.
pad_value (`int`, *optional*, defaults to 1):
The value to pad the aspect ratios with.

Returns:
`np.ndarray`:
The aspect ratios stacked into a numpy array with shape (batch_size, max_num_images, 2).


def delete_bq_dataset(project, dataset_ref):
    client = bigquery.Client(project=project)
    client.delete_dataset(dataset_ref, delete_contents=True)

Deletes a BigQuery dataset and its contents.

Args:
project: Project to work in.
dataset_ref: A ``google.cloud.bigquery.dataset.DatasetReference`` object
pointing to the dataset to delete.


def _expand_to_event_rank(self, x):

    expanded_x = x
    for _ in range(tensorshape_util.rank(self.event_shape)):
      expanded_x = tf.expand_dims(expanded_x, -1)
    return expanded_x

Expand the rank of x up to static_event_rank times for broadcasting.

The static event rank was checked to not be None at construction time.

Args:
x: A tensor to expand.
Returns:
The expanded tensor.


def normalize_number_fields(pb: _T) -> _T:
    for desc, values in pb.ListFields():
        is_repeated = True
        if desc.label != descriptor.FieldDescriptor.LABEL_REPEATED:
            is_repeated = False
            values = [values]
        normalized_values = None
        if desc.type in (descriptor.FieldDescriptor.TYPE_INT64, descriptor.FieldDescriptor.TYPE_UINT64, descriptor.FieldDescriptor.TYPE_SINT64):
            normalized_values = [int(x) for x in values]
        elif desc.type in (descriptor.FieldDescriptor.TYPE_INT32, descriptor.FieldDescriptor.TYPE_UINT32, descriptor.FieldDescriptor.TYPE_SINT32, descriptor.FieldDescriptor.TYPE_ENUM):
            normalized_values = [int(x) for x in values]
        elif desc.type == descriptor.FieldDescriptor.TYPE_FLOAT:
            normalized_values = [round(x, 6) for x in values]
        elif desc.type == descriptor.FieldDescriptor.TYPE_DOUBLE:
            normalized_values = [round(float(x), 7) for x in values]
        if normalized_values is not None:
            if is_repeated:
                pb.ClearField(desc.name)
                getattr(pb, desc.name).extend(normalized_values)
            else:
                setattr(pb, desc.name, normalized_values[0])
        if desc.type == descriptor.FieldDescriptor.TYPE_MESSAGE or desc.type == descriptor.FieldDescriptor.TYPE_GROUP:
            if desc.type == descriptor.FieldDescriptor.TYPE_MESSAGE and desc.message_type.has_options and desc.message_type.GetOptions().map_entry:
                if desc.message_type.fields_by_number[2].type == descriptor.FieldDescriptor.TYPE_MESSAGE:
                    for v in values:
                        normalize_number_fields(v)
            else:
                for v in values:
                    normalize_number_fields(v)
    return pb

Normalizes types and precisions of number fields in a protocol buffer.

Due to subtleties in the python protocol buffer implementation, it is possible
for values to have different types and precision depending on whether they
were set and retrieved directly or deserialized from a protobuf. This function
normalizes integer values to ints and longs based on width, 32-bit floats to
five digits of precision to account for python always storing them as 64-bit,
and ensures doubles are floating point for when they're set to integers.

Modifies pb in place. Recurses into nested objects.

Args:
pb: proto2 message.

Returns:
the given pb, modified in place.


def get_size_with_aspect_ratio(image_size: Tuple[int, int], size: int, max_size: Optional[int]=None, mod_size: int=16) -> Tuple[int, int]:
    height, width = image_size
    raw_size = None
    if max_size is not None:
        min_original_size = float(min((height, width)))
        max_original_size = float(max((height, width)))
        if max_original_size / min_original_size * size > max_size:
            raw_size = max_size * min_original_size / max_original_size
            size = int(round(raw_size))
    if width < height:
        ow = size
        if max_size is not None and raw_size is not None:
            oh = int(raw_size * height / width)
        else:
            oh = int(size * height / width)
    elif height <= width and height == size or (width <= height and width == size):
        oh, ow = (height, width)
    else:
        oh = size
        if max_size is not None and raw_size is not None:
            ow = int(raw_size * width / height)
        else:
            ow = int(size * width / height)
    if mod_size is not None:
        ow_mod = torch.remainder(torch.tensor(ow), mod_size).item()
        oh_mod = torch.remainder(torch.tensor(oh), mod_size).item()
        ow = ow - ow_mod
        oh = oh - oh_mod
    return (oh, ow)

Computes the output image size given the input image size and the desired output size with multiple of divisible_size.

Args:
image_size (`Tuple[int, int]`):
The input image size.
size (`int`):
The desired output size.
max_size (`int`, *optional*):
The maximum allowed output size.
mod_size (`int`, *optional*):
The size to make multiple of mod_size.


def run(self, args):
    jlink = self.create_jlink(args)
    if args.list:
        print(('Built-in Licenses: %s' % ', '.join(jlink.licenses.split(','))))
        print(('Custom Licenses: %s' % ', '.join(jlink.custom_licenses.split(','))))
    elif (args.add is not None):
        if jlink.add_license(args.add):
            print('Successfully added license.')
        else:
            print('License already exists.')
    elif args.erase:
        if jlink.erase_licenses():
            print('Successfully erased all custom licenses.')
        else:
            print('Failed to erase custom licenses.')

Runs the license command.

Args:
self (LicenseCommand): the ``LicenseCommand`` instance
args (Namespace): the arguments passed on the command-line

Returns:
``None``


def _format_message(self, channel, text):
    payload = {'type': 'message', 'id': next(self._msg_ids)}
    payload.update(channel=channel, text=text)
    return json.dumps(payload)

Format an outgoing message for transmission.

Note:
Adds the message type (``'message'``) and incremental ID.

Arguments:
channel (:py:class:`str`): The channel to send to.
text (:py:class:`str`): The message text to send.

Returns:
:py:class:`str`: The JSON string of the message.


def account_id(self, value):

    if type(value) is not str:
      raise TypeError("commit value must be string")
    self._account_id = value

Sets the current account id

Args:
value: current account id (string)

Returns:
None


def _maybe_save_assets(write_fn, assets_to_add=None):
    asset_filename_map = {}
    if assets_to_add is None:
        tf_logging.info('No assets to save.')
        return asset_filename_map
    for asset_tensor in assets_to_add:
        asset_source_filepath = _asset_path_from_tensor(asset_tensor)
        if not asset_source_filepath:
            raise ValueError(f'Asset filepath tensor {asset_tensor} in is invalid.')
        asset_filename = get_asset_filename_to_add(asset_source_filepath, asset_filename_map)
        write_fn(asset_filename, asset_tensor)
        asset_filename_map[asset_filename] = asset_source_filepath
    tf_logging.info('Assets added to graph.')
    return asset_filename_map

Saves assets to the meta graph.

Args:
write_fn: A function callback that writes assets into meta graph.
assets_to_add: The list where the asset paths are setup.

Returns:
A dict of asset basenames for saving to the original full path to the asset.

Raises:
ValueError: Indicating an invalid filepath tensor.


def get_locations_list(self, lower_bound=0, upper_bound=None):

        real_upper_bound = upper_bound
        if upper_bound is None:
            real_upper_bound = self.nbr_of_sub_locations()

        try:
            return self._locations_list[lower_bound:real_upper_bound]
        except:
            return list()

Return the internal location list.

Args:
lower_bound:
upper_bound:

Returns:


def get_monochrome_handler(
        extranames: List[str] = None,
        with_process_id: bool = False,
        with_thread_id: bool = False,
        stream: TextIO = None) -> logging.StreamHandler:

    fmt = "%(asctime)s.%(msecs)03d"
    if with_process_id or with_thread_id:
        procinfo = []
        if with_process_id:
            procinfo.append("p%(process)d")
        if with_thread_id:
            procinfo.append("t%(thread)d")
        fmt += " [{}]".format(".".join(procinfo))
    extras = ":" + ":".join(extranames) if extranames else ""
    fmt += " %(name)s{extras}:%(levelname)s: ".format(extras=extras)
    fmt += "%(message)s"
    f = logging.Formatter(fmt, datefmt=LOG_DATEFMT, style='%')
    h = logging.StreamHandler(stream)
    h.setFormatter(f)
    return h

Gets a monochrome log handler using a standard format.

Args:
extranames: additional names to append to the logger's name
with_process_id: include the process ID in the logger's name?
with_thread_id: include the thread ID in the logger's name?
stream: ``TextIO`` stream to send log output to

Returns:
the :class:`logging.StreamHandler`


def run_optimization(self):
    penalty_val = self.params['init_penalty']
    self.smooth_on = False
    smooth_val = 0
    learning_rate_val = self.params['init_learning_rate']
    self.current_outer_step = 1
    while (self.current_outer_step <= self.params['outer_num_steps']):
        tf.logging.info('Running outer step %d with penalty %f', self.current_outer_step, penalty_val)
        self.current_step = 0
        found_cert = self.run_one_step(self.dual_object.m_min_vec_estimate, self.params['large_eig_num_steps'], smooth_val, penalty_val, learning_rate_val)
        if found_cert:
            return True
        while (self.current_step < self.params['inner_num_steps']):
            self.current_step = (self.current_step + 1)
            found_cert = self.run_one_step(self.dual_object.m_min_vec_estimate, self.params['small_eig_num_steps'], smooth_val, penalty_val, learning_rate_val)
            if found_cert:
                return True
        if (self.current_total_objective < UPDATE_PARAM_CONSTANT):
            penalty_val = (penalty_val * self.params['beta'])
            learning_rate_val = (learning_rate_val * self.params['learning_rate_decay'])
        else:
            self.params['small_eig_num_steps'] = (1.5 * self.params['small_eig_num_steps'])
        if (np.abs(self.current_eig_val_estimate) < 0.01):
            smooth_val = self.params['smoothness_parameter']
        self.current_outer_step = (self.current_outer_step + 1)
    return False

Run the optimization, call run_one_step with suitable placeholders.

Returns:
True if certificate is found
False otherwise


def to_dict(self) -> Dict[str, Any]:
    output = copy.deepcopy(self.__dict__)
    output['video_processor_type'] = self.__class__.__name__
    return output

Serializes this instance to a Python dictionary.

Returns:
`Dict[str, Any]`: Dictionary of all the attributes that make up this video processor instance.


def __iter__(self) -> iterator_ops.OwnedIterator:
    if context.executing_eagerly() or ops.inside_function():
        with ops.colocate_with(self._variant_tensor):
            return iterator_ops.OwnedIterator(self)
    else:
        raise RuntimeError('`tf.data.Dataset` only supports Python-style iteration in eager mode or within tf.function.')

Creates an iterator for elements of this dataset.

The returned iterator implements the Python Iterator protocol.

Returns:
An `tf.data.Iterator` for the elements of this dataset.

Raises:
RuntimeError: If not inside of tf.function and not executing eagerly.


def csv_to_numpy(string_like, dtype=None):

    stream = StringIO(string_like)
    return np.genfromtxt(stream, dtype=dtype, delimiter=',')

Convert a CSV object to a numpy array.

Args:
string_like (str): CSV string.
dtype (dtype, optional):  Data type of the resulting array. If None, the dtypes will be determined by the
contents of each column, individually. This argument can only be used to
'upcast' the array.  For downcasting, use the .astype(t) method.
Returns:
(np.array): numpy array


def OpenClient(client_id=None, token=None):

  if not token:
    try:
      token = ApprovalFind(client_id, token=token)
    except access_control.UnauthorizedAccess as e:
      logging.debug("No authorization found for access to client: %s", e)

  try:

    client = aff4.FACTORY.Open(
        rdfvalue.RDFURN(client_id), mode="r", token=token)
    return client, token
  except access_control.UnauthorizedAccess:
    logging.warning(
        "Unable to find a valid reason for client %s. You may need "
        "to request approval.", client_id)
    return None, None

Opens the client, getting potential approval tokens.

Args:
client_id: The client id that should be opened.
token: Token to use to open the client

Returns:
tuple containing (client, token) objects or (None, None) on if
no appropriate aproval tokens were found.


def __init__(self, ver_str='', make_token=AlphanumericVersionToken):

        self.tokens = []
        self.seps = []
        self._str = None
        self._hash = None

        if ver_str:
            toks = re_token.findall(ver_str)
            if not toks:
                raise VersionError(ver_str)

            seps = re_token.split(ver_str)
            if seps[0] or seps[-1] or max(len(x) for x in seps) > 1:
                raise VersionError("Invalid version syntax: '%s'" % ver_str)

            for tok in toks:
                try:
                    self.tokens.append(make_token(tok))
                except VersionError as e:
                    raise VersionError("Invalid version '%s': %s"
                                       % (ver_str, str(e)))

            self.seps = seps[1:-1]

Create a Version object.

Args:
ver_str: Version string.
make_token: Callable that creates a VersionToken subclass from a
string.


def cosine(w, A=1, phi=0, offset=0):
    from math import cos

    def f(i):
        return ((A * cos(((w * i) + phi))) + offset)
    return partial(force, sequence=_advance(f))

Return a driver function that can advance a sequence of cosine values.

.. code-block:: none

value = A * cos(w*i + phi) + offset

Args:
w (float) : a frequency for the cosine driver
A (float) : an amplitude for the cosine driver
phi (float) : a phase offset to start the cosine driver with
offset (float) : a global offset to add to the driver values


def _required_idiom(tag_name, index, notfoundmsg):

    cond = ""
    if index > 0:
        cond = " or len(el) - 1 < %d" % index

    tag_name = str(tag_name)

    output = IND + "if not el%s:\n" % cond
    output += IND + IND + "raise UserWarning(\n"
    output += IND + IND + IND + "%s +\n" % repr(notfoundmsg.strip() + "\n")
    output += IND + IND + IND + repr("Tag name: " + tag_name) + " + '\\n' +\n"
    output += IND + IND + IND + "'El:' + str(el) + '\\n' +\n"
    output += IND + IND + IND + "'Dom:' + str(dom)\n"
    output += IND + IND + ")\n\n"

    return output + IND + "el = el[%d]\n\n" % index

Generate code, which make sure that `tag_name` has enoug items.

Args:
tag_name (str): Name of the container.
index (int): Index of the item you want to obtain from container.
notfoundmsg (str): Raise :class:`.UserWarning` with debug data and
following message.

Returns:
str: Python code.


def GetFileEntryByPathSpec(self, path_spec):

    fsntfs_file_entry = None
    location = getattr(path_spec, 'location', None)
    mft_attribute = getattr(path_spec, 'mft_attribute', None)
    mft_entry = getattr(path_spec, 'mft_entry', None)

    if (location == self.LOCATION_ROOT or
        mft_entry == self.MFT_ENTRY_ROOT_DIRECTORY):
      fsntfs_file_entry = self._fsntfs_volume.get_root_directory()
      return ntfs_file_entry.NTFSFileEntry(
          self._resolver_context, self, path_spec,
          fsntfs_file_entry=fsntfs_file_entry, is_root=True)

    try:
      if mft_attribute is not None and mft_entry is not None:
        fsntfs_file_entry = self._fsntfs_volume.get_file_entry(mft_entry)
      elif location is not None:
        fsntfs_file_entry = self._fsntfs_volume.get_file_entry_by_path(location)

    except IOError as exception:
      raise errors.BackEndError(exception)

    if fsntfs_file_entry is None:
      return None

    return ntfs_file_entry.NTFSFileEntry(
        self._resolver_context, self, path_spec,
        fsntfs_file_entry=fsntfs_file_entry)

Retrieves a file entry for a path specification.

Args:
path_spec (PathSpec): path specification.

Returns:
NTFSFileEntry: file entry or None if not available.

Raises:
BackEndError: if the file entry cannot be opened.


def _ParseLNKFile(self, parser_mediator, file_entry, file_offset, remaining_file_size):
    path_spec = path_spec_factory.Factory.NewPathSpec(definitions.TYPE_INDICATOR_DATA_RANGE, range_offset=file_offset, range_size=remaining_file_size, parent=file_entry.path_spec)
    display_name = '{0:s}
    try:
        lnk_file_object = resolver.Resolver.OpenFileObject(path_spec)
    except (dfvfs_errors.BackEndError, RuntimeError) as exception:
        message = 'unable to open LNK file: {0:s} with error: {1!s}'.format(display_name, exception)
        parser_mediator.ProduceExtractionWarning(message)
        return 0
    parser_mediator.AppendToParserChain(self._WINLNK_PARSER)
    try:
        lnk_file_object.seek(0, os.SEEK_SET)
        self._WINLNK_PARSER.ParseFileLNKFile(parser_mediator, lnk_file_object, display_name)
    finally:
        parser_mediator.PopFromParserChain()
    lnk_file_size = lnk_file_object.get_offset()
    lnk_file_object.close()
    return lnk_file_size

Parses a LNK file stored within the .customDestinations-ms file.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
file_entry (dfvfs.FileEntry): a file entry.
file_offset (int): offset to the LNK file, relative to the start of the
.customDestinations-ms file.
remaining_file_size (int): size of the data remaining in the
.customDestinations-ms file.

Returns:
int: size of the LNK file data or 0 if the LNK file could not be read.


def get(cls, session, record_id, endpoint_override=None):
    cls._check_implements('get')
    try:
        return cls((endpoint_override or ('/%s/%d.json' % (cls.__endpoint__, record_id))), singleton=True, session=session)
    except HelpScoutRemoteException as e:
        if (e.status_code == 404):
            return None
        else:
            raise

Return a specific record.

Args:
session (requests.sessions.Session): Authenticated session.
record_id (int): The ID of the record to get.
endpoint_override (str, optional): Override the default
endpoint using this.

Returns:
helpscout.BaseModel: A record singleton, if existing. Otherwise
``None``.


def reshard(self, checkpoint_values: tensor.Tensor, shape_and_slice: str) -> tensor.Tensor:
    return _shard_from_cpu_to_sc(checkpoint_values, shape_and_slice, self._to_shard_layout)

Reshards the checkpoint values according to the resharding plan.

Args:
checkpoint_values: The checkpoint values to be resharded.
shape_and_slice: The shape and slice spec to be returned after resharding.

Returns:
The resharded tensor slice.


def run(self, tasklet, **kwds):

    start_time = time.time()
    n = 1

    while True:
      e = None
      result = None
      got_result = False

      try:
        result = yield tasklet(**kwds)
        got_result = True
        if not self.should_retry(result):
          raise ndb.Return(result)
      except runtime.DeadlineExceededError:
        logging.debug(
            'Tasklet has exceeded request deadline after %s seconds total',
            time.time() - start_time)
        raise
      except self.retriable_exceptions as e:
        pass

      if n == 1:
        logging.debug('Tasklet is %r', tasklet)

      delay = self.retry_params.delay(n, start_time)

      if delay <= 0:
        logging.debug(
            'Tasklet failed after %s attempts and %s seconds in total',
            n, time.time() - start_time)
        if got_result:
          raise ndb.Return(result)
        elif e is not None:
          raise e
        else:
          assert False, 'Should never reach here.'

      if got_result:
        logging.debug(
            'Got result %r from tasklet.', result)
      else:
        logging.debug(
            'Got exception "%r" from tasklet.', e)
      logging.debug('Retry in %s seconds.', delay)
      n += 1
      yield tasklets.sleep(delay)

Run a tasklet with retry.

The retry should be transparent to the caller: if no results
are successful, the exception or result from the last retry is returned
to the caller.

Args:
tasklet: the tasklet to run.
**kwds: keywords arguments to run the tasklet.

Raises:
The exception from running the tasklet.

Returns:
The result from running the tasklet.


def filename(self, appendix=None, create_if_not_existing=False):

        if os.path.isabs(self.settings['path']) == False and self.data_path is not None:
            path = os.path.join(self.data_path, self.settings['path'])
        else:
            path = self.settings['path']

        tag = self.settings['tag']

        filename = os.path.join(path, "{:s}_{:s}".format(self.start_time.strftime('%y%m%d-%H_%M_%S'),tag))

        if os.path.exists(filename) == False and create_if_not_existing:
            os.makedirs(filename)

        if appendix is not None:
            filename = os.path.join(filename,  "{:s}_{:s}{:s}".format(self.start_time.strftime('%y%m%d-%H_%M_%S'),tag,appendix))

        return filename

creates a filename based
Args:
appendix: appendix for file

Returns: filename


def __getitem__(self,key):

        self.rdb.expire(self.session_hash,self.ttl)
        encoded_result = self.rdb.hget(self.session_hash,key)
        if encoded_result is None:
            return None
        else:
            return encoded_result.decode('utf-8')

Return a value associated with a key from the session dictionary.

Args:
key (str): The dictionary key.

Returns:
str: The value associate with that key or None if the key is
not in the dictionary.


def _MergeIdentical(self, a, b):
    if (a != b):
        raise MergeError(("values must be identical ('%s' vs '%s')" % (transitfeed.EncodeUnicode(a), transitfeed.EncodeUnicode(b))))
    return b

Tries to merge two values. The values are required to be identical.

Args:
a: The first value.
b: The second value.

Returns:
The trivially merged value.

Raises:
MergeError: The values were not identical.


def list_filepaths(self, wildcard=None):
    fnames = [f for f in os.listdir(self.path)]
    filepaths = filter(os.path.isfile, [os.path.join(self.path, f) for f in fnames])
    if (wildcard is not None):
        filepaths = WildCard(wildcard).filter(filepaths)
    return filepaths

Return the list of absolute filepaths in the directory.

Args:
wildcard: String of tokens separated by "|". Each token represents a pattern.
If wildcard is not None, we return only those files that match the given shell pattern (uses fnmatch).
Example:
wildcard="*.nc|*.pdf" selects only those files that end with .nc or .pdf


def random(percent, fields=None, count=0):

    def _random_sampling(sql):
        projection = Sampling._create_projection(fields)
        sql = ('SELECT %s FROM (%s) WHERE rand() < %f' % (projection, sql, (float(percent) / 100.0)))
        if (count != 0):
            sql = ('%s LIMIT %d' % (sql, count))
        return sql
    return _random_sampling

Provides a sampling strategy that picks a semi-random set of rows.

Args:
percent: the percentage of the resulting hashes to select.
fields: an optional list of field names to retrieve.
count: maximum number of rows to limit the sampled results to (default 5).
Returns:
A sampling function that can be applied to get some random rows. In order for this to
provide a good random sample percent should be chosen to be ~count/#rows where #rows
is the number of rows in the object (query, view or table) being sampled.
The rows will be returned in order; i.e. the order itself is not randomized.


def send(self, stream=False):
    try:
        response = self.session.request(self._http_method, self._url, auth=self._basic_auth, data=self._body, files=self._files, headers=self._headers, params=self._payload, stream=stream, timeout=self._timeout)
    except Exception as e:
        err = 'Failed making HTTP request ({}).'.format(e)
        raise RuntimeError(err)
    self.tcex.log.info(u'Status Code: {}'.format(response.status_code))
    return response

Send the HTTP request via Python Requests modules.

This method will send the request to the remote endpoint.  It will try to handle
temporary communications issues by retrying the request automatically.

Args:
stream (bool): Boolean to enable stream download.

Returns:
Requests.Response: The Request response


def trash_note(self, note_id):
    (note, status) = self.get_note(note_id)
    if (status == (- 1)):
        return (note, status)
    if (not note['deleted']):
        note['deleted'] = True
        note['modificationDate'] = time.time()
        return self.update_note(note)
    else:
        return (note, 0)

Method to move a note to the trash

Arguments:
- note_id (string): key of the note to trash

Returns:
A tuple `(note, status)`

- note (dict): the newly created note or an error message
- status (int): 0 on success and -1 otherwise


def clear_redis(self, variable, clear_type):

        if variable is None:
            return
        if variable in self._clear_redis_tracker:
            return
        if not re.match(self._vars_match, variable):
            return
        self.log.info('[{}] Deleting redis variable: {}.'.format(clear_type, variable))
        print('Clearing Variables: {}{}{}'.format(c.Style.BRIGHT, c.Fore.MAGENTA, variable))
        self.tcex.playbook.delete(variable)
        self._clear_redis_tracker.append(variable)

Delete Redis data for provided variable.

Args:
variable (str): The Redis variable to delete.
clear_type (str): The type of clear action.


def clinvar_submissions(self, user_id, institute_id):

        LOG.info("Retrieving all clinvar submissions for user '%s', institute '%s'", user_id, institute_id)

        query = dict(user_id=user_id, institute_id=institute_id)
        results = list(self.clinvar_submission_collection.find(query))

        submissions = []
        for result in results:
            submission = {}
            submission['_id'] =  result.get('_id')
            submission['status'] = result.get('status')
            submission['user_id'] = result.get('user_id')
            submission['institute_id'] = result.get('institute_id')
            submission['created_at'] = result.get('created_at')
            submission['updated_at'] = result.get('updated_at')

            if 'clinvar_subm_id' in result:
                submission['clinvar_subm_id'] = result['clinvar_subm_id']

            if result.get('variant_data'):
                submission['variant_data'] = self.clinvar_collection.find({'_id': { "$in": result['variant_data'] } })
            if result.get('case_data'):
                submission['case_data'] = self.clinvar_collection.find({'_id' : { "$in": result['case_data'] } })

            submissions.append(submission)

        return submissions

Collect all open and closed clinvar submission created by a user for an institute

Args:
user_id(str): a user ID
institute_id(str): an institute ID

Returns:
submissions(list): a list of clinvar submission objects


def load_map_info(self, map_file):

        if self.ensemble_name.upper() == "SSEF":
            proj_dict, grid_dict = read_arps_map_file(map_file)
            self.dx = int(grid_dict["dx"])
            mapping_data = make_proj_grids(proj_dict, grid_dict)
            for m, v in mapping_data.items():
                setattr(self, m, v)
            self.i, self.j = np.indices(self.lon.shape)
            self.proj = get_proj_obj(proj_dict)
        elif self.ensemble_name.upper() in ["NCAR", "NCARSTORM", "HRRR", "VSE", "HREFV2"]:
            proj_dict, grid_dict = read_ncar_map_file(map_file)
            if self.member_name[0:7] == "1km_pbl":
                grid_dict["dx"] = 1000
                grid_dict["dy"] = 1000
                grid_dict["sw_lon"] = 258.697
                grid_dict["sw_lat"] = 23.999
                grid_dict["ne_lon"] = 282.868269206236
                grid_dict["ne_lat"] = 36.4822338520542

            self.dx = int(grid_dict["dx"])
            mapping_data = make_proj_grids(proj_dict, grid_dict)
            for m, v in mapping_data.items():
                setattr(self, m, v)
            self.i, self.j = np.indices(self.lon.shape)
            self.proj = get_proj_obj(proj_dict)

Load map projection information and create latitude, longitude, x, y, i, and j grids for the projection.

Args:
map_file: File specifying the projection information.


def size(self):
    width = c_double(0)
    height = c_double(0)
    rc = self._libinput.libinput_device_get_size(self._handle, byref(width), byref(height))
    assert (rc == 0), 'This device does not provide size information'
    return (width.value, height.value)

The physical size of a device in mm, where meaningful.

This property is only valid on devices with the required data, i.e.
tablets, touchpads and touchscreens. For other devices this property
raises :exc:`AssertionError`.

Returns:
(float, float): (Width, Height) in mm.
Raises:
AssertionError


def _prefix_from_prefix_string(self, prefixlen_str):

        if not _BaseV4._DECIMAL_DIGITS.issuperset(prefixlen_str):
            self._report_invalid_netmask(prefixlen_str)
        try:
            prefixlen = int(prefixlen_str)
        except ValueError:
            self._report_invalid_netmask(prefixlen_str)
        if not (0 <= prefixlen <= self._max_prefixlen):
            self._report_invalid_netmask(prefixlen_str)
        return prefixlen

Return prefix length from a numeric string

Args:
prefixlen_str: The string to be converted

Returns:
An integer, the prefix length.

Raises:
NetmaskValueError: If the input is not a valid netmask


def generate_sentence(self, chain):

        def weighted_choice(choices):
            total_weight = sum(weight for val, weight in choices)
            rand = random.uniform(0, total_weight)

            upto = 0
            for val, weight in choices:
                if upto + weight >= rand:
                    return val
                upto += weight

        sentence = list(random.choice(chain.startwords))

        while not sentence[-1][-1] in ['.', '?', '!']:
            sentence.append(
                weighted_choice(
                    chain.content[tuple(sentence[-2:])].items()
                )
            )

        return ' '.join(sentence)

!DEMO!
Demo function that shows how to generate a simple sentence starting with
uppercase letter without lenght limit.

Args:
chain: MarkovChain that will be used to generate sentence


def hist_file(ticker: str, dt, typ='TRADE') -> str:
    data_path = os.environ.get(assist.BBG_ROOT, '').replace('\\', '/')
    if (not data_path):
        return ''
    asset = ticker.split()[(- 1)]
    proper_ticker = ticker.replace('/', '_')
    cur_dt = pd.Timestamp(dt).strftime('%Y-%m-%d')
    return f'{data_path}/{asset}/{proper_ticker}/{typ}/{cur_dt}.parq'

Data file location for Bloomberg historical data

Args:
ticker: ticker name
dt: date
typ: [TRADE, BID, ASK, BID_BEST, ASK_BEST, BEST_BID, BEST_ASK]

Returns:
file location

Examples:
>>> os.environ['BBG_ROOT'] = ''
>>> hist_file(ticker='ES1 Index', dt='2018-08-01') == ''
True
>>> os.environ['BBG_ROOT'] = '/data/bbg'
>>> hist_file(ticker='ES1 Index', dt='2018-08-01')
'/data/bbg/Index/ES1 Index/TRADE/2018-08-01.parq'


def load_and_check(self, base_settings, prompt=None):
    checker = Checker(self.file_name, self.section, self.registry, self.strategy_type, prompt)
    settings = self.load(base_settings)
    if checker.check(settings):
        return (settings, True)
    return (None, False)

Load settings and check them.

Loads the settings from ``base_settings``, then checks them.

Returns:
(merged settings, True) on success
(None, False) on failure


def eval_features(json):
    return {'close': json[(- 1)]['close'], 'sma': SMA.eval_from_json(json), 'rsi': RSI.eval_from_json(json), 'so': SO.eval_from_json(json), 'obv': OBV.eval_from_json(json)}

Gets technical analysis features from market data JSONs

Args:
json: JSON data as a list of dict dates, where the keys are
the raw market statistics.

Returns:
Dict of market features and their values


def batch_reduce_implementation(self, reduce_op, value_destination_pairs, options):
    raise NotImplementedError('batch_reduce_implementation method must be implemented in descendants.')

Implementation of `batch_reduce`.

Overriding this method is useful for subclass implementers.

Args:
reduce_op: a `tf.distribute.ReduceOp` specifying how values should be
combined.
value_destination_pairs: a sequence of (value, destinations) pairs. See
`reduce` for descriptions.
options: a `tf.distribute.experimental.CommunicationOptions`. See
`tf.distribute.experimental.CommunicationOptions` for details.

Returns:
A list of `tf.Tensor` or `tf.distribute.DistributedValues`, one per pair
in `value_destination_pairs`.

Raises:
ValueError: if `value_destination_pairs` is not an iterable of
tuples of `tf.distribute.DistributedValues` and destinations.


def elu(x, alpha=1.0):
    res = nn.elu(x)
    if alpha == 1:
        return res
    else:
        return array_ops.where_v2(x > 0, res, alpha * res)

Exponential linear unit.

Args:
x: A tensor or variable to compute the activation function for.
alpha: A scalar, slope of negative section.

Returns:
A tensor.


def get_resource(self, feature_column, resource_name):
    if feature_column not in self._cols_to_resources_map or resource_name not in self._cols_to_resources_map[feature_column]:
        raise ValueError('Resource does not exist.')
    return self._cols_to_resources_map[feature_column][resource_name]

Returns an already created resource.

Resources can be things such as tables, variables, trackables, etc.

Args:
feature_column: A `FeatureColumn` object this variable corresponds to.
resource_name: Name of the resource.


def get_program(self, label: str) -> moderngl.Program:
    return self._project.get_program(label)

Get a program by its label

Args:
label (str): The label for the program

Returns: py:class:`moderngl.Program` instance


def _subdivide_nodes(nodes):
    (_, num_nodes) = np.shape(nodes)
    if (num_nodes == 2):
        left_nodes = _helpers.matrix_product(nodes, _LINEAR_SUBDIVIDE_LEFT)
        right_nodes = _helpers.matrix_product(nodes, _LINEAR_SUBDIVIDE_RIGHT)
    elif (num_nodes == 3):
        left_nodes = _helpers.matrix_product(nodes, _QUADRATIC_SUBDIVIDE_LEFT)
        right_nodes = _helpers.matrix_product(nodes, _QUADRATIC_SUBDIVIDE_RIGHT)
    elif (num_nodes == 4):
        left_nodes = _helpers.matrix_product(nodes, _CUBIC_SUBDIVIDE_LEFT)
        right_nodes = _helpers.matrix_product(nodes, _CUBIC_SUBDIVIDE_RIGHT)
    else:
        (left_mat, right_mat) = make_subdivision_matrices((num_nodes - 1))
        left_nodes = _helpers.matrix_product(nodes, left_mat)
        right_nodes = _helpers.matrix_product(nodes, right_mat)
    return (left_nodes, right_nodes)

Subdivide a curve into two sub-curves.

Does so by taking the unit interval (i.e. the domain of the surface) and
splitting it into two sub-intervals by splitting down the middle.

.. note::

There is also a Fortran implementation of this function, which
will be used if it can be built.

Args:
nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve.

Returns:
Tuple[numpy.ndarray, numpy.ndarray]: The nodes for the two sub-curves.


def get_axes_layout(self, figure_list):

        axes_list = []
        if self._plot_refresh is True:
            for fig in figure_list:
                fig.clf()
                axes_list.append(fig.add_subplot(111))

        else:
            for fig in figure_list:
                axes_list.append(fig.axes[0])

        return axes_list

returns the axes objects the script needs to plot its data
the default creates a single axes object on each figure
This can/should be overwritten in a child script if more axes objects are needed
Args:
figure_list: a list of figure objects
Returns:
axes_list: a list of axes objects


def ProcessUnusedLoopExits(self, pending_count, to_ops_set):
    loop_exits = []
    for grad_state in self._map.values():
        for y in grad_state.forward_loop_exits:
            if pending_count[y.op] == 0:
                grad_state.pending_exits_count -= 1
                if y.op not in to_ops_set:
                    grad_state.unused_exits.append(y)
                if grad_state.pending_exits_count == 0:
                    loop_exits.extend(grad_state.unused_exits)
        for y in grad_state.forward_context.loop_enters:
            if pending_count[y.op] == 0:
                pending_count[y.op] = 1
    return loop_exits

Process all the "unused" loop exits.

The "unused" exits of the loops are added to `unused_exits`. An exit is
unused if its pending_count is 0. If there is an exit with real gradient,
all these deferred exits will enter the backprop loop with zero gradient.
Otherwise, they will enter the backprop loop with None. As an example,
people often write:

```python
v1, _ = tf.while_loop(p, b, [x1, x2])
result = gradients(v1, x1)
```

The exit node for x2 is not included by the betweenness analysis. But we
need to backprop x2 if x2 is involved in computing v1.

Args:
pending_count: The number of backprop inputs for every op.
to_ops_set: The set of ops for ys in gradients(ys, xs)

Returns:
The set of unused loop exits that we know at this point we need
to backprop.


def maybe_sandbox_call(func: Callable[..., Any], *args, sandbox: Optional[bool]=None, timeout: Optional[float]=None, **kwargs) -> Any:
    if sandbox is None:
        try:
            return sandbox_call(func, *args, timeout=timeout, **kwargs)
        except errors.SerializationError:
            return func(*args, **kwargs)
    elif sandbox:
        return sandbox_call(func, *args, timeout=timeout, **kwargs)
    else:
        return func(*args, **kwargs)

Maybe calls a function with sandboxing.

Args:
func: Function to call.
*args: Postional args that will be passed to `func`.
sandbox: If True, run code in sandbox; If False, run code in current
process. If None, run in sandbox first, if the output could not be
serialized and pass to current process, run the code again in current
process.
timeout: Execution timeout in seconds. If None, wait the code the complete.
**kwargs: Keyword args that will be passed to `func`.

Returns:
The return value of `func`.

Raises:
TimeoutError: If the execution time exceeds the timeout.
Exception: Exception  that are raised from `func`.


def keyword(self, **kwargs):
    path = self._get_path('keyword')
    response = self._GET(path, kwargs)
    self._set_attrs_to_values(response)
    return response

Search for keywords by name.

Args:
query: CGI escpaed string.
page: (optional) Minimum value of 1. Expected value is an integer.

Returns:
A dict respresentation of the JSON returned from the API.


def _from_proto_sparse_tensor(sparse_tensor_proto, process_leafs):
    if (not sparse_tensor_proto.HasField('named_tuple')):
        raise base_errors.ModuleInfoError('Error while deserializing a SparseTensor: expected proto tuple.')
    if (sparse_tensor_proto.named_tuple.name != _SPARSE_TENSOR_NAME):
        raise base_errors.ModuleInfoError('Error while deserializing a SparseTensor: The name of the tuple should have been {} but was {}.'.format(_SPARSE_TENSOR_NAME, sparse_tensor_proto.named_tuple.name))
    named_tuple_map = sparse_tensor_proto.named_tuple.map
    return tf.SparseTensor(indices=process_leafs(named_tuple_map['indices'].value), values=process_leafs(named_tuple_map['values'].value), dense_shape=process_leafs(named_tuple_map['dense_shape'].value))

Deserializes a `tf.SparseTensor` from `sparse_tensor_proto`.

Args:
sparse_tensor_proto: A proto representing a `tf.SparseTensor`.
process_leafs: A function to be applied to the leaf valued of the nested
structure.

Returns:
An instance of `tf.SparseTensor`.


def set_pattern_additional_cycles(self, patternnumber, value):
    _checkPatternNumber(patternnumber)
    minimalmodbus._checkInt(value, minvalue=0, maxvalue=99, description='number of additional cycles')
    address = _calculateRegisterAddress('cycles', patternnumber)
    self.write_register(address, value, 0)

Set the number of additional cycles for a given pattern.

Args:
* patternnumber (integer): 0-7
* value (integer): 0-99


def scale_gaussian_prior(name, z, logscale_factor=3.0, trainable=True):

  with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
    z_shape = common_layers.shape_list(z)
    latent_multiplier = tf.get_variable(
        "latent_multiplier", shape=z_shape, dtype=tf.float32,
        initializer=tf.ones_initializer())
    log_scale = tf.get_variable(
        "log_scale_latent", shape=z_shape, dtype=tf.float32,
        initializer=tf.zeros_initializer(), trainable=trainable)
    log_scale = log_scale * logscale_factor
    return tfp.distributions.Normal(
        loc=latent_multiplier * z, scale=tf.exp(log_scale))

Returns N(s^i * z^i, std^i) where s^i and std^i are pre-component.

s^i is a learnable parameter with identity initialization.
std^i is optionally learnable with identity initialization.

Args:
name: variable scope.
z: input_tensor
logscale_factor: equivalent to scaling up the learning_rate by a factor
of logscale_factor.
trainable: Whether or not std^i is learnt.


def datetime_from_isoformat(value: str):

    if sys.version_info >= (3, 7):
        return datetime.fromisoformat(value)

    return datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f')

Return a datetime object from an isoformat string.

Args:
value (str): Datetime string in isoformat.


def _ReadMemberHeader(self, file_object):

    file_offset = file_object.get_offset()
    member_header = self._ReadStructure(
        file_object, file_offset, self._MEMBER_HEADER_SIZE,
        self._MEMBER_HEADER, 'member header')

    if member_header.signature != self._GZIP_SIGNATURE:
      raise errors.FileFormatError(
          'Unsupported signature: 0x{0:04x}.'.format(member_header.signature))

    if member_header.compression_method != self._COMPRESSION_METHOD_DEFLATE:
      raise errors.FileFormatError(
          'Unsupported compression method: {0:d}.'.format(
              member_header.compression_method))

    self.modification_time = member_header.modification_time
    self.operating_system = member_header.operating_system

    if member_header.flags & self._FLAG_FEXTRA:
      file_offset = file_object.get_offset()
      extra_field_data_size = self._ReadStructure(
          file_object, file_offset, self._UINT16LE_SIZE,
          self._UINT16LE, 'extra field data size')

      file_object.seek(extra_field_data_size, os.SEEK_CUR)

    if member_header.flags & self._FLAG_FNAME:
      file_offset = file_object.get_offset()
      string_value = self._ReadString(
          file_object, file_offset, self._CSTRING, 'original filename')

      self.original_filename = string_value.rstrip('\x00')

    if member_header.flags & self._FLAG_FCOMMENT:
      file_offset = file_object.get_offset()
      string_value = self._ReadString(
          file_object, file_offset, self._CSTRING, 'comment')

      self.comment = string_value.rstrip('\x00')

    if member_header.flags & self._FLAG_FHCRC:
      file_object.read(2)

Reads a member header.

Args:
file_object (FileIO): file-like object to read from.

Raises:
FileFormatError: if the member header cannot be read.


def add_key_value(self, key, value):

        key = self._metadata_map.get(key, key)
        if key in ['dateAdded', 'lastModified']:
            self._indicator_data[key] = self._utils.format_datetime(
                value, date_format='%Y-%m-%dT%H:%M:%SZ'
            )
        elif key == 'confidence':
            self._indicator_data[key] = int(value)
        elif key == 'rating':
            self._indicator_data[key] = float(value)
        else:
            self._indicator_data[key] = value

Add custom field to Indicator object.

.. note:: The key must be the exact name required by the batch schema.

Example::

file_hash = tcex.batch.file('File', '1d878cdc391461e392678ba3fc9f6f32')
file_hash.add_key_value('size', '1024')

Args:
key (str): The field key to add to the JSON batch data.
value (str): The field value to add to the JSON batch data.


def create(self, friendly_name=None, description=None):

    if not self.exists():
      try:
        response = self._api.datasets_insert(self._name_parts,
                                             friendly_name=friendly_name,
                                             description=description)
      except Exception as e:
        raise e
      if 'selfLink' not in response:
        raise Exception("Could not create dataset %s" % self._full_name)
    return self

Creates the Dataset with the specified friendly name and description.

Args:
friendly_name: (optional) the friendly name for the dataset if it is being created.
description: (optional) a description for the dataset if it is being created.
Returns:
The Dataset.
Raises:
Exception if the Dataset could not be created.


def query(self, *, time: Timestamp, duration: Union[(Duration, timedelta)]=Duration(), qubits: Iterable[Qid]=None, include_query_end_time=False, include_op_end_times=False) -> List[ScheduledOperation]:
    duration = Duration.create(duration)
    earliest_time = (time - self._max_duration)
    end_time = (time + duration)
    qubits = (None if (qubits is None) else frozenset(qubits))

    def overlaps_interval(op):
        if ((not include_op_end_times) and ((op.time + op.duration) == time)):
            return False
        if ((not include_query_end_time) and (op.time == end_time)):
            return False
        return (((op.time + op.duration) >= time) and (op.time <= end_time))

    def overlaps_qubits(op):
        if (qubits is None):
            return True
        return (not qubits.isdisjoint(op.operation.qubits))
    potential_matches = self.scheduled_operations.irange_key(earliest_time, end_time)
    return [op for op in potential_matches if (overlaps_interval(op) and overlaps_qubits(op))]

Finds operations by time and qubit.

Args:
time: Operations must end after this time to be returned.
duration: Operations must start by time+duration to be
returned.
qubits: If specified, only operations touching one of the included
qubits will be returned.
include_query_end_time: Determines if the query interval includes
its end time. Defaults to no.
include_op_end_times: Determines if the scheduled operation
intervals include their end times or not. Defaults to no.

Returns:
A list of scheduled operations meeting the specified conditions.


def run_tests(tests):
    jobs = []
    for filename, script in tests:
        parameters = '%s%s' % (HARNESS_DIRECTORY, filename)
        command = ['python', '%s/starthinker/tool/recipe.py' % UI_ROOT, '%s%s' % (TEST_DIRECTORY, filename), '-u $STARTHINKER_USER', '-s $STARTHINKER_SERVICE', '-c $STARTHINKER_CLIENT', '-p $STARTHINKER_PROJECT', '-i %s' % INCLUDES_DIRECTORY, '--verbose', '--force']
        if os.path.isfile(parameters):
            command.insert(6, '-pi "%s"' % parameters)
        print('LAUNCHED:', ' '.join(command))
        jobs.append({'recipe': filename, 'output': '', 'errors': '', 'process': subprocess.Popen(' '.join(command), shell=True, cwd=UI_ROOT, stdout=subprocess.PIPE, stderr=subprocess.PIPE)})
        make_non_blocking(jobs[-1]['process'].stdout)
        make_non_blocking(jobs[-1]['process'].stderr)
    i = len(jobs)
    while i:
        print('.', end='', flush=True)
        sleep(10)
        i = i - 1
        stdout = jobs[i]['process'].stdout.read()
        if stdout is not None:
            jobs[i]['output'] += stdout.decode()
        stderr = jobs[i]['process'].stderr.read()
        if stderr is not None:
            jobs[i]['errors'] += stderr.decode()
        poll = jobs[i]['process'].poll()
        if poll is not None:
            job = jobs.pop(i)
            print('\nOK:' if poll == 0 else '\nFAILED:', job['recipe'], 'REMAINING:', len(jobs), [j['recipe'].replace('.json', '') for j in jobs])
            with open(LOG_DIRECTORY + ('OK_' if poll == 0 else 'FAILED_') + job['recipe'].replace('.json', '.log'), 'w') as f:
                f.write(job['output'])
                f.write(job['errors'])
        if i == 0:
            i = len(jobs)
    print('')
    print('------')
    print('------------')
    print('------------------------')
    print('TEST RESULTS: ls -1 %s*.log' % LOG_DIRECTORY)
    print('------------------------')
    print('------------')
    print('------')
    print('')

Run tests tests/*.json and parameters starthinker_assets/tests/*.json.

Each test will execute in a unique process and allowing parallel execution.
Each process is monitored for exit status.

If status is OK, all tasks OK, writes to starthinker_assets/logs/OK_*.json.
If status is ERROR, any task ERROR, writes to starthinker_assets/logs/OK_*.json.

Args:
test: List of (filename, json) pairs containing test definitions.

Returns:
Writes data to starthinker_assets/logs/ as [FAILED][OK]_test_name.log.
The files contain all STDOUT and STDERR output from each task.


def ParseLines(self, input_lines):

    current_macro = None
    for line in input_lines:
      if line.startswith('PDDM-'):
        directive = line.split(' ', 1)[0]
        if directive == 'PDDM-DEFINE':
          name, args = self._ParseDefineLine(line)
          if self._macros.get(name):
            raise PDDMError('Attempt to redefine macro: "%s"' % line)
          current_macro = self.MacroDefinition(name, args)
          self._macros[name] = current_macro
          continue
        if directive == 'PDDM-DEFINE-END':
          if not current_macro:
            raise PDDMError('Got DEFINE-END directive without an active macro:'
                            ' "%s"' % line)
          current_macro = None
          continue
        raise PDDMError('Hit a line with an unknown directive: "%s"' % line)

      if current_macro:
        current_macro.AppendLine(line)
        continue

      if line.strip() == '':
        continue

      raise PDDMError('Hit a line that wasn\'t a directive and no open macro'
                      ' definition: "%s"' % line)

Parses list of lines.

Args:
input_lines: A list of strings of input to parse (no newlines on the
strings).

Raises:
PDDMError if there are any issues.


def result(self):
    if self.exception():
        raise self.exception()

    @contextlib.contextmanager
    def f():
        try:
            (yield self._wrapped.result())
        finally:
            self._exit_callback()
    return f()

The result method which returns a context manager

Returns:
ContextManager: The corresponding context manager


def run(argv=None):
    parser = argparse.ArgumentParser()
    parser.add_argument('--input', dest='input', default='gs:
    parser.add_argument('--output', required=True, help='Output file to write results to.')
    parser.add_argument('--sampling_threshold', type=float, default=0.1, help='Fraction of entries used for session tracking')
    known_args, pipeline_args = parser.parse_known_args(argv)
    pipeline_options = PipelineOptions(pipeline_args)
    pipeline_options.view_as(SetupOptions).save_main_session = True
    with beam.Pipeline(options=pipeline_options) as p:
        p | ReadFromText(known_args.input) | ComputeTopSessions(known_args.sampling_threshold) | WriteToText(known_args.output)

Runs the Wikipedia top edits pipeline.

Args:
argv: Pipeline options as a list of arguments.


def transform_feature(self, transformation_cache, state_manager):
    input_tensor = transformation_cache.get(self.key, state_manager)
    return self._transform_input_tensor(input_tensor)

See `FeatureColumn` base class.

In this case, we apply the `normalizer_fn` to the input tensor.

Args:
transformation_cache: A `FeatureTransformationCache` object to access
features.
state_manager: A `StateManager` to create / access resources such as
lookup tables.

Returns:
Normalized input tensor.
Raises:
ValueError: If a SparseTensor is passed in.


def browse(self, path=None):

        params = None
        if path:
            assert isinstance(path, string_types)
            params = {'current': path}
        return self.get('browse', params=params)

Returns a list of directories matching the path given.

Args:
path (str): glob pattern.

Returns:
List[str]


def query(cls, sql, params=None, param_types=None):
    if params:
        assert param_types is not None
    return cls(is_sql=True, is_table=False, read_operation='process_query_batch', kwargs={'sql': sql, 'params': params, 'param_types': param_types})

A convenient method to construct ReadOperation from sql query.

Args:
sql: SQL query statement
params: (optional) values for parameter replacement. Keys must match the
names used in sql
param_types: (optional) maps explicit types for one or more param values;
required if parameters are passed.


def _LoadDataIntoCache(
      self, file_object, minimum_offset, read_all_data=False):

    if minimum_offset < self._decompressor_state.uncompressed_offset:
      self._ResetDecompressorState()

    while not self.IsCacheFull() or read_all_data:
      decompressed_data = self._decompressor_state.Read(file_object)

      if not decompressed_data:
        break

      decompressed_data_length = len(decompressed_data)
      decompressed_end_offset = self._decompressor_state.uncompressed_offset
      decompressed_start_offset = (
          decompressed_end_offset - decompressed_data_length)

      data_to_add = decompressed_data
      added_data_start_offset = decompressed_start_offset

      if decompressed_start_offset < minimum_offset:
        data_to_add = None

      if decompressed_start_offset < minimum_offset < decompressed_end_offset:
        data_add_offset = decompressed_end_offset - minimum_offset
        data_to_add = decompressed_data[-data_add_offset]
        added_data_start_offset = decompressed_end_offset - data_add_offset

      if not self.IsCacheFull() and data_to_add:
        self._cache = b''.join([self._cache, data_to_add])
        if self._cache_start_offset is None:
          self._cache_start_offset = added_data_start_offset
        if self._cache_end_offset is None:
          self._cache_end_offset = self._cache_start_offset + len(data_to_add)
        else:
          self._cache_end_offset += len(data_to_add)

      unused_data = self._decompressor_state.GetUnusedData()
      if unused_data:
        seek_offset = -len(unused_data)
        file_object.seek(seek_offset, os.SEEK_CUR)
        self._ResetDecompressorState()
        break

Reads and decompresses the data in the member.

This function already loads as much data as possible in the cache, up to
UNCOMPRESSED_DATA_CACHE_SIZE bytes.

Args:
file_object (FileIO): file-like object.
minimum_offset (int): offset into this member's uncompressed data at
which the cache should start.
read_all_data (bool): True if all the compressed data should be read
from the member.


def create_file_for(self, script):

        temp = tempfile.NamedTemporaryFile(
            prefix="pipeline-script-", mode='w+t', suffix=".sh", delete=False,
            dir=self.get_temporary_scripts_path())

        self.update_environment_variables(temp.name)
        rendered_script = render(script, model=self.config.model, env=self.env, item=self.config.item,
                                 variables=self.config.variables)
        if rendered_script is None:
            self.success = False
            temp.close()
            os.remove(temp.name)
            return None

        to_file_map = {2: lambda s: s.encode('utf-8'), 3: lambda s: s}

        if all(ord(ch) < 128 for ch in rendered_script) and os.path.isfile(rendered_script):
            with open(rendered_script) as handle:
                content = str(handle.read())
                temp.writelines(content)
        else:
            temp.write(u"
            temp.write(to_file_map[sys.version_info.major](rendered_script))
        temp.close()

        os.chmod(temp.name, 0o700)
        return temp.name

Create a temporary, executable bash file.

It also does render given script (string) with the model and
the provided environment variables and optional also an item
when using the B{with} field.

Args:
script (str): either pather and filename or Bash code.

Returns:
str: path and filename of a temporary file.


def multiply(self, other):

        if not isinstance(other, Number):
            raise QiskitError("other is not a number")

        if isinstance(other, complex) or other < 0:

            return Kraus(Choi(self).multiply(other))

        val = np.sqrt(other)
        kraus_r = None
        kraus_l = [val * k for k in self._data[0]]
        if self._data[1] is not None:
            kraus_r = [val * k for k in self._data[1]]
        return Kraus((kraus_l, kraus_r), self._input_dim, self._output_dim)

Return the QuantumChannel self + other.

Args:
other (complex): a complex number.

Returns:
Kraus: the scalar multiplication other * self as a Kraus object.

Raises:
QiskitError: if other is not a valid scalar.


def hwvtep_activate_hwvtep(self, **kwargs):

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

Activate the hwvtep

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

Returns:
Return value of `callback`.

Raises:
None


def _create_metric_extractor(metric_name):

    def extractor_fn(session_or_group):
        metric_value = _find_metric_value(session_or_group, metric_name)
        return (metric_value.value if metric_value else None)
    return extractor_fn

Returns function that extracts a metric from a session group or a session.

Args:
metric_name: tensorboard.hparams.MetricName protobuffer. Identifies the
metric to extract from the session group.
Returns:
A function that takes a tensorboard.hparams.SessionGroup or
tensorborad.hparams.Session protobuffer and returns the value of the metric
identified by 'metric_name' or None if the value doesn't exist.


def get_provider_uri(self, provider_display_name):
    providers = self._provider_client.get_by('displayName', provider_display_name)
    return (providers[0]['uri'] if providers else None)

Gets uri for a specific provider.

Args:
provider_display_name: Display name of the provider.

Returns:
uri