def _collect_grades_data(self, enterprise_enrollment, course_details):
    if (self.grades_api is None):
        self.grades_api = GradesApiClient(self.user)
    course_id = enterprise_enrollment.course_id
    username = enterprise_enrollment.enterprise_customer_user.user.username
    try:
        grades_data = self.grades_api.get_course_grade(course_id, username)
    except HttpNotFoundError as error:
        if hasattr(error, 'content'):
            response_content = json.loads(error.content)
            if (response_content.get('error_code', '') == 'user_not_enrolled'):
                LOGGER.info('User [%s] not enrolled in course [%s], enterprise enrollment [%d]', username, course_id, enterprise_enrollment.pk)
                return (None, None, None)
        LOGGER.error('No grades data found for [%d]: [%s], [%s]', enterprise_enrollment.pk, course_id, username)
        return (None, None, None)
    course_end_date = course_details.get('end')
    if (course_end_date is not None):
        course_end_date = parse_datetime(course_end_date)
    now = timezone.now()
    is_passing = grades_data.get('passed')
    if ((course_end_date is not None) and (course_end_date < now)):
        completed_date = course_end_date
        grade = (self.grade_passing if is_passing else self.grade_failing)
    elif is_passing:
        completed_date = now
        grade = self.grade_passing
    else:
        completed_date = None
        grade = self.grade_incomplete
    return (completed_date, grade, is_passing)

Collect the learner completion data from the Grades API.

Used for self-paced courses.

Args:
enterprise_enrollment (EnterpriseCourseEnrollment): the enterprise enrollment record for which we need to
collect completion/grade data
course_details (dict): the course details for the course in the enterprise enrollment record.

Returns:
completed_date: Date the course was completed, this is None if course has not been completed.
grade: Current grade in the course.
is_passing: Boolean indicating if the grade is a passing grade or not.


def _input_optional(inp):

        if 'default' in inp.keys():
            return True

        typ = inp.get('type')
        if isinstance(typ, six.string_types):
            return typ.endswith('?')
        elif isinstance(typ, dict):

            return False
        elif isinstance(typ, list):

            return bool(u'null' in typ)
        else:
            raise ValueError('Invalid input "{}"'.format(inp.get['id']))

Returns True if a step input parameter is optional.

Args:
inp (dict): a dictionary representation of an input.

Raises:
ValueError: The inp provided is not valid.


def _split_bytecode(bytecode: list[opcodes.Opcode], processed_blocks: set[Block], python_version) -> list[Block]:
    targets = {op.target for op in bytecode if op.target}
    blocks = []
    code = []
    prev_block: Block = None
    i = 0
    while i < len(bytecode):
        op = bytecode[i]
        if python_version >= (3, 12) and isinstance(op, opcodes.SEND):
            if code:
                prev_block = Block(code)
                blocks.append(prev_block)
                code = []
            new_blocks, i = _preprocess_async_for_and_yield(i, bytecode, prev_block, processed_blocks)
            blocks.extend(new_blocks)
            prev_block = blocks[-1]
            continue
        code.append(op)
        if op.no_next() or op.does_jump() or op.pops_block() or (op.next is None) or (op.next in targets and (not isinstance(op.next, opcodes.GET_ANEXT) or python_version < (3, 12))):
            prev_block = Block(code)
            blocks.append(prev_block)
            code = []
        i += 1
    return blocks

Given a sequence of bytecodes, return basic blocks.

This will split the code at "basic block boundaries". These occur at
every instruction that is jumped to, and after every instruction that jumps
somewhere else (or returns / aborts).

Args:
bytecode: A list of instances of opcodes.Opcode. (E.g. returned from
opcodes.dis())

Returns:
A list of _Block instances.


def _tf_predict(model_dir, input_csvlines):

  with tf.Graph().as_default(), tf.Session() as sess:
    input_alias_map, output_alias_map = _tf_load_model(sess, model_dir)
    csv_tensor_name = list(input_alias_map.values())[0]
    results = sess.run(fetches=output_alias_map,
                       feed_dict={csv_tensor_name: input_csvlines})

  if len(input_csvlines) == 1:
    for k, v in six.iteritems(results):
      if not isinstance(v, (list, np.ndarray)):
        results[k] = [v]

  for k, v in six.iteritems(results):
    if any(isinstance(x, bytes) for x in v):
      results[k] = [x.decode('utf-8') for x in v]

  return results

Prediction with a tf savedmodel.

Args:
model_dir: directory that contains a saved model
input_csvlines: list of csv strings

Returns:
Dict in the form tensor_name:prediction_list. Note that the value is always
a list, even if there was only 1 row in input_csvlines.


def moving_average_variables(scope=None):
    return ops.get_collection(ops.GraphKeys.MOVING_AVERAGE_VARIABLES, scope)

Returns all variables that maintain their moving averages.

If an `ExponentialMovingAverage` object is created and the `apply()`
method is called on a list of variables, these variables will
be added to the `GraphKeys.MOVING_AVERAGE_VARIABLES` collection.
This convenience function returns the contents of that collection.

Args:
scope: (Optional.) A string. If supplied, the resulting list is filtered to
include only items whose `name` attribute matches `scope` using
`re.match`. Items without a `name` attribute are never returned if a scope
is supplied. The choice of `re.match` means that a `scope` without special
tokens filters by prefix.

Returns:
A list of Variable objects.


def protocol(alias_name, default=None, allow_none=False):

    warnings.warn('Will be removed in v1.0', DeprecationWarning, stacklevel=2)
    try:
        return _split_docker_link(alias_name)[0]
    except KeyError as err:
        if default or allow_none:
            return default
        else:
            raise err

Get the protocol from the docker link alias or return the default.

Args:
alias_name: The docker link alias
default: The default value if the link isn't available
allow_none: If the return value can be `None` (i.e. optional)

Examples:
Assuming a Docker link was created with ``docker --link postgres:db``
and the resulting environment variable is ``DB_PORT=tcp://172.17.0.82:5432``.

>>> envitro.docker.protocol('DB')
tcp


def compress_dir(path, compression='gz'):
    for (parent, subdirs, files) in os.walk(path):
        for f in files:
            compress_file(os.path.join(parent, f), compression=compression)

Recursively compresses all files in a directory. Note that this
compresses all files singly, i.e., it does not create a tar archive. For
that, just use Python tarfile class.

Args:
path (str): Path to parent directory.
compression (str): A compression mode. Valid options are "gz" or
"bz2". Defaults to gz.


def WriteTaskStart(self):
    self._RaiseIfNotWritable()
    if (self._storage_type != definitions.STORAGE_TYPE_TASK):
        raise IOError('Unsupported storage type.')
    task_start = self._task.CreateTaskStart()
    self._storage_file.WriteTaskStart(task_start)

Writes task start information.

Raises:
IOError: if the storage type is not supported or
when the storage writer is closed.
OSError: if the storage type is not supported or
when the storage writer is closed.


def extract_version(exepath, version_arg, word_index=-1, version_rank=3):

    if isinstance(version_arg, basestring):
        version_arg = [version_arg]
    args = [exepath] + version_arg

    stdout, stderr, returncode = _run_command(args)
    if returncode:
        raise RezBindError("failed to execute %s: %s\n(error code %d)"
                           % (exepath, stderr, returncode))

    stdout = stdout.strip().split('\n')[0].strip()
    log("extracting version from output: '%s'" % stdout)

    try:
        strver = stdout.split()[word_index]
        toks = strver.replace('.', ' ').replace('-', ' ').split()
        strver = '.'.join(toks[:version_rank])
        version = Version(strver)
    except Exception as e:
        raise RezBindError("failed to parse version from output '%s': %s"
                           % (stdout, str(e)))

    log("extracted version: '%s'" % str(version))
    return version

Run an executable and get the program version.

Args:
exepath: Filepath to executable.
version_arg: Arg to pass to program, eg "-V". Can also be a list.
word_index: Expect the Nth word of output to be the version.
version_rank: Cap the version to this many tokens.

Returns:
`Version` object.


def init_args(cls):

    try:

        argspec = getargspec(cls)
    except TypeError:

        argspec = getargspec(cls.__init__)
    args = argspec.args

    if args[0] == 'self':
        args.remove('self')

    return args

Return the __init__ args (minus 'self') for @cls

Args:
cls: class, instance or callable
Returns:
list of str, the arguments minus 'self'


def element_at(self, index):
    if self.closed():
        raise ValueError('Attempt to call element_at() on a closed Queryable.')
    if (index < 0):
        raise OutOfRangeError('Attempt to use negative index.')
    try:
        return self._iterable[index]
    except IndexError:
        raise OutOfRangeError('Index out of range.')
    except TypeError:
        pass
    for (i, item) in enumerate(self):
        if (i == index):
            return item
    raise OutOfRangeError('element_at(index) out of range.')

Return the element at ordinal index.

Note: This method uses immediate execution.

Args:
index: The index of the element to be returned.

Returns:
The element at ordinal index in the source sequence.

Raises:
ValueError: If the Queryable is closed().
ValueError: If index is out of range.


def HasDefinition(self, name):
    return name in self.consts or name in self.roles or name in self.states or (name in self.qualifiers) or (name in self.messages) or (name in self.events) or (name in self.transitions)

Whether this module has a named object |name|.

Args:
name: The string name of the object to look for.

Returns:
True if this module has an object with name |name|, False otherwise.


def __init__(self, token_provider):

    if token_provider is None:
      raise ValueError("token_provider is required")
    if not isinstance(token_provider, TokenProvider):
      raise ValueError("token_provider must be instance of TokenProvider")

    self.__token = token_provider.get_token()

The Neurio API client.

Args:
token_provider (TokenProvider): object providing authentication services


def _as_indexed_slices_list(inputs, optimize=True):
    if not isinstance(inputs, (list, tuple)):
        raise TypeError(f'Expected a list or tuple, not {type(inputs)}.')
    outputs = [_as_indexed_slices(i, optimize=optimize) for i in inputs]
    with_int32_index = [o.indices for o in outputs if o.indices.dtype == dtypes.int32]
    if not with_int32_index or len(with_int32_index) == len(outputs):
        return outputs
    casted_outputs = []
    for o in outputs:
        if o.indices.dtype == dtypes.int32:
            casted_outputs.append(indexed_slices.IndexedSlices(o.values, cast(o.indices, dtypes.int64), o.dense_shape))
        else:
            casted_outputs.append(o)
    return casted_outputs

Convert all elements of 'inputs' to IndexedSlices.

Additionally, homogenize the types of all the indices to
either int32 or int64.

Args:
inputs: List containing either Tensor or IndexedSlices objects.
optimize: if true, attempt to optimize the conversion of each input.

Returns:
A list of IndexedSlices objects.

Raises:
TypeError: If 'inputs' is not a list or a tuple.


def user_warning(channel, user, warnings, max_warnings):

    username = user.name
    if isinstance(user, discord.Member):
        if user.nick is not None:
            username = user.nick

    warning_count_text = "warnings" if warnings != 1 else "warning"
    warning_text = "{} {}".format(warnings, warning_count_text)
    result_text = "at {} you will be banned".format(max_warnings)
    if warnings >= max_warnings:
        result_text = "you are being banned because you have more than the maximum warnings"

    gui = ui_embed.UI(
        channel,
        "Warning {}".format(username),
        "You now have {} {}, {}".format(warning_text, username, result_text),
        modulename=modulename
    )

    return gui

Creates an embed UI containing an user warning message

