def try_get_column(column_name, node, context):

    selectable = get_node_selectable(node, context)
    if not hasattr(selectable, 'c'):
        raise AssertionError(
            u'Selectable "{}" does not have a column collection. Context is {}.'.format(
                selectable, context))
    return selectable.c.get(column_name, None)

Attempt to get a column by name from the selectable.

Args:
column_name: str, name of the column to retrieve.
node: SqlNode, the node the column is being retrieved for.
context: CompilationContext, compilation specific metadata.

Returns:
Optional[column], the SQLAlchemy column if found, None otherwise.


def _testMultipleReduceJoin(self, input_array, axis, separator=' '):
    with self.cached_session():
        output = string_ops.reduce_join(inputs=input_array, axis=axis, keep_dims=False, separator=separator)
        output_keep_dims = string_ops.reduce_join(inputs=input_array, axis=axis, keep_dims=True, separator=separator)
        truth = input_array
        for index in axis:
            truth = string_ops.reduce_join(inputs=truth, axis=index, keep_dims=True, separator=separator)
        if not axis:
            truth = constant_op.constant(truth)
        truth_squeezed = array_ops.squeeze(truth, axis=axis)
        output_array = self.evaluate(output)
        output_keep_dims_array = self.evaluate(output_keep_dims)
        truth_array = self.evaluate(truth)
        truth_squeezed_array = self.evaluate(truth_squeezed)
    self.assertAllEqualUnicode(truth_array, output_keep_dims_array)
    self.assertAllEqualUnicode(truth_squeezed_array, output_array)
    self.assertAllEqual(truth.get_shape(), output_keep_dims.get_shape())
    self.assertAllEqual(truth_squeezed.get_shape(), output.get_shape())

Tests reduce_join for one input and multiple axes.

Does so by comparing the output to that from nested reduce_string_joins.
The correctness of single-dimension reduce_join is verified by other
tests below using _testReduceJoin.

Args:
input_array: The input to test.
axis: The indices to reduce.
separator: The separator to use when joining.


def _convert_template_option(template):

    option = {}
    extraction_method = template.get('extraction_method')
    if extraction_method == 'guess':
        option['guess'] = True
    elif extraction_method == 'lattice':
        option['lattice'] = True
    elif extraction_method == 'stream':
        option['stream'] = True

    option['pages'] = template.get('page')
    option['area'] = [round(template['y1'], 3), round(template['x1'], 3), round(template['y2'], 3), round(template['x2'], 3)]

    return option

Convert Tabula app template to tabula-py option

Args:
template (dict): Tabula app template

Returns:
`obj`:dict: tabula-py option


def create_token_type_ids_from_sequences(self, token_ids_0: List[int], token_ids_1: Optional[List[int]]=None) -> List[int]:
    sep = [self.sep_token_id]
    cls = [self.cls_token_id]
    if token_ids_1 is None:
        return len(cls + token_ids_0 + sep) * [0]
    return len(cls + token_ids_0 + sep + sep + token_ids_1 + sep) * [0]

Create a mask from the two sequences passed to be used in a sequence-pair classification task. nllb does not
make use of token type ids, therefore a list of zeros is returned.

Args:
token_ids_0 (`List[int]`):
List of IDs.
token_ids_1 (`List[int]`, *optional*):
Optional second list of IDs for sequence pairs.

Returns:
`List[int]`: List of zeros.


def iter_compress(item_iter, flag_iter):
    true_items = (item for (item, flag) in zip(item_iter, flag_iter) if flag)
    return true_items

iter_compress - like numpy compress

Args:
item_iter (list):
flag_iter (list): of bools

Returns:
list: true_items

Example:
>>> # ENABLE_DOCTEST
>>> from utool.util_iter import *  # NOQA
>>> item_iter = [1, 2, 3, 4, 5]
>>> flag_iter = [False, True, True, False, True]
>>> true_items = iter_compress(item_iter, flag_iter)
>>> result = list(true_items)
>>> print(result)
[2, 3, 5]


