def convert(self):
    if self.experimental_lower_to_saved_model:
        saved_model_convert_result = self._convert_as_saved_model()
        if saved_model_convert_result:
            return saved_model_convert_result
    graph_def, input_tensors, output_tensors, frozen_func = self._freeze_concrete_function()
    graph_def = self._optimize_tf_model(graph_def, input_tensors, output_tensors, frozen_func)
    return super(TFLiteFrozenGraphConverterV2, self).convert(graph_def, input_tensors, output_tensors)

Converts a TensorFlow GraphDef based on instance variables.

Returns:
The converted data in serialized format.

Raises:
ValueError:
No concrete function is specified.
Multiple concrete functions are specified.
Input shape is not specified.
Invalid quantization parameters.


def pull_datapackage(descriptor, name, backend, **backend_options):

    warnings.warn(
        'Functions "push/pull_datapackage" are deprecated. '
        'Please use "Package" class',
        UserWarning)

    datapackage_name = name

    plugin = import_module('jsontableschema.plugins.%s' % backend)
    storage = plugin.Storage(**backend_options)

    resources = []
    for table in storage.buckets:

        schema = storage.describe(table)
        base = os.path.dirname(descriptor)
        path, name = _restore_path(table)
        fullpath = os.path.join(base, path)

        helpers.ensure_dir(fullpath)
        with io.open(fullpath, 'wb') as file:
            model = Schema(deepcopy(schema))
            data = storage.iter(table)
            writer = csv.writer(file, encoding='utf-8')
            writer.writerow(model.headers)
            for row in data:
                writer.writerow(row)

        resource = {'schema': schema, 'path': path}
        if name is not None:
            resource['name'] = name
        resources.append(resource)

    mode = 'w'
    encoding = 'utf-8'
    if six.PY2:
        mode = 'wb'
        encoding = None
    resources = _restore_resources(resources)
    helpers.ensure_dir(descriptor)
    with io.open(descriptor,
                 mode=mode,
                 encoding=encoding) as file:
        descriptor = {
            'name': datapackage_name,
            'resources': resources,
        }
        json.dump(descriptor, file, indent=4)
    return storage

Pull Data Package from storage.

All parameters should be used as keyword arguments.

Args:
descriptor (str): path where to store descriptor
name (str): name of the pulled datapackage
backend (str): backend name like `sql` or `bigquery`
backend_options (dict): backend options mentioned in backend docs


def launch_minecraft(ports = [], wait_timeout = 360):

    if "MALMO_XSD_PATH" not in os.environ:
        print("Please set the MALMO_XSD_PATH environment variable.")
        return
    cwd = os.getcwd()
    try:
        os.chdir(malmo_install_dir + "/Minecraft")
        launch_minecraft_in_background(os.getcwd(), ports, wait_timeout)
    finally:
        os.chdir(cwd)

Launch Malmo Minecraft Mod in one or more clients from
the Minecraft directory on the (optionally) given ports.
Args:
ports: an optionsl list of ports to start minecraft clients on.
Defaults to a single Minecraft client on port 10000.
wait_timeout: optional time in seconds to wait (defaults to 3 mins).


def get_special_tokens_mask(self, token_ids_0: List[int], token_ids_1: Optional[List[int]]=None, already_has_special_tokens: bool=False) -> List[int]:
    if already_has_special_tokens:
        return super().get_special_tokens_mask(token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True)
    prefix_ones = [1] * len(self.prefix_tokens)
    suffix_ones = [1] * len(self.suffix_tokens)
    if token_ids_1 is None:
        return prefix_ones + [0] * len(token_ids_0) + suffix_ones
    return prefix_ones + [0] * len(token_ids_0) + [0] * len(token_ids_1) + suffix_ones

Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
special tokens using the tokenizer `prepare_for_model` method.

Args:
token_ids_0 (`List[int]`):
List of IDs.
token_ids_1 (`List[int]`, *optional*):
Optional second list of IDs for sequence pairs.
already_has_special_tokens (`bool`, *optional*, defaults to `False`):
Whether or not the token list is already formatted with special tokens for the model.

Returns:
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.


def update_instance(self, data):

        for key, val in iteritems(data):
            if not hasattr(self, key):
                raise AttributeError(
                    "No field named {key} for model {model}".format(
                        key=key,
                        model=self.__class__.__name__
                    )
                )

            setattr(self, key, val)

        self.save()

        return self

Update a single record by id with the provided data.

Args:
data (dict): The new data to update the record with.

Returns:
self: This is an instance of itself with the updated data.

Raises:
AttributeError: This is raised if a key in the ``data`` isn't
a field on the model.


def read_message(self, timeout=None):
    raw_data = self.stream.read(struct.calcsize(self.RECV_MSG_TYPE.struct_format), timeout)
    try:
        raw_message = struct.unpack(self.RECV_MSG_TYPE.struct_format, raw_data)
    except struct.error:
        raise usb_exceptions.AdbProtocolError('%s expected format "%s", got data %s', self, self.RECV_MSG_TYPE.struct_format, raw_data)
    if (raw_message[0] not in self.WIRE_TO_CMD):
        raise usb_exceptions.AdbProtocolError('Unrecognized command id: %s', raw_message)
    raw_message = ((self.WIRE_TO_CMD[raw_message[0]],) + raw_message[1:])
    if (self.RECV_MSG_TYPE.has_data and raw_message[(- 1)]):
        data_len = raw_message[(- 1)]
        raw_message = (raw_message[:(- 1)] + (self.stream.read(data_len, timeout),))
    if (raw_message[0] not in self.VALID_RESPONSES):
        raise usb_exceptions.AdbProtocolError('%s not a valid response for %s', raw_message[0], self)
    if (raw_message[0] == 'FAIL'):
        raise usb_exceptions.AdbRemoteError('Remote ADB failure: %s', raw_message)
    return self.RECV_MSG_TYPE(*raw_message)

Read a message from this transport and return it.

Reads a message of RECV_MSG_TYPE and returns it.  Note that this method
abstracts the data length and data read so that the caller simply gets the
data along with the header in the returned message.

Args:
timeout: timeouts.PolledTimeout to use for the operation.

Returns:
An instance of self.RECV_MSG_TYPE that was read from self.stream.

Raises:
AdbProtocolError: If an invalid response is received.
AdbRemoteError: If a FAIL response is received.


def single_qubit_matrix_to_pauli_rotations(mat: np.ndarray, atol: float=0) -> List[Tuple[(ops.Pauli, float)]]:

    def is_clifford_rotation(half_turns):
        return near_zero_mod(half_turns, 0.5, atol=atol)

    def to_quarter_turns(half_turns):
        return (round((2 * half_turns)) % 4)

    def is_quarter_turn(half_turns):
        return (is_clifford_rotation(half_turns) and ((to_quarter_turns(half_turns) % 2) == 1))

    def is_half_turn(half_turns):
        return (is_clifford_rotation(half_turns) and (to_quarter_turns(half_turns) == 2))

    def is_no_turn(half_turns):
        return (is_clifford_rotation(half_turns) and (to_quarter_turns(half_turns) == 0))
    (z_rad_before, y_rad, z_rad_after) = linalg.deconstruct_single_qubit_matrix_into_angles(mat)
    z_ht_before = ((z_rad_before / np.pi) - 0.5)
    m_ht = (y_rad / np.pi)
    m_pauli = ops.pauli_gates.X
    z_ht_after = ((z_rad_after / np.pi) + 0.5)
    if is_clifford_rotation(z_ht_before):
        if ((is_quarter_turn(z_ht_before) or is_quarter_turn(z_ht_after)) ^ (is_half_turn(m_ht) and is_no_turn((z_ht_before - z_ht_after)))):
            z_ht_before += 0.5
            z_ht_after -= 0.5
            m_pauli = ops.pauli_gates.Y
        if (is_half_turn(z_ht_before) or is_half_turn(z_ht_after)):
            z_ht_before -= 1
            z_ht_after += 1
            m_ht = (- m_ht)
    if is_no_turn(m_ht):
        z_ht_before += z_ht_after
        z_ht_after = 0
    elif is_half_turn(m_ht):
        z_ht_after -= z_ht_before
        z_ht_before = 0
    rotation_list = [(ops.pauli_gates.Z, z_ht_before), (m_pauli, m_ht), (ops.pauli_gates.Z, z_ht_after)]
    return [(pauli, ht) for (pauli, ht) in rotation_list if (not is_no_turn(ht))]

Implements a single-qubit operation with few rotations.

Args:
mat: The 2x2 unitary matrix of the operation to implement.
atol: A limit on the amount of absolute error introduced by the
construction.

Returns:
A list of (Pauli, half_turns) tuples that, when applied in order,
perform the desired operation.


def fill_wildcards(self, field=None, value=0):
    if ((field in [None, 'wildcards']) or isinstance(value, Pad)):
        return
    default_value = getattr(Match, field)
    if isinstance(default_value, IPAddress):
        if (field == 'nw_dst'):
            shift = FlowWildCards.OFPFW_NW_DST_SHIFT
            base_mask = FlowWildCards.OFPFW_NW_DST_MASK
        else:
            shift = FlowWildCards.OFPFW_NW_SRC_SHIFT
            base_mask = FlowWildCards.OFPFW_NW_SRC_MASK
        self.wildcards &= (FlowWildCards.OFPFW_ALL ^ base_mask)
        wildcard = ((value.max_prefix - value.netmask) << shift)
        self.wildcards |= wildcard
    else:
        wildcard_field = 'OFPFW_{}'.format(field.upper())
        wildcard = getattr(FlowWildCards, wildcard_field)
        if (((value == default_value) and (not (self.wildcards & wildcard))) or ((value != default_value) and (self.wildcards & wildcard))):
            self.wildcards ^= wildcard

Update wildcards attribute.

This method update a wildcards considering the attributes of the
current instance.

Args:
field (str): Name of the updated field.
value (GenericType): New value used in the field.


def uninstall(device: AndroidDevice, package_name: str) -> None:
    if is_apk_installed(device, package_name):
        try:
            device.adb.uninstall([package_name])
        except adb.AdbError as e1:
            if ADB_UNINSTALL_INTERNAL_ERROR_MSG in str(e1):
                device.log.debug('Encountered uninstall internal error, try pm remove with UID 0.')
                try:
                    device.adb.shell(['pm', 'uninstall', '-k', '--user', '0', package_name])
                    return
                except adb.AdbError as e2:
                    device.log.exception('Second attempt to uninstall failed: %s', e2)
            raise e1

Uninstall an apk on an Android device if it is installed.

Works for regular app and OEM pre-installed non-system app.

Args:
device: AndroidDevice, Mobly's Android controller object.
package_name: string, package name of the app.


def __init__(self, scope, parent, id_, name, definition=True):

        CodeEntity.__init__(self, scope, parent)
        self.id = id_
        self.name = name
        self.members = []
        self.superclasses = []
        self.member_of = None
        self.references = []
        self._definition = self if definition else None

Constructor for classes.

Args:
scope (CodeEntity): The program scope where this object belongs.
parent (CodeEntity): This object's parent in the program tree.
id: An unique identifier for this class.
name (str): The name of the class in the program.


def from_fhir_path_expression(fhir_path_expression: str, fhir_context: context.FhirPathContext, structdef_type: _fhir_path_data_types.StructureDataType, handler: primitive_handler.PrimitiveHandler, root_node_context: Optional[Builder]=None) -> 'Builder':
    ast = _ast.build_fhir_path_ast(fhir_path_expression)
    new_context = root_node_context.node if root_node_context else None
    visitor = _evaluation.FhirPathCompilerVisitor(handler, fhir_context, structdef_type, new_context)
    root = visitor.visit(ast)
    return Builder(root, handler)

