def extract_imports(module_fname: str, cache: Optional[Dict[str, List[str]]]=None) -> List[str]:
    if cache is not None and module_fname in cache:
        return cache[module_fname]
    with open(PATH_TO_REPO / module_fname, 'r', encoding='utf-8') as f:
        content = f.read()
    splits = content.split('"""')
    content = ''.join(splits[::2])
    module_parts = str(module_fname).split(os.path.sep)
    imported_modules = []
    relative_imports = _re_single_line_relative_imports.findall(content)
    relative_imports = [(mod, imp) for mod, imp in relative_imports if '
    multiline_relative_imports = _re_multi_line_relative_imports.findall(content)
    relative_imports += [(mod, imp) for mod, imp in multiline_relative_imports if '
    for module, imports in relative_imports:
        level = 0
        while module.startswith('.'):
            module = module[1:]
            level += 1
        if len(module) > 0:
            dep_parts = module_parts[:len(module_parts) - level] + module.split('.')
        else:
            dep_parts = module_parts[:len(module_parts) - level]
        imported_module = os.path.sep.join(dep_parts)
        imported_modules.append((imported_module, [imp.strip() for imp in imports.split(',')]))
    direct_imports = _re_single_line_direct_imports.findall(content)
    direct_imports = [(mod, imp) for mod, imp in direct_imports if '
    multiline_direct_imports = _re_multi_line_direct_imports.findall(content)
    direct_imports += [(mod, imp) for mod, imp in multiline_direct_imports if '
    for module, imports in direct_imports:
        import_parts = module.split('.')[1:]
        dep_parts = ['src', 'transformers'] + import_parts
        imported_module = os.path.sep.join(dep_parts)
        imported_modules.append((imported_module, [imp.strip() for imp in imports.split(',')]))
    result = []
    for module_file, imports in imported_modules:
        if (PATH_TO_REPO / f'{module_file}.py').is_file():
            module_file = f'{module_file}.py'
        elif (PATH_TO_REPO / module_file).is_dir() and (PATH_TO_REPO / module_file / '__init__.py').is_file():
            module_file = os.path.sep.join([module_file, '__init__.py'])
        imports = [imp for imp in imports if len(imp) > 0 and re.match('^[A-Za-z0-9_]*$', imp)]
        if len(imports) > 0:
            result.append((module_file, imports))
    if cache is not None:
        cache[module_fname] = result
    return result

Get the imports a given module makes.

Args:
module_fname (`str`):
The name of the file of the module where we want to look at the imports (given relative to the root of
the repo).
cache (Dictionary `str` to `List[str]`, *optional*):
To speed up this function if it was previously called on `module_fname`, the cache of all previously
computed results.

Returns:
`List[str]`: The list of module filenames imported in the input `module_fname` (a submodule we import from that
is a subfolder will give its init file).


def _module_info_from_proto_safe(module_info_def, import_scope=None):
    try:
        return _module_info_from_proto(module_info_def, import_scope)
    except Exception as e:
        logging.warning('Error encountered when deserializing sonnet ModuleInfo:\n%s', str(e))
        return None

Deserializes the `module_info_def` proto without raising exceptions.

Args:
module_info_def: An instance of `module_pb2.SonnetModule`.
import_scope: Optional `string`. Name scope to use.

Returns:
An instance of `ModuleInfo`.


def get_size(self):
    raise NotImplementedError

Return the size (number of batches) for the dataset created.

For certain type of the data input, the number of batches is known, eg for
Numpy data, the size is same as (number_of_element / batch_size). Whereas
for dataset or python generator, the size is unknown since it may or may not
have a end state.

Returns:
int, the number of batches for the dataset, or None if it is unknown. The
caller could use this to control the loop of training, show progress bar,
or handle unexpected StopIteration error.


def set_lock_config(self, device_label, volume=None, voice_level=None, auto_lock_enabled=None):
    response = None
    data = {}
    if volume:
        data['volume'] = volume
    if voice_level:
        data['voiceLevel'] = voice_level
    if (auto_lock_enabled is not None):
        data['autoLockEnabled'] = auto_lock_enabled
    try:
        response = requests.put(urls.lockconfig(self._giid, device_label), headers={'Content-Type': 'application/json', 'Cookie': 'vid={}'.format(self._vid)}, data=json.dumps(data))
    except requests.exceptions.RequestException as ex:
        raise RequestError(ex)
    _validate_response(response)

Set lock configuration

Args:
device_label (str): device label of lock
volume (str): 'SILENCE', 'LOW' or 'HIGH'
voice_level (str): 'ESSENTIAL' or 'NORMAL'
auto_lock_enabled (boolean): auto lock enabled


def run_display_profile(self, program_main):

        install_json = self.profile.get('install_json')

        output = 'Profile: '
        output += '{}{}{}{} '.format(
            c.Style.BRIGHT, c.Fore.CYAN, self.profile.get('profile_name'), c.Style.RESET_ALL
        )
        output += '[{}{}{}{}'.format(
            c.Style.BRIGHT, c.Fore.MAGENTA, program_main, c.Style.RESET_ALL
        )
        if install_json.get('programVersion') is not None:
            output += '{}:{}'.format(c.Style.BRIGHT, c.Style.RESET_ALL)
            output += '{}{}{}{}'.format(
                c.Style.BRIGHT,
                c.Fore.MAGENTA,
                install_json.get('programVersion'),
                c.Style.RESET_ALL,
            )
        output += ']'
        print(output)

Print profile name with programMain.

Args:
program_main (str): The executable name.


def trace(self, name='trace'):
    with self._name_scope(name):
        return self._trace()

Trace of the linear operator, equal to sum of `self.diag_part()`.

If the operator is square, this is also the sum of the eigenvalues.

Args:
name:  A name for this `Op`.

Returns:
Shape `[B1,...,Bb]` `Tensor` of same `dtype` as `self`.


def assert_listed_tensors(tst, out, expected_tensor_names, expected_op_types, node_name_regex=None, op_type_regex=None, tensor_filter_name=None, sort_by='timestamp', reverse=False):
    line_iter = iter(out.lines)
    attr_segs = out.font_attr_segs
    line_counter = 0
    num_dumped_tensors = int(next(line_iter).split(' ')[0])
    line_counter += 1
    tst.assertGreaterEqual(num_dumped_tensors, len(expected_tensor_names))
    if op_type_regex is not None:
        tst.assertEqual('Op type regex filter: "%s"' % op_type_regex, next(line_iter))
        line_counter += 1
    if node_name_regex is not None:
        tst.assertEqual('Node name regex filter: "%s"' % node_name_regex, next(line_iter))
        line_counter += 1
    tst.assertEqual('', next(line_iter))
    line_counter += 1
    line = next(line_iter)
    tst.assertIn('t (ms)', line)
    tst.assertIn('Op type', line)
    tst.assertIn('Tensor name', line)
    attr_segs = out.font_attr_segs[line_counter]
    attr_seg = attr_segs[0]
    tst.assertEqual(0, attr_seg[0])
    tst.assertEqual(len('t (ms)'), attr_seg[1])
    command = attr_seg[2][0].content
    tst.assertIn('-s timestamp', command)
    assert_column_header_command_shortcut(tst, command, reverse, node_name_regex, op_type_regex, tensor_filter_name)
    tst.assertEqual('bold', attr_seg[2][1])
    idx0 = line.index('Size')
    attr_seg = attr_segs[1]
    tst.assertEqual(idx0, attr_seg[0])
    tst.assertEqual(idx0 + len('Size (B)'), attr_seg[1])
    command = attr_seg[2][0].content
    tst.assertIn('-s dump_size', command)
    assert_column_header_command_shortcut(tst, command, reverse, node_name_regex, op_type_regex, tensor_filter_name)
    tst.assertEqual('bold', attr_seg[2][1])
    idx0 = line.index('Op type')
    attr_seg = attr_segs[2]
    tst.assertEqual(idx0, attr_seg[0])
    tst.assertEqual(idx0 + len('Op type'), attr_seg[1])
    command = attr_seg[2][0].content
    tst.assertIn('-s op_type', command)
    assert_column_header_command_shortcut(tst, command, reverse, node_name_regex, op_type_regex, tensor_filter_name)
    tst.assertEqual('bold', attr_seg[2][1])
    idx0 = line.index('Tensor name')
    attr_seg = attr_segs[3]
    tst.assertEqual(idx0, attr_seg[0])
    tst.assertEqual(idx0 + len('Tensor name'), attr_seg[1])
    command = attr_seg[2][0].content
    tst.assertIn('-s tensor_name', command)
    assert_column_header_command_shortcut(tst, command, reverse, node_name_regex, op_type_regex, tensor_filter_name)
    tst.assertEqual('bold', attr_seg[2][1])
    tensor_timestamps = []
    dump_sizes_bytes = []
    op_types = []
    tensor_names = []
    for line in line_iter:
        items = line.split(' ')
        items = [item for item in items if item]
        rel_time = float(items[0][1:-1])
        tst.assertGreaterEqual(rel_time, 0.0)
        tensor_timestamps.append(rel_time)
        dump_sizes_bytes.append(command_parser.parse_readable_size_str(items[1]))
        op_types.append(items[2])
        tensor_names.append(items[3])
    if sort_by == 'timestamp':
        sorted_timestamps = sorted(tensor_timestamps)
        if reverse:
            sorted_timestamps.reverse()
        tst.assertEqual(sorted_timestamps, tensor_timestamps)
    elif sort_by == 'dump_size':
        sorted_dump_sizes_bytes = sorted(dump_sizes_bytes)
        if reverse:
            sorted_dump_sizes_bytes.reverse()
        tst.assertEqual(sorted_dump_sizes_bytes, dump_sizes_bytes)
    elif sort_by == 'op_type':
        sorted_op_types = sorted(op_types)
        if reverse:
            sorted_op_types.reverse()
        tst.assertEqual(sorted_op_types, op_types)
    elif sort_by == 'tensor_name':
        sorted_tensor_names = sorted(tensor_names)
        if reverse:
            sorted_tensor_names.reverse()
        tst.assertEqual(sorted_tensor_names, tensor_names)
    else:
        tst.fail('Invalid value in sort_by: %s' % sort_by)
    for tensor_name, op_type in zip(expected_tensor_names, expected_op_types):
        tst.assertIn(tensor_name, tensor_names)
        index = tensor_names.index(tensor_name)
        tst.assertEqual(op_type, op_types[index])

Check RichTextLines output for list_tensors commands.

Args:
tst: A test_util.TensorFlowTestCase instance.
out: The RichTextLines object to be checked.
expected_tensor_names: (list of str) Expected tensor names in the list.
expected_op_types: (list of str) Expected op types of the tensors, in the
same order as the expected_tensor_names.
node_name_regex: Optional: node name regex filter.
op_type_regex: Optional: op type regex filter.
tensor_filter_name: Optional: name of the tensor filter.
sort_by: (str) (timestamp | op_type | tensor_name) the field by which the
tensors in the list are sorted.
reverse: (bool) whether the sorting is in reverse (i.e., descending) order.


def write_json(data, path, file_name):
    if (os.path.exists(path) and (not os.path.isdir(path))):
        return
    elif (not os.path.exists(path)):
        mkdir_p(path)
    with open(os.path.join(path, file_name), 'w') as f:
        json_tricks.dump(data, f, indent=4, primitives=True, allow_nan=True)

Write out data to a json file.

Args:
data: A dictionary representation of the data to write out
path: The directory to output the file in
file_name: The name of the file to write out


def set(self, key, value):

        match = self._get_match(key=key)

        if not match:
            self._log.info('"%s" does not exist, so it will be added.', key)

            if isinstance(value, str):
                self._log.info('"%s" will be added as a PHP string value.',
                               key)
                value_str = '\'{}\''.format(value)
            else:
                self._log.info('"%s" will be added as a PHP object value.',
                               key)
                value_str = str(value).lower()

            new = 'define(\'{key}\', {value});'.format(
                key=key,
                value=value_str)

            self._log.info('"%s" will be added as: %s', key, new)

            replace_this = '<?php\n'
            replace_with = '<?php\n' + new + '\n'
            self._content = self._content.replace(replace_this, replace_with)
            self._log.info('Content string has been updated.')
            return True

        if self._get_value_from_match(key=key, match=match) == value:
            self._log.info('"%s" is already up-to-date.', key)
            return False

        self._log.info('"%s" exists and will be updated.', key)

        start_index = match.start(1)
        end_index = match.end(1)

        if isinstance(value, bool):
            value = str(value).lower()
            self._log.info('"%s" will be updated with boolean value: %s',
                           key,
                           value)
        else:
            self._log.info('"%s" will be updated with string value: %s',
                           key,
                           value)

        start = self._content[:start_index]
        end = self._content[end_index:]

        self._content = start + value + end
        return True

Updates the value of the given key in the loaded content.

Args:
key (str): Key of the property to update.
value (str): New value of the property.

Return:
bool: Indicates whether or not a change was made.


def make_dataset_iterator(self, dataset):
    return self._extended._make_dataset_iterator(dataset)

Makes an iterator for input provided via `dataset`.

DEPRECATED: This method is not available in TF 2.x.

Data from the given dataset will be distributed evenly across all the
compute replicas. We will assume that the input dataset is batched by the
global batch size. With this assumption, we will make a best effort to
divide each batch across all the replicas (one or more workers).
If this effort fails, an error will be thrown, and the user should instead
use `make_input_fn_iterator` which provides more control to the user, and
does not try to divide a batch across replicas.

The user could also use `make_input_fn_iterator` if they want to
customize which input is fed to which replica/worker etc.

Args:
dataset: `tf.data.Dataset` that will be distributed evenly across all
replicas.

Returns:
An `tf.distribute.InputIterator` which returns inputs for each step of the
computation.  User should call `initialize` on the returned iterator.


def read(self, size=None):

        if size is None:
            size = self.__size
        ret_list = []
        while size > 0 and self.__buf:
            data = self.__buf.popleft()
            size -= len(data)
            ret_list.append(data)
        if size < 0:
            ret_list[-1], remainder = ret_list[-1][:size], ret_list[-1][size:]
            self.__buf.appendleft(remainder)
        ret = b''.join(ret_list)
        self.__size -= len(ret)
        return ret

Read at most size bytes from this buffer.

Bytes read from this buffer are consumed and are permanently removed.

Args:
size: If provided, read no more than size bytes from the buffer.
Otherwise, this reads the entire buffer.

Returns:
The bytes read from this buffer.


def aggregate(metric_values: list[float], metric_name: str):
    if str.endswith(metric_name, '.max'):
        return max(metric_values)
    if str.endswith(metric_name, '.min'):
        return min(metric_values)
    if str.endswith(metric_name, '.sum'):
        return sum(metric_values)
    return metric_values[0]

Aggregates metric values using a function based on the metric name.

If metric name does not match any of the known patterns, the first value from
the input list is returned.

Args:
metric_values: list of metric values, floats
metric_name: metric name

Returns:
aggregated metric value


def is_locator(self, path, relative=False):

        if not relative:
            path = self.relpath(path)

        return path and '/' not in path.rstrip('/')

Returns True if path refer to a locator.

Depending the storage, locator may be a bucket or container name,
a hostname, ...

args:
path (str): path or URL.
relative (bool): Path is relative to current root.

Returns:
bool: True if locator.


def hash_stream(fileobj, hasher=None, blocksize=65536):

    hasher = hasher or hashlib.sha1()
    buf = fileobj.read(blocksize)
    while buf:
        hasher.update(buf)
        buf = fileobj.read(blocksize)
    return hasher

Read from fileobj stream, return hash of its contents.

Args:
fileobj: File-like object with read()
hasher: Hash object such as hashlib.sha1(). Defaults to sha1.
blocksize: Read from fileobj this many bytes at a time.


def as_future(self, object_id, check_ready=True):
    if (not isinstance(object_id, ray.ObjectID)):
        raise TypeError('Input should be an ObjectID.')
    plain_object_id = plasma.ObjectID(object_id.binary())
    fut = PlasmaObjectFuture(loop=self._loop, object_id=plain_object_id)
    if check_ready:
        (ready, _) = ray.wait([object_id], timeout=0)
        if ready:
            if self._loop.get_debug():
                logger.debug('%s has been ready.', plain_object_id)
            self._complete_future(fut)
            return fut
    if (plain_object_id not in self._waiting_dict):
        linked_list = PlasmaObjectLinkedList(self._loop, plain_object_id)
        linked_list.add_done_callback(self._unregister_callback)
        self._waiting_dict[plain_object_id] = linked_list
    self._waiting_dict[plain_object_id].append(fut)
    if self._loop.get_debug():
        logger.debug('%s added to the waiting list.', fut)
    return fut

Turn an object_id into a Future object.

Args:
object_id: A Ray's object_id.
check_ready (bool): If true, check if the object_id is ready.

Returns:
PlasmaObjectFuture: A future object that waits the object_id.


def get_size(self, value=None):
    if (value is None):
        return sum((cls_val.get_size(obj_val) for (obj_val, cls_val) in self._get_attributes()))
    elif isinstance(value, type(self)):
        return value.get_size()
    else:
        msg = '{} is not an instance of {}'.format(value, type(self).__name__)
        raise PackException(msg)

Calculate the total struct size in bytes.

For each struct attribute, sum the result of each one's ``get_size()``
method.

Args:
value: In structs, the user can assign other value instead of a
class' instance.

Returns:
int: Total number of bytes used by the struct.

Raises:
Exception: If the struct is not valid.


def call_fn(fn, args):
    if expand_as_args(args):
        return fn(*args)
    elif _expand_as_kwargs(args):
        return fn(**args)
    else:
        return fn(args)

Calls `fn` with `args`, possibly expanding `args`.

Use this function when calling a user-provided callable using user-provided
arguments.

The expansion rules are as follows:

`fn(*args)` if `args` is a `list` or a `tuple`, but not a `namedtuple`.
`fn(**args)` if `args` is a `dict`.
`fn(args)` otherwise.

Args:
fn: A callable that takes either `args` as an argument(s).
args: Arguments to `fn`.

Returns:
result: Return value of `fn`.


def ParseRecord(self, parser_mediator, key, structure):

    if key != 'log_entry':
      raise errors.ParseError(
          'Unable to parse record, unknown structure: {0:s}'.format(key))

    event_data = BashHistoryEventData()
    event_data.command = structure.command

    date_time = dfdatetime_posix_time.PosixTime(timestamp=structure.timestamp)
    event = time_events.DateTimeValuesEvent(
        date_time, definitions.TIME_DESCRIPTION_MODIFICATION)
    parser_mediator.ProduceEventWithEventData(event, event_data)

Parses a record and produces a Bash history event.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
key (str): name of the parsed structure.
structure (pyparsing.ParseResults): elements parsed from the file.

Raises:
ParseError: when the structure type is unknown.


def delete_metadata(self, entity_type, entity_id, metadata_keys):
    if (not is_valid_uuid(entity_id)):
        raise StorageArgumentException('Invalid UUID for entity_id: {0}'.format(entity_id))
    if (not isinstance(metadata_keys, list)):
        raise StorageArgumentException('The metadata was not provided as a dictionary')
    return self._authenticated_request.to_endpoint('{}/{}/metadata/'.format(entity_type, entity_id)).with_json_body({'keys': metadata_keys}).return_body().delete()

Delete the selected metadata entries of an entity.

Only deletes selected metadata keys, for a complete wipe, use set_metadata.

Args:
entity_type (str): Type of the entity. Admitted values: ['project',
'folder', 'file'].
entity_id (srt): The UUID of the entity to be modified.
metadata_keys (lst): A list of metada keys to be deleted.

Returns:
A dictionary of the updated object metadata::

{
u'bar': u'200',
u'foo': u'100'
}

Raises:
StorageArgumentException: Invalid arguments
StorageForbiddenException: Server response code 403
StorageNotFoundException: Server response code 404
StorageException: other 400-600 error codes


def get(self, name):
    config = self.get_block(('interface %s' % name))
    if ('no switchport\n' in config):
        return
    resource = dict(name=name)
    resource.update(self._parse_mode(config))
    resource.update(self._parse_access_vlan(config))
    resource.update(self._parse_trunk_native_vlan(config))
    resource.update(self._parse_trunk_allowed_vlans(config))
    resource.update(self._parse_trunk_groups(config))
    return resource

Returns a dictionary object that represents a switchport

The Switchport resource returns the following:

* name (str): The name of the interface
* mode (str): The switchport mode value
* access_vlan (str): The switchport access vlan value
* trunk_native_vlan (str): The switchport trunk native vlan vlaue
* trunk_allowed_vlans (str): The trunk allowed vlans value
* trunk_groups (list): The list of trunk groups configured

Args:
name (string): The interface identifier to get.  Note: Switchports
are only supported on Ethernet and Port-Channel interfaces

Returns:
dict: A Python dictionary object of key/value pairs that represent
the switchport configuration for the interface specified  If
the specified argument is not a switchport then None
is returned


def __random_density_bures(N, rank=None, seed=None):

    P = np.eye(N) + random_unitary(N).data
    G = P.dot(__ginibre_matrix(N, rank, seed))
    G = G.dot(G.conj().T)
    return G / np.trace(G)

Generate a random density matrix from the Bures metric.

Args:
N (int): the length of the density matrix.
rank (int or None): the rank of the density matrix. The default
value is full-rank.
seed (int): Optional. To set a random seed.
Returns:
ndarray: rho (N,N) a density matrix.


def _dataset_merge_filestore_resource(self, resource, updated_resource, filestore_resources, ignore_fields):
    if updated_resource.get_file_to_upload():
        resource.set_file_to_upload(updated_resource.get_file_to_upload())
        filestore_resources.append(resource)
    merge_two_dictionaries(resource, updated_resource)
    resource.check_required_fields(ignore_fields=ignore_fields)
    if resource.get_file_to_upload():
        resource['url'] = Dataset.temporary_url

Helper method to merge updated resource from dataset into HDX resource read from HDX including filestore.

Args:
resource (hdx.data.Resource): Resource read from HDX
updated_resource (hdx.data.Resource): Updated resource from dataset
filestore_resources (List[hdx.data.Resource]): List of resources that use filestore (to be appended to)
ignore_fields (List[str]): List of fields to ignore when checking resource

Returns:
None


def _get_ops_details(self):
    return [self._get_op_details(idx) for idx in range(self._interpreter.NumNodes())]

Gets op details for every node.

Returns:
A list of dictionaries containing arrays with lists of tensor ids for
tensors involved in the op.


def reset(self, name):

        message_type = type(self)
        try:
            field = message_type.field_by_name(name)
        except KeyError:
            if name not in message_type.__by_name:
                raise AttributeError('Message %s has no field %s' % (
                    message_type.__name__, name))
        if field.repeated:
            self.__tags[field.number] = FieldList(field, [])
        else:
            self.__tags.pop(field.number, None)

Reset assigned value for field.

Resetting a field will return it to its default value or None.

Args:
name: Name of field to reset.


def listEverything(matching=False):
    pages = pageNames()
    if matching:
        pages = [x for x in pages if (matching in x)]
    for (i, page) in enumerate(pages):
        pages[i] = ('%s%s (%s)' % (pageFolder(page), page, getPageType(page)))
    print('\n'.join(sorted(pages)))

Prints every page in the project to the console.

Args:
matching (str, optional): if given, only return names with this string in it


def clone_with_copy(src_path, dest_path):

    log.info('Cloning directory tree %s to %s', src_path, dest_path)
    shutil.copytree(src_path, dest_path)

Clone a directory try by copying it.

Args:
src_path: The directory to be copied.
dest_path: The location to copy the directory to.


def patch_splitMax(self, patches):

    patch_size = self.Match_MaxBits
    if patch_size == 0:

      return
    for x in range(len(patches)):
      if patches[x].length1 <= patch_size:
        continue
      bigpatch = patches[x]

      del patches[x]
      x -= 1
      start1 = bigpatch.start1
      start2 = bigpatch.start2
      precontext = ''
      while len(bigpatch.diffs) != 0:

        patch = patch_obj()
        empty = True
        patch.start1 = start1 - len(precontext)
        patch.start2 = start2 - len(precontext)
        if precontext:
          patch.length1 = patch.length2 = len(precontext)
          patch.diffs.append((self.DIFF_EQUAL, precontext))

        while (len(bigpatch.diffs) != 0 and
               patch.length1 < patch_size - self.Patch_Margin):
          (diff_type, diff_text) = bigpatch.diffs[0]
          if diff_type == self.DIFF_INSERT:

            patch.length2 += len(diff_text)
            start2 += len(diff_text)
            patch.diffs.append(bigpatch.diffs.pop(0))
            empty = False
          elif (diff_type == self.DIFF_DELETE and len(patch.diffs) == 1 and
              patch.diffs[0][0] == self.DIFF_EQUAL and
              len(diff_text) > 2 * patch_size):

            patch.length1 += len(diff_text)
            start1 += len(diff_text)
            empty = False
            patch.diffs.append((diff_type, diff_text))
            del bigpatch.diffs[0]
          else:

            diff_text = diff_text[:patch_size - patch.length1 -
                                  self.Patch_Margin]
            patch.length1 += len(diff_text)
            start1 += len(diff_text)
            if diff_type == self.DIFF_EQUAL:
              patch.length2 += len(diff_text)
              start2 += len(diff_text)
            else:
              empty = False

            patch.diffs.append((diff_type, diff_text))
            if diff_text == bigpatch.diffs[0][1]:
              del bigpatch.diffs[0]
            else:
              bigpatch.diffs[0] = (bigpatch.diffs[0][0],
                                   bigpatch.diffs[0][1][len(diff_text):])

        precontext = self.diff_text2(patch.diffs)
        precontext = precontext[-self.Patch_Margin:]

        postcontext = self.diff_text1(bigpatch.diffs)[:self.Patch_Margin]
        if postcontext:
          patch.length1 += len(postcontext)
          patch.length2 += len(postcontext)
          if len(patch.diffs) != 0 and patch.diffs[-1][0] == self.DIFF_EQUAL:
            patch.diffs[-1] = (self.DIFF_EQUAL, patch.diffs[-1][1] +
                               postcontext)
          else:
            patch.diffs.append((self.DIFF_EQUAL, postcontext))

        if not empty:
          x += 1
          patches.insert(x, patch)

Look through the patches and break up any which are longer than the
maximum limit of the match algorithm.
Intended to be called only from within patch_apply.

Args:
patches: Array of Patch objects.


class AfterWatermark(TriggerFn):
    LATE_TAG = _CombiningValueStateTag('is_late', any)

    def __init__(self, early=None, late=None):
        self.early = Repeatedly(early) if early else None
        self.late = Repeatedly(late) if late else None

    def __repr__(self):
        qualifiers = []
        if self.early:
            qualifiers.append('early=%s' % self.early.underlying)
        if self.late:
            qualifiers.append('late=%s' % self.late.underlying)
        return 'AfterWatermark(%s)' % ', '.join(qualifiers)

    def is_late(self, context):
        return self.late and context.get_state(self.LATE_TAG)

    def on_element(self, element, window, context):
        if self.is_late(context):
            self.late.on_element(element, window, NestedContext(context, 'late'))
        else:
            context.set_timer('', TimeDomain.WATERMARK, window.end)
            if self.early:
                self.early.on_element(element, window, NestedContext(context, 'early'))

    def on_merge(self, to_be_merged, merge_result, context):
        if self.is_late(context):
            self.late.on_merge(to_be_merged, merge_result, NestedContext(context, 'late'))
        else:
            for window in to_be_merged:
                if window.end != merge_result.end:
                    context.clear_timer('', TimeDomain.WATERMARK)
            if self.early:
                self.early.on_merge(to_be_merged, merge_result, NestedContext(context, 'early'))

    def should_fire(self, time_domain, watermark, window, context):
        if self.is_late(context):
            return self.late.should_fire(time_domain, watermark, window, NestedContext(context, 'late'))
        elif watermark >= window.end:
            context.clear_timer('', TimeDomain.WATERMARK)
            return True
        elif self.early:
            return self.early.should_fire(time_domain, watermark, window, NestedContext(context, 'early'))
        return False

    def on_fire(self, watermark, window, context):
        if self.is_late(context):
            return self.late.on_fire(watermark, window, NestedContext(context, 'late'))
        elif watermark >= window.end:
            context.add_state(self.LATE_TAG, True)
            return not self.late
        elif self.early:
            self.early.on_fire(watermark, window, NestedContext(context, 'early'))
            return False

    def reset(self, window, context):
        if self.late:
            context.clear_state(self.LATE_TAG)
        if self.early:
            self.early.reset(window, NestedContext(context, 'early'))
        if self.late:
            self.late.reset(window, NestedContext(context, 'late'))

    def may_lose_data(self, windowing):

        if windowing.allowed_lateness == 0:
            return DataLossReason.NO_POTENTIAL_LOSS
        if self.late is None:
            return DataLossReason.MAY_FINISH
        return self.late.may_lose_data(windowing)

    def __eq__(self, other):
        return type(self) == type(other) and self.early == other.early and (self.late == other.late)

    def __hash__(self):
        return hash((type(self), self.early, self.late))

    @staticmethod
    def from_runner_api(proto, context):
        return AfterWatermark(early=TriggerFn.from_runner_api(proto.after_end_of_window.early_firings, context) if proto.after_end_of_window.HasField('early_firings') else None, late=TriggerFn.from_runner_api(proto.after_end_of_window.late_firings, context) if proto.after_end_of_window.HasField('late_firings') else None)

    def to_runner_api(self, context):
        early_proto = self.early.underlying.to_runner_api(context) if self.early else None
        late_proto = self.late.underlying.to_runner_api(context) if self.late else None
        return beam_runner_api_pb2.Trigger(after_end_of_window=beam_runner_api_pb2.Trigger.AfterEndOfWindow(early_firings=early_proto, late_firings=late_proto))

    def has_ontime_pane(self):
        return True

Fire exactly once when the watermark passes the end of the window.

Args:
early: if not None, a speculative trigger to repeatedly evaluate before
the watermark passes the end of the window
late: if not None, a speculative trigger to repeatedly evaluate after
the watermark passes the end of the window


def build_inputs_with_special_tokens(self, token_ids_0: List[int], token_ids_1: Optional[List[int]]=None) -> List[int]:
    if token_ids_1 is None:
        return [self.cls_token_id] + token_ids_0 + [self.sep_token_id]
    cls = [self.cls_token_id]
    sep = [self.sep_token_id]
    return cls + token_ids_0 + sep + token_ids_1 + sep

Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
adding special tokens. A BERT sequence has the following format:

- single sequence: `[CLS] X [SEP]`
- pair of sequences: `[CLS] A [SEP] B [SEP]`

Args:
token_ids_0 (`List[int]`):
List of IDs to which the special tokens will be added.
token_ids_1 (`List[int]`, *optional*):
Optional second list of IDs for sequence pairs.

Returns:
`List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.


def get_crumbs(self):
    crumbs = []
    for cls in reversed(type(self).__mro__[1:]):
        crumbs.extend(getattr(cls, 'crumbs', ()))
    crumbs.extend(list(self.crumbs))
    return tuple(crumbs)

Get crumbs for navigation links.

Returns:
tuple:
concatenated list of crumbs using these crumbs and the
crumbs of the parent classes through ``__mro__``.


def viewTemplate(id):
    conn = Qubole.agent()
    return conn.get(Template.element_path(id))

View an existing Template details.

Args:
`id`: ID of the template to fetch

Returns:
Dictionary containing the details of the template.


def __init__(self, message):

        super(InvalidMessage, self).__init__(
            reason=enums.ResultReason.INVALID_MESSAGE,
            message=message
        )

Create an InvalidMessage exception.

Args:
message (string): A string containing information about the error.


def __init__(self, pattern=None, *args, **kwargs):

        super(PatternOption, self).__init__(*args, **kwargs)

        if pattern is None:

            raise ValueError("The pattern cannot be None.")

        self._pattern = pattern
        self._re = re.compile(pattern)

Initialize the option with a regex pattern.

Args:
pattern (str): The regex pattern to match against.
*args: Any position arguments required by base classes.
**kwargs: Any keyword arguments required by base classes.

Raises:
ValueError: If a pattern is not given.
TypeError: If the pattern is not a string.


def get_no_mixing_energy(self):
    assert (self.grand == 1), 'Please provide grand potential phase diagram for computing no_mixing_energy!'
    energy1 = (self.pd.get_hull_energy(self.comp1) - self._get_grand_potential(self.c1))
    energy2 = (self.pd.get_hull_energy(self.comp2) - self._get_grand_potential(self.c2))
    unit = 'eV/f.u.'
    if self.norm:
        unit = 'eV/atom'
    return [((self.c1_original.reduced_formula + ' ({0})'.format(unit)), energy1), ((self.c2_original.reduced_formula + ' ({0})'.format(unit)), energy2)]

Generates the opposite number of energy above grand potential
convex hull for both reactants.

Returns:
[(reactant1, no_mixing_energy1),(reactant2,no_mixing_energy2)].


def print_graph(self, format=None, output=sys.stdout, depth=0, **kwargs):

        graph = self.as_graph(depth=depth)
        graph.print(format=format, output=output, **kwargs)

Print the graph for self's nodes.

Args:
format (str): output format (csv, json or text).
output (file): file descriptor on which to write.
depth (int): depth of the graph.


def log_response(
        self, response: Response, trim_log_values: bool = False, **kwargs: Any
    ) -> None:

        return log_(response.text, response_log, "info", trim=trim_log_values, **kwargs)

Log a response.

Note this is different to log_request, in that it takes a Response object, not a
string.

Args:
response: The Response object to log. Note this is different to log_request
which takes a string.
trim_log_values: Log an abbreviated version of the response.


def write(self, data, timeout_ms=None):

    timeout = timeouts.PolledTimeout.from_millis(timeout_ms)

    while data:
      self._transport.write(
          data[:self._transport.adb_connection.maxdata], timeout)
      data = data[self._transport.adb_connection.maxdata:]

Write data to this stream.

Args:
data: Data to write.
timeout_ms: Timeout to use for the write/Ack transaction, in
milliseconds (or as a PolledTimeout object).

Raises:
AdbProtocolError: If an ACK is not received.
AdbStreamClosedError: If the stream is already closed, or gets closed
before the write completes.


def annotate_test_case(self, test_case: Iterator[str]) -> tuple[Iterator[str], Iterator[str]]:
    split_input = HloStreamSplitter(test_case, record_directives={DirectiveComment.RUN})
    optimizer_input, passthrough_input = itertools.tee(_fix_whitespace(split_input.non_directive_lines()))
    test_case_output = optimize_hlo(optimizer_input, self._optimizer_path, self._optimizer_args, expand_to_input=self._expand_to_input)
    test_case_checks = HloFileCheckLines(test_case_output)
    transformed_test_case = itertools.chain(test_case_checks, ['\n'], passthrough_input)
    run_directives = split_input.directive_history(DirectiveComment.RUN)
    return (transformed_test_case, run_directives)

Transforms a test case, inserting FileCheck directives above it.

Args:
test_case: An iterator over the lines of a single test case.

Returns:
A tuple of two iterators. The first iterator yields the annotated test
case containing FileCheck directives followed by the original HLO IR. The
second iterator yields any RUN directives that were stripped from the
original test case.


def save_qasm(self,
                  file_path: Union[str, bytes, int],
                  header: Optional[str] = None,
                  precision: int = 10,
                  qubit_order: ops.QubitOrderOrList = ops.QubitOrder.DEFAULT,
                  ) -> None:

        self._to_qasm_output(header, precision, qubit_order).save(file_path)

Save a QASM file equivalent to the circuit.

Args:
file_path: The location of the file where the qasm will be written.
header: A multi-line string that is placed in a comment at the top
of the QASM. Defaults to a cirq version specifier.
precision: Number of digits to use when representing numbers.
qubit_order: Determines how qubits are ordered in the QASM
register.


def recipe_sheets_copy(config, auth_read, from_sheet, from_tab, to_sheet, to_tab):
    sheets(config, {'auth': auth_read, 'template': {'sheet': from_sheet, 'tab': from_tab}, 'sheet': to_sheet, 'tab': to_tab})

Copy tab from a sheet to a sheet.

Args:
auth_read (authentication) - Credentials used for reading data.
from_sheet (string) - NA
from_tab (string) - NA
to_sheet (string) - NA
to_tab (string) - NA


def get_video_features(self, pixel_values: torch.FloatTensor, qformer_input_ids: torch.LongTensor, qformer_attention_mask: Optional[torch.LongTensor]=None, interpolate_pos_encoding: Optional[bool]=False, return_dict: Optional[bool]=False):
    batch_size, frames, channel, height, width = pixel_values.shape
    pixel_values = pixel_values.reshape(batch_size * frames, channel, height, width)
    vision_outputs = self.vision_model(pixel_values=pixel_values, interpolate_pos_encoding=interpolate_pos_encoding, return_dict=True)
    image_embeds = vision_outputs[0]
    image_attention_mask = torch.ones(image_embeds.size()[:-1], dtype=torch.long, device=image_embeds.device)
    query_tokens = self.query_tokens.expand(image_embeds.shape[0], -1, -1)
    query_attention_mask = torch.ones(query_tokens.size()[:-1], dtype=torch.long, device=image_embeds.device)
    if qformer_attention_mask is None:
        qformer_attention_mask = torch.ones_like(qformer_input_ids)
    qformer_input_ids = qformer_input_ids.repeat_interleave(frames, dim=0)
    qformer_attention_mask = qformer_attention_mask.repeat_interleave(frames, dim=0)
    qformer_attention_mask = torch.cat([query_attention_mask, qformer_attention_mask], dim=1)
    query_outputs = self.qformer(input_ids=qformer_input_ids, attention_mask=qformer_attention_mask, query_embeds=query_tokens, encoder_hidden_states=image_embeds, encoder_attention_mask=image_attention_mask, return_dict=True)
    query_output = query_outputs[0][:, :query_tokens.size(1), :]
    language_model_inputs = self.language_projection(query_output)
    language_model_inputs = language_model_inputs.reshape(batch_size, self.config.num_query_tokens * frames, -1)
    if return_dict:
        return (language_model_inputs, vision_outputs, query_outputs)
    return language_model_inputs

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

Args:
pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`):
The tensors corresponding to the input images.


def get_pkg_id(pkg):
    pkg = _quote(pkg)
    package_ids = []
    temp_dir = __salt__['temp.dir'](prefix='pkg-')
    try:
        cmd = 'xar -t -f {0} | grep PackageInfo'.format(pkg)
        out = __salt__['cmd.run'](cmd, python_shell=True, output_loglevel='quiet')
        files = out.split('\n')
        if ('Error opening' not in out):
            cmd = 'xar -x -f {0} {1}'.format(pkg, ' '.join(files))
            __salt__['cmd.run'](cmd, cwd=temp_dir, output_loglevel='quiet')
            for f in files:
                i = _get_pkg_id_from_pkginfo(os.path.join(temp_dir, f))
                if i:
                    package_ids.extend(i)
        else:
            package_ids = _get_pkg_id_dir(pkg)
    finally:
        __salt__['file.remove'](temp_dir)
    return package_ids

Attempt to get the package ID from a .pkg file

Args:
pkg (str): The location of the pkg file

Returns:
list: List of all of the package IDs

CLI Example:

.. code-block:: bash

salt '*' macpackage.get_pkg_id /tmp/test.pkg


def __init__(self, name: str, coder: Optional[Coder]=None, combine_fn: Any=None) -> None:
    from apache_beam.transforms.core import CombineFn
    if combine_fn is None:
        if coder is None:
            raise ValueError('combine_fn must be provided')
        else:
            coder, combine_fn = (None, coder)
    self.combine_fn = CombineFn.maybe_from_callable(combine_fn)
    if coder is None:
        coder = self.combine_fn.get_accumulator_coder()
    super().__init__(name, coder)

Initialize the specification for CombiningValue state.

CombiningValueStateSpec(name, combine_fn) -> Coder-inferred combining value
state spec.
CombiningValueStateSpec(name, coder, combine_fn) -> Combining value state
spec with coder and combine_fn specified.

Args:
name (str): The name by which the state is identified.
coder (Coder): Coder specifying how to encode the values to be combined.
May be inferred.
combine_fn (``CombineFn`` or ``callable``): Function specifying how to
combine the values passed to state.


def get_external_id(self, submission_id):
    submission = self.find_by_id(submission_id)
    if (not submission):
        return None
    if ('team_id' in submission.participant_id):
        return submission.participant_id['team_id']
    elif ('baseline_id' in submission.participant_id):
        return ('baseline_' + submission.participant_id['baseline_id'])
    else:
        return ''

Returns human readable submission external ID.

Args:
submission_id: internal submission ID.

Returns:
human readable ID.


def window_at(self, geom, window_shape):

        y_size, x_size = window_shape[0], window_shape[1]
        bounds = box(*geom.bounds)
        px = ops.transform(self.__geo_transform__.rev, bounds).centroid
        miny, maxy = int(px.y - y_size/2), int(px.y + y_size/2)
        minx, maxx = int(px.x - x_size/2), int(px.x + x_size/2)
        _, y_max, x_max = self.shape
        if minx < 0 or miny < 0 or maxx > x_max or maxy > y_max:
            raise ValueError("Input geometry resulted in a window outside of the image")
        return self[:, miny:maxy, minx:maxx]

Return a subsetted window of a given size, centered on a geometry object

Useful for generating training sets from vector training data
Will throw a ValueError if the window is not within the image bounds

Args:
geom (shapely,geometry): Geometry to center the image on
window_shape (tuple): The desired shape of the image as (height, width) in pixels.

Returns:
image: image object of same type


def model_custom_source(count):
    with beam.Pipeline() as pipeline:
        numbers = pipeline | 'ProduceNumbers' >> beam.io.Read(CountingSource(count))
        lines = numbers | beam.core.Map(lambda number: 'line %d' % number)
        assert_that(lines, equal_to(['line ' + str(number) for number in range(0, count)]))
    with beam.Pipeline() as pipeline:
        numbers = pipeline | 'ProduceNumbers' >> ReadFromCountingSource(count)
        lines = numbers | beam.core.Map(lambda number: 'line %d' % number)
        assert_that(lines, equal_to(['line ' + str(number) for number in range(0, count)]))

Demonstrates creating a new custom source and using it in a pipeline.

Defines a new source ``CountingSource`` that produces integers starting from 0
up to a given size.

Uses the new source in an example pipeline.

Additionally demonstrates how a source should be implemented using a
``PTransform``. This is the recommended way to develop sources that are to
distributed to a large number of end users.

This method runs two pipelines.

(1) A pipeline that uses ``CountingSource`` directly using the ``df.Read``
transform.
(2) A pipeline that uses a custom ``PTransform`` that wraps
``CountingSource``.

Args:
count: the size of the counting source to be used in the pipeline
demonstrated in this method.


def impad_to_multiple(img, divisor, pad_val=0):
    pad_h = (int(np.ceil((img.shape[0] / divisor))) * divisor)
    pad_w = (int(np.ceil((img.shape[1] / divisor))) * divisor)
    return impad(img, (pad_h, pad_w), pad_val)

Pad an image to ensure each edge to be multiple to some number.

Args:
img (ndarray): Image to be padded.
divisor (int): Padded image edges will be multiple to divisor.
pad_val (number or sequence): Same as :func:`impad`.

Returns:
ndarray: The padded image.


def __init__(self):

        self._local_id = str(uuid4())
        self._creation_date = None
        self._last_updated_date = None
        self._id = None
        self._owner = None
        self._parent_id = None
        self._parent_type = None
        self._parent = None
        self._is_dirty = False

        self._attribute_errors = dict()
        self._attributes = dict()

        self.expose_attribute(local_name='id', remote_name=BambouConfig.get_id_remote_name(), attribute_type=BambouConfig.get_id_type(), is_identifier=True)
        self.expose_attribute(local_name='parent_id', remote_name='parentID', attribute_type=str)
        self.expose_attribute(local_name='parent_type', remote_name='parentType', attribute_type=str)
        self.expose_attribute(local_name='creation_date', remote_name='creationDate', attribute_type=float, is_editable=False)
        self.expose_attribute(local_name='last_updated_date', remote_name='lastUpdatedDate', attribute_type=float, is_editable=False)
        self.expose_attribute(local_name='owner', attribute_type=str, is_readonly=True)

        self._fetchers_registry = dict()

Initializes the object with general information

Args:
creation_date: float representing time since epoch
id: identifier of the object
local_id: internal identifier of the object
owner: string representing the owner
parent_id: identifier of the object's parent
parent_type: type of the parent


def get_variation_from_key(self, experiment_key, variation_key):
    variation_map = self.variation_key_map.get(experiment_key)
    if variation_map:
        variation = variation_map.get(variation_key)
        if variation:
            return variation
        else:
            self.logger.error(('Variation key "%s" is not in datafile.' % variation_key))
            self.error_handler.handle_error(exceptions.InvalidVariationException(enums.Errors.INVALID_VARIATION_ERROR))
            return None
    self.logger.error(('Experiment key "%s" is not in datafile.' % experiment_key))
    self.error_handler.handle_error(exceptions.InvalidExperimentException(enums.Errors.INVALID_EXPERIMENT_KEY_ERROR))
    return None

Get variation given experiment and variation key.

Args:
experiment: Key representing parent experiment of variation.
variation_key: Key representing the variation.

Returns
Object representing the variation.


def bulkRead(self, endpoint, size, timeout = 100):
        r
        return self.dev.read(endpoint, size, timeout)

r"""Performs a bulk read request to the endpoint specified.

Arguments:
endpoint: endpoint number.
size: number of bytes to read.
timeout: operation timeout in milliseconds. (default: 100)
Returns a tuple with the data read.


def _create_saveables_by_attribute_name(self, saveable_factories):
    named_saveables = {}
    existing_restore_ops = []
    created_compat_names = set()
    for serialized_tensor in self.object_proto.attributes:
        if context.executing_eagerly():
            existing_op = None
        else:
            existing_op = self._checkpoint.restore_ops_by_name.get(serialized_tensor.checkpoint_key, None)
        if existing_op is not None:
            existing_restore_ops.append(existing_op)
            continue
        if any((serialized_tensor.name.startswith(name) for name in created_compat_names)):
            continue
        saveables_cache = self._checkpoint.saveables_cache
        if saveables_cache is None:
            saveable = None
        else:
            saveable_list = saveables_cache.get(self.trackable, {}).get(serialized_tensor.name, (None,))
            if len(saveable_list) == 1:
                saveable, = saveable_list
            else:
                saveable = None
        if saveable is not None:
            if serialized_tensor.checkpoint_key not in saveable.name:
                saveable = None
                del saveables_cache[self.trackable]
        if saveable is None:
            saveable = _get_saveable_from_factory(saveable_factories, serialized_tensor, created_compat_names)
            if saveable is None:
                self._checkpoint.unused_attributes.setdefault(self._proto_id, []).append(serialized_tensor.name)
                continue
            if saveables_cache is not None:
                saveables_cache.setdefault(self.trackable, {})[serialized_tensor.name] = [saveable]
        named_saveables[serialized_tensor.checkpoint_key] = saveable
    return (existing_restore_ops, named_saveables)

Creates or caches SaveableObjects by matching the attribute names.

The attribute name keys in the `saveable_factories` is used to find the
corresponding attribute in the object proto. Attributes contain checkpoint
keys which are passed to the factory function to generate the
SaveableObject.

Args:
saveable_factories: a dict mapping attribute name to a callable factory
function that produces a SaveableObject.

Returns:
A tuple of (
existing_restore_ops: list,
named_saveables: dict)


def reverse_guard(lst):

    rev = {'<': '>=', '>': '=<', '>=': '<', '=<': '>'}
    return [rev[l] if l in rev else l for l in lst]

Reverse guard expression. not
(@a > 5) ->  (@a =< 5)
Args:
lst (list): Expression
returns:
list


def update_dimension(self, key, value, description=None,
                         custom_properties=None, tags=None, **kwargs):

        data = {'description': description or '',
                'customProperties': custom_properties or {},
                'tags': tags or [],
                'key': key,
                'value': value}
        resp = self._put(self._u(self._DIMENSION_ENDPOINT_SUFFIX, key, value),
                         data=data, **kwargs)
        resp.raise_for_status()
        return resp.json()

update a dimension
Args:
key (string): key of the dimension
value (string): value of the dimension
description (optional[string]): a description
custom_properties (optional[dict]): dictionary of custom properties
tags (optional[list of strings]): list of tags associated with
metric


def read(self, domain, type_name, search_command, body=None):
    return self._request(domain, type_name, search_command, 'GET', body)

Read entry in ThreatConnect Data Store

Args:
domain (string): One of 'local', 'organization', or 'system'.
type_name (string): This is a free form index type name. The ThreatConnect API will use
this resource verbatim.
search_command (string): Search command to pass to ES.
body (str): JSON body


def CreateShapeFromNumpy(value):
    if isinstance(value, tuple):
        return Shape(xla_data_pb2.TUPLE, [CreateShapeFromNumpy(component) for component in value])
    else:
        return _CreateShapeFromNumpy(value)

Create a Shape from a Numpy array or a nested tuple structure thereof.

Args:
value: Numpy array or (possibly nested) tuple structure that bottoms out in
Numpy arrays.

Returns:
A Shape object.


def build_recursive_delocalize_command(source, outputs, file_provider):

  command = _LOCALIZE_COMMAND_MAP[file_provider]
  filtered_outputs = [
      var for var in outputs
      if var.recursive and var.file_provider == file_provider
  ]

  return '\n'.join([
      textwrap.dedent().format(
          command=command,
          data_mount=source.rstrip('/'),
          docker_path=var.docker_path,
          destination_uri=var.uri) for var in filtered_outputs
  ])

Return a multi-line string with a shell script to copy recursively.

Arguments:
source: Folder with the data.
For example /mnt/data
outputs: a list of OutputFileParam.
file_provider: file provider string used to filter the output params; the
returned command will only apply outputs whose file provider
matches this file filter.

Returns:
a multi-line string with a shell script that copies the inputs
recursively to GCS.


def stop(self) -> None:
    self._stop()

Stops the server.

Raises:
tf.errors.OpError: Or one of its subclasses if an error occurs while
stopping the server.


def _addupdate_hdxobject(self, hdxobjects, id_field, new_hdxobject):
    for hdxobject in hdxobjects:
        if (hdxobject[id_field] == new_hdxobject[id_field]):
            merge_two_dictionaries(hdxobject, new_hdxobject)
            return hdxobject
    hdxobjects.append(new_hdxobject)
    return new_hdxobject

Helper function to add a new HDX object to a supplied list of HDX objects or update existing metadata if the object
already exists in the list

Args:
hdxobjects (List[T <= HDXObject]): list of HDX objects to which to add new objects or update existing ones
id_field (str): Field on which to match to determine if object already exists in list
new_hdxobject (T <= HDXObject): The HDX object to be added/updated

Returns:
T <= HDXObject: The HDX object which was added or updated


def merge_ddb_files(self, delete_source_ddbs=True, only_dfpt_tasks=True, exclude_tasks=None, include_tasks=None):
    if exclude_tasks:
        my_tasks = [task for task in self if (task not in exclude_tasks)]
    elif include_tasks:
        my_tasks = [task for task in self if (task in include_tasks)]
    else:
        my_tasks = [task for task in self]
    if only_dfpt_tasks:
        ddb_files = list(filter(None, [task.outdir.has_abiext('DDB') for task in my_tasks if isinstance(task, DfptTask)]))
    else:
        ddb_files = list(filter(None, [task.outdir.has_abiext('DDB') for task in my_tasks]))
    self.history.info(('Will call mrgddb to merge %s DDB files:' % len(ddb_files)))
    if (not ddb_files):
        raise RuntimeError(('Cannot find any DDB file to merge by the task of ' % self))
    out_ddb = self.outdir.path_in('out_DDB')
    if (len(ddb_files) == 1):
        shutil.copy(ddb_files[0], out_ddb)
    else:
        desc = ('DDB file merged by %s on %s' % (self.__class__.__name__, time.asctime()))
        mrgddb = wrappers.Mrgddb(manager=self[0].manager, verbose=0)
        mrgddb.merge(self.outdir.path, ddb_files, out_ddb=out_ddb, description=desc, delete_source_ddbs=delete_source_ddbs)
    return out_ddb

This method is called when all the q-points have been computed.
It runs `mrgddb` in sequential on the local machine to produce
the final DDB file in the outdir of the `Work`.

Args:
delete_source_ddbs: True if input DDB should be removed once final DDB is created.
only_dfpt_tasks: False to merge all DDB files produced by the tasks of the work
Useful e.g. for finite stress corrections in which the stress in the
initial configuration should be merged in the final DDB.
exclude_tasks: List of tasks that should be excluded when merging the partial DDB files.
include_tasks: List of tasks that should be included when merging the partial DDB files.
Mutually exclusive with exclude_tasks.

Returns:
path to the output DDB file


def prune_directory(self):
    glob = '*.{ext}'.format(ext=self.backend.file_extension)
    totalsize = 0
    totalnum = 0
    for f in self._path.glob(glob):
        filesize = f.stat().st_size
        key_hash = f.stem
        in_cache = (key_hash in self._cache)
        try:
            self._get_obj_from_hash(key_hash)
        except KeyExpirationError:
            totalsize += filesize
            totalnum += 1
        except KeyInvalidError:
            pass
        except Exception:
            raise
        else:
            if (not in_cache):
                del self._cache[key_hash]
    return PrunedFilesInfo(size=totalsize, num=totalnum)

Delete any objects that can be loaded and are expired according to
the current lifetime setting.

A file will be deleted if the following conditions are met:

- The file extension matches :py:meth:`bucketcache.backends.Backend.file_extension`
- The object can be loaded by the configured backend.
- The object's expiration date has passed.

Returns:
File size and number of files deleted.

:rtype: :py:class:`~bucketcache.utilities.PrunedFilesInfo`

.. note::

For any buckets that share directories, ``prune_directory`` will
affect files saved with both, if they use the same backend class.

This is not destructive, because only files that have expired
according to the lifetime of the original bucket are deleted.


def runcoro(async_function):
    future = _asyncio.run_coroutine_threadsafe(async_function, client.loop)
    result = future.result()
    return result

Runs an asynchronous function without needing to use await - useful for lambda

Args:
async_function (Coroutine): The asynchronous function to run


def GetBasePathSpecs(self, source_path):

    if not source_path:
      raise errors.ScannerError('Invalid source path.')

    if (not source_path.startswith('\\\\.\\') and
        not os.path.exists(source_path)):
      raise errors.ScannerError(
          'No such device, file or directory: {0:s}.'.format(source_path))

    scan_context = source_scanner.SourceScannerContext()
    scan_context.OpenSourcePath(source_path)

    try:
      self._source_scanner.Scan(scan_context)
    except (ValueError, errors.BackEndError) as exception:
      raise errors.ScannerError(
          'Unable to scan source with error: {0!s}'.format(exception))

    self._source_path = source_path
    self._source_type = scan_context.source_type

    if self._source_type not in [
        definitions.SOURCE_TYPE_STORAGE_MEDIA_DEVICE,
        definitions.SOURCE_TYPE_STORAGE_MEDIA_IMAGE]:
      scan_node = scan_context.GetRootScanNode()
      return [scan_node.path_spec]

    scan_node = scan_context.GetRootScanNode()
    while len(scan_node.sub_nodes) == 1:
      scan_node = scan_node.sub_nodes[0]

    base_path_specs = []
    if scan_node.type_indicator != definitions.TYPE_INDICATOR_TSK_PARTITION:
      self._ScanVolume(scan_context, scan_node, base_path_specs)

    else:

      partition_identifiers = self._GetTSKPartitionIdentifiers(scan_node)
      for partition_identifier in partition_identifiers:
        location = '/{0:s}'.format(partition_identifier)
        sub_scan_node = scan_node.GetSubNodeByLocation(location)
        self._ScanVolume(scan_context, sub_scan_node, base_path_specs)

    return base_path_specs

Determines the base path specifications.

Args:
source_path (str): source path.

Returns:
list[PathSpec]: path specifications.

Raises:
ScannerError: if the source path does not exists, or if the source path
is not a file or directory, or if the format of or within the source
file is not supported.


def _setup_logger(self, logging_level: int, log_to_console: bool):
    self.logger = logging.getLogger('discord')
    self.logger.handlers = []
    self.logger.setLevel(logging_level)
    formatter = logging.Formatter(style='{', fmt='{asctime} [{levelname}] {message}', datefmt='%Y-%m-%d %H:%M:%S')
    file_handler = logging.FileHandler('pycord.log')
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging_level)
    self.logger.addHandler(file_handler)
    if log_to_console:
        stream_handler = logging.StreamHandler(sys.stdout)
        stream_handler.setFormatter(formatter)
        stream_handler.setLevel(logging_level)
        self.logger.addHandler(stream_handler)

Sets up the internal logger

Args:
logging_level: what logging level to use
log_to_console: whether or not to log to the console


def set_help_intro(self, help_intro):
    self._command_handler_registry.set_help_intro(help_intro=help_intro)

Set an introductory message to the help output of the command registry.

Args:
help_intro: (RichTextLines) Rich text lines appended to the beginning of
the output of the command "help", as introductory information.


def many(self, main_type, sub_type, api_entity, owner=None, filters=None, params=None):

        params = params or {}

        if owner:
            params['owner'] = owner
        if filters and filters.filters:
            params['filters'] = filters.filters_string
        if not sub_type:
            url = '/v2/{}'.format(main_type)
        else:
            url = '/v2/{}/{}'.format(main_type, sub_type)

        for i in self._iterate(url, params, api_entity):
            yield i

Args:
main_type:
sub_type:
api_entity:
filters:
owner:
params:

Returns:


def retrieve(url):

    try:
        pem_data = urlopen(url).read()
    except (ValueError, HTTPError):
        warnings.warn('Certificate URL is invalid.')
        return False

    if sys.version >= '3':
        try:
            pem_data = pem_data.decode()
        except(UnicodeDecodeError):
            warnings.warn('Certificate encoding is not utf-8.')
            return False

    return _parse_pem_data(pem_data)

Retrieve and parse PEM-encoded X.509 certificate chain.

See `validate.request` for additional info.

Args:
url: str. SignatureCertChainUrl header value sent by request.

Returns:
list or bool: If url is valid, returns the certificate chain as a list
of cryptography.hazmat.backends.openssl.x509._Certificate
certificates where certs[0] is the first certificate in the file; if
url is invalid, returns False.


def coordinate_to_patch_index(bbox: Tuple[float, float, float, float], num_patches_per_side: int) -> Tuple[int, int]:
    x1, y1, x2, y2 = bbox
    if not (x2 > x1 and y2 > y1):
        raise ValueError('The coordinates in `bbox` should be `(x1, y1, x2, y2)` with `x2 > x1` and `y2 > y1`.')
    ul_x = math.floor(x1 * num_patches_per_side)
    ul_y = math.floor(y1 * num_patches_per_side)
    lr_x = math.ceil(x2 * num_patches_per_side - 1)
    lr_y = math.ceil(y2 * num_patches_per_side - 1)
    ul_idx = ul_y * num_patches_per_side + ul_x
    lr_idx = lr_y * num_patches_per_side + lr_x
    return (ul_idx, lr_idx)

Convert a bounding box to a pair of patch indices.

Args:
bbox (`Tuple[float, float, float, float]`):
The 4 coordinates of the bounding box, with the format being (x1, y1, x2, y2) specifying the upper-left and
lower-right corners of the box. It should have x2 > x1 and y2 > y1.
num_patches_per_side (`int`): the number of patches along each side.

Returns:
`Tuple[int, int]`: A pair of patch indices representing the upper-left patch and lower-right patch.


def _copy_script_migrated(self, filename, id_=-1,
                              file_type=SCRIPT_FILE_TYPE):

        basefname = os.path.basename(filename)

        resource = open(filename, "rb")
        headers = {"DESTINATION": "1", "OBJECT_ID": str(id_), "FILE_TYPE":
                   file_type, "FILE_NAME": basefname}
        response = self.connection["jss"].session.post(
            url="%s/%s" % (self.connection["jss"].base_url, "dbfileupload"),
            data=resource, headers=headers)
        return response

Upload a script to a migrated JSS's database.

On a "migrated" JSS, scripts are POSTed to the JSS. Pass an id
if you wish to associate the script with an existing Script
object, otherwise, it will create a new Script object.

Args:
filename: Path to script file.
id_: Int ID of Script object to associate this file with.
Default is -1, which creates a new Script.


def _get_default_radius(site):

        try:
            return CovalentRadius.radius[site.specie.symbol]
        except:
            return site.specie.atomic_radius

An internal method to get a "default" covalent/element radius

Args:
site: (Site)

Returns:
Covalent radius of element on site, or Atomic radius if unavailable


def FindCoinsByVins(self, vins):

        ret = []
        for coin in self.GetCoins():
            coinref = coin.Reference
            for vin in vins:
                if coinref.PrevIndex == vin.PrevIndex and \
                        coinref.PrevHash == vin.PrevHash:
                    ret.append(coin)
        return ret

Looks through the current collection of coins in a wallet
and chooses coins that match the specified CoinReference objects.

Args:
vins: A list of ``neo.Core.CoinReference`` objects.

Returns:
list: A list of ``neo.Wallet.Coin`` objects.


def parse_cl_function(cl_code, dependencies=()):
    from mot.lib.cl_function import SimpleCLFunction

    def separate_cl_functions(input_str):
        'Separate all the OpenCL functions.\n\n        This creates a list of strings, with for each function found the OpenCL code.\n\n        Args:\n            input_str (str): the string containing one or more functions.\n\n        Returns:\n            list: a list of strings, with one string per found CL function.\n        '

        class Semantics():

            def __init__(self):
                self._functions = []

            def result(self, ast):
                return self._functions

            def arglist(self, ast):
                return '({})'.format(', '.join(ast))

            def function(self, ast):

                def join(items):
                    result = ''
                    for item in items:
                        if isinstance(item, str):
                            result += item
                        else:
                            result += join(item)
                    return result
                self._functions.append(join(ast).strip())
                return ast
        return _extract_cl_functions_parser.parse(input_str, semantics=Semantics())
    functions = separate_cl_functions(cl_code)
    return SimpleCLFunction.from_string(functions[(- 1)], dependencies=(list((dependencies or [])) + [SimpleCLFunction.from_string(s) for s in functions[:(- 1)]]))

Parse the given OpenCL string to a single SimpleCLFunction.

If the string contains more than one function, we will return only the last, with all the other added as a
dependency.

Args:
cl_code (str): the input string containing one or more functions.
dependencies (Iterable[CLCodeObject]): The list of CL libraries this function depends on

Returns:
mot.lib.cl_function.SimpleCLFunction: the CL function for the last function in the given strings.


def verify_branch(branch_name):
    try:
        shell.run('git rev-parse --verify {}'.format(branch_name), never_pretend=True)
        return True
    except IOError:
        return False

Verify if the given branch exists.

Args:
branch_name (str):
The name of the branch to check.

Returns:
bool: **True** if a branch with name *branch_name* exits, **False**
otherwise.


def _from_signer_and_info(cls, signer, info, **kwargs):

        kwargs.setdefault('subject', info['client_email'])
        kwargs.setdefault('issuer', info['client_email'])
        return cls(signer, **kwargs)

Creates a Credentials instance from a signer and service account
info.

Args:
signer (google.auth.crypt.Signer): The signer used to sign JWTs.
info (Mapping[str, str]): The service account info.
kwargs: Additional arguments to pass to the constructor.

Returns:
google.auth.jwt.Credentials: The constructed credentials.

Raises:
ValueError: If the info is not in the expected format.


def _get_tflite_converter(flags):
    input_arrays = _parse_array(flags.input_arrays)
    input_shapes = None
    if flags.input_shapes:
        input_shapes_list = [_parse_array(shape, type_fn=int) for shape in flags.input_shapes.split(':')]
        input_shapes = dict(list(zip(input_arrays, input_shapes_list)))
    output_arrays = _parse_array(flags.output_arrays)
    converter_kwargs = {'input_arrays': input_arrays, 'input_shapes': input_shapes, 'output_arrays': output_arrays}
    if flags.graph_def_file:
        converter_fn = lite.TFLiteConverter.from_frozen_graph
        converter_kwargs['graph_def_file'] = flags.graph_def_file
    elif flags.saved_model_dir:
        converter_fn = lite.TFLiteConverter.from_saved_model
        converter_kwargs['saved_model_dir'] = flags.saved_model_dir
        converter_kwargs['tag_set'] = _parse_set(flags.saved_model_tag_set)
        converter_kwargs['signature_key'] = flags.saved_model_signature_key
    elif flags.keras_model_file:
        converter_fn = lite.TFLiteConverter.from_keras_model_file
        converter_kwargs['model_file'] = flags.keras_model_file
    else:
        raise ValueError('--graph_def_file, --saved_model_dir, or --keras_model_file must be specified.')
    return converter_fn(**converter_kwargs)

Makes a TFLiteConverter object based on the flags provided.

Args:
flags: argparse.Namespace object containing TFLite flags.

Returns:
TFLiteConverter object.

Raises:
ValueError: Invalid flags.


def reset_spent_time(self, **kwargs):

        path = '%s/%s/reset_spent_time' % (self.manager.path, self.get_id())
        return self.manager.gitlab.http_post(path, **kwargs)

Resets the time spent working on the object.

Args:
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabTimeTrackingError: If the time tracking update cannot be done


def search(self, query_term):

        fedora_search_url = "/".join([self.base_url, 'rest', 'fcr:search'])
        fedora_search_url = "{}?{}".format(
            fedora_search_url,
            urllib.parse.urlencode({"q": query_term}))
        search_request = urllib.request.Request(
            fedora_search_url,
            method='GET')
        search_request.add_header('Accept', 'text/turtle')
        try:
            search_response = urllib.request.urlopen(search_request)
        except urllib.error.URLError as error:
            raise error
        fedora_results = rdflib.Graph().parse(
            data=search_response.read(),
            format='turtle')
        return fedora_results

DEPRECIATED
Method takes a query term and searches Fedora Repository using SPARQL
search endpoint and returns a RDF graph of the search results.

Args:
query_term(str): String to search repository

Returns:
rdflib.Graph()


def get_nn_images(self, structure, n):

        return [e['image'] for e in self.get_nn_info(structure, n)]

Get image location of all near neighbors of site with index n in
structure.

Args:
structure (Structure): input structure.
n (integer): index of site for which to determine the image
location of near neighbors.
Returns:
images (list of 3D integer array): image locations of
near neighbors.


def convert_into(input_path, output_path, output_format='csv', java_options=None, **kwargs):
    if ((output_path is None) or (len(output_path) is 0)):
        raise AttributeError("'output_path' shoud not be None or empty")
    kwargs['output_path'] = output_path
    kwargs['format'] = _extract_format_for_conversion(output_format)
    if (java_options is None):
        java_options = []
    elif isinstance(java_options, str):
        java_options = shlex.split(java_options)
    (path, temporary) = localize_file(input_path)
    if (not os.path.exists(path)):
        raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)
    try:
        _run(java_options, kwargs, path)
    finally:
        if temporary:
            os.unlink(path)

Convert tables from PDF into a file.

Args:
input_path (file like obj):
File like object of tareget PDF file.
output_path (str):
File path of output file.
output_format (str, optional):
Output format of this function (csv, json or tsv). Default: csv
java_options (list, optional):
Set java options like `-Xmx256m`.
kwargs (dict):
Dictionary of option for tabula-java. Details are shown in `build_options()`

Returns:
Nothing. Output file will be saved into `output_path`


def put(f, s3_path, multipart_chunk_size_mb=500, logger=None):
    if (not logger):
        logger = log.get_logger('s3')
    fname = os.path.basename(f)
    target = os.path.join(s3_path, fname)
    s3cmd_cline = 's3cmd put {} {} --multipart-chunk-size-mb {}'.format(f, target, multipart_chunk_size_mb)
    print_put_info(fname, target, logger)
    s3cmd = sp.Popen(s3cmd_cline, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
    (stdout, stderr) = s3cmd.communicate()

Uploads a single file to S3, using s3cmd.

Args:

f (str): Path to a single file.

s3_path (str): The S3 path, with the filename omitted. The S3 filename
will be the basename of the ``f``. For example::

put(f='/path/to/myfile.tar.gz', s3_path='s3://my_bucket/path/to/')

will result in an uploaded S3 path of ``s3://my_bucket/path/to/myfile.tar.gz``


def entityComment(rdfGraph, anEntity, language=DEFAULT_LANGUAGE, getall=True):

    if getall:
        temp = []
        for o in rdfGraph.objects(anEntity, RDFS.comment):
            temp += [o]
        return temp
    else:
        for o in rdfGraph.objects(anEntity, RDFS.comment):
            if getattr(o, 'language') and getattr(o, 'language') == language:
                return o
        return ""

Returns the rdfs.comment value of an entity (class or property), if existing.
Defaults to DEFAULT_LANGUAGE. Returns the RDF.Literal resource

Args:
language: 'en', 'it' etc..
getall: returns a list of all labels rather than a string


def set_parent(self, parent):

        if not isinstance(parent, Expression):
            raise FiqlObjectException("Parent must be of %s not %s" % (
                Expression, type(parent)))
        self.parent = parent

Set parent ``Expression`` for this object.

Args:
parent (Expression): The ``Expression`` which contains this object.

Raises:
FiqlObjectException: Parent must be of type ``Expression``.


def set_room_topic(self, topic):
    try:
        self.client.api.set_room_topic(self.room_id, topic)
        self.topic = topic
        return True
    except MatrixRequestError:
        return False

Set room topic.

Returns:
boolean: True if the topic changed, False if not


def getPagePixmap(doc, pno, matrix=None, colorspace=csRGB, clip=None, alpha=True):
    return doc[pno].getPixmap(matrix=matrix, colorspace=colorspace, clip=clip, alpha=alpha)

Create pixmap of document page by page number.

Notes:
Convenience function calling page.getPixmap.
Args:
pno: (int) page number
matrix: Matrix for transformation (default: Identity).
colorspace: (str/Colorspace) rgb, rgb, gray - case ignored, default csRGB.
clip: (irect-like) restrict rendering to this area.
alpha: (bool) include alpha channel


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

Get the reviews for a particular movie id.

Args:
page: (optional) Minimum value of 1.  Expected value is an integer.
language: (optional) ISO 639-1 code.
append_to_response: (optional) Comma separated, any movie method.

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


def draw_char(self, x, y, char, fg=Ellipsis, bg=Ellipsis):
    _put_char_ex(self.console_c, x, y, _format_char(char), _format_color(fg, self._fg), _format_color(bg, self._bg), 1)

Draws a single character.

Args:
x (int): x-coordinate to draw on.
y (int): y-coordinate to draw on.
char (Optional[Union[int, Text]]): An integer, single character
string, or None.

You can set the char parameter as None if you only want to change
the colors of the tile.
fg (Optional[Union[Tuple[int, int, int], int, Ellipsis]])
bg (Optional[Union[Tuple[int, int, int], int, Ellipsis]])

Raises: AssertionError: Having x or y values that can't be placed
inside of the console will raise an AssertionError.
You can use always use ((x, y) in console) to
check if a tile is drawable.

.. seealso:: :any:`get_char`


def log_every_n(level, msg, n, *args):
    count = _GetNextLogCountPerToken(_GetFileAndLine())
    log_if(level, msg, (not (count % n)), *args)

Log 'msg % args' at level 'level' once per 'n' times.

Logs the 1st call, (N+1)st call, (2N+1)st call,  etc.
Not threadsafe.

Args:
level: The level at which to log.
msg: The message to be logged.
n: The number of times this should be called before it is logged.
*args: The args to be substituted into the msg.


def create_datapipeline(self):
    tags = [{'key': 'app_group', 'value': self.group}, {'key': 'app_name', 'value': self.app_name}]
    response = self.client.create_pipeline(name=self.datapipeline_data.get('name', self.app_name), uniqueId=self.app_name, description=self.datapipeline_data['description'], tags=tags)
    self.pipeline_id = response.get('pipelineId')
    LOG.debug(response)
    LOG.info('Successfully configured Data Pipeline - %s', self.app_name)
    return response

Creates the data pipeline if it does not already exist

Returns:
dict: the response of the Boto3 command


def from_json(cls, data):

        optional_keys = ('city', 'state', 'country', 'latitude', 'longitude',
                         'time_zone', 'elevation', 'station_id', 'source')
        for key in optional_keys:
            if key not in data:
                data[key] = None

        return cls(data['city'], data['state'], data['country'], data['latitude'],
                   data['longitude'], data['time_zone'], data['elevation'],
                   data['station_id'], data['source'])

Create a location from a dictionary.

Args:
data: {
"city": "-",
"latitude": 0,
"longitude": 0,
"time_zone": 0,
"elevation": 0}


def SendReply(self, response, tag=None):

    del tag

    if not isinstance(response, rdfvalue.RDFValue):
      raise ValueError("SendReply can only send a Semantic Value")

    if self.runner_args.request_state.session_id:

      request_state = self.runner_args.request_state

      request_state.response_count += 1

      msg = rdf_flows.GrrMessage(
          session_id=request_state.session_id,
          request_id=request_state.id,
          response_id=request_state.response_count,
          auth_state=rdf_flows.GrrMessage.AuthorizationState.AUTHENTICATED,
          type=rdf_flows.GrrMessage.Type.MESSAGE,
          payload=response,
          args_rdf_name=response.__class__.__name__,
          args_age=int(response.age))

      self.queue_manager.QueueResponse(msg)

      if self.runner_args.write_intermediate_results:
        self.QueueReplyForResultCollection(response)

    else:

      self.QueueReplyForResultCollection(response)

Allows this flow to send a message to its parent flow.

If this flow does not have a parent, the message is ignored.

Args:
response: An RDFValue() instance to be sent to the parent.
tag: If specified, tag the result with the following tag. NOTE: supported
in REL_DB implementation only.

Raises:
ValueError: If responses is not of the correct type.


def _load(cls, prefix, user_agent_config_yaml, user_agent_lookup=None):

        if not user_agent_config_yaml:
            user_agent_config_yaml = cls.default_user_agent_config_yaml
            logger.info(
                'No user agent or user agent config file given. Using default user agent config file: %s.' % user_agent_config_yaml)
        if not isfile(user_agent_config_yaml):
            raise UserAgentError(
                "User_agent should be supplied in a YAML config file. It can be your project's name for example.")
        logger.info('Loading user agent config from: %s' % user_agent_config_yaml)
        user_agent_config_dict = load_yaml(user_agent_config_yaml)
        if user_agent_lookup:
            user_agent_config_dict = user_agent_config_dict.get(user_agent_lookup)
        if not user_agent_config_dict:
            raise UserAgentError("No user agent information read from: %s" % user_agent_config_yaml)
        ua = user_agent_config_dict.get('user_agent')
        return cls._construct(user_agent_config_dict, prefix, ua)

Load user agent YAML file

Args:
prefix (str): Text to put at start of user agent
user_agent_config_yaml (str): Path to user agent YAML file
user_agent_lookup (Optional[str]): Lookup key for YAML. Ignored if user_agent supplied.

Returns:
str: user agent


def delete(self, project_id):
    self.logger.debug(('Deleting project by id: ' + project_id))
    url = ('%(base_url)s/%(project_id)s' % {'base_url': self.base_url, 'project_id': project_id})
    r = self.gbdx_connection.delete(url)
    r.raise_for_status()

Deletes a project by id

Args:
project_id: The project id to delete

Returns:
Nothing


def is_unit_acceptable(self, unit, raise_exception=True):
    _is_acceptable = (unit in self.units)
    if (_is_acceptable or (raise_exception is False)):
        return _is_acceptable
    else:
        raise ValueError('{0} is not an acceptable unit type for {1}. Choose from the following: {2}'.format(unit, self.__class__.__name__, self.units))

Check if a certain unit is acceptable for the data type.

Args:
unit: A text string representing the abbreviated unit.
raise_exception: Set to True to raise an exception if not acceptable.


def register_revived_type(identifier, predicate, versions):
    versions.sort(key=lambda reg: reg.version, reverse=True)
    if not versions:
        raise AssertionError('Need at least one version of a registered type.')
    version_numbers = set()
    for registration in versions:
        registration.identifier = identifier
        if registration.version in version_numbers:
            raise AssertionError(f'Got multiple registrations with version {registration.version} for type {identifier}.')
        version_numbers.add(registration.version)
    _REVIVED_TYPE_REGISTRY[identifier] = (predicate, versions)
    _TYPE_IDENTIFIERS.append(identifier)

Register a type for revived objects.

Args:
identifier: A unique string identifying this class of objects.
predicate: A Boolean predicate for this registration. Takes a
trackable object as an argument. If True, `type_registration` may be
used to save and restore the object.
versions: A list of `VersionedTypeRegistration` objects.


def read_tree_dendropy(tree):

    out = Tree(); d2t = dict()
    if not hasattr(tree, 'preorder_node_iter') or not hasattr(tree, 'seed_node') or not hasattr(tree, 'is_rooted'):
        raise TypeError("tree must be a DendroPy Tree object")
    if tree.is_rooted != True:
        out.is_rooted = False
    for node in tree.preorder_node_iter():
        if node == tree.seed_node:
            curr = out.root
        else:
            curr = Node(); d2t[node.parent_node].add_child(curr)
        d2t[node] = curr; curr.edge_length = node.edge_length
        if hasattr(node, 'taxon') and node.taxon is not None:
            curr.label = node.taxon.label
        else:
            curr.label = node.label
    return out

Create a TreeSwift tree from a DendroPy tree

Args:
``tree`` (``dendropy.datamodel.treemodel``): A Dendropy ``Tree`` object

Returns:
``Tree``: A TreeSwift tree created from ``tree``


def change_password(self, username, newpassword, raise_on_error=False):

        response = self._put(self.rest_url + "/user/password",
                             data=json.dumps({"value": newpassword}),
                             params={"username": username})

        if response.ok:
            return True

        if raise_on_error:
            raise RuntimeError(response.json()['message'])

        return False

Change new password for a user

Args:
username: The account username.

newpassword: The account new password.

raise_on_error: optional (default: False)

Returns:
True: Succeeded
False: If unsuccessful


def cmd_set(context):

  if context.value in EFConfig.SPECIAL_VERSIONS and context.env_short not in EFConfig.SPECIAL_VERSION_ENVS:
    fail("special version: {} not allowed in env: {}".format(context.value, context.env_short))

  if context.value in EFConfig.SPECIAL_VERSIONS and context.stable:
    fail("special versions such as: {} cannot be marked 'stable'".format(context.value))

  if context.value == "=prod":
    context.value = context.versionresolver.lookup("{},{}/{}".format(context.key, "prod", context.service_name))
  elif context.value == "=staging":
    context.value = context.versionresolver.lookup("{},{}/{}".format(context.key, "staging", context.service_name))
  elif context.value == "=latest":
    if not EFConfig.VERSION_KEYS[context.key]["allow_latest"]:
      fail("=latest cannot be used with key: {}".format(context.key))
    func_name = "_getlatest_" + context.key.replace("-", "_")
    if func_name in globals() and isfunction(globals()[func_name]):
      context.value = globals()[func_name](context)
    else:
      raise RuntimeError("{} version for {}/{} is '=latest' but can't look up because method not found: {}".format(
                         context.key, context.env, context.service_name, func_name))

  try:
    precheck(context)
  except Exception as e:
    fail("Precheck failed: {}".format(e.message))

  s3_key = "{}/{}/{}".format(context.service_name, context.env, context.key)
  s3_version_status = EFConfig.S3_VERSION_STATUS_STABLE if context.stable else EFConfig.S3_VERSION_STATUS_UNDEFINED

  context.limit = 1
  current_version = get_versions(context)

  if len(current_version) == 1 and current_version[0].status == s3_version_status and \
          current_version[0].value == context.value:
      print("Version not written because current version and new version have identical value and status: {} {}"
            .format(current_version[0].value, current_version[0].status))
      return

  if not context.commit:
    print("=== DRY RUN ===\nUse --commit to set value\n=== DRY RUN ===")
    print("would set key: {} with value: {} {} {} {} {}".format(
          s3_key, context.value, context.build_number, context.commit_hash, context.location, s3_version_status))
  else:
    context.aws_client("s3").put_object(
        ACL='bucket-owner-full-control',
        Body=context.value,
        Bucket=EFConfig.S3_VERSION_BUCKET,
        ContentEncoding=EFConfig.S3_VERSION_CONTENT_ENCODING,
        Key=s3_key,
        Metadata={
            EFConfig.S3_VERSION_BUILDNUMBER_KEY: context.build_number,
            EFConfig.S3_VERSION_COMMITHASH_KEY: context.commit_hash,
            EFConfig.S3_VERSION_LOCATION_KEY: context.location,
            EFConfig.S3_VERSION_MODIFIEDBY_KEY: context.aws_client("sts").get_caller_identity()["Arn"],
            EFConfig.S3_VERSION_STATUS_KEY: s3_version_status
        },
        StorageClass='STANDARD'
    )
    print("set key: {} with value: {} {} {} {} {}".format(
          s3_key, context.value, context.build_number, context.commit_hash, context.location, s3_version_status))

Set the new "current" value for a key.
If the existing current version and the new version have identical /value/ and /status,
then nothing is written, to avoid stacking up redundant entreis in the version table.
Args:
context: a populated EFVersionContext object


def _get_raw_feature_as_tensor(self, key):
    raw_feature = self._features[key]
    feature_tensor = sparse_tensor_lib.convert_to_tensor_or_sparse_tensor(raw_feature)

    def expand_dims(input_tensor):
        if isinstance(input_tensor, sparse_tensor_lib.SparseTensor):
            return sparse_ops.sparse_reshape(input_tensor, [array_ops.shape(input_tensor)[0], 1])
        else:
            return array_ops.expand_dims(input_tensor, -1)
    rank = feature_tensor.get_shape().ndims
    if rank is not None:
        if rank == 0:
            raise ValueError('Feature (key: {}) cannot have rank 0. Given: {}'.format(key, feature_tensor))
        return feature_tensor if rank != 1 else expand_dims(feature_tensor)
    with ops.control_dependencies([check_ops.assert_positive(array_ops.rank(feature_tensor), message='Feature (key: {}) cannot have rank 0. Given: {}'.format(key, feature_tensor))]):
        return cond.cond(math_ops.equal(1, array_ops.rank(feature_tensor)), lambda: expand_dims(feature_tensor), lambda: feature_tensor)

Gets the raw_feature (keyed by `key`) as `tensor`.

The raw feature is converted to (sparse) tensor and maybe expand dim.

For both `Tensor` and `SparseTensor`, the rank will be expanded (to 2) if
the rank is 1. This supports dynamic rank also. For rank 0 raw feature, will
error out as it is not supported.

Args:
key: A `str` key to access the raw feature.

Returns:
A `Tensor` or `SparseTensor`.

Raises:
ValueError: if the raw feature has rank 0.


def latest_checkpoint(self):
    return self._latest_checkpoint

The prefix of the most recent checkpoint in `directory`.

Equivalent to `tf.train.latest_checkpoint(directory)` where `directory` is
the constructor argument to `CheckpointManager`.

Suitable for passing to `tf.train.Checkpoint.restore` to resume training.

Returns:
The checkpoint prefix. If there are no checkpoints, returns `None`.


def __init__(self, channel):

        self.CreateIndex = channel.unary_unary(
            "/google.firestore.admin.v1beta1.FirestoreAdmin/CreateIndex",
            request_serializer=google_dot_cloud_dot_firestore__v1beta1_dot_proto_dot_admin_dot_firestore__admin__pb2.CreateIndexRequest.SerializeToString,
            response_deserializer=google_dot_longrunning_dot_operations__pb2.Operation.FromString,
        )
        self.ListIndexes = channel.unary_unary(
            "/google.firestore.admin.v1beta1.FirestoreAdmin/ListIndexes",
            request_serializer=google_dot_cloud_dot_firestore__v1beta1_dot_proto_dot_admin_dot_firestore__admin__pb2.ListIndexesRequest.SerializeToString,
            response_deserializer=google_dot_cloud_dot_firestore__v1beta1_dot_proto_dot_admin_dot_firestore__admin__pb2.ListIndexesResponse.FromString,
        )
        self.GetIndex = channel.unary_unary(
            "/google.firestore.admin.v1beta1.FirestoreAdmin/GetIndex",
            request_serializer=google_dot_cloud_dot_firestore__v1beta1_dot_proto_dot_admin_dot_firestore__admin__pb2.GetIndexRequest.SerializeToString,
            response_deserializer=google_dot_cloud_dot_firestore__v1beta1_dot_proto_dot_admin_dot_index__pb2.Index.FromString,
        )
        self.DeleteIndex = channel.unary_unary(
            "/google.firestore.admin.v1beta1.FirestoreAdmin/DeleteIndex",
            request_serializer=google_dot_cloud_dot_firestore__v1beta1_dot_proto_dot_admin_dot_firestore__admin__pb2.DeleteIndexRequest.SerializeToString,
            response_deserializer=google_dot_protobuf_dot_empty__pb2.Empty.FromString,
        )

Constructor.

Args:
channel: A grpc.Channel.


def expect_true(condition, msg, extras=None):
    try:
        asserts.assert_true(condition, msg, extras)
    except signals.TestSignal as e:
        logging.exception('Expected a `True` value, got `False`.')
        recorder.add_error(e)

Expects an expression evaluates to True.

If the expectation is not met, the test is marked as fail after its
execution finishes.

Args:
expr: The expression that is evaluated.
msg: A string explaining the details in case of failure.
extras: An optional field for extra information to be included in test
result.


def ordinal_to_year_month_day(ordinals):
    with tf.compat.v1.name_scope(None, 'o2ymd', [ordinals]):
        ordinals = tf.convert_to_tensor(ordinals, dtype=tf.int32, name='ordinals')
        ordinals -= _ORDINAL_OF_1_3_0000
        era = ordinals
        day_of_era = ordinals % _DAYS_IN_ERA
        year_of_era = (day_of_era - day_of_era
        year = year_of_era + era * _YEARS_IN_ERA
        day_of_year = day_of_era - (_DAYS_IN_YEAR * year_of_era + year_of_era
        months = _day_of_year_to_month(day_of_year)
        days = day_of_year - _days_in_year_before_month(months) + 1
        months = months + tf.compat.v2.where(months < 10, 3, -9)
        year += tf.compat.v2.where(months <= 2, 1, 0)
        return (year, months, days)

Calculates years, months and dates Tensor given ordinals Tensor.

Args:
ordinals: Tensor of int32 type. Each element is number of days since 1 Jan
0001. 1 Jan 0001 has `ordinal = 1`.

Returns:
Tuple (years, months, days), each element is an int32 Tensor of the same
shape as `ordinals`. `months` and `days` are one-based.


def ReadClientFullInfo(self, client_id):
    result = self.MultiReadClientFullInfo([client_id])
    try:
        return result[client_id]
    except KeyError:
        raise UnknownClientError(client_id)

Reads full client information for a single client.

Args:
client_id: A GRR client id string, e.g. "C.ea3b2b71840d6fa7".

Returns:
A `ClientFullInfo` instance for given client.

Raises:
UnknownClientError: if no client with such id was found.


def get_enterprise_customer_user(user_id, enterprise_uuid):
    EnterpriseCustomerUser = apps.get_model('enterprise', 'EnterpriseCustomerUser')
    try:
        return EnterpriseCustomerUser.objects.get(enterprise_customer__uuid=enterprise_uuid, user_id=user_id)
    except EnterpriseCustomerUser.DoesNotExist:
        return None

Return the object for EnterpriseCustomerUser.

Arguments:
user_id (str): user identifier
enterprise_uuid (UUID): Universally unique identifier for the enterprise customer.

Returns:
(EnterpriseCustomerUser): enterprise customer user record


def ValidateCertificateHostname(cert, hostname):

  hosts = GetValidHostsForCert(cert)
  boto.log.debug(
      "validating server certificate: hostname=%s, certificate hosts=%s",
      hostname, hosts)
  for host in hosts:
    host_re = host.replace('.', '\.').replace('*', '[^.]*')
    if re.search('^%s$' % (host_re,), hostname, re.I):
      return True
  return False

Validates that a given hostname is valid for an SSL certificate.

Args:
cert: A dictionary representing an SSL certificate.
hostname: The hostname to test.
Returns:
bool: Whether or not the hostname is valid for this certificate.


def cert_chain(certs):

    if len(certs) < 2:
        warnings.warn('Certificate chain contains < 3 certificates.')
        return False

    cert = certs[0]
    today = datetime.datetime.today()
    if not today > cert.not_valid_before:
        warnings.warn('Certificate Not Before date is invalid.')
        return False
    if not today < cert.not_valid_after:
        warnings.warn('Certificate Not After date is invalid.')
        return False

    oid_san = x509.oid.ExtensionOID.SUBJECT_ALTERNATIVE_NAME
    ext = cert.extensions.get_extension_for_oid(oid_san)
    sans = ext.value.get_values_for_type(x509.DNSName)
    if not 'echo-api.amazon.com' in sans:
        return False

    for i in range(len(certs) - 1):
        if not certs[i].issuer == certs[i + 1].subject:
            return False

    return True

Validate PEM-encoded X.509 certificate chain.

See `validate.request` for additional info.

Args:
certs: list. The certificate chain as a list of
cryptography.hazmat.backends.openssl.x509._Certificate certificates.
See `validate.retrieve` to create certs obj.

Returns:
bool: True if valid, False otherwise.


def convert_atomic_to_mass_fractions(atomic_fractions):

    atomic_masses = {}
    total_atomic_mass = 0.0
    for z, atomic_fraction in atomic_fractions.items():
        atomic_mass = pyxray.element_atomic_weight(z)
        atomic_masses[z] = atomic_mass
        total_atomic_mass += atomic_fraction * atomic_mass

    mass_fractions = {}
    for z, atomic_fraction in atomic_fractions.items():
        mass_fractions[z] = atomic_fraction * atomic_masses[z] / total_atomic_mass

    return mass_fractions

Converts an atomic fraction :class:`dict` to a mass fraction :class:`dict`.

Args:
atomic_fractions (dict): atomic fraction :class:`dict`.
The composition is specified by a dictionary.
The keys are atomic numbers and the values atomic fractions.
No wildcard are accepted.


def do_quantize_training_on_graphdef(input_graph, num_bits):
    graph = graph_pb2.GraphDef()
    result_graph_string = DoQuantizeTrainingOnGraphDefHelper(input_graph.SerializeToString(), num_bits)
    graph.ParseFromString(result_graph_string)
    return graph

A general quantization scheme is being developed in `tf.contrib.quantize`.

Consider using that instead, though since it is in the tf.contrib namespace,
it is not subject to backward compatibility guarantees.

Args:
input_graph: A `GraphDef`.
num_bits: The number of bits for quantize training.

Returns:
The graph with quantize training done.


def format_candidate(self, index: int, display_format: str='choice_and_literal') -> Union[str, int, float]:
    if display_format not in ['choice', 'literal', 'choice_and_literal']:
        raise ValueError(f"`display_format` must be either 'choice', 'literal', or 'choice_and_literal'. Encountered: {display_format!r}.")
    if self.literal_values:
        if display_format == 'literal':
            return self.literal_values[index]
        elif display_format == 'choice_and_literal':
            return f'{index}/{len(self.candidates)} ({self.literal_values[index]})'
    return f'{index}/{len(self.candidates)}'

Get a formatted candidate value by index.

Args:
index: The index of the candidate to format.
display_format: One of 'choice', 'literal' and 'choice_and_literal' as
the output format for human consumption.

Returns:
A int, float or string that represent the candidate based on the
display format.