How to use the typing.Tuple function in typing

To help you get started, we’ve selected a few typing examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github justindujardin / mathy / mathy / a3c / replay_buffer.py View on Github external
def to_features(
        self, feature_states: Optional[List[MathyObservation]] = None
    ) -> MathyWindowObservation:
        if feature_states is None:
            feature_states = self.states
        context_feature_keys: List[str] = [
            FEATURE_LAST_RULE,
            FEATURE_NODE_COUNT,
            FEATURE_MOVE_COUNTER,
            FEATURE_MOVES_REMAINING,
        ]
        text_feature_keys: List[str] = [FEATURE_PROBLEM_TYPE]
        sequence_feature_keys: List[Tuple[str, bool]] = [
            (FEATURE_FWD_VECTORS, False),
            (FEATURE_BWD_VECTORS, True),
        ]
        out = {
            FEATURE_LAST_RULE: [],
            FEATURE_NODE_COUNT: [],
            FEATURE_FWD_VECTORS: [],
            FEATURE_BWD_VECTORS: [],
            FEATURE_LAST_FWD_VECTORS: [],
            FEATURE_LAST_BWD_VECTORS: [],
            FEATURE_MOVE_MASK: [],
            FEATURE_MOVE_COUNTER: [],
            FEATURE_MOVES_REMAINING: [],
            FEATURE_PROBLEM_TYPE: [],
        }
        lengths = [len(s[FEATURE_BWD_VECTORS][0]) for s in feature_states]
github hyperledger / indy-plenum / plenum / server / router.py View on Github external
from collections import deque, OrderedDict
from inspect import isawaitable
from typing import Callable, Any, NamedTuple, Union, Iterable
from typing import Tuple

Route = Tuple[Union[type, NamedTuple], Callable]


class Router:
    """
    A simple router.

    Routes messages to functions based on their type.

    Constructor takes an iterable of tuples of
    (1) a class type and
    (2) a function that handles the message
    """

    def __init__(self, *routes: Route):
        """
        Create a new router with a list of routes
github lablup / backend.ai-manager / src / ai / backend / manager / models / scaling_group.py View on Github external
async def schedule(self, available_agent_infos: List[ResourceSlot],
                       pending_jobs: List[SessionCreationJob],
                       conn=None,
                       dry_run=False) -> List[Tuple[str, SessionCreationJob]]:
        '''
        This callback method is invoked when there are new session creation requests,
        terminated sessions, and increases of the scaling group resources.

        Its job is to determine which pending session creation requests can be
        scheduled on the given scaling group now.
        '''
        raise NotImplementedError
github groboclown / petronia / src / petronia / defimpl / bus / local / basic_event_bus.py View on Github external
log, TRACE, DEBUG, VERBOSE
)
from ....base.bus import (
    QueuePriority,
    QUEUE_EVENT_TYPES,
)
from ....base.validation import (
    assert_formatted, assert_all, assert_has_signature
)
from ....base.util import RWLock
# Special handling...
from ....base.events.system_events import EVENT_ID_SYSTEM_HALTED


BasicEventCallbackArguments = Tuple[EventId, ParticipantId, object]
BasicEventCallback = Callable[[EventId, ParticipantId, object], None]

# The QueueFunction must take very special care with events of type
# EVENT_ID_REGISTER_EVENT - these must be invoked in the current thread,
# otherwise things can go very wrong.
QueueFunction = Callable[
    [QueuePriority, Sequence[BasicEventCallback], BasicEventCallbackArguments],
    None
]


class BasicEventBus:
    """
    Simple event bus implementation, with support for wildcard listeners.

    The bus does not use weak references for the attached listeners.  It is up
github TriOptima / tri.form / lib / tri_form / __init__.py View on Github external
def group_actions(actions: Dict[str, Action]):
    grouped_links = {}
    if actions is not None:
        grouped_links = groupby((action for action in actions.values() if action.group is not None), key=lambda l: l.group)
        grouped_links: Dict[str, Tuple[str, str, List[Action]]] = [(g, slugify(g), list(lg)) for g, lg in grouped_links]  # list(lg) because django templates touches the generator and then I can't iterate it

        for _, _, group_links in grouped_links:
            for link in group_links:
                link.attrs.role = 'menuitem'

        actions = [action for action in actions.values() if action.group is None]

    return actions, grouped_links
