def check_column(state, name, missing_msg=None, expand_msg=None):
    if (missing_msg is None):
        missing_msg = "We expected to find a column named `{{name}}` in the result of your query, but couldn't."
    if (expand_msg is None):
        expand_msg = 'Have another look at your query result. '
    msg_kwargs = {'name': name}
    has_result(state)
    stu_res = state.student_result
    sol_res = state.solution_result
    if (name not in sol_res):
        raise BaseException(('name %s not in solution column names' % name))
    if (name not in stu_res):
        _msg = state.build_message(missing_msg, fmt_kwargs=msg_kwargs)
        state.do_test(_msg)
    return state.to_child(append_message={'msg': expand_msg, 'kwargs': msg_kwargs}, student_result={name: stu_res[name]}, solution_result={name: sol_res[name]})

Zoom in on a particular column in the query result, by name.

After zooming in on a column, which is represented as a single-column query result,
you can use ``has_equal_value()`` to verify whether the column in the solution query result
matches the column in student query result.

Args:
name: name of the column to zoom in on.
missing_msg: if specified, this overrides the automatically generated feedback
message in case the column is missing in the student query result.
expand_msg: if specified, this overrides the automatically generated feedback
message that is prepended to feedback messages that are thrown
further in the SCT chain.

:Example:

Suppose we are testing the following SELECT statements

* solution: ``SELECT artist_id as id, name FROM artists``
* student : ``SELECT artist_id, name       FROM artists``

We can write the following SCTs: ::

# fails, since no column named id in student result
Ex().check_column('id')

# passes, since a column named name is in student_result
Ex().check_column('name')


def setup_session(self, server, hooks, graph_default_context):

        if self.execution_type == "distributed":

            session_creator = tf.train.ChiefSessionCreator(
                scaffold=self.scaffold,
                master=server.target,
                config=self.session_config,
                checkpoint_dir=None,
                checkpoint_filename_with_path=None
            )

            self.monitored_session = tf.train.MonitoredSession(
                session_creator=session_creator,
                hooks=hooks,
                stop_grace_period_secs=120
            )

            if self.tf_session_dump_dir != "":
                self.monitored_session = DumpingDebugWrapperSession(self.monitored_session, self.tf_session_dump_dir)
        else:

            self.monitored_session = tf.train.SingularMonitoredSession(
                hooks=hooks,
                scaffold=self.scaffold,
                master='',
                config=self.session_config,
                checkpoint_dir=None
            )

        if graph_default_context:
            graph_default_context.__exit__(None, None, None)
        self.graph.finalize()

        self.monitored_session.__enter__()
        self.session = self.monitored_session._tf_sess()

Creates and then enters the session for this model (finalizes the graph).

Args:
server (tf.train.Server): The tf.train.Server object to connect to (None for single execution).
hooks (list): A list of (saver, summary, etc..) hooks to be passed to the session.
graph_default_context: The graph as_default() context that we are currently in.


def add_custom_column_spec(self, spec: ColumnSpec) -> 'ColumnSpecsBuilder':
    self._specs.append(spec)
    return self

Add a custom :class:`.ColumnSpec` to the builder.

Use this method when you need complete control over the :class:`.ColumnSpec`
, including custom value extraction and type handling.

Args:
spec: A :class:`.ColumnSpec` instance defining the column name, type,
value extraction, and optional SQL type casting.

Returns:
Self for method chaining

Examples:
Custom text column from chunk metadata:

>>> builder.add_custom_column_spec(
...     ColumnSpec.text(
...         name="source_and_id",
...         value_fn=lambda chunk:         ...             f"{chunk.metadata.get('source')}_{chunk.id}"
...     )
... )


def load(self, fobj, index=None):

        if index is None:
            index = self._get_tab_index()
        page = self.pages[index]

        if fobj is None:
            return

        if not isinstance(fobj, tuple(page.clss_load)):
            raise RuntimeError('Object to load must be in {0!s} (not a {1!s})'.format(
             [x.__name__ for x in page.clss_load], fobj.__class__.__name__))

        page.editor.load(fobj)
        self._update_gui_text_tabs()

Loads given DataFile object. **tolerant with None**

Args:
fobj: object of one of accepted classes
index: tab index to load fobj into. If not passed, loads into current tab


def horizontal_infrared_radiation_intensity(self, value=9999.0):
    if (value is not None):
        try:
            value = float(value)
        except ValueError:
            raise ValueError('value {} need to be of type float for field `horizontal_infrared_radiation_intensity`'.format(value))
        if (value < 0.0):
            raise ValueError('value need to be greater or equal 0.0 for field `horizontal_infrared_radiation_intensity`')
    self._horizontal_infrared_radiation_intensity = value

Corresponds to IDD Field `horizontal_infrared_radiation_intensity`

Args:
value (float): value for IDD Field `horizontal_infrared_radiation_intensity`
Unit: Wh/m2
value >= 0.0
Missing value: 9999.0
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 run(self, circuit):
    name = circuit.name
    dag = circuit_to_dag(circuit)
    del circuit
    for passset in self.working_list:
        for pass_ in passset:
            dag = self._do_pass(pass_, dag, passset.options)
    circuit = dag_to_circuit(dag)
    circuit.name = name
    return circuit

Run all the passes on a QuantumCircuit

Args:
circuit (QuantumCircuit): circuit to transform via all the registered passes

Returns:
QuantumCircuit: Transformed circuit.


def __init__(self, func, name=None, indexed=None,
               repeated=None, verbose_name=None):

    super(ComputedProperty, self).__init__(name=name, indexed=indexed,
                                           repeated=repeated,
                                           verbose_name=verbose_name)
    self._func = func

Constructor.

Args:
func: A function that takes one argument, the model instance, and returns
a calculated value.


def modified_files(root, tracked_only=False, commit=None):
    assert os.path.isabs(root), ('Root has to be absolute, got: %s' % root)
    command = ['hg', 'status']
    if commit:
        command.append(('--change=%s' % commit))
    status_lines = subprocess.check_output(command).decode('utf-8').split(os.linesep)
    modes = ['M', 'A']
    if (not tracked_only):
        modes.append('\\?')
    modes_str = '|'.join(modes)
    modified_file_status = utils.filter_lines(status_lines, ('(?P<mode>%s) (?P<filename>.+)' % modes_str), groups=('filename', 'mode'))
    return dict(((os.path.join(root, filename), mode) for (filename, mode) in modified_file_status))

Returns a list of files that has been modified since the last commit.

Args:
root: the root of the repository, it has to be an absolute path.
tracked_only: exclude untracked files when True.
commit: SHA1 of the commit. If None, it will get the modified files in the
working copy.

Returns: a dictionary with the modified files as keys, and additional
information as value. In this case it adds the status returned by
hg status.


def parse_config_file(config_file, skip_unknown=False):

  for reader, existence_check in _FILE_READERS:
    if existence_check(config_file):
      with reader(config_file) as f:
        parse_config(f, skip_unknown=skip_unknown)
        return
  raise IOError('Unable to open file: {}'.format(config_file))

Parse a Gin config file.

Args:
config_file: The path to a Gin config file.
skip_unknown: A boolean indicating whether unknown configurables and imports
should be skipped instead of causing errors (alternatively a list of
configurable names to skip if unknown). See `parse_config` for additional
details.

Raises:
IOError: If `config_file` cannot be read using any register file reader.


def ParseOptions(self, options):

    self._ParseInformationalOptions(options)

    self._verbose = getattr(options, 'verbose', False)

    self._output_filename = getattr(options, 'write', None)

    argument_helper_names = ['process_resources', 'storage_file']
    helpers_manager.ArgumentHelperManager.ParseOptions(
        options, self, names=argument_helper_names)

    if not self._storage_file_path:
      raise errors.BadConfigOption('Missing storage file option.')

    if not os.path.isfile(self._storage_file_path):
      raise errors.BadConfigOption(
          'No such storage file: {0:s}.'.format(self._storage_file_path))

    compare_storage_file_path = self.ParseStringOption(
        options, 'compare_storage_file')
    if compare_storage_file_path:
      if not os.path.isfile(compare_storage_file_path):
        raise errors.BadConfigOption(
            'No such storage file: {0:s}.'.format(compare_storage_file_path))

      self._compare_storage_file_path = compare_storage_file_path
      self.compare_storage_information = True

    self._output_format = self.ParseStringOption(options, 'output_format')

    if self._output_filename:
      if os.path.exists(self._output_filename):
        raise errors.BadConfigOption(
            'Output file already exists: {0:s}.'.format(self._output_filename))
      output_file_object = open(self._output_filename, 'wb')
      self._output_writer = tools.FileObjectOutputWriter(output_file_object)

    self._EnforceProcessMemoryLimit(self._process_memory_limit)

Parses the options.

Args:
options (argparse.Namespace): command line arguments.

Raises:
BadConfigOption: if the options are invalid.


def genome_name_from_fasta_path(fasta_path):

    filename = os.path.basename(fasta_path)
    return re.sub(r'(\.fa$)|(\.fas$)|(\.fasta$)|(\.fna$)|(\.\w{1,}$)', '', filename)

Extract genome name from fasta filename

Get the filename without directory and remove the file extension.

Example:
With fasta file path ``/path/to/genome_1.fasta``::

fasta_path = '/path/to/genome_1.fasta'
genome_name = genome_name_from_fasta_path(fasta_path)
print(genome_name)
# => "genome_1"

Args:
fasta_path (str): fasta file path

Returns:
str: genome name


async def remove(self, *, node_id: str, force: bool=False) -> Mapping[(str, Any)]:
    params = {'force': force}
    response = (await self.docker._query_json('nodes/{node_id}'.format(node_id=node_id), method='DELETE', params=params))
    return response

Remove a node from a swarm.

Args:
node_id: The ID or name of the node


def __init__(self, options):

        self._options = options
        self._tasks = {}
        self._task_lock = threading.RLock()

Constructor.

Args:
options (gax.BundleOptions): configures strategy this instance
uses when executing bundled functions.


def decompress_decoder_2d(x, hparams, name=None):

  return decompress_decoder(x, hparams,
                            strides=(2, 2),
                            kernel=(hparams.kernel_size, hparams.kernel_size),
                            name=name)

Decoder that decompresses 2-D inputs by 2**num_compress_steps.

Args:
x: Tensor of shape [batch, compress_height, compress_width, channels].
hparams: HParams.
name: string, variable scope.

Returns:
Tensor of shape [batch, height, width, hparams.hidden_size].


def _NormalizedVolumeIdentifiers(
      self, volume_system, volume_identifiers, prefix='v'):

    normalized_volume_identifiers = []
    for volume_identifier in volume_identifiers:
      if isinstance(volume_identifier, int):
        volume_identifier = '{0:s}{1:d}'.format(prefix, volume_identifier)

      elif not volume_identifier.startswith(prefix):
        try:
          volume_identifier = int(volume_identifier, 10)
          volume_identifier = '{0:s}{1:d}'.format(prefix, volume_identifier)
        except (TypeError, ValueError):
          pass

      try:
        volume = volume_system.GetVolumeByIdentifier(volume_identifier)
      except KeyError:
        volume = None

      if not volume:
        raise errors.ScannerError(
            'Volume missing for identifier: {0:s}.'.format(volume_identifier))

      normalized_volume_identifiers.append(volume_identifier)

    return normalized_volume_identifiers

Normalizes volume identifiers.

