Python typing.NamedTuple() Examples

The following are 30 code examples of typing.NamedTuple(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module typing , or try the search function .
Example #1
Source File: factory.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def construct_record_manager(
        self,
        record_class: Optional[type],
        sequenced_item_class: Optional[Type[NamedTuple]] = None,
        **kwargs: Any
    ) -> AbstractRecordManager:
        """
        Constructs an record manager.
        """
        assert self.record_manager_class is not None
        return self.record_manager_class(
            sequenced_item_class=sequenced_item_class or self.sequenced_item_class,
            record_class=record_class,
            contiguous_record_ids=self.contiguous_record_ids,
            application_name=self.application_name,
            pipeline_id=self.pipeline_id,
            **kwargs
        ) 
Example #2
Source File: behavior_id_utils.py    From RLs with Apache License 2.0 6 votes vote down vote up
def from_name_behavior_id(name_behavior_id: str) -> "BehaviorIdentifiers":
        """
        Parses a name_behavior_id of the form name?team=0
        into a BehaviorIdentifiers NamedTuple.
        This allows you to access the brain name and team id of an agent
        :param name_behavior_id: String of behavior params in HTTP format.
        :returns: A BehaviorIdentifiers object.
        """

        parsed = urlparse(name_behavior_id)
        name = parsed.path
        ids = parse_qs(parsed.query)
        team_id: int = 0
        if "team" in ids:
            team_id = int(ids["team"][0])
        return BehaviorIdentifiers(
            behavior_id=name_behavior_id, brain_name=name, team_id=team_id
        ) 
Example #3
Source File: factory.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def construct_record_manager(
        self,
        record_class: Optional[type],
        sequenced_item_class: Optional[Type[NamedTuple]] = None,
        **kwargs: Any
    ) -> AbstractRecordManager:
        """
        Constructs SQLAlchemy record manager.

        :return: An SQLAlchemy record manager.
        :rtype: SQLAlchemyRecordManager
        """
        return super(SQLAlchemyInfrastructureFactory, self).construct_record_manager(
            record_class,
            sequenced_item_class=sequenced_item_class,
            session=self.session,
            **kwargs
        ) 
Example #4
Source File: iterators.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(
        self,
        record_manager: AbstractRecordManager,
        sequence_id: UUID,
        gt: Optional[int] = None,
        gte: Optional[int] = None,
        lt: Optional[int] = None,
        lte: Optional[int] = None,
        page_size: Optional[int] = None,
        is_ascending: bool = True,
        *args: Any,
        **kwargs: Any
    ):
        super(GetEntityEventsThread, self).__init__(*args, **kwargs)
        assert isinstance(record_manager, BaseRecordManager), type(record_manager)
        self.record_manager = record_manager
        self.stored_entity_id = sequence_id
        self.gt = gt
        self.gte = gte
        self.lt = lt
        self.lte = lte
        self.page_size = page_size
        self.is_ascending = is_ascending
        self.stored_events: List[NamedTuple] = [] 
Example #5
Source File: toolkit.py    From aztk with MIT License 6 votes vote down vote up
def execute(args: typing.NamedTuple):
    if not args.toolkit_software:
        return print_available_softwares()

    if not validate_software(args.toolkit_software):
        return None

    if not args.version:
        return print_available_software_version(args.toolkit_software)
    if not args.environment:
        print_available_environments(args.toolkit_software)

    toolkit = Toolkit(software=args.toolkit_software, version=args.version, environment=args.environment)

    toolkit.validate()
    log.info("Docker image picked for this toolkit: %s", toolkit.get_docker_repo(args.gpu))
    return None 
Example #6
Source File: factory.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def construct_record_manager(
        self,
        record_class: Optional[type],
        sequenced_item_class: Optional[Type[NamedTuple]] = None,
        **kwargs: Any
    ) -> AbstractRecordManager:
        """
        Constructs Axon record manager.

        :return: An Axon record manager.
        :rtype: AxonRecordManager
        """
        return super(AxonInfrastructureFactory, self).construct_record_manager(
            record_class,
            sequenced_item_class=sequenced_item_class,
            axon_client=self.axon_client,
            **kwargs
        ) 
Example #7
Source File: project.py    From nucleus7 with Mozilla Public License 2.0 6 votes vote down vote up
def get_active_project_dirs() -> NamedTuple:
    """
    Get activation project directories

    Returns
    -------
    project_dirs
        project directories

    Raises
    ------
    ValueError
        if no project was created
    """
    _raise_if_no_active_project()
    return _ActiveProject.project_dirs 
Example #8
Source File: pyupgrade.py    From pyupgrade with MIT License 6 votes vote down vote up
def _replace_typed_class(
        tokens: List[Token],
        i: int,
        call: ast.Call,
        types: Dict[str, ast.expr],
) -> None:
    if i > 0 and tokens[i - 1].name in {'INDENT', UNIMPORTANT_WS}:
        indent = f'{tokens[i - 1].src}{" " * 4}'
    else:
        indent = ' ' * 4

    # NT = NamedTuple("nt", [("a", int)])
    # ^i                                 ^end
    end = i + 1
    while end < len(tokens) and tokens[end].name != 'NEWLINE':
        end += 1

    attrs = '\n'.join(f'{indent}{k}: {_unparse(v)}' for k, v in types.items())
    src = f'class {tokens[i].src}({_unparse(call.func)}):\n{attrs}'
    tokens[i:end] = [Token('CODE', src)] 
Example #9
Source File: guffin.py    From NGU-scripts with GNU Lesser General Public License v3.0 6 votes vote down vote up
def init(settings: NamedTuple) -> None:
        """Initialize settings."""
        GuffinRun.max_rb_duration = settings.max_rb_duration
        GuffinRun.zone = settings.zone
        GuffinRun.gold_zone = settings.gold_zone
        GuffinRun.hacks = settings.hacks
        GuffinRun.diggers = settings.diggers
        GuffinRun.butter = settings.butter
        GuffinRun.aug = settings.aug
        GuffinRun.allocate_wishes = settings.allocate_wishes
        GuffinRun.wandoos_version = settings.wandoos_version
        GuffinRun.wish_min_time = settings.wish_min_time
        GuffinRun.wish_slots = settings.wish_slots

        if GuffinRun.allocate_wishes:
            GuffinRun.wishes = Wishes(GuffinRun.wish_slots, GuffinRun.wish_min_time)
            lst = [GuffinRun.wishes.epow, GuffinRun.wishes.mpow, GuffinRun.wishes.rpow]
            i = 0
            while 1 in lst:
                print("OCR reading failed for stat breakdowns, trying again...")
                GuffinRun.wishes = Wishes(GuffinRun.wish_slots, GuffinRun.wish_min_time)
                i += 1
                if i > 5:
                    print("Wishes will be disabled.")
                    GuffinRun.wishes = None
                    break 
Example #10
Source File: cluster_delete.py    From aztk with MIT License 6 votes vote down vote up
def execute(args: typing.NamedTuple):
    spark_client = aztk.spark.Client(config.load_aztk_secrets())
    cluster_ids = args.cluster_ids

    for cluster_id in cluster_ids:
        if not args.force:
            if not args.keep_logs:
                log.warning("All logs persisted for this cluster will be deleted.")

            confirmation_cluster_id = input(
                "Please confirm the id of the cluster you wish to delete [{}]: ".format(cluster_id))

            if confirmation_cluster_id != cluster_id:
                log.error("Confirmation cluster id does not match. Please try again.")
                return

        if spark_client.cluster.delete(id=cluster_id, keep_logs=args.keep_logs):
            log.info("Deleting cluster %s", cluster_id)
        else:
            log.error("Cluster with id '%s' doesn't exist or was already deleted.", cluster_id) 
Example #11
Source File: cluster.py    From aztk with MIT License 6 votes vote down vote up
def execute(args: typing.NamedTuple):
    actions = {}

    actions[ClusterAction.create] = cluster_create.execute
    actions[ClusterAction.add_user] = cluster_add_user.execute
    actions[ClusterAction.delete] = cluster_delete.execute
    actions[ClusterAction.get] = cluster_get.execute
    actions[ClusterAction.list] = cluster_list.execute
    actions[ClusterAction.ssh] = cluster_ssh.execute
    actions[ClusterAction.submit] = cluster_submit.execute
    actions[ClusterAction.app_logs] = cluster_app_logs.execute
    actions[ClusterAction.run] = cluster_run.execute
    actions[ClusterAction.copy] = cluster_copy.execute
    actions[ClusterAction.debug] = cluster_debug.execute

    func = actions[args.cluster_action]
    func(args) 
Example #12
Source File: cluster_add_user.py    From aztk with MIT License 6 votes vote down vote up
def execute(args: typing.NamedTuple):
    spark_client = aztk.spark.Client(config.load_aztk_secrets())

    log.info("-------------------------------------------")
    log.info("spark cluster id:    %s", args.cluster_id)
    log.info("username:            %s", args.username)
    log.info("-------------------------------------------")

    if args.ssh_key:
        ssh_key = args.ssh_key
    else:
        ssh_key = spark_client.secrets_configuration.ssh_pub_key

    ssh_key, password = utils.get_ssh_key_or_prompt(ssh_key, args.username, args.password,
                                                    spark_client.secrets_configuration)

    spark_client.cluster.create_user(id=args.cluster_id, username=args.username, password=password, ssh_key=ssh_key)

    if password:
        log.info("password:            %s", "*" * len(password))
    elif ssh_key:
        log.info("ssh public key:      %s", ssh_key)

    log.info("-------------------------------------------") 
Example #13
Source File: tasks.py    From gym-jsbsim with MIT License 6 votes vote down vote up
def task_step(self, sim: Simulation, action: Sequence[float], sim_steps: int) \
            -> Tuple[NamedTuple, float, bool, Dict]:
        # input actions
        for prop, command in zip(self.action_variables, action):
            sim[prop] = command

        # run simulation
        for _ in range(sim_steps):
            sim.run()

        self._update_custom_properties(sim)
        state = self.State(*(sim[prop] for prop in self.state_variables))
        done = self._is_terminal(sim)
        reward = self.assessor.assess(state, self.last_state, done)
        if done:
            reward = self._reward_terminal_override(reward, sim)
        if self.debug:
            self._validate_state(state, done, action, reward)
        self._store_reward(reward, sim)
        self.last_state = state
        info = {'reward': reward}

        return state, reward.agent_reward(), done, info 
Example #14
Source File: util_test.py    From allennlp with Apache License 2.0 5 votes vote down vote up
def test_move_to_device(self):
        # We're faking the tensor here so that we can test the calls to .cuda() without actually
        # needing a GPU.
        class FakeTensor(torch.Tensor):
            def __init__(self):
                self._device = None

            def cuda(self, device):
                self._device = device
                return self

        class A(NamedTuple):
            a: int
            b: torch.Tensor

        structured_obj = {
            "a": [A(1, FakeTensor()), A(2, FakeTensor())],
            "b": FakeTensor(),
            "c": (1, FakeTensor()),
        }
        new_device = torch.device(4)
        moved_obj = util.move_to_device(structured_obj, new_device)
        assert moved_obj["a"][0].a == 1
        assert moved_obj["a"][0].b._device == new_device
        assert moved_obj["a"][1].b._device == new_device
        assert moved_obj["b"]._device == new_device
        assert moved_obj["c"][0] == 1
        assert moved_obj["c"][1]._device == new_device 
Example #15
Source File: cluster_run.py    From aztk with MIT License 5 votes vote down vote up
def execute(args: typing.NamedTuple):
    spark_client = aztk.spark.Client(config.load_aztk_secrets())
    with utils.Spinner():
        if args.node_id:
            results = [
                spark_client.cluster.node_run(args.cluster_id, args.node_id, args.command, args.host, args.internal)
            ]
        else:
            results = spark_client.cluster.run(args.cluster_id, args.command, args.host, args.internal)
    for node_output in results:
        utils.log_node_run_output(node_output) 
Example #16
Source File: cluster_debug.py    From aztk with MIT License 5 votes vote down vote up
def execute(args: typing.NamedTuple):
    spark_client = aztk.spark.Client(config.load_aztk_secrets())
    timestr = time.strftime("%Y%m%d-%H%M%S")

    if not args.output:
        args.output = os.path.join(os.getcwd(), "debug-{0}-{1}".format(args.cluster_id, timestr))
    with utils.Spinner():
        spark_client.cluster.diagnostics(id=args.cluster_id, output_directory=args.output, brief=args.brief)
    # TODO: analyze results, display some info about status 
Example #17
Source File: cluster_copy.py    From aztk with MIT License 5 votes vote down vote up
def execute(args: typing.NamedTuple):
    spark_client = aztk.spark.Client(config.load_aztk_secrets())
    with utils.Spinner():
        copy_output = spark_client.cluster.copy(
            id=args.cluster_id, source_path=args.source_path, destination_path=args.dest_path, internal=args.internal)
    for node_output in copy_output:
        utils.log_node_copy_output(node_output)
    sys.exit(0 if not any([node_output.error for node_output in copy_output]) else 1) 
Example #18
Source File: test_typing.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_basics(self):
        Emp = NamedTuple('Emp', [('name', str), ('id', int)])
        assert issubclass(Emp, tuple)
        joe = Emp('Joe', 42)
        jim = Emp(name='Jim', id=1)
        assert isinstance(joe, Emp)
        assert isinstance(joe, tuple)
        assert joe.name == 'Joe'
        assert joe.id == 42
        assert jim.name == 'Jim'
        assert jim.id == 1
        assert Emp.__name__ == 'Emp'
        assert Emp._fields == ('name', 'id')
        assert Emp._field_types == dict(name=str, id=int) 
Example #19
Source File: factory.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def construct_sqlalchemy_eventstore(
    session: Any,
    sequenced_item_class: Optional[Type[NamedTuple]] = None,
    sequence_id_attr_name: Optional[str] = None,
    position_attr_name: Optional[str] = None,
    json_encoder_class: Optional[Type[ObjectJSONEncoder]] = None,
    json_decoder_class: Optional[Type[ObjectJSONDecoder]] = None,
    cipher: Optional[AESCipher] = None,
    record_class: Optional[type] = None,
    contiguous_record_ids: bool = False,
    application_name: Optional[str] = None,
    pipeline_id: int = DEFAULT_PIPELINE_ID,
) -> EventStore:
    sequenced_item_class = sequenced_item_class or StoredEvent  # type: ignore
    sequenced_item_mapper = SequencedItemMapper[DomainEvent](
        sequenced_item_class=sequenced_item_class,
        sequence_id_attr_name=sequence_id_attr_name,
        position_attr_name=position_attr_name,
        json_encoder_class=json_encoder_class,
        json_decoder_class=json_decoder_class,
        cipher=cipher,
    )
    factory = SQLAlchemyInfrastructureFactory(
        session=session,
        integer_sequenced_record_class=record_class or StoredEventRecord,
        sequenced_item_class=sequenced_item_class,
        contiguous_record_ids=contiguous_record_ids,
        application_name=application_name,
        pipeline_id=pipeline_id,
    )
    record_manager = factory.construct_integer_sequenced_record_manager()
    event_store = EventStore[DomainEvent, AbstractRecordManager](
        record_manager=record_manager, event_mapper=sequenced_item_mapper
    )
    return event_store 
Example #20
Source File: cluster_ssh.py    From aztk with MIT License 5 votes vote down vote up
def execute(args: typing.NamedTuple):
    spark_client = aztk.spark.Client(config.load_aztk_secrets())
    cluster = spark_client.cluster.get(args.cluster_id)
    cluster_configuration = spark_client.cluster.get_configuration(args.cluster_id)
    ssh_conf = SshConfig()

    ssh_conf.merge(
        cluster_id=args.cluster_id,
        username=args.username,
        job_ui_port=args.jobui,
        job_history_ui_port=args.jobhistoryui,
        web_ui_port=args.webui,
        host=args.host,
        connect=args.connect,
        internal=args.internal,
    )

    log.info("-------------------------------------------")
    utils.log_property("spark cluster id", ssh_conf.cluster_id)
    utils.log_property("open webui", "{0}{1}".format(http_prefix, ssh_conf.web_ui_port))
    utils.log_property("open jobui", "{0}{1}".format(http_prefix, ssh_conf.job_ui_port))
    utils.log_property("open jobhistoryui", "{0}{1}".format(http_prefix, ssh_conf.job_history_ui_port))
    print_plugin_ports(cluster_configuration)
    utils.log_property("ssh username", ssh_conf.username)
    utils.log_property("connect", ssh_conf.connect)
    log.info("-------------------------------------------")

    try:
        shell_out_ssh(spark_client, cluster_configuration, ssh_conf)
    except OSError:
        # no ssh client is found, falling back to pure python
        native_python_ssh_into_master(spark_client, cluster, cluster_configuration, ssh_conf, args.password) 
Example #21
Source File: cluster_app_logs.py    From aztk with MIT License 5 votes vote down vote up
def execute(args: typing.NamedTuple):
    spark_client = aztk.spark.Client(config.load_aztk_secrets())

    if args.tail:
        utils.stream_logs(client=spark_client, cluster_id=args.cluster_id, application_name=args.app_name)
    else:
        app_log = spark_client.cluster.get_application_log(id=args.cluster_id, application_name=args.app_name)
        if args.output:
            with utils.Spinner():
                with open(os.path.abspath(os.path.expanduser(args.output)), "w", encoding="UTF-8") as f:
                    f.write(app_log.log)
        else:
            log.print(app_log.log) 
Example #22
Source File: sequenceditem.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, sequenced_item_class: Type[NamedTuple]):
        self._field_names = sequenced_item_class._fields 
