def restore(cls, metadata_checkpoint_dir, search_alg=None, scheduler=None, trial_executor=None):
    newest_ckpt_path = _find_newest_ckpt(metadata_checkpoint_dir)
    with open(newest_ckpt_path, 'r') as f:
        runner_state = json.load(f, cls=_TuneFunctionDecoder)
    logger.warning(''.join(['Attempting to resume experiment from {}. '.format(metadata_checkpoint_dir), 'This feature is experimental, and may not work with all search algorithms. ', 'This will ignore any new changes to the specification.']))
    from ray.tune.suggest import BasicVariantGenerator
    runner = TrialRunner((search_alg or BasicVariantGenerator()), scheduler=scheduler, trial_executor=trial_executor)
    runner.__setstate__(runner_state['runner_data'])
    trials = []
    for trial_cp in runner_state['checkpoints']:
        new_trial = Trial(trial_cp['trainable_name'])
        new_trial.__setstate__(trial_cp)
        trials += [new_trial]
    for trial in sorted(trials, key=(lambda t: t.last_update_time), reverse=True):
        runner.add_trial(trial)
    return runner

Restores all checkpointed trials from previous run.

Requires user to manually re-register their objects. Also stops
all ongoing trials.

Args:
metadata_checkpoint_dir (str): Path to metadata checkpoints.
search_alg (SearchAlgorithm): Search Algorithm. Defaults to
BasicVariantGenerator.
scheduler (TrialScheduler): Scheduler for executing
the experiment.
trial_executor (TrialExecutor): Manage the execution of trials.

Returns:
runner (TrialRunner): A TrialRunner to resume experiments from.


def to_text_diagram_drawer(self, *, use_unicode_characters: bool=True, qubit_namer: Optional[Callable[([ops.Qid], str)]]=None, transpose: bool=False, precision: Optional[int]=3, qubit_order: ops.QubitOrderOrList=ops.QubitOrder.DEFAULT, get_circuit_diagram_info: Optional[Callable[([ops.Operation, protocols.CircuitDiagramInfoArgs], protocols.CircuitDiagramInfo)]]=None) -> TextDiagramDrawer:
    qubits = ops.QubitOrder.as_qubit_order(qubit_order).order_for(self.all_qubits())
    qubit_map = {qubits[i]: i for i in range(len(qubits))}
    if (qubit_namer is None):
        qubit_namer = (lambda q: (str(q) + ('' if transpose else ': ')))
    diagram = TextDiagramDrawer()
    for (q, i) in qubit_map.items():
        diagram.write(0, i, qubit_namer(q))
    moment_groups = []
    for moment in self._moments:
        _draw_moment_in_diagram(moment, use_unicode_characters, qubit_map, diagram, precision, moment_groups, get_circuit_diagram_info)
    w = diagram.width()
    for i in qubit_map.values():
        diagram.horizontal_line(i, 0, w)
    if moment_groups:
        _draw_moment_groups_in_diagram(moment_groups, use_unicode_characters, diagram)
    if transpose:
        diagram = diagram.transpose()
    return diagram

Returns a TextDiagramDrawer with the circuit drawn into it.

Args:
use_unicode_characters: Determines if unicode characters are
allowed (as opposed to ascii-only diagrams).
qubit_namer: Names qubits in diagram. Defaults to str.
transpose: Arranges qubit wires vertically instead of horizontally.
precision: Number of digits to use when representing numbers.
qubit_order: Determines how qubits are ordered in the diagram.
get_circuit_diagram_info: Gets circuit diagram info. Defaults to
protocol with fallback.

Returns:
The TextDiagramDrawer instance.


def sg_max(tensor, opt):
    r
    return tf.reduce_max(tensor, axis=opt.axis, keep_dims=opt.keep_dims, name=opt.name)

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

See `tf.reduce_max()` in tensorflow.

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

Returns:
A `Tensor`.


def get_resource_group(access_token, subscription_id, rgname):

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

Get details about the named resource group.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.
rgname (str): Azure resource group name.

Returns:
HTTP response. JSON body.


def _CheckGitkitError(self, raw_response):

    try:
      response = simplejson.loads(raw_response)
      if 'error' not in response:
        return response
      else:
        error = response['error']
        if 'code' in error:
          code = error['code']
          if str(code).startswith('4'):
            raise errors.GitkitClientError(error['message'])
          else:
            raise errors.GitkitServerError(error['message'])
    except simplejson.JSONDecodeError:
      pass
    raise errors.GitkitServerError('null error code from Gitkit server')

Raises error if API invocation failed.

Args:
raw_response: string, the http response.

Raises:
GitkitClientError: if the error code is 4xx.
GitkitServerError: if the response if malformed.

Returns:
Successful response as dict.


def make_string_field_value(cls, field):

        if field.regex is not None:
            raise NotImplementedError

        string_range = cls.get_range(field)

        return cls.get_random_string(string_range)

String Field has three constraints (apart from anything
in the super class)

Args:
field (StringField): actual string field object from a
model declaration

Returns:
random string value


def _CreateLineString(self, parent, coordinate_list):

    if not coordinate_list:
      return None
    linestring = ET.SubElement(parent, 'LineString')
    tessellate = ET.SubElement(linestring, 'tessellate')
    tessellate.text = '1'
    if len(coordinate_list[0]) == 3:
      altitude_mode = ET.SubElement(linestring, 'altitudeMode')
      altitude_mode.text = 'absolute'
    coordinates = ET.SubElement(linestring, 'coordinates')
    if len(coordinate_list[0]) == 3:
      coordinate_str_list = ['%f,%f,%f' % t for t in coordinate_list]
    else:
      coordinate_str_list = ['%f,%f' % t for t in coordinate_list]
    coordinates.text = ' '.join(coordinate_str_list)
    return linestring

Create a KML LineString element.

The points of the string are given in coordinate_list. Every element of
coordinate_list should be one of a tuple (longitude, latitude) or a tuple
(longitude, latitude, altitude).

Args:
parent: The parent ElementTree.Element instance.
coordinate_list: The list of coordinates.

Returns:
The LineString ElementTree.Element instance or None if coordinate_list is
empty.


def move(self, x, y):
    self._cursor = self._normalizePoint(x, y)

Move the virtual cursor.

Args:
x (int): x-coordinate to place the cursor.
y (int): y-coordinate to place the cursor.

.. seealso:: :any:`get_cursor`, :any:`print_str`, :any:`write`


def get(self, config_id):

        return self.prepare_model(self.client.api.inspect_config(config_id))

Get a config.

Args:
config_id (str): Config ID.

Returns:
(:py:class:`Config`): The config.

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


def global_variables_initializer():
    if context.executing_eagerly():
        return control_flow_ops.no_op(name='global_variables_initializer')
    return variables_initializer(global_variables())

Returns an Op that initializes global variables.

This is just a shortcut for `variables_initializer(global_variables())`

@compatibility(TF2)
In TF2, variables are initialized immediately when they are created. There is
no longer a need to run variable initializers before using them.
@end_compatibility

Returns:
An Op that initializes global variables in the graph.


def __init__(
      self, resolver_context, file_system, path_spec, is_root=False,
      is_virtual=False):

    encoded_stream = resolver.Resolver.OpenFileObject(
        path_spec, resolver_context=resolver_context)
    if not encoded_stream:
      raise errors.BackEndError(
          'Unable to open encoded stream: {0:s}.'.format(
              self.path_spec.comparable))

    super(EncodedStreamFileEntry, self).__init__(
        resolver_context, file_system, path_spec, is_root=is_root,
        is_virtual=is_virtual)
    self._encoded_stream = encoded_stream
    self.entry_type = definitions.FILE_ENTRY_TYPE_FILE

Initializes a file entry.

Args:
resolver_context (Context): resolver context.
file_system (FileSystem): file system.
path_spec (PathSpec): path specification.
is_root (Optional[bool]): True if the file entry is the root file entry
of the corresponding file system.
is_virtual (Optional[bool]): True if the file entry is a virtual file

Raises:
BackEndError: when the encoded stream is missing.


def _ParseMRUListExValue(self, registry_key):

    mrulistex_value = registry_key.GetValueByName('MRUListEx')

    if not mrulistex_value:
      return None

    mrulistex_entries_map = self._GetDataTypeMap('mrulistex_entries')

    context = dtfabric_data_maps.DataTypeMapContext(values={
        'data_size': len(mrulistex_value.data)})

    return self._ReadStructureFromByteStream(
        mrulistex_value.data, 0, mrulistex_entries_map, context=context)

Parses the MRUListEx value in a given Registry key.

Args:
registry_key (dfwinreg.WinRegistryKey): Windows Registry key that contains
the MRUListEx value.

Returns:
mrulistex_entries: MRUListEx entries or None if not available.


def poll(self, channel_id=None, json=None, **kwargs):

        path = "/event-service/v1/channels/{}/poll".format(channel_id)
        r = self._httpclient.request(
            method="POST",
            url=self.url,
            json=json,
            path=path,
            **kwargs
        )
        return r

Read one or more events from a channel.

Reads events (log records) from the identified channel. Events
are read in chronological order.

Args:
channel_id (str): The channel ID.
json (dict): Payload/request body.
**kwargs: Supported :meth:`~pancloud.httpclient.HTTPClient.request` parameters.

Returns:
requests.Response: Requests Response() object.

Examples:
Refer to ``event_poll.py`` example.


def _GetCachedEntryDataTypeMap(self, format_type, value_data, cached_entry_offset):
    if (format_type not in self._SUPPORTED_FORMAT_TYPES):
        raise errors.ParseError('Unsupported format type: {0:d}'.format(format_type))
    data_type_map_name = ''
    if (format_type == self._FORMAT_TYPE_XP):
        data_type_map_name = 'appcompatcache_cached_entry_xp_32bit'
    elif (format_type in (self._FORMAT_TYPE_8, self._FORMAT_TYPE_10)):
        data_type_map_name = 'appcompatcache_cached_entry_header_8'
    else:
        cached_entry = self._ParseCommon2003CachedEntry(value_data, cached_entry_offset)
        if ((cached_entry.path_offset_32bit == 0) and (cached_entry.path_offset_64bit != 0)):
            number_of_bits = '64'
        else:
            number_of_bits = '32'
        if (format_type == self._FORMAT_TYPE_2003):
            data_type_map_name = 'appcompatcache_cached_entry_2003_{0:s}bit'.format(number_of_bits)
        elif (format_type == self._FORMAT_TYPE_VISTA):
            data_type_map_name = 'appcompatcache_cached_entry_vista_{0:s}bit'.format(number_of_bits)
        elif (format_type == self._FORMAT_TYPE_7):
            data_type_map_name = 'appcompatcache_cached_entry_7_{0:s}bit'.format(number_of_bits)
    return self._GetDataTypeMap(data_type_map_name)

Determines the cached entry data type map.

Args:
format_type (int): format type.
value_data (bytes): value data.
cached_entry_offset (int): offset of the first cached entry data
relative to the start of the value data.

Returns:
dtfabric.DataTypeMap: data type map which contains a data type definition,
such as a structure, that can be mapped onto binary data or None
if the data type map is not defined.

Raises:
ParseError: if the cached entry data type map cannot be determined.


def _DictToListOfStrings(self, data_dict):

    ret_list = []
    for key, value in iter(data_dict.items()):
      if key in ('body', 'datetime', 'type', 'room', 'rooms', 'id'):
        continue
      ret_list.append('{0:s} = {1!s}'.format(key, value))

    return ret_list

