def validate(self):
    if self.value:
        if (not isinstance(self.value, bool)):
            raise TypeError('expected: {0}, observed: {1}'.format(bool, type(self.value)))

Verify that the value of the Boolean object is valid.

Raises:
TypeError: if the value is not of type bool.


def paint(self):
    snippet = {'heatmap-radius': VectorStyle.get_style_value(self.radius), 'heatmap-opacity': VectorStyle.get_style_value(self.opacity), 'heatmap-color': VectorStyle.get_style_value(self.color), 'heatmap-intensity': VectorStyle.get_style_value(self.intensity), 'heatmap-weight': VectorStyle.get_style_value(self.weight)}
    return snippet

Renders a javascript snippet suitable for use as a mapbox-gl heatmap paint entry

Returns:
A dict that can be converted to a mapbox-gl javascript paint snippet


def image_size_to_num_patches(image_size, grid_pinpoints, patch_size: int):
    if not isinstance(grid_pinpoints, list):
        raise TypeError('grid_pinpoints should be a list of tuples or lists')
    if not isinstance(image_size, (list, tuple)):
        if not isinstance(image_size, (torch.Tensor, np.ndarray)):
            raise TypeError(f'image_size invalid type {type(image_size)} with value {image_size}')
        image_size = image_size.tolist()
    best_resolution = select_best_resolution(image_size, grid_pinpoints)
    height, width = best_resolution
    num_patches = 0
    for i in range(0, height, patch_size):
        for j in range(0, width, patch_size):
            num_patches += 1
    num_patches += 1
    return num_patches

Calculate the number of patches after the preprocessing for images of any resolution.

Args:
image_size (`torch.LongTensor` or `np.ndarray` or `Tuple[int, int]`):
The size of the input image in the format (height, width). ?
grid_pinpoints (`List`):
A list containing possible resolutions. Each item in the list should be a tuple or list
of the form `(height, width)`.
patch_size (`int`):
The size of each image patch.

Returns:
int: the number of patches


def assert_count_equal(first, second, msg=None, extras=None):
    _call_unittest_assertion(_pyunit_proxy.assertCountEqual, first, second, msg=msg, extras=extras)

Asserts that two iterables have the same elements, the same number of
times, without regard to order.

Similar to assert_equal(Counter(list(first)), Counter(list(second))).

Args:
first: The first iterable to compare.
second: The second iterable to compare.
msg: A string that adds additional info about the failure.
extras: An optional field for extra information to be included in
test result.

Example:
assert_count_equal([0, 1, 1], [1, 0, 1]) passes the assertion.
assert_count_equal([0, 0, 1], [0, 1]) raises an assertion error.


def decode_schedule(string):
    splits = string.split()
    steps = [int(x[1:]) for x in splits[1:] if (x[0] == '@')]
    pmfs = np.reshape([float(x) for x in splits[1:] if (x[0] != '@')], [len(steps), (- 1)])
    return (splits[0], tuplize(steps), tuplize(pmfs))

Decodes a string into a schedule tuple.

Args:
string: The string encoding of a schedule tuple.

Returns:
A schedule tuple, see encode_schedule for details.


def OverwriteAndClose(self, compressed_data, size):
    self.Set(self.Schema.CONTENT(compressed_data))
    self.Set(self.Schema.SIZE(size))
    super(AFF4MemoryStreamBase, self).Close()

Directly overwrite the current contents.

Replaces the data currently in the stream with compressed_data,
and closes the object. Makes it possible to avoid recompressing
the data.
Args:
compressed_data: The data to write, must be zlib compressed.
size: The uncompressed size of the data.