Args:
volume_system (VolumeSystem): volume system.
volume_identifiers (list[int|str]): allowed volume identifiers, formatted
as an integer or string with prefix.
prefix (Optional[str]): volume identifier prefix.

Returns:
list[str]: volume identifiers with prefix.

Raises:
ScannerError: if the volume identifier is not supported or no volume
could be found that corresponds with the identifier.


def get_device_name():
    return context().device_name

Get the device name for the current thread.

Returns:
The device name for the current thread.


def extension_to_message(extension: message.Message, message_cls: Type[_T]) -> _T:
    msg = message_cls()
    add_extension_to_message(extension, msg)
    return msg

Serializes a provided FHIR extension into a message of type message_cls.

This function is a convenience wrapper around add_extension_to_message.

Args:
extension: The FHIR extension to serialize.
message_cls: The type of protobuf message to serialize extension to.

Returns:
A message of type message_cls.


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()

    event = event_values.get('event', None)
    if event:
      event_values['event_map'] = self.EVENT_NAMES.get(event, 'Unknown')

    category = event_values.get('cat', None)
    if category:
      event_values['category_map'] = self.CATEGORY_NAMES.get(
          category, 'Unknown')

    action = event_values.get('action0', None)
    if action:
      event_values['action0_map'] = self.ACTION_0_NAMES.get(action, 'Unknown')

    action = event_values.get('action1', None)
    if action:
      event_values['action1_map'] = self.ACTION_1_2_NAMES.get(
          action, 'Unknown')

    action = event_values.get('action2', None)
    if action:
      event_values['action2_map'] = self.ACTION_1_2_NAMES.get(
          action, 'Unknown')

    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 convert_dict_to_compatible_tensor(values, targets):
    result = {}
    for (key, value) in sorted(values.items()):
        result[key] = _convert_to_compatible_tensor(value, targets[key], error_prefix=("Can't convert %r" % key))
    return result

Converts dict `values` in tensors that are compatible with `targets`.

Args:
values: A dict to objects to convert with same keys as `targets`.
targets: A dict returned by `parse_tensor_info_map`.

Returns:
A map with the same keys as `values` but values converted into
Tensor/SparseTensors that can be fed into `protomap`.

Raises:
TypeError: If it fails to convert.


def extract_report_spec(service, label_is_supported=label_descriptor.KnownLabels.is_supported, metric_is_supported=metric_descriptor.KnownMetrics.is_supported):
    resource_descs = service.monitoredResources
    labels_dict = {}
    logs = set()
    if service.logging:
        logs = _add_logging_destinations(service.logging.producerDestinations, resource_descs, service.logs, labels_dict, label_is_supported)
    metrics_dict = {}
    monitoring = service.monitoring
    if monitoring:
        for destinations in (monitoring.consumerDestinations, monitoring.producerDestinations):
            _add_monitoring_destinations(destinations, resource_descs, service.metrics, metrics_dict, metric_is_supported, labels_dict, label_is_supported)
    return (logs, metrics_dict.keys(), labels_dict.keys())

Obtains the used logs, metrics and labels from a service.

label_is_supported and metric_is_supported are filter functions used to
determine if label_descriptors or metric_descriptors found in the service
are supported.

Args:
service (:class:`endpoints_management.gen.servicecontrol_v1_messages.Service`):
a service instance
label_is_supported (:func): determines if a given label is supported
metric_is_supported (:func): determines if a given metric is supported

Return:
tuple: (
logs (set[string}), # the logs to report to
metrics (list[string]), # the metrics to use
labels (list[string]) # the labels to add
)


def remove_handler(self, name):
    index = None
    for (i, h) in enumerate(self.capture_handlers):
        if (h['name'] == name):
            index = i
    if (index is not None):
        self.capture_handlers[index]['logger'].close()
        del self.capture_handlers[index]

Remove a handler given a name

Note, if multiple handlers have the same name the last matching
instance in the handler list will be removed.

Args:
name:
The name of the handler to remove


def from_string(cls, string, format_=None, fps=None, **kwargs):
    fp = io.StringIO(string)
    return cls.from_file(fp, format_, fps=fps, **kwargs)

Load subtitle file from string.

See :meth:`SSAFile.load()` for full description.

Arguments:
string (str): Subtitle file in a string. Note that the string
must be Unicode (in Python 2).

Returns:
SSAFile

Example:
>>> text = '''
... 1
... 00:00:00,000 --> 00:00:05,000
... An example SubRip file.
... '''
>>> subs = SSAFile.from_string(text)


def get_block(self, parent, config='running_config'):

        try:
            parent = r'^%s$' % parent
            return self.node.section(parent, config=config)
        except TypeError:
            return None

Scans the config and returns a block of code

Args:
parent (str): The parent string to search the config for and
return the block
config (str): A text config string to be searched. Default
is to search the running-config of the Node.

Returns:
A string object that represents the block from the config.  If
the parent string is not found, then this method will
return None.


def _mark_func_graph_as_unsaveable(graph, learning_phase):
    if graph.building_function and is_placeholder(learning_phase):
        graph.mark_as_unsaveable('The keras learning phase placeholder was used inside a function. Exporting placeholders is not supported when saving out a SavedModel. Please call `tf.keras.backend.set_learning_phase(0)` in the function to set the learning phase to a constant value.')

Mark func graph as unsaveable due to use of symbolic keras learning phase.

Functions that capture the symbolic learning phase cannot be exported to
SavedModel. Mark the funcgraph as unsaveable, so that an error will be raised
if it is exported.

Args:
graph: Graph or FuncGraph object.
learning_phase: Learning phase placeholder or int defined in the graph.


def convert_bbox_yolo_to_pascal(boxes: torch.Tensor, image_size: tuple[int, int]) -> torch.Tensor:
    boxes = center_to_corners_format(boxes)
    height, width = image_size
    boxes = boxes * torch.tensor([[width, height, width, height]])
    return boxes

Convert bounding boxes from YOLO format (x_center, y_center, width, height) in range [0, 1]
to Pascal VOC format (x_min, y_min, x_max, y_max) in absolute coordinates.

Args:
boxes (torch.Tensor): Bounding boxes in YOLO format
image_size (Tuple[int, int]): Image size in format (height, width)

Returns:
torch.Tensor: Bounding boxes in Pascal VOC format (x_min, y_min, x_max, y_max)


def _ReadFormatDataTypeDefinition(self, definitions_registry, definition_values, definition_name, is_member=False):
    if is_member:
        error_message = 'data type not supported as member'
        raise errors.DefinitionReaderError(definition_name, error_message)
    definition_object = self._ReadLayoutDataTypeDefinition(definitions_registry, definition_values, data_types.FormatDefinition, definition_name, self._SUPPORTED_DEFINITION_VALUES_FORMAT)
    definition_object.metadata = definition_values.get('metadata', {})
    attributes = definition_values.get('attributes', None)
    if attributes:
        unsupported_attributes = set(attributes.keys()).difference(self._SUPPORTED_ATTRIBUTES_FORMAT)
        if unsupported_attributes:
            error_message = 'unsupported attributes: {0:s}'.format(', '.join(unsupported_attributes))
            raise errors.DefinitionReaderError(definition_name, error_message)
        byte_order = attributes.get('byte_order', definitions.BYTE_ORDER_NATIVE)
        if (byte_order not in definitions.BYTE_ORDERS):
            error_message = 'unsupported byte-order attribute: {0!s}'.format(byte_order)
            raise errors.DefinitionReaderError(definition_name, error_message)
        definition_object.byte_order = byte_order
    return definition_object

Reads a format data type definition.

Args:
definitions_registry (DataTypeDefinitionsRegistry): data type definitions
registry.
definition_values (dict[str, object]): definition values.
definition_name (str): name of the definition.
is_member (Optional[bool]): True if the data type definition is a member
data type definition.

Returns:
FormatDefinition: format definition.

Raises:
DefinitionReaderError: if the definitions values are missing or if
the format is incorrect.


def GetIndentLevel(line):

  indent = Match(r'^( *)\S', line)
  if indent:
    return len(indent.group(1))
  else:
    return 0

Return the number of leading spaces in line.

Args:
line: A string to check.

Returns:
An integer count of leading spaces, possibly zero.


def add_payload(self, key, val, append=False):

        self._request.add_payload(key, val, append)

Add a key value pair to payload for this request.

.. Note:: For ``_search`` you can pass a search argument. (e.g. _search?summary=1.1.1.1).

Args:
key (string): The payload key
val (string): The payload value
append (bool): Indicate whether the value should be appended


def to_dict(self):
    return dict(addr=self.addr, protocol=self.protocol, weight=self.weight, last_checked=self.last_checked)

convert detailed proxy info into a dict

Returns:
dict: A dict with four keys: ``addr``, ``protocol``,
``weight`` and ``last_checked``


def add_redistribution(self, protocol, route_map_name=None):

        protocols = ['bgp', 'rip', 'static', 'connected']
        if protocol not in protocols:
            raise ValueError('redistributed protocol must be'
                             'bgp, connected, rip or static')
        if route_map_name is None:
            cmd = 'redistribute {}'.format(protocol)
        else:
            cmd = 'redistribute {} route-map {}'.format(protocol,
                                                        route_map_name)
        return self.configure_ospf(cmd)

Adds a protocol redistribution to OSPF

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


def reorder(miz_file_path: typing.Union[str, Path],
                target_dir: typing.Union[str, Path],
                skip_options_file: bool,
                ):

        miz_file_path = Path(miz_file_path).absolute()
        if not miz_file_path.exists():
            raise FileNotFoundError(miz_file_path)
        if not miz_file_path.is_file():
            raise ValueError(f'not a file: {miz_file_path}')

        target_dir_path = Path(target_dir).absolute()
        if not target_dir_path.exists():
            target_dir_path.mkdir(parents=True)
        else:
            if not target_dir_path.is_dir():
                raise ValueError(f'not a directory: {target_dir_path}')

        LOGGER.debug('re-ordering miz file: %s', miz_file_path)
        LOGGER.debug('destination folder: %s', target_dir)
        LOGGER.debug('%s option file', "skipping" if skip_options_file else "including")

        if not target_dir_path.exists():
            LOGGER.debug('creating directory %s', target_dir_path)
            target_dir_path.mkdir(exist_ok=True)

        Miz._do_reorder(miz_file_path, skip_options_file, target_dir_path)

Re-orders a miz file into a folder (flattened)

Args:
miz_file_path: source miz file
target_dir: folder to flatten the content into
skip_options_file: do not re-order option file


def get_interpolated_value(self, energy, integrated=False):

        inter = {}
        for spin in self.cohp:
            if not integrated:
                inter[spin] = get_linear_interpolated_value(self.energies,
                                                            self.cohp[spin],
                                                            energy)
            elif self.icohp is not None:
                inter[spin] = get_linear_interpolated_value(self.energies,
                                                            self.icohp[spin],
                                                            energy)
            else:
                raise ValueError("ICOHP is empty.")
        return inter

Returns the COHP for a particular energy.

Args:
energy: Energy to return the COHP value for.


def report_file(config, auth, report_id=None, name=None, timeout=60, chunksize=DBM_CHUNKSIZE):
    storage_path = report_fetch(config, auth, report_id, name, timeout)
    if storage_path == False:
        return (None, None)
    elif storage_path == True:
        return ('report_running.csv', None)
    else:
        filename = RE_FILENAME.search(storage_path).groups(0)[0]
        if chunksize:
            if config.verbose:
                print('REPORT FILE STREAM:', storage_path)
            return (filename, response_utf8_stream(urlopen(storage_path), chunksize))
        else:
            if config.verbose:
                print('REPORT FILE SINGLE:', storage_path)
            return (filename, urlopen(storage_path).read().decode('UTF-8'))