Converts a dictionary into a list of strings.

Args:
data_dict (dict[str, object]): dictionary to convert.

Returns:
list[str]: list of strings.


def delete_box(self, key):

		if key:
			uri = self.box_root_uri + '/' + key
			return self._req('delete', uri)
		else:
			return requests.codes.bad_request, None

Deletes the box specified by the key
Args:
returns 	(status code for the DELETE request, success message dict)


def _row_from_mapping(mapping, schema):
    if (len(schema) == 0):
        raise ValueError(_TABLE_HAS_NO_SCHEMA)
    row = []
    for field in schema:
        if (field.mode == 'REQUIRED'):
            row.append(mapping[field.name])
        elif (field.mode == 'REPEATED'):
            row.append(mapping.get(field.name, ()))
        elif (field.mode == 'NULLABLE'):
            row.append(mapping.get(field.name))
        else:
            raise ValueError('Unknown field mode: {}'.format(field.mode))
    return tuple(row)

Convert a mapping to a row tuple using the schema.

Args:
mapping (Dict[str, object])
Mapping of row data: must contain keys for all required fields in
the schema. Keys which do not correspond to a field in the schema
are ignored.
schema (List[google.cloud.bigquery.schema.SchemaField]):
The schema of the table destination for the rows

Returns:
Tuple[object]:
Tuple whose elements are ordered according to the schema.

Raises:
ValueError: If schema is empty.


def get_neighbor_ip(ip_addr, cidr="30"):

    our_octet = None
    neighbor_octet = None
    try:
        ip_addr_split = ip_addr.split(".")
        max_counter = 0
        if int(cidr) == 30:
            ranger = 4
        elif int(cidr) == 31:
            ranger = 2
        while max_counter < 256:
            try:
                if int(ip_addr_split[3]) >= max_counter and int(ip_addr_split[3]) < (max_counter + ranger):
                    if ranger == 4:
                        our_octet = max_counter + 1
                        neighbor_octet = max_counter + 2
                        break
                    elif ranger == 2:
                        our_octet = max_counter
                        neighbor_octet = max_counter + 1
                        break
                max_counter += ranger
            except UnboundLocalError:
                print("The mask between the neighbors must be 30, or 31")
                exit("BAD NEIGHBOR MASK")
        if int(ip_addr_split[3]) == our_octet:
            our_ip_addr = "%s.%s.%s.%i" % (ip_addr_split[0], ip_addr_split[1], ip_addr_split[2], our_octet)
            neighbor_ip_addr = "%s.%s.%s.%i" % (ip_addr_split[0], ip_addr_split[1], ip_addr_split[2], neighbor_octet)
        elif int(ip_addr_split[3]) == neighbor_octet:
            neighbor_ip_addr = "%s.%s.%s.%i" % (ip_addr_split[0], ip_addr_split[1], ip_addr_split[2], our_octet)
            our_ip_addr = "%s.%s.%s.%i" % (ip_addr_split[0], ip_addr_split[1], ip_addr_split[2], neighbor_octet)
        else:
            our_ip_addr = "%s.%s.%s.%i" % (ip_addr_split[0], ip_addr_split[1], ip_addr_split[2], our_octet)
            neighbor_ip_addr = "%s.%s.%s.%i" % (ip_addr_split[0], ip_addr_split[1], ip_addr_split[2], neighbor_octet)
        return our_ip_addr, neighbor_ip_addr
    except IndexError:
        LOGGER.critical('Function get_neighbor_ip IndexError ip_addr {item} cidr {cidr}'.format(item=ip_addr,
                                                                                cidr=cidr))
        raise IndexError("You have entered invalid input, you must enter a ipv4 address")

Function to figure out the IP's between neighbors address
Args:
ip_addr: Unicast IP address in the following format 192.168.1.1
cidr: CIDR value of 30, or 31

Returns: returns Our IP and the Neighbor IP in a tuple


def CreateSharedBudget(client):

  budget_service = client.GetService('BudgetService', version='v201809')

  budget = {
      'name': 'Shared Interplanetary Budget
      'amount': {
          'microAmount': '2000000'
      },
      'deliveryMethod': 'STANDARD',
      'isExplicitlyShared': 'true'
  }

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

  response = budget_service.mutate([operation])
  return response['value'][0]

Creates an explicit budget to be used only to create the Campaign.

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

Returns:
dict An object representing a shared budget.


def load(url_or_handle, cache=None, **kwargs):

    ext = get_extension(url_or_handle)
    try:
        loader = loaders[ext.lower()]
        message = "Using inferred loader '%s' due to passed file extension '%s'."
        log.debug(message, loader.__name__[6:], ext)
        return load_using_loader(url_or_handle, loader, cache, **kwargs)

    except KeyError:

        log.warning("Unknown extension '%s', attempting to load as image.", ext)
        try:
            with read_handle(url_or_handle, cache=cache) as handle:
                result = _load_img(handle)
        except Exception as e:
            message = "Could not load resource %s as image. Supported extensions: %s"
            log.error(message, url_or_handle, list(loaders))
            raise RuntimeError(message.format(url_or_handle, list(loaders)))
        else:
            log.info("Unknown extension '%s' successfully loaded as image.", ext)
            return result

Load a file.

File format is inferred from url. File retrieval strategy is inferred from
URL. Returned object type is inferred from url extension.

Args:
url_or_handle: a (reachable) URL, or an already open file handle

Raises:
RuntimeError: If file extension or URL is not supported.


def make_connection_with_forwarded_port(self, host_port, device_port, uid=UNKNOWN_UID, cmd=ConnectionHandshakeCommand.INIT):
    self.host_port = host_port
    self.device_port = device_port
    self._counter = self._id_counter()
    self.create_socket_connection()
    self.send_handshake_request(uid, cmd)

Makes a connection to the server with the given forwarded port.

This process assumes that a device port has already been forwarded to a
host port, and it only makes a connection to the snippet server based on
the forwarded port. This is typically used by clients that share the same
snippet server, e.g. the snippet client and its event client.

Args:
host_port: int, the host port which has already been forwarded.
device_port: int, the device port listened by the snippet server.
uid: int, the uid of the server session to continue. It will be ignored
if the `cmd` requires the server to create a new session.
cmd: ConnectionHandshakeCommand, the handshake command Enum for the
server, which requires the server to create a new session or use the
current session.


Return the Coordinator used by the Supervisor.

The Coordinator can be useful if you want to run multiple threads
during your training.

Returns:
A Coordinator object.


def insert(self, table, insert_obj, ignore=True):

        if isinstance(insert_obj, pd.DataFrame):
            if insert_obj.empty:
                raise ValueError('The input DataFrame is empty, please check!')
            insert_obj = insert_obj.to_dict(orient='records')
        elif not isinstance(insert_obj, list):
            raise ValueError(
                f"The {reprlib.repr(insert_obj)} must be list of dicts type!")

        ignore_str = 'IGNORE' if ignore else ''
        return self._session.execute(
            table.__table__.insert().prefix_with(ignore_str), insert_obj)

[insert bulk data]

Arguments:
table {[DeclarativeMeta cls]} -- [reflection of table]
insert_obj {[pd.DataFrame or list of dicts]} -- [insert_obj]

Keyword Arguments:
ignore {bool} -- [wether ignore exception or not] (default: {True})

Raises:
ValueError -- [f"The {reprlib.repr(insert_obj)} must be list of dicts type!"]

Returns:
[type] -- [description]


def __init__(self, action, error):

        logger.error(action, exc_info=error)

        QDialog.__init__(self)
        self.setWindowTitle(_('Autosave error'))
        self.setModal(True)

        layout = QVBoxLayout()

        header = _('Error message:')
        txt = '
{}

{}
{!s}'.format(action, header, error)
        layout.addWidget(QLabel(txt))
        layout.addSpacing(15)

        txt = _("Hide all future autosave-related errors during this session")
        self.dismiss_box = QCheckBox(txt)
        layout.addWidget(self.dismiss_box)
        layout.addSpacing(15)

        button_box = QDialogButtonBox(QDialogButtonBox.Ok)
        button_box.accepted.connect(self.accept)
        layout.addWidget(button_box)

        self.setLayout(layout)

Constructor.

Args:
action (str): what Spyder was trying to do when error occured
error (Exception): the error that occured


def ConvertOutputToUnicode(self, buf):
    if isinstance(buf, str):
        buf = buf.encode(self._encoding)
    return str(buf, self._encoding, 'replace')

Converts a console output string buf to unicode.

Mainly used for testing. Allows test comparisons in unicode while ensuring
that unicode => encoding => unicode works.

Args:
buf: The console output string to convert.

Returns:
The console output string buf converted to unicode.


def __init__(self, dims):

    self._dims = [convert_to_dimension(d) for d in tuple(dims)]
    if len(set(dims)) != len(dims):
      raise ValueError("Shape must not have repeated dimensions %s" % dims)

Constructs a shape for a Tensor or Mesh.

Args:
dims: List-like of Dimensions.

Raises:
ValueError: If Dimensions are repeated.


def load_entity(self, name, file_name, reload_cache=False):

        Entity.verify_name(name)
        self.entities.load(Entity.wrap_name(name), file_name, reload_cache)
        with open(file_name) as f:
            self.padaos.add_entity(name, f.read().split('\n'))
        self.must_train = True

Loads an entity, optionally checking the cache first

Args:
name (str): The associated name of the entity
file_name (str): The location of the entity file
reload_cache (bool): Whether to refresh all of cache


def __init__(self, etk, cdr_document: Dict, mime_type, url, doc_id=None) -> None:

        Segment.__init__(self, json_path="$", _value=cdr_document, _document=self)
        self.etk = etk
        self.cdr_document = cdr_document
        self.mime_type = mime_type
        self.url = url
        if doc_id:
            self.cdr_document["doc_id"] = doc_id
        self.extraction_provenance_records = list()
        if self.etk.kg_schema:
            self.kg = KnowledgeGraph(self.etk.kg_schema, self.etk.ontology, self)
        else:
            self.kg = None
            if not self.etk.kg_schema:
                self.etk.log("Schema not found.", "warning", self.doc_id, self.url)
        self._provenance_id_index = 0
        self._provenances = dict()
        self._jsonpath_provenances = dict()
        self._kg_provenances = dict()

Wrapper object for CDR documents.

Args:
etk (ETK): embed the etk object so that docs have access to global info.
cdr_document (JSON): the raw CDR document received in ETK.

Returns: the wrapped CDR document


def auth(self, token):
    t = self.sendToken(token)
    return self.getToken(t)

Take an existing Skype token and refresh it, to extend the expiry time without other credentials.

Args:
token (str): existing Skype token

Returns:
(str, datetime.datetime) tuple: Skype token, and associated expiry if known

Raises:
.SkypeAuthException: if the login request is rejected
.SkypeApiException: if the login form can't be processed


def _snapshot_task_progresses(self) -> Iterable[_pywrap_server_lib.SnapshotTaskProgressWrapper]:
    return self._server.snapshot_task_progresses()

Returns the progresses of the snapshot tasks currently being executed.

Returns:
An `Iterable[common_pb2.SnapshotTaskProgress]`.


def make_iterable(value):
    if (sys.version_info <= (3, 0)):
        if isinstance(value, unicode):
            value = str(value)
    if (isinstance(value, str) or isinstance(value, dict)):
        value = [value]
    if (not isinstance(value, collections.Iterable)):
        raise TypeError('value must be an iterable object')
    return value

Converts the supplied value to a list object

