def nested_update(d, u):
    for (k, v) in list(u.items()):
        if isinstance(v, collections.Mapping):
            r = nested_update(d.get(k, {}), v)
            d[k] = r
        else:
            d[k] = u[k]
    return d

Merge two nested dicts.

Nested dicts are sometimes used for representing various recursive structures. When
updating such a structure, it may be convenient to present the updated data as a
corresponding recursive structure. This function will then apply the update.

Args:
d: dict
dict that will be updated in-place. May or may not contain nested dicts.

u: dict
dict with contents that will be merged into ``d``. May or may not contain
nested dicts.


def __init__(
      self, cipher_mode=None, initialization_vector=None, key=None, **kwargs):

    if not key:
      raise ValueError('Missing key.')

    cipher_mode = self.ENCRYPTION_MODES.get(cipher_mode, None)
    if cipher_mode is None:
      raise ValueError('Unsupported cipher mode: {0!s}'.format(cipher_mode))

    if cipher_mode != AES.MODE_ECB and not initialization_vector:

      raise ValueError('Missing initialization vector.')

    super(AESDecrypter, self).__init__()
    if cipher_mode == AES.MODE_ECB:
      self._aes_cipher = AES.new(key, mode=cipher_mode)
    else:
      self._aes_cipher = AES.new(
          key, IV=initialization_vector, mode=cipher_mode)

Initializes a decrypter.

Args:
cipher_mode (Optional[str]): cipher mode.
initialization_vector (Optional[bytes]): initialization vector.
key (Optional[bytes]): key.
kwargs (dict): keyword arguments depending on the decrypter.

Raises:
ValueError: when key is not set, block cipher mode is not supported,
or initialization_vector is required and not set.


def _format_ase2clusgeo(obj, all_atomtypes=None):
    totalAN = len(obj)
    if (all_atomtypes is not None):
        atomtype_set = set(all_atomtypes)
    else:
        atomtype_set = set(obj.get_atomic_numbers())
    atomtype_lst = np.sort(list(atomtype_set))
    n_atoms_per_type_lst = []
    pos_lst = []
    for atomtype in atomtype_lst:
        condition = (obj.get_atomic_numbers() == atomtype)
        pos_onetype = obj.get_positions()[condition]
        n_onetype = pos_onetype.shape[0]
        pos_lst.append(pos_onetype)
        n_atoms_per_type_lst.append(n_onetype)
    typeNs = n_atoms_per_type_lst
    Ntypes = len(n_atoms_per_type_lst)
    atomtype_lst
    Apos = np.concatenate(pos_lst).ravel()
    return (Apos, typeNs, Ntypes, atomtype_lst, totalAN)

Takes an ase Atoms object and returns numpy arrays and integers
which are read by the internal clusgeo. Apos is currently a flattened
out numpy array

Args:
obj():
all_atomtypes():
sort():


def __init__(self, *others):

        selectors = list()
        heads = collections.defaultdict(set)
        for other in others:
            if isinstance(other, MultiFieldSelector):
                for head, tail in other.heads.iteritems():
                    heads[head].add(tail)
            elif isinstance(other, FieldSelector):
                selectors.append(other)
            else:
                selectors.append(self.FieldSelector(other))

        for selector in selectors:
            chain = selector.selectors
            if chain:
                head = chain[0]
                tail = self.FieldSelector(chain[1:]) if len(chain) > 1 else all
                heads[head].add(tail)
            else:
                heads[None].add(all)

        self.heads = dict(
            (head, all if all in tail else MultiFieldSelector(*tail))
            for head, tail in heads.iteritems()
        ) if None not in heads or heads[None] is not all else {None: all}

        head_types = set(type(x) for x in self.heads)
        self.has_int = int in head_types or long in head_types
        self.has_string = any(issubclass(x, basestring) for x in head_types)
        self.has_none = types.NoneType in head_types
        self.complete = self.has_none and self.heads[None] is all
        if self.has_none and (self.has_int or self.has_string):

            raise ValueError(
                "MultiFieldSelector cannot yet specify a list and a hash/"
                "object at the same level: %r" % self.heads.keys()
            )

Returns a MultiFieldSelector based on combining the passed-in
FieldSelector and MultiFieldSelector objects.

args:

``*others=``\ *FieldSelector*\ \|\ *iterable*

Each argument is interpreted as either a FieldSelector, or a
FieldSelector constructor.


def modify_module(channel, module_name, module_state):
    gui = ui_embed.UI(channel, '{} updated'.format(module_name), '{} is now {}'.format(module_name, ('activated' if module_state else 'deactivated')), modulename=modulename)
    return gui

Creates an embed UI containing the module modified message

Args:
channel (discord.Channel): The Discord channel to bind the embed to
module_name (str): The name of the module that was updated
module_state (bool): The current state of the module

Returns:
embed: The created embed


def to_sigproc_keyword(keyword, value=None):

    keyword = bytes(keyword)

    if value is None:
        return np.int32(len(keyword)).tostring() + keyword
    else:
        dtype = header_keyword_types[keyword]

        dtype_to_type = {b'<l'  : np.int32,
                         b'str' : str,
                         b'<d'  : np.float64,
                         b'angle' : to_sigproc_angle}

        value_dtype = dtype_to_type[dtype]

        if value_dtype is str:
            return np.int32(len(keyword)).tostring() + keyword + np.int32(len(value)).tostring() + value
        else:
            return np.int32(len(keyword)).tostring() + keyword + value_dtype(value).tostring()

Generate a serialized string for a sigproc keyword:value pair

If value=None, just the keyword will be written with no payload.
Data type is inferred by keyword name (via a lookup table)

Args:
keyword (str): Keyword to write
value (None, float, str, double or angle): value to write to file

Returns:
value_str (str): serialized string to write to file.


def run(self, blocking: bool=True):

        if not self._run_control_loop:
            err = ("`run` called, but not using the internal control loop. Use"
                   " `start` instead")

            raise RuntimeError(err)

        self._setup()
        self._heartbeat_reciever.start()

        if blocking:
            return self.loop.start()
        else:
            self._run_thread = _threading.Thread(target=self.loop.start,
                                                 daemon=True)

            self._thread.run()

Run the internal control loop.
Args:
blocking (bool): Defaults to `True`. If set to `False`, will
intialize a thread to run the control loop.
Raises:
RuntimeError: If called and not using the internal control loop
via `self._run_control_loop`, set in the intializer of the
class


def has_cwd(state, dir, incorrect_msg='Your current working directory should be `{{dir}}`. Use `cd {{dir}}` to navigate there.'):
    expr = "[[ $PWD == '{}' ]]".format(dir)
    _msg = state.build_message(incorrect_msg, fmt_kwargs={'dir': dir})
    has_expr_exit_code(state, expr, output='0', incorrect_msg=_msg)
    return state

Check whether the student is in the expected directory.

This check is typically used before using ``has_expr_output()``
to make sure the student didn't navigate somewhere else.

Args:
state: State instance describing student and solution code. Can be omitted if used with ``Ex()``.
dir: Directory that the student should be in. Always use the absolute path.
incorrect_msg: If specified, this overrides the automatically generated message in
case the student is not in the expected directory.

:Example:

If you want to be sure that the student is in ``/home/repl/my_dir``: ::

Ex().has_cwd('/home/repl/my_dir')


def call_for_each_replica(self, fn, args=(), kwargs=None):
    distribute_lib._require_cross_replica_or_default_context_extended(self)
    if kwargs is None:
        kwargs = {}
    map_fn = functools.partial(dtensor_util.convert_inputs_to_dtensor, mesh=self._mesh)
    d_args = nest.map_structure(map_fn, args)
    d_kwargs = nest.map_structure(map_fn, kwargs)
    with self._container_strategy().scope():
        with dtensor_util.DTensorReplicaContext(self._container_strategy()):
            dtensor_result = fn(*d_args, **d_kwargs)
    return nest.map_structure(dtensor_util.DTensorDistributedValue, dtensor_result)

Run `fn` once per replica.

This is a method that expected by the strategy base class in its `run()`.

Args:
fn: function to run (will be run once per replica).
args: Tuple or list with positional arguments for `fn`.
kwargs: Dict with keyword arguments for `fn`.

Returns:
Merged return value of `fn` across all replicas.


def torque_on(self):
    data = []
    data.append(10)
    data.append(self.servoid)
    data.append(RAM_WRITE_REQ)
    data.append(TORQUE_CONTROL_RAM)
    data.append(1)
    data.append(96)
    send_data(data)

Enable the torques of Herkulex

In this mode, position control and velocity control
will work.

Args:
none


def dump_session_params(path):

    var = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
    var.extend(tf.get_collection(tf.GraphKeys.MODEL_VARIABLES))

    assert len(set(var)) == len(var), "TRAINABLE and MODEL variables have duplication!"
    gvars = set([k.name for k in tf.global_variables()])
    var = [v for v in var if v.name in gvars]
    result = {}
    for v in var:
        result[v.name] = v.eval()
    save_chkpt_vars(result, path)

Dump value of all TRAINABLE + MODEL variables to a dict, and save as
npz format (loadable by :func:`sessinit.get_model_loader`).

Args:
path(str): the file name to save the parameters. Must ends with npz.


def create(self, resource):

        return self.service.create(
            resource, self.url_prefix, self.auth, self.session,
            self.session_send_opts)

Create the given resource.

Args:
resource (intern.resource.boss.BossResource): Create a data model object with attributes matching those of the resource.

Returns:
(intern.resource.boss.BossResource): Returns resource of type requested on success.

Raises:
requests.HTTPError on failure.


def print_info(self, buf=None, format_=FileFormat.yaml,
                   skip_attributes=None, include_release=False):

        data = self.validated_data().copy()

        data.pop("config", None)
        if self.config:
            if isinstance(self, Package):
                config_dict = self.data.get("config")
            else:
                config_dict = self.parent.data.get("config")
            data["config"] = config_dict

        if not include_release:
            skip_attributes = list(skip_attributes or []) + list(package_release_keys)

        buf = buf or sys.stdout
        dump_package_data(data, buf=buf, format_=format_,
                          skip_attributes=skip_attributes)

Print the contents of the package.

Args:
buf (file-like object): Stream to write to.
format_ (`FileFormat`): Format to write in.
skip_attributes (list of str): List of attributes to not print.
include_release (bool): If True, include release-related attributes,
such as 'timestamp' and 'changelog'


def testBasic(self, count, batch_size, drop_remainder, num_parallel_calls):
    components = (np.arange(7), np.array([[1, 2, 3]]) * np.arange(7)[:, np.newaxis], np.array(37.0) * np.arange(7))

    def _map_fn(x, y, z):
        return (math_ops.square(x), math_ops.square(y), math_ops.square(z))
    dataset = dataset_ops.Dataset.from_tensor_slices(components).map(_map_fn).repeat(count).batch(batch_size, drop_remainder, num_parallel_calls)
    get_next = self.getNext(dataset)
    if drop_remainder:
        dim0 = batch_size
    else:
        dim0 = None
    self.assertEqual([ts.as_list() for ts in nest.flatten(dataset_ops.get_legacy_output_shapes(dataset))], [[dim0] + list(c.shape[1:]) for c in components])
    num_full_batches = count * 7
    for i in range(num_full_batches):
        result = self.evaluate(get_next())
        for component, result_component in zip(components, result):
            for j in range(batch_size):
                self.assertAllEqual(component[(i * batch_size + j) % 7] ** 2, result_component[j])
    if not drop_remainder and count * 7 % batch_size > 0:
        result = self.evaluate(get_next())
        for component, result_component in zip(components, result):
            for j in range(count * 7 % batch_size):
                self.assertAllEqual(component[(num_full_batches * batch_size + j) % 7] ** 2, result_component[j])
    with self.assertRaises(errors.OutOfRangeError):
        result = self.evaluate(get_next())