Retrieves most recent DBM file by name or ID, if in progress, waits for it to complete.

Timeout is in minutes ( retries will happen at 1 minute interval, default
total time is 60 minutes )
If chunksize is set to None then the whole file is downloaded at once.

Args:
* auth: (string) Either user or service.
* report_id: (int) ID of DCm report to fetch ( either or name ).
* name: (string) Name of report to fetch ( either or report_id ).
* timeout: (int) Minutes to wait for in progress report before giving up.
* chunksize: (int) number of bytes to download at a time, for memory
constrained systems.

Returns:
* (filename, iterator) if file exists and is ready to download in chunks.
* (filename, file) if file exists and chunking is off.
* ('report_running.csv', None) if report is in progress.
* (None, None) if file does not exist.


def market_exact(self, session, start_time: str, end_time: str) -> Session:

        if session not in self.exch: return SessNA
        ss = self.exch[session]

        same_day = ss[0] < ss[-1]

        if not start_time: s_time = ss[0]
        else:
            s_time = param.to_hour(start_time)
            if same_day: s_time = max(s_time, ss[0])

        if not end_time: e_time = ss[-1]
        else:
            e_time = param.to_hour(end_time)
            if same_day: e_time = min(e_time, ss[-1])

        if same_day and (s_time > e_time): return SessNA
        return Session(start_time=s_time, end_time=e_time)

Explicitly specify start time and end time

Args:
session: predefined session
start_time: start time in terms of HHMM string
end_time: end time in terms of HHMM string

Returns:
Session of start_time and end_time


def _GetFileByPath(self, key_path_upper):
    (key_path_prefix, registry_file) = self._GetCachedFileByPath(key_path_upper)
    if (not registry_file):
        for mapping in self._GetFileMappingsByPath(key_path_upper):
            try:
                registry_file = self._OpenFile(mapping.windows_path)
            except IOError:
                registry_file = None
            if (not registry_file):
                continue
            if (not key_path_prefix):
                key_path_prefix = mapping.key_path_prefix
            self.MapFile(key_path_prefix, registry_file)
            key_path_prefix = key_path_prefix.upper()
            break
    return (key_path_prefix, registry_file)

Retrieves a Windows Registry file for a specific path.

Args:
key_path_upper (str): Windows Registry key path, in upper case with
a resolved root key alias.

Returns:
tuple: consists:

str: upper case key path prefix
WinRegistryFile: corresponding Windows Registry file or None if not
available.


def get_test_dataloader(self, test_dataset: Dataset) -> DataLoader:
    return self._get_dataloader(dataset=test_dataset, description='test', batch_size=self.args.eval_batch_size, sampler_fn=self._get_eval_sampler)

Returns the test [`~torch.utils.data.DataLoader`].

Subclass and override this method if you want to inject some custom behavior.

Args:
test_dataset (`torch.utils.data.Dataset`, *optional*):
The test dataset to use. If it is a [`~datasets.Dataset`], columns not accepted by the
`model.forward()` method are automatically removed. It must implement `__len__`.


def create_nanopubs_fh(output_fn: str):
    (json_flag, jsonl_flag, yaml_flag) = (False, False, False)
    if output_fn:
        if re.search('gz$', output_fn):
            out_fh = gzip.open(output_fn, 'wt')
        else:
            out_fh = click.open_file(output_fn, mode='wt')
        if re.search('ya?ml', output_fn):
            yaml_flag = True
        elif (('jsonl' in output_fn) or ('-' == output_fn)):
            jsonl_flag = True
        elif ('json' in output_fn):
            json_flag = True
    else:
        out_fh = sys.stdout
    return (out_fh, yaml_flag, jsonl_flag, json_flag)

Create Nanopubs output filehandle

\b
If output fn is '-' will write JSONlines to STDOUT
If output fn has *.gz, will written as a gzip file
If output fn has *.jsonl*, will written as a JSONLines file
IF output fn has *.json*, will be written as a JSON file
If output fn has *.yaml* or *.yml*,  will be written as a YAML file

Args:
output_fn: Name of output file

Returns:
(filehandle, yaml_flag, jsonl_flag, json_flag)


def add_info_field(self, field):

        if field in self.info_dict:
            msg = "New info field [{}] already exists.".format(field)
            raise KeyError(msg)

        if "=" in field:
            key, value = field.split("=")
            self.info_dict[key] = value
        else:
            self.info_dict[field] = field

        self._join_info_fields()

Adds new info field (flag or key=value pair).

Args:
field: String flag (e.g. "SOMATIC") or key-value ("NEW_DP=42")

Raises:
KeyError: if info field already exists


def AddEventData(self, event_data):

    self._RaiseIfNotWritable()

    event_data = self._PrepareAttributeContainer(event_data)

    identifier = event_data.GetIdentifier()
    lookup_key = identifier.CopyToString()
    self._event_data[lookup_key] = event_data

Adds event data.

Args:
event_data (EventData): event data.

Raises:
IOError: when the storage writer is closed.
OSError: when the storage writer is closed.


def getUrlMeta(self, url):
    return self.conn('GET', SkypeConnection.API_URL, params={'url': url}, auth=SkypeConnection.Auth.Authorize).json()

Retrieve various metadata associated with a URL, as seen by Skype.

Args:
url (str): address to ping for info

Returns:
dict: metadata for the website queried


def _apply_merge_op_and_or_mask(self, op_fn, inputs):
    output = None
    output_mask = None
    for x in inputs:
        mask = backend.get_keras_mask(x)
        if mask is not None:
            mask = ops.broadcast_to(ops.expand_dims(mask, -1), ops.shape(x))
        if output is None:
            output = x
            output_mask = mask
            continue
        if mask is not None:
            x = ops.where(mask, x, output)
        if output_mask is not None:
            output = ops.where(output_mask, output, x)
        if mask is not None and output_mask is not None:
            output_mask = ops.logical_or(output_mask, mask)
        else:
            output_mask = None
        output = op_fn(output, x)
    if output_mask is not None:
        output_mask = ops.any(output_mask, axis=-1, keepdims=False)
        backend.set_keras_mask(output, output_mask)
    return output

Merge a set of inputs by applying `op_fn` and ORing the masks.

We use this for `Minimum` and `Maximum` as it handles the fact that
there is no identity element. If applicable, the mask obtained by ORing
all masks is set on the output.

Args:
op_fn: binary operation to apply to tensor pair.
inputs: array of tensors to apply operation on.


def get_dos(self, partial_dos=False, npts_mu=10000, T=None):

        spin = self.data.spin if isinstance(self.data.spin,int) else 1

        energies, densities, vvdos, cdos = BL.BTPDOS(self.eband, self.vvband, npts=npts_mu)
        if T is not None:
            densities = BL.smoothen_DOS(energies, densities, T)

        tdos = Dos(self.efermi / units.eV, energies / units.eV,
                   {Spin(spin): densities})

        if partial_dos:
            tdos = self.get_partial_doses(tdos=tdos, npts_mu=npts_mu, T=T)

        return tdos

Return a Dos object interpolating bands

Args:
partial_dos: if True, projections will be interpolated as well
and partial doses will be return. Projections must be available
in the loader.
npts_mu: number of energy points of the Dos
T: parameter used to smooth the Dos


def get_pattern_link_topattern(self, patternnumber):

        _checkPatternNumber(patternnumber)

        address = _calculateRegisterAddress('linkpattern', patternnumber)
        return self.read_register(address)

Get the 'linked pattern' value for a given pattern.

Args:
patternnumber (integer): From 0-7

Returns:
The 'linked pattern' value (int).


def cache_file(symbol, func, has_date, root, date_type='date'):

    cur_mod = sys.modules[func.__module__]
    data_tz = getattr(cur_mod, 'DATA_TZ') if hasattr(cur_mod, 'DATA_TZ') else 'UTC'
    cur_dt = utils.cur_time(typ=date_type, tz=data_tz, trading=False)

    if has_date:
        if hasattr(cur_mod, 'FILE_WITH_DATE'):
            file_fmt = getattr(cur_mod, 'FILE_WITH_DATE')
        else:
            file_fmt = '{root}/{typ}/{symbol}/{cur_dt}.parq'
    else:
        if hasattr(cur_mod, 'FILE_NO_DATE'):
            file_fmt = getattr(cur_mod, 'FILE_NO_DATE')
        else:
            file_fmt = '{root}/{typ}/{symbol}.parq'

    return data_file(
        file_fmt=file_fmt, root=root, cur_dt=cur_dt, typ=func.__name__, symbol=symbol
    )

Data file

Args:
symbol: symbol
func: use function to categorize data
has_date: contains date in data file
root: root path
date_type: parameters pass to utils.cur_time, [date, time, time_path, ...]

Returns:
str: date file


def request_and_check(self, url, method='get', expected_content_type=None, **kwargs):
    assert (method in ['get', 'post'])
    result = self.driver.request(method, url, **kwargs)
    if (result.status_code != requests.codes.ok):
        raise RuntimeError(('Error requesting %r, status = %d' % (url, result.status_code)))
    if (expected_content_type is not None):
        content_type = result.headers.get('content-type', '')
        if (not re.match(expected_content_type, content_type)):
            raise RuntimeError(('Error requesting %r, content type %r does not match %r' % (url, content_type, expected_content_type)))
    return result

Performs a request, and checks that the status is OK, and that the
content-type matches expectations.

Args:
url: URL to request
method: either 'get' or 'post'
expected_content_type: prefix to match response content-type against
**kwargs: passed to the request method directly.

Raises:
RuntimeError if status_code does not match.


def initialize_logger(debug):
    level = (logging.DEBUG if debug else logging.INFO)
    logger = logging.getLogger('cucco')
    logger.setLevel(level)
    formatter = logging.Formatter('%(asctime)s %(levelname).1s %(message)s')
    console_handler = logging.StreamHandler()
    console_handler.setLevel(level)
    console_handler.setFormatter(formatter)
    logger.addHandler(console_handler)
    return logger

Set up logger to be used by the library.

Args:
debug: Wheter to use debug level or not.

Returns:
A logger ready to be used.


def process_configs(file_lookup, app_config_format, pipeline_config):
    app_configs = collections.defaultdict(dict)
    for env in ENVS:
        file_json = app_config_format.format(env=env)
        try:
            env_config = file_lookup.json(filename=file_json)
            app_configs[env] = apply_region_configs(env_config)
        except FileNotFoundError:
            LOG.critical('Application configuration not available for %s.', env)
            continue
    try:
        app_configs['pipeline'] = file_lookup.json(filename=pipeline_config)
    except FileNotFoundError:
        LOG.warning('Unable to process pipeline.json. Using defaults.')
        app_configs['pipeline'] = {'env': ['stage', 'prod']}
    LOG.debug('Application configs:\n%s', app_configs)
    return app_configs

Processes the configs from lookup sources.

Args:
file_lookup (FileLookup): Source to look for file/config
app_config_format (str): The format for application config files.
pipeline_config (str): Name/path of the pipeline config

Returns:
dict: Retreived application config