This function will inspect the supplied value and return an
iterable in the form of a list.

Args:
value (object): An valid Python object

Returns:
An iterable object of type list


Returns the HTML-style hex code for the Colour.

Returns:
str: the colour as a HTML-sytle hex string


def fastrcnn_predictions(boxes, scores):
    assert (boxes.shape[1] == cfg.DATA.NUM_CLASS)
    assert (scores.shape[1] == cfg.DATA.NUM_CLASS)
    boxes = tf.transpose(boxes, [1, 0, 2])[(1:, :, :)]
    scores = tf.transpose(scores[(:, 1:)], [1, 0])

    def f(X):
        '\n        prob: n probabilities\n        box: nx4 boxes\n\n        Returns: n boolean, the selection\n        '
        (prob, box) = X
        output_shape = tf.shape(prob, out_type=tf.int64)
        ids = tf.reshape(tf.where((prob > cfg.TEST.RESULT_SCORE_THRESH)), [(- 1)])
        prob = tf.gather(prob, ids)
        box = tf.gather(box, ids)
        selection = tf.image.non_max_suppression(box, prob, cfg.TEST.RESULTS_PER_IM, cfg.TEST.FRCNN_NMS_THRESH)
        selection = tf.gather(ids, selection)
        if (get_tf_version_tuple() >= (1, 13)):
            sorted_selection = tf.sort(selection, direction='ASCENDING')
            mask = tf.sparse.SparseTensor(indices=tf.expand_dims(sorted_selection, 1), values=tf.ones_like(sorted_selection, dtype=tf.bool), dense_shape=output_shape)
            mask = tf.sparse.to_dense(mask, default_value=False)
        else:
            sorted_selection = (- tf.nn.top_k((- selection), k=tf.size(selection))[0])
            mask = tf.sparse_to_dense(sparse_indices=sorted_selection, output_shape=output_shape, sparse_values=True, default_value=False)
        return mask
    buggy_tf = (get_tf_version_tuple() in [(1, 11), (1, 12)])
    masks = tf.map_fn(f, (scores, boxes), dtype=tf.bool, parallel_iterations=(1 if buggy_tf else 10))
    selected_indices = tf.where(masks)
    scores = tf.boolean_mask(scores, masks)
    (topk_scores, topk_indices) = tf.nn.top_k(scores, tf.minimum(cfg.TEST.RESULTS_PER_IM, tf.size(scores)), sorted=False)
    filtered_selection = tf.gather(selected_indices, topk_indices)
    (cat_ids, box_ids) = tf.unstack(filtered_selection, axis=1)
    final_scores = tf.identity(topk_scores, name='scores')
    final_labels = tf.add(cat_ids, 1, name='labels')
    final_ids = tf.stack([cat_ids, box_ids], axis=1, name='all_ids')
    final_boxes = tf.gather_nd(boxes, final_ids, name='boxes')
    return (final_boxes, final_scores, final_labels)

Generate final results from predictions of all proposals.

Args:
boxes: n#classx4 floatbox in float32
scores: nx#class

Returns:
boxes: Kx4
scores: K
labels: K


def dequantize(arr, min_val, max_val, levels, dtype=np.float64):

    if not (isinstance(levels, int) and levels > 1):
        raise ValueError(
            'levels must be a positive integer, but got {}'.format(levels))
    if min_val >= max_val:
        raise ValueError(
            'min_val ({}) must be smaller than max_val ({})'.format(
                min_val, max_val))

    dequantized_arr = (arr + 0.5).astype(dtype) * (
        max_val - min_val) / levels + min_val

    return dequantized_arr

Dequantize an array.

Args:
arr (ndarray): Input array.
min_val (scalar): Minimum value to be clipped.
max_val (scalar): Maximum value to be clipped.
levels (int): Quantization levels.
dtype (np.type): The type of the dequantized array.

Returns:
tuple: Dequantized array.


def _prefix_from_prefix_string(self, prefixlen_str):
    try:
        if (not _BaseV4._DECIMAL_DIGITS.issuperset(prefixlen_str)):
            raise ValueError
        prefixlen = int(prefixlen_str)
        if (not (0 <= prefixlen <= self._max_prefixlen)):
            raise ValueError
    except ValueError:
        raise NetmaskValueError(('%s is not a valid prefix length' % prefixlen_str))
    return prefixlen

Turn a prefix length string into an integer.

Args:
prefixlen_str: A decimal string containing the prefix length.

Returns:
The prefix length as an integer.

Raises:
NetmaskValueError: If the input is malformed or out of range.


def date_clean(date, dashboard_style=False):

  if dashboard_style:
    dt = str(date)
    out = dt[4:6] + '/' + dt[6:] + '/' + dt[:4]
  else:
    dt = str(date)
    out = dt[:4] + '-' + dt[4:6] + '-' + dt[6:]
  return out

Clean the numerical date value in order to present it.

Args:
boo: numerical date (20160205)
Returns:
Stringified version of the input date ("2016-02-05")


def _process_parameter_type(param, param_name, func):
    optional = False
    if param.annotation != inspect.Parameter.empty:
        param_type = param.annotation
        if 'typing' in str(param_type):
            param_type = ''.join(str(param_type).split('typing.')).replace('transformers.', '~')
        elif hasattr(param_type, '__module__'):
            param_type = f'{param_type.__module__.replace('transformers.', '~').replace('builtins', '')}.{param.annotation.__name__}'
            if param_type[0] == '.':
                param_type = param_type[1:]
        elif False:
            print(f'🚨 {param_type} for {param_name} of {func.__qualname__} in file {func.__code__.co_filename} has an invalid type')
        if 'ForwardRef' in param_type:
            param_type = re.sub("ForwardRef\\('([\\w.]+)'\\)", '\\1', param_type)
        if 'Optional' in param_type:
            param_type = re.sub('Optional\\[(.*?)\\]', '\\1', param_type)
            optional = True
    else:
        param_type = ''
    return (param_type, optional)

Process and format a parameter's type annotation.

Args:
param (`inspect.Parameter`): The parameter from the function signature
param_name (`str`): The name of the parameter
func (`function`): The function the parameter belongs to


def _openfile(instance, filething, filename, fileobj, writable, create):
    assert ((not create) or writable)
    if isinstance(filething, FileThing):
        filename = filething.filename
        fileobj = filething.fileobj
        filething = None
    if (filething is not None):
        if is_fileobj(filething):
            fileobj = filething
        elif hasattr(filething, '__fspath__'):
            filename = filething.__fspath__()
            if (not isinstance(filename, (bytes, text_type))):
                raise TypeError('expected __fspath__() to return a filename')
        else:
            filename = filething
    if (instance is not None):
        if (not writable):
            instance.filename = filename
        elif (filename is None):
            filename = getattr(instance, 'filename', None)
    if (fileobj is not None):
        verify_fileobj(fileobj, writable=writable)
        (yield FileThing(fileobj, filename, (filename or fileobj_name(fileobj))))
    elif (filename is not None):
        verify_filename(filename)
        inmemory_fileobj = False
        try:
            fileobj = open(filename, ('rb+' if writable else 'rb'))
        except IOError as e:
            if (writable and (e.errno == errno.EOPNOTSUPP)):
                try:
                    with open(filename, 'rb') as fileobj:
                        fileobj = BytesIO(fileobj.read())
                except IOError as e2:
                    raise MutagenError(e2)
                inmemory_fileobj = True
            elif (create and (e.errno == errno.ENOENT)):
                assert writable
                try:
                    fileobj = open(filename, 'wb+')
                except IOError as e2:
                    raise MutagenError(e2)
            else:
                raise MutagenError(e)
        with fileobj as fileobj:
            (yield FileThing(fileobj, filename, filename))
            if inmemory_fileobj:
                assert writable
                data = fileobj.getvalue()
                try:
                    with open(filename, 'wb') as fileobj:
                        fileobj.write(data)
                except IOError as e:
                    raise MutagenError(e)
    else:
        raise TypeError('Missing filename or fileobj argument')

yields a FileThing

Args:
filething: Either a file name, a file object or None
filename: Either a file name or None
fileobj: Either a file object or None
writable (bool): if the file should be opened
create (bool): if the file should be created if it doesn't exist.
implies writable
Raises:
MutagenError: In case opening the file failed
TypeError: in case neither a file name or a file object is passed


def write_file(self, path, contents):

		path = self._get_dist_path(path)
		if not os.path.isdir(os.path.dirname(path)):
			os.makedirs(os.path.dirname(path))
		if isinstance(contents, bytes):
			mode = 'wb+'
		else:
			mode = 'w'
		with open(path, mode) as file:
			file.write(contents)

Write a file of any type to the destination path. Useful for files like
robots.txt, manifest.json, and so on.

Args:
path (str): The name of the file to write to.
contents (str or bytes): The contents to write.


def to_json(self):
    return {'lat': self.lat, 'lon': self.lon, 'time': (self.time.isoformat() if (self.time is not None) else None)}

Creates a JSON serializable representation of this instance

Returns:
:obj:`dict`: For example,
{
"lat": 9.3470298,
"lon": 3.79274,
"time": "2016-07-15T15:27:53.574110"
}


def remove(package_name):
    if (package_name not in packages):
        raise HolodeckException(('Unknown package name ' + package_name))
    for (config, path) in _iter_packages():
        if (config['name'] == package_name):
            shutil.rmtree(path)

Removes a holodeck package.

Args:
package_name (str): the name of the package to remove


def pack_small_tensors(tower_grads, max_bytes=0):
    assert (max_bytes >= 0)
    orig_grads = [g for (g, _) in tower_grads[0]]
    assert all(((g.dtype == tf.float32) for g in orig_grads))
    sizes = [(4 * g.shape.num_elements()) for g in orig_grads]
    print_stats(sizes)
    small_ranges = []
    large_indices = []
    new_sizes = []

    def end_interval(indices, small_ranges, large_indices):
        if (len(indices) > 1):
            small_ranges.insert(0, [indices[0], indices[(- 1)]])
        else:
            large_indices.insert(0, indices[0])
    cur_range = []
    cur_size = 0
    for (i, s) in reversed(list(enumerate(sizes))):
        if (cur_size > max_bytes):
            end_interval(cur_range, small_ranges, large_indices)
            new_sizes.insert(0, cur_size)
            cur_range = []
            cur_size = 0
        cur_range.insert(0, i)
        cur_size += s
    end_interval(cur_range, small_ranges, large_indices)
    new_sizes.insert(0, cur_size)
    print_stats(new_sizes)
    num_gv = len(orig_grads)
    packing = {}
    if len(small_ranges):
        new_tower_grads = []
        for (dev_idx, gv_list) in enumerate(tower_grads):
            assert (len(gv_list) == num_gv), "Possible cause: Networks constructed on different workers don't have the same number of variables. If you use tf.GraphKeys or tf.global_variables() with multiple graphs per worker during network construction, you need to use appropriate scopes, see https:
            new_gv_list = []
            for r in small_ranges:
                key = ('%d:%d' % (dev_idx, len(new_gv_list)))
                new_gv_list.append((pack_range(key, packing, gv_list, r), 'packing_var_placeholder'))
            for i in large_indices:
                new_gv_list.append(gv_list[i])
            new_tower_grads.append(new_gv_list)
        return (new_tower_grads, packing)
    else:
        return (tower_grads, None)

Concatenate gradients together more intelligently.

Does binpacking
Args:
tower_grads: List of lists of (gradient, variable) tuples.
max_bytes: Int giving max number of bytes in a tensor that
may be considered small.


