def namespace_for_prefix(self, prefix):

        try:
            ni = self.__lookup_prefix(prefix)
        except PrefixNotFoundError:
            return None
        else:
            return ni.uri

Get the namespace the given prefix maps to.

Args:
prefix (str): The prefix

Returns:
str: The namespace, or None if the prefix isn't mapped to
anything in this set.


def schema_from_json(self, file_or_path):
    if isinstance(file_or_path, io.IOBase):
        return self._schema_from_json_file_object(file_or_path)
    with open(file_or_path) as file_obj:
        return self._schema_from_json_file_object(file_obj)

Takes a file object or file path that contains json that describes
a table schema.

Returns:
List of schema field objects.


def video_augmentation(features, hue=False, saturate=False, contrast=False):
    (inputs, targets) = (features['inputs'], features['targets'])
    in_steps = common_layers.shape_list(inputs)[0]
    video = tf.concat((inputs, targets), axis=0)
    if hue:
        video = tf.image.random_hue(video, max_delta=0.2)
    if saturate:
        video = tf.image.random_saturation(video, lower=0.5, upper=1.5)
    if contrast:
        video = tf.image.random_contrast(video, lower=0.5, upper=1.5)
    (features['inputs'], features['targets']) = (video[:in_steps], video[in_steps:])
    return features

Augments video with optional hue, saturation and constrast.

Args:
features: dict, with keys "inputs", "targets".
features["inputs"], 4-D Tensor, shape=(THWC)
features["targets"], 4-D Tensor, shape=(THWC)
hue: bool, apply hue_transform.
saturate: bool, apply saturation transform.
contrast: bool, apply constrast transform.
Returns:
augment_features: dict with transformed "inputs" and "targets".


def minimize(self, minimize):

        self._minimize = minimize
        self._logger.log('debug', 'Minimize set to {}'.format(minimize))

Configures the ABC to minimize fitness function return value or
derived score

Args:
minimize (bool): if True, minimizes fitness function return value;
if False, minimizes derived score


def dump(o, f):
    if (not f.write):
        raise TypeError('You can only dump an object to a file descriptor')
    d = dumps(o)
    f.write(d)
    return d

Writes out dict as toml to a file

Args:
o: Object to dump into toml
f: File descriptor where the toml should be stored

Returns:
String containing the toml corresponding to dictionary

Raises:
TypeError: When anything other than file descriptor is passed


def get_axis_value(self, axis):
    if (self.type != EventType.POINTER_AXIS):
        raise AttributeError(_wrong_meth.format(self.type))
    return self._libinput.libinput_event_pointer_get_axis_value(self._handle, axis)

Return the axis value of the given axis.

The interpretation of the value depends on the axis. For the two
scrolling axes :attr:`~libinput.constant.PointerAxis.SCROLL_VERTICAL`
and :attr:`~libinput.constant.PointerAxis.SCROLL_HORIZONTAL`, the value
of the event is in relative scroll units, with the positive direction
being down or right, respectively. For the interpretation of the value,
see :attr:`axis_source`.

If :meth:`has_axis` returns False for an axis, this method returns 0
for that axis.

For pointer events that are not of type
:attr:`~libinput.constant.Event.POINTER_AXIS`, this method raises
:exc:`AttributeError`.

Args:
axis (~libinput.constant.PointerAxis): The axis who's value to get.
Returns:
float: The axis value of this event.
Raises:
AttributeError


def list_pull_requests(self, username, page, status=None):
    request_url = '{}/api/0/user/{}/requests/filed'.format(self.instance, username)
    payload = {}
    if (username is not None):
        payload['username'] = username
    if (page is not None):
        payload['page'] = page
    if (status is not None):
        payload['status'] = status
    return_value = self._call_api(request_url, params=payload)
    return return_value['requests']

List pull-requests filed by user.

Params:
username (string): filters the username of the user whose activity you are interested in.
page (integer): the page requested. Defaults to 1.
status (string): filter the status of pull requests. Default: Open,
can be Closed, Merged, All.

Returns:
list: A list of Pull-Requests filed by a given user for all the
projects for given Pagure instance.


def find_primitive(self):
    (lattice, scaled_positions, numbers) = spglib.find_primitive(self._cell, symprec=self._symprec)
    species = [self._unique_species[(i - 1)] for i in numbers]
    return Structure(lattice, species, scaled_positions, to_unit_cell=True).get_reduced_structure()

Find a primitive version of the unit cell.

Returns:
A primitive cell in the input cell is searched and returned
as an Structure object. If no primitive cell is found, None is
returned.


def _hard_upsample(self, hidden_states, durations):
    if hidden_states.size(0) == 1:
        hidden_states = torch.repeat_interleave(hidden_states, durations.view(-1), dim=1)
    else:
        if hidden_states.shape[0] > 1 and self.training:
            logger.warning_once('`self.training=True` and you use batching. You lose parallelism during the hifigan\n                               forward pass because the samples are interleaved.')
        hidden_states = [torch.repeat_interleave(hidden_state, duration, dim=0) for hidden_state, duration in zip(hidden_states, durations)]
        hidden_states = nn.utils.rnn.pad_sequence(hidden_states, batch_first=True)
    return hidden_states

Repeats the time dimension of each sample in the batch based on the corresponding duration.

