def disconnect_sync(self, conn_id):
    done = threading.Event()
    result = {}

    def disconnect_done(conn_id, adapter_id, status, reason):
        result['success'] = status
        result['failure_reason'] = reason
        done.set()
    self.disconnect_async(conn_id, disconnect_done)
    done.wait()
    return result

Synchronously disconnect from a connected device

Args:
conn_id (int): A unique identifier that will refer to this connection

Returns:
dict: A dictionary with two elements
'success': a bool with the result of the connection attempt
'failure_reason': a string with the reason for the failure if we failed


class Mamba2Output(ModelOutput):
    last_hidden_state: Optional[torch.FloatTensor] = None
    cache_params: Optional[Mamba2Cache] = None
    hidden_states: Optional[Tuple[torch.FloatTensor]] = None

Class for the MAMBA2 model outputs.

Args:
last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
Sequence of hidden-states at the output of the last layer of the model.
cache_params (`Mamba2Cache`):
The state of the model at the last time step. Can be used in a forward method with the next `input_ids` to
avoid providing the old `input_ids`.

Includes both the State space model state matrices after the selective scan, and the Convolutional states
hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.

Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.


def beta_to_uni(text, strict=False):
    param_key = (strict,)
    try:
        t = _BETA_CONVERSION_TRIES[param_key]
    except KeyError:
        t = _create_conversion_trie(*param_key)
        _BETA_CONVERSION_TRIES[param_key] = t
    transform = []
    idx = 0
    possible_word_boundary = False
    while (idx < len(text)):
        if (possible_word_boundary and _penultimate_sigma_word_final(transform)):
            transform[(- 2)] = _FINAL_LC_SIGMA
        step = t.longest_prefix(text[idx:(idx + _MAX_BETA_TOKEN_LEN)])
        if step:
            possible_word_boundary = (text[idx] in _BETA_PUNCTUATION)
            (key, value) = step
            transform.append(value)
            idx += len(key)
        else:
            possible_word_boundary = True
            transform.append(text[idx])
            idx += 1
    if (possible_word_boundary and _penultimate_sigma_word_final(transform)):
        transform[(- 2)] = _FINAL_LC_SIGMA
    elif ((len(transform) > 0) and (transform[(- 1)] == _MEDIAL_LC_SIGMA)):
        transform[(- 1)] = _FINAL_LC_SIGMA
    converted = ''.join(transform)
    return converted

Converts the given text from betacode to unicode.

Args:
text: The beta code text to convert. All of this text must be betacode.
strict: Flag to allow for flexible diacritic order on input.

Returns:
The converted text.


def handle_subscribe(self, request, path):
    ret = []
    if path:
        name = path[0]
        if (name not in self.children):
            self.children[name] = NotifierNode(getattr(self.data, name, None), self)
        ret += self.children[name].handle_subscribe(request, path[1:])
    else:
        serialized = serialize_object(self.data)
        if request.delta:
            self.delta_requests.append(request)
            ret.append(request.delta_response([[[], serialized]]))
        else:
            self.update_requests.append(request)
            ret.append(request.update_response(serialized))
    return ret

Add to the list of request to notify, and notify the initial value of
the data held

Args:
request (Subscribe): The subscribe request
path (list): The relative path from ourself

Returns:
list: [(callback, Response)] that need to be called


def evaluate_ising(linear, quad, state):

    if _numpy and isinstance(state, np.ndarray):
        return evaluate_ising(linear, quad, state.tolist())

    energy = 0.0
    for index, value in uniform_iterator(linear):
        energy += state[index] * value
    for (index_a, index_b), value in six.iteritems(quad):
        energy += value * state[index_a] * state[index_b]
    return energy

Calculate the energy of a state given the Hamiltonian.

Args:
linear: Linear Hamiltonian terms.
quad: Quadratic Hamiltonian terms.
state: Vector of spins describing the system state.

Returns:
Energy of the state evaluated by the given energy function.


def __init__(self, nonce_id=None, nonce_value=None):

        super(Nonce, self).__init__(tag=enums.Tags.NONCE)

        self._nonce_id = None
        self._nonce_value = None

        self.nonce_id = nonce_id
        self.nonce_value = nonce_value

Construct a Nonce struct.

Args:
nonce_id (bytes): A binary string representing the ID of the nonce
value. Optional, defaults to None. Required for encoding and
decoding.
nonce_value (bytes): A binary string representing a random value.
Optional, defaults to None. Required for encoding and decoding.


def _empty_dict_pylist_from_row_partitions(row_partitions, nrows):
    if not row_partitions:
        return [{} for _ in range(nrows)]
    else:
        values = _empty_dict_pylist_from_row_partitions(row_partitions[1:], row_partitions[0].row_splits()[-1])
        splits = row_partitions[0].row_splits()
        return [values[splits[i]:splits[i + 1]] for i in range(len(splits) - 1)]

Returns a python list of empty dicts from the given row partitions.

Args:
row_partitions: The row-partitions describing the ragged shape of the
result.
nrows: The number of rows in the outermost row-partition.  (Or if
`len(row_partitions)==0`, then the number of empty dicts to return.)

Returns:
A nested python list whose leaves (if any) are empty python dicts.


def _cursor_pb(cursor_pair):

    if cursor_pair is not None:
        data, before = cursor_pair
        value_pbs = [_helpers.encode_value(value) for value in data]
        return query_pb2.Cursor(values=value_pbs, before=before)

Convert a cursor pair to a protobuf.

If ``cursor_pair`` is :data:`None`, just returns :data:`None`.

Args:
cursor_pair (Optional[Tuple[list, bool]]): Two-tuple of

* a list of field values.
* a ``before`` flag

Returns:
Optional[google.cloud.firestore_v1beta1.types.Cursor]: A
protobuf cursor corresponding to the values.


def writeCmdMsg(self, msg):
    ekm_log(((('(writeCmdMsg | ' + self.getContext()) + ') ') + msg))
    self.m_command_msg = msg

Internal method to set the command result string.

Args:
msg (str): Message built during command.


def _jacobian_both(nodes, degree, dimension):
    r
    _, num_nodes = nodes.shape
    result = np.empty((2 * dimension, num_nodes - degree - 1), order="F")
    result[:dimension, :] = jacobian_s(nodes, degree, dimension)
    result[dimension:, :] = jacobian_t(nodes, degree, dimension)
    return result

