def get_nets_jpnic(self, response):

        nets = []

        for match in re.finditer(
                r'^.*?(\[Network Number\])[^\S\n]+.+?>(?P<val>.+?)</A>$',
                response,
                re.MULTILINE
        ):

            try:

                net = copy.deepcopy(BASE_NET)
                tmp = ip_network(match.group(2))

                try:
                    network_address = tmp.network_address
                except AttributeError:
                    network_address = tmp.ip
                    pass

                try:
                    broadcast_address = tmp.broadcast_address
                except AttributeError:
                    broadcast_address = tmp.broadcast
                    pass

                net['range'] = '{0} - {1}'.format(
                    network_address + 1, broadcast_address
                )

                cidr = ip_network(match.group(2).strip()).__str__()

                net['cidr'] = cidr
                net['start'] = match.start()
                net['end'] = match.end()
                nets.append(net)

            except (ValueError, TypeError):

                pass

        return nets

The function for parsing network blocks from jpnic whois data.

Args:
response (:obj:`str`): The response from the jpnic server.

Returns:
list of dict: Mapping of networks with start and end positions.

::

[{
'cidr' (str) - The network routing block
'start' (int) - The starting point of the network
'end' (int) - The endpoint point of the network
}]


def _convert_observ(self, observ):
    if (not np.isfinite(observ).all()):
        raise ValueError('Infinite observation encountered.')
    if (observ.dtype == np.float64):
        return observ.astype(np.float32)
    if (observ.dtype == np.int64):
        return observ.astype(np.int32)
    return observ

Convert the observation to 32 bits.

Args:
observ: Numpy observation.

Raises:
ValueError: Observation contains infinite values.

Returns:
Numpy observation with 32-bit data type.


def console_set_alignment(con: tcod.console.Console, alignment: int) -> None:

    lib.TCOD_console_set_alignment(_console(con), alignment)

Change this consoles current alignment mode.

* tcod.LEFT
* tcod.CENTER
* tcod.RIGHT

Args:
con (Console): Any Console instance.
alignment (int):

.. deprecated:: 8.5
Set :any:`Console.default_alignment` instead.


def get_by_name(self, name):
    managed_sans = self.get_all()
    result = [x for x in managed_sans if (x['name'] == name)]
    resource = (result[0] if result else None)
    if resource:
        resource = self.new(self._connection, resource)
    return resource

Gets a Managed SAN by name.

Args:
name: Name of the Managed SAN

Returns:
dict: Managed SAN.


def fit_transform(self, X, y=None, **params):

        return self.fit(X, y).transform(X, y)

Learn vocabulary and return document id matrix.

This is equivalent to fit followed by transform.

Args:
X : iterable
an iterable which yields either str, unicode or file objects.

Returns:
list : document id matrix.
list: label id matrix.


def _add_qasm_measure(self, qubit, cmembit, cregbit=None):
    (outcome, probability) = self._get_measure_outcome(qubit)
    membit = (1 << cmembit)
    self._classical_memory = ((self._classical_memory & (~ membit)) | (int(outcome) << cmembit))
    if (cregbit is not None):
        regbit = (1 << cregbit)
        self._classical_register = ((self._classical_register & (~ regbit)) | (int(outcome) << cregbit))
    if (outcome == '0'):
        update_diag = [[(1 / np.sqrt(probability)), 0], [0, 0]]
    else:
        update_diag = [[0, 0], [0, (1 / np.sqrt(probability))]]
    self._add_unitary_single(update_diag, qubit)

Apply a measure instruction to a qubit.

Args:
qubit (int): qubit is the qubit measured.
cmembit (int): is the classical memory bit to store outcome in.
cregbit (int, optional): is the classical register bit to store outcome in.


def SetName(obj, name):
    precondition.AssertType(name, str)
    if PY2:
        obj.__name__ = name.encode('ascii')
    else:
        obj.__name__ = name

A compatibility wrapper for setting object's name.

See documentation for `GetName` for more information.

Args:
obj: A type or function object to set the name for.
name: A name to set.


def distance(cls, q0, q1):
    q = Quaternion.log_map(q0, q1)
    return q.norm

Quaternion intrinsic distance.

Find the intrinsic geodesic distance between q0 and q1.

Params:
q0: the first quaternion
q1: the second quaternion

Returns:
A positive amount corresponding to the length of the geodesic arc
connecting q0 to q1.

Note:
Although the q0^(-1)*q1 != q1^(-1)*q0, the length of the path joining
them is given by the logarithm of those product quaternions, the norm
of which is the same.


def repack_weights(packed_parameter: torch.Tensor, sharded_dim: int, world_size: int, num_blocks: int=2) -> torch.Tensor:
    if num_blocks != 2:
        raise ValueError('Num blocks different from 2 is not supported yet. This is most likely a bug in your implementation as we only pack gate and up projections together.')
    actual_sharded_dim = sharded_dim if sharded_dim >= 0 else sharded_dim + packed_parameter.ndim
    total_size_on_sharded_dim = packed_parameter.shape[actual_sharded_dim]
    original_block_size_on_dim = total_size_on_sharded_dim
    shard_chunk_size = original_block_size_on_dim
    prefix_shape = packed_parameter.shape[:actual_sharded_dim]
    suffix_shape = packed_parameter.shape[actual_sharded_dim + 1:]
    tensor_view = packed_parameter.view(*prefix_shape, world_size, num_blocks, shard_chunk_size, *suffix_shape)
    axis_ws_abs = len(prefix_shape)
    axis_npp_abs = len(prefix_shape) + 1
    permute_order = list(range(tensor_view.ndim))
    permute_order[axis_ws_abs], permute_order[axis_npp_abs] = (permute_order[axis_npp_abs], permute_order[axis_ws_abs])
    tensor_permuted = tensor_view.permute(*permute_order)
    final_ordered_tensor = tensor_permuted.reshape_as(packed_parameter)
    return final_ordered_tensor

Reorders a tensor that was reconstructed from sharded packed weights into its canonical packed format.

For example, if a weight was packed (e.g., gate_proj and up_proj) and then sharded,
DTensor.full_tensor() might produce an interleaved layout like [G0, U0, G1, U1, ...]
along the sharded dimension. This function reorders it to [G0, G1, ..., U0, U1, ...].
This is an inverse operation to get_packed_weights.

Args:
reconstructed_tensor: The tensor reconstructed from DTensor (e.g., via .full_tensor().contiguous()).
sharded_dim: The dimension index in the reconstructed_tensor that was originally sharded.
world_size: The tensor parallel world size.
num_packed_projs: The number of projections that were packed together (e.g., 2 for gate_up_proj).

Returns:
The reordered tensor in canonical packed format.


def __init__(self, app, db, UserClass, UserEmailClass=None, UserInvitationClass=None, RoleClass=None):

        self.app = app
        self.db = db
        self.UserClass = UserClass
        self.UserEmailClass = UserEmailClass
        self.UserInvitationClass = UserInvitationClass
        self.RoleClass = RoleClass

        self.user_manager = app.user_manager
        self.db_adapter = None

        if self.db_adapter is None:
            try:
                from flask_sqlalchemy import SQLAlchemy

                if isinstance(db, SQLAlchemy):
                    self.db_adapter = SQLDbAdapter(app, db)
            except ImportError:
                pass

        if self.db_adapter is None:
            try:
                from flask_mongoengine import MongoEngine

                if isinstance(db, MongoEngine):
                    self.db_adapter = MongoDbAdapter(app, db)
            except ImportError:
                pass

        if self.db_adapter is None:
            try:
                from flask_flywheel import Flywheel

                if isinstance(db, Flywheel):
                    self.db_adapter = DynamoDbAdapter(app, db)
            except ImportError:
                pass

        if self.db_adapter is None:
            try:
                from pynamodb.models import Model

                if issubclass(UserClass, Model):
                    self.db_adapter = PynamoDbAdapter(app)
            except ImportError:
                pass

        if self.db_adapter is None:
            raise ConfigError(
                'No Flask-SQLAlchemy, Flask-MongoEngine or Flask-Flywheel installed and no Pynamo Model in use.'\
                ' You must install one of these Flask extensions.')

Initialize the appropriate DbAdapter, based on the ``db`` parameter type.

Args:
app(Flask): The Flask application instance.
db: The Object-Database Mapper instance.
UserClass: The User class.
UserEmailClass: Optional UserEmail class for multiple-emails-per-user feature.
UserInvitationClass: Optional UserInvitation class for user-invitation feature.
RoleClass: For testing purposes only.


def input(self):
    return self._nested_inputs

Retrieves the input tensor(s) of a layer.

Only applicable if the layer has exactly one input,
i.e. if it is connected to one incoming layer.

Returns:
Input tensor or list of input tensors.

Raises:
RuntimeError: If called in Eager mode.
AttributeError: If no inbound nodes are found.


def _GetMetadataUpdate(
      self, metadata_key='', recursive=True, wait=True, timeout=None):

    metadata_key = os.path.join(metadata_key, '') if recursive else metadata_key
    metadata_url = os.path.join(METADATA_SERVER, metadata_key)
    params = {
        'alt': 'json',
        'last_etag': self.etag,
        'recursive': recursive,
        'timeout_sec': timeout or self.timeout,
        'wait_for_change': wait,
    }
    while True:
      response = self._GetMetadataRequest(
          metadata_url, params=params, timeout=timeout)
      etag_updated = self._UpdateEtag(response)
      if wait and not etag_updated and not timeout:

        continue
      else:

        break
    return json.loads(response.read().decode('utf-8'))

Request the contents of metadata server and deserialize the response.

Args:
metadata_key: string, the metadata key to watch for changes.
recursive: bool, True if we should recursively watch for metadata changes.
wait: bool, True if we should wait for a metadata change.
timeout: int, timeout in seconds for returning metadata output.

Returns:
json, the deserialized contents of the metadata server.


def run(self, *args, **kwargs):

        self.log.debug('Starting EBSAuditor')
        data = self.update_data()

        notices = defaultdict(list)
        for account, issues in data.items():
            for issue in issues:
                for recipient in account.contacts:
                    notices[NotificationContact(type=recipient['type'], value=recipient['value'])].append(issue)

        self.notify(notices)

Main execution point for the auditor

Args:
*args:
**kwargs:

Returns:
`None`


def split_to_tiles(image: np.ndarray, num_tiles_height: int, num_tiles_width: int) -> np.ndarray:
    num_channels, height, width = image.shape
    tile_height = height
    tile_width = width
    image = image.reshape(num_channels, num_tiles_height, tile_height, num_tiles_width, tile_width)
    image = image.transpose(1, 3, 0, 2, 4)
    image = image.reshape(num_tiles_width * num_tiles_height, num_channels, tile_height, tile_width)
    return np.ascontiguousarray(image)

Split an image into a specified number of tiles along its width and height dimensions.

Args:
image (`np.ndarray`):
Input image with shape (num_channels, height, width).
num_tiles_height (`int`):
Number of tiles to split the image into along its height.
num_tiles_width (`int`):
Number of tiles to split the image into along its width.

Returns:
`np.ndarray`:
Array of image tiles with shape (num_tiles_width * num_tiles_height, num_channels, tile_height, tile_width).


