def _make_columnar(self, x):

    if tensorshape_util.rank(x.shape) is not None:
      if tensorshape_util.rank(x.shape) == 1:
        x = x[tf.newaxis, :]
      return x
    shape = tf.shape(input=x)
    maybe_expanded_shape = tf.concat([
        shape[:-1],
        distribution_util.pick_vector(
            tf.equal(tf.rank(x), 1), [1], np.array([], dtype=np.int32)),
        shape[-1:],
    ], 0)
    return tf.reshape(x, maybe_expanded_shape)

Ensures non-scalar input has at least one column.

Example:
If `x = [1, 2, 3]` then the output is `[[1], [2], [3]]`.

If `x = [[1, 2, 3], [4, 5, 6]]` then the output is unchanged.

If `x = 1` then the output is unchanged.

Args:
x: `Tensor`.

Returns:
columnar_x: `Tensor` with at least two dimensions.


def task_ordinal_at_coordinates(self, device_coordinates):
    return self._topology_tasks[tuple(device_coordinates)]

Returns the TensorFlow task number attached to `device_coordinates`.

Args:
device_coordinates: An integer sequence describing a device's physical
coordinates in the TPU fabric.

Returns:
Returns the TensorFlow task number that contains the TPU device with those
physical coordinates.


def unpack(self, buff, offset=0):

        super().unpack(buff, offset)

        try:
            self.oxm_field = self._unpack_oxm_field()
        except ValueError as exception:
            raise UnpackException(exception)

        self.oxm_hasmask = (self.oxm_field_and_mask & 1) == 1

        start = offset + 4
        end = start + self.oxm_length
        self.oxm_value = buff[start:end]

Unpack the buffer into a OxmTLV.

Args:
buff (bytes): The binary data to be unpacked.
offset (int): If we need to shift the beginning of the data.


def mpim_open(self, *, users: List[str], **kwargs) -> SlackResponse:

        kwargs.update({"users": users})
        return self.api_call("mpim.open", json=kwargs)

This method opens a multiparty direct message.

Args:
users (list): A lists of user ids. The ordering of the users
is preserved whenever a MPIM group is returned.
e.g. ['W1234567890', 'U2345678901', 'U3456789012']


def get_artist_location(self, cache=True):

        if not (cache and ('artist_location' in self.cache)):
            response = self.get_attribute('profile', bucket='artist_location')
            self.cache['artist_location'] = response['songs'][0]['artist_location']
        return self.cache['artist_location']

Get the location of a song's artist.

Args:
cache (bool): A boolean indicating whether or not the cached value should be used (if available). Defaults to True.

Returns:
An artist location object.

Example:
>>> s = song.Song('SOQKVPH12A58A7AF4D')
>>> s.artist_location
{u'latitude': 34.053489999999996, u'location': u'Los Angeles, CA', u'longitude': -118.24532000000001}
>>>


def apply(self, func, axis, *args, **kwargs):
    if callable(func):
        return self._callable_func(func, axis, *args, **kwargs)
    elif isinstance(func, dict):
        return self._dict_func(func, axis, *args, **kwargs)
    elif is_list_like(func):
        return self._list_like_func(func, axis, *args, **kwargs)
    else:
        pass

Apply func across given axis.

Args:
func: The function to apply.
axis: Target axis to apply the function along.

Returns:
A new PandasQueryCompiler.


def advance_for_next_slice(self, recovery_slice=False):

    if recovery_slice:
      self.slice_id += 2

      self.input_reader = self.input_reader.from_json(self._input_reader_json)
    else:
      self.slice_id += 1

Advance relavent states for next slice.

Args:
recovery_slice: True if this slice is running recovery logic.
See handlers.MapperWorkerCallbackHandler._attempt_slice_recovery
for more info.


def find_record(self, model_class, record_id, reload=False):
    cached_model = self.peek_record(model_class, record_id)
    if ((cached_model is not None) and (reload is False)):
        return cached_model
    else:
        return self._get_record(model_class, record_id)

Return a instance of model_class from the API or the local cache.

Args:
model_class (:class:`cinder_data.model.CinderModel`): A subclass of
:class:`cinder_data.model.CinderModel` of your chosen model.
record_id (int): The id of the record requested.
reload (bool, optional): Don't return the cached version if reload==True.

Returns:
:class:`cinder_data.model.CinderModel`: An instance of model_class or None.


def split_vector_ctype(ctype):
    if (not is_vector_ctype(ctype)):
        raise ValueError('The given ctype is not a vector type.')
    for vector_length in [2, 3, 4, 8, 16]:
        if ctype.endswith(str(vector_length)):
            vector_str_len = len(str(vector_length))
            return (ctype[:(- vector_str_len)], int(ctype[(- vector_str_len):]))

Split a vector ctype into a raw ctype and the vector length.

If the given ctype is not a vector type, we raise an error. I

Args:
ctype (str): the ctype to possibly split into a raw ctype and the vector length

Returns:
tuple: the raw ctype and the vector length


def reward(self, state: Sequence[tf.Tensor], action: Sequence[tf.Tensor], next_state: Sequence[tf.Tensor]) -> tf.Tensor:
    scope = self.reward_scope(state, action, next_state)
    r = self.compile_reward(scope).tensor
    with self.graph.as_default():
        with tf.name_scope('reward'):
            return tf.expand_dims(r, (- 1))

Compiles the reward function given the current `state`, `action` and
`next_state`.

Args:
state (Sequence[tf.Tensor]): A tuple of current state tensors.
action (Sequence[tf.Tensor]): A tuple of action tensors.
next_state (Sequence[tf.Tensor]): A tuple of next state tensors.

Returns:
(:obj:`tf.Tensor`): A tensor representing the reward function.


def request_with_retry(func, *args, **kwargs):

    max_retries = kwargs.pop('max_retries', 30)
    sleep = 2
    retry_count = 0
    while True:
        try:
            response = func(*args, **kwargs)
            response.raise_for_status()
            return response
        except (requests.exceptions.ConnectionError,
                requests.exceptions.HTTPError,
                requests.exceptions.Timeout) as e:
            if retry_count == max_retries:
                return e
            retry_count += 1
            delay = sleep + random.random() * 0.25 * sleep
            if isinstance(e, requests.exceptions.HTTPError) and e.response.status_code == 429:
                logger.info(
                    "Rate limit exceeded, retrying in %s seconds" % delay)
            else:
                logger.warning('requests_with_retry encountered retryable exception: %s. args: %s, kwargs: %s',
                               e, args, kwargs)
            time.sleep(delay)
            sleep *= 2
            if sleep > MAX_SLEEP_SECONDS:
                sleep = MAX_SLEEP_SECONDS
        except requests.exceptions.RequestException as e:
            logger.error(response.json()['error'])
            logger.exception(
                'requests_with_retry encountered unretryable exception: %s', e)
            return e

Perform a requests http call, retrying with exponential backoff.

Args:
func: An http-requesting function to call, like requests.post
max_retries: Maximum retries before giving up. By default we retry 30 times in ~2 hours before dropping the chunk
*args: passed through to func
**kwargs: passed through to func


def _get_param_matcher(self, callable_type):
    callable_param_count = collections.Counter(self.ctx.annotation_utils.get_type_parameters(callable_type))
    if isinstance(callable_type, abstract.CallableClass):
        callable_param_count.subtract(self.ctx.annotation_utils.get_type_parameters(callable_type.get_formal_type_parameter(abstract_utils.ARGS)))

    def match(left, right, subst):
        if not isinstance(left, abstract.TypeParameter) or not isinstance(right, abstract.TypeParameter) or right.constraints or right.bound or (callable_param_count[right] != 1):
            return None
        self._type_params.seen.add(right)
        subst = subst.copy()
        subst[right.full_name] = self.ctx.program.NewVariable([self.ctx.convert.empty], [], self._node)
        return subst
    return match

Helper for matching the parameters of a callable.

Args:
callable_type: The callable being matched against.

Returns:
A special param matcher: (left, right, subst) -> Optional[subst].
left: An argument to be matched against a parameter of callable_type.
right: A parameter of callable_type.
subst: The current substitution dictionary.
If the matcher returns a non-None subst dict, then the match has succeeded
via special matching rules for single TypeVars. Otherwise, the caller
should next attempt normal matching on the inputs. (See
_match_signature_against_callable for a usage example.)


def set_messages(self, messages: Sequence[CachedMessage]) -> None:

        uids = {msg.uid for msg in messages}
        expunged = self._messages._uids - uids
        return self.add_updates(messages, expunged)

This is the non-optimized alternative to :meth:`.add_updates` for
backend implementations that cannot detect their own updates and must
instead compare the entire state of the mailbox.

The ``messages`` list should contain the entire set of messages in the
mailbox, ordered by UID. Any UID that previously existed and is not
included in ``messages`` will be expunged.

Args:
messages: The entire set of cached message objects.


def _getfullargspec(target):
    return _convert_maybe_argspec_to_fullargspec(getargspec(target))

A python2 version of getfullargspec.

Args:
target: the target object to inspect.

Returns:
A FullArgSpec with empty kwonlyargs, kwonlydefaults and annotations.


def update_headers(self, headers):
    check_type(headers, dict, may_be_none=False)
    self._req_session.headers.update(headers)

Update the HTTP headers used for requests in this session.

Note: Updates provided by the dictionary passed as the `headers`
parameter to this method are merged into the session headers by adding
new key-value pairs and/or updating the values of existing keys. The
session headers are not replaced by the provided dictionary.

Args:
headers(dict): Updates to the current session headers.


async def puts(self, items, seqn=None):

        size = 0

        for chunk in s_common.chunks(items, 1000):
            metrics = self._items.save(chunk)
            self._metrics.add(metrics)
            await self.fire('cryotank:puts', numrecords=len(chunk))
            size += len(chunk)
            await asyncio.sleep(0)

        if seqn is not None:
            iden, offs = seqn
            self.setOffset(iden, offs + size)

        return size

Add the structured data from items to the CryoTank.

Args:
items (list):  A list of objects to store in the CryoTank.
seqn (iden, offs): An iden / offset pair to record.

Returns:
int: The ending offset of the items or seqn.


def fresh(t, non_generic):
    mappings = {}

    def freshrec(tp):
        p = prune(tp)
        if isinstance(p, TypeVariable):
            if is_generic(p, non_generic):
                if (p not in mappings):
                    mappings[p] = TypeVariable()
                return mappings[p]
            else:
                return p
        elif isinstance(p, dict):
            return p
        elif isinstance(p, Collection):
            return Collection(*[freshrec(x) for x in p.types])
        elif isinstance(p, Scalar):
            return Scalar([freshrec(x) for x in p.types])
        elif isinstance(p, TypeOperator):
            return TypeOperator(p.name, [freshrec(x) for x in p.types])
        elif isinstance(p, MultiType):
            return MultiType([freshrec(x) for x in p.types])
        else:
            assert False, 'missing freshrec case {}'.format(type(p))
    return freshrec(t)

Makes a copy of a type expression.

The type t is copied. The generic variables are duplicated and the
non_generic variables are shared.

Args:
t: A type to be copied.
non_generic: A set of non-generic TypeVariables


def conversations_replies(self, *, channel: str, ts: str, **kwargs) -> SlackResponse:
    kwargs.update({'channel': channel, 'ts': ts})
    return self.api_call('conversations.replies', http_verb='GET', params=kwargs)

Retrieve a thread of messages posted to a conversation

Args:
channel (str): Conversation ID to fetch thread from. e.g. 'C1234567890'
ts (str): Unique identifier of a thread's parent message. e.g. '1234567890.123456'