r"""Compute :math:`s` and :math:`t` partial of :math:`B`.

.. note::

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

Args:
nodes (numpy.ndarray): Array of nodes in a surface.
degree (int): The degree of the surface.
dimension (int): The dimension the surface lives in.

Returns:
numpy.ndarray: Nodes of the Jacobian surfaces in
B |eacute| zier form.


def __get_state_by_id(cls, job_id):
    state = model.MapreduceState.get_by_job_id(job_id)
    if (state is None):
        raise ValueError(('Job state for job %s is missing.' % job_id))
    return state

Get job state by id.

Args:
job_id: job id.

Returns:
model.MapreduceState for the job.

Raises:
ValueError: if the job state is missing.


def build_synchronize_decorator():
    lock = threading.Lock()

    def lock_decorator(fn):

        @functools.wraps(fn)
        def lock_decorated(*args, **kwargs):
            with lock:
                return fn(*args, **kwargs)
        return lock_decorated
    return lock_decorator

Returns a decorator which prevents concurrent calls to functions.

Usage:
synchronized = build_synchronize_decorator()

@synchronized
def read_value():
...

@synchronized
def write_value(x):
...

Returns:
make_threadsafe (fct): The decorator which lock all functions to which it
is applied under a same lock


def get_list(self, obj_class, data, subset):
    url = obj_class.get_url(data)
    if (obj_class.can_list and obj_class.can_get):
        if ((subset and (len(subset) == 1) and (subset[0].upper() == 'BASIC')) and (obj_class is jssobjects.Computer)):
            url += '/subset/basic'
        result = self.jss.get(url)
        if obj_class.container:
            result = result.find(obj_class.container)
        return self._build_jss_object_list(result, obj_class)
    elif obj_class.can_get:
        xmldata = self.jss.get(url)
        return obj_class(self.jss, xmldata)
    else:
        raise JSSMethodNotAllowedError(obj_class.__class__.__name__)

Get a list of objects as JSSObjectList.

Args:
obj_class: The JSSObject subclass type to search for.
data: None
subset: Some objects support a subset for listing; namely
Computer, with subset="basic".

Returns:
JSSObjectList


def jump( self ):

        potential_jumps = self.potential_jumps()
        if not potential_jumps:
            raise BlockedLatticeError('No moves are possible in this lattice')
        all_transitions = transitions.Transitions( self.potential_jumps() )
        random_jump = all_transitions.random()
        delta_t = all_transitions.time_to_jump()
        self.time += delta_t
        self.update_site_occupation_times( delta_t )
        self.update( random_jump )
        return( all_transitions.time_to_jump() )

Select a jump at random from all potential jumps, then update the lattice state.

Args:
None

Returns:
None


def take_bug_report(self, test_name=None, begin_time=None, timeout=300, destination=None):
    prefix = DEFAULT_BUG_REPORT_NAME
    if test_name:
        prefix = '%s,%s' % (DEFAULT_BUG_REPORT_NAME, test_name)
    if begin_time is None:
        begin_time = mobly_logger.get_log_file_timestamp()
    new_br = True
    try:
        stdout = self.adb.shell('bugreportz -v').decode('utf-8')
        if 'not found' in stdout:
            new_br = False
    except adb.AdbError:
        new_br = False
    if destination is None:
        destination = os.path.join(self.log_path, 'BugReports')
    br_path = utils.abs_path(destination)
    utils.create_dir(br_path)
    filename = self.generate_filename(prefix, str(begin_time), 'txt')
    if new_br:
        filename = filename.replace('.txt', '.zip')
    full_out_path = os.path.join(br_path, filename)
    self.wait_for_boot_completion()
    self.log.debug('Start taking bugreport.')
    if new_br:
        out = self.adb.shell('bugreportz', timeout=timeout).decode('utf-8')
        if not out.startswith('OK'):
            raise DeviceError(self, 'Failed to take bugreport: %s' % out)
        br_out_path = out.split(':')[1].strip()
        self.adb.pull([br_out_path, full_out_path])
        self.adb.shell(['rm', br_out_path])
    else:
        self.adb.bugreport(' > "%s"' % full_out_path, shell=True, timeout=timeout)
    self.log.debug('Bugreport taken at %s.', full_out_path)
    return full_out_path

Takes a bug report on the device and stores it in a file.

Args:
test_name: Name of the test method that triggered this bug report.
begin_time: Timestamp of when the test started. If not set, then
this will default to the current time.
timeout: float, the number of seconds to wait for bugreport to
complete, default is 5min.
destination: string, path to the directory where the bugreport
should be saved.

Returns:
A string that is the absolute path to the bug report on the host.


def space(self, newlines=1):

        space = Space()
        for line in range(newlines):
            space.add_line('\n')
        self._container.structure.insert(self._idx, space)
        self._idx += 1
        return self

Creates a vertical space of newlines

Args:
newlines (int): number of empty lines

Returns:
self for chaining


def relative_batch_tokens_ids_to_midi(self, tokens: np.ndarray, beatstep: np.ndarray, beat_offset_idx: int=0, bars_per_batch: int=2, cutoff_time_idx: int=12):
    beat_offset_idx = 0 if beat_offset_idx is None else beat_offset_idx
    notes = self.relative_batch_tokens_ids_to_notes(tokens=tokens, beat_offset_idx=beat_offset_idx, bars_per_batch=bars_per_batch, cutoff_time_idx=cutoff_time_idx)
    midi = self.notes_to_midi(notes, beatstep, offset_sec=beatstep[beat_offset_idx])
    return midi

Converts tokens to Midi. This method calls `relative_batch_tokens_ids_to_notes` method to convert batch tokens
to notes then uses `notes_to_midi` method to convert them to Midi.

Args:
tokens (`numpy.ndarray`):
Denotes tokens which alongside beatstep will be converted to Midi.
beatstep (`np.ndarray`):
We get beatstep from feature extractor which is also used to get Midi.
beat_offset_idx (`int`, *optional*, defaults to 0):
Denotes beat offset index for each note in generated Midi.
bars_per_batch (`int`, *optional*, defaults to 2):
A parameter to control the Midi output generation.
cutoff_time_idx (`int`, *optional*, defaults to 12):
Denotes the cutoff time index for each note in generated Midi.


def compress_mean(x, dim, compression_factor):

  dims = x.shape.dims
  pos = dims.index(dim)
  compressed_dim = mtf.Dimension(dim.name, dim.size
  compression_factor_dim = mtf.Dimension(
      "compression_factor", compression_factor)
  new_shape = (
      dims[:pos] + [compressed_dim, compression_factor_dim] + dims[pos + 1:])
  x = mtf.reshape(x, new_shape)
  x = mtf.reduce_mean(x, reduced_dim=compression_factor_dim)
  return x

Compress by taking group means.

Args:
x: a Tensor
dim: a dimension in x.shape
compression_factor: an integer

Returns:
a Tensor


def deploy(app_id, version, promote, quiet):
    gae_app = GaeApp.for_branch(git.current_branch().name)
    if ((gae_app is None) and (None in (app_id, version))):
        msg = "Can't find an AppEngine app setup for branch <35>{}<32> and--project and --version were not given."
        log.err(msg, git.current_branch().name)
        sys.exit(1)
    if (version is not None):
        gae_app.version = version
    if (app_id is not None):
        gae_app.app_id = app_id
    gae_app.deploy(promote, quiet)

Deploy the app to AppEngine.

Args:
app_id (str):
AppEngine App ID. Overrides config value app_id if given.
version (str):
AppEngine project version. Overrides config values if given.
promote (bool):
If set to **True** promote the current remote app version to the one
that's being deployed.
quiet (bool):
If set to **True** this will pass the ``--quiet`` flag to gcloud
command.


def _broadcast_dynamic_shape_extended_helper(a: DynamicRaggedShape, b: DynamicRaggedShape) -> Tuple[DynamicRaggedShape, _Broadcaster, _Broadcaster]:
    assert a.rank <= b.rank
    assert 2 <= b.rank
    assert 1 <= a.rank
    a_rps = a._as_row_partitions()
    b_rps = b._as_row_partitions()
    if len(a_rps) < len(b_rps):
        a_nrows = a[0]
        a_nrows_static = tensor_util.constant_value(a_nrows)
        if a_nrows_static is not None:
            a_nrows = a_nrows_static
        neg_one_a_rp = RowPartition.from_uniform_row_length(uniform_row_length=a_nrows, nrows=1, nvals=a_nrows)
        neg_one_b_rp = b_rps[-(len(a_rps) + 1)]
        neg_one_ac, neg_one_bc = _broadcast_dynamic_shape_first_layer(constant_op.constant(1, dtype=b_rps[0].dtype), neg_one_b_rp.nrows())
        c_zero, ac_zero, bc_zero = _broadcast_dynamic_shape_next_layer(neg_one_ac, neg_one_bc, neg_one_a_rp, neg_one_b_rp)
        b_rps_tail = b_rps[-len(a_rps):] if len(a_rps) >= 1 else []
        c_suffix, ac_layers, bc_layers = _broadcast_dynamic_shape_from_rps(ac_zero, bc_zero, a_rps, b_rps_tail)
        return _broadcast_dynamic_shape_extended_complete(a=a, b=b, b_rps=b_rps, c_suffix=[c_zero] + c_suffix, ac=[ac_zero] + ac_layers, bc_suffix=[neg_one_bc, bc_zero] + bc_layers)
    else:
        assert len(a_rps) == len(b_rps)
        ac_zero, bc_zero = _broadcast_dynamic_shape_first_layer(a_rps[0].nrows(), b_rps[0].nrows())
        c_rps, a_layers, b_layers = _broadcast_dynamic_shape_from_rps(ac_zero, bc_zero, a_rps, b_rps)
        return _broadcast_dynamic_shape_extended_complete(a=a, b=b, b_rps=b_rps, c_suffix=c_rps, ac=[ac_zero] + a_layers, bc_suffix=[bc_zero] + b_layers)

Helper for broadcast_dynamic_shape_extended.

Here, we force:
a.rank <= b.rank
2 <= b.rank
1 <= a.rank
Args:
a: a DynamicRaggedShape
b: a DynamicRaggedShape

Returns:
A triple of a shape and two broadcasters.


def read_chunks(self, chunk_size, start, step, count) -> bytes:

        return self.mglo.read_chunks(chunk_size, start, step, count)

Read the content.

Read and concatenate the chunks of size chunk_size
using offsets calculated from start, step and stop.

Args:
chunk_size (int): The chunk size.
start (int): First offset.
step (int): Offset increment.
count (int): The number of offsets.

Returns:
bytes


def __init__(self, namespace: Optional[str], name: Optional[str], urn: Optional[str]=None, labels: Optional[Dict[str, str]]=None) -> None:
    if not urn:
        if not namespace:
            raise ValueError('Metric namespace must be non-empty')
        if not name:
            raise ValueError('Metric name must be non-empty')
    self.namespace = namespace
    self.name = name
    self.urn = urn
    self.labels = labels if labels else {}

Initializes ``MetricName``.

Note: namespace and name should be set for user metrics,
urn and labels should be set for an arbitrary metric to package into a
MonitoringInfo.

Args:
namespace: A string with the namespace of a metric.
name: A string with the name of a metric.
urn: URN to populate on a MonitoringInfo, when sending to RunnerHarness.
labels: Labels to populate on a MonitoringInfo


def update_current_state(self, value: str,
                             force: bool = False) -> datetime:

        value = value.lower()
        if not force:
            current_state = self.current_state

            if current_state == 'unknown':
                allowed_transitions = self._allowed_states
            else:
                allowed_transitions = self._allowed_transitions[current_state]
                allowed_transitions.append(current_state)

            LOG.debug('Updating current state of %s to %s', self._id, value)

            if value not in allowed_transitions:
                raise ValueError("Invalid current state update: '{}'. '{}' "
                                 "can be transitioned to states: {}"
                                 .format(value, current_state,
                                         allowed_transitions))

        return self._update_state('current', value)

Update the current state.

Args:
value (str): New value for sdp state
force (bool): If true, ignore allowed transitions

Returns:
datetime, update timestamp

Raises:
ValueError: If the specified current state is not allowed.


def compute_mask_offsets(shard_id2num_examples):

  total_num_examples = sum(shard_id2num_examples)

  mask_offsets = []
  total_num_examples = 0
  for num_examples_in_shard in shard_id2num_examples:

    mask_offsets.append(total_num_examples % 100)
    total_num_examples += num_examples_in_shard

  return mask_offsets

Return the list of offsets associated with each shards.

Args:
shard_id2num_examples: `list[int]`, mapping shard_id=>num_examples

Returns:
mask_offsets: `list[int]`, offset to skip for each of the shard


def files_info(self, *, id: str, **kwargs) -> SlackResponse:
    kwargs.update({'id': id})
    return self.api_call('files.info', http_verb='GET', params=kwargs)

Gets information about a team file.

Args:
id (str): The file id. e.g. 'F1234467890'


def get(self, name):
    return self.prepare_model(self.client.api.inspect_plugin(name))

Gets a plugin.

Args:
name (str): The name of the plugin.

Returns:
(:py:class:`Plugin`): The plugin.

Raises:
:py:class:`docker.errors.NotFound` If the plugin does not
exist.
:py:class:`docker.errors.APIError`
If the server returns an error.


def load(self, label_lookup_path, uid_lookup_path):

    if not tf.gfile.Exists(uid_lookup_path):
      tf.logging.fatal('File does not exist %s', uid_lookup_path)
    if not tf.gfile.Exists(label_lookup_path):
      tf.logging.fatal('File does not exist %s', label_lookup_path)

    proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines()
    uid_to_human = {}
    p = re.compile(r'[n\d]*[ \S,]*')
    for line in proto_as_ascii_lines:
      parsed_items = p.findall(line)
      uid = parsed_items[0]
      human_string = parsed_items[2]
      uid_to_human[uid] = human_string

    node_id_to_uid = {}
    proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()
    for line in proto_as_ascii:
      if line.startswith('  target_class:'):
        target_class = int(line.split(': ')[1])
      if line.startswith('  target_class_string:'):
        target_class_string = line.split(': ')[1]
        node_id_to_uid[target_class] = target_class_string[1:-2]

    node_id_to_name = {}
    for key, val in node_id_to_uid.items():
      if val not in uid_to_human:
        tf.logging.fatal('Failed to locate: %s', val)
      name = uid_to_human[val]
      node_id_to_name[key] = name

    return node_id_to_name

Loads a human readable English name for each softmax node.

Args:
label_lookup_path: string UID to integer node ID.
uid_lookup_path: string UID to human-readable string.

Returns:
dict from integer node ID to human-readable string.


def _ProcessUnknownEnums(message, encoded_message):
    if (not encoded_message):
        return message
    decoded_message = json.loads(six.ensure_str(encoded_message))
    for field in message.all_fields():
        if (isinstance(field, messages.EnumField) and (field.name in decoded_message) and (message.get_assigned_value(field.name) is None)):
            message.set_unrecognized_field(field.name, decoded_message[field.name], messages.Variant.ENUM)
    return message

Add unknown enum values from encoded_message as unknown fields.

ProtoRPC diverges from the usual protocol buffer behavior here and
doesn't allow unknown fields. Throwing on unknown fields makes it
impossible to let servers add new enum values and stay compatible
with older clients, which isn't reasonable for us. We simply store
unrecognized enum values as unknown fields, and all is well.

Args:
message: Proto message we've decoded thus far.
encoded_message: JSON string we're decoding.

Returns:
message, with any unknown enums stored as unrecognized fields.


def get_modules():
    ret = list()
    valid_extensions = ('.psd1', '.psm1', '.cdxml', '.xaml', '.dll')
    root_paths = []
    home_dir = os.environ.get('HOME', os.environ.get('HOMEPATH'))
    system_dir = '{0}\\System32'.format(os.environ.get('WINDIR', 'C:\\Windows'))
    program_files = os.environ.get('ProgramFiles', 'C:\\Program Files')
    default_paths = ['{0}/.local/share/powershell/Modules'.format(home_dir), '/usr/local/share/powershell/Modules', '{0}\\WindowsPowerShell\\v1.0\\Modules\\'.format(system_dir), '{0}\\WindowsPowerShell\\Modules'.format(program_files)]
    default_paths = ';'.join(default_paths)
    ps_module_path = os.environ.get('PSModulePath', default_paths)
    ps_module_path = ps_module_path.split(';')
    for item in ps_module_path:
        if os.path.exists(item):
            root_paths.append(item)
    if (not root_paths):
        log.error('Default paths not found')
        return ret
    for root_path in root_paths:
        if (not os.path.isdir(root_path)):
            continue
        for (root_dir, sub_dirs, file_names) in salt.utils.path.os_walk(root_path):
            for file_name in file_names:
                (base_name, file_extension) = os.path.splitext(file_name)
                if (file_extension.lower() in valid_extensions):
                    dir_name = os.path.basename(os.path.normpath(root_dir))
                    if ((dir_name not in ret) and (base_name.lower() == dir_name.lower())):
                        del sub_dirs[:]
                        ret.append(dir_name)
    return ret

Get a list of the PowerShell modules which are potentially available to be
imported. The intent is to mimic the functionality of ``Get-Module
-ListAvailable | Select-Object -Expand Name``, without the delay of loading
PowerShell to do so.

Returns:
list: A list of modules available to Powershell

Example:

.. code-block:: python

import salt.utils.powershell
modules = salt.utils.powershell.get_modules()


def _ReadRecordHeader(self, file_object, record_header_offset):
    data_type_map = self._GetDataTypeMap('keychain_record_header')
    (record_header, _) = self._ReadStructureFromFileObject(file_object, record_header_offset, data_type_map)
    return record_header

Reads the record header.

Args:
file_object (file): file-like object.
record_header_offset (int): offset of the record header relative to
the start of the file.

Returns:
keychain_record_header: record header.

Raises:
ParseError: if the record header cannot be read.


def _GetModuleCodeObjects(module):
    visit_recorder = _VisitRecorder()
    current = [module]
    code_objects = set()
    while current:
        current = _FindCodeObjectsReferents(module, current, visit_recorder)
        code_objects |= current
        current = [code_object.co_consts for code_object in current]
    return code_objects

Gets all code objects defined in the specified module.

There are two BFS traversals involved. One in this function and the other in
_FindCodeObjectsReferents. Only the BFS in _FindCodeObjectsReferents has
a depth limit. This function does not. The motivation is that this function
explores code object of the module and they can have any arbitrary nesting
level. _FindCodeObjectsReferents, on the other hand, traverses through class
definitions and random references. It's much more expensive and will likely
go into unrelated objects.

There is also a limit on how many total objects are going to be traversed in
all. This limit makes sure that if something goes wrong, the lookup doesn't
hang.

Args:
module: module to explore.

Returns:
Set of code objects defined in module.


def restore_collection(backup):
    for (k, v) in six.iteritems(backup):
        del tf.get_collection_ref(k)[:]
        tf.get_collection_ref(k).extend(v)

Restore from a collection backup.

Args:
backup (dict):


def gather(params, indices, validate_indices=None, name=None, axis=None, batch_dims=0):
    if name is None:
        name = 'gather'
    with ops.name_scope(name):
        if axis is None:
            axis = batch_dims
        axis = array_ops.get_positive_axis(axis, params.shape.rank, ndims_name='params.shape.rank')
        indices = ragged_tensor.convert_to_tensor_or_ragged_tensor(indices, name='indices')

        def leaf_op(p):
            return array_ops.gather(p, indices, validate_indices=validate_indices, axis=axis, batch_dims=batch_dims, name=None)
        return _extend_op_single(params, leaf_op)

tf.gather for structured tensors.

Does not support (yet) checks on illegal axis values, et cetera.

Indices must be a ragged or dense tensor.
Args:
params: a structured tensor to be gathered
indices: a ragged tensor or tensor to gather by.
validate_indices: whether to validate the indices
name: the name of the op(s).
axis: the axis in params to gather on.
batch_dims: the number of batch dimensions.

Returns:
the params reorganized according to indices.


def save_screenshot(driver, name):
    if hasattr(driver, 'save_screenshot'):
        screenshot_dir = os.environ.get('SCREENSHOT_DIR')
        if (not screenshot_dir):
            LOGGER.warning('The SCREENSHOT_DIR environment variable was not set; not saving a screenshot')
            return
        elif (not os.path.exists(screenshot_dir)):
            os.makedirs(screenshot_dir)
        image_name = os.path.join(screenshot_dir, (name + '.png'))
        driver.save_screenshot(image_name)
    else:
        msg = u"Browser does not support screenshots. Could not save screenshot '{name}'".format(name=name)
        LOGGER.warning(msg)

Save a screenshot of the browser.

The location of the screenshot can be configured
by the environment variable `SCREENSHOT_DIR`.  If not set,
this defaults to the current working directory.

Args:
driver (selenium.webdriver): The Selenium-controlled browser.
name (str): A name for the screenshot, which will be used in the output file name.

Returns:
None


def load(self, *modules):
    for module in modules:
        if isinstance(module, six.string_types):
            try:
                module = get_object(module)
            except Exception as e:
                self.errors[module] = e
                continue
        self.modules[module.__package__] = module
        for (loader, module_name, is_pkg) in pkgutil.walk_packages(module.__path__):
            full_name = '{}.{}'.format(_package(module), module_name)
            try:
                self.modules[full_name] = get_object(full_name)
                if is_pkg:
                    self.load(self.modules[full_name])
            except Exception as e:
                self.errors[full_name] = e

Load one or more modules.

Args:
modules: Either a string full path to a module or an actual module
object.


def __init__(self, context_name = 'default'):

		if context_name in self.contexts:
			raise Error("A context named '%s' already exists" % (context_name,))

		self.name = context_name
		self.handlers = {}

		self.contexts[self.name] = self

Create a new Bubbler context

Params:
context_name (string):
Name of this context

Raises:
bubbler.Error:
If this context name already exists


def changes(self, **kwargs):
    path = ('%s/%s/changes' % (self.manager.path, self.get_id()))
    return self.manager.gitlab.http_get(path, **kwargs)

List the merge request changes.

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

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabListError: If the list could not be retrieved

Returns:
RESTObjectList: List of changes


def parse(self, body):
    if isinstance(body, six.string_types):
        body = json.loads(body)
    version = body['version']
    self.version = version
    session = body['session']
    self.session.new = session['new']
    self.session.session_id = session['sessionId']
    application_id = session['application']['applicationId']
    self.session.application.application_id = application_id
    if (('attributes' in session) and session['attributes']):
        self.session.attributes = session.get('attributes', {})
    else:
        self.session.attributes = {}
    self.session.user.user_id = session['user']['userId']
    self.session.user.access_token = session['user'].get('accessToken', 0)
    request = body['request']
    if (request['type'] == 'LaunchRequest'):
        self.request = LaunchRequest()
    elif (request['type'] == 'IntentRequest'):
        self.request = IntentRequest()
        self.request.intent = Intent()
        intent = request['intent']
        self.request.intent.name = intent['name']
        if (('slots' in intent) and intent['slots']):
            for (name, slot) in six.iteritems(intent['slots']):
                self.request.intent.slots[name] = Slot()
                self.request.intent.slots[name].name = slot['name']
                self.request.intent.slots[name].value = slot.get('value')
    elif (request['type'] == 'SessionEndedRequest'):
        self.request = SessionEndedRequest()
        self.request.reason = request['reason']
    self.request.type = request['type']
    self.request.request_id = request['requestId']
    self.request.timestamp = request['timestamp']
    return self

Parse JSON request, storing content in object attributes.

Args:
body: str. HTTP request body.

Returns:
self


def get_student_certificate(self, username, course_id):

        resp = self.requester.get(
            urljoin(
                self.base_url,
                '/api/certificates/v0/certificates/{username}/courses/{course_key}/'.format(
                    username=username,
                    course_key=course_id
                )
            )
        )

        resp.raise_for_status()

        return Certificate(resp.json())

Returns an Certificate object with the user certificates

Args:
username (str): an edx user's username
course_id (str): an edX course id.

Returns:
Certificate: object representing the student certificate for a course


def read(self, n):

    if self._EOF:
      return ""

    while self._seg_index <= self._last_seg_index:
      result = self._read_from_seg(n)
      if result != "":
        return result
      else:
        self._next_seg()

    self._EOF = True
    return ""

Read data from file segs.

Args:
n: max bytes to read. Must be positive.

Returns:
some bytes. May be smaller than n bytes. "" when no more data is left.


def console_get_width(con: tcod.console.Console) -> int:

    return int(lib.TCOD_console_get_width(_console(con)))

Return the width of a console.

Args:
con (Console): Any Console instance.

Returns:
int: The width of a Console.

.. deprecated:: 2.0
Use `Console.width` instead.


def lookup_prefix(self, prefix, timestamp=timestamp_now):
    prefix = prefix.strip().upper()
    if ((self._lookuptype == 'clublogxml') or (self._lookuptype == 'countryfile')):
        return self._check_data_for_date(prefix, timestamp, self._prefixes, self._prefixes_index)
    elif (self._lookuptype == 'redis'):
        (data_dict, index) = self._get_dicts_from_redis('_prefix_', '_prefix_index_', self._redis_prefix, prefix)
        return self._check_data_for_date(prefix, timestamp, data_dict, index)
    raise KeyError

Returns lookup data of a Prefix

Args:
prefix (string): Prefix of a Amateur Radio callsign
timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC)

Returns:
dict: Dictionary containing the country specific data of the Prefix

Raises:
KeyError: No matching Prefix found
APIKeyMissingError: API Key for Clublog missing or incorrect

Example:
The following code shows how to obtain the information for the prefix "DH" from the countryfile.com
database (default database).

>>> from pyhamtools import LookupLib
>>> myLookupLib = LookupLib()
>>> print myLookupLib.lookup_prefix("DH")
{
'adif': 230,
'country': u'Fed. Rep. of Germany',
'longitude': 10.0,
'cqz': 14,
'ituz': 28,
'latitude': 51.0,
'continent': u'EU'
}

Note:
This method is available for

- clublogxml
- countryfile
- redis


def _freeze_concrete_function(self):
    if len(self._funcs) == 0:
        raise ValueError('No ConcreteFunction is specified.')
    if len(self._funcs) > 1:
        raise ValueError('This converter can only convert a single ConcreteFunction. Converting multiple functions is under development.')
    frozen_func, graph_def = _convert_to_constants.convert_variables_to_constants_v2_as_graph(self._funcs[0], lower_control_flow=False)
    input_tensors = [tensor for tensor in frozen_func.inputs if tensor.dtype != _dtypes.resource]
    output_tensors = frozen_func.outputs
    return (graph_def, input_tensors, output_tensors, frozen_func)

Convert the given ConcreteFunction to frozen graph.

Returns:
graph_def: The frozen GraphDef.
input_tensors: List of input tensors.
output_tensors: List of output tensors.
frozen_func: The frozen ConcreteFunction.

Raises:
ValueError: none or multiple ConcreteFunctions provided.


def get_changelog(repo_path, from_commit=None):

    repo = dulwich.repo.Repo(repo_path)
    tags = get_tags(repo)
    refs = get_refs(repo)
    changelog = []
    maj_version = 0
    feat_version = 0
    fix_version = 0
    start_including = False

    cur_line = ''
    if from_commit is None:
        start_including = True

    for commit_sha, children in reversed(
        get_children_per_first_parent(repo_path).items()
    ):
        commit = repo.get_object(commit_sha)
        maj_version, feat_version, fix_version = get_version(
            commit=commit,
            tags=tags,
            maj_version=maj_version,
            feat_version=feat_version,
            fix_version=fix_version,
        )
        version = '%s.%s.%s' % (maj_version, feat_version, fix_version)

        if (
            start_including or commit_sha.startswith(from_commit)
            or fuzzy_matches_refs(from_commit, refs.get(commit_sha, []))
        ):
            cur_line = pretty_commit(
                commit,
                version,
            )
            for child in children:
                cur_line += pretty_commit(repo.get_object(child), version=None)
            start_including = True
            changelog.append(cur_line)

    return '\n'.join(reversed(changelog))

Given a repo path and an option commit/tag/refspec to start from, will
get the rpm compatible changelog

Args:
repo_path (str): path to the git repo
from_commit (str): refspec (partial commit hash, tag, branch, full
refspec, partial refspec) to start the changelog from

Returns:
str: Rpm compatible changelog


def get_by_ip_hostname(self, ip_hostname):
    resources = self._client.get_all()
    resources_filtered = [x for x in resources if (x['credentials']['ip_hostname'] == ip_hostname)]
    if resources_filtered:
        return resources_filtered[0]
    else:
        return None

Retrieve a storage system by its IP.

Works only with API version <= 300.

Args:
ip_hostname: Storage system IP or hostname.

Returns:
dict


def map_defun(fn, elems, output_dtypes, output_shapes, max_intra_op_parallelism=1):
    if not isinstance(elems, list):
        raise ValueError(f'`elems` must be a list of tensors, but was {elems}.')
    if not isinstance(output_dtypes, list):
        raise ValueError(f'`output_dtypes` must be a list of `tf.DType` objects, but was {output_dtypes}.')
    if not isinstance(output_shapes, list):
        raise ValueError(f'`output_shapes` must be a list of `tf.TensorShape` objects, but was {output_shapes}.')
    concrete_fn = fn.get_concrete_function()
    elems = [ops.convert_to_tensor(e) for e in elems]
    output_shapes = [tensor_shape.TensorShape(s) for s in output_shapes]
    return gen_dataset_ops.map_defun(elems, concrete_fn.captured_inputs, output_dtypes, output_shapes, concrete_fn, max_intra_op_parallelism)

Map a function on the list of tensors unpacked from `elems` on dimension 0.

Args:
fn: A function (`function.defun`) that takes a list of tensors and returns
another list of tensors. The output list has the same types as
output_dtypes. The elements of the output list have the same dimension 0
as `elems`, and the remaining dimensions correspond to those of
`fn_output_shapes`.
elems: A list of tensors.
output_dtypes: A list of dtypes corresponding to the output types of the
function.
output_shapes: A list of `TensorShape`s corresponding to the output shapes
from each invocation of the function on slices of inputs.
max_intra_op_parallelism: An integer. If positive, sets the max parallelism
limit of each function call to this.

Raises:
ValueError: if any of the inputs are malformed.

Returns:
A list of `Tensor` objects with the same types as `output_dtypes`.


def element_wise_op(array, other, op, ty):

    weld_obj = WeldObject(encoder_, decoder_)

    array_var = weld_obj.update(array)
    if isinstance(array, WeldObject):
        array_var = array.obj_id
        weld_obj.dependencies[array_var] = array

    other_var = weld_obj.update(other)
    if isinstance(other, WeldObject):
        other_var = other.obj_id
        weld_obj.dependencies[other_var] = other

    weld_template =

    weld_obj.weld_code = weld_template % {"array": array_var,
                                          "other": other_var,
                                          "ty": ty, "op": op}
    return weld_obj

Operation of series and other, element-wise (binary operator add)

Args:
array (WeldObject / Numpy.ndarray): Input array
other (WeldObject / Numpy.ndarray): Second Input array
op (str): Op string used to compute element-wise operation (+ / *)
ty (WeldType): Type of each element in the input array

Returns:
A WeldObject representing this computation


def get_variation_from_id(self, experiment_key, variation_id):

    variation_map = self.variation_id_map.get(experiment_key)

    if variation_map:
      variation = variation_map.get(variation_id)
      if variation:
        return variation
      else:
        self.logger.error('Variation ID "%s" is not in datafile.' % variation_id)
        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 ID.

Args:
experiment: Key representing parent experiment of variation.
variation_id: ID representing the variation.

Returns
Object representing the variation.


def __init__(self, max_str_len: int=100):
    self.training_bar = None
    self.prediction_bar = None
    self.max_str_len = max_str_len

Initialize the callback with optional max_str_len parameter to control string truncation length.

Args:
max_str_len (`int`):
Maximum length of strings to display in logs.
Longer strings will be truncated with a message.


def pop_chunk(self, chunk_max_size):

        if self._total_length < chunk_max_size:

            res = self._tobytes()
            self.clear()
            return res
        first_iteration = True
        while True:
            try:
                data = self._deque.popleft()
                data_length = len(data)
                self._total_length -= data_length
                if first_iteration:

                    if data_length == chunk_max_size:

                        return data
                    elif data_length > chunk_max_size:

                        view = self._get_pointer_or_memoryview(data,
                                                               data_length)
                        self.appendleft(view[chunk_max_size:])
                        return view[:chunk_max_size]
                    else:

                        chunk_write_buffer = WriteBuffer()
                else:

                    if chunk_write_buffer._total_length + data_length \
                       > chunk_max_size:
                        view = self._get_pointer_or_memoryview(data,
                                                               data_length)
                        limit = chunk_max_size - \
                            chunk_write_buffer._total_length - data_length
                        self.appendleft(view[limit:])
                        data = view[:limit]
                chunk_write_buffer.append(data)
                if chunk_write_buffer._total_length >= chunk_max_size:
                    break
            except IndexError:

                self._has_view = False
                break
            first_iteration = False
        return chunk_write_buffer._tobytes()

Pops a chunk of the given max size.

Optimized to avoid too much string copies.

Args:
chunk_max_size (int): max size of the returned chunk.

Returns:
string (bytes) with a size <= chunk_max_size.


def listNodes(self, vendorSpecific=None):

        response = self.listNodesResponse(vendorSpecific)
        return self._read_dataone_type_response(response, 'NodeList')

See Also: listNodesResponse()

Args:
vendorSpecific:

Returns:


def any_to_datetime(self, time_input, tz=None):
    dt_value = self.unix_time_to_datetime(time_input, tz)
    if (dt_value is None):
        dt_value = self.date_to_datetime(time_input, tz)
    if (dt_value is None):
        dt_value = self.human_date_to_datetime(time_input, tz)
    if (dt_value is None):
        raise RuntimeError('Could not format input ({}) to datetime string.'.format(time_input))
    return dt_value

Return datetime object from multiple formats.

Formats:

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

Args:
time_input (string): The time input string (see formats above).
tz (string): The time zone for the returned data.

Returns:
(datetime.datetime): Python datetime.datetime object.


def register_for_auto_class(cls, auto_class='TFAutoModel'):
    if not isinstance(auto_class, str):
        auto_class = auto_class.__name__
    import transformers.models.auto as auto_module
    if not hasattr(auto_module, auto_class):
        raise ValueError(f'{auto_class} is not a valid auto class.')
    cls._auto_class = auto_class

Register this class with a given auto class. This should only be used for custom models as the ones in the
library are already mapped with an auto class.

Args:
auto_class (`str` or `type`, *optional*, defaults to `"TFAutoModel"`):
The auto class to register this new model with.


def remove_redistribution(self, protocol):
    protocols = ['bgp', 'rip', 'static', 'connected']
    if (protocol not in protocols):
        raise ValueError('redistributed protocol must bebgp, connected, rip or static')
    cmd = 'no redistribute {}'.format(protocol)
    return self.configure_ospf(cmd)

Removes a protocol redistribution to OSPF

Args:
protocol (str):  protocol to redistribute
route_map_name (str): route-map to be used to
filter the protocols
Returns:
bool: True if the command completes successfully
Exception:
ValueError:  This will be raised if the protocol pass is not one
of the following: [rip, bgp, static, connected]


def _get_degree(num_nodes):

        d_float = 0.5 * (np.sqrt(8.0 * num_nodes + 1.0) - 3.0)
        d_int = int(np.round(d_float))
        if (d_int + 1) * (d_int + 2) == 2 * num_nodes:
            return d_int

        else:
            raise ValueError(num_nodes, "not a triangular number")

Get the degree of the current surface.

Args:
num_nodes (int): The number of control points for a
B |eacute| zier surface.

Returns:
int: The degree :math:`d` such that :math:`(d + 1)(d + 2)/2`
equals ``num_nodes``.

Raises:
ValueError: If ``num_nodes`` isn't a triangular number.


class MimiDecoderOutput(ModelOutput):
    audio_values: Optional[torch.FloatTensor] = None
    decoder_past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None

Args:
audio_values (`torch.FloatTensor`  of shape `(batch_size, segment_length)`, *optional*):
Decoded audio values, obtained using the decoder part of Mimi.
decoder_past_key_values (`Cache`, *optional*):
Pre-computed hidden-states (key and values in the self-attention blocks) that can be used to speed up sequential decoding of the decoder transformer.
This typically consists in the `past_key_values` returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.

The model will output the same cache format that is fed as input.

If `past_key_values` are used, the user can optionally input only the last `audio_values` or `audio_codes (those that don't
have their past key value states given to this model).


def kill_mprocess(process):

    if process and proc_alive(process):
        process.terminate()
        process.communicate()
    return not proc_alive(process)

kill process
Args:
process - Popen object for process


def getargspec(obj):
    if isinstance(obj, functools.partial):
        return _get_argspec_for_partial(obj)
    decorators, target = tf_decorator.unwrap(obj)
    spec = next((d.decorator_argspec for d in decorators if d.decorator_argspec is not None), None)
    if spec:
        return spec
    try:
        return _getargspec(target)
    except TypeError:
        pass
    if isinstance(target, type):
        try:
            return _getargspec(target.__init__)
        except TypeError:
            pass
        try:
            return _getargspec(target.__new__)
        except TypeError:
            pass
    return _getargspec(type(target).__call__)

TFDecorator-aware replacement for `inspect.getargspec`.

Note: `getfullargspec` is recommended as the python 2/3 compatible
replacement for this function.

Args:
obj: A function, partial function, or callable object, possibly decorated.

Returns:
The `ArgSpec` that describes the signature of the outermost decorator that
changes the callable's signature, or the `ArgSpec` that describes
the object if not decorated.

Raises:
ValueError: When callable's signature can not be expressed with
ArgSpec.
TypeError: For objects of unsupported types.


def wait_until_final(self, poll_interval=1, timeout=60):

        start_time = time.time()
        elapsed = 0
        while (self.status != "complete" and
                (timeout <= 0 or elapsed < timeout)):
            time.sleep(poll_interval)
            self.refresh()
            elapsed = time.time() - start_time

It will poll the URL to grab the latest status resource in a given
timeout and time interval.

Args:
poll_interval (int): how often to poll the status service.
timeout (int): how long to poll the URL until giving up. Use <= 0
to wait forever


def save_graph_def(file_name, frozen_graph_def):
    tf.io.write_graph(frozen_graph_def, os.path.dirname(file_name), os.path.basename(file_name), as_text=False)
    tf.compat.v1.logging.info('Saved frozen graph to %s', file_name)

Writes a graph def file out to disk.

Args:
file_name: Where to save the file.
frozen_graph_def: GraphDef proto object to save.


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

Corresponds to IDD Field `ws_db004`
Mean wind speed coincident with 0.4% dry-bulb temperature

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

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


def pretty_dump(fn):

    @wraps(fn)
    def pretty_dump_wrapper(*args, **kwargs):
        response.content_type = 'application/json; charset=utf-8'
        return json.dumps(fn(*args, **kwargs), indent=4, separators=(',', ': '))
    return pretty_dump_wrapper

Decorator used to output prettified JSON.

``response.content_type`` is set to ``application/json; charset=utf-8``.

Args:
fn (fn pointer): Function returning any basic python data structure.

Returns:
str: Data converted to prettified JSON.


def __init__(self, key_type=None, value_type=None, min_length=None, max_length=None, empty=True):

        super(DictTypeChecker, self).__init__(base_type=dict)
        self.key_type = key_type
        self.value_type = value_type
        self.min_length = min_length
        self.max_length = max_length
        self.empty = empty

Initialization method.

Args:
key_type (type): the type of the dict keys.
value_type (type): the type of the dict values.
min_length (int): minimum length of the dict (included).
max_length (int): maximum length of the dict (included).
empty (bool): whether empty dict is allowed.


def _get_argspec_for_partial(obj):
    n_prune_args = len(obj.args)
    partial_keywords = obj.keywords or {}
    args, varargs, keywords, defaults = getargspec(obj.func)
    args = args[n_prune_args:]
    no_default = object()
    all_defaults = [no_default] * len(args)
    if defaults:
        all_defaults[-len(defaults):] = defaults
    for kw, default in iter(partial_keywords.items()):
        if kw in args:
            idx = args.index(kw)
            all_defaults[idx] = default
        elif not keywords:
            raise ValueError(f'{obj} does not have a **kwargs parameter, but contains an unknown partial keyword {kw}.')
    first_default = next((idx for idx, x in enumerate(all_defaults) if x is not no_default), None)
    if first_default is None:
        return ArgSpec(args, varargs, keywords, None)
    invalid_default_values = [args[i] for i, j in enumerate(all_defaults) if j is no_default and i > first_default]
    if invalid_default_values:
        raise ValueError(f'{obj} has some keyword-only arguments, which are not supported: {invalid_default_values}.')
    return ArgSpec(args, varargs, keywords, tuple(all_defaults[first_default:]))

Implements `getargspec` for `functools.partial` objects.

Args:
obj: The `functools.partial` object
Returns:
An `inspect.ArgSpec`
Raises:
ValueError: When callable's signature can not be expressed with
ArgSpec.


def set_custom_getter_compose(custom_getter):
    tf.get_variable_scope().set_custom_getter(_compose_custom_getters(tf.get_variable_scope().custom_getter, custom_getter))

Set a custom getter in the current variable scope.

Do not overwrite the existing custom getter - rather compose with it.

Args:
custom_getter: a custom getter.


def __init__(self, resolver_context, encoding='utf-8'):

    super(ZipFileSystem, self).__init__(resolver_context)
    self._file_object = None
    self._zip_file = None
    self.encoding = encoding

Initializes a file system.

Args:
resolver_context (Context): a resolver context.
encoding (Optional[str]): encoding of the file entry name.


def _publish_to_subscribers(event: Event):
    subscribers = get_subscribers(event.object_type)
    for sub in subscribers:
        DB.prepend_to_list(_keys.published(event.object_type, sub), event.id, pipeline=True)
        event_dict = deepcopy(event.config)
        event_dict.pop('id')
        DB.set_hash_value(_keys.data(event.object_type, sub), event.id, str(event_dict), pipeline=True)
    DB.publish(event.object_type, event.id, pipeline=True)

Publish and event to all subscribers.

- Adds the event id to the published event list for all subscribers.
- Adds the event data to the published event data for all subscribers.
- Publishes the event id notification to all subscribers.

Args:
event (Event): Event object to publish.


def resolve_import(name, is_from, is_star):
    if (name.startswith('.') or is_builtin(name)):
        return None
    ret = _resolve_import(name)
    if ((ret is None) and is_from and (not is_star)):
        (package, _) = name.rsplit('.', 1)
        ret = _resolve_import(package)
    return ret

Use python to resolve an import.

Args:
name: The fully qualified module name.

Returns:
The path to the module source file or None.


def send_message(msg: 'EFBMsg') -> Optional['EFBMsg']:

    global middlewares, master, slaves

    if msg is None:
        return

    for i in middlewares:
        m = i.process_message(msg)
        if m is None:
            return None

        assert m is not None
        msg = m

    msg.verify()

    if msg.deliver_to.channel_id == master.channel_id:
        return master.send_message(msg)
    elif msg.deliver_to.channel_id in slaves:
        return slaves[msg.deliver_to.channel_id].send_message(msg)
    else:
        raise EFBChannelNotFound(msg)

Deliver a message to the destination channel.

Args:
msg (EFBMsg): The message

Returns:
The message sent by the destination channel,
includes the updated message ID from there.
Returns ``None`` if the message is not sent.


def enable_sns_notification(self, region, trailName):
    ct = self.session.client('cloudtrail', region_name=region)
    ct.update_trail(Name=trailName, SnsTopicName=self.topic_name)
    auditlog(event='cloudtrail.enable_sns_notification', actor=self.ns, data={'account': self.account.account_name, 'region': region})
    self.log.info('Enabled SNS notifications for trail {} in {}/{}'.format(trailName, self.account.account_name, region))

Enable SNS notifications for a Trail

Args:
region (`str`): Name of the AWS region
trailName (`str`): Name of the CloudTrail Trail

Returns:
`None`


def grepPDF(self, path):
    with open(path, 'rb') as pdf_file_obj:
        match = set()
        text = ''
        pdf_reader = PyPDF2.PdfFileReader(pdf_file_obj)
        pages = pdf_reader.numPages
        for page in range(pages):
            page_obj = pdf_reader.getPage(page)
            text += ('\n' + page_obj.extractText())
        match.update(set((x.lower() for x in re.findall(self._keywords, text, re.IGNORECASE))))
    return match

Parse PDF files text content for keywords.

Args:
path: PDF file path.

Returns:
match: set of unique occurrences of every match.


def convert_builtin_to_typing(typ):
    if getattr(typ, '__origin__', None) in _BUILTINS_TO_TYPING:
        args = map(convert_builtin_to_typing, typ.__args__)
        typ = _BUILTINS_TO_TYPING[typ.__origin__].copy_with(tuple(args))
    return typ

Convert recursively a given builtin to a typing object.

Args:
typ (`builtins`): builtin object that exist in _BUILTINS_TO_TYPING.

Returns:
type: The given builtins converted to a type.


def __init__(self, validator_map):

        self.validators = dict(validator_map)
        v_sorted = sorted(self.validators.items(), key=lambda t: t[0])
        self.validator_descriptions = ['{}:<{}>'.format(k, v) for k, v in v_sorted]
        self.name = 'dict({})'.format(', '.join(self.validator_descriptions))
        self.description = '\nDict options: \n  '
        self.description += '\n  '.join(self.validator_descriptions)
        self.kv_regex = re.compile(r'[=:]+')

Create a dictonary type from a dictionary of other types
Args:
validator_map -- a mapping from names to types
Examples:
>>> Dict({'a': int, 'b': int})('a:1,b:2')
{'a': 1, 'b': 2}

>>> Dict({'a': str, 'b': int})('a:asdf b=1234')
{'a': 'asdf', 'b': 1234}

>>> Dict({'a': Int() | Keyword('', None), 'b': Int()})('a,b=1')
{'a': None, 'b': 1}


def __call__(self, class_logits, box_regression):

        class_logits = cat(class_logits, dim=0)
        box_regression = cat(box_regression, dim=0)
        device = class_logits.device

        if not hasattr(self, "_proposals"):
            raise RuntimeError("subsample needs to be called before")

        proposals = self._proposals

        labels = cat([proposal.get_field("labels") for proposal in proposals], dim=0)
        regression_targets = cat(
            [proposal.get_field("regression_targets") for proposal in proposals], dim=0
        )

        classification_loss = F.cross_entropy(class_logits, labels)

        sampled_pos_inds_subset = torch.nonzero(labels > 0).squeeze(1)
        labels_pos = labels[sampled_pos_inds_subset]
        if self.cls_agnostic_bbox_reg:
            map_inds = torch.tensor([4, 5, 6, 7], device=device)
        else:
            map_inds = 4 * labels_pos[:, None] + torch.tensor(
                [0, 1, 2, 3], device=device)

        box_loss = smooth_l1_loss(
            box_regression[sampled_pos_inds_subset[:, None], map_inds],
            regression_targets[sampled_pos_inds_subset],
            size_average=False,
            beta=1,
        )
        box_loss = box_loss / labels.numel()

        return classification_loss, box_loss

Computes the loss for Faster R-CNN.
This requires that the subsample method has been called beforehand.

Arguments:
class_logits (list[Tensor])
box_regression (list[Tensor])

Returns:
classification_loss (Tensor)
box_loss (Tensor)


async def request(context, url, timeout=60, method='get', good=(200,), retry=tuple(range(500, 512)), return_type='text', **kwargs):
    session = context.session
    loggable_url = get_loggable_url(url)
    async with async_timeout.timeout(timeout):
        log.debug('{} {}'.format(method.upper(), loggable_url))
        async with session.request(method, url, **kwargs) as resp:
            log.debug('Status {}'.format(resp.status))
            message = 'Bad status {}'.format(resp.status)
            if (resp.status in retry):
                raise ScriptWorkerRetryException(message)
            if (resp.status not in good):
                raise ScriptWorkerException(message)
            if (return_type == 'text'):
                return (await resp.text())
            elif (return_type == 'json'):
                return (await resp.json())
            else:
                return resp

Async aiohttp request wrapper.

Args:
context (scriptworker.context.Context): the scriptworker context.
url (str): the url to request
timeout (int, optional): timeout after this many seconds. Default is 60.
method (str, optional): The request method to use.  Default is 'get'.
good (list, optional): the set of good status codes.  Default is (200, )
retry (list, optional): the set of status codes that result in a retry.
Default is tuple(range(500, 512)).
return_type (str, optional): The type of value to return.  Takes
'json' or 'text'; other values will return the response object.
Default is text.
**kwargs: the kwargs to send to the aiohttp request function.

Returns:
object: the response text() if return_type is 'text'; the response
json() if return_type is 'json'; the aiohttp request response
object otherwise.

Raises:
ScriptWorkerRetryException: if the status code is in the retry list.
ScriptWorkerException: if the status code is not in the retry list or
good list.


def CanSplit(self, must_split):
    current = self.next_token
    previous = current.previous_token
    if current.is_pseudo:
        return False
    if not must_split and subtypes.DICTIONARY_KEY_PART in current.subtypes and (subtypes.DICTIONARY_KEY not in current.subtypes) and (not style.Get('ALLOW_MULTILINE_DICTIONARY_KEYS')):
        return False
    if not must_split and subtypes.DICTIONARY_VALUE in current.subtypes and (not style.Get('ALLOW_SPLIT_BEFORE_DICT_VALUE')):
        return False
    if previous and previous.value == '(' and (current.value == ')'):
        token = previous.previous_token
        while token:
            prev = token.previous_token
            if not prev or prev.name not in {'NAME', 'DOT'}:
                break
            token = token.previous_token
        if token and subtypes.DICTIONARY_VALUE in token.subtypes:
            if not style.Get('ALLOW_SPLIT_BEFORE_DICT_VALUE'):
                return False
    if previous and previous.value == '.' and (current.value == '.'):
        return False
    return current.can_break_before

Determine if we can split before the next token.

Arguments:
must_split: (bool) A newline was required before this token.

Returns:
True if the line can be split before the next token.


def assignment_propagation(node):

  n_reads = read_counts(node)

  to_remove = []
  for succ in gast.walk(node):

    if (isinstance(succ, gast.Assign) and isinstance(succ.value, gast.Name) and
        len(succ.targets) == 1 and isinstance(succ.targets[0], gast.Name)):
      rhs_name = succ.value.id

      rhs_defs = [def_[1] for def_ in anno.getanno(succ, 'definitions_in')
                  if def_[0] == rhs_name]

      if (len(rhs_defs) == 1 and isinstance(rhs_defs[0], gast.Assign) and
          n_reads[rhs_defs[0]] == 1 and
          isinstance(rhs_defs[0].value, gast.Name) and
          isinstance(rhs_defs[0].targets[0], gast.Name)):

        to_remove.append(rhs_defs[0])

        succ.value = rhs_defs[0].value

  transformers.Remove(to_remove).visit(node)
  anno.clearanno(node)
  return node

Perform assignment propagation.

Assignment propagation is not a compiler optimization as much as a
readability optimization. If a variable name is used only once, it gets
renamed when possible e.g. `y = x; z = y` will become `z = x`.

Args:
node: The AST to optimize.

Returns:
The optimized AST.


def foreach_worker(self, fn):
    results = ray.get([w.foreach_worker.remote(fn) for w in self.workers])
    return results

Apply the given function to each remote worker.

Returns:
List of results from applying the function.


def complies_with_scope(queue_item, new_request, scope):

        if not URLHelper.is_parsable(queue_item.request.url):
            return False

        if not URLHelper.is_parsable(new_request.url):
            return False

        if scope.request_methods:
            if not queue_item.request.method in scope.request_methods:
                return False

        if scope.protocol_must_match:
            if URLHelper.get_protocol(queue_item.request.url) != URLHelper.get_protocol(new_request.url):
                return False

        if scope.subdomain_must_match:
            current_subdomain = URLHelper.get_subdomain(queue_item.request.url)
            new_subdomain = URLHelper.get_subdomain(new_request.url)

            www_matches = False

            if current_subdomain == "www" and new_subdomain == "":
                www_matches = True

            if new_subdomain == "www" and current_subdomain == "":
                www_matches = True

            if not www_matches and current_subdomain != new_subdomain:
                return False

        if scope.hostname_must_match:
            if URLHelper.get_hostname(queue_item.request.url) != URLHelper.get_hostname(new_request.url):
                return False

        if scope.tld_must_match:
            if URLHelper.get_tld(queue_item.request.url) != URLHelper.get_tld(new_request.url):
                return False

        return True

Check if the new request complies with the crawling scope.

Args:
queue_item (:class:`nyawc.QueueItem`): The parent queue item of the new request.
new_request (:class:`nyawc.http.Request`): The request to check.
scope (:class:`nyawc.Options.OptionsScope`): The scope to check.

Returns:
bool: True if it complies, False otherwise.


def get_tokens(self, *, payer_id, credit_card_token_id, start_date, end_date):
    payload = {'language': self.client.language.value, 'command': PaymentCommand.GET_TOKENS.value, 'merchant': {'apiLogin': self.client.api_login, 'apiKey': self.client.api_key}, 'creditCardTokenInformation': {'payerId': payer_id, 'creditCardTokenId': credit_card_token_id, 'startDate': start_date.strftime('%Y-%m-%dT%H:%M:%S'), 'endDate': end_date.strftime('%Y-%m-%dT%H:%M:%S')}, 'test': self.client.is_test}
    return self.client._post(self.url, json=payload)

With this functionality you can query previously the Credit Cards Token.

Args:
payer_id:
credit_card_token_id:
start_date:
end_date:

Returns:


def initialize(self, input_shape, rng):
    try:
        if (not self._first_init):
            return ()
        self._first_init = False
        self._params = self.new_parameters(input_shape, rng)
        return self._params
    except Exception:
        (name, trace) = (self.__class__.__name__, _short_traceback())
        raise LayerError(name, 'initialize', self._caller, input_shape, trace)

Initialize the layer given an input shape and rng.

Returns new_parameters(input_shape, rng) on the first call and () on any
subsequent call, as the layer is already initialized. This is used for
networks that share parameters, so the layer only produces them once.

Note that all arguments and return values can be tuples or dictionaries
or arbitraty nested structures composed of tuples and dictionaries.

Args:
input_shape: a tuple representing the shape of the input.
rng: random number generator.

Returns:
Newly created parameters on the first call and () on all subsequent calls.


def open_file_with_default_program(file_path, background=False, return_cmd=False):
    desktop_env = system.get_name()
    if (desktop_env == 'windows'):
        open_file_cmd = ('explorer.exe ' + ("'%s'" % file_path))
    elif (desktop_env == 'mac'):
        open_file_cmd = ('open ' + ("'%s'" % file_path))
    else:
        file_mime_type = system.get_cmd_out(['xdg-mime', 'query', 'filetype', file_path])
        desktop_file = system.get_cmd_out(['xdg-mime', 'query', 'default', file_mime_type])
        open_file_cmd = desktopfile.execute(desktopfile.locate(desktop_file)[0], files=[file_path], return_cmd=True)
    if return_cmd:
        return open_file_cmd
    else:
        def_program_proc = sp.Popen(open_file_cmd, shell=True)
        if (not background):
            def_program_proc.wait()

Opens a file with the default program for that type.

Open the file with the user's preferred application.

Args:
file_path  (str) : Path to the file to be opened.
background (bool): Run the program in the background, instead of waiting for completion. Defaults to ``False``.
return_cmd (bool): Returns the command to run the program (str) instead of running it. Defaults to ``False``.

Returns:
str: Only if ``return_cmd``, the command to run the program is returned instead of running it. Else returns nothing.


def _parse_result_block_line(self, instrumentation_block, line):
    instrumentation_block.add_value(line)
    return instrumentation_block

Parses the instrumentation result block's line.

Args:
instrumentation_block: _InstrumentationBlock, the instrumentation
result block for the instrumentation run.
line: string, the raw instrumentation output to add to the
instrumenation result block's _InstrumentationResultBlocki
object.

Returns:
The instrumentation result block for the instrumentation run.


def find(self, binding_id, instance):

        binding = AtlasServiceBinding.Binding(binding_id, instance)
        self.backend.storage.populate(binding)
        return binding

find an instance

Create a new instance and populate it with data stored if it exists.

Args:
binding_id (string): UUID of the binding
instance (AtlasServiceInstance.Instance): instance

Returns:
AtlasServiceBinding: A binding


def valid_ip_prefix(ip_prefix):
    try:
        ip_prefix = ipaddress.ip_network(ip_prefix)
    except ValueError:
        return False
    else:
        if ((ip_prefix.version == 4) and (ip_prefix.max_prefixlen != 32)):
            return False
        if ((ip_prefix.version == 6) and (ip_prefix.max_prefixlen != 128)):
            return False
        return True

Perform a sanity check on ip_prefix.

Arguments:
ip_prefix (str): The IP-Prefix to validate

Returns:
True if ip_prefix is a valid IPv4 address with prefix length 32 or a
valid IPv6 address with prefix length 128, otherwise False


def download_files_maybe_extract(urls, directory, check_files=[]):

    check_files = [os.path.join(directory, f) for f in check_files]
    if _check_download(*check_files):
        return

    for url in urls:
        download_file_maybe_extract(url=url, directory=directory)

    if not _check_download(*check_files):
        raise ValueError('[DOWNLOAD FAILED] `*check_files` not found')

Download the files at ``urls`` to ``directory``. Extract to ``directory`` if tar or zip.

Args:
urls (str): Url of files.
directory (str): Directory to download to.
check_files (list of str): Check if these files exist, ensuring the download succeeded.
If these files exist before the download, the download is skipped.

Raises:
ValueError: Error if one of the ``check_files`` are not found following the download.


def help_members(obj, use_other=False):
    r
    import utool as ut
    attrnames = dir(obj)
    attr_list = [getattr(obj, attrname) for attrname in attrnames]
    attr_types = ut.lmap(ut.type_str, map(type, attr_list))
    unique_types, groupxs = ut.group_indices(attr_types)
    type_to_items = ut.dzip(unique_types, ut.apply_grouping(attr_list, groupxs))
    type_to_itemname = ut.dzip(unique_types, ut.apply_grouping(attrnames, groupxs))

    memtypes = ['instancemethod']
    func_mems = ut.dict_subset(type_to_items, memtypes, [])

    func_list = ut.flatten(func_mems.values())
    defsig_list = []
    num_unbound_args_list = []
    num_args_list = []
    for func in func_list:

        argspec = ut.get_func_argspec(func)
        args = argspec.args
        unbound_args = get_unbound_args(argspec)
        defsig = ut.func_defsig(func)
        defsig_list.append(defsig)
        num_unbound_args_list.append(len(unbound_args))
        num_args_list.append(len(args))

    group = ut.hierarchical_group_items(defsig_list, [num_unbound_args_list, num_args_list])
    print(repr(obj))
    print(ut.repr3(group, strvals=True))

    if use_other:
        other_mems = ut.delete_keys(type_to_items.copy(), memtypes)
        other_mems_attrnames = ut.dict_subset(type_to_itemname, other_mems.keys())
        named_other_attrs = ut.dict_union_combine(other_mems_attrnames, other_mems, lambda x, y: list(zip(x, y)))
        print(ut.repr4(named_other_attrs, nl=2, strvals=True))

r"""
Inspects members of a class