def make_slot_check(wanted):
    if isinstance(wanted, types.FunctionType):
        return wanted
    if isinstance(wanted, int):
        (item, meta) = (wanted, None)
    elif isinstance(wanted, Slot):
        (item, meta) = (wanted.item_id, wanted.damage)
    elif isinstance(wanted, (Item, Block)):
        (item, meta) = (wanted.id, wanted.metadata)
    elif isinstance(wanted, str):
        item_or_block = get_item_or_block(wanted, init=True)
        (item, meta) = (item_or_block.id, item_or_block.metadata)
    else:
        try:
            (item, meta) = wanted
        except TypeError:
            raise ValueError(('Illegal args for make_slot_check(): %s' % wanted))
    return (lambda slot: ((item == slot.item_id) and (meta in (None, slot.damage))))

Creates and returns a function that takes a slot
and checks if it matches the wanted item.

Args:
wanted: function(Slot) or Slot or itemID or (itemID, metadata)


def clientConnectionFailed(self, err, address: Address):

        if type(err.value) == error.TimeoutError:
            logger.debug(f"Failed connecting to {address} connection timed out")
        elif type(err.value) == error.ConnectError:
            ce = err.value
            if len(ce.args) > 0:
                logger.debug(f"Failed connecting to {address} {ce.args[0].value}")
            else:
                logger.debug(f"Failed connecting to {address}")
        else:
            logger.debug(f"Failed connecting to {address} {err.value}")
        self.peers_connecting -= 1
        self.RemoveKnownAddress(address)
        self.RemoveFromQueue(address)

        self.AddDeadAddress(address)

        return err.type

Called when we fail to connect to an endpoint
Args:
err: Twisted Failure instance
address: the address we failed to connect to


def cut_matrix(self, n):

        return connectivity.relevant_connections(n, self.from_nodes,
                                                 self.to_nodes)

Compute the cut matrix for this cut.

The cut matrix is a square matrix which represents connections severed
by the cut.

Args:
n (int): The size of the network.

Example:
>>> cut = Cut((1,), (2,))
>>> cut.cut_matrix(3)
array([[0., 0., 0.],
[0., 0., 1.],
[0., 0., 0.]])


def ScanForFileSystem(self, source_path_spec):
    if (source_path_spec.type_indicator == definitions.TYPE_INDICATOR_APFS_CONTAINER):
        return path_spec_factory.Factory.NewPathSpec(definitions.TYPE_INDICATOR_APFS, location='/', parent=source_path_spec)
    try:
        type_indicators = analyzer.Analyzer.GetFileSystemTypeIndicators(source_path_spec, resolver_context=self._resolver_context)
    except RuntimeError as exception:
        raise errors.BackEndError('Unable to process source path specification with error: {0!s}'.format(exception))
    if (not type_indicators):
        return None
    type_indicator = type_indicators[0]
    if (len(type_indicators) > 1):
        if (definitions.PREFERRED_NTFS_BACK_END not in type_indicators):
            raise errors.BackEndError('Unsupported source found more than one file system types.')
        type_indicator = definitions.PREFERRED_NTFS_BACK_END
    if (type_indicator == definitions.TYPE_INDICATOR_NTFS):
        root_location = '\\'
    else:
        root_location = '/'
    file_system_path_spec = path_spec_factory.Factory.NewPathSpec(type_indicator, location=root_location, parent=source_path_spec)
    if (type_indicator == definitions.TYPE_INDICATOR_TSK):
        try:
            file_system = resolver.Resolver.OpenFileSystem(file_system_path_spec, resolver_context=self._resolver_context)
            file_system.Close()
        except errors.BackEndError:
            file_system_path_spec = None
    return file_system_path_spec

Scans the path specification for a supported file system format.

Args:
source_path_spec (PathSpec): source path specification.

Returns:
PathSpec: file system path specification or None if no supported file
system type was found.

Raises:
BackEndError: if the source cannot be scanned or more than one file
system type is found.


def num_employers(self, num_employers):
    if (num_employers < 2):
        self._logger.log('warn', 'Two employers are needed: setting to two')
        num_employers = 2
    self._num_employers = num_employers
    self._logger.log('debug', 'Number of employers set to {}'.format(num_employers))
    self._limit = (num_employers * len(self._value_ranges))
    self._logger.log('debug', 'Limit set to {}'.format(self._limit))

Sets the number of employer bees; at least two are required

Args:
num_employers (int): number of employer bees


def un(byts):
    return msgpack.loads(byts, use_list=False, raw=False, unicode_errors='surrogatepass')

Use msgpack to de-serialize a python object.

Args:
byts (bytes): The bytes to de-serialize

Notes:
String objects are decoded using utf8 encoding.  In order to handle
potentially malformed input, ``unicode_errors='surrogatepass'`` is set
to allow decoding bad input strings.

Returns:
obj: The de-serialized object


def read_from_source(source, start_position=None, stop_position=None):
    values = []
    range_tracker = source.get_range_tracker(start_position, stop_position)
    assert isinstance(range_tracker, iobase.RangeTracker)
    reader = source.read(range_tracker)
    for value in reader:
        values.append(value)
    return values

Reads elements from the given ```BoundedSource```.

Only reads elements within the given position range.
Args:
source (~apache_beam.io.iobase.BoundedSource):
:class:`~apache_beam.io.iobase.BoundedSource` implementation.
start_position (int): start position for reading.
stop_position (int): stop position for reading.

Returns:
List[str]: the set of values read from the sources.


def get_model_filepath(self, infodict):

        u = infodict['uniprot_ac']

        original_filename = '{}_{}_{}_{}'.format(infodict['from'], infodict['to'],
                                                 infodict['template'], infodict['coordinate_id'])
        file_path = op.join(self.metadata_dir, u[:2], u[2:4], u[4:6],
                            'swissmodel', '{}.pdb'.format(original_filename))

        if op.exists(file_path):
            return file_path
        else:
            log.warning('{}: no file {} found for model'.format(u, file_path))
            return None

Get the path to the homology model using information from the index dictionary for a single model.

Example: use self.get_models(UNIPROT_ID) to get all the models, which returns a list of dictionaries.
Use one of those dictionaries as input to this function to get the filepath to the model itself.

Args:
infodict (dict): Information about a model from get_models

Returns:
str: Path to homology model


def raisefrom(exc_type, message, exc):
    if (sys.version_info[:2] >= (3, 2)):
        six.raise_from(exc_type(message), exc)
    else:
        six.reraise(exc_type, ('%s - %s' % (message, exc)), sys.exc_info()[2])

Call Python 3 raise from or emulate it for Python 2

Args:
exc_type (Any): Type of Exception
message (str): Error message to display
exc (BaseException): original exception

Returns:
None


def find_tested_models(test_file: str) -> List[str]:
    with open(os.path.join(PATH_TO_TESTS, test_file), 'r', encoding='utf-8', newline='\n') as f:
        content = f.read()
    all_models = re.findall('all_model_classes\\s+=\\s+\\(\\s*\\(([^\\)]*)\\)', content)
    all_models += re.findall('all_model_classes\\s+=\\s+\\(([^\\)]*)\\)', content)
    if len(all_models) > 0:
        model_tested = []
        for entry in all_models:
            for line in entry.split(','):
                name = line.strip()
                if len(name) > 0:
                    model_tested.append(name)
        return model_tested

Parse the content of test_file to detect what's in `all_model_classes`. This detects the models that inherit from
the common test class.

Args:
test_file (`str`): The path to the test file to check

Returns:
`List[str]`: The list of models tested in that file.


def add_from_existing(self, resource, timeout=-1):

        uri = self.URI + "/from-existing"
        return self._client.create(resource, uri=uri, timeout=timeout)

Adds a volume that already exists in the Storage system

Args:
resource (dict):
Object to create.
timeout:
Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation
in OneView, just stop waiting for its completion.

Returns:
dict: Added resource.


def isCaCert(self, name):

        crtpath = self._getPathJoin('cas', '%s.crt' % name)
        return os.path.isfile(crtpath)

Checks if a CA certificate exists.

Args:
name (str): The name of the CA keypair.

Examples:
Check if the CA certificate for "myca" exists:

exists = cdir.isCaCert('myca')

Returns:
bool: True if the certificate is present, False otherwise.


def query_snl(self, criteria):

        try:
            payload = {"criteria": json.dumps(criteria)}
            response = self.session.post("{}/snl/query".format(self.preamble),
                                         data=payload)
            if response.status_code in [200, 400]:
                resp = json.loads(response.text)
                if resp["valid_response"]:
                    if resp.get("warning"):
                        warnings.warn(resp["warning"])
                    return resp["response"]
                else:
                    raise MPRestError(resp["error"])

            raise MPRestError("REST error with status code {} and error {}"
                              .format(response.status_code, response.text))

        except Exception as ex:
            raise MPRestError(str(ex))

Query for submitted SNLs.

.. note::

As of now, this MP REST feature is open only to a select group of
users. Opening up submissions to all users is being planned for
the future.

Args:
criteria (dict): Query criteria.

Returns:
A dict, with a list of submitted SNLs in the "response" key.

Raises:
MPRestError


def __add__(self, r):

        if not isinstance(r, TestResult):
            raise TypeError('Operand %s of type %s is not a TestResult.' %
                            (r, type(r)))
        sum_result = TestResult()
        for name in sum_result.__dict__:
            r_value = getattr(r, name)
            l_value = getattr(self, name)
            if isinstance(r_value, list):
                setattr(sum_result, name, l_value + r_value)
        return sum_result

Overrides '+' operator for TestResult class.

The add operator merges two TestResult objects by concatenating all of
their lists together.

Args:
r: another instance of TestResult to be added

Returns:
A TestResult instance that's the sum of two TestResult instances.


def delay(self, identifier: typing.Any, until: typing.Union[(int, float)]=(- 1)) -> bool:
    raise NotImplementedError()

Delay a deferred function until the given time.

Args:
identifier (typing.Any): The identifier returned from a call
to defer or defer_for.
until (typing.Union[int, float]): A numeric value that represents
the clock time when the callback becomes available for
execution. Values that are less than the current time result in
the function being called at the next opportunity.

Returns:
bool: True if the call is delayed. False if the identifier is
invalid or if the deferred call is already executed.


def fetch(self, customer_id, data={}, **kwargs):

        return super(Customer, self).fetch(customer_id, data, **kwargs)

Fetch Customer for given Id

Args:
customer_id : Id for which customer object has to be retrieved

Returns:
Order dict for given customer Id


def new_log_files(self, name, redirect_output=True):
    if (redirect_output is None):
        redirect_output = self._ray_params.redirect_output
    if (not redirect_output):
        return (None, None)
    log_stdout = self._make_inc_temp(suffix='.out', prefix=name, directory_name=self._logs_dir)
    log_stderr = self._make_inc_temp(suffix='.err', prefix=name, directory_name=self._logs_dir)
    log_stdout_file = open(log_stdout, 'a', buffering=1)
    log_stderr_file = open(log_stderr, 'a', buffering=1)
    return (log_stdout_file, log_stderr_file)

Generate partially randomized filenames for log files.

Args:
name (str): descriptive string for this log file.
redirect_output (bool): True if files should be generated for
logging stdout and stderr and false if stdout and stderr
should not be redirected.
If it is None, it will use the "redirect_output" Ray parameter.

Returns:
If redirect_output is true, this will return a tuple of two
file handles. The first is for redirecting stdout and the
second is for redirecting stderr.
If redirect_output is false, this will return a tuple
of two None objects.


def _create_job_info(self, job_dir):

        meta = self._build_job_meta(job_dir)

        self.logger.debug("Create job: %s" % meta)

        job_record = JobRecord.from_json(meta)
        job_record.save()