def format_sympy_expr(sympy_expr, functions=None):

  if functions is None:
    functions = {}
  str_expr = str(sympy_expr)
  result = str_expr.replace(" ", "")
  for fn_name, char in six.iteritems(functions):
    result = result.replace(fn_name, char)
  return result

Convert sympy expression into a string which can be encoded.

Args:
sympy_expr: Any sympy expression tree or string.
functions: Defines special functions. A dict mapping human readable string
names, like "log", "exp", "sin", "cos", etc., to single chars. Each
function gets a unique token, like "L" for "log".

Returns:
A string representation of the expression suitable for encoding as a
sequence input.


def WinChmod(filename, acl_list, user=None):

  if user is None:
    user = win32api.GetUserName()

  if not os.path.exists(filename):
    raise RuntimeError("filename %s does not exist" % filename)

  acl_bitmask = 0
  for acl in acl_list:
    acl_bitmask |= getattr(ntsecuritycon, acl)

  dacl = win32security.ACL()
  win_user, _, _ = win32security.LookupAccountName("", user)

  dacl.AddAccessAllowedAce(win32security.ACL_REVISION, acl_bitmask, win_user)

  security_descriptor = win32security.GetFileSecurity(
      filename, win32security.DACL_SECURITY_INFORMATION)

  security_descriptor.SetSecurityDescriptorDacl(DACL_PRESENT, dacl,
                                                DACL_DEFAULT)
  win32security.SetFileSecurity(
      filename, win32security.DACL_SECURITY_INFORMATION, security_descriptor)

Provide chmod-like functionality for windows.

Doco links:
goo.gl/n7YR1
goo.gl/rDv81
goo.gl/hDobb

Args:
filename: target filename for acl

acl_list: list of ntsecuritycon acl strings to be applied with bitwise OR.
e.g. ["FILE_GENERIC_READ", "FILE_GENERIC_WRITE"]

user: username string. If not specified we use the user we are running as.

Raises:
AttributeError: if a bad permission is passed
RuntimeError: if filename doesn't exist


def save_aggregate_report_to_elasticsearch(aggregate_report,
                                           index_suffix=None,
                                           monthly_indexes=False):

    logger.debug("Saving aggregate report to Elasticsearch")
    aggregate_report = aggregate_report.copy()
    metadata = aggregate_report["report_metadata"]
    org_name = metadata["org_name"]
    report_id = metadata["report_id"]
    domain = aggregate_report["policy_published"]["domain"]
    begin_date = human_timestamp_to_datetime(metadata["begin_date"])
    end_date = human_timestamp_to_datetime(metadata["end_date"])
    begin_date_human = begin_date.strftime("%Y-%m-%d %H:%M:%S")
    end_date_human = end_date.strftime("%Y-%m-%d %H:%M:%S")
    if monthly_indexes:
        index_date = begin_date.strftime("%Y-%m")
    else:
        index_date = begin_date.strftime("%Y-%m-%d")
    aggregate_report["begin_date"] = begin_date
    aggregate_report["end_date"] = end_date
    date_range = [aggregate_report["begin_date"],
                  aggregate_report["end_date"]]

    org_name_query = Q(dict(match=dict(org_name=org_name)))
    report_id_query = Q(dict(match=dict(report_id=report_id)))
    domain_query = Q(dict(match={"published_policy.domain": domain}))
    begin_date_query = Q(dict(match=dict(date_range=begin_date)))
    end_date_query = Q(dict(match=dict(date_range=end_date)))

    search = Search(index="dmarc_aggregate*")
    query = org_name_query & report_id_query & domain_query
    query = query & begin_date_query & end_date_query
    search.query = query

    existing = search.execute()
    if len(existing) > 0:
        raise AlreadySaved("An aggregate report ID {0} from {1} about {2} "
                           "with a date range of {3} UTC to {4} UTC already "
                           "exists in "
                           "Elasticsearch".format(report_id,
                                                  org_name,
                                                  domain,
                                                  begin_date_human,
                                                  end_date_human))
    published_policy = _PublishedPolicy(
        domain=aggregate_report["policy_published"]["domain"],
        adkim=aggregate_report["policy_published"]["adkim"],
        aspf=aggregate_report["policy_published"]["aspf"],
        p=aggregate_report["policy_published"]["p"],
        sp=aggregate_report["policy_published"]["sp"],
        pct=aggregate_report["policy_published"]["pct"],
        fo=aggregate_report["policy_published"]["fo"]
    )

    for record in aggregate_report["records"]:
        agg_doc = _AggregateReportDoc(
            xml_schemea=aggregate_report["xml_schema"],
            org_name=metadata["org_name"],
            org_email=metadata["org_email"],
            org_extra_contact_info=metadata["org_extra_contact_info"],
            report_id=metadata["report_id"],
            date_range=date_range,
            errors=metadata["errors"],
            published_policy=published_policy,
            source_ip_address=record["source"]["ip_address"],
            source_country=record["source"]["country"],
            source_reverse_dns=record["source"]["reverse_dns"],
            source_base_domain=record["source"]["base_domain"],
            message_count=record["count"],
            disposition=record["policy_evaluated"]["disposition"],
            dkim_aligned=record["policy_evaluated"]["dkim"] == "pass",
            spf_aligned=record["policy_evaluated"]["spf"] == "pass",
            header_from=record["identifiers"]["header_from"],
            envelope_from=record["identifiers"]["envelope_from"],
            envelope_to=record["identifiers"]["envelope_to"]
        )

        for override in record["policy_evaluated"]["policy_override_reasons"]:
            agg_doc.add_policy_override(type_=override["type"],
                                        comment=override["comment"])

        for dkim_result in record["auth_results"]["dkim"]:
            agg_doc.add_dkim_result(domain=dkim_result["domain"],
                                    selector=dkim_result["selector"],
                                    result=dkim_result["result"])

        for spf_result in record["auth_results"]["spf"]:
            agg_doc.add_spf_result(domain=spf_result["domain"],
                                   scope=spf_result["scope"],
                                   result=spf_result["result"])

        index = "dmarc_aggregate"
        if index_suffix:
            index = "{0}_{1}".format(index, index_suffix)
        index = "{0}-{1}".format(index, index_date)
        create_indexes([index])
        agg_doc.meta.index = index

        try:
            agg_doc.save()
        except Exception as e:
            raise ElasticsearchError(
                "Elasticsearch error: {0}".format(e.__str__()))

Saves a parsed DMARC aggregate report to ElasticSearch

Args:
aggregate_report (OrderedDict): A parsed forensic report
index_suffix (str): The suffix of the name of the index to save to
monthly_indexes (bool): Use monthly indexes instead of daily indexes

Raises:
AlreadySaved


def load_all_yamls(cls, directories):
    yaml_files = []
    loaded_yamls = {}
    for d in directories:
        if (d.startswith('/home') and (not os.path.exists(d))):
            os.makedirs(d)
        for (dirname, subdirs, files) in os.walk(d):
            yaml_files.extend(map((lambda x: os.path.join(dirname, x)), filter((lambda x: x.endswith('.yaml')), files)))
    for f in yaml_files:
        loaded_yamls[f] = cls.load_yaml_by_path(f)
    return loaded_yamls

Loads yaml files from all given directories.

Args:
directories: list of directories to search
Returns:
dict of {fullpath: loaded_yaml_structure}


def DeleteSignedBinary(binary_urn, token=None):
    if _ShouldUseLegacyDatastore():
        try:
            aff4.FACTORY.Open(binary_urn, aff4_type=aff4.AFF4Stream, mode='r', token=token)
        except aff4.InstantiationError:
            raise SignedBinaryNotFoundError(binary_urn)
        aff4.FACTORY.Delete(binary_urn, token=token)
    if data_store.RelationalDBEnabled():
        try:
            data_store.REL_DB.ReadSignedBinaryReferences(_SignedBinaryIDFromURN(binary_urn))
        except db.UnknownSignedBinaryError:
            if _ShouldUseLegacyDatastore():
                return
            else:
                raise SignedBinaryNotFoundError(binary_urn)
        data_store.REL_DB.DeleteSignedBinaryReferences(_SignedBinaryIDFromURN(binary_urn))

Deletes the binary with the given urn from the datastore.

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

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


def open_file(cls, filename: str, response: BaseResponse, mode='wb+'):

        _logger.debug('Saving file to {0}, mode={1}.',
                      filename, mode)

        dir_path = os.path.dirname(filename)
        if dir_path and not os.path.exists(dir_path):
            os.makedirs(dir_path)

        response.body = Body(open(filename, mode))

Open a file object on to the Response Body.

Args:
filename: The path where the file is to be saved
response: Response
mode: The file mode

This function will create the directories if not exist.


def _create_keras_history_helper(tensors, processed_ops, created_layers):
    if ops.executing_eagerly_outside_functions():
        raise ValueError('`create_keras_history` should only be called if eager is disabled!')
    from tensorflow.python.keras.engine import base_layer
    tensor_list = nest.flatten(tensors)
    sparse_ops = []
    ragged_tensors = []
    for tensor in tensor_list:
        if getattr(tensor, '_keras_history', None) is not None:
            continue
        if isinstance(tensor, (sparse_tensor.SparseTensor, sparse_tensor.SparseTensorValue)):
            sparse_ops.append(tensor.op)
            continue
        if tf_utils.is_ragged(tensor):
            ragged_tensors.append(tensor)
            continue
        op = tensor.op
        if op not in processed_ops:
            op_inputs = list(op.inputs)
            constants = {}
            layer_inputs = []
            for i, op_input in enumerate(op_inputs):
                if uses_keras_history(op_input):
                    layer_inputs.append(op_input)
                else:
                    ds_with_session = distribute_lib.in_cross_replica_context() and (not ops.executing_eagerly_outside_functions())
                    using_xla = control_flow_util.GraphOrParentsInXlaContext(ops.get_default_graph())
                    if ds_with_session or using_xla or _UNSAFE_GRAPH_OP_LAYER_CREATION:
                        constants[i] = op_input
                    else:
                        with ops.init_scope():
                            constants[i] = backend.function([], op_input)([])
            layer_inputs = unnest_if_single_tensor(layer_inputs)
            processed_ops, created_layers = _create_keras_history_helper(layer_inputs, processed_ops, created_layers)
            name = op.name
            node_def = op.node_def.SerializeToString()
            op_layer = base_layer.TensorFlowOpLayer(node_def, constants=constants, name=name)
            created_layers.append(op_layer)
            op_layer._set_connectivity_metadata(args=(layer_inputs,), kwargs={}, outputs=op.outputs)
            processed_ops.update([op])
    if sparse_ops or ragged_tensors:
        lambda_example = '\n    weights_mult = lambda x: tf.sparse.sparse_dense_matmul(x, weights)\n    output = tf.keras.layers.Lambda(weights_mult)(input)\n    '
        raise ValueError('Tensorflow ops that generate ragged or sparse tensor outputs are currently not supported by Keras automatic op wrapping. Please wrap these ops in a Lambda layer: \n\n```\n{example}\n```\nSparse ops encountered: {sparse_ops}\nRagged tensors encountered: {ragged_tensors}\n'.format(example=lambda_example, sparse_ops=str(sparse_ops), ragged_tensors=str(ragged_tensors)))
    return (processed_ops, created_layers)

Helper method for `create_keras_history`.

Args:
tensors: A structure of Tensors for which to create Keras metadata.
processed_ops: Set. TensorFlow operations that have already been wrapped in
`TensorFlowOpLayer` instances.
created_layers: List. The `TensorFlowOpLayer` instances created.