Args:
obj (class or module):

CommandLine:
python -m utool.util_inspect help_members

Example:
>>> # ENABLE_DOCTEST
>>> from utool.util_inspect import *  # NOQA
>>> import utool as ut
>>> obj = ut.DynStruct
>>> result = help_members(obj)
>>> print(result)


def nltk_stemmer(stemmer, token, i=None, tokens=None):

    def wrapped_stem(token, metadata=None):
        return stemmer.stem(token)

    return token.update(wrapped_stem)

Wrapper around a NLTK SnowballStemmer, which includes stop words for
each language.

Args:
stemmer (SnowballStemmer): Stemmer instance that performs the stemming.
token (lunr.Token): The token to stem.
i (int): The index of the token in a set.
tokens (list): A list of tokens representing the set.


def __init__(self, key_path_prefix):

    super(WindowsRegistryKeyPathPrefixFilter, self).__init__()
    self._key_path_prefix = key_path_prefix

Initializes a Windows Registry key filter.

Args:
key_path_prefix (str): the key path prefix.


def filter_spec(spec, paths, wildcard='*', separator='/'):

    def remove_key(path, spec):
        if len(path) == 0:
            return
        elif len(path) == 1:
            key = path.pop()
            if not isinstance(spec, collections.Mapping):
                raise LagoUserException(
                    'You have tried to remove the following key - "{key}".\n'
                    'Keys can not be removed from type {spec_type}\n'
                    'Please verify that path - "{{path}}" is valid'.format(
                        key=key, spec_type=type(spec)
                    )
                )
            if key == wildcard:
                spec.clear()
            else:
                spec.pop(key, None)
        else:
            current = path[0]
            if current == wildcard:
                if isinstance(spec, list):
                    iterator = iter(spec)
                elif isinstance(spec, collections.Mapping):
                    iterator = spec.itervalues()
                else:
                    raise LagoUserException(
                        'Glob char {char} should refer only to dict or list, '
                        'not to {spec_type}\n'
                        'Please fix path - "{{path}}"'.format(
                            char=wildcard, spec_type=type(spec)
                        )
                    )

                for i in iterator:
                    remove_key(path[1:], i)
            else:
                try:
                    remove_key(path[1:], spec[current])
                except KeyError:
                    raise LagoUserException(
                        'Malformed path "{{path}}", key "{key}" '
                        'does not exist'.format(key=current)
                    )
                except TypeError:
                    raise LagoUserException(
                        'Malformed path "{{path}}", can not get '
                        'by key from type {spec_type}'.format(
                            spec_type=type(spec)
                        )
                    )

    for path in paths:
        try:
            remove_key(path.split(separator), spec)
        except LagoUserException as e:
            e.message = e.message.format(path=path)
            raise