def _wait_for_response(self, requests):
    failed_requests = []
    responses_for_requests = OrderedDict.fromkeys(requests)
    for retry in range(self._max_retry):
        try:
            logging.debug('Try
            self._availability_limiter.map_with_retries(requests, responses_for_requests)
            failed_requests = []
            for (request, response) in responses_for_requests.items():
                if (self._drop_404s and (response is not None) and (response.status_code == 404)):
                    logging.warning('Request to {0} failed with status code 404, dropping.'.format(request.url))
                elif (not response):
                    failed_requests.append((request, response))
            if (not failed_requests):
                break
            logging.warning('Try
            requests = [fr[0] for fr in failed_requests]
        except InvalidRequestError:
            raise
        except Exception as e:
            logging.exception('Try
            pass
    if failed_requests:
        logging.warning('Still {0} failed request(s) after {1} retries:'.format(len(failed_requests), self._max_retry))
        for (failed_request, failed_response) in failed_requests:
            if (failed_response is not None):
                failed_response_text = failed_response.text.encode('ascii', 'xmlcharrefreplace')
                logging.warning('Request to {0} failed with status code {1}. Response text: {2}'.format(failed_request.url, failed_response.status_code, failed_response_text))
            else:
                logging.warning('Request to {0} failed with None response.'.format(failed_request.url))
    return list(responses_for_requests.values())

Issues a batch of requests and waits for the responses.
If some of the requests fail it will retry the failed ones up to `_max_retry` times.

Args:
requests - A list of requests
Returns:
A list of `requests.models.Response` objects
Raises:
InvalidRequestError - if any of the requests returns "403 Forbidden" response


def trace_function(args=None, kwargs=None, tracing_options=None):
    if not tracing_options:
        tracing_options = TracingOptions()
    args = args if args else ()
    kwargs = kwargs if kwargs else {}
    if tracing_options.input_signature and (args or kwargs):
        bound_args = function_type_utils.bind_function_inputs(args, kwargs, tracing_options.polymorphic_type, tracing_options.default_values)
        args, kwargs = (bound_args.args, bound_args.kwargs)
    with tracing_options.lock or contextlib.nullcontext():
        if tracing_options.input_signature and (not args) and (not kwargs):
            args = tracing_options.input_signature
            kwargs = {}
        concrete_function = _maybe_define_function(args, kwargs, tracing_options)
    if not tracing_options.bind_graph_to_function:
        concrete_function._garbage_collector.release()
    return concrete_function

Returns a `ConcreteFunction` specialized to inputs and execution context.

Compiles a Graph corresponding to the Python function logic and uses that
to generate a differentiable ConcreteFunction.

Args:
args: inputs to specialize on. Can be concrete values (e.g. 1) or
`tf.Tensor` or `tf.TensorSpec`.
kwargs: keyword inputs to specialize on. Concrete values (e.g. 1) or
`tf.Tensor` or `tf.TensorSpec`.
tracing_options: TracingOptions for the tracing process.


def format_datetime(self, time_input, tz=None, date_format=None):
    dt_value = self.any_to_datetime(time_input, tz)
    if (date_format == '%s'):
        dt_value = calendar.timegm(dt_value.timetuple())
    elif date_format:
        dt_value = dt_value.strftime(date_format)
    else:
        dt_value = dt_value.isoformat()
    return dt_value

Return timestamp from multiple input formats.

Formats:

#. Human Input (e.g 30 days ago, last friday)
#. ISO 8601 (e.g. 2017-11-08T16:52:42Z)
#. Loose Date format (e.g. 2017 12 25)
#. Unix Time/Posix Time/Epoch Time (e.g. 1510686617 or 1510686617.298753)

.. note:: To get a unix timestamp format use the strftime format **%s**. Python
does not natively support **%s**, however this method has support.

Args:
time_input (string): The time input string (see formats above).
tz (string): The time zone for the returned data.
date_format (string): The strftime format to use, ISO by default.

Returns:
(string): Formatted datetime string.


def GetUpdates(self, gcs_client, bucket_name, obj, since):
    bucket = gcs_client.bucket(bucket_name)
    blob = bucket.get_blob(obj)
    if blob is None:
        self.log.error('GCS object gs:
        raise error.SourceUnavailable('unable to download object from GCS.')
    if since and timestamps.FromDateTimeToTimestamp(blob.updated) < since:
        return []
    data_map = self.GetMap(cache_info=blob.open())
    data_map.SetModifyTimestamp(timestamps.FromDateTimeToTimestamp(blob.updated))
    return data_map

Gets updates from a source.

Args:
gcs_client: initialized gcs client
bucket_name: gcs bucket name
obj: object with the data
since: a timestamp representing the last change (None to force-get)

Returns:
A tuple containing the map of updates and a maximum timestamp


def device_function(self, var):

    if var.type not in ('Variable', 'VariableV2', 'VarHandleOp'):
      tf.logging.debug('Place {} on last device: {}.'.format(
          var.name, self._last_device))
      return self._last_device

    shape = tf.TensorShape(var.get_attr('shape'))
    assert shape.num_elements() is not None

    size = var.get_attr('dtype').size
    mem, device = heapq.heappop(self._mem_device_heap)
    mem += shape.num_elements() * size
    heapq.heappush(self._mem_device_heap, (mem, device))
    tf.logging.debug('Place variable {} on {} and consumes {} Bytes.'.format(
        var.name, device, mem))
    self._last_device = device

    return device

Choose a device for the input variable.

Args:
var: an Variable.

Returns:
The device for placing the var.


def _filter_headers(self):
    headers = {}
    for user in self.usernames:
        headers['fedora_messaging_user_{}'.format(user)] = True
    for package in self.packages:
        headers['fedora_messaging_rpm_{}'.format(package)] = True
    for container in self.containers:
        headers['fedora_messaging_container_{}'.format(container)] = True
    for module in self.modules:
        headers['fedora_messaging_module_{}'.format(module)] = True
    for flatpak in self.flatpaks:
        headers['fedora_messaging_flatpak_{}'.format(flatpak)] = True
    return headers

Add headers designed for filtering messages based on objects.

Returns:
dict: Filter-related headers to be combined with the existing headers


def parse_readable_size_str(size_str):
    size_str = size_str.strip()
    if size_str.endswith('B'):
        size_str = size_str[:-1]
    if size_str.isdigit():
        return int(size_str)
    elif size_str.endswith('k'):
        return int(float(size_str[:-1]) * 1024)
    elif size_str.endswith('M'):
        return int(float(size_str[:-1]) * 1048576)
    elif size_str.endswith('G'):
        return int(float(size_str[:-1]) * 1073741824)
    else:
        raise ValueError('Failed to parsed human-readable byte size str: "%s"' % size_str)

Convert a human-readable str representation to number of bytes.

Only the units "kB", "MB", "GB" are supported. The "B character at the end
of the input `str` may be omitted.

Args:
size_str: (`str`) A human-readable str representing a number of bytes
(e.g., "0", "1023", "1.1kB", "24 MB", "23GB", "100 G".

Returns:
(`int`) The parsed number of bytes.

Raises:
ValueError: on failure to parse the input `size_str`.


def _add_countriesdata(cls, iso3, country):

        countryname = country.get('
        cls._countriesdata['countrynames2iso3'][countryname.upper()] = iso3
        iso2 = country.get('
        if iso2:
            cls._countriesdata['iso2iso3'][iso2] = iso3

            cls._countriesdata['iso2iso3'][iso3] = iso2
        m49 = country.get('
        if m49:
            m49 = int(m49)
            cls._countriesdata['m49iso3'][m49] = iso3

            cls._countriesdata['m49iso3'][iso3] = m49
        cls._countriesdata['aliases'][iso3] = re.compile(country.get('
        regionname = country.get('
        sub_regionname = country.get('
        intermediate_regionname = country.get('
        regionid = country.get('
        if regionid:
            regionid = int(regionid)
        sub_regionid = country.get('
        if sub_regionid:
            sub_regionid = int(sub_regionid)
        intermediate_regionid = country.get('
        if intermediate_regionid:
            intermediate_regionid = int(intermediate_regionid)

        def add_country_to_set(colname, idval, iso3):
            value = cls._countriesdata[colname].get(idval)
            if value is None:
                value = set()
                cls._countriesdata['regioncodes2countries'][idval] = value
            value.add(iso3)

        if regionname:
            add_country_to_set('regioncodes2countries', regionid, iso3)
            cls._countriesdata['regioncodes2names'][regionid] = regionname
            cls._countriesdata['regionnames2codes'][regionname.upper()] = regionid
        if sub_regionname:
            add_country_to_set('regioncodes2countries', sub_regionid, iso3)
            cls._countriesdata['regioncodes2names'][sub_regionid] = sub_regionname
            cls._countriesdata['regionnames2codes'][sub_regionname.upper()] = sub_regionid
        if intermediate_regionname:
            add_country_to_set('regioncodes2countries', intermediate_regionid, iso3)
            cls._countriesdata['regioncodes2names'][intermediate_regionid] = intermediate_regionname
            cls._countriesdata['regionnames2codes'][intermediate_regionname.upper()] = \
                intermediate_regionid

Set up countries data from data in form provided by UNStats and World Bank

Args:
iso3 (str): ISO3 code for country
country (hxl.Row): Country information

Returns:
None


def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT):

        return KNNModel(self.model_data, self.role, sagemaker_session=self.sagemaker_session,
                        vpc_config=self.get_vpc_config(vpc_config_override))

Return a :class:`~sagemaker.amazon.KNNModel` referencing the latest
s3 model data produced by this Estimator.

Args:
vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model.
Default: use subnets and security groups from this Estimator.
* 'Subnets' (list[str]): List of subnet ids.
* 'SecurityGroupIds' (list[str]): List of security group ids.


def next_counter(start=0, step=1):
    r
    count_gen = it.count(start, step)
    next_ = functools.partial(six.next, count_gen)
    return next_

r"""
Args:
start (int): (default = 0)
step (int): (default = 1)

Returns:
func: next_

CommandLine:
python -m utool.util_iter --test-next_counter

Example:
>>> # ENABLE_DOCTEST
>>> from utool.util_iter import *  # NOQA
>>> start = 1
>>> step = 1
>>> next_ = next_counter(start, step)
>>> result = str([next_(), next_(), next_()])
>>> print(result)
[1, 2, 3]


def apply_and_name(self, aggregator):

        reduced_df = self._apply(aggregator)
        if len(self.names) != len(reduced_df.columns):
            raise IndexError("ColumnFunction creates more columns than it has names for.")
        reduced_df.columns = self.names
        return reduced_df

Fetches the row-aggregated input columns for this ColumnFunction.

Args:
aggregator (Aggregator)

Returns:
pd.DataFrame: The dataframe has columns with names self.names
that were created by this ColumnFunction,
and is indexed by the index that was passed to
aggregator.aggregate(index).


def __init__(self, structuring_element=None):

        if structuring_element is None:
            self.strel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
        else:
            self.strel = structuring_element
        self.fgbg = cv2.bgsegm.createBackgroundSubtractorGMG()

Initializes the `BackgroundSubtractorGMG`.

*Note:* Requires OpenCV to be built with `--contrib` as it uses the
`bgsegm` package.

Unless a custom `structuring_element` is specified, it uses:
`cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))`

Args:
structuring_element: The structuring element.


def _get_tensor_by_tf_output(self, tf_output) -> tensor_lib.Tensor:
    op = self._get_operation_by_tf_operation(tf_output.oper)
    return op.outputs[tf_output.index]

Returns the `Tensor` representing `tf_output`.

Note that there is only one such `Tensor`, i.e. multiple calls to this
function with the same TF_Output value will always return the same `Tensor`
object.

Args:
tf_output: A wrapped `TF_Output` (the C API equivalent of `Tensor`).

Returns:
The `Tensor` that represents `tf_output`.


def get_dG_at_T(seq, temp):
    r_cal = (scipy.constants.R / scipy.constants.calorie)
    seq = ssbio.protein.sequence.utils.cast_to_str(seq)
    oobatake = {}
    for t in range(20, 51):
        oobatake[t] = calculate_oobatake_dG(seq, t)
    stable = [i for i in oobatake.values() if (i > 0)]
    if (len(stable) == 0):
        dG = (0.238846 * calculate_dill_dG(len(seq), temp))
        method = 'Dill'
    else:
        dG = oobatake[temp]
        method = 'Oobatake'
    keq = math.exp((((- 1) * dG) / (r_cal * (temp + 273.15))))
    return (dG, keq, method)

Predict dG at temperature T, using best predictions from Dill or Oobatake methods.

Args:
seq (str, Seq, SeqRecord): Amino acid sequence
temp (float): Temperature in degrees C

Returns:
(tuple): tuple containing:

dG (float) Free energy of unfolding dG (cal/mol)
keq (float): Equilibrium constant Keq
method (str): Method used to calculate


def package_info(package, image=None):
    cmd = ['DISM', '/English', ('/Image:{0}'.format(image) if image else '/Online'), '/Get-PackageInfo']
    if ('~' in package):
        cmd.append('/PackageName:{0}'.format(package))
    else:
        cmd.append('/PackagePath:{0}'.format(package))
    out = __salt__['cmd.run_all'](cmd)
    if (out['retcode'] == 0):
        ret = dict()
        for line in six.text_type(out['stdout']).splitlines():
            if (' : ' in line):
                info = line.split(' : ')
                if (len(info) < 2):
                    continue
                ret[info[0]] = info[1]
    else:
        ret = out
    return ret

Display information about a package

Args:
package (str): The full path to the package. Can be either a .cab file
or a folder. Should point to the original source of the package, not
to where the file is installed. You cannot use this command to get
package information for .msu files
image (Optional[str]): The path to the root directory of an offline
Windows image. If `None` is passed, the running operating system is
targeted. Default is None.

Returns:
dict: A dictionary containing the results of the command

CLI Example:

.. code-block:: bash

salt '*' dism. package_info C:\\packages\\package.cab


def get_num_bytes(self, batch: Sequence[Union[tf.Tensor, torch.Tensor]]) -> int:
    if self._framework == 'tf':
        return sum((sys.getsizeof(element) for element in batch))
    else:
        return sum((el.element_size() for tensor in batch for el in tensor.values()))

Returns:
The number of bytes of data for the Tensors batch.


class DepthAnythingNeck(nn.Module):

    def __init__(self, config):
        super().__init__()
        self.config = config
        self.reassemble_stage = DepthAnythingReassembleStage(config)
        self.convs = nn.ModuleList()
        for channel in config.neck_hidden_sizes:
            self.convs.append(nn.Conv2d(channel, config.fusion_hidden_size, kernel_size=3, padding=1, bias=False))
        self.fusion_stage = DepthAnythingFeatureFusionStage(config)

    def forward(self, hidden_states: List[torch.Tensor], patch_height=None, patch_width=None) -> List[torch.Tensor]:

        if not isinstance(hidden_states, (tuple, list)):
            raise TypeError('hidden_states should be a tuple or list of tensors')
        if len(hidden_states) != len(self.config.neck_hidden_sizes):
            raise ValueError('The number of hidden states should be equal to the number of neck hidden sizes.')
        hidden_states = self.reassemble_stage(hidden_states, patch_height, patch_width)
        features = [self.convs[i](feature) for i, feature in enumerate(hidden_states)]
        output = self.fusion_stage(features)
        return output

DepthAnythingNeck. A neck is a module that is normally used between the backbone and the head. It takes a list of tensors as
input and produces another list of tensors as output. For DepthAnything, it includes 2 stages:

* DepthAnythingReassembleStage
* DepthAnythingFeatureFusionStage.

Args:
config (dict): config dict.


def generate_tree_path(fileDigest, depth):

        if(depth < 0):
            raise Exception("depth level can not be negative")
        if(os.path.split(fileDigest)[1] != fileDigest):
            raise Exception("fileDigest cannot contain path separator")

        min = (2**(depth + 1)) - 1
        if(len(fileDigest) < min):
            raise Exception("fileDigest too short for the given depth")

        path = ""
        index = 0
        for p in range(1, depth + 1):
            jump = 2**p
            path = os.path.join(path, fileDigest[index:index + jump])
            index += jump
        path = os.path.join(path, fileDigest[index:])
        return path

Generate a relative path from the given fileDigest
relative path has a numbers of directories levels according to @depth

Args:
fileDigest -- digest for which the relative path will be generate
depth -- number of levels to use in relative path generation
Returns:
relative path for the given digest


def launch_job(job_name, cmd=None,
               code_dir=None, excludes='*.ipynb .git .ipynb_checkpoints', dependencies=tuple(),
               queue='john', image='codalab/python', memory='18g',
               debug=False, tail=False):

    print 'Remember to set up SSH tunnel and LOG IN through the command line before calling this.'

    def execute(cmd):
        return shell(cmd, verbose=True, debug=debug)

    if code_dir:
        execute('cl up -n code -w {} {} -x {}'.format(worksheet, code_dir, excludes))

    options = '-v -n {} -w {} --request-queue {} --request-docker-image {} --request-memory {}'.format(
        job_name, worksheet, queue, image, memory)
    dep_str = ' '.join(['{0}:{0}'.format(dep) for dep in dependencies])
    cmd = "cl run {} {} '{}'".format(options, dep_str, cmd)
    if tail:
        cmd += ' -t'
    execute(cmd)

Launch a job on CodaLab (optionally upload code that the job depends on).

Args:
job_name: name of the job
cmd: command to execute
code_dir: path to code folder. If None, no code is uploaded.
excludes: file types to exclude from the upload
dependencies: list of other bundles that we depend on
debug: if True, prints SSH commands, but does not execute them
tail: show the streaming output returned by CodaLab once it launches the job


def __init__(self, channel):

        self.ListTraces = channel.unary_unary(
            "/google.devtools.cloudtrace.v1.TraceService/ListTraces",
            request_serializer=google_dot_devtools_dot_cloudtrace__v1_dot_proto_dot_trace__pb2.ListTracesRequest.SerializeToString,
            response_deserializer=google_dot_devtools_dot_cloudtrace__v1_dot_proto_dot_trace__pb2.ListTracesResponse.FromString,
        )
        self.GetTrace = channel.unary_unary(
            "/google.devtools.cloudtrace.v1.TraceService/GetTrace",
            request_serializer=google_dot_devtools_dot_cloudtrace__v1_dot_proto_dot_trace__pb2.GetTraceRequest.SerializeToString,
            response_deserializer=google_dot_devtools_dot_cloudtrace__v1_dot_proto_dot_trace__pb2.Trace.FromString,
        )
        self.PatchTraces = channel.unary_unary(
            "/google.devtools.cloudtrace.v1.TraceService/PatchTraces",
            request_serializer=google_dot_devtools_dot_cloudtrace__v1_dot_proto_dot_trace__pb2.PatchTracesRequest.SerializeToString,
            response_deserializer=google_dot_protobuf_dot_empty__pb2.Empty.FromString,
        )

Constructor.

Args:
channel: A grpc.Channel.


def __init__(self,
                 asynchronous_correlation_value=None,
                 cancellation_result=None):

        super(CancelResponsePayload, self).__init__(
            enums.Tags.RESPONSE_PAYLOAD
        )

        self._asynchronous_correlation_value = None
        self._cancellation_result = None

        self.asynchronous_correlation_value = asynchronous_correlation_value
        self.cancellation_result = cancellation_result

Construct a Cancel response payload struct.

Args:
asynchronous_correlation_value (bytes): The ID of a pending
operation that was cancelled, in bytes. Optional, defaults to
None.
cancellation_result (enum): A CancellationResult enumeration
specifying the result of canceling the operation. Optional,
defaults to None.


def should_close(http_version, connection_field):
    connection_field = (connection_field or '').lower()
    if (http_version == 'HTTP/1.0'):
        return (connection_field.replace('-', '') != 'keepalive')
    else:
        return (connection_field == 'close')

Return whether the connection should be closed.

Args:
http_version (str): The HTTP version string like ``HTTP/1.0``.
connection_field (str): The value for the ``Connection`` header.


def DummyMethod(name, *params):

    def make_param(param):
        return pytd.Parameter(param, type=pytd.AnythingType(), kind=pytd.ParameterKind.REGULAR, optional=False, mutated_type=None)
    sig = pytd.Signature(tuple((make_param(param) for param in params)), starargs=None, starstarargs=None, return_type=pytd.AnythingType(), exceptions=(), template=())
    return pytd.Function(name=name, signatures=(sig,), kind=pytd.MethodKind.METHOD, flags=pytd.MethodFlag.NONE)

Create a simple method using only "Any"s as types.

Arguments:
name: The name of the method
*params: The parameter names.

Returns:
A pytd.Function.


def save_pcoder(self, pcoder, *labels):
    raise NotImplementedError

Saves pcoder for given PCollection.

Correct reading of PCollection from Cache requires PCoder to be known.
This method saves desired PCoder for PCollection that will subsequently
be used by sink(...), source(...), and, most importantly, read(...) method.
The latter must be able to read a PCollection written by Beam using
non-Beam IO.

Args:
pcoder: A PCoder to be used for reading and writing a PCollection.
*labels: List of labels for PCollection instance.


def convert_avgpool(params, w_name, scope_name, inputs, layers, weights, names):
    print('Converting pooling ...')
    if (names == 'short'):
        tf_name = ('P' + random_string(7))
    elif (names == 'keep'):
        tf_name = w_name
    else:
        tf_name = (w_name + str(random.random()))
    if ('kernel_shape' in params):
        (height, width) = params['kernel_shape']
    else:
        (height, width) = params['kernel_size']
    if ('strides' in params):
        (stride_height, stride_width) = params['strides']
    else:
        (stride_height, stride_width) = params['stride']
    if ('pads' in params):
        (padding_h, padding_w, _, _) = params['pads']
    else:
        (padding_h, padding_w) = params['padding']
    input_name = inputs[0]
    pad = 'valid'
    if (((height % 2) == 1) and ((width % 2) == 1) and ((height
        pad = 'same'
    else:
        padding_name = (tf_name + '_pad')
        padding_layer = keras.layers.ZeroPadding2D(padding=(padding_h, padding_w), name=padding_name)
        layers[padding_name] = padding_layer(layers[inputs[0]])
        input_name = padding_name
    pooling = keras.layers.AveragePooling2D(pool_size=(height, width), strides=(stride_height, stride_width), padding=pad, name=tf_name, data_format='channels_first')
    layers[scope_name] = pooling(layers[input_name])

Convert Average pooling.

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


def log(self, message):
    self._buffer.append([datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.000%z'), message])
    if not self.buffered or (self._flush_threshold and len(self._buffer) >= self._flush_threshold):
        self.flush()

Logs a message to the Bulkdozer feed's Log tab.

Args:
message: The message to log to the feed, it will be appended at the bottom
of the log, after the last message that was written.


def GetAnalyzersInformation(cls):
    analyzer_information = []
    for (_, analyzer_class) in cls.GetAnalyzers():
        description = getattr(analyzer_class, 'DESCRIPTION', '')
        analyzer_information.append((analyzer_class.NAME, description))
    return analyzer_information

Retrieves the analyzers information.

Returns:
list[tuple]: containing:

str: analyzer name.
str: analyzer description.


def update_offset(self, new_offset):

        self.offset = new_offset
        self.data_points = self._data_points[self.offset:]
        self.timestamps = self._timestamps[self.offset:]

Updates how many data points to skip in caculations.

Always use this function to update offset instead of directly setting
self.offset.

Args:
new_offset: The new offset.


def make_path_writable(path):
    from rez.config import config
    try:
        orig_mode = os.stat(path).st_mode
        new_mode = orig_mode
        if (config.make_package_temporarily_writable and (not os.access(path, os.W_OK))):
            new_mode = (orig_mode | stat.S_IWUSR)
        if (new_mode != orig_mode):
            os.chmod(path, new_mode)
    except OSError:
        orig_mode = None
        new_mode = None
    try:
        (yield)
    finally:
        if (new_mode != orig_mode):
            os.chmod(path, orig_mode)

Temporarily make `path` writable, if possible.

Does nothing if:
- config setting 'make_package_temporarily_writable' is False;
- this can't be done (eg we don't own `path`).

Args:
path (str): Path to make temporarily writable


def write_int64(self, value, little_endian=True):

        if little_endian:
            endian = "<"
        else:
            endian = ">"
        return self.pack('%sq' % endian, value)

Pack the value as a signed integer and write 8 bytes to the stream.

Args:
value:
little_endian (bool): specify the endianness. (Default) Little endian.

Returns:
int: the number of bytes written.


def create_composite_loss(self,
                            losses,
                            regularize=True,
                            include_marked=True,
                            name='cost'):

    all_losses = []
    if losses:
      all_losses.extend(losses)
    if include_marked:
      all_losses.extend(self.marked_losses)
    if not all_losses:
      raise ValueError('No losses specified!')
    if regularize:
      all_losses.extend(self.regularization_losses)
    with self._g.as_default():
      result = tf.add_n(all_losses, name=name)
      self.add_scalar_summary(result)
      return result

Creates a loss that is the sum of all specified losses.

Args:
losses: A sequence of losses to include.
regularize: Whether or not to include regularization losses.
include_marked: Whether or not to use the marked losses.
name: The name for this variable.
Returns:
A single tensor that is the sum of all losses.
Raises:
ValueError: if there are no losses.


def _maybe_truncate_traceback(traceback):
    if len(traceback) > MAX_TRACEBACK_LENGTH:
        return traceback[:MAX_TRACEBACK_LENGTH - 2] + [_ELLIPSIS, traceback[-1]]
    else:
        return traceback

Truncate the traceback if it is too long.

Args:
traceback: A list representing an error's traceback. There should be one
list item per entry in the traceback (in the right order); beyond that,
this function does not care about the item types.

Returns:
The traceback, possibly with some items removed and an _ELLIPSIS inserted.
Guaranteed to be no longer than MAX_TRACEBACK_LENGTH.


def json(self, data):

        self._headers['Content-Type'] = 'application/json'
        if not isinstance(data, str):
            data = json.dumps(data, indent=4)
        self._body = data

Defines the mock response JSON body.

Arguments:
data (dict|list|str): JSON body data.

Returns:
self: ``pook.Response`` current instance.


def sort_edge(edges):

        return sorted(edges, key=lambda x: (x.L, x.R))

Sort iterable of edges first by left node indices then right.

Args:
edges(list[Edge]): List of edges to be sorted.

Returns:
list[Edge]: Sorted list by left and right node indices.


def preprocess_JPEG(self, image, **kwargs):
    save_kwargs = {'progressive': VERSATILEIMAGEFIELD_PROGRESSIVE_JPEG, 'quality': QUAL}
    if (image.mode != 'RGB'):
        image = image.convert('RGB')
    return (image, save_kwargs)

Receive a PIL Image instance of a JPEG and returns 2-tuple.

Args:
* [0]: Image instance, converted to RGB
* [1]: Dict with a quality key (mapped to the value of `QUAL` as
defined by the `VERSATILEIMAGEFIELD_JPEG_RESIZE_QUALITY`
setting)


def _GetEarliestYearFromFileEntry(self):
    file_entry = self.GetFileEntry()
    if (not file_entry):
        return None
    stat_object = file_entry.GetStat()
    posix_time = getattr(stat_object, 'crtime', None)
    if (posix_time is None):
        posix_time = getattr(stat_object, 'ctime', None)
    if (file_entry.TYPE_INDICATOR == dfvfs_definitions.TYPE_INDICATOR_GZIP):
        posix_time = getattr(stat_object, 'mtime', None)
    if (posix_time is None):
        logger.warning('Unable to determine earliest year from file stat information.')
        return None
    try:
        year = timelib.GetYearFromPosixTime(posix_time, timezone=self._knowledge_base.timezone)
        return year
    except ValueError as exception:
        logger.error('Unable to determine earliest year from file stat information with error: {0!s}'.format(exception))
        return None

Retrieves the year from the file entry date and time values.

This function uses the creation time if available otherwise the change
time (metadata last modification time) is used.

Returns:
int: year of the file entry or None.


def summary(self, title, sentences=0, chars=0, auto_suggest=True, redirect=True):
    page_info = self.page(title, auto_suggest=auto_suggest, redirect=redirect)
    return page_info.summarize(sentences, chars)

Get the summary for the title in question

Args:
title (str): Page title to summarize
sentences (int): Number of sentences to return in summary
chars (int): Number of characters to return in summary
auto_suggest (bool): Run auto-suggest on title before \
summarizing
redirect (bool): Use page redirect on title before summarizing
Returns:
str: The summarized results of the page
Note:
Precedence for parameters: sentences then chars; if both are \
0 then the entire first section is returned


def observations_np(self, boundary=20):
    list_observations_np_ts = [t.observations_np for t in self.trajectories]
    OBS = list_observations_np_ts[0].shape[1:]
    num_time_steps = [t.num_time_steps for t in self.trajectories]
    t_max = max(num_time_steps)
    boundary = int(boundary)
    bucket_length = (boundary * int(np.ceil((float(t_max) / boundary))))

    def padding_config(obs):
        num_to_pad = ((bucket_length + 1) - obs.shape[0])
        return ([(0, num_to_pad)] + ([(0, 0)] * len(OBS)))
    return (np.stack([np.pad(obs, padding_config(obs), 'constant') for obs in list_observations_np_ts]), num_time_steps)

Pads the observations in all the trajectories and returns them.

Args:
boundary: integer, Observations will be padded to (n * boundary) + 1 where
n is an integer.

Returns:
a tuple(padded_observations, time_steps), with shapes:
padded_observations: (self.batch_size, n * boundary + 1) + OBS
time_steps: integer list of length = self.batch_size


def __str__(self):
    info = {'section': self._section, 'config': self.config, 'req_type': self._req_type, 'req': str(self.req), 'range': str(self.range), 'exclude': str(self.exclude), 'include': str(self.include), 'init': str(self._initialized)}
    req_str = '\n >>> _Reqs Instance <<<\n'
    req_str += 'Section: {section}\n'
    req_str += 'Configuration name: {config}\n'
    req_str += 'Requirement type: {req_type}\n'
    req_str += 'Requirement: {req}\n'
    req_str += 'Range: {range}\n'
    req_str += 'Exclude: {exclude}\n'
    req_str += 'Include: {include}\n'
    req_str += 'Initialized: {init}\n\n'
    return req_str.format(**info)

Prints a requirement and its components.

Returns:
String that has concatenated information about a requirement.


def random_shuffle(value, seed=None, name=None):
    with ops.name_scope(name, 'shuffle', [value, seed]):
        if value.rank == 0:
            raise ValueError('Cannot shuffle a scalar StructuredTensor')
        first_dimension = value.nrows()
        index = random_ops.random_shuffle(math_ops.range(first_dimension), seed=seed)
        return gather(value, index, axis=0)

Shuffle a structured tensor on the zeroth axis.

Args:
value: a structured tensor of rank at least one.
seed: the seed for shuffling.
name: the name for shuffle.

Returns:
The shuffled structured tensor.


def invitation_backend(backend=None, namespace=None):
    backend = (backend or ORGS_INVITATION_BACKEND)
    (class_module, class_name) = backend.rsplit('.', 1)
    mod = import_module(class_module)
    return getattr(mod, class_name)(namespace=namespace)

Returns a specified invitation backend

Args:
backend: dotted path to the invitation backend class
namespace: URL namespace to use

Returns:
an instance of an InvitationBackend


def getFingerprintsForExpressions(self, body, sparsity=1.0):

        return self._expressions.resolveBulkExpression(self._retina, body, sparsity)

Bulk resolution of expressions
Args:
body, ExpressionOperation: The JSON encoded expression to be evaluated (required)
sparsity, float: Sparsify the resulting expression to this percentage (optional)
Returns:
list of Fingerprint
Raises:
CorticalioException: if the request was not successful


def get_tokens(max_value):

  vocab = [str(i) for i in range(max_value)]
  vocab = set(vocab)
  vocab.update(CodeOp.LITERALS)
  vocab.update(CodeOp.KEYWORDS)
  vocab |= set("".join(vocab))
  return sorted(vocab)

Defines tokens.

Args:
max_value: the maximum numeric range for the token.

Returns:
list of string tokens in vocabulary.


def dismiss_confirm(self, text=None, wait=None):

        with self.driver.dismiss_modal("confirm", text=text, wait=wait):
            yield

Execute the wrapped code, dismissing a confirm.

Args:
text (str | RegexObject, optional): Text to match against the text in the modal.
wait (int | float, optional): Maximum time to wait for the modal to appear after
executing the wrapped code.

Raises:
ModalNotFound: If a modal dialog hasn't been found.


def fit_size_distribution_models(self, model_names, model_objs, input_columns,
                                     output_columns=None, calibrate=False):

        if output_columns is None:
            output_columns = ["Shape", "Location", "Scale"]
        groups = np.unique(self.data["train"]["member"][self.group_col])

        weights=None

        for group in groups:
            group_data = self.data["train"]["combo"].loc[self.data["train"]["combo"][self.group_col] == group]
            group_data = group_data.dropna()
            group_data = group_data[group_data[output_columns[-1]] > 0]
            if self.sector:

                lon_obj = group_data.loc[:,'Centroid_Lon']
                lat_obj = group_data.loc[:,'Centroid_Lat']

                conus_lat_lon_points = zip(lon_obj.values.ravel(),lat_obj.values.ravel())
                center_lon, center_lat = self.proj_dict["lon_0"],self.proj_dict["lat_0"]

                distances = np.array([np.sqrt((x-center_lon)**2+\
                        (y-center_lat)**2) for (x, y) in conus_lat_lon_points])

                min_dist, max_minus_min = min(distances),max(distances)-min(distances)

                distance_0_1 = [1.0-((d - min_dist)/(max_minus_min)) for d in distances]
                weights = np.array(distance_0_1)

            self.size_distribution_models[group] = {"multi": {}, "lognorm": {}}
            if calibrate:
                self.size_distribution_models[group]["calshape"] = {}
                self.size_distribution_models[group]["calscale"] = {}
            log_labels = np.log(group_data[output_columns].values)
            log_means = log_labels.mean(axis=0)
            log_sds = log_labels.std(axis=0)
            self.size_distribution_models[group]['lognorm']['mean'] = log_means
            self.size_distribution_models[group]['lognorm']['sd'] = log_sds
            for m, model_name in enumerate(model_names):
                print(group, model_name)
                self.size_distribution_models[group]["multi"][model_name] = deepcopy(model_objs[m])
                try:
                    self.size_distribution_models[group]["multi"][model_name].fit(group_data[input_columns],
                                                                              (log_labels - log_means) / log_sds,
                                                                        sample_weight=weights)
                except:
                    self.size_distribution_models[group]["multi"][model_name].fit(group_data[input_columns],
                                                                              (log_labels - log_means) / log_sds)
                if calibrate:
                    training_predictions = self.size_distribution_models[
                        group]["multi"][model_name].predict(group_data[input_columns])
                    self.size_distribution_models[group]["calshape"][model_name] = LinearRegression()
                    self.size_distribution_models[group]["calshape"][model_name].fit(training_predictions[:, 0:1],
                                                                                (log_labels[:, 0] - log_means[0]) /
                                                                                log_sds[
                                                                                0],
                                                                                sample_weight=weights)
                    self.size_distribution_models[group]["calscale"][model_name] = LinearRegression()
                    self.size_distribution_models[group]["calscale"][model_name].fit(training_predictions[:, 1:],
                                                                                (log_labels[:, 1] - log_means[1]) /
                                                                                log_sds[
                                                                                1],
                                                                            sample_weight=weights)

Fits multitask machine learning models to predict the parameters of a size distribution
Args:
model_names: List of machine learning model names
model_objs: scikit-learn style machine learning model objects
input_columns: Training data columns used as input for ML model
output_columns: Training data columns used for prediction
calibrate: Whether or not to fit a log-linear regression to predictions from ML model


def encode(self, s):
    try:
        import matplotlib.image as im
    except ImportError as e:
        tf.logging.warning('Reading an image requires matplotlib to be installed: %s', e)
        raise NotImplementedError('Image reading not implemented.')
    return im.imread(s)

Transform a string with a filename into a list of RGB integers.

Args:
s: path to the file with an image.

Returns:
ids: list of integers


def SetBalanceFor(self, assetId, fixed8_val):

        found = False
        for key, val in self.Balances.items():
            if key == assetId:
                self.Balances[key] = fixed8_val
                found = True

        if not found:
            self.Balances[assetId] = fixed8_val

Set the balance for an asset id.
Args:
assetId (UInt256):
fixed8_val (Fixed8): balance value.


def _select_position(self, width, height):
    positions = self._generate_placements(width, height)
    if (self.rot and (width != height)):
        positions += self._generate_placements(height, width)
    if (not positions):
        return (None, None)
    return min(((p[0], self._rect_fitness(*p)) for p in positions), key=operator.itemgetter(1))

Search for the placement with the bes fitness for the rectangle.

Returns:
tuple (Rectangle, fitness) - Rectangle placed in the fittest position
None - Rectangle couldn't be placed


def _absolute_position_to_relative_position_unmasked(x):
    (batch, heads, length, _) = common_layers.shape_list(x)
    x = tf.pad(x, [[0, 0], [0, 0], [0, 0], [0, (length - 1)]])
    x_flat = tf.reshape(x, [batch, heads, ((length ** 2) + (length * (length - 1)))])
    x_flat = tf.pad(x_flat, [[0, 0], [0, 0], [length, 0]])
    x = tf.reshape(x_flat, [batch, heads, length, (2 * length)])
    x = tf.slice(x, [0, 0, 0, 1], [batch, heads, length, ((2 * length) - 1)])
    return x

Helper function for dot_product_unmasked_self_attention_relative_v2.

Rearrange an attention logits or weights Tensor.

The dimensions of the input represent:
[batch, heads, query_position, memory_position]

The dimensions of the output represent:
[batch, heads, query_position, memory_position - query_position + length - 1]

Only works with unmasked_attention.

Args:
x: a Tensor with shape [batch, heads, length, length]

Returns:
a Tensor with shape [batch, heads, length, 2*length-1]


def AddKeyByPath(self, key_path, registry_key):
    if (not key_path.startswith(definitions.KEY_PATH_SEPARATOR)):
        raise ValueError('Key path does not start with: {0:s}'.format(definitions.KEY_PATH_SEPARATOR))
    if (not self._root_key):
        self._root_key = FakeWinRegistryKey(self._key_path_prefix)
    path_segments = key_paths.SplitKeyPath(key_path)
    parent_key = self._root_key
    for path_segment in path_segments:
        try:
            subkey = FakeWinRegistryKey(path_segment)
            parent_key.AddSubkey(subkey)
        except KeyError:
            subkey = parent_key.GetSubkeyByName(path_segment)
        parent_key = subkey
    parent_key.AddSubkey(registry_key)

Adds a Windows Registry key for a specific key path.

Args:
key_path (str): Windows Registry key path to add the key.
registry_key (WinRegistryKey): Windows Registry key.

Raises:
KeyError: if the subkey already exists.
ValueError: if the Windows Registry key cannot be added.


def __eq__(self, other):

        if (type(self) is type(other) and
                self._index == other._index and
                self._drives == other._drives and
                self._controls == other._controls and
                self._measures == other._measures and
                self._acquires == other._acquires):
            return True
        return False

Two physical qubits are the same if they have the same index and channels.

Args:
other (Qubit): other Qubit

Returns:
bool: are self and other equal.


def export(self, remote_function):

        if self._worker.mode is None:

            self._functions_to_export.append(remote_function)
            return
        if self._worker.mode != ray.worker.SCRIPT_MODE:

            return
        self._do_export(remote_function)

Export a remote function.

Args:
remote_function: the RemoteFunction object.


def tox(args=''):

    basedir = dirname(__file__)

    latest_pythons = _determine_latest_pythons()

    highest_minor_python = _highest_minor(latest_pythons)

    _local_needs_pythons(flo('cd {basedir}  &&  '
                             'python{highest_minor_python} -m tox {args}'))

Run tox.

Build package and run unit tests against several pythons.

Args:
args: Optional arguments passed to tox.
Example:

fab tox:'-e py36 -r'


def search(self, q):

        results = self._api.search(q=q)

        return results

Search tweets by keyword.

Args:
q: keyword

Returns:
list: tweet list


def get_version():

    if not 'win' in sys.platform:
        return NO_WIN

    win_ver = sys.getwindowsversion()
    try:

        major, minor, build = win_ver.platform_version
    except AttributeError:
        if sys.version_info < (3, 0):

            from platform import _get_real_winver
            major, minor, build = _get_real_winver(win_ver.major, win_ver.minor, win_ver.build)
            major, minor, build = int(major), int(minor), int(build)
        else:

            major, minor, build = win_ver.major, win_ver.minor, win_ver.build

    try:
        is_server = 1 if win_ver.product_type == 3 else 0
    except AttributeError:
        is_server = 0

    try:
        if major == 10:

            sp_ver = build
        else:
            sp_ver = win_ver.service_pack_major or 0
    except AttributeError:
        try:
            sp_ver = int(win_ver.service_pack.rsplit(' ', 1))
        except (IndexError, ValueError):
            sp_ver = 0

    return (major, minor, sp_ver, is_server)

Get the Windows OS version running on the machine.

Params:
None

Returns:
The Windows OS version running on the machine (comparables with the values list in the class).


def categorical_partition_data(data):
    series = pd.Series(data)
    value_counts = series.value_counts(dropna=True)
    null_indexes = series.isnull()
    nonnull_count = (null_indexes == False).sum()
    weights = (value_counts.values / nonnull_count)
    return {'values': value_counts.index.tolist(), 'weights': weights}

Convenience method for creating weights from categorical data.

Args:
data (list-like): The data from which to construct the estimate.

Returns:
A new partition object::

{
"partition": (list) The categorical values present in the data
"weights": (list) The weights of the values in the partition.
}


def __init__(self, engine_id, client):

        self._client = client
        self._dv = client.direct_view(targets='all')
        self._dv.use_dill()
        nengines = len(client)
        super(ObjectHub, self).__init__(engine_id, nengines)

Make an ObjectHub.
Args:
engine_id: ipyparallel engine id number where this Hub is located,
or a negative number if it is on an ipyparallel client.
client: ipyparallel.Client


def create(cls, session, record, imported=False, auto_reply=False):
    return super(Conversations, cls).create(session, record, imported=imported, auto_reply=auto_reply)

Create a conversation.

Please note that conversation cannot be created with more than 100
threads, if attempted the API will respond with HTTP 412.

Args:
session (requests.sessions.Session): Authenticated session.
record (helpscout.models.Conversation): The conversation
to be created.
imported (bool, optional): The ``imported`` request parameter
enables conversations to be created for historical purposes (i.e.
if moving from a different platform, you can import your
history). When ``imported`` is set to ``True``, no outgoing
emails or notifications will be generated.
auto_reply (bool): The ``auto_reply`` request parameter enables
auto replies to be sent when a conversation is created via the
API. When ``auto_reply`` is set to ``True``, an auto reply will
be sent as long as there is at least one ``customer`` thread in
the conversation.

Returns:
helpscout.models.Conversation: Newly created conversation.


def input_selector_schema(config_cls):

    config_type = resolve_config_cls_arg(config_cls)
    check.param_invariant(config_type.is_selector, 'config_cls')

    def _wrap(func):
        def _selector(context, config_value):
            selector_key, selector_value = single_item(config_value)
            return func(context, selector_key, selector_value)

        return _create_input_schema(config_type, _selector)

    return _wrap

A decorator for annotating a function that can take the selected properties
from a ``config_value`` in to an instance of a custom type.

Args:
config_cls (Selector)


def resize(x, mode, factor=4):
    assert (mode in ['bilinear', 'nearest']), mode
    shp = (tf.shape(x)[2:] * factor)
    x = tf.transpose(x, [0, 2, 3, 1])
    if (mode == 'bilinear'):
        x = tf.image.resize_bilinear(x, shp, align_corners=True)
    else:
        x = tf.image.resize_nearest_neighbor(x, shp, align_corners=False)
    return tf.transpose(x, [0, 3, 1, 2])

Resize input tensor with unkown input-shape by a factor

Args:
x (tf.Tensor): tensor NCHW
factor (int, optional): resize factor for H, W

Note:
Differences here against Caffe have huge impacts on the
quality of the predictions.

Returns:
tf.Tensor: resized tensor NCHW


def _reset(self, indices):

    self.assert_common_preconditions()

    return np.stack([self._envs[index].reset() for index in indices])

Resets environments at indices shouldn't pre-process or record.

Subclasses should override this to do the actual reset if something other
than the default implementation is desired.

Args:
indices: list of indices of underlying envs to call reset on.

Returns:
np.ndarray of stacked observations from the reset-ed envs.


def should_use(intersection):
    if (intersection.interior_curve in ACCEPTABLE_CLASSIFICATIONS):
        return True
    if (intersection.interior_curve in TANGENT_CLASSIFICATIONS):
        return ((intersection.s == 0.0) or (intersection.t == 0.0))
    return False

Check if an intersection can be used as part of a curved polygon.

Will return :data:`True` if the intersection is classified as
:attr:`~.IntersectionClassification.FIRST`,
:attr:`~.IntersectionClassification.SECOND` or
:attr:`~.IntersectionClassification.COINCIDENT` or if the intersection
is classified is a corner / edge end which is classified as
:attr:`~.IntersectionClassification.TANGENT_FIRST` or
:attr:`~.IntersectionClassification.TANGENT_SECOND`.

Args:
intersection (.Intersection): An intersection to be added.

Returns:
bool: Indicating if the intersection will be used.


def lists_to_tuples(structure):
    return tree_impl.lists_to_tuples(structure)

Returns the structure with list instances changed to tuples.

Args:
structure: Arbitrarily nested structure.

Returns:
The same structure but with tuples instead of lists.


def get_training_or_validation_split(samples, labels, validation_split, subset):
    if not validation_split:
        return (samples, labels)
    num_val_samples = int(validation_split * len(samples))
    if subset == 'training':
        io_utils.print_msg(f'Using {len(samples) - num_val_samples} files for training.')
        samples = samples[:-num_val_samples]
        if labels is not None:
            labels = labels[:-num_val_samples]
    elif subset == 'validation':
        io_utils.print_msg(f'Using {num_val_samples} files for validation.')
        samples = samples[-num_val_samples:]
        if labels is not None:
            labels = labels[-num_val_samples:]
    else:
        raise ValueError(f'`subset` must be either "training" or "validation", received: {subset}')
    return (samples, labels)

Potentially restrict samples & labels to a training or validation split.

Args:
samples: List of elements.
labels: List of corresponding labels.
validation_split: Float, fraction of data to reserve for validation.
subset: Subset of the data to return.
Either `"training"`, `"validation"`, or `None`.
If `None`, we return all of the data.

Returns:
tuple (samples, labels), potentially restricted to the specified subset.


def __init__(self, dtype, shape=None, shared_name=None, name='conditional_accumulator', reduction_type='MEAN'):
    accumulator_ref = gen_data_flow_ops.resource_conditional_accumulator(dtype=dtype, shape=shape, shared_name=shared_name, name=name, reduction_type=reduction_type)
    if context.executing_eagerly():
        self._resource_deleter = resource_variable_ops.EagerResourceDeleter(handle=accumulator_ref, handle_device=context.context().device_name)
    super(ConditionalAccumulator, self).__init__(dtype, shape, accumulator_ref)

Creates a new ConditionalAccumulator.

Args:
dtype: Datatype of the accumulated gradients.
shape: Shape of the accumulated gradients.
shared_name: Optional. If non-empty, this accumulator will be shared under
the given name across multiple sessions.
name: Optional name for the accumulator.
reduction_type: Reduction type to use when taking the gradient.


def csv(self, ondemand=False):
    self._request_uri = '{}/{}'.format(self._api_uri, 'csv')
    self._stream = True
    if ondemand:
        self._request.add_payload('runNow', True)

Update request URI to return CSV data.

For onDemand bulk generation to work it must first be enabled in the
ThreatConnect platform under System settings.

Args:
ondemand (boolean): Enable on demand bulk generation.


def supported_language(lang):
    try:
        self.get_collection(lang=lang)
        return True
    except LanguageNotSupported as e:
        return False

Return True if polyglot supports the language.

Args:
lang (string): Language code.


def take_screenshot(webdriver, file_name):

        folder_location = os.path.join(ProjectUtils.get_project_root(),
                                       WebScreenShotUtil.SCREEN_SHOT_LOCATION)

        WebScreenShotUtil.__capture_screenshot(
            webdriver, folder_location, file_name + ".png")

Captures a screenshot.

Args:
webdriver (WebDriver) - Selenium webdriver.
file_name (str) - File name to save screenshot as.


def full_like(array, fill_value, reverse=False, dtype=None, keepmeta=True):
    if keepmeta:
        return (dc.zeros_like(array) + fill_value).astype(dtype)
    else:
        return dc.full(array.shape, fill_value, dtype)

Create an array of `fill_value` with the same shape and type as the input array.

Args:
array (xarray.DataArray): The shape and data-type of it define
these same attributes of the output array.
fill_value (scalar or numpy.ndarray): Fill value or array.
dtype (data-type, optional): If spacified, this function overrides
the data-type of the output array.
keepmeta (bool, optional): Whether *coords, attrs, and name of the input
array are kept in the output one. Default is True.

Returns:
array (decode.array): Decode array filled with `fill_value`.


def InitFromDataPoints(self, start_stats, complete_stats):

    self.start_points = self._ConvertToResultList(start_stats)
    self.complete_points = self._ConvertToResultList(complete_stats)
    return self

Check that this approval applies to the given token.

Args:
start_stats: A list of lists, each containing two values (a timestamp and
the number of clients started at this time).
complete_stats: A list of lists, each containing two values (a timestamp
and the number of clients completed at this time).

Returns:
A reference to the current instance to allow method chaining.


def DEFINE_multi_enum(name, default, enum_values, help, flag_values=_flagvalues.FLAGS, case_sensitive=True, **args):
    parser = _argument_parser.EnumParser(enum_values, case_sensitive)
    serializer = _argument_parser.ArgumentSerializer()
    DEFINE_multi(parser, serializer, name, default, help, flag_values, **args)

Registers a flag whose value can be a list strings from enum_values.

Use the flag on the command line multiple times to place multiple
enum values into the list.  The 'default' may be a single string
(which will be converted into a single-element list) or a list of
strings.

Args:
name: str, the flag name.
default: Union[Iterable[Text], Text, None], the default value of the flag;
see `DEFINE_multi`.
enum_values: [str], a non-empty list of strings with the possible values for
the flag.
help: str, the help message.
flag_values: FlagValues, the FlagValues instance with which the flag will
be registered. This should almost never need to be overridden.
case_sensitive: Whether or not the enum is to be case-sensitive.
**args: Dictionary with extra keyword args that are passed to the
Flag __init__.


def parse_google_format_docstring(docstring: str) -> tuple[Optional[str], Optional[dict], Optional[str]]:
    description_match = description_re.search(docstring)
    args_match = args_re.search(docstring)
    returns_match = returns_re.search(docstring)
    description = description_match.group(1).strip() if description_match else None
    docstring_args = args_match.group(1).strip() if args_match else None
    returns = returns_match.group(1).strip() if returns_match else None
    if docstring_args is not None:
        docstring_args = '\n'.join([line for line in docstring_args.split('\n') if line.strip()])
        matches = args_split_re.findall(docstring_args)
        args_dict = {match[0]: re.sub('\\s*\\n+\\s*', ' ', match[1].strip()) for match in matches}
    else:
        args_dict = {}
    return (description, args_dict, returns)

Parses a Google-style docstring to extract the function description,
argument descriptions, and return description.

Args:
docstring (str): The docstring to parse.

Returns:
The function description, arguments, and return description.


def add_stream_logger(level=logging.DEBUG, name=None):

    logger = logging.getLogger(name)
    logger.setLevel(level)
    handler = logging.StreamHandler()
    handler.setFormatter(get_default_log_formatter())
    handler.setLevel(level)
    logger.addHandler(handler)

Add a stream logger. This can be used for printing all SDK calls to stdout
while working in an interactive session. Note this is a logger for the
entire module, which will apply to all environments started in the same
session. If you need a specific logger pass a ``logfile`` to
:func:`~sdk.init`

Args:
level(int): :mod:`logging` log level
name(str): logger name, will default to the root logger.

Returns:
None


def AddComment(self, comment):

    if not comment:
      return

    if not self.comment:
      self.comment = comment
    else:
      self.comment = ''.join([self.comment, comment])

Adds a comment to the event tag.

Args:
comment (str): comment.


def _CompareFields(field, other_field):

  field_attrs = _GetFieldAttributes(field)
  other_field_attrs = _GetFieldAttributes(other_field)
  if field_attrs != other_field_attrs:
    return False
  return field.__class__ == other_field.__class__

Checks if two ProtoRPC fields are "equal".

Compares the arguments, rather than the id of the elements (which is
the default __eq__ behavior) as well as the class of the fields.

Args:
field: A ProtoRPC message field to be compared.
other_field: A ProtoRPC message field to be compared.

Returns:
Boolean indicating whether the fields are equal.


def setNetworkDataRequirement(self, eDataRequirement):

        print '%s call setNetworkDataRequirement' % self.port
        print eDataRequirement

        if eDataRequirement == Device_Data_Requirement.ALL_DATA:
            self.networkDataRequirement = 'n'
        return True

set whether the Thread device requires the full network data
or only requires the stable network data

Args:
eDataRequirement: is true if requiring the full network data

Returns:
True: successful to set the network requirement


def broadcast(cls,
            shape1: 'TensorFluentShape',
            shape2: 'TensorFluentShape') -> Tuple[Reshaping, Reshaping]:

        reshape_1, reshape_2 = None, None

        if not (shape1._batch or shape2._batch):
            return reshape_1, reshape_2

        size_1, size_2 = shape1.fluent_size, shape2.fluent_size
        size_diff = abs(size_1 - size_2)
        if size_diff == 0:
            return reshape_1, reshape_2

        if size_2 > size_1 and not (size_1 == 0 and not shape1._batch):
            reshape_1 = [1] * size_diff + list(shape1.fluent_shape)
            if shape1._batch:
                reshape_1 = [shape1.batch_size] + reshape_1
        elif size_1 > size_2 and not (size_2 == 0 and not shape2._batch):
            reshape_2 = [1] * size_diff + list(shape2.fluent_shape)
            if shape2._batch:
                reshape_2 = [shape2.batch_size] + reshape_2
        return reshape_1, reshape_2

It broadcasts the fluent shapes if any input is in batch mode.

It handles input shapes in different modes, expanding its
dimensions if necessary. It outputs a tuple with new shapes.
If no input shape is in batch mode, return (None, None).
If an input shape does not need to be changed, return None.

Args:
shape1: A fluent's shape.
shape2: A fluent's shape.

Returns:
A pair of new shapes.


def from_string(contents):
    if (contents[(- 1)] != '\n'):
        contents += '\n'
    white_space = '[ \\t\\r\\f\\v]'
    natoms_line = (((white_space + '*\\d+') + white_space) + '*\\n')
    comment_line = '[^\\n]*\\n'
    coord_lines = '(\\s*\\w+\\s+[0-9\\-\\+\\.eEdD]+\\s+[0-9\\-\\+\\.eEdD]+\\s+[0-9\\-\\+\\.eEdD]+\\s*\\n)+'
    frame_pattern_text = ((natoms_line + comment_line) + coord_lines)
    pat = re.compile(frame_pattern_text, re.MULTILINE)
    mols = []
    for xyz_match in pat.finditer(contents):
        xyz_text = xyz_match.group(0)
        mols.append(XYZ._from_frame_string(xyz_text))
    return XYZ(mols)

Creates XYZ object from a string.

Args:
contents: String representing an XYZ file.

Returns:
XYZ object


def get_climate(self, device_label):

        response = None
        try:
            response = requests.get(
                urls.climate(self._giid),
                headers={
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Cookie': 'vid={}'.format(self._vid)},
                params={
                    "deviceLabel": device_label})
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        return json.loads(response.text)

Get climate history
Args:
device_label: device label of climate device


def singleprint_from_saved_model_proto(export_dir: str) -> str:
    try:
        return fingerprinting_pywrap.SingleprintFromSM(export_dir)
    except FingerprintException as e:
        raise ValueError(e) from None

Returns the singleprint of `saved_model.pb` in `export_dir`.

Args:
export_dir: The directory that contains `saved_model.pb`.

Returns:
A string containing the singleprint of `saved_model.pb` in `export_dir`.

Raises:
ValueError: If a valid singleprint cannot be constructed from
`saved_model.pb`.


def call(self, func, *args, **kwargs):
    for timer in self:
        with timer:
            func(*args, **kwargs)
    return self

Alternative way to time a simple function call using condensed syntax.

Returns:
self (timerit.Timerit): Use `min`, or `mean` to get a scalar. Use
`print` to output a report to stdout.

Example:
>>> import math
>>> time = Timerit(num=10).call(math.factorial, 50).min()
>>> assert time > 0


def _check_properties(cls, property_names, require_indexed=True):
    assert isinstance(property_names, (list, tuple)), repr(property_names)
    for name in property_names:
        assert isinstance(name, basestring), repr(name)
        if ('.' in name):
            (name, rest) = name.split('.', 1)
        else:
            rest = None
        prop = cls._properties.get(name)
        if (prop is None):
            cls._unknown_property(name)
        else:
            prop._check_property(rest, require_indexed=require_indexed)

Internal helper to check the given properties exist and meet specified
requirements.

Called from query.py.

Args:
property_names: List or tuple of property names -- each being a string,
possibly containing dots (to address subproperties of structured
properties).

Raises:
InvalidPropertyError if one of the properties is invalid.
AssertionError if the argument is not a list or tuple of strings.


def trace_start(self):

        cmd = enums.JLinkTraceCommand.START
        res = self._dll.JLINKARM_TRACE_Control(cmd, 0)
        if (res == 1):
            raise errors.JLinkException('Failed to start trace.')
        return None

Starts collecting trace data.

Args:
self (JLink): the ``JLink`` instance.

Returns:
``None``


def initialize_typeshed_or_die():
    try:
        return typeshed.Typeshed()
    except OSError as e:
        logging.critical(str(e))
        sys.exit(1)

Initialize a Typeshed object or die.

Returns:
An instance of Typeshed()


def __init__(self, name=None, url=None, timezone=None, id=None, email=None,
               field_dict=None, lang=None, **kwargs):

    self._schedule = None

    if not field_dict:
      if name:
        kwargs['agency_name'] = name
      if url:
        kwargs['agency_url'] = url
      if timezone:
        kwargs['agency_timezone'] = timezone
      if id:
        kwargs['agency_id'] = id
      if lang:
        kwargs['agency_lang'] = lang
      if email:
        kwargs['agency_email'] = email
      field_dict = kwargs

    self.__dict__.update(field_dict)

Initialize a new Agency object.

Args:
field_dict: A dictionary mapping attribute name to unicode string
name: a string, ignored when field_dict is present
url: a string, ignored when field_dict is present
timezone: a string, ignored when field_dict is present
id: a string, ignored when field_dict is present
kwargs: arbitrary keyword arguments may be used to add attributes to the
new object, ignored when field_dict is present


def substitute(dict_, source):

    d_esc = (re.escape(k) for k in dict_.keys())
    pattern = re.compile('|'.join(d_esc))
    return pattern.sub(lambda x: dict_[x.group()], source)

Perform re.sub with the patterns in the given dict
Args:
dict_: {pattern: repl}
source: str


def get_content_field(self, name):
    fields = self._content.findall(name)
    if (not fields):
        return None
    elif (len(fields) == 1):
        return etree_to_dict(fields[0])[name]
    else:
        return [etree_to_dict(field)[name] for field in fields]

Get the contents of a specific subtag from Clusterpoint Storage's response's content tag.

Args:
name -- A name string of the content's subtag to be returned.

Returns:
A dict representing the contents of the specified field or a list of dicts
if there are multiple fields with that tag name. Returns None if no field found.


def filter_parts(cls, part_info):

        filtered = OrderedDict()
        for part_name, info_list in part_info.items():
            if info_list is None or isinstance(info_list, Exception):
                continue
            info_list = [i for i in info_list if isinstance(i, cls)]
            if info_list:
                filtered[part_name] = info_list
        return filtered

Filter the part_info dict looking for instances of our class

Args:
part_info (dict): {part_name: [Info] or None} as returned from
Controller.run_hook()

Returns:
dict: {part_name: [info]} where info is a subclass of cls


def _MakeGroupFromRootSection(root_section, undefined_str):

    group = {}
    for statement in root_section.Statements():
        if isinstance(statement, six.string_types):
            continue
        func, args = statement

        if func is _DoDef and isinstance(args, _Section):
            section = args

            t = Template._FromSection(section, group, undefined_str)
            group[section.section_name] = t
    return group

Construct a dictinary { template name -> Template() instance }

Args:
root_section: _Section instance -- root of the original parse tree


def remove_pad(x, pad_remover, mode):
    x = expert_utils.flatten_all_but_last(x)
    if (mode != ModeKeys.PREDICT):
        x = pad_remover.remove(x)
    x = tf.expand_dims(x, axis=0)
    return x

Remove padding by concatenating all dimension into one.

Args:
x (tf.Tensor): input of shape [batch_size, length, depth]
pad_remover (obj): a PadRemover object
mode (ModeKeys): infer, train or eval. If inference, the padding remover is
not applied

Returns:
tf.Tensor of shape [1,length_nonpad,depth] where
length_nonpad <= batch_size*length


def pad_trajectories(trajectories, boundary=20):
    t_max = max((r.shape[0] for (_, _, r) in trajectories))
    boundary = int(boundary)
    bucket_length = (boundary * int(np.ceil((float(t_max) / boundary))))
    padded_observations = []
    padded_actions = []
    padded_rewards = []
    padded_lengths = []
    reward_masks = []
    for (o, a, r) in trajectories:
        num_to_pad = ((bucket_length + 1) - o.shape[0])
        padded_lengths.append(num_to_pad)
        if (num_to_pad == 0):
            padded_observations.append(o)
            padded_actions.append(a)
            padded_rewards.append(r)
            reward_masks.append(onp.ones_like(r, dtype=np.int32))
            continue
        padding_config = [(0, num_to_pad, 0)]
        for _ in range((o.ndim - 1)):
            padding_config.append((0, 0, 0))
        padding_config = tuple(padding_config)
        padding_value = get_padding_value(o.dtype)
        action_padding_value = get_padding_value(a.dtype)
        reward_padding_value = get_padding_value(r.dtype)
        padded_obs = lax.pad(o, padding_value, padding_config)
        padded_observations.append(padded_obs)
        assert ((a.ndim == 1) and (r.ndim == 1))
        padding_config = ((0, num_to_pad, 0),)
        padded_action = lax.pad(a, action_padding_value, padding_config)
        padded_actions.append(padded_action)
        padded_reward = lax.pad(r, reward_padding_value, padding_config)
        padded_rewards.append(padded_reward)
        reward_mask = onp.ones_like(r, dtype=np.int32)
        reward_masks.append(lax.pad(reward_mask, 0, padding_config))
    return (padded_lengths, np.stack(reward_masks), np.stack(padded_observations), np.stack(padded_actions), np.stack(padded_rewards))

Pad trajectories to a bucket length that is a multiple of boundary.

Args:
trajectories: list[(observation, actions, rewards)], where each observation
is shaped (t+1,) + OBS and actions & rewards are shaped (t,), with the
length of the list being B (batch size).
boundary: int, bucket length, the actions and rewards are padded to integer
multiples of boundary.

Returns:
tuple: (padding lengths, reward_mask, padded_observations, padded_actions,
padded_rewards) where padded_observations is shaped (B, T+1) + OBS and
padded_actions, padded_rewards & reward_mask are shaped (B, T).
Where T is max(t) rounded up to an integer multiple of boundary.
padded_length is how much padding we've added and
reward_mask is 1s for actual rewards and 0s for the padding.


def preprocess_input_examples_arg_string(input_examples_str):
    input_dict = preprocess_input_exprs_arg_string(input_examples_str)
    for input_key, example_list in input_dict.items():
        if not isinstance(example_list, list):
            raise ValueError('tf.Example input must be a list of dictionaries, but "%s" is %s' % (example_list, type(example_list)))
        input_dict[input_key] = [_create_example_string(example) for example in example_list]
    return input_dict

Parses input into dict that maps input keys to lists of tf.Example.

Parses input string in the format of 'input_key1=[{feature_name:
feature_list}];input_key2=[{feature_name:feature_list}];' into a dictionary
that maps each input_key to its list of serialized tf.Example.

Args:
input_examples_str: A string that specifies a list of dictionaries of
feature_names and their feature_lists for each input.
Each input is separated by semicolon. For each input key:
'input=[{feature_name1: feature_list1, feature_name2:feature_list2}]'
items in feature_list can be the type of float, int, long or str.

Returns:
A dictionary that maps input keys to lists of serialized tf.Example.

Raises:
ValueError: An error when the given tf.Example is not a list.


def _run_graph(self, device, input_shape, axes, num_layers, mode, scale, train, num_iters):
    graph = ops.Graph()
    with graph.as_default():
        outputs = build_graph(device, input_shape, axes, num_layers, mode, scale, train)
    with session_lib.Session(graph=graph) as session:
        variables.global_variables_initializer().run()
        _ = session.run([out.op for out in outputs])
        start_time = time.time()
        for _ in range(num_iters):
            _ = session.run([out.op for out in outputs])
        duration = time.time() - start_time
    print('%s shape:%d/%d
    name_template = 'batch_norm_{device}_input_shape_{shape}_axes_{axes}_mode_{mode}_layers_{num_layers}_scale_{scale}_train_{train}'
    self.report_benchmark(name=name_template.format(device=device, mode=mode, num_layers=num_layers, scale=scale, train=train, shape=str(input_shape).replace(' ', ''), axes=str(axes)).replace(' ', ''), iters=num_iters, wall_time=duration / num_iters)
    return duration

Run the graph and print its execution time.

Args:
device: string, the device to run on.
input_shape: shape of the input tensor.
axes: axes that are to be normalized across.
num_layers: number of batch normalization layers in the graph.
mode: "op", "py" or "slow" depending on the implementation.
scale: scale after normalization.
train: if true, also run backprop.
num_iters: number of steps to run.

Returns:
The duration of the run in seconds.


def _parse_trunk_native_vlan(self, config):

        match = re.search(r'switchport trunk native vlan (\d+)', config)
        return dict(trunk_native_vlan=match.group(1))

Scans the specified config and parse the trunk native vlan value

Args:
config (str): The interface configuration block to scan

Returns:
dict: A Python dict object with the value of switchport trunk
native vlan value.  The dict returned is intended to be
merged into the resource dict


def get_yaml_parser_roundtrip():
    yaml_writer = yamler.YAML(typ='rt', pure=True)
    yaml_writer.indent(mapping=2, sequence=4, offset=2)
    return yaml_writer

Create the yaml parser object with this factory method.

The round-trip parser preserves:
- comments
- block style and key ordering are kept, so you can diff the round-tripped
source
- flow style sequences ( ‘a: b, c, d’) (based on request and test by
Anthony Sottile)
- anchor names that are hand-crafted (i.e. not of the form``idNNN``)
- merges in dictionaries are preserved

Returns:
ruamel.yaml.YAML object with round-trip loader


def _post_process(self, feed_item, item):
    if item['assetIdentifier']['name']:
        feed_item[FieldMap.CREATIVE_ASSET_NAME] = item['assetIdentifier']['name']

Maps ids and names of related entities so they can be updated in the Bulkdozer feed.

When Bulkdozer is done processing an item, it writes back the updated names
and ids of related objects, this method makes sure those are updated in the
creative asset feed.

Args:
feed_item: Feed item representing the creative asset from the Bulkdozer
feed.
item: The DCM creative asset being updated or created.


def _assign_method(self, resource_class, method_type):
    "\n        If we assigned the same method to each method, it's the same\n        method in memory, so we need one for each acceptable HTTP method.\n        "
    method_name = resource_class.get_method_name(resource_class, method_type)
    valid_status_codes = getattr(resource_class.Meta, 'valid_status_codes', DEFAULT_VALID_STATUS_CODES)

    def get(self, method_type=method_type, method_name=method_name, valid_status_codes=valid_status_codes, resource=resource_class, data=None, uid=None, **kwargs):
        return self.call_api(method_type, method_name, valid_status_codes, resource, data, uid=uid, **kwargs)

    def put(self, method_type=method_type, method_name=method_name, valid_status_codes=valid_status_codes, resource=resource_class, data=None, uid=None, **kwargs):
        return self.call_api(method_type, method_name, valid_status_codes, resource, data, uid=uid, **kwargs)

    def post(self, method_type=method_type, method_name=method_name, valid_status_codes=valid_status_codes, resource=resource_class, data=None, uid=None, **kwargs):
        return self.call_api(method_type, method_name, valid_status_codes, resource, data, uid=uid, **kwargs)

    def patch(self, method_type=method_type, method_name=method_name, valid_status_codes=valid_status_codes, resource=resource_class, data=None, uid=None, **kwargs):
        return self.call_api(method_type, method_name, valid_status_codes, resource, data, uid=uid, **kwargs)

    def delete(self, method_type=method_type, method_name=method_name, valid_status_codes=valid_status_codes, resource=resource_class, data=None, uid=None, **kwargs):
        return self.call_api(method_type, method_name, valid_status_codes, resource, data, uid=uid, **kwargs)
    method_map = {'GET': get, 'PUT': put, 'POST': post, 'PATCH': patch, 'DELETE': delete}
    setattr(self, method_name, types.MethodType(method_map[method_type], self))

Using reflection, assigns a new method to this class.

Args:
resource_class: A resource class
method_type: The HTTP method type


def item(self, key):

    return _item.Item(self._name, key, context=self._context)

Retrieves an Item object for the specified key in this bucket.

The item need not exist.

Args:
key: the key of the item within the bucket.
Returns:
An Item instance representing the specified key.


def update_handler(Model, name=None, **kwds):

    async def action_handler(service, action_type, payload, props, notify=True, **kwds):

        if action_type == get_crud_action('update', name or Model):
            try:

                message_props = {}

                if 'correlation_id' in props:

                    message_props['correlation_id'] = props['correlation_id']

                pk_field = Model.primary_key()

                if not pk_field.name in payload:

                    raise ValueError("Must specify the pk of the model when updating")

                model = Model.select().where(pk_field == payload[pk_field.name]).get()

                payload.pop(pk_field.name, None)

                for key, value in payload.items():

                    setattr(model, key, value)

                model.save()

                if notify:

                    await service.event_broker.send(
                        payload=ModelSerializer().serialize(model),
                        action_type=change_action_status(action_type, success_status()),
                        **message_props
                    )

            except Exception as err:

                if notify:

                    await service.event_broker.send(
                        payload=str(err),
                        action_type=change_action_status(action_type, error_status()),
                        **message_props
                    )

                else:

                    raise err

    return action_handler

This factory returns an action handler that updates a new instance of
the specified model when a update action is recieved, assuming the
action follows nautilus convetions.

Args:
Model (nautilus.BaseModel): The model to update when the action
received.

Returns:
function(type, payload): The action handler for this model


def _find_furious_yaml(start, checked):

    directory = start
    while directory not in checked:
        checked.add(directory)
        for fs_yaml_name in FURIOUS_YAML_NAMES:
            yaml_path = os.path.join(directory, fs_yaml_name)
            if os.path.exists(yaml_path):
                return yaml_path
        directory = os.path.dirname(directory)
    return None

Traverse the directory tree identified by start
until a directory already in checked is encountered or the path
of furious.yaml is found.

Checked is present both to make the loop termination easy
to reason about and so the same directories do not get
rechecked

Args:
start: the path to start looking in and work upward from
checked: the set of already checked directories

Returns:
the path of the furious.yaml file or None if it is not found


def getexcfo(e):
    tb = sys.exc_info()[2]
    tbinfo = traceback.extract_tb(tb)
    (path, line, name, src) = ('', '', '', None)
    if tbinfo:
        (path, line, name, sorc) = tbinfo[(- 1)]
    retd = {'msg': str(e), 'file': path, 'line': line, 'name': name, 'src': src}
    if isinstance(e, s_exc.SynErr):
        retd['syn:err'] = e.errinfo
    return (e.__class__.__name__, retd)

Get an err tufo from an exception.

Args:
e (Exception): An Exception (or Exception subclass).

Notes:
This can be called outside of the context of an exception handler,
however details such as file, line, function name and source may be
missing.

Returns:
((str, dict)):


def GetFileObject(self, data_stream_name=''):

    data_stream_names = [
        data_stream.name for data_stream in self._GetDataStreams()]
    if data_stream_name and data_stream_name not in data_stream_names:
      return None

    path_spec = copy.deepcopy(self.path_spec)
    if data_stream_name:

      if self._file_system.IsHFS() and data_stream_name == 'DECOMP':
        data_stream_name = ''

      setattr(path_spec, 'data_stream', data_stream_name)

    return resolver.Resolver.OpenFileObject(
        path_spec, resolver_context=self._resolver_context)

Retrieves the file-like object.

Args:
data_stream_name (Optional[str]): data stream name, where an empty
string represents the default data stream.

Returns:
TSKFileIO: file-like object or None.


def convert_config_value(self, value, label):
    if isinstance(value, six.string_types):
        value = value.lower()
    if (value in self.TRUTHY_VALUES):
        return True
    elif (value in self.FALSY_VALUES):
        return False
    else:
        raise YapconfValueError('Cowardly refusing to interpret config value as a boolean. Name: {0}, Value: {1}'.format(self.name, value))

Converts all 'Truthy' values to True and 'Falsy' values to False.

Args:
value: Value to convert
label: Label of the config which this item was found.

Returns:


def argpartition(x, kth, axis=-1):
    if any_symbolic_tensors((x,)):
        return Argpartition(kth, axis).symbolic_call(x)
    return backend.numpy.argpartition(x, kth, axis)

Performs an indirect partition along the given axis.

It returns an array
of indices of the same shape as `x` that index data along the given axis
in partitioned order.

Args:
a: Array to sort.
kth: Element index to partition by.
The k-th element will be in its final sorted position and all
smaller elements will be moved before it and all larger elements
behind it. The order of all elements in the partitions is undefined.
If provided with a sequence of k-th it will partition all of them
into their sorted position at once.
axis: Axis along which to sort. The default is -1 (the last axis).
If `None`, the flattened array is used.

Returns:
Array of indices that partition `x` along the specified `axis`.


def get_input_mask_at(self, node_index):
    inputs = self.get_input_at(node_index)
    if isinstance(inputs, list):
        return [getattr(x, '_keras_mask', None) for x in inputs]
    else:
        return getattr(inputs, '_keras_mask', None)

Retrieves the input mask tensor(s) of a layer at a given node.

Args:
node_index: Integer, index of the node
from which to retrieve the attribute.
E.g. `node_index=0` will correspond to the
first time the layer was called.

Returns:
A mask tensor
(or list of tensors if the layer has multiple inputs).


def get_data_xls(file_name, file_contents=None, on_demand=False):

    def tuple_to_iso_date(tuple_date):

        (y,m,d, hh,mm,ss) = tuple_date
        non_zero = lambda n: n!=0
        date = "%04d-%02d-%02d"  % (y,m,d)    if list(filter(non_zero, (y,m,d)))               else ''
        time = "T%02d:%02d:%02d" % (hh,mm,ss) if list(filter(non_zero, (hh,mm,ss))) or not date else ''
        return date+time

    def format_excel_val(book, val_type, value, want_tuple_date):

        if   val_type == 2:
            if value == int(value): value = int(value)
        elif val_type == 3:
            datetuple = xlrd.xldate_as_tuple(value, book.datemode)
            value = datetuple if want_tuple_date else tuple_to_iso_date(datetuple)
        elif val_type == 5:
            value = xlrd.error_text_from_code[value]
        return value

    def xlrd_xsl_to_array(file_name, file_contents=None):

        book = xlrd.open_workbook(file_name, file_contents=file_contents, on_demand=on_demand)
        formatter = lambda t_v: format_excel_val(book, t_v[0], t_v[1], False)
        row_builder = lambda s, r: list(map(formatter, zip(s.row_types(r), s.row_values(r))))

        data = [SheetYielder(book, index, row_builder) for index in range(book.nsheets)]
        if not on_demand:
            for sheet in data:
                sheet.load()
            book.release_resources()
        return data

    return xlrd_xsl_to_array(file_name, file_contents)

Loads the old excel format files. New format files will automatically
get loaded as well.

Args:
file_name: The name of the local file, or the holder for the
extension type when the file_contents are supplied.
file_contents: The file-like object holding contents of file_name.
If left as None, then file_name is directly loaded.
on_demand: Requests that a yielder be used in place of a full data
copy.


def quickhull(sample):

    link = lambda a, b: np.concatenate((a, b[1:]))
    edge = lambda a, b: np.concatenate(([a], [b]))

    def dome(sample, base):
        h, t = base
        dists = np.dot(sample - h, np.dot(((0, -1), (1, 0)), (t - h)))
        outer = np.repeat(sample, dists > 0, axis=0)

        if len(outer):
            pivot = sample[np.argmax(dists)]
            return link(dome(outer, edge(h, pivot)),
                dome(outer, edge(pivot, t)))
        else:
            return base

    if len(sample) > 2:
        axis = sample[:, 0]
        base = np.take(sample, [np.argmin(axis), np.argmax(axis)], axis=0)
        return link(dome(sample, base),
            dome(sample, base[::-1]))
    else:
        return sample

Find data points on the convex hull of a supplied data set

Args:
sample: data points as column vectors n x d
n - number samples
d - data dimension (should be two)

Returns:
a k x d matrix containint the convex hull data points


def _queue_dag(self, name, *, data=None):
    if self._stop_workflow:
        return None
    if (name not in self._dags_blueprint):
        raise DagNameUnknown()
    new_dag = copy.deepcopy(self._dags_blueprint[name])
    new_dag.workflow_name = self.name
    self._dags_running[new_dag.name] = self._celery_app.send_task(JobExecPath.Dag, args=(new_dag, self._workflow_id, data), queue=new_dag.queue, routing_key=new_dag.queue)
    return new_dag.name

Add a new dag to the queue.

If the stop workflow flag is set, no new dag can be queued.

Args:
name (str): The name of the dag that should be queued.
data (MultiTaskData): The data that should be passed on to the new dag.

Raises:
DagNameUnknown: If the specified dag name does not exist

Returns:
str: The name of the queued dag.


def load_config(self, filepath=None):

        def load_settings(filepath):

            instruments_loaded = {}
            probes_loaded = {}
            scripts_loaded = {}

            if filepath and os.path.isfile(filepath):
                in_data = load_b26_file(filepath)

                instruments = in_data['instruments'] if 'instruments' in in_data else {}
                scripts = in_data['scripts'] if 'scripts' in in_data else {}
                probes = in_data['probes'] if 'probes' in in_data else {}

                try:
                    instruments_loaded, failed = Instrument.load_and_append(instruments)
                    if len(failed) > 0:
                        print(('WARNING! Following instruments could not be loaded: ', failed))

                    scripts_loaded, failed, instruments_loaded = Script.load_and_append(
                        script_dict=scripts,
                        instruments=instruments_loaded,
                        log_function=self.log,
                        data_path=self.gui_settings['data_folder'])

                    if len(failed) > 0:
                        print(('WARNING! Following scripts could not be loaded: ', failed))

                    probes_loaded, failed, instruments_loadeds = Probe.load_and_append(
                        probe_dict=probes,
                        probes=probes_loaded,
                        instruments=instruments_loaded)

                    self.log('Successfully loaded from previous save.')
                except ImportError:
                    self.log('Could not load instruments or scripts from file.')
                    self.log('Opening with blank GUI.')
            return instruments_loaded, scripts_loaded, probes_loaded

        config = None

        try:
            config = load_b26_file(filepath)
            config_settings = config['gui_settings']
            if config_settings['gui_settings'] != filepath:
                print((
                'WARNING path to settings file ({:s}) in config file is different from path of settings file ({:s})'.format(
                    config_settings['gui_settings'], filepath)))
            config_settings['gui_settings'] = filepath
        except Exception as e:
            if filepath:
                self.log('The filepath was invalid --- could not load settings. Loading blank GUI.')
            config_settings = self._DEFAULT_CONFIG

            for x in self._DEFAULT_CONFIG.keys():
                if x in config_settings:
                    if not os.path.exists(config_settings[x]):
                        try:
                            os.makedirs(config_settings[x])
                        except Exception:
                            config_settings[x] = self._DEFAULT_CONFIG[x]
                            os.makedirs(config_settings[x])
                            print(('WARNING: failed validating or creating path: set to default path'.format(config_settings[x])))
                else:
                    config_settings[x] = self._DEFAULT_CONFIG[x]
                    os.makedirs(config_settings[x])
                    print(('WARNING: path {:s} not specified set to default {:s}'.format(x, config_settings[x])))

        if filepath is not None and os.path.exists(os.path.dirname(filepath)):
            config_settings['gui_settings'] = filepath

        self.gui_settings = config_settings

        if(config):
            self.gui_settings_hidden = config['gui_settings_hidden']
        else:
            self.gui_settings_hidden['script_source_folder'] = ''

        self.instruments, self.scripts, self.probes = load_settings(filepath)

        self.refresh_tree(self.tree_gui_settings, self.gui_settings)
        self.refresh_tree(self.tree_scripts, self.scripts)
        self.refresh_tree(self.tree_settings, self.instruments)

        self._hide_parameters(filepath)

checks if the file is a valid config file
Args:
filepath:


def slice_arrays(arrays, indices, contiguous=True):
    converted_to_list = False
    if not isinstance(arrays, list):
        converted_to_list = True
        arrays = [arrays]
    if any((tensor_util.is_tf_type(x) for x in arrays)):
        if not contiguous:
            entries = [[x[i:i + 1] for i in indices] for x in arrays]
            slices = [array_ops.concat(x, axis=0) for x in entries]
        else:
            slices = [x[indices[0]:indices[-1] + 1] for x in arrays]
    else:
        slices = generic_utils.slice_arrays(arrays, indices)
    if converted_to_list:
        slices = slices[0]
    return slices

Slices batches out of provided arrays (workaround for eager tensors).

Unfortunately eager tensors don't have the same slicing behavior as
Numpy arrays (they follow the same slicing behavior as symbolic TF tensors),
hence we cannot use `generic_utils.slice_arrays` directly
and we have to implement this workaround based on `concat`. This has a
performance cost.

Args:
arrays: Single array or list of arrays.
indices: List of indices in the array that should be included in the output
batch.
contiguous: Boolean flag indicating whether the indices are contiguous.

Returns:
Slice of data (either single array or list of arrays).


def __init__(self,corpus_dir,datastore_type='file',db_name='corpus.db'):

        self.g = Goose({'browser_user_agent': 'Mozilla','parser_class':'soup'})

        self.corpus_dir = corpus_dir
        self.datastore_type = datastore_type
        self.db_name = db_name
        self.stats = defaultdict(int)

        self._create_corpus_dir(self.corpus_dir)

        self.db = None
        if self.datastore_type == 'sqlite':
            self.db = self.corpus_dir + '/' + self.db_name
            self._set_up_db(self.db)

Read links and associated categories for specified articles
in text file seperated by a space

Args:
corpus_dir (str): The directory to save the generated corpus
datastore_type (Optional[str]): Format to save generated corpus.
Specify either 'file' or 'sqlite'.
db_name (Optional[str]): Name of database if 'sqlite' is selected.


def parse(self, args: List[str]) -> Optional[argparse.Namespace]:
    try:
        return self._parser.parse_args(args)
    except KeyboardInterrupt:
        raise
    except:
        return None

Parses a list of string inputs.

The parsed namespace contains these attributes:
output_name: Optional[str], the output variable name.
verbose: bool, whether to display more details of the magic execution.
query: Optional[List[str]], the beam SQL query to execute.

Returns:
The parsed args or None if fail to parse.


def get_by(self, field, value):

        if not field:
            logger.exception(RESOURCE_CLIENT_INVALID_FIELD)
            raise ValueError(RESOURCE_CLIENT_INVALID_FIELD)

        filter = "\"{0}='{1}'\"".format(field, value)
        results = self.get_all(filter=filter)

        if "." not in field:

            results = [item for item in results if str(item.get(field, "")).lower() == value.lower()]

        return results

Get the resource by passing a field and its value.

Note:
This function uses get_all passing a filter.The search is case-insensitive.

Args:
field: Field name to filter.
value: Value to filter.

Returns:
dict


def _parse_networks(self, config):
    networks = list()
    regexp = 'network (.+)/(\\d+) area (\\d+\\.\\d+\\.\\d+\\.\\d+)'
    matches = re.findall(regexp, config)
    for (network, netmask, area) in matches:
        networks.append(dict(network=network, netmask=netmask, area=area))
    return dict(networks=networks)

Parses config file for the networks advertised
by the OSPF process

Args:
config(str):  Running configuration
Returns:
list: dict:
keys: network (str)
netmask (str)
area (str)


def _vmap_for_bhqkv(mask_function: Callable, bh_indices: bool=True) -> Callable:
    dimensions = [(None, None, None, 0), (None, None, 0, None)]
    if bh_indices:
        dimensions.extend([(None, 0, None, None), (0, None, None, None)])
    for dims in dimensions:
        mask_function = torch.vmap(mask_function, in_dims=dims, out_dims=0)
    return mask_function

Used to vmap our mask_functions over the q_idx and kv_idx dimensions of the inputs. Optionally, vmap over
the batch and head indices as well if `bh_indices=True`.
Using vmap here allows us to keep the performance of vectorized ops, while having a single set of primitive
functions between attention interfaces (i.e. between flex and sdpa/eager, FA2 being a bit different).

Args:
mask_function (`Callable`):
The mask_function to vmap.
bh_indices (`bool`, optional):
Whether to vmap over the batch and head indices as well, or only q and kv indices.

Returns:
Callable: The vmapped function.