Tests the batch dataset logic for various input configurations.

Args:
count: the number of input elements
batch_size: the batch size
drop_remainder: whether a smaller batch size should be produced if batch
size does not divide number of inputs evenly
num_parallel_calls: the number batches to process asynchronously in
parallel


def process_openxml_file(filename: str,
                         print_good: bool,
                         delete_if_bad: bool) -> None:

    print_bad = not print_good
    try:
        file_good = is_openxml_good(filename)
        file_bad = not file_good
        if (print_good and file_good) or (print_bad and file_bad):
            print(filename)
        if delete_if_bad and file_bad:
            log.warning("Deleting: {}", filename)
            os.remove(filename)
    except Exception as e:

        log.critical("Uncaught error in subprocess: {!r}\n{}", e,
                     traceback.format_exc())
        raise

Prints the filename of, or deletes, an OpenXML file depending on whether
it is corrupt or not.

Args:
filename: filename to check
print_good: if ``True``, then prints the filename if the file
appears good.
delete_if_bad: if ``True``, then deletes the file if the file
appears corrupt.


def ensure_tensor_on_device(self, **inputs):
    return self._ensure_tensor_on_device(inputs, self.device)

Ensure PyTorch tensors are on the specified device.

Args:
inputs (keyword arguments that should be `torch.Tensor`, the rest is ignored):
The tensors to place on `self.device`.
Recursive on lists **only**.

Return:
`Dict[str, torch.Tensor]`: The same as `inputs` but on the proper device.


def insert(self, **fields):

        if self.conflict_target or self.conflict_action:
            compiler = self._build_insert_compiler([fields])
            rows = compiler.execute_sql(return_id=True)

            pk_field_name = self.model._meta.pk.name
            return rows[0][pk_field_name]

        return super().create(**fields).pk

Creates a new record in the database.

This allows specifying custom conflict behavior using .on_conflict().
If no special behavior was specified, this uses the normal Django create(..)

Arguments:
fields:
The fields of the row to create.

Returns:
The primary key of the record that was created.


def find_next(self, *strings, **kwargs):
    start = kwargs.pop('start', None)
    keys_only = kwargs.pop('keys_only', False)
    staht = (start if (start is not None) else self.cursor)
    for (start, stop) in [(staht, len(self)), (0, staht)]:
        for i in range(start, stop):
            for string in strings:
                if (string in self[i]):
                    tup = (i, self[i])
                    self.cursor = (i + 1)
                    if keys_only:
                        return i
                    return tup

From the editor's current cursor position find the next instance of the
given string.

Args:
strings (iterable): String or strings to search for

Returns:
tup (tuple): Tuple of cursor position and line or None if not found

Note:
This function cycles the entire editor (i.e. cursor to length of
editor to zero and back to cursor position).


def get_qubit_los(self, user_lo_config):

        try:
            _q_los = self.default_qubit_los.copy()
        except KeyError:
            raise PulseError('Qubit default frequencies not exist.')

        for channel, lo_freq in user_lo_config.qubit_lo_dict().items():
            _q_los[channel.index] = lo_freq

        if _q_los == self.default_qubit_los:
            return None
        return _q_los

Embed default qubit LO frequencies from backend and format them to list object.
If configured lo frequency is the same as default, this method returns `None`.

Args:
user_lo_config (LoConfig): A dictionary of LOs to format.

Returns:
list: A list of qubit LOs.

Raises:
PulseError: when LO frequencies are missing.


def GetSysFeeAmountByHeight(self, height):
    hash = self.GetBlockHash(height)
    return self.GetSysFeeAmount(hash)

Get the system fee for the specified block.

Args:
height (int): block height.

Returns:
int:


def __init__(self, orig_image, dpi, save_image):

        self._shreds = None
        self.orig_img = orig_image

        self.save_image = save_image

        self._fg_mask = None
        self._shreds = None

        if dpi is None:
            self.res_x, self.res_y = self._guess_dpi()
        else:
            self.res_x, self.res_y = dpi

Initializes a Sheet instance.

Args:
orig_image: cv.Mat instance with the original sheet image.
dpi: optional (x resolution, y resolution) tuple or None.
If set to None, will try to guess dpi.
save_image: A callback to save debug images with args (name, img)


def getindex(self, child, recursive=True, ignore=True):
    for (i, c) in enumerate(self.data):
        if (c is child):
            return i
    if recursive:
        for (i, c) in enumerate(self.data):
            if (ignore is True):
                try:
                    if (not c.auth):
                        continue
                except AttributeError:
                    pass
            elif ignore:
                doignore = False
                for e in ignore:
                    if (e is True):
                        try:
                            if (not c.auth):
                                doignore = True
                                break
                        except AttributeError:
                            pass
                    elif ((e == c.__class__) or issubclass(c.__class__, e)):
                        doignore = True
                        break
                if doignore:
                    continue
            if isinstance(c, AbstractElement):
                j = c.getindex(child, recursive)
                if (j != (- 1)):
                    return i
    return (- 1)

Get the index at which an element occurs, recursive by default!

Returns:
int


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

    query_hash = hash(query)

    event_data = TangoAndroidContactEventData()

    first_name = self._GetRowValue(query_hash, row, 'first_name')
    try:
      decoded_text = base64_decode(first_name)
      event_data.first_name = codecs.decode(decoded_text, 'utf-8')
    except ValueError:
      event_data.first_name = first_name
      parser_mediator.ProduceExtractionWarning(
          'unable to parse first name: {0:s}'.format(first_name))

    last_name = self._GetRowValue(query_hash, row, 'last_name')
    try:
      decoded_text = base64_decode(last_name)
      event_data.last_name = codecs.decode(decoded_text, 'utf-8')
    except ValueError:
      event_data.last_name = last_name
      parser_mediator.ProduceExtractionWarning(
          'unable to parse last name: {0:s}'.format(last_name))

    event_data.birthday = self._GetRowValue(query_hash, row, 'birthday')
    event_data.gender = self._GetRowValue(query_hash, row, 'gender')

    status = self._GetRowValue(query_hash, row, 'status')
    try:
      decoded_text = base64_decode(status)
      event_data.status = codecs.decode(decoded_text, 'utf-8')
    except ValueError:
      event_data.status = status
      parser_mediator.ProduceExtractionWarning(
          'unable to parse status: {0:s}'.format(status))

    event_data.distance = self._GetRowValue(query_hash, row, 'distance')

    is_friend = self._GetRowValue(query_hash, row, 'friend')
    event_data.is_friend = False
    if is_friend:
      event_data.is_friend = True

    event_data.friend_request_type = self._GetRowValue(
        query_hash, row, 'friend_request_type')

    friend_request_message = self._GetRowValue(
        query_hash, row, 'friend_request_message')
    try:
      decoded_text = base64_decode(friend_request_message)
      event_data.friend_request_message = codecs.decode(decoded_text, 'utf-8')
    except ValueError:
      event_data.friend_request_message = friend_request_message
      parser_mediator.ProduceExtractionWarning(
          'unable to parse status: {0:s}'.format(friend_request_message))

    timestamp = self._GetRowValue(query_hash, row, 'last_active_time')
    if timestamp:
      date_time = dfdatetime_java_time.JavaTime(timestamp=timestamp)
      event = time_events.DateTimeValuesEvent(
          date_time, definitions.TIME_DESCRIPTION_LAST_ACTIVE)
      parser_mediator.ProduceEventWithEventData(event, event_data)

    timestamp = self._GetRowValue(query_hash, row, 'last_access_time')
    if timestamp:
      date_time = dfdatetime_java_time.JavaTime(timestamp=timestamp)
      event = time_events.DateTimeValuesEvent(
          date_time, definitions.TIME_DESCRIPTION_LAST_ACCESS)
      parser_mediator.ProduceEventWithEventData(event, event_data)

    timestamp = self._GetRowValue(query_hash, row, 'friend_request_time')
    if timestamp:
      date_time = dfdatetime_java_time.JavaTime(timestamp=timestamp)
      event = time_events.DateTimeValuesEvent(
          date_time, definitions.TIME_DESCRIPTION_SENT)
      parser_mediator.ProduceEventWithEventData(event, event_data)

Parses a contact row from the database.

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


def _SetupValuesForDevice(self, tensor_in_sizes, filter_in_sizes, dilations, strides, padding, data_format, dtype, use_gpu, op_name):
    x1 = self._CreateNumpyTensor(tensor_in_sizes)
    x2 = self._CreateNumpyTensor(filter_in_sizes)
    with test_util.device(use_gpu):
        t1 = constant_op.constant(x1, shape=tensor_in_sizes, dtype=dtype)
        t2 = constant_op.constant(x2, shape=filter_in_sizes, dtype=dtype)
        strides = [1] + strides + [1]
        dilations = [1] + dilations + [1]
        if isinstance(padding, (list, tuple)):
            padding = [(0, 0)] + padding + [(0, 0)]
        if data_format == 'NCHW':
            t1 = test_util.NHWCToNCHW(t1)
            strides = test_util.NHWCToNCHW(strides)
            dilations = test_util.NHWCToNCHW(dilations)
            if isinstance(padding, (list, tuple)):
                padding = test_util.NHWCToNCHW(padding)
        if op_name == 'Conv2D':
            conv = nn_ops.conv2d(t1, t2, dilations=dilations, strides=strides, padding=padding, data_format=data_format)
        elif op_name == 'Conv':
            conv_format = 'CHANNELS_LAST' if data_format == 'NHWC' else 'CHANNELS_FIRST'
            conv_padding, explicit_paddings = nn_ops.convert_padding(padding)
            conv = gen_nn_ops.conv(t1, t2, strides=strides, padding=conv_padding, explicit_paddings=explicit_paddings, data_format=conv_format, dilations=dilations)
        else:
            raise ValueError('Invalid op name: %s' % op_name)
        self.assertEqual(conv.dtype, dtype)
        if data_format == 'NCHW':
            conv = test_util.NCHWToNHWC(conv)
        return conv

Verifies the output values of the convolution function.

Args:
tensor_in_sizes: Input tensor dimensions in [batch, input_rows,
input_cols, input_depth].
filter_in_sizes: Filter tensor dimensions in [kernel_rows, kernel_cols,
input_depth, output_depth].
dilations: Dilated rate: [col_dilation, row_dilation]
strides: Stride: [col_stride, row_stride]
padding: Padding type.
data_format: Format of the data tensors.
dtype: Data type for inputs and outputs.
use_gpu: True if the operations should be run on GPU
op_name: Name of the op to be tested

Returns:
Symbolic tensor value that can be used to execute the computation


def run_inference(self, batch: Sequence[ExampleT], model: ModelT, inference_args: Optional[dict[str, Any]]=None) -> Iterable[PredictionT]:
    while self.throttler.throttle_request(time.time() * _MILLISECOND_TO_SECOND):
        self.logger.info('Delaying request for %d seconds due to previous failures', self.throttle_delay_secs)
        time.sleep(self.throttle_delay_secs)
        self.throttled_secs.inc(self.throttle_delay_secs)
    try:
        req_time = time.time()
        predictions = self.request(batch, model, inference_args)
        self.throttler.successful_request(req_time * _MILLISECOND_TO_SECOND)
        return predictions
    except Exception as e:
        self.logger.error('exception raised as part of request, got %s', e)
        raise

Runs inferences on a batch of examples. Calls a remote model for
predictions and will retry if a retryable exception is raised.

Args:
batch: A sequence of examples or features.
model: The model used to make inferences.
inference_args: Extra arguments for models whose inference call requires
extra parameters.