Function to create an expression builder from a fhir path string.

Args:
fhir_path_expression: The FHIRPath expression to parse.
fhir_context: The context containing the FHIR resources.
structdef_type: The root structure definition for the expression.
handler: The primitive handler.
root_node_context: Optional root expression that fhir_path_expression may
reference.

Returns:
The expression Builder equivalent of the fhir_path_expression.


def unreduce_array(array, shape, axis, keepdims):

  if axis is not None and (not keepdims or keepdims is numpy._NoValue):
    if isinstance(axis, int):
      axis = axis,
    for ax in sorted(axis):
      array = numpy.expand_dims(array, ax)
  return numpy.broadcast_to(array, shape)

Reverse summing over a dimension, NumPy implementation.

Args:
array: The array that was reduced.
shape: The original shape of the array before reduction.
axis: The axis or axes that were summed.
keepdims: Whether these axes were kept as singleton axes.

Returns:
An array with axes broadcast to match the shape of the original array.


def translate_config(self, profile, merge=None, replace=None):
    result = []
    for (k, v) in self:
        other_merge = (getattr(merge, k) if merge else None)
        other_replace = (getattr(replace, k) if replace else None)
        translator = Translator(v, profile, merge=other_merge, replace=other_replace)
        result.append(translator.translate())
    return '\n'.join(result)

Translate the object to native configuration.

In this context, merge and replace means the following:

* **Merge** - Elements that exist in both ``self`` and ``merge`` will use by default the
values in ``merge`` unless ``self`` specifies a new one. Elements that exist only
in ``self`` will be translated as they are and elements present only in ``merge``
will be removed.
* **Replace** - All the elements in ``replace`` will either be removed or replaced by
elements in ``self``.

You can specify one of ``merge``, ``replace`` or none of them. If none of them are set we
will just translate configuration.

Args:
profile (list): Which profiles to use.
merge (Root): Object we want to merge with.
replace (Root): Object we want to replace.