def _bfd_rx(self, **kwargs):

        int_type = kwargs['int_type']
        method_name = 'interface_%s_bfd_interval_min_rx' % int_type
        bfd_rx = getattr(self._interface, method_name)
        config = bfd_rx(**kwargs)
        if kwargs['delete']:
            tag = 'min-rx'
            config.find('.
            pass
        return config

Return the BFD minimum receive interval XML.

You should not use this method.
You probably want `BGP.bfd`.

Args:
min_rx (str): BFD receive interval in milliseconds (300, 500, etc)
delete (bool): Remove the configuration if ``True``.

Returns:
XML to be passed to the switch.

Raises:
None


def __init__(self,
               block_shape,
               block_rows,
               name='block_diagonal_matrix'):

    super(BlockDiagonalMatrix, self).__init__(
        block_shape=block_shape,
        block_rows=block_rows,
        include_diagonal=True,
        include_off_diagonal=False,
        name=name)

Constructs a new `BlockDiagonalMatrix` module.

Args:
block_shape: tuple, 2-dimensional tuple indicating the shape of each
individual block.
block_rows: int, the number of blocks in each row (and column) of the
output matrix.
name: string, name of the module.


def luhn(base, num_only=False, allow_lower_case=False):

    if num_only:
        alphabet = _ALPHABET[:10]
    else:
        alphabet = _ALPHABET
    if allow_lower_case:
        base = base.upper()
    try:
        pre_calc = (_PRE_CALC[alphabet.index(c)] for c in reversed(base))
        cum = 0
        parity = 1
        for elem in pre_calc:
            val, parity = elem[parity]
            cum += val
    except ValueError:
        pass
    else:
        return 10 - cum % 10

    if num_only:
        msg = 'The string given must only contain digits.'
    elif allow_lower_case:
        msg = 'The string given must only contain digits and ascii letters.'
    else:
        msg = 'The string given must only contain digits and upper case ' \
              'ascii letters.'
    raise ValueError(msg)

Return the Luhn check digit for the given string.

Args:
base(str): string for which to calculate the check digit
num_only(bool): allow only digits in `base` (default: False)
allow_lower_case(bool): allow lower case letters in `base`
(default: False)

Returns:
int: Luhn check digit

Raises:
ValueError: given `base` contains an unallowed character


def create_prefetch(self, addresses):
    with self._lock:
        for add in addresses:
            self._state[add] = _ContextFuture(address=add, wait_for_tree=True)

Create futures needed before starting the process of reading the
address's value from the merkle tree.

Args:
addresses (list of str): addresses in the txn's inputs that
aren't in any base context (or any in the chain).


def insert(self, start_time: int, schedule: ScheduleComponent) -> 'ScheduleComponent':

        return ops.insert(self, start_time, schedule)

Return a new schedule with `schedule` inserted within `self` at `start_time`.

Args:
start_time: time to be inserted
schedule: schedule to be inserted


def series_with_permutation(self, other):

        combined_permutation = tuple([self.permutation[p]
                                      for p in other.permutation])
        return CPermutation.create(combined_permutation)

Compute the series product with another channel permutation circuit

Args:
other (CPermutation):

Returns:
Circuit: The composite permutation circuit (could also be the
identity circuit for n channels)


def decstr2int(dec_str, decimals):

    if not isinstance(decimals, int):
        raise TypeError('decimals must be an integer')
    try:
        dollars, cents = dec_str.split('.')
    except ValueError:
        if '.' not in dec_str:
            dollars = dec_str
            cents = '0'
        else:
            raise ValueError('Invalid decimal string')
    else:
        if len(cents) < decimals:
            cents = cents.ljust(decimals, '0')
        elif decimals < 1:
            cents = '0'
        elif len(cents) > decimals:
            cents = cents[:decimals]
    try:
        cents = int(cents)
    except:
        cents = 0
    try:
        return int(int(dollars) * (10 ** decimals)) + cents
    except:
        raise ValueError('Invalid decimal string')

Returns an integer that has the value of the decimal string:
dec_str*10^decimals

Arguments:
dec_str (string) that represents a decimal number
decimals (int): number of decimals for creating the integer output
Returns:
(int)
Raises:
ValueError if dec_string is not a valid decimal string
TypeError if decimals is not an integer
Note: values may be truncated (not rounded).


def download_extract_tar(tar_url, folder, tar_filename=''):
    try:
        makedirs(folder)
    except OSError:
        if (not isdir(folder)):
            raise
    data_file = tar_filename
    if (not data_file):
        (fd, data_file) = mkstemp('.tar.gz')
        download(tar_url, os.fdopen(fd, 'wb'))
    else:
        download(tar_url, data_file)
    with tarfile.open(data_file) as tar:
        tar.extractall(path=folder)

Download and extract the tar at the url to the given folder

Args:
tar_url (str): URL of tar file to download
folder (str): Location of parent directory to extract to. Doesn't have to exist
tar_filename (str): Location to download tar. Default is to a temp file


def id_range(self):
    if (len(self._anchor_points) == 0):
        return (0, 0)
    return (self._anchor_points[0].reading_id, self._anchor_points[(- 1)].reading_id)

Get the range of archor reading_ids.

Returns:
(int, int): The lowest and highest reading ids.

If no reading ids have been loaded, (0, 0) is returned.


def request_json(link, outfile, force_rerun_flag, outdir=None):
    if (not outdir):
        outdir = ''
    outfile = op.join(outdir, outfile)
    if force_rerun(flag=force_rerun_flag, outfile=outfile):
        text_raw = requests.get(link)
        my_dict = text_raw.json()
        with open(outfile, 'w') as f:
            json.dump(my_dict, f)
        log.debug('Loaded and saved {} to {}'.format(link, outfile))
    else:
        with open(outfile, 'r') as f:
            my_dict = json.load(f)
        log.debug('Loaded {}'.format(outfile))
    return my_dict

Download a file in JSON format from a web request

Args:
link: Link to web request
outfile: Name of output file
outdir: Directory of output file
force_rerun_flag: If true, redownload the file

Returns:
dict: contents of the JSON request


def get_script_module(script_information, package='pylabcontrol', verbose=False):
    (module, _, _, _, _, _, _) = Script.get_script_information(script_information=script_information, package=package, verbose=verbose)
    return module

wrapper to get the module for a script

Args:
script_information: information of the script. This can be
- a dictionary
- a Script instance
- name of Script class
package (optional): name of the package to which the script belongs, i.e. pylabcontrol or b26toolkit only used when script_information is a string
Returns:
module


def async_call(self, *args, **kwargs):

    def after_autoconnect_callback(future):
        if self.is_connected():
            self._call(*args, **kwargs)
        else:
            pass
    if ('callback' not in kwargs):
        kwargs['callback'] = discard_reply_cb
    if (not self.is_connected()):
        if self.autoconnect:
            connect_future = self.connect()
            cb = after_autoconnect_callback
            self.__connection._ioloop.add_future(connect_future, cb)
        else:
            error = ConnectionError('you are not connected and autoconnect=False')
            kwargs['callback'](error)
    else:
        self._call(*args, **kwargs)

Calls a redis command, waits for the reply and call a callback.

Following options are available (not part of the redis command itself):

- callback
Function called (with the result as argument) when the result
is available. If not set, the reply is silently discarded. In
case of errors, the callback is called with a
TornadisException object as argument.

Args:
*args: full redis command as variable length argument list or
a Pipeline object (as a single argument).
**kwargs: options as keyword parameters.

Examples:

>>> def cb(result):
pass
>>> client.async_call("HSET", "key", "field", "val", callback=cb)


def event_date(self, event_date):

        if not self.can_update():
            self._tcex.handle_error(910, [self.type])

        event_date = self._utils.format_datetime(event_date, date_format='%Y-%m-%dT%H:%M:%SZ')
        self._data['eventDate'] = event_date
        request = {'eventDate': event_date}
        return self.tc_requests.update(self.api_type, self.api_sub_type, self.unique_id, request)

Updates the event_date.

Args:
event_date: Converted to %Y-%m-%dT%H:%M:%SZ date format.

Returns:


def parsed_top_level_errors(parsed, errors, component_type: str='') -> Errors:
    fn_cnt = 0
    rel_cnt = 0
    nested_cnt = 0
    for key in parsed:
        if (parsed[key]['type'] == 'Function'):
            fn_cnt += 1
        if (parsed[key]['type'] == 'Relation'):
            rel_cnt += 1
        if (parsed[key]['type'] == 'Nested'):
            nested_cnt += 1
    if (not component_type):
        if (nested_cnt > 1):
            errors.append(('Error', 'Too many nested objects - can only have one per BEL Assertion'))
        if nested_cnt:
            if (rel_cnt > 2):
                errors.append(('Error', 'Too many relations - can only have two in a nested BEL Assertion'))
            elif (fn_cnt > 4):
                errors.append(('Error', 'Too many BEL subject and object candidates'))
        elif (rel_cnt > 1):
            errors.append(('Error', 'Too many relations - can only have one in a BEL Assertion'))
        elif (fn_cnt > 2):
            errors.append(('Error', 'Too many BEL subject and object candidates'))
    elif (component_type == 'subject'):
        if (rel_cnt > 0):
            errors.append(('Error', 'Too many relations - cannot have any in a BEL Subject'))
        elif (fn_cnt > 1):
            errors.append(('Error', 'Too many BEL subject candidates - can only have one'))
    elif (component_type == 'object'):
        if nested_cnt:
            if (rel_cnt > 1):
                errors.append(('Error', 'Too many relations - can only have one in a nested BEL object'))
            elif (fn_cnt > 2):
                errors.append(('Error', 'Too many BEL subject and object candidates in a nested BEL object'))
        elif (rel_cnt > 0):
            errors.append(('Error', 'Too many relations - cannot have any in a BEL Subject'))
        elif (fn_cnt > 1):
            errors.append(('Error', 'Too many BEL subject candidates - can only have one'))
    return errors

Check full parse for errors

Args:
parsed:
errors:
component_type: Empty string or 'subject' or 'object' to indicate that we
are parsing the subject or object field input


def extraction_data_statistics(path):
    with functions.DBContextManager(path) as session:
        extraction = session.query(models.Extraction).first()
        (X, y) = extraction.return_main_dataset()
        functions.verify_dataset(X, y)
        if (extraction.test_dataset['method'] == 'split_from_main'):
            (X, X_test, y, y_test) = train_test_split(X, y, test_size=extraction.test_dataset['split_ratio'], random_state=extraction.test_dataset['split_seed'], stratify=y)
        elif (extraction.test_dataset['method'] == 'source'):
            if (('source' not in extraction.test_dataset) or (not extraction.test_dataset['source'])):
                raise exceptions.UserError('Source is empty')
            extraction_code = extraction.test_dataset['source']
            extraction_function = functions.import_object_from_string_code(extraction_code, 'extract_test_dataset')
            (X_test, y_test) = extraction_function()
        else:
            (X_test, y_test) = (None, None)
        extraction_code = extraction.meta_feature_generation['source']
        return_splits_iterable = functions.import_object_from_string_code(extraction_code, 'return_splits_iterable')
        number_of_splits = 0
        test_indices = []
        try:
            for (train_idx, test_idx) in return_splits_iterable(X, y):
                number_of_splits += 1
                test_indices.append(test_idx)
        except Exception as e:
            raise exceptions.UserError('User code exception', exception_message=str(e))
        test_indices = np.concatenate(test_indices)
        (X, y) = (X[test_indices], y[test_indices])
        extraction_code = extraction.stacked_ensemble_cv['source']
        return_splits_iterable = functions.import_object_from_string_code(extraction_code, 'return_splits_iterable')
        number_of_splits_stacked_cv = 0
        try:
            for (train_idx, test_idx) in return_splits_iterable(X, y):
                number_of_splits_stacked_cv += 1
        except Exception as e:
            raise exceptions.UserError('User code exception', exception_message=str(e))
        data_stats = dict()
        data_stats['train_data_stats'] = functions.verify_dataset(X, y)
        if (X_test is not None):
            data_stats['test_data_stats'] = functions.verify_dataset(X_test, y_test)
        else:
            data_stats['test_data_stats'] = None
        data_stats['holdout_data_stats'] = {'number_of_splits': number_of_splits}
        data_stats['stacked_ensemble_cv_stats'] = {'number_of_splits': number_of_splits_stacked_cv}
        extraction.data_statistics = data_stats
        session.add(extraction)
        session.commit()

Generates data statistics for the given data extraction setup stored
in Xcessiv notebook.

This is in rqtasks.py but not as a job yet. Temporarily call this directly
while I'm figuring out Javascript lel.

Args:
path (str, unicode): Path to xcessiv notebook


def call(method: Method, *args: Any, **kwargs: Any) -> Any:
    return validate_args(method, *args, **kwargs)(*args, **kwargs)

Validates arguments and then calls the method.

Args:
method: The method to call.
*args, **kwargs: Arguments to the method.

Returns:
The "result" part of the JSON-RPC response (the return value from the method).

Raises:
TypeError: If arguments don't match function signature.


def get_access_token(self, http=None, additional_claims=None):
    if (additional_claims is None):
        if ((self.access_token is None) or self.access_token_expired):
            self.refresh(None)
        return client.AccessTokenInfo(access_token=self.access_token, expires_in=self._expires_in())
    else:
        (token, unused_expiry) = self._create_token(additional_claims)
        return client.AccessTokenInfo(access_token=token, expires_in=self._MAX_TOKEN_LIFETIME_SECS)

Create a signed jwt.

Args:
http: unused
additional_claims: dict, additional claims to add to
the payload of the JWT.
Returns:
An AccessTokenInfo with the signed jwt


def AddStopTime(self, stop, problems=None, schedule=None, **kwargs):
    if (problems is None):
        problems = problems_module.default_problem_reporter
    stoptime = self.GetGtfsFactory().StopTime(problems=problems, stop=stop, **kwargs)
    self.AddStopTimeObject(stoptime, schedule)

Add a stop to this trip. Stops must be added in the order visited.

Args:
stop: A Stop object
kwargs: remaining keyword args passed to StopTime.__init__

Returns:
None


async def get_final_ranking(self) -> OrderedDict:
    if (self._state != TournamentState.complete.value):
        return None
    ranking = {}
    for p in self.participants:
        if (p.final_rank in ranking):
            ranking[p.final_rank].append(p)
        else:
            ranking[p.final_rank] = [p]
    return OrderedDict(sorted(ranking.items(), key=(lambda t: t[0])))

Get the ordered players ranking

Returns:
collections.OrderedDict[rank, List[Participant]]:

Raises:
APIException


def _snapshot_device_function_stack_metadata(self) -> list[traceable_stack.TraceableObject]:
    snapshot = []
    for obj in self._device_function_stack.peek_traceable_objs():
        obj_copy = obj.copy_metadata()
        obj_copy.obj = obj.obj.display_name
        snapshot.append(obj_copy)
    return snapshot

Return device function stack as a list of TraceableObjects.

Returns:
[traceable_stack.TraceableObject, ...] where each TraceableObject's .obj
member is a displayable name for the user's argument to Graph.device, and
the filename and lineno members point to the code location where
Graph.device was called directly or indirectly by the user.


def _is_autocomplete_valid(cur_commands, alias_command):

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

Determine whether autocomplete can be performed at the current state.

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

Returns:
True if autocomplete can be performed.


def period_from_list(period: Tuple[int, List[int]]) -> dateslib.PeriodTensor:
    amount = period[1]
    period_type = period_pb2.PeriodType.Name(period[0])
    return dateslib.PeriodTensor(amount, dateslib.PeriodType[period_type])

Utility to convert a list of periods to a PeriodTensor.

Args:
period: A tuple of an integer (which corresponds to the proto type of the
period (see `period_pb2.Period`)) and a list of period values.

Returns:
An instance of the `PeriodTensor`.


def window_unpartition(self, windows: torch.Tensor, window_size: int, padding_shape: Tuple[int, int], original_shape: Tuple[int, int]) -> torch.Tensor:
    pad_height, pad_width = padding_shape
    height, width = original_shape
    batch_size = windows.shape[0]
    hidden_states = windows.reshape(batch_size, pad_height
    hidden_states = hidden_states.permute(0, 1, 3, 2, 4, 5).contiguous().reshape(batch_size, pad_height, pad_width, -1)
    hidden_states = hidden_states[:, :height, :width, :].contiguous()
    return hidden_states

Args:
Window unpartition into original sequences and removing padding.
hidden_states (tensor):
input tokens with [batch_size * num_windows, window_size, window_size, channel].
window_size (int):
window size.
padding_shape (Tuple):
padded height and width (pad_height, pad_width).
original_shape (Tuple): original height and width (height, width) before padding.

Returns:
hidden_states: unpartitioned sequences with [batch_size, height, width, channel].


def rsolve(A, b, epsilon=_epsilon):
    A = asarray(A, float)
    b = asarray(b, float)
    if (A.shape[0] == 0):
        return zeros((A.shape[1],))
    if (A.shape[1] == 0):
        return zeros((0,))
    try:
        x = lstsq(A, b, rcond=epsilon)
        r = sum((x[3] > epsilon))
        if (r == 0):
            return zeros(A.shape[1])
        return x[0]
    except (ValueError, LinAlgError) as e:
        warnings.warn(str(e), RuntimeWarning)
    return solve(A, b)

r"""Robust solve for the linear equations.

Args:
A (array_like): Coefficient matrix.
b (array_like): Ordinate values.

Returns:
:class:`numpy.ndarray`: Solution ``x``.


def broadcast_tensors(*args: Sequence[tf.Tensor], name: Optional[str]=None) -> Tuple[tf.Tensor]:
    name = 'broadcast_tensors' if name is None else name
    with tf.name_scope(name):
        output_shape = common_shape(*args)
        return tuple((tf.broadcast_to(arg, output_shape) for arg in args))

Broadcasts arguments to the common shape.

#### Example
```python
import tensorflow as tf
import tf_quant_finance as tff

args = [tf.ones([1, 2], dtype=tf.float64), tf.constant([[True], [False]])]
tff.utils.broadcast_tensor_shapes(*args)
# Expected: (array([[1., 1.], [1., 1.]]),
#            array([[True, True], [False, False]])
```

Args:
*args: A sequence of `Tensor`s of compatible shapes and any `dtype`s.
name: Python string. The name to give to the ops created by this function.
Default value: `None` which maps to the default name
`broadcast_tensor_shapes`.

Returns:
A tuple of broadcasted `Tensor`s. Each `Tensor` has the same `dtype` as the
corresponding input `Tensor`.

Raises:
ValueError: If inputs are of incompatible shapes.


def _infer_binary_broadcast_shape(shape1, shape2, given_output_shape=None):
    shape1 = convert_to_shape(shape1)
    shape2 = convert_to_shape(shape2)
    given_output_shape = convert_to_shape(given_output_shape)
    if (given_output_shape is not None):
        return given_output_shape
    if is_subsequence(shape1.dims, shape2.dims):
        return shape2
    if is_subsequence(shape2.dims, shape1.dims):
        return shape1
    return Shape((shape1.dims + [d for d in shape2.dims if (d not in shape1.dims)]))

Infer shape of the output of a binary op with broadcasting.

If the output shape is not given with given_output_shape, then we check
to see if one of the shapes is a subsequence of the other one, and we
return the one that is the supersequence.  Otherwise, we list the dimensions
of shape1, followed by all new dimensions in shape2.

Args:
shape1: a Shape
shape2: a Shape
given_output_shape: an optional Shape
Returns:
a Shape


def collapse(self, dimensions=None, function=None, spreadfn=None, **kwargs):
    from .data import concat
    if (not dimensions):
        dimensions = self.kdims
    if (not isinstance(dimensions, list)):
        dimensions = [dimensions]
    if ((self.ndims > 1) and (len(dimensions) != self.ndims)):
        groups = self.groupby([dim for dim in self.kdims if (dim not in dimensions)])
    elif all(((d in self.kdims) for d in dimensions)):
        groups = HoloMap([(0, self)])
    else:
        raise KeyError('Supplied dimensions not found.')
    collapsed = groups.clone(shared_data=False)
    for (key, group) in groups.items():
        if hasattr(group.last, 'interface'):
            group_data = concat(group)
            if function:
                agg = group_data.aggregate(group.last.kdims, function, spreadfn, **kwargs)
                group_data = group.type(agg)
        else:
            group_data = [el.data for el in group]
            args = (group_data, function, group.last.kdims)
            data = group.type.collapse_data(*args, **kwargs)
            group_data = group.last.clone(data)
        collapsed[key] = group_data
    return (collapsed if (self.ndims - len(dimensions)) else collapsed.last)

Concatenates and aggregates along supplied dimensions

Useful to collapse stacks of objects into a single object,
e.g. to average a stack of Images or Curves.

Args:
dimensions: Dimension(s) to collapse
Defaults to all key dimensions
function: Aggregation function to apply, e.g. numpy.mean
spreadfn: Secondary reduction to compute value spread
Useful for computing a confidence interval, spread, or
standard deviation.
**kwargs: Keyword arguments passed to the aggregation function

Returns:
Returns the collapsed element or HoloMap of collapsed
elements


def url_to_text(self, url):
    (path, headers) = urllib.request.urlretrieve(url)
    return self.path_to_text(path)

Download PDF file and transform its document to string.

Args:
url:   PDF url.

Returns:
string.


def _to_snake_case(string):
    sub_string = '\\1_\\2'
    string = REGEX_CAMEL_FIRST.sub(sub_string, string)
    return REGEX_CAMEL_SECOND.sub(sub_string, string).lower()

Return a snake cased version of the input string.

Args:
string (str): A camel cased string.

Returns:
str: A snake cased string.


def GetHelp(self, prefix='', include_special_flags=True):

    helplist = []

    flags_by_module = self.FlagsByModuleDict()
    if flags_by_module:
      modules = sorted(flags_by_module)

      main_module = sys.argv[0]
      if main_module in modules:
        modules.remove(main_module)
        modules = [main_module] + modules

      for module in modules:
        self.__RenderOurModuleFlags(module, helplist)
      if include_special_flags:
        self.__RenderModuleFlags('gflags',
                                 _helpers.SPECIAL_FLAGS.FlagDict().values(),
                                 helplist)
    else:

      values = self.FlagDict().values()
      if include_special_flags:
        values.append(_helpers.SPECIAL_FLAGS.FlagDict().values())
      self.__RenderFlagList(values, helplist, prefix)

    return '\n'.join(helplist)

Generates a help string for all known flags.

Args:
prefix: str, per-line output prefix.
include_special_flags: bool, whether to include description of
_SPECIAL_FLAGS, i.e. --flagfile and --undefok.

Returns:
str, formatted help message.


def close(self):
    if (self._fd is None):
        return
    try:
        os.close(self._fd)
    except OSError as e:
        raise LEDError(e.errno, ('Closing LED: ' + e.strerror))
    self._fd = None

Close the sysfs LED.

Raises:
LEDError: if an I/O or OS error occurs.


def clear(self, url=None, xpath=None):

        if url is not None:
            query = self._query(url, xpath)
            if query.count() > 0:
                query.delete()
                self.session.commit()
            else:
                raise KeyError("Cannot clear URL, not in cache: " + str(url) + " xpath:" + str(xpath))
        else:

            self.close()
            if path.exists(self.db_path):
                remove(self.db_path)

Clear cache

Args:
url (str): If given, clear specific item only. Otherwise remove the DB file.
xpath (str): xpath to search (may be ``None``)


def set_atten(self, idx, value):
    if not self.is_open:
        raise attenuator.Error('Connection to attenuator at %s is not open!' % self._telnet_client.host)
    if idx + 1 > self.path_count:
        raise IndexError('Attenuator index out of range!', self.path_count, idx)
    if value > self.max_atten:
        raise ValueError('Attenuator value out of range!', self.max_atten, value)
    self._telnet_client.cmd('CHAN:%s:SETATT:%s' % (idx + 1, value))

Sets the attenuation value for a particular signal path.

Args:
idx: Zero-based index int which is the identifier for a particular
signal path in an instrument. For instruments that only has one
channel, this is ignored by the device.
value: A float that is the attenuation value to set.

Raises:
Error: The underlying telnet connection to the instrument is not
open.
IndexError: The index of the attenuator is greater than the maximum
index of the underlying instrument.
ValueError: The requested set value is greater than the maximum
attenuation value.


def expand(self, pcoll):
    return pcoll | core.CombinePerKey(TopCombineFn(self._n, self._key, self._reverse))

Expands the transform.

Raises TypeCheckError: If the output type of the input PCollection is not
compatible with tuple[A, B].

Args:
pcoll: PCollection to process

Returns:
the PCollection containing the result.


def put(value):
    worker = global_worker
    worker.check_connected()
    with profiling.profile('ray.put'):
        if (worker.mode == LOCAL_MODE):
            return value
        object_id = ray._raylet.compute_put_id(worker.current_task_id, worker.task_context.put_index)
        worker.put_object(object_id, value)
        worker.task_context.put_index += 1
        return object_id

Store an object in the object store.

Args:
value: The Python object to be stored.

Returns:
The object ID assigned to this value.


def is_separating(direction, polygon1, polygon2):
    norm_squared = ((direction[0] * direction[0]) + (direction[1] * direction[1]))
    params = []
    vertex = np.empty((2,), order='F')
    for polygon in (polygon1, polygon2):
        (_, polygon_size) = polygon.shape
        min_param = np.inf
        max_param = (- np.inf)
        for index in six.moves.xrange(polygon_size):
            vertex[:] = polygon[(:, index)]
            param = (cross_product(direction, vertex) / norm_squared)
            min_param = min(min_param, param)
            max_param = max(max_param, param)
        params.append((min_param, max_param))
    return ((params[0][0] > params[1][1]) or (params[0][1] < params[1][0]))

Checks if a given ``direction`` is a separating line for two polygons.

.. note::

This is a helper for :func:`_polygon_collide`.

Args:
direction (numpy.ndarray): A 1D ``2``-array (``float64``) of a
potential separating line for the two polygons.
polygon1 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered
points in a polygon.
polygon2 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered
points in a polygon.

Returns:
bool: Flag indicating if ``direction`` is a separating line.


def __init__(self, lookup_list, do_not_log_prefix=None):
    super().__init__()
    self._lookup_list = lookup_list
    self._do_not_log_prefix = do_not_log_prefix

Create this visitor.

Args:
lookup_list: An iterable of symbol tables (i.e., objects that have a
"lookup" function)
do_not_log_prefix: If given, don't log error messages for classes with
this prefix.


def average_name(self, var):
    if var.ref() in self._averages:
        return self._averages[var.ref()].name[:-len(':0')]
    return ops.get_default_graph().unique_name(var.name[:-len(':0')] + '/' + self.name, mark_as_used=False)

[Meant for TF1] Returns name of `Variable` holding the average for `var`.

(Designed to work with legacy `tf.compat.v1.train.Saver`, it is sensitive to
specific variable names and not recommended for TF2)

The typical scenario for `ExponentialMovingAverage` is to compute moving
averages of variables during training, and restore the variables from the
computed moving averages during evaluations.

To restore variables, you have to know the name of the shadow variables.
That name and the original variable can then be passed to a `Saver()` object
to restore the variable from the moving average value with:
`saver = tf.compat.v1.train.Saver({ema.average_name(var): var})`

`average_name()` can be called whether or not `apply()` has been called.

Args:
var: A `Variable` object.

Returns:
A string: The name of the variable that will be used or was used
by the `ExponentialMovingAverage class` to hold the moving average of
`var`.


def create_dummy_files(backend_specific_objects: Optional[Dict[str, List[str]]]=None) -> Dict[str, str]:
    if backend_specific_objects is None:
        backend_specific_objects = read_init()
    dummy_files = {}
    for backend, objects in backend_specific_objects.items():
        backend_name = '[' + ', '.join((f'"{b}"' for b in backend.split('_and_'))) + ']'
        dummy_file = '
        dummy_file += 'from ..utils import DummyObject, requires_backends\n\n'
        dummy_file += '\n'.join([create_dummy_object(o, backend_name) for o in objects])
        dummy_files[backend] = dummy_file
    return dummy_files

Create the content of the dummy files.

Args:
backend_specific_objects (`Dict[str, List[str]]`, *optional*):
The mapping backend name to list of backend-specific objects. If not passed, will be obtained by calling
`read_init()`.

Returns:
`Dict[str, str]`: A dictionary mapping backend name to code of the corresponding backend file.


def _data_to_tensor(data_list, batch_size, name=None):
    r

    const_list = [tf.constant(data) for data in data_list]

    queue_list = tf.train.slice_input_producer(const_list, capacity=batch_size*128, name=name)

    return tf.train.shuffle_batch(queue_list, batch_size, capacity=batch_size*128,
                                  min_after_dequeue=batch_size*32, name=name)

r"""Returns batch queues from the whole data.

Args:
data_list: A list of ndarrays. Every array must have the same size in the first dimension.
batch_size: An integer.
name: A name for the operations (optional).

Returns:
A list of tensors of `batch_size`.


def pairwise(lst):

    if not lst:
        return
    length = len(lst)
    for i in range(length - 1):
        yield lst[i], lst[i + 1]
    yield lst[-1], None

yield item i and item i+1 in lst. e.g.
(lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None)
Args:
lst (list): List to process
Returns:
list


def _recur_flatten(key, x, out, sep='.'):

    if x is None or isinstance(x, (str, int, float, bool)):
        out[key] = x
        return out
    if isinstance(x, list):
        for i, v in enumerate(x):
            new_key = '{}{}{}'.format(key, sep, i)
            out = _recur_flatten(new_key, v, out, sep)
    if isinstance(x, dict):
        for k, v in x.items():
            new_key = '{}{}{}'.format(key, sep, k)
            out = _recur_flatten(new_key, v, out, sep)
    return out

Helper function to flatten_dict

Recursively flatten all nested values within a dict

Args:
key (str): parent key
x (object): object to flatten or add to out dict
out (dict): 1D output dict
sep (str): flattened key separator string

Returns:
dict: flattened 1D dict


def disambiguate_pdf(self, file, language=None, entities=None):

        body = {
            "customisation": "generic"
        }

        if language:
            body['language'] = {"lang": language}

        if entities:
            body['entities'] = entities

        files = {
            'query': str(body),
            'file': (
                file,
                open(file, 'rb'),
                'application/pdf',
                {'Expires': '0'}
            )
        }

        res, status = self.post(
            self.disambiguate_service,
            files=files,
            headers={'Accept': 'application/json'},
        )

        if status != 200:
            logger.debug('Disambiguation failed with error ' + str(status))

        return self.decode(res), status

Call the disambiguation service in order to process a pdf file .

Args:
pdf (file): PDF file to be disambiguated.
language (str): language of text (if known)

Returns:
dict, int: API response and API status.


def UploadSignedBinary(source_path,
                       binary_type,
                       platform,
                       upload_subdirectory=""):

  file_size = os.path.getsize(source_path)
  if file_size > _MAX_SIGNED_BINARY_BYTES:
    raise BinaryTooLargeError(
        "File [%s] is of size %d (bytes), which exceeds the allowed maximum "
        "of %d bytes." % (source_path, file_size, _MAX_SIGNED_BINARY_BYTES))

  context = ["Platform:%s" % platform.title(), "Client Context"]
  signing_key = grr_config.CONFIG.Get(
      "PrivateKeys.executable_signing_private_key", context=context)

  root_api = maintenance_utils.InitGRRRootAPI()
  binary_path = "/".join([
      platform.lower(),
      upload_subdirectory,
      os.path.basename(source_path),
  ])
  binary = root_api.GrrBinary(binary_type, binary_path)

  with open(source_path, "rb") as fd:
    binary.Upload(
        fd,
        sign_fn=binary.DefaultUploadSigner(
            private_key=signing_key.GetRawPrivateKey()))

  print("Uploaded %s to %s" % (binary_type, binary_path))

Signs a binary and uploads it to the datastore.

Args:
source_path: Path to the binary to upload.
binary_type: Type of the binary, e.g python-hack or executable.
platform: Client platform where the binary is intended to be run.
upload_subdirectory: Path of a subdirectory to upload the binary to,
relative to the canonical path for binaries of the given type and
platform.

Raises:
BinaryTooLargeError: If the binary to upload is too large.


def GetParserObjectByName(cls, parser_name):
    parser_class = cls._parser_classes.get(parser_name, None)
    if parser_class:
        return parser_class()
    return None

Retrieves a specific parser object by its name.

Args:
parser_name (str): name of the parser.

Returns:
BaseParser: parser object or None.


def update_device_info(self, device_id, display_name):
    content = {'display_name': display_name}
    return self._send('PUT', ('/devices/%s' % device_id), content=content)

Update the display name of a device.

Args:
device_id (str): The device ID of the device to update.
display_name (str): New display name for the device.


def _format_device(var):

  if var.dtype.name.endswith("_ref"):
    resource_var_annotation = "(legacy)"
  else:
    resource_var_annotation = "(resource)"

  if var.device:
    return "{} {}".format(var.device, resource_var_annotation)
  else:
    return resource_var_annotation

Returns the device with an annotation specifying `ResourceVariable`.

"legacy" means a normal tf.Variable while "resource" means a ResourceVariable.

For example:
`(legacy)`
`(resource)`
`/job:learner/task:0/device:CPU:* (legacy)`
`/job:learner/task:0/device:CPU:* (resource)`

Args:
var: The Tensorflow Variable to print.


def getInfo(self, query=None, process=False, mode='phonefy', qURI=None):
    results = []
    data = ''
    if (self._modeIsValid(mode=mode) and self._isValidQuery(query, mode=mode)):
        if (mode in ['mailfy', 'phonefy', 'searchfy', 'usufy']):
            try:
                results = getattr(self, 'do_{}'.format(mode))(query)
            except AttributeError as e:
                raise NotImplementedModeError(str(self), mode)
    return json.dumps(results)

Method that checks the presence of a given query and recovers the first list of complains.

Args:
-----
query: Query to verify.
process: Calling the processing function.
mode: Mode to be executed.
qURI: A query to be checked.

Return:
-------
Python structure for the html processed.

Raises:
-------
NoCredentialsException.
NotImplementedModeError.
BadImplementationError.


def join(self, other, **kwargs):

        if not isinstance(other, list):
            other = [other]
        return self._join_list_of_managers(other, **kwargs)

Joins a list or two objects together.

Args:
other: The other object(s) to join on.

Returns:
Joined objects.


def to_proto(self, export_scope=None):
    if context.executing_eagerly():
        raise RuntimeError('This operation is not supported when eager execution is enabled.')
    if export_scope is None or self.handle.name.startswith(export_scope):
        var_def = variable_pb2.VariableDef()
        var_def.variable_name = ops.strip_name_scope(self.handle.name, export_scope)
        if self._initial_value is not None:
            var_def.initial_value_name = ops.strip_name_scope(self._initial_value.name, export_scope)
        var_def.initializer_name = ops.strip_name_scope(self.initializer.name, export_scope)
        if self._cached_value is not None:
            var_def.snapshot_name = ops.strip_name_scope(self._cached_value.name, export_scope)
        else:
            var_def.snapshot_name = ops.strip_name_scope(self._graph_element.name, export_scope)
        var_def.is_resource = True
        var_def.trainable = self.trainable
        var_def.synchronization = self.synchronization.value
        var_def.aggregation = self.aggregation.value
        if self._save_slice_info:
            var_def.save_slice_info_def.MergeFrom(self._save_slice_info.to_proto(export_scope=export_scope))
        return var_def
    else:
        return None

Converts a `ResourceVariable` to a `VariableDef` protocol buffer.

Args:
export_scope: Optional `string`. Name scope to remove.

Raises:
RuntimeError: If run in EAGER mode.

Returns:
A `VariableDef` protocol buffer, or `None` if the `Variable` is not
in the specified name scope.


def validate_artifact_url(valid_artifact_rules, valid_artifact_task_ids, url):

    def callback(match):
        path_info = match.groupdict()
        if (('taskId' in path_info) and (path_info['taskId'] not in valid_artifact_task_ids)):
            return
        if ('filepath' not in path_info):
            return
        return path_info['filepath']
    filepath = match_url_regex(valid_artifact_rules, url, callback)
    if (filepath is None):
        raise ScriptWorkerTaskException("Can't validate url {}".format(url), exit_code=STATUSES['malformed-payload'])
    return unquote(filepath).lstrip('/')

Ensure a URL fits in given scheme, netloc, and path restrictions.

If we fail any checks, raise a ScriptWorkerTaskException with
``malformed-payload``.

Args:
valid_artifact_rules (tuple): the tests to run, with ``schemas``, ``netlocs``,
and ``path_regexes``.
valid_artifact_task_ids (list): the list of valid task IDs to download from.
url (str): the url of the artifact.

Returns:
str: the ``filepath`` of the path regex.

Raises:
ScriptWorkerTaskException: on failure to validate.


def use_value_spec(self, value_spec: Optional[pg_typing.List], allow_partial: bool=False) -> 'List':
    if value_spec is None:
        self._value_spec = None
        self._accessor_writable = True
        return self
    if not isinstance(value_spec, pg_typing.List):
        raise ValueError(self._error_message(f'Value spec for list must be a `pg.typing.List` object. Encountered: {value_spec!r}'))
    if self._value_spec and self._value_spec != value_spec:
        raise RuntimeError(self._error_message(f'List is already bound with a different value spec: {self._value_spec}. New value spec: {value_spec}.'))
    self._allow_partial = allow_partial
    if flags.is_type_check_enabled():
        value_spec.apply(self, allow_partial=base.accepts_partial(self), child_transform=base.symbolic_transform_fn(self._allow_partial), root_path=self.sym_path)
    else:
        self._value_spec = value_spec
    return self

Applies a ``pg.List`` as the value spec for current list.

Args:
value_spec: A List ValueSpec to apply to this List.
If current List is schema-less (whose immediate members are not
validated against schema), and `value_spec` is not None, the value spec
will be applied to the List.
Or else if current List is already symbolic (whose immediate members
are under the constraint of a List value spec), and `value_spec` is
None, current List will become schema-less. However, the schema
constraints for non-immediate members will remain.
allow_partial: Whether allow partial dict based on the schema. This flag
will override allow_partial flag in __init__ for spec-less List.

Returns:
Self.

Raises:
ValueError: schema validation failed due to value error.
RuntimeError: List is already bound with another value_spec.
TypeError: type errors during validation.
KeyError: key errors during validation.


def delete_association(self, target, api_type=None, api_sub_type=None, unique_id=None):

        api_type = api_type or target.api_type
        api_sub_type = api_sub_type or target.api_sub_type
        unique_id = unique_id or target.unique_id
        if not self.can_update():
            self._tcex.handle_error(910, [self.type])

        if not target.can_update():
            self._tcex.handle_error(910, [target.type])

        return self.tc_requests.delete_association(
            self.api_type,
            self.api_sub_type,
            self.unique_id,
            api_type,
            api_sub_type,
            unique_id,
            owner=self.owner,
        )

Deletes a association from a Indicator/Group/Victim

Args:
target:
api_type:
api_sub_type:
unique_id:

Returns:


def add_comments(self, comments):

        for comment in comments:
            if comment not in self.comments and len(comment) > 0:
                self.comments.append(comment)
            if len(self.comments[0]) == 0:
                self.comments.pop(0)

Add comments to the localization entry

Args:
comments (list of str): The comments to be added to the localization entry.


def get_atoms(structure, **kwargs):
    if (not structure.is_ordered):
        raise ValueError('ASE Atoms only supports ordered structures')
    symbols = [str(site.specie.symbol) for site in structure]
    positions = [site.coords for site in structure]
    cell = structure.lattice.matrix
    return Atoms(symbols=symbols, positions=positions, pbc=True, cell=cell, **kwargs)

Returns ASE Atoms object from pymatgen structure.

Args:
structure: pymatgen.core.structure.Structure
**kwargs: other keyword args to pass into the ASE Atoms constructor

Returns:
ASE Atoms object


def to_file_async(self, destination, format='csv', csv_delimiter=',', csv_header=True):
    self.to_file(destination, format=format, csv_delimiter=csv_delimiter, csv_header=csv_header)

Start saving the results to a local file in CSV format and return a Job for completion.

Args:
destination: path on the local filesystem for the saved results.
format: the format to use for the exported data; currently only 'csv' is supported.
csv_delimiter: for CSV exports, the field delimiter to use. Defaults to ','
csv_header: for CSV exports, whether to include an initial header line. Default true.
Returns:
A Job for the async save operation.
Raises:
An Exception if the operation failed.


def outer_multiply(x, y):
    x_shape = tf.shape(x)
    padded_shape = tf.concat([x_shape, tf.ones(tf.rank(y), dtype=x_shape.dtype)], axis=0)
    return tf.reshape(x, padded_shape) * y

Performs an outer multiplication of two tensors.

Given two `Tensor`s, `S` and `T` of shape `s` and `t` respectively, the outer
product `P` is a `Tensor` of shape `s + t` whose components are given by:

```none
P_{i1,...ik, j1, ... , jm} = S_{i1...ik} T_{j1, ... jm}
```

Args:
x: A `Tensor` of any shape and numeric dtype.
y: A `Tensor` of any shape and the same dtype as `x`.

Returns:
outer_product: A `Tensor` of shape Shape[x] + Shape[y] and the same dtype
as `x`.


def model_code_key_prefix(code_location_key_prefix, model_name, image):
    training_job_name = sagemaker.utils.name_from_image(image)
    return '/'.join(filter(None, [code_location_key_prefix, (model_name or training_job_name)]))

Returns the s3 key prefix for uploading code during model deployment

The location returned is a potential concatenation of 2 parts
1. code_location_key_prefix if it exists
2. model_name or a name derived from the image

Args:
code_location_key_prefix (str): the s3 key prefix from code_location
model_name (str): the name of the model
image (str): the image from which a default name can be extracted

Returns:
str: the key prefix to be used in uploading code


def stop(name, file=sys.stderr):

    if is_enabled():
        elapsed = (time() - __TIMERS[name])
        if elapsed > 60:
            elapsed_str = '{:.1f} m'.format(elapsed / 60)
        elif elapsed > 1:
            elapsed_str = '{:.1f} s'.format(elapsed)
        else:
            elapsed_str = '{:.1f} ms'.format(elapsed * 1000)

        del __TIMERS[name]
        print("[prof]", name, elapsed_str, file=file)
    return is_enabled()

Stop a profiling timer.

Arguments:

name (str): The name of the timer to stop. If no name is given, stop
the global anonymous timer.

Returns:

bool: Whether or not profiling is enabled.

Raises:

KeyError: If the named timer does not exist.


def _fill_in_missing(x):
    default_value = '' if x.dtype == tf.string else 0
    return tf.squeeze(tf.sparse.to_dense(tf.SparseTensor(x.indices, x.values, [x.dense_shape[0], 1]), default_value), axis=1)

Replace missing values in a SparseTensor.

Fills in missing values of `x` with '' or 0, and converts to a dense tensor.

Args:
x: A `SparseTensor` of rank 2.  Its dense shape should have size at most 1
in the second dimension.

Returns:
A rank 1 tensor where missing values of `x` have been filled in.


def unreduce_tensor(tensor, shape, axis, keepdims):

  if not keepdims:
    if axis is None:
      axis = range(len(shape))
    elif isinstance(axis, int):
      axis = axis,
    for ax in sorted(axis):
      tensor = tf.expand_dims(tensor, ax)
  tile_shape = np.array(shape) / np.array(shape_as_list(tensor))
  return tf.tile(tensor, tile_shape)

Reverse summing over a dimension.

See utils.py.

Args:
tensor: The tensor that was reduced.
shape: A list, the original shape of the tensor before reduction.
axis: The axis or axes that were summed.
keepdims: Whether these axes were kept as singleton axes.

Returns:
A tensor with axes broadcast to match the shape of the original tensor.


def output(self, _filename):

        txt = ""
        for c in self.contracts:
            (name, _inheritance, _var, func_summaries, _modif_summaries) = c.get_summary()
            txt += blue("\n+ Contract %s\n"%name)

            public = [(elem[0], (elem[1], elem[2]) ) for elem in func_summaries]

            collect = collections.defaultdict(list)
            for a,b in public:
                collect[a].append(b)
            public = list(collect.items())

            for contract, functions in public:
                txt += blue("  - From {}\n".format(contract))
                functions = sorted(functions)
                for (function, visi) in functions:
                    if visi in ['external', 'public']:
                        txt += green("    - {} ({})\n".format(function, visi))
                for (function, visi) in functions:
                    if visi in ['internal', 'private']:
                        txt += magenta("    - {} ({})\n".format(function, visi))
                for (function, visi) in functions:
                    if visi not in ['external', 'public', 'internal', 'private']:
                        txt += "    - {}  ({})\n".format(function, visi)

        self.info(txt)

_filename is not used
Args:
_filename(string)


def asdict(self):
    return {'stream': self.stream, 'device_timestamp': self.raw_time, 'streamer_local_id': self.reading_id, 'timestamp': self.reading_time, 'extra_data': self.summary_data, 'data': self.raw_data}

Encode the data in this event into a dictionary.

The dictionary returned from this method is a reference to the data
stored in the IOTileEvent, not a copy.  It should be treated as read
only.

Returns:
dict: A dictionary containing the information from this event.


def get_tensor_shard(param, empty_param, device_mesh, rank, dim):
    param_dim = empty_param.dim()
    if dim < 0:
        dim = param_dim + dim
    if dim >= param_dim:
        raise ValueError(f'dim {dim} is out of bounds for tensor of dimension {param_dim}')
    mesh_shape = device_mesh.shape
    world_size = reduce(operator.mul, mesh_shape)
    if rank >= world_size:
        raise ValueError(f'Rank {rank} is out of bounds for mesh size {world_size}')
    shard_size = empty_param.shape[dim]
    start = rank * shard_size
    end = start + shard_size
    slice_indices = [slice(None)] * param_dim
    slice_indices[dim] = slice(start, end)
    return param[tuple(slice_indices)]

Generalized tensor sharding across a multi-dimensional device mesh.

Args:
param (torch.Tensor): The tensor to shard.
empty_param (torch.Tensor): A tensor used for shape reference.
device_mesh (torch.Tensor): Shape [d_0, ..., d_n] representing the mesh.
rank (int): Global rank of the current process/device.
dim (int): Dimension along which to shard the tensor.


def price(self, valuation_date, market, model=None, name=None):
    del model, valuation_date
    name = name or self._name + '_price'
    with tf.name_scope(name):
        reference_curve = market.reference_curve
        df1 = reference_curve.get_discount_factor(self._accrual_start_dates)
        df2 = reference_curve.get_discount_factor(self._accrual_end_dates)
        fwd_rates = (df1 / df2 - 1.0) / self._accrual_daycount
        total_accrual = tf.math.segment_sum(self._daycount_fractions, self._contract_idx)
        if self._averaging_type == rc.AverageType.ARITHMETIC_AVERAGE:
            settlement_rate = tf.math.segment_sum(fwd_rates * self._daycount_fractions, self._contract_idx) / total_accrual
        else:
            settlement_rate = (tf.math.segment_prod(1.0 + fwd_rates * self._daycount_fractions, self._contract_idx) - 1.0) / total_accrual
        return 100.0 * (1.0 - settlement_rate)

Returns the price of the contract on the valuation date.

Args:
valuation_date: A scalar `DateTensor` specifying the date on which
valuation is being desired.
market: An object of type `InterestRateMarket` which contains the
necessary information for pricing the FRA instrument.
model: Reserved for future use.
name: Python string. The name to give this op.
Default value: `None` which maps to `price`.

Returns:
A Rank 1 `Tensor` of real type containing the modeled price of each
futures contract based on the input market data.


def convert_lstm_weights(weights, from_cudnn=True):
    kernels = transform_kernels(weights[0], transpose_input(from_cudnn), n_gates)
    recurrent_kernels = transform_kernels(weights[1], lambda k: k.T, n_gates)
    if from_cudnn:
        biases = np.sum(np.split(weights[2], 2, axis=0), axis=0)
    else:
        biases = np.tile(0.5 * weights[2], 2)
    return [kernels, recurrent_kernels, biases]

Converts the weights between CuDNNLSTM and LSTM.

Args:
weights: Original weights.
from_cudnn: Indicates whether original weights are from CuDNN layer.

Returns:
Updated weights compatible with LSTM.


def _get_backend(p0: _GPath, p1: _GPath) -> backend_lib.Backend:
    if p0._backend in _GCS_BACKENDS:
        return p0._backend
    elif p1._backend in _GCS_BACKENDS:
        return p1._backend
    else:
        return p0._backend

When composing with another backend, GCS win.

To allow `Path('.').replace('gs://')`

Args:
p0: Path to compare
p1: Path to compare

Returns:
GCS backend if one of the 2 path is GCS, else p0 backend.


def set_timing(self, timing: bool, reset: bool = False) -> None:

        self._timing = timing
        if reset:
            self.reset()

Manually set the ``timing`` parameter, and optionally reset the timers.

Args:
timing: should we be timing?
reset: reset the timers?


def _convert_path(path, name):
    table = os.path.splitext(path)[0]
    table = table.replace(os.path.sep, '__')
    if (name is not None):
        table = '___'.join([table, name])
    table = re.sub('[^0-9a-zA-Z_]+', '_', table)
    table = table.lower()
    return table

Convert resource's path and name to storage's table name.

Args:
path (str): resource path
name (str): resource name

Returns:
str: table name


def delete(filething):

    t = OggOpus(filething)
    filething.fileobj.seek(0)
    t.delete(filething)

delete(filething)

Arguments:
filething (filething)
Raises:
mutagen.MutagenError

Remove tags from a file.


def direct_normal_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 `direct_normal_radiation`'.format(value))
        if (value < 0.0):
            raise ValueError('value need to be greater or equal 0.0 for field `direct_normal_radiation`')
    self._direct_normal_radiation = value

Corresponds to IDD Field `direct_normal_radiation`

Args:
value (float): value for IDD Field `direct_normal_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 _updateEncoding(self, index):
    encoding = self._encodingComboBox.itemText(index)
    encoding = encoding.lower()
    self._encodingKey = _calculateEncodingKey(encoding)
    self._previewFile()

Changes the value of the encoding combo box to the value of given index.

This method is also a `SLOT`.
After the encoding is changed, the file will be reloaded and previewed.

Args:
index (int): An valid index of the combo box.


def vdot(x1, x2):
    if any_symbolic_tensors((x1, x2)):
        return Vdot().symbolic_call(x1, x2)
    return backend.numpy.vdot(x1, x2)

Return the dot product of two vectors.

If the first argument is complex, the complex conjugate of the first
argument is used for the calculation of the dot product.

Multidimensional tensors are flattened before the dot product is taken.

Args:
x1: First input tensor. If complex, its complex conjugate is taken
before calculation of the dot product.
x2: Second input tensor.

Returns:
Output tensor.


def flux_minimization(model, fixed, solver, weights={}):

    fba = FluxBalanceProblem(model, solver)

    for reaction_id, value in iteritems(fixed):
        flux = fba.get_flux_var(reaction_id)
        fba.prob.add_linear_constraints(flux >= value)

    fba.minimize_l1()

    return ((reaction_id, fba.get_flux(reaction_id))
            for reaction_id in model.reactions)

Minimize flux of all reactions while keeping certain fluxes fixed.

The fixed reactions are given in a dictionary as reaction id
to value mapping. The weighted L1-norm of the fluxes is minimized.

Args:
model: MetabolicModel to solve.
fixed: dict of additional lower bounds on reaction fluxes.
solver: LP solver instance to use.
weights: dict of weights on the L1-norm terms.

Returns:
An iterator of reaction ID and reaction flux pairs.


def start_proc_mask_signal(proc):

    if not isinstance(proc, list):
        proc = [proc]

    with mask_sigint():
        for p in proc:
            if isinstance(p, mp.Process):
                if sys.version_info < (3, 4) or mp.get_start_method() == 'fork':
                    log_once(
"Starting a process with 'fork' method is not safe and may consume unnecessary extra memory."
" Use 'forkserver' method (available after Py3.4) instead if you run into any issues. "
"See https:
'warn')
            p.start()

Start process(es) with SIGINT ignored.

Args:
proc: (mp.Process or list)

Note:
The signal mask is only applied when called from main thread.


def _process_new(self, feed_item):
    creative = {'advertiserId': feed_item.get(FieldMap.ADVERTISER_ID, None), 'name': feed_item.get(FieldMap.CREATIVE_NAME, None), 'active': True}
    self._associate_third_party_urls(feed_item, creative)
    self._associate_click_tags(feed_item, creative)
    if feed_item.get(FieldMap.CREATIVE_TYPE, None) == 'VIDEO':
        creative['type'] = 'INSTREAM_VIDEO'
        for association in feed_item.get('associations', []):
            identifier = self.creative_asset_dao.get_identifier(association, self._creative_asset_feed)
            creative['creativeAssets'] = [{'assetIdentifier': identifier, 'role': 'PARENT_VIDEO'}]
        del creative['active']
    elif feed_item.get(FieldMap.CREATIVE_TYPE, None) == 'DISPLAY':
        creative['type'] = 'DISPLAY'
        if feed_item.get(FieldMap.CREATIVE_WIDTH, None) and feed_item.get(FieldMap.CREATIVE_HEIGHT, None):
            creative['size'] = {'kind': 'dfareporting
        for association in feed_item.get('associations', []):
            identifier = self.creative_asset_dao.get_identifier(association, self._creative_asset_feed)
            creative['creativeAssets'] = [{'assetIdentifier': identifier, 'role': 'PRIMARY'}]
            if feed_item.get(FieldMap.CREATIVE_BACKUP_ASSET_ID, None) and feed_item.get(FieldMap.CREATIVE_BACKUP_ASSET_ID, None) != '':
                backup_identifier = self.creative_asset_dao.get_backup_identifier(association, self._creative_asset_feed)
                creative['backupImageReportingLabel'] = feed_item.get(FieldMap.CREATIVE_BACKUP_NAME, None)
                backup_features = feed_item.get(FieldMap.BACKUP_IMAGE_FEATURES, None)
                if backup_features != None or backup_features != '':
                    features = backup_features.split(',')
                    creative['backupImageFeatures'] = features
                creative['backupImageTargetWindow'] = {'targetWindowOption': feed_item.get(FieldMap.BACKUP_IMAGE_TARGET_WINDOW_OPTION, None), 'customHtml': feed_item.get(FieldMap.BACKUP_IMAGE_CUSTOM_HTML, None)}
                lp = self.landing_page_dao.get(feed_item, column_name=FieldMap.BACKUP_IMAGE_CLICK_THROUGH_LANDING_PAGE_ID)
                creative['backupImageClickThroughUrl'] = {'landingPageId': feed_item.get(FieldMap.BACKUP_IMAGE_CLICK_THROUGH_LANDING_PAGE_ID) if not lp else lp['id']}
                creative['creativeAssets'].append({'assetIdentifier': backup_identifier, 'role': 'BACKUP_IMAGE'})
        del creative['active']
    else:
        raise Exception('Only video and display are supported at the moment!')
    return creative

Creates a new creative DCM object from a feed item representing an creative from the Bulkdozer feed.

This function simply creates the object to be inserted later by the BaseDAO
object.

Args:
feed_item: Feed item representing the creative from the Bulkdozer feed.

Returns:
A creative object ready to be inserted in DCM through the API.


def EquilibrateEigenVectorPhases(x, y):
    phases = np.sum(np.conj(x) * y, -2, keepdims=True)
    phases /= np.abs(phases)
    return phases * x

Equilibrate the phase of the Eigenvectors in the columns of `x` and `y`.

Eigenvectors are only unique up to an arbitrary phase. This function rotates x
such that it matches y. Precondition: The columns of x and y differ by a
multiplicative complex phase factor only.

Args:
x: `np.ndarray` with Eigenvectors
y: `np.ndarray` with Eigenvectors

Returns:
`np.ndarray` containing an equilibrated version of x.


def distance(self, physical_qubit1, physical_qubit2):
    if (physical_qubit1 not in self.physical_qubits):
        raise CouplingError(('%s not in coupling graph' % (physical_qubit1,)))
    if (physical_qubit2 not in self.physical_qubits):
        raise CouplingError(('%s not in coupling graph' % (physical_qubit2,)))
    if (self._dist_matrix is None):
        self._compute_distance_matrix()
    return self._dist_matrix[(physical_qubit1, physical_qubit2)]

Returns the undirected distance between physical_qubit1 and physical_qubit2.

Args:
physical_qubit1 (int): A physical qubit
physical_qubit2 (int): Another physical qubit

Returns:
int: The undirected distance

Raises:
CouplingError: if the qubits do not exist in the CouplingMap


def _get_fullname(obj):
    if (not hasattr(obj, '__name__')):
        obj = obj.__class__
    if (obj.__module__ in ('builtins', '__builtin__')):
        return obj.__name__
    return '{}.{}'.format(obj.__module__, obj.__name__)

Get the full name of an object including the module.

Args:
obj: An object.

Returns:
The full class name of the object.


def trace(self, data, callback=None):
    conn_id = self._find_connection(self.conn_string)
    if (conn_id is not None):
        self.adapter.notify_event_nowait(self.conn_string, 'trace', data)
    if (callback is not None):
        callback((conn_id is not None))

Queue data for tracing

Args:
data (bytearray, string): Unstructured data to trace to any
connected client.
callback (callable): An optional callback that will be called with
a bool value of True when this data actually gets traced.
If the client disconnects and the data is dropped instead,
callback will be called with False.


def extract_output(self, accumulator, *args, **kwargs):
    raise NotImplementedError(str(self))

Return result of converting accumulator into the output value.

Args:
accumulator: the final accumulator value computed by this CombineFn
for the entire input key or PCollection. Can be modified for
efficiency.
*args: Additional arguments and side inputs.
**kwargs: Additional arguments and side inputs.


def dims(x):

  if isinstance(x, tf.TensorShape):
    return x.dims
  r = tf.TensorShape(x).dims
  return None if r is None else list(map(tf.compat.dimension_value, r))

Returns a list of dimension sizes, or `None` if `rank` is unknown.

For more details, see `help(tf.TensorShape.dims)`.

Args:
x: object representing a shape; convertible to `tf.TensorShape`.

Returns:
shape_as_list: list of sizes or `None` values representing each
dimensions size if known. A size is `tf.Dimension` if input is a
`tf.TensorShape` and an `int` otherwise.


def set_tuple_types(self, tuple_types):
    if len(tuple_types) != self.number_of_tuple_elements:
        raise ValueError(f'tuple_types is {str(tuple_types)}, but must be a list of length {self.number_of_tuple_elements}')
    if self._frozen:
        for frozen, updated in zip(self._tuple_types, tuple_types):
            if frozen != updated:
                raise ValueError(f'Trying to update InfeedQueue with frozen configuration with an incompatible type. Frozen types are {str(self._tuple_types)}, updated types are {str(tuple_types)}')
    else:
        try:
            self._tuple_types = [dtypes.as_dtype(t) for t in tuple_types]
        except TypeError as e:
            raise TypeError(f'tuple_types is {str(tuple_types)}, but must be a list of elements each convertible to dtype: got error {str(e)}') from e

Sets the type of each element of the queue.

tuple_types must be a list of length
self.number_of_tuple_elements, and each element must be
convertible to a dtype.

Args:
tuple_types: the types of each queue element.

Raises:
ValueError: if tuple_types is not of length
self.number_of_tuple_elements.
TypeError: if an element of tuple_types cannot be converted to a
dtype.


def translate_opcodes(code_obj, target):

    target = get_py_internals(target)
    src_ops = code_obj.disassemble()

    dst_opmap = target['opmap']
    dst_ops = []

    op_iter = enumerate(src_ops)
    for i, op in op_iter:
        if isinstance(op, pwnypack.bytecode.Label):
            dst_ops.append(op)
            continue

        if op.name not in dst_opmap:
            if op.name == 'POP_JUMP_IF_FALSE' and 'JUMP_IF_TRUE' in dst_opmap:
                lbl = pwnypack.bytecode.Label()
                dst_ops.extend([
                    pwnypack.bytecode.Op('JUMP_IF_TRUE', lbl),
                    pwnypack.bytecode.Op('POP_TOP', None),
                    pwnypack.bytecode.Op('JUMP_ABSOLUTE', op.arg),
                    lbl,
                    pwnypack.bytecode.Op('POP_TOP', None),
                ])
            elif op.name == 'POP_JUMP_IF_TRUE' and 'JUMP_IF_FALSE' in dst_opmap:
                lbl = pwnypack.bytecode.Label()
                dst_ops.extend([
                    pwnypack.bytecode.Op('JUMP_IF_FALSE', lbl),
                    pwnypack.bytecode.Op('POP_TOP', None),
                    pwnypack.bytecode.Op('JUMP_ABSOLUTE', op.arg),
                    lbl,
                    pwnypack.bytecode.Op('POP_TOP', None),
                ])
            elif op.name == 'JUMP_IF_FALSE' and 'JUMP_IF_FALSE_OR_POP' in dst_opmap and \
                    src_ops[i + 1].name == 'POP_TOP':
                next(op_iter)
                dst_ops.append(pwnypack.bytecode.Op('JUMP_IF_FALSE_OR_POP', op.arg))
            elif op.name == 'JUMP_IF_TRUE' and 'JUMP_IF_TRUE_OR_POP' in dst_opmap and \
                    src_ops[i + 1].name == 'POP_TOP':
                next(op_iter)
                dst_ops.append(pwnypack.bytecode.Op('JUMP_IF_TRUE_OR_POP', op.arg))
            else:
                raise SyntaxError('Opcode %s not supported on target.' % op.name)
        else:
            dst_ops.append(op)

    code_obj.assemble(dst_ops, target)

Very crude inter-python version opcode translator. Raises SyntaxError when
the opcode doesn't exist in the destination opmap. Used to transcribe
python code objects between python versions.

Arguments:
code_obj(pwnypack.bytecode.CodeObject): The code object representation
to translate.
target(dict): The py_internals structure for the target
python version.


def verify(self, flag_values):

    param = self._get_input_to_checker_function(flag_values)
    if not self.checker(param):
      raise _exceptions.ValidationError(self.message)

Verifies that constraint is satisfied.

flags library calls this method to verify Validator's constraint.

Args:
flag_values: flags.FlagValues, the FlagValues instance to get flags from.
Raises:
Error: Raised if constraint is not satisfied.


def is_original_format(tweet):

    if "created_at" in tweet:
        original_format = True
    elif "postedTime" in tweet:
        original_format = False
    else:
        raise NotATweetError("This dict has neither 'created_at' or 'postedTime' as keys")
    return original_format

Simple checker to flag the format of a tweet.

Args:
tweet (Tweet): tweet in qustion

Returns:
Bool

Example:
>>> import tweet_parser.tweet_checking as tc
>>> tweet = {"created_at": 124125125125,
...          "text": "just setting up my twttr",
...          "nested_field": {"nested_1": "field", "nested_2": "field2"}}
>>> tc.is_original_format(tweet)
True


def validates(version):

    def _validates(cls):
        validators[version] = cls
        meta_schema_id = cls.ID_OF(cls.META_SCHEMA)
        if meta_schema_id:
            meta_schemas[meta_schema_id] = cls
        return cls
    return _validates

Register the decorated validator for a ``version`` of the specification.

Registered validators and their meta schemas will be considered when
parsing ``$schema`` properties' URIs.

Arguments:

version (str):

An identifier to use as the version's name

Returns:

callable: a class decorator to decorate the validator with the version


def to_pandas(self, is_transposed=False):

        if is_transposed:
            return self.transpose().to_pandas(False).T
        else:
            retrieved_objects = [
                [obj.to_pandas() for obj in part] for part in self.partitions
            ]
            if all(
                isinstance(part, pandas.Series)
                for row in retrieved_objects
                for part in row
            ):
                axis = 0
            elif all(
                isinstance(part, pandas.DataFrame)
                for row in retrieved_objects
                for part in row
            ):
                axis = 1
            else:
                ErrorMessage.catch_bugs_and_request_email(True)
            df_rows = [
                pandas.concat([part for part in row], axis=axis)
                for row in retrieved_objects
                if not all(part.empty for part in row)
            ]
            if len(df_rows) == 0:
                return pandas.DataFrame()
            else:
                return pandas.concat(df_rows)

Convert this object into a Pandas DataFrame from the partitions.

Args:
is_transposed: A flag for telling this object that the external
representation is transposed, but not the internal.

Returns:
A Pandas DataFrame


def load_flax_sharded_weights(cls, shard_files):
    state_sharded_dict = {}
    for shard_file in shard_files:
        try:
            with open(shard_file, 'rb') as state_f:
                state = from_bytes(cls, state_f.read())
        except (UnpicklingError, msgpack.exceptions.ExtraData) as e:
            with open(shard_file) as f:
                if f.read().startswith('version'):
                    raise OSError('You seem to have cloned a repository without having git-lfs installed. Please install git-lfs and run `git lfs install` followed by `git lfs pull` in the folder you cloned.')
                else:
                    raise ValueError from e
        except (UnicodeDecodeError, ValueError):
            raise OSError(f'Unable to convert {shard_file} to Flax deserializable object. ')
        state = flatten_dict(state, sep='/')
        state_sharded_dict.update(state)
        del state
        gc.collect()
    return unflatten_dict(state_sharded_dict, sep='/')

This is the same as [`flax.serialization.from_bytes`]
(https:lax.readthedocs.io/en/latest/_modules/flax/serialization.html#from_bytes) but for a sharded checkpoint.

This load is performed efficiently: each checkpoint shard is loaded one by one in RAM and deleted after being
loaded in the model.

Args:
shard_files (`List[str]`:
The list of shard files to load.

Returns:
`Dict`: A nested dictionary of the model parameters, in the expected format for flax models : `{'model':
{'params': {'...'}}}`.


def get_structure_seqrecords(model):

    structure_seq_records = []

    for chain in model:
        tracker = 0
        chain_seq = ''
        chain_resnums = []

        for res in chain.get_residues():

            res_id = res.id
            res_num = res_id[1]
            res_icode = res_id[2]

            if Polypeptide.is_aa(res, standard=True):
                end_tracker = res_num
                res_aa_one = Polypeptide.three_to_one(res.get_resname())

                if end_tracker != (tracker + 1):
                    if res_icode != ' ':
                        chain_seq += res_aa_one
                        chain_resnums.append(res_num)
                        tracker = end_tracker + 1
                        continue
                    else:
                        multiplier = (end_tracker - tracker - 1)
                        chain_seq += 'X' * multiplier

                        chain_resnums.extend([float("Inf")] * multiplier)

                chain_seq += res_aa_one
                chain_resnums.append(res_num)
                tracker = end_tracker

            else:
                continue

        chain_seq_record = SeqRecord(Seq(chain_seq, IUPAC.protein), id=chain.get_id())
        chain_seq_record.letter_annotations['structure_resnums'] = chain_resnums
        structure_seq_records.append(chain_seq_record)

    return structure_seq_records

Get a dictionary of a PDB file's sequences.

Special cases include:
- Insertion codes. In the case of residue numbers like "15A", "15B", both residues are written out. Example: 9LPR
- HETATMs. Currently written as an "X", or unknown amino acid.

Args:
model: Biopython Model object of a Structure

Returns:
list: List of SeqRecords


def lin_moma(self, wt_fluxes):
    reactions = set(self._adjustment_reactions())
    z_diff = self._z_diff
    v = self._v
    with self.constraints() as constr:
        for (f_reaction, f_value) in iteritems(wt_fluxes):
            if (f_reaction in reactions):
                constr.add((z_diff[f_reaction] >= (f_value - v[f_reaction])), ((f_value - v[f_reaction]) >= (- z_diff[f_reaction])))
        self._prob.set_objective(z_diff.sum(reactions))
        self._solve(lp.ObjectiveSense.Minimize)

Minimize the redistribution of fluxes using a linear objective.

The change in flux distribution is mimimized by minimizing the sum
of the absolute values of the differences of wild type FBA solution
and the knockout strain flux solution.

This formulation bases the solution on the wild type fluxes that
are specified by the user. If these wild type fluxes were calculated
using FBA, then an arbitrary flux vector that optimizes the objective
function is used. See [Segre`_02] for more information.

Args:
wt_fluxes: Dictionary of all the wild type fluxes. Use
:meth:`.get_fba_flux(objective)` to return a dictionary of
fluxes found by FBA.


def _get_bond_data(line):

        line = line.split()
        length = float(line[2])

        sites = line[0].replace("/", "-").split("-")
        site_indices = tuple(int(ind) - 1 for ind in sites[1:4:2])
        species = tuple(re.split(r"\d+", spec)[0] for spec in sites[0:3:2])
        label = "%s%d-%s%d" % (species[0], site_indices[0] + 1,
                               species[1], site_indices[1] + 1)
        return label, length, site_indices

Subroutine to extract bond label, site indices, and length from
a COPL header line. The site indices are zero-based, so they
can be easily used with a Structure object.

Example header line: Fe-1/Fe-1-tr(-1,-1,-1) :  2.482 Ang.

Args:
line: line in the COHPCAR header describing the bond.

Returns:
The bond label, the bond length and a tuple of the site
indices.


class TableTransformerEncoder(TableTransformerPreTrainedModel):

    def __init__(self, config: TableTransformerConfig):
        super().__init__(config)
        self.dropout = config.dropout
        self.layerdrop = config.encoder_layerdrop
        self.layers = nn.ModuleList([TableTransformerEncoderLayer(config) for _ in range(config.encoder_layers)])
        self.layernorm = nn.LayerNorm(config.d_model)
        self.post_init()

    def forward(self, inputs_embeds=None, attention_mask=None, object_queries=None, output_attentions=None, output_hidden_states=None, return_dict=None):

        output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
        output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        hidden_states = inputs_embeds
        hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
        if attention_mask is not None:
            attention_mask = _prepare_4d_attention_mask(attention_mask, inputs_embeds.dtype)
        encoder_states = () if output_hidden_states else None
        all_attentions = () if output_attentions else None
        for encoder_layer in self.layers:
            if output_hidden_states:
                encoder_states = encoder_states + (hidden_states,)
            to_drop = False
            if self.training:
                dropout_probability = torch.rand([])
                if dropout_probability < self.layerdrop:
                    to_drop = True
            if to_drop:
                layer_outputs = (None, None)
            else:
                layer_outputs = encoder_layer(hidden_states, attention_mask, object_queries=object_queries, output_attentions=output_attentions)
                hidden_states = layer_outputs[0]
            if output_attentions:
                all_attentions = all_attentions + (layer_outputs[1],)
        if output_hidden_states:
            encoder_states = encoder_states + (hidden_states,)
        hidden_states = self.layernorm(hidden_states)
        if not return_dict:
            return tuple((v for v in [hidden_states, encoder_states, all_attentions] if v is not None))
        return BaseModelOutput(last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions)

Transformer encoder consisting of *config.encoder_layers* self attention layers. Each layer is a
[`TableTransformerEncoderLayer`].

The encoder updates the flattened feature map through multiple self-attention layers.

Small tweak for Table Transformer:

- object_queries are added to the forward pass.

Args:
config: TableTransformerConfig


def create_sp(operations, operation):

    operations.execute(
        "CREATE FUNCTION %s %s" % (
            operation.target.name, operation.target.sqltext
        )
    )

Implements ``CREATE FUNCTION``.

Args:
operations: instance of ``alembic.operations.base.Operations``
operation: instance of :class:`.ReversibleOp`

Returns:
``None``


def _GenerateStatsTable(self, feed_merger):
    rows = []
    rows.append('
Merged	Copied from old feed	Copied from new feed
')
    for merger in feed_merger.GetMergerList():
        stats = merger.GetMergeStats()
        if (stats is None):
            continue
        (merged, not_merged_a, not_merged_b) = stats
        rows.append(('
%s	%d	%d	%d
' % (merger.DATASET_NAME, merged, not_merged_a, not_merged_b)))
    return ('
%s
' % '\n'.join(rows))

Generate an HTML table of merge statistics.

Args:
feed_merger: The FeedMerger instance.

Returns:
The generated HTML as a string.


def validate(self):
    self._attribute_errors = dict()
    for (local_name, attribute) in self._attributes.items():
        value = getattr(self, local_name, None)
        if (attribute.is_required and ((value is None) or (value == ''))):
            self._attribute_errors[local_name] = {'title': 'Invalid input', 'description': 'This value is mandatory.', 'remote_name': attribute.remote_name}
            continue
        if (value is None):
            continue
        if (not self._validate_type(local_name, attribute.remote_name, value, attribute.attribute_type)):
            continue
        if ((attribute.min_length is not None) and (len(value) < attribute.min_length)):
            self._attribute_errors[local_name] = {'title': 'Invalid length', 'description': ('Attribute %s minimum length should be %s but is %s' % (attribute.remote_name, attribute.min_length, len(value))), 'remote_name': attribute.remote_name}
            continue
        if ((attribute.max_length is not None) and (len(value) > attribute.max_length)):
            self._attribute_errors[local_name] = {'title': 'Invalid length', 'description': ('Attribute %s maximum length should be %s but is %s' % (attribute.remote_name, attribute.max_length, len(value))), 'remote_name': attribute.remote_name}
            continue
        if (attribute.attribute_type == list):
            valid = True
            for item in value:
                if (valid is True):
                    valid = self._validate_value(local_name, attribute, item)
        else:
            self._validate_value(local_name, attribute, value)
    return self.is_valid()

Validate the current object attributes.

Check all attributes and store errors

Returns:
Returns True if all attibutes of the object
respect contraints. Returns False otherwise and
store error in errors dict.


def relpath(self, path):

        for root in self.roots:

            if isinstance(root, Pattern):
                match = root.match(path)
                if not match:
                    continue
                root = match.group(0)

            try:
                relative = path.split(root, 1)[1]

                return relative.lstrip('/')
            except IndexError:
                continue

        return path

Get path relative to storage.

args:
path (str): Absolute path or URL.

Returns:
str: relative path.


def random_new(algo: int = RNG_CMWC) -> tcod.random.Random:

    return tcod.random.Random(algo)

Return a new Random instance.  Using ``algo``.

Args:
algo (int): The random number algorithm to use.

Returns:
Random: A new Random instance using the given algorithm.


def _WriteFileEntry(self, file_entry, data_stream_name, destination_file):

    source_file_object = file_entry.GetFileObject(
        data_stream_name=data_stream_name)
    if not source_file_object:
      return

    try:
      with open(destination_file, 'wb') as destination_file_object:
        source_file_object.seek(0, os.SEEK_SET)

        data = source_file_object.read(self._COPY_BUFFER_SIZE)
        while data:
          destination_file_object.write(data)
          data = source_file_object.read(self._COPY_BUFFER_SIZE)

    finally:
      source_file_object.close()

Writes the contents of the source file entry to a destination file.

Note that this function will overwrite an existing file.

Args:
file_entry (dfvfs.FileEntry): file entry whose content is to be written.
data_stream_name (str): name of the data stream whose content is to be
written.
destination_file (str): path of the destination file.


def connect(self, component):
    if (not isinstance(component, ThreadPool)):
        raise TypeError('"component" must be a ThreadPool object')
    component.in_queue = self.out_queue
    return component

Connect two ThreadPools.

The ``in_queue`` of the second pool will be set as the ``out_queue`` of
the current pool, thus all the output will be input to the second pool.

Args:
component (ThreadPool): the ThreadPool to be connected.
Returns:
ThreadPool: the modified second ThreadPool.


def parse(self, words):

        def exact(words):

            try:
                return float(words)
            except:
                return None

        guess = exact(words)
        if guess is not None:
            return guess

        split = words.split(' ')

        if split[-1] in self.__fractions__:
            split[-1] = self.__fractions__[split[-1]]
        elif split[-1] in self.__ordinals__:
            split[-1] = self.__ordinals__[split[-1]]

        parsed_ordinals = ' '.join(split)

        return self.parseFloat(parsed_ordinals)

A general method for parsing word-representations of numbers.
Supports floats and integers.

Args:
words (str): Description of an arbitrary number.

Returns:
A double representation of the words.


def update(self, data):

        updated = False

        if 'state' in data:
            updated = self.set_property('state', data['state'])

        if 'end' in data:
            updated |= self.set_property('end', data['end'])

        if 'last_alert' in data:
            updated |= self.set_property('last_alert', data['last_alert'])

        return updated

Updates the object information based on live data, if there were any changes made. Any changes will be
automatically applied to the object, but will not be automatically persisted. You must manually call
`db.session.add(instance)` on the object.

Args:
data (:obj:): AWS API Resource object fetched from AWS API

Returns:
`bool`


def system_repertoire_distance(r1, r2):

    if config.MEASURE in measures.asymmetric():
        raise ValueError(
            '{} is asymmetric and cannot be used as a system-level '
            'irreducibility measure.'.format(config.MEASURE))

    return measures[config.MEASURE](r1, r2)

Compute the distance between two repertoires of a system.

Args:
r1 (np.ndarray): The first repertoire.
r2 (np.ndarray): The second repertoire.

Returns:
float: The distance between ``r1`` and ``r2``.


def repr(self, changed_widgets=None):
    if (changed_widgets is None):
        changed_widgets = {}
    return super(Widget, self).repr(changed_widgets)

Represents the widget as HTML format, packs all the attributes, children and so on.

Args:
client (App): Client instance.
changed_widgets (dict): A dictionary containing a collection of widgets that have to be updated.
The Widget that have to be updated is the key, and the value is its textual repr.


def _OpenParentFile(self, file_system, path_spec, vhdi_file):

    location = getattr(path_spec, 'location', None)
    if not location:
      raise errors.PathSpecError(
          'Unsupported path specification without location.')

    location_path_segments = file_system.SplitPath(location)

    parent_filename = vhdi_file.parent_filename
    _, _, parent_filename = parent_filename.rpartition('\\')

    location_path_segments.pop()
    location_path_segments.append(parent_filename)
    parent_file_location = file_system.JoinPath(location_path_segments)

    kwargs = path_spec_factory.Factory.GetProperties(path_spec)

    kwargs['location'] = parent_file_location
    if path_spec.parent is not None:
      kwargs['parent'] = path_spec.parent

    parent_file_path_spec = path_spec_factory.Factory.NewPathSpec(
        path_spec.type_indicator, **kwargs)

    if not file_system.FileEntryExistsByPathSpec(parent_file_path_spec):
      return

    file_object = resolver.Resolver.OpenFileObject(
        parent_file_path_spec, resolver_context=self._resolver_context)

    vhdi_parent_file = pyvhdi.file()
    vhdi_parent_file.open_file_object(file_object)

    if vhdi_parent_file.parent_identifier:
      self._OpenParentFile(
          file_system, parent_file_path_spec, vhdi_parent_file)

    vhdi_file.set_parent(vhdi_parent_file)

    self._parent_vhdi_files.append(vhdi_parent_file)
    self._sub_file_objects.append(file_object)

Opens the parent file.

Args:
file_system (FileSystem): file system of the VHDI file.
path_spec (PathSpec): path specification of the VHDI file.
vhdi_file (pyvhdi.file): VHDI file.

Raises:
PathSpecError: if the path specification is incorrect.