Returns:
An Iterable of Predictions.


def version(msg):

    tc = typecode(msg)

    if tc != 31:
        raise RuntimeError("%s: Not a status operation message, expecting TC = 31" % msg)

    msgbin = common.hex2bin(msg)
    version = common.bin2int(msgbin[72:75])

    return version

ADS-B Version

Args:
msg (string): 28 bytes hexadecimal message string, TC = 31

Returns:
int: version number


def ConfigureLogging(
    debug_output=False, filename=None, mode='w', quiet_mode=False):

  for handler in logging.root.handlers:
    logging.root.removeHandler(handler)

  logger = logging.getLogger()

  if filename and filename.endswith('.gz'):
    handler = CompressedFileHandler(filename, mode=mode)
  elif filename:
    handler = logging.FileHandler(filename, mode=mode)
  else:
    handler = logging.StreamHandler()

  format_string = (
      '%(asctime)s [%(levelname)s] (%(processName)-10s) PID:%(process)d '
      '<%(module)s> %(message)s')

  formatter = logging.Formatter(format_string)
  handler.setFormatter(formatter)

  if debug_output:
    level = logging.DEBUG
  elif quiet_mode:
    level = logging.WARNING
  else:
    level = logging.INFO

  logger.setLevel(level)
  handler.setLevel(level)

  logger.addHandler(handler)

Configures the logging root logger.

Args:
debug_output (Optional[bool]): True if the logging should include debug
output.
filename (Optional[str]): log filename.
mode (Optional[str]): log file access mode.
quiet_mode (Optional[bool]): True if the logging should not include
information output. Note that debug_output takes precedence over
quiet_mode.


def mask_from_embedding(emb):

  return weights_nonzero(tf.reduce_sum(tf.abs(emb), axis=3, keepdims=True))

Input embeddings -> padding mask.

We have hacked symbol_modality to return all-zero embeddings for padding.
Returns a mask with 0.0 in the padding positions and 1.0 elsewhere.

Args:
emb: a Tensor with shape [batch, width, height, depth].
Returns:
a 0.0/1.0 Tensor with shape [batch, width, height, 1].


def grab_data(self, f_start=None, f_stop=None,t_start=None, t_stop=None, if_id=0):

        self.freqs = self.populate_freqs()
        self.timestamps = self.populate_timestamps()

        if f_start is None:
            f_start = self.freqs[0]
        if f_stop is None:
            f_stop = self.freqs[-1]

        i0 = np.argmin(np.abs(self.freqs - f_start))
        i1 = np.argmin(np.abs(self.freqs - f_stop))

        if i0 < i1:
            plot_f    = self.freqs[i0:i1 + 1]
            plot_data = np.squeeze(self.data[t_start:t_stop, ..., i0:i1 + 1])
        else:
            plot_f    = self.freqs[i1:i0 + 1]
            plot_data = np.squeeze(self.data[t_start:t_stop, ..., i1:i0 + 1])

        return plot_f, plot_data

Extract a portion of data by frequency range.

Args:
f_start (float): start frequency in MHz
f_stop (float): stop frequency in MHz
if_id (int): IF input identification (req. when multiple IFs in file)

Returns:
(freqs, data) (np.arrays): frequency axis in MHz and data subset


def get_svg_layers(svg_sources):

    layers = []
    width, height = None, None

    def extract_length(attr):
        'Extract length in pixels.'
        match = CRE_MM_LENGTH.match(attr)
        if match:

            return INKSCAPE_PPmm.magnitude * float(match.group('length'))
        else:
            return float(attr)

    for svg_source_i in svg_sources:

        xml_root = etree.parse(svg_source_i)
        svg_root = xml_root.xpath('/svg:svg', namespaces=INKSCAPE_NSMAP)[0]
        width = max(extract_length(svg_root.attrib['width']), width)
        height = max(extract_length(svg_root.attrib['height']), height)
        layers += svg_root.xpath('
                                 namespaces=INKSCAPE_NSMAP)

    for i, layer_i in enumerate(layers):
        layer_i.attrib['id'] = 'layer%d' % (i + 1)
    return (width, height), layers

Collect layers from input svg sources.

Args:

svg_sources (list) : A list of file-like objects, each containing
one or more XML layers.

Returns
-------
(width, height), layers : (int, int), list
The first item in the tuple is the shape of the largest layer, and the
second item is a list of ``Element`` objects (from :mod:`lxml.etree`
module), one per SVG layer.


def generate_sb(date: datetime.datetime, project: str, programme_block: str) -> dict:
    date = date.strftime('%Y%m%d')
    instance_id = randint(0, 9999)
    sb_id = 'SB-{}-{}-{:04d}'.format(date, project, instance_id)
    return dict(id=sb_id, project=project, programme_block=programme_block)

Generate a Scheduling Block data object.

Args:
date (datetime.datetime): UTC date of the SBI
project (str): Project Name
programme_block (str): Programme

Returns:
str, Scheduling Block Instance (SBI) ID.


def eventFilter(self, object, event):

        if (object is self.tree_scripts):

            if (event.type() == QtCore.QEvent.ChildAdded):
                item = self.tree_scripts.selectedItems()[0]
                if not isinstance(item.value, Script):
                    print('ONLY SCRIPTS CAN BE DRAGGED')
                    return False
                print(('XXX ChildAdded', self.tree_scripts.selectedItems()[0].name))

            if (event.type() == QtCore.QEvent.ChildRemoved):
                print(('XXX ChildRemoved', self.tree_scripts.selectedItems()[0].name))
            if (event.type() == QtCore.QEvent.Drop):
                print('XXX Drop')

            return False

        return False

TEMPORARY / UNDER DEVELOPMENT

THIS IS TO ALLOW COPYING OF PARAMETERS VIA DRAP AND DROP

Args:
object:
event:

Returns:


def _AddToTree(self, x, prevx):
    self.s.add(x)
    self.prev[x] = prevx
    for y in self.right:
        slack = self._CalcSlack(x, y)
        if slack < self.slack[y]:
            self.slack[y] = slack
            self.slackx[y] = x

Adds |x| to the current augmenting tree.

x is a node which has already been matched to a node y in Right (which is
itself connected to prevx via a non-matching edge in the equality subgraph).
We indicate prevx comes before x in the tree so we can trace the path later.

Args:
x: Node which has already been matched to a node y in right
prevx: Previous node in Left along the path.


def get_all_without_ethernet(self, start=0, count=(- 1), filter='', sort=''):
    without_ethernet_client = ResourceClient(self._connection, '/rest/logical-downlinks/withoutEthernet')
    return without_ethernet_client.get_all(start, count, filter=filter, sort=sort)

Gets a paginated collection of logical downlinks without ethernet. The collection is
based on optional sorting and filtering and is constrained by start and count parameters.

Args:
start:
The first item to return, using 0-based indexing.
If not specified, the default is 0 - start with the first available item.
count:
The number of resources to return. A count of -1 requests all items.
The actual number of items in the response might differ from the requested
count if the sum of start and count exceeds the total number of items.
filter (list or str):
A general filter/query string to narrow the list of items returned. The
default is no filter; all resources are returned.
sort:
The sort order of the returned data set. By default, the sort order is based
on create time with the oldest entry first.

Returns:
dict


def plot_path(line, lattice=None, coords_are_cartesian=False, ax=None, **kwargs):
    (ax, fig, plt) = get_ax3d_fig_plt(ax)
    if ('color' not in kwargs):
        kwargs['color'] = 'r'
    if ('linewidth' not in kwargs):
        kwargs['linewidth'] = 3
    for k in range(1, len(line)):
        vertex1 = line[(k - 1)]
        vertex2 = line[k]
        if (not coords_are_cartesian):
            if (lattice is None):
                raise ValueError('coords_are_cartesian False requires the lattice')
            vertex1 = lattice.get_cartesian_coords(vertex1)
            vertex2 = lattice.get_cartesian_coords(vertex2)
        ax.plot(*zip(vertex1, vertex2), **kwargs)
    return (fig, ax)

Adds a line passing through the coordinates listed in 'line' to a matplotlib Axes

Args:
line: list of coordinates.
lattice: Lattice object used to convert from reciprocal to cartesian coordinates
coords_are_cartesian: Set to True if you are providing
coordinates in cartesian coordinates. Defaults to False.
Requires lattice if False.
ax: matplotlib :class:`Axes` or None if a new figure should be created.
kwargs: kwargs passed to the matplotlib function 'plot'. Color defaults to red
and linewidth to 3.

Returns:
matplotlib figure and matplotlib ax


def _maybe_init_run(self, experiment_name, run_name):

    experiment_id = self._maybe_init_experiment(experiment_name)
    cursor = self._db.cursor()
    cursor.execute(
        ,
        (experiment_id, run_name))
    row = cursor.fetchone()
    if row:
      return row[0]
    run_id = self._create_id()

    started_time = 0
    cursor.execute(
        ,
        (experiment_id, run_id, run_name, time.time(), started_time))
    return run_id

Returns the ID for the given run, creating the row if needed.

Args:
experiment_name: name of experiment containing this run.
run_name: name of run.


def format(self, exclude_class=False):
    if exclude_class:
        msg = self.msg
    else:
        msg = ('%s: %s' % (self.__class__.__name__, self.msg))
    if (len(self.params) != 0):
        paramstring = '\n'.join([((str(key) + ': ') + str(val)) for (key, val) in self.params.items()])
        msg += ('\nAdditional Information:\n' + paramstring)
    return msg

Format this exception as a string including class name.

Args:
exclude_class (bool): Whether to exclude the exception class
name when formatting this exception

Returns:
string: a multiline string with the message, class name and
key value parameters passed to create the exception.


def _process_image_files(name, filenames, texts, labels, num_shards):
    assert (len(filenames) == len(texts))
    assert (len(filenames) == len(labels))
    spacing = np.linspace(0, len(filenames), (FLAGS.num_threads + 1)).astype(np.int)
    ranges = []
    for i in range((len(spacing) - 1)):
        ranges.append([spacing[i], spacing[(i + 1)]])
    print(('Launching %d threads for spacings: %s' % (FLAGS.num_threads, ranges)))
    sys.stdout.flush()
    coord = tf.train.Coordinator()
    coder = ImageCoder()
    threads = []
    for thread_index in range(len(ranges)):
        args = (coder, thread_index, ranges, name, filenames, texts, labels, num_shards)
        t = threading.Thread(target=_process_image_files_batch, args=args)
        t.start()
        threads.append(t)
    coord.join(threads)
    print(('%s: Finished writing all %d images in data set.' % (datetime.now(), len(filenames))))
    sys.stdout.flush()

Process and save list of images as TFRecord of Example protos.

Args:
name: string, unique identifier specifying the data set
filenames: list of strings; each string is a path to an image file
texts: list of strings; each string is human readable, e.g. 'dog'
labels: list of integer; each integer identifies the ground truth
num_shards: integer number of shards for this data set.


def delete_permissions(self, grp_name, resource):

        self.project_service.set_auth(self._token_project)
        self.project_service.delete_permissions(grp_name, resource)

Removes permissions from the group for the given resource.

Args:
grp_name (string): Name of group.
resource (intern.resource.boss.Resource): Identifies which data
model object to operate on.

Raises:
requests.HTTPError on failure.


def get_range(self, name_prefix, vlan_id_range):
    filter = '"\'name\' matches \'{}\\_%\'"'.format(name_prefix)
    ethernet_networks = self.get_all(filter=filter, sort='vlanId:ascending')
    vlan_ids = self.dissociate_values_or_ranges(vlan_id_range)
    for net in ethernet_networks[:]:
        if (int(net['vlanId']) not in vlan_ids):
            ethernet_networks.remove(net)
    return ethernet_networks