Remove keys from a spec file.
For example, with the following path: domains/*/disks/*/metadata
all the metadata dicts from all domains disks will be removed.

Args:
spec (dict): spec to remove keys from
paths (list): list of paths to the keys that should be removed
wildcard (str): wildcard character
separator (str): path separator

Returns:
None

Raises:
utils.LagoUserException: If a malformed path was detected


def parse_datetime(value):

    if not value:
        return None
    elif isinstance(value, datetime.datetime):
        return value
    return dateutil.parser.parse(value)

Attempts to parse `value` into an instance of ``datetime.datetime``. If
`value` is ``None``, this function will return ``None``.

Args:
value: A timestamp. This can be a string or datetime.datetime value.


def check_initial_web_request(self, item_session: ItemSession, request: HTTPRequest) -> Tuple[(bool, str)]:
    (verdict, reason, test_info) = self.consult_filters(item_session.request.url_info, item_session.url_record)
    if (verdict and self._robots_txt_checker):
        can_fetch = (yield from self.consult_robots_txt(request))
        if (not can_fetch):
            verdict = False
            reason = 'robotstxt'
    (verdict, reason) = self.consult_hook(item_session, verdict, reason, test_info)
    return (verdict, reason)

Check robots.txt, URL filters, and scripting hook.

Returns:
tuple: (bool, str)