Create information for given job.

Meta file will be loaded if exists, and the job information will
be saved in db backend.

Args:
job_dir (str): Directory path of the job.


def parse_rsa_data(rsa_outfile, ignore_hets=True):

    naccess_rel_dict = OrderedDict()

    with open(rsa_outfile, 'r') as f:
        for line in f:
            if line.startswith('RES'):
                res_name = line[4:7]
                chain_id = line[8]
                resseq = int(line[9:13])
                icode = line[13]
                res_id = (' ', resseq, icode)
                all_atoms_abs = line[16:22].strip()
                all_atoms_rel = line[23:28].strip()
                side_chain_abs = line[29:35].strip()
                side_chain_rel = line[36:41].strip()
                main_chain_abs = line[42:48].strip()
                main_chain_rel = line[49:54].strip()
                non_polar_abs = line[55:61].strip()
                non_polar_rel = line[62:67].strip()
                all_polar_abs = line[68:74].strip()
                all_polar_rel = line[75:80].strip()

                if all_atoms_rel =='N/A' and main_chain_rel =='N/A' and all_polar_rel =='N/A' and non_polar_rel =='N/A' and side_chain_rel =='N/A' and ignore_hets:
                    continue

                naccess_rel_dict[(chain_id, res_id)] = {
                    'res_name'      : res_name,
                    'all_atoms_abs' : ssbio.utils.conv_to_float(all_atoms_abs, inf_str='N/A'),
                    'all_atoms_rel' : ssbio.utils.conv_to_float(all_atoms_rel, inf_str='N/A'),
                    'side_chain_abs': ssbio.utils.conv_to_float(side_chain_abs, inf_str='N/A'),
                    'side_chain_rel': ssbio.utils.conv_to_float(side_chain_rel, inf_str='N/A'),
                    'main_chain_abs': ssbio.utils.conv_to_float(main_chain_abs, inf_str='N/A'),
                    'main_chain_rel': ssbio.utils.conv_to_float(main_chain_rel, inf_str='N/A'),
                    'non_polar_abs' : ssbio.utils.conv_to_float(non_polar_abs, inf_str='N/A'),
                    'non_polar_rel' : ssbio.utils.conv_to_float(non_polar_rel, inf_str='N/A'),
                    'all_polar_abs' : ssbio.utils.conv_to_float(all_polar_abs, inf_str='N/A'),
                    'all_polar_rel' : ssbio.utils.conv_to_float(all_polar_rel, inf_str='N/A')}

    return naccess_rel_dict

Process a NACCESS or freesasa RSA output file. Adapted from Biopython NACCESS modele.

Args:
rsa_outfile (str): Path to RSA output file
ignore_hets (bool): If HETATMs should be excluded from the final dictionary. This is extremely important
when loading this information into a ChainProp's SeqRecord, since this will throw off the sequence matching.

Returns:
dict: Per-residue dictionary of RSA values


def _create_flow(self, request_handler):

        if self.flow is None:
            redirect_uri = request_handler.request.relative_url(
                self._callback_path)
            self.flow = client.OAuth2WebServerFlow(
                self._client_id, self._client_secret, self._scope,
                redirect_uri=redirect_uri, user_agent=self._user_agent,
                auth_uri=self._auth_uri, token_uri=self._token_uri,
                revoke_uri=self._revoke_uri, **self._kwargs)

Create the Flow object.

The Flow is calculated lazily since we don't know where this app is
running until it receives a request, at which point redirect_uri can be
calculated and then the Flow object can be constructed.

Args:
request_handler: webapp.RequestHandler, the request handler.


def CheckDataVisiblity(self, value):
    if (not self.data_visibility_policy):
        return None
    (visible, reason) = self.data_visibility_policy.IsDataVisible(DetermineType(value))
    if visible:
        return None
    return {'status': {'isError': True, 'refersTo': 'VARIABLE_NAME', 'description': {'format': reason}}}

Returns a status object if the given name is not visible.

Args:
value: The value to check.  The actual value here is not important but the
value's metadata (e.g. package and type) will be checked.

Returns:
None if the value is visible.  A variable structure with an error status
if the value should not be visible.


def _verify_parsed_token(parsed_token, issuers, audiences, allowed_client_ids, is_legacy_google_auth=True):

  if parsed_token.get('iss') not in issuers:
    _logger.warning('Issuer was not valid: %s', parsed_token.get('iss'))
    return False

  aud = parsed_token.get('aud')
  if not aud:
    _logger.warning('No aud field in token')
    return False

  cid = parsed_token.get('azp')
  audience_allowed = (aud in audiences) or (is_legacy_google_auth and aud == cid)
  if not audience_allowed:
    _logger.warning('Audience not allowed: %s', aud)
    return False

  if is_legacy_google_auth:
    if list(allowed_client_ids) == SKIP_CLIENT_ID_CHECK:
      _logger.warning('Client ID check can\'t be skipped for ID tokens.  '
                      'Id_token cannot be verified.')
      return False
    elif not cid or cid not in allowed_client_ids:
      _logger.warning('Client ID is not allowed: %s', cid)
      return False

  if 'email' not in parsed_token:
    return False

  return True

Verify a parsed user ID token.

Args:
parsed_token: The parsed token information.
issuers: A list of allowed issuers
audiences: The allowed audiences.
allowed_client_ids: The allowed client IDs.

Returns:
True if the token is verified, False otherwise.


def FinalizeTaskStorage(self, task):

    if task.identifier not in self._task_storage_writers:
      raise IOError('Storage writer for task: {0:s} does not exist.'.format(
          task.identifier))

Finalizes a processed task storage.

Args:
task (Task): task.

Raises:
IOError: if the task storage does not exist.
OSError: if the task storage does not exist.


def delete_folder(self, folder):

        if not is_valid_uuid(folder):
            raise StorageArgumentException(
                'Invalid UUID for folder: {0}'.format(folder))
        self._authenticated_request \
            .to_endpoint('folder/{}/'.format(folder)) \
            .delete()

Delete a folder. It will recursively delete all the content.

Args:
folder_id (str): The UUID of the folder to be deleted.

Returns:
None

Raises:
StorageArgumentException: Invalid arguments
StorageForbiddenException: 403
StorageNotFoundException: 404
HTTPError: other non-20x error codes


def __init__(self, subdir, experiment_name, run_name):

    self._subdir = subdir
    self._experiment_name = experiment_name
    self._run_name = run_name
    self._directory_watcher = directory_watcher.DirectoryWatcher(
        subdir,
        event_file_loader.RawEventFileLoader,
        io_wrapper.IsTensorFlowEventsFile)

Constructs a `_RunLoader`.

Args:
subdir: string, filesystem path of the run directory
experiment_name: string, name of the run's experiment
run_name: string, name of the run


def new_typed_dict(self, name, items, keywords):
    cls_name = escape.pack_typeddict_base_class(name, len(self.generated_classes[name]))
    processed_keywords = []
    for k in keywords:
        if k.arg != 'total':
            raise _ParseError(f'Unexpected kwarg {k.arg!r} passed to TypedDict')
        if not isinstance(k.value, types.Pyval) or not isinstance(k.value.value, bool):
            raise _ParseError(f"Illegal value {k.value!r} for 'total' kwarg to TypedDict")
        processed_keywords.append((k.arg, k.value.to_pytd_literal()))
    constants = tuple((pytd.Constant(k, v) for k, v in items.items()))
    cls = pytd.Class(name=cls_name, keywords=tuple(processed_keywords), bases=(pytd.NamedType('typing.TypedDict'),), methods=(), constants=constants, decorators=(), classes=(), slots=None, template=())
    self.generated_classes[name].append(cls)
    self.add_import('typing', ['TypedDict'])
    return pytd.NamedType(cls_name)

Returns a type for a TypedDict.

This method is called only for TypedDict objects defined via the following
function-based syntax:

Foo = TypedDict('Foo', {'a': int, 'b': str}, total=False)

rather than the recommended class-based syntax.

Args:
name: the name of the TypedDict instance, e.g., "'Foo'".
items: a {key: value_type} dict, e.g., {"'a'": "int", "'b'": "str"}.
keywords: A sequence of kwargs passed to the function.


def to_value(original_string, corenlp_value=None):
    if isinstance(original_string, Value):
        return original_string
    if (not corenlp_value):
        corenlp_value = original_string
    amount = NumberValue.parse(corenlp_value)
    if (amount is not None):
        return NumberValue(amount, original_string)
    ymd = DateValue.parse(corenlp_value)
    if (ymd is not None):
        if (ymd[1] == ymd[2] == (- 1)):
            return NumberValue(ymd[0], original_string)
        else:
            return DateValue(ymd[0], ymd[1], ymd[2], original_string)
    return StringValue(original_string)

Convert the string to Value object.

Args:
original_string (basestring): Original string
corenlp_value (basestring): Optional value returned from CoreNLP
Returns:
Value


def add_affiliation(self, value, curated_relation=None, record=None):

        if value:
            affiliation = {
                'value': value
            }
            if record:
                affiliation['record'] = record
            if curated_relation is not None:
                affiliation['curated_relation'] = curated_relation
            self._ensure_list_field('affiliations', affiliation)

Add an affiliation.

Args:
value (string): affiliation value
curated_relation (bool): is relation curated
record (dict): affiliation JSON reference


def remove(self, email):
    if (email in self._collaborators):
        if (self._collaborators[email] == ShareRequestValue.Add):
            del self._collaborators[email]
        else:
            self._collaborators[email] = ShareRequestValue.Remove
    self._dirty = True

Remove a Collaborator.

Args:
str : Collaborator email address.


def get_component(self, colour, tolerance=0, default=None):
    if (not (0 <= tolerance <= np.sqrt(195075))):
        raise LegendError('Tolerance must be between 0 and 441.67')
    for decor in self.__list:
        if (colour.lower() == decor.colour):
            return decor.component
    (r1, g1, b1) = utils.hex_to_rgb(colour)
    best_match = '
    best_match_dist = np.sqrt((((r1 ** 2.0) + (g1 ** 2.0)) + (b1 ** 2.0)))
    for decor in self.__list:
        (r2, g2, b2) = decor.rgb
        distance = np.sqrt(((((r2 - r1) ** 2.0) + ((g2 - g1) ** 2.0)) + ((b2 - b1) ** 2.0)))
        if (distance < best_match_dist):
            best_match = decor.component
            best_match_dist = distance
            best_match_colour = decor.colour
    if (best_match_dist <= tolerance):
        return best_match
    else:
        with warnings.catch_warnings():
            warnings.simplefilter('always')
            w = 'No match found for {0} '.format(colour.lower())
            w += 'with tolerance of {0}. Best match is '.format(tolerance)
            w += '{0}, {1}'.format(best_match.summary(), best_match_colour)
            w += ', d={0}'.format(best_match_dist)
            warnings.warn(w)
        return default

Get the component corresponding to a display colour. This is for
generating a Striplog object from a colour image of a striplog.

Args:
colour (str): The hex colour string to look up.
tolerance (float): The colourspace distance within which to match.
default (component or None): The component to return in the event
of no match.

Returns:
component. The component best matching the provided colour.


def _send(self, method, path, data, filename):

        if filename is None:
            return self._send_json(method, path, data)
        else:
            return self._send_file(method, path, data, filename)

Send data to a remote server, either with a POST or a PUT request.