Gets a list of Ethernet Networks that match the 'given name_prefix' and the 'vlan_id_range'.

Examples:
>>> enet.get_range('Enet_name', '1-2,5')
# The result contains the ethernet network with names:
['Enet_name_1', 'Enet_name_2', 'Enet_name_5']

>>> enet.get_range('Enet_name', '2')
# The result contains the ethernet network with names:
['Enet_name_1', 'Enet_name_2']

Args:
name_prefix: The Ethernet Network prefix
vlan_id_range: A combination of values or ranges to be retrieved. For example, '1-10,50,51,500-700'.

Returns:
list: A list of Ethernet Networks.


def _get_element_attr_or_none(document, selector, attribute):
    element = document.cssselect(selector)
    if element:
        return element[0].get(attribute)
    return None

Using a CSS selector, get the element and return the given attribute value, or None if no element.

Args:
document (HTMLElement) - HTMLElement document
selector (str) - CSS selector
attribute (str) - The attribute to get from the element


def _set_bearer_user_vars_local(token, allowed_client_ids, scopes):

  result = urlfetch.fetch(
      '%s?%s' % (_TOKENINFO_URL, urllib.urlencode({'access_token': token})))
  if result.status_code != 200:
    try:
      error_description = json.loads(result.content)['error_description']
    except (ValueError, KeyError):
      error_description = ''
    _logger.error('Token info endpoint returned status %s: %s',
                  result.status_code, error_description)
    return
  token_info = json.loads(result.content)

  if 'email' not in token_info:
    _logger.warning('Oauth token doesn\'t include an email address.')
    return
  if token_info.get('email_verified') != 'true':
    _logger.warning('Oauth token email isn\'t verified.')
    return

  client_id = token_info.get('azp')
  if (list(allowed_client_ids) != SKIP_CLIENT_ID_CHECK and
      client_id not in allowed_client_ids):
    _logger.warning('Client ID is not allowed: %s', client_id)
    return

  _, sufficient_scopes = _process_scopes(scopes)
  authorized_scopes = token_info.get('scope', '').split(' ')
  if not _are_scopes_sufficient(authorized_scopes, sufficient_scopes):
    _logger.warning('Oauth token scopes don\'t match any acceptable scopes.')
    return

  os.environ[_ENV_AUTH_EMAIL] = token_info['email']
  os.environ[_ENV_AUTH_DOMAIN] = ''
  _logger.debug('Local dev returning user from token.')

Validate the oauth bearer token on the dev server.

Since the functions in the oauth module return only example results in local
development, this hits the tokeninfo endpoint and attempts to validate the
token.  If it's valid, we'll set _ENV_AUTH_EMAIL and _ENV_AUTH_DOMAIN so we
can get the user from the token.

Args:
token: String with the oauth token to validate.
allowed_client_ids: List of client IDs that are acceptable.
scopes: List of acceptable scopes.


def search(self, scope, search, **kwargs):
    data = {'scope': scope, 'search': search}
    return self.http_list('/search', query_data=data, **kwargs)

Search GitLab resources matching the provided string.'

Args:
scope (str): Scope of the search
search (str): Search string
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabSearchError: If the server failed to perform the request

Returns:
GitlabList: A list of dicts describing the resources found.


def monkhorst(cls, ngkpt, shiftk=(0.5, 0.5, 0.5), chksymbreak=None, use_symmetries=True, use_time_reversal=True, comment=None):
    return cls(kpts=[ngkpt], kpt_shifts=shiftk, use_symmetries=use_symmetries, use_time_reversal=use_time_reversal, chksymbreak=chksymbreak, comment=(comment if comment else 'Monkhorst-Pack scheme with user-specified shiftk'))

Convenient static constructor for a Monkhorst-Pack mesh.

Args:
ngkpt: Subdivisions N_1, N_2 and N_3 along reciprocal lattice vectors.
shiftk: Shift to be applied to the kpoints.
use_symmetries: Use spatial symmetries to reduce the number of k-points.
use_time_reversal: Use time-reversal symmetry to reduce the number of k-points.

Returns:
:class:`KSampling` object.


def save_args(conditions, out_path):
    if isinstance(conditions, argparse.Namespace):
        args = vars(conditions)
    else:
        args = conditions
    try:
        os.makedirs(out_path)
    except OSError:
        pass
    with tempdir(prefix='args', dir=out_path) as tempd:
        path = os.path.join(tempd, 'args.json')
        with open(path, 'w') as f:
            json.dump(args, f, indent=4)
        new_path = os.path.join(out_path, 'args')
        shutil.move(path, new_path)

A util function to save experiment condition for job table.

Args:
conditions (:class:`argparse.Namespace` or dict): Experiment conditions
to show on a job table. Keys are show as table header and values
are show at a job row.
out_path (str): Output directory name to save conditions.


def _optimize_tf_model(self, graph_def, input_tensors, output_tensors, quant_mode):
    if self.saved_model_dir or quant_mode.is_quantization_aware_trained_model():
        return graph_def
    try:
        graph = _convert_to_constants.disable_lower_using_switch_merge(graph_def)
        optimized_graph = _run_graph_optimizations(graph, input_tensors, output_tensors, config=self._grappler_config(['function']))
        return optimized_graph
    except Exception:
        return graph_def

Run a Grappler pass to optimize the TensorFlow graph.

Args:
graph_def: Frozen GraphDef to be optimized.
input_tensors: List of input tensors.
output_tensors: List of output tensors.
quant_mode: the quantization mode.

Returns:
The optimized TensorFlow graph.


def __init__(self, message, raises=False):

        super(CustodianError, self).__init__(message)
        self.raises = raises
        self.message = message

Initializes the error with a message.

Args:
message (str): Message passed to Exception
raises (bool): Whether this should be raised outside custodian


def __init__(self, dfk, *args, threshold=20, interval=5):

        self.dfk = dfk
        self.threshold = threshold
        self.interval = interval
        self.cb_args = args
        self.strategy = Strategy(dfk)
        self.callback = self.strategy.strategize
        self._handle = None
        self._event_count = 0
        self._event_buffer = []
        self._wake_up_time = time.time() + 1
        self._kill_event = threading.Event()
        self._thread = threading.Thread(target=self._wake_up_timer, args=(self._kill_event,))
        self._thread.daemon = True
        self._thread.start()

Initialize the flowcontrol object.

We start the timer thread here

Args:
- dfk (DataFlowKernel) : DFK object to track parsl progress

KWargs:
- threshold (int) : Tasks after which the callback is triggered
- interval (int) : seconds after which timer expires


def send_to_prv_exchange(self, user_id, message=None):
    exchange = ('prv_%s' % user_id.lower())
    msg = json.dumps(message, cls=ZEngineJSONEncoder)
    log.debug(('Sending following users "%s" exchange:\n%s ' % (exchange, msg)))
    self.get_channel().publish(exchange=exchange, routing_key='', body=msg)

Send messages through logged in users private exchange.

Args:
user_id string: User key
message dict: Message object


def __init__(self, graph, name=None):

        if not isinstance(graph, BipartiteGraph):
            raise ValueError(
                "Given graph is not instance of Bipartite:", graph)

        self._graph = graph
        if name:
            self.name = name
        else:
            self.name = super(_Node, self).__str__()
        self._hash = None

Construct a new node.

Args:
name: Specifying the name of this node.
If not given, use strings returned from __str__ method.


def GetFileEntryByPathSpec(self, path_spec):

    return encrypted_stream_file_entry.EncryptedStreamFileEntry(
        self._resolver_context, self, path_spec, is_root=True, is_virtual=True)

Retrieves a file entry for a path specification.

Args:
path_spec (PathSpec): a path specification.

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


def segment(self, source, language=None):

    if language and not language in self.supported_languages:
      raise ValueError(
          'Language {} is not supported by NLAPI segmenter'.format(language))

    chunks = ChunkList()
    results = tinysegmenter.tokenize(source)
    seek = 0
    for word in results:
      word = word.strip()
      if not word:
        continue
      if source[seek: seek + len(word)] != word:
        assert source[seek] == ' '
        assert source[seek + 1: seek + len(word) + 1] == word
        chunks.append(Chunk.space())
        seek += 1

      dependency = None
      if word in _PARTICLES or word in _AUX_VERBS or is_hiragana(word):
        dependency = False

      chunk = Chunk(word, dependency=dependency)
      if chunk.is_punct():
        chunk.dependency = chunk.is_open_punct()
      chunks.append(chunk)
      seek += len(word)
    chunks.resolve_dependencies()
    return chunks

Returns a chunk list from the given sentence.

Args:
source (str): Source string to segment.
language (:obj:`str`, optional): A language code.

Returns:
A chunk list. (:obj:`budou.chunk.ChunkList`)

Raises:
ValueError: If :obj:`language` is given and it is not included in
:obj:`supported_languages`.


def GreaterThanOrEqualTo(self, value):
    self._awql = self._CreateSingleValueCondition(value, '>=')
    return self._query_builder

Sets the type of the WHERE clause as "greater than or equal to".

Args:
value: The value to be used in the WHERE condition.

Returns:
The query builder that this WHERE builder links to.


def setValues(self, values):

        ncols = self.getNumCols()
        nindices = self.getNumIndices()
        for key, value in values.items():
            key = Utils.convToList(key)
            assert len(key) == nindices
            value = Utils.convToList(value)
            assert len(value) == ncols-nindices
            self.addRow(key + value)

Set the values of a DataFrame from a dictionary.

Args:
values: Dictionary with the values to set.


def _GetGdbThreadMapping(self, position):
    if (len(gdb.selected_inferior().threads()) == 1):
        return {position[1]: 1}
    thread_line_regexp = '\\s*\\**\\s*([0-9]+)\\s+[a-zA-Z]+\\s+([x0-9a-fA-F]+)\\s.*'
    output = gdb.execute('info threads', to_string=True)
    matches = [re.match(thread_line_regexp, line) for line in output.split('\n')[1:]]
    return {int(match.group(2), 16): int(match.group(1)) for match in matches if match}

Gets a mapping from python tid to gdb thread num.

There's no way to get the thread ident from a gdb thread.  We only get the
"ID of the thread, as assigned by GDB", which is completely useless for
everything except talking to gdb.  So in order to translate between these
two, we have to execute 'info threads' and parse its output. Note that this
may only work on linux, and only when python was compiled to use pthreads.
It may work elsewhere, but we won't guarantee it.

Args:
position: array of pid, tid, framedepth specifying the requested position.
Returns:
A dictionary of the form {python_tid: gdb_threadnum}.


def int64_user_gauge(namespace, name, metric, ptransform=None) -> metrics_pb2.MonitoringInfo:
    labels = create_labels(ptransform=ptransform, namespace=namespace, name=name)
    if isinstance(metric, GaugeData):
        coder = coders.VarIntCoder()
        value = metric.value
        timestamp = metric.timestamp
    else:
        raise TypeError('Expected GaugeData metric type but received %s with value %s' % (type(metric), metric))
    payload = _encode_gauge(coder, timestamp, value)
    return create_monitoring_info(USER_GAUGE_URN, LATEST_INT64_TYPE, payload, labels)

Return the gauge monitoring info for the URN, metric and labels.

Args:
namespace: User-defined namespace of gauge metric.
name: Name of gauge metric.
metric: The GaugeData containing the metrics.
ptransform: The ptransform id used as a label.


def start(self, wait=False):

        if self._status is not TaskStatus.IDLE:
            raise RuntimeError("Cannot start %s in state %s" %
                               (self, self._status))
        self._status = TaskStatus.STARTED
        STARTED_TASKS.add(self)
        self._start()

        if wait:
            self.wait()

        return self.return_values