Coroutine.


def _rows_event_to_dict(e, stream):

    pk_cols = e.primary_key if isinstance(e.primary_key, (list, tuple)) \
        else (e.primary_key, )

    if isinstance(e, row_event.UpdateRowsEvent):
        sig = signals.rows_updated
        action = 'update'
        row_converter = _convert_update_row
    elif isinstance(e, row_event.WriteRowsEvent):
        sig = signals.rows_inserted
        action = 'insert'
        row_converter = _convert_write_row
    elif isinstance(e, row_event.DeleteRowsEvent):
        sig = signals.rows_deleted
        action = 'delete'
        row_converter = _convert_write_row
    else:
        assert False, 'Invalid binlog event'

    meta = {
        'time': e.timestamp,
        'log_pos': stream.log_pos,
        'log_file': stream.log_file,
        'schema': e.schema,
        'table': e.table,
        'action': action,
    }
    rows = list(map(row_converter, e.rows))
    for row in rows:
        row['keys'] = {k: row['values'][k] for k in pk_cols}
    return rows, meta

Convert RowsEvent to a dict

Args:
e (pymysqlreplication.row_event.RowsEvent): the event
stream (pymysqlreplication.BinLogStreamReader):
the stream that yields event

Returns:
dict: event's data as a dict


def get_variable_layout(self, variable):
    raise NotImplementedError()