def lit(literal: Sequence[Input], *literals: Sequence[Sequence[Input]]) -> Parser:
    if (len(literals) > 0):
        return AlternativeParser(options.handle_literal(literal), *map(options.handle_literal, literals))
    else:
        return options.handle_literal(literal)

Match a literal sequence.

In the `TextParsers`` context, this matches the literal string
provided. In the ``GeneralParsers`` context, this matches a sequence of
input.

If multiple literals are provided, they are treated as alternatives. e.g.
``lit('+', '-')`` is the same as ``lit('+') | lit('-')``.

Args:
literal: A literal to match
*literals: Alternative literals to match

Returns:
A ``LiteralParser`` in the ``GeneralContext``, a ``LiteralStringParser``
in the ``TextParsers`` context, and an ``AlternativeParser`` if multiple
arguments are provided.


def list(name, default=None, allow_none=False, fallback=None, separator=','):
    value = read(name, default, allow_none, fallback=fallback)
    if isinstance(value, builtins.list):
        return value
    elif isinstance(value, builtins.str):
        return _str_to_list(value, separator)
    elif ((value is None) and allow_none):
        return None
    else:
        return [builtins.str(value)]

Get a list of strings or the default.

The individual list elements are whitespace-stripped.

Args:
name: The environment variable name
default: The default value to use if no environment variable is found
allow_none: If the return value can be `None` (i.e. optional)
separator: The list item separator character or pattern


def validate_and_copy_one_submission(self, submission_path):
    if os.path.exists(self.download_dir):
        shutil.rmtree(self.download_dir)
    os.makedirs(self.download_dir)
    if os.path.exists(self.validate_dir):
        shutil.rmtree(self.validate_dir)
    os.makedirs(self.validate_dir)
    logging.info(((('\n' + ('
    local_path = self.copy_submission_locally(submission_path)
    metadata = self.base_validator.validate_submission(local_path)
    if (not metadata):
        logging.error('Submission "%s" is INVALID', submission_path)
        self.stats.add_failure()
        return
    submission_type = metadata['type']
    container_name = metadata['container_gpu']
    logging.info('Submission "%s" is VALID', submission_path)
    self.list_of_containers.add(container_name)
    self.stats.add_success(submission_type)
    if self.do_copy:
        submission_id = '{0:04}'.format(self.cur_submission_idx)
        self.cur_submission_idx += 1
        self.copy_submission_to_destination(submission_path, TYPE_TO_DIR[submission_type], submission_id)
        self.id_to_path_mapping[submission_id] = submission_path

Validates one submission and copies it to target directory.

Args:
submission_path: path in Google Cloud Storage of the submission file


def gunzip_file(gz_path, new_path):
    if tf.gfile.Exists(new_path):
        tf.logging.info(('File %s already exists, skipping unpacking' % new_path))
        return
    tf.logging.info(('Unpacking %s to %s' % (gz_path, new_path)))
    mode = (stat.S_IRWXU or stat.S_IXGRP or stat.S_IRGRP or stat.S_IROTH)
    os.chmod(os.path.dirname(new_path), mode)
    with gzip.open(gz_path, 'rb') as gz_file:
        with tf.gfile.GFile(new_path, mode='wb') as new_file:
            for line in gz_file:
                new_file.write(line)

Unzips from gz_path into new_path.

Args:
gz_path: path to the zipped file.
new_path: path to where the file will be unzipped.


def assert_equal_graph_def_v1(actual: graph_pb2.GraphDef, expected: graph_pb2.GraphDef, checkpoint_v2: bool=False, hash_table_shared_name: bool=False) -> None:
    assert_equal_graph_def(actual, expected, checkpoint_v2, hash_table_shared_name)

Asserts that two `GraphDef`s are (mostly) the same.

Compares two `GraphDef` protos for equality, ignoring versions and ordering of
nodes, attrs, and control inputs.  Node names are used to match up nodes
between the graphs, so the naming of nodes must be consistent.

Args:
actual: The `GraphDef` we have.
expected: The `GraphDef` we expected.
checkpoint_v2: boolean determining whether to ignore randomized attribute
values that appear in V2 checkpoints.
hash_table_shared_name: boolean determining whether to ignore randomized
shared_names that appear in HashTableV2 op defs.

Raises:
AssertionError: If the `GraphDef`s do not match.
TypeError: If either argument is not a `GraphDef`.


def __init__(self, timestamp=None):

    super(OLEAutomationDate, self).__init__()
    self._precision = definitions.PRECISION_1_MICROSECOND
    self._timestamp = timestamp

Initializes an OLE Automation date.

Args:
timestamp (Optional[float]): OLE Automation date.


def coerce_to_pendulum(x: PotentialDatetimeType,
                       assume_local: bool = False) -> Optional[DateTime]:

    if not x:
        return None
    if isinstance(x, DateTime):
        return x
    tz = get_tz_local() if assume_local else get_tz_utc()
    if isinstance(x, datetime.datetime):
        return pendulum.instance(x, tz=tz)
    elif isinstance(x, datetime.date):

        midnight = DateTime.min.time()
        dt = DateTime.combine(x, midnight)
        return pendulum.instance(dt, tz=tz)
    elif isinstance(x, str):
        return pendulum.parse(x, tz=tz)
    else:
        raise ValueError("Don't know how to convert to DateTime: "
                         "{!r}".format(x))

Converts something to a :class:`pendulum.DateTime`.

Args:
x: something that may be coercible to a datetime
assume_local: if ``True``, assume local timezone; if ``False``, assume
UTC

Returns:
a :class:`pendulum.DateTime`, or ``None``.

Raises:
pendulum.parsing.exceptions.ParserError: if a string fails to parse
ValueError: if no conversion possible


def _RemoveAllFlagAppearances(self, name):

    flag_dict = self.FlagDict()
    if name not in flag_dict:
      raise exceptions.UnrecognizedFlagError(name)
    flag = flag_dict[name]
    names_to_remove = {name}
    names_to_remove.add(flag.name)
    if flag.short_name:
      names_to_remove.add(flag.short_name)
    for n in names_to_remove:
      self.__delattr__(n)

Removes flag with name for all appearances.

A flag can be registered with its long name and an optional short name.
This method removes both of them. This is different than __delattr__.

Args:
name: Either flag's long name or short name.

Raises:
UnrecognizedFlagError: When flag name is not found.


def get_link_or_none(pattern_name, request, view_kwargs=None):
    from is_core.patterns import reverse_pattern
    pattern = reverse_pattern(pattern_name)
    assert (pattern is not None), 'Invalid pattern name {}'.format(pattern_name)
    if pattern.has_permission('get', request, view_kwargs=view_kwargs):
        return pattern.get_url_string(request, view_kwargs=view_kwargs)
    else:
        return None

Helper that generate URL prom pattern name and kwargs and check if current request has permission to open the URL.
If not None is returned.

Args:
pattern_name (str): slug which is used for view registratin to pattern
request (django.http.request.HttpRequest): Django request object
view_kwargs (dict): list of kwargs necessary for URL generator

Returns:


def _get_jwt_for_audience(self, audience):

        token, expiry = self._cache.get(audience, (None, None))

        if token is None or expiry < _helpers.utcnow():
            token, expiry = self._make_jwt_for_audience(audience)
            self._cache[audience] = token, expiry

        return token

Get a JWT For a given audience.

If there is already an existing, non-expired token in the cache for
the audience, that token is used. Otherwise, a new token will be
created.

Args:
audience (str): The intended audience.

Returns:
bytes: The encoded JWT.


def outputs(self) -> Mapping[str, Mapping[int, str]]:
    common_outputs = self._tasks_to_common_outputs[self.task]
    return copy.deepcopy(common_outputs)

Mapping containing the axis definition of the output tensors to provide to the model

Returns:
For each output: its name associated to the axes symbolic name and the axis position within the tensor


def _get(self, obj):
    if (not hasattr(obj, '_property_values')):
        raise RuntimeError(("Cannot get a property value '%s' from a %s instance before HasProps.__init__" % (self.name, obj.__class__.__name__)))
    if (self.name not in obj._property_values):
        return self._get_default(obj)
    else:
        return obj._property_values[self.name]

Internal implementation of instance attribute access for the
``BasicPropertyDescriptor`` getter.

If the value has not been explicitly set by a user, return that
value. Otherwise, return the default.

Args:
obj (HasProps) : the instance to get a value of this property for

Returns:
object

Raises:
RuntimeError
If the |HasProps| instance has not yet been initialized, or if
this descriptor is on a class that is not a |HasProps|.


def add_error(self, error):
    self._count += 1
    self._record.add_error('expect@%s+%s' % (time.time(), self._count), error)

Record an error from expect APIs.

This method generates a position stamp for the expect. The stamp is
composed of a timestamp and the number of errors recorded so far.

Args:
error: Exception or signals.ExceptionRecord, the error to add.


def _assertOpOutputMatchesExpected(self, op, inp, expected, equality_test=None, rtol=0.001, atol=1e-05):
    with self.session() as session:
        with self.test_scope():
            pinp = array_ops.placeholder(dtypes.as_dtype(inp.dtype), inp.shape, name='a')
            output = op(pinp)
        result = session.run(output, {pinp: inp})
        if equality_test is None:
            self.assertEqual(output.dtype, expected.dtype)
            self.assertAllCloseAccordingToType(expected, result, rtol=rtol, atol=atol, bfloat16_rtol=0.03)
        else:
            equality_test(result, expected, rtol=rtol, atol=atol)

Verifies that 'op' produces 'expected' when fed input 'inp' .

Args:
op: operator to test
inp: numpy input array to use as input to 'op'.
expected: numpy array representing the expected output of 'op'.
equality_test: either None, or a function that tests two numpy arrays for
equality. If None, self.assertAllClose is used.
rtol: relative tolerance for equality test.
atol: absolute tolerance for equality test.


def animation(frame_function: types.FrameFunction) -> types.Animation:
    animation_ = core.Animation(frame_function)

    @functools.wraps(frame_function)
    def wrapper(*args, **kwargs):
        return animation_(*args, **kwargs)
    return wrapper

Turn a FrameFunction into an Animation.

Args:
frame_function: A function that returns a FrameGenerator.

Returns:
an Animation decorator function.


def GetAnalysisStatusUpdateCallback(self):
    if (self._mode == self.MODE_LINEAR):
        return self._PrintAnalysisStatusUpdateLinear
    if (self._mode == self.MODE_WINDOW):
        return self._PrintAnalysisStatusUpdateWindow
    return None

Retrieves the analysis status update callback function.

Returns:
function: status update callback function or None if not available.


def get(self):
    return self._quantile_tracker.get()

Calculates and returns the median (q = 0.5).

Returns:
float: The median of the values in the window setting specified in the
internal quantile tracker. Returns NaN if the window is empty.


def integer_key_convert(dictin, dropfailedkeys=False):

    return key_value_convert(dictin, keyfn=int, dropfailedkeys=dropfailedkeys)

Convert keys of dictionary to integers

Args:
dictin (DictUpperBound): Input dictionary
dropfailedkeys (bool): Whether to drop dictionary entries where key conversion fails. Defaults to False.

Returns:
Dict: Dictionary with keys converted to integers


def decrypt(self, message):

        message = json.loads(message)

        unencrypted_msg = []

        for line in message:

            enc_line = binascii.a2b_base64(line)

            unencrypted_line = rsa.decrypt(enc_line, self.private_key)

            unencrypted_msg.append(unencrypted_line)

        unencrypted_msg = "".join(unencrypted_msg)

        return unencrypted_msg

Decrypts a string using our own private key object.

Args:
message (string): The string of the message to decrypt.

Returns:
The unencrypted string.


def get_repo_data(saltenv='base'):
    repo_details = _get_repo_details(saltenv)
    if (repo_details.winrepo_age == (- 1)):
        log.debug('No winrepo.p cache file. Refresh pkg db now.')
        refresh_db(saltenv=saltenv)
    if ('winrepo.data' in __context__):
        log.trace('get_repo_data returning results from __context__')
        return __context__['winrepo.data']
    else:
        log.trace('get_repo_data called reading from disk')
    try:
        serial = salt.payload.Serial(__opts__)
        with salt.utils.files.fopen(repo_details.winrepo_file, 'rb') as repofile:
            try:
                repodata = salt.utils.data.decode((serial.loads(repofile.read()) or {}))
                __context__['winrepo.data'] = repodata
                return repodata
            except Exception as exc:
                log.exception(exc)
                return {}
    except IOError as exc:
        log.error('Not able to read repo file')
        log.exception(exc)
        return {}

Returns the existing package metadata db. Will create it, if it does not
exist, however will not refresh it.

Args:
saltenv (str): Salt environment. Default ``base``

Returns:
dict: A dict containing contents of metadata db.

CLI Example:

.. code-block:: bash

salt '*' pkg.get_repo_data


def evaluate_repeatedly(self, accuracy, num_steps, feed_vars=(), feed_data=None, summary_tag=None, evaluation_times=(- 1)):
    current_checkpoint = None
    try:
        for i in itertools.count(0):
            with self.session() as sess:
                current_checkpoint = self.load_new_checkpoint_when_available(sess, current_checkpoint)
                self._run_init_test_vars_op()
                accuracy_result = self.evaluate_model(accuracy, num_steps, summary_tag=summary_tag, print_every=0, feed_vars=feed_vars, feed_data=feed_data)
                if (not summary_tag):
                    print(('[%d] %s' % (sess.run(bookkeeper.global_step()), accuracy_result)))
                if ((i + 1) == evaluation_times):
                    return accuracy_result
    finally:
        print('Shutting down')
        sys.stdout.flush()
        self.stop_queues()

Runs the evaluation in a loop for `evaluation_times`.

On each iteration, `evaluate_model` is called with the supplied arguments.
This manages the queue threads itself.

Args:
accuracy: The metric that is being evaluated.
num_steps: The number of steps to run in the evaluator.
feed_vars: A list or tuple of the variables that will be fed.
feed_data: A generator that produces tuples of the same length as
feed_vars.
summary_tag: If provided, the final result of each evaluation will be
published to this tag.
evaluation_times: Run this loop for this many times or forever if it is
`-1`.

Returns:
The final evaluation result from `evaluate_model` if `evaluation_times`
ever ends.


def filter_aliases(alias_table):

    for alias in alias_table.sections():
        if alias_table.has_option(alias, 'command'):
            yield (alias.split()[0], remove_pos_arg_placeholders(alias_table.get(alias, 'command')))

Filter aliases that does not have a command field in the configuration file.

Args:
alias_table: The alias table.

Yield:
A tuple with [0] being the first word of the alias and
[1] being the command that the alias points to.


def reload(self, napps=None):

        client = NAppsClient(self._config)
        client.reload_napps(napps)

Reload a NApp or all NApps.

Args:
napps (list): NApp list to be reloaded.
Raises:
requests.HTTPError: When there's a server error.


def prepare_to_run_task(context, claim_task):

    current_task_info = {}
    context.claim_task = claim_task
    current_task_info['taskId'] = get_task_id(claim_task)
    current_task_info['runId'] = get_run_id(claim_task)
    log.info("Going to run taskId {taskId} runId {runId}!".format(
        **current_task_info
    ))
    context.write_json(
        os.path.join(context.config['work_dir'], 'current_task_info.json'),
        current_task_info, "Writing current task info to {path}..."
    )
    return current_task_info

Given a `claim_task` json dict, prepare the `context` and `work_dir`.

Set `context.claim_task`, and write a `work_dir/current_task_info.json`

Args:
context (scriptworker.context.Context): the scriptworker context.
claim_task (dict): the claim_task dict.

Returns:
dict: the contents of `current_task_info.json`


def __init__(self, channel):

    self.LeaseGrant = channel.unary_unary(
        '/etcdserverpb.Lease/LeaseGrant',
        request_serializer=rpc__pb2.LeaseGrantRequest.SerializeToString,
        response_deserializer=rpc__pb2.LeaseGrantResponse.FromString,
        )
    self.LeaseRevoke = channel.unary_unary(
        '/etcdserverpb.Lease/LeaseRevoke',
        request_serializer=rpc__pb2.LeaseRevokeRequest.SerializeToString,
        response_deserializer=rpc__pb2.LeaseRevokeResponse.FromString,
        )
    self.LeaseKeepAlive = channel.stream_stream(
        '/etcdserverpb.Lease/LeaseKeepAlive',
        request_serializer=rpc__pb2.LeaseKeepAliveRequest.SerializeToString,
        response_deserializer=rpc__pb2.LeaseKeepAliveResponse.FromString,
        )
    self.LeaseTimeToLive = channel.unary_unary(
        '/etcdserverpb.Lease/LeaseTimeToLive',
        request_serializer=rpc__pb2.LeaseTimeToLiveRequest.SerializeToString,
        response_deserializer=rpc__pb2.LeaseTimeToLiveResponse.FromString,
        )
    self.LeaseLeases = channel.unary_unary(
        '/etcdserverpb.Lease/LeaseLeases',
        request_serializer=rpc__pb2.LeaseLeasesRequest.SerializeToString,
        response_deserializer=rpc__pb2.LeaseLeasesResponse.FromString,
        )

Constructor.

Args:
channel: A grpc.Channel.


def apply_to_tensor(self, tensor, assign_tuple_sharding=False, use_sharding_op=False, unspecified_dims=None):
    if unspecified_dims:
        assert use_sharding_op and (not assign_tuple_sharding)
    proto = self._proto
    if isinstance(tensor, resource_variable_ops.BaseResourceVariable) and context.xla_sharding_for_resource_variables_enabled():
        if assign_tuple_sharding:
            proto = self._create_tuple_proto(num_outputs=1)
        tensor._set_xla_sharding(proto)
        return tensor
    if use_sharding_op:
        if assign_tuple_sharding:
            proto = self._create_tuple_proto(num_outputs=1)
            tensor = tf2xla.sharding(tensor, sharding=proto.SerializeToString())
        else:
            tensor = tf2xla.sharding(tensor, sharding=proto.SerializeToString(), unspecified_dims=unspecified_dims or [])
    elif assign_tuple_sharding or len(tensor.op.outputs) > 1:
        proto = self._get_or_create_tuple_proto(tensor.op)
        tuple_shardings = list(proto.tuple_shardings)
        tuple_shardings[tensor.value_index] = self._proto
        proto = xla_data_pb2.OpSharding(type=xla_data_pb2.OpSharding.TUPLE, tuple_shardings=tuple_shardings)
    tensor.op._set_attr('_XlaSharding', attr_value_pb2.AttrValue(s=proto.SerializeToString()))
    return tensor

Applies this Sharding attribute to `tensor`.

Args:
tensor: A tf.Tensor to split.
assign_tuple_sharding: If the sharding type should be a tuple.
use_sharding_op: Whether to create a sharding op on `tensor`.
unspecified_dims: An optional list of dimensions unspecified.

Returns:
The tensor with Sharding attribute.


def choose_branch(exclude=None):
    if (exclude is None):
        master = conf.get('git.master_branch', 'master')
        develop = conf.get('git.devel_branch', 'develop')
        exclude = {master, develop}
    branches = list((set(git.branches()) - exclude))
    for (i, branch_name) in enumerate(branches):
        shell.cprint('<90>[{}] <33>{}'.format((i + 1), branch_name))
    choice = 0
    while ((choice < 1) or (choice > len(branches))):
        prompt = 'Pick a base branch from the above [1-{}]'.format(len(branches))
        choice = click.prompt(prompt, value_proc=int)
        if (not (1 <= choice <= len(branches))):
            fmt = 'Invalid choice {}, you must pick a number between {} and {}'
            log.err(fmt.format(choice, 1, len(branches)))
    return branches[(choice - 1)]

Show the user a menu to pick a branch from the existing ones.

Args:
exclude (list[str]):
List of branch names to exclude from the menu. By default it will
exclude master and develop branches. To show all branches pass an
empty array here.

Returns:
str: The name of the branch chosen by the user. If the user inputs an
invalid choice, he will be asked again (and again) until he picks a
a valid branch.


def decode(self, ids, strip_extraneous=False):
    if strip_extraneous:
        ids = strip_ids(ids, list(range((self._num_reserved_ids or 0))))
    return unicode_to_native(tokenizer.decode(self._subtoken_ids_to_tokens(ids)))

Converts a sequence of subtoken ids to a native string.

Args:
ids: a list of integers in the range [0, vocab_size)
strip_extraneous: bool, whether to strip off extraneous tokens
(EOS and PAD).

Returns:
a native string


def read_html_file(data_dir, fileroot, encoding=None):

    fname = os.path.join(
        data_dir, RAW_HTML_DIRNAME, fileroot + RAW_HTML_EXT)
    encodings = (encoding,) if encoding else ('utf-8', 'iso-8859-1')
    for encoding in encodings:
        try:
            with io.open(fname, mode='rt', encoding=encoding) as f:
                raw_html = f.read()
            break
        except (UnicodeDecodeError, UnicodeError):
            raw_html = None

    return ftfy.fix_encoding(raw_html).strip()

Read the HTML file corresponding to identifier ``fileroot``
in the raw HTML directory below the root ``data_dir``.

Args:
data_dir (str)
fileroot (str)
encoding (str)

Returns:
str


def write_pattern(lines_per_file, no_data=False, return_filenames=False):
    temp_dir = tempfile.mkdtemp()
    all_data = []
    file_name = None
    start_index = 0
    for i in range(len(lines_per_file)):
        file_name, data = write_data(lines_per_file[i], no_data=no_data, directory=temp_dir, prefix='mytemp')
        if return_filenames:
            all_data.extend(zip([file_name] * len(data), data))
        else:
            all_data.extend(data)
        start_index += lines_per_file[i]
    assert file_name
    return (file_name[:file_name.rfind(os.path.sep)] + os.path.sep + 'mytemp*', all_data)

Writes a pattern of temporary files.

Args:
lines_per_file (List[int]): The number of lines to write per file.
no_data (bool): If :data:`True`, empty lines will be written, otherwise
each line will contain a concatenation of b'line' and the line number.
return_filenames (bool): If True, returned list will contain
(filename, data) pairs.

Returns:
Tuple[str, List[Union[str, (str, str)]]]: A tuple of the filename pattern
and a list of the utf-8 decoded written data or (filename, data) pairs.


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

        local_buffer = utils.BytearrayStream()

        if self._maximum_items:
            self._maximum_items.write(local_buffer, kmip_version=kmip_version)

        if self._offset_items:
            self._offset_items.write(local_buffer, kmip_version=kmip_version)

        if self._storage_status_mask:
            self._storage_status_mask.write(
                local_buffer,
                kmip_version=kmip_version
            )

        if self._object_group_member:
            self._object_group_member.write(
                local_buffer,
                kmip_version=kmip_version
            )

        if kmip_version < enums.KMIPVersion.KMIP_2_0:
            if self._attributes:
                for attribute in self.attributes:
                    attribute.write(
                        local_buffer,
                        kmip_version=kmip_version
                    )
        else:
            if self._attributes:

                template_attribute = objects.TemplateAttribute(
                    attributes=self.attributes
                )
                attributes = objects.convert_template_attribute_to_attributes(
                    template_attribute
                )
                attributes.write(local_buffer, kmip_version=kmip_version)
            else:
                raise exceptions.InvalidField(
                    "The Locate request payload is missing the attributes "
                    "list."
                )

        self.length = local_buffer.length()
        super(LocateRequestPayload, self).write(
            output_buffer,
            kmip_version=kmip_version
        )
        output_buffer.write(local_buffer.buffer)

Write the data encoding the Locate request payload to a buffer.

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


def line_count(fn):
    with open(fn) as f:
        for (i, l) in enumerate(f):
            pass
    return (i + 1)

Get line count of file

Args:
fn (str): Path to file

Return:
Number of lines in file (int)


async def update_read_timestamp(self, read_timestamp=None):
    if (read_timestamp is None):
        read_timestamp = (self.events[(- 1)].timestamp if self.events else datetime.datetime.now(datetime.timezone.utc))
    if (read_timestamp > self.latest_read_timestamp):
        logger.info('Setting {} latest_read_timestamp from {} to {}'.format(self.id_, self.latest_read_timestamp, read_timestamp))
        state = self._conversation.self_conversation_state
        state.self_read_state.latest_read_timestamp = parsers.to_timestamp(read_timestamp)
        try:
            (await self._client.update_watermark(hangouts_pb2.UpdateWatermarkRequest(request_header=self._client.get_request_header(), conversation_id=hangouts_pb2.ConversationId(id=self.id_), last_read_timestamp=parsers.to_timestamp(read_timestamp))))
        except exceptions.NetworkError as e:
            logger.warning('Failed to update read timestamp: {}'.format(e))
            raise

Update the timestamp of the latest event which has been read.

This method will avoid making an API request if it will have no effect.

Args:
read_timestamp (datetime.datetime): (optional) Timestamp to set.
Defaults to the timestamp of the newest event.

Raises:
.NetworkError: If the timestamp cannot be updated.


def check_origin(self, origin):

        from ..util import check_whitelist
        parsed_origin = urlparse(origin)
        origin_host = parsed_origin.netloc.lower()

        allowed_hosts = self.application.websocket_origins
        if settings.allowed_ws_origin():
            allowed_hosts = set(settings.allowed_ws_origin())

        allowed = check_whitelist(origin_host, allowed_hosts)
        if allowed:
            return True
        else:
            log.error("Refusing websocket connection from Origin '%s'; \
                      use --allow-websocket-origin=%s or set BOKEH_ALLOW_WS_ORIGIN=%s to permit this; currently we allow origins %r",
                      origin, origin_host, origin_host, allowed_hosts)
            return False

Implement a check_origin policy for Tornado to call.

The supplied origin will be compared to the Bokeh server whitelist. If the
origin is not allow, an error will be logged and ``False`` will be returned.

Args:
origin (str) :
The URL of the connection origin

Returns:
bool, True if the connection is allowed, False otherwise


def _check_initialized(self):
    baddies = self._find_uninitialized()
    if baddies:
        raise datastore_errors.BadValueError(('Entity has uninitialized properties: %s' % ', '.join(baddies)))

Internal helper to check for uninitialized properties.

Raises:
BadValueError if it finds any.


def get_average_voltage(self, min_voltage=None, max_voltage=None):
    pairs_in_range = self._select_in_voltage_range(min_voltage, max_voltage)
    if (len(pairs_in_range) == 0):
        return 0
    total_cap_in_range = sum([p.mAh for p in pairs_in_range])
    total_edens_in_range = sum([(p.mAh * p.voltage) for p in pairs_in_range])
    return (total_edens_in_range / total_cap_in_range)

Average voltage for path satisfying between a min and max voltage.

Args:
min_voltage (float): The minimum allowable voltage for a given
step.
max_voltage (float): The maximum allowable voltage allowable for a
given step.

Returns:
Average voltage in V across the insertion path (a subset of the
path can be chosen by the optional arguments)


def _merge_heads(self, x: torch.Tensor) -> torch.Tensor:
    batch_size_and_num_heads, seq_length, _ = x.shape
    batch_size = batch_size_and_num_heads
    x = x.view(batch_size, self.num_heads, seq_length, self.head_dim)
    x = x.permute(0, 2, 1, 3)
    return x.reshape(batch_size, seq_length, self.num_heads * self.head_dim)

Merge heads together over the last dimension

Args:
x (`torch.tensor`): [batch_size * num_heads, seq_length, head_dim]

Returns:
torch.tensor: [batch_size, seq_length, num_heads * head_dim]


def _prepare_replacement(self, replaced, key):
    repl = self.replacements[key]
    new_nodes = ast_util.copy_clean(repl, preserve_annos=self.preserved_annos)
    if isinstance(new_nodes, gast.AST):
        new_nodes = [new_nodes]
    return new_nodes

Prepares a replacement AST that's safe to swap in for a node.

Args:
replaced: ast.AST, the node being replaced
key: Hashable, the key of the replacement AST
Returns:
ast.AST, the replacement AST


def plot_spectra_overlapped(ss, title=None, setup=_default_setup):
    plt.figure()
    draw_spectra_overlapped(ss, title, setup)
    plt.show()

Plots one or more spectra in the same plot.

Args:
ss: list of Spectrum objects
title=None: window title
setup: PlotSpectrumSetup object


def attention_bias_proximal(length):

  r = tf.to_float(tf.range(length))
  diff = tf.expand_dims(r, 0) - tf.expand_dims(r, 1)
  return tf.expand_dims(tf.expand_dims(-tf.log1p(tf.abs(diff)), 0), 0)

Bias for self-attention to encourage attention to close positions.

Args:
length: an integer scalar.

Returns:
a Tensor with shape [1, 1, length, length]


def get_likelihood(self, uni_matrix):
    uni_dim = uni_matrix.shape[1]
    num_edge = len(self.edges)
    values = np.zeros([1, num_edge])
    new_uni_matrix = np.empty([uni_dim, uni_dim])
    for i in range(num_edge):
        edge = self.edges[i]
        (value, left_u, right_u) = edge.get_likelihood(uni_matrix)
        new_uni_matrix[(edge.L, edge.R)] = left_u
        new_uni_matrix[(edge.R, edge.L)] = right_u
        values[(0, i)] = np.log(value)
    return (np.sum(values), new_uni_matrix)

Compute likelihood of the tree given an U matrix.

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

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


class AriaGroupedExpertsMLP(nn.Module):

    def __init__(self, config: AriaTextConfig) -> None:
        super().__init__()
        self.config = config
        self.fc1 = AriaGroupedExpertsGemm(config.hidden_size, config.intermediate_size * 2, config.moe_num_experts)
        self.fc2 = AriaGroupedExpertsGemm(config.intermediate_size, config.hidden_size, config.moe_num_experts)

    def forward(self, permuted_tokens, tokens_per_expert):

        fc1_output = self.fc1(permuted_tokens, tokens_per_expert)
        projection, gate = torch.chunk(fc1_output, 2, dim=-1)
        fc1_output = nn.functional.silu(projection) * gate
        fc2_output = self.fc2(fc1_output, tokens_per_expert)
        return fc2_output

Grouped MLP module for Mixture of Experts.

Args:
config (`AriaTextConfig`):
Configuration object for the model.


def _step(time, output_ta_t, *states):
    current_input = tuple((ta.read(time) for ta in input_ta))
    current_input = tf.nest.pack_sequence_as(inputs, current_input)
    output, new_states = step_function(current_input, tuple(states) + tuple(constants))
    flat_state = tf.nest.flatten(states)
    flat_new_state = tf.nest.flatten(new_states)
    for state, new_state in zip(flat_state, flat_new_state):
        if isinstance(new_state, tf.Tensor):
            new_state.set_shape(state.shape)
    flat_output = tf.nest.flatten(output)
    ta_index_to_write = time if return_all_outputs else 0
    output_ta_t = tuple((ta.write(ta_index_to_write, out) for ta, out in zip(output_ta_t, flat_output)))
    new_states = tf.nest.pack_sequence_as(initial_states, flat_new_state)
    return (time + 1, output_ta_t) + tuple(new_states)

RNN step function.

Args:
time: Current timestep value.
output_ta_t: TensorArray.
*states: List of states.

Returns:
Tuple: `(time + 1,output_ta_t) + tuple(new_states)`


def get_instances(serials):

    results = []
    for s in serials:
        results.append(AndroidDevice(s))
    return results

Create AndroidDevice instances from a list of serials.

Args:
serials: A list of android device serials.

Returns:
A list of AndroidDevice objects.


def invert_apply(self, pts: torch.Tensor) -> torch.Tensor:
    pts = pts - self._trans
    return self._rots.invert_apply(pts)

Applies the inverse of the transformation to a coordinate tensor.

Args:
pts: A [*, 3] coordinate tensor
Returns:
The transformed points.


def time_to_jump(self):
    k_tot = (rate_prefactor * np.sum(self.p))
    return ((- (1.0 / k_tot)) * math.log(random.random()))

The timestep until the next jump.

Args:
None

Returns:
(Float): The timestep until the next jump.


def get_type_name_in_language(cls, type_name, sub_type, language):
    if (language in cls.type_methods_cache):
        m = cls.type_methods_cache[language]
        if (not m):
            return type_name
        return m(type_name)
    (found, method) = load_language_plugins(language, 'get_type_name')
    if found:
        cls.type_methods_cache[language] = method
        if method:
            return method(type_name, sub_type)
        else:
            return type_name
    module = importlib.import_module(('.lang.%s' % language), package='monolithe.generators')
    if (not hasattr(module, 'get_type_name')):
        cls.type_methods_cache[language] = None
        return type_name
    method = getattr(module, 'get_type_name')
    cls.type_methods_cache[language] = method
    return method(type_name, sub_type)

Get the type for the given language

Args:
type_name (str): the type to convert
language (str): the language to use

Returns:
a type name in the given language

Example:
get_type_name_in_language("Varchar", "python")
>>> str


def get_handler(progname, fmt=None, datefmt=None, project_id=None, credentials=None, debug_thread_worker=False, **_):
    builder = CloudLoggingHandlerBuilder(progname, fmt=fmt, datefmt=datefmt, project_id=project_id, credentials=credentials, debug_thread_worker=debug_thread_worker)
    return builder.get_handler()

Helper function to create a Stackdriver handler.

See `ulogger.stackdriver.CloudLoggingHandlerBuilder` for arguments
and supported keyword arguments.

Returns:
(obj): Instance of `google.cloud.logging.handlers.
CloudLoggingHandler`


def create_temp_parfile(self):
    output_dir = os.path.dirname(self.output_filename)
    return tempfile.NamedTemporaryFile(dir=output_dir, delete=False)

Create the first part of a parfile.

Returns:
A file-like object with a 'name' attribute


def dot_distance(t1, t2, name=None):

  with tf.name_scope(name, 'dot_distance', [t1, t2]) as scope:
    return -dot_product(t1, t2, name=scope)

dot "distance" between t1 and t2.

Args:
t1: A tensor.
t2: A tensor that is the same size as t1.
name: Optional name for this op.
Returns:
The dot distance between t1 and t2.


def diff_prof(step):

    rbot, rtop = misc.get_rbounds(step)
    rad = step.rprof['r'].values + rbot
    tprof = step.rprof['Tmean'].values
    diff = (tprof[:-1] - tprof[1:]) / (rad[1:] - rad[:-1])

    diff = np.insert(diff, 0, (1 - tprof[0]) / (rad[0] - rbot))

    diff = np.append(diff, tprof[-1] / (rtop - rad[-1]))

    return diff, np.append(rad, rtop)

Diffusion.

Args:
step (:class:`~stagpy.stagyydata._Step`): a step of a StagyyData
instance.
Returns:
tuple of :class:`numpy.array`: the diffusion and the radial position
at which it is evaluated.


def add_vcf_info(keyword, variant_line=None, variant_dict=None, annotation=None):

    logger = logging.getLogger(__name__)

    if annotation:
        new_info = '{0}={1}'.format(keyword, annotation)
    else:
        new_info = keyword

    logger.debug("Adding new variant information {0}".format(new_info))

    fixed_variant = None

    if variant_line:
        logger.debug("Adding information to a variant line")
        splitted_variant = variant_line.rstrip('\n').split('\t')
        logger.debug("Adding information to splitted variant line")
        old_info = splitted_variant[7]
        if old_info == '.':
            splitted_variant[7] = new_info
        else:
            splitted_variant[7] = "{0};{1}".format(splitted_variant[7], new_info)

        fixed_variant = '\t'.join(splitted_variant)

    elif variant_dict:
        logger.debug("Adding information to a variant dict")
        old_info = variant_dict['INFO']
        if old_info == '.':
            variant_dict['INFO'] = new_info
        else:
            variant_dict['INFO'] = "{0};{1}".format(old_info, new_info)
        fixed_variant = variant_dict

    return fixed_variant

Add information to the info field of a vcf variant line.

Arguments:
variant_line (str): A vcf formatted variant line
keyword (str): The info field key
annotation (str): If the annotation is a key, value pair
this is the string that represents the value

Returns:
fixed_variant : str if variant line, or dict if variant_dict


def run(self, host: str = '0.0.0.0', port: int = 8080):

        self._loop.run_until_complete(self._configure_plugins())
        web.run_app(self._app, host=host, port=port)

Start sirbot

Configure sirbot and start the aiohttp.web.Application

Args:
host (str): host
port (int): port


def do_phonefy(self, query, **kwargs):

        results = []

        test = self.check_phonefy(query, kwargs)

        if test:
            r = {
                "type": "i3visio.phone",
                "value": self.platformName + " - " + query,
                "attributes": []
            }

            try:
                aux = {
                    "type": "i3visio.uri",
                    "value": self.createURL(query, mode="phonefy"),
                    "attributes": []
                }
                r["attributes"].append(aux)
            except:
                pass

            aux = {
                "type": "i3visio.platform",
                "value": self.platformName,
                "attributes": []
            }
            r["attributes"].append(aux)

            r["attributes"] += self.process_phonefy(test)
            results.append(r)

        return results

Verifying a phonefy query in this platform.

This might be redefined in any class inheriting from Platform.

Args:
-----
query: The element to be searched.

Return:
-------
A list of elements to be appended.


def report_clean(rows):
    print('DCM REPORT CLEAN')
    first = True
    last = False
    for row in rows:
        if row and row[0] == 'Report Fields':
            break
    for row in rows:
        if 'No data returned by the reporting service.' in row:
            break
        if not row or row[0] == 'Grand Total:':
            break
        if first:
            try:
                date_column = row.index('Date')
                row[date_column] = 'Report_Day'
            except ValueError:
                pass
            row = [column_header_sanitize(cell) for cell in row]
        row = ['' if cell.strip() in ('(not set)', '-') else cell for cell in row]
        yield row
        first = False

Helper to fix DCM report issues for BigQuery and ensure schema compliance.

Memory efficiently cleans each row by fixing:
* Strips header and footer to preserve only data rows.
* Changes 'Date' to 'Report_Day' to avoid using reserved name in BigQuery.
* removes '-' as columns
* Changes data format to match data studio if datastusio=True.

Usage example:

```
filename, report = report_file(...)
rows = report_to_rows(report)
rows = report_clean(rows)
```

Args:
* rows: (iterator) Rows to clean.

Returns:
* Iterator of cleaned rows.


def _make_fake_dataset_fn(initial_delay_us, remainder_delay_us):

    def fake_dataset_fn(unused):

        del unused

        def make_dataset(time_us, num_elements):
            dataset = dataset_ops.Dataset.range(num_elements)
            if time_us > 0:
                dataset = dataset.apply(testing.sleep(time_us))
            return dataset
        if not initial_delay_us:
            return make_dataset(remainder_delay_us, 100)
        return make_dataset(initial_delay_us, 0).concatenate(make_dataset(remainder_delay_us, 100))
    return fake_dataset_fn

Returns a dataset that emulates a remote storage data source.

Returns a dataset factory which creates a dataset with 100 elements that
emulates the performance characteristic of a file-based dataset stored in a
remote storage. In particular, the first element will take an order of
magnitude longer to produce than the remaining elements (100ms vs. 1ms).

Args:
initial_delay_us: How long to wait before producing the first element.
remainder_delay_us: How long to wait before producing subsequent elements.


def __init__(self, model: PreTrainedModel, max_batch_size: int=1, max_cache_len: int=4096):
    super().__init__()
    if not hasattr(model.config, 'use_cache') or model.config.use_cache is False:
        raise ValueError('The model must have caching enabled to be performant.')
    if hasattr(model.config, 'layer_types') and getattr(model.config, 'sliding_window', None) is not None:
        self.model = TorchExportableModuleWithHybridCache(model, max_batch_size, max_cache_len)
    else:
        logging.info('Using `StaticCache` for export as `layer_types` is not specified or `sliding_window` is `null` in the config.')
        self.model = TorchExportableModuleWithStaticCache(model)

Initializes the exportable module with `HybridCache`.

Args:
model (`PreTrainedModel`): The pretrained model to wrap.
max_batch_size (int): Maximum batch size for the cache.
max_cache_len (int): Maximum sequence length for the cache.

Raises:
ValueError: If the model is configured with a unsupported cache implementation.


def highlight_html(code: str) -> str:
    theme = resource_utils.resource_import('static/highlight.css', module='etils.ecolab')
    html_str = '\n  {theme}\n  <script src="
    html_str = epy.dedent(html_str)
    html_str = html_str.format(theme=theme, code=html.escape(code))
    return html_str

Add Python syntax highlighting to a Python code string.

Usage:

Example:

```python
@dataclasses.dataclass
class A:
x: int