Start a task.

This function depends on the underlying implementation of _start, which
any subclass of ``Task`` should implement.

Args:
wait (bool): Whether or not to wait on the task to finish before
returning from this function. Default `False`.

Raises:
RuntimeError: If the task has already been started without a
subsequent call to ``reset()``.


def get_random_transform(self, img_shape, seed=None):
    img_row_axis = self.row_axis - 1
    img_col_axis = self.col_axis - 1
    if seed is not None:
        np.random.seed(seed)
    if self.rotation_range:
        theta = np.random.uniform(-self.rotation_range, self.rotation_range)
    else:
        theta = 0
    if self.height_shift_range:
        try:
            tx = np.random.choice(self.height_shift_range)
            tx *= np.random.choice([-1, 1])
        except ValueError:
            tx = np.random.uniform(-self.height_shift_range, self.height_shift_range)
        if np.max(self.height_shift_range) < 1:
            tx *= img_shape[img_row_axis]
    else:
        tx = 0
    if self.width_shift_range:
        try:
            ty = np.random.choice(self.width_shift_range)
            ty *= np.random.choice([-1, 1])
        except ValueError:
            ty = np.random.uniform(-self.width_shift_range, self.width_shift_range)
        if np.max(self.width_shift_range) < 1:
            ty *= img_shape[img_col_axis]
    else:
        ty = 0
    if self.shear_range:
        shear = np.random.uniform(-self.shear_range, self.shear_range)
    else:
        shear = 0
    if self.zoom_range[0] == 1 and self.zoom_range[1] == 1:
        zx, zy = (1, 1)
    else:
        zx, zy = np.random.uniform(self.zoom_range[0], self.zoom_range[1], 2)
    flip_horizontal = (np.random.random() < 0.5) * self.horizontal_flip
    flip_vertical = (np.random.random() < 0.5) * self.vertical_flip
    channel_shift_intensity = None
    if self.channel_shift_range != 0:
        channel_shift_intensity = np.random.uniform(-self.channel_shift_range, self.channel_shift_range)
    brightness = None
    if self.brightness_range is not None:
        brightness = np.random.uniform(self.brightness_range[0], self.brightness_range[1])
    transform_parameters = {'theta': theta, 'tx': tx, 'ty': ty, 'shear': shear, 'zx': zx, 'zy': zy, 'flip_horizontal': flip_horizontal, 'flip_vertical': flip_vertical, 'channel_shift_intensity': channel_shift_intensity, 'brightness': brightness}
    return transform_parameters

Generates random parameters for a transformation.

Args:
img_shape: Tuple of integers.
Shape of the image that is transformed.
seed: Random seed.

Returns:
A dictionary containing randomly chosen parameters describing the
transformation.


def remove(self, layers):

        if not isinstance(layers, list):
            layers = [layers]
        for l in layers:
            if isinstance(l, string_types):
                if l not in self.layers:
                    raise ValueError("There's no image/layer named '%s' in "
                                     "the masking stack!" % l)
                self.stack.remove(l)
            else:
                l = self.stack.pop(l)
            del self.layers[l]

        self.set_mask()

Remove one or more layers from the stack of masking layers.
Args:
layers: An int, string or list of strings and/or ints. Ints are
interpreted as indices in the stack to remove; strings are
interpreted as names of layers to remove. Negative ints will
also work--i.e., remove(-1) will drop the last layer added.


def _FormatMessages(self, format_string, short_format_string, event_values):

    message_string = self._FormatMessage(format_string, event_values)

    if short_format_string:
      short_message_string = self._FormatMessage(
          short_format_string, event_values)
    else:
      short_message_string = message_string

    if len(short_message_string) > 80:
      short_message_string = '{0:s}...'.format(short_message_string[:77])

    return message_string, short_message_string

Determines the formatted message strings.

Args:
format_string (str): message format string.
short_format_string (str): short message format string.
event_values (dict[str, object]): event values.

Returns:
tuple(str, str): formatted message string and short message string.


def _plot_cwt(ts, coefs, freqs, tsize=1024, fsize=512):

    import matplotlib.style
    import matplotlib as mpl
    mpl.style.use('classic')
    import matplotlib.pyplot as plt
    from scipy import interpolate
    channels = ts.shape[1]
    fig = plt.figure()
    for i in range(channels):
        rect = (0.1, 0.85*(channels - i - 1)/channels + 0.1,
                0.8, 0.85/channels)
        ax = fig.add_axes(rect)
        logpowers = np.log((coefs[:, :, i] * coefs[:, :, i].conj()).real)
        tmin, tmax = ts.tspan[0], ts.tspan[-1]
        fmin, fmax = freqs[0], freqs[-1]
        tgrid, fgrid = np.mgrid[tmin:tmax:tsize*1j, fmin:fmax:fsize*1j]
        gd = interpolate.interpn((ts.tspan, freqs), logpowers,
                                 (tgrid, fgrid)).T
        ax.imshow(gd, cmap='gnuplot2', aspect='auto', origin='lower',
                   extent=(tmin, tmax, fmin, fmax))
        ax.set_ylabel('freq (Hz)')
    fig.axes[0].set_title(u'log(power spectral density)')
    fig.axes[channels - 1].set_xlabel('time (s)')
    fig.show()

Plot time resolved power spectral density from cwt results
Args:
ts: the original Timeseries
coefs:  continuous wavelet transform coefficients as calculated by cwt()
freqs: list of frequencies (in Hz) corresponding to coefs.
tsize, fsize: size of the plot (time axis and frequency axis, in pixels)


def init_from_adversarial_batches(self, adv_batches):

    for idx, (adv_batch_id, adv_batch_val) in enumerate(iteritems(adv_batches)):
      work_id = ATTACK_WORK_ID_PATTERN.format(idx)
      self.work[work_id] = {
          'claimed_worker_id': None,
          'claimed_worker_start_time': None,
          'is_completed': False,
          'error': None,
          'elapsed_time': None,
          'submission_id': adv_batch_val['submission_id'],
          'shard_id': None,
          'output_adversarial_batch_id': adv_batch_id,
      }

Initializes work pieces from adversarial batches.

Args:
adv_batches: dict with adversarial batches,
could be obtained as AversarialBatches.data


def mark_causative(self, institute, case, user, link, variant):

        display_name = variant['display_name']
        LOG.info("Mark variant {0} as causative in the case {1}".format(
            display_name, case['display_name']))

        LOG.info("Adding variant to causatives in case {0}".format(
            case['display_name']))

        LOG.info("Marking case {0} as solved".format(
            case['display_name']))

        updated_case = self.case_collection.find_one_and_update(
            {'_id': case['_id']},
            {
                '$push': {'causatives': variant['_id']},
                '$set': {'status': 'solved'}
            },
            return_document=pymongo.ReturnDocument.AFTER
        )

        LOG.info("Creating case event for marking {0}" \
                    " causative".format(variant['display_name']))

        self.create_event(
            institute=institute,
            case=case,
            user=user,
            link=link,
            category='case',
            verb='mark_causative',
            variant=variant,
            subject=variant['display_name'],
        )

        LOG.info("Creating variant event for marking {0}" \
                    " causative".format(case['display_name']))

        self.create_event(
            institute=institute,
            case=case,
            user=user,
            link=link,
            category='variant',
            verb='mark_causative',
            variant=variant,
            subject=variant['display_name'],
        )
        return updated_case

Create an event for marking a variant causative.

Arguments:
institute (dict): A Institute object
case (dict): Case object
user (dict): A User object
link (str): The url to be used in the event
variant (variant): A variant object

Returns:
updated_case(dict)


def noisy_moments(self, moments: 'Iterable[cirq.Moment]', system_qubits: Sequence['cirq.Qid']) -> Sequence['cirq.OP_TREE']:
    if (not hasattr(self.noisy_moment, '_not_overridden')):
        result = []
        for moment in moments:
            result.append(self.noisy_moment(moment, system_qubits))
        return result
    if (not hasattr(self.noisy_operation, '_not_overridden')):
        result = []
        for moment in moments:
            result.append([self.noisy_operation(op) for op in moment])
        return result
    assert False, 'Should be unreachable.'

Adds possibly stateful noise to a series of moments.

Args:
moments: The moments to add noise to.
system_qubits: A list of all qubits in the system.

Returns:
A sequence of OP_TREEs, with the k'th tree corresponding to the
noisy operations for the k'th moment.


def market_open(self, session, mins) -> Session:

        if session not in self.exch: return SessNA
        start_time = self.exch[session][0]
        return Session(start_time, shift_time(start_time, int(mins)))

Time intervals for market open

Args:
session: [allday, day, am, pm, night]
mins: mintues after open

Returns:
Session of start_time and end_time


def sg_sum(tensor, opt):
    r
    return tf.reduce_sum(tensor, axis=opt.axis, keep_dims=opt.keep_dims, name=opt.name)