Args:
`method`: The method (POST or PUT) to use.
`path`: The path to the resource.
`data`: The data to send.
`filename`: The filename of the file to send (if any).
Returns:
The content of the response.
Raises:
An exception depending on the HTTP status code of the response.


def GetRelativePath(self, path_spec):

    location = getattr(path_spec, 'location', None)
    if location is None:
      raise errors.PathSpecError('Path specification missing location.')

    if path_spec_factory.Factory.IsSystemLevelTypeIndicator(
        self._file_system.type_indicator):
      if not location.startswith(self._mount_point.location):
        raise errors.PathSpecError(
            'Path specification does not contain mount point.')
    else:
      if not hasattr(path_spec, 'parent'):
        raise errors.PathSpecError('Path specification missing parent.')

      if path_spec.parent != self._mount_point:
        raise errors.PathSpecError(
            'Path specification does not contain mount point.')

    path_segments = self._file_system.SplitPath(location)

    if path_spec_factory.Factory.IsSystemLevelTypeIndicator(
        self._file_system.type_indicator):
      mount_point_path_segments = self._file_system.SplitPath(
          self._mount_point.location)
      path_segments = path_segments[len(mount_point_path_segments):]

    return '{0:s}{1:s}'.format(
        self._file_system.PATH_SEPARATOR,
        self._file_system.PATH_SEPARATOR.join(path_segments))

Returns the relative path based on a resolved path specification.

The relative path is the location of the upper most path specification.
The the location of the mount point is stripped off if relevant.

Args:
path_spec (PathSpec): path specification.

Returns:
str: corresponding relative path or None if the relative path could not
be determined.

Raises:
PathSpecError: if the path specification is incorrect.


def inet_to_str(inet):
    try:
        return socket.inet_ntop(socket.AF_INET, inet)
    except ValueError:
        return socket.inet_ntop(socket.AF_INET6, inet)

Convert inet object to a string

Args:
inet (inet struct): inet network address
Returns:
str: Printable/readable IP address


def destroy_iam(app='', env='dev', **_):

    session = boto3.Session(profile_name=env)
    client = session.client('iam')

    generated = get_details(env=env, app=app)
    generated_iam = generated.iam()
    app_details = collections.namedtuple('AppDetails', generated_iam.keys())
    details = app_details(**generated_iam)

    LOG.debug('Application details: %s', details)

    resource_action(
        client,
        action='remove_user_from_group',
        log_format='Removed user from group: %(UserName)s ~> %(GroupName)s',
        GroupName=details.group,
        UserName=details.user)
    resource_action(client, action='delete_user', log_format='Destroyed user: %(UserName)s', UserName=details.user)
    resource_action(client, action='delete_group', log_format='Destroyed group: %(GroupName)s', GroupName=details.group)

    resource_action(
        client,
        action='remove_role_from_instance_profile',
        log_format='Destroyed Instance Profile from Role: '
        '%(InstanceProfileName)s ~> %(RoleName)s',
        InstanceProfileName=details.profile,
        RoleName=details.role)
    resource_action(
        client,
        action='delete_instance_profile',
        log_format='Destroyed Instance Profile: %(InstanceProfileName)s',
        InstanceProfileName=details.profile)

    role_policies = []
    try:
        role_policies = resource_action(
            client,
            action='list_role_policies',
            log_format='Found Role Policies for %(RoleName)s.',
            RoleName=details.role)['PolicyNames']
    except TypeError:
        LOG.info('Role %s not found.', details.role)

    for policy in role_policies:
        resource_action(
            client,
            action='delete_role_policy',
            log_format='Removed Inline Policy from Role: '
            '%(PolicyName)s ~> %(RoleName)s',
            RoleName=details.role,
            PolicyName=policy)

    attached_role_policies = []
    try:
        attached_role_policies = resource_action(
            client,
            action='list_attached_role_policies',
            log_format='Found attached Role Polices for %(RoleName)s.',
            RoleName=details.role)['AttachedPolicies']
    except TypeError:
        LOG.info('Role %s not found.', details.role)

    for policy in attached_role_policies:
        resource_action(
            client,
            action='detach_role_policy',
            log_format='Detached Policy from Role: '
            '%(PolicyArn)s ~> %(RoleName)s',
            RoleName=details.role,
            PolicyArn=policy['PolicyArn'])

    resource_action(client, action='delete_role', log_format='Destroyed Role: %(RoleName)s', RoleName=details.role)

Destroy IAM Resources.

Args:
app (str): Spinnaker Application name.
env (str): Deployment environment, i.e. dev, stage, prod.

Returns:
True upon successful completion.


def movie_list(self, **kwargs):

        path = self._get_path('movie_list')

        response = self._GET(path, kwargs)
        self._set_attrs_to_values(response)
        return response

Get the list of Movie genres.

Args:
language: (optional) ISO 639-1 code.

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


def get_mnemonic(self, mnemonic, alias=None):
    alias = (alias or {})
    aliases = alias.get(mnemonic, [mnemonic])
    for a in aliases:
        if (a in self.data):
            return a
    return None

Instead of picking curves by name directly from the data dict, you
can pick them up with this method, which takes account of the alias
dict you pass it. If you do not pass an alias dict, then you get the
curve you asked for, if it exists, or None. NB Wells do not have alias
dicts, but Projects do.

Args:
mnemonic (str): the name of the curve you want.
alias (dict): an alias dictionary, mapping mnemonics to lists of
mnemonics.

Returns:
Curve.


def ParseFileObject(self, parser_mediator, file_object):
    file_offset = 0
    file_size = file_object.get_size()
    record_map = self._GetDataTypeMap('pls_recall_record')
    while (file_offset < file_size):
        try:
            (pls_record, record_data_size) = self._ReadStructureFromFileObject(file_object, file_offset, record_map)
        except (ValueError, errors.ParseError) as exception:
            if (file_offset == 0):
                raise errors.UnableToParseFile('Unable to parse first record.')
            parser_mediator.ProduceExtractionWarning('unable to parse record at offset: 0x{0:08x} with error: {1!s}'.format(file_offset, exception))
            break
        if ((file_offset == 0) and (not self._VerifyRecord(pls_record))):
            raise errors.UnableToParseFile('Verification of first record failed.')
        event_data = PlsRecallEventData()
        event_data.database_name = pls_record.database_name.rstrip('\x00')
        event_data.sequence_number = pls_record.sequence_number
        event_data.offset = file_offset
        event_data.query = pls_record.query.rstrip('\x00')
        event_data.username = pls_record.username.rstrip('\x00')
        date_time = dfdatetime_delphi_date_time.DelphiDateTime(timestamp=pls_record.last_written_time)
        event = time_events.DateTimeValuesEvent(date_time, definitions.TIME_DESCRIPTION_WRITTEN)
        parser_mediator.ProduceEventWithEventData(event, event_data)
        file_offset += record_data_size

Parses a PLSRecall.dat file-like object.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
file_object (dfvfs.FileIO): a file-like object.

Raises:
UnableToParseFile: when the file cannot be parsed.


def loss(logits, labels, batch_size=None):

  if not batch_size:
    batch_size = FLAGS.batch_size

  sparse_labels = tf.reshape(labels, [batch_size, 1])
  indices = tf.reshape(tf.range(batch_size), [batch_size, 1])
  concated = tf.concat(axis=1, values=[indices, sparse_labels])
  num_classes = logits[0].get_shape()[-1].value
  dense_labels = tf.sparse_to_dense(concated,
                                    [batch_size, num_classes],
                                    1.0, 0.0)

  slim.losses.cross_entropy_loss(logits[0],
                                 dense_labels,
                                 label_smoothing=0.1,
                                 weight=1.0)

  slim.losses.cross_entropy_loss(logits[1],
                                 dense_labels,
                                 label_smoothing=0.1,
                                 weight=0.4,
                                 scope='aux_loss')

Adds all losses for the model.

Note the final loss is not returned. Instead, the list of losses are collected
by slim.losses. The losses are accumulated in tower_loss() and summed to
calculate the total loss.

Args:
logits: List of logits from inference(). Each entry is a 2-D float Tensor.
labels: Labels from distorted_inputs or inputs(). 1-D tensor
of shape [batch_size]
batch_size: integer


def _maintain_LC(self, obj, slice_id, last_slice=False, begin_slice=True,
                   shard_ctx=None, slice_ctx=None):

    if obj is None or not isinstance(obj, shard_life_cycle._ShardLifeCycle):
      return

    shard_context = shard_ctx or self.shard_context
    slice_context = slice_ctx or self.slice_context
    if begin_slice:
      if slice_id == 0:
        obj.begin_shard(shard_context)
      obj.begin_slice(slice_context)
    else:
      obj.end_slice(slice_context)
      if last_slice:
        obj.end_shard(shard_context)

Makes sure shard life cycle interface are respected.

Args:
obj: the obj that may have implemented _ShardLifeCycle.
slice_id: current slice_id
last_slice: whether this is the last slice.
begin_slice: whether this is the beginning or the end of a slice.
shard_ctx: shard ctx for dependency injection. If None, it will be read
from self.
slice_ctx: slice ctx for dependency injection. If None, it will be read
from self.


def update(self, **kwargs):
    for arg in kwargs:
        if hasattr(self, arg):
            setattr(self, arg, kwargs[arg])
        else:
            raise ValueError(('Invalid RayParams parameter in update: %s' % arg))
    self._check_usage()

Update the settings according to the keyword arguments.

Args:
kwargs: The keyword arguments to set corresponding fields.


def assert_like_rnncell(cell_name, cell):
    conditions = [_hasattr(cell, 'output_size'), _hasattr(cell, 'state_size'), _hasattr(cell, 'get_initial_state') or _hasattr(cell, 'zero_state'), callable(cell)]
    errors = ["'output_size' property is missing", "'state_size' property is missing", "either 'zero_state' or 'get_initial_state' method is required", 'is not callable']
    if not all(conditions):
        errors = [error for error, cond in zip(errors, conditions) if not cond]
        raise TypeError('The argument {!r} ({}) is not an RNNCell: {}.'.format(cell_name, cell, ', '.join(errors)))

Raises a TypeError if cell is not like an RNNCell.

NOTE: Do not rely on the error message (in particular in tests) which can be
subject to change to increase readability. Use
ASSERT_LIKE_RNNCELL_ERROR_REGEXP.

Args:
cell_name: A string to give a meaningful error referencing to the name of
the functionargument.
cell: The object which should behave like an RNNCell.

Raises:
TypeError: A human-friendly exception.


def step(self, actions):

    for index, (env, action) in enumerate(zip(self._envs, actions)):
      if not env.action_space.contains(action):
        message = 'Invalid action at index {}: {}'
        raise ValueError(message.format(index, action))
    if self._blocking:
      transitions = [
          env.step(action)
          for env, action in zip(self._envs, actions)]
    else:
      transitions = [
          env.step(action, blocking=False)
          for env, action in zip(self._envs, actions)]
      transitions = [transition() for transition in transitions]
    observs, rewards, dones, infos = zip(*transitions)
    observ = np.stack(observs)
    reward = np.stack(rewards)
    done = np.stack(dones)
    info = tuple(infos)
    return observ, reward, done, info

Forward a batch of actions to the wrapped environments.

Args:
actions: Batched action to apply to the environment.

Raises:
ValueError: Invalid actions.

Returns:
Batch of observations, rewards, and done flags.