Retrieve the `TensorLayout` for the variable.

Args:
variable: A `Variable` instance.

return:
The `TensorLayout` for the variable, which can be used by
`backend.distribute_value()` to redistribute a variable.


def __init__(self, components):
    global _next_device_number, _next_device_number_lock
    self.components = tuple((device_util.canonicalize(d) for d in components))
    if not self.components:
        raise ValueError('ParallelDevice requires at least one component.')
    ctx = context.context()
    with _next_device_number_lock:
        self._name = '{}/device:CUSTOM:{}'.format(ctx.host_address_space(), _next_device_number)
        _next_device_number += 1
    device, device_info = _pywrap_parallel_device.GetParallelDeviceCapsules(self._name, self.components)
    context.register_custom_device(device, self._name, device_info)
    self._device_ids = None
    self._device_scope = None
    _all_parallel_devices[self._name] = self

Creates a device which executes operations in parallel on `components`.

Args:
components: A list of device names. Each operation executed on the
returned device executes on these component devices.

Returns:
A string with the name of the newly created device.


def put_image(self, name, val):
    assert isinstance(val, np.ndarray)
    arr = image_to_nhwc(val)
    self._dispatch((lambda m: m.process_image(name, arr)))
    s = create_image_summary(name, arr)
    self._dispatch((lambda m: m.process_summary(s)))

