def get_image_patches(self, image: np.array, grid_pinpoints: List[Tuple[int, int]], patch_size: int, resample: PILImageResampling, data_format: ChannelDimension, input_data_format: ChannelDimension) -> List[np.array]:
    if not isinstance(grid_pinpoints, list):
        raise TypeError('grid_pinpoints must be a list of possible resolutions.')
    possible_resolutions = grid_pinpoints
    image_size = get_image_size(image, channel_dim=input_data_format)
    best_resolution = select_best_resolution(image_size, possible_resolutions)
    resized_image = self._resize_for_patching(image, best_resolution, resample=resample, input_data_format=input_data_format)
    padded_image = self._pad_for_patching(resized_image, best_resolution, input_data_format=input_data_format)
    patches = divide_to_patches(padded_image, patch_size=patch_size, input_data_format=input_data_format)
    patches = [to_channel_dimension_format(patch, channel_dim=data_format, input_channel_dim=input_data_format) for patch in patches]
    return patches

Process an image with variable resolutions by dividing it into patches.

Args:
image (`np.array`):
The input image to be processed.
grid_pinpoints (List[Tuple[int, int]]):
A list of possible resolutions as tuples.
patch_size (`int`):
Size of the patches to divide the image into.
resample (`PILImageResampling`):
Resampling filter to use if resizing the image.
data_format (`ChannelDimension` or `str`):
The channel dimension format for the output image.
input_data_format (`ChannelDimension` or `str`):
The channel dimension format of the input image.

Returns:
`List[np.array]`: A list of NumPy arrays containing the processed image patches.


def update(self, rid, data, raise_on_error=True):

        return self.put(rid, data, raise_on_error)

Update the for the provided Id. Alias for put() method.

Args:
rid (str): The record identifier.
data (dict): The record data.
raise_on_error (bool): If True and not r.ok this method will raise a RunTimeError.

Returns:
object : Python request response.


def find_vasp_calculations():

    dir_list = [ './' + re.sub( r'vasprun\.xml', '', path ) for path in glob.iglob( '**/vasprun.xml', recursive=True ) ]
    gz_dir_list = [ './' + re.sub( r'vasprun\.xml\.gz', '', path ) for path in glob.iglob( '**/vasprun.xml.gz', recursive=True ) ]
    return dir_list + gz_dir_list

Returns a list of all subdirectories that contain either a vasprun.xml file
or a compressed vasprun.xml.gz file.

Args:
None

Returns:
(List): list of all VASP calculation subdirectories.


def parse_newsgroup(line):
    parts = line.split()
    try:
        group = parts[0]
        low = int(parts[1])
        high = int(parts[2])
        status = parts[3]
    except (IndexError, ValueError):
        raise ValueError('Invalid newsgroup info')
    return (group, low, high, status)

Parse a newsgroup info line to python types.

Args:
line: An info response line containing newsgroup info.

Returns:
A tuple of group name, low-water as integer, high-water as integer and
posting status.

Raises:
ValueError: If the newsgroup info cannot be parsed.

Note:
Posting status is a character is one of (but not limited to):
"y" posting allowed
"n" posting not allowed
"m" posting is moderated


def createTemplate(data):
    conn = Qubole.agent()
    return conn.post(Template.rest_entity_path, data)

Create a new template.

Args:
`data`: json data required for creating a template
Returns:
Dictionary containing the details of the template with its ID.


def get_bit_mask_from_enumerations(enumerations):
    return functools.reduce((lambda x, y: (x | y)), [z.value for z in enumerations])

A utility function that computes a bit mask from a collection of
enumeration values.

Args:
enumerations (list): A list of enumeration values to be combined in a
composite bit mask.

Returns:
int: The composite bit mask.