def to_image(self, filename='palette.png', band_width=1, length=60, max_width=0, vertical=True, alpha_channel=False):
    if (max_width < 1):
        pass
    else:
        band_width = int((max_width / len(self._colours)))
    image_width = (band_width * len(self._colours))
    if alpha_channel:
        my_image = Image.new('RGBA', (image_width, length))
    else:
        my_image = Image.new('RGB', (image_width, length))
    image_loaded = my_image.load()
    x = 0
    for my_colour in self._colours:
        for x1 in range(band_width):
            for y in range(length):
                image_loaded[(x, y)] = my_colour.rgb()
            x = (x + 1)
    if vertical:
        my_image = my_image.rotate(270)
    my_image.save(filename)

Creates an image from the palette.

Args:
filename(Optional[string]): filename of saved file. Defaults to
``palette.png`` in the current working directory.
band_width(optional[int]): how wide each colour band should be.
Defaults to 1 pixel.
length(Optional[int]): the length of the overall image in pixels.
This is the dimension orthogonal to ``band_width``. Defaults
to 60 pixels.
max_width(Optional[int]): if ``band_width`` is not set and this is,
this determines how wide the whole image should be.
vertical(Optional[bool]): if the image runs vertical (``True``,
default) or horizontal (``False``).
alpha_channel(Optional[bool]): if ``True``, the created image will
have an Alpha channel. Defaults to ``False``.