def CheckComment(line, filename, linenum, next_line_start, error):
    commentpos = line.find('
    if (commentpos != (- 1)):
        if ((re.sub('\\\\.', '', line[0:commentpos]).count('"') % 2) == 0):
            if ((not (Match('^.*{ *
                error(filename, linenum, 'whitespace/comments', 2, 'At least two spaces is best between code and comments')
            comment = line[commentpos:]
            match = _RE_PATTERN_TODO.match(comment)
            if match:
                leading_whitespace = match.group(1)
                if (len(leading_whitespace) > 1):
                    error(filename, linenum, 'whitespace/todo', 2, 'Too many spaces before TODO')
                username = match.group(2)
                if (not username):
                    error(filename, linenum, 'readability/todo', 2, 'Missing username in TODO; it should look like "
                middle_whitespace = match.group(3)
                if ((middle_whitespace != ' ') and (middle_whitespace != '')):
                    error(filename, linenum, 'whitespace/todo', 2, 'TODO(my_username) should be followed by a space')
            if (Match('
                error(filename, linenum, 'whitespace/comments', 4, 'Should have a space between

Checks for common mistakes in comments.

Args:
line: The line in question.
filename: The name of the current file.
linenum: The number of the line to check.
next_line_start: The first non-whitespace column of the next line.
error: The function to call with any errors found.


def _parse_authors(authors):
    link = authors.find('a')
    link = (link[0].params.get('href') if link else None)
    author_list = _strip_content(authors)
    if ('(' in author_list):
        author_list = author_list.split('(')[0]
    if (not author_list.strip()):
        return []
    return map((lambda author: Author(author.strip(), link)), author_list.strip().split(','))

Parse informations about authors of the book.

Args:
dom (obj): HTMLElement containing slice of the page with details.

Returns:
list: List of :class:`.Author` objects. Blank if no author \
found.


def main(args):

  if not args:
    raise Exception('Please specify at least one JSON config path')
  inputs = []
  program = []
  outputs = []
  for arg in args:
    with open(arg) as fd:
      config = json.load(fd)
    inputs.extend(config.get('inputs', []))
    program.extend(config.get('program', []))
    outputs.extend(config.get('outputs', []))
  if not program:
    raise Exception('Please specify a program')
  return run(inputs, program, outputs)

Invokes run function using a JSON file config.

Args:
args: CLI args, which can be a JSON file containing an object whose
attributes are the parameters to the run function. If multiple JSON
files are passed, their contents are concatenated.
Returns:
0 if succeeded or nonzero if failed.
Raises:
Exception: If input data is missing.


def slice_hidden(x, hidden_size, num_blocks):

  batch_size, latent_dim, _ = common_layers.shape_list(x)
  block_dim = hidden_size
  x_sliced = tf.reshape(x,
                        shape=[batch_size, latent_dim, num_blocks, block_dim])
  return x_sliced

Slice encoder hidden state under num_blocks.

Args:
x: Encoder hidden state of shape [batch_size, latent_dim, hidden_size].
hidden_size: Dimension of the latent space.
num_blocks: Number of blocks in DVQ.

Returns:
Sliced states of shape [batch_size, latent_dim, num_blocks, block_dim].


def http_exception(channel, title):

    gui = ui_embed.UI(
        channel,
        "Too much help",
        "{} is too helpful! Try trimming some of the help messages.".format(title),
        modulename=modulename
    )

    return gui

Creates an embed UI containing the 'too long' error message

Args:
channel (discord.Channel): The Discord channel to bind the embed to
title (str): The title of the embed

Returns:
ui (ui_embed.UI): The embed UI object


def get_cohp(self, spin=None, integrated=False):
    if (not integrated):
        populations = self.cohp
    else:
        populations = self.icohp
    if (populations is None):
        return None
    elif (spin is None):
        return populations
    else:
        if isinstance(spin, int):
            spin = Spin(spin)
        elif isinstance(spin, str):
            s = {'up': 1, 'down': (- 1)}[spin.lower()]
            spin = Spin(s)
        return {spin: populations[spin]}

Returns the COHP or ICOHP for a particular spin.

Args:
spin: Spin. Can be parsed as spin object, integer (-1/1)
or str ("up"/"down")
integrated: Return COHP (False) or ICOHP (True)

Returns:
Returns the CHOP or ICOHP for the input spin. If Spin is
None and both spins are present, both spins will be returned
as a dictionary.


def _get_input_readers(self, state):

    serialized_input_readers_key = (self._SERIALIZED_INPUT_READERS_KEY %
                                    state.key().id_or_name())
    serialized_input_readers = model._HugeTaskPayload.get_by_key_name(
        serialized_input_readers_key, parent=state)

    input_reader_class = state.mapreduce_spec.mapper.input_reader_class()
    split_param = state.mapreduce_spec.mapper
    if issubclass(input_reader_class, map_job.InputReader):
      split_param = map_job.JobConfig._to_map_job_config(
          state.mapreduce_spec,
          os.environ.get("HTTP_X_APPENGINE_QUEUENAME"))
    if serialized_input_readers is None:
      readers = input_reader_class.split_input(split_param)
    else:
      readers = [input_reader_class.from_json_str(_json) for _json in
                 json.loads(zlib.decompress(
                 serialized_input_readers.payload))]

    if not readers:
      return None, None

    state.mapreduce_spec.mapper.shard_count = len(readers)
    state.active_shards = len(readers)

    if serialized_input_readers is None:

      serialized_input_readers = model._HugeTaskPayload(
          key_name=serialized_input_readers_key, parent=state)
      readers_json_str = [i.to_json_str() for i in readers]
      serialized_input_readers.payload = zlib.compress(json.dumps(
                                                       readers_json_str))
    return readers, serialized_input_readers

Get input readers.

Args:
state: a MapreduceState model.

Returns:
A tuple: (a list of input readers, a model._HugeTaskPayload entity).
The payload entity contains the json serialized input readers.
(None, None) when input reader inplitting returned no data to process.


def _url_dirname(self, url_or_path):
    scheme, path = self._split_scheme(url_or_path)
    return self._combine_scheme(scheme, posixpath.dirname(path))

Like posixpath.dirname, but preserves scheme:// prefix.

Args:
url_or_path: A string in the form of scheme://some/path OR /some/path.


def phase_flip(p: Optional[float]=None) -> Union[(common_gates.ZPowGate, PhaseFlipChannel)]:
    if (p is None):
        return _phase_flip_Z()
    return _phase_flip(p)

r"""
Returns a PhaseFlipChannel that flips a qubit's phase with probability p
if p is None, return a guaranteed phase flip in the form of a Z operation.

This channel evolves a density matrix via:

$$
\rho \rightarrow M_0 \rho M_0^\dagger + M_1 \rho M_1^\dagger
$$

With:

$$
\begin{aligned}
M_0 =& \sqrt{p} \begin{bmatrix}
1 & 0  \\
0 & 1
\end{bmatrix}
\\
M_1 =& \sqrt{1-p} \begin{bmatrix}
1 & 0 \\
0 & -1
\end{bmatrix}
\end{aligned}
$$

Args:
p: the probability of a phase flip.

Raises:
ValueError: if p is not a valid probability.


def to_file(self, filename):

        d = {"mass_info": self.mass_info,
             "nonbond_coeffs": self.nonbond_coeffs,
             "topo_coeffs": self.topo_coeffs}
        yaml = YAML(typ="safe")
        with open(filename, "w") as f:
            yaml.dump(d, f)

Saves object to a file in YAML format.

Args:
filename (str): Filename.


def do_load(self, design, init=False):

        if design:
            filename = self._validated_config_filename(design)
            with open(filename, "r") as f:
                text = f.read()
            structure = json_decode(text)
        else:
            structure = {}

        attributes = structure.get("attributes", structure)
        children = structure.get("children", structure)

        name, mri, x, y, visible = [], [], [], [], []
        for part_name, d in attributes.get("layout", {}).items():
            name.append(part_name)
            mri.append("")
            x.append(d["x"])
            y.append(d["y"])
            visible.append(d["visible"])
        self.set_layout(LayoutTable(name, mri, x, y, visible))

        source, export = [], []
        for source_name, export_name in attributes.get("exports", {}).items():
            source.append(source_name)
            export.append(export_name)
        self.exports.set_value(ExportTable(source, export))

        our_values = {k: v for k, v in attributes.items()
                      if k in self.our_config_attributes}
        block = self.block_view()
        block.put_attribute_values(our_values)

        self.run_hooks(
            LoadHook(p, c, children.get(p.name, {}), init)
            for p, c in self.create_part_contexts(only_visible=False).items())
        self._mark_clean(design, init)

Load a design name, running the child LoadHooks.

Args:
design: Name of the design json file, without extension
init: Passed to the LoadHook to tell the children if this is being
run at Init or not


def __init__(self, key_spec: Union[KeySpec, str], value_spec: ValueSpec, description: Optional[str]=None, metadata: Optional[Dict[str, Any]]=None, origin: Optional[Type[Any]]=None) -> None:
    if isinstance(key_spec, str):
        key_spec = KeySpec.from_str(key_spec)
    assert isinstance(key_spec, KeySpec), key_spec
    self._key = key_spec
    self._value = value_spec
    self._description = description
    self._origin = origin
    if metadata and (not isinstance(metadata, dict)):
        raise ValueError('metadata must be a dict.')
    self._metadata = metadata or {}

Constructor.

Args:
key_spec: Key specification of the field. Can be a string or a KeySpec
instance.
value_spec: Value specification of the field.
description: Description of the field.
metadata: A dict of objects as metadata for the field.
origin: The class that this field originates from.

Raises:
ValueError: metadata is not a dict.


def _version_from_file(
        path_to_version,
        default_version=DEFAULT_VERSION,
):

    version_filepath = os.path.join(path_to_version, 'version.txt')
    if not os.path.isfile(version_filepath):
        warnings.warn(
            'Unable to resolve current version',
            exceptions.ProsperDefaultVersionWarning)
        return default_version

    with open(version_filepath, 'r') as v_fh:
        data = v_fh.read()

    return data

for PyPI installed versions, just get data from file

Args:
path_to_version (str): abspath to dir where version.txt exists
default_version (str): fallback version in case of error

Returns:
str: current working version


def _CheckLine(self, line):
    for rule in self._cur_state:
        matched = self._CheckRule(rule, line)
        if matched:
            for value in matched.groupdict():
                self._AssignVar(matched, value)
            if self._Operations(rule):
                if rule.new_state:
                    if (rule.new_state not in ('End', 'EOF')):
                        self._cur_state = self.states[rule.new_state]
                    self._cur_state_name = rule.new_state
                break

Passes the line through each rule until a match is made.

Args:
line: A string, the current input line.


def __init__(self):

        super(JLinkTraceRegion, self).__init__()
        self.SizeOfStruct = ctypes.sizeof(self)

Initializes the trace region.

Sets the size of the structure.

Args:
self (JLinkTraceRegion): the ``JLinkTraceRegion`` instance.

Returns:
``None``


def set_ylim(self, xlims, dx, xscale, reverse=False):

        self._set_axis_limits('y', xlims, dx, xscale, reverse)
        return

Set y limits for plot.

This will set the limits for the y axis
for the specific plot.

Args:
ylims (len-2 list of floats): The limits for the axis.
dy (float): Amount to increment by between the limits.
yscale (str): Scale of the axis. Either `log` or `lin`.
reverse (bool, optional): If True, reverse the axis tick marks. Default is False.


def get_model(self):
    model_cls = get_connected_model_for_table_name(self.table_name)
    return model_cls._default_manager.filter(id=self.record_id).first()

Fetch the instance of the connected model referenced by this log record.

Returns:
The connected instance, or ``None`` if it does not exists.


def _augment_observation(self, ob, reward, cumulative_reward):

    img = PIL_Image().new("RGB",
                          (ob.shape[1], self.HEADER_HEIGHT,))
    draw = PIL_ImageDraw().Draw(img)
    draw.text(
        (1, 0), "c:{:3}, r:{:3}".format(int(cumulative_reward), int(reward)),
        fill=(255, 0, 0)
    )
    draw.text(
        (1, 15), "fc:{:3}".format(int(self._frame_counter)),
        fill=(255, 0, 0)
    )
    header = np.asarray(img)
    del img
    header.setflags(write=1)

    if self._wait:
      pixel_fill = (0, 255, 0)
    else:
      pixel_fill = (255, 0, 0)
    header[0, :, :] = pixel_fill
    return np.concatenate([header, ob], axis=0)

Expand observation array with additional information header (top rows).

Args:
ob: observation
reward: reward to be included in header.
cumulative_reward: total cumulated reward to be included in header.

Returns:
Expanded observation array.


def clean_value(self):
    result = []
    for mdl in self:
        result.append(super(ListNode, mdl).clean_value())
    return result

Populates json serialization ready data.
This is the method used to serialize and store the object data in to DB

Returns:
List of dicts.


def execute(self, correlation_id, args):

        if self._schema != None:
            self.validate_and_throw_exception(correlation_id, args)

        try:
            return self._function(correlation_id, args)

        except Exception as ex:
            raise InvocationException(
                correlation_id,
                "EXEC_FAILED",
                "Execution " + self._name + " failed: " + str(ex)
            ).with_details("command", self._name).wrap(ex)

Executes the command given specific arguments as an input.

Args:
correlation_id: a unique correlation/transaction id
args: command arguments

Returns: an execution result.

Raises:
ApplicationException: when execution fails for whatever reason.


def request(self, method, params=None):

        msg_id = self._id_generator()
        log.debug('Sending request with id %s: %s %s', msg_id, method, params)

        message = {
            'jsonrpc': JSONRPC_VERSION,
            'id': msg_id,
            'method': method,
        }
        if params is not None:
            message['params'] = params

        request_future = futures.Future()
        request_future.add_done_callback(self._cancel_callback(msg_id))

        self._server_request_futures[msg_id] = request_future
        self._consumer(message)

        return request_future

Send a JSON RPC request to the client.

Args:
method (str): The method name of the message to send
params (any): The payload of the message

Returns:
Future that will resolve once a response has been received


def trace_format(self):

        cmd = enums.JLinkTraceCommand.GET_FORMAT
        data = ctypes.c_uint32(0)
        res = self._dll.JLINKARM_TRACE_Control(cmd, ctypes.byref(data))
        if (res == 1):
            raise errors.JLinkException('Failed to get trace format.')
        return data.value

Retrieves the current format the trace buffer is using.

Args:
self (JLink): the ``JLink`` instance.

Returns:
The current format the trace buffer is using.  This is one of the
attributes of ``JLinkTraceFormat``.


def _read_and_batch_from_files(file_pattern, batch_size, max_length, num_cpu_cores, shuffle, repeat):
    dataset = tf.data.Dataset.list_files(file_pattern)
    if shuffle:
        mlperf_log.transformer_print(key=mlperf_log.INPUT_ORDER)
        dataset = dataset.shuffle(buffer_size=_FILE_SHUFFLE_BUFFER)
    dataset = dataset.apply(tf.contrib.data.parallel_interleave(_load_records, sloppy=shuffle, cycle_length=num_cpu_cores))
    dataset = dataset.map(_parse_example, num_parallel_calls=num_cpu_cores)
    dataset = dataset.filter((lambda x, y: _filter_max_length((x, y), max_length)))
    mlperf_log.transformer_print(key=mlperf_log.INPUT_BATCH_SIZE, value=batch_size)
    mlperf_log.transformer_print(key=mlperf_log.INPUT_MAX_LENGTH, value=max_length)
    dataset = _batch_examples(dataset, batch_size, max_length)
    dataset = dataset.repeat(repeat)
    dataset = dataset.prefetch(1)
    return dataset

Create dataset where each item is a dict of "inputs" and "targets".

Args:
file_pattern: String used to match the input TFRecord files.
batch_size: Maximum number of tokens per batch of examples
max_length: Maximum number of tokens per example
num_cpu_cores: Number of cpu cores for parallel input processing.
shuffle: If true, randomizes order of elements.
repeat: Number of times to repeat the dataset. If None, the dataset is
repeated forever.

Returns:
tf.data.Dataset object containing examples loaded from the files.


def finalise(self):
    assert self._is_solved()
    g = self._get_minimal_graph()
    scopes = dict(((x.package_name, x) for x in self.scopes if (not x.package_request.conflict)))
    fam_cycle = find_cycle(g)
    if fam_cycle:
        cycle = []
        for fam in fam_cycle:
            scope = scopes[fam]
            variant = scope._get_solved_variant()
            stmt = VersionedObject.construct(fam, variant.version)
            cycle.append(stmt)
        phase = copy.copy(self)
        phase.scopes = scopes.values()
        phase.failure_reason = Cycle(cycle)
        phase.status = SolverStatus.cyclic
        return phase
    fams = [x.name for x in self.solver.request_list]
    ordered_fams = _get_dependency_order(g, fams)
    scopes_ = []
    for fam in ordered_fams:
        scope = scopes[fam]
        if (not scope.package_request.conflict):
            scopes_.append(scope)
    phase = copy.copy(self)
    phase.scopes = scopes_
    return phase

Remove conflict requests, detect cyclic dependencies, and reorder
packages wrt dependency and then request order.

Returns:
A new copy of the phase with conflict requests removed and packages
correctly ordered; or, if cyclic dependencies were detected, a new
phase marked as cyclic.


def label_count(self, label_list_ids=None):

        count = collections.defaultdict(int)

        for utterance in self.utterances.values():
            for label_value, utt_count in utterance.label_count(label_list_ids=label_list_ids).items():
                count[label_value] += utt_count

        return count

Return a dictionary containing the number of times, every label-value in this corpus is occurring.

Args:
label_list_ids (list): If not None, only labels from label-lists with an id contained in this list
are considered.

Returns:
dict: A dictionary containing the number of occurrences with the label-value as key.


def delete_devices(self, auth_body, devices):

        content = {
            "auth": auth_body,
            "devices": devices
        }
        return self._send("POST", "/delete_devices", content=content)

Bulk deletion of devices.

NOTE: This endpoint uses the User-Interactive Authentication API.

Args:
auth_body (dict): Authentication params.
devices (list): List of device ID"s to delete.


def get_disk_usage(self, path=None):

        DiskUsage = namedtuple('usage', 'total, used, free')
        if path is None:
            mount_point = self.mount_points[self.root.name]
        else:
            mount_point = self._mount_point_for_path(path)
        if mount_point and mount_point['total_size'] is not None:
            return DiskUsage(mount_point['total_size'],
                             mount_point['used_size'],
                             mount_point['total_size'] -
                             mount_point['used_size'])
        return DiskUsage(
            1024 * 1024 * 1024 * 1024, 0, 1024 * 1024 * 1024 * 1024)

Return the total, used and free disk space in bytes as named tuple,
or placeholder values simulating unlimited space if not set.

.. note:: This matches the return value of shutil.disk_usage().

Args:
path: The disk space is returned for the file system device where
`path` resides.
Defaults to the root path (e.g. '/' on Unix systems).


def __init__(self, version_string, first_matched_type, second_matched_type):

        super(TooManyTypesError, self).__init__(
            'Release "{}" cannot match types "{}" and "{}"'.format(
                version_string, first_matched_type, second_matched_type
            )
        )

Constructor.

Args:
version_string (str): The string that gave too many types.
first_matched_type (str): The name of the first detected type.
second_matched_type (str): The name of the second detected type


def all_subnets_shorter_prefix(ip_net, cidr, include_default=False):

    subnets_list = list()
    if include_default:
        while int(cidr) >= 0:
            try:
                subnets_list.append('%s/%s' % (whole_subnet_maker(ip_net, cidr), cidr))
            except Exception as e:
                LOGGER.critical('Function all_subnets_shorter_prefix {item}'.format(item=e))
            cidr = str(int(cidr) - 1)
    else:
        while int(cidr) > 0:
            try:
                subnets_list.append('%s/%s' % (whole_subnet_maker(ip_net, cidr), cidr))
            except Exception as e:
                LOGGER.critical('Function all_subnets_shorter_prefix {item}'.format(item=e))
            cidr = str(int(cidr) - 1)
    return subnets_list

Function to return every subnet a ip can belong to with a shorter prefix
Args:
ip_net: Unicast or Multicast IP address or subnet in the following format 192.168.1.1, 239.1.1.1
cidr: CIDR value of 0 to 32
include_default: If you want the list to inlclude the default route set to True

Returns: returns a list of subnets


def findall(self, title=None):
    if (title is None):
        return list(self)
    files = backend.iterfiles(self._drive, name=title)
    return [self[id] for (id, _) in files]

Fetch and return a list of spreadsheets with the given title.

Args:
title(str): title/name of the spreadsheets to return, or ``None`` for all
Returns:
list: list of new SpreadSheet instances (possibly empty)


def set_working_directory(working_directory):

    logger.debug("starting")

    logger.debug(f"adding {working_directory} to sys.paths")
    sys.path.append(working_directory)

    logger.debug("done")

Add working_directory to sys.paths.

This allows dynamic loading of arbitrary python modules in cwd.

Args:
working_directory: string. path to add to sys.paths


Implements 64bit murmur3 hash, as per ``pymmh3``. Returns a tuple.

Args:
key: data to hash
seed: seed
x64arch: is a 64-bit architecture available?

Returns:
tuple: tuple of integers, ``(signed_val1, signed_val2)``


def augment(self, dct: NonAugmentedDict,
                document: Optional[YamlDocument] = None) -> AugmentedDict:

        Validator.instance_of(dict, raise_ex=True, dct=dct)

        for instance in self._extensions:
            nodes = list(dict_find_pattern(dct, **instance.config()))
            for parent, k, val in nodes:
                parent.pop(k)
                fragment = instance.apply(
                    ExtensionContext(
                        mentor=self,
                        document=document or dct,
                        dct=dct,
                        parent_node=parent,
                        node=(k, val)
                    )
                )
                if fragment is not None:
                    parent.update(fragment)

        return dct

Augments the given dictionary by using all the bound extensions.

Args:
dct: Dictionary to augment.
document: The document the dictionary was loaded from.

Returns:
The augmented dictionary.


def _custom_diag_normal_kl(lhs, rhs, name=None):
    with tf.name_scope((name or 'kl_divergence')):
        mean0 = lhs.mean()
        mean1 = rhs.mean()
        logstd0 = tf.log(lhs.stddev())
        logstd1 = tf.log(rhs.stddev())
        (logstd0_2, logstd1_2) = ((2 * logstd0), (2 * logstd1))
        return (0.5 * ((((tf.reduce_sum(tf.exp((logstd0_2 - logstd1_2)), (- 1)) + tf.reduce_sum((((mean1 - mean0) ** 2) / tf.exp(logstd1_2)), (- 1))) + tf.reduce_sum(logstd1_2, (- 1))) - tf.reduce_sum(logstd0_2, (- 1))) - mean0.shape[(- 1)].value))

Empirical KL divergence of two normals with diagonal covariance.

Args:
lhs: Diagonal Normal distribution.
rhs: Diagonal Normal distribution.
name: Name scope for the op.

Returns:
KL divergence from lhs to rhs.


def get_module_object_and_name(globals_dict):

  name = globals_dict.get('__name__', None)
  module = sys.modules.get(name, None)

  return _ModuleObjectAndName(module,
                              (sys.argv[0] if name == '__main__' else name))

Returns the module that defines a global environment, and its name.

Args:
globals_dict: A dictionary that should correspond to an environment
providing the values of the globals.

Returns:
_ModuleObjectAndName - pair of module object & module name.
Returns (None, None) if the module could not be identified.


def waitForEvent(self, event_name, predicate, timeout=DEFAULT_TIMEOUT):
    deadline = time.perf_counter() + timeout
    while time.perf_counter() <= deadline:
        rpc_timeout = deadline - time.perf_counter()
        if rpc_timeout < 0:
            break
        rpc_timeout = min(rpc_timeout, MAX_TIMEOUT)
        try:
            event = self.waitAndGet(event_name, rpc_timeout)
        except TimeoutError:
            break
        if predicate(event):
            return event
    raise TimeoutError(self._ad, 'Timed out after %ss waiting for an "%s" event that satisfies the predicate "%s".' % (timeout, event_name, predicate.__name__))

Wait for an event of a specific name that satisfies the predicate.

This call will block until the expected event has been received or time
out.

The predicate function defines the condition the event is expected to
satisfy. It takes an event and returns True if the condition is
satisfied, False otherwise.

Note all events of the same name that are received but don't satisfy
the predicate will be discarded and not be available for further
consumption.

Args:
event_name: string, the name of the event to wait for.
predicate: function, a function that takes an event (dictionary) and
returns a bool.
timeout: float, default is 120s.

Returns:
dictionary, the event that satisfies the predicate if received.

Raises:
TimeoutError: raised if no event that satisfies the predicate is
received after timeout seconds.


def export_template(access_token, subscription_id, rgname):
    endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/resourcegroups/', rgname, '/exportTemplate', '?api-version=', RESOURCE_API])
    rg_body = {'options': 'IncludeParameterDefaultValue', 'resources': ['*']}
    body = json.dumps(rg_body)
    return do_post(endpoint, body, access_token)

Capture the specified resource group as a template

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

Returns:
HTTP response. JSON body.


def test(cls, test_expr: bool = True) -> None:

        cls.test_dialect_specific_1()
        cls.test_identifiers()
        if test_expr:
            cls.test_expr()
        cls.test_sql_core()
        cls.test_dialect_specific_2()

Runs self-tests.

Args:
test_expr: include tests of expressions (which can be slow).


def __init__(self, room_id, api):

        self.room_id = room_id
        self.api = api

Instantiates MatrixRoom object.

Args:
room_id(str): Matrix room id (e.g. !1234567:example.com)
api(MatrixASHttpAPI): Api for calls to the server.


def upcoming(self, **kwargs):

        path = self._get_path('upcoming')

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

Get the list of upcoming movies. This list refreshes every day.
The maximum number of items this list will include is 100.

Args:
page: (optional) Minimum value of 1.  Expected value is an integer.
language: (optional) ISO 639-1 code.

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


def PyParseRangeCheck(lower_bound, upper_bound):

    def CheckRange(string, location, tokens):
        'Parse the arguments.\n\n    Args:\n      string (str): original string.\n      location (int): location in the string where the match was made\n      tokens (list[str]): tokens.\n    '
        try:
            check_number = tokens[0]
        except IndexError:
            check_number = (- 1)
        if (check_number < lower_bound):
            raise pyparsing.ParseException('Value: {0:d} precedes lower bound: {1:d}'.format(check_number, lower_bound))
        if (check_number > upper_bound):
            raise pyparsing.ParseException('Value: {0:d} exceeds upper bound: {1:d}'.format(check_number, upper_bound))
    return CheckRange

Verify that a number is within a defined range.

This is a callback method for pyparsing setParseAction
that verifies that a read number is within a certain range.

To use this method it needs to be defined as a callback method
in setParseAction with the upper and lower bound set as parameters.

Args:
lower_bound (int): lower bound of the range.
upper_bound (int): upper bound of the range.

Returns:
Function: callback method that can be used by pyparsing setParseAction.


def __init__(self, lmda, theta, phi) -> None:

        self.lmda = lmda % 2
        self.theta = theta % 2
        self.phi = phi % 2

A QASM gate representing any single qubit unitary with a series of
three rotations, Z, Y, and Z.

The angles are normalized to the range [0, 2) half_turns.

Args:
lmda: Half turns to rotate about Z (applied first).
theta: Half turns to rotate about Y.
phi: Half turns to rotate about Z (applied last).


def join_sources(source_module: DeploymentModule, contract_name: str):
    joined_file = Path(__file__).parent.joinpath('joined.sol')
    remapping = {module: str(path) for (module, path) in contracts_source_path().items()}
    command = ['./utils/join-contracts.py', '--import-map', json.dumps(remapping), str(contracts_source_path_of_deployment_module(source_module).joinpath((contract_name + '.sol'))), str(joined_file)]
    working_dir = Path(__file__).parent.parent
    try:
        subprocess.check_call(command, cwd=working_dir)
    except subprocess.CalledProcessError as ex:
        print(f'cd {str(working_dir)}; {subprocess.list2cmdline(command)} failed.')
        raise ex
    return joined_file.read_text()

Use join-contracts.py to concatenate all imported Solidity files.

Args:
source_module: a module name to look up contracts_source_path()
contract_name: 'TokenNetworkRegistry', 'SecretRegistry' etc.


def block_start(self, previous_block):
    previous_header_bytes = previous_block.header
    previous_header = BlockHeader()
    previous_header.ParseFromString(previous_header_bytes)
    block_info = BlockInfo(block_num=previous_header.block_num, previous_block_id=previous_header.previous_block_id, signer_public_key=previous_header.signer_public_key, header_signature=previous_block.header_signature, timestamp=int(time.time()))
    return [self.create_batch(block_info)]

Returns an ordered list of batches to inject at the beginning of the
block. Can also return None if no batches should be injected.

Args:
previous_block (Block): The previous block.

Returns:
A list of batches to inject.


def format_snippet(sensor_graph):
    output = []
    output.append('disable')
    output.append('clear')
    output.append('reset')
    for node in sensor_graph.dump_nodes():
        output.append('add_node "{}"'.format(node))
    for streamer in sensor_graph.streamers:
        line = "add_streamer '{}' '{}' {} {} {}".format(streamer.selector, streamer.dest, streamer.automatic, streamer.format, streamer.report_type)
        if (streamer.with_other is not None):
            line += ' --withother {}'.format(streamer.with_other)
        output.append(line)
    for (stream, value) in sorted(sensor_graph.constant_database.items(), key=(lambda x: x[0].encode())):
        output.append("set_constant '{}' {}".format(stream, value))
    output.append('persist')
    output.append('back')
    app_tag = sensor_graph.metadata_database.get('app_tag')
    app_version = sensor_graph.metadata_database.get('app_version')
    if (app_tag is not None):
        if (app_version is None):
            app_version = '0.0'
        output.append('test_interface')
        output.append(("set_version app %d --version '%s'" % (app_tag, app_version)))
        output.append('back')
    output.append('config_database')
    output.append('clear_variables')
    for (slot, conf_vars) in sensor_graph.config_database.items():
        for (conf_var, conf_def) in conf_vars.items():
            (conf_type, conf_val) = conf_def
            if (conf_type == 'binary'):
                conf_val = ('hex:' + hexlify(conf_val))
            elif isinstance(conf_val, str):
                conf_val = ('"%s"' % conf_val)
            output.append("set_variable '{}' {} {} {}".format(slot, conf_var, conf_type, conf_val))
    output.append('back')
    output.append('reset')
    return ('\n'.join(output) + '\n')

Format this sensor graph as iotile command snippets.

This includes commands to reset and clear previously stored
sensor graphs.

Args:
sensor_graph (SensorGraph): the sensor graph that we want to format


def validate_functions(ast: BELAst, bo):

    if isinstance(ast, Function):
        log.debug(f"Validating: {ast.name}, {ast.function_type}, {ast.args}")
        function_signatures = bo.spec["functions"]["signatures"][ast.name]["signatures"]

        function_name = ast.name
        (valid_function, messages) = check_function_args(
            ast.args, function_signatures, function_name
        )
        if not valid_function:
            message = ", ".join(messages)
            bo.validation_messages.append(
                (
                    "ERROR",
                    "Invalid BEL Statement function {} - problem with function signatures: {}".format(
                        ast.to_string(), message
                    ),
                )
            )
            bo.parse_valid = False

    if hasattr(ast, "args"):
        for arg in ast.args:
            validate_functions(arg, bo)

    return bo

Recursively validate function signatures

Determine if function matches one of the available signatures. Also,

1. Add entity types to AST NSArg, e.g. Abundance, ...
2. Add optional to  AST Arg (optional means it is not a
fixed, required argument and needs to be sorted for
canonicalization, e.g. reactants(A, B, C) )

Args:
bo: bel object

Returns:
bel object


def _addConfig(instance, config, parent_section):

		try:
			section_name = "{p}/{n}".format(p = parent_section, n=instance.NAME.lower())
			config.add_section(section_name)
			for k in instance.CONFIG.keys():
				config.set(section_name, k, instance.CONFIG[k])
		except Exception as e:
			print "[!] %s" % e

Writes a section for a plugin.

Args:
instance (object): Class instance for plugin
config (object): Object (ConfigParser) which the current config
parent_section (str): Parent section for plugin. Usually 'checkers' or 'reports'


def _find_docstring_line(self, start, end):
    for i in range(start, (end + 1)):
        if (i in self._tokenized_triple_quotes):
            return i
    return None

Find the row where a docstring starts in a function or class.

This will search for the first match of a triple quote token in
row sequence from the start of the class or function.

Args:
start: the row where the class / function starts.
end: the row where the class / function ends.

Returns:
int: the row number where the docstring is found.


def __init__(cls, name, bases, dictionary):

    super(GeneratedServiceStubType, cls).__init__(name, bases, dictionary)

    if GeneratedServiceStubType._DESCRIPTOR_KEY not in dictionary:
      return
    descriptor = dictionary[GeneratedServiceStubType._DESCRIPTOR_KEY]
    service_stub_builder = _ServiceStubBuilder(descriptor)
    service_stub_builder.BuildServiceStub(cls)

Creates a message service stub class.

Args:
name: Name of the class (ignored, here).
bases: Base classes of the class being constructed.
dictionary: The class dictionary of the class being constructed.
dictionary[_DESCRIPTOR_KEY] must contain a ServiceDescriptor object
describing this protocol service type.


def _merge_field(self, json_value: Any, field: descriptor.FieldDescriptor, parent: message.Message) -> None:
    if not annotation_utils.is_primitive_type(field.message_type) and proto_utils.field_is_set(parent, field):
        raise ValueError(f'Target field {field.full_name} is already set.')
    if field.containing_oneof is not None:
        oneof_field = parent.DESCRIPTOR.oneofs_by_name[field.containing_oneof.name]
        if annotation_utils.is_primitive_type(field.message_type) and oneof_field.full_name == field.full_name:
            raise ValueError(f'Cannot set field {field.full_name} since oneof field {oneof_field.full_name} is already set.')
    existing_field_size = proto_utils.field_content_length(parent, field)
    if proto_utils.field_is_repeated(field):
        if not isinstance(json_value, list):
            raise ValueError(f'Attempted to merge a repeated field, {field.name}, a json_value with type {type(json_value)} instead of a list.')
        if existing_field_size != 0 and existing_field_size != len(json_value):
            raise ValueError('Repeated primitive list length does not match extension list for field: {field.full_name!r}.')
    json_value = json_value if proto_utils.field_is_repeated(field) else [json_value]
    for i, value in enumerate(json_value):
        parsed_value = self._parse_field_value(field, value)
        if existing_field_size > 0:
            field_value = proto_utils.get_value_at_field_index(parent, field, i)
            field_value.MergeFrom(parsed_value)
            extensions.clear_fhir_extensions_with_url(field_value, extensions.PRIMITIVE_HAS_NO_VALUE_URL)
        else:
            field_value = proto_utils.set_in_parent_or_add(parent, field)
            field_value.MergeFrom(parsed_value)

Merges the json_value into the provided field of the parent Message.

Args:
json_value: The JSON value to set.
field: The FieldDescriptor of the field to set in parent.
parent: The parent Message to set the value on.

Raises:
ValueError: In the event that a non-primitive field has already been set.
ValueError: In the event that a oneof field has already been set.


def _get_scopes(state, names: Sequence[str], ctx) -> Sequence[abstract.InterpreterClass | abstract.InterpreterFunction]:
    scopes = []
    for name in names:
        prev = scopes[-1] if scopes else None
        if not prev:
            try:
                _, var = ctx.vm.load_global(state, name)
            except KeyError:
                break
        elif isinstance(prev, abstract.InterpreterClass):
            if name in prev.members:
                var = prev.members[name]
            else:
                break
        else:
            assert isinstance(prev, abstract.InterpreterFunction)
            if prev.last_frame and name in prev.last_frame.f_locals.pyval:
                var = prev.last_frame.f_locals.pyval[name]
            else:
                break
        try:
            scopes.append(abstract_utils.get_atomic_value(var, (abstract.InterpreterClass, abstract.InterpreterFunction)))
        except abstract_utils.ConversionError:
            break
    return scopes

Gets the class or function objects for a sequence of nested scope names.

For example, if the code under analysis is:
class Foo:
def f(self):
def g(): ...
then when called with ['Foo', 'f', 'g'], this method returns
[InterpreterClass(Foo), InterpreterFunction(f), InterpreterFunction(g)].

Arguments:
state: The current state.
names: A sequence of names for consecutive nested scopes in the module under
analysis. Must start with a module-level name.
ctx: The current context.

Returns:
The class or function object corresponding to each name in 'names'.


def add_loss(loss, loss_collection=ops.GraphKeys.LOSSES):
    if loss_collection and (not context.executing_eagerly()):
        ops.add_to_collection(loss_collection, loss)

Adds a externally defined loss to the collection of losses.

Args:
loss: A loss `Tensor`.
loss_collection: Optional collection to add the loss to.


def in_same_dir(as_file, target_file):

    return os.path.abspath(os.path.join(os.path.dirname(as_file), target_file))

Return an absolute path to a target file that is located in the same directory as as_file

Args:
as_file: File name (including __file__)
Use the directory path of this file
target_file: Name of the target file


def fail_steamid(channel):

    gui = ui_embed.UI(
        channel,
        "That SteamID doesn't exist.",
        "You can get your SteamID by going to your profile page and looking at the url, "
        "or you can set a custom ID by going to edit profile on your profile page.",
        modulename=modulename,
        colour=0x0088FF
    )

    return gui

Creates an embed UI for invalid SteamIDs

Args:
channel (discord.Channel): The Discord channel to bind the embed to

Returns:
ui (ui_embed.UI): The embed UI object


def _build(self, one_hot_input_sequence):

    input_shape = one_hot_input_sequence.get_shape()
    batch_size = input_shape[1]

    batch_embed_module = snt.BatchApply(self._embed_module)
    input_sequence = batch_embed_module(one_hot_input_sequence)
    input_sequence = tf.nn.relu(input_sequence)

    initial_state = self._core.initial_state(batch_size)

    if self._use_dynamic_rnn:
      output_sequence, final_state = tf.nn.dynamic_rnn(
          cell=self._core,
          inputs=input_sequence,
          time_major=True,
          initial_state=initial_state)
    else:
      rnn_input_sequence = tf.unstack(input_sequence)
      output, final_state = tf.contrib.rnn.static_rnn(
          cell=self._core,
          inputs=rnn_input_sequence,
          initial_state=initial_state)
      output_sequence = tf.stack(output)

    batch_output_module = snt.BatchApply(self._output_module)
    output_sequence_logits = batch_output_module(output_sequence)

    return output_sequence_logits, final_state

Builds the deep LSTM model sub-graph.

Args:
one_hot_input_sequence: A Tensor with the input sequence encoded as a
one-hot representation. Its dimensions should be `[truncation_length,
batch_size, output_size]`.

Returns:
Tuple of the Tensor of output logits for the batch, with dimensions
`[truncation_length, batch_size, output_size]`, and the
final state of the unrolled core,.


def xw_plus_b(x, weights, biases, name=None):
    with ops.name_scope(name, 'xw_plus_b', [x, weights, biases]) as name:
        x = ops.convert_to_tensor(x, name='x')
        weights = ops.convert_to_tensor(weights, name='weights')
        biases = ops.convert_to_tensor(biases, name='biases')
        mm = math_ops.matmul(x, weights)
        return bias_add(mm, biases, name=name)

Computes matmul(x, weights) + biases.

Args:
x: a 2D tensor.  Dimensions typically: batch, in_units
weights: a 2D tensor.  Dimensions typically: in_units, out_units
biases: a 1D tensor.  Dimensions: out_units
name: A name for the operation (optional).  If not specified
"xw_plus_b" is used.

Returns:
A 2-D Tensor computing matmul(x, weights) + biases.
Dimensions typically: batch, out_units.


def add_argument_to(self, parser):

        from devassistant.cli.devassistant_argparse import DefaultIffUsedActionFactory
        if isinstance(self.kwargs.get('action', ''), list):

            if self.kwargs['action'][0] == 'default_iff_used':
                self.kwargs['action'] = DefaultIffUsedActionFactory.generate_action(
                    self.kwargs['action'][1])

        self.kwargs.pop('preserved', None)
        try:
            parser.add_argument(*self.flags, **self.kwargs)
        except Exception as ex:
            problem = "Error while adding argument '{name}': {error}".\
                format(name=self.name, error=repr(ex))
            raise exceptions.ExecutionException(problem)

Used by cli to add this as an argument to argparse parser.

Args:
parser: parser to add this argument to


def grow(self, times=1):
    self.nodes.append([])
    for (n, node) in enumerate(self.nodes[self.age]):
        if (self.age == 0):
            p_node = Node(self.pos[:2])
        else:
            p_node = self._get_node_parent((self.age - 1), n)
        angle = node.get_node_angle(p_node)
        for i in range(self.comp):
            tot_angle = self.__get_total_angle(angle, i)
            length = self.__get_total_length((self.age + 1), i)
            self.nodes[(self.age + 1)].append(node.make_new_node(length, tot_angle))
    self.age += 1
    if (times > 1):
        self.grow((times - 1))

Let the tree grow.

Args:
times (integer): Indicate how many times the tree will grow.


def logged(level=logging.DEBUG):

    def wrap(f):
        _logger = logging.getLogger('{}.{}'.format(f.__module__, f.__name__))

        def wrapped_f(*args, **kwargs):
            _logger.log(level, 'Called at {} with args = {} and kwargs = {}'.format(datetime.datetime.now(), args, kwargs))
            data = f(*args, **kwargs)
            _logger.log(level, 'Done at {} with args = {} and kwargs = {}'.format(datetime.datetime.now(), args, kwargs))
            return data
        return wrapped_f
    return wrap

Useful logging decorator. If a method is logged, the beginning and end of
the method call will be logged at a pre-specified level.

Args:
level: Level to log method at. Defaults to DEBUG.


def _GetUnsortedNotifications(self, queue_shard, notifications_by_session_id=None):
    if (notifications_by_session_id is None):
        notifications_by_session_id = {}
    end_time = (self.frozen_timestamp or rdfvalue.RDFDatetime.Now())
    for notification in self.data_store.GetNotifications(queue_shard, end_time):
        existing = notifications_by_session_id.get(notification.session_id)
        if existing:
            if (notification.first_queued > existing.first_queued):
                notifications_by_session_id[notification.session_id] = notification
            elif ((notification.first_queued == existing.first_queued) and (notification.last_status > existing.last_status)):
                logging.warning('Notifications with equal first_queued fields detected: %s %s', notification, existing)
                notifications_by_session_id[notification.session_id] = notification
        else:
            notifications_by_session_id[notification.session_id] = notification
    return notifications_by_session_id

Returns all the available notifications for a queue_shard.

Args:
queue_shard: urn of queue shard
notifications_by_session_id: store notifications in this dict rather than
creating a new one

Returns:
dict of notifications. keys are session ids.


def forward(self, hidden_states: torch.FloatTensor, attention_mask: torch.FloatTensor, layer_head_mask: torch.FloatTensor, output_attentions: Optional[bool]=False) -> Tuple[torch.FloatTensor, Optional[torch.FloatTensor]]:
    residual = hidden_states
    hidden_states, attn_weights, _ = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask, layer_head_mask=layer_head_mask, output_attentions=output_attentions)
    hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
    hidden_states = residual + hidden_states
    hidden_states = self.self_attn_layer_norm(hidden_states)
    residual = hidden_states
    hidden_states = self.activation_fn(self.fc1(hidden_states))
    hidden_states = nn.functional.dropout(hidden_states, p=self.activation_dropout, training=self.training)
    hidden_states = self.fc2(hidden_states)
    hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
    hidden_states = residual + hidden_states
    hidden_states = self.final_layer_norm(hidden_states)
    if hidden_states.dtype == torch.float16 and (torch.isinf(hidden_states).any() or torch.isnan(hidden_states).any()):
        clamp_value = torch.finfo(hidden_states.dtype).max - 1000
        hidden_states = torch.clamp(hidden_states, min=-clamp_value, max=clamp_value)
    outputs = (hidden_states,)
    if output_attentions:
        outputs += (attn_weights,)
    return outputs

Args:
hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
attention_mask (`torch.FloatTensor`): attention mask of size
`(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
layer_head_mask (`torch.FloatTensor`): mask for attention heads in a given layer of size
`(encoder_attention_heads,)`.
output_attentions (`bool`, *optional*):
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
returned tensors for more detail.


def parse_flags_with_usage(args):

  try:
    return FLAGS(args)
  except flags.Error as error:
    sys.stderr.write('FATAL Flags parsing error: %s\n' % error)
    sys.stderr.write('Pass --helpshort or --helpfull to see help on flags.\n')
    sys.exit(1)

Tries to parse the flags, print usage, and exit if unparseable.

Args:
args: [str], a non-empty list of the command line arguments including
program name.

Returns:
[str], a non-empty list of remaining command line arguments after parsing
flags, including program name.


def __init__(self, campfire, data):

        dataType = type(data)
        if dataType == types.StringType or dataType == types.UnicodeType:
            messageType = self._TYPE_PASTE if data.find("\n") >= 0 else self._TYPE_TEXT
            if messageType == self._TYPE_TEXT:
                matches = re.match("^https?:
                if matches:
                    messageType = self._TYPE_TWEET
            data = {
                "type": messageType,
                "body": data
            }

        super(Message, self).__init__(campfire)

        self.set_data(data, ["created_at"])

        self.user = None
        self.room = None

        if "user_id" in data and data["user_id"]:
            self.user = self._campfire.get_user(data["user_id"])
        if "room_id" in data and data["room_id"]:
            self.room = self._campfire.get_room(data["room_id"])
            if self.is_upload():
                self.upload = self._connection.get("room/%s/messages/%s/upload" % (self.room.id, self.id), key="upload")
                if "full_url" in self.upload:
                    self.upload["url"] = self.upload["full_url"]
                    del self.upload["full_url"]

        if self.is_tweet():

            matches = re.match("(.+)\s+--\s+@([^,]+),\s*(.+)$", self.body)
            if matches:
                self.tweet = {
                    "tweet": matches.group(1),
                    "user": matches.group(2),
                    "url": matches.group(3)
                }
            else:
                tweet_data = {}
                if re.match("^---", self.body):
                    for line in self.body.split("\n")[1:]:
                        matches = re.match('^:([^:]+):\s*"?(.+)"?$', line)
                        if matches:
                            tweet_data[matches.group(1)] = matches.group(2)

                if tweet_data and "author_username" in tweet_data and "message" in tweet_data and "id" in tweet_data:
                    self.tweet = {
                        "tweet": tweet_data["message"],
                        "user": tweet_data["author_username"],
                        "url": "http:
                    }
                else:
                    self.type = self._TYPE_TEXT

Initialize.

Args:
campfire (:class:`Campfire`): Campfire instance
data (dict or str): If string, message type will be set to either paste or text


def select_embedding_from_source(cur, source_nodelist, source_edgelist, target_nodelist, target_edgelist):
    encoded_data = {'target_num_nodes': len(target_nodelist), 'target_num_edges': len(target_edgelist), 'target_edges': json.dumps(target_edgelist, separators=(',', ':')), 'source_num_nodes': len(source_nodelist), 'source_num_edges': len(source_edgelist), 'source_edges': json.dumps(source_edgelist, separators=(',', ':'))}
    select = '\n        SELECT\n            source_node,\n            chain\n        FROM\n            embedding_component_view\n        WHERE\n            source_num_edges = :source_num_edges AND\n            source_edges = :source_edges AND\n            source_num_nodes = :source_num_nodes AND\n\n            target_num_edges = :target_num_edges AND\n            target_edges = :target_edges AND\n            target_num_nodes = :target_num_nodes\n        '
    embedding = {v: json.loads(chain) for (v, chain) in cur.execute(select, encoded_data)}
    return embedding

Select an embedding from the source graph and target graph.

Args:
cur (:class:`sqlite3.Cursor`):
An sqlite3 cursor. This function is meant to be run within a :obj:`with` statement.

target_nodelist (list):
The nodes in the target graph. Should be integer valued.

target_edgelist (list):
The edges in the target graph.

embedding_tag (str):
A string tag to associate with the embedding.

Returns:
dict: The mapping from the source graph to the target graph.
In the form {v: {s, ...}, ...} where v is a variable in the
source model and s is a variable in the target model.


def Substitute(self, pattern):

    if isinstance(pattern, bytes):
      substs = [re.escape(subst.encode("ascii")) for subst in self._substs]
      regex = re.compile(b"|".join(substs))

      def Replacement(match):
        key = match.group(0).decode("ascii")
        return self._substs[key].encode("utf-8")

    elif isinstance(pattern, Text):
      substs = [re.escape(subst) for subst in self._substs]
      regex = re.compile("|".join(substs))

      def Replacement(match):
        key = match.group(0)
        return self._substs[key]

    else:
      raise TypeError("Unexpected pattern type '{}'".format(type(pattern)))

    if not substs:
      return pattern
    else:
      return regex.sub(Replacement, pattern)

Formats given pattern with this substitution environment.

A pattern can contain placeholders for variables (`%%foo%%`) and scopes
(`%%bar.baz%%`) that are replaced with concrete values in this substiution
environment (specified in the constructor).

Args:
pattern: A pattern with placeholders to substitute.

Returns:
A pattern with placeholders substituted with concrete values.


def __init__(self, server_address, username, password):

        _wtflog.info(
            "connecting to %s, using %s:%s", server_address, username, password)
        self._mail = imaplib.IMAP4_SSL(server_address)
        self._mail.login(username, password)
        _wtflog.info("connected.")

Constructor

Args:
server_address (str): Email Server address.
username (str): Username
password (str): Password


def invoke(self, line):
    finished = True
    while (len(line) > 0):
        (val, line, finished) = self.invoke_one(line)
        if (val is not None):
            iprint(val)
    return finished

Invoke a one or more function given a list of arguments.

The functions are searched for using the current context on the context stack
and its annotated type information is used to convert all of the string parameters
passed in line to appropriate python types.

Args:
line (list): The list of command line arguments.

Returns:
bool: A boolean specifying if the last function created a new context
(False if a new context was created) and a list with the remainder of the
command line if this function did not consume all arguments.)


def jsoned(struct, wrap=True, meta=None, struct_key='result', pre_render_callback=None):
    return _json.dumps(structured(struct, wrap=wrap, meta=meta, struct_key=struct_key, pre_render_callback=pre_render_callback), default=json_encoder)

Provides a json dump of the struct

Args:
struct: The data to dump
wrap (bool, optional): Specify whether to wrap the
struct in an enclosing dict
struct_key (str, optional): The string key which will
contain the struct in the result dict
meta (dict, optional): An optional dictonary to merge
with the output dictionary.

Examples:

>>> jsoned([3,4,5])
... '{"status": "success", "result": [3, 4, 5]}'

>>> jsoned([3,4,5], wrap=False)
... '[3, 4, 5]'


def tracker(obj):

    import types as typ
    global oids, uuids
    import six
    from inspect import isclass
    untracked = (six.string_types, six.integer_types, float,
                 complex, six.text_type)

    semitrack = (list, dict, set, tuple)
    if six.PY3:
        semitrack = semitrack + (range, filter, map)

    if (isinstance(obj, semitrack) and
        all([isinstance(t, untracked) for t in obj])):
        if len(obj) > 0:
            semiform = "{0} len={1:d} min={2} max={3}"
            return semiform.format(type(obj), len(obj), min(obj), max(obj))
        else:
            semiform = "{0} len={1:d}"
            return semiform.format(type(obj), len(obj))
    elif isinstance(obj, semitrack):

        result = []

        for o in obj[0:min((len(obj), 5))]:
            track = tracker(o)
            if isinstance(track, Instance):
                result.append(track.uuid)
            else:
                result.append(track)

        if len(obj) > 5:
            result.append("... ({0:d} items)".format(len(obj)))

        return tuple(result)
    elif isinstance(obj, slice):
        return "slice({}, {}, {})".format(obj.start, obj.stop, obj.step)
    elif type(obj) is type:
        return obj.__name__
    elif type(obj) is typ.LambdaType:
        if hasattr(obj, "__fqdn__"):

            return obj.__fqdn__
        else:
            if six.PY2:
                _code = obj.func_code
            else:
                _code = obj.__code__
            return "lambda ({})".format(', '.join(_code.co_varnames))
    elif type(obj) in [typ.FunctionType, typ.MethodType]:
        return obj.__name__
    elif not isinstance(obj, untracked):

        oid = id(obj)
        if oid in oids:
            result = oids[oid]
        else:
            result = Instance(oid, obj)
            oids[oid] = result
            uuids[result.uuid] = result
        return result
    else:
        return None

Returns the :class:`Instance` of the specified object if it is one that
we track by default.

Args:
obj (object): any python object passed as an argument to a method.

Returns:
Instance: if the object is trackable, the Instance instance of
that object; else None.


def with_rank(self, rank):
    try:
        return self.merge_with(unknown_shape(rank=rank))
    except ValueError:
        raise ValueError('Shape %s must have rank %d' % (self, rank))

Returns a shape based on `self` with the given rank.

This method promotes a completely unknown shape to one with a
known rank.

Args:
rank: An integer.

Returns:
A shape that is at least as specific as `self` with the given rank.

Raises:
ValueError: If `self` does not represent a shape with the given `rank`.


def sanity_check_actions(actions_spec):

    actions = copy.deepcopy(actions_spec)

    is_unique = ('type' in actions)
    if is_unique:
        actions = dict(action=actions)

    for name, action in actions.items():

        if 'type' not in action:
            action['type'] = 'int'

        if action['type'] == 'int':
            if 'num_actions' not in action:
                raise TensorForceError("Action requires value 'num_actions' set!")
        elif action['type'] == 'float':
            if ('min_value' in action) != ('max_value' in action):
                raise TensorForceError("Action requires both values 'min_value' and 'max_value' set!")

        if 'shape' not in action:
            action['shape'] = ()

        if isinstance(action['shape'], int):
            action['shape'] = (action['shape'],)

    return actions, is_unique

Sanity checks an actions dict, used to define the action space for an MDP.
Throws an error or warns if mismatches are found.

Args:
actions_spec (Union[None,dict]): The spec-dict to check (or None).

Returns: Tuple of 1) the action space desc and 2) whether there is only one component in the action space.


def CreateDataTypeMapByType(cls, data_type_definition):
    data_type_map_class = cls._MAP_PER_DEFINITION.get(data_type_definition.TYPE_INDICATOR, None)
    if (not data_type_map_class):
        return None
    return data_type_map_class(data_type_definition)

Creates a specific data type map by type indicator.

Args:
data_type_definition (DataTypeDefinition): data type definition.

Returns:
DataTypeMap: data type map or None if the date type definition
is not available.


def _TensorArrayConcatGrad(op: ops.Operation, grad, unused_lengths_grad):
    handle = op.inputs[0]
    flow = op.inputs[1]
    lengths = op.outputs[1]
    dtype = op.get_attr('dtype')
    grad_source = _GetGradSource(grad)
    g = tensor_array_ops.TensorArray(dtype=dtype, handle=handle, flow=flow, colocate_with_first_write_call=False).grad(source=grad_source, flow=flow)
    u_g = g.split(grad, lengths=lengths)
    return [None, u_g.flow]

Gradient for TensorArrayConcat.

Args:
op: Forward TensorArrayConcat op.
grad: Gradient `Tensor` to TensorArrayConcat.

Returns:
A flow `Tensor`, which can be used in control dependencies to
force the write of `grad` to the gradient `TensorArray`.


def _ProcessFileEntry(self, mediator, file_entry):

    display_name = mediator.GetDisplayName()
    logger.debug(
        '[ProcessFileEntry] processing file entry: {0:s}'.format(display_name))

    reference_count = mediator.resolver_context.GetFileObjectReferenceCount(
        file_entry.path_spec)

    try:
      if self._IsMetadataFile(file_entry):
        self._ProcessMetadataFile(mediator, file_entry)

      else:
        file_entry_processed = False
        for data_stream in file_entry.data_streams:
          if self._abort:
            break

          if self._CanSkipDataStream(file_entry, data_stream):
            logger.debug((
                '[ProcessFileEntry] Skipping datastream {0:s} for {1:s}: '
                '{2:s}').format(
                    data_stream.name, file_entry.type_indicator, display_name))
            continue

          self._ProcessFileEntryDataStream(mediator, file_entry, data_stream)

          file_entry_processed = True

        if not file_entry_processed:

          self._ProcessFileEntryDataStream(mediator, file_entry, None)

    finally:
      new_reference_count = (
          mediator.resolver_context.GetFileObjectReferenceCount(
              file_entry.path_spec))
      if reference_count != new_reference_count:

        if mediator.resolver_context.ForceRemoveFileObject(
            file_entry.path_spec):
          logger.warning(
              'File-object not explicitly closed for file: {0:s}'.format(
                  display_name))

    logger.debug(
        '[ProcessFileEntry] done processing file entry: {0:s}'.format(
            display_name))

Processes a file entry.

Args:
mediator (ParserMediator): mediates the interactions between
parsers and other components, such as storage and abort signals.
file_entry (dfvfs.FileEntry): file entry.


def wait_for_batches(self, batch_ids, timeout=None):

        self._batch_tracker.watch_statuses(self, batch_ids)
        timeout = timeout or DEFAULT_TIMEOUT
        start_time = time()

        with self._wait_condition:
            while True:
                if self._statuses is not None:
                    return _format_batch_statuses(
                        self._statuses, batch_ids, self._batch_tracker)

                if time() - start_time > timeout:
                    statuses = self._batch_tracker.get_statuses(batch_ids)
                    return _format_batch_statuses(
                        statuses, batch_ids, self._batch_tracker)

                self._wait_condition.wait(timeout - (time() - start_time))

Locks until a list of batch ids is committed to the block chain
or a timeout is exceeded. Returns the statuses of those batches.

Args:
batch_ids (list of str): The ids of the batches to wait for
timeout(int): Maximum time in seconds to wait for

Returns:
list of BatchStatus: BatchStatuses to send back to client


def ParseCmd(self, cmd_input, attributes=None, templates=None):

        self.raw = cmd_input

        if not templates:

            row_idx = self.index.GetRowMatch(attributes)
            if row_idx:
                templates = self.index.index[row_idx]["Template"]
            else:
                raise CliTableError(
                    'No template found for attributes: "%s"' % attributes
                )

        template_files = self._TemplateNamesToFiles(templates)

        try:

            self.Reset()
            self._keys = set()
            self.table = self._ParseCmdItem(self.raw, template_file=template_files[0])

            for tmplt in template_files[1:]:
                self.extend(
                    self._ParseCmdItem(self.raw, template_file=tmplt), set(self._keys)
                )
        finally:
            for f in template_files:
                f.close()

Creates a TextTable table of values from cmd_input string.
Parses command output with template/s. If more than one template is found
subsequent tables are merged if keys match (dropped otherwise).
Args:
cmd_input: String, Device/command response.
attributes: Dict, attribute that further refine matching template.
templates: String list of templates to parse with. If None, uses index
Raises:
CliTableError: A template was not found for the given command.


def is_global(self):
    return ((not ((self.network_address in IPv4Network('100.64.0.0/10')) and (self.broadcast_address in IPv4Network('100.64.0.0/10')))) and (not self.is_private))

Test if this address is allocated for public networks.

Returns:
A boolean, True if the address is not reserved per
iana-ipv4-special-registry.


def _query(self, query_type, query_str, verbose=False):

        cached = self.query_cache.get(query_str)
        if cached:
            if verbose:
                print('Returning Cached VT Query Results')
            return cached

        if query_type == 'file':
            response = requests.get('https:
                                    params={'apikey': self.apikey, 'resource': query_str, 'allinfo': 1})
        else:
            response = requests.post('https:
                                     params={'apikey': self.apikey, 'resource': query_str, 'allinfo': 1})

        try:
            vt_output = response.json()
        except ValueError:
            error_msg = 'VirusTotal no valid response, throttling and trying again...'
            if self.throttle:
                if verbose:
                    print(error_msg)
                time.sleep(30)
                return self._query(query_type, query_str)

            return {'vt_error': error_msg}

        if not vt_output or vt_output['response_code'] == 0:
            output = {'query': query_str, 'not_found': True}
            self.query_cache.set(query_str, output)
            return output

        output = {field: vt_output[field] for field in vt_output.keys() if field not in self.exclude}

        output['query'] = query_str

        scan_results = collections.Counter()
        for scan in vt_output['scans'].values():
            if 'result' in scan:
                if scan['result']:
                    scan_results[scan['result']] += 1
        output['scan_results'] = scan_results.most_common(5)

        self.query_cache.set(query_str, output)

        return output

Internal query method for the VirusTotal Service
Args:
query_type(str): The type of query (either 'file' or 'url')
query_str (str): The file hash or domain/url to be queried


def zeros(shape, dtype=None, **kwargs):

    data = np.zeros(shape, dtype)
    return dc.array(data, **kwargs)

Create an array of given shape and type, filled with zeros.

Args:
shape (sequence of ints): 2D shape of the array.
dtype (data-type, optional): Desired data-type for the array.
kwargs (optional): Other arguments of the array (*coords, attrs, and name).

Returns:
array (decode.array): Decode array filled with zeros.


def GetCredential(self, path_spec, identifier):
    credentials = self._credentials_per_path_spec.get(path_spec.comparable, {})
    return credentials.get(identifier, None)

Retrieves a specific credential from the key chain.

Args:
path_spec (PathSpec): path specification.
identifier (str): credential identifier.

Returns:
object: credential or None if the credential for the path specification
is not set.


def init_cache(self, batch_size, max_length):
    input_ids = jnp.ones((batch_size, max_length), dtype='i4')
    attention_mask = jnp.ones_like(input_ids, dtype='i4')
    position_ids = jnp.broadcast_to(jnp.arange(jnp.atleast_2d(input_ids).shape[-1]), input_ids.shape)
    init_variables = self.module.init(jax.random.PRNGKey(0), input_ids, attention_mask, position_ids, return_dict=False, init_cache=True)
    return unfreeze(init_variables['cache'])

Args:
batch_size (`int`):
batch_size used for fast auto-regressive decoding. Defines the batch size of the initialized cache.
max_length (`int`):
maximum possible length for auto-regressive decoding. Defines the sequence length of the initialized
cache.


def update(self, domain, type_name, search_command, body):

        return self._request(domain, type_name, search_command, 'PUT', body)

Update entry in ThreatConnect Data Store

Args:
domain (string): One of 'local', 'organization', or 'system'.
type_name (string): This is a free form index type name. The ThreatConnect API will use
this resource verbatim.
search_command (string): Search command to pass to ES.
body (str): JSON body


def key_validation_check(tweet_keys_list, superset_keys, minset_keys):
    tweet_keys = set(tweet_keys_list)
    minset_overlap = (tweet_keys & minset_keys)
    if (minset_overlap != minset_keys):
        raise UnexpectedFormatError('keys ({}) missing from Tweet (Public API data is not supported)'.format((minset_keys - tweet_keys)))
    unexpected_keys = (tweet_keys - superset_keys)
    if (len(unexpected_keys) > 0):
        raise UnexpectedFormatError('Unexpected keys ({}) are in this Tweet'.format(unexpected_keys))
    return 0

Validates the keys present in a Tweet.

Args:
tweet_keys_list (list): the keys present in a tweet
superset_keys (set): the set of all possible keys for a tweet
minset_keys (set): the set of minimal keys expected in a tweet.

Returns:
0 if no errors

Raises:
UnexpectedFormatError on any mismatch of keys.


def add(self, coro, *args, **kw):
    if asyncio.iscoroutinefunction(coro):
        coro = coro(*args, **kw)
    if (not asyncio.iscoroutine(coro)):
        raise TypeError('paco: coro must be a coroutine object')
    index = max(len(self.pool), 0)
    task = Task(index, coro)
    self.pool.append(task)
    return coro

Adds a new coroutine function with optional variadic argumetns.

Arguments:
coro (coroutine function): coroutine to execute.
*args (mixed): optional variadic arguments

Raises:
TypeError: if the coro object is not a valid coroutine

Returns:
future: coroutine wrapped future


def charge_balance(model):

    compound_charge = {}
    for compound in model.compounds:
        if compound.charge is not None:
            compound_charge[compound.id] = compound.charge

    for reaction in model.reactions:
        charge = reaction_charge(reaction.equation, compound_charge)
        yield reaction, charge

Calculate the overall charge for all reactions in the model.

Yield (reaction, charge) pairs.

Args:
model: :class:`psamm.datasource.native.NativeModel`.


def clip_range(nodes1, nodes2):
    (coeff_a, coeff_b, coeff_c, d_min, d_max) = compute_fat_line(nodes1)
    (_, num_nodes2) = nodes2.shape
    polynomial = np.empty((2, num_nodes2), order='F')
    denominator = float((num_nodes2 - 1))
    for index in six.moves.xrange(num_nodes2):
        polynomial[(0, index)] = (index / denominator)
        polynomial[(1, index)] = (((coeff_a * nodes2[(0, index)]) + (coeff_b * nodes2[(1, index)])) + coeff_c)
    start_bottom = np.asfortranarray([0.0, d_min])
    end_bottom = np.asfortranarray([1.0, d_min])
    start_top = np.asfortranarray([0.0, d_max])
    end_top = np.asfortranarray([1.0, d_max])
    s_min = DEFAULT_S_MIN
    s_max = DEFAULT_S_MAX
    for start_index in six.moves.xrange((num_nodes2 - 1)):
        for end_index in six.moves.xrange((start_index + 1), num_nodes2):
            (s_min, s_max) = _update_parameters(s_min, s_max, start_bottom, end_bottom, polynomial[(:, start_index)], polynomial[(:, end_index)])
            (s_min, s_max) = _update_parameters(s_min, s_max, start_top, end_top, polynomial[(:, start_index)], polynomial[(:, end_index)])
    return _check_parameter_range(s_min, s_max)

r"""Reduce the parameter range where two curves can intersect.

Does so by using the "fat line" for ``nodes1`` and computing the
distance polynomial against ``nodes2``.

.. note::

This assumes, but does not check that the curves being considered
will only have one intersection in the parameter ranges
:math:`s \in \left[0, 1\right]`, :math:`t \in \left[0, 1\right]`.
This assumption is based on the fact that B |eacute| zier clipping
is meant to be used to find tangent intersections for already
subdivided (i.e. sufficiently zoomed in) curve segments.

Args:
nodes1 (numpy.ndarray): ``2 x N1`` array of nodes in a curve which
will define the clipping region.
nodes2 (numpy.ndarray): ``2 x N2`` array of nodes in a curve which
will be clipped.

Returns:
Tuple[float, float]: The pair of

* The start parameter of the clipped range.
* The end parameter of the clipped range.


def from_file(cls, filename, *, strict=True):
    config = cls()
    config.load_from_file(filename, strict=strict)
    return config

Create a new Config object from a configuration file.

Args:
filename (str): The location and name of the configuration file.
strict (bool): If true raises a ConfigLoadError when the configuration
cannot be found.

Returns:
An instance of the Config class.

Raises:
ConfigLoadError: If the configuration cannot be found.


def MakeSimpleProtoClass(fields, full_name=None, pool=None):

  factory = message_factory.MessageFactory(pool=pool)

  if full_name is not None:
    try:
      proto_cls = _GetMessageFromFactory(factory, full_name)
      return proto_cls
    except KeyError:

      pass

  field_items = fields.items()
  if not isinstance(fields, OrderedDict):
    field_items = sorted(field_items)

  fields_hash = hashlib.sha1()
  for f_name, f_type in field_items:
    fields_hash.update(f_name.encode('utf-8'))
    fields_hash.update(str(f_type).encode('utf-8'))
  proto_file_name = fields_hash.hexdigest() + '.proto'

  if full_name is None:
    full_name = ('net.proto2.python.public.proto_builder.AnonymousProto_' +
                 fields_hash.hexdigest())
    try:
      proto_cls = _GetMessageFromFactory(factory, full_name)
      return proto_cls
    except KeyError:

      pass

  factory.pool.Add(
      _MakeFileDescriptorProto(proto_file_name, full_name, field_items))
  return _GetMessageFromFactory(factory, full_name)

Create a Protobuf class whose fields are basic types.

Note: this doesn't validate field names!

Args:
fields: dict of {name: field_type} mappings for each field in the proto. If
this is an OrderedDict the order will be maintained, otherwise the
fields will be sorted by name.
full_name: optional str, the fully-qualified name of the proto type.
pool: optional DescriptorPool instance.
Returns:
a class, the new protobuf class with a FileDescriptor.


def _get_example_from_properties(self, spec):
    local_spec = deepcopy(spec)
    additional_property = False
    if ('additionalProperties' in local_spec):
        additional_property = True
        if ('properties' not in local_spec):
            local_spec['properties'] = {}
        local_spec['properties'].update({'any_prop1': local_spec['additionalProperties'], 'any_prop2': local_spec['additionalProperties']})
        del local_spec['additionalProperties']
        required = local_spec.get('required', [])
        required += ['any_prop1', 'any_prop2']
        local_spec['required'] = required
    example = {}
    properties = local_spec.get('properties')
    if (properties is not None):
        required = local_spec.get('required', properties.keys())
        for (inner_name, inner_spec) in properties.items():
            if (inner_name not in required):
                continue
            partial = self.get_example_from_prop_spec(inner_spec)
            if isinstance(partial, list):
                partial = partial[0]
            example[inner_name] = partial
    return (example, additional_property)

Get example from the properties of an object defined inline.

Args:
prop_spec: property specification you want an example of.

Returns:
An example for the given spec
A boolean, whether we had additionalProperties in the spec, or not


def _get_suffix(path):
    suffix = os.path.basename(path).split('.')[(- 1)]
    if ('/' in suffix):
        raise UserWarning(("Filename can't contain '/' in suffix (%s)!" % path))
    return suffix

Return suffix from `path`.

``/home/xex/somefile.txt`` --> ``txt``.

Args:
path (str): Full file path.

Returns:
str: Suffix.

Raises:
UserWarning: When ``/`` is detected in suffix.


def write_string_to_file(filename, file_content):
    with FileIO(filename, mode='w') as f:
        f.write(file_content)

Writes a string to a given file.

Args:
filename: string, path to a file
file_content: string, contents that need to be written to the file

Raises:
errors.OpError: If there are errors during the operation.


def baredoc(obj):

    doc = getdoc(obj)
    if not doc:
        return ''
    doc = doc.splitlines()[0]
    return doc.rstrip(' .').lstrip()

Return the first line of the docstring of an object.

Trailing periods and spaces as well as leading spaces are removed from the
output.

Args:
obj: any Python object.
Returns:
str: the first line of the docstring of obj.


def add_field(self, fieldname, fieldspec=whoosh_module_fields.TEXT):
    self._whoosh.add_field(fieldname, fieldspec)
    return self._whoosh.schema

Add a field in the index of the model.

Args:
fieldname (Text): This parameters register a new field in specified model.
fieldspec (Name, optional): This option adds various options as were described before.

Returns:
TYPE: The new schema after deleted is returned.


def create_projection(self, fov: float = 75.0, near: float = 1.0, far: float = 100.0, aspect_ratio: float = None):

        return matrix44.create_perspective_projection_matrix(
            fov,
            aspect_ratio or self.window.aspect_ratio,
            near,
            far,
            dtype='f4',
        )

Create a projection matrix with the following parameters.
When ``aspect_ratio`` is not provided the configured aspect
ratio for the window will be used.

Args:
fov (float): Field of view (float)
near (float): Camera near value
far (float): Camrea far value

Keyword Args:
aspect_ratio (float): Aspect ratio of the viewport

Returns:
The projection matrix as a float32 :py:class:`numpy.array`


def _randomized_roundoff_to_bfloat16(x, noise, cand1, cand2):
    cand1_f = tf.to_float(cand1)
    cand2_f = tf.to_float(cand2)
    step_size = (cand2_f - cand1_f)
    fpart = ((x - cand1_f) / step_size)
    ret = tf.where(tf.greater(fpart, noise), cand2, cand1)
    return ret

Round-off x to cand1 or to cand2 in an unbiased way.

Cand1 and cand2 are the same shape as x.
For every element of x, the corresponding elements of cand1 and cand2 should
be the two closest bfloat16 values to x.  Order does not matter.
cand1 and cand2 must differ from each other.

Args:
x: A float32 Tensor.
noise: A Tensor broadcastable to the shape of x containing
random uniform values in [0.0, 1.0].
cand1: A bfloat16 Tensor the same shape as x.
cand2: A bfloat16 Tensor the same shape as x.

Returns:
A bfloat16 Tensor.


def protorpc_to_endpoints_error(self, status, body):
    try:
        rpc_error = self.__PROTOJSON.decode_message(remote.RpcStatus, body)
    except (ValueError, messages.ValidationError):
        rpc_error = remote.RpcStatus()
    if (rpc_error.state == remote.RpcStatus.State.APPLICATION_ERROR):
        error_class = _ERROR_NAME_MAP.get(rpc_error.error_name)
        if error_class:
            (status, body) = self.__write_error(error_class.http_status, rpc_error.error_message)
    return (status, body)

Convert a ProtoRPC error to the format expected by Google Endpoints.

If the body does not contain an ProtoRPC message in state APPLICATION_ERROR
the status and body will be returned unchanged.

Args:
status: HTTP status of the response from the backend
body: JSON-encoded error in format expected by Endpoints frontend.

Returns:
Tuple of (http status, body)


def optimize(objective_function, domain,
             stopping_condition, parameters=None,
             position_update=functions.std_position,
             velocity_update=functions.std_velocity,
             parameter_update=functions.std_parameter_update,
             measurements=(),
             measurer=dictionary_based_metrics):

    params = __init_parameters__(parameters)

    rng = np.random.RandomState(params['seed'])

    initial_swarm = [functions.initialize_particle(rng, domain,
                                                   objective_function)
                     for i in range(params['swarm_size'])]
    state = types.PSOState(rng, params, iterations=0, swarm=initial_swarm)

    topology_function = state.params['topology']
    update_fitness = functions.update_fitness
    update_particle = functions.update_particle

    results, measure = measurer(measurements)
    while not stopping_condition(state):
        n_bests = topology_function(state)

        state = state._replace(swarm=[update_particle(position_update,
                                                      velocity_update,
                                                      state, n_bests, ip)
                                      for ip in enumerate(state.swarm)])

        state = state._replace(swarm=[update_fitness(objective_function,
                                                     particle)
                                      for particle in state.swarm],
                               iterations=state.iterations + 1)

        state = parameter_update(state, objective_function)

        results = measure(results, state)

    return functions.solution(state.swarm), results

Perform particle swarm optimization of the given fitness function.
Args:
objective_function: the cost function to optimize.
stopping_condition: function specifying the stopping condition.
parameters: dictionary: parameter dictionary for the PSO.

Returns:
cipy.algorithms.pso.Particle: The global best particle.


def get_metric_by_name(self, metric_name, **kwargs):
    return self._get_object_by_name(self._METRIC_ENDPOINT_SUFFIX, metric_name, **kwargs)

get a metric by name

Args:
metric_name (string): name of metric

Returns:
dictionary of response


def load(self, response):
    self._response = response
    if self.next_location(raw=True):
        self._num_redirects += 1

Load the response and increment the counter.

Args:
response (:class:`.http.request.Response`): The response from
a previous request.


def load_delivery_report(adapter: MongoAdapter, report_path: str, case_id: str, update: bool=False):
    case_obj = adapter.case(case_id=case_id)
    if (case_obj is None):
        raise DataNotFoundError('no case found')
    if (not case_obj.get('delivery_report')):
        _put_report_in_case_root(case_obj, report_path)
    elif update:
        _put_report_in_case_root(case_obj, report_path)
    else:
        raise IntegrityError('Existing delivery report found, use update = True to overwrite')
    logger.info('Saving report for case {} in database'.format(case_obj['_id']))
    return adapter.replace_case(case_obj)

Load a delivery report into a case in the database

If the report already exists the function will exit.
If the user want to load a report that is already in the database
'update' has to be 'True'

Args:
adapter     (MongoAdapter): Connection to the database
report_path (string):       Path to delivery report
case_id     (string):       Optional case identifier
update      (bool):         If an existing report should be replaced

Returns:
updated_case(dict)


def save_forensic_reports_to_kafka(self, forensic_reports, forensic_topic):

        if type(forensic_reports) == dict:
            forensic_reports = [forensic_reports]

        if len(forensic_reports) < 1:
            return

        try:
            logger.debug("Saving forensic reports to Kafka")
            self.producer.send(forensic_topic, forensic_reports)
        except UnknownTopicOrPartitionError:
            raise KafkaError(
                "Kafka error: Unknown topic or partition on broker")
        except Exception as e:
            raise KafkaError(
                "Kafka error: {0}".format(e.__str__()))
        try:
            self.producer.flush()
        except Exception as e:
            raise KafkaError(
                "Kafka error: {0}".format(e.__str__()))

Saves forensic DMARC reports to Kafka, sends individual
records (slices) since Kafka requires messages to be <= 1MB
by default.

Args:
forensic_reports (list):  A list of forensic report dicts
to save to Kafka
forensic_topic (str): The name of the Kafka topic


def WriteStatEntries(stat_entries, client_id, mutation_pool, token=None):
    for stat_response in stat_entries:
        if stat_response.pathspec.last.stream_name:
            stat_response.st_mode &= (~ stat_type_mask)
            stat_response.st_mode |= stat.S_IFREG
    if data_store.AFF4Enabled():
        for stat_entry in stat_entries:
            CreateAFF4Object(stat_entry, client_id_urn=rdf_client.ClientURN(client_id), mutation_pool=mutation_pool, token=token)
    if data_store.RelationalDBEnabled():
        path_infos = [rdf_objects.PathInfo.FromStatEntry(s) for s in stat_entries]
        data_store.REL_DB.WritePathInfos(client_id, _FilterOutPathInfoDuplicates(path_infos))

Persists information about stat entries.

Args:
stat_entries: A list of `StatEntry` instances.
client_id: An id of a client the stat entries come from.
mutation_pool: A mutation pool used for writing into the AFF4 data store.
token: A token used for writing into the AFF4 data store.


def get_session(db_url):

        engine = create_engine(db_url, poolclass=NullPool, echo=False)
        Session = sessionmaker(bind=engine)
        Base.metadata.create_all(engine)
        return Session()

Gets SQLAlchemy session given url. Your tables must inherit
from Base in hdx.utilities.database.

Args:
db_url (str): SQLAlchemy url

Returns:
sqlalchemy.orm.session.Session: SQLAlchemy session


def get_ip_prefixes_from_config(config, services, ip_version):
    ip_prefixes = set()
    for service in services:
        ip_prefix = ipaddress.ip_network(config.get(service, 'ip_prefix'))
        if (ip_prefix.version == ip_version):
            ip_prefixes.add(ip_prefix.with_prefixlen)
    return ip_prefixes

Build a set of IP prefixes found in service configuration files.

Arguments:
config (obg): A configparser object which holds our configuration.
services (list): A list of section names which are the name of the
service checks.
ip_version (int): IP protocol version

Returns:
A set of IP prefixes.


def as_graph(self, depth=0):

        if depth in self._graph_cache:
            return self._graph_cache[depth]
        self._graph_cache[depth] = graph = Graph(self, depth=depth)
        return graph

Create a graph with self as node, cache it, return it.

Args:
depth (int): depth of the graph.

Returns:
Graph: an instance of Graph.


def _launch_cli(self):
    self._register_this_run_info(self._run_cli)
    response = self._run_cli.run_ui(init_command=self._init_command, title=self._title, title_color=self._title_color)
    return response

Launch the interactive command-line interface.

Returns:
The OnRunStartResponse specified by the user using the "run" command.


def _wait_after(provider, job_ids, poll_interval, stop_on_failure):
    job_ids_to_check = {j for j in job_ids if (j != dsub_util.NO_JOB)}
    error_messages = []
    while (job_ids_to_check and ((not error_messages) or (not stop_on_failure))):
        print(('Waiting for: %s.' % ', '.join(job_ids_to_check)))
        jobs_left = _wait_for_any_job(provider, job_ids_to_check, poll_interval)
        jobs_completed = job_ids_to_check.difference(jobs_left)
        tasks_completed = provider.lookup_job_tasks({'*'}, job_ids=jobs_completed)
        dominant_job_tasks = _dominant_task_for_jobs(tasks_completed)
        if (len(dominant_job_tasks) != len(jobs_completed)):
            jobs_found = dsub_util.tasks_to_job_ids(dominant_job_tasks)
            jobs_not_found = jobs_completed.difference(jobs_found)
            for j in jobs_not_found:
                error = ('%s: not found' % j)
                print_error(('  %s' % error))
                error_messages += [error]
        for t in dominant_job_tasks:
            job_id = t.get_field('job-id')
            status = t.get_field('task-status')
            print(('  %s: %s' % (str(job_id), str(status))))
            if (status in ['FAILURE', 'CANCELED']):
                error_messages += [provider.get_tasks_completion_messages([t])]
        job_ids_to_check = jobs_left
    return error_messages

Print status info as we wait for those jobs.

Blocks until either all of the listed jobs succeed,
or one of them fails.

Args:
provider: job service provider
job_ids: a set of job IDs (string) to wait for
poll_interval: integer seconds to wait between iterations
stop_on_failure: whether to stop waiting if one of the tasks fails.

Returns:
Empty list if there was no error,
a list of error messages from the failed tasks otherwise.


def last_updated(self, path):
    return self._gcsIO().last_updated(path)

Get UNIX Epoch time in seconds on the FileSystem.

Args:
path: string path of file.

Returns: float UNIX Epoch time

Raises:
``BeamIOError``: if path doesn't exist.


def _HasDuplicateRegistryKeyPaths(
      self, filename, artifact_definition, source):

    result = False
    intersection = self._artifact_registry_key_paths.intersection(
        set(source.keys))
    if intersection:
      duplicate_key_paths = '\n'.join(intersection)
      logging.warning((
          'Artifact definition: {0:s} in file: {1:s} has duplicate '
          'Registry key paths:\n{2:s}').format(
              artifact_definition.name, filename, duplicate_key_paths))
      result = True

    self._artifact_registry_key_paths.update(source.keys)
    return result

Checks if Registry key paths are not already defined by other artifacts.

Note that at the moment this function will only find exact duplicate
Registry key paths.

Args:
filename (str): name of the artifacts definition file.
artifact_definition (ArtifactDefinition): artifact definition.
source (SourceType): source definition.

Returns:
bool: True if the Registry key paths defined by the source type
are used in other artifacts.


def scored_to_phenotype(self,phenotypes):

        def _apply_score(scored_calls,phenotypes):
            present = sorted(list(set(phenotypes)&set(scored_calls.keys())))
            total = sum([scored_calls[x] for x in present])
            if total > 1:
                raise ValueError("You cant extract phenotypes from scores if they are not mutually exclusive")
            if total == 0: return np.nan
            for label in present:
                if scored_calls[label] == 1: return label
            raise ValueError("Should have hit an exit criteria already")
        output = self.copy()
        output['phenotype_label'] = output.apply(lambda x: _apply_score(x['scored_calls'],phenotypes),1)

        output['phenotype_calls'] = output.apply(lambda x:
            dict([(y,1 if x['phenotype_label']==y else 0) for y in phenotypes])
        ,1)
        return output

Convert binary pehnotypes to mutually exclusive phenotypes.
If none of the phenotypes are set, then phenotype_label becomes nan
If any of the phenotypes are multiply set then it throws a fatal error.

Args:
phenotypes (list): a list of scored_names to convert to phenotypes

Returns:
CellDataFrame


def export(self, name=None):
    with tf.name_scope(name or '%s_lookup_table_export' % self._name):
        keys, values = gen_simple_hash_table_op.examples_simple_hash_table_export(self.resource_handle, key_dtype=self._key_dtype, value_dtype=self._value_dtype)
        return (keys, values)

Export all `key` and `value` pairs.

Args:
name: A name for the operation (optional).

Returns:
A tuple of two tensors, the first with the `keys` and the second with
the `values`.


def transform_content(tags, content_transformer):

    if type(tags) not in [tuple, list]:
        tags = [tags]

    for tag in tags:
        new_child = dhtmlparser.HTMLElement(content_transformer(tag))

        if hasattr(tag, "parent"):
            new_child.parent = tag

        tag.childs = [new_child]

Transform content in all `tags` using result of `content_transformer(tag)`
call.

Args:
tags (obj/list): HTMLElement instance, or list of HTMLElement
instances.
content_transformer (function): Function which is called as
``content_transformer(tag)``.


def _parse_hextet(self, hextet_str):

        if not self._HEX_DIGITS.issuperset(hextet_str):
            raise ValueError
        if len(hextet_str) > 4:
          raise ValueError
        hextet_int = int(hextet_str, 16)
        if hextet_int > 0xFFFF:
            raise ValueError
        return hextet_int

Convert an IPv6 hextet string into an integer.

Args:
hextet_str: A string, the number to parse.

Returns:
The hextet as an integer.

Raises:
ValueError: if the input isn't strictly a hex number from [0..FFFF].