Put an image.

Args:
name (str):
val (np.ndarray): 2D, 3D (HWC) or 4D (NHWC) numpy array of images
in range [0,255]. If channel is 3, assumed to be RGB.


def copy_all_a(input_a, *other_inputs, **kwargs):
    output = []
    while (input_a.count() > 0):
        output.append(input_a.pop())
    for input_x in other_inputs:
        input_x.skip_all()
    return output

Copy all readings in input a into the output.

All other inputs are skipped so that after this function runs there are no
readings left in any of the input walkers when the function finishes, even
if it generated no output readings.

Returns:
list(IOTileReading)


def generate_zip_data(M, L, n_cells, cluster_probs=None):

    genes, clusters = M.shape
    output = np.zeros((genes, n_cells))
    if cluster_probs is None:
        cluster_probs = np.ones(clusters)/clusters
    zip_p = np.random.random((genes, n_cells))
    labels = []
    for i in range(n_cells):
        c = np.random.choice(range(clusters), p=cluster_probs)
        labels.append(c)
        output[:,i] = np.where(zip_p[:,i] < L[:,c], 0, np.random.poisson(M[:,c]))
    return output, np.array(labels)

Generates zero-inflated poisson-distributed data, given a set of means and zero probs for each cluster.

Args:
M (array): genes x clusters matrix
L (array): genes x clusters matrix - zero-inflation parameters
n_cells (int): number of output cells
cluster_probs (array): prior probability for each cluster.
Default: uniform.

Returns:
output - array with shape genes x n_cells
labels - array of cluster labels


def _create_and_save_state(cls, mapreduce_spec, _app):

    state = model.MapreduceState.create_new(mapreduce_spec.mapreduce_id)
    state.mapreduce_spec = mapreduce_spec
    state.active = True
    state.active_shards = 0
    if _app:
      state.app_id = _app
    config = util.create_datastore_write_config(mapreduce_spec)
    state.put(config=config)
    return state

Save mapreduce state to datastore.

Save state to datastore so that UI can see it immediately.

Args:
mapreduce_spec: model.MapreduceSpec,
_app: app id if specified. None otherwise.

Returns:
The saved Mapreduce state.


def concat(self, name=None):
    return self._implementation.concat(name=name)

Return the values in the TensorArray as a concatenated `Tensor`.

All of the values must have been written, their ranks must match, and
and their shapes must all match for all dimensions except the first.

Args:
name: A name for the operation (optional).

Returns:
All the tensors in the TensorArray concatenated into one tensor.


def clone(self, name=None):

    if name is None:
      name = self.module_name + "_clone"
    return Linear(output_size=self.output_size,
                  use_bias=self._use_bias,
                  initializers=self._initializers,
                  partitioners=self._partitioners,
                  regularizers=self._regularizers,
                  name=name)

Returns a cloned `Linear` module.

Args:
name: Optional string assigning name of cloned module. The default name
is constructed by appending "_clone" to `self.module_name`.

Returns:
Cloned `Linear` module.


def get_nmr_quadrupole_moment(self, isotope=None):

        quad_mom = self._el.nmr_quadrupole_moment

        if len(quad_mom) == 0:
            return 0.0

        if isotope is None:
            isotopes = list(quad_mom.keys())
            isotopes.sort(key=lambda x: int(x.split("-")[1]), reverse=False)
            return quad_mom.get(isotopes[0], 0.0)
        else:
            if isotope not in quad_mom:
                raise ValueError("No quadrupole moment for isotope {}".format(
                    isotope))
            return quad_mom.get(isotope, 0.0)

Gets the nuclear electric quadrupole moment in units of
e*millibarns

Args:
isotope (str): the isotope to get the quadrupole moment for
default is None, which gets the lowest mass isotope


def create_grad(node, namer, tangent=False):
    if (not isinstance(node, (gast.Subscript, gast.Name, gast.Str))):
        raise TypeError
    if anno.hasanno(node, 'temp_var'):
        return create_grad(anno.getanno(node, 'temp_var'), namer, tangent)

    def _name_grad(node):
        if (not isinstance(node, gast.Name)):
            raise TypeError
        varname = node.id
        name = namer.grad(varname, tangent)
        grad_node = gast.Name(id=name, ctx=None, annotation=None)
        anno.setanno(grad_node, 'adjoint_var', node)
        return grad_node
    if isinstance(node, gast.Subscript):
        grad_node = create_grad(node.value, namer, tangent=tangent)
        grad_node.ctx = gast.Load()
        return gast.Subscript(value=grad_node, slice=node.slice, ctx=None)
    elif isinstance(node, gast.Str):
        grad_node = create_grad(gast.Name(id=node.s, ctx=None, annotation=None), namer, tangent=tangent)
        return gast.Str(grad_node.id)
    else:
        return _name_grad(node)

Given a variable, create a variable for the gradient.

Args:
node: A node to create a gradient for, can be a normal variable (`x`) or a
subscript (`x[i]`).
namer: The namer object which will determine the name to use for the
gradient.
tangent: Whether a tangent (instead of adjoint) is created.

Returns:
node: A node representing the gradient with the correct name e.g. the
gradient of `x[i]` is `dx[i]`.

Note that this returns an invalid node, with the `ctx` attribute
missing. It is assumed that this attribute is filled in later.

Node has an `adjoint_var` annotation referring to the node it is an
adjoint of.


def _create_mirrored_tpu_variables(**kwargs):
    initial_value = None
    value_list = []
    for i, d in enumerate(devices):
        with ops.device(d):
            if i == 0:
                initial_value = kwargs['initial_value']
                with maybe_init_scope():
                    initial_value = initial_value() if callable(initial_value) else initial_value
            if i > 0:
                var0name = value_list[0].name.split(':')[0]
                kwargs['name'] = '%s/replica_%d/' % (var0name, i)
            kwargs['initial_value'] = initial_value
            with context.device_policy(context.DEVICE_PLACEMENT_SILENT):
                v = next_creator(**kwargs)
            assert not isinstance(v, tpu_values.TPUMirroredVariable)
            value_list.append(v)
    return value_list

Returns a list of `tf.Variable`s.

The list contains `number_replicas` `tf.Variable`s and can be used to
initialize a `TPUMirroredVariable`.