Args:
hidden_states (`torch.Tensor` of shape `(batch_size, sequence_length, *)`, *optional*):
The sequence to repeat, where `*` is any number of sequence-specific dimensions including none.
durations (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
Indicates how many times to repeat time segments.


def _update_inplace(self, new_query_compiler):

        old_query_compiler = self._query_compiler
        self._query_compiler = new_query_compiler
        old_query_compiler.free()

Updates the current DataFrame inplace.

Args:
new_query_compiler: The new QueryCompiler to use to manage the data


def write_config_file(config_instance, appdirs=DEFAULT_APPDIRS,
        file_name=DEFAULT_CONFIG_FILENAME):

    path = get_config_path(appdirs, file_name)
    with open(path, 'w') as fobj:
        config_instance.write(fobj)
    return config_instance

Write a ConfigParser instance to file at the correct location.

Args:
config_instance: Config instance to safe to file.
appdirs (HamsterAppDirs, optional): ``HamsterAppDirs`` instance storing app/user specific
path information.
file_name (text_type, optional): Name of the config file. Defaults to
``DEFAULT_CONFIG_FILENAME``.

Returns:
SafeConfigParser: Instance written to file.


def _get_corrupted_example(self, x):
    corruption_type = self.builder_config.corruption_type
    severity = self.builder_config.severity
    return {'gaussian_noise': corruptions.gaussian_noise, 'shot_noise': corruptions.shot_noise, 'impulse_noise': corruptions.impulse_noise, 'defocus_blur': corruptions.defocus_blur, 'frosted_glass_blur': corruptions.frosted_glass_blur, 'zoom_blur': corruptions.zoom_blur, 'fog': corruptions.fog, 'brightness': corruptions.brightness, 'contrast': corruptions.contrast, 'elastic': corruptions.elastic, 'pixelate': corruptions.pixelate, 'jpeg_compression': corruptions.jpeg_compression}[corruption_type](x, severity)

Return corrupted images.

Args:
x: numpy array, uncorrupted image.

Returns:
numpy array, corrupted images.


def cos(times: np.ndarray, amp: complex, freq: float, phase: float=0) -> np.ndarray:
    return (amp * np.cos(((((2 * np.pi) * freq) * times) + phase)).astype(np.complex_))

Continuous cosine wave.

Args:
times: Times to output wave for.
amp: Pulse amplitude.
freq: Pulse frequency, units of 1/dt.
phase: Pulse phase.


def CreateUnit(self, parent=None, value=None, bid_amount=None):
    unit = {'xsi_type': 'ProductPartition', 'partitionType': 'UNIT'}
    if (parent is not None):
        unit['parentCriterionId'] = parent['id']
        unit['caseValue'] = value
    if ((bid_amount is not None) and (bid_amount > 0)):
        bidding_strategy_configuration = {'bids': [{'xsi_type': 'CpcBid', 'bid': {'xsi_type': 'Money', 'microAmount': str(bid_amount)}}]}
        adgroup_criterion = {'xsi_type': 'BiddableAdGroupCriterion', 'biddingStrategyConfiguration': bidding_strategy_configuration}
    else:
        adgroup_criterion = {'xsi_type': 'NegativeAdGroupCriterion'}
    adgroup_criterion['adGroupId'] = self.adgroup_id
    adgroup_criterion['criterion'] = unit
    self.CreateAddOperation(adgroup_criterion)
    return unit

Creates a unit node.

Args:
parent: The node that should be this node's parent.
value: The value being partitioned on.
bid_amount: The amount to bid for matching products, in micros.
Returns:
A new unit node.


def normalize(code):
    if (len(code) == 3):
        return code
    normalized = translate(code)
    if normalized:
        return normalized
    country = countries.get(code, None)
    if country:
        return country.alpha3.lower()
    return code

Normalize language codes to ISO 639-2. If all conversions fails, return the
`code` as it was given.

Args:
code (str): Language / country code.

Returns:
str: ISO 639-2 country code.


def unpack_small_tensors(tower_grads, packing):

    if not packing:
        return tower_grads
    new_tower_grads = []
    num_devices = len(tower_grads)
    num_packed = len(packing.keys())
    for dev_idx, gv_list in enumerate(tower_grads):
        new_gv_list = gv_list[num_packed:]
        for i in xrange(0, num_packed):
            k = "%d:%d" % (dev_idx, i)
            gpt = packing[k]
            gv = unpack_grad_tuple(gv_list[i], gpt)
            for gi, idx in enumerate(gpt.indices):
                assert idx == gpt.indices[gi]
                new_gv_list.insert(idx, gv[gi])
        new_tower_grads.append(new_gv_list)
    return new_tower_grads

Undo the structure alterations to tower_grads done by pack_small_tensors.

Args:
tower_grads: List of List of (grad, var) tuples.
packing: A dict generated by pack_small_tensors describing the changes
it made to tower_grads.

Returns:
new_tower_grads: identical to tower_grads except that concatentations
of small tensors have been split apart and returned to their original
positions, paired with their original variables.


def vcf_records(self, qualified=False):

        if qualified:
            sample_names = self.qualified_sample_names
        else:
            sample_names = self.sample_names

        for line in self._file_reader.read_lines():
            if line.startswith("
                continue
            yield VcfRecord.parse_record(line, sample_names)

Generates parsed VcfRecord objects.

Typically called in a for loop to process each vcf record in a
VcfReader. VcfReader must be opened in advanced and closed when
complete. Skips all headers.

Args:
qualified: When True, sample names are prefixed with file name

Returns:
Parsed VcfRecord

Raises:
StopIteration: when reader is exhausted.
TypeError: if reader is closed.


def convert(self, vroot, entry_variables):

        self.graph_info = GraphInfo(vroot)
        self.entry_variables = entry_variables

        cnt = 0
        with nn.parameter_scope(self.name):

            for t, func in enumerate(self.graph_info.funcs):
                if func.name == "BatchNormalization":
                    bn_func = func

                    if bn_func.info.args["batch_stat"] == False:
                        o = self._bn_linear_conversion(bn_func, cnt)
                        cnt += 1
                        continue

                o = self._identity_conversion(func)

        self.end_variable = o
        return self.end_variable

All functions are replaced with the same `new` function.

Args:
vroot (:obj:`Variable`): NNabla Variable
entry_variables (:obj:`Variable`): Entry variable from which the conversion starts.


def _compute_upper_group(df):

    upper_group = df.groupby(['groups']).agg({
        'value': sum,
        'value_start': sum,
        'upperGroup_label': 'first',
        'upperGroup_order': 'first'
    }).reset_index()
    upper_group['type'] = 'parent'
    upper_group['variation'] = upper_group['value'] / upper_group[
        'value_start']
    upper_group.drop(['value_start'], axis=1, inplace=True)
    upper_group.rename(columns={'upperGroup_label': 'label'}, inplace=True)
    return upper_group

Compute upperGroup
Args:
df (Dataframe):

Returns: Dataframe


def CreateBiddingStrategy(client):

  bidding_strategy_service = client.GetService(
      'BiddingStrategyService', version='v201809')

  shared_bidding_strategy = {
      'name': 'Maximize Clicks %s' % uuid.uuid4(),
      'biddingScheme': {
          'xsi_type': 'TargetSpendBiddingScheme',

          'bidCeiling': {
              'microAmount': '2000000'
          }
      }
  }

  operation = {
      'operator': 'ADD',
      'operand': shared_bidding_strategy
  }

  response = bidding_strategy_service.mutate([operation])
  new_bidding_strategy = response['value'][0]

  print ('Shared bidding strategy with name "%s" and ID "%s" of type "%s"'
         'was created.' %
         (new_bidding_strategy['name'], new_bidding_strategy['id'],
          new_bidding_strategy['biddingScheme']['BiddingScheme.Type']))

  return new_bidding_strategy

Creates a bidding strategy object.

Args:
client: AdWordsClient the client to run the example with.

Returns:
dict An object representing a bidding strategy.


def apply2(self, func, *args, **kwargs):
    ret = func(args[0], self._t, *args[1:], **kwargs)
    return LinearWrap(ret)

Apply a function on the wrapped tensor. The tensor
will be the second argument of func.

This is because many symbolic functions
(such as tensorpack's layers) takes 'scope' as the first argument.

Returns:
LinearWrap: ``LinearWrap(func(args[0], self.tensor(), *args[1:], **kwargs))``.


def _avro_rows(block, avro_schema):

    blockio = six.BytesIO(block.avro_rows.serialized_binary_rows)
    while True:

        try:

            yield fastavro.schemaless_reader(blockio, avro_schema)
        except StopIteration:
            break

Parse all rows in a stream block.

Args:
block ( \
~google.cloud.bigquery_storage_v1beta1.types.ReadRowsResponse \
):
A block containing Avro bytes to parse into rows.
avro_schema (fastavro.schema):
A parsed Avro schema, used to deserialized the bytes in the
block.

Returns:
Iterable[Mapping]:
A sequence of rows, represented as dictionaries.


def execute_show(args, root_dir):
    key = None
    if args.get('key'):
        key = args['key']
        status = command_factory('status')({}, root_dir=root_dir)
        if ((key not in status['data']) or (status['data'][key]['status'] != 'running')):
            print('No running process with this key, use `log` to show finished processes.')
            return
    else:
        status = command_factory('status')({}, root_dir=root_dir)
        if isinstance(status['data'], str):
            print(status['data'])
            return
        for k in sorted(status['data'].keys()):
            if (status['data'][k]['status'] == 'running'):
                key = k
                break
        if (key is None):
            print('No running process, use `log` to show finished processes.')
            return
    config_dir = os.path.join(root_dir, '.config/pueue')
    stdoutFile = os.path.join(config_dir, 'pueue_process_{}.stdout'.format(key))
    stderrFile = os.path.join(config_dir, 'pueue_process_{}.stderr'.format(key))
    stdoutDescriptor = open(stdoutFile, 'r')
    stderrDescriptor = open(stderrFile, 'r')
    running = True
    if args['watch']:
        stdscr = curses.initscr()
        curses.noecho()
        curses.cbreak()
        curses.curs_set(2)
        stdscr.keypad(True)
        stdscr.refresh()
        try:
            while running:
                stdscr.clear()
                stdoutDescriptor.seek(0)
                message = stdoutDescriptor.read()
                stdscr.addstr(0, 0, message)
                stdscr.refresh()
                time.sleep(2)
        except Exception:
            curses.nocbreak()
            stdscr.keypad(False)
            curses.echo()
            curses.endwin()
    else:
        print('Stdout output:\n')
        stdoutDescriptor.seek(0)
        print(get_descriptor_output(stdoutDescriptor, key))
        print('\n\nStderr output:\n')
        stderrDescriptor.seek(0)
        print(get_descriptor_output(stderrDescriptor, key))

Print stderr and stdout of the current running process.

Args:
args['watch'] (bool): If True, we open a curses session and tail
the output live in the console.
root_dir (string): The path to the root directory the daemon is running in.


def compute_loss(self, model: nn.Module, inputs: dict[str, Union[torch.Tensor, Any]], return_outputs: bool=False, num_items_in_batch: Optional[torch.Tensor]=None):
    if (self.label_smoother is not None or self.compute_loss_func is not None) and 'labels' in inputs:
        labels = inputs.pop('labels')
    else:
        labels = None
    if self.model_accepts_loss_kwargs:
        loss_kwargs = {}
        if num_items_in_batch is not None:
            loss_kwargs['num_items_in_batch'] = num_items_in_batch
        inputs = {**inputs, **loss_kwargs}
    outputs = model(**inputs)
    if self.args.past_index >= 0:
        self._past = outputs[self.args.past_index]
    if labels is not None:
        unwrapped_model = self.accelerator.unwrap_model(model)
        if _is_peft_model(unwrapped_model):
            model_name = unwrapped_model.base_model.model._get_name()
        else:
            model_name = unwrapped_model._get_name()
        if self.compute_loss_func is not None:
            loss = self.compute_loss_func(outputs, labels, num_items_in_batch=num_items_in_batch)
        elif model_name in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES.values():
            loss = self.label_smoother(outputs, labels, shift_labels=True)
        else:
            loss = self.label_smoother(outputs, labels)
    else:
        if isinstance(outputs, dict) and 'loss' not in outputs:
            raise ValueError(f'The model did not return a loss from the inputs, only the following keys: {','.join(outputs.keys())}. For reference, the inputs it received are {','.join(inputs.keys())}.')
        loss = outputs['loss'] if isinstance(outputs, dict) else outputs[0]
    if self.args.average_tokens_across_devices and (self.model_accepts_loss_kwargs or self.compute_loss_func) and (num_items_in_batch is not None):
        loss *= self.accelerator.num_processes
    return (loss, outputs) if return_outputs else loss

How the loss is computed by Trainer. By default, all models return the loss in the first element.

Args:
model (`nn.Module`):
The model to compute the loss for.
inputs (`Dict[str, Union[torch.Tensor, Any]]`):
The input data for the model.
return_outputs (`bool`, *optional*, defaults to `False`):
Whether to return the model outputs along with the loss.
num_items_in_batch (Optional[torch.Tensor], *optional*):
The number of items in the batch. If num_items_in_batch is not passed,

Returns:
The loss of the model along with its output if return_outputs was set to True

Subclass and override for custom behavior. If you are not using `num_items_in_batch` when computing your loss,
make sure to overwrite `self.model_accepts_loss_kwargs` to `False`. Otherwise, the loss calculationg might be slightly inacurate when performing gradient accumulation.


def proxy_num(self, protocol=None):
    http_num = len(self.proxies['http'])
    https_num = len(self.proxies['https'])
    if (protocol == 'http'):
        return http_num
    elif (protocol == 'https'):
        return https_num
    else:
        return (http_num + https_num)

Get the number of proxies in the pool

Args:
protocol (str, optional): 'http' or 'https' or None. (default None)

Returns:
If protocol is None, return the total number of proxies, otherwise,
return the number of proxies of corresponding protocol.


def get_njobs_in_queue(self, username=None):
    if (username is None):
        username = getpass.getuser()
    (njobs, process) = self._get_njobs_in_queue(username=username)
    if ((process is not None) and (process.returncode != 0)):
        err_msg = ('Error trying to get the number of jobs in the queue' + 'The error response reads:\n {}'.format(process.stderr.read()))
        logger.critical(err_msg)
    if (not isinstance(self, ShellAdapter)):
        logger.info('The number of jobs currently in the queue is: {}'.format(njobs))
    return njobs

returns the number of jobs in the queue, probably using subprocess or shutil to
call a command like 'qstat'. returns None when the number of jobs cannot be determined.

Args:
username: (str) the username of the jobs to count (default is to autodetect)


def forward(self, hidden_states: torch.Tensor, attention_mask: torch.Tensor, position_embeddings: Optional[torch.Tensor]=None, output_attentions: bool=False):
    residual = hidden_states
    query = key = self.with_pos_embed(hidden_states, position_embeddings)
    hidden_states = self.self_attn(queries=query, keys=key, values=hidden_states, attention_mask=attention_mask, output_attentions=output_attentions)
    hidden_states, attentions = hidden_states if output_attentions else (hidden_states[0], None)
    hidden_states = self.dropout(hidden_states)
    hidden_states = residual + hidden_states
    hidden_states = self.self_attn_layer_norm(hidden_states)
    residual = hidden_states
    hidden_states = self.activation_fn(self.fc1(hidden_states))
    hidden_states = self.encoder_feedforward_dropout(hidden_states)
    hidden_states = self.fc2(hidden_states)
    hidden_states = self.dropout(hidden_states)
    hidden_states = residual + hidden_states
    hidden_states = self.final_layer_norm(hidden_states)
    if self.training:
        if torch.isinf(hidden_states).any() or torch.isnan(hidden_states).any():
            clamp_value = torch.finfo(hidden_states.dtype).max - 1000
            hidden_states = torch.clamp(hidden_states, min=-clamp_value, max=clamp_value)
    if output_attentions:
        return (hidden_states, attentions)
    return (hidden_states,)

Args:
hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
attention_mask (`torch.FloatTensor`): attention mask of size
`(batch, 1, target_len, source_len)` where padding elements are indicated by very large negative
values.
position_embeddings (`torch.FloatTensor`, *optional*):
Object queries (also called content embeddings), to be added to the hidden states.
output_attentions (`bool`, *optional*, defaults to `False`):
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
returned tensors for more detail.


def CacheFileSystem(self, path_spec, file_system):
    identifier = self._GetFileSystemCacheIdentifier(path_spec)
    self._file_system_cache.CacheObject(identifier, file_system)

Caches a file system object based on a path specification.

Args:
path_spec (PathSpec): path specification.
file_system (FileSystem): file system object.


def insert_tokenizer_in_auto_module(old_model_patterns: ModelPatterns, new_model_patterns: ModelPatterns):
    if old_model_patterns.tokenizer_class is None or new_model_patterns.tokenizer_class is None:
        return
    with open(TRANSFORMERS_PATH / 'models' / 'auto' / 'tokenization_auto.py', 'r', encoding='utf-8') as f:
        content = f.read()
    pattern_tokenizer = re.compile('^\\s*TOKENIZER_MAPPING_NAMES\\s*=\\s*OrderedDict\\b')
    lines = content.split('\n')
    idx = 0
    while not pattern_tokenizer.search(lines[idx]):
        idx += 1
    idx += 1
    while not lines[idx].startswith('TOKENIZER_MAPPING = _LazyAutoMapping'):
        if lines[idx].endswith(','):
            block = lines[idx]
        else:
            block = []
            while not lines[idx].startswith('        ),'):
                block.append(lines[idx])
                idx += 1
            block.append(lines[idx])
            block = '\n'.join(block)
        idx += 1
        if f'"{old_model_patterns.model_type}"' in block and old_model_patterns.tokenizer_class in block:
            break
    new_block = block.replace(old_model_patterns.model_type, new_model_patterns.model_type)
    new_block = new_block.replace(old_model_patterns.tokenizer_class, new_model_patterns.tokenizer_class)
    new_lines = lines[:idx] + [new_block] + lines[idx:]
    with open(TRANSFORMERS_PATH / 'models' / 'auto' / 'tokenization_auto.py', 'w', encoding='utf-8') as f:
        f.write('\n'.join(new_lines))

Add a tokenizer to the relevant mappings in the auto module.

Args:
old_model_patterns (`ModelPatterns`): The patterns for the old model.
new_model_patterns (`ModelPatterns`): The patterns for the new model.


def load_generation_config(gen_config_arg: Union[str, GenerationConfig]) -> GenerationConfig:
    if isinstance(gen_config_arg, GenerationConfig):
        gen_config = deepcopy(gen_config_arg)
    else:
        pretrained_model_name = Path(gen_config_arg) if isinstance(gen_config_arg, str) else gen_config_arg
        config_file_name = None
        if pretrained_model_name.is_file():
            config_file_name = pretrained_model_name.name
            pretrained_model_name = pretrained_model_name.parent
        elif pretrained_model_name.is_dir():
            pass
        else:
            pretrained_model_name = gen_config_arg
        gen_config = GenerationConfig.from_pretrained(pretrained_model_name, config_file_name)
    try:
        gen_config.validate(strict=True)
    except ValueError as exc:
        raise ValueError(str(exc) + '\n\nFix these issues to train your model.')
    return gen_config

Loads a `~generation.GenerationConfig` from the `Seq2SeqTrainingArguments.generation_config` arguments.

Args:
gen_config_arg (`str` or [`~generation.GenerationConfig]`):
`Seq2SeqTrainingArguments.generation_config` argument.

Returns:
A `~generation.GenerationConfig`.


def from_linearized(first, second, intersections):
    (s, t, success) = segment_intersection(first.start_node, first.end_node, second.start_node, second.end_node)
    bad_parameters = False
    if success:
        if (not (_helpers.in_interval(s, 0.0, 1.0) and _helpers.in_interval(t, 0.0, 1.0))):
            bad_parameters = True
    else:
        if ((first.error == 0.0) and (second.error == 0.0)):
            raise ValueError(_UNHANDLED_LINES)
        bad_parameters = True
        s = 0.5
        t = 0.5
    if bad_parameters:
        if (not convex_hull_collide(first.curve.nodes, second.curve.nodes)):
            return
    orig_s = (((1 - s) * first.curve.start) + (s * first.curve.end))
    orig_t = (((1 - t) * second.curve.start) + (t * second.curve.end))
    (refined_s, refined_t) = _intersection_helpers.full_newton(orig_s, first.curve.original_nodes, orig_t, second.curve.original_nodes)
    (refined_s, success) = _helpers.wiggle_interval(refined_s)
    if (not success):
        return
    (refined_t, success) = _helpers.wiggle_interval(refined_t)
    if (not success):
        return
    add_intersection(refined_s, refined_t, intersections)

Determine curve-curve intersection from pair of linearizations.

.. note::

This assumes that at least one of ``first`` and ``second`` is
not a line. The line-line case should be handled "early"
by :func:`check_lines`.

.. note::

This assumes the caller has verified that the bounding boxes
for ``first`` and ``second`` actually intersect.

If there is an intersection along the segments, adds that intersection
to ``intersections``. Otherwise, returns without doing anything.

Args:
first (Linearization): First curve being intersected.
second (Linearization): Second curve being intersected.
intersections (list): A list of existing intersections.

Raises:
ValueError: If ``first`` and ``second`` both have linearization error
of ``0.0`` (i.e. they are both lines). This is because this
function expects the caller to have used :func:`check_lines`
already.


def transform(self, path):
    if ((path is None) or (not path)):
        return None
    obj_parent_modules = path.split('.')
    objects = [obj_parent_modules.pop((- 1))]
    while True:
        try:
            parent_module_path = '.'.join(obj_parent_modules)
            parent_module = importlib.import_module(parent_module_path)
            break
        except ImportError:
            if (len(obj_parent_modules) == 1):
                raise ImportError(("No module named '%s'" % obj_parent_modules[0]))
            objects.insert(0, obj_parent_modules.pop((- 1)))
    current_object = parent_module
    for obj in objects:
        current_object = getattr(current_object, obj)
    return current_object

Transform a path into an actual Python object.

The path can be arbitrary long. You can pass the path to a package,
a module, a class, a function or a global variable, as deep as you
want, as long as the deepest module is importable through
``importlib.import_module`` and each object is obtainable through
the ``getattr`` method. Local objects will not work.

Args:
path (str): the dot-separated path of the object.

Returns:
object: the imported module or obtained object.


def list_leases(self, uuid=None):
    try:
        lease_files = os.listdir(self.path)
    except OSError as e:
        raise_from(LagoSubnetLeaseBadPermissionsException(self.path, e.strerror), e)
    leases = [self.create_lease_object_from_idx(lease_file.split('.')[0]) for lease_file in lease_files if (lease_file != LOCK_NAME)]
    if (not uuid):
        return leases
    else:
        return [lease for lease in leases if (lease.uuid == uuid)]

List current subnet leases

Args:
uuid(str): Filter the leases by uuid

Returns:
list of :class:~Lease: current leases


async def download_cot_artifact(chain, task_id, path):
    link = chain.get_link(task_id)
    log.debug('Verifying {} is in {} cot artifacts...'.format(path, task_id))
    if (not link.cot):
        log.warning('Chain of Trust for "{}" in {} does not exist. See above log for more details. Skipping download of this artifact'.format(path, task_id))
        return
    if (path not in link.cot['artifacts']):
        raise CoTError('path {} not in {} {} chain of trust artifacts!'.format(path, link.name, link.task_id))
    url = get_artifact_url(chain.context, task_id, path)
    loggable_url = get_loggable_url(url)
    log.info('Downloading Chain of Trust artifact:\n{}'.format(loggable_url))
    (await download_artifacts(chain.context, [url], parent_dir=link.cot_dir, valid_artifact_task_ids=[task_id]))
    full_path = link.get_artifact_full_path(path)
    for (alg, expected_sha) in link.cot['artifacts'][path].items():
        if (alg not in chain.context.config['valid_hash_algorithms']):
            raise CoTError('BAD HASH ALGORITHM: {}: {} {}!'.format(link.name, alg, full_path))
        real_sha = get_hash(full_path, hash_alg=alg)
        if (expected_sha != real_sha):
            raise CoTError('BAD HASH on file {}: {}: Expected {} {}; got {}!'.format(full_path, link.name, alg, expected_sha, real_sha))
        log.debug('{} matches the expected {} {}'.format(full_path, alg, expected_sha))
    return full_path

Download an artifact and verify its SHA against the chain of trust.

Args:
chain (ChainOfTrust): the chain of trust object
task_id (str): the task ID to download from
path (str): the relative path to the artifact to download

Returns:
str: the full path of the downloaded artifact

Raises:
CoTError: on failure.


def _format_line(headers, fields):

  assert len(fields) == len(headers), (fields, headers)
  fields = ["%2.4f" % field if isinstance(field, float) else str(field)
            for field in fields]
  return '  '.join(' ' * max(0, len(header) - len(field)) + field
                   for (header, field) in zip(headers, fields))

Format a line of a table.

Arguments:
headers: A list of strings that are used as the table headers.
fields: A list of the same length as `headers` where `fields[i]` is
the entry for `headers[i]` in this row. Elements can be of
arbitrary types. Pass `headers` to print the header row.

Returns:
A pretty string.


def eig_one_step(current_vector, learning_rate, vector_prod_fn):

  grad = 2*vector_prod_fn(current_vector)

  current_objective = tf.reshape(tf.matmul(tf.transpose(current_vector),
                                           grad) / 2., shape=())

  grad = grad - current_vector*tf.matmul(tf.transpose(current_vector), grad)
  grad_norm = tf.norm(grad)
  grad_norm_sq = tf.square(grad_norm)

  norm_grad = grad / grad_norm

  directional_second_derivative = (
      tf.reshape(2*tf.matmul(tf.transpose(norm_grad),
                             vector_prod_fn(norm_grad)),
                 shape=()))

  grad_m_grad = directional_second_derivative*grad_norm_sq / 2

  if directional_second_derivative / 2. < current_objective:
    return norm_grad

  if directional_second_derivative > 0.:
    step = -1. * grad_norm / directional_second_derivative
  else:

    if grad_norm_sq <= 1e-16:
      step = 0.0
    else:

      step = -2. * tf.reduce_sum(current_vector*grad) / grad_norm_sq

      gain = -(2 * tf.reduce_sum(current_vector*grad) +
               (step*step) * grad_m_grad)

      if gain < 0.:
        step = -learning_rate * grad_norm
  current_vector = current_vector + step * norm_grad
  return tf.nn.l2_normalize(current_vector)

Function that performs one step of gd (variant) for min eigen value.

Args:
current_vector: current estimate of the eigen vector with minimum eigen
value.
learning_rate: learning rate.
vector_prod_fn: function which returns product H*x, where H is a matrix for
which we computing eigenvector.

Returns:
updated vector after one step


def sg_regularizer_loss(scale=1.0):
    return (scale * tf.reduce_mean(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)))

r""" Get regularizer losss

Args:
scale: A scalar. A weight applied to regularizer loss


def RegisterDefinition(self, artifact_definition):

    artifact_definition_name = artifact_definition.name.lower()
    if artifact_definition_name in self._artifact_definitions:
      raise KeyError(
          'Artifact definition already set for name: {0:s}.'.format(
              artifact_definition.name))

    self._artifact_definitions[artifact_definition_name] = artifact_definition
    self._defined_artifact_names.add(artifact_definition.name)

    for source in artifact_definition.sources:
      if source.type_indicator == definitions.TYPE_INDICATOR_ARTIFACT_GROUP:
        self._artifact_name_references.update(source.names)

Registers an artifact definition.

Artifact definitions are identified based on their lower case name.

Args:
artifact_definition (ArtifactDefinition): an artifact definition.

Raises:
KeyError: if artifact definition is already set for the corresponding
name.


def __init__(self, unicodeHexValue, block):

        if unicodeHexValue < 0 or unicodeHexValue > 0x10FFFF:
            raise (ValueError, "numeric value outside Unicode range")
        self.unicodeHexValue = unicodeHexValue

        self.chr = chr(self.unicodeHexValue)
        self.name = unicodedata.name(self.chr)
        self.equivalents = {}
        self._block = block

Set up a unicode character.

Arguments:
unicodeHexValue -- an integer that should correspond to a
Unicode code point.
block -- the CharacterBlock this character belongs to.

Raises:
ValueError -- if unicodeHexValue is not a valid code point.


def list_json_files(directory, recursive=False):
    json_files = []
    for (top, dirs, files) in os.walk(directory):
        dirs.sort()
        paths = (os.path.join(top, f) for f in sorted(files))
        json_files.extend((x for x in paths if is_json(x)))
        if (not recursive):
            break
    return json_files

Return a list of file paths for JSON files within `directory`.

Args:
directory: A path to a directory.
recursive: If ``True``, this function will descend into all
subdirectories.

Returns:
A list of JSON file paths directly under `directory`.


def run(self):
    accounts = list(AWSAccount.get_all(include_disabled=False).values())
    for account in accounts:
        self.log.debug('Updating VPC Flow Logs for {}'.format(account))
        self.session = get_aws_session(account)
        role_arn = self.confirm_iam_role(account)
        for aws_region in AWS_REGIONS:
            try:
                vpc_list = VPC.get_all(account, aws_region).values()
                need_vpc_flow_logs = [x for x in vpc_list if (x.vpc_flow_logs_status != 'ACTIVE')]
                for vpc in need_vpc_flow_logs:
                    if self.confirm_cw_log(account, aws_region, vpc.id):
                        self.create_vpc_flow_logs(account, aws_region, vpc.id, role_arn)
                    else:
                        self.log.info('Failed to confirm log group for {}/{}'.format(account, aws_region))
            except Exception:
                self.log.exception('Failed processing VPCs for {}/{}.'.format(account, aws_region))
        db.session.commit()

Main entry point for the auditor worker.

Returns:
`None`


def multiply(x1, x2, output_shape=None, name=None):
    if (not isinstance(x2, Tensor)):
        return ScalarMultiplyOperation(x1, x2).outputs[0]
    with tf.name_scope(name, default_name='mul'):
        (x1, x2) = binary_arguments_to_tensors(x1, x2)
        return einsum([x1, x2], output_shape=_infer_binary_broadcast_shape(x1.shape, x2.shape, output_shape))

Binary multiplication with broadcasting.

Args:
x1: a Tensor
x2: a Tensor
output_shape: an optional Shape
name: an optional string
Returns:
a Tensor


def relu(x):
    if any_symbolic_tensors((x,)):
        return Relu().symbolic_call(x)
    return backend.nn.relu(x)

Rectified linear unit activation function.

It is defined as `f(x) = max(0, x)`.

Args:
x: Input tensor.

Returns:
A tensor with the same shape as `x`.

Example:

>>> x1 = keras.ops.convert_to_tensor([-1.0, 0.0, 1.0, 0.2])
>>> keras.ops.relu(x1)
array([0.0, 0.0, 1.0, 0.2], dtype=float32)


def replace_characters(self, text, characters, replacement=''):
    if (not characters):
        return text
    characters = ''.join(sorted(characters))
    if (characters in self._characters_regexes):
        characters_regex = self._characters_regexes[characters]
    else:
        characters_regex = re.compile(('[%s]' % re.escape(characters)))
        self._characters_regexes[characters] = characters_regex
    return characters_regex.sub(replacement, text)

Remove characters from text.

Removes custom characters from input text or replaces them
with a string if specified.

Args:
text: The text to be processed.
characters: Characters that will be replaced.
replacement: New text that will replace the custom characters.

Returns:
The text without the given characters.


def regroup(values, wrap_class=values_lib.PerReplica, always_wrap=False):
    v0 = values[0]
    if isinstance(v0, list):
        for v in values[1:]:
            assert isinstance(v, list)
            assert len(v) == len(v0), 'len(v) == %d, len(v0) == %d, v: %s, v0: %s' % (len(v), len(v0), v, v0)
        return [regroup(tuple((v[i] for v in values)), wrap_class, always_wrap) for i in range(len(v0))]
    if isinstance(v0, tuple):
        for v in values[1:]:
            assert isinstance(v, tuple)
            assert len(v) == len(v0), f'Values to regroup had different lengths: len(v) == {len(v)}, len(v0) == {len(v0)}, v: {v}, v0: {v0}'
        regrouped_tuple = tuple((regroup(tuple((v[i] for v in values)), wrap_class, always_wrap) for i in range(len(v0))))
        if hasattr(v0, '_fields'):
            assert hasattr(v0, '_make')
            return v0._make(regrouped_tuple)
        else:
            return regrouped_tuple
    if isinstance(v0, abc.Mapping):
        v0keys = v0.keys()
        for v in values[1:]:
            assert isinstance(v, abc.Mapping), 'v[0]: %r  v[i]: %r' % (v0, v)
            assert set(v.keys()) == set(v0keys), 'v[0].keys: %s  v[i].keys: %s' % (set(v0keys), set(v.keys()))
        return type(v0)({key: regroup(tuple((v[key] for v in values)), wrap_class, always_wrap) for key in v0keys})
    same_id = True
    for v in values[1:]:
        if v is not v0:
            same_id = False
            break
    if same_id and isinstance(v0, values_lib.DistributedVariable):
        return v0
    if same_id and (not always_wrap) and (value_container(v0) is v0):
        return v0
    if not isinstance(v0, resource_variable_ops._UnreadVariable) and value_container(v0) is not v0:
        assert not isinstance(v0, values_lib.MirroredVariable), 'ids = %s, values = %s' % ([id(v) for v in values], values)
        distributed_container = value_container(v0)
        assert distributed_container is not None
        for v in values[1:]:
            assert distributed_container is value_container(v)
        return distributed_container
    return wrap_class(values)

Makes a nest per-replica into a nest of PerReplica/Mirrored values.

Args:
values: Values to regroup
wrap_class: Class that `values` be wrapped in.
always_wrap: Always wrap the `values` in `wrap_class` even if the values
are the same except for DistributeVariable.
Returns:
Wrapped `values`.


def authenticate(self, username, password):

        if self.config.get('LDAP_BIND_DIRECT_CREDENTIALS'):
            result = self.authenticate_direct_credentials(username, password)

        elif not self.config.get('LDAP_ALWAYS_SEARCH_BIND') and \
                self.config.get('LDAP_USER_RDN_ATTR') == \
                self.config.get('LDAP_USER_LOGIN_ATTR'):

            result = self.authenticate_direct_bind(username, password)
        else:

            result = self.authenticate_search_bind(username, password)

        return result

An abstracted authentication method. Decides whether to perform a
direct bind or a search bind based upon the login attribute configured
in the config.

Args:
username (str): Username of the user to bind
password (str): User's password to bind with.

Returns:
AuthenticationResponse


def get_metrics_for_resource(access_token, subscription_id, resource_group, resource_provider, resource_type, resource_name):
    endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/resourceGroups/', resource_group, '/providers/', resource_provider, '/', resource_type, '/', resource_name, '/providers/microsoft.insights', '/metrics?api-version=', INSIGHTS_PREVIEW_API])
    return do_get(endpoint, access_token)

Get the monitoring metrics for a resource.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.
resource_group (str): Azure resource group name.
resource_type (str): Type of resource.
resource_name (str): Name of resource.

Returns:
HTTP response. JSON body of resource metrics.


def get_posts(self, num=None, tag=None, private=False):

		posts = self.posts

		if not private:
			posts = [post for post in posts if post.public]

		if tag:
			posts = [post for post in posts if tag in post.tags]

		if num:
			return posts[:num]
		return posts

Get all the posts added to the blog.

Args:
num (int): Optional. If provided, only return N posts (sorted by date,
most recent first).
tag (Tag): Optional. If provided, only return posts that have a
specific tag.
private (bool): By default (if False), private posts are not included.
If set to True, private posts will also be included.


def load_steps(working_dir=None, steps_dir=None, step_file=None,
               step_list=None):

    if steps_dir is not None:
        step_files = glob.glob(os.path.join(steps_dir, '*.cwl'))
    elif step_file is not None:
        step_files = [step_file]
    elif step_list is not None:
        step_files = []
        for path in step_list:
            if os.path.isdir(path):
                step_files += glob.glob(os.path.join(path, '*.cwl'))
            else:
                step_files.append(path)
    else:
        step_files = []

    if working_dir is not None:
        step_files = sort_loading_order(step_files)

    steps = {}
    for f in step_files:
        if working_dir is not None:

            if not working_dir == os.path.dirname(f) and not is_url(f):
                copied_file = os.path.join(working_dir, os.path.basename(f))
                shutil.copy2(f, copied_file)
                f = copied_file

        try:
            s = Step(f)
            steps[s.name] = s
        except (NotImplementedError, ValidationException,
                PackedWorkflowException) as e:
            logger.warning(e)

    return steps

Return a dictionary containing Steps read from file.

Args:
steps_dir (str, optional): path to directory containing CWL files.
step_file (str, optional): path or http(s) url to a single CWL file.
step_list (list, optional): a list of directories, urls or local file
paths to CWL files or directories containing CWL files.

Return:
dict containing (name, Step) entries.


def moveRel(xOffset=None, yOffset=None, duration=0.0, tween=linear, pause=None, _pause=True):
    _failSafeCheck()
    (xOffset, yOffset) = _unpackXY(xOffset, yOffset)
    _mouseMoveDrag('move', None, None, xOffset, yOffset, duration, tween)
    _autoPause(pause, _pause)

Moves the mouse cursor to a point on the screen, relative to its current
position.

The x and y parameters detail where the mouse event happens. If None, the
current mouse position is used. If a float value, it is rounded down. If
outside the boundaries of the screen, the event happens at edge of the
screen.

Args:
x (int, float, None, tuple, optional): How far left (for negative values) or
right (for positive values) to move the cursor. 0 by default. If tuple, this is used for x and y.
y (int, float, None, optional): How far up (for negative values) or
down (for positive values) to move the cursor. 0 by default.
duration (float, optional): The amount of time it takes to move the mouse
cursor to the new xy coordinates. If 0, then the mouse cursor is moved
instantaneously. 0.0 by default.
tween (func, optional): The tweening function used if the duration is not
0. A linear tween is used by default. See the tweens.py file for
details.

Returns:
None


def Kdp(scatterer):

    if (scatterer.thet0 != scatterer.thet) or \
        (scatterer.phi0 != scatterer.phi):

        raise ValueError("A forward scattering geometry is needed to " + \
            "compute the specific differential phase.")

    S = scatterer.get_S()
    return 1e-3 * (180.0/np.pi) * scatterer.wavelength * (S[1,1]-S[0,0]).real

Specific differential phase (K_dp) for the current setup.

Args:
scatterer: a Scatterer instance.

Returns:
K_dp [deg/km].

NOTE: This only returns the correct value if the particle diameter and
wavelength are given in [mm]. The scatterer object should be set to
forward scattering geometry before calling this function.


def _ircounts2radiance(counts, scale, offset):
    rad = ((counts - offset) / scale)
    return rad.clip(min=0)

Convert IR counts to radiance

Reference: [IR].

Args:
counts: Raw detector counts
scale: Scale [mW-1 m2 cm sr]
offset: Offset [1]

Returns:
Radiance [mW m-2 cm-1 sr-1]


def random_hermitian_matrix(num_qubits):
    dim = 2 ** num_qubits
    val_range = 2
    random_real = tf.cast(tf.random.uniform([dim, dim], -val_range, val_range), tf.complex128)
    random_imag = 1j * tf.cast(tf.random.uniform([dim, dim], -val_range, val_range), tf.complex128)
    random_matrix = random_real + random_imag
    return random_matrix + tf.linalg.adjoint(random_matrix)

Returns a random Hermitian matrix.

Uses the property that A + A* is Hermitian for any matrix A.

Args:
num_qubits: Number of qubits on which the matrix acts.


def select_char_code_table(self, table):

        tables = {'standard': 0,
                  'eastern european': 1,
                  'western european': 2,
                  'spare': 3
                  }
        if table in tables:
            self.send(chr(27)+'t'+chr(tables[table]))
        else:
            raise RuntimeError('Invalid char table.')

Select character code table, from tree built in ones.

Args:
table: The desired character code table. Choose from 'standard', 'eastern european', 'western european', and 'spare'
Returns:
None
Raises:
RuntimeError: Invalid chartable.


def mols_to_file(mols, path):
    with open(path, 'w') as f:
        f.write(mols_to_text(mols))

Save molecules to the SDFile format file

Args:
mols: list of molecule objects
path: file path to save


def create(labels=None, **kw):

    if labels is not None:
        kw[u'labels'] = encoding.PyValueToMessage(MetricValue.LabelsValue,
                                                  labels)
    return MetricValue(**kw)

Constructs a new metric value.

This acts as an alternate to MetricValue constructor which
simplifies specification of labels.  Rather than having to create
a MetricValue.Labels instance, all that's necessary to specify the
required string.

Args:
labels (dict([string, [string]]):
**kw: any other valid keyword args valid in the MetricValue constructor

Returns
:class:`MetricValue`: the created instance


def __init__(self, batch_env, step, is_training, should_log, config):

    self._batch_env = batch_env
    self._step = step
    self._is_training = is_training
    self._should_log = should_log
    self._config = config
    self._observ_filter = parts.StreamingNormalize(
        self._batch_env.observ[0], center=True, scale=True, clip=5,
        name='normalize_observ')
    self._reward_filter = parts.StreamingNormalize(
        self._batch_env.reward[0], center=False, scale=True, clip=10,
        name='normalize_reward')
    self._use_gpu = self._config.use_gpu and utility.available_gpus()
    policy_params, state = self._initialize_policy()
    self._initialize_memory(policy_params)

    with tf.device('/gpu:0' if self._use_gpu else '/cpu:0'):
      self._optimizer = self._config.optimizer(self._config.learning_rate)
    self._penalty = tf.Variable(
        self._config.kl_init_penalty, False, dtype=tf.float32)

    with tf.variable_scope('ppo_temporary'):
      with tf.device('/gpu:0'):
        if state is None:
          self._last_state = None
        else:
          var_like = lambda x: tf.Variable(lambda: tf.zeros_like(x), False)
          self._last_state = tools.nested.map(var_like, state)

    with tf.variable_scope('ppo_temporary'):
      self._last_action = tf.Variable(
          tf.zeros_like(self._batch_env.action), False, name='last_action')
      self._last_policy = tools.nested.map(
          lambda x: tf.Variable(tf.zeros_like(x[:, 0], False)), policy_params)

Create an instance of the PPO algorithm.

Args:
batch_env: In-graph batch environment.
step: Integer tensor holding the current training step.
is_training: Boolean tensor for whether the algorithm should train.
should_log: Boolean tensor for whether summaries should be returned.
config: Object containing the agent configuration as attributes.


def __init__(self, filename, content_generator=None, content_length=None):

    precondition.AssertType(filename, Text)
    self.filename = filename
    self.content_length = content_length

    if content_generator is None:
      raise ValueError("content_generator can't be None")
    self.content_generator = content_generator

ApiBinaryStream constructor.

Args:
filename: A file name to be used by the browser when user downloads the
file.
content_generator: A generator that yields byte chunks (of any size) to
be streamed to the user.
content_length: The length of the stream, if known upfront.

Raises:
ValueError: if content_generator is None.


def _create_w_objective(m, X, R):

    genes, clusters = m.shape
    cells = X.shape[1]
    R1 = R.reshape((genes, 1)).dot(np.ones((1, cells)))
    def objective(w):

        w = w.reshape((m.shape[1], X.shape[1]))
        d = m.dot(w)+eps
        return np.sum((X + R1)*np.log(d + R1) - X*np.log(d))/genes
    def deriv(w):

        w2 = w.reshape((m.shape[1], X.shape[1]))
        d = m.dot(w2)+eps
        temp = X/d
        temp2 = (X+R1)/(d+R1)
        m1 = m.T.dot(temp2)
        m2 = m.T.dot(temp)
        deriv = m1 - m2
        return deriv.flatten()/genes
    return objective, deriv

Creates an objective function and its derivative for W, given M and X (data)

Args:
m (array): genes x clusters
X (array): genes x cells
R (array): 1 x genes


def all_distances(coords1, coords2):

    c1 = np.array(coords1)
    c2 = np.array(coords2)
    z = (c1[:, None, :] - c2[None, :, :]) ** 2
    return np.sum(z, axis=-1) ** 0.5

Returns the distances between two lists of coordinates

Args:
coords1: First set of cartesian coordinates.
coords2: Second set of cartesian coordinates.

Returns:
2d array of cartesian distances. E.g the distance between
coords1[i] and coords2[j] is distances[i,j]


def write_index(self, overwrite: bool=False, mock: bool=False) -> None:
    write_if_allowed(self.index_filename, self.index_content(), overwrite=overwrite, mock=mock)

Writes the index file, if permitted.

Args:
overwrite: allow existing files to be overwritten?
mock: pretend to write, but don't


def tournament_number2name(self, number):

        tournaments = self.get_tournaments()
        d = {t['tournament']: t['name'] for t in tournaments}
        return d.get(number, None)

Translate tournament number to tournament name.

Args:
number (int): tournament number to translate

Returns:
name (str): name of the tournament or `None` if unknown.

Examples:
>>> NumerAPI().tournament_number2name(4)
'delta'
>>> NumerAPI().tournament_number2name(99)
None


def build_ellipse(X, Y):
    x_mean = np.mean(X)
    y_mean = np.mean(Y)
    cov_matrix = np.cov(np.vstack((X, Y)))
    (U, s, V) = linalg.svd(cov_matrix, full_matrices=False)
    chi_95 = np.sqrt(4.61)
    width = ((np.sqrt(cov_matrix[0][0]) * chi_95) * 2)
    height = ((np.sqrt(cov_matrix[1][1]) * chi_95) * 2)
    eigenvector = V.T[0]
    angle = np.arctan((eigenvector[1] / eigenvector[0]))
    return (x_mean, y_mean, width, height, angle)

Construct ellipse coordinates from two arrays of numbers.

Args:
X (1D array_like)
Y (1D array_like)

Returns:
float: The mean of `X`.
float: The mean of `Y`.
float: The width of the ellipse.
float: The height of the ellipse.
float: The angle of orientation of the ellipse.


def record(*fields):

    @six.add_metaclass(_RecordMetaClass)
    class RecordType(object):
        _record_sentinel = True
        _record_fields = fields
    return RecordType

Constructs a type that can be extended to create immutable, value types.

Examples:
A typical declaration looks like::

class MyRecord(record('a', ('b', 1))):
pass

The above would make a sub-class of ``collections.namedtuple`` that was named ``MyRecord`` with
a constructor that had the ``b`` field set to 1 by default.

Note:
This uses meta-class machinery to rewrite the inheritance hierarchy.
This is done in order to make sure that the underlying ``namedtuple`` instance is
bound to the right type name and to make sure that the synthetic class that is generated
to enable this machinery is not enabled for sub-classes of a user's record class.

Args:
fields (list[str | (str, any)]): A sequence of str or pairs that


def remove_site(name):

    current_sites = list_sites()

    if name not in current_sites:
        log.debug('Site already absent: %s', name)
        return True

    ps_cmd = ['Remove-WebSite', '-Name', r"'{0}'".format(name)]

    cmd_ret = _srvmgr(ps_cmd)

    if cmd_ret['retcode'] != 0:
        msg = 'Unable to remove site: {0}\nError: {1}' \
              ''.format(name, cmd_ret['stderr'])
        raise CommandExecutionError(msg)

    log.debug('Site removed successfully: %s', name)
    return True

Delete a website from IIS.

Args:
name (str): The IIS site name.

Returns:
bool: True if successful, otherwise False

.. note::

This will not remove the application pool used by the site.

CLI Example:

.. code-block:: bash

salt '*' win_iis.remove_site name='My Test Site'


def get_likelihood(self, uni_matrix):

        uni_dim = uni_matrix.shape[1]
        num_edge = len(self.edges)
        values = np.zeros([1, num_edge])
        new_uni_matrix = np.empty([uni_dim, uni_dim])

        for i in range(num_edge):
            edge = self.edges[i]
            value, left_u, right_u = edge.get_likelihood(uni_matrix)
            new_uni_matrix[edge.L, edge.R] = left_u
            new_uni_matrix[edge.R, edge.L] = right_u
            values[0, i] = np.log(value)

        return np.sum(values), new_uni_matrix

Compute likelihood of the tree given an U matrix.

Args:
uni_matrix(numpy.array): univariate matrix to evaluate likelihood on.

Returns:
tuple[float, numpy.array]:
likelihood of the current tree, next level conditional univariate matrix


def updateParams(self, newvalues):

        for (param, value) in newvalues.items():
            if param not in self.model.freeparams:
                raise RuntimeError("Can't handle param: {0}".format(
                        param))
        if newvalues:
            self.model.updateParams(newvalues)
            self._updateInternals()
            self._paramsarray = None

Update model parameters and re-compute likelihoods.

This method is the **only** acceptable way to update model
parameters. The likelihood is re-computed as needed
by this method.

Args:
`newvalues` (dict)
A dictionary keyed by param name and with value as new
value to set. Each parameter name must either be a
valid model parameter (in `model.freeparams`).


class ByteRewriter:
    LEAF = '[LEAF]'

    def __init__(self, rewriting_rules: Union[str, Dict[str, str]]):
        if isinstance(rewriting_rules, str):
            with open(rewriting_rules, 'r') as f:
                rewriting_rules = json.load(f)
        elif not isinstance(rewriting_rules, dict):
            raise ValueError(f'rewriting_rules should be either a path to json file or a dict, got {type(rewriting_rules)}')
        self.hash_tree = self.construct_hash_tree(rewriting_rules)
        reverse_rewriting_rules = {v: k for k, v in rewriting_rules.items()}
        self.reverse_hash_tree = self.construct_hash_tree(reverse_rewriting_rules)

    def add_leaf(self, hash_tree: Dict[str, Union[dict, List[str]]], byte_in_sequence: str, byte_out_sequence: str):

        byte_in_list = byte_in_sequence.split(' ')
        byte_out_list = byte_out_sequence.split(' ')
        tree_pointer = hash_tree
        for b in byte_in_list:
            if b not in tree_pointer:
                tree_pointer[b] = {}
            tree_pointer = tree_pointer[b]
        tree_pointer[self.LEAF] = byte_out_list

    def construct_hash_tree(self, rewriting_rules: Dict[str, str]) -> Dict[str, Union[dict, List[str]]]:

        hash_tree = defaultdict(dict)
        for b in (f'{x:02x}' for x in range(256)):
            hash_tree[b][self.LEAF] = [b]
        for in_sequence, out_sequence in rewriting_rules.items():
            self.add_leaf(hash_tree, in_sequence, out_sequence)
        return hash_tree

    def search_hash_tree(self, byte_sequence: List[str]) -> Union[None, List[str]]:

        tree_pointer = self.hash_tree
        for b in byte_sequence:
            if b in tree_pointer:
                tree_pointer = tree_pointer[b]
            else:
                return None
        return tree_pointer[self.LEAF]

    def rewrite_bytes(self, in_bytes: List[str], reverse=False) -> List[str]:

        out_bytes = []
        b_start = 0
        b_end = 0
        while b_start < len(in_bytes):
            tree_pointer = self.hash_tree if not reverse else self.reverse_hash_tree
            for j in range(b_start, len(in_bytes)):
                b = in_bytes[j]
                if b in tree_pointer:
                    tree_pointer = tree_pointer[b]
                elif j == b_start:
                    cur_leaf = [b]
                    b_end = j
                    break
                else:
                    break
                if self.LEAF in tree_pointer:
                    cur_leaf = tree_pointer[self.LEAF]
                    b_end = j
            out_bytes.extend(cur_leaf)
            b_start = b_end + 1
        return out_bytes

Byte rewriter class for MyT5 tokenizer.
This class is used to rewrite bytes using a hash tree. The hash tree is constructed from a set of rewriting rules.

Args:
rewriting_rules (`str` or `Dict[str, str]`):
A path to a json file containing the rewriting rules or a dictionary containing the rewriting rules.


def load_compositors(self, sensor_names):
    comps = {}
    mods = {}
    for sensor_name in sensor_names:
        if (sensor_name not in self.compositors):
            self.load_sensor_composites(sensor_name)
        if (sensor_name in self.compositors):
            comps[sensor_name] = DatasetDict(self.compositors[sensor_name].copy())
            mods[sensor_name] = self.modifiers[sensor_name].copy()
    return (comps, mods)

Load all compositor configs for the provided sensors.

Args:
sensor_names (list of strings): Sensor names that have matching
``sensor_name.yaml`` config files.

Returns:
(comps, mods): Where `comps` is a dictionary:

sensor_name -> composite ID -> compositor object

And `mods` is a dictionary:

sensor_name -> modifier name -> (modifier class,
modifiers options)

Note that these dictionaries are copies of those cached in
this object.


def DeleteSignedBinary(binary_urn,
                       token = None):

  if _ShouldUseLegacyDatastore():
    try:
      aff4.FACTORY.Open(
          binary_urn, aff4_type=aff4.AFF4Stream, mode="r", token=token)
    except aff4.InstantiationError:
      raise SignedBinaryNotFoundError(binary_urn)
    aff4.FACTORY.Delete(binary_urn, token=token)

  if data_store.RelationalDBEnabled():
    try:
      data_store.REL_DB.ReadSignedBinaryReferences(
          _SignedBinaryIDFromURN(binary_urn))
    except db.UnknownSignedBinaryError:
      if _ShouldUseLegacyDatastore():

        return
      else:
        raise SignedBinaryNotFoundError(binary_urn)
    data_store.REL_DB.DeleteSignedBinaryReferences(
        _SignedBinaryIDFromURN(binary_urn))

Deletes the binary with the given urn from the datastore.

Args:
binary_urn: RDFURN that serves as a unique identifier for the binary.
token: ACL token to use with the legacy (non-relational) datastore.

Raises:
SignedBinaryNotFoundError: If the signed binary does not exist.


def appliance_device_snmp_v3_trap_destinations(self):
    if (not self.__appliance_device_snmp_v3_trap_destinations):
        self.__appliance_device_snmp_v3_trap_destinations = ApplianceDeviceSNMPv3TrapDestinations(self.__connection)
    return self.__appliance_device_snmp_v3_trap_destinations

Gets the ApplianceDeviceSNMPv3TrapDestinations API client.

Returns:
ApplianceDeviceSNMPv3TrapDestinations:


def mark_flags_as_mutual_exclusive(flag_names, required=False, flag_values=_flagvalues.FLAGS):
    for flag_name in flag_names:
        if (flag_values[flag_name].default is not None):
            warnings.warn('Flag --{} has a non-None default value. That does not make sense with mark_flags_as_mutual_exclusive, which checks whether the listed flags have a value other than None.'.format(flag_name))

    def validate_mutual_exclusion(flags_dict):
        flag_count = sum((1 for val in flags_dict.values() if (val is not None)))
        if ((flag_count == 1) or ((not required) and (flag_count == 0))):
            return True
        raise _exceptions.ValidationError('{} one of ({}) must have a value other than None.'.format(('Exactly' if required else 'At most'), ', '.join(flag_names)))
    register_multi_flags_validator(flag_names, validate_mutual_exclusion, flag_values=flag_values)

Ensures that only one flag among flag_names is not None.

Important note: This validator checks if flag values are None, and it does not
distinguish between default and explicit values. Therefore, this validator
does not make sense when applied to flags with default values other than None,
including other false values (e.g. False, 0, '', []). That includes multi
flags with a default value of [] instead of None.

Args:
flag_names: [str], names of the flags.
required: bool. If true, exactly one of the flags must have a value other
than None. Otherwise, at most one of the flags can have a value other
than None, and it is valid for all of the flags to be None.
flag_values: flags.FlagValues, optional FlagValues instance where the flags
are defined.


def __init__(self, client=None, workingdir='/workingdir'):

        self.client = self.connect_to_docker(client)
        self.default_wdir = workingdir
        self.hostname = self.client.base_url

Initialization:

Args:
client (docker.Client): a docker-py client. If not passed, we will try to create the
client from the job's environmental varaibles
workingdir (str): default working directory to create in the containers


def find_priority_list(py_files):
    dependencies = map_dependencies(py_files)
    ordered_files = topological_sort(dependencies)
    return (ordered_files, dependencies)

Given a list of modular files, sorts them by topological order. Modular models that DON'T depend on other modular
models will be higher in the topological order.

Args:
py_files: List of paths to the modular files

Returns:
A tuple with the ordered files (list) and their dependencies (dict)


def _assert_same_base_type(items, expected_type=None):
    original_expected_type = expected_type
    mismatch = False
    for item in items:
        if item is not None:
            item_type = item.dtype.base_dtype
            if not expected_type:
                expected_type = item_type
            elif expected_type != item_type:
                mismatch = True
                break
    if mismatch:
        expected_type = original_expected_type
        original_item_str = None
        for item in items:
            if item is not None:
                item_type = item.dtype.base_dtype
                if not expected_type:
                    expected_type = item_type
                    original_item_str = item.name if hasattr(item, 'name') else str(item)
                elif expected_type != item_type:
                    raise ValueError('%s, type=%s, must be of the same type (%s)%s.' % (item.name if hasattr(item, 'name') else str(item), item_type, expected_type, ' as %s' % original_item_str if original_item_str else ''))
        return expected_type
    else:
        return expected_type

Asserts all items are of the same base type.

Args:
items: List of graph items (e.g., `Variable`, `Tensor`, `SparseTensor`,
`Operation`, or `IndexedSlices`). Can include `None` elements, which
will be ignored.
expected_type: Expected type. If not specified, assert all items are
of the same base type.

Returns:
Validated type, or none if neither expected_type nor items provided.

Raises:
ValueError: If any types do not match.


def get(self, blocking=True):

    if self.closed:
      raise PoolAlreadyClosedError("Connection pool is already closed.")

    if not self.limiter.acquire(blocking=blocking):
      return None
    c = None

    try:
      c = self.idle_conns.pop()
    except IndexError:

      try:
        c = self.connect_func()
      except Exception:
        self.limiter.release()
        raise
    return _ConnectionProxy(self, c)

Gets a connection.

Args:
blocking: Whether to block when max_size connections are already in use.
If false, may return None.

Returns:
A connection to the database.

Raises:
PoolAlreadyClosedError: if close() method was already called on
this pool.


def ts_to_str(jwt_dict):
    d = ts_to_dt(jwt_dict)
    for (k, v) in list(d.items()):
        if isinstance(v, datetime.datetime):
            d[k] = v.isoformat().replace('T', ' ')
    return d

Convert timestamps in JWT to human readable dates.

Args:
jwt_dict: dict
JWT with some keys containing timestamps.

Returns:
dict: Copy of input dict where timestamps have been replaced with human readable
dates.


def _RegisterCredentialsMethod(method, position=None):
    if (position is None):
        position = len(_CREDENTIALS_METHODS)
    else:
        position = min(position, len(_CREDENTIALS_METHODS))
    _CREDENTIALS_METHODS.insert(position, method)
    return method

Register a new method for fetching credentials.

This new method should be a function with signature:
client_info, **kwds -> Credentials or None
This method can be used as a decorator, unless position needs to
be supplied.

Note that method must *always* accept arbitrary keyword arguments.

Args:
method: New credential-fetching method.
position: (default: None) Where in the list of methods to
add this; if None, we append. In all but rare cases,
this should be either 0 or None.
Returns:
method, for use as a decorator.


def find_backend(line: str) -> Optional[str]:
    if _re_test_backend.search(line) is None:
        return None
    backends = [b[0] for b in _re_backend.findall(line)]
    backends.sort()
    return '_and_'.join(backends)

Find one (or multiple) backend in a code line of the init.

Args:
line (`str`): A code line in an init file.

Returns:
Optional[`str`]: If one (or several) backend is found, returns it. In the case of multiple backends (the line
contains `if is_xxx_available() and `is_yyy_available()`) returns all backends joined on `_and_` (so
`xxx_and_yyy` for instance).


def create(self, reference, document_data):

        write_pbs = _helpers.pbs_for_create(reference._document_path, document_data)
        self._add_write_pbs(write_pbs)

Add a "change" to this batch to create a document.

If the document given by ``reference`` already exists, then this
batch will fail when :meth:`commit`-ed.

Args:
reference (~.firestore_v1beta1.document.DocumentReference): A
document reference to be created in this batch.
document_data (dict): Property names and values to use for
creating a document.


def gray2bgr(img):
    img = (img[(..., None)] if (img.ndim == 2) else img)
    out_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    return out_img

Convert a grayscale image to BGR image.

Args:
img (ndarray or str): The input image.

Returns:
ndarray: The converted BGR image.


def associated_stream(self):
    if (not self.important):
        raise InternalError('You may only call autocopied_stream on when DataStream.important is True', stream=self)
    if (self.stream_id >= DataStream.ImportantSystemStorageStart):
        stream_type = DataStream.BufferedType
    else:
        stream_type = DataStream.OutputType
    return DataStream(stream_type, self.stream_id, True)

Return the corresponding output or storage stream for an important system input.

Certain system inputs are designed as important and automatically
copied to output streams without requiring any manual interaction.

This method returns the corresponding stream for an important system
input.  It will raise an InternalError unlesss the self.important
property is True.

Returns:
DataStream: The corresponding output or storage stream.

Raises:
InternalError: If this stream is not marked as an important system input.


def from_bytes(b):
    if (len(b) != 64):
        raise ValueError('from_bytes: Signature length != 64.')
    r = int.from_bytes(b[0:32], 'big')
    s = int.from_bytes(b[32:64], 'big')
    return Signature(r, s)

Extracts the r and s components from a byte string.

Args:
b (bytes): A 64-byte long string. The first 32 bytes are
extracted as the r component and the second 32 bytes
are extracted as the s component.

Returns:
Signature: A Signature object.

Raises:
ValueError: If signature is incorrect length


def remote_command(task: Task, command: str) -> Result:

    client = task.host.get_connection("paramiko", task.nornir.config)
    connection_state = task.host.get_connection_state("paramiko")

    chan = client.get_transport().open_session()

    if connection_state["ssh_forward_agent"]:
        AgentRequestHandler(chan)

    chan.exec_command(command)

    with chan.makefile() as f:
        stdout = f.read().decode()
    with chan.makefile_stderr() as f:
        stderr = f.read().decode()

    exit_status_code = chan.recv_exit_status()

    if exit_status_code:
        raise CommandError(command, exit_status_code, stdout, stderr)

    result = stderr if stderr else stdout
    return Result(result=result, host=task.host, stderr=stderr, stdout=stdout)

Executes a command remotely on the host

Arguments:
command (``str``): command to execute

Returns:
Result object with the following attributes set:
* result (``str``): stderr or stdout
* stdout (``str``): stdout
* stderr (``str``): stderr

Raises:
:obj:`nornir.core.exceptions.CommandError`: when there is a command error


def pretty_plot_two_axis(x, y1, y2, xlabel=None, y1label=None, y2label=None, width=8, height=None, dpi=300):
    import palettable.colorbrewer.diverging
    colors = palettable.colorbrewer.diverging.RdYlBu_4.mpl_colors
    c1 = colors[0]
    c2 = colors[(- 1)]
    golden_ratio = ((math.sqrt(5) - 1) / 2)
    if (not height):
        height = int((width * golden_ratio))
    import matplotlib.pyplot as plt
    width = 12
    labelsize = int((width * 3))
    ticksize = int((width * 2.5))
    styles = ['-', '--', '-.', '.']
    (fig, ax1) = plt.subplots()
    fig.set_size_inches((width, height))
    if dpi:
        fig.set_dpi(dpi)
    if isinstance(y1, dict):
        for (i, (k, v)) in enumerate(y1.items()):
            ax1.plot(x, v, c=c1, marker='s', ls=styles[(i % len(styles))], label=k)
        ax1.legend(fontsize=labelsize)
    else:
        ax1.plot(x, y1, c=c1, marker='s', ls='-')
    if xlabel:
        ax1.set_xlabel(xlabel, fontsize=labelsize)
    if y1label:
        ax1.set_ylabel(y1label, color=c1, fontsize=labelsize)
    ax1.tick_params('x', labelsize=ticksize)
    ax1.tick_params('y', colors=c1, labelsize=ticksize)
    ax2 = ax1.twinx()
    if isinstance(y2, dict):
        for (i, (k, v)) in enumerate(y2.items()):
            ax2.plot(x, v, c=c2, marker='o', ls=styles[(i % len(styles))], label=k)
        ax2.legend(fontsize=labelsize)
    else:
        ax2.plot(x, y2, c=c2, marker='o', ls='-')
    if y2label:
        ax2.set_ylabel(y2label, color=c2, fontsize=labelsize)
    ax2.tick_params('y', colors=c2, labelsize=ticksize)
    return plt

Variant of pretty_plot that does a dual axis plot. Adapted from matplotlib
examples. Makes it easier to create plots with different axes.

Args:
x (np.ndarray/list): Data for x-axis.
y1 (dict/np.ndarray/list): Data for y1 axis (left). If a dict, it will
be interpreted as a {label: sequence}.
y2 (dict/np.ndarray/list): Data for y2 axis (right). If a dict, it will
be interpreted as a {label: sequence}.
xlabel (str): If not None, this will be the label for the x-axis.
y1label (str): If not None, this will be the label for the y1-axis.
y2label (str): If not None, this will be the label for the y2-axis.
width (float): Width of plot in inches. Defaults to 8in.
height (float): Height of plot in inches. Defaults to width * golden
ratio.
dpi (int): Sets dot per inch for figure. Defaults to 300.

Returns:
matplotlib.pyplot


def FromTXOutputsConfirmed(outputs):
    uns = UnspentCoinState()
    uns.Items = ([0] * len(outputs))
    for i in range(0, len(outputs)):
        uns.Items[i] = int(CoinState.Confirmed)
    return uns

Get unspent outputs from a list of transaction outputs.

Args:
outputs (list): of neo.Core.TX.Transaction.TransactionOutput items.

Returns:
UnspentCoinState:


def helper_add(access_token, ck_id, path, body):

    full_path = ''.join([path, "('", ck_id, "')"])
    full_path_encoded = urllib.parse.quote(full_path, safe='')
    endpoint = ''.join([ams_rest_endpoint, full_path_encoded])
    return do_ams_put(endpoint, full_path_encoded, body, access_token, "json_only", "1.0;NetFx")

Helper Function to add strings to a URL path.

Args:
access_token (str): A valid Azure authentication token.
ck_id (str): A CK ID.
path (str): A URL Path.
body (str): A Body.

Returns:
HTTP response. JSON body.


def set_status(self, name: str = None):

        game = None
        if name:
            game = {
                'name': name
            }
        payload = {
            'op': WebSocketEvent.STATUS_UPDATE.value,
            'd': {
                'game': game,
                'status': 'online',
                'afk': False,
                'since': 0.0
            }
        }
        data = json.dumps(payload, indent=2)
        self.logger.debug(f'Sending status update payload: {data}')
        self._ws.send(data)

Updates the bot's status

This is used to get the game that the bot is "playing" or to clear it.
If you want to set a game, pass a name; if you want to clear it, either
call this method without the optional ``name`` parameter or explicitly
pass ``None``.

Args:
name: the game's name, or None


def convert_image_to_example_proto(tensor: tf.Tensor) -> tf.train.Example:
    serialized_non_scalar = tf.io.serialize_tensor(tensor)
    feature_of_bytes = tf.train.Feature(bytes_list=tf.train.BytesList(value=[serialized_non_scalar.numpy()]))
    features_for_example = {'image': feature_of_bytes}
    example_proto = tf.train.Example(features=tf.train.Features(feature=features_for_example))
    return example_proto

This method performs the following:
1. Accepts the tensor as input
2. Serializes the tensor into bytes and pass it through
tf.train.Feature
3. Pass the serialized tensor feature using tf.train.Example
Proto to the RunInference transform.

Args:
tensor: A TF tensor.
Returns:
example_proto: A tf.train.Example containing serialized tensor.


def lint(self, content, **kwargs):
    post_data = {'content': content}
    data = self.http_post('/ci/lint', post_data=post_data, **kwargs)
    return ((data['status'] == 'valid'), data['errors'])

Validate a gitlab CI configuration.

Args:
content (txt): The .gitlab-ci.yml content
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabVerifyError: If the validation could not be done

Returns:
tuple: (True, []) if the file is valid, (False, errors(list))
otherwise


def indent(self, space=4):
    if (not isinstance(space, int)):
        raise TypeError('space must be an int')
    if (space < 0):
        raise ValueError('space must be a non-negative integer')
    space = (' ' * space)
    o = []
    l = 0
    for c in self.newick():
        if (c == '('):
            o.append('(\n')
            l += 1
            o.append((space * l))
        elif (c == ')'):
            o.append('\n')
            l -= 1
            o.append((space * l))
            o.append(')')
        elif (c == ','):
            o.append(',\n')
            o.append((space * l))
        else:
            o.append(c)
    return ''.join(o)

Return an indented Newick string, just like ``nw_indent`` in Newick Utilities

Args:
``space`` (``int``): The number of spaces a tab should equal

Returns:
``str``: An indented Newick string


def __init__(self, file_pattern, batch_size=1, buffer_size=1, parallelism=1, shift_ratio=0, seed=0, name=None, batches=None, compression_type=None):
    self._batch_size = batch_size
    if batches is not None:
        self._batch_size *= batches
    self._batches = batches
    self._file_pattern = file_pattern
    self._buffer_size = buffer_size
    self._parallelism = parallelism
    self._shift_ratio = shift_ratio
    self._seed = seed
    self._name = name
    self._compression_type = python_io.TFRecordCompressionType.NONE
    if compression_type is not None:
        self._compression_type = compression_type

Constructs a RecordInput Op.

Args:
file_pattern: File path to the dataset, possibly containing wildcards.
All matching files will be iterated over each epoch.
batch_size: How many records to return at a time.
buffer_size: The maximum number of records the buffer will contain.
parallelism: How many reader threads to use for reading from files.
shift_ratio: What percentage of the total number files to move the start
file forward by each epoch.
seed: Specify the random number seed used by generator that randomizes
records.
name: Optional name for the operation.
batches: None by default, creating a single batch op. Otherwise specifies
how many batches to create, which are returned as a list when
`get_yield_op()` is called. An example use case is to split processing
between devices on one computer.
compression_type: The type of compression for the file. Currently ZLIB and
GZIP are supported. Defaults to none.

Raises:
ValueError: If one of the arguments is invalid.


def write_json(self, path, contents, message):

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

Write json to disk.

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


def start_app(self, bundle_id):

        idevicedebug = must_look_exec('idevicedebug')

        kwargs = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE}
        if sys.platform != 'darwin':
            kwargs['close_fds'] = True
        return subprocess.Popen([idevicedebug, "--udid", self.udid, 'run', bundle_id], **kwargs)

Start app by bundle_id
Args:
- bundle_id(string): ex com.netease.my
Returns:
idevicedebug subprocess instance


def get_student_enrollments(self):
    resp = self.requester.get(urljoin(self.base_url, self.enrollment_url))
    resp.raise_for_status()
    return Enrollments(resp.json())

Returns an Enrollments object with the user enrollments

Returns:
Enrollments: object representing the student enrollments


def read(self, vals):

        i = 0
        if len(vals[i]) == 0:
            self.typical_or_extreme_period_name = None
        else:
            self.typical_or_extreme_period_name = vals[i]
        i += 1
        if len(vals[i]) == 0:
            self.typical_or_extreme_period_type = None
        else:
            self.typical_or_extreme_period_type = vals[i]
        i += 1
        if len(vals[i]) == 0:
            self.period_start_day = None
        else:
            self.period_start_day = vals[i]
        i += 1
        if len(vals[i]) == 0:
            self.period_end_day = None
        else:
            self.period_end_day = vals[i]
        i += 1

Read values.

Args:
vals (list): list of strings representing values


def play_random(env, steps):

    try:
        done = True
        progress = tqdm(range(steps))
        for _ in progress:
            if done:
                _ = env.reset()
            action = env.action_space.sample()
            _, reward, done, info = env.step(action)
            progress.set_postfix(reward=reward, info=info)
            env.render()
    except KeyboardInterrupt:
        pass

    env.close()

Play the environment making uniformly random decisions.

Args:
env (gym.Env): the initialized gym environment to play
steps (int): the number of random steps to take

Returns:
None


def ToTsvExcel(self, columns_order=None, order_by=()):
    csv_result = self.ToCsv(columns_order, order_by, separator='\t')
    if (not isinstance(csv_result, six.text_type)):
        csv_result = csv_result.decode('utf-8')
    return csv_result.encode('UTF-16LE')

Returns a file in tab-separated-format readable by MS Excel.

Returns a file in UTF-16 little endian encoding, with tabs separating the
values.

Args:
columns_order: Delegated to ToCsv.
order_by: Delegated to ToCsv.

Returns:
A tab-separated little endian UTF16 file representing the table.


def require_debian_packages(packages: List[str]) -> None:

    present = are_debian_packages_installed(packages)
    missing_packages = [k for k, v in present.items() if not v]
    if missing_packages:
        missing_packages.sort()
        msg = (
            "Debian packages are missing, as follows. Suggest:\n\n"
            "sudo apt install {}".format(" ".join(missing_packages))
        )
        log.critical(msg)
        raise ValueError(msg)

Ensure specific packages are installed under Debian.

Args:
packages: list of packages

Raises:
ValueError: if any are missing


def dispatch_event(event):

    try:
      if event.http_verb == enums.HTTPVerbs.GET:
        requests.get(event.url, params=event.params, timeout=REQUEST_TIMEOUT).raise_for_status()
      elif event.http_verb == enums.HTTPVerbs.POST:
        requests.post(
          event.url, data=json.dumps(event.params), headers=event.headers, timeout=REQUEST_TIMEOUT
        ).raise_for_status()

    except request_exception.RequestException as error:
      logging.error('Dispatch event failed. Error: %s' % str(error))

Dispatch the event being represented by the Event object.

Args:
event: Object holding information about the request to be dispatched to the Optimizely backend.


def __init__(self, linter_name, path, msg, line_nr=None, col=None):

        if line_nr:
            line_nr = int(line_nr)
        if col:
            col = int(col)
        self._linter_name = linter_name
        self.path = path
        self.line_nr = line_nr
        self.msg = msg
        self.col = col

Optionally set all attributes.

Args:
path (str): Relative file path.
line (int): Line number.
msg (str): Explanation of what is wrong.
col (int): Column where the problem begins.


def get_configuration(head, update, head_source=None):
    head_source = (head_source or get_head_source(head))
    update_source = get_acquisition_source(update)
    if ((not is_arxiv_and_publisher(head_source, update_source)) and is_manual_merge(head, update)):
        return ManualMergeOperations
    if (head_source == 'arxiv'):
        if (update_source == 'arxiv'):
            return ArxivOnArxivOperations
        else:
            return PublisherOnArxivOperations
    elif (update_source == 'arxiv'):
        return ArxivOnPublisherOperations
    else:
        return PublisherOnPublisherOperations

This function return the right configuration for the inspire_merge
function in according to the given sources. Both parameters can not be None.

Params:
head(dict): the HEAD record
update(dict): the UPDATE record
head_source(string): the source of the HEAD record

Returns:
MergerConfigurationOperations: an object containing
the rules needed to merge HEAD and UPDATE


def _get_segments(self, start, request_size):
    if (not request_size):
        return []
    end = (start + request_size)
    futures = []
    while (request_size > self._max_request_size):
        futures.append(self._get_segment(start, self._max_request_size))
        request_size -= self._max_request_size
        start += self._max_request_size
    if (start < end):
        futures.append(self._get_segment(start, (end - start)))
    return [fut.get_result() for fut in futures]

Get segments of the file from Google Storage as a list.

A large request is broken into segments to avoid hitting urlfetch
response size limit. Each segment is returned from a separate urlfetch.

Args:
start: start offset to request. Inclusive. Have to be within the
range of the file.
request_size: number of bytes to request.

Returns:
A list of file segments in order


def _serve_plugins_listing(self, request):
    response = {}
    for plugin in self._plugins:
        start = time.time()
        response[plugin.plugin_name] = plugin.is_active()
        elapsed = (time.time() - start)
        logger.info('Plugin listing: is_active() for %s took %0.3f seconds', plugin.plugin_name, elapsed)
    return http_util.Respond(request, response, 'application/json')

Serves an object mapping plugin name to whether it is enabled.

Args:
request: The werkzeug.Request object.

Returns:
A werkzeug.Response object.


def review_score(self, reviewer, product):

        return self._g.retrieve_review(reviewer, product).score

Find a review score from a given reviewer to a product.

Args:
reviewer: Reviewer i.e. an instance of :class:`ria.bipartite.Reviewer`.
product: Product i.e. an instance of :class:`ria.bipartite.Product`.

Returns:
A review object representing the review from the reviewer to the product.


def create_attachment(cls, session, attachment):
    return super(Conversations, cls).create(session, attachment, endpoint_override='/attachments.json', out_type=Attachment)

Create an attachment.

An attachment must be sent to the API before it can be used in a
thread. Use this method to create the attachment, then use the
resulting hash when creating a thread.

Note that HelpScout only supports attachments of 10MB or lower.

Args:
session (requests.sessions.Session): Authenticated session.
attachment (helpscout.models.Attachment): The attachment to be
created.

Returns:
helpscout.models.Attachment: The newly created attachment (hash
property only). Use this hash when associating the attachment with
a new thread.


def forward(self, hidden_states):
    hidden_states = hidden_states.transpose(1, 2)
    hidden_states = self.pointwise_conv1(hidden_states)
    hidden_states = nn.functional.glu(hidden_states, dim=1)
    hidden_states = self.depthwise_conv(hidden_states)
    hidden_states = self.norm(hidden_states)
    hidden_states = hidden_states * torch.sigmoid(hidden_states)
    hidden_states = self.pointwise_conv2(hidden_states)
    return hidden_states.transpose(1, 2)

Compute convolution module.

Args:
hidden_states (`torch.Tensor` of shape `(batch, time, channels)`): Input tensor.

Returns:
`torch.Tensor`: Output tensor of shape `(batch, time, channels)`.


def are_debian_packages_installed(packages: List[str]) -> Dict[(str, bool)]:
    assert (len(packages) >= 1)
    require_executable(DPKG_QUERY)
    args = ([DPKG_QUERY, '-W', '-f=${Package} ${Status}\n'] + packages)
    completed_process = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
    encoding = sys.getdefaultencoding()
    stdout = completed_process.stdout.decode(encoding)
    stderr = completed_process.stderr.decode(encoding)
    present = OrderedDict()
    for line in stdout.split('\n'):
        if line:
            words = line.split()
            assert (len(words) >= 2)
            package = words[0]
            present[package] = ('installed' in words[1:])
    for line in stderr.split('\n'):
        if line:
            words = line.split()
            assert (len(words) >= 2)
            package = words[(- 1)]
            present[package] = False
    log.debug('Debian package presence: {}', present)
    return present

Check which of a list of Debian packages are installed, via ``dpkg-query``.

Args:
packages: list of Debian package names

Returns:
dict: mapping from package name to boolean ("present?")


class PatchingSpec:
    o: Any
    name: str
    custom_op: Callable
    orig_op: Optional[Callable] = None
    op_wrapper: Optional[Callable] = None

Data class that holds patching specifications.

Args:
o: Module / object where the op to patch is located
name: Name of the op to monkey patch
custom_op: Custom op that patches the original op
orig_op: Original op that is being patched
op_wrapper: Wrapper (optional) that wraps both the original and custom ops.
It is useful for ops that are class or static methods for instance.


def make_noise_surface(dims=DEFAULT_DIMS, blur=10, seed=None):

    if seed is not None:
        np.random.seed(seed)

    return gaussian_filter(np.random.normal(size=dims), blur)

Makes a surface by generating random noise and blurring it.

Args:
dims (pair): the dimensions of the surface to create
blur (float): the amount of Gaussian blur to apply
seed (int): a random seed to use (optional)

Returns:
surface: A surface.


def _open_tracing_interface(self, conn_id, callback):
    try:
        handle = self._find_handle(conn_id)
        services = self._connections[handle]['services']
    except (ValueError, KeyError):
        callback(conn_id, self.id, False, 'Connection closed unexpectedly before we could open the streaming interface')
        return
    self._command_task.async_command(['_enable_tracing', handle, services], self._on_interface_finished, {'connection_id': conn_id, 'callback': callback})

Enable the debug tracing interface for this IOTile device

Args:
conn_id (int): the unique identifier for the connection
callback (callback): Callback to be called when this command finishes
callback(conn_id, adapter_id, success, failure_reason)


def get_sequence_properties(self, clean_seq=False, representatives_only=True):

        for g in tqdm(self.genes):
            g.protein.get_sequence_properties(clean_seq=clean_seq, representative_only=representatives_only)

Run Biopython ProteinAnalysis and EMBOSS pepstats to summarize basic statistics of all protein sequences.
Results are stored in the protein's respective SeqProp objects at ``.annotations``

Args:
representative_only (bool): If analysis should only be run on the representative sequences


def members(self):
    resp = self._rtm_client.get('v1/current_team.members?all=true')
    if resp.is_fail():
        raise RTMServiceError('Failed to get members of current team', resp)
    return resp.data['result']

Gets members of current team

Returns:
list of User

Throws:
RTMServiceError when request failed


def _find_docstring_line_for_no_body(self, start):

        tracked = sorted(list(self._tokenized_triple_quotes.keys()))

        for i in tracked:
            if min(start, i) == start:
                return i
        return None

Find the docstring associated with a definition with no body
in the node.

In these cases, the provided start and end line number for that
element are the same, so we must get the docstring based on the
sequential position of known docstrings.

Args:
start: the row where the class / function starts.

Returns:
int: the row number where the docstring is found.


def ParseOptions(cls, options, output_module):

    if not hasattr(output_module, 'SetServerInformation'):
      raise errors.BadConfigObject('Unable to set server information.')

    server = cls._ParseStringOption(
        options, 'server', default_value=cls._DEFAULT_SERVER)
    port = cls._ParseNumericOption(
        options, 'port', default_value=cls._DEFAULT_PORT)

    output_module.SetServerInformation(server, port)

Parses and validates options.

Args:
options (argparse.Namespace): parser options.
output_module (OutputModule): output module to configure.

Raises:
BadConfigObject: when the output module object does not have the
SetServerInformation method.


def _split_ir_into_match_steps(pruned_ir_blocks):
    output = []
    current_tuple = None
    for block in pruned_ir_blocks:
        if isinstance(block, OutputSource):
            continue
        elif isinstance(block, root_block_types):
            if (current_tuple is not None):
                output.append(current_tuple)
            current_tuple = (block,)
        elif isinstance(block, (CoerceType, Filter, MarkLocation)):
            current_tuple += (block,)
        else:
            raise AssertionError(u'Unexpected block type when converting to MATCH query: {} {}'.format(block, pruned_ir_blocks))
    if (current_tuple is None):
        raise AssertionError(u'current_tuple was unexpectedly None: {}'.format(pruned_ir_blocks))
    output.append(current_tuple)
    return [_per_location_tuple_to_step(x) for x in output]

Split a list of IR blocks into per-location MATCH steps.

Args:
pruned_ir_blocks: list of IR basic block objects that have gone through a lowering step.

Returns:
list of MatchStep namedtuples, each of which contains all basic blocks that correspond
to a single MATCH step.


def setup(self, disk_name, project, turbinia_zone):
    if ((project is None) or (turbinia_zone is None)):
        self.state.add_error('project or turbinia_zone are not all specified, bailing out', critical=True)
        return
    self.disk_name = disk_name
    self.project = project
    self.turbinia_zone = turbinia_zone
    try:
        turbinia_config.LoadConfig()
        self.turbinia_region = turbinia_config.TURBINIA_REGION
        self.instance = turbinia_config.PUBSUB_TOPIC
        if (turbinia_config.PROJECT != self.project):
            self.state.add_error('Specified project {0:s} does not match Turbinia configured project {1:s}. Use gcp_turbinia_import recipe to copy the disk into the same project.'.format(self.project, turbinia_config.PROJECT), critical=True)
            return
        self._output_path = tempfile.mkdtemp()
        self.client = turbinia_client.TurbiniaClient()
    except TurbiniaException as e:
        self.state.add_error(e, critical=True)
        return

Sets up the object attributes.

Args:
disk_name (string): Name of the disk to process
project (string): The project containing the disk to process
turbinia_zone (string): The zone containing the disk to process


def PyParseIntCast(string, location, tokens):

  for index, token in enumerate(tokens):
    try:
      tokens[index] = int(token)
    except ValueError:
      logger.error('Unable to cast [{0:s}] to an int, setting to 0'.format(
          token))
      tokens[index] = 0

  for key in tokens.keys():
    try:
      tokens[key] = int(tokens[key], 10)
    except ValueError:
      logger.error(
          'Unable to cast [{0:s} = {1:d}] to an int, setting to 0'.format(
              key, tokens[key]))
      tokens[key] = 0

Return an integer from a string.

This is a pyparsing callback method that converts the matched
string into an integer.

The method modifies the content of the tokens list and converts
them all to an integer value.

Args:
string (str): original string.
location (int): location in the string where the match was made.
tokens (list[str]): extracted tokens, where the string to be converted
is stored.


def encode_chunk(dataframe):

    csv_buffer = six.StringIO()
    dataframe.to_csv(
        csv_buffer,
        index=False,
        header=False,
        encoding="utf-8",
        float_format="%.15g",
        date_format="%Y-%m-%d %H:%M:%S.%f",
    )

    body = csv_buffer.getvalue()
    if isinstance(body, bytes):
        body = body.decode("utf-8")
    body = body.encode("utf-8")
    return six.BytesIO(body)

Return a file-like object of CSV-encoded rows.

Args:
dataframe (pandas.DataFrame): A chunk of a dataframe to encode


def __init__(self, level, message, message_id, timestamp=None, now_reference=None):

        self.level = level
        self.message = message
        self.count = 1
        self.id = message_id

        if timestamp is None:
            self.created = monotonic()
        elif now_reference is None:
            self.created = timestamp
        else:

            now = monotonic()
            adj = now - now_reference
            self.created = timestamp + adj

            if self.created > now:
                self.created = now

Constructor.

Args:
level (int): The message importance
message (string): The message contents
message_id (int): A unique id for the message
timestamp (float): An optional monotonic value in seconds for when the message was created
now_reference (float): If timestamp is not relative to monotonic() as called from this
module then this should be now() as seen by whoever created the timestamp.