def _repr_html_(self) -> str:
from etils import ecolab  # Lazy-import ecolab

return ecolab.highlight_html(repr(self))

```

Args:
code: The string to wrap

Returns:
The HTML string representation


def nPr(n, r):

    f = math.factorial
    return int(f(n) / f(n-r))

Calculates nPr.

Args:
n (int): total number of items.
r (int): items to permute

Returns:
nPr.


def unpause(self, container):

        url = self._url('/containers/{0}/unpause', container)
        res = self._post(url)
        self._raise_for_status(res)

Unpause all processes within a container.

Args:
container (str): The container to unpause


def attributes(self, main_type, sub_type, unique_id, owner=None, params=None):

        params = params or {}

        if owner:
            params['owner'] = owner

        if not sub_type:
            url = '/v2/{}/{}/attributes'.format(main_type, unique_id)
        else:
            url = '/v2/{}/{}/{}/attributes'.format(main_type, sub_type, unique_id)

        for a in self._iterate(url, params, 'attribute'):
            yield a

Args:
owner:
main_type:
sub_type:
unique_id:
params:

Return:


def forward(self, input_points: Optional[Tuple[torch.Tensor, torch.Tensor]], input_labels: Optional[torch.Tensor], input_boxes: Optional[torch.Tensor], input_masks: Optional[torch.Tensor]) -> Tuple[torch.Tensor, torch.Tensor]:
    sparse_embeddings = None
    batch_size = 1
    target_device = self.shared_embedding.positional_embedding.device
    if input_points is not None:
        batch_size, point_batch_size = input_points.shape[:2]
        if input_labels is None:
            raise ValueError('If points are provided, labels must also be provided.')
        point_embeddings = self._embed_points(input_points, input_labels, pad=input_boxes is None)
        sparse_embeddings = point_embeddings
    if input_boxes is not None:
        batch_size = input_boxes.shape[0]
        box_embeddings = self._embed_boxes(input_boxes)
        if sparse_embeddings is None:
            sparse_embeddings = box_embeddings
        else:
            sparse_embeddings = torch.cat([sparse_embeddings, box_embeddings], dim=2)
    if input_masks is not None:
        dense_embeddings = self.mask_embed(input_masks)
    else:
        dense_embeddings = self.no_mask_embed.weight.reshape(1, -1, 1, 1).expand(batch_size, -1, self.image_embedding_size[0], self.image_embedding_size[1])
    if sparse_embeddings is None:
        sparse_embeddings = torch.zeros((batch_size, 1, 1, self.hidden_size), device=target_device)
    return (sparse_embeddings, dense_embeddings)

Embeds different types of prompts, returning both sparse and dense embeddings.

Args:
points (`torch.Tensor`, *optional*):
point coordinates and labels to embed.
boxes (`torch.Tensor`, *optional*):
boxes to embed
masks (`torch.Tensor`, *optional*):
masks to embed


def name_based_save(mesh: layout_lib.Mesh, checkpoint_prefix: Union[str, tensor_lib.Tensor], name_tensor_dict: Dict[str, Union[tensor_lib.Tensor, tf_variables.Variable]]):
    if not context.executing_eagerly():
        raise ValueError('name based save must run eagerly.')
    ordered_name_tensor_dict = name_tensor_dict
    if not isinstance(name_tensor_dict, collections.OrderedDict):
        ordered_name_tensor_dict = collections.OrderedDict(name_tensor_dict)
    checkpoint_prefix = api.pack([checkpoint_prefix] * mesh.num_local_devices(), layout_lib.Layout.replicated(mesh.host_mesh(), rank=0))
    tensor_names = api.pack([list(ordered_name_tensor_dict.keys())] * mesh.num_local_devices(), layout_lib.Layout.replicated(mesh.host_mesh(), rank=1))
    sharded_save(mesh, file_prefix=checkpoint_prefix, tensor_names=tensor_names, shape_and_slices=[''] * len(ordered_name_tensor_dict), tensors=list(ordered_name_tensor_dict.values()))

Saves name based Tensor into a Checkpoint.

The function prepares the input dictionary to the format of a `sharded_save`,
so that it can take advantage of DTensor SPMD based distributed save.

Same as restore, the function only supports saving on the single mesh.

Args:
mesh: The single mesh that all Tensors would be restored to.
checkpoint_prefix : The prefix of checkpoint to be restored.
name_tensor_dict: A ordered dictionary of tensor_names to a DTensor. The
DTensor shape/dtype must match the tensors being saved/restored for now.


def generate_identified_filename(filename: Path, identifier: str) -> Path:
    return filename.parent.joinpath(filename.stem + identifier).with_suffix(filename.suffix)

Append a string-identifier at the end (before the extension, if any) to the provided filepath

Args:
filename: pathlib.Path The actual path object we would like to add an identifier suffix
identifier: The suffix to add

Returns: String with concatenated identifier at the end of the filename


def _normalize_feature_columns(feature_columns):
    if isinstance(feature_columns, fc_types.FeatureColumn):
        feature_columns = [feature_columns]
    if isinstance(feature_columns, collections_abc.Iterator):
        feature_columns = list(feature_columns)
    if isinstance(feature_columns, dict):
        raise ValueError('Expected feature_columns to be iterable, found dict.')
    for column in feature_columns:
        if not isinstance(column, fc_types.FeatureColumn):
            raise ValueError('Items of feature_columns must be a FeatureColumn. Given (type {}): {}.'.format(type(column), column))
    if not feature_columns:
        raise ValueError('feature_columns must not be empty.')
    name_to_column = {}
    for column in feature_columns:
        if column.name in name_to_column:
            raise ValueError('Duplicate feature column name found for columns: {} and {}. This usually means that these columns refer to same base feature. Either one must be discarded or a duplicated but renamed item must be inserted in features dict.'.format(column, name_to_column[column.name]))
        name_to_column[column.name] = column
    return sorted(feature_columns, key=lambda x: x.name)

Normalizes the `feature_columns` input.

This method converts the `feature_columns` to list type as best as it can. In
addition, verifies the type and other parts of feature_columns, required by
downstream library.

Args:
feature_columns: The raw feature columns, usually passed by users.

Returns:
The normalized feature column list.

Raises:
ValueError: for any invalid inputs, such as empty, duplicated names, etc.


def add_variant(self, variant):

        LOG.debug("Upserting variant: {0}".format(variant.get('_id')))

        update = self._get_update(variant)

        message = self.db.variant.update_one(
            {'_id': variant['_id']},
            update,
            upsert=True
        )
        if message.modified_count == 1:
            LOG.debug("Variant %s was updated", variant.get('_id'))
        else:
            LOG.debug("Variant was added to database for first time")
        return

Add a variant to the variant collection

If the variant exists we update the count else we insert a new variant object.

Args:
variant (dict): A variant dictionary


def take_at_most_n_seconds(time_s, func, *args, **kwargs):
    thread = threading.Thread(target=func, args=args, kwargs=kwargs)
    thread.start()
    thread.join(time_s)
    if thread.is_alive():
        return False
    return True

A function that returns whether a function call took less than time_s.

NOTE: The function call is not killed and will run indefinitely if hung.

Args:
time_s: Maximum amount of time to take.
func: Function to call.
*args: Arguments to call the function with.
**kwargs: Keyword arguments to call the function with.
Returns:
True if the function finished in less than time_s seconds.


def split(self, desired_bundle_size: int, start_position: Optional[Any]=None, stop_position: Optional[Any]=None) -> Iterator[SourceBundle]:
    raise NotImplementedError

Splits the source into a set of bundles.

Bundles should be approximately of size ``desired_bundle_size`` bytes.

Args:
desired_bundle_size: the desired size (in bytes) of the bundles returned.
start_position: if specified the given position must be used as the
starting position of the first bundle.
stop_position: if specified the given position must be used as the ending
position of the last bundle.
Returns:
an iterator of objects of type 'SourceBundle' that gives information about
the generated bundles.


def verify_mfa(self, mfa_token):

        response = self.resource.verify_mfa({'mfa_token': mfa_token})
        return (response['valid'] == True or response['valid'] == 'true')

Verify an SMS or TOTP MFA token for this user.

Args:
mfa_token (str): An alphanumeric code from either a User's TOTP
application or sent to them via SMS.

Returns:
True if the mfa_token is valid, False otherwise.


def compile(self, ops):

        def _compile():
            code = []

            for op in ops:
                if isinstance(op, SyscallInvoke):
                    code.extend(self.syscall(op))
                elif isinstance(op, LoadRegister):
                    code.extend(self.reg_load(op.register, op.value))
                elif isinstance(op, str):
                    code.extend(op.split('\n'))
                else:
                    raise ValueError('No idea how to assemble "%s"' % repr(op))
            return ['\t%s' % line for line in code]

        _compile()
        return '\n'.join(self.finalize(self.data_finalizer(_compile(), self.data))) + '\n'

Translate a list of operations into its assembler source.

Arguments:
ops(list): A list of shellcode operations.

Returns:
str: The assembler source code that implements the shellcode.


def multiplier_with_docstring(num, rate=2):
    return num * rate

Multiplies num by rate.

Args:
num (int): the num you want to multiply
rate (int): the rate for multiplication
Returns:
Multiplication of num by rate


def forecast(stl, fc_func, steps=10, seasonal=False, **fc_func_kwargs):
    forecast_array = np.array([])
    trend_array = stl.trend
    for step in range(steps):
        pred = fc_func(np.append(trend_array, forecast_array), **fc_func_kwargs)
        forecast_array = np.append(forecast_array, pred)
    col_name = fc_func.__name__
    observed_timedelta = (stl.observed.index[(- 1)] - stl.observed.index[(- 2)])
    forecast_idx_start = (stl.observed.index[(- 1)] + observed_timedelta)
    forecast_idx = pd.date_range(start=forecast_idx_start, periods=steps, freq=pd.tseries.frequencies.to_offset(observed_timedelta))
    if seasonal:
        seasonal_ix = 0
        max_correlation = (- np.inf)
        detrended_array = np.asanyarray((stl.observed - stl.trend)).squeeze()
        for (i, x) in enumerate(stl.period_averages):
            if (i == 0):
                detrended_slice = detrended_array[(- len(stl.period_averages)):]
            else:
                detrended_slice = detrended_array[(- (len(stl.period_averages) + i)):(- i)]
            this_correlation = np.correlate(detrended_slice, stl.period_averages)[0]
            if (this_correlation > max_correlation):
                max_correlation = this_correlation
                seasonal_ix = i
        rolled_period_averages = np.roll(stl.period_averages, (- seasonal_ix))
        tiled_averages = np.tile(rolled_period_averages, ((steps
        forecast_array += tiled_averages
        col_name += '+seasonal'
    forecast_frame = pd.DataFrame(data=forecast_array, index=forecast_idx)
    forecast_frame.columns = [col_name]
    return forecast_frame

Forecast the given decomposition ``stl`` forward by ``steps`` steps using the forecasting
function ``fc_func``, optionally including the calculated seasonality.

This is an additive model, Y[t] = T[t] + S[t] + e[t]

Args:
stl (a modified statsmodels.tsa.seasonal.DecomposeResult): STL decomposition of observed time
series created using the ``stldecompose.decompose()`` method.
fc_func (function): Function which takes an array of observations and returns a single
valued forecast for the next point.
steps (int, optional): Number of forward steps to include in the forecast
seasonal (bool, optional): Include seasonal component in forecast
fc_func_kwargs: keyword arguments
All remaining arguments are passed to the forecasting function ``fc_func``

Returns:
forecast_frame (pd.Dataframe): A ``pandas.Dataframe`` containing forecast values and a
DatetimeIndex matching the observed index.


def get_unverified_claims(token):

    try:
        claims = jws.get_unverified_claims(token)
    except:
        raise JWTError('Error decoding token claims.')

    try:
        claims = json.loads(claims.decode('utf-8'))
    except ValueError as e:
        raise JWTError('Invalid claims string: %s' % e)

    if not isinstance(claims, Mapping):
        raise JWTError('Invalid claims string: must be a json object')

    return claims

Returns the decoded claims without verification of any kind.

Args:
token (str): A signed JWT to decode the headers from.

Returns:
dict: The dict representation of the token claims.

Raises:
JWTError: If there is an exception decoding the token.


def star(self, input_string):

        if input_string != self.epsilon and input_string != self.empty:
            return "(" + input_string + ")*"
        else:
            return ""

Kleene star operation
Args:
input_string (str): The string that the kleene star will be made
Returns:
str: The applied Kleene star operation on the input string


def play_human(env):
    try:
        play(env, fps=env.metadata['video.frames_per_second'])
    except KeyboardInterrupt:
        pass
    env.close()

Play the environment using keyboard as a human.

Args:
env (gym.Env): the initialized gym environment to play

Returns:
None


def index_of_coincidence(*texts):
    if (not texts):
        raise ValueError('texts must not be empty')
    return statistics.mean((_calculate_index_of_coincidence(frequency_analyze(text), len(text)) for text in texts))

Calculate the index of coincidence for one or more ``texts``.
The results are averaged over multiple texts to return the delta index of coincidence.

Examples:
>>> index_of_coincidence("aabbc")
0.2

>>> index_of_coincidence("aabbc", "abbcc")
0.2

Args:
*texts (variable length argument list): The texts to analyze

Returns:
Decimal value of the index of coincidence

Raises:
ValueError: If texts is empty
ValueError: If any text is less that 2 character long


def run_shell_command(state, host, command, get_pty=False, timeout=None, print_output=False, **command_kwargs):
    command = make_command(command, **command_kwargs)
    logger.debug('--> Running command on localhost: {0}'.format(command))
    if print_output:
        print('{0}>>> {1}'.format(host.print_prefix, command))
    process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
    stdout_reader = gevent.spawn(read_buffer, process.stdout, print_output=print_output, print_func=(lambda line: '{0}{1}'.format(host.print_prefix, line)))
    stderr_reader = gevent.spawn(read_buffer, process.stderr, print_output=print_output, print_func=(lambda line: '{0}{1}'.format(host.print_prefix, click.style(line, 'red'))))
    greenlets = gevent.wait((stdout_reader, stderr_reader), timeout=timeout)
    if (len(greenlets) != 2):
        stdout_reader.kill()
        stderr_reader.kill()
        raise timeout_error()
    stdout = stdout_reader.get()
    stderr = stderr_reader.get()
    logger.debug('--> Waiting for exit status...')
    process.wait()
    process.stdout.close()
    logger.debug('--> Command exit status: {0}'.format(process.returncode))
    return ((process.returncode == 0), stdout, stderr)

Execute a command on the local machine.

Args:
state (``pyinfra.api.State`` obj): state object for this command
hostname (string): hostname of the target
command (string): actual command to execute
sudo (boolean): whether to wrap the command with sudo
sudo_user (string): user to sudo to
get_pty (boolean): whether to get a PTY before executing the command
env (dict): envrionment variables to set
timeout (int): timeout for this command to complete before erroring

Returns:
tuple: (exit_code, stdout, stderr)
stdout and stderr are both lists of strings from each buffer.


def get_events_for_blocks(self, blocks, subscriptions):
    events = []
    for blkw in blocks:
        events.extend(self.get_events_for_block(blkw, subscriptions))
    return events

Get a list of events associated with all the blocks.

Args:
blocks (list of BlockWrapper): The blocks to search for events that
match each subscription.
subscriptions (list of EventSubscriptions): EventFilter and
event type to filter events.

Returns (list of Events): The Events associated which each block id.

Raises:
KeyError A receipt is missing from the receipt store.


def FormatSOAPDateTime(value):

  value_date = value['date']
  return '%s-%s-%s %s:%s:%s (%s)' % (
      value_date['year'], value_date['month'], value_date['day'],
      value['hour'], value['minute'], value['second'], value['timeZoneId'])

Format a SOAP DateTime object for printing.

Args:
value: The DateTime object to format.

Returns:
A string representing the value.