def torch_extract_patches(image_tensor, patch_height, patch_width):
    requires_backends(torch_extract_patches, ['torch'])
    image_tensor = image_tensor.unsqueeze(0)
    patches = torch.nn.functional.unfold(image_tensor, (patch_height, patch_width), stride=(patch_height, patch_width))
    patches = patches.reshape(image_tensor.size(0), image_tensor.size(1), patch_height, patch_width, -1)
    patches = patches.permute(0, 4, 2, 3, 1).reshape(image_tensor.size(2)
    return patches.unsqueeze(0)

Utiliy function to extract patches from a given image tensor. Returns a tensor of shape (1, `patch_height`,
`patch_width`, `num_channels`x `patch_height` x `patch_width`)

Args:
image_tensor (torch.Tensor):
The image tensor to extract patches from.
patch_height (int):
The height of the patches to extract.
patch_width (int):
The width of the patches to extract.


def listen_loop(self):

        while self.listening:
            try:
                data, address = self.sock.recvfrom(self.bufsize)
                self.receive_datagram(data, address)
                if self.stats_enabled:
                    self.stats['bytes_recieved'] += len(data)
            except socket.error as error:
                if error.errno == errno.WSAECONNRESET:
                    logger.info("connection reset")
                else:
                    raise

        logger.info("Shutting down the listener...")

Starts the listen loop and executes the receieve_datagram method
whenever a packet is receieved.

Args:
None

Returns:
None


def generate_nb_data(P, R, n_cells, assignments=None):
    (genes, clusters) = P.shape
    output = np.zeros((genes, n_cells))
    if (assignments is None):
        cluster_probs = (np.ones(clusters) / clusters)
    labels = []
    for i in range(n_cells):
        if (assignments is None):
            c = np.random.choice(range(clusters), p=cluster_probs)
        else:
            c = assignments[i]
        labels.append(c)
        output[(:, i)] = np.random.negative_binomial(R[(:, c)], (1.0 - P[(:, c)]))
    return (output, np.array(labels))

Generates negative binomial data

Args:
P (array): genes x clusters
R (array): genes x clusters
n_cells (int): number of cells
assignments (list): cluster assignment of each cell. Default:
random uniform

Returns:
data array with shape genes x cells
labels - array of cluster labels


def load_intent(self, name, file_name, reload_cache=False):

        self.intents.load(name, file_name, reload_cache)
        with open(file_name) as f:
            self.padaos.add_intent(name, f.read().split('\n'))
        self.must_train = True

Loads an intent, optionally checking the cache first

Args:
name (str): The associated name of the intent
file_name (str): The location of the intent file
reload_cache (bool): Whether to refresh all of cache


def extract_derivative_feature(feature):

    first_derivative_feature = processing.derivative_extraction(
        feature, DeltaWindows=2)
    second_derivative_feature = processing.derivative_extraction(
        first_derivative_feature, DeltaWindows=2)

    feature_cube = np.concatenate(
        (feature[:, :, None], first_derivative_feature[:, :, None],
         second_derivative_feature[:, :, None]),
        axis=2)
    return feature_cube

This function extracts temporal derivative features which are
first and second derivatives.

Args:
feature (array): The feature vector which its size is: N x M

Return:
array: The feature cube vector which contains the static, first and second derivative features of size: N x M x 3


def get_plan(self, plan_code):
    return self.client._get((self.url + 'plans/{}'.format(plan_code)), headers=self.get_headers())

Check all the information of a plan for subscriptions associated with the merchant.

Args:
plan_code: Plan’s identification code for the merchant.

Returns:


def _ParseMFTEntry(self, parser_mediator, mft_entry):

    for attribute_index in range(0, mft_entry.number_of_attributes):
      try:
        mft_attribute = mft_entry.get_attribute(attribute_index)
        self._ParseMFTAttribute(parser_mediator, mft_entry, mft_attribute)

      except IOError as exception:
        parser_mediator.ProduceExtractionWarning((
            'unable to parse MFT attribute: {0:d} with error: {1!s}').format(
                attribute_index, exception))

Extracts data from a NFTS $MFT entry.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
mft_entry (pyfsntfs.file_entry): MFT entry.


def get_pixel(self, x: int, y: int) -> Tuple[(int, int, int)]:
    color = lib.TCOD_image_get_pixel(self.image_c, x, y)
    return (color.r, color.g, color.b)

Get the color of a pixel in this Image.

Args:
x (int): X pixel of the Image.  Starting from the left at 0.
y (int): Y pixel of the Image.  Starting from the top at 0.

Returns:
Tuple[int, int, int]:
An (r, g, b) tuple containing the pixels color value.
Values are in a 0 to 255 range.


def change_window(self, size_window):

        self.size_window = size_window
        self.window = self.lambert_window(
            self.size_window, self.lat0, self.lon0)

Change the region of interest

Args:
size_window (float): Radius of the region of interest (km)

Notes:
Change the attributes ``size_window`` and ``window`` to
correspond to the new region of interest.


def members(name, members_list, **kwargs):
    members_list = [salt.utils.win_functions.get_sam_name(m) for m in members_list.split(',')]
    if (not isinstance(members_list, list)):
        log.debug('member_list is not a list')
        return False
    try:
        obj_group = _get_group_object(name)
    except pywintypes.com_error as exc:
        msg = 'Failed to access group {0}. {1}'.format(name, win32api.FormatMessage(exc.excepinfo[5]))
        log.error(msg)
        return False
    existing_members = [_get_username(x) for x in obj_group.members()]
    existing_members.sort()
    members_list.sort()
    if (existing_members == members_list):
        log.info('%s membership is correct', name)
        return True
    success = True
    for member in members_list:
        if (member not in existing_members):
            try:
                obj_group.Add(('WinNT:
                log.info('User added: %s', member)
            except pywintypes.com_error as exc:
                msg = 'Failed to add {0} to {1}. {2}'.format(member, name, win32api.FormatMessage(exc.excepinfo[5]))
                log.error(msg)
                success = False
    for member in existing_members:
        if (member not in members_list):
            try:
                obj_group.Remove(('WinNT:
                log.info('User removed: %s', member)
            except pywintypes.com_error as exc:
                msg = 'Failed to remove {0} from {1}. {2}'.format(member, name, win32api.FormatMessage(exc.excepinfo[5]))
                log.error(msg)
                success = False
    return success

Ensure a group contains only the members in the list

Args:

name (str):
The name of the group to modify

members_list (str):
A single user or a comma separated list of users. The group will
contain only the users specified in this list.

Returns:
bool: ``True`` if successful, otherwise ``False``

CLI Example:

.. code-block:: bash

salt '*' group.members foo 'user1,user2,user3'


def get_student_item_dict(self, anonymous_user_id=None):
    item_id = self._serialize_opaque_key(self.scope_ids.usage_id)
    if hasattr(self, 'xmodule_runtime'):
        course_id = self.get_course_id()
        if anonymous_user_id:
            student_id = anonymous_user_id
        else:
            student_id = self.xmodule_runtime.anonymous_student_id
    else:
        course_id = 'edX/Enchantment_101/April_1'
        if (self.scope_ids.user_id is None):
            student_id = ''
        else:
            student_id = unicode(self.scope_ids.user_id)
    student_item_dict = dict(student_id=student_id, item_id=item_id, course_id=course_id, item_type='ubcpi')
    return student_item_dict

Create a student_item_dict from our surrounding context.

See also: submissions.api for details.

Args:
anonymous_user_id(str): A unique anonymous_user_id for (user, course) pair.
Returns:
(dict): The student item associated with this XBlock instance. This
includes the student id, item id, and course id.


def load_default_japanese_parser() -> Parser:
    with open(os.path.join(MODEL_DIR, 'ja.json'), encoding='utf-8') as f:
        model = json.load(f)
    return Parser(model)

Loads a parser equipped with the default Japanese model.

Returns:
A parser (:obj:`budoux.Parser`).


def refs(self, type='all', **kwargs):
    path = ('%s/%s/refs' % (self.manager.path, self.get_id()))
    data = {'type': type}
    return self.manager.gitlab.http_get(path, query_data=data, **kwargs)

List the references the commit is pushed to.

Args:
type (str): The scope of references ('branch', 'tag' or 'all')
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabGetError: If the references could not be retrieved

Returns:
list: The references the commit is pushed to.


def AddValue(self, name, number, aliases=None, description=None):
    if (name in self.values_per_name):
        raise KeyError('Value with name: {0:s} already exists.'.format(name))
    if (number in self.values_per_number):
        raise KeyError('Value with number: {0!s} already exists.'.format(number))
    for alias in (aliases or []):
        if (alias in self.values_per_alias):
            raise KeyError('Value with alias: {0:s} already exists.'.format(alias))
    enumeration_value = EnumerationValue(name, number, aliases=aliases, description=description)
    self.values.append(enumeration_value)
    self.values_per_name[name] = enumeration_value
    self.values_per_number[number] = enumeration_value
    for alias in (aliases or []):
        self.values_per_alias[alias] = enumeration_value

Adds an enumeration value.

Args:
name (str): name.
number (int): number.
aliases (Optional[list[str]]): aliases.
description (Optional[str]): description.

Raises:
KeyError: if the enumeration value already exists.


def calculate_checksum_on_bytes(
    b, algorithm=d1_common.const.DEFAULT_CHECKSUM_ALGORITHM
):

    checksum_calc = get_checksum_calculator_by_dataone_designator(algorithm)
    checksum_calc.update(b)
    return checksum_calc.hexdigest()

Calculate the checksum of ``bytes``.

Warning: This method requires the entire object to be buffered in (virtual) memory,
which should normally be avoided in production code.

Args:
b: bytes
Raw bytes

algorithm: str
Checksum algorithm, ``MD5`` or ``SHA1`` / ``SHA-1``.

Returns:
str : Checksum as a hexadecimal string, with length decided by the algorithm.


def wait_for_boot_completion(
            self, timeout=DEFAULT_TIMEOUT_BOOT_COMPLETION_SECOND):

        timeout_start = time.time()

        self.adb.wait_for_device(timeout=timeout)
        while time.time() < timeout_start + timeout:
            try:
                if self.is_boot_completed():
                    return
            except adb.AdbError:

                pass
            time.sleep(5)
        raise DeviceError(self, 'Booting process timed out')

Waits for Android framework to broadcast ACTION_BOOT_COMPLETED.

This function times out after 15 minutes.

Args:
timeout: float, the number of seconds to wait before timing out.
If not specified, no timeout takes effect.


def update_restore_inputs(self, checkpoint_key: str, shape_and_slice_spec: str) -> tuple[Sequence[str], Sequence[str]]:
    logging.vlog(1, 'Updating restore v2 inputs for %s[%s]: %s', checkpoint_key, self._object_local_name, shape_and_slice_spec)
    slices = []
    first_layout = self._from_shard_layouts[0]
    full_vocab_size = first_layout.total_rows_per_sparse_core_shard * first_layout.num_sparse_cores
    stack_dim = first_layout.unsharded_padded_shape[1]
    full_shape = [full_vocab_size, stack_dim]
    logging.vlog(1, 'Read checkpoint_key %s: %s', checkpoint_key, full_shape)
    slices.append(_shard_info_str(full_shape, trackable_base.ShardInfo(offset=[0, 0], shape=full_shape)))
    return ([checkpoint_key], slices)

Return the full shape of the stacked that is passed into restore_v2.

This shape information is required by the restore_v2 process to ensure it
loads the complete tensor from the checkpoint. The full tensor is required
to perform resharding operations.

Args:
checkpoint_key: The input checkpoint key to be read.
shape_and_slice_spec: The shape and slice spec of the checkpoint key to be
read.

Returns:
A tuple of (keys, slices) that should be passed to restore_v2 in order to
reshard according to the resharding plan. The restored tensors from
restore_v2 op will usually be passed to reshard method of this class to
get the final resharded value.


def end_episode(self, agent_indices):
    with tf.name_scope('end_episode/'):
        return tf.cond(self._is_training, (lambda : self._define_end_episode(agent_indices)), str)

Add episodes to the memory and perform update steps if memory is full.

During training, add the collected episodes of the batch indices that
finished their episode to the memory. If the memory is full, train on it,
and then clear the memory. A summary string is returned if requested at
this step.

Args:
agent_indices: Tensor containing current batch indices.

Returns:
Summary tensor.


def _WriteSerializedAttributeContainerList(self, container_type):
    if (container_type == self._CONTAINER_TYPE_EVENT):
        if (not self._serialized_event_heap.data_size):
            return
        number_of_attribute_containers = self._serialized_event_heap.number_of_events
    else:
        container_list = self._GetSerializedAttributeContainerList(container_type)
        if (not container_list.data_size):
            return
        number_of_attribute_containers = container_list.number_of_attribute_containers
    if self._serializers_profiler:
        self._serializers_profiler.StartTiming('write')
    if (container_type == self._CONTAINER_TYPE_EVENT):
        query = 'INSERT INTO event (_timestamp, _data) VALUES (?, ?)'
    else:
        query = 'INSERT INTO {0:s} (_data) VALUES (?)'.format(container_type)
    values_tuple_list = []
    for _ in range(number_of_attribute_containers):
        if (container_type == self._CONTAINER_TYPE_EVENT):
            (timestamp, serialized_data) = self._serialized_event_heap.PopEvent()
        else:
            serialized_data = container_list.PopAttributeContainer()
        if (self.compression_format == definitions.COMPRESSION_FORMAT_ZLIB):
            compressed_data = zlib.compress(serialized_data)
            serialized_data = sqlite3.Binary(compressed_data)
        else:
            compressed_data = ''
        if self._storage_profiler:
            self._storage_profiler.Sample('write', container_type, len(serialized_data), len(compressed_data))
        if (container_type == self._CONTAINER_TYPE_EVENT):
            values_tuple_list.append((timestamp, serialized_data))
        else:
            values_tuple_list.append((serialized_data,))
    self._cursor.executemany(query, values_tuple_list)
    if self._serializers_profiler:
        self._serializers_profiler.StopTiming('write')
    if (container_type == self._CONTAINER_TYPE_EVENT):
        self._serialized_event_heap.Empty()
    else:
        container_list.Empty()

Writes a serialized attribute container list.

Args:
container_type (str): attribute container type.


def get_key_pair(self, alias_name):

        uri = self.URI + "/keypair/" + alias_name
        return self._client.get(uri)

Retrieves the public and private key pair associated with the specified alias name.

Args:
alias_name: Key pair associated with the RabbitMQ

Returns:
dict: RabbitMQ certificate


def has_arg(fn, name, accept_all=False):
    arg_spec = tf_inspect.getfullargspec(fn)
    if accept_all and arg_spec.varkw is not None:
        return True
    return name in arg_spec.args or name in arg_spec.kwonlyargs

Checks if a callable accepts a given keyword argument.

Args:
fn: Callable to inspect.
name: Check if `fn` can be called with `name` as a keyword argument.
accept_all: What to return if there is no parameter called `name` but the
function accepts a `**kwargs` argument.

Returns:
bool, whether `fn` accepts a `name` keyword argument.


def compstat(sdat, tstart=None, tend=None):
    data = sdat.tseries_between(tstart, tend)
    time = data['t'].values
    delta_time = (time[(- 1)] - time[0])
    data = data.iloc[(:, 1:)].values
    mean = (np.trapz(data, x=time, axis=0) / delta_time)
    rms = np.sqrt((np.trapz(((data - mean) ** 2), x=time, axis=0) / delta_time))
    with open(misc.out_name('statistics.dat'), 'w') as out_file:
        mean.tofile(out_file, sep=' ', format='%10.5e')
        out_file.write('\n')
        rms.tofile(out_file, sep=' ', format='%10.5e')
        out_file.write('\n')

Compute statistics from series output by StagYY.

Create a file 'statistics.dat' containing the mean and standard deviation
of each series on the requested time span.

Args:
sdat (:class:`~stagpy.stagyydata.StagyyData`): a StagyyData instance.
tstart (float): starting time. Set to None to start at the beginning of
available data.
tend (float): ending time. Set to None to stop at the end of available
data.


def AddTokenToState(self, newline, dry_run, must_split=False):
    self._PushParameterListState(newline)
    penalty = 0
    if newline:
        penalty = self._AddTokenOnNewline(dry_run, must_split)
    else:
        self._AddTokenOnCurrentLine(dry_run)
    penalty += self._CalculateComprehensionState(newline)
    penalty += self._CalculateParameterListState(newline)
    return self.MoveStateToNextToken() + penalty

Add a token to the format decision state.

Allow the heuristic to try out adding the token with and without a newline.
Later on, the algorithm will determine which one has the lowest penalty.

Arguments:
newline: (bool) Add the token on a new line if True.
dry_run: (bool) Don't commit whitespace changes to the FormatToken if
True.
must_split: (bool) A newline was required before this token.

Returns:
The penalty of splitting after the current token.


def get_tokens(condition):
    try:
        ast_tokens = list(ast.walk(ast.parse(condition.strip())))
    except SyntaxError as exception:
        Logger.get_logger(__name__).error('Syntax error: %s', exception)
        ast_tokens = []
    return ast_tokens

Get AST tokens for Python condition.

Returns:
list: list of AST tokens


async def _async_forward(async_chunks: collections.AsyncIterable,
                         out: Optional[Union[TeeCapture, IO[str]]]
                         ) -> Optional[str]:

    capture = isinstance(out, TeeCapture)
    out_pipe = out.out_pipe if isinstance(out, TeeCapture) else out

    chunks = [] if capture else None
    async for chunk in async_chunks:
        if not isinstance(chunk, str):
            chunk = chunk.decode()
        if out_pipe:
            print(chunk, file=out_pipe, end='')
        if chunks is not None:
            chunks.append(chunk)

    return ''.join(chunks) if chunks is not None else None

Prints/captures output from the given asynchronous iterable.

Args:
async_chunks: An asynchronous source of bytes or str.
out: Where to put the chunks.

Returns:
The complete captured output, or else None if the out argument wasn't a
TeeCapture instance.


def _create_hunt(self, name, args):
    runner_args = self.grr_api.types.CreateHuntRunnerArgs()
    runner_args.description = self.reason
    hunt = self.grr_api.CreateHunt(flow_name=name, flow_args=args, hunt_runner_args=runner_args)
    print('{0!s}: Hunt created'.format(hunt.hunt_id))
    self._check_approval_wrapper(hunt, hunt.Start)
    return hunt

Create specified hunt.

Args:
name: string containing hunt name.
args: proto (*FlowArgs) for type of hunt, as defined in GRR flow proto.

Returns:
The newly created GRR hunt object.

Raises:
ValueError: if approval is needed and approvers were not specified.


def topics(self, exclude_internal_topics=True):

        topics = set(self._partitions.keys())
        if exclude_internal_topics:
            return topics - self.internal_topics
        else:
            return topics

Get set of known topics.

Arguments:
exclude_internal_topics (bool): Whether records from internal topics
(such as offsets) should be exposed to the consumer. If set to
True the only way to receive records from an internal topic is
subscribing to it. Default True

Returns:
set: {topic (str), ...}


def scp_file_remote_to_local(self, remote_path, local_path):

        scp_command = [
            'scp',
            '-o',
            'StrictHostKeyChecking=no',
            '-i',
            self.browser_config.get('ssh_key_path'),
            '%s@%s:"%s"' %
            (
                self.browser_config.get('username'),
                self.get_ip(),
                remote_path
            ),
            local_path
        ]
        self.info_log(
            "executing command: %s" %
            ' '.join(scp_command)
        )
        p = Popen(scp_command)
        p.wait()

Scp a remote file to local

Args:
remote_path (str)
local_path (str)


def typing(self, room: Room, timeout: int = 5000):

        path = f'/rooms/{quote(room.room_id)}/typing/{quote(self.user_id)}'
        return self.api._send('PUT', path, {'typing': True, 'timeout': timeout})

Send typing event directly to api

Args:
room: room to send typing event to
timeout: timeout for the event, in ms


def _ConvertListToObject(cls, json_list):

    list_value = []
    for json_list_element in json_list:
      if isinstance(json_list_element, dict):
        list_value.append(cls._ConvertDictToObject(json_list_element))

      elif isinstance(json_list_element, list):
        list_value.append(cls._ConvertListToObject(json_list_element))

      else:
        list_value.append(json_list_element)

    return list_value

Converts a JSON list into an object.

Args:
json_list (list[object]): JSON serialized objects.

Returns:
list[object]: a deserialized list.


def sentencecase(string):

    joiner = ' '
    string = re.sub(r"[\-_\.\s]", joiner, str(string))
    if not string:
        return string
    return capitalcase(trimcase(
        re.sub(r"[A-Z]", lambda matched: joiner +
                                         lowercase(matched.group(0)), string)
    ))

Convert string into sentence case.
First letter capped and each punctuations are joined with space.

Args:
string: String to convert.

Returns:
string: Sentence cased string.


def frequency_to_probability(frequency_map, decorator=(lambda f: f)):
    total = sum(frequency_map.values())
    return {k: decorator((v / total)) for (k, v) in frequency_map.items()}

Transform a ``frequency_map`` into a map of probability using the sum of all frequencies as the total.

Example:
>>> frequency_to_probability({'a': 2, 'b': 2})
{'a': 0.5, 'b': 0.5}

Args:
frequency_map (dict): The dictionary to transform
decorator (function): A function to manipulate the probability

Returns:
Dictionary of ngrams to probability


def add(self, handler):

        self._handlers.append(handler)

        static_paths = set(h.static_path() for h in self.handlers)
        static_paths.discard(None)
        if len(static_paths) > 1:
            raise RuntimeError("More than one static path requested for app: %r" % list(static_paths))
        elif len(static_paths) == 1:
            self._static_path = static_paths.pop()
        else:
            self._static_path = None

Add a handler to the pipeline used to initialize new documents.

Args:
handler (Handler) : a handler for this Application to use to
process Documents


def _load_checkpoints(self, checkpointDirs):
    memo_lookup_table = {}
    for checkpoint_dir in checkpointDirs:
        logger.info('Loading checkpoints from {}'.format(checkpoint_dir))
        checkpoint_file = os.path.join(checkpoint_dir, 'tasks.pkl')
        try:
            with open(checkpoint_file, 'rb') as f:
                while True:
                    try:
                        data = pickle.load(f)
                        memo_fu = Future()
                        if data['exception']:
                            memo_fu.set_exception(data['exception'])
                        else:
                            memo_fu.set_result(data['result'])
                        memo_lookup_table[data['hash']] = memo_fu
                    except EOFError:
                        break
        except FileNotFoundError:
            reason = 'Checkpoint file was not found: {}'.format(checkpoint_file)
            logger.error(reason)
            raise BadCheckpoint(reason)
        except Exception:
            reason = 'Failed to load checkpoint: {}'.format(checkpoint_file)
            logger.error(reason)
            raise BadCheckpoint(reason)
        logger.info('Completed loading checkpoint:{0} with {1} tasks'.format(checkpoint_file, len(memo_lookup_table.keys())))
    return memo_lookup_table

Load a checkpoint file into a lookup table.

The data being loaded from the pickle file mostly contains input
attributes of the task: func, args, kwargs, env...
To simplify the check of whether the exact task has been completed
in the checkpoint, we hash these input params and use it as the key
for the memoized lookup table.

Args:
- checkpointDirs (list) : List of filepaths to checkpoints
Eg. ['runinfo/001', 'runinfo/002']

Returns:
- memoized_lookup_table (dict)


def __init__(self, topic_path, add_uuids=None, expansion_service=None):
    super().__init__()
    self._source = _WriteExternal(topic_path=topic_path, add_uuids=add_uuids, expansion_service=expansion_service)

Initializes ``WriteToPubSubLite``.

Args:
topic_path: A Pub/Sub Lite Topic path.
add_uuids: Whether to add uuids to the 'x-goog-pubsublite-dataflow-uuid'
uuid attribute. Defaults to False.


def _get_required_param(self, param_name):
    value = self.request.get(param_name)
    if (not value):
        raise errors.NotEnoughArgumentsError((param_name + ' not specified'))
    return value

Get a required request parameter.

Args:
param_name: name of request parameter to fetch.

Returns:
parameter value

Raises:
errors.NotEnoughArgumentsError: if parameter is not specified.


def traverse_by(self, fixers, traversal):

        if not fixers:
            return
        for node in traversal:
            for fixer in fixers[node.type]:
                results = fixer.match(node)
                if results:
                    new = fixer.transform(node, results)
                    if new is not None:
                        node.replace(new)
                        node = new

Traverse an AST, applying a set of fixers to each node.

This is a helper method for refactor_tree().

Args:
fixers: a list of fixer instances.
traversal: a generator that yields AST nodes.

Returns:
None


def is_slice_but_not_on_extension(element_definition: message.Message) -> bool:
    return is_slice_element(element_definition) and (not is_slice_on_extension(element_definition))

Returns `True` if the `element_definition` is a slice not on extension.

Args:
element_definition: The element definition that we are checking.


Retrieve a given byte range [start, end) from this download.

Range must be in this form:
0 <= start < end: Fetch the bytes from start to end.

Args:
start: (int) Initial byte offset.
end: (int) Final byte offset, exclusive.

Returns:
(string) A buffer containing the requested data.


def dumps(self):
    old_stream = self.stream
    try:
        self.stream = six.StringIO()
        self.write_table()
        tabular_text = self.stream.getvalue()
    finally:
        self.stream = old_stream
    return tabular_text

Get rendered tabular text from the table data.

Only available for text format table writers.

Returns:
str: Rendered tabular text.


def __add__(self, other):

        sum_rel = DistributedReliability(self.thresholds, self.obs_threshold)
        sum_rel.frequencies = self.frequencies + other.frequencies
        return sum_rel

Add two DistributedReliability objects together and combine their values.

Args:
other: a DistributedReliability object

Returns:
A DistributedReliability Object


def detect_framebuffer(self, glo=None) -> 'Framebuffer':
    res = Framebuffer.__new__(Framebuffer)
    (res.mglo, res._size, res._samples, res._glo) = self.mglo.detect_framebuffer(glo)
    res._color_attachments = None
    res._depth_attachment = None
    res.ctx = self
    res.extra = None
    return res

Detect framebuffer.

Args:
glo (int): Framebuffer object.

Returns:
:py:class:`Framebuffer` object


def convertDate(self, date, prefix='', weekday=False):
    dayString = self.convertDay(date, prefix=prefix, weekday=weekday)
    timeString = self.convertTime(date)
    return ((dayString + ' at ') + timeString)

Convert a datetime object representing into a human-ready
string that can be read, spoken aloud, etc. In effect, runs
both convertDay and convertTime on the input, merging the results.

Args:
date (datetime.date): A datetime object to be converted into text.
prefix (str): An optional argument that prefixes the converted
string. For example, if prefix="in", you'd receive "in two
days", rather than "two days", while the method would still
return "tomorrow" (rather than "in tomorrow").
weekday (bool): An optional argument that returns "Monday, Oct. 1"
if True, rather than "Oct. 1".

Returns:
A string representation of the input day and time.


def GetFileEntryByPathSpec(self, path_spec):

    volume_index = lvm.LVMPathSpecGetVolumeIndex(path_spec)

    if volume_index is None:
      location = getattr(path_spec, 'location', None)
      if location is None or location != self.LOCATION_ROOT:
        return None

      return lvm_file_entry.LVMFileEntry(
          self._resolver_context, self, path_spec, is_root=True,
          is_virtual=True)

    if (volume_index < 0 or
        volume_index >= self._vslvm_volume_group.number_of_logical_volumes):
      return None

    return lvm_file_entry.LVMFileEntry(self._resolver_context, self, path_spec)

Retrieves a file entry for a path specification.

Args:
path_spec (PathSpec): path specification.

Returns:
LVMFileEntry: a file entry or None if not available.


def _summary(tag, hparams_plugin_data):
    summary = tf.compat.v1.Summary()
    summary.value.add(tag=tag, metadata=metadata.create_summary_metadata(hparams_plugin_data))
    return summary

Returns a summary holding the given HParamsPluginData message.

Helper function.

Args:
tag: string. The tag to use.
hparams_plugin_data: The HParamsPluginData message to use.


Compute the quadratic estimate of the centroid in a 2d-array.

Args:
data (2darray): two dimensional data array

Returns
center (tuple): centroid estimate on the row and column directions,
respectively


def yaml_dump(data, stream=None):

    return yaml.dump(
        data,
        stream=stream,
        Dumper=Dumper,
        default_flow_style=False
    )

Dump data to a YAML string/file.

Args:
data (YamlData):
The data to serialize as YAML.
stream (TextIO):
The file-like object to save to. If given, this function will write
the resulting YAML to that stream.

Returns:
str: The YAML string.


def get(self, rid, data_callback=None, raise_on_error=True):
    cached_data = None
    ds_data = self.ds.get(rid, raise_on_error=False)
    if (ds_data is not None):
        expired = True
        if (ds_data.get('found') is True):
            if (self.ttl < int(ds_data.get('_source', {}).get('cache-date', 0))):
                cached_data = ds_data.get('_source', {}).get('cache-data')
                expired = False
                self.tcex.log.debug('Using cached data for ({}).'.format(rid))
            else:
                self.tcex.log.debug('Cached data is expired for ({}).'.format(rid))
        if (expired or (ds_data.get('found') is False)):
            if callable(data_callback):
                cached_data = data_callback(rid)
                self.tcex.log.debug('Using callback data for ({}).'.format(rid))
                if cached_data:
                    self.update(rid, cached_data, raise_on_error)
    return cached_data

Get cached data from the data store.

Args:
rid (str): The record identifier.
data_callback (callable): A method that will return the data.
raise_on_error (bool): If True and not r.ok this method will raise a RunTimeError.

Returns:
object : Python request response.


def vr60baro(msg):
    d = hex2bin(data(msg))
    if (d[34] == '0'):
        return None
    sign = int(d[35])
    value = bin2int(d[36:45])
    if ((value == 0) or (value == 511)):
        return 0
    value = ((value - 512) if sign else value)
    roc = (value * 32)
    return roc

Vertical rate from barometric measurement, this value may be very noisy.

Args:
msg (String): 28 bytes hexadecimal message (BDS60) string

Returns:
int: vertical rate in feet/minutes


def __init__(self, storage_writer):

    super(StorageFileMergeReader, self).__init__(storage_writer)
    self._serializer = json_serializer.JSONAttributeContainerSerializer
    self._serializers_profiler = None

Initializes a storage merge reader.

Args:
storage_writer (StorageWriter): storage writer.


def encode(cls, command):

        args = []
        for arg in command.args:
            if not isinstance(arg, str):
                arg = str(arg)

            if "," in arg or arg.startswith(" ") or arg.endswith(" ") or arg.startswith("hex:"):
                arg = "hex:{}".format(hexlify(arg.encode('utf-8')).decode('utf-8'))

            args.append(arg)

        argstr = ""

        if len(args) > 0:
            argstr = " {" + ",".join(args) + "}"

        return command.name + argstr

Encode a command as an unambiguous string.

Args:
command (Command): The command to encode.

Returns:
str: The encoded command


def __init__(self, table_id=Meter.OFPM_ALL):

        super().__init__(InstructionType.OFPIT_GOTO_TABLE)
        self.table_id = table_id

Create a InstructionGotoTable with the optional parameters below.

Args:
length (int): Length of this struct in bytes.
table_id (int): set next table in the lookup pipeline.


def validate_allowed_values(allowed_values, value):
    if ((not allowed_values) or isinstance(value, CFNParameter)):
        return True
    return (value in allowed_values)

Support a variable defining which values it allows.

Args:
allowed_values (Optional[list]): A list of allowed values from the
variable definition
value (obj): The object representing the value provided for the
variable

Returns:
bool: Boolean for whether or not the value is valid.


def to_value_list(original_strings, corenlp_values=None):

    assert isinstance(original_strings, (list, tuple, set))
    if corenlp_values is not None:
        assert isinstance(corenlp_values, (list, tuple, set))
        assert len(original_strings) == len(corenlp_values)
        return list(set(to_value(x, y) for (x, y)
                        in zip(original_strings, corenlp_values)))
    else:
        return list(set(to_value(x) for x in original_strings))

Convert a list of strings to a list of Values

Args:
original_strings (list[basestring])
corenlp_values (list[basestring or None])
Returns:
list[Value]


def is_stateful(self) -> bool:
    return False

Indicates whether this ThresholdFn is stateful.

Returns:
bool: Always False for `FixedThreshold` as it is stateless.


def create(self, emails, displayName=None, firstName=None, lastName=None, avatar=None, orgId=None, roles=None, licenses=None, **request_parameters):
    check_type(emails, list, may_be_none=False)
    check_type(displayName, basestring)
    check_type(firstName, basestring)
    check_type(lastName, basestring)
    check_type(avatar, basestring)
    check_type(orgId, basestring)
    check_type(roles, list)
    check_type(licenses, list)
    post_data = dict_from_items_with_values(request_parameters, emails=emails, displayName=displayName, firstName=firstName, lastName=lastName, avatar=avatar, orgId=orgId, roles=roles, licenses=licenses)
    json_data = self._session.post(API_ENDPOINT, json=post_data)
    return self._object_factory(OBJECT_TYPE, json_data)

Create a new user account for a given organization

Only an admin can create a new user account.

Args:
emails(`list`): Email address(es) of the person (list of strings).
displayName(basestring): Full name of the person.
firstName(basestring): First name of the person.
lastName(basestring): Last name of the person.
avatar(basestring): URL to the person's avatar in PNG format.
orgId(basestring): ID of the organization to which this
person belongs.
roles(`list`): Roles of the person (list of strings containing
the role IDs to be assigned to the person).
licenses(`list`): Licenses allocated to the person (list of
strings - containing the license IDs to be allocated to the
person).
**request_parameters: Additional request parameters (provides
support for parameters that may be added in the future).

Returns:
Person: A Person object with the details of the created person.

Raises:
TypeError: If the parameter types are incorrect.
ApiError: If the Webex Teams cloud returns an error.


def AddSerializedFile(self, serialized_file_desc_proto):

    from google.protobuf import descriptor_pb2
    file_desc_proto = descriptor_pb2.FileDescriptorProto.FromString(
        serialized_file_desc_proto)
    self.Add(file_desc_proto)

Adds the FileDescriptorProto and its types to this pool.

Args:
serialized_file_desc_proto: A bytes string, serialization of the
FileDescriptorProto to add.


def random_square_mask(shape, fraction):

  mask = np.ones(shape)

  patch_area = shape[0]*shape[1]*fraction
  patch_dim = np.int(math.floor(math.sqrt(patch_area)))
  if patch_area == 0 or patch_dim == 0:
    return mask

  x = np.random.randint(shape[0] - patch_dim)
  y = np.random.randint(shape[1] - patch_dim)

  mask[x:(x + patch_dim), y:(y + patch_dim), :] = 0

  return mask

Create a numpy array with specified shape and masked fraction.

Args:
shape: tuple, shape of the mask to create.
fraction: float, fraction of the mask area to populate with `mask_scalar`.

Returns:
numpy.array: A numpy array storing the mask.


def save_data(self, filename):

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

Save the assimilated data to a file.

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


class AllVote(LabelAggregation):

    def __init__(self, **kwargs):

        def inner(predictions: Iterable[int]) -> int:
            return self._outlier_label if all(map(lambda p: p == self._outlier_label, predictions)) else self._normal_label
        super().__init__(agg_func=inner, **kwargs)

Aggregates anomaly labels using an "all vote" (AND) scheme.

This `AggregationFn` implements an "all vote" strategy. It aggregates
anomaly labels such that the result is considered an outlier only if all
input `AnomalyPrediction` objects are labeled as outliers.

Example:
If input labels are [outlier, outlier, outlier], and outlier_label=1,
then the aggregated label will be outlier (1).
If input labels are [outlier, normal, outlier], and outlier_label=1,
then the aggregated label will be normal (0).

Args:
normal_label (int): The integer label for normal predictions. Defaults to 0.
outlier_label (int): The integer label for outlier predictions. Defaults to
1.
**kwargs: Additional keyword arguments to pass to the base
`LabelAggregation` class.


def _find_and_cache_best_function(self, dispatch_type):
    result = self._dispatch_table.get(dispatch_type)
    if result:
        return result
    with self._write_lock:
        try:
            dispatch_mro = dispatch_type.mro()
        except TypeError:
            dispatch_mro = ()
        best_match = None
        result_type = None
        for (candidate_type, candidate_func) in self.implementations:
            if (not issubclass(dispatch_type, candidate_type)):
                continue
            try:
                match = dispatch_mro.index(candidate_type)
            except ValueError:
                match = None
            if (best_match is None):
                if (result and (match is None)):
                    if self._preferred(candidate_type, over=result_type):
                        result = candidate_func
                        result_type = candidate_type
                    elif self._preferred(result_type, over=candidate_type):
                        pass
                    else:
                        raise TypeError(('Two candidate implementations found for multimethod function %s (dispatch type %s) and neither is preferred.' % (self.func_name, dispatch_type)))
                else:
                    result = candidate_func
                    result_type = candidate_type
                    best_match = match
            if ((match or 0) < (best_match or 0)):
                result = candidate_func
                result_type = candidate_type
                best_match = match
        self._dispatch_table[dispatch_type] = result
        return result

Finds the best implementation of this function given a type.

This function caches the result, and uses locking for thread safety.

Returns:
Implementing function, in below order of preference:
1. Explicitly registered implementations (through
multimethod.implement) for types that 'dispatch_type' either is
or inherits from directly.
2. Explicitly registered implementations accepting an abstract type
(interface) in which dispatch_type participates (through
abstract_type.register() or the convenience methods).
3. Default behavior of the multimethod function. This will usually
raise a NotImplementedError, by convention.

Raises:
TypeError: If two implementing functions are registered for
different abstract types, and 'dispatch_type' participates in
both, and no order of preference was specified using
prefer_type.


def _process_skipability(self, feed_item, item):
    if feed_item.get(FieldMap.PLACEMENT_SKIPPABLE, False):
        if not 'videoSettings' in item:
            item['videoSettings'] = {}
        item['videoSettings']['skippableSettings'] = {'skippable': feed_item.get(FieldMap.PLACEMENT_SKIPPABLE, False), 'skipOffset': {}, 'progressOffset': {}}
        skippable_settings = item['videoSettings']['skippableSettings']
        if feed_item.get(FieldMap.PLACEMENT_SKIP_OFFSET_SECONDS, None):
            skippable_settings['skipOffset']['offsetSeconds'] = feed_item.get(FieldMap.PLACEMENT_SKIP_OFFSET_SECONDS, None)
        if feed_item.get(FieldMap.PLACEMENT_SKIP_OFFSET_PERCENTAGE, None):
            skippable_settings['skipOffset']['offsetPercentage'] = feed_item.get(FieldMap.PLACEMENT_SKIP_OFFSET_PERCENTAGE, None)
        if feed_item.get(FieldMap.PLACEMENT_PROGRESS_OFFSET_SECONDS, None):
            skippable_settings['progressOffset']['offsetSeconds'] = feed_item.get(FieldMap.PLACEMENT_SKIP_OFFSET_SECONDS, None)
        if feed_item.get(FieldMap.PLACEMENT_PROGRESS_OFFSET_PERCENTAGE, None):
            skippable_settings['progressOffset']['offsetPercentage'] = feed_item.get(FieldMap.PLACEMENT_SKIP_OFFSET_PERCENTAGE, None)
    elif 'skippableSettings' in item and 'videoSettings' in item:
        del item['videoSettings']['skippableSettings']

Process skipability settings.

Args:
feed_item: A feed item representing a placement from the bulkdozer feed;
item: A campaign manager placement object to be updated with the
skipability settings defined in the feed item


def _append_defects(self, part, part_content_type):

        part_defects = {}

        for e in part.defects:
            defects = "{}: {}".format(e.__class__.__name__, e.__doc__)
            self._defects_categories.add(e.__class__.__name__)
            part_defects.setdefault(part_content_type, []).append(defects)
            log.debug("Added defect {!r}".format(defects))

        if part_defects:
            self._has_defects = True

            self._defects.append(part_defects)

Add new defects and defects categories to object attributes.

The defects are a list of all the problems found
when parsing this message.

Args:
part (string): mail part
part_content_type (string): content type of part


def triggered(self):
    trigs = [x[1].triggered(x[0]) for x in self.inputs]
    if (self.trigger_combiner == self.OrTriggerCombiner):
        return (True in trigs)
    return (False not in trigs)

Test if we should trigger our operation.

We test the trigger condition on each of our inputs and then
combine those triggers using our configured trigger combiner
to get an overall result for whether this node is triggered.

Returns:
bool: True if we should trigger and False otherwise


def _wrap_result(self, response):

        if isinstance(response, int):
            response = self._wrap_response(response)

        return HandlerResult(
            status=HandlerStatus.RETURN,
            message_out=self._response_proto(**response),
            message_type=self._response_type)

Wraps child's response in a HandlerResult to be sent back to client.

Args:
response (enum or dict): Either an integer status enum, or a dict
of attributes to be added to the protobuf response.


def create_explicit(bounds):

    safe_bounds = sorted(float(x) for x in bounds)
    if len(safe_bounds) != len(set(safe_bounds)):
        raise ValueError(u'Detected two elements of bounds that are the same')
    return sc_messages.Distribution(
        bucketCounts=[0] * (len(safe_bounds) + 1),
        explicitBuckets=sc_messages.ExplicitBuckets(bounds=safe_bounds))

Creates a new instance of distribution with explicit buckets.

bounds is an iterable of ordered floats that define the explicit buckets

Args:
bounds (iterable[float]): initializes the bounds

Return:
:class:`endpoints_management.gen.servicecontrol_v1_messages.Distribution`

Raises:
ValueError: if the args are invalid for creating an instance


def _VerifyValues(self, input_sizes=None, filter_sizes=None, strides=None, dilations=None, padding=None, data_format_src='NDHWC', data_format_dst='NDHWC', expected=None, op_name='Conv3D'):
    total_size_1 = np.prod(input_sizes)
    total_size_2 = np.prod(filter_sizes)
    x1 = np.reshape([f * 1.0 / total_size_1 for f in range(1, total_size_1 + 1)], input_sizes)
    x2 = np.reshape([f * 1.0 / total_size_2 for f in range(1, total_size_2 + 1)], filter_sizes)
    strides = [1] + strides + [1]
    if dilations is None:
        dilations = [1, 1, 1]
    dilations = [1] + dilations + [1]
    expected = test_utils.ConvertBetweenDataFormats(expected, data_format_src, data_format_dst)
    x1 = test_utils.ConvertBetweenDataFormats(x1, data_format_src, data_format_dst)
    input_sizes = test_utils.PermuteDimsBetweenDataFormats(input_sizes, data_format_src, data_format_dst)
    strides = test_utils.PermuteDimsBetweenDataFormats(strides, data_format_src, data_format_dst)
    dilations = test_utils.PermuteDimsBetweenDataFormats(dilations, data_format_src, data_format_dst)
    with self.session() as sess:
        t1 = array_ops.placeholder(dtypes.bfloat16, shape=input_sizes)
        t2 = array_ops.placeholder(dtypes.bfloat16, shape=filter_sizes)
        with self.test_scope():
            if op_name == 'Conv':
                conv_format = 'CHANNELS_LAST' if data_format_dst == 'NDHWC' else 'CHANNELS_FIRST'
                out = gen_nn_ops.conv(t1, t2, strides=strides, padding=padding, data_format=conv_format, dilations=dilations)
            elif op_name == 'Conv3D':
                out = nn_ops.conv3d(t1, t2, strides=strides, padding=padding, data_format=data_format_dst, dilations=dilations)
            else:
                raise ValueError('Invalid op name: %s' % op_name)
        value = sess.run(out, {t1: x1, t2: x2})
        self.assertAllCloseAccordingToType(expected, value)

Tests that tf.nn.conv3d produces the expected value.

Args:
input_sizes: Input tensor dimensions in [batch, input_rows, input_cols,
input_depth].
filter_sizes: Filter tensor dimensions in [kernel_rows, kernel_cols,
input_depth, output_depth].
strides: Strides.
dilations: RHS dilations.
padding: Padding type.
data_format_src: Data format input is in.
data_format_dst: Data format verification will run and input is converted
to.
expected: Expected output.
op_name: Name of operation to test (Conv/Conv2D)


def auditlog(*, event, actor, data, level=logging.INFO):

    try:
        entry = AuditLog()
        entry.event = event
        entry.actor = actor
        entry.data = data

        db.session.add(entry)
        db.session.commit()

        _AUDIT_LOGGER.log(
            logging.getLevelName(level) if type(level) == str else level,
            {
                'event': event,
                'actor': actor,
                'data': data,
            }
        )

    except Exception:
        logging.getLogger(__name__).exception('Failed adding audit log event')
        db.session.rollback()

Generate and insert a new event

Args:
event (`str`): Action performed
actor (`str`): Actor (user or subsystem) triggering the event
data (`dict`): Any extra data necessary for describing the event
level (`str` or `int`): Log level for the message. Uses standard python logging level names / numbers

Returns:
`None`


def ParseMessageRow(self, parser_mediator, query, row, **unused_kwargs):

    query_hash = hash(query)

    event_data = KikIOSMessageEventData()
    event_data.body = self._GetRowValue(query_hash, row, 'ZBODY')
    event_data.displayname = self._GetRowValue(query_hash, row, 'ZDISPLAYNAME')
    event_data.message_status = self._GetRowValue(query_hash, row, 'ZSTATE')
    event_data.message_type = self._GetRowValue(query_hash, row, 'ZTYPE')
    event_data.offset = self._GetRowValue(query_hash, row, 'id')
    event_data.query = query
    event_data.username = self._GetRowValue(query_hash, row, 'ZUSERNAME')

    timestamp = self._GetRowValue(query_hash, row, 'ZRECEIVEDTIMESTAMP')

    timestamp = int(timestamp)
    date_time = dfdatetime_cocoa_time.CocoaTime(timestamp=timestamp)
    event = time_events.DateTimeValuesEvent(
        date_time, definitions.TIME_DESCRIPTION_CREATION)
    parser_mediator.ProduceEventWithEventData(event, event_data)

Parses a message row.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
query (str): query that created the row.
row (sqlite3.Row): row.


def convert(model_path: str, out_file: str):
    print('Converting', model_path, 'to', out_file, '...')
    import tensorflow as tf
    from precise.model import load_precise_model
    from keras import backend as K
    (out_dir, filename) = split(out_file)
    out_dir = (out_dir or '.')
    os.makedirs(out_dir, exist_ok=True)
    K.set_learning_phase(0)
    model = load_precise_model(model_path)
    out_name = 'net_output'
    tf.identity(model.output, name=out_name)
    print('Output node name:', out_name)
    print('Output folder:', out_dir)
    sess = K.get_session()
    tf.train.write_graph(sess.graph.as_graph_def(), out_dir, (filename + 'txt'), as_text=True)
    print('Saved readable graph to:', (filename + 'txt'))
    from tensorflow.python.framework import graph_util
    from tensorflow.python.framework import graph_io
    cgraph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), [out_name])
    graph_io.write_graph(cgraph, out_dir, filename, as_text=False)
    if isfile((model_path + '.params')):
        copyfile((model_path + '.params'), (out_file + '.params'))
    print('Saved graph to:', filename)
    del sess

Converts an HD5F file from Keras to a .pb for use with TensorFlow

Args:
model_path: location of Keras model
out_file: location to write protobuf


def reminder_date(self, reminder_date):

        if not self.can_update():
            self._tcex.handle_error(910, [self.type])

        reminder_date = self._utils.format_datetime(reminder_date, date_format='%Y-%m-%dT%H:%M:%SZ')
        self._data['reminderDate'] = reminder_date
        request = {'reminderDate': reminder_date}
        return self.tc_requests.update(self.api_type, self.api_sub_type, self.unique_id, request)

Sets the task reminder_date
Args:
reminder_date: Converted to %Y-%m-%dT%H:%M:%SZ date format


def get_new_doctest_files(repo, base_commit, branching_commit) -> List[str]:
    for diff_obj in branching_commit.diff(base_commit):
        if diff_obj.a_path != 'utils/not_doctested.txt':
            continue
        folder = Path(repo.working_dir)
        with checkout_commit(repo, branching_commit):
            with open(folder / 'utils/not_doctested.txt', 'r', encoding='utf-8') as f:
                old_content = f.read()
        with open(folder / 'utils/not_doctested.txt', 'r', encoding='utf-8') as f:
            new_content = f.read()
        removed_content = {x.split(' ')[0] for x in old_content.split('\n')} - {x.split(' ')[0] for x in new_content.split('\n')}
        return sorted(removed_content)
    return []

Get the list of files that were removed from "utils/not_doctested.txt", between `base_commit` and
`branching_commit`.

Returns:
`List[str]`: List of files that were removed from "utils/not_doctested.txt".


def get_point(
        self, x: float = 0, y: float = 0, z: float = 0, w: float = 0
    ) -> float:

        return float(lib.NoiseGetSample(self._tdl_noise_c, (x, y, z, w)))

Return the noise value at the (x, y, z, w) point.

Args:
x (float): The position on the 1st axis.
y (float): The position on the 2nd axis.
z (float): The position on the 3rd axis.
w (float): The position on the 4th axis.


def decode_list(cls, obj, element_type):

        if not isinstance(obj, list):
            raise Exception("expected a python list")

        return list(map(lambda x: cls.do_decode(x, element_type), obj))

Decodes json into a list, handling conversion of the elements.

Args:
obj: the json object to decode
element_type: a class object which is the conjure type of
the elements in this list.
Returns:
A python list where the elements are instances of type
element_type.


def ParseEnum(field, value):

  enum_descriptor = field.enum_type
  try:
    number = int(value, 0)
  except ValueError:

    enum_value = enum_descriptor.values_by_name.get(value, None)
    if enum_value is None:
      raise ValueError('Enum type "%s" has no value named %s.' %
                       (enum_descriptor.full_name, value))
  else:

    enum_value = enum_descriptor.values_by_number.get(number, None)
    if enum_value is None:
      raise ValueError('Enum type "%s" has no value with number %d.' %
                       (enum_descriptor.full_name, number))
  return enum_value.number

Parse an enum value.

The value can be specified by a number (the enum value), or by
a string literal (the enum name).

Args:
field: Enum field descriptor.
value: String value.

Returns:
Enum value number.

Raises:
ValueError: If the enum value could not be parsed.


def _write_init_models(self, filenames):
    self.write(destination=self.output_directory, filename='__init__.py', template_name='__init_model__.py.tpl', filenames=self._prepare_filenames(filenames), class_prefix=self._class_prefix, product_accronym=self._product_accronym, header=self.header_content)

Write init file

Args:
filenames (dict): dict of filename and classes


def run_inside_wrap_function_in_eager_mode(graph_function):

    def wrap_and_execute(self):
        if context.executing_eagerly():
            wrapped = wrap_function.wrap_function(graph_function, [self])
            wrapped()
        else:
            graph_function(self)
    return wrap_and_execute

Decorator to execute the same graph code in eager and graph modes.

In graph mode, we just execute the graph_function passed as argument. In eager
mode, we wrap the function using wrap_function and then execute the wrapped
result.

Args:
graph_function: python function containing graph code to be wrapped

Returns:
decorated function


def _handle_captcha(captcha_data, message=''):
    from tempfile import NamedTemporaryFile
    tmpf = NamedTemporaryFile(suffix='.png')
    tmpf.write(captcha_data)
    tmpf.flush()
    captcha_text = input(('Please take a look at the captcha image "%s" and provide the code:' % tmpf.name))
    tmpf.close()
    return captcha_text

Called when a captcha must be solved
Writes the image to a temporary file and asks the user to enter the code.

Args:
captcha_data: Bytestring of the PNG captcha image.
message: Optional. A message from Steam service.

Returns:
A string containing the solved captcha code.


def get_filename_by_suffixes(dir_src, suffixes):
    list_files = os.listdir(dir_src)
    re_files = list()
    if is_string(suffixes):
        suffixes = [suffixes]
    if (not isinstance(suffixes, list)):
        return None
    for (i, suf) in enumerate(suffixes):
        if ((len(suf) >= 1) and (suf[0] != '.')):
            suffixes[i] = ('.' + suf)
    for f in list_files:
        (name, ext) = os.path.splitext(f)
        if StringClass.string_in_list(ext, suffixes):
            re_files.append(f)
    return re_files

get file names with the given suffixes in the given directory

Args:
dir_src: directory path
suffixes: wanted suffixes list, the suffix in suffixes can with or without '.'

Returns:
file names with the given suffixes as list


def change_kernel(self, kernel, return_dict=True):

        if type(kernel) != Kernel:
            raise BadKernelObject("Use Kernel object")

        return self._perform_action(
            {'type': 'change_kernel', 'kernel': kernel.id},
            return_dict
        )

Change the kernel to a new one

Args:
kernel : instance of digitalocean.Kernel.Kernel

Optional Args:
return_dict (bool): Return a dict when True (default),
otherwise return an Action.

Returns dict or Action


def init_cache(self, batch_size, max_length):
    input_ids = jnp.ones((batch_size, max_length), dtype='i4')
    attention_mask = jnp.ones_like(input_ids, dtype='i4')
    position_ids = jnp.broadcast_to(jnp.arange(jnp.atleast_2d(input_ids).shape[-1]), input_ids.shape)
    init_variables = self.module.init(jax.random.PRNGKey(0), input_ids, attention_mask, position_ids, return_dict=False, init_cache=True)
    return unfreeze(init_variables['cache'])

Args:
batch_size (`int`):
batch_size used for fast auto-regressive decoding. Defines the batch size of the initialized cache.
max_length (`int`):
maximum possible length for auto-regressive decoding. Defines the sequence length of the initialized
cache.


def __init__(self, actions=None):

        super().__init__(InstructionType.OFPIT_APPLY_ACTIONS)
        self.actions = actions if actions else []

Create a InstructionApplyAction with the optional parameters below.

Args:
actions (:class:`~.actions.ListOfActions`):
Actions associated with OFPIT_APPLY_ACTIONS.


def _extract_files(archive_path):
    extract_dir = tempfile.mkdtemp()

    def _extract_files_cleanup():
        shutil.rmtree(extract_dir, ignore_errors=True)
    atexit.register(_extract_files_cleanup)
    _log('
    zip_file = zipfile.ZipFile(archive_path, mode='r')
    zip_file.extractall(extract_dir)
    zip_file.close()
    return extract_dir

Extract the contents of this .par file to disk.

This creates a temporary directory, and registers an atexit
handler to clean that directory on program exit.  Extraction and
cleanup will potentially use significant time and disk space.

Returns:
Directory where contents were extracted to.


def basis(sample_paths: types.RealTensor, time_index: types.IntTensor) -> types.RealTensor:
    sample_paths = tf.convert_to_tensor(sample_paths, name='sample_paths')
    if sample_paths.shape.rank == 3:
        sample_paths = tf.expand_dims(sample_paths, axis=0)
    shape = tf.shape(sample_paths)
    num_samples = shape[1]
    batch_size = shape[0]
    dim = sample_paths.shape[-1]
    slice_samples = tf.slice(sample_paths, [0, 0, time_index, 0], [batch_size, num_samples, 1, dim])
    samples_centered = slice_samples - tf.math.reduce_mean(slice_samples, axis=1, keepdims=True)
    grid = tf.range(degree + 1, dtype=samples_centered.dtype)
    grid = tf.meshgrid(*dim * [grid])
    grid = tf.reshape(tf.stack(grid, -1), [-1, dim])
    basis_expansion = tf.reduce_prod(samples_centered ** grid, axis=-1)
    return tf.transpose(basis_expansion, [0, 2, 1])

Computes polynomial basis expansion at the given sample points.

Args:
sample_paths: A `Tensor` of either `flaot32` or `float64` dtype and of
either shape `[num_samples, num_times, dim]` or
`[batch_size, num_samples, num_times, dim]`.
time_index: An integer scalar `Tensor` that corresponds to the time
coordinate at which the basis function is computed.

Returns:
A `Tensor`s of shape `[batch_size, (degree + 1)**dim, num_samples]`.


def __init__(self, solution_size, population_size=20):

        super(StandardOptimizer, self).__init__()

        self._solution_size = solution_size
        self._population_size = population_size

        self._hyperparameters['_population_size'] = {
            'type': 'int',
            'min': 2,
            'max': 1026
        }

Initialize general optimization attributes and bookkeeping

Args:
solution_size: The number of values in each solution.
population_size: The number of solutions in every generation.


def _EuclidianDistances(self, slist):
    e_dists2 = [transitfeed.ApproximateDistanceBetweenStops(stop, tail) for (stop, tail) in itertools.izip(slist, slist[1:])]
    return e_dists2

Calculate euclidian distances between stops.

Uses the stoplists long/lats to approximate distances
between stations and build a list with y-coordinates for the
horizontal lines in the graph.

Args:
# Class Stop is defined in transitfeed.py
stoplist: [Stop, Stop, ...]

Returns:
# One integer for each pair of stations
# indicating the approximate distance
[0,33,140, ... ,X]


def push(self, filename, data):

        self._queue.put(Chunk(filename, data))

Push a chunk of a file to the streaming endpoint.

Args:
filename: Name of file that this is a chunk of.
chunk_id: TODO: change to 'offset'
chunk: File data.


def to_timestamp(dt, timestamp):
    if dt.tzinfo:
        raise TypeError('Cannot store a timezone aware datetime. Convert to UTC and store the naive datetime.')
    timestamp.seconds = calendar.timegm(dt.timetuple())
    timestamp.nanos = (dt.microsecond * _NANOS_PER_MICRO)

Convert datetime to google.protobuf.Timestamp.

Args:
dt: a timezone naive datetime.
timestamp: a google.protobuf.Timestamp to populate.

Raises:
TypeError: if a timezone aware datetime was provided.


def AddFile(self, fd, external=True):
    files_for_write = []
    for sub_store in self.GetChildrenByPriority(allow_external=external):
        new_file = sub_store.AddFile(fd)
        if new_file:
            files_for_write.append(new_file)
    fd.Seek(0)
    while files_for_write:
        data = fd.Read(self.CHUNK_SIZE)
        if (not data):
            break
        for child in files_for_write:
            child.Write(data)
    for child in files_for_write:
        child.Close()

Create a new file in the file store.

We delegate the actual file addition to our contained
implementations. Implementations can either implement the AddFile() method,
returning a file like object which will be written on, or directly support
the AddBlobToStore() method which can copy the VFSBlobImage efficiently.

Args:
fd: An AFF4 object open for read/write.
external: If true, attempt to add files to stores defined as EXTERNAL.


def sort(self, cmp=None, key=None, reverse=False):

    def _DefaultKey(value):
        'Default key func is to create a list of all fields.'
        result = []
        for key in self.header:
            try:
                result.append(float(value[key]))
            except ValueError:
                result.append(value[key])
        return result
    key = (key or _DefaultKey)
    new_table = self._table[1:]
    if (cmp is not None):
        key = cmp_to_key(cmp)
    new_table.sort(key=key, reverse=reverse)
    self._table = [self.header]
    self._table.extend(new_table)
    for (index, row) in enumerate(self._table):
        row.row = index

Sorts rows in the texttable.

Args:
cmp: func, non default sort algorithm to use.
key: func, applied to each element before sorting.
reverse: bool, reverse order of sort.


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

        super(Certificate, self).read(istream, kmip_version=kmip_version)
        tstream = BytearrayStream(istream.read(self.length))

        self.certificate_type = CertificateType()
        self.certificate_value = CertificateValue()

        self.certificate_type.read(tstream, kmip_version=kmip_version)
        self.certificate_value.read(tstream, kmip_version=kmip_version)

        self.is_oversized(tstream)

Read the data encoding the Certificate object and decode it into its
constituent parts.

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


def _letter_map(word):
    lmap = {}
    for letter in word:
        try:
            lmap[letter] += 1
        except KeyError:
            lmap[letter] = 1
    return lmap

Creates a map of letter use in a word.

Args:
word: a string to create a letter map from

Returns:
a dictionary of {letter: integer count of letter in word}


def __init__(self, options, log):
    self.options = options
    self.log = log
    self.compile_pattern()

Initializer.  Subclass may override.

Args:
options: a dict containing the options passed to RefactoringTool
that could be used to customize the fixer through the command line.
log: a list to append warnings and other messages to.


def get_phonopy_structure(pmg_structure):

    symbols = [site.specie.symbol for site in pmg_structure]

    return PhonopyAtoms(symbols=symbols, cell=pmg_structure.lattice.matrix,
                        scaled_positions=pmg_structure.frac_coords)

Convert a pymatgen Structure object to a PhonopyAtoms object.

Args:
pmg_structure (pymatgen Structure): A Pymatgen structure object.


def retry_auth_check(exception):
    if isinstance(exception, apiclient.errors.HttpError):
        if (exception.resp.status in HTTP_AUTH_ERROR_CODES):
            _print_error('Retrying...')
            return True
    return False

Specific check for auth error codes.

Return True if we should retry.

False otherwise.
Args:
exception: An exception to test for transience.

Returns:
True if we should retry. False otherwise.


def generate(self, cache_root):
    generator_cwd = os.path.join(cache_root, 'generated', self.vlnv.sanitized_name)
    generator_input_file = os.path.join(generator_cwd, (self.name + '_input.yml'))
    logger.info(('Generating ' + str(self.vlnv)))
    if (not os.path.exists(generator_cwd)):
        os.makedirs(generator_cwd)
    with open(generator_input_file, 'w') as f:
        f.write(yaml.dump(self.generator_input))
    args = [os.path.join(os.path.abspath(self.generator.root), self.generator.command), generator_input_file]
    if self.generator.interpreter:
        args[0:0] = [self.generator.interpreter]
    Launcher(args[0], args[1:], cwd=generator_cwd).run()
    cores = []
    logger.debug(('Looking for generated cores in ' + generator_cwd))
    for (root, dirs, files) in os.walk(generator_cwd):
        for f in files:
            if f.endswith('.core'):
                try:
                    cores.append(Core(os.path.join(root, f)))
                except SyntaxError as e:
                    w = ((('Failed to parse generated core file ' + f) + ': ') + e.msg)
                    raise RuntimeError(w)
    logger.debug(('Found ' + ', '.join((str(c.name) for c in cores))))
    return cores

Run a parametrized generator

Args:
cache_root (str): The directory where to store the generated cores

Returns:
list: Cores created by the generator


def _normalize_direction(heading: int) -> int:

        while heading > 359:
            heading = int(heading - 359)
        while heading < 0:
            heading = int(heading + 359)
        return heading

Make sure that 0 < heading < 360

Args:
heading: base heading

Returns: corrected heading


def compute_actor_handle_id(actor_handle_id, num_forks):

    assert isinstance(actor_handle_id, ActorHandleID)
    handle_id_hash = hashlib.sha1()
    handle_id_hash.update(actor_handle_id.binary())
    handle_id_hash.update(str(num_forks).encode("ascii"))
    handle_id = handle_id_hash.digest()
    return ActorHandleID(handle_id)

Deterministically compute an actor handle ID.

A new actor handle ID is generated when it is forked from another actor
handle. The new handle ID is computed as hash(old_handle_id || num_forks).

Args:
actor_handle_id (common.ObjectID): The original actor handle ID.
num_forks: The number of times the original actor handle has been
forked so far.

Returns:
An ID for the new actor handle.


def wait_key(keys=None):
    if is_a_tty():
        if keys:
            if (not isinstance(keys, tuple)):
                keys = (keys,)
            while True:
                key = _getch()
                if (key in keys):
                    return key
        else:
            return _getch()

Waits for a keypress at the console and returns it.
"Where's the any key?"

Arguments:
keys - if passed, wait for this specific key, e.g. ESC.
may be a tuple.
Returns:
char or ESC - depending on key hit.
None - immediately under i/o redirection, not an interactive tty.


def _encode(self, tokens: List[str], mean: bool) -> Union[(List[np.ndarray], np.ndarray)]:
    embedded_tokens = []
    for t in tokens:
        try:
            emb = self.tok2emb[t]
        except KeyError:
            try:
                emb = self._get_word_vector(t)
            except KeyError:
                emb = np.zeros(self.dim, dtype=np.float32)
            self.tok2emb[t] = emb
        embedded_tokens.append(emb)
    if (mean is None):
        mean = self.mean
    if mean:
        filtered = [et for et in embedded_tokens if np.any(et)]
        if filtered:
            return np.mean(filtered, axis=0)
        return np.zeros(self.dim, dtype=np.float32)
    return embedded_tokens

Embed one text sample

Args:
tokens: tokenized text sample
mean: whether to return mean embedding of tokens per sample

Returns:
list of embedded tokens or array of mean values


def send_emote(self, room_id, text_content, timestamp=None):

        return self.send_message_event(
            room_id, "m.room.message",
            self.get_emote_body(text_content),
            timestamp=timestamp
        )

Perform PUT /rooms/$room_id/send/m.room.message with m.emote msgtype

Args:
room_id (str): The room ID to send the event in.
text_content (str): The m.emote body to send.
timestamp (int): Set origin_server_ts (For application services only)


def _EncodeString(self, string):
    try:
        encoded_string = string.encode(self._encoding, errors=self._errors)
    except UnicodeEncodeError:
        if (self._errors == 'strict'):
            logging.error('Unable to properly write output due to encoding error. Switching to error tolerant encoding which can result in non Basic Latin (C0) characters to be replaced with "?" or "\\ufffd".')
            self._errors = 'replace'
        encoded_string = string.encode(self._encoding, errors=self._errors)
    return encoded_string

Encodes the string.

Args:
string (str): string to encode.

Returns:
bytes: encoded string.


def representative_batch_size(self):
    return self.batch_size()

Return a representative size for batches in the dataset.

This is not guaranteed to be the batch size for all batches in the
dataset. It just needs to be a rough approximation for batch sizes in
the dataset.

Returns:
int, a representative size for batches found in the dataset,
or None if it is unknown.


def get_model_objects(model, wfi_role=None, **kwargs):
    query_dict = {}
    for (k, v) in kwargs.items():
        if isinstance(v, list):
            query_dict[k] = [str(x) for x in v]
        else:
            parse = str(v).split('.')
            if ((parse[0] == 'role') and wfi_role):
                query_dict[k] = wfi_role
                for i in range(1, len(parse)):
                    query_dict[k] = query_dict[k].__getattribute__(parse[i])
            else:
                query_dict[k] = parse[0]
    return model.objects.all(**query_dict)

Fetches model objects by filtering with kwargs

If wfi_role is specified, then we expect kwargs contains a
filter value starting with role,

e.g. {'user': 'role.program.user'}

We replace this `role` key with role instance parameter `wfi_role` and try to get
object that filter value 'role.program.user' points by iterating `getattribute`. At
the end filter argument becomes {'user': user}.

Args:
model (Model): Model class
wfi_role (Role): role instance of wf instance
**kwargs: filter arguments

Returns:
(list): list of model object instances


def _validate_number_sequence(self, seq, n):
    if (seq is None):
        return np.zeros(n)
    if (len(seq) is n):
        try:
            l = [float(e) for e in seq]
        except ValueError:
            raise ValueError((('One or more elements in sequence <' + repr(seq)) + '> cannot be interpreted as a real number'))
        else:
            return np.asarray(l)
    elif (len(seq) is 0):
        return np.zeros(n)
    else:
        raise ValueError((((('Unexpected number of elements in sequence. Got: ' + str(len(seq))) + ', Expected: ') + str(n)) + '.'))

Validate a sequence to be of a certain length and ensure it's a numpy array of floats.

Raises:
ValueError: Invalid length or non-numeric value


def write_json(self, path, contents, message):
    log.debug(message.format(path=path))
    makedirs(os.path.dirname(path))
    with open(path, 'w') as fh:
        json.dump(contents, fh, indent=2, sort_keys=True)

Write json to disk.

Args:
path (str): the path to write to
contents (dict): the contents of the json blob
message (str): the message to log


def _GetLoadConfigTimestamp(self, pefile_object):

    if not hasattr(pefile_object, 'DIRECTORY_ENTRY_LOAD_CONFIG'):
      return None
    timestamp = getattr(
        pefile_object.DIRECTORY_ENTRY_LOAD_CONFIG.struct, 'TimeDateStamp', 0)
    return timestamp

Retrieves the timestamp from the Load Configuration directory.

Args:
pefile_object (pefile.PE): pefile object.

Returns:
int: load configuration timestamps or None if there are none present.


def diet_adam_optimizer_params():
    return hparam.HParams(quantize=True, quantization_scale=(10.0 / tf.int16.max), optimizer='DietAdam', learning_rate=1.0, learning_rate_warmup_steps=2000, learning_rate_decay_scheme='noam', epsilon=1e-10, beta1=0.0, beta2=0.98, factored_second_moment_accumulator=True)

Default hyperparameters for a DietAdamOptimizer.

Returns:
a hyperparameters object.


def use_gradient(grad_f):

  grad_f_name = register_to_random_name(grad_f)

  def function_wrapper(f):
    def inner(*inputs):

      state = {"out_value": None}

      out = f(*inputs)

      def store_out(out_value):

        state["out_value"] = out_value

      store_name = "store_" + f.__name__
      store = tf.py_func(store_out, [out], (), stateful=True, name=store_name)

      def mock_f(*inputs):

        return state["out_value"]

      with tf.control_dependencies([store]):
        with gradient_override_map({"PyFunc": grad_f_name}):
          mock_name = "mock_" + f.__name__
          mock_out = tf.py_func(mock_f, inputs, out.dtype, stateful=True,
                                name=mock_name)
          mock_out.set_shape(out.get_shape())

      return mock_out
    return inner
  return function_wrapper

Decorator for easily setting custom gradients for TensorFlow functions.

* DO NOT use this function if you need to serialize your graph.
* This function will cause the decorated function to run slower.

Example:

def _foo_grad(op, grad): ...

@use_gradient(_foo_grad)
def foo(x1, x2, x3): ...

Args:
grad_f: function to use as gradient.

Returns:
A decorator to apply to the function you wish to override the gradient of.


def write_input(self, output_dir='.', make_dir_if_not_present=True):
    if (make_dir_if_not_present and (not os.path.exists(output_dir))):
        os.makedirs(output_dir)
    for (k, v) in self.items():
        with zopen(os.path.join(output_dir, k), 'wt') as f:
            f.write(v.__str__())

Write VASP input to a directory.

Args:
output_dir (str): Directory to write to. Defaults to current
directory (".").
make_dir_if_not_present (bool): Create the directory if not
present. Defaults to True.


def get_patient_reference_element_paths(structdef: StructureDefinition) -> List[str]:
    results = []
    struct_id = cast(Any, structdef).id.value
    for elem in cast(Any, structdef).snapshot.element:
        for t in elem.type:
            for tp in t.target_profile:
                if tp.value.endswith('Patient'):
                    results.append(elem.id.value[len(struct_id) + 1:])
    return results

Returns all the top level patient elements for a given Reference.

Args:
structdef: a FHIR StructureDefinition proto.

Returns:
A list of patients.


def minhash(self, v):
    if (not isinstance(v, collections.Iterable)):
        raise TypeError('Input vector must be an iterable')
    if (not (len(v) == self.dim)):
        raise ValueError(('Input dimension mismatch, expecting %d' % self.dim))
    if (not isinstance(v, np.ndarray)):
        v = np.array(v, dtype=np.float32)
    elif (v.dtype != np.float32):
        v = v.astype(np.float32)
    hashvalues = np.zeros((self.sample_size, 2), dtype=np.int)
    vzeros = (v == 0)
    if vzeros.all():
        raise ValueError('Input is all zeros')
    v[vzeros] = np.nan
    vlog = np.log(v)
    for i in range(self.sample_size):
        t = np.floor(((vlog / self.rs[i]) + self.betas[i]))
        ln_y = ((t - self.betas[i]) * self.rs[i])
        ln_a = ((self.ln_cs[i] - ln_y) - self.rs[i])
        k = np.nanargmin(ln_a)
        (hashvalues[i][0], hashvalues[i][1]) = (k, int(t[k]))
    return WeightedMinHash(self.seed, hashvalues)

Create a new weighted MinHash given a weighted Jaccard vector.
Each dimension is an integer
frequency of the corresponding element in the multi-set represented
by the vector.

Args:
v (numpy.array): The Jaccard vector.


def __eval_validator_check(self, check_item, resp_obj):
    if (isinstance(check_item, (dict, list)) or isinstance(check_item, parser.LazyString)):
        check_value = self.eval_content(check_item)
    else:
        check_value = resp_obj.extract_field(check_item)
    return check_value

evaluate check item in validator.

Args:
check_item: check_item should only be the following 5 formats:
1, variable reference, e.g. $token
2, function reference, e.g. ${is_status_code_200($status_code)}
3, dict or list, maybe containing variable/function reference, e.g. {"var": "$abc"}
4, string joined by delimiter. e.g. "status_code", "headers.content-type"
5, regex string, e.g. "LB[\d]*(.*)RB[\d]*"

resp_obj: response object


def __init__(self, sample_rate: int, delay_other_parts: bool=True):
    self._sample_rate = sample_rate
    self._delay_other_parts = delay_other_parts

Initializes the rate limiter.

Args:
sample_rate: The sample rate of the audio. A typical value is 24000
(24KHz)
delay_other_parts: If true, other parts will be delayed until the audio is
played out. If false, other parts will be passed through as soon as
possible, overtaking audio if needed.


def SendSerializedMessage(self, message):

        try:
            ba = Helper.ToArray(message)
            ba2 = binascii.unhexlify(ba)
            self.bytes_out += len(ba2)
            self.transport.write(ba2)
        except Exception as e:
            logger.debug(f"Could not send serialized message {e}")

Send the `message` to the remote client.

Args:
message (neo.Network.Message):


def _create_table_and_update_context(node, context):
    schema_type_name = sql_context_helpers.get_schema_type_name(node, context)
    table = context.compiler_metadata.get_table(schema_type_name).alias()
    context.query_path_to_selectable[node.query_path] = table
    return table

Create an aliased table for a SqlNode.

Updates the relevant Selectable global context.

Args:
node: SqlNode, the current node.
context: CompilationContext, global compilation state and metadata.

Returns:
Table, the newly aliased SQLAlchemy table.


def get_alarms(zone=None):
    if (zone is None):
        zone = discovery.any_soco()
    response = zone.alarmClock.ListAlarms()
    alarm_list = response['CurrentAlarmList']
    tree = XML.fromstring(alarm_list.encode('utf-8'))
    alarms = tree.findall('Alarm')
    result = set()
    for alarm in alarms:
        values = alarm.attrib
        alarm_id = values['ID']
        if Alarm._all_alarms.get(alarm_id):
            instance = Alarm._all_alarms.get(alarm_id)
        else:
            instance = Alarm(None)
            instance._alarm_id = alarm_id
            Alarm._all_alarms[instance._alarm_id] = instance
        instance.start_time = datetime.strptime(values['StartTime'], '%H:%M:%S').time()
        instance.duration = (None if (values['Duration'] == '') else datetime.strptime(values['Duration'], '%H:%M:%S').time())
        instance.recurrence = values['Recurrence']
        instance.enabled = (values['Enabled'] == '1')
        instance.zone = next((z for z in zone.all_zones if (z.uid == values['RoomUUID'])), None)
        if (instance.zone is None):
            continue
        instance.program_uri = (None if (values['ProgramURI'] == 'x-rincon-buzzer:0') else values['ProgramURI'])
        instance.program_metadata = values['ProgramMetaData']
        instance.play_mode = values['PlayMode']
        instance.volume = values['Volume']
        instance.include_linked_zones = (values['IncludeLinkedZones'] == '1')
        result.add(instance)
    return result

Get a set of all alarms known to the Sonos system.

Args:
zone (`SoCo`, optional): a SoCo instance to query. If None, a random
instance is used. Defaults to `None`.

Returns:
set: A set of `Alarm` instances

Note:
Any existing `Alarm` instance will have its attributes updated to those
currently stored on the Sonos system.


def pascalcase(text, acronyms=None):

    words, _case, _sep = case_parse.parse_case(text, acronyms)
    return ''.join(words)

Return text in PascalCase style (aka MixedCase).

Args:
text: input string to convert case
detect_acronyms: should attempt to detect acronyms
acronyms: a list of acronyms to detect

>>> pascalcase("hello world")
'HelloWorld'
>>> pascalcase("HELLO_HTML_WORLD", True, ["HTML"])
'HelloHTMLWorld'


def ensuredir(path_, verbose=None, info=False, mode=0o1777):
    r
    if verbose is None:
        verbose = VERYVERBOSE
    if isinstance(path_, (list, tuple)):
        path_ = join(*path_)
    if HAVE_PATHLIB and isinstance(path_, pathlib.Path):
        path_ = str(path_)
    if not checkpath(path_, verbose=verbose, info=info):
        if verbose:
            print('[util_path] mkdir(%r)' % path_)
        try:
            os.makedirs(normpath(path_), mode=mode)
        except OSError as ex:
            util_dbg.printex(
                ex,
                'check that the longest existing path '
                'is not a bad windows symlink.', keys=['path_'])
            raise
    return path_

r"""
Ensures that directory will exist. creates new dir with sticky bits by
default

Args:
path (str): dpath to ensure. Can also be a tuple to send to join
info (bool): if True prints extra information
mode (int): octal mode of directory (default 0o1777)

Returns:
str: path - the ensured directory


def _update_watermarks_for_side_input_and_unblock_tasks(self, side_input, watermark) -> List[Tuple[TransformExecutor, Timestamp]]:
    with self._lock:
        view = self._views[side_input]
        view.watermark = watermark
        unblocked_tasks = []
        tasks_just_unblocked = []
        for task, block_until in view.blocked_tasks:
            if watermark.output_watermark >= block_until:
                view.value = self._pvalue_to_value(side_input, view.elements)
                unblocked_tasks.append(task)
                tasks_just_unblocked.append((task, block_until))
                task.blocked = False
        for task in tasks_just_unblocked:
            view.blocked_tasks.remove(task)
        return unblocked_tasks

Helps update _SideInputsContainer after a watermark update.

For each view of the side input, it updates the value of the watermark
recorded when the watermark moved and unblocks tasks accordingly.

Args:
side_input: ``_UnpickledSideInput`` value.
watermark: Value of the watermark after an update for a PTransform.

Returns:
Tasks that get unblocked as a result of the watermark advancing.