Args:
channel (discord.Channel): The Discord channel to bind the embed to
user (discord.User): The user to warn
warnings (str): The warnings for the user
max_warnings (str): The maximum warnings for the user

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


def click_slot(self, slot, right=False):
    if isinstance(slot, int):
        slot = self.window.slots[slot]
    button = (constants.INV_BUTTON_RIGHT if right else constants.INV_BUTTON_LEFT)
    return self.send_click(windows.SingleClick(slot, button))

Left-click or right-click the slot.

Args:
slot (Slot): The clicked slot. Can be ``Slot`` instance or integer.
Set to ``inventory.cursor_slot``
for clicking outside the window.


def read_init() -> Dict[str, List[str]]:
    with open(os.path.join(PATH_TO_TRANSFORMERS, '__init__.py'), 'r', encoding='utf-8', newline='\n') as f:
        lines = f.readlines()
    line_index = 0
    while not lines[line_index].startswith('if TYPE_CHECKING'):
        line_index += 1
    backend_specific_objects = {}
    while line_index < len(lines):
        backend = find_backend(lines[line_index])
        if backend is not None:
            while not lines[line_index].startswith('    else:'):
                line_index += 1
            line_index += 1
            objects = []
            while len(lines[line_index]) <= 1 or lines[line_index].startswith(' ' * 8):
                line = lines[line_index]
                single_line_import_search = _re_single_line_import.search(line)
                if single_line_import_search is not None:
                    objects.extend(single_line_import_search.groups()[0].split(', '))
                elif line.startswith(' ' * 12):
                    objects.append(line[12:-2])
                line_index += 1
            backend_specific_objects[backend] = objects
        else:
            line_index += 1
    return backend_specific_objects

Read the init and extract backend-specific objects.

Returns:
Dict[str, List[str]]: A dictionary mapping backend name to the list of object names requiring that backend.


def set(self, time):
    self._time = time
    self._pb.sec = int(self._time)
    self._pb.nsec = int(((self._time - self._pb.sec) * (10 ** 9)))

Sets time in seconds since Epoch

Args:
time (:obj:`float`): time in seconds since Epoch (see time.time())

Returns:
None


def volume( self ):

        return np.dot( self.matrix[0], np.cross( self.matrix[1], self.matrix[2] ) )

The cell volume.

Args:
None

Returns:
(float): The cell volume.


def _is_png(contents, name=None):
    with ops.name_scope(name, 'is_png'):
        substr = string_ops.substr(contents, 0, 3)
        return math_ops.equal(substr, b'\x89PN', name=name)

Convenience function to check if the 'contents' encodes a PNG image.

Args:
contents: 0-D `string`. The encoded image bytes.
name: A name for the operation (optional)

Returns:
A scalar boolean tensor indicating if 'contents' may be a PNG image.
is_png is susceptible to false positives.


def join(self, basepath, *paths):
    return os.path.join(basepath, *paths)

Join two or more pathname components for the filesystem

Args:
basepath: string path of the first component of the path
paths: path components to be added

Returns: full path after combining all the passed components


def get_config(self):
    raise NotImplementedError(f'{self} does not implement get_config()')

Returns the config of the regularizer.

An regularizer config is a Python dictionary (serializable)
containing all configuration parameters of the regularizer.
The same regularizer can be reinstantiated later
(without any saved state) from this configuration.

This method is optional if you are just training and executing models,
exporting to and from SavedModels, or using weight checkpoints.

This method is required for Keras `model_to_estimator`, saving and
loading models to HDF5 formats, Keras model cloning, some visualization
utilities, and exporting models to and from JSON.

Returns:
Python dictionary.


def ExpandSuperClasses(self, t):
    superclasses = set()
    self._CollectSuperclasses(t, superclasses)
    return superclasses

Generate a list of all (known) superclasses for a type.

Arguments:
t: A type name. E.g. "int".

Returns:
A set of types. This set includes t as well as all its superclasses. For
example, this will return "bool", "int" and "object" for "bool".


def truncate_repetitions(text: str, min_len: int=30) -> str:
    text_lower = text.lower()
    text_length = len(text_lower)
    if text_length < 2 * min_len:
        return text
    max_repetition_length = None
    for repetition_length in range(min_len, int(text_length / 2)):
        same = True
        for i in range(0, repetition_length):
            if text_lower[text_length - repetition_length - i - 1] != text_lower[text_length - i - 1]:
                same = False
                break
        if same:
            max_repetition_length = repetition_length
    if max_repetition_length is None:
        return text
    lcs = text_lower[-max_repetition_length:]
    substituted_text = text
    substituted_text_lower = text_lower
    while substituted_text_lower.endswith(lcs):
        substituted_text = substituted_text[:-max_repetition_length]
        substituted_text_lower = substituted_text_lower[:-max_repetition_length]
    repeating_tail = text_lower[len(substituted_text_lower):]
    substituted_text_lower_out = substituted_text_lower
    while True:
        sentence_end = find_next_punctuation(text_lower, len(substituted_text_lower_out))
        sentence_start = find_next_punctuation(text_lower[::-1], len(substituted_text_lower_out))
        if sentence_end and sentence_start:
            sentence = text_lower[sentence_start:sentence_end]
            substituted_text_lower_out = text_lower[:sentence_end + 1]
            if sentence in repeating_tail:
                break
        else:
            break
    text_out = text[:len(substituted_text_lower_out)]
    return text_out

Attempt to truncate repeating segments in the input string.

This function looks for the longest repeating substring at the end of the input string and truncates it to appear
only once. To be considered for removal, repetitions need to be continuous.

Args:
text (`str`):
The input raw prediction to be truncated.
min_len (int):
The minimum length of the repeating segment.

Returns:
`str`: The input string with repeated segments truncated.


def __init__(self, time: Timestamp, duration: Union[Duration, timedelta],
                 operation: ops.Operation) -> None:

        self.time = time
        self.duration = Duration.create(duration)
        self.operation = operation

Initializes the scheduled operation.

Args:
time: When the operation starts.
duration: How long the operation lasts.
operation: The operation.


def _ParseKey(self, parser_mediator, registry_key):

    matching_plugin = None

    normalized_key_path = self._NormalizeKeyPath(registry_key.path)
    if self._path_filter.CheckPath(normalized_key_path):
      matching_plugin = self._plugin_per_key_path[normalized_key_path]
    else:
      for plugin in self._plugins_without_key_paths:
        if self._CanProcessKeyWithPlugin(registry_key, plugin):
          matching_plugin = plugin
          break

    if not matching_plugin:
      matching_plugin = self._default_plugin

    if matching_plugin:
      self._ParseKeyWithPlugin(parser_mediator, registry_key, matching_plugin)

Parses the Registry key with a specific plugin.

Args:
parser_mediator (ParserMediator): parser mediator.
registry_key (dfwinreg.WinRegistryKey): Windwos Registry key.


def _compute_valid(self):
    if (self._dimension != 2):
        raise NotImplementedError('Validity check only implemented in R^2')
    poly_sign = None
    if (self._degree == 1):
        first_deriv = (self._nodes[(:, 1:)] - self._nodes[(:, :(- 1))])
        poly_sign = _SIGN(np.linalg.det(first_deriv))
    elif (self._degree == 2):
        bernstein = _surface_helpers.quadratic_jacobian_polynomial(self._nodes)
        poly_sign = _surface_helpers.polynomial_sign(bernstein, 2)
    elif (self._degree == 3):
        bernstein = _surface_helpers.cubic_jacobian_polynomial(self._nodes)
        poly_sign = _surface_helpers.polynomial_sign(bernstein, 4)
    else:
        raise _helpers.UnsupportedDegree(self._degree, supported=(1, 2, 3))
    return (poly_sign == 1)