github dcavar / Py-JSON-NLP / pyjsonnlp / conversion.py View on Github external
# create the new paragraph
        document['paragraphs'][par_num] = {
            'id': par_num,
            'tokens': []
        }
        par_num += 1

    def wrap_up_doc():
        if all(map(lambda ds: 'text' in ds, document['sentences'].values())):
            document['text'] = ' '.join(map(lambda ds: ds['text'], document['sentences'].values()))
        #j['documents'][document['id']] = document
        j['documents'].append(document)

    # init
    j: OrderedDict = get_base()
    token_lookup: Dict[Tuple[int, str], int] = {}
    token_id = 1
    document = None
    parsed = conllu.parse(c)

    # start parsing sentences
    for sent_num, sent in enumerate(parsed):
        # documents
        if 'newdoc id' in sent.metadata or 'newdoc' in sent.metadata or document is None:
            if document is not None:
                wrap_up_doc()
            document = get_base_document(doc_num)
            document['conllId'] = sent.metadata.get('newdoc id', '')
            doc_num += 1

        # paragraphs
        if 'newpar id' in sent.metadata or 'newpar' in sent.metadata:
github torchvideo / torchvideo / src / torchvideo / transforms / transforms.py View on Github external
def _canonicalize_size(size: Union[int, Tuple[int, int]]) -> ImageShape:
    """

    Args:
        size:

    Returns:

    """
    if isinstance(size, numbers.Number):
        return ImageShape(int(size), int(size))
    else:
        size = cast(Tuple[int, int], size)
        return ImageShape(size[0], size[1])
github marioortizmanero / spotify-music-videos / vidify / player / __init__.py View on Github external
from dataclasses import dataclass

from vidify import is_installed, BaseModuleData
from vidify.gui import Res
from vidify.config import Config
from vidify.player.generic import PlayerBase


@dataclass(frozen=True)
class PlayerData(BaseModuleData):
    """
    Information structure about the different Players supported, with a
    description for the user and how to initialize it.
    """

    flags: Tuple[str]


PLAYERS = (
    PlayerData(
        id='VLC',
        short_name='VLC',
        description='Play the music videos locally with the VLC player.',
        icon=Res.vlc_icon,
        compatible=True,
        installed=is_installed('python-vlc'),
        module='vidify.player.vlc',
        class_name='VLCPlayer',
        flags=('vlc_args',)),

    PlayerData(
        id='MPV',
github Unity-Technologies / ml-agents / ml-agents-envs / mlagents_envs / rpc_utils.py View on Github external
def batched_step_result_from_proto(
    agent_info_list: Collection[
        AgentInfoProto
    ],  # pylint: disable=unsubscriptable-object
    group_spec: AgentGroupSpec,
) -> BatchedStepResult:
    obs_list: List[np.ndarray] = []
    for obs_index, obs_shape in enumerate(group_spec.observation_shapes):
        is_visual = len(obs_shape) == 3
        if is_visual:
            obs_shape = cast(Tuple[int, int, int], obs_shape)
            obs_list += [
                _process_visual_observation(obs_index, obs_shape, agent_info_list)
            ]
        else:
            obs_list += [
                _process_vector_observation(obs_index, obs_shape, agent_info_list)
            ]
    rewards = np.array(
        [agent_info.reward for agent_info in agent_info_list], dtype=np.float32
    )

    d = np.dot(rewards, rewards)
    has_nan = np.isnan(d)
    has_inf = not np.isfinite(d)
    # In we have any NaN or Infs, use np.nan_to_num to replace these with finite values
    if has_nan or has_inf:
github opendatacube / odc-tools / libs / algo / odc / algo / _dask.py View on Github external
chunks: Union[Tuple[int, ...], Tuple[Tuple[int, ...]]],
                        summed: bool = False) -> Tuple[ROI, ROI]:
    """
    Convert ROI in pixels to ROI in blocks (broi) + ROI in pixels (crop) such that

       xx[roi] == stack_blocks(blocks(xx)[broi])[crop]

    Returns
    =======
      broi, crop
    """
    if isinstance(roi, slice):
        chunks = cast(Tuple[int, ...], chunks)
        return _compute_chunk_range(roi, chunks, summed)

    chunks = cast(Tuple[Tuple[int, ...]], chunks)
    assert len(roi) == len(chunks)
    broi = []
    crop = []

    for span, _chunks in zip(roi, chunks):
        bspan, pspan = _compute_chunk_range(span, _chunks)
        broi.append(bspan)
        crop.append(pspan)

    return tuple(broi), tuple(crop)