Example #23
Source File: eventstore.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def items_from_events(self, events: Iterable[TEvent]) -> Iterable[NamedTuple]:
        """
        Maps domain event to sequenced item namedtuple.

        An iterable of events.
        """
        # Convert the domain event(s) to sequenced item(s).
        return map(self.event_mapper.item_from_event, events) 
Example #24
Source File: cluster_list.py    From aztk with MIT License 5 votes vote down vote up
def execute(args: typing.NamedTuple):
    spark_client = aztk.spark.Client(config.load_aztk_secrets())
    clusters = spark_client.cluster.list()
    if args.quiet:
        utils.print_clusters_quiet(clusters)
    else:
        utils.print_clusters(clusters) 
Example #25
Source File: stop.py    From aztk with MIT License 5 votes vote down vote up
def execute(args: typing.NamedTuple):
    spark_client = aztk.spark.Client(config.load_aztk_secrets())
    spark_client.job.stop(args.job_id)
    log.print("Stopped Job {0}".format(args.job_id)) 
Example #26
Source File: stop_app.py    From aztk with MIT License 5 votes vote down vote up
def execute(args: typing.NamedTuple):
    spark_client = aztk.spark.Client(config.load_aztk_secrets())

    if spark_client.job.stop_application(args.job_id, args.app_name):
        log.info("Stopped app %s", args.app_name)
    else:
        log.error("App with name %s does not exist or was already deleted", args.app_name) 