r"""Determines if the current surface is "valid".

Does this by checking if the Jacobian of the map from the
reference triangle is everywhere positive.

Returns:
bool: Flag indicating if the current surface is valid.

Raises:
NotImplementedError: If the surface is in a dimension other
than :math:`\mathbf{R}^2`.
.UnsupportedDegree: If the degree is not 1, 2 or 3.


def _parse_config(self, requires_cfg=True):

        if len(self.config_paths) > 0:
            try:
                self._find_config()
            except BisonError:
                if not requires_cfg:
                    return
                raise
            try:
                with open(self.config_file, 'r') as f:
                    parsed = self._fmt_to_parser[self.config_format](f)
            except Exception as e:
                raise BisonError(
                    'Failed to parse config file: {}'.format(self.config_file)
                ) from e

            self._full_config = None
            self._config = parsed

Parse the configuration file, if one is configured, and add it to
the `Bison` state.

Args:
requires_cfg (bool): Specify whether or not parsing should fail
if a config file is not found. (default: True)


def save_data_files(vr, bs, prefix=None, directory=None):
    filename = ('{}_band.dat'.format(prefix) if prefix else 'band.dat')
    directory = (directory if directory else '.')
    filename = os.path.join(directory, filename)
    if bs.is_metal():
        zero = vr.efermi
    else:
        zero = bs.get_vbm()['energy']
    with open(filename, 'w') as f:
        header = '
        f.write(header)
        for band in bs.bands[Spin.up]:
            for (d, e) in zip(bs.distance, band):
                f.write('{:.8f} {:.8f}\n'.format(d, (e - zero)))
            f.write('\n')
        if bs.is_spin_polarized:
            for band in bs.bands[Spin.down]:
                for (d, e) in zip(bs.distance, band):
                    f.write('{:.8f} {:.8f}\n'.format(d, (e - zero)))
                f.write('\n')
    return filename

Write the band structure data files to disk.

Args:
vs (`Vasprun`): Pymatgen `Vasprun` object.
bs (`BandStructureSymmLine`): Calculated band structure.
prefix (`str`, optional): Prefix for data file.
directory (`str`, optional): Directory in which to save the data.

Returns:
The filename of the written data file.


def _merge_tensors(t1, t2, name, validate):
    if t1 is None:
        return (t2, False)
    elif t2 is None:
        return (t1, False)
    elif t1 is t2:
        return (t1, True)
    else:
        err_msg = 'RowPartition._merge_precomputed_encodings: partitions have incompatible %s' % name
        if not t1.shape.is_compatible_with(t2.shape):
            raise ValueError(err_msg)
        if validate:
            checks = [check_ops.assert_equal(t1, t2, message=err_msg)]
            return (control_flow_ops.with_dependencies(checks, t1), True)
        else:
            return (t1, False)

Merge two optional Tensors with equal values into a single Tensor.

Args:
t1: tf.Tensor or None
t2: tf.Tensor or None
name: A name for the tensors (for error messages)
validate: If true, then check that `t1` is compatible with `t2` (if both are
non-None).

Returns:
A pair `(merged_value, validated)`:
* `merged_value` is `t1` if it is not None; or `t2` otherwise.
* `validated` is true if we validated that t1 and t2 are equal (either
by adding a check, or because t1 is t2).


def _get_combined_properties(self, dev):
    return (dev.job if dev.job is not None else self.job, dev.replica if dev.replica is not None else self.replica, dev.task if dev.task is not None else self.task, dev.device_type if dev.device_type is not None else self.device_type, dev.device_index if dev.device_index is not None else self.device_index)

Combine the current DeviceSpec with another DeviceSpec.

The combination of DeviceSpecs is will give priority to dev.

Args:
dev: a `DeviceSpec`

Returns:
A tuple of (job, replica, task, device_type, device_index) which
represents the combination of self and dev.


def lookup(self, iterable, index=0, gather=False, edit_distance=0, max_edit_distance=0, match_threshold=0.0, matched_length=0):

        if self.is_terminal:
            if index == len(iterable) or \
                    (gather and index < len(iterable) and iterable[index] == ' '):
                confidence = float(len(self.key) - edit_distance) / float(max(len(self.key), index))
                if confidence > match_threshold:
                    yield {
                        'key': self.key,
                        'match': iterable[:index],
                        'data': self.data,
                        'confidence': confidence * self.weight
                    }

        if index < len(iterable) and iterable[index] in self.children:
            for result in self.children[iterable[index]]\
                    .lookup(iterable, index + 1, gather=gather,
                            edit_distance=edit_distance, max_edit_distance=max_edit_distance, matched_length=matched_length + 1):
                yield result

        potential_confidence = float(index - edit_distance + (max_edit_distance - edit_distance)) / \
                               (float(index) + (max_edit_distance - edit_distance)) if index + max_edit_distance - edit_distance > 0 else 0.0
        if edit_distance < max_edit_distance and potential_confidence > match_threshold:
            for child in list(self.children):
                if index >= len(iterable) or child != iterable[index]:

                    for result in self.children[child]\
                        .lookup(iterable, index + 1, gather=gather,
                                edit_distance=edit_distance + 1, max_edit_distance=max_edit_distance, matched_length=matched_length):
                        yield result

                    for result in self.children[child]\
                        .lookup(iterable, index + 2, gather=gather,
                                edit_distance=edit_distance + 1, max_edit_distance=max_edit_distance, matched_length=matched_length):
                        yield result

                    for result in self.children[child]\
                        .lookup(iterable, index, gather=gather,
                                edit_distance=edit_distance + 1, max_edit_distance=max_edit_distance, matched_length=matched_length):
                        yield result

TODO: Implement trie lookup with edit distance

Args:
iterable(list?): key used to find what is requested this could
be a generator.
index(int): index of what is requested
gather(bool): of weather to gather or not
edit_distance(int): the distance -- currently not used
max_edit_distance(int): the max distance -- not currently used

yields:
object: yields the results of the search


def prepare_for_tokenization(self, text: str, is_split_into_words: bool=False, **kwargs) -> tuple[str, dict[str, Any]]:
    return (text, kwargs)

Performs any necessary transformations before tokenization.

This method should pop the arguments from kwargs and return the remaining `kwargs` as well. We test the
`kwargs` at the end of the encoding process to be sure all the arguments have been used.

Args:
text (`str`):
The text to prepare.
is_split_into_words (`bool`, *optional*, defaults to `False`):
Whether or not the input is already pre-tokenized (e.g., split into words). If set to `True`, the
tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace)
which it will tokenize. This is useful for NER or token classification.
kwargs (`Dict[str, Any]`, *optional*):
Keyword arguments to use for the tokenization.

Returns:
`Tuple[str, Dict[str, Any]]`: The prepared text and the unused kwargs.


def get_user_roles(self, user):

        self.project_service.set_auth(self._token_project)
        return self.project_service.get_user_roles(user)

Get roles associated with the given user.

Args:
user (string): User name.

Returns:
(list): List of roles that user has.

Raises:
requests.HTTPError on failure.


def retrieve(self, block_height, headers=None):

        path = self.path + block_height
        return self.transport.forward_request(
            method='GET', path=path, headers=None)

Retrieves the block with the given ``block_height``.

Args:
block_height (str): height of the block to retrieve.
headers (dict): Optional headers to pass to the request.

Returns:
dict: The block with the given ``block_height``.


def _is_node_return_ended(self, node):
    if isinstance(node, astroid.Return):
        return True
    if isinstance(node, astroid.Call):
        try:
            funcdef_node = node.func.inferred()[0]
            if self._is_function_def_never_returning(funcdef_node):
                return True
        except astroid.InferenceError:
            pass
    if isinstance(node, astroid.While):
        return True
    if isinstance(node, astroid.Raise):
        if (not node.exc):
            return True
        if (not utils.is_node_inside_try_except(node)):
            return True
        exc = utils.safe_infer(node.exc)
        if ((exc is None) or (exc is astroid.Uninferable)):
            return False
        exc_name = exc.pytype().split('.')[(- 1)]
        handlers = utils.get_exception_handlers(node, exc_name)
        handlers = (list(handlers) if (handlers is not None) else [])
        if handlers:
            return any((self._is_node_return_ended(_handler) for _handler in handlers))
        return True
    if isinstance(node, astroid.If):
        is_orelse_returning = any((self._is_node_return_ended(_ore) for _ore in node.orelse if (not isinstance(_ore, astroid.FunctionDef))))
        is_if_returning = any((self._is_node_return_ended(_ifn) for _ifn in node.body if (not isinstance(_ifn, astroid.FunctionDef))))
        return (is_if_returning and is_orelse_returning)
    return any((self._is_node_return_ended(_child) for _child in node.get_children() if (not isinstance(_child, astroid.ExceptHandler))))

Check if the node ends with an explicit return statement.

Args:
node (astroid.NodeNG): node to be checked.

Returns:
bool: True if the node ends with an explicit statement, False otherwise.


def __init__(self, token_list):

        self.__token_arr = np.array(list(set(token_list)))

Initialize.

Args:
token_list:    The list of all tokens.


def shared_symbol_table(name, version, symbols, imports=None):
    return SymbolTable(table_type=SHARED_TABLE_TYPE, symbols=symbols, name=name, version=version, imports=imports)

Constructs a shared symbol table.

Args:
name (unicode): The name of the shared symbol table.
version (int): The version of the shared symbol table.
symbols (Iterable[unicode]): The symbols to associate with the table.
imports (Optional[Iterable[SymbolTable]): The shared symbol tables to inject into this one.

Returns:
SymbolTable: The constructed table.


def GrabFileSystem(self, path_spec):

    identifier = self._GetFileSystemCacheIdentifier(path_spec)
    self._file_system_cache.GrabObject(identifier)

Grabs a cached file system object defined by path specification.

Args:
path_spec (PathSpec): path specification.


def image_format(value):

    if value.image.format.upper() not in constants.ALLOWED_IMAGE_FORMATS:
        raise ValidationError(MESSAGE_INVALID_IMAGE_FORMAT)

Confirms that the uploaded image is of supported format.

Args:
value (File): The file with an `image` property containing the image

Raises:
django.forms.ValidationError


def add_variable(self, shape, initializer='zeros', dtype=None, aggregation='none', layout=None, name=None):
    self._check_super_called()
    initializer = initializers.get(initializer)
    with backend.name_scope(self.name, caller=self):
        variable = backend.Variable(initializer=initializer, shape=shape, dtype=dtype, trainable=False, aggregation=aggregation, layout=layout, name=name)
    self._track_variable(variable)
    return variable

Add a variable to the optimizer.

Args:
shape: Shape tuple for the variable. Must be fully-defined
(no `None` entries).
initializer: Initializer object to use to populate the initial
variable value, or string name of a built-in initializer
(e.g. `"random_normal"`). Defaults to `"zeros"`.
dtype: Dtype of the variable to create, e.g. `"float32"`. If
unspecified, defaults to the `keras.backend.floatx()`.
aggregation: Optional string, one of `None`, `"none"`, `"mean"`,
`"sum"` or `"only_first_replica"`. Annotates the variable with
the type of multi-replica aggregation to be used for this
variable when writing custom data parallel training loops.
Defaults to `"none"`.
layout: Optional tensor layout.  Defaults to `None`.
name: String name of the variable. Useful for debugging purposes.

Returns:
An optimizer variable, in the format of `keras.Variable`.


def assert_true(expr, msg, extras=None):
    if not expr:
        fail(msg, extras)

Assert an expression evaluates to true, otherwise fail the test.

Args:
expr: The expression that is evaluated.
msg: A string explaining the details in case of failure.
extras: An optional field for extra information to be included in
test result.


def status(self, targets, jobs=None, remote=None, show_checksums=False):
    cloud = self._get_cloud(remote, 'status')
    return self.repo.cache.local.status(targets, jobs=jobs, remote=cloud, show_checksums=show_checksums)

Check status of data items in a cloud-agnostic way.

Args:
targets (list): list of targets to check status for.
jobs (int): number of jobs that can be running simultaneously.
remote (dvc.remote.base.RemoteBase): optional remote to compare
targets to. By default remote from core.remote config option
is used.
show_checksums (bool): show checksums instead of file names in
information messages.


def validate_sqs_policy(self, accounts):

        sqs_queue_name = self.dbconfig.get('sqs_queue_name', self.ns)
        sqs_queue_region = self.dbconfig.get('sqs_queue_region', self.ns)
        sqs_account = AWSAccount.get(self.dbconfig.get('sqs_queue_account', self.ns))
        session = get_aws_session(sqs_account)

        sqs = session.client('sqs', region_name=sqs_queue_region)
        sqs_queue_url = sqs.get_queue_url(QueueName=sqs_queue_name, QueueOwnerAWSAccountId=sqs_account.account_number)
        sqs_attribs = sqs.get_queue_attributes(QueueUrl=sqs_queue_url['QueueUrl'], AttributeNames=['Policy'])

        policy = json.loads(sqs_attribs['Attributes']['Policy'])

        for account in accounts:
            arn = 'arn:aws:sns:*:{}:{}'.format(account.account_number, sqs_queue_name)
            if arn not in policy['Statement'][0]['Condition']['ForAnyValue:ArnEquals']['aws:SourceArn']:
                self.log.warning('SQS policy is missing condition for ARN {}'.format(arn))
                policy['Statement'][0]['Condition']['ForAnyValue:ArnEquals']['aws:SourceArn'].append(arn)

        sqs.set_queue_attributes(QueueUrl=sqs_queue_url['QueueUrl'], Attributes={'Policy': json.dumps(policy)})

Given a list of accounts, ensures that the SQS policy allows all the accounts to write to the queue

Args:
accounts (`list` of :obj:`Account`): List of accounts

Returns:
`None`


def get_orbital_derivative_between_states(self, band_i, band_j, kpoint, spin, cart_dir):

        if band_i < 0 or band_i > self.nbands - 1 or band_j < 0 or band_j > self.nelect - 1:
            raise ValueError("Band index out of bounds")
        if kpoint > self.nkpoints:
            raise ValueError("K-point index out of bounds")
        if cart_dir > 2 or cart_dir < 0:
            raise ValueError("cart_dir index out of bounds")

        return self.cder_data[band_i, band_j, kpoint, spin, cart_dir]

Method returning a value
between bands band_i and band_j for k-point index, spin-channel and cartesian direction.
Args:
band_i (Integer): Index of band i
band_j (Integer): Index of band j
kpoint (Integer): Index of k-point
spin   (Integer): Index of spin-channel (0 or 1)
cart_dir (Integer): Index of cartesian direction (0,1,2)

Returns:
a float value


def get_graph(self, run_key, device_name, debug=False):
    return self.get_graphs(run_key, debug=debug).get(device_name, None)

Get the runtime GraphDef proto associated with a run key and a device.

Args:
run_key: A Session.run kay.
device_name: Name of the device in question.
debug: Whether the debugger-decoratedgraph is to be retrieved.

Returns:
A `GraphDef` proto.


def ws45(msg):

    d = hex2bin(data(msg))
    if d[3] == '0':
        return None

    ws = bin2int(d[4:6])
    return ws

Wind shear.

Args:
msg (String): 28 bytes hexadecimal message string

Returns:
int: Wind shear level. 0=NIL, 1=Light, 2=Moderate, 3=Severe


def request_file(link, outfile, force_rerun_flag=False):

    if force_rerun(flag=force_rerun_flag, outfile=outfile):
        req = requests.get(link)
        if req.status_code == 200:
            with open(outfile, 'w') as f:
                f.write(req.text)
            log.debug('Loaded and saved {} to {}'.format(link, outfile))
        else:
            log.error('{}: request error {}'.format(link, req.status_code))
    return outfile

Download a file given a URL if the outfile does not exist already.

Args:
link (str): Link to download file.
outfile (str): Path to output file, will make a new file if it does not exist. Will not download if it does
exist, unless force_rerun_flag is True.
force_rerun_flag (bool): Flag to force re-downloading of the file if it exists already.

Returns:
str: Path to downloaded file.


def bpe_decode(self, sequences):
    return self.bpe_tokenizer.batch_decode(sequences)

Convert a list of lists of bpe token ids into a list of strings by calling bpe tokenizer.

Args:
sequences (`torch.Tensor`):
List of tokenized input ids.
Returns:
`List[str]`: The list of bpe decoded sentences.


def pnl(self, account='', modelCode='') -> List[PnL]:

        return [v for v in self.wrapper.pnls.values() if
                (not account or v.account == account) and
                (not modelCode or v.modelCode == modelCode)]

List of subscribed :class:`.PnL` objects (profit and loss),
optionally filtered by account and/or modelCode.

The :class:`.PnL` objects are kept live updated.

Args:
account: If specified, filter for this account name.
modelCode: If specified, filter for this account model.


def inverse_event_shape(self, output_shape):
    return self._inverse_event_shape(output_shape)

Shape of a single sample from a single batch as a `TensorShape`.

Same meaning as `inverse_event_shape_tensor`. May be only partially defined.

Args:
output_shape: `TensorShape` indicating event-portion shape passed into
`inverse` function.

Returns:
inverse_event_shape_tensor: `TensorShape` indicating event-portion shape
after applying `inverse`. Possibly unknown.


def matches_any(patterns: List[Pattern[str]], line: str) -> bool:
    stripped_line = line.strip()
    for pattern in patterns:
        if pattern.match(stripped_line):
            return True
    return False

Checks if the line matches any of the given patterns.

Args:
patterns: A list of compiled regular expression patterns.
line: The line to check for matches.

Returns:
True if the line matches any of the patterns, False otherwise.


def read_lines(self, max_lines=None):
    if (max_lines is None):
        return self.read_stream().split('\n')
    max_to_read = self.metadata.size
    bytes_to_read = min((100 * max_lines), self.metadata.size)
    while True:
        content = self.read_stream(byte_count=bytes_to_read)
        lines = content.split('\n')
        if ((len(lines) > max_lines) or (bytes_to_read >= max_to_read)):
            break
        bytes_to_read = min((bytes_to_read * 10), max_to_read)
    del lines[(- 1)]
    return lines[0:max_lines]

Reads the content of this object as text, and return a list of lines up to some max.

Args:
max_lines: max number of lines to return. If None, return all lines.
Returns:
The text content of the object as a list of lines.
Raises:
Exception if there was an error requesting the object's content.


def _common_prefix(self, m):
    if not m:
        return ''
    s1 = min(m)
    s2 = max(m)
    for i, c in enumerate(s1):
        if c != s2[i]:
            return s1[:i]
    return s1

Given a list of str, returns the longest common prefix.

Args:
m: (list of str) A list of strings.

Returns:
(str) The longest common prefix.


def files_upload(
        self, *, file: Union[str, IOBase] = None, content: str = None, **kwargs
    ) -> SlackResponse:

        if file is None and content is None:
            raise e.SlackRequestError("The file or content argument must be specified.")
        if file is not None and content is not None:
            raise e.SlackRequestError(
                "You cannot specify both the file and the content argument."
            )

        if file:
            return self.api_call("files.upload", files={"file": file}, data=kwargs)
        elif content:
            data = kwargs.copy()
            data.update({"content": content})
            return self.api_call("files.upload", data=data)

Uploads or creates a file.

Args:
file (str): Supply a file path.
when you'd like to upload a specific file. e.g. 'dramacat.gif'
content (str): Supply content when you'd like to create an
editable text file containing the specified text. e.g. 'launch plan'
Raises:
SlackRequestError: If niether or both the `file` and `content` args are specified.


def _cast_dict(self, data_dict):
    for (key, value) in data_dict.iteritems():
        data_dict[key] = self._cast_value(value)
    if ('resp_body_data' in data_dict):
        del data_dict['resp_body_data']
    return data_dict

Internal method that makes sure any dictionary elements
are properly cast into the correct types, instead of
just treating everything like a string from the csv file.

Args:
data_dict: dictionary containing bro log data.

Returns:
Cleaned Data dict.


def register_date_conversion_handler(date_specifier_patterns):

    def _decorator(func):
        global DATE_SPECIFIERS_CONVERSION_HANDLERS
        DATE_SPECIFIERS_CONVERSION_HANDLERS[DATE_SPECIFIERS_REGEXES[date_specifier_patterns]] = func
        return func
    return _decorator

Decorator for registering handlers that convert text dates to dates.

Args:
date_specifier_patterns (str): the date specifier (in regex pattern format) for which the handler is registered


def set_trunk_groups(self, intf, value=None, default=False, disable=False):
    if default:
        cmd = 'default switchport trunk group'
        return self.configure_interface(intf, cmd)
    if disable:
        cmd = 'no switchport trunk group'
        return self.configure_interface(intf, cmd)
    current_value = self.get(intf)['trunk_groups']
    failure = False
    value = make_iterable(value)
    for name in set(value).difference(current_value):
        if (not self.add_trunk_group(intf, name)):
            failure = True
    for name in set(current_value).difference(value):
        if (not self.remove_trunk_group(intf, name)):
            failure = True
    return (not failure)

Configures the switchport trunk group value

Args:
intf (str): The interface identifier to configure.
value (str): The set of values to configure the trunk group
default (bool): Configures the trunk group default value
disable (bool): Negates all trunk group settings

Returns:
True if the config operation succeeds otherwise False


def __init__(self, ethertype=None):

        super().__init__(action_type=ActionType.OFPAT_POP_MPLS)
        self.ethertype = ethertype

Create an ActionPopMPLS with the optional parameters below.

Args:
ethertype (int): indicates the Ethertype of the payload.


def _expand_dims(x, input_shape, output_shape):

  verify_no_new_dims([output_shape], input_shape)
  if input_shape == output_shape or input_shape.ndims == 0:
    return x
  perm = [input_shape.dims.index(d) for d in output_shape.dims
          if d in input_shape.dims]
  x = tf.transpose(x, perm)
  for i, d in enumerate(output_shape.dims):
    if d not in input_shape.dims:
      x = tf.expand_dims(x, i)
  return x

Expand dimensions and transpose if necessary.

Args:
x: a tf.Tensor
input_shape: a Shape
output_shape: a Shape whose dimensions are a superset of
those in input_shape

Returns:
a tf.Tensor


def format(self, exclude_class=False):

        if exclude_class:
            msg = self.msg
        else:
            msg = "%s: %s" % (self.__class__.__name__, self.msg)

        if len(self.params) != 0:
            paramstring = "\n".join([str(key) + ": " + str(val) for key, val in self.params.items()])
            msg += "\nAdditional Information:\n" + paramstring

        return msg

Format this exception as a string including class name.

Args:
exclude_class (bool): Whether to exclude the exception class
name when formatting this exception

Returns:
string: a multiline string with the message, class name and
key value parameters passed to create the exception.


def websocket_url_for_server_url(url):

    if url.startswith("http:"):
        reprotocoled = "ws" + url[4:]
    elif url.startswith("https:"):
        reprotocoled = "wss" + url[5:]
    else:
        raise ValueError("URL has unknown protocol " + url)
    if reprotocoled.endswith("/"):
        return reprotocoled + "ws"
    else:
        return reprotocoled + "/ws"

Convert an ``http(s)`` URL for a Bokeh server websocket endpoint into
the appropriate ``ws(s)`` URL

Args:
url (str):
An ``http(s)`` URL

Returns:
str:
The corresponding ``ws(s)`` URL ending in ``/ws``

Raises:
ValueError:
If the input URL is not of the proper form.


def get_program(self, program_resource_name: str) -> Dict:

        return self.service.projects().programs().get(
            name=program_resource_name).execute()

Returns the previously created quantum program.

Params:
program_resource_name: A string of the form
`projects/project_id/programs/program_id`.

Returns:
A dictionary containing the metadata and the program.


def get(self, recipe_id):

        self.logger.debug('Retrieving recipe by id: ' + recipe_id)
        url = '%(base_url)s/recipe/%(recipe_id)s' % {
            'base_url': self.base_url, 'recipe_id': recipe_id
        }
        r = self.gbdx_connection.get(url)
        r.raise_for_status()
        return r.json()

Retrieves an AnswerFactory Recipe by id

Args:
recipe_id The id of the recipe

Returns:
A JSON representation of the recipe


def load_json(filename, **kwargs):

    with open(filename, 'r', encoding='utf-8') as f:
        return json.load(f, **kwargs)

Load a JSON object from the specified file.

Args:
filename: Path to the input JSON file.
**kwargs: Additional arguments to `json.load`.

Returns:
The object deserialized from JSON.


def checksum(self, path):
    try:
        return self._blobstorageIO().checksum(path)
    except Exception as e:
        raise BeamIOError('Checksum operation failed', {path, e})

Fetch checksum metadata of a file on the
:class:`~apache_beam.io.filesystem.FileSystem`.

Args:
path: string path of a file.

Returns: string containing checksum

Raises:
``BeamIOError``: if path isn't a file or doesn't exist.


def with_params(self, **kwargs):

    if _TEST_MODE:
      logging.info(
          'Setting runtime parameters for %s
          self, self.pipeline_id, kwargs)
      return self

    if self.pipeline_id is not None:
      raise UnexpectedPipelineError(
          'May only call with_params() on a Pipeline that has not yet '
          'been scheduled for execution.')

    ALLOWED = ('backoff_seconds', 'backoff_factor', 'max_attempts', 'target')
    for name, value in kwargs.iteritems():
      if name not in ALLOWED:
        raise TypeError('Unexpected keyword: %s=%r' % (name, value))
      setattr(self, name, value)
    return self

Modify various execution parameters of a Pipeline before it runs.

This method has no effect in test mode.

Args:
kwargs: Attributes to modify on this Pipeline instance before it has
been executed.

Returns:
This Pipeline instance, for easy chaining.


def orthonormalize_righthanded(basis):
    (v1, v2) = (basis[(:, 0)], basis[(:, 1)])
    e1 = normalize(v1)
    e3 = normalize(np.cross(e1, v2))
    e2 = normalize(np.cross(e3, e1))
    return np.array([e1, e2, e3]).T

Orthonormalizes righthandedly a given 3D basis.

This functions returns a right handed orthonormalize_righthandedd basis.
Since only the first two vectors in the basis are used, it does not matter
if you give two or three vectors.

Right handed means, that:

.. math::

\\vec{e_1} \\times \\vec{e_2} &= \\vec{e_3} \\\\
\\vec{e_2} \\times \\vec{e_3} &= \\vec{e_1} \\\\
\\vec{e_3} \\times \\vec{e_1} &= \\vec{e_2} \\\\

Args:
basis (np.array): An array of shape = (3,2) or (3,3)

Returns:
new_basis (np.array): A right handed orthonormalized basis.


def _unflatten_dict(flat_dict, prefixes):
    original_dict = {}
    for (key, value) in flat_dict.items():
        prefix_found = False
        for prefix in prefixes:
            full_prefix = (('__' + prefix) + '_')
            if key.startswith(full_prefix):
                if (prefix not in original_dict):
                    original_dict[prefix] = {}
                original_dict[prefix][key[len(full_prefix):]] = value
                prefix_found = True
                break
        if (not prefix_found):
            original_dict[key] = value
    return original_dict

Returns a dict of dicts if any prefixes match keys in the flat dict.

The function handles the case where the prefix may not be a dict.

Args:
flat_dict: A dict without any nesting.
prefixes: A list of strings which may have been dicts in the
original structure.


def from_string(cls, model_id, default_project=None):
    (proj, dset, model) = _helpers._parse_3_part_id(model_id, default_project=default_project, property_name='model_id')
    return cls.from_api_repr({'projectId': proj, 'datasetId': dset, 'modelId': model})

Construct a model reference from model ID string.

Args:
model_id (str):
A model ID in standard SQL format. If ``default_project``
is not specified, this must included a project ID, dataset
ID, and model ID, each separated by ``.``.
default_project (str):
Optional. The project ID to use when ``model_id`` does not
include a project ID.

Returns:
google.cloud.bigquery.model.ModelReference:
Model reference parsed from ``model_id``.

Raises:
ValueError:
If ``model_id`` is not a fully-qualified table ID in
standard SQL format.


def get_pourbaix_plot(self, limits=None, title="",
                          label_domains=True, plt=None):

        if limits is None:
            limits = [[-2, 16], [-3, 3]]

        plt = plt or pretty_plot(16)

        xlim = limits[0]
        ylim = limits[1]

        h_line = np.transpose([[xlim[0], -xlim[0] * PREFAC],
                               [xlim[1], -xlim[1] * PREFAC]])
        o_line = np.transpose([[xlim[0], -xlim[0] * PREFAC + 1.23],
                               [xlim[1], -xlim[1] * PREFAC + 1.23]])
        neutral_line = np.transpose([[7, ylim[0]], [7, ylim[1]]])
        V0_line = np.transpose([[xlim[0], 0], [xlim[1], 0]])

        ax = plt.gca()
        ax.set_xlim(xlim)
        ax.set_ylim(ylim)
        lw = 3
        plt.plot(h_line[0], h_line[1], "r--", linewidth=lw)
        plt.plot(o_line[0], o_line[1], "r--", linewidth=lw)
        plt.plot(neutral_line[0], neutral_line[1], "k-.", linewidth=lw)
        plt.plot(V0_line[0], V0_line[1], "k-.", linewidth=lw)

        for entry, vertices in self._pd._stable_domain_vertices.items():
            center = np.average(vertices, axis=0)
            x, y = np.transpose(np.vstack([vertices, vertices[0]]))
            plt.plot(x, y, 'k-', linewidth=lw)
            if label_domains:
                plt.annotate(generate_entry_label(entry), center, ha='center',
                             va='center', fontsize=20, color="b")

        plt.xlabel("pH")
        plt.ylabel("E (V)")
        plt.title(title, fontsize=20, fontweight='bold')
        return plt

Plot Pourbaix diagram.

Args:
limits: 2D list containing limits of the Pourbaix diagram
of the form [[xlo, xhi], [ylo, yhi]]
title (str): Title to display on plot
label_domains (bool): whether to label pourbaix domains
plt (pyplot): Pyplot instance for plotting

Returns:
plt (pyplot) - matplotlib plot object with pourbaix diagram


def texture_cube(self, size, components, data=None, *, alignment=1, dtype='f1') -> 'TextureCube':

        res = TextureCube.__new__(TextureCube)
        res.mglo, res._glo = self.mglo.texture_cube(size, components, data, alignment, dtype)
        res._size = size
        res._components = components
        res._dtype = dtype
        res.ctx = self
        res.extra = None
        return res

Create a :py:class:`TextureCube` object.

Args:
size (tuple): The width, height of the texture. Each side of the cube will have this size.
components (int): The number of components 1, 2, 3 or 4.
data (bytes): Content of the texture.

Keyword Args:
alignment (int): The byte alignment 1, 2, 4 or 8.
dtype (str): Data type.

Returns:
:py:class:`TextureCube` object


def split_folder_and_path(filepath):
    dirname = op.dirname(filepath)
    filename = op.basename(filepath)
    splitext = op.splitext(filename)
    filename_without_extension = splitext[0]
    extension = splitext[1]
    return (dirname, filename_without_extension, extension)

Split a file path into its folder, filename, and extension

Args:
path (str): Path to a file

Returns:
tuple: of (folder, filename (without extension), extension)


def include(filename, hosts=False, when=True):

    if not pyinfra.is_cli:
        raise PyinfraError('local.include is only available in CLI mode.')

    if not when:
        return

    if hosts is not False:
        hosts = ensure_host_list(hosts, inventory=pseudo_state.inventory)
        if pseudo_host not in hosts:
            return

    if pseudo_state.deploy_dir:
        filename = path.join(pseudo_state.deploy_dir, filename)

    frameinfo = get_caller_frameinfo()

    logger.debug('Including local file: {0}'.format(filename))

    try:

        from pyinfra_cli.config import extract_file_config
        from pyinfra_cli.util import exec_file

        config_data = extract_file_config(filename)
        kwargs = {
            key.lower(): value
            for key, value in six.iteritems(config_data)
            if key in [
                'SUDO', 'SUDO_USER', 'SU_USER',
                'PRESERVE_SUDO_ENV', 'IGNORE_ERRORS',
            ]
        }
        with pseudo_state.deploy(
            filename, kwargs, None, frameinfo.lineno,
            in_deploy=False,
        ):
            exec_file(filename)

    except IOError as e:
        raise PyinfraError(
            'Could not include local file: {0}\n{1}'.format(filename, e),
        )

Executes a local python file within the ``pyinfra.pseudo_state.deploy_dir``
directory.

Args:
hosts (string, list): group name or list of hosts to limit this include to
when (bool): indicate whether to trigger operations in this include


def set_userdata(self, key: str, value: Any, cloneable: bool=False) -> 'DNA':
    self._userdata[key] = value
    if cloneable:
        self._cloneable_userdata_keys.add(key)
    return self

Sets user data associated with a key.

User data associated with the DNA will live only within current process,
and is not carried over during serialization/deserialization, which is
different from DNA metadata. (See `set_metadata` for more details.)

Args:
key: Key of the user data.
value: Value of the user data.
cloneable: If True, the key/value will be carry over to the cloned DNA.

Returns:
Self.


def CaptureVariableInternal(self, value, depth, limits, can_enqueue=True):

    if depth == limits.max_depth:
      return {'varTableIndex': 0}

    if value is None:
      self._total_size += 4
      return {'value': 'None'}

    if isinstance(value, _PRIMITIVE_TYPES):
      r = _TrimString(repr(value),
                      min(limits.max_value_len,
                          self.max_size - self._total_size))
      self._total_size += len(r)
      return {'value': r, 'type': type(value).__name__}

    if isinstance(value, _DATE_TYPES):
      r = str(value)
      self._total_size += len(r)
      return {'value': r, 'type': 'datetime.'+ type(value).__name__}

    if isinstance(value, dict):

      items = [(repr(k), v) for (k, v) in value.items()]
      return {'members':
              self.CaptureVariablesList(items, depth + 1,
                                        EMPTY_DICTIONARY, limits),
              'type': 'dict'}

    if isinstance(value, _VECTOR_TYPES):
      fields = self.CaptureVariablesList(
          (('[%d]' % i, x) for i, x in enumerate(value)),
          depth + 1, EMPTY_COLLECTION, limits)
      return {'members': fields, 'type': type(value).__name__}

    if isinstance(value, types.FunctionType):
      self._total_size += len(value.__name__)

      return {'value': 'function ' + value.__name__}

    if isinstance(value, Exception):
      fields = self.CaptureVariablesList(
          (('[%d]' % i, x) for i, x in enumerate(value.args)),
          depth + 1, EMPTY_COLLECTION, limits)
      return {'members': fields, 'type': type(value).__name__}

    if can_enqueue:
      index = self._var_table_index.get(id(value))
      if index is None:
        index = len(self._var_table)
        self._var_table_index[id(value)] = index
        self._var_table.append(value)
      self._total_size += 4
      return {'varTableIndex': index}

    for pretty_printer in CaptureCollector.pretty_printers:
      pretty_value = pretty_printer(value)
      if not pretty_value:
        continue

      fields, object_type = pretty_value
      return {'members':
              self.CaptureVariablesList(fields, depth + 1, OBJECT_HAS_NO_FIELDS,
                                        limits),
              'type': object_type}

    if not hasattr(value, '__dict__'):

      r = str(type(value))
      self._total_size += len(r)
      return {'value': r}

    items = value.__dict__.items()
    if six.PY3:

      items = list(itertools.islice(items, limits.max_list_items + 1))
    members = self.CaptureVariablesList(items, depth + 2,
                                        OBJECT_HAS_NO_FIELDS, limits)
    v = {'members': members}

    type_string = DetermineType(value)
    if type_string:
      v['type'] = type_string

    return v

Captures a single nameless object into Variable message.

TODO(vlif): safely evaluate iterable types.
TODO(vlif): safely call str(value)

Args:
value: data to capture
depth: nested depth of dictionaries and vectors so far.
limits: Per-object limits for capturing variable data.
can_enqueue: allows referencing the object in variables table.

Returns:
Formatted captured data as per Variable proto.


def update(self, puts, deletes):

        with self._lmdb.begin(write=True, buffers=True) as txn:
            cursor = txn.cursor(self._main_db)

            for key in deletes:
                if not cursor.set_key(key.encode()):

                    continue

                value = self._deserializer(bytes(cursor.value()))
                cursor.delete()

                for (index_db, index_key_fn) in self._indexes.values():
                    index_keys = index_key_fn(value)
                    index_cursor = txn.cursor(index_db)
                    for idx_key in index_keys:
                        if index_cursor.set_key(idx_key):
                            index_cursor.delete()

            for key, value in puts:
                packed = self._serializer(value)

                cursor.put(key.encode(), packed, overwrite=True)

                for (index_db, index_key_fn) in self._indexes.values():
                    index_keys = index_key_fn(value)
                    index_cursor = txn.cursor(index_db)
                    for idx_key in index_keys:
                        index_cursor.put(idx_key, key.encode())

        self.sync()

Applies the given puts and deletes atomically.

Args:
puts (:iterable:`tuple`): an iterable of key/value pairs to insert
deletes (:iterable:str:) an iterable of keys to delete


def _get_return_value(self, tensors, indices):
    tensors = self._create_device_transfers(tensors)
    for output, i in zip(tensors, indices):
        output.set_shape(self._shapes[i])
    if self._names:
        return {self._names[i]: t for t, i in zip(tensors, indices)}
    return tensors

Return the value to return from a get op.

If the staging area has names, return a dictionary with the
names as keys.  Otherwise return either a single tensor
or a list of tensors depending on the length of `tensors`.

Args:
tensors: List of tensors from the get op.
indices: Indices of associated names and shapes

Returns:
A single tensor, a list of tensors, or a dictionary
of tensors.


def recode_dwgsim_reads(dwgsim_prefix, fastq_rnf_fo, fai_fo, genome_id, estimate_unknown_values, number_of_read_tuples=(10 ** 9)):
    dwgsim_pattern = re.compile('@(.*)_([0-9]+)_([0-9]+)_([01])_([01])_([01])_([01])_([0-9]+):([0-9]+):([0-9]+)_([0-9]+):([0-9]+):([0-9]+)_(([0-9abcdef])+)')
    fai_index = rnftools.utils.FaIdx(fai_fo=fai_fo)
    read_tuple_id_width = len(format(number_of_read_tuples, 'x'))
    read_tuple_id = 0
    last_read_tuple_name = None
    old_fq = '{}.bfast.fastq'.format(dwgsim_prefix)
    fq_creator = rnftools.rnfformat.FqCreator(fastq_fo=fastq_rnf_fo, read_tuple_id_width=read_tuple_id_width, genome_id_width=2, chr_id_width=fai_index.chr_id_width, coor_width=fai_index.coor_width, info_reads_in_tuple=True, info_simulator='dwgsim')
    i = 0
    with open(old_fq, 'r+') as f1:
        for line in f1:
            if ((i % 4) == 0):
                read_tuple_name = line[1:].strip()
                if (read_tuple_name != last_read_tuple_name):
                    new_tuple = True
                    if (last_read_tuple_name is not None):
                        read_tuple_id += 1
                else:
                    new_tuple = False
                last_read_tuple_name = read_tuple_name
                m = dwgsim_pattern.search(line)
                if (m is None):
                    rnftools.utils.error("Read tuple '{}' was not created by DwgSim.".format(line[1:]), program='RNFtools', subprogram='MIShmash', exception=ValueError)
                contig_name = m.group(1)
                start_1 = int(m.group(2))
                start_2 = int(m.group(3))
                direction_1 = ('F' if (int(m.group(4)) == 0) else 'R')
                direction_2 = ('F' if (int(m.group(5)) == 0) else 'R')
                chr_id = (fai_index.dict_chr_ids[contig_name] if (fai_index.dict_chr_ids != {}) else '0')
            elif ((i % 4) == 1):
                bases = line.strip()
                if new_tuple:
                    segment = rnftools.rnfformat.Segment(genome_id=genome_id, chr_id=chr_id, direction=direction_1, left=start_1, right=(((start_1 + len(bases)) - 1) if estimate_unknown_values else 0))
                else:
                    segment = rnftools.rnfformat.Segment(genome_id=genome_id, chr_id=chr_id, direction=direction_2, left=start_2, right=(((start_2 + len(bases)) - 1) if estimate_unknown_values else 0))
            elif ((i % 4) == 2):
                pass
            elif ((i % 4) == 3):
                qualities = line.strip()
                fq_creator.add_read(read_tuple_id=read_tuple_id, bases=bases, qualities=qualities, segments=[segment])
            i += 1
    fq_creator.flush_read_tuple()

Convert DwgSim FASTQ file to RNF FASTQ file.

Args:
dwgsim_prefix (str): DwgSim prefix of the simulation (see its commandline parameters).
fastq_rnf_fo (file): File object of RNF FASTQ.
fai_fo (file): File object for FAI file of the reference genome.
genome_id (int): RNF genome ID to be used.
estimate_unknown_values (bool): Estimate unknown values (right coordinate of each end).
number_of_read_tuples (int): Estimate of number of simulated read tuples (to set width).


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

Corresponds to IDD Field `diffuse_horizontal_radiation`

Args:
value (float): value for IDD Field `diffuse_horizontal_radiation`
Unit: Wh/m2
value >= 0.0
Missing value: 9999.0
if `value` is None it will not be checked against the
specification and is assumed to be a missing value

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


def _get_fields(mcs, bases, namespace):

        fields = [
            (name, namespace.pop(name))
            for name, attribute
            in list(namespace.items())
            if isinstance(attribute, BaseField)
        ]

        for base in reversed(bases):
            if hasattr(base, mcs._fields_storage_key):
                fields = list(
                    getattr(base, mcs._fields_storage_key).items()
                ) + fields

        return OrderedDict(fields)

Create fields dictionary to be used in resource class namespace.

Pop all field objects from attributes dict (namespace) and store them
under _field_storage_key atrribute. Also collect all fields from base
classes in order that ensures fields can be overriden.

Args:
bases: all base classes of created serializer class
namespace (dict): namespace as dictionary of attributes


def __set_type(self, obj, prop):

        if TypeHandler.is_pure(obj, prop):
            self.args_type = "PURE"
            self.pure = SinonBase.Pure()
            setattr(self.pure, "func", Wrapper.empty_function)
            self.orig_func = None
        elif TypeHandler.is_module_function(obj, prop):
            self.args_type = "MODULE_FUNCTION"
            self.orig_func = None
        elif TypeHandler.is_function(obj):
            self.args_type = "FUNCTION"
            self.orig_func = None
        elif TypeHandler.is_module(obj):
            self.args_type = "MODULE"
        elif TypeHandler.is_instance(obj):
            obj = obj.__class__
            self.args_type = "MODULE"

Triage type based on arguments
Here are four types of base: PURE, MODULE, MODULE_FUNCTION, FUNCTION
Args:
obj: None, FunctionType, ModuleType, Class, Instance
prop: None, string


def _encode_required_field(self, name: str, containing_type_builder: expressions.Builder, builder: expressions.Builder, element_definition: message.Message) -> Optional[validation_pb2.SqlRequirement]:
    element = cast(Any, element_definition)
    if not _is_elem_supported(element):
        return None
    field_name = _last_path_token(builder)
    min_size = element.min.value
    max_size = element.max.value
    element_count = builder.count()
    query_list = []
    if _fhir_path_data_types.is_collection(builder.return_type) and max_size.isdigit():
        query_list.append(element_count <= int(max_size))
    if min_size == 1:
        query_list.append(builder.exists())
    elif min_size > 0:
        query_list.append(element_count >= min_size)
    if not query_list:
        return None
    constraint_key = f'{name}-cardinality-is-valid'
    description = f'The length of {name} must be maximum {max_size} and minimum {min_size}.'
    fhir_path_builder = query_list[0]
    for query in query_list[1:]:
        fhir_path_builder = fhir_path_builder & query
    if constraint_key in self._options.skip_keys:
        return None
    type_codes = _utils.element_type_codes(element)
    if 'Reference' not in type_codes and (not _SKIP_TYPE_CODES.isdisjoint(type_codes)):
        return None
    result = self._encode_fhir_path_builder_constraint(fhir_path_builder, containing_type_builder)
    if result is None:
        return None
    element_definition_path = self._abs_path_invocation(containing_type_builder)
    constraint_key_column_name: str = _key_to_sql_column_name(_path_to_sql_column_name(constraint_key))
    column_name_base: str = _path_to_sql_column_name(element_definition_path)
    column_name = f'{column_name_base}_{constraint_key_column_name}'
    requirement = validation_pb2.SqlRequirement(column_name=column_name, sql_expression=result.sql, fhir_path_sql_expression=result.fhir_path_sql, severity=validation_pb2.ValidationSeverity.SEVERITY_ERROR, type=validation_pb2.ValidationType.VALIDATION_TYPE_CARDINALITY, element_path=element_definition_path, description=description, fhir_path_key=constraint_key, fhir_path_expression=result.builder.fhir_path, fields_referenced_by_expression=[field_name])
    return requirement

Returns `SqlRequirement` for the required field passed.

Args:
name: name of the constraint key.
containing_type_builder: The builder of the Structure definition for the
required field.
builder: The builder containing the element to encode required field for.
element_definition: Element definition of the builder.

Returns:
A `SqlRequirement` representing the requirement generated from
the element.


def infer_return_type(c, input_types, debug=False, depth=5):
    try:
        if hashable(c) and c in known_return_types:
            return known_return_types[c]
        elif isinstance(c, types.FunctionType):
            return infer_return_type_func(c, input_types, debug, depth)
        elif isinstance(c, types.MethodType):
            if c.__self__ is not None:
                input_types = [Const(c.__self__)] + input_types
            return infer_return_type_func(c.__func__, input_types, debug, depth)
        elif isinstance(c, BoundMethod):
            input_types = [c.type] + input_types
            return infer_return_type_func(c.func, input_types, debug, depth)
        elif inspect.isclass(c):
            if c in typehints.DISALLOWED_PRIMITIVE_TYPES:
                return {list: typehints.List[Any], set: typehints.Set[Any], frozenset: typehints.FrozenSet[Any], tuple: typehints.Tuple[Any, ...], dict: typehints.Dict[Any, Any]}[c]
            return c
        elif c == getattr and len(input_types) == 2 and isinstance(input_types[1], Const):
            from apache_beam.typehints import opcodes
            return opcodes._getattr(input_types[0], input_types[1].value)
        elif isinstance(c, python_callable.PythonCallableWithSource):
            return infer_return_type(c._callable, input_types, debug, depth)
        else:
            return Any
    except TypeInferenceError:
        if debug:
            traceback.print_exc()
        return Any
    except Exception:
        if debug:
            sys.stdout.flush()
            raise
        else:
            return Any

Analyses a callable to deduce its return type.

Args:
c: A Python callable to infer the return type of.
input_types: A sequence of inputs corresponding to the input types.
debug: Whether to print verbose debugging information.
depth: Maximum inspection depth during type inference.

Returns:
A TypeConstraint that that the return value of this function will (likely)
satisfy given the specified inputs.


def extended_capabilities(self):

        buf = (ctypes.c_uint8 * 32)()
        self._dll.JLINKARM_GetEmuCapsEx(buf, 32)
        return list(buf)

Gets the capabilities of the connected emulator as a list.

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

Returns:
List of 32 integers which define the extended capabilities based on
their value and index within the list.


def wheel_delta(self):
    delta = self._libinput.libinput_event_tablet_tool_get_wheel_delta(self._handle)
    changed = self._libinput.libinput_event_tablet_tool_wheel_has_changed(self._handle)
    return (delta, changed)

The delta for the wheel in degrees and whether it has changed in
this event.

Returns:
(float, bool): The delta of the wheel, in degrees, compared to
the last event and whether it has changed.


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

        local_buffer = utils.BytearrayStream()

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

        if self._unique_identifiers:
            for unique_identifier in self._unique_identifiers:
                unique_identifier.write(
                    local_buffer,
                    kmip_version=kmip_version
                )

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

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

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


def get_content(self, url):

        cache_path = self._url_to_path(url)
        try:
            with open(cache_path, 'rb') as f:
                return f.read()
        except IOError:
            return None

Returns the content of a cached resource.

Args:
url: The url of the resource

Returns:
The content of the cached resource or None if not in the cache


def __random_density_bures(N, rank=None, seed=None):
    P = (np.eye(N) + random_unitary(N).data)
    G = P.dot(__ginibre_matrix(N, rank, seed))
    G = G.dot(G.conj().T)
    return (G / np.trace(G))

Generate a random density matrix from the Bures metric.

Args:
N (int): the length of the density matrix.
rank (int or None): the rank of the density matrix. The default
value is full-rank.
seed (int): Optional. To set a random seed.
Returns:
ndarray: rho (N,N) a density matrix.


def _should_get_another_batch(self, content):
    if (('max-keys' in self._options) and (self._options['max-keys'] <= common._MAX_GET_BUCKET_RESULT)):
        return False
    elements = self._find_elements(content, set([common._T_IS_TRUNCATED, common._T_NEXT_MARKER]))
    if (elements.get(common._T_IS_TRUNCATED, 'false').lower() != 'true'):
        return False
    next_marker = elements.get(common._T_NEXT_MARKER)
    if (next_marker is None):
        self._options.pop('marker', None)
        return False
    self._options['marker'] = next_marker
    return True

Whether to issue another GET bucket call.

Args:
content: response XML.

Returns:
True if should, also update self._options for the next request.
False otherwise.


def empty(cls, base_uri=None, draft=AUTO):

        return cls.from_object({}, base_uri=base_uri, draft=draft)

Returns an empty ``Document``.

Arguments:

- ``base_uri``: optional URL used as the basis when expanding
relative URLs in the document.
- ``draft``: a ``Draft`` instance that selects the version of the spec
to which the document should conform. Defaults to
``drafts.AUTO``.


def text(self, x, y, text):
    for (i, char) in enumerate(text):
        self.point((x + i), y, char)

Print a text on ASCII canvas.

Args:
x (int): x coordinate where the text should start.
y (int): y coordinate where the text should start.
text (str): string that should be printed.


def mkdir(path):

    try:
        os.makedirs(path)

        if not os.path.isdir(path):
            raise IOError('path is not a directory')
    except OSError as e:

        if e.errno == 17 and os.path.isdir(path):
            return
        raise

Make a directory and its parents.

Args:
path (str): path to create

Returns:
None

Raises:
OSError if the directory cannot be created.


def position(string, index):

	if not string:
		return None

	if index < 0 or index >= len(string):
		raise InternalError("Out-of-range index passed to errors.position!")

	lines = string.split("\n")

	if len(lines) == 1:
		return str(index)

	before = n = 0

	for n, line in enumerate(lines):
		future = before + len(line) + 1

		if future > index:
			break
		before = future

	return "{0}:{1}".format(n, index - before)

Returns a helpful position description for an index in a
(multi-line) string using the format line:column.

Arguments:
string (str): The string to which the index refers.
index (int): The index of the character in question.

Returns:
A string with the format line:column where line refers to the
1-indexed row/line in which the character is found within the
string and column to the position of the character within
(relative to) that  line.


def serialize(self, items, default_flow_style=False):

        yaml = self._load_yaml()

        items = dict(items)
        return yaml.dump(items, default_flow_style=default_flow_style)

Does the inverse of config parsing by taking parsed values and
converting them back to a string representing config file contents.

Args:
default_flow_style: defines serialization format (see PyYAML docs)


def _finalize_job(cls, mapreduce_spec, mapreduce_state):

    config = util.create_datastore_write_config(mapreduce_spec)
    queue_name = util.get_queue_name(mapreduce_spec.params.get(
        model.MapreduceSpec.PARAM_DONE_CALLBACK_QUEUE))
    done_callback = mapreduce_spec.params.get(
        model.MapreduceSpec.PARAM_DONE_CALLBACK)
    done_task = None
    if done_callback:
      done_task = taskqueue.Task(
          url=done_callback,
          headers=util._get_task_headers(mapreduce_spec.mapreduce_id,
                                         util.CALLBACK_MR_ID_TASK_HEADER),
          method=mapreduce_spec.params.get("done_callback_method", "POST"))

    @db.transactional(retries=5)
    def _put_state():

      fresh_state = model.MapreduceState.get_by_job_id(
          mapreduce_spec.mapreduce_id)
      if not fresh_state.active:
        logging.warning(
            "Job %s is not active. Looks like spurious task execution. "
            "Dropping task.", mapreduce_spec.mapreduce_id)
        return
      mapreduce_state.put(config=config)

      if done_task and not _run_task_hook(
          mapreduce_spec.get_hooks(),
          "enqueue_done_task",
          done_task,
          queue_name):
        done_task.add(queue_name, transactional=True)

    _put_state()
    logging.info("Final result for job '%s' is '%s'",
                 mapreduce_spec.mapreduce_id, mapreduce_state.result_status)
    cls._clean_up_mr(mapreduce_spec)

Finalize job execution.

Invokes done callback and save mapreduce state in a transaction,
and schedule necessary clean ups. This method is idempotent.

Args:
mapreduce_spec: an instance of MapreduceSpec
mapreduce_state: an instance of MapreduceState


def piola_kirchoff_1(self, def_grad):

        if not self.is_symmetric:
            raise ValueError("The stress tensor is not symmetric, \
                             PK stress is based on a symmetric stress tensor.")
        def_grad = SquareTensor(def_grad)
        return def_grad.det*np.dot(self, def_grad.inv.trans)

calculates the first Piola-Kirchoff stress

Args:
def_grad (3x3 array-like): deformation gradient tensor


def FindExtensionByName(self, full_name):
    full_name = _NormalizeFullyQualifiedName(full_name)
    (message_name, _, extension_name) = full_name.rpartition('.')
    try:
        scope = self.FindMessageTypeByName(message_name)
    except KeyError:
        scope = self.FindFileContainingSymbol(full_name)
    return scope.extensions_by_name[extension_name]

Loads the named extension descriptor from the pool.

Args:
full_name: The full name of the extension descriptor to load.

Returns:
A FieldDescriptor, describing the named extension.


def _get_package_name(prefix=settings.TEMP_DIR, book_id=None):
    if (book_id is None):
        book_id = str(uuid.uuid4())
    return os.path.join(prefix, book_id)

Return package path. Use uuid to generate package's directory name.

Args:
book_id (str, default None): UUID of the book.
prefix (str, default settings.TEMP_DIR): Where the package will be
stored. Default :attr:`settings.TEMP_DIR`.

Returns:
str: Path to the root directory.


def reset_sequence(cls, value=None, force=False):
    cls._meta.reset_sequence(value, force=force)

Reset the sequence counter.

Args:
value (int or None): the new 'next' sequence value; if None,
recompute the next value from _setup_next_sequence().
force (bool): whether to force-reset parent sequence counters
in a factory inheritance chain.


def find(lst, a, case_sensitive=True):
    a = force_list(a)
    if (not case_sensitive):
        lst = [x.lower() for x in lst]
        a = [y.lower() for y in a]
    return [i for (i, x) in enumerate(lst) if (x in a)]

Return indices of a list which have elements that match an object or list of objects

Args:
lst: list of values
a: object(s) to check equality
case_sensitive: if the search should be case sensitive

Returns:
list: list of indicies of lst which equal a


def json_to_numpy(string_like, dtype=None):
    data = json.loads(string_like)
    return np.array(data, dtype=dtype)

Convert a JSON object to a numpy array.

Args:
string_like (str): JSON string.
dtype (dtype, optional):  Data type of the resulting array. If None, the dtypes will be determined by the
contents of each column, individually. This argument can only be used to
'upcast' the array.  For downcasting, use the .astype(t) method.
Returns:
(np.array): numpy array


def _redirect_with_params(url_name, *args, **kwargs):
    url = urlresolvers.reverse(url_name, args=args)
    params = parse.urlencode(kwargs, True)
    return '{0}?{1}'.format(url, params)

Helper method to create a redirect response with URL params.

This builds a redirect string that converts kwargs into a
query string.

Args:
url_name: The name of the url to redirect to.
kwargs: the query string param and their values to build.

Returns:
A properly formatted redirect string.


def _export(self, path, variables_saver):

    self._saved_model_handler.export(path, variables_saver=variables_saver)

    module_def_proto = module_def_pb2.ModuleDef()
    module_def_proto.format = module_def_pb2.ModuleDef.FORMAT_V3
    module_def_filename = get_module_proto_path(path)
    tf_utils.atomic_write_string_to_file(
        module_def_filename,
        module_def_proto.SerializeToString(),
        overwrite=False)
    logging.info("Exported TF-Hub module to: %s", path)

Internal.

Args:
path: string where to export the module to.
variables_saver: an unary-function that writes the module variables
checkpoint on the given path.


def i2le_script(number):

    if number == 0:
        return '00'
    for i in range(80):
        try:
            return number.to_bytes(
                length=i,
                byteorder='little',
                signed=True).hex()
        except Exception:
            continue

Convert int to signed little endian (l.e.) hex for scripts
Args:
number  (int): int value to convert to bytes in l.e. format
Returns:
(str): the hex-encoded signed LE number


def _extract_id_token(id_token):
    if (type(id_token) == bytes):
        segments = id_token.split(b'.')
    else:
        segments = id_token.split(u'.')
    if (len(segments) != 3):
        raise VerifyJwtTokenError('Wrong number of segments in token: {0}'.format(id_token))
    return json.loads(_helpers._from_bytes(_helpers._urlsafe_b64decode(segments[1])))

Extract the JSON payload from a JWT.

Does the extraction w/o checking the signature.

Args:
id_token: string or bytestring, OAuth 2.0 id_token.

Returns:
object, The deserialized JSON payload.


def auto_convert_cell(flagable, cell, position, worksheet, flags, units, parens_as_neg=True):

    conversion = cell

    if isinstance(cell, (int, float)):
        pass

    elif isinstance(cell, basestring):

        if not cell:
            conversion = None
        else:
            conversion = auto_convert_string_cell(flagable, cell, position, worksheet,
                                                  flags, units, parens_as_neg=parens_as_neg)

    elif cell != None:

        flagable.flag_change(flags, 'warning', position, worksheet,
                             flagable.FLAGS['unknown-to-string'])
        conversion = str(cell)

        if not conversion:
            conversion = None
    else:

        pass

    return conversion

Performs a first step conversion of the cell to check
it's type or try to convert if a valid conversion exists.

Args:
parens_as_neg: Converts numerics surrounded by parens to negative values


def get_shell_code(self, shell=None, parent_environ=None, style=OutputStyle.file):

        executor = self._create_executor(interpreter=create_shell(shell),
                                         parent_environ=parent_environ)

        if self.load_path and os.path.isfile(self.load_path):
            executor.env.REZ_RXT_FILE = self.load_path

        self._execute(executor)
        return executor.get_output(style)

Get the shell code resulting from intepreting this context.

Args:
shell (str): Shell type, for eg 'bash'. If None, the current shell
type is used.
parent_environ (dict): Environment to interpret the context within,
defaults to os.environ if None.
style (): Style to format shell code in.


def _set_values_internal(self, context, pipeline_key, root_pipeline_key, outputs, result_status):
    self._context = context
    self._pipeline_key = pipeline_key
    self._root_pipeline_key = root_pipeline_key
    self._result_status = result_status
    self.outputs = outputs

Sets the user-visible values provided as an API by this class.

Args:
context: The _PipelineContext used for this Pipeline.
pipeline_key: The db.Key of this pipeline.
root_pipeline_key: The db.Key of the root pipeline.
outputs: The PipelineFuture for this pipeline.
result_status: The result status of this pipeline.


def random_bernoulli(shape, p=0.0, dtype=None, seed=None):
    if dtype is None:
        dtype = floatx()
    if seed is None:
        seed = np.random.randint(10000000.0)
    return array_ops.where_v2(random_ops.random_uniform(shape, dtype=dtype, seed=seed) <= p, array_ops.ones(shape, dtype=dtype), array_ops.zeros(shape, dtype=dtype))

Returns a tensor with random bernoulli distribution of values.

Args:
shape: A tuple of integers, the shape of tensor to create.
p: A float, `0. <= p <= 1`, probability of bernoulli distribution.
dtype: String, dtype of returned tensor.
seed: Integer, random seed.

Returns:
A tensor.


def with_mfa(self, mfa_token):
    if hasattr(mfa_token, '__call__'):
        self.context.mfa_token = mfa_token.__call__()
    else:
        self.context.mfa_token = mfa_token
    return self

Set the MFA token for the next request.
`mfa_token`s are only good for one request. Use this method to chain into
the protected action you want to perform.

Note: Only useful for Application authentication.
Usage:
account.with_mfa(application.totp.now()).pay(...)

Args:
mfa_token (str/function, optional): TOTP token for the Application
OR a callable/function which will generate such a token when called.

Returns:
self


def send(self, src_file, filename, st_mode=DEFAULT_PUSH_MODE, mtime=None,
           timeout=None):

    transport = DataFilesyncTransport(self.stream)
    transport.write_data('SEND', '%s,%s' % (filename, st_mode), timeout)

    try:
      while True:
        data = src_file.read(MAX_PUSH_DATA_BYTES)
        if not data:
          break
        transport.write_data('DATA', data, timeout)

      mtime = mtime or int(time.time())
      transport.write_message(
          FilesyncMessageTypes.DoneMessage('DONE', mtime), timeout)
    except usb_exceptions.AdbStreamClosedError:

      self._check_for_fail_message(transport, sys.exc_info(), timeout)

    data_msg = transport.read_message(timeout)
    data_msg.assert_command_is('OKAY')

Push a file-like object to the device.

Args:
src_file: File-like object for reading from
filename: Filename to push to on the device
st_mode: stat mode for filename on the device
mtime: modification time to set for the file on the device
timeout: Timeout to use for the send operation.

Raises:
AdbProtocolError: If we get an unexpected response.
AdbRemoteError: If there's a remote error (but valid protocol).


def filter(self, versions, key=(lambda x: x)):
    return [x for x in versions if self.check(key(x))]

Filter all of the versions in an iterable that match this version range

Args:
versions (iterable): An iterable of SemanticVersion objects

Returns:
list: A list of the SemanticVersion objects that matched this range


def profile_update_args_v3(self, profile):
    ij = self.load_install_json(profile.get('install_json', 'install.json'))
    ijp = self.install_json_params(ij)
    if ((profile.get('args', {}).get('app', {}).get('optional') is None) and (profile.get('args', {}).get('app', {}).get('required') is None)):
        app_args = profile['args'].pop('app')
        profile['args']['app'] = {}
        profile['args']['app']['optional'] = {}
        profile['args']['app']['required'] = {}
        for arg in self.profile_settings_args_install_json(ij, None):
            required = ijp.get(arg).get('required', False)
            try:
                if required:
                    profile['args']['app']['required'][arg] = app_args.pop(arg)
                else:
                    profile['args']['app']['optional'][arg] = app_args.pop(arg)
            except KeyError:
                if self.args.verbose:
                    print('{}{}Input "{}" not found in profile "{}".'.format(c.Style.BRIGHT, c.Fore.YELLOW, arg, profile.get('profile_name')))
        print('{}{}Updating args section to v3 schema for profile {}.'.format(c.Style.BRIGHT, c.Fore.YELLOW, profile.get('profile_name')))

Update v1 profile args to v3 schema for args.

.. code-block:: javascript

"args": {
"app": {
"required": {
"input_strings": "capitalize",
"tc_action": "Capitalize"
},
"optional": {
"fail_on_error": true
}
}
},
"default": {
"api_access_id": "$env.API_ACCESS_ID",
"api_default_org": "$env.API_DEFAULT_ORG",
},

Args:
profile (dict): The dictionary containting the profile settings.


def write_filepath(filepath, strategy):
    dirpath = os.path.dirname(filepath)
    base = os.path.basename(filepath)
    return os.path.join(write_dirpath(dirpath, strategy), base)

Returns the writing file path to be used to save file distributedly.

Directory to contain `filepath` would be created if it doesn't exist.

Args:
filepath: Original filepath that would be used without distribution.
strategy: The tf.distribute strategy object currently used.

Returns:
The writing filepath that should be used to save file with distribution.


def __init__(self, name, type_var, impl_type, type_checker):

        assert isinstance(name, str), repr(name)
        assert isinstance(impl_type, type), repr(impl_type)
        assert not isinstance(impl_type, TypingMeta), repr(impl_type)
        assert isinstance(type_var, (type, _TypingBase)), repr(type_var)
        self.name = name
        self.type_var = type_var
        self.impl_type = impl_type
        self.type_checker = type_checker

Initializer.

Args:
name: The name, e.g. 'Pattern'.
type_var: The type parameter, e.g. AnyStr, or the
specific type, e.g. str.
impl_type: The implementation type.
type_checker: Function that takes an impl_type instance.
and returns a value that should be a type_var instance.


def pbc_diff(fcoords1, fcoords2):
    fdist = np.subtract(fcoords1, fcoords2)
    return (fdist - np.round(fdist))

Returns the 'fractional distance' between two coordinates taking into
account periodic boundary conditions.

Args:
fcoords1: First set of fractional coordinates. e.g., [0.5, 0.6,
0.7] or [[1.1, 1.2, 4.3], [0.5, 0.6, 0.7]]. It can be a single
coord or any array of coords.
fcoords2: Second set of fractional coordinates.

Returns:
Fractional distance. Each coordinate must have the property that
abs(a) <= 0.5. Examples:
pbc_diff([0.1, 0.1, 0.1], [0.3, 0.5, 0.9]) = [-0.2, -0.4, 0.2]
pbc_diff([0.9, 0.1, 1.01], [0.3, 0.5, 0.9]) = [-0.4, -0.4, 0.11]


def serialize_to_json(self, name, datas):

        data_object = datas.get('object', None)

        if data_object is None:
            msg = ("JSON reference '{}' lacks of required 'object' variable")
            raise SerializerError(msg.format(name))

        try:
            content = json.loads(data_object, object_pairs_hook=OrderedDict)
        except json.JSONDecodeError as e:
            msg = "JSON reference '{}' raised error from JSON decoder: {}"
            raise SerializerError(msg.format(name, e))
        else:
            return content

Serialize given datas to any object from assumed JSON string.

Arguments:
name (string): Name only used inside possible exception message.
datas (dict): Datas to serialize.

Returns:
object: Object depending from JSON content.


def from_path_and_array(cls, path, folder, y, classes=None, val_idxs=None, test_name=None, num_workers=8, tfms=(None, None), bs=64):
    assert (not ((tfms[0] is None) or (tfms[1] is None))), 'please provide transformations for your train and validation sets'
    assert (not os.path.isabs(folder)), 'folder needs to be a relative path'
    fnames = np.core.defchararray.add(f'{folder}/', sorted(os.listdir(f'{path}{folder}')))
    return cls.from_names_and_array(path, fnames, y, classes, val_idxs, test_name, num_workers=num_workers, tfms=tfms, bs=bs)

Read in images given a sub-folder and their labels given a numpy array

Arguments:
path: a root path of the data (used for storing trained models, precomputed values, etc)
folder: a name of the folder in which training images are contained.
y: numpy array which contains target labels ordered by filenames.
bs: batch size
tfms: transformations (for data augmentations). e.g. output of `tfms_from_model`
val_idxs: index of images to be used for validation. e.g. output of `get_cv_idxs`.
If None, default arguments to get_cv_idxs are used.
test_name: a name of the folder which contains test images.
num_workers: number of workers

Returns:
ImageClassifierData


def _fits_surface(self, width, height):
    assert ((width > 0) and (height > 0))
    if (self.rot and ((width > self.width) or (height > self.height))):
        (width, height) = (height, width)
    if ((width > self.width) or (height > self.height)):
        return False
    else:
        return True

Test surface is big enough to place a rectangle

Arguments:
width (int, float): Rectangle width
height (int, float): Rectangle height

Returns:
boolean: True if it could be placed, False otherwise


def affine(img, angle, translate, scale, shear, resample=0, fillcolor=None):
    if (not _is_pil_image(img)):
        raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
    assert (isinstance(translate, (tuple, list)) and (len(translate) == 2)), 'Argument translate should be a list or tuple of length 2'
    assert (scale > 0.0), 'Argument scale should be positive'
    output_size = img.size
    center = (((img.size[0] * 0.5) + 0.5), ((img.size[1] * 0.5) + 0.5))
    matrix = _get_inverse_affine_matrix(center, angle, translate, scale, shear)
    kwargs = ({'fillcolor': fillcolor} if (PILLOW_VERSION[0] == '5') else {})
    return img.transform(output_size, Image.AFFINE, matrix, resample, **kwargs)

Apply affine transformation on the image keeping image center invariant

Args:
img (PIL Image): PIL Image to be rotated.
angle (float or int): rotation angle in degrees between -180 and 180, clockwise direction.
translate (list or tuple of integers): horizontal and vertical translations (post-rotation translation)
scale (float): overall scale
shear (float): shear angle value in degrees between -180 to 180, clockwise direction.
resample (``PIL.Image.NEAREST`` or ``PIL.Image.BILINEAR`` or ``PIL.Image.BICUBIC``, optional):
An optional resampling filter.
See `filters`_ for more information.
If omitted, or if the image has mode "1" or "P", it is set to ``PIL.Image.NEAREST``.
fillcolor (int): Optional fill color for the area outside the transform in the output image. (Pillow>=5.0.0)


def serialize(self, user=None):
    return {'content': self.body, 'type': self.typ, 'updated_at': self.updated_at, 'timestamp': self.updated_at, 'is_update': (not hasattr(self, 'unsaved')), 'attachments': [attachment.serialize() for attachment in self.attachment_set], 'title': self.msg_title, 'url': self.url, 'sender_name': self.sender.full_name, 'sender_key': self.sender.key, 'channel_key': self.channel.key, 'cmd': 'message', 'avatar_url': self.sender.avatar, 'key': self.key}

Serializes message for given user.

Note:
Should be called before first save(). Otherwise "is_update" will get wrong value.

Args:
user: User object

Returns:
Dict. JSON serialization ready dictionary object


def get_data_node(self, path: DataPath) -> Optional[DataNode]:

        addr = self.schema_data.path2route(path)
        node = self.schema
        for p in addr:
            node = node.get_data_child(*p)
            if node is None:
                return None
        return node

Return the data node addressed by a data path.

Args:
path: Data path.

Returns:
Data node if found in the schema, or ``None``.

Raises:
InvalidSchemaPath: If the schema path is invalid.


def read_mutiple_items(f, container_type, item_type, separator=' '):
    return __read(f, (lambda line: container_type((item_type(item) for item in line.split(separator)))))

Extract an iterable from the current line of a file-like object.

Args:
f (file): the file-like object to read from
container_type (type): type of the iterable that will be returned
item_type (type): type of the values that will be elements of the returned iterable
separator (str): the separator between two consecutive items

Returns:
The extracted iterable

Example:
The file "a.input" contains three lines and three comma-separated digits on each::

>>> with open("a.input") as f:
...     print(utools.files.read_multiple_items(f, list, int, separator=","))
...     print(utools.files.read_multiple_items(f, set, str, separator=","))
...     print(utools.files.read_multiple_items(f, tuple, float, separator=","))
...
[1, 2, 3]
{"4", "5", "6"}
(7.0, 8.0, 9.0)