def firmware_version(self):
    namespace = 'urn:brocade.com:mgmt:brocade-firmware-ext'
    request_ver = ET.Element('show-firmware-version', xmlns=namespace)
    ver = self._callback(request_ver, handler='get')
    return ver.find(('.

Returns firmware version.

Args:
None

Returns:
Dictionary

Raises:
None


def __init__(self, field, value, **kwargs):

        return super(DomainCondition, self).__init__(
            field=field, value=value, **kwargs
        )

Initialize a new generic query condition.

Args:
field (str): Field name to search on. This should be the
Pythonified name as in the internal models, not the
name as provided in the API e.g. ``first_name`` for
the Customer's first name instead of ``firstName``.
value (mixed): The value of the field.


def cumulative_distribution(self, X):

        self.check_fit()

        U, V = self.split_matrix(X)

        if self.theta == 1:
            return np.multiply(U, V)

        else:
            h = np.power(-np.log(U), self.theta) + np.power(-np.log(V), self.theta)
            h = -np.power(h, 1.0 / self.theta)
            cdfs = np.exp(h)
            return cdfs

Computes the cumulative distribution function for the copula, :math:`C(u, v)`

Args:
X: `np.ndarray`

Returns:
np.array: cumulative probability


def add(self, index, value):
    self.buf.append(value)
    if ((index - self.flush_at) < self.interval):
        return
    value = np.mean(self.buf)
    if self.verbose:
        logger.info('iter={} {{{}}}={}'.format(index, self.name, value))
    if (self.fd is not None):
        print('{} {:g}'.format(index, value), file=self.fd)
    self.flush_at = index
    self.buf = []

Add a value to the series.

Args:
index (int): Index.
value (float): Value.


def get_student_current_grade(self, username, course_id):

        resp = self.requester.get(
            urljoin(
                self.base_url,
                '/api/grades/v1/courses/{course_key}/?username={username}'.format(
                    username=username,
                    course_key=course_id
                )
            )
        )

        resp.raise_for_status()

        return CurrentGrade(resp.json()[0])

Returns an CurrentGrade object for the user in a course

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

Returns:
CurrentGrade: object representing the student current grade for a course


def create_cloudwatch_log_event(app_name, env, region, rules):
    session = boto3.Session(profile_name=env, region_name=region)
    cloudwatch_client = session.client('logs')
    log_group = rules.get('log_group')
    filter_name = rules.get('filter_name')
    filter_pattern = rules.get('filter_pattern')
    if (not log_group):
        LOG.critical('Log group is required and no "log_group" is defined!')
        raise InvalidEventConfiguration('Log group is required and no "log_group" is defined!')
    if (not filter_name):
        LOG.critical('Filter name is required and no filter_name is defined!')
        raise InvalidEventConfiguration('Filter name is required and no filter_name is defined!')
    if (filter_pattern is None):
        LOG.critical('Filter pattern is required and no filter_pattern is defined!')
        raise InvalidEventConfiguration('Filter pattern is required and no filter_pattern is defined!')
    lambda_alias_arn = get_lambda_alias_arn(app=app_name, account=env, region=region)
    statement_id = '{}_cloudwatchlog_{}'.format(app_name, filter_name.replace(' ', '_'))
    principal = 'logs.{}.amazonaws.com'.format(region)
    account_id = get_env_credential(env=env)['accountId']
    source_arn = 'arn:aws:logs:{0}:{1}:log-group:{2}:*'.format(region, account_id, log_group)
    add_lambda_permissions(function=lambda_alias_arn, statement_id=statement_id, action='lambda:InvokeFunction', principal=principal, source_arn=source_arn, env=env, region=region)
    cloudwatch_client.put_subscription_filter(logGroupName=log_group, filterName=filter_name, filterPattern=filter_pattern, destinationArn=lambda_alias_arn)
    LOG.info('Created Cloudwatch log event with filter: %s', filter_pattern)

Create cloudwatch log event for lambda from rules.

Args:
app_name (str): name of the lambda function
env (str): Environment/Account for lambda function
region (str): AWS region of the lambda function
rules (str): Trigger rules from the settings


def delete_tag(self, key, update_session=True):

        existing_tags = {x.key: x for x in self.tags}
        if key in existing_tags:
            if update_session:
                db.session.delete(existing_tags[key])

            self.tags.remove(existing_tags[key])
            return True

        return False

Removes a tag from a resource based on the tag key. Returns `True` if the tag was removed or `False` if the
tag didn't exist

Args:
key (str): Key of the tag to delete
update_session (bool): Automatically add the change to the SQLAlchemy session. Default: True

Returns:


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

    query_hash = hash(query)

    title = self._GetRowValue(query_hash, row, 'title')

    event_data = FirefoxPlacesBookmarkFolderEventData()
    event_data.offset = self._GetRowValue(query_hash, row, 'id')
    event_data.query = query
    event_data.title = title or 'N/A'

    timestamp = self._GetRowValue(query_hash, row, 'dateAdded')
    if timestamp:
      date_time = dfdatetime_posix_time.PosixTimeInMicroseconds(
          timestamp=timestamp)
      event = time_events.DateTimeValuesEvent(
          date_time, definitions.TIME_DESCRIPTION_ADDED)
      parser_mediator.ProduceEventWithEventData(event, event_data)

    timestamp = self._GetRowValue(query_hash, row, 'lastModified')
    if timestamp:
      date_time = dfdatetime_posix_time.PosixTimeInMicroseconds(
          timestamp=timestamp)
      event = time_events.DateTimeValuesEvent(
          date_time, definitions.TIME_DESCRIPTION_MODIFICATION)
      parser_mediator.ProduceEventWithEventData(event, event_data)

Parses a bookmark folder row.

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


def parse_dict(self, args: dict[str, Any], allow_extra_keys: bool=False) -> tuple[DataClass, ...]:
    unused_keys = set(args.keys())
    outputs = []
    for dtype in self.dataclass_types:
        keys = {f.name for f in dataclasses.fields(dtype) if f.init}
        inputs = {k: v for k, v in args.items() if k in keys}
        unused_keys.difference_update(inputs.keys())
        obj = dtype(**inputs)
        outputs.append(obj)
    if not allow_extra_keys and unused_keys:
        raise ValueError(f'Some keys are not used by the HfArgumentParser: {sorted(unused_keys)}')
    return tuple(outputs)

Alternative helper method that does not use `argparse` at all, instead uses a dict and populating the dataclass
types.

Args:
args (`dict`):
dict containing config values
allow_extra_keys (`bool`, *optional*, defaults to `False`):
Defaults to False. If False, will raise an exception if the dict contains keys that are not parsed.

Returns:
Tuple consisting of:

- the dataclass instances in the same order as they were passed to the initializer.


def copy_directory_structure(destination_directory, relative_path):
    full_path = os.path.join(destination_directory, relative_path)
    if os.path.exists(full_path):
        return
    os.makedirs(destination_directory, relative_path)

Create all the intermediate directories required for relative_path to exist within destination_directory.
This assumes that relative_path is a directory located within root_dir.

Examples:
destination_directory: /tmp/destination
relative_path: test/unit/

will create:  /tmp/destination/test/unit

Args:
destination_directory (str): root of the destination directory where the directory structure will be created.
relative_path (str): relative path that will be created within destination_directory


def _colocate_with_for_gradient(op, gradient_uid, ignore_existing=False) -> ContextManager[None]:
    if context.executing_eagerly():
        if op is not None:
            if not hasattr(op, 'device'):
                op = convert_to_tensor(op)
            return device(op.device)
        else:
            return NullContextmanager()
    else:
        default_graph = get_default_graph()
        if isinstance(op, EagerTensor):
            if default_graph.building_function:
                return default_graph.device(op.device)
            else:
                raise ValueError('Encountered an Eager-defined Tensor during graph construction, but a function was not being built.')
        return default_graph._colocate_with_for_gradient(op, gradient_uid=gradient_uid, ignore_existing=ignore_existing)

Returns a context manager for colocating op gradients with an op.

Internal API. In eager mode, returns a context manager that sets the default
device for new ops to the same device as the given op. Does the same if a
function is currently being built (i.e. the current mode is graph, but the
overall mode is eager).

In all other cases, returns a `Graph.colocate_with()` context manager,
optionally accounting for gradients (if a gradient UID is specified).

Args:
op: Operation or Tensor with which to colocate.
gradient_uid: Optional gradient UID to enable colocation of gradients during
compilation.
ignore_existing: See `Graph.colocate_with()`.

Returns:
A context manager used to colocate ops and gradients with the specified
operation.


def assert_scalar(tensor, name=None, message=None):
    with ops.name_scope(name, 'assert_scalar', [tensor]) as name_scope:
        tensor = ops.convert_to_tensor(tensor, name=name_scope)
        shape = tensor.get_shape()
        message = _message_prefix(message)
        if shape.ndims != 0:
            if context.executing_eagerly():
                raise ValueError('%sExpected scalar shape, saw shape: %s.' % (message, shape))
            else:
                raise ValueError('%sExpected scalar shape for %s, saw shape: %s.' % (message, tensor.name, shape))
        return tensor

Asserts that the given `tensor` is a scalar (i.e. zero-dimensional).

This function raises `ValueError` unless it can be certain that the given
`tensor` is a scalar. `ValueError` is also raised if the shape of `tensor` is
unknown.

Args:
tensor: A `Tensor`.
name:  A name for this operation. Defaults to "assert_scalar"
message: A string to prefix to the default message.

Returns:
The input tensor (potentially converted to a `Tensor`).

Raises:
ValueError: If the tensor is not scalar (rank 0), or if its shape is
unknown.


def __eq__(self, other):

        if not isinstance(other, self.__class__):
            return False

        headers = self._headers.copy()
        other_headers = other._headers.copy()
        try:
            del headers["sent-at"]
        except KeyError:
            pass
        try:
            del other_headers["sent-at"]
        except KeyError:
            pass

        return (
            self.topic == other.topic
            and self.body == other.body
            and headers == other_headers
        )

Two messages of the same class with the same topic, headers, and body are equal.

The "sent-at" header is excluded from the equality check as this is set
automatically and is dependent on when the object is created.

Args:
other (object): The object to check for equality.

Returns:
bool: True if the messages are equal.


def DEFINE_boolean(flag_name, default_value, docstring):

    def str2bool(bool_str):

        return bool_str.lower() in ('true', 't', '1')

    get_context_parser().add_argument(
        '--' + flag_name,
        nargs='?',
        const=True,
        help=docstring,
        default=default_value,
        type=str2bool)

    get_context_parser().add_argument(
        '--no' + flag_name,
        action='store_false',
        dest=flag_name.replace('-', '_'))

Defines a flag of type 'boolean'.
Args:
flag_name: The name of the flag as a string.
default_value: The default value the flag should take as a boolean.
docstring: A helpful message explaining the use of the flag.


def wait_stopped(self, timeout=None, force=False):
    self.join(timeout)
    if (self.is_alive() and (force is False)):
        raise TimeoutExpiredError('Error waiting for background thread to exit', timeout=timeout)

Wait for the thread to stop.

You must have previously called signal_stop or this function will
hang.

Args:

timeout (float): The maximum time to wait for the thread to stop
before raising a TimeoutExpiredError.  If force is True,
TimeoutExpiredError is not raised and the thread is just
marked as a daemon thread so that it does not block cleanly
exiting the process.
force (bool): If true and the thread does not exit in timeout seconds
no error is raised since the thread is marked as daemon and will
be killed when the process exits.


class FixedThreshold(ThresholdFn):

    def __init__(self, cutoff: float, **kwargs):
        super().__init__(**kwargs)
        self._cutoff = cutoff

    @property
    def is_stateful(self) -> bool:

        return False

    @property
    def threshold(self) -> float:

        return self._cutoff

    def apply(self, score: Optional[float]) -> Optional[int]:

        if score is None:
            return None
        if math.isnan(score):
            return self._missing_label
        if score < self.threshold:
            return self._normal_label
        return self._outlier_label

Applies a fixed cutoff value to anomaly scores.

This `ThresholdFn` is stateless and uses a pre-defined cutoff value to
classify anomaly scores. Scores below the cutoff are considered normal, while
scores at or above the cutoff are classified as outliers.

Args:
cutoff (float): The fixed threshold value. Anomaly scores at or above this
value will be labeled as outliers.
**kwargs: Additional keyword arguments to be passed to the base
`ThresholdFn` constructor.


def circuit_diagram_info(val: Any, args: Optional[CircuitDiagramInfoArgs]=None, default=RaiseTypeErrorIfNotProvided):
    if (args is None):
        args = CircuitDiagramInfoArgs.UNINFORMED_DEFAULT
    getter = getattr(val, '_circuit_diagram_info_', None)
    result = (NotImplemented if (getter is None) else getter(args))
    if isinstance(result, str):
        return CircuitDiagramInfo(wire_symbols=(result,))
    if isinstance(result, collections.Iterable):
        return CircuitDiagramInfo(wire_symbols=tuple(result))
    if (result is not NotImplemented):
        return result
    if (default is not RaiseTypeErrorIfNotProvided):
        return default
    if (getter is None):
        raise TypeError("object of type '{}' has no _circuit_diagram_info_ method.".format(type(val)))
    raise TypeError("object of type '{}' does have a _circuit_diagram_info_ method, but it returned NotImplemented.".format(type(val)))

Requests information on drawing an operation in a circuit diagram.

Calls _circuit_diagram_info_ on `val`. If `val` doesn't have
_circuit_diagram_info_, or it returns NotImplemented, that indicates that
diagram information is not available.

Args:
val: The operation or gate that will need to be drawn.
args: A CircuitDiagramInfoArgs describing the desired drawing style.
default: A default result to return if the value doesn't have circuit
diagram information. If not specified, a TypeError is raised
instead.

Returns:
If `val` has no _circuit_diagram_info_ method or it returns
NotImplemented, then `default` is returned (or a TypeError is
raised if no `default` is specified).

Otherwise, the value returned by _circuit_diagram_info_ is returned.

Raises:
TypeError:
`val` doesn't have circuit diagram information and `default` was
not specified.


def GetHostname(self, session_identifier=CURRENT_SESSION):

    hostname_artifact = self._hostnames.get(session_identifier, None)
    if not hostname_artifact:
      return ''

    return hostname_artifact.name or ''

Retrieves the hostname related to the event.

If the hostname is not stored in the event it is determined based
on the preprocessing information that is stored inside the storage file.

Args:
session_identifier (Optional[str])): session identifier, where
CURRENT_SESSION represents the active session.

Returns:
str: hostname.


def diff_parameters(old_params, new_params):

    [changes, diff] = diff_dictionaries(old_params, new_params)
    if changes == 0:
        return []
    return diff

Compares the old vs. new parameters and returns a "diff"

If there are no changes, we return an empty list.

Args:
old_params(dict): old paramters
new_params(dict): new parameters

Returns:
list: A list of differences


def _get_events_data(object_key: str) -> List[dict]:

    events_data = []
    key = _keys.events_data(object_key)
    for event_id in _get_events_list(object_key):
        event_dict = literal_eval(DB.get_hash_value(key, event_id))
        events_data.append(event_dict)
    return events_data

Get the list of event data for the object with the specified key.

Args:
object_key (str): Key of an object in the database.


def _restore_output_tensor_names(graph_def: graph_pb2.GraphDef) -> graph_pb2.GraphDef:
    output_renaming_map = {}
    with session.Session(graph=ops.Graph()):
        importer.import_graph_def(graph_def, name='')
        graph = ops.get_default_graph()
        for op in graph.get_operations():
            if op.type == '_Retval':
                expected_node_name = op.name
                if op.get_attr('tf_saved_model.index_path') is not None:
                    index_path_name = op.get_attr('tf_saved_model.index_path')[0]
                    index_path_name = index_path_name.decode('utf-8').split(':')[0]
                    try:
                        index_path_node = graph.get_operation_by_name(index_path_name)
                        if index_path_node.type == '_Retval':
                            expected_node_name = index_path_name
                    except KeyError:
                        pass
                retval_input_node_name = op.inputs[0].op.name
                output_renaming_map[retval_input_node_name] = expected_node_name
    for node in reversed(graph_def.node):
        if node.name in output_renaming_map:
            node.name = output_renaming_map[node.name]
        elif node.op == '_Retval':
            graph_def.node.remove(node)
        else:
            for idx, input_name in enumerate(node.input):
                if input_name in output_renaming_map:
                    node.input[idx] = output_renaming_map[input_name]
            updating_inputs = []
            for input_name in reversed(node.input):
                if input_name.startswith('^') and input_name[1:] in output_renaming_map:
                    updating_inputs.append(input_name[1:])
                    node.input.remove(input_name)
            for updating_input in updating_inputs:
                node.input.append('^' + output_renaming_map[updating_input])
    return graph_def

Restores the output tensor names of the converted model.

During the conversion, the output tensor names of the original model are
embedded in the `tf_saved_model.index_path` attribute of the RetVal nodes and
might become the name of Retval nodes as well (with an index suffix if there
are multiple output tensors from one node). Since Retval nodes are not used in
SavedModel, this function removes them and restore the names to the actual
output tensors.

Args:
graph_def: the converted GraphDef.

Returns:
The GraphDef with Retval nodes removed and output tensor names restored.


def _ParseHeader(self, parser_mediator, file_object):
    header_map = self._GetDataTypeMap('cups_ipp_header')
    try:
        (header, _) = self._ReadStructureFromFileObject(file_object, 0, header_map)
    except (ValueError, errors.ParseError) as exception:
        raise errors.UnableToParseFile('[{0:s}] Unable to parse header with error: {1!s}'.format(self.NAME, exception))
    format_version = '{0:d}.{1:d}'.format(header.major_version, header.minor_version)
    if (format_version not in self._SUPPORTED_FORMAT_VERSIONS):
        raise errors.UnableToParseFile('[{0:s}] Unsupported format version {1:s}.'.format(self.NAME, format_version))
    if (header.operation_identifier != 5):
        display_name = parser_mediator.GetDisplayName()
        logger.debug('[{0:s}] Non-standard operation identifier: 0x{1:08x} in file header of: {2:s}.'.format(self.NAME, header.operation_identifier, display_name))

Parses a CUPS IPP header from a file-like object.

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

Raises:
UnableToParseFile: when the header cannot be parsed.


def parameterized_send(self, request, parameter_list):

        response_queues = OrderedDict()
        for parameter in parameter_list:
            response_queues[parameter] = self.send(request % parameter)
        return response_queues

Send batched requests for a list of parameters

Args:
request (str): Request to send, like "%s.*?\n"
parameter_list (list): parameters to format with, like
["TTLIN", "TTLOUT"]

Returns:
dict: {parameter: response_queue}


def write_structure(times=None):

    if times is None:
        return report_loc.write_structure(f.root.times)
    else:
        if not isinstance(times, Times):
            raise TypeError("Expected Times instance for param 'times' (default is root).")
        return report_loc.write_structure(times)

Produce a formatted record of a times data structure.

Args:
times (Times, optional): If not provided, uses the current root timer.

Returns:
str: Timer tree hierarchy in a formatted string.

Raises:
TypeError: If provided argument is not a Times object.


def __init__(self, encoding=None, suppress_output=False):
    if not encoding:
        encoding = self._GetConsoleEncoding()
    elif encoding == 'win':
        encoding = 'cp437'
    self._encoding = encoding or 'ascii'
    self._term = '' if suppress_output else os.getenv('TERM', '').lower()
    if self.SupportsAnsi():
        self._csi = '\x1b['
        self._font_bold = '1'
        self._font_italic = '4'
    else:
        self._csi = None
        self._font_bold = ''
        self._font_italic = ''
    is_screen_reader = False
    if self._encoding == 'utf8' and (not is_screen_reader):
        self._box_line_characters = BoxLineCharactersUnicode()
        self._bullets = self._BULLETS_UNICODE
        self._progress_tracker_symbols = ProgressTrackerSymbolsUnicode()
    elif self._encoding == 'cp437' and (not is_screen_reader):
        self._box_line_characters = BoxLineCharactersUnicode()
        self._bullets = self._BULLETS_WINDOWS
        self._progress_tracker_symbols = ProgressTrackerSymbolsAscii()
    else:
        self._box_line_characters = BoxLineCharactersAscii()
        if is_screen_reader:
            self._box_line_characters = BoxLineCharactersScreenReader()
        self._bullets = self._BULLETS_ASCII
        self._progress_tracker_symbols = ProgressTrackerSymbolsAscii()
    self._get_raw_key = [console_attr_os.GetRawKeyFunction()]
    self._term_size = (0, 0) if suppress_output else console_attr_os.GetTermSize()
    self._display_width_cache = {}

Constructor.

Args:
encoding: Encoding override.
ascii -- ASCII art. This is the default.
utf8 -- UTF-8 unicode.
win -- Windows code page 437.
suppress_output: True to create a ConsoleAttr that doesn't want to output
anything.


def build_byte_align_buff(bits):
    bitmod = (len(bits) % 8)
    if (bitmod == 0):
        rdiff = bitarray()
    else:
        rdiff = bitarray((8 - bitmod))
        rdiff.setall(False)
    return (rdiff + bits)

Pad the left side of a bitarray with 0s to align its length with byte boundaries.

Args:
bits: A bitarray to be padded and aligned.

Returns:
A newly aligned bitarray.


def _ValidateDataTypeDefinition(cls, data_type_definition):

    if not cls._IsIdentifier(data_type_definition.name):
      raise ValueError(
          'Data type definition name: {0!s} not a valid identifier'.format(
              data_type_definition.name))

    if keyword.iskeyword(data_type_definition.name):
      raise ValueError(
          'Data type definition name: {0!s} matches keyword'.format(
              data_type_definition.name))

    members = getattr(data_type_definition, 'members', None)
    if not members:
      raise ValueError(
          'Data type definition name: {0!s} missing members'.format(
              data_type_definition.name))

    defined_attribute_names = set()

    for member_definition in members:
      attribute_name = member_definition.name

      if not cls._IsIdentifier(attribute_name):
        raise ValueError('Attribute name: {0!s} not a valid identifier'.format(
            attribute_name))

      if attribute_name.startswith('_'):
        raise ValueError('Attribute name: {0!s} starts with underscore'.format(
            attribute_name))

      if keyword.iskeyword(attribute_name):
        raise ValueError('Attribute name: {0!s} matches keyword'.format(
            attribute_name))

      if attribute_name in defined_attribute_names:
        raise ValueError('Attribute name: {0!s} already defined'.format(
            attribute_name))

      defined_attribute_names.add(attribute_name)

Validates the data type definition.

Args:
data_type_definition (DataTypeDefinition): data type definition.

Raises:
ValueError: if the data type definition is not considered valid.


def convert(self):
    if not _jit:
        raise ImportError('Cannot import jit from jax.')
    if not self._serving_funcs:
        raise ValueError('No serving func is specified.')
    if not self._inputs:
        raise ValueError('Input tensors are not specified.')
    if len(self._inputs) != len(self._serving_funcs):
        msg = 'Input tensor mapping len {} does not match serving func len {}.'.format(len(self._inputs), len(self._serving_funcs))
        raise ValueError(msg)
    if not isinstance(self._inputs, (tuple, list)):
        raise ValueError('Input tensors should be pass in a tuple list wrapped in an array.')
    if len(self._serving_funcs) > 1:
        raise ValueError('Currently only support single serving function.')
    if not isinstance(self._inputs[0], (tuple, list)):
        raise ValueError('The input placeholders are not a dictionary.')
    input_names = []
    ordered_inputs = []
    for input_name, tensor in self._inputs[0]:
        input_names.append(input_name)
        ordered_inputs.append(tensor)
    try:
        hlo_proto = _jit(self._serving_funcs[0]).trace(*ordered_inputs).lower(lowering_platforms=('cpu',)).compiler_ir('hlo').as_serialized_hlo_module_proto()
    except Exception:
        raise ValueError('Failed to convert the given Jax function to hlo.')
    converter_kwargs = {'input_content': hlo_proto, 'input_names': input_names, 'is_proto_format': True}
    converter_kwargs.update(self._get_base_converter_args())
    quant_mode = QuantizationMode(self.optimizations, self.target_spec, self.representative_dataset, None, experimental_qdq_annotation=self._experimental_strict_qdq)
    self._validate_inference_input_output_types(quant_mode)
    converter_kwargs.update(quant_mode.converter_flags())
    result = _convert_jax_hlo(**converter_kwargs)
    return self._optimize_tflite_model(result, quant_mode, _build_conversion_flags(**converter_kwargs).debug_options, quant_io=self.experimental_new_quantizer)

Converts a Jax serving func based on instance variables.

Returns:
The converted data in serialized format.

Raises:
ImportError:
If cannot import the jit from jax.
ValueError:
No serving function is specified.
Input tensors are not specified.
The truth value of an array with more than one element is ambiguous.
Failed to convert the given Jax function to hlo.


class CustomObjectScope(object):

    def __init__(self, *args):
        self.custom_objects = args
        self.backup = None

    def __enter__(self):
        self.backup = _GLOBAL_CUSTOM_OBJECTS.copy()
        for objects in self.custom_objects:
            _GLOBAL_CUSTOM_OBJECTS.update(objects)
        return self

    def __exit__(self, *args, **kwargs):
        _GLOBAL_CUSTOM_OBJECTS.clear()
        _GLOBAL_CUSTOM_OBJECTS.update(self.backup)

Exposes custom classes/functions to Keras deserialization internals.

Under a scope `with custom_object_scope(objects_dict)`, Keras methods such
as `tf.keras.models.load_model` or `tf.keras.models.model_from_config`
will be able to deserialize any custom object referenced by a
saved config (e.g. a custom layer or metric).

Example:

Consider a custom regularizer `my_regularizer`:

```python
layer = Dense(3, kernel_regularizer=my_regularizer)
config = layer.get_config()  # Config contains a reference to `my_regularizer`
...
# Later:
with custom_object_scope({'my_regularizer': my_regularizer}):
layer = Dense.from_config(config)
```

Args:
*args: Dictionary or dictionaries of `{name: object}` pairs.


def _adjust_block(p, ip, filters, block_id=None):
    channel_dim = 1 if backend.image_data_format() == 'channels_first' else -1
    img_dim = 2 if backend.image_data_format() == 'channels_first' else -2
    with backend.name_scope('adjust_block'):
        if p is None:
            p = ip
        elif p.shape[img_dim] != ip.shape[img_dim]:
            with backend.name_scope(f'adjust_reduction_block_{block_id}'):
                p = layers.Activation('relu', name=f'adjust_relu_1_{block_id}')(p)
                p1 = layers.AveragePooling2D((1, 1), strides=(2, 2), padding='valid', name=f'adjust_avg_pool_1_{block_id}')(p)
                p1 = layers.Conv2D(filters
                p2 = layers.ZeroPadding2D(padding=((0, 1), (0, 1)))(p)
                p2 = layers.Cropping2D(cropping=((1, 0), (1, 0)))(p2)
                p2 = layers.AveragePooling2D((1, 1), strides=(2, 2), padding='valid', name=f'adjust_avg_pool_2_{block_id}')(p2)
                p2 = layers.Conv2D(filters
                p = layers.concatenate([p1, p2], axis=channel_dim)
                p = layers.BatchNormalization(axis=channel_dim, momentum=0.9997, epsilon=0.001, name=f'adjust_bn_{block_id}')(p)
        elif p.shape[channel_dim] != filters:
            with backend.name_scope(f'adjust_projection_block_{block_id}'):
                p = layers.Activation('relu')(p)
                p = layers.Conv2D(filters, (1, 1), strides=(1, 1), padding='same', name=f'adjust_conv_projection_{block_id}', use_bias=False, kernel_initializer='he_normal')(p)
                p = layers.BatchNormalization(axis=channel_dim, momentum=0.9997, epsilon=0.001, name=f'adjust_bn_{block_id}')(p)
    return p

Adjusts the input `previous path` to match the shape of the `input`.

Used in situations where the output number of filters needs to be changed.

Args:
p: Input tensor which needs to be modified
ip: Input tensor whose shape needs to be matched
filters: Number of output filters to be matched
block_id: String block_id

Returns:
Adjusted Keras tensor


def to_image(dataset):

    dataset = dataset.squeeze()
    if dataset.ndim < 2:
        raise ValueError("Need at least a 2D array to make an image.")
    else:
        return XRImage(dataset)

convert ``dataset`` into a :class:`~trollimage.xrimage.XRImage` instance.

Convert the ``dataset`` into an instance of the
:class:`~trollimage.xrimage.XRImage` class.  This function makes no other
changes.  To get an enhanced image, possibly with overlays and decoration,
see :func:`~get_enhanced_image`.

Args:
dataset (xarray.DataArray): Data to be converted to an image.

Returns:
Instance of :class:`~trollimage.xrimage.XRImage`.


def _validate_all_blocks_supported(ir_blocks, query_metadata_table):
    if (len(ir_blocks) < 3):
        raise AssertionError(u'Unexpectedly attempting to validate IR blocks with fewer than 3 blocks. A minimal query is expected to have at least a QueryRoot, GlobalOperationsStart, and ConstructResult block. The query metadata table is {}.'.format(query_metadata_table))
    construct_result = _get_construct_result(ir_blocks)
    unsupported_blocks = []
    unsupported_fields = []
    for block in ir_blocks[:(- 1)]:
        if isinstance(block, constants.SUPPORTED_BLOCK_TYPES):
            continue
        if isinstance(block, constants.SKIPPABLE_BLOCK_TYPES):
            continue
        unsupported_blocks.append(block)
    for (field_name, field) in six.iteritems(construct_result.fields):
        if (not isinstance(field, constants.SUPPORTED_OUTPUT_EXPRESSION_TYPES)):
            unsupported_fields.append((field_name, field))
        elif (field.location.field in constants.UNSUPPORTED_META_FIELDS):
            unsupported_fields.append((field_name, field))
    if ((len(unsupported_blocks) > 0) or (len(unsupported_fields) > 0)):
        raise NotImplementedError(u'Encountered unsupported blocks {} and unsupported fields {} during construction of SQL query tree for IR blocks {} with query metadata table {}.'.format(unsupported_blocks, unsupported_fields, ir_blocks, query_metadata_table))

Validate that all IR blocks and ConstructResult fields passed to the backend are supported.

Args:
ir_blocks: List[BasicBlock], IR blocks to validate.
query_metadata_table: QueryMetadataTable, object containing all metadata collected during
query processing, including location metadata (e.g. which locations
are folded or optional).

Raises:
NotImplementedError, if any block or ConstructResult field is unsupported.


def remove_room_alias(self, room_alias):
    try:
        self.api.remove_room_alias(room_alias)
        return True
    except MatrixRequestError:
        return False

Remove mapping of an alias

Args:
room_alias(str): The alias to be removed.

Returns:
bool: True if the alias is removed, False otherwise.


def _log_score(score):
    logger.info('Score of ({}/{}) set for submission {}'.format(score.points_earned, score.points_possible, score.submission.uuid))

Log the creation of a score.

Args:
score (Score): The score model.

Returns:
None


def sg_float(tensor, opt):
    r
    return tf.cast(tensor, tf.sg_floatx, name=opt.name)

r"""Casts a tensor to floatx.

See `tf.cast()` in tensorflow.

Args:
tensor: A `Tensor` or `SparseTensor` (automatically given by chain).
opt:
name : If provided, it replaces current tensor's name

Returns:
A `Tensor` or `SparseTensor` with same shape as `tensor`.


def usufyToXlsExport(d, fPath):

    from pyexcel_xls import get_data
    try:

        oldData = {"OSRFramework": get_data(fPath) }
    except:

        oldData = {"OSRFramework":[]}

    tabularData = _generateTabularData(d, oldData)
    from pyexcel_xls import save_data

    save_data(fPath, tabularData)

Workaround to export to a .xls file.

Args:
-----
d: Data to export.
fPath: File path for the output file.


def build_chain(self, source, chain):

        for group in WalkByGroup(source, chain.order+1):
            pre = group[:-1]
            res = group[-1]

            if pre not in chain.content:
                chain.content[pre] = {res: 1}
            else:
                if res not in chain.content[pre]:
                    chain.content[pre][res] = 1
                else:
                    chain.content[pre][res] += 1

        chain.decache()

Build markov chain from source on top of existin chain

Args:
source: iterable which will be used to build chain
chain: MarkovChain in currently loaded shelve file that
will be extended by source


def list(self, accountID, **kwargs):
    request = Request('GET', '/v3/accounts/{accountID}/orders')
    request.set_path_param('accountID', accountID)
    request.set_param('ids', kwargs.get('ids'))
    request.set_param('state', kwargs.get('state'))
    request.set_param('instrument', kwargs.get('instrument'))
    request.set_param('count', kwargs.get('count'))
    request.set_param('beforeID', kwargs.get('beforeID'))
    response = self.ctx.request(request)
    if (response.content_type is None):
        return response
    if (not response.content_type.startswith('application/json')):
        return response
    jbody = json.loads(response.raw_body)
    parsed_body = {}
    if (str(response.status) == '200'):
        if (jbody.get('orders') is not None):
            parsed_body['orders'] = [self.ctx.order.Order.from_dict(d, self.ctx) for d in jbody.get('orders')]
        if (jbody.get('lastTransactionID') is not None):
            parsed_body['lastTransactionID'] = jbody.get('lastTransactionID')
    elif (str(response.status) == '400'):
        if (jbody.get('errorCode') is not None):
            parsed_body['errorCode'] = jbody.get('errorCode')
        if (jbody.get('errorMessage') is not None):
            parsed_body['errorMessage'] = jbody.get('errorMessage')
    elif (str(response.status) == '404'):
        if (jbody.get('errorCode') is not None):
            parsed_body['errorCode'] = jbody.get('errorCode')
        if (jbody.get('errorMessage') is not None):
            parsed_body['errorMessage'] = jbody.get('errorMessage')
    elif (str(response.status) == '405'):
        if (jbody.get('errorCode') is not None):
            parsed_body['errorCode'] = jbody.get('errorCode')
        if (jbody.get('errorMessage') is not None):
            parsed_body['errorMessage'] = jbody.get('errorMessage')
    else:
        parsed_body = jbody
    response.body = parsed_body
    return response

Get a list of Orders for an Account

Args:
accountID:
Account Identifier
ids:
List of Order IDs to retrieve
state:
The state to filter the requested Orders by
instrument:
The instrument to filter the requested orders by
count:
The maximum number of Orders to return
beforeID:
The maximum Order ID to return. If not provided the most recent
Orders in the Account are returned

Returns:
v20.response.Response containing the results from submitting the
request


def create(self, name, domain_name):

        name = self.wrap(self.resource.create(dict(name=name,
                                                   domain_name=domain_name)))

        self.add(name)
        return name

Register a url (e.g. wallet.gem.co) for

Args:
name (str): human-readable wallet name (e.g. wallet)
domain_name (str): the domain name to create subdomain on (e.g. gem.co)
this domain must already be registered with Gem

Returns: The new round.NetkiName


def console_print_rect_ex(con: tcod.console.Console, x: int, y: int, w: int, h: int, flag: int, alignment: int, fmt: str) -> int:
    return int(lib.TCOD_console_printf_rect_ex(_console(con), x, y, w, h, flag, alignment, _fmt(fmt)))

Print a string constrained to a rectangle with blend and alignment.

Returns:
int: The number of lines of text once word-wrapped.

.. deprecated:: 8.5
Use :any:`Console.print_rect` instead.


def ParseFileObject(self, parser_mediator, file_object):
    encoding = (self._ENCODING or parser_mediator.codepage)
    text_file_object = text_file.TextFile(file_object, encoding=encoding)
    if (not self._ParseAndValidateRecord(parser_mediator, text_file_object)):
        raise errors.UnableToParseFile('Unable to parse as Opera global_history.dat.')
    while self._ParseRecord(parser_mediator, text_file_object):
        pass

Parses an Opera global history file-like object.

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

Raises:
UnableToParseFile: when the file cannot be parsed.


def convert_strain_to_deformation(strain, shape="upper"):

    strain = SquareTensor(strain)
    ftdotf = 2*strain + np.eye(3)
    if shape == "upper":
        result = scipy.linalg.cholesky(ftdotf)
    elif shape == "symmetric":
        result = scipy.linalg.sqrtm(ftdotf)
    else:
        raise ValueError("shape must be \"upper\" or \"symmetric\"")
    return Deformation(result)

This function converts a strain to a deformation gradient that will
produce that strain.  Supports three methods:

Args:
strain (3x3 array-like): strain matrix
shape: (string): method for determining deformation, supports
"upper" produces an upper triangular defo
"lower" produces a lower triangular defo
"symmetric" produces a symmetric defo


def GetFileSystemReferenceCount(self, path_spec):
    identifier = self._GetFileSystemCacheIdentifier(path_spec)
    cache_value = self._file_system_cache.GetCacheValue(identifier)
    if (not cache_value):
        return None
    return cache_value.reference_count

Retrieves the reference count of a cached file system object.

Args:
path_spec (PathSpec): path specification.

Returns:
int: reference count or None if there is no file system object for
the corresponding path specification cached.


def _randomUniformAvoidAnchors(self, low, high, anchors, radius, num_samples):
    self.assertTrue(low < high)
    self.assertTrue(radius >= 0)
    num_anchors = len(anchors)
    self.assertTrue(2 * radius * num_anchors < 0.5 * (high - low))
    anchors = np.reshape(anchors, num_anchors)
    samples = []
    while len(samples) < num_samples:
        sample = np.random.uniform(low, high)
        if np.all(np.fabs(sample - anchors) > radius):
            samples.append(sample)
    return samples

Generate samples that are far enough from a set of anchor points.

We generate uniform samples in [low, high], then reject those that are less
than radius away from any point in anchors. We stop after we have accepted
num_samples samples.

Args:
low: The lower end of the interval.
high: The upper end of the interval.
anchors: A list of length num_crops with anchor points to avoid.
radius: Distance threshold for the samples from the anchors.
num_samples: How many samples to produce.

Returns:
samples: A list of length num_samples with the accepted samples.


def nic_v1(msg, NICs):

    if typecode(msg) < 5 or typecode(msg) > 22:
        raise RuntimeError(
            "%s: Not a surface position message (5<TC<8), \
            airborne position message (8<TC<19), \
            or airborne position with GNSS height (20<TC<22)" % msg
        )

    tc = typecode(msg)
    NIC = uncertainty.TC_NICv1_lookup[tc]

    if isinstance(NIC, dict):
        NIC = NIC[NICs]

    try:
        Rc = uncertainty.NICv1[NIC][NICs]['Rc']
        VPL = uncertainty.NICv1[NIC][NICs]['VPL']
    except KeyError:
        Rc, VPL = uncertainty.NA, uncertainty.NA

    return Rc, VPL

Calculate NIC, navigation integrity category, for ADS-B version 1

Args:
msg (string): 28 bytes hexadecimal message string
NICs (int or string): NIC supplement

Returns:
int or string: Horizontal Radius of Containment
int or string: Vertical Protection Limit


def run_server(cls, args=None, **kwargs):
    if (args is None):
        args = sys.argv[1:]
    args = ([cls.__name__] + list(args))
    green_mode = getattr(cls, 'green_mode', None)
    kwargs.setdefault('green_mode', green_mode)
    return run((cls,), args, **kwargs)

Run the class as a device server.
It is based on the tango.server.run method.

The difference is that the device class
and server name are automatically given.

Args:
args (iterable): args as given in the tango.server.run method
without the server name. If None, the sys.argv
list is used
kwargs: the other keywords argument are as given
in the tango.server.run method.


def is_legal_subject(self, c: OntologyClass) -> bool:

        domains = self.included_domains()
        return c and (not domains or c in domains or c.super_classes_closure() & domains)

is_legal_subject(c) = true if
- c in included_domains(self) or
- super_classes_closure(c) intersection included_domains(self) is not empty

There is no need to check the included_domains(super_properties_closure(self)) because
included_domains(super_properties_closure(self)) is subset of super_classes_closure(included_domains(self))

Args:
c:

Returns:


def interm_fluent_ordering(self) -> List[str]:
    interm_fluents = self.intermediate_fluents.values()
    key = (lambda pvar: (pvar.level, pvar.name))
    return [str(pvar) for pvar in sorted(interm_fluents, key=key)]

The list of intermediate-fluent names in canonical order.

Returns:
List[str]: A list of fluent names.


async def download_file(context, url, abs_filename, session=None, chunk_size=128):

    session = session or context.session
    loggable_url = get_loggable_url(url)
    log.info("Downloading %s", loggable_url)
    parent_dir = os.path.dirname(abs_filename)
    async with session.get(url) as resp:
        if resp.status == 404:
            await _log_download_error(resp, "404 downloading %(url)s: %(status)s; body=%(body)s")
            raise Download404("{} status {}!".format(loggable_url, resp.status))
        elif resp.status != 200:
            await _log_download_error(resp, "Failed to download %(url)s: %(status)s; body=%(body)s")
            raise DownloadError("{} status {} is not 200!".format(loggable_url, resp.status))
        makedirs(parent_dir)
        with open(abs_filename, "wb") as fd:
            while True:
                chunk = await resp.content.read(chunk_size)
                if not chunk:
                    break
                fd.write(chunk)
    log.info("Done")

Download a file, async.

Args:
context (scriptworker.context.Context): the scriptworker context.
url (str): the url to download
abs_filename (str): the path to download to
session (aiohttp.ClientSession, optional): the session to use.  If
None, use context.session.  Defaults to None.
chunk_size (int, optional): the chunk size to read from the response
at a time.  Default is 128.


def get_band_structure_from_vasp_multiple_branches(dir_name, efermi=None, projections=False):
    if os.path.exists(os.path.join(dir_name, 'branch_0')):
        branch_dir_names = [os.path.abspath(d) for d in glob.glob('{i}/branch_*'.format(i=dir_name)) if os.path.isdir(d)]
        sort_by = (lambda x: int(x.split('_')[(- 1)]))
        sorted_branch_dir_names = sorted(branch_dir_names, key=sort_by)
        branches = []
        for dir_name in sorted_branch_dir_names:
            xml_file = os.path.join(dir_name, 'vasprun.xml')
            if os.path.exists(xml_file):
                run = Vasprun(xml_file, parse_projected_eigen=projections)
                branches.append(run.get_band_structure(efermi=efermi))
            else:
                warnings.warn('Skipping {}. Unable to find {}'.format(d=dir_name, f=xml_file))
        return get_reconstructed_band_structure(branches, efermi)
    else:
        xml_file = os.path.join(dir_name, 'vasprun.xml')
        if os.path.exists(xml_file):
            return Vasprun(xml_file, parse_projected_eigen=projections).get_band_structure(kpoints_filename=None, efermi=efermi)
        else:
            return None

This method is used to get band structure info from a VASP directory. It
takes into account that the run can be divided in several branches named
"branch_x". If the run has not been divided in branches the method will
turn to parsing vasprun.xml directly.

The method returns None is there"s a parsing error

Args:
dir_name: Directory containing all bandstructure runs.
efermi: Efermi for bandstructure.
projections: True if you want to get the data on site projections if
any. Note that this is sometimes very large

Returns:
A BandStructure Object


def get_extended_attention_mask(self, attention_mask: torch.Tensor, input_shape: Tuple[int], device: torch.device, has_query: bool=False) -> torch.Tensor:
    if attention_mask.dim() == 3:
        extended_attention_mask = attention_mask[:, None, :, :]
    elif attention_mask.dim() == 2:
        extended_attention_mask = attention_mask[:, None, None, :]
    else:
        raise ValueError('Wrong shape for input_ids (shape {}) or attention_mask (shape {})'.format(input_shape, attention_mask.shape))
    extended_attention_mask = extended_attention_mask.to(dtype=self.dtype)
    extended_attention_mask = (1.0 - extended_attention_mask) * -10000.0
    return extended_attention_mask

Makes broadcastable attention and causal masks so that future and masked tokens are ignored.

Arguments:
attention_mask (`torch.Tensor`):
Mask with ones indicating tokens to attend to, zeros for tokens to ignore.
input_shape (`Tuple[int]`):
The shape of the input to the model.
device (`torch.device`):
The device of the input to the model.

Returns:
`torch.Tensor` The extended attention mask, with a the same dtype as `attention_mask.dtype`.


def end_at(self, document_fields):
    query = query_mod.Query(self)
    return query.end_at(document_fields)

End query at a cursor with this collection as parent.

See
:meth:`~.firestore_v1beta1.query.Query.end_at` for
more information on this method.

Args:
document_fields (Union[~.firestore_v1beta1.\
document.DocumentSnapshot, dict, list, tuple]): a document
snapshot or a dictionary/list/tuple of fields representing a
query results cursor. A cursor is a collection of values that
represent a position in a query result set.

Returns:
~.firestore_v1beta1.query.Query: A query with cursor.


def get_base_branch():
    base_branch = git.guess_base_branch()
    if (base_branch is None):
        log.info("Can't guess the base branch, you have to pick one yourself:")
        base_branch = choose_branch()
    return base_branch

Return the base branch for the current branch.

This function will first try to guess the base branch and if it can't it
will let the user choose the branch from the list of all local branches.

Returns:
str: The name of the branch the current branch is based on.


def __init__(self, samplerate, nframes, wavpath):

        imp = minidom.getDOMImplementation()
        dt = imp.createDocumentType('sonic-visualiser', None, None)
        self.doc = doc = imp.createDocument(None,'sv', dt)
        root = doc.documentElement
        self.__dname = dict()

        self.data = root.appendChild(doc.createElement('data'))
        self.display = root.appendChild(doc.createElement('display'))
        window = self.display.appendChild(doc.createElement('window'))
        self.defwidth = 900
        window.setAttribute('width', str(self.defwidth))
        window.setAttribute('height', str(856))
        self.selections = root.appendChild(doc.createElement('selections'))

        self.nbdata = 0

        self.samplerate =  samplerate
        self.nframes = nframes

        self.__setMainWaveModel(wavpath)

Init a sonic visualiser environment structure based on
the attributes of the main audio file

Args:
samplerate(int): media sample rate (Hz)
nframes(int): number of samples
wavpath(str): Full path to the wav file used in the current environment


def _page_to_text(page):

  start_pos = page.find(u"<text")
  assert start_pos != -1
  end_tag_pos = page.find(u">", start_pos)
  assert end_tag_pos != -1
  end_tag_pos += len(u">")
  end_pos = page.find(u"</text>")
  if end_pos == -1:
    return u""
  return page[end_tag_pos:end_pos]

Extract the text from a page.

Args:
page: a unicode string
Returns:
a unicode string


def _CreateProcessingConfiguration(self, knowledge_base):
    configuration = configurations.ProcessingConfiguration()
    configuration.artifact_filters = self._artifact_filters
    configuration.credentials = self._credential_configurations
    configuration.debug_output = self._debug_mode
    configuration.event_extraction.text_prepend = self._text_prepend
    configuration.extraction.hasher_file_size_limit = self._hasher_file_size_limit
    configuration.extraction.hasher_names_string = self._hasher_names_string
    configuration.extraction.process_archives = self._process_archives
    configuration.extraction.process_compressed_streams = self._process_compressed_streams
    configuration.extraction.yara_rules_string = self._yara_rules_string
    configuration.filter_file = self._filter_file
    configuration.input_source.mount_path = self._mount_path
    configuration.log_filename = self._log_file
    configuration.parser_filter_expression = self._parser_filter_expression
    configuration.preferred_year = self._preferred_year
    configuration.profiling.directory = self._profiling_directory
    configuration.profiling.sample_rate = self._profiling_sample_rate
    configuration.profiling.profilers = self._profilers
    configuration.temporary_directory = self._temporary_directory
    if (not configuration.parser_filter_expression):
        operating_system = knowledge_base.GetValue('operating_system')
        operating_system_product = knowledge_base.GetValue('operating_system_product')
        operating_system_version = knowledge_base.GetValue('operating_system_version')
        preset_definitions = parsers_manager.ParsersManager.GetPresetsForOperatingSystem(operating_system, operating_system_product, operating_system_version)
        if preset_definitions:
            preset_names = [preset_definition.name for preset_definition in preset_definitions]
            filter_expression = ','.join(preset_names)
            logger.info('Parser filter expression set to: {0:s}'.format(filter_expression))
            configuration.parser_filter_expression = filter_expression
    return configuration

Creates a processing configuration.

Args:
knowledge_base (KnowledgeBase): contains information from the source
data needed for parsing.

Returns:
ProcessingConfiguration: processing configuration.

Raises:
BadConfigOption: if more than 1 parser and parser plugins preset
was found for the detected operating system.


def plot_timestream(array, kidid, xtick='time', scantypes=None, ax=None, **kwargs):
    if (ax is None):
        ax = plt.gca()
    index = np.where((array.kidid == kidid))[0]
    if (len(index) == 0):
        raise KeyError('Such a kidid does not exist.')
    index = int(index)
    if (scantypes is None):
        if (xtick == 'time'):
            ax.plot(array.time, array[(:, index)], label='ALL', **kwargs)
        elif (xtick == 'index'):
            ax.plot(np.ogrid[:len(array.time)], array[(:, index)], label='ALL', **kwargs)
    else:
        for scantype in scantypes:
            if (xtick == 'time'):
                ax.plot(array.time[(array.scantype == scantype)], array[(:, index)][(array.scantype == scantype)], label=scantype, **kwargs)
            elif (xtick == 'index'):
                ax.plot(np.ogrid[:len(array.time[(array.scantype == scantype)])], array[(:, index)][(array.scantype == scantype)], label=scantype, **kwargs)
    ax.set_xlabel('{}'.format(xtick))
    ax.set_ylabel(str(array.datatype.values))
    ax.legend()
    kidtpdict = {0: 'wideband', 1: 'filter', 2: 'blind'}
    try:
        kidtp = kidtpdict[int(array.kidtp[index])]
    except KeyError:
        kidtp = 'filter'
    ax.set_title('ch
    logger.info('timestream data (ch={}) has been plotted.'.format(kidid))

Plot timestream data.

Args:
array (xarray.DataArray): Array which the timestream data are included.
kidid (int): Kidid.
xtick (str): Type of x axis.
'time': Time.
'index': Time index.
scantypes (list): Scantypes. If None, all scantypes are used.
ax (matplotlib.axes): Axis you want to plot on.
kwargs (optional): Plot options passed to ax.plot().


def sendline(self, text):

        logger.debug("Sending input '{0}' to '{1}'".format(text, self.name))
        try:
            return self._spawn.sendline(text)
        except pexpect.exceptions.EOF as e:
            logger.debug("Raising termination exception.")
            raise TerminationException(instance=self, real_exception=e, output=self.get_output())
        except pexpect.exceptions.TIMEOUT as e:
            logger.debug("Raising timeout exception.")
            raise TimeoutException(instance=self, real_exception=e, output=self.get_output())
        except Exception as e:
            logger.debug("Sending input failed: " + str(e))
            raise NestedException(instance=self, real_exception=e, output=self.get_output())

Sends an input line to the running program, including os.linesep.

Args:
text (str): The input text to be send.

Raises:
TerminationException: The program terminated before / while / after sending the input.
NestedException: An internal problem occured while waiting for the output.


def _simplify_non_context_field_binary_composition(expression):
    if any((isinstance(expression.left, ContextField), isinstance(expression.right, ContextField))):
        raise AssertionError(u'Received a BinaryComposition {} with a ContextField operand. This should never happen.'.format(expression))
    if (expression.operator == u'||'):
        if ((expression.left == TrueLiteral) or (expression.right == TrueLiteral)):
            return TrueLiteral
        else:
            return expression
    elif (expression.operator == u'&&'):
        if (expression.left == TrueLiteral):
            return expression.right
        if (expression.right == TrueLiteral):
            return expression.left
        else:
            return expression
    else:
        return expression

Return a simplified BinaryComposition if either operand is a TrueLiteral.

Args:
expression: BinaryComposition without any ContextField operand(s)

Returns:
simplified expression if the given expression is a disjunction/conjunction
and one of it's operands is a TrueLiteral,
and the original expression otherwise


def __init__(self, cell):
    self._cell = cell

Creates a new CounterCell.

Args:
cell: A c pointer of TFE_MonitoringCounterCell.


def generate_key_pair(secret=None):

    if secret:
        keypair_raw = ed25519_generate_key_pair_from_secret(secret)
        return CryptoKeypair(
            *(k.decode() for k in keypair_raw))
    else:
        return generate_keypair()

Generates a cryptographic key pair.
Args:
secret (:class:`string`): A secret that serves as a seed
Returns:
:class:`~bigchaindb.common.crypto.CryptoKeypair`: A
:obj:`collections.namedtuple` with named fields
:attr:`~bigchaindb.common.crypto.CryptoKeypair.private_key` and
:attr:`~bigchaindb.common.crypto.CryptoKeypair.public_key`.


def from_json(cls, data):

        assert 'header' in data, 'Required keyword "header" is missing!'
        assert 'values' in data, 'Required keyword "values" is missing!'
        assert 'datetimes' in data, 'Required keyword "datetimes" is missing!'
        coll = cls(Header.from_json(data['header']), data['values'], data['datetimes'])
        if 'validated_a_period' in data:
            coll._validated_a_period = data['validated_a_period']
        return coll

Create a Data Collection from a dictionary.

Args:
{
"header": A Ladybug Header,
"values": An array of values,
"datetimes": An array of datetimes,
"validated_a_period": Boolean for whether header analysis_period is valid
}


def set_time(self, value: float):

        if value < 0:
            value = 0

        self.controller.row = self.rps * value

Set the current time jumping in the timeline.

Args:
value (float): The new time


def all_to_all(x, concat_dimension, split_dimension, split_count, group_assignment=None, name=None):
    if group_assignment is None:
        group_assignment = _create_default_group_assignment()
    return gen_tpu_ops.all_to_all(x, group_assignment, concat_dimension=concat_dimension, split_dimension=split_dimension, split_count=split_count, name=name)

Exchange data across TPU replicas.

Args:
x: The local tensor.
concat_dimension: The dimension number to concatenate.
split_dimension: The dimension number to split.
split_count: The number of splits, this number must equal to the sub-group
size(group_assignment.get_shape()[1])
group_assignment: Optional 2d int32 lists with shape [num_groups,
num_replicas_per_group]. `group_assignment[i]` represents the replica ids
in the ith subgroup.
name: Optional op name.

Returns:
A `Tensor` which is concatenated by data from different replicas.


def query_source_file_line(self, file_path, lineno):
    if not self._source_files:
        raise ValueError('This debug server has not received any source file contents yet.')
    for source_files in self._source_files:
        for source_file_proto in source_files.source_files:
            if source_file_proto.file_path == file_path:
                return source_file_proto.lines[lineno - 1]
    raise ValueError('Source file at path %s has not been received by the debug server', file_path)

Query the content of a given line in a source file.

Args:
file_path: Path to the source file.
lineno: Line number as an `int`.

Returns:
Content of the line as a string.

Raises:
ValueError: If no source file is found at the given file_path.


def __init__(self, stream):

        super(BinaryReader, self).__init__()
        self.stream = stream

Create an instance.

Args:
stream (BytesIO): a stream to operate on. i.e. a neo.IO.MemoryStream or raw BytesIO.


def inspect_swarm(self):
    url = self._url('/swarm')
    return self._result(self._get(url), True)

Retrieve low-level information about the current swarm.

Returns:
A dictionary containing data about the swarm.

Raises:
:py:class:`docker.errors.APIError`
If the server returns an error.


def _DropCommonSuffixes(filename):
    for suffix in itertools.chain((('%s.%s' % (test_suffix.lstrip('_'), ext)) for (test_suffix, ext) in itertools.product(_test_suffixes, GetNonHeaderExtensions())), (('%s.%s' % (suffix, ext)) for (suffix, ext) in itertools.product(['inl', 'imp', 'internal'], GetHeaderExtensions()))):
        if (filename.endswith(suffix) and (len(filename) > len(suffix)) and (filename[((- len(suffix)) - 1)] in ('-', '_'))):
            return filename[:((- len(suffix)) - 1)]
    return os.path.splitext(filename)[0]

Drops common suffixes like _test.cc or -inl.h from filename.

For example:
>>> _DropCommonSuffixes('foo/foo-inl.h')
'foo/foo'
>>> _DropCommonSuffixes('foo/bar/foo.cc')
'foo/bar/foo'
>>> _DropCommonSuffixes('foo/foo_internal.h')
'foo/foo'
>>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
'foo/foo_unusualinternal'

Args:
filename: The input filename.

Returns:
The filename with the common suffix removed.


def GetValueByName(self, name):

    if not self._registry_key and self._registry:
      self._GetKeyFromRegistry()

    if not self._registry_key:
      return None

    return self._registry_key.GetValueByName(name)

Retrieves a value by name.

Args:
name (str): name of the value or an empty string for the default value.

Returns:
WinRegistryValue: Windows Registry value or None if not found.


def find_one(self, collection, query):
    obj = getattr(self.db, collection)
    result = obj.find_one(query)
    return result

Search a collection for the query provided and return one result. Just
a raw interface to mongo to do any query you want.

Args:
collection: The db collection. See main class documentation.
query: A mongo find query.
Returns:
pymongo Cursor object with the results.


def sequence_equal(self, second_iterable, equality_comparer=operator.eq):
    if self.closed():
        raise ValueError('Attempt to call to_tuple() on a closed Queryable.')
    if (not is_iterable(second_iterable)):
        raise TypeError('Cannot compute sequence_equal() with second_iterable of non-iterable {type}'.format(type=str(type(second_iterable))[7:(- 1)]))
    if (not is_callable(equality_comparer)):
        raise TypeError('aggregate() parameter equality_comparer={equality_comparer} is not callable'.format(equality_comparer=repr(equality_comparer)))
    try:
        if (len(self._iterable) != len(second_iterable)):
            return False
    except TypeError:
        pass
    sentinel = object()
    for (first, second) in izip_longest(self, second_iterable, fillvalue=sentinel):
        if ((first is sentinel) or (second is sentinel)):
            return False
        if (not equality_comparer(first, second)):
            return False
    return True

Determine whether two sequences are equal by elementwise comparison.

Sequence equality is defined as the two sequences being equal length
and corresponding elements being equal as determined by the equality
comparer.

Note: This method uses immediate execution.

Args:
second_iterable: The sequence which will be compared with the
source sequence.

equality_comparer: An optional binary predicate function which is
used to compare corresponding elements. Should return True if
the elements are equal, otherwise False.  The default equality
comparer is operator.eq which calls __eq__ on elements of the
source sequence with the corresponding element of the second
sequence as a parameter.

Returns:
True if the sequences are equal, otherwise False.

Raises:
ValueError: If the Queryable is closed.
TypeError: If second_iterable is not in fact iterable.
TypeError: If equality_comparer is not callable.


def codemirror_instance(config_name, varname, element_id, assets=True):
    output = io.StringIO()
    manifesto = CodemirrorAssetTagRender()
    manifesto.register(config_name)
    if assets:
        output.write(manifesto.css_html())
        output.write(manifesto.js_html())
    html = manifesto.codemirror_html(config_name, varname, element_id)
    output.write(html)
    content = output.getvalue()
    output.close()
    return mark_safe(content)

Return HTML to init a CodeMirror instance for an element.

This will output the whole HTML needed to initialize a CodeMirror instance
with needed assets loading. Assets can be omitted with the ``assets``
option.

Example:
::

{% load djangocodemirror_tags %}
{% codemirror_instance 'a-config-name' 'foo_codemirror' 'foo' %}

Arguments:
config_name (string): A registred config name.
varname (string): A Javascript variable name.
element_id (string): An HTML element identifier (without
leading ``#``) to attach to a CodeMirror instance.

Keyword Arguments:
assets (Bool): Adds needed assets before the HTML if ``True``, else
only CodeMirror instance will be outputed. Default value is
``True``.

Returns:
string: HTML.


def _remove_string_from_commastring(self, field, string):
    commastring = self.data.get(field, '')
    if (string in commastring):
        self.data[field] = commastring.replace(string, '')
        return True
    return False

Remove a string from a comma separated list of strings

Args:
field (str): Field containing comma separated list
string (str): String to remove

Returns:
bool: True if string removed or False if not


def warning_handler(self, handler):

        if not self.opened():
            handler = handler or util.noop
            self._warning_handler = enums.JLinkFunctions.LOG_PROTOTYPE(handler)
            self._dll.JLINKARM_SetWarnOutHandler(self._warning_handler)

Setter for the warning handler function.

If the DLL is open, this function is a no-op, so it should be called
prior to calling ``open()``.

Args:
self (JLink): the ``JLink`` instance
handler (function): function to call on warning messages

Returns:
``None``


def selected(self):
    query_results = self.map((lambda el: el.is_selected()), 'selected').results
    if query_results:
        return all(query_results)
    return False

Check whether all the matched elements are selected.

Returns:
bool


def list_vmss_vm_instance_view_pg(access_token, subscription_id, resource_group, vmss_name, link=None):
    if (link is None):
        endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/resourceGroups/', resource_group, '/providers/Microsoft.Compute/virtualMachineScaleSets/', vmss_name, '/virtualMachines?$expand=instanceView&$select=instanceView', '&api-version=', COMP_API])
    else:
        endpoint = link
    return do_get(endpoint, access_token)

Gets one page of a paginated list of scale set VM instance views.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.
resource_group (str): Azure resource group name.
vmss_name (str): Name of the virtual machine scale set.
link (str): Optional link to URI to get list (as part of a paginated API query).

Returns:
HTTP response. JSON body of list of VM instance views.


def submit(self, func, *args, executors='all', fn_hash=None, cache=False, **kwargs):
    if self.cleanup_called:
        raise ValueError('Cannot submit to a DFK that has been cleaned up')
    task_id = self.task_count
    self.task_count += 1
    if (isinstance(executors, str) and (executors.lower() == 'all')):
        choices = list((e for e in self.executors if (e != 'data_manager')))
    elif isinstance(executors, list):
        choices = executors
    executor = random.choice(choices)
    (args, kwargs) = self._add_input_deps(executor, args, kwargs)
    task_def = {'depends': None, 'executor': executor, 'func': func, 'func_name': func.__name__, 'args': args, 'kwargs': kwargs, 'fn_hash': fn_hash, 'memoize': cache, 'callback': None, 'exec_fu': None, 'checkpoint': None, 'fail_count': 0, 'fail_history': [], 'env': None, 'status': States.unsched, 'id': task_id, 'time_submitted': None, 'time_returned': None, 'app_fu': None}
    if (task_id in self.tasks):
        raise DuplicateTaskError('internal consistency error: Task {0} already exists in task list'.format(task_id))
    else:
        self.tasks[task_id] = task_def
    (dep_cnt, depends) = self._gather_all_deps(args, kwargs)
    self.tasks[task_id]['depends'] = depends
    task_stdout = kwargs.get('stdout')
    task_stderr = kwargs.get('stderr')
    logger.info('Task {} submitted for App {}, waiting on tasks {}'.format(task_id, task_def['func_name'], [fu.tid for fu in depends]))
    self.tasks[task_id]['task_launch_lock'] = threading.Lock()
    app_fu = AppFuture(tid=task_id, stdout=task_stdout, stderr=task_stderr)
    self.tasks[task_id]['app_fu'] = app_fu
    app_fu.add_done_callback(partial(self.handle_app_update, task_id))
    self.tasks[task_id]['status'] = States.pending
    logger.debug('Task {} set to pending state with AppFuture: {}'.format(task_id, task_def['app_fu']))
    for d in depends:

        def callback_adapter(dep_fut):
            self.launch_if_ready(task_id)
        try:
            d.add_done_callback(callback_adapter)
        except Exception as e:
            logger.error('add_done_callback got an exception {} which will be ignored'.format(e))
    self.launch_if_ready(task_id)
    return task_def['app_fu']

Add task to the dataflow system.

If the app task has the executors attributes not set (default=='all')
the task will be launched on a randomly selected executor from the
list of executors. If the app task specifies a particular set of
executors, it will be targeted at the specified executors.

>>> IF all deps are met:
>>>   send to the runnable queue and launch the task
>>> ELSE:
>>>   post the task in the pending queue

Args:
- func : A function object
- *args : Args to the function

KWargs :
- executors (list or string) : List of executors this call could go to.
Default='all'
- fn_hash (Str) : Hash of the function and inputs
Default=None
- cache (Bool) : To enable memoization or not
- kwargs (dict) : Rest of the kwargs to the fn passed as dict.

Returns:
(AppFuture) [DataFutures,]


def install_package(tar_url, folder, md5_url='{tar_url}.md5',
                    on_download=lambda: None, on_complete=lambda: None):

    data_file = join(folder, basename(tar_url))

    md5_url = md5_url.format(tar_url=tar_url)
    try:
        remote_md5 = download(md5_url).decode('utf-8').split(' ')[0]
    except (UnicodeDecodeError, URLError):
        raise ValueError('Invalid MD5 url: ' + md5_url)
    if remote_md5 != calc_md5(data_file):
        on_download()
        if isfile(data_file):
            try:
                with tarfile.open(data_file) as tar:
                    for i in reversed(list(tar)):
                        try:
                            os.remove(join(folder, i.path))
                        except OSError:
                            pass
            except (OSError, EOFError):
                pass

        download_extract_tar(tar_url, folder, data_file)
        on_complete()
        if remote_md5 != calc_md5(data_file):
            raise ValueError('MD5 url does not match tar: ' + md5_url)
        return True
    return False

Install or update a tar package that has an md5

Args:
tar_url (str): URL of package to download
folder (str): Location to extract tar. Will be created if doesn't exist
md5_url (str): URL of md5 to use to check for updates
on_download (Callable): Function that gets called when downloading a new update
on_complete (Callable): Function that gets called when a new download is complete

Returns:
bool: Whether the package was updated


def get_reverse_dns(ip_address, cache=None, nameservers=None, timeout=2.0):

    hostname = None
    try:
        address = dns.reversename.from_address(ip_address)
        hostname = query_dns(address, "PTR", cache=cache,
                             nameservers=nameservers,
                             timeout=timeout)[0]

    except dns.exception.DNSException:
        pass

    return hostname

Resolves an IP address to a hostname using a reverse DNS query

Args:
ip_address (str): The IP address to resolve
cache (ExpiringDict): Cache storage
nameservers (list): A list of one or more nameservers to use
(Cloudflare's public DNS resolvers by default)
timeout (float): Sets the DNS query timeout in seconds

Returns:
str: The reverse DNS hostname (if any)


def delete_object(self, ref, delete_arguments=None):

        opts = self._get_request_options()
        if not isinstance(delete_arguments, dict):
            delete_arguments = {}
        url = self._construct_url(ref, query_params=delete_arguments)
        self._log_request('delete', url, opts)
        r = self.session.delete(url, **opts)

        self._validate_authorized(r)

        if r.status_code != requests.codes.ok:
            self._check_service_availability('delete', r, ref)

            raise ib_ex.InfobloxCannotDeleteObject(
                response=jsonutils.loads(r.content),
                ref=ref,
                content=r.content,
                code=r.status_code)

        return self._parse_reply(r)

Remove an Infoblox object

Args:
ref               (str): Object reference
delete_arguments (dict): Extra delete arguments
Returns:
The object reference of the removed object
Raises:
InfobloxException


def pcolls_from_streaming_cache(user_pipeline: beam.Pipeline, query_pipeline: beam.Pipeline, name_to_pcoll: Dict[str, beam.PCollection]) -> Dict[str, beam.PCollection]:

    def exception_handler(e):
        _LOGGER.error(str(e))
        return True
    cache_manager = ie.current_env().get_cache_manager(user_pipeline, create_if_absent=True)
    test_stream_service = ie.current_env().get_test_stream_service_controller(user_pipeline)
    if not test_stream_service:
        test_stream_service = TestStreamServiceController(cache_manager, exception_handler=exception_handler)
        test_stream_service.start()
        ie.current_env().set_test_stream_service_controller(user_pipeline, test_stream_service)
    tag_to_name = {}
    for name, pcoll in name_to_pcoll.items():
        key = CacheKey.from_pcoll(name, pcoll).to_str()
        tag_to_name[key] = name
    output_pcolls = query_pipeline | test_stream.TestStream(output_tags=set(tag_to_name.keys()), coder=cache_manager._default_pcoder, endpoint=test_stream_service.endpoint)
    sql_source = {}
    for tag, output in output_pcolls.items():
        name = tag_to_name[tag]
        output.element_type = name_to_pcoll[name].element_type
        sql_source[name] = output
    return sql_source

Reads PCollection cache through the TestStream.

Args:
user_pipeline: The beam.Pipeline object defined by the user in the
notebook.
query_pipeline: The beam.Pipeline object built by the magic to execute the
SQL query.
name_to_pcoll: PCollections with variable names used in the SQL query.

Returns:
A Dict[str, beam.PCollection], where each PCollection is tagged with
their PCollection variable names, read from the cache.

When the user_pipeline has unbounded sources, we force all cache reads to go
through the TestStream even if they are bounded sources.


def _validate_bool(value):
    if isinstance(value, six.text_type):
        if (value.strip().lower() == 'true'):
            value = True
        elif (value.strip().lower() == 'false'):
            value = False
        else:
            raise ValueError('"{}" must be a boolean ("True" or "False")'.format(value))
    if (not isinstance(value, bool)):
        raise ValueError('"{}" is not a boolean value.'.format(value))
    return value

Validate a setting is a bool.

Returns:
bool: The value as a boolean.

Raises:
ValueError: If the value can't be parsed as a bool string or isn't already bool.


def load_schema(schema_name, resolved=False):
    schema_data = ''
    with open(get_schema_path(schema_name, resolved)) as schema_fd:
        schema_data = json.loads(schema_fd.read())
    return schema_data

Load the given schema from wherever it's installed.

Args:
schema_name(str): Name of the schema to load, for example 'authors'.
resolved(bool): If True will return the resolved schema, that is with
all the $refs replaced by their targets.

Returns:
dict: the schema with the given name.


def _update(self, namespace, name, oldobj, newobj, is_class_namespace=False):
    try:
        notify_info2('Updating: ', oldobj)
        if (oldobj is newobj):
            return
        if (type(oldobj) is not type(newobj)):
            notify_error(('Type of: %s changed... Skipping.' % (oldobj,)))
            return
        if isinstance(newobj, types.FunctionType):
            self._update_function(oldobj, newobj)
            return
        if isinstance(newobj, types.MethodType):
            self._update_method(oldobj, newobj)
            return
        if isinstance(newobj, classmethod):
            self._update_classmethod(oldobj, newobj)
            return
        if isinstance(newobj, staticmethod):
            self._update_staticmethod(oldobj, newobj)
            return
        if hasattr(types, 'ClassType'):
            classtype = (types.ClassType, type)
        else:
            classtype = type
        if isinstance(newobj, classtype):
            self._update_class(oldobj, newobj)
            return
        if (hasattr(newobj, '__metaclass__') and hasattr(newobj, '__class__') and (newobj.__metaclass__ == newobj.__class__)):
            self._update_class(oldobj, newobj)
            return
        if (namespace is not None):
            if ((oldobj != newobj) and (str(oldobj) != str(newobj)) and (repr(oldobj) != repr(newobj))):
                xreload_old_new = None
                if is_class_namespace:
                    xreload_old_new = getattr(namespace, '__xreload_old_new__', None)
                    if (xreload_old_new is not None):
                        self.found_change = True
                        xreload_old_new(name, oldobj, newobj)
                elif ('__xreload_old_new__' in namespace):
                    xreload_old_new = namespace['__xreload_old_new__']
                    xreload_old_new(namespace, name, oldobj, newobj)
                    self.found_change = True
    except:
        notify_error(('Exception found when updating %s. Proceeding for other items.' % (name,)))
        pydev_log.exception()

Update oldobj, if possible in place, with newobj.

If oldobj is immutable, this simply returns newobj.

Args:
oldobj: the object to be updated
newobj: the object used as the source for the update


def CheckPrintf(filename, clean_lines, linenum, error):
    line = clean_lines.elided[linenum]
    match = Search('snprintf\\s*\\(([^,]*),\\s*([0-9]*)\\s*,', line)
    if (match and (match.group(2) != '0')):
        error(filename, linenum, 'runtime/printf', 3, ('If you can, use sizeof(%s) instead of %s as the 2nd arg to snprintf.' % (match.group(1), match.group(2))))
    if Search('\\bsprintf\\s*\\(', line):
        error(filename, linenum, 'runtime/printf', 5, 'Never use sprintf. Use snprintf instead.')
    match = Search('\\b(strcpy|strcat)\\s*\\(', line)
    if match:
        error(filename, linenum, 'runtime/printf', 4, ('Almost always, snprintf is better than %s' % match.group(1)))

Check for printf related issues.

Args:
filename: The name of the current file.
clean_lines: A CleansedLines instance containing the file.
linenum: The number of the line to check.
error: The function to call with any errors found.


def create_bq_dataset(project, dataset_base_name):
    client = bigquery.Client(project=project)
    unique_dataset_name = '%s%d%s' % (dataset_base_name, int(time.time()), secrets.token_hex(3))
    dataset_ref = client.dataset(unique_dataset_name, project=project)
    dataset = bigquery.Dataset(dataset_ref)
    client.create_dataset(dataset)
    return dataset_ref

Creates an empty BigQuery dataset.

Args:
project: Project to work in.
dataset_base_name: Prefix for dataset id.

Returns:
A ``google.cloud.bigquery.dataset.DatasetReference`` object pointing to the
new dataset.


def _update_triplestore(self, es_result, action_list, **kwargs):

        idx_time = XsdDatetime(datetime.datetime.utcnow())
        uri_keys = {}
        bnode_keys = {}
        for item in action_list:
            try:
                uri_keys[item['_id']] = item['_source']["uri"]
            except KeyError:
                bnode_keys[item['_id']] = item['_id']
        error_dict = {}
        error_bnodes = {}
        if es_result[1]:
            for result in es_result[1]:
                err_item = list(result.values())[0]
                try:
                    error_dict[uri_keys.pop(err_item['_id'])] = \
                            XsdString(err_item['error']['reason'])
                except KeyError:
                    error_bnodes[bnode_keys.pop(err_item['_id'])] = \
                            XsdString(err_item['error']['reason'])
        if uri_keys:
            sparql_good = .format(idx_time=idx_time.sparql,
                           subj_list="<%s>" % ">\n<".join(uri_keys.values()))
            self.tstore_conn.update_query(sparql_good)

        if not error_dict:
            return

        sparql_error = .format(subj_list="<%s>" % ">\n<".join(error_dict.keys()))
        self.tstore_conn.update_query(sparql_error)
        del sparql_error
        sparql_update = .format(
                    idx_time=idx_time.sparql,
                    error_list="\n".join(["(<%s> %s)" % (key, val.sparql)
                                          for key, val in error_dict.items()]))

        self.tstore_conn.update_query(sparql_update)
        del sparql_update

updates the triplestore with success of saves and failues of indexing

Args:
-----
es_result: the elasticsearch result list
action_list: list of elasticsearch action items that were indexed


def full_name(decl, with_defaults=True):
    if (None is decl):
        raise RuntimeError('Unable to generate full name for None object!')
    if with_defaults:
        if (not decl.cache.full_name):
            path = declaration_path(decl)
            if (path == ['']):
                decl.cache.full_name = ''
            else:
                decl.cache.full_name = full_name_from_declaration_path(path)
        return decl.cache.full_name
    else:
        if (not decl.cache.full_partial_name):
            path = partial_declaration_path(decl)
            if (path == ['']):
                decl.cache.full_partial_name = ''
            else:
                decl.cache.full_partial_name = full_name_from_declaration_path(path)
        return decl.cache.full_partial_name

Returns declaration full qualified name.

If `decl` belongs to anonymous namespace or class, the function will return
C++ illegal qualified name.

Args:
decl (declaration_t): declaration for which the full qualified name
should be calculated.

Returns:
list[(str | basestring)]: full name of the declaration.


def account_states(self, **kwargs):
    path = self._get_id_path('account_states')
    response = self._GET(path, kwargs)
    self._set_attrs_to_values(response)
    return response

This method lets users get the status of whether or not the movie has
been rated or added to their favourite or watch lists. A valid session
id is required.

Args:
session_id: see Authentication.

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


def __field_to_parameter_type_and_format(self, field):

    variant = field.variant
    if variant == messages.Variant.MESSAGE:
      raise TypeError('A message variant can\'t be used in a parameter.')

    custom_variant_map = {
        messages.Variant.DOUBLE: ('number', 'double'),
        messages.Variant.FLOAT: ('number', 'float'),
        messages.Variant.INT64: ('string', 'int64'),
        messages.Variant.SINT64: ('string', 'int64'),
        messages.Variant.UINT64: ('string', 'uint64'),
        messages.Variant.INT32: ('integer', 'int32'),
        messages.Variant.SINT32: ('integer', 'int32'),
        messages.Variant.UINT32: ('integer', 'uint32'),
        messages.Variant.BOOL: ('boolean', None),
        messages.Variant.STRING: ('string', None),
        messages.Variant.BYTES: ('string', 'byte'),
        messages.Variant.ENUM: ('string', None),
    }
    return custom_variant_map.get(variant) or (variant.name.lower(), None)

Converts the field variant type into a tuple describing the parameter.

Args:
field: An instance of a subclass of messages.Field.

Returns:
A tuple with the type and format of the field, respectively.

Raises:
TypeError: if the field variant is a message variant.


def parse_global_args(argv):
    parser = create_parser()
    args = parser.parse_args(argv)
    should_log = (args.include or args.exclude or (args.verbose > 0))
    verbosity = args.verbose
    root = logging.getLogger()
    if should_log:
        formatter = logging.Formatter('%(asctime)s.%(msecs)03d %(levelname).3s %(name)s %(message)s', '%y-%m-%d %H:%M:%S')
        if args.logfile:
            handler = logging.FileHandler(args.logfile)
        else:
            handler = logging.StreamHandler()
        handler.setFormatter(formatter)
        if (args.include and args.exclude):
            print('You cannot combine whitelisted (-i) and blacklisted (-e) loggers, you must use one or the other.')
            sys.exit(1)
        loglevels = [logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG]
        if (verbosity >= len(loglevels)):
            verbosity = (len(loglevels) - 1)
        level = loglevels[verbosity]
        if args.include:
            for name in args.include:
                logger = logging.getLogger(name)
                logger.setLevel(level)
                logger.addHandler(handler)
            root.addHandler(logging.NullHandler())
        else:
            for name in args.exclude:
                logger = logging.getLogger(name)
                logger.disabled = True
            root.setLevel(level)
            root.addHandler(handler)
    else:
        root.addHandler(logging.NullHandler())
    return args

Parse all global iotile tool arguments.

Any flag based argument at the start of the command line is considered as
a global flag and parsed.  The first non flag argument starts the commands
that are passed to the underlying hierarchical shell.

Args:
argv (list): The command line for this command

Returns:
Namespace: The parsed arguments, with all of the commands that should
be executed in an iotile shell as the attribute 'commands'


def extract_formats(config_handle):
    configurations = dict(config_handle)
    formats = dict(configurations.get('formats', {}))
    return formats

Get application formats.

See :class:`gogoutils.Formats` for available options.

Args:
config_handle (configparser.ConfigParser): Instance of configurations.

Returns:
dict: Formats in ``{$format_type: $format_pattern}``.


def base_path(self):
    path = self.request.path
    base_path = path[:path.rfind('/')]
    if (not base_path.endswith('/command')):
        raise BadRequestPathError('Json handlers should have /command path prefix')
    return base_path[:base_path.rfind('/')]

Base path for all mapreduce-related urls.

JSON handlers are mapped to /base_path/command/command_name thus they
require special treatment.

Raises:
BadRequestPathError: if the path does not end with "/command".

Returns:
The base path.


def get_current_and_head_revision(
        database_url: str,
        alembic_config_filename: str,
        alembic_base_dir: str = None,
        version_table: str = DEFAULT_ALEMBIC_VERSION_TABLE) -> Tuple[str, str]:

    head_revision = get_head_revision_from_alembic(
        alembic_config_filename=alembic_config_filename,
        alembic_base_dir=alembic_base_dir,
        version_table=version_table
    )
    log.info("Intended database version: {}", head_revision)

    current_revision = get_current_revision(
        database_url=database_url,
        version_table=version_table
    )
    log.info("Current database version: {}", current_revision)

    return current_revision, head_revision

Returns a tuple of ``(current_revision, head_revision)``; see
:func:`get_current_revision` and :func:`get_head_revision_from_alembic`.

Arguments:
database_url: SQLAlchemy URL for the database
alembic_config_filename: config filename
alembic_base_dir: directory to start in, so relative paths in the
config file work.
version_table: table name for Alembic versions


def getVariable(self, name):

        return lock_and_call(
            lambda: Variable(self._impl.getVariable(name)),
            self._lock
        )

Get the variable with the corresponding name.

Args:
name: Name of the variable to be found.

Raises:
TypeError: if the specified variable does not exist.


def plot_spectrum(self, t=0, f_start=None, f_stop=None, logged=False, if_id=0, c=None, **kwargs):

        if self.header[b'nbits'] <=2:
            logged = False
            t='all'
        ax = plt.gca()

        plot_f, plot_data = self.grab_data(f_start, f_stop, if_id)

        if self.header[b'foff'] < 0:
            plot_data = plot_data[..., ::-1]
            plot_f = plot_f[::-1]

        if isinstance(t, int):
            print("extracting integration %i..." % t)
            plot_data = plot_data[t]
        elif t == b'all':
            print("averaging along time axis...")

            if len(plot_data.shape) > 1:
                plot_data = plot_data.mean(axis=0)
            else:
                plot_data = plot_data.mean()
        else:
            raise RuntimeError("Unknown integration %s" % t)

        dec_fac_x = 1
        if plot_data.shape[0] > MAX_PLT_POINTS:
            dec_fac_x = int(plot_data.shape[0] / MAX_PLT_POINTS)

        plot_data = rebin(plot_data, dec_fac_x, 1)
        plot_f    = rebin(plot_f, dec_fac_x, 1)

        if not c:
            kwargs['c'] = '

        if logged:
            plt.plot(plot_f, db(plot_data),label='Stokes I', **kwargs)
            plt.ylabel("Power [dB]")
        else:

            plt.plot(plot_f, plot_data,label='Stokes I', **kwargs)
            plt.ylabel("Power [counts]")
        plt.xlabel("Frequency [MHz]")
        plt.legend()

        try:
            plt.title(self.header[b'source_name'])
        except KeyError:
            plt.title(self.filename)

        plt.xlim(plot_f[0], plot_f[-1])

Plot frequency spectrum of a given file

Args:
t (int): integration number to plot (0 -> len(data))
logged (bool): Plot in linear (False) or dB units (True)
if_id (int): IF identification (if multiple IF signals in file)
c: color for line
kwargs: keyword args to be passed to matplotlib plot()


def _validate_clientsecrets(clientsecrets_dict):
    _INVALID_FILE_FORMAT_MSG = 'Invalid file format. See https:
    if (clientsecrets_dict is None):
        raise InvalidClientSecretsError(_INVALID_FILE_FORMAT_MSG)
    try:
        ((client_type, client_info),) = clientsecrets_dict.items()
    except (ValueError, AttributeError):
        raise InvalidClientSecretsError((_INVALID_FILE_FORMAT_MSG + ' Expected a JSON object with a single property for a "web" or "installed" application'))
    if (client_type not in VALID_CLIENT):
        raise InvalidClientSecretsError('Unknown client type: {0}.'.format(client_type))
    for prop_name in VALID_CLIENT[client_type]['required']:
        if (prop_name not in client_info):
            raise InvalidClientSecretsError('Missing property "{0}" in a client type of "{1}".'.format(prop_name, client_type))
    for prop_name in VALID_CLIENT[client_type]['string']:
        if client_info[prop_name].startswith('[['):
            raise InvalidClientSecretsError('Property "{0}" is not configured.'.format(prop_name))
    return (client_type, client_info)

Validate parsed client secrets from a file.

Args:
clientsecrets_dict: dict, a dictionary holding the client secrets.

Returns:
tuple, a string of the client type and the information parsed
from the file.


def __init__(self, controller,
                 device_initializer=\
                 lambda sc, idcode: JTAGDevice(sc,idcode),
                 ignore_jtag_enabled=False, debug=False,
                 collect_compiler_artifacts=False,
                 collect_compiler_merge_artifacts=False,
                 print_statistics=False):

        self._debug = debug
        self._collect_compiler_artifacts = collect_compiler_artifacts
        self._collect_compiler_merge_artifacts = collect_compiler_merge_artifacts
        self._print_statistics = print_statistics
        self._fitted_lv1_prim_cache = {}
        self._devices = []
        self._hasinit = False
        self._sm = JTAGStateMachine()
        self._ignore_jtag_enabled = ignore_jtag_enabled
        self._desired_speed = None

        self.initialize_device_from_id = device_initializer
        self.get_descriptor_for_idcode = \
                    jtagDeviceDescription.get_descriptor_for_idcode

        if isinstance(controller, InaccessibleController):
            raise DevicePermissionDeniedError()
        self._controller = controller

        self._controller._scanchain = self

        self._command_queue = CommandQueue(self)

        default_prims = {RunInstruction,
                         TransitionTAP, RWReg, RWDR, RWIR, Sleep,
                         RWDevDR, RWDevIR}
        self._chain_primitives = {}
        self._device_primitives = {}
        self._lv1_chain_primitives = []

        for prim in default_prims:
            assert issubclass(prim, Primitive)
            if issubclass(prim, DeviceTarget):
                self._device_primitives[prim._function_name] = prim
            else:
                self._chain_primitives[prim._function_name] = prim

        for prim in self._controller._primitives:
            if not issubclass(prim, Primitive):
                raise Exception("Registered Controller Prim has "
                                "unknown type. (%s)"%prim)
            if issubclass(prim, DeviceTarget):
                self._device_primitives[prim._function_name] = prim
            else:
                self._chain_primitives[prim._function_name] = prim
                if issubclass(prim, Level1Primitive):
                    self._lv1_chain_primitives.append(prim)

        for func_name, prim in self._chain_primitives.items():
            if not self._gen_prim_adder(prim):
                raise Exception("Failed adding primitive %s, "\
                                "primitive with name %s "\
                                "already exists on scanchain"%\
                                (prim, prim._function_name))

Create a new JTAGScanChain to track and control a real chain.

Args:
controller: The CableDriver that this ScanChain will control.
device_initializer: A callable that can map a (JTAGScanChain, Bitarray) to an instance of a JTAGDevice (Allows custom classes to be used).
ignore_jtag_enabled: A boolean on if errors should be ignored when JTA is already enabled on the controller.
debug: A boolean to enable extra debug printing.