Example #27
Source File: submit.py    From aztk with MIT License 5 votes vote down vote up
def execute(args: typing.NamedTuple):
    spark_client = aztk.spark.Client(config.load_aztk_secrets())
    job_conf = JobConfig()

    job_conf.merge(args.job_id, args.job_conf)

    # by default, load spark configuration files in .aztk/
    spark_configuration = config.load_aztk_spark_config()
    # overwrite with values in job_conf if they exist
    if job_conf.spark_defaults_conf:
        spark_configuration.spark_defaults_conf = job_conf.spark_defaults_conf
    if job_conf.spark_env_sh:
        spark_configuration.spark_env_sh = job_conf.spark_env_sh
    if job_conf.core_site_xml:
        spark_configuration.core_site_xml = job_conf.core_site_xml

    job_configuration = aztk.spark.models.JobConfiguration(
        id=job_conf.id,
        applications=job_conf.applications,
        spark_configuration=spark_configuration,
        vm_size=job_conf.vm_size,
        toolkit=job_conf.toolkit,
        max_dedicated_nodes=job_conf.max_dedicated_nodes,
        max_low_pri_nodes=job_conf.max_low_pri_nodes,
        subnet_id=job_conf.subnet_id,
        worker_on_master=job_conf.worker_on_master,
        scheduling_target=job_conf.scheduling_target,
    )

    # TODO: utils.print_job_conf(job_configuration)
    spark_client.job.submit(job_configuration) 
Example #28
Source File: get.py    From aztk with MIT License 5 votes vote down vote up
def execute(args: typing.NamedTuple):
    spark_client = aztk.spark.Client(config.load_aztk_secrets())

    utils.print_job(spark_client, spark_client.job.get(id=args.job_id)) 
Example #29
Source File: manager.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def to_records(self, sequenced_items: Iterable[NamedTuple]) -> Iterable[Any]:
        return (PopoStoredEventRecord(s) for s in sequenced_items) 
Example #30
Source File: spark.py    From aztk with MIT License 5 votes vote down vote up
def execute(args: typing.NamedTuple):
    actions = dict(cluster=cluster.execute, job=job.execute, init=init.execute)
    func = actions[args.action]
    func(args)