Returns:
Tuple. First element is the updated set of TensorFlow Operations that
have been wrapped in `TensorFlowOpLayer` instances. Second element is
a list of the `TensorFlowOpLayer` instances created.


def infer_tests_to_run(output_file: str, diff_with_last_commit: bool=False, filter_models: bool=False, test_all: bool=False):
    if not test_all:
        modified_files = get_modified_python_files(diff_with_last_commit=diff_with_last_commit)
    else:
        modified_files = [str(k) for k in PATH_TO_TESTS.glob('*test_**.py', recursive=True) + glob.glob('examples*.py', recursive=True)
        if len(model_impacted) >= NUM_MODELS_TO_TRIGGER_FULL_CI and filter_models:
            print(f'More than {NUM_MODELS_TO_TRIGGER_FULL_CI - 1} models are impacted and `filter_models=False`. CI is configured to test everything.')
    else:
        test_files_to_run = [f for f in modified_files if f.startswith('tests') and '/test_' in f]
        impacted_files = get_impacted_files_from_tiny_model_summary(diff_with_last_commit=diff_with_last_commit)
        test_map = create_module_to_test_map(reverse_map=reverse_map, filter_models=filter_models)
        for f in modified_files + impacted_files:
            if f in test_map:
                test_files_to_run.extend(test_map[f])
        test_files_to_run = sorted(set(test_files_to_run))
        test_files_to_run = [f for f in test_files_to_run if not f.split(os.path.sep)[1] == 'repo_utils']
        test_files_to_run = [f for f in test_files_to_run if not f.split(os.path.sep)[1] == 'sagemaker']
        test_files_to_run = [f for f in test_files_to_run if (PATH_TO_REPO / f).exists()]
    print(f'\n
    create_test_list_from_filter(test_files_to_run, out_path='test_preparation/')
    doctest_list = get_doctest_files()
    print(f'\n
    if len(doctest_list) > 0:
        doctest_file = Path(output_file).parent / 'doctest_list.txt'
        with open(doctest_file, 'w', encoding='utf-8') as f:
            f.write(' '.join(doctest_list))

The main function called by the test fetcher. Determines the tests to run from the diff.

Args:
output_file (`str`):
The path where to store the summary of the test fetcher analysis. Other files will be stored in the same
folder:

- examples_test_list.txt: The list of examples tests to run.
- test_repo_utils.txt: Will indicate if the repo utils tests should be run or not.
- doctest_list.txt: The list of doctests to run.

diff_with_last_commit (`bool`, *optional*, defaults to `False`):
Whether to analyze the diff with the last commit (for use on the main branch after a PR is merged) or with
the branching point from main (for use on each PR).
filter_models (`bool`, *optional*, defaults to `True`):
Whether or not to filter the tests to core models only, when a file modified results in a lot of model
tests.


def remove_objects_from_args(args: Iterable[Any], kwargs: Dict[str, Any], pvalue_class: Union[Type[T], Tuple[Type[T], ...]]) -> Tuple[List[Any], Dict[str, Any], List[T]]:
    pvals = []

    def swapper(value):
        pvals.append(value)
        return ArgumentPlaceholder()
    new_args = [swapper(v) if isinstance(v, pvalue_class) else v for v in args]
    new_kwargs = dict(((k, swapper(v)) if isinstance(v, pvalue_class) else (k, v) for k, v in sorted(kwargs.items())))
    return (new_args, new_kwargs, pvals)

For internal use only; no backwards-compatibility guarantees.

Replaces all objects of a given type in args/kwargs with a placeholder.

Args:
args: A list of positional arguments.
kwargs: A dictionary of keyword arguments.
pvalue_class: A class object representing the types of arguments that must
be replaced with a placeholder value (instance of ArgumentPlaceholder).

Returns:
A 3-tuple containing a modified list of positional arguments, a modified
dictionary of keyword arguments, and a list of all objects replaced with
a placeholder value.


def load_transcripts(adapter, transcripts_lines=None, build='37', ensembl_genes=None):
    ensembl_genes = (ensembl_genes or adapter.ensembl_genes(build))
    if (transcripts_lines is None):
        transcripts_lines = fetch_ensembl_transcripts(build=build)
    transcripts_dict = parse_transcripts(transcripts_lines)
    for ens_tx_id in list(transcripts_dict):
        parsed_tx = transcripts_dict[ens_tx_id]
        ens_gene_id = parsed_tx['ensembl_gene_id']
        gene_obj = ensembl_genes.get(ens_gene_id)
        if (not gene_obj):
            transcripts_dict.pop(ens_tx_id)
            LOG.debug('Gene %s does not exist in build %s', ens_gene_id, build)
            continue
        parsed_tx['hgnc_id'] = gene_obj['hgnc_id']
        parsed_tx['primary_transcripts'] = set(gene_obj.get('primary_transcripts', []))
    ref_seq_transcripts = 0
    nr_primary_transcripts = 0
    nr_transcripts = len(transcripts_dict)
    transcript_objs = []
    with progressbar(transcripts_dict.values(), label='Building transcripts', length=nr_transcripts) as bar:
        for tx_data in bar:
            tx_data['is_primary'] = False
            primary_transcripts = tx_data['primary_transcripts']
            refseq_identifier = None
            refseq_identifiers = []
            for category in TRANSCRIPT_CATEGORIES:
                identifiers = tx_data[category]
                if (not identifiers):
                    continue
                for refseq_id in identifiers:
                    refseq_identifiers.append(refseq_id)
                    ref_seq_transcripts += 1
                    if (refseq_id in primary_transcripts):
                        refseq_identifier = refseq_id
                        tx_data['is_primary'] = True
                        nr_primary_transcripts += 1
                    if (not refseq_identifier):
                        refseq_identifier = refseq_id
            if refseq_identifier:
                tx_data['refseq_id'] = refseq_identifier
            if refseq_identifiers:
                tx_data['refseq_identifiers'] = refseq_identifiers
            tx_obj = build_transcript(tx_data, build)
            transcript_objs.append(tx_obj)
    LOG.info('Loading transcripts...')
    if (len(transcript_objs) > 0):
        adapter.load_transcript_bulk(transcript_objs)
    LOG.info('Number of transcripts in build %s: %s', build, nr_transcripts)
    LOG.info('Number of transcripts with refseq identifier: %s', ref_seq_transcripts)
    LOG.info('Number of primary transcripts: %s', nr_primary_transcripts)
    return transcript_objs

Load all the transcripts

Transcript information is from ensembl.

Args:
adapter(MongoAdapter)
transcripts_lines(iterable): iterable with ensembl transcript lines
build(str)
ensembl_genes(dict): Map from ensembl_id -> HgncGene

Returns:
transcript_objs(list): A list with all transcript objects


def start_dag(self, dag, *, data=None):
    return self._client.send(Request(action='start_dag', payload={'name': (dag.name if isinstance(dag, Dag) else dag), 'data': (data if isinstance(data, MultiTaskData) else None)})).payload['dag_name']

Schedule the execution of a dag by sending a signal to the workflow.

Args:
dag (Dag, str): The dag object or the name of the dag that should be started.
data (MultiTaskData): The data that should be passed on to the new dag.

Returns:
str: The name of the successfully started dag.


def _pick_or_create_inserted_op_moment_index(self, splitter_index: int, op: ops.Operation, strategy: InsertStrategy) -> int:
    if ((strategy is InsertStrategy.NEW) or (strategy is InsertStrategy.NEW_THEN_INLINE)):
        self._moments.insert(splitter_index, ops.Moment())
        return splitter_index
    if (strategy is InsertStrategy.INLINE):
        if ((0 <= (splitter_index - 1) < len(self._moments)) and self._can_add_op_at((splitter_index - 1), op)):
            return (splitter_index - 1)
        return self._pick_or_create_inserted_op_moment_index(splitter_index, op, InsertStrategy.NEW)
    if (strategy is InsertStrategy.EARLIEST):
        if self._can_add_op_at(splitter_index, op):
            p = self._prev_moment_available(op, splitter_index)
            return (p or 0)
        return self._pick_or_create_inserted_op_moment_index(splitter_index, op, InsertStrategy.INLINE)
    raise ValueError('Unrecognized append strategy: {}'.format(strategy))

Determines and prepares where an insertion will occur.

Args:
splitter_index: The index to insert at.
op: The operation that will be inserted.
strategy: The insertion strategy.

Returns:
The index of the (possibly new) moment where the insertion should
occur.

Raises:
ValueError: Unrecognized append strategy.


def write(self, b):
    self._checkClosed()
    self._uploader.put(b)
    bytes_written = len(b)
    self._position += bytes_written
    return bytes_written

Write bytes from b.

Returns number of bytes written (<= len(b)).

Args:
b: (memoryview) Buffer with data to write.


def instantiate_references_json(references_json):

    references = {}
    for obj in references_json:
        obj_id = obj['id']
        obj_type = obj.get('subtype', obj['type'])

        cls = get_class(obj_type)
        instance = cls.__new__(cls, id=obj_id)
        if instance is None:
            raise RuntimeError('Error loading model from JSON (type: %s, id: %s)' % (obj_type, obj_id))
        references[instance.id] = instance

    return references

Given a JSON representation of all the models in a graph, return a
dict of new model objects.

Args:
references_json (``JSON``)
JSON specifying new Bokeh models to create

Returns:
dict[str, Model]


def run(self):

        for build_dir in self.build_dirs:
            if os.path.isdir(build_dir):
                sys.stdout.write('Removing %s%s' % (build_dir, os.linesep))
                shutil.rmtree(build_dir)

        for (root, dirs, files) in os.walk(self.cwd):
            for name in files:
                fullpath = os.path.join(root, name)
                if any(fullpath.endswith(ext) for ext in self.build_artifacts):
                    sys.stdout.write('Removing %s%s' % (fullpath, os.linesep))
                    os.remove(fullpath)

Runs the command.

Args:
self (CleanCommand): the ``CleanCommand`` instance

Returns:
``None``


def _CalculateComprehensionState(self, newline):
    current = self.next_token
    previous = current.previous_token
    top_of_stack = self.comp_stack[-1] if self.comp_stack else None
    penalty = 0
    if top_of_stack is not None:
        if current == top_of_stack.closing_bracket:
            last = self.comp_stack.pop()
            if last.has_interior_split:
                penalty += style.Get('SPLIT_PENALTY_COMPREHENSION')
            return penalty
        if newline:
            top_of_stack.has_interior_split = True
    if subtypes.COMP_EXPR in current.subtypes and subtypes.COMP_EXPR not in previous.subtypes:
        self.comp_stack.append(object_state.ComprehensionState(current))
        return penalty
    if current.value == 'for' and subtypes.COMP_FOR in current.subtypes:
        if top_of_stack.for_token is not None:
            if style.Get('SPLIT_COMPLEX_COMPREHENSION') and top_of_stack.has_split_at_for != newline and (top_of_stack.has_split_at_for or not top_of_stack.HasTrivialExpr()):
                penalty += split_penalty.UNBREAKABLE
        else:
            top_of_stack.for_token = current
            top_of_stack.has_split_at_for = newline
            if style.Get('SPLIT_COMPLEX_COMPREHENSION') and newline and top_of_stack.HasTrivialExpr():
                penalty += split_penalty.CONNECTED
    if subtypes.COMP_IF in current.subtypes and subtypes.COMP_IF not in previous.subtypes:
        if style.Get('SPLIT_COMPLEX_COMPREHENSION') and top_of_stack.has_split_at_for != newline and (top_of_stack.has_split_at_for or not top_of_stack.HasTrivialExpr()):
            penalty += split_penalty.UNBREAKABLE
    return penalty

Makes required changes to comprehension state.

Args:
newline: Whether the current token is to be added on a newline.

Returns:
The penalty for the token-newline combination given the current
comprehension state.


def get_leaves(self, item_ids=None, language=None, forbidden_item_ids=None):

        forbidden_item_ids = set() if forbidden_item_ids is None else set(forbidden_item_ids)
        children = self.get_children_graph(item_ids, language=language, forbidden_item_ids=forbidden_item_ids)
        counts = self.get_children_counts(active=None)
        if item_ids is None:

            item_ids = set(children.keys())

        def _get_leaves(item_id):
            leaves = set()

            def __search(item_ids):
                result = set(flatten([children.get(item_id, []) for item_id in item_ids]))
                new_leaves = {item_id for item_id in result if item_id not in children.keys()}
                leaves.update(new_leaves)
                return result - new_leaves

            fixed_point(
                is_zero=lambda to_visit: len(to_visit) == 0,
                minus=lambda to_visit, visited: to_visit - visited,
                plus=lambda visited_x, visited_y: visited_x | visited_y,
                f=__search,
                x={item_id}
            )
            leaves = {leaf for leaf in leaves if counts[leaf] == 0}
            if len(leaves) > 0:
                return leaves
            if counts[item_id] == 0 and item_id not in forbidden_item_ids:
                return {item_id}
            return set()

        return {item_id: _get_leaves(item_id) for item_id in item_ids}

Get mapping of items to their reachable leaves. Leaves having
inactive relations to other items are omitted.

Args:
item_ids (list): items which are taken as roots for the reachability
language (str): if specified, filter out items which are not
available in the given language

Returns:
dict: item id -> list of items (reachable leaves)


def _validate(self):
    probably_good_to_go = True
    sheet = self.table
    identity = self.db_sheet_cols.id
    id_col = sheet.loc[(:, identity)]
    if any(id_col.duplicated()):
        warnings.warn('your database is corrupt: duplicates encountered in the srno-column')
        logger.debug(('srno duplicates:\n' + str(id_col.duplicated())))
        probably_good_to_go = False
    return probably_good_to_go

Checks that the db-file is ok

Returns:
True if OK, False if not.


def _any_overlap_or_contiguous(self, test_overlap: bool) -> bool:

        for i in range(len(self.intervals)):
            for j in range(i + 1, len(self.intervals)):
                first = self.intervals[i]
                second = self.intervals[j]
                if test_overlap:
                    test = first.overlaps(second)
                else:
                    test = first.contiguous(second)
                if test:
                    return True
        return False

Do any of the intervals overlap?

Args:
test_overlap: if ``True``, test for overlapping intervals; if
``False``, test for contiguous intervals.


def get_gdns_publisher(config, metrics, **kwargs):
    builder = gdns_publisher.GDNSPublisherBuilder(config, metrics, **kwargs)
    return builder.build_publisher()

Get a GDNSPublisher client.

A factory function that validates configuration and returns a
publisher client (:interface:`gordon.interfaces.IMessageHandler`)
provider.

Args:
config (dict): Google Cloud DNS API related configuration.
metrics (obj): :interface:`IMetricRelay` implementation.
kwargs (dict): Additional keyword arguments to pass to the
publisher.
Returns:
A :class:`GDNSPublisher` instance.


def operator_and_matrix(self, shapes_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False):
    raise NotImplementedError('Not implemented yet.')

Build a batch matrix and an Operator that should have similar behavior.

Every operator acts like a (batch) matrix.  This method returns both
together, and is used by tests.

Args:
shapes_info: `OperatorShapesInfo`, encoding shape information about the
operator.
dtype:  Numpy dtype.  Data type of returned array/operator.
use_placeholder:  Python bool.  If True, initialize the operator with a
placeholder of undefined shape and correct dtype.
ensure_self_adjoint_and_pd: If `True`,
construct this operator to be Hermitian Positive Definite, as well
as ensuring the hints `is_positive_definite` and `is_self_adjoint`
are set.
This is useful for testing methods such as `cholesky`.

Returns:
operator:  `LinearOperator` subclass instance.
mat:  `Tensor` representing operator.


def profile_view(request, user_id=None):
    if (request.user.is_eighthoffice and ('full' not in request.GET) and (user_id is not None)):
        return redirect('eighth_profile', user_id=user_id)
    if (user_id is not None):
        try:
            profile_user = User.objects.get(id=user_id)
            if (profile_user is None):
                raise Http404
        except User.DoesNotExist:
            raise Http404
    else:
        profile_user = request.user
    num_blocks = 6
    eighth_schedule = []
    start_block = EighthBlock.objects.get_first_upcoming_block()
    blocks = []
    if start_block:
        blocks = ([start_block] + list(start_block.next_blocks((num_blocks - 1))))
    for block in blocks:
        sch = {'block': block}
        try:
            sch['signup'] = EighthSignup.objects.get(scheduled_activity__block=block, user=profile_user)
        except EighthSignup.DoesNotExist:
            sch['signup'] = None
        except MultipleObjectsReturned:
            client.captureException()
            sch['signup'] = None
        eighth_schedule.append(sch)
    if profile_user.is_eighth_sponsor:
        sponsor = EighthSponsor.objects.get(user=profile_user)
        start_date = get_start_date(request)
        eighth_sponsor_schedule = EighthScheduledActivity.objects.for_sponsor(sponsor).filter(block__date__gte=start_date).order_by('block__date', 'block__block_letter')
        eighth_sponsor_schedule = eighth_sponsor_schedule[:10]
    else:
        eighth_sponsor_schedule = None
    admin_or_teacher = (request.user.is_eighth_admin or request.user.is_teacher)
    can_view_eighth = (profile_user.can_view_eighth or (request.user == profile_user))
    eighth_restricted_msg = ((not can_view_eighth) and admin_or_teacher)
    if ((not can_view_eighth) and (not request.user.is_eighth_admin) and (not request.user.is_teacher)):
        eighth_schedule = []
    has_been_nominated = (profile_user.username in [u.nominee.username for u in request.user.nomination_votes.filter(position__position_name=settings.NOMINATION_POSITION)])
    context = {'profile_user': profile_user, 'eighth_schedule': eighth_schedule, 'can_view_eighth': can_view_eighth, 'eighth_restricted_msg': eighth_restricted_msg, 'eighth_sponsor_schedule': eighth_sponsor_schedule, 'nominations_active': settings.NOMINATIONS_ACTIVE, 'nomination_position': settings.NOMINATION_POSITION, 'has_been_nominated': has_been_nominated}
    return render(request, 'users/profile.html', context)

Displays a view of a user's profile.

Args:
user_id
The ID of the user whose profile is being viewed. If not
specified, show the user's own profile.


def _import_templates(force=False):

    tmplpath = os.path.join(resource_filename('cloud_inquisitor', 'data'), 'templates')
    disk_templates = {f: os.path.join(root, f) for root, directory, files in os.walk(tmplpath) for f in files}
    db_templates = {tmpl.template_name: tmpl for tmpl in db.Template.find()}

    for name, template_file in disk_templates.items():
        with open(template_file, 'r') as f:
            body = f.read()
        disk_hash = get_hash(body)

        if name not in db_templates:
            template = Template()
            template.template_name = name
            template.template = body

            db.session.add(template)
            auditlog(
                event='template.import',
                actor='init',
                data={
                    'template_name': name,
                    'template': body
                }
            )
            logger.info('Imported template {}'.format(name))
        else:
            template = db_templates[name]
            db_hash = get_hash(template.template)

            if db_hash != disk_hash:
                if force or not db_templates[name].is_modified:
                    template.template = body

                    db.session.add(template)
                    auditlog(
                        event='template.update',
                        actor='init',
                        data={
                            'template_name': name,
                            'template_diff': diff(template.template, body)
                        }
                    )
                    logger.info('Updated template {}'.format(name))
                else:
                    logger.warning(
                        'Updated template available for {}. Will not import as it would'
                        ' overwrite user edited content and force is not enabled'.format(name)
                    )

Import templates from disk into database

Reads all templates from disk and adds them to the database. By default, any template that has been modified by
the user will not be updated. This can however be changed by setting `force` to `True`, which causes all templates
to be imported regardless of status

Args:
force (`bool`): Force overwrite any templates with local changes made. Default: `False`

Returns:
`None`


def _IsotonicRegressionGrad(op: ops.Operation, grad_output, grad_segments):
    del grad_segments
    segments = op.outputs[1]
    return _MeanAggregator(grad_output, segments)

Gradient for the isotonic regression function.

Args:
op: The IsotonicRegression tensorflow op.
grad_output: Tensor of incoming gradients with respect to the output.
grad_segments: Tensor of incoming gradients with respect to the segments.

Returns:
A tensor, same size as `grad_output` with the gradient with respect to
the input.


def set_parameters(self, parameters_dict):

        DB.set_hash_value(self._key, 'parameters', parameters_dict)
        self.publish("parameters_updated")

Set the subarray parameters.

Args:
parameters_dict (dict): Dictionary of Subarray parameters


def box(self, x0, y0, width, height):
    assert (width > 1)
    assert (height > 1)
    width -= 1
    height -= 1
    for x in range(x0, (x0 + width)):
        self.point(x, y0, '-')
        self.point(x, (y0 + height), '-')
    for y in range(y0, (y0 + height)):
        self.point(x0, y, '|')
        self.point((x0 + width), y, '|')
    self.point(x0, y0, '+')
    self.point((x0 + width), y0, '+')
    self.point(x0, (y0 + height), '+')
    self.point((x0 + width), (y0 + height), '+')

Create a box on ASCII canvas.

Args:
x0 (int): x coordinate of the box corner.
y0 (int): y coordinate of the box corner.
width (int): box width.
height (int): box height.


def append(self, text, afterline=None):

        if afterline:
            self._vim.current.buffer.append(text, afterline)
        else:
            self._vim.current.buffer.append(text)

Append text to the current buffer.

Args:
text (str or Sequence[str]): One or many lines of text to append.
afterline (Optional[int]):
Line number to append after. If 0, text is prepended before the
first line; if ``None``, at end of the buffer.


def _add_saveable(saveables, seen_ops, saveable):
    if saveable.op is not None and saveable.op in seen_ops:
        raise ValueError(f'The same saveable will be restored with two names: {saveable.name}')
    saveables.append(saveable)
    seen_ops.add(saveable.op)

Adds the saveable to the saveables list.

Args:
saveables: List to append the SaveableObject to.
seen_ops: Set of the ops of the saveables already processed.  Used to
check that each saveable is only saved once.
saveable: The saveable.

Raises:
ValueError: If the saveable has already been processed.


def emit(self, record):

        properties = {
            'process': record.processName,
            'module': record.module,
            'fileName': record.filename,
            'lineNumber': record.lineno,
            'level': record.levelname,
        }

        if record.exc_info:
            self.client.track_exception(*record.exc_info, properties=properties)
            return

        formatted_message = self.format(record)
        self.client.track_trace(formatted_message, properties=properties, severity=record.levelname)

Emit a record.

If a formatter is specified, it is used to format the record. If exception information is present, an Exception
telemetry object is sent instead of a Trace telemetry object.

Args:
record (:class:`logging.LogRecord`). the record to format and send.


def collect_function_arg_names(function_names, return_all_args_function_names, function_renames):
    function_name_v1_to_attr = {}
    function_name_v2_to_attr = {}

    def visit(unused_path, unused_parent, children):

        for child in children:
            _, attr = tf_decorator.unwrap(child[1])
            api_names_v1 = ['tf.' + name for name in tf_export.get_v1_names(attr)]
            if any((name in function_names for name in api_names_v1)):
                for name in api_names_v1:
                    function_name_v1_to_attr[name] = attr
            api_names_v2 = ['tf.' + name for name in tf_export.get_v2_names(attr)]
            for name in api_names_v2:
                function_name_v2_to_attr[name] = attr
    visitor = public_api.PublicAPIVisitor(visit)
    visitor.do_not_descend_map['tf'].append('contrib')
    visitor.private_map['tf.compat'] = ['v1', 'v2']
    traverse.traverse(tf.compat.v1, visitor)
    traverse.traverse(tf.compat.v2, visitor)

    def get_arguments_list(attr):
        if tf_inspect.isclass(attr):
            arg_list = tf_inspect.getargspec(getattr(attr, '__init__'))[0]
            return arg_list[1:]
        else:
            return tf_inspect.getargspec(attr)[0]
    function_to_args = {}
    if any((name not in function_name_v1_to_attr for name in function_names)):
        raise ValueError(f'Symbols not found in `tf.compat.v1`: `{'`, `'.join(function_names - function_name_v1_to_attr.keys())}`')
    for name_v1, attr_v1 in function_name_v1_to_attr.items():
        args_v1 = get_arguments_list(attr_v1)
        if name_v1 in return_all_args_function_names:
            function_to_args[name_v1] = args_v1
            continue
        name_v2 = name_v1
        if name_v1 in function_renames:
            name_v2 = function_renames[name_v1]
            if name_v2.startswith('tf.compat.v1.'):
                raise ValueError(f'Symbol `{name_v1}` is renamed to `{name_v2}`, no need to add keyword argument names, remove from `reordered_function_names`')
        if name_v2 not in function_name_v2_to_attr:
            raise ValueError(f'Symbol `{name_v2}` not found in `tf.compat.v2`')
        args_v2 = get_arguments_list(function_name_v2_to_attr[name_v2])
        if args_v1 == args_v2:
            raise ValueError(f'Symbol `{name_v1}` has no changes in arguments, no need to add keyword argument names, remove from `reordered_function_names`')
        needed_arg_names = []
        same_so_far = True
        for index, arg in enumerate(args_v1):
            if same_so_far and index < len(args_v2) and (arg == args_v2[index]):
                needed_arg_names.append(None)
            else:
                same_so_far = False
                needed_arg_names.append(arg)
        function_to_args[name_v1] = needed_arg_names
    return function_to_args

Determines argument names for reordered function signatures.

Args:
function_names: Functions to collect arguments for.
return_all_args_function_names: Functions to collect all argument names for.
function_renames: Function renames between v1 and v2.

Returns:
Dictionary mapping function names to a list of argument names. Each argument
name list can have leading `None` elements to indicate that some of the
function arguments did not change between v1 and v2.


def bam2es(
        bam_fn,
        es_fo,
        allowed_delta,
    ):

        es_fo.write("
        es_fo.write("
        es_fo.write("
        es_fo.write("
        es_fo.write("
        es_fo.write("
        es_fo.write("
        es_fo.write("
        es_fo.write("
        es_fo.write("
        es_fo.write("
        es_fo.write("
        es_fo.write("
        es_fo.write("
        es_fo.write("

        with pysam.AlignmentFile(bam_fn, "rb") as sam:
            references_dict = {}

            for i in range(len(sam.references)):
                references_dict[sam.references[i]] = i + 1

            for read in sam:
                rnf_read_tuple = rnftools.rnfformat.ReadTuple()
                rnf_read_tuple.destringize(read.query_name)

                left = read.reference_start + 1
                right = read.reference_end
                chrom_id = references_dict[sam.references[read.reference_id]]

                nb_of_segments = len(rnf_read_tuple.segments)

                if rnf_read_tuple.segments[0].genome_id == 1:
                    should_be_mapped = True
                else:
                    should_be_mapped = False

                if read.is_unmapped:

                    if should_be_mapped:
                        category = "u"

                    else:
                        category = "U"

                else:

                    if should_be_mapped:
                        exists_corresponding_segment = False

                        for j in range(len(rnf_read_tuple.segments)):
                            segment = rnf_read_tuple.segments[j]
                            if (
                                (segment.left == 0 or abs(segment.left - left) <= allowed_delta)
                                and (segment.right == 0 or abs(segment.right - right) <= allowed_delta)
                                and (segment.left != 0 or segment.right == 0)
                                and (chrom_id == 0 or chrom_id == segment.chr_id)
                            ):
                                exists_corresponding_segment = True
                                segment = str(j + 1)
                                break

                        if exists_corresponding_segment:
                            category = "M_" + segment

                        else:
                            category = "w"

                    else:
                        category = "m"

                es_fo.write(
                    "\t".join(
                        map(
                            str,
                            [

                                read.query_name,

                                "unmapped" if read.is_unmapped else "mapped_" + str(read.mapping_quality),

                                chrom_id,

                                "R" if read.is_reverse else "F",

                                left,

                                right,

                                category,

                                nb_of_segments
                            ]
                        )
                    ) + os.linesep
                )

Convert BAM file to ES file.

Args:
bam_fn (str): File name of the BAM file.
bam_fo (file): File object of the ES file.
allowed_delta (int): Maximal allowed coordinates difference for correct reads.


def _SetSELinuxContext(path):

  restorecon = '/sbin/restorecon'
  if os.path.isfile(restorecon) and os.access(restorecon, os.X_OK):
    subprocess.call([restorecon, path])

Set the appropriate SELinux context, if SELinux tools are installed.

Calls /sbin/restorecon on the provided path to set the SELinux context as
specified by policy. This call does not operate recursively.

Only some OS configurations use SELinux. It is therefore acceptable for
restorecon to be missing, in which case we do nothing.

Args:
path: string, the path on which to fix the SELinux context.


def write_config(config, config_path=CONFIG_PATH):
    if (not os.path.exists(config_path)):
        os.makedirs(os.path.dirname(config_path))
    with open(config_path, 'w', encoding='utf-8') as f:
        config.write(f)

Write the config to the output path.
Creates the necessary directories if they aren't there.

Args:
config (configparser.ConfigParser): A ConfigParser.


def update_state(self, y_true, y_pred, sample_weight=None):
    y_true = ops.convert_to_tensor(y_true, self._dtype)
    y_pred = ops.convert_to_tensor(y_pred, self._dtype)
    y_true, y_pred = squeeze_or_expand_to_same_rank(y_true, y_pred)
    error_sq = ops.square(y_pred - y_true)
    return super().update_state(error_sq, sample_weight=sample_weight)

Accumulates root mean squared error statistics.

Args:
y_true: The ground truth values.
y_pred: The predicted values.
sample_weight: Optional weighting of each example. Can
be a `Tensor` whose rank is either 0, or the same rank as
`y_true`, and must be broadcastable to `y_true`.
Defaults to `1`.

Returns:
Update op.


def run(self, fn, args=(), kwargs=None, options=None):
    return super(OneDeviceStrategy, self).run(fn, args, kwargs, options)

Run `fn` on each replica, with the given arguments.

In `OneDeviceStrategy`, `fn` is simply called within a device scope for the
given device, with the provided arguments.

Args:
fn: The function to run. The output must be a `tf.nest` of `Tensor`s.
args: (Optional) Positional arguments to `fn`.
kwargs: (Optional) Keyword arguments to `fn`.
options: (Optional) An instance of `tf.distribute.RunOptions` specifying
the options to run `fn`.

Returns:
Return value from running `fn`.


def _ragged_embedding_lookup_with_reduce(table: tf_variables.Variable, ragged: ragged_tensor.RaggedTensor, weights: ragged_tensor.RaggedTensor, combiner: str) -> core.Tensor:
    if weights is None:
        weights = array_ops.ones_like(ragged, dtype=table.dtype)
    weights = array_ops.expand_dims(weights, axis=2)
    ragged_result = embedding_ops.embedding_lookup(table, ragged)
    ragged_result = math_ops.reduce_sum(ragged_result * weights, axis=1)
    if combiner == 'mean':
        ragged_result = math_ops.div_no_nan(ragged_result, math_ops.reduce_sum(weights, axis=1))
    elif combiner == 'sqrtn':
        ragged_result = math_ops.div_no_nan(ragged_result, math_ops.sqrt(math_ops.reduce_sum(weights * weights, axis=1)))
    return ragged_result

Compute a ragged lookup followed by a reduce on axis 1.

Args:
table: The embedding table.
ragged: A RaggedTensor of ids to look up.
weights: A RaggedTensor of weights (or None).
combiner: One of "mean", "sum", "sqrtn".

Returns:
A Tensor.


def repository_blob(self, sha, **kwargs):
    path = ('/projects/%s/repository/blobs/%s' % (self.get_id(), sha))
    return self.manager.gitlab.http_get(path, **kwargs)

Return a file by blob SHA.

Args:
sha(str): ID of the blob
**kwargs: Extra options to send to the server (e.g. sudo)

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

Returns:
dict: The blob content and metadata


def get_supervisor(func: types.AnyFunction) -> types.Supervisor:

    if not callable(func):
        raise TypeError("func is not callable")
    if asyncio.iscoroutinefunction(func):
        supervisor = _async_supervisor
    else:
        supervisor = _sync_supervisor
    return functools.partial(supervisor, func)

Get the appropriate supervisor to use and pre-apply the function.

Args:
func: A function.


def _ResizeBilinearGrad(op: ops.Operation, grad):
    grad0 = gen_image_ops.resize_bilinear_grad(grad, op.inputs[0], align_corners=op.get_attr('align_corners'), half_pixel_centers=op.get_attr('half_pixel_centers'))
    return [grad0, None]

The derivatives for bilinear resizing.

Args:
op: The ResizeBilinear op.
grad: The tensor representing the gradient w.r.t. the output.

Returns:
The gradients w.r.t. the input.


def sunset(self, date=None, zenith=None):

        return (segment.sunset(date, zenith) for segment in self)

Calculate sunset times for locations.

Args:
date (datetime.date): Calculate rise or set for given date
zenith (str): Calculate sunset events, or start of twilight times

Returns:
list of list of datetime.datetime: The time for the sunset for each
point in each segment


def ws004c(self, value=None):

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

        self._ws004c = value

Corresponds to IDD Field `ws004c`

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

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


def add_positional_embedding(x, max_length, name=None, positions=None):
    with tf.name_scope('add_positional_embedding'):
        (_, length, depth) = common_layers.shape_list(x)
        var = tf.cast(tf.get_variable(name, [max_length, depth]), x.dtype)
        if (positions is None):
            pad_length = tf.maximum(0, (length - max_length))
            sliced = tf.cond(tf.less(length, max_length), (lambda : tf.slice(var, [0, 0], [length, (- 1)])), (lambda : tf.pad(var, [[0, pad_length], [0, 0]])))
            return (x + tf.expand_dims(sliced, 0))
        else:
            return (x + tf.gather(var, tf.to_int32(positions)))

Adds positional embedding.

Args:
x: Tensor with shape [batch, length, depth].
max_length: int representing static maximum size of any dimension.
name: str representing name of the embedding tf.Variable.
positions: Tensor with shape [batch, length].

Returns:
Tensor of same shape as x.


def xzhdr(self, header, msgid_range=None):

        args = header
        if msgid_range is not None:
            args += " " + utils.unparse_msgid_range(msgid_range)

        code, message = self.command("XZHDR", args)
        if code != 221:
            raise NNTPReplyError(code, message)

        return self.info(code, message, compressed=True)

XZHDR command.

Args:
msgid_range: A message-id as a string, or an article number as an
integer, or a tuple of specifying a range of article numbers in
the form (first, [last]) - if last is omitted then all articles
after first are included. A msgid_range of None (the default)
uses the current article.


def _calculate_minimum_silent_period(baudrate):

    _checkNumerical(baudrate, minvalue=1, description='baudrate')

    BITTIMES_PER_CHARACTERTIME = 11
    MINIMUM_SILENT_CHARACTERTIMES = 3.5

    bittime = 1 / float(baudrate)
    return bittime * BITTIMES_PER_CHARACTERTIME * MINIMUM_SILENT_CHARACTERTIMES

Calculate the silent period length to comply with the 3.5 character silence between messages.

Args:
baudrate (numerical): The baudrate for the serial port

Returns:
The number of seconds (float) that should pass between each message on the bus.

Raises:
ValueError, TypeError.


def get_entities(seq, suffix=False):
    if any((isinstance(s, list) for s in seq)):
        seq = [item for sublist in seq for item in (sublist + ['O'])]
    prev_tag = 'O'
    prev_type = ''
    begin_offset = 0
    chunks = []
    for (i, chunk) in enumerate((seq + ['O'])):
        if suffix:
            tag = chunk[(- 1)]
            type_ = chunk.split('-')[0]
        else:
            tag = chunk[0]
            type_ = chunk.split('-')[(- 1)]
        if end_of_chunk(prev_tag, tag, prev_type, type_):
            chunks.append((prev_type, begin_offset, (i - 1)))
        if start_of_chunk(prev_tag, tag, prev_type, type_):
            begin_offset = i
        prev_tag = tag
        prev_type = type_
    return chunks

Gets entities from sequence.

Args:
seq (list): sequence of labels.

Returns:
list: list of (chunk_type, chunk_start, chunk_end).

Example:
>>> from seqeval.metrics.sequence_labeling import get_entities
>>> seq = ['B-PER', 'I-PER', 'O', 'B-LOC']
>>> get_entities(seq)
[('PER', 0, 1), ('LOC', 3, 3)]


def search(self, term: str, case_sensitive: bool = False) -> 'PrettyDir':

        if case_sensitive:
            return PrettyDir(
                self.obj, [pattr for pattr in self.pattrs if term in pattr.name]
            )
        else:
            term = term.lower()
            return PrettyDir(
                self.obj, [pattr for pattr in self.pattrs if term in pattr.name.lower()]
            )

Searches for names that match some pattern.

Args:
term: String used to match names. A name is returned if it matches
the whole search term.
case_sensitive: Boolean to match case or not, default is False
(case insensitive).

Return:
A PrettyDir object with matched names.


def get_current(cls):
    filepath = os.getenv('REZ_RXT_FILE')
    if ((not filepath) or (not os.path.exists(filepath))):
        return None
    return cls.load(filepath)

Get the context for the current env, if there is one.

Returns:
`ResolvedContext`: Current context, or None if not in a resolved env.


def GetMessages(self, formatter_mediator, event):

    if self.DATA_TYPE != event.data_type:
      raise errors.WrongFormatter('Unsupported data type: {0:s}.'.format(
          event.data_type))

    event_values = event.CopyToDict()

    cookie_flags = event_values.get('flags', None)
    if cookie_flags == 0:
      del event_values['flags']
    elif cookie_flags:
      flags = []
      for flag_value, flag_description in iter(self._COOKIE_FLAGS.items()):
        if cookie_flags & flag_value:
          flags.append(flag_description)

      event_values['flags'] = '|'.join(flags)

    return self._ConditionalFormatMessages(event_values)

Determines the formatted message strings for an event object.

Args:
formatter_mediator (FormatterMediator): mediates the interactions
between formatters and other components, such as storage and Windows
EventLog resources.
event (EventObject): event.

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

Raises:
WrongFormatter: if the event object cannot be formatted by the formatter.


def to(self, new_unit):
    return self.__class__((np.array(self) * self.unit.get_conversion_factor(new_unit)), unit_type=self.unit_type, unit=new_unit)

Conversion to a new_unit.

Args:
new_unit:
New unit type.

Returns:
A ArrayWithFloatWithUnit object in the new units.

Example usage:
>>> e = EnergyArray([1, 1.1], "Ha")
>>> e.to("eV")
array([ 27.21138386,  29.93252225]) eV


def set_tpu_core_ids(self, mesh_name, tpu_core_ids):
    _pywrap_dtensor_device.SetTPUCoreIDs(self._device_info, mesh_name, tpu_core_ids)

Sets the singleton global device ID-to-physical core ID map.

Args:
mesh_name: The name of a mesh. If empty, set the default mapping.
tpu_core_ids: TPU core IDs sorted by TF task/device ordinal.


def write_xls(data, file_name, worksheet_names=None):

    workbook = xlwt.Workbook()
    for sheet_index, sheet_data in enumerate(data):
        if worksheet_names and sheet_index < len(worksheet_names) and worksheet_names[sheet_index]:
            name = worksheet_names[sheet_index]
        else:
            name = 'Worksheet {}'.format(sheet_index)
        sheet = workbook.add_sheet(name)
        for row_index, row in enumerate(sheet_data):
            for col_index, value in enumerate(row):
                sheet.write(row_index, col_index, value)
    workbook.save(file_name)

Writes out to old excel format.

Args:
data: 2D list of tables/worksheets.
file_name: Name of the output file.
worksheet_names: A list of worksheet names (optional).


def param_type(self, name):
    self._ensure_loaded()
    if (name not in self.annotated_params):
        return None
    return self.annotated_params[name].type_name

Get the parameter type information by name.

Args:
name (str): The full name of a parameter.

Returns:
str: The type name or None if no type information is given.


def write_dot_file(G, filename):
    with io.open(filename, 'w') as fh:
        fh.write('strict digraph DependencyDiagram {\n')
        edge_list = G.edges()
        node_list = set(G.nodes())
        if edge_list:
            for edge in sorted(edge_list):
                (source, targ) = edge
                node_list = (node_list - set(source))
                node_list = (node_list - set(targ))
                line = '"{}" -> "{}";\n'
                fh.write(line.format(source, targ))
        if node_list:
            for node in sorted(node_list):
                line = '"{}"\n'.format(node)
                fh.write(line)
        fh.write('}')

Writes the graph G in dot file format for graphviz visualization.

Args:
a Networkx graph
A filename to name the dot files


def get_student_certificate(self, username, course_id):
    resp = self.requester.get(urljoin(self.base_url, '/api/certificates/v0/certificates/{username}/courses/{course_key}/'.format(username=username, course_key=course_id)))
    resp.raise_for_status()
    return Certificate(resp.json())

Returns an Certificate object with the user certificates

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

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


def update(self, reference, field_updates, option=None):
    if (option.__class__.__name__ == 'ExistsOption'):
        raise ValueError('you must not pass an explicit write option to update.')
    write_pbs = _helpers.pbs_for_update(reference._document_path, field_updates, option)
    self._add_write_pbs(write_pbs)

Add a "change" to update a document.

See
:meth:`~.firestore_v1beta1.document.DocumentReference.update` for
more information on ``field_updates`` and ``option``.

Args:
reference (~.firestore_v1beta1.document.DocumentReference): A
document reference that will be deleted in this batch.
field_updates (dict): Field names or paths to update and values
to update with.
option (Optional[~.firestore_v1beta1.client.WriteOption]): A
write option to make assertions / preconditions on the server
state of the document before applying changes.


def _is_autocomplete_valid(cur_commands, alias_command):
    parent_command = ' '.join(cur_commands[1:])
    with open(GLOBAL_ALIAS_TAB_COMP_TABLE_PATH, 'r') as tab_completion_table_file:
        try:
            tab_completion_table = json.loads(tab_completion_table_file.read())
            return ((alias_command in tab_completion_table) and (parent_command in tab_completion_table[alias_command]))
        except Exception:
            return False

Determine whether autocomplete can be performed at the current state.

Args:
parser: The current CLI parser.
cur_commands: The current commands typed in the console.
alias_command: The alias command.

Returns:
True if autocomplete can be performed.


def _print_list(self, values: List[Any], print_func: Callable[[Any], None]) -> None:
    self.generator.open_json_list()
    field_size = len(values)
    for i in range(field_size):
        print_func(values[i])
        if i < field_size - 1:
            self.generator.push(',')
            self.generator.add_newline()
    self.generator.close_json_list()

Adds the printed JSON list representation of values to _output.

Args:
values: The values to print as a JSON list.
print_func: A function responsible for printing a single value.


def _package_path(package):

    from os import path
    confdir = config_dir()
    return path.join(confdir, "{}.cfg".format(package))

Returns the full path to the default package configuration file.

Args:
package (str): name of the python package to return a path for.


def offsets_in_rows(self):
    return gen_ragged_math_ops.ragged_range(starts=constant_op.constant(0, self.dtype), limits=self.row_lengths(), deltas=constant_op.constant(1, self.dtype)).rt_dense_values

Return the offset of each value.

RowPartition takes an array x and converts it into sublists.
offsets[i] is the index of x[i] in its sublist.
Given a shape, such as:
[*,*,*],[*,*],[],[*,*]
This returns:
0,1,2,0,1,0,1

Returns:
an offset for every value.


def slice_list(in_list, lens):
    if (not isinstance(lens, list)):
        raise TypeError('"indices" must be a list of integers')
    elif (sum(lens) != len(in_list)):
        raise ValueError('sum of lens and list length does not match: {} != {}'.format(sum(lens), len(in_list)))
    out_list = []
    idx = 0
    for i in range(len(lens)):
        out_list.append(in_list[idx:(idx + lens[i])])
        idx += lens[i]
    return out_list

Slice a list into several sub lists by a list of given length.

Args:
in_list (list): The list to be sliced.
lens(int or list): The expected length of each out list.

Returns:
list: A list of sliced list.


def ChunkedCausalMultiHeadedAttention(
    feature_depth, num_heads=8, dropout=0.0, chunk_selector=None, mode='train'):

  prepare_attention_input = combinators.Serial(
      combinators.Branch(),
      combinators.Parallel(
          combinators.Branch(num_branches=3),
          CausalMask(axis=-2),
      ),
      combinators.Parallel(
          combinators.Parallel(
              core.Dense(feature_depth),
              core.Dense(feature_depth),
              core.Dense(feature_depth),
          ),
          combinators.Identity()
      )
  )
  return combinators.Serial(
      combinators.Map(prepare_attention_input),
      ChunkedAttentionSelector(selector=chunk_selector),
      combinators.Map(PureMultiHeadedAttention(
          feature_depth=feature_depth, num_heads=num_heads,
          dropout=dropout, mode=mode), check_shapes=False),
      combinators.Map(core.Dense(feature_depth))
  )

Transformer-style causal multi-headed attention operating on chunks.

Accepts inputs that are a list of chunks and applies causal attention.

Args:
feature_depth: int:  depth of embedding
num_heads: int: number of attention heads
dropout: float: dropout rate
chunk_selector: a function from chunk number to list of chunks to attend.
mode: str: 'train' or 'eval'

Returns:
Multi-headed self-attention layer.