r"""Computes the sum of elements across axis of a tensor.

See `tf.reduce_sum()` in tensorflow.

Args:
tensor: A `Tensor` with zero-padding (automatically given by chain).
opt:
axis: A tuple/list of integers or an integer. The axis to reduce.
keep_dims: If true, retains reduced dimensions with length 1.
name: If provided, replace current tensor's name.

Returns:
A `Tensor`.


def FullTransactions(self):
    is_trimmed = False
    try:
        tx = self.Transactions[0]
        if (type(tx) is str):
            is_trimmed = True
    except Exception as e:
        pass
    if (not is_trimmed):
        return self.Transactions
    txs = []
    for hash in self.Transactions:
        (tx, height) = GetBlockchain().GetTransaction(hash)
        txs.append(tx)
    self.Transactions = txs
    return self.Transactions

Get the list of full Transaction objects.

Note: Transactions can be trimmed to contain only the header and the hash. This will get the full data if
trimmed transactions are found.

Returns:
list: of neo.Core.TX.Transaction.Transaction objects.


def get_camera_imageseries(self, number_of_imageseries=10, offset=0):

        response = None
        try:
            response = requests.get(
                urls.get_imageseries(self._giid),
                headers={
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Cookie': 'vid={}'.format(self._vid)},
                params={
                    "numberOfImageSeries": int(number_of_imageseries),
                    "offset": int(offset),
                    "fromDate": "",
                    "toDate": "",
                    "onlyNotViewed": "",
                    "_": self._giid})
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        return json.loads(response.text)

Get smartcam image series

Args:
number_of_imageseries (int): number of image series to get
offset (int): skip offset amount of image series


def on_graph_def(self, graph_def, device_name, wall_time):
    raise NotImplementedError('on_graph_def() is not implemented in the base servicer class')

Callback for Event proto received through the gRPC stream.

This Event proto carries a GraphDef, encoded as bytes, in its graph_def
field.

Args:
graph_def: A GraphDef object.
device_name: Name of the device on which the graph was created.
wall_time: An epoch timestamp (in microseconds) for the graph.

Returns:
`None` or an `EventReply` proto to be sent back to the client. If `None`,
an `EventReply` proto construct with the default no-arg constructor will
be sent back to the client.


def get_plot(self, xlim=None, ylim=None):

        plt = pretty_plot(12, 8)
        base = 0.0
        i = 0
        for key, sp in self._spectra.items():
            if not self.stack:
                plt.plot(sp.x, sp.y + self.yshift * i, color=self.colors[i],
                         label=str(key), linewidth=3)
            else:
                plt.fill_between(sp.x, base, sp.y + self.yshift * i,
                                 color=self.colors[i],
                                 label=str(key), linewidth=3)
                base = sp.y + base
            plt.xlabel(sp.XLABEL)
            plt.ylabel(sp.YLABEL)
            i += 1

        if xlim:
            plt.xlim(xlim)
        if ylim:
            plt.ylim(ylim)

        plt.legend()
        leg = plt.gca().get_legend()
        ltext = leg.get_texts()
        plt.setp(ltext, fontsize=30)
        plt.tight_layout()
        return plt

Get a matplotlib plot showing the DOS.

Args:
xlim: Specifies the x-axis limits. Set to None for automatic
determination.
ylim: Specifies the y-axis limits.


def getEstTraitCovar(self, term_i=None):
    assert (self.P > 1), 'Trait covars not defined for single trait analysis'
    if (term_i == None):
        RV = SP.zeros((self.P, self.P))
        for term_i in range(self.n_terms):
            RV += self.vd.getTerm(term_i).getTraitCovar().K()
    else:
        assert (term_i < self.n_terms), 'Term index non valid'
        RV = self.vd.getTerm(term_i).getTraitCovar().K()
    return RV

Returns explicitly the estimated trait covariance matrix

Args:
term_i:     index of the term we are interested in


def is_displayed(target):
    is_displayed = getattr(target, 'is_displayed', None)
    if ((not is_displayed) or (not callable(is_displayed))):
        raise TypeError("Target has no attribute 'is_displayed' or not callable")
    if (not is_displayed()):
        raise WebDriverException('element not visible')

Assert whether the target is displayed

Args:
target(WebElement): WebElement Object.

Returns:
Return True if the element is displayed or return False otherwise.


def read_uint64(self, little_endian=True):

        if little_endian:
            endian = "<"
        else:
            endian = ">"
        return self.unpack('%sQ' % endian, 8)

Read 8 bytes as an unsigned integer value from the stream.

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

Returns:
int:


def get_crypt_class(self):
    crypt_type = getattr(settings, 'ENCRYPTED_FIELD_MODE', 'DECRYPT_AND_ENCRYPT')
    if (crypt_type == 'ENCRYPT'):
        crypt_class_name = 'Encrypter'
    elif (crypt_type == 'DECRYPT_AND_ENCRYPT'):
        crypt_class_name = 'Crypter'
    else:
        raise ImproperlyConfigured(('ENCRYPTED_FIELD_MODE must be either DECRYPT_AND_ENCRYPT or ENCRYPT, not %s.' % crypt_type))
    return getattr(keyczar, crypt_class_name)

Get the Keyczar class to use.

The class can be customized with the ENCRYPTED_FIELD_MODE setting. By default,
this setting is DECRYPT_AND_ENCRYPT. Set this to ENCRYPT to disable decryption.
This is necessary if you are only providing public keys to Keyczar.

Returns:
keyczar.Encrypter if ENCRYPTED_FIELD_MODE is ENCRYPT.
keyczar.Crypter if ENCRYPTED_FIELD_MODE is DECRYPT_AND_ENCRYPT.

Override this method to customize the type of Keyczar class returned.


async def pull(
        self,
        from_image: str,
        *,
        auth: Optional[Union[MutableMapping, str, bytes]] = None,
        tag: str = None,
        repo: str = None,
        stream: bool = False
    ) -> Mapping:

        image = from_image
        params = {"fromImage": image}
        headers = {}
        if repo:
            params["repo"] = repo
        if tag:
            params["tag"] = tag
        if auth is not None:
            registry, has_registry_host, _ = image.partition("/")
            if not has_registry_host:
                raise ValueError(
                    "Image should have registry host "
                    "when auth information is provided"
                )

            headers["X-Registry-Auth"] = compose_auth_header(auth, registry)
        response = await self.docker._query(
            "images/create", "POST", params=params, headers=headers
        )
        return await json_stream_result(response, stream=stream)

Similar to `docker pull`, pull an image locally

Args:
fromImage: name of the image to pull
repo: repository name given to an image when it is imported
tag: if empty when pulling an image all tags
for the given image to be pulled
auth: special {'auth': base64} pull private repo


def __init__(self, devices=None, cross_device_ops=None, *, mesh=None):
    self._validate_init_args(mesh, devices)
    if not mesh:
        mesh = self._build_mesh_from_device_list(devices)
    extended = dtensor_strategy_extended.DTensorStrategyExtended(container_strategy=self, mesh=mesh)
    super().__init__(extended)
    self._mesh = mesh
    self._devices = devices

Synchronous training across multiple replicas on one machine.

Args:
devices: a list of device strings, such as ['/gpu:0', '/gpu:1']. If both
`mesh` and `devices` are None, all the available GPU/TPU will be used.
If no accelerators are found, CPU is used.
cross_device_ops: optional, a descendant of `CrossDeviceOps`. The value is
ignored at the moment, and support will be added later.
mesh: optional DTensor mesh for the computation. Note that either `mesh`
or `devices` should be provided, and not both. The mesh should be 1D,
and will be used to split the input data among that dimension.


def invert_dict(d):
    inverted = collections.defaultdict(list)
    for key, value_list in d.items():
        for val in value_list:
            inverted[val].append(key)
    return inverted

Invert a dictionary.

Converts a dictionary (mapping strings to lists of strings) to a dictionary
that maps into the other direction.

Arguments:
d: Dictionary to be inverted

Returns:
A dictionary n with the property that if "y in d[x]", then "x in n[y]".


def _parse_date(dataset_date, date_format):

        if date_format is None:
            try:
                return parser.parse(dataset_date)
            except (ValueError, OverflowError) as e:
                raisefrom(HDXError, 'Invalid dataset date!', e)
        else:
            try:
                return datetime.strptime(dataset_date, date_format)
            except ValueError as e:
                raisefrom(HDXError, 'Invalid dataset date!', e)

Parse dataset date from string using specified format. If no format is supplied, the function will guess.
For unambiguous formats, this should be fine.

Args:
dataset_date (str): Dataset date string
date_format (Optional[str]): Date format. If None is given, will attempt to guess. Defaults to None.

Returns:
datetime.datetime


def phase_histogram(dts, times=None, nbins=30, colormap=mpl.cm.Blues):

    if times is None:
        times = np.linspace(dts.tspan[0], dts.tspan[-1], num=4)
    elif isinstance(times, numbers.Number):
        times = np.array([times], dtype=np.float64)
    indices = distob.gather(dts.tspan.searchsorted(times))
    if indices[-1] == len(dts.tspan):
        indices[-1] -= 1
    nplots = len(indices)
    fig = plt.figure()
    n = np.zeros((nbins, nplots))
    for i in range(nplots):
        index = indices[i]
        time = dts.tspan[index]
        phases = distob.gather(dts.mod2pi()[index, 0, :])
        ax = fig.add_subplot(1, nplots, i + 1, projection='polar')
        n[:,i], bins, patches = ax.hist(phases, nbins, (-np.pi, np.pi),
                                        density=True, histtype='bar')
        ax.set_title('time = %d s' % time)
        ax.set_xticklabels(['0', r'$\frac{\pi}{4}$', r'$\frac{\pi}{2}$',
                            r'$\frac{3\pi}{4}$', r'$\pi$', r'$\frac{-3\pi}{4}$',
                            r'$\frac{-\pi}{2}$', r'$\frac{-\pi}{4}$'])
    nmin, nmax = n.min(), n.max()

    norm = mpl.colors.Normalize(1.2*nmin - 0.2*nmax,
                                0.6*nmin + 0.4*nmax, clip=True)
    for i in range(nplots):
        ax = fig.get_axes()[i]
        ax.set_ylim(0, nmax)
        for this_n, thispatch in zip(n[:,i], ax.patches):
            color = colormap(norm(this_n))
            thispatch.set_facecolor(color)
            thispatch.set_edgecolor(color)
    fig.show()

Plot a polar histogram of a phase variable's probability distribution
Args:
dts: DistTimeseries with axis 2 ranging over separate instances of an
oscillator (time series values are assumed to represent an angle)
times (float or sequence of floats): The target times at which
to plot the distribution
nbins (int): number of histogram bins
colormap


def _LogForwardedIpChanges(self, configured, desired, to_add, to_remove, interface):
    if ((not to_add) and (not to_remove)):
        return
    self.logger.info('Changing %s IPs from %s to %s by adding %s and removing %s.', interface, (configured or None), (desired or None), (to_add or None), (to_remove or None))

Log the planned IP address changes.

Args:
configured: list, the IP address strings already configured.
desired: list, the IP address strings that will be configured.
to_add: list, the forwarded IP address strings to configure.
to_remove: list, the forwarded IP address strings to delete.
interface: string, the output device to modify.


def update_pipeline_and_auto_class_table(table: Dict[str, Tuple[str, str]]) -> Dict[str, Tuple[str, str]]:
    auto_modules = [transformers_module.models.auto.modeling_auto, transformers_module.models.auto.modeling_tf_auto, transformers_module.models.auto.modeling_flax_auto]
    for pipeline_tag, model_mapping, auto_class in PIPELINE_TAGS_AND_AUTO_MODELS:
        model_mappings = [model_mapping, f'TF_{model_mapping}', f'FLAX_{model_mapping}']
        auto_classes = [auto_class, f'TF_{auto_class}', f'Flax_{auto_class}']
        for module, cls, mapping in zip(auto_modules, auto_classes, model_mappings):
            if not hasattr(module, mapping):
                continue
            model_names = []
            for name in getattr(module, mapping).values():
                if isinstance(name, str):
                    model_names.append(name)
                else:
                    model_names.extend(list(name))
            table.update(dict.fromkeys(model_names, (pipeline_tag, cls)))
    return table

Update the table mapping models to pipelines and auto classes without removing old keys if they don't exist anymore.

Args:
table (`Dict[str, Tuple[str, str]]`):
The existing table mapping model names to a tuple containing the pipeline tag and the auto-class name with
which they should be used.

Returns:
`Dict[str, Tuple[str, str]]`: The updated table in the same format.


def fit(self, col):
    dates = self.safe_datetime_cast(col)
    self.default_val = (dates.groupby(dates).count().index[0].timestamp() * 1000000000.0)

Prepare the transformer to convert data.

Args:
col(pandas.DataFrame): Data to transform.

Returns:
None


def ParseNumericOption(self, options, name, base=10, default_value=None):
    numeric_value = getattr(options, name, None)
    if (not numeric_value):
        return default_value
    try:
        return int(numeric_value, base)
    except (TypeError, ValueError):
        name = name.replace('_', ' ')
        raise errors.BadConfigOption('Unsupported numeric value {0:s}: {1!s}.'.format(name, numeric_value))

Parses a numeric option.

If the option is not set the default value is returned.

Args:
options (argparse.Namespace): command line arguments.
name (str): name of the numeric option.
base (Optional[int]): base of the numeric value.
default_value (Optional[object]): default value.

Returns:
int: numeric value.

Raises:
BadConfigOption: if the options are invalid.


def get_caching_key(self, user_context):
    raise NotImplementedError('subclasses must override this')

Returns a unique key to use for caching.

Subclasses must override this.

Calls made to `transform_function` with functions that have the same code
object and caching key will return a cached instance on subsequent
invocations.

Args:
user_context: The context object which was passed to `transform`.

Returns:
extra_locals: A hashable.


def __convertLongToString(self, iValue):

        string = ''
        strValue = str(hex(iValue))

        string = strValue.lstrip('0x')
        string = string.rstrip('L')

        return string

convert a long hex integer to string
remove '0x' and 'L' return string

Args:
iValue: long integer in hex format

Returns:
string of this long integer without "0x" and "L"


def ToName(param_type):

    items = inspect.getmembers(ContractParameterType)

    if type(param_type) is bytes:
        param_type = int.from_bytes(param_type, 'little')

    for item in items:
        name = item[0]
        val = int(item[1].value)

        if val == param_type:
            return name

    return None

Gets the name of a ContractParameterType based on its value
Args:
param_type (ContractParameterType): type to get the name of

Returns:
str


def _query(self, url, xpath):
    return self.session.query(CachedRequest).filter((CachedRequest.url == url)).filter((CachedRequest.xpath == xpath))

Base query for an url and xpath

Args:
url (str): URL to search
xpath (str): xpath to search (may be ``None``)


def MapByteStream(
      self, byte_stream, byte_offset=0, context=None, **unused_kwargs):

    data_type_size = self._data_type_definition.GetByteSize()
    self._CheckByteStreamSize(byte_stream, byte_offset, data_type_size)

    try:
      if self._byte_order == definitions.BYTE_ORDER_BIG_ENDIAN:
        mapped_value = uuid.UUID(
            bytes=byte_stream[byte_offset:byte_offset + 16])
      elif self._byte_order == definitions.BYTE_ORDER_LITTLE_ENDIAN:
        mapped_value = uuid.UUID(
            bytes_le=byte_stream[byte_offset:byte_offset + 16])

    except Exception as exception:
      error_string = (
          'Unable to read: {0:s} from byte stream at offset: {1:d} '
          'with error: {2!s}').format(
              self._data_type_definition.name, byte_offset, exception)
      raise errors.MappingError(error_string)

    if context:
      context.byte_size = data_type_size

    return mapped_value

Maps the data type on a byte stream.

Args:
byte_stream (bytes): byte stream.
byte_offset (Optional[int]): offset into the byte stream where to start.
context (Optional[DataTypeMapContext]): data type map context.

Returns:
uuid.UUID: mapped value.

Raises:
MappingError: if the data type definition cannot be mapped on
the byte stream.


def firmware_version(self):

        namespace = "urn:brocade.com:mgmt:brocade-firmware-ext"

        request_ver = ET.Element("show-firmware-version", xmlns=namespace)

        ver = self._callback(request_ver, handler='get')
        return ver.find('.

Returns firmware version.

Args:
None

Returns:
Dictionary

Raises:
None


def preprocess_input(x, data_format=None):
    return x

A placeholder method for backward compatibility.

The preprocessing logic has been included in the efficientnet model
implementation. Users are no longer required to call this method to
normalize the input data. This method does nothing and only kept as a
placeholder to align the API surface between old and new version of model.

Args:
x: A floating point `numpy.array` or a tensor.
data_format: Optional data format of the image tensor/array. `None`
means the global setting `keras.backend.image_data_format()`
is used (unless you changed it, it uses `"channels_last"`).
Defaults to `None`.

Returns:
Unchanged `numpy.array` or tensor.


def build_panel(panel_info, adapter):
    panel_name = panel_info.get('panel_id', panel_info.get('panel_name'))
    if (not panel_name):
        raise KeyError('Panel has to have a id')
    panel_obj = dict(panel_name=panel_name)
    LOG.info('Building panel with name: {0}'.format(panel_name))
    try:
        institute_id = panel_info['institute']
    except KeyError as err:
        raise KeyError('Panel has to have a institute')
    if (adapter.institute(institute_id) is None):
        raise IntegrityError(('Institute %s could not be found' % institute_id))
    panel_obj['institute'] = panel_info['institute']
    panel_obj['version'] = float(panel_info['version'])
    try:
        panel_obj['date'] = panel_info['date']
    except KeyError as err:
        raise KeyError('Panel has to have a date')
    panel_obj['display_name'] = panel_info.get('display_name', panel_obj['panel_name'])
    gene_objs = []
    fail = False
    for gene_info in panel_info.get('genes', []):
        try:
            gene_obj = build_gene(gene_info, adapter)
            gene_objs.append(gene_obj)
        except IntegrityError as err:
            LOG.warning(err)
            fail = True
    if fail:
        raise IntegrityError('Some genes did not exist in database. Please see log messages.')
    panel_obj['genes'] = gene_objs
    return panel_obj

Build a gene_panel object

Args:
panel_info(dict): A dictionary with panel information
adapter (scout.adapter.MongoAdapter)

Returns:
panel_obj(dict)

gene_panel = dict(
panel_id = str, # required
institute = str, # institute_id, required
version = float, # required
date = datetime, # required
display_name = str, # default is panel_name
genes = list, # list of panel genes, sorted on panel_gene['symbol']
)


def run(self, dag):

        for node in dag.threeQ_or_more_gates():

            rule = node.op.definition
            if not rule:
                raise QiskitError("Cannot unroll all 3q or more gates. "
                                  "No rule to expand instruction %s." %
                                  node.op.name)

            decomposition = DAGCircuit()
            decomposition.add_qreg(rule[0][1][0][0])
            for inst in rule:
                decomposition.apply_operation_back(*inst)
            decomposition = self.run(decomposition)
            dag.substitute_node_with_dag(node, decomposition)
        return dag

Expand 3+ qubit gates using their decomposition rules.

Args:
dag(DAGCircuit): input dag
Returns:
DAGCircuit: output dag with maximum node degrees of 2
Raises:
QiskitError: if a 3q+ gate is not decomposable


def to_proj4(self, as_dict=False, toplevel=True):

        if toplevel:
            string = "+proj=longlat %s %s +nodef" % (self.datum.to_proj4(), self.prime_mer.to_proj4())
        else:
            string = "%s %s" % (self.datum.to_proj4(), self.prime_mer.to_proj4())
        if as_dict:
            return dict([
                        entry.lstrip('+').split('=')
                        for entry in string.split()
                        if entry != "+no_defs"
                         ])
        else:
            return string

Returns the CS as a proj4 formatted string or dict.

Arguments:

- **as_dict** (optional): If True, returns the proj4 string as a dict (defaults to False).
- **toplevel** (optional): If True, treats this CS as the final toplevel CS and adds the necessary proj4 elements (defaults to True).


def right_shift_blockwise(x, query_shape, name=None):
    with tf.variable_scope(name, default_name='right_shift_blockwise', values=[x]):
        x_list_shape = x.get_shape().as_list()
        x_shape = common_layers.shape_list(x)
        x = tf.expand_dims(x, axis=1)
        x = pad_to_multiple_2d(x, query_shape)
        padded_x_shape = common_layers.shape_list(x)
        x_indices = gather_indices_2d(x, query_shape, query_shape)
        x_new = get_shifted_center_blocks(x, x_indices)
        output = scatter_blocks_2d(x_new, x_indices, padded_x_shape)
        output = tf.squeeze(output, axis=1)
        output = tf.slice(output, [0, 0, 0, 0], [(- 1), x_shape[1], x_shape[2], (- 1)])
        output.set_shape(x_list_shape)
        return output

Right shifts once in every block.

Args:
x: a tensor of shape [batch, height, width, depth]
query_shape: A 2d tuple of ints
name: a string

Returns:
output: a tensor of the same shape as x


def get_service_name(*args):
    raw_services = _get_services()
    services = dict()
    for raw_service in raw_services:
        if args:
            if ((raw_service['DisplayName'] in args) or (raw_service['ServiceName'] in args) or (raw_service['ServiceName'].lower() in args)):
                services[raw_service['DisplayName']] = raw_service['ServiceName']
        else:
            services[raw_service['DisplayName']] = raw_service['ServiceName']
    return services

The Display Name is what is displayed in Windows when services.msc is
executed.  Each Display Name has an associated Service Name which is the
actual name of the service.  This function allows you to discover the
Service Name by returning a dictionary of Display Names and Service Names,
or filter by adding arguments of Display Names.

If no args are passed, return a dict of all services where the keys are the
service Display Names and the values are the Service Names.

If arguments are passed, create a dict of Display Names and Service Names

Returns:
dict: A dictionary of display names and service names

CLI Examples:

.. code-block:: bash

salt '*' service.get_service_name
salt '*' service.get_service_name 'Google Update Service (gupdate)' 'DHCP Client'


class MaxPooling1D(keras_layers.MaxPooling1D, base.Layer):

    def __init__(self, pool_size, strides, padding='valid', data_format='channels_last', name=None, **kwargs):
        if strides is None:
            raise ValueError('Argument `strides` must not be None.')
        super(MaxPooling1D, self).__init__(pool_size=pool_size, strides=strides, padding=padding, data_format=data_format, name=name, **kwargs)

Max Pooling layer for 1D inputs.

Args:
pool_size: An integer or tuple/list of a single integer,
representing the size of the pooling window.
strides: An integer or tuple/list of a single integer, specifying the
strides of the pooling operation.
padding: A string. The padding method, either 'valid' or 'same'.
Case-insensitive.
data_format: A string, one of `channels_last` (default) or `channels_first`.
The ordering of the dimensions in the inputs.
`channels_last` corresponds to inputs with shape
`(batch, length, channels)` while `channels_first` corresponds to
inputs with shape `(batch, channels, length)`.
name: A string, the name of the layer.


def hash_stream(self, url):
    md5hash = hashlib.md5()
    try:
        for chunk in self.response.iter_content(chunk_size=10240):
            if chunk:
                md5hash.update(chunk)
        return md5hash.hexdigest()
    except Exception as e:
        raisefrom(DownloadError, ('Download of %s failed in retrieval of stream!' % url), e)

Stream file from url and hash it using MD5. Must call setup method first.

Args:
url (str): URL to download

Returns:
str: MD5 hash of file


def _get_arg_parser(func, types, args_and_defaults, delimiter_chars):
    _LOG.debug("Creating ArgumentParser for '%s'", func.__name__)
    (description, arg_help) = _prepare_doc(func, [x for (x, _) in args_and_defaults], delimiter_chars)
    parser = argparse.ArgumentParser(description=description)
    for ((arg, default), arg_type) in zip_longest(args_and_defaults, types):
        help_msg = arg_help[arg]
        if (default is NoDefault):
            arg_type = (arg_type or identity_type)
            if (arg_type == bool):
                _LOG.debug('Adding optional flag %s.%s', func.__name__, arg)
                parser.add_argument(('--%s' % arg), default=True, required=False, action='store_false', help=('%s. Defaults to True if not specified' % help_msg))
            else:
                _LOG.debug('Adding positional argument %s.%s', func.__name__, arg)
                parser.add_argument(arg, help=help_msg, type=arg_type)
        else:
            if ((default is None) and (arg_type is None)):
                raise ParseThisError("To use default value of 'None' you need to specify the type of the argument '{}' for the method '{}'".format(arg, func.__name__))
            arg_type = (arg_type or type(default))
            if (arg_type == bool):
                action = ('store_false' if default else 'store_true')
                _LOG.debug('Adding optional flag %s.%s', func.__name__, arg)
                parser.add_argument(('--%s' % arg), help=help_msg, default=default, action=action)
            else:
                _LOG.debug('Adding optional argument %s.%s', func.__name__, arg)
                parser.add_argument(('--%s' % arg), help=help_msg, default=default, type=arg_type)
    return parser

Return an ArgumentParser for the given function. Arguments are defined
from the function arguments and their associated defaults.

Args:
func: function for which we want an ArgumentParser
types: types to which the command line arguments should be converted to
args_and_defaults: list of 2-tuples (arg_name, arg_default)
delimiter_chars: characters used to separate the parameters from their
help message in the docstring


def _parse_state(self, config):
    value = STATE_RE.search(config).group('value')
    return dict(state=value)

_parse_state scans the provided configuration block and extracts
the vlan state value.  The config block is expected to always return
the vlan state config.  The return dict is inteded to be merged into
the response dict.

Args:
config (str): The vlan configuration block from the nodes
running configuration

Returns:
dict: resource dict attribute


def is_str(string):
    if (sys.version_info[:2] >= (3, 0)):
        return isinstance(string, str)
    return isinstance(string, basestring)

Python 2 and 3 compatible string checker.

Args:
string (str | basestring): the string to check

Returns:
bool: True or False


def notify(self, method_name: str, *args: Any, trim_log_values: Optional[bool]=None, validate_against_schema: Optional[bool]=None, **kwargs: Any) -> Response:
    return self.send(Notification(method_name, *args, **kwargs), trim_log_values=trim_log_values, validate_against_schema=validate_against_schema)

Send a JSON-RPC request, without expecting a response.

Args:
method_name: The remote procedure's method name.
args: Positional arguments passed to the remote procedure.
kwargs: Keyword arguments passed to the remote procedure.
trim_log_values: Abbreviate the log entries of requests and responses.
validate_against_schema: Validate response against the JSON-RPC schema.


def execute_with_cancellation(op_name, num_outputs, inputs, attrs, ctx, cancellation_manager, name=None):
    device_name = ctx.device_name
    try:
        ctx.ensure_initialized()
        tensors = pywrap_tfe.TFE_Py_ExecuteCancelable(ctx._handle, device_name, op_name, inputs, attrs, cancellation_manager._impl, num_outputs)
    except core._NotOkStatusException as e:
        if name is not None:
            e.message += ' name: ' + name
        raise core._status_to_exception(e) from None
    except TypeError as e:
        keras_symbolic_tensors = [x for x in inputs if _is_keras_symbolic_tensor(x)]
        if keras_symbolic_tensors:
            raise core._SymbolicException('Inputs to eager execution function cannot be Keras symbolic tensors, but found {}'.format(keras_symbolic_tensors))
        raise e
    return tensors

Execute a TensorFlow operation.

Args:
op_name: Name of the TensorFlow operation (see REGISTER_OP in C++ code) to
execute.
num_outputs: The number of outputs of the operation to fetch. (Explicitly
provided instead of being inferred for performance reasons).
inputs: A list of inputs to the operation. Each entry should be a Tensor, or
a value which can be passed to the Tensor constructor to create one.
attrs: A tuple with alternating string attr names and attr values for this
operation.
ctx: The value of context.context().
cancellation_manager: a `CancellationManager` object that can be used to
cancel the operation.
name: Customized name for the operation.

Returns:
List of output Tensor objects. The list is empty if there are no outputs

Raises:
An exception on error.


def make_decoder(activation, latent_size, output_shape, base_depth):

  deconv = functools.partial(
      tf.keras.layers.Conv2DTranspose, padding="SAME", activation=activation)
  conv = functools.partial(
      tf.keras.layers.Conv2D, padding="SAME", activation=activation)

  decoder_net = tf.keras.Sequential([
      deconv(2 * base_depth, 7, padding="VALID"),
      deconv(2 * base_depth, 5),
      deconv(2 * base_depth, 5, 2),
      deconv(base_depth, 5),
      deconv(base_depth, 5, 2),
      deconv(base_depth, 5),
      conv(output_shape[-1], 5, activation=None),
  ])

  def decoder(codes):
    original_shape = tf.shape(input=codes)

    codes = tf.reshape(codes, (-1, 1, 1, latent_size))
    logits = decoder_net(codes)
    logits = tf.reshape(
        logits, shape=tf.concat([original_shape[:-1], output_shape], axis=0))
    return tfd.Independent(tfd.Bernoulli(logits=logits),
                           reinterpreted_batch_ndims=len(output_shape),
                           name="image")

  return decoder

Creates the decoder function.

Args:
activation: Activation function in hidden layers.
latent_size: Dimensionality of the encoding.
output_shape: The output image shape.
base_depth: Smallest depth for a layer.

Returns:
decoder: A `callable` mapping a `Tensor` of encodings to a
`tfd.Distribution` instance over images.


def is_type(url: str, message_or_descriptor: annotation_utils.MessageOrDescriptorBase) -> bool:
    return annotation_utils.get_structure_definition_url(message_or_descriptor) == url

Returns True if message_or_descriptor has a structure definition of url.

Args:
url: The FHIR structure definition URL to compare against.
message_or_descriptor: The Message or Descriptor to examine.

Returns:
True if message_or_descriptor has a structure definition equal to url.


def bulk_write(self, metrics):

        actions = []
        index = self.get_index()
        for metric in metrics:
            actions.append({'index': {'_index': index, '_type': self.doc_type}})
            actions.append(metric)
        try:
            self.client.bulk(actions)
        except TransportError as exc:
            logger.warning('bulk_write metrics %r failure %r', metrics, exc)

Write multiple metrics to elasticsearch in one request

Args:
metrics (list): data with mappings to send to elasticsearch


def get_owner_emails(self, partial_owner_match=True):
    for tag in self.tags:
        if (tag.key.lower() == 'owner'):
            rgx = re.compile(RGX_EMAIL_VALIDATION_PATTERN, re.I)
            if partial_owner_match:
                match = rgx.findall(tag.value)
                if match:
                    return [NotificationContact('email', email) for email in match]
            else:
                match = rgx.match(tag.value)
                if match:
                    return [NotificationContact('email', email) for email in match.groups()]
    return None

Return a list of email addresses associated with the instance, based on tags

Returns:
List of email addresses if any, else None


class CombinePerKey(PTransformWithSideInputs):

    def with_hot_key_fanout(self, fanout):

        from apache_beam.transforms.combiners import curry_combine_fn
        if fanout is None:
            return self
        else:
            return _CombinePerKeyWithHotKeyFanout(curry_combine_fn(self.fn, self.args, self.kwargs), fanout)

    def display_data(self):
        return {'combine_fn': DisplayDataItem(self.fn.__class__, label='Combine Function'), 'combine_fn_dd': self.fn}

    def make_fn(self, fn, has_side_inputs):
        self._fn_label = ptransform.label_from_callable(fn)
        return CombineFn.maybe_from_callable(fn, has_side_inputs)

    def default_label(self):
        return '%s(%s)' % (self.__class__.__name__, self._fn_label)

    def _process_argspec_fn(self):
        return lambda element, *args, **kwargs: None

    def expand(self, pcoll):
        args, kwargs = util.insert_values_in_args(self.args, self.kwargs, self.side_inputs)
        return pcoll | GroupByKey() | 'Combine' >> CombineValues(self.fn, *args, **kwargs)

    def default_type_hints(self):
        result = self.fn.get_type_hints()
        k = typehints.TypeVariable('K')
        if result.input_types:
            args, kwargs = result.input_types
            args = (typehints.Tuple[k, args[0]],) + args[1:]
            result = result.with_input_types(*args, **kwargs)
        else:
            result = result.with_input_types(typehints.Tuple[k, typehints.Any])
        if result.output_types:
            main_output_type = result.simple_output_type('')
            result = result.with_output_types(typehints.Tuple[k, main_output_type])
        else:
            result = result.with_output_types(typehints.Tuple[k, typehints.Any])
        return result

    def to_runner_api_parameter(self, context):
        if self.args or self.kwargs:
            from apache_beam.transforms.combiners import curry_combine_fn
            combine_fn = curry_combine_fn(self.fn, self.args, self.kwargs)
        else:
            combine_fn = self.fn
        return (common_urns.composites.COMBINE_PER_KEY.urn, _combine_payload(combine_fn, context))

    @staticmethod
    @PTransform.register_urn(common_urns.composites.COMBINE_PER_KEY.urn, beam_runner_api_pb2.CombinePayload)
    def from_runner_api_parameter(unused_ptransform, combine_payload, context):
        return CombinePerKey(CombineFn.from_runner_api(combine_payload.combine_fn, context))

    def runner_api_requires_keyed_input(self):
        return True

A per-key Combine transform.

Identifies sets of values associated with the same key in the input
PCollection, then applies a CombineFn to condense those sets to single
values. See documentation in CombineFn for details on the specifics on how
CombineFns are applied.

Args:
pcoll: input pcollection.
fn: instance of CombineFn to apply to all values under the same key in
pcoll, or a callable whose signature is ``f(iterable, *args, **kwargs)``
(e.g., sum, max).
*args: arguments and side inputs, passed directly to the CombineFn.
**kwargs: arguments and side inputs, passed directly to the CombineFn.

Returns:
A PObject holding the result of the combine operation.


def AddEventTags(self, event_tags):
    self._RaiseIfNotWritable()
    for event_tag in event_tags:
        self.AddEventTag(event_tag)

Adds event tags.

Args:
event_tags (list[EventTag]): event tags.

Raises:
IOError: when the storage file is closed or read-only or
if the event tags cannot be serialized.
OSError: when the storage file is closed or read-only or
if the event tags cannot be serialized.


def __init__(self, sources, stacker_cache_dir=None):

        if not stacker_cache_dir:
            stacker_cache_dir = os.path.expanduser("~/.stacker")
        package_cache_dir = os.path.join(stacker_cache_dir, 'packages')
        self.stacker_cache_dir = stacker_cache_dir
        self.package_cache_dir = package_cache_dir
        self.sources = sources
        self.configs_to_merge = []
        self.create_cache_directories()

Process a config's defined package sources.

Args:
sources (dict): Package sources from Stacker config dictionary
stacker_cache_dir (string): Path where remote sources will be
cached.


def write(self, output_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):

        local_stream = utils.BytearrayStream()

        if self._unique_identifier is not None:
            self._unique_identifier.write(
                local_stream,
                kmip_version=kmip_version
            )
        if self._key_format_type is not None:
            self._key_format_type.write(
                local_stream,
                kmip_version=kmip_version
            )
        if self._key_compression_type is not None:
            self._key_compression_type.write(
                local_stream,
                kmip_version=kmip_version
            )
        if self._key_wrapping_specification is not None:
            self._key_wrapping_specification.write(
                local_stream,
                kmip_version=kmip_version
            )

        self.length = local_stream.length()
        super(GetRequestPayload, self).write(
            output_stream,
            kmip_version=kmip_version
        )
        output_stream.write(local_stream.buffer)

Write the data encoding the Get request payload to a stream.

Args:
output_stream (stream): A data stream in which to encode object
data, supporting a write method; usually a BytearrayStream
object.
kmip_version (KMIPVersion): An enumeration defining the KMIP
version with which the object will be encoded. Optional,
defaults to KMIP 1.0.


def tf_retrieve_indices(self, indices):
    states = dict()
    for name in sorted(self.states_memory):
        states[name] = tf.gather(params=self.states_memory[name], indices=indices)
    internals = dict()
    for name in sorted(self.internals_memory):
        internals[name] = tf.gather(params=self.internals_memory[name], indices=indices)
    actions = dict()
    for name in sorted(self.actions_memory):
        actions[name] = tf.gather(params=self.actions_memory[name], indices=indices)
    terminal = tf.gather(params=self.terminal_memory, indices=indices)
    reward = tf.gather(params=self.reward_memory, indices=indices)
    if self.include_next_states:
        assert (util.rank(indices) == 1)
        next_indices = ((indices + 1) % self.capacity)
        next_states = dict()
        for name in sorted(self.states_memory):
            next_states[name] = tf.gather(params=self.states_memory[name], indices=next_indices)
        next_internals = dict()
        for name in sorted(self.internals_memory):
            next_internals[name] = tf.gather(params=self.internals_memory[name], indices=next_indices)
        return dict(states=states, internals=internals, actions=actions, terminal=terminal, reward=reward, next_states=next_states, next_internals=next_internals)
    else:
        return dict(states=states, internals=internals, actions=actions, terminal=terminal, reward=reward)

Fetches experiences for given indices.

Args:
indices: Index tensor

Returns: Batch of experiences