Args:
**kwargs: the keyword arguments for creating a variable


def org(self, notification_type, priority='Low'):
    self._notification_type = notification_type
    self._recipients = None
    self._priority = priority
    self._is_organization = True

Set vars for the passed in data. Used for org notification.

.. code-block:: javascript

{
"notificationType": notification_type,
"priority": priority
"isOrganization": true
}

Args:
notification_type (str): The notification type.
priority (str): The priority: Low, Medium, High.


def _build_kernel(self, kernel_source, compile_flags=()):

        return cl.Program(self._cl_context, kernel_source).build(' '.join(compile_flags))

Convenience function for building the kernel for this worker.

Args:
kernel_source (str): the kernel source to use for building the kernel

Returns:
cl.Program: a compiled CL kernel


def flush(cls, *args):

        return _remove_keys([], [(cls._make_key(args) if args else cls.PREFIX) + '*'])

Removes all keys of this namespace
Without args, clears all keys starting with cls.PREFIX
if called with args, clears keys starting with given cls.PREFIX + args

Args:
*args: Arbitrary number of arguments.

Returns:
List of removed keys.


def num_nodes(self, leaves=True, internal=True):
    if (not isinstance(leaves, bool)):
        raise TypeError('leaves must be a bool')
    if (not isinstance(internal, bool)):
        raise TypeError('internal must be a bool')
    num = 0
    for node in self.traverse_preorder():
        if ((leaves and node.is_leaf()) or (internal and (not node.is_leaf()))):
            num += 1
    return num

Compute the total number of selected nodes in this ``Tree``

Args:
``leaves`` (``bool``): ``True`` to include leaves, otherwise ``False``

``internal`` (``bool``): ``True`` to include internal nodes, otherwise ``False``

Returns:
``int``: The total number of selected nodes in this ``Tree``


def datporod(gnomoutfile):
    results = subprocess.check_output(['datporod', gnomoutfile]).decode('utf-8').strip().split()
    return (float(results[0]), float(results[1]), float(results[2]))

Run datporod and return the estimated Porod volume.

Returns:
Radius of gyration found in the input file
I0 found in the input file
Vporod: the estimated Porod volume


def tf_output(c_op, index):
    ret = c_api.TF_Output()
    ret.oper = c_op
    ret.index = index
    return ret

Returns a wrapped TF_Output with specified operation and index.

Args:
c_op: wrapped TF_Operation
index: integer

Returns:
Wrapped TF_Output


def _dict_func(self, func, axis, *args, **kwargs):

        if "axis" not in kwargs:
            kwargs["axis"] = axis

        if axis == 0:
            index = self.columns
        else:
            index = self.index
        func = {idx: func[key] for key in func for idx in index.get_indexer_for([key])}

        def dict_apply_builder(df, func_dict={}):

            return pandas.DataFrame(df.apply(func_dict, *args, **kwargs))

        result_data = self.data.apply_func_to_select_indices_along_full_axis(
            axis, dict_apply_builder, func, keep_remaining=False
        )
        full_result = self._post_process_apply(result_data, axis)
        return full_result

Apply function to certain indices across given axis.

Args:
func: The function to apply.
axis: Target axis to apply the function along.

Returns:
A new PandasQueryCompiler.


def _reset(self, indices):
    self.assert_common_preconditions()
    return np.stack([self._envs[index].reset() for index in indices])

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

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

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

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


def CaseGroups(unicode_dir=_UNICODE_DIR):

  togroup = {}

  def DoLine(codes, fields):

    (_, foldtype, lower, _) = fields
    if foldtype not in ("C", "S"):
      return
    lower = _UInt(lower)
    togroup.setdefault(lower, [lower]).extend(codes)

  ReadUnicodeTable(unicode_dir+"/CaseFolding.txt", 4, DoLine)

  groups = togroup.values()
  for g in groups:
    g.sort()
  groups.sort()
  return togroup, groups

Returns list of Unicode code groups equivalent under case folding.

Each group is a sorted list of code points,
and the list of groups is sorted by first code point
in the group.

Args:
unicode_dir: Unicode data directory

Returns:
list of Unicode code groups


def _get_response(self, endpoint, request_dict):
    http_error = 'Could not connect to the API. This could be because you have no internet connection, a parameter was input incorrectly, or the API is currently down. Please try again.'
    json_error = 'Could not retrieve JSON values. Try again with a shorter date range.'
    try:
        qsp = urllib.parse.urlencode(request_dict, doseq=True)
        resp = urllib.request.urlopen((((self.base_url + endpoint) + '?') + qsp)).read()
    except (AttributeError or NameError):
        try:
            qsp = urllib.urlencode(request_dict, doseq=True)
            resp = urllib2.urlopen((((self.base_url + endpoint) + '?') + qsp)).read()
        except urllib2.URLError:
            raise MesoPyError(http_error)
    except urllib.error.URLError:
        raise MesoPyError(http_error)
    try:
        json_data = json.loads(resp.decode('utf-8'))
    except ValueError:
        raise MesoPyError(json_error)
    return self._checkresponse(json_data)

Returns a dictionary of data requested by each function.

Arguments:
----------
endpoint: string, mandatory
Set in all other methods, this is the API endpoint specific to each function.
request_dict: string, mandatory
A dictionary of parameters that are formatted into the API call.

Returns:
--------
response: A dictionary that has been dumped from JSON.

Raises:
-------
MesoPyError: Overrides the exceptions given in the requests library to give more custom error messages.
Connection_error occurs if no internet connection exists. Timeout_error occurs if the request takes too
long and redirect_error is shown if the url is formatted incorrectly.


def from_proto(cls, struct_def_proto: message.Message, backbone_element_path: Optional[str]=None, element_type: Optional[str]=None, parent_definitions: Optional[ChildDefinitions]=None) -> 'StructureDataType':
    struct_def = cast(Any, struct_def_proto)
    raw_url = struct_def.url.value
    base_type = struct_def.type.value
    element_type = element_type if element_type else base_type
    qualified_path = f'{element_type}.{backbone_element_path}' if backbone_element_path else element_type
    child_defs = ChildDefinitions()
    slices: dict[str, _SliceBuilder] = collections.defaultdict(lambda: _SliceBuilder(None, None, []))
    root_element_definition = None
    for elem in struct_def.snapshot.element:
        if elem.base.path.value == 'Extension.url':
            continue
        path = _get_analytic_path(elem.path.value, elem.id.value)
        if path == qualified_path:
            if elem.slice_name.value:
                slice_def = slices[f':{elem.slice_name.value}']
                slice_def.slice_def = elem
                slice_def.relative_path = ''
            else:
                root_element_definition = elem
            continue
        if re.search(f'^{qualified_path}\\.\\w+', path):
            relative_path = path[len(qualified_path) + 1:]
            closest_slice_ancestor = re.search(f'^{qualified_path}[\\.]?(.*(?<!.extension):[\\w-]+)(?:$|\\.)', elem.id.value)
            if closest_slice_ancestor is None:
                child_defs.add_definition(relative_path, elem)
            else:
                slice_def = slices[closest_slice_ancestor[1]]
                if elem.slice_name.value:
                    slice_def.slice_def = elem
                    slice_def.relative_path = relative_path
                else:
                    slice_def.slice_rules.append((relative_path, elem))
    if parent_definitions is not None:
        child_defs.update(parent_definitions)
    if not root_element_definition:
        raise ValueError(f'StructureDataType {raw_url} searching on {qualified_path}  missing root element definition. {struct_def}')
    return cls(structure_definition=struct_def, backbone_element_path=backbone_element_path, base_type=base_type, element_type=element_type, _child_defs=child_defs, _slices=tuple((slice_def.to_slice() for slice_def in slices.values())), _raw_url=raw_url, root_element_definition=root_element_definition, cardinality=Cardinality.SCALAR)

Creates a StructureDataType from a proto.

Args:
struct_def_proto: Proto containing information about the structure
definition.
backbone_element_path: Optional path to the structure def.
element_type: Potential alternative type name for the type.
parent_definitions: Element definitions defined by parent structure
definitions which should override definitions in `struct_def_proto`. If
structure definitions supply element definitions at nested paths, e.g.
Foo.bar.baz.quux, those element definitions need to be passed via the
`parent_definitions` argument to ensure element definitions will be
chosen from the parent rather than `struct_def_proto`. e.g. if
`struct_def_proto` defines 'Baz.quux,' the parent's 'Foo.bar.baz.quux'
definition must be provided here in order to be chosen over the
`struct_def_proto` definition.

Returns:
A StructureDataType.


class Idefics2Encoder(nn.Module):

    def __init__(self, config: Idefics2Config):
        super().__init__()
        self.config = config
        self.layers = nn.ModuleList([Idefics2EncoderLayer(config) for _ in range(config.num_hidden_layers)])
        self.gradient_checkpointing = False

    def forward(self, inputs_embeds, attention_mask: Optional[torch.Tensor]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None) -> Union[Tuple, BaseModelOutput]:

        output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
        output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        encoder_states = () if output_hidden_states else None
        all_attentions = () if output_attentions else None
        hidden_states = inputs_embeds
        for encoder_layer in self.layers:
            if output_hidden_states:
                encoder_states = encoder_states + (hidden_states,)
            if self.gradient_checkpointing and self.training:
                layer_outputs = self._gradient_checkpointing_func(encoder_layer.__call__, hidden_states, attention_mask, output_attentions)
            else:
                layer_outputs = encoder_layer(hidden_states, attention_mask, output_attentions=output_attentions)
            hidden_states = layer_outputs[0]
            if output_attentions:
                all_attentions = all_attentions + (layer_outputs[1],)
        if output_hidden_states:
            encoder_states = encoder_states + (hidden_states,)
        if not return_dict:
            return tuple((v for v in [hidden_states, encoder_states, all_attentions] if v is not None))
        return BaseModelOutput(last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions)

Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a
[`Idefics2EncoderLayer`].

Args:
config: Idefics2Config


def merge_tags(left, right, factory=Tags):

    if isinstance(left, Mapping):
        tags = dict(left)
    elif hasattr(left, 'tags'):
        tags = _tags_to_dict(left.tags)
    else:
        tags = _tags_to_dict(left)

    if isinstance(right, Mapping):
        tags.update(right)
    elif hasattr(left, 'tags'):
        tags.update(_tags_to_dict(right.tags))
    else:
        tags.update(_tags_to_dict(right))

    return factory(**tags)

Merge two sets of tags into a new troposphere object

Args:
left (Union[dict, troposphere.Tags]): dictionary or Tags object to be
merged with lower priority
right (Union[dict, troposphere.Tags]): dictionary or Tags object to be
merged with higher priority
factory (type): Type of object to create. Defaults to the troposphere
Tags class.


def _get_paddings_constant(paddings):
    if isinstance(paddings, tensor_lib.Tensor):
        return tensor_util.constant_value(paddings, partial=True)
    elif isinstance(paddings, (list, tuple)):
        return [_get_paddings_constant(x) for x in paddings]
    else:
        return paddings

Helper to get the constant values of the paddings arg to pad().

Used under V1 graph mode to facilitate computation of the shape of the output
tensor of `pad()`.

Args:
paddings: The same paddings arg as passed to pad(). Can be a Tensor, or
a nested list or tuple of Tensor and/or numbers.

Returns:
A nested list or numbers or `None`, in which `None` indicates unknown
padding size.