def export_panels(adapter, panels, versions=None, build='37'):

    if versions and (len(versions) != len(panels)):
        raise SyntaxError("If version specify for each panel")

    headers = []
    build_string = ("

    headers.append(build_string.format(build))
    header_string = ("
    contig_string = ("
    bed_string = ("{0}\t{1}\t{2}\t{3}\t{4}")

    panel_geneids = set()

    chromosomes_found = set()

    hgnc_geneobjs = []

    for i,panel_id in enumerate(panels):
        version = None
        if versions:
            version = versions[i]

        panel_obj = adapter.gene_panel(panel_id, version=version)
        if not panel_obj:
            LOG.warning("Panel {0} version {1} could not be found".format(panel_id, version))
            continue

        headers.append(header_string.format(
            panel_obj['panel_name'],
            panel_obj['version'],
            panel_obj['date'].date(),
            panel_obj['display_name'],
        ))

        for gene_obj in panel_obj['genes']:
            panel_geneids.add(gene_obj['hgnc_id'])

    gene_objs = adapter.hgncid_to_gene(build=build)

    for hgnc_id in panel_geneids:
        hgnc_geneobj = gene_objs.get(hgnc_id)
        if hgnc_geneobj is None:
            LOG.warn("missing HGNC gene: %s", hgnc_id)
            continue
        chrom = hgnc_geneobj['chromosome']
        start = hgnc_geneobj['start']
        chrom_int = CHROMOSOME_INTEGERS.get(chrom)
        if not chrom_int:
            LOG.warn("Chromosome %s out of scope", chrom)
            continue

        hgnc_geneobjs.append((chrom_int, start, hgnc_geneobj))
        chromosomes_found.add(chrom)

    hgnc_geneobjs.sort(key=lambda tup: (tup[0], tup[1]))

    for chrom in CHROMOSOMES:
        if chrom in chromosomes_found:
            headers.append(contig_string.format(chrom))

    headers.append("

    for header in headers:
        yield header

    for hgnc_gene in hgnc_geneobjs:
        gene_obj = hgnc_gene[-1]
        gene_line = bed_string.format(gene_obj['chromosome'], gene_obj['start'],
                                      gene_obj['end'], gene_obj['hgnc_id'],
                                      gene_obj['hgnc_symbol'])
        yield gene_line

Export all genes in gene panels

Exports the union of genes in one or several gene panels to a bed like format with coordinates.

Args:
adapter(scout.adapter.MongoAdapter)
panels(iterable(str)): Iterable with panel ids
bed(bool): If lines should be bed formated


def running(processid):
    try:
        os.kill(processid, 0)
    except OverflowError as exc:
        print('checking validity of pid ({p}) failed with: {e}'.format(p=processid, e=exc))
        sys.exit(1)
    except OSError:
        return False
    else:
        return True

Check the validity of a process ID.

Arguments:
processid (int): Process ID number.

Returns:
True if process ID is found otherwise False.


def make_repr(self, attrs):
    if (config.REPR_VERBOSITY in [MEDIUM, HIGH]):
        return self.__str__()
    elif (config.REPR_VERBOSITY is LOW):
        return '{}({})'.format(self.__class__.__name__, ', '.join((((attr + '=') + repr(getattr(self, attr))) for attr in attrs)))
    raise ValueError('Invalid value for `config.REPR_VERBOSITY`')

Construct a repr string.

If `config.REPR_VERBOSITY` is ``1`` or ``2``, this function calls the
object's __str__ method. Although this breaks the convention that __repr__
should return a string which can reconstruct the object, readable reprs are
invaluable since the Python interpreter calls `repr` to represent all
objects in the shell. Since PyPhi is often used in the interpreter we want
to have meaningful and useful representations.

Args:
self (obj): The object in question
attrs (Iterable[str]): Attributes to include in the repr

Returns:
str: the ``repr``esentation of the object


def override_default_args(**kwargs):

  override_default_kwargs = kwargs

  def custom_getter(getter, *args, **kwargs):

    updated_kwargs = override_default_kwargs.copy()
    updated_kwargs.update({kw: value for kw, value in six.iteritems(kwargs)
                           if value is not None})
    return getter(*args, **updated_kwargs)

  return custom_getter

Creates a custom getter that applies specified named arguments.

The returned custom getter treats the specified named arguments as revised
defaults, and does not override any non-`None` argument values supplied by
the original get_variable call (or by a nested scope's custom getter).

Args:
**kwargs: Overriding arguments for the custom getter to use in preference
the named arguments it's called with.

Returns:
Custom getter.


def render(raw_config, environment=None):

    t = Template(raw_config)
    buff = StringIO()
    if not environment:
        environment = {}
    try:
        substituted = t.substitute(environment)
    except KeyError as e:
        raise exceptions.MissingEnvironment(e.args[0])
    except ValueError:

        substituted = t.safe_substitute(environment)

    if not isinstance(substituted, str):
        substituted = substituted.decode('utf-8')

    buff.write(substituted)
    buff.seek(0)
    return buff.read()

Renders a config, using it as a template with the environment.

Args:
raw_config (str): the raw stacker configuration string.
environment (dict, optional): any environment values that should be
passed to the config

Returns:
str: the stacker configuration populated with any values passed from
the environment


def update_hparams_for_universal_transformer(hparams):
    hparams.daisy_chain_variables = False
    hparams.add_hparam('mix_with_transformer', None)
    hparams.add_hparam('num_mixedin_layers', 2)
    hparams.add_hparam('num_inrecurrence_layers', 1)
    hparams.add_hparam('recurrence_type', 'basic')
    hparams.add_hparam('num_rec_steps', hparams.num_hidden_layers)
    hparams.add_hparam('add_position_timing_signal', True)
    if hparams.add_position_timing_signal:
        hparams.pos = None
    hparams.add_hparam('position_start_index', None)
    hparams.add_hparam('add_step_timing_signal', True)
    hparams.add_hparam('step_timing_signal_type', 'learned')
    hparams.add_hparam('add_or_concat_timing_signal', 'add')
    hparams.add_hparam('add_sru', False)
    hparams.add_hparam('transformer_ffn_type', 'fc')
    hparams.add_hparam('transform_bias_init', (- 1.0))
    hparams.add_hparam('couple_carry_transform_gates', True)
    hparams.add_hparam('depth_embedding', True)
    hparams.add_hparam('dwa_elements', True)
    hparams.add_hparam('gate_ffn_layer', 'dense')
    hparams.add_hparam('lstm_forget_bias', 1.0)
    hparams.add_hparam('use_memory_as_final_state', False)
    hparams.add_hparam('add_ffn_unit_to_the_transition_function', False)
    hparams.add_hparam('act_type', 'basic')
    hparams.add_hparam('act_max_steps', (2 * hparams.num_hidden_layers))
    hparams.add_hparam('act_halting_bias_init', 1.0)
    hparams.add_hparam('act_epsilon', 0.01)
    hparams.add_hparam('act_loss_weight', 0.01)
    return hparams

Adds default hparams for all of the variants of the Universal Transformer.

Args:
hparams: default hparams (usually one of the standard hparams from
transformer model (like "transformer_base")

Returns:
hparams with default values for Universal Transformers hyper-parameters


def InitUser():
    result = AppUser.query((AppUser.user == users.get_current_user())).fetch()
    if result:
        app_user = result[0]
    else:
        app_user = AppUser(user=users.get_current_user(), email=users.get_current_user().email())
        app_user.put()
    return app_user

Initialize application user.

Retrieve existing user credentials from datastore or add new user.

Returns:
AppUser instance of the application user.


def _get_rand_attn_plan(from_seq_length, from_block_size, num_rand_blocks):
    plan_from_length = []
    plan_num_rand_blocks = []
    if 2 * num_rand_blocks + 5 < from_seq_length
        plan_from_length.append(int((2 * num_rand_blocks + 5) * from_block_size))
        plan_num_rand_blocks.append(num_rand_blocks)
        plan_from_length.append(from_seq_length)
        plan_num_rand_blocks.append(0)
    elif num_rand_blocks + 5 < from_seq_length
        plan_from_length.append(int((num_rand_blocks + 5) * from_block_size))
        plan_num_rand_blocks.append(num_rand_blocks
        plan_from_length.append(from_seq_length)
        plan_num_rand_blocks.append(num_rand_blocks - num_rand_blocks
    else:
        plan_from_length.append(from_seq_length)
        plan_num_rand_blocks.append(num_rand_blocks)
    return (plan_from_length, plan_num_rand_blocks)

Gives the plan of where to put random attention.

Args:
from_seq_length: int. length of from sequence.
from_block_size: int. size of block in from sequence.
num_rand_blocks: int. Number of random chunks per row.

Returns:
plan_from_length: ending location of from block plan_num_rand_blocks: number of random ending location for
each block


def json_set_description(recipe, variables):
    if 'script' in recipe:
        if 'description' in recipe['script']:
            try:
                recipe['script']['description'] = text_set_fields(recipe['script']['description'], variables)
            except KeyError:
                pass

Replaces all fields in description with values provided.

Checks if recipe['script']['description'] exist.  The replaces all %(???)s
variables
with values provided.  Note: %(???)s must match { "field":{ "name":"???" }}
in JOSN.

Args:
recipe: (dict) A dictionary representation of the JSON script.
variables: (dict) A lookup table of all values to be replaced, key is name
of field.

Returns:
Nothig. Description is modified in place.


def _resize_for_patching(self, image: np.array, target_resolution: tuple, resample, input_data_format: ChannelDimension) -> np.array:
    new_height, new_width = get_patch_output_size(image, target_resolution, input_data_format)
    resized_image = resize(image, (new_height, new_width), resample=resample, input_data_format=input_data_format)
    return resized_image

Resizes an image to a target resolution while maintaining aspect ratio.

Args:
image (np.array):
The input image.
target_resolution (tuple):
The target resolution (height, width) of the image.
resample (`PILImageResampling`):
Resampling filter to use if resizing the image.
input_data_format (`ChannelDimension` or `str`):
The channel dimension format of the input image.

Returns:
np.array: The resized and padded image.


def __init__(self, bits: List[int], order: int, initializer: tf.keras.initializers.Initializer=tf.keras.initializers.RandomUniform(), name: Union[None, str]=None):
    parity_layer = energy_utils.Parity(bits, order)
    self._num_terms = parity_layer.num_terms
    self._indices = parity_layer.indices
    pre_process = [energy_utils.SpinsFromBitstrings(), parity_layer]
    post_process = [energy_utils.VariableDot(initializer=initializer)]
    super().__init__(bits, pre_process + post_process, name)
    self._post_process = post_process

Initializes a KOBE.

Args:
bits: Each entry is an index on which the distribution is supported.
order: The order of the KOBE.
initializer: Specifies how to initialize the values of the parameters.
name: Optional name for the model.


def holiday_name(self, value=None):

        if value is not None:
            try:
                value = str(value)
            except ValueError:
                raise ValueError('value {} need to be of type str '
                                 'for field `holiday_name`'.format(value))
            if ',' in value:
                raise ValueError('value should not contain a comma '
                                 'for field `holiday_name`')

        self._holiday_name = value

Corresponds to IDD Field `holiday_name`

Args:
value (str): value for IDD Field `holiday_name`
if `value` is None it will not be checked against the
specification and is assumed to be a missing value

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


def _minimum_one_is_missing(self, **kwargs):
    rqset = self._meta_data['minimum_additional_parameters']
    if rqset:
        kwarg_set = set(iterkeys(kwargs))
        if kwarg_set.isdisjoint(rqset):
            args = sorted(rqset)
            error_message = ('This resource requires at least one of the mandatory additional parameters to be provided: %s' % ', '.join(args))
            raise MissingRequiredCreationParameter(error_message)

Helper function to do operation on sets

Verify if at least one of the elements
is present in **kwargs. If no items of rqset
are contained in **kwargs  the function
raises exception.

This check will only trigger if rqset is not empty.

Raises:
MissingRequiredCreationParameter


def _define_loop(graph, logdir, train_steps, eval_steps):
    loop = tools.Loop(logdir, graph.step, graph.should_log, graph.do_report, graph.force_reset)
    loop.add_phase('train', graph.done, graph.score, graph.summary, train_steps, report_every=train_steps, log_every=(train_steps
    loop.add_phase('eval', graph.done, graph.score, graph.summary, eval_steps, report_every=eval_steps, log_every=(eval_steps
    return loop

Create and configure a training loop with training and evaluation phases.

Args:
graph: Object providing graph elements via attributes.
logdir: Log directory for storing checkpoints and summaries.
train_steps: Number of training steps per epoch.
eval_steps: Number of evaluation steps per epoch.

Returns:
Loop object.


def set_x_grid_info(self, x_low, x_high, num_x, xscale, xval_name):

        self._set_grid_info('x', x_low, x_high, num_x, xscale, xval_name)
        return

Set the grid values for x.

Create information for the grid of x values.

Args:
num_x (int): Number of points on axis.
x_low/x_high (float): Lowest/highest value for the axis.
xscale (str): Scale of the axis. Choices are 'log' or 'lin'.
xval_name (str): Name representing the axis. See GenerateContainer documentation
for options for the name.


def get_word_index(path='reuters_word_index.json'):
    origin_folder = 'https:
    path = get_file(path, origin=origin_folder + 'reuters_word_index.json', file_hash='4d44cc38712099c9e383dc6e5f11a921')
    with open(path) as f:
        return json.load(f)

Retrieves a dict mapping words to their index in the Reuters dataset.

Actual word indices starts from 3, with 3 indices reserved for:
0 (padding), 1 (start), 2 (oov).

E.g. word index of 'the' is 1, but the in the actual training data, the
index of 'the' will be 1 + 3 = 4. Vice versa, to translate word indices in
training data back to words using this mapping, indices need to subtract 3.

Args:
path: where to cache the data (relative to `~/.keras/dataset`).

Returns:
The word index dictionary. Keys are word strings, values are their
index.


def _set_request_cache_if_django_cache_hit(key, django_cached_response):
    if django_cached_response.is_found:
        DEFAULT_REQUEST_CACHE.set(key, django_cached_response.value)

Sets the value in the request cache if the django cached response was a hit.

Args:
key (string)
django_cached_response (CachedResponse)


def GetConsoleAttr(encoding=None, reset=False):
    attr = ConsoleAttr._CONSOLE_ATTR_STATE
    if not reset:
        if not attr:
            reset = True
        elif encoding and encoding != attr.GetEncoding():
            reset = True
    if reset:
        attr = ConsoleAttr(encoding=encoding)
        ConsoleAttr._CONSOLE_ATTR_STATE = attr
    return attr

Gets the console attribute state.

If this is the first call or reset is True or encoding is not None and does
not match the current encoding or out is not None and does not match the
current out then the state is (re)initialized. Otherwise the current state
is returned.

This call associates the out file stream with the console. All console related
output should go to the same stream.

Args:
encoding: Encoding override.
ascii -- ASCII. This is the default.
utf8 -- UTF-8 unicode.
win -- Windows code page 437.
reset: Force re-initialization if True.

Returns:
The global ConsoleAttr state object.


class Sliceable:

    def __init__(self, array):
        self.array = array

    def __getitem__(self, indices):

        return self.array[indices]

    @classmethod
    def cast(cls, x, dtype):

        return x.astype(dtype)

    @classmethod
    def convert_to_numpy(cls, x):

        return x

    @classmethod
    def convert_to_tf_dataset_compatible(cls, x):

        return x

    @classmethod
    def convert_to_jax_compatible(cls, x):

        return x

    @classmethod
    def convert_to_torch_compatible(cls, x):

        return x

`Sliceable` wrapping a tensor.

A `Sliceable` implements the subscript operator to slice or index against
the first dimension of the array. It also has conversion methods for each
one of the backends.

Args:
array: the native array or tensor to wrap.

Attributes:
shape: the shape of the full dense native array.


def destroy_cloudwatch_event(app='', env='dev', region=''):

    session = boto3.Session(profile_name=env, region_name=region)
    cloudwatch_client = session.client('events')

    event_rules = get_cloudwatch_event_rule(app_name=app, account=env, region=region)

    for rule in event_rules:
        cloudwatch_client.remove_targets(Rule=rule, Ids=[app])

    return True

Destroy Cloudwatch event subscription.

Args:
app (str): Spinnaker Application name.
env (str): Deployment environment.
region (str): AWS region.
Returns:
bool: True upon successful completion.


def print_tensors_in_checkpoint_file(file_name, tensor_name, all_tensors, all_tensor_names=False, count_exclude_pattern=''):
    try:
        reader = py_checkpoint_reader.NewCheckpointReader(file_name)
        if all_tensors or all_tensor_names:
            var_to_shape_map = reader.get_variable_to_shape_map()
            var_to_dtype_map = reader.get_variable_to_dtype_map()
            for key, value in sorted(var_to_shape_map.items()):
                print('tensor: %s (%s) %s' % (key, var_to_dtype_map[key].name, value))
                if all_tensors:
                    try:
                        print(reader.get_tensor(key))
                    except errors_impl.InternalError:
                        print('<not convertible to a numpy dtype>')
        elif not tensor_name:
            print(reader.debug_string().decode('utf-8', errors='ignore'))
        else:
            if not reader.has_tensor(tensor_name):
                print('Tensor %s not found in checkpoint' % tensor_name)
                return
            var_to_shape_map = reader.get_variable_to_shape_map()
            var_to_dtype_map = reader.get_variable_to_dtype_map()
            print('tensor: %s (%s) %s' % (tensor_name, var_to_dtype_map[tensor_name].name, var_to_shape_map[tensor_name]))
            print(reader.get_tensor(tensor_name))
        print('
    except Exception as e:
        print(str(e))
        if 'corrupted compressed block contents' in str(e):
            print("It's likely that your checkpoint file has been compressed with SNAPPY.")
        if 'Data loss' in str(e) and any((e in file_name for e in ['.index', '.meta', '.data'])):
            proposed_file = '.'.join(file_name.split('.')[0:-1])
            v2_file_error_template = "\nIt's likely that this is a V2 checkpoint and you need to provide the filename\n*prefix*.  Try removing the '.' and extension.  Try:\ninspect checkpoint --file_name = {}"
            print(v2_file_error_template.format(proposed_file))

Prints tensors in a checkpoint file.

If no `tensor_name` is provided, prints the tensor names and shapes
in the checkpoint file.

If `tensor_name` is provided, prints the content of the tensor.

Args:
file_name: Name of the checkpoint file.
tensor_name: Name of the tensor in the checkpoint file to print.
all_tensors: Boolean indicating whether to print all tensors.
all_tensor_names: Boolean indicating whether to print all tensor names.
count_exclude_pattern: Regex string, pattern to exclude tensors from count.


def create_combination(list_of_sentences):

  num_sentences = len(list_of_sentences) - 1
  combinations = []
  for i, _ in enumerate(list_of_sentences):
    if i == num_sentences:
      break
    num_pairs = num_sentences - i
    populated = num_pairs * [list_of_sentences[i]]
    zipped = list(zip(populated, list_of_sentences[i + 1:]))
    combinations += zipped
  return combinations

Generates all possible pair combinations for the input list of sentences.

For example:

input = ["paraphrase1", "paraphrase2", "paraphrase3"]

output = [("paraphrase1", "paraphrase2"),
("paraphrase1", "paraphrase3"),
("paraphrase2", "paraphrase3")]

Args:
list_of_sentences: the list of input sentences.
Returns:
the list of all possible sentence pairs.


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

Resets estimated time for the object to 0 seconds.

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

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


def get_key_by_job_id(cls, mapreduce_id):

    return db.Key.from_path(cls.kind(), "%s:%s" % (mapreduce_id, cls._KEY_NAME))

Retrieves the Key for a mapreduce ID.

Args:
mapreduce_id: The job to fetch.

Returns:
Datastore Key for the command for the given job ID.


def get_attribute_from_config(config, section, attribute):

    section = config.get(section)
    if section:
        option = section.get(attribute)
        if option:
            return option
    raise ConfigurationError("Config file badly formed!\n"
                             "Failed to get attribute '{}' from section '{}'!"
                             .format(attribute, section))

Try to parse an attribute of the config file.

Args:
config (defaultdict): A defaultdict.
section (str): The section of the config file to get information from.
attribute (str): The attribute of the section to fetch.
Returns:
str: The string corresponding to the section and attribute.
Raises:
ConfigurationError


def hertz_to_mel(freq: Union[float, np.ndarray], mel_scale: str='htk') -> Union[float, np.ndarray]:
    if mel_scale not in ['slaney', 'htk', 'kaldi']:
        raise ValueError('mel_scale should be one of "htk", "slaney" or "kaldi".')
    if mel_scale == 'htk':
        return 2595.0 * np.log10(1.0 + freq / 700.0)
    elif mel_scale == 'kaldi':
        return 1127.0 * np.log(1.0 + freq / 700.0)
    min_log_hertz = 1000.0
    min_log_mel = 15.0
    logstep = 27.0 / np.log(6.4)
    mels = 3.0 * freq / 200.0
    if isinstance(freq, np.ndarray):
        log_region = freq >= min_log_hertz
        mels[log_region] = min_log_mel + np.log(freq[log_region] / min_log_hertz) * logstep
    elif freq >= min_log_hertz:
        mels = min_log_mel + np.log(freq / min_log_hertz) * logstep
    return mels

Convert frequency from hertz to mels.

Args:
freq (`float` or `np.ndarray`):
The frequency, or multiple frequencies, in hertz (Hz).
mel_scale (`str`, *optional*, defaults to `"htk"`):
The mel frequency scale to use, `"htk"`, `"kaldi"` or `"slaney"`.

Returns:
`float` or `np.ndarray`: The frequencies on the mel scale.


def union(df, other, index=False, keep='first'):
    validate_set_ops(df, other)
    stacked = df.append(other)
    if index:
        stacked_reset_indexes = stacked.reset_index()
        index_cols = [col for col in stacked_reset_indexes.columns if (col not in df.columns)]
        index_name = df.index.names
        return_df = stacked_reset_indexes.drop_duplicates(keep=keep).set_index(index_cols)
        return_df.index.names = index_name
        return return_df
    else:
        return stacked.drop_duplicates(keep=keep)

Returns rows that appear in either DataFrame.

Args:
df (pandas.DataFrame): data passed in through the pipe.
other (pandas.DataFrame): other DataFrame to use for set operation with
the first.

Kwargs:
index (bool): Boolean indicating whether to consider the pandas index
as part of the set operation (default `False`).
keep (str): Indicates which duplicate should be kept. Options are `'first'`
and `'last'`.


def calculate_query_times(**kwargs):
    return {'total_time_avg': round(numpy.mean(kwargs['total_times']), 1), 'total_time_min': round(numpy.min(kwargs['total_times']), 1), 'total_time_max': round(numpy.max(kwargs['total_times']), 1), 'total_time_85': round(numpy.percentile(kwargs['total_times'], 85), 1), 'execution_time_avg': round(numpy.mean(kwargs['execution_times']), 1), 'execution_time_min': round(numpy.min(kwargs['execution_times']), 1), 'execution_time_max': round(numpy.max(kwargs['execution_times']), 1), 'execution_time_85': round(numpy.percentile(kwargs['execution_times'], 85), 1), 'execution_time_25': round(numpy.percentile(kwargs['execution_times'], 25), 1), 'execution_time_std': round(numpy.std(kwargs['execution_times']), 1), 'connect_time_avg': round(numpy.mean(kwargs['connect_times']), 1), 'connect_time_min': round(numpy.min(kwargs['connect_times']), 1), 'connect_time_max': round(numpy.max(kwargs['connect_times']), 1), 'connect_time_85': round(numpy.percentile(kwargs['connect_times'], 85), 1), 'results_iter_time_avg': round(numpy.mean(kwargs['results_iter_times']), 1), 'results_iter_time_min': round(numpy.min(kwargs['results_iter_times']), 1), 'results_iter_time_max': round(numpy.max(kwargs['results_iter_times']), 1), 'results_iter_time_85': round(numpy.percentile(kwargs['results_iter_times'], 85), 1)}

Calculates aggregate query times from all iteration times

Kwargs:
total_times(list): List of total time calculations
execution_times(list): List of execution_time calculations
results_iter_times(list): List of results_iter_time calculations
connect_times(list): List of connect_time calculations

Returns:
query_execution(dict): Query times
False(bool): The query failed. Exception should be logged.


def send_batches(self, batch_list):

        if isinstance(batch_list, BaseMessage):
            batch_list = batch_list.SerializeToString()

        return self._post('/batches', batch_list)

Sends a list of batches to the validator.

Args:
batch_list (:obj:`BatchList`): the list of batches

Returns:
dict: the json result data, as a dict


def _get_user_id(arguments_dict):

    if 'user_id' not in arguments_dict:
        raise TypeError('Each invocation of a UserTaskMixin subclass must include the user_id')
    user_id = arguments_dict['user_id']
    try:
        get_user_model().objects.get(pk=user_id)
    except (ValueError, get_user_model().DoesNotExist):
        raise TypeError('Invalid user_id: {}'.format(user_id))
    return user_id

Get and validate the `user_id` argument to a task derived from `UserTaskMixin`.

Arguments:
arguments_dict (dict): The parsed positional and keyword arguments to the task

Returns
-------
int: The primary key of a user record (may not be an int if using a custom user model)


def import_certificate(self, certificate_data, bay_number=None):
    uri = '{}/https/certificaterequest'.format(self.data['uri'])
    if bay_number:
        uri += ('?bayNumber=%d' % bay_number)
    headers = {'Content-Type': 'application/json'}
    return self._helper.do_put(uri, certificate_data, (- 1), headers)

Imports a signed server certificate into the enclosure.

Args:
certificate_data: Dictionary with Signed certificate and type.
bay_number: OA to which the signed certificate will be imported.

Returns:
Enclosure.


def add_handler(self, handler):
    handler['logger'] = self._get_logger(handler)
    handler['reads'] = 0
    handler['data_read'] = 0
    self.capture_handlers.append(handler)

Add an additional handler

Args:
handler:
A dictionary of handler configuration for the handler
that should be added. See :func:`__init__` for details
on valid parameters.


def picture_view(request, user_id, year=None):

    try:
        user = User.objects.get(id=user_id)
    except User.DoesNotExist:
        raise Http404
    default_image_path = os.path.join(settings.PROJECT_ROOT, "static/img/default_profile_pic.png")

    if user is None:
        raise Http404
    else:
        if year is None:
            preferred = user.preferred_photo

            if preferred is None:
                data = user.default_photo
                if data is None:
                    image_buffer = io.open(default_image_path, mode="rb")
                else:
                    image_buffer = io.BytesIO(data)

            else:
                data = preferred.binary
                if data:
                    image_buffer = io.BytesIO(data)
                else:
                    image_buffer = io.open(default_image_path, mode="rb")
        else:
            grade_number = Grade.number_from_name(year)
            if user.photos.filter(grade_number=grade_number).exists():
                data = user.photos.filter(grade_number=grade_number).first().binary
            else:
                data = None
            if data:
                image_buffer = io.BytesIO(data)
            else:
                image_buffer = io.open(default_image_path, mode="rb")

        response = HttpResponse(content_type="image/jpeg")
        response["Content-Disposition"] = "filename={}_{}.jpg".format(user_id, year or preferred)
        try:
            img = image_buffer.read()
        except UnicodeDecodeError:
            img = io.open(default_image_path, mode="rb").read()

        image_buffer.close()
        response.write(img)

        return response

Displays a view of a user's picture.

Args:
user_id
The ID of the user whose picture is being fetched.
year
The user's picture from this year is fetched. If not
specified, use the preferred picture.


def listFormats(self, vendorSpecific=None):

        response = self.listFormatsResponse(vendorSpecific)
        return self._read_dataone_type_response(response, 'ObjectFormatList')

See Also: listFormatsResponse()

Args:
vendorSpecific:

Returns:


def is_supported(cls, desc):
    for l in cls:
        if l.matches(desc):
            return True
    return False

Determines if the given label descriptor is supported.

Args:
desc (:class:`endpoints_management.gen.servicemanagement_v1_messages.LabelDescriptor`):
the label descriptor to test

Return:
`True` if desc is supported, otherwise `False`


def _get_longest_diag_index(input_matrix):
    diags = AssistedCandidateGeneratorDifferentTokenizers._get_longest_diag_dict(input_matrix, input_matrix.nonzero())
    diags_values = list(diags.values())
    diags_keys = list(diags.keys())
    best_diag = np.argmax(diags_values)
    diag_start_index = diags_keys[best_diag]
    diag_start_length = diags_values[best_diag]
    return (diag_start_index, diag_start_length)

Returns the start index and length of the longest diagonal in the given input.
Args:
input_matrix (numpy.ndarray): The input matrix.
Returns:
tuple: A tuple containing the start index and length of the longest diagonal.


def is_attribute_multivalued(self, attribute):
    rule_set = self._attribute_rule_sets.get(attribute)
    return rule_set.multiple_instances_permitted

Check if the attribute is allowed to have multiple instances.

Args:
attribute (string): The name of the attribute
(e.g., 'State'). Required.


def load_configuration(config_file, config_dir, service_file):
    config_files = [config_file]
    config = configparser.ConfigParser()
    config.read_dict(DEFAULT_OPTIONS)
    if (not os.path.isfile(config_file)):
        raise ValueError("{f} configuration file either isn't readable or doesn't exist".format(f=config_file))
    if (service_file is not None):
        if (not os.path.isfile(service_file)):
            raise ValueError("{f} configuration file for a service check doesn't exist".format(f=service_file))
        else:
            config_files.append(service_file)
    elif (config_dir is not None):
        if (not os.path.isdir(config_dir)):
            raise ValueError("{d} directory with configuration files for service checks doesn't exist".format(d=config_dir))
        else:
            config_files.extend(glob.glob(os.path.join(config_dir, '*.conf')))
    try:
        config.read(config_files)
    except configparser.Error as exc:
        raise ValueError(exc)
    configuration_check(config)
    bird_configuration = build_bird_configuration(config)
    create_bird_config_files(bird_configuration)
    return (config, bird_configuration)

Build configuration objects.

If all sanity checks against daemon and service check settings are passed
then it builds a ConfigParser object which holds all our configuration
and a dictionary data structure which holds Bird configuration per IP
protocol version.

Arguments:
config_file (str): The file name which holds daemon settings
config_dir (str): The directory name which has configuration files
for each service check
service_file (str): A file which contains configuration for a single
service check

Returns:
A tuple with 1st element a ConfigParser object and 2nd element
a dictionary.
Raises:
ValueError if a sanity check fails.


def select_by_key(self, key):

        self._selected_key = None
        self._selected_item = None
        for item in self.children.values():
            item.attributes['selected'] = False

        if key in self.children:
            self.children[key].attributes['selected'] = True
            self._selected_key = key
            self._selected_item = self.children[key]

Selects an item by its key.

Args:
key (str): The unique string identifier of the item that have to be selected.


def _process_update(self, item, feed_item):
    pass

Handles updates to the creative asset object.

Since creative assets are read only in DCM, there is nothing to do here,
this method is mandatory as it is invoked by the BaseDAO class.

Args:
item: The creative asset DCM object being updated.
feed_item: The feed item representing the creative asset from the
Bulkdozer feed.


def roots_in_unit_interval(coeffs):
    all_roots = polynomial.polyroots(coeffs)
    all_roots = all_roots[((_UNIT_INTERVAL_WIGGLE_START < all_roots.real) & (all_roots.real < _UNIT_INTERVAL_WIGGLE_END))]
    real_inds = (np.abs(all_roots.imag) < _IMAGINARY_WIGGLE)
    return all_roots[real_inds].real

r"""Compute roots of a polynomial in the unit interval.

Args:
coeffs (numpy.ndarray): A 1D array (size ``d + 1``) of coefficients in
monomial / power basis.

Returns:
numpy.ndarray: ``N``-array of real values in :math:`\left[0, 1\right]`.


def list_resource_groups(access_token, subscription_id):
    endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/resourceGroups/', '?api-version=', RESOURCE_API])
    return do_get(endpoint, access_token)

List the resource groups in a subscription.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.

Returns:
HTTP response.


def find_gaps(self, index=False):
    return self.__find_incongruities(op=operator.lt, index=index)

Finds gaps in a striplog.

Args:
index (bool): If True, returns indices of intervals with
gaps after them.

Returns:
Striplog: A striplog of all the gaps. A sort of anti-striplog.


def inspect_plugin(self, name):

        url = self._url('/plugins/{0}/json', name)
        return self._result(self._get(url), True)

Retrieve plugin metadata.

Args:
name (string): The name of the plugin. The ``:latest`` tag is
optional, and is the default if omitted.

Returns:
A dict containing plugin info


def show(config, section, opt):
    if (section not in config.keys()):
        raise ConfigError("section '{}' doesn't exist".format(section))
    if (opt not in config[section].keys()):
        raise ConfigError("option '{}.{}' doesn't exist".format(section, opt))
    logger.info(config[section][opt])

Prints option value from the config.

Args:
config (configobj.ConfigObj): config to work on.
section (str): section name.
opt (str): option name.


def _run(self, cmd):
    if isinstance(cmd, six.string_types):
        cmd = salt.utils.args.shlex_split(cmd)
    try:
        log.debug(cmd)
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        return p.communicate()
    except (OSError, IOError) as exc:
        log.debug('Command Failed: %s', ' '.join(cmd))
        log.debug('Error: %s', exc)
        raise CommandExecutionError(exc)

Internal function for running commands. Used by the uninstall function.

Args:
cmd (str, list): The command to run

Returns:
str: The stdout of the command


def parse_query_param(url, param):
    try:
        return parse.parse_qs(parse.urlparse(url).query)[param][0]
    except:
        return None

Parses the query string of a URL and returns the value of a parameter.

Args:
url: A URL.
param: A string representing the name of the parameter.

Returns:
The value of the parameter.


def get_lang_tags(index_page):
    dom = dhtmlparser.parseString(index_page)
    lang_tags = [get_html_lang_tags(dom), get_dc_lang_tags(dom), [detect_language(dom)], get_html_tag_lang_params(dom)]
    return list(sorted(set((SourceString(normalize(lang), source=lang.source) for lang in sum(lang_tags, [])))))

Collect informations about language of the page from HTML and Dublin core
tags and langdetect guesses.

Args:
index_page (str): HTML content of the page you wish to analyze.

Returns:
list: List of :class:`.SourceString` objects.


def resolve(self, name, version, max_id):
    if (not isinstance(name, six.text_type)):
        raise TypeError(('Name must be a Unicode sequence: %r' % name))
    if (not isinstance(version, int)):
        raise TypeError(('Version must be an int: %r' % version))
    if (version <= 0):
        raise ValueError(('Version must be positive: %s' % version))
    if ((max_id is not None) and (max_id < 0)):
        raise ValueError(('Max ID must be zero or positive: %s' % max_id))
    versions = self.__tables.get(name)
    if (versions is None):
        if (max_id is None):
            raise CannotSubstituteTable(('Found no table for %s, but no max_id' % name))
        return placeholder_symbol_table(name, version, max_id)
    table = versions.get(version)
    if (table is None):
        keys = list(versions)
        keys.sort()
        table = versions[keys[(- 1)]]
    if ((table.version == version) and ((max_id is None) or (table.max_id == max_id))):
        return table
    if (max_id is None):
        raise CannotSubstituteTable(('Found match for %s, but not version %d, and no max_id' % (name, version)))
    return substitute_symbol_table(table, version, max_id)

Resolves the table for a given name and version.

Args:
name (unicode): The name of the table to resolve.
version (int): The version of the table to resolve.
max_id (Optional[int]): The maximum ID of the table requested.
May be ``None`` in which case an exact match on ``name`` and ``version``
is required.

Returns:
SymbolTable: The *closest* matching symbol table.  This is either an exact match,
a placeholder, or a derived substitute depending on what tables are registered.


def set_rgb_dim_level_with_time(
        self,
        channelIndex: int,
        rgb: RGBColorState,
        dimLevel: float,
        onTime: float,
        rampTime: float,
    ):

        data = {
            "channelIndex": channelIndex,
            "deviceId": self.id,
            "simpleRGBColorState": rgb,
            "dimLevel": dimLevel,
            "onTime": onTime,
            "rampTime": rampTime,
        }
        return self._restCall(
            "device/control/setSimpleRGBColorDimLevelWithTime", body=json.dumps(data)
        )

sets the color and dimlevel of the lamp

Args:
channelIndex(int): the channelIndex of the lamp. Use self.topLightChannelIndex or self.bottomLightChannelIndex
rgb(RGBColorState): the color of the lamp
dimLevel(float): the dimLevel of the lamp. 0.0 = off, 1.0 = MAX
onTime(float):
rampTime(float):
Returns:
the result of the _restCall


def incoming(self, messages):

        if self._observers:
            campfire = self._room.get_campfire()
            for message in messages:
                for observer in self._observers:
                    observer(Message(campfire, message))

Called when incoming messages arrive.

Args:
messages (tuple): Messages (each message is a dict)


def _separable_conv_block(ip, filters, kernel_size=(3, 3), strides=(1, 1), block_id=None):
    channel_dim = 1 if backend.image_data_format() == 'channels_first' else -1
    with backend.name_scope(f'separable_conv_block_{block_id}'):
        x = layers.Activation('relu')(ip)
        if strides == (2, 2):
            x = layers.ZeroPadding2D(padding=imagenet_utils.correct_pad(x, kernel_size), name=f'separable_conv_1_pad_{block_id}')(x)
            conv_pad = 'valid'
        else:
            conv_pad = 'same'
        x = layers.SeparableConv2D(filters, kernel_size, strides=strides, name=f'separable_conv_1_{block_id}', padding=conv_pad, use_bias=False)(x)
        x = layers.BatchNormalization(axis=channel_dim, momentum=0.9997, epsilon=0.001, name=f'separable_conv_1_bn_{block_id}')(x)
        x = layers.Activation('relu')(x)
        x = layers.SeparableConv2D(filters, kernel_size, name=f'separable_conv_2_{block_id}', padding='same', use_bias=False)(x)
        x = layers.BatchNormalization(axis=channel_dim, momentum=0.9997, epsilon=0.001, name=f'separable_conv_2_bn_{block_id}')(x)
    return x

Adds 2 blocks of [relu-separable conv-batchnorm].

Args:
ip: Input tensor
filters: Number of output filters per layer
kernel_size: Kernel size of separable convolutions
strides: Strided convolution for downsampling
block_id: String block_id

Returns:
A Keras tensor


def _transform_binary_composition_to_expression(expression, node, context):

    if expression.operator not in constants.SUPPORTED_OPERATORS:
        raise NotImplementedError(
            u'Filter operation "{}" is not supported by the SQL backend.'.format(
                expression.operator))
    sql_operator = constants.SUPPORTED_OPERATORS[expression.operator]
    left = _expression_to_sql(expression.left, node, context)
    right = _expression_to_sql(expression.right, node, context)
    if sql_operator.cardinality == constants.CARDINALITY_UNARY:
        left, right = _get_column_and_bindparam(left, right, sql_operator)
        clause = getattr(left, sql_operator.name)(right)
        return clause
    elif sql_operator.cardinality == constants.CARDINALITY_BINARY:
        clause = getattr(sql_expressions, sql_operator.name)(left, right)
        return clause
    elif sql_operator.cardinality == constants.CARDINALITY_LIST_VALUED:
        left, right = _get_column_and_bindparam(left, right, sql_operator)

        right.expanding = True
        clause = getattr(left, sql_operator.name)(right)
        return clause
    raise AssertionError(u'Unreachable, operator cardinality {} for compiler expression {} is '
                         u'unknown'.format(sql_operator.cardinality, expression))

Transform a BinaryComposition compiler expression into a SQLAlchemy expression.

Recursively calls _expression_to_sql to convert its left and right sub-expressions.

Args:
expression: expression, BinaryComposition compiler expression.
node: SqlNode, the SqlNode the expression applies to.
context: CompilationContext, global compilation state and metadata.

Returns:
Expression, SQLAlchemy expression.


def cast(self, value):

        if self.type is None:
            return value

        if self.type in (str, int, float):
            try:
                return self.type(value)
            except Exception as e:
                raise errors.BisonError(
                    'Failed to cast {} to {}'.format(value, self.type)
                ) from e

        elif self.type == bool:
            return value.lower() == 'true'

        else:
            raise errors.BisonError('Unsupported type for casting: {}'.format(self.type))

Cast a value to the type required by the option, if one is set.

This is used to cast the string values gathered from environment
variable into their required type.

Args:
value: The value to cast.

Returns:
The value casted to the expected type for the option.


def _convert_appengine_app_assertion_credentials(credentials):

    return google.auth.app_engine.Credentials(
        scopes=_helpers.string_to_scopes(credentials.scope),
        service_account_id=credentials.service_account_id)

Converts to :class:`google.auth.app_engine.Credentials`.

Args:
credentials (oauth2client.contrib.app_engine.AppAssertionCredentials):
The credentials to convert.

Returns:
google.oauth2.service_account.Credentials: The converted credentials.


def exp(array, ty):

    weld_obj = WeldObject(encoder_, decoder_)

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

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

Computes the per-element exponenet of the passed-in array.

Args:
array (WeldObject / Numpy.ndarray): Input array
ty (WeldType): Type of each element in the input array

Returns:
A WeldObject representing this computation


def update_config(self, new_config):
    self._assert_not_running()
    self._ad.log.info('[LogcatService] Changing config from %s to %s', self._config, new_config)
    self._config = new_config

Updates the configuration for the service.

The service needs to be stopped before updating, and explicitly started
after the update.

This will reset the service. Previous output files may be orphaned if
output path is changed.

Args:
new_config: Config, the new config to use.


def get_language(self, text):

        files = {'text': text}
        res, status_code = self.post(self.language_service, files=files)

        if status_code != 200:
            logger.debug('Language recognition failed.')

        return self.decode(res), status_code

Recognise the language of the text in input

Args:
id (str): The text whose the language needs to be recognised

Returns:
dict, int: A dict containing the recognised language and the
confidence score.


def get_arrhenius_plot(temps, diffusivities, diffusivity_errors=None, **kwargs):
    (Ea, c, _) = fit_arrhenius(temps, diffusivities)
    from pymatgen.util.plotting import pretty_plot
    plt = pretty_plot(12, 8)
    arr = (c * np.exp(((- Ea) / ((const.k / const.e) * np.array(temps)))))
    t_1 = (1000 / np.array(temps))
    plt.plot(t_1, diffusivities, 'ko', t_1, arr, 'k--', markersize=10, **kwargs)
    if (diffusivity_errors is not None):
        n = len(diffusivity_errors)
        plt.errorbar(t_1[0:n], diffusivities[0:n], yerr=diffusivity_errors, fmt='ko', ecolor='k', capthick=2, linewidth=2)
    ax = plt.axes()
    ax.set_yscale('log')
    plt.text(0.6, 0.85, 'E$_a$ = {:.0f} meV'.format((Ea * 1000)), fontsize=30, transform=plt.axes().transAxes)
    plt.ylabel('D (cm$^2$/s)')
    plt.xlabel('1000/T (K$^{-1}$)')
    plt.tight_layout()
    return plt

Returns an Arrhenius plot.

Args:
temps ([float]): A sequence of temperatures.
diffusivities ([float]): A sequence of diffusivities (e.g.,
from DiffusionAnalyzer.diffusivity).
diffusivity_errors ([float]): A sequence of errors for the
diffusivities. If None, no error bar is plotted.
\\*\\*kwargs:
Any keyword args supported by matplotlib.pyplot.plot.

Returns:
A matplotlib.pyplot object. Do plt.show() to show the plot.