当前位置: 首页>>代码示例>>Python>>正文


Python contextlib.ExitStack方法代码示例

本文整理汇总了Python中contextlib.ExitStack方法的典型用法代码示例。如果您正苦于以下问题:Python contextlib.ExitStack方法的具体用法?Python contextlib.ExitStack怎么用?Python contextlib.ExitStack使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在contextlib的用法示例。


在下文中一共展示了contextlib.ExitStack方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: upload_files

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def upload_files(self, *filenames):
        """ Upload files using form """
        # The ExitStack closes files for us when the with block exits
        with contextlib.ExitStack() as stack:
            files = {}
            for i, filename in enumerate(filenames):
                open_file = stack.enter_context(open(filename, 'rb'))
                mime_type, _ = mimetypes.guess_type(filename)
                if not mime_type or mime_type.split('/')[0] != 'image':
                    raise ValueError(
                        'Unknown image file type {}'.format(mime_type))

                name = os.path.basename(filename)
                try:
                    # until https://github.com/shazow/urllib3/issues/303 is
                    # resolved, only use the filename if it is Latin-1 safe
                    name.encode('latin1')
                except UnicodeEncodeError:
                    name = 'justfilename'
                files['file-upload[{}]'.format(i)] = (
                    name, open_file, mime_type)
            return self._perform(files=files) 
开发者ID:theirix,项目名称:ptpimg-uploader,代码行数:24,代码来源:ptpimg_uploader.py

示例2: upload_urls

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def upload_urls(self, *urls):
        """ Upload image URLs by downloading them before """
        with contextlib.ExitStack() as stack:
            files = {}
            for i, url in enumerate(urls):
                resp = requests.get(url, timeout=self.timeout)
                if resp.status_code != requests.codes.ok:
                    raise ValueError(
                        'Cannot fetch url {} with error {}'.format(url, resp.status_code))

                mime_type = resp.headers['content-type']
                if not mime_type or mime_type.split('/')[0] != 'image':
                    raise ValueError(
                        'Unknown image file type {}'.format(mime_type))
                open_file = stack.enter_context(BytesIO(resp.content))
                files['file-upload[{}]'.format(i)] = (
                    'file-{}'.format(i), open_file, mime_type)

            return self._perform(files=files) 
开发者ID:theirix,项目名称:ptpimg-uploader,代码行数:21,代码来源:ptpimg_uploader.py

示例3: on_draw

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def on_draw():
    window.clear()
    with ExitStack() as stack:
        for shader in [rc.resources.shadow_shader, rc.default_shader]:
            with shader, rc.default_states, light, rc.default_camera:
                if shader == rc.resources.shadow_shader:
                    stack.enter_context(fbo_shadow)
                    window.clear()
                else:
                    stack.close()

                for x, y in it.product([-2, -1, 0, 1, 2], [-2, -1, 0, 1, 2]):
                    monkey.position.x = x
                    monkey.position.y = y
                    monkey.drawmode = rc.GL_POINTS if x % 2 and y % 2 else rc.GL_TRIANGLES
                    monkey.uniforms['diffuse'][0] = (x + 1) / 4.
                    monkey.uniforms['diffuse'][1:] = (y + 1) / 4.
                    monkey.scale.z = np.linalg.norm((x, y)) / 10. + .03
                    monkey.draw()

                plane.draw()

    fps_display.draw() 
开发者ID:ratcave,项目名称:ratcave,代码行数:25,代码来源:shadow_demo.py

示例4: test__prepare_container

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def test__prepare_container():
    with contextlib.ExitStack() as stack:
        # mock modules
        mocked_client_call = stack.enter_context(
            patch(f"{MODULE_TO_TEST}.skein.ApplicationClient.from_current"))
        mocked_logs = stack.enter_context(patch(f'{MODULE_TO_TEST}._setup_container_logs'))
        mocked_cluster_spec = stack.enter_context(patch(f'{MODULE_TO_TEST}.cluster.start_cluster'))

        # fill client mock
        mocked_client = mock.MagicMock(spec=skein.ApplicationClient)
        host_port = ('localhost', 1234)
        instances = [('worker', 10), ('chief', 1)]
        mocked_client.kv.wait.return_value = json.dumps(instances).encode()
        mocked_client_call.return_value = mocked_client
        (client, cluster_spec, cluster_tasks) = _prepare_container(host_port)

        # checks
        mocked_logs.assert_called_once()
        mocked_cluster_spec.assert_called_once_with(host_port, mocked_client, cluster_tasks)
        assert client == mocked_client
        assert cluster_tasks == list(iter_tasks(instances)) 
开发者ID:criteo,项目名称:tf-yarn,代码行数:23,代码来源:test__task_commons.py

示例5: test__execute_dispatched_function

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def test__execute_dispatched_function():
    with contextlib.ExitStack() as stack:
        mocked_event = stack.enter_context(patch(f'{MODULE_TO_TEST}.event'))
        mocked_train = stack.enter_context(
            patch(f'{MODULE_TO_TEST}.tf.estimator.train_and_evaluate'))
        passed_args = []
        mocked_train.side_effect = lambda *args: passed_args.append(args)
        mocked_cluster = stack.enter_context(patch(f'{MODULE_TO_TEST}.cluster'))
        mocked_cluster.get_task_description.return_value = ("worker", "0")

        mocked_client = mock.MagicMock(spec=skein.ApplicationClient)
        mocked_experiment = Experiment(None, None, None)
        thread = _execute_dispatched_function(mocked_client, mocked_experiment)
        # assert thread.state == 'RUNNING'
        thread.join()
        mocked_event.start_event.assert_called_once()
        assert passed_args == [(None, None, None)]
        assert thread.state == 'SUCCEEDED' 
开发者ID:criteo,项目名称:tf-yarn,代码行数:20,代码来源:test__task_commons.py

示例6: test_start_tf_server

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def test_start_tf_server(task_name, task_index, is_server_started):

    CLUSTER_SPEC = {"worker": [f"worker0.{WORKER0_HOST}:{WORKER0_PORT}",
                              f"worker1.{WORKER1_HOST}:{WORKER1_PORT}"],
                    "ps": [f"ps0.{CURRENT_HOST}:{CURRENT_PORT}"]}

    with contextlib.ExitStack() as stack:
        stack.enter_context(mock.patch.dict(os.environ))
        os.environ["SKEIN_CONTAINER_ID"] = f"{task_name}_{task_index}"
        mock_server = stack.enter_context(mock.patch(f"{MODULE_TO_TEST}.tf.distribute"))
        cluster.start_tf_server(CLUSTER_SPEC)

        if is_server_started:
            assert mock_server.Server.call_count == 1
            _, kwargs = mock_server.Server.call_args
            assert kwargs["job_name"] == task_name
            assert kwargs["task_index"] == task_index
            assert kwargs["start"] is True
        else:
            assert mock_server.Server.call_count == 0 
开发者ID:criteo,项目名称:tf-yarn,代码行数:22,代码来源:test_cluster.py

示例7: mock_available_location_types

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def mock_available_location_types():
    mock_types = [
        'runtimeenv',
        'ecosystem',
        'superregion',
        'region',
        'habitat',
    ]
    patchers = [
        mock.patch(
            'environment_tools.type_utils.available_location_types',
            return_value=mock_types,
        ),
        mock.patch(
            'synapse_tools.configure_synapse.available_location_types',
            return_value=mock_types,
        ),
    ]

    with contextlib.ExitStack() as stack:
        yield tuple(stack.enter_context(patch) for patch in patchers) 
开发者ID:Yelp,项目名称:synapse-tools,代码行数:23,代码来源:configure_synapse_test.py

示例8: __call__

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def __call__(self, *dec_args, **dec_kwargs):
        kw_name, dec_args, dec_kwargs = self.process_decorator_args(*dec_args, **dec_kwargs)
        num_socket_str = dec_kwargs.pop('num_socket')

        def decorator(func):
            @wraps(func)
            def wrapper(*args, **kwargs):
                num_socket = getattr(args[0], num_socket_str)
                targets = [self.get_target(*args, **kwargs) for _ in range(num_socket)]
                with ExitStack() as stack:
                    for target in targets:
                        obj = stack.enter_context(target(*dec_args, **dec_kwargs))
                        args = args + (obj,)

                    return func(*args, **kwargs)

            return wrapper

        return decorator 
开发者ID:hanxiao,项目名称:bert-as-service,代码行数:21,代码来源:zmq_decor.py

示例9: create_node_and_not_start

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def create_node_and_not_start(testNodeClass,
                              node_config_helper_class,
                              tconf,
                              tdir,
                              allPluginsPath,
                              looper,
                              tdirWithPoolTxns,
                              tdirWithDomainTxns,
                              tdirWithNodeKeepInited):
    with ExitStack() as exitStack:
        node = exitStack.enter_context(create_new_test_node(testNodeClass,
                                node_config_helper_class,
                                "Alpha",
                                tconf,
                                tdir,
                                allPluginsPath))
        yield node
        node.stop() 
开发者ID:hyperledger,项目名称:indy-plenum,代码行数:20,代码来源:test_start_vc_ts_in_node_info.py

示例10: txnPoolNodeSet

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def txnPoolNodeSet(node_config_helper_class,
                   patchPluginManager,
                   txnPoolNodesLooper,
                   tdirWithPoolTxns,
                   tdirWithDomainTxns,
                   tdir,
                   tconf,
                   poolTxnNodeNames,
                   allPluginsPath,
                   tdirWithNodeKeepInited,
                   testNodeClass,
                   do_post_node_creation,
                   testNodeBootstrapClass):
    with ExitStack() as exitStack:
        nodes = []
        for nm in poolTxnNodeNames:
            node = exitStack.enter_context(create_new_test_node(
                testNodeClass, node_config_helper_class, nm, tconf, tdir,
                allPluginsPath, bootstrap_cls=testNodeBootstrapClass))
            do_post_node_creation(node)
            txnPoolNodesLooper.add(node)
            nodes.append(node)
        txnPoolNodesLooper.run(checkNodesConnected(nodes))
        ensureElectionsDone(looper=txnPoolNodesLooper, nodes=nodes)
        yield nodes 
开发者ID:hyperledger,项目名称:indy-plenum,代码行数:27,代码来源:conftest.py

示例11: txnPoolNodeSetNotStarted

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def txnPoolNodeSetNotStarted(node_config_helper_class,
                             patchPluginManager,
                             txnPoolNodesLooper,
                             tdirWithPoolTxns,
                             tdirWithDomainTxns,
                             tdir,
                             tconf,
                             poolTxnNodeNames,
                             allPluginsPath,
                             tdirWithNodeKeepInited,
                             testNodeClass,
                             do_post_node_creation):
    with ExitStack() as exitStack:
        nodes = []
        for nm in poolTxnNodeNames:
            node = exitStack.enter_context(create_new_test_node(
                testNodeClass, node_config_helper_class, nm, tconf, tdir,
                allPluginsPath))
            do_post_node_creation(node)
            nodes.append(node)
        yield nodes 
开发者ID:hyperledger,项目名称:indy-plenum,代码行数:23,代码来源:conftest.py

示例12: create_node_and_not_start

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def create_node_and_not_start(testNodeClass,
                              node_config_helper_class,
                              tconf,
                              tdir,
                              allPluginsPath,
                              looper,
                              tdirWithPoolTxns,
                              tdirWithDomainTxns,
                              tdirWithNodeKeepInited):
    with ExitStack() as exitStack:
        node = exitStack.enter_context(create_new_test_node(testNodeClass,
                                node_config_helper_class,
                                "Alpha",
                                tconf,
                                tdir,
                                allPluginsPath))
        node.write_manager.on_catchup_finished()
        yield node
        node.stop() 
开发者ID:hyperledger,项目名称:indy-plenum,代码行数:21,代码来源:conftest.py

示例13: create_node_and_not_start

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def create_node_and_not_start(testNodeClass,
                              node_config_helper_class,
                              tconf,
                              tdir,
                              allPluginsPath,
                              looper,
                              tdirWithPoolTxns,
                              tdirWithDomainTxns,
                              tdirWithNodeKeepInited):
    with ExitStack() as exitStack:
        node = exitStack.enter_context(create_new_test_node(testNodeClass,
                                                            node_config_helper_class,
                                                            "Alpha",
                                                            tconf,
                                                            tdir,
                                                            allPluginsPath))
        yield node
        node.stop() 
开发者ID:hyperledger,项目名称:indy-plenum,代码行数:20,代码来源:test_start_view_change_ts_set.py

示例14: __call__

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def __call__(self):
        status = 1
        with log_exception(status=1):
            args = self.parser.parse_args()
            log_args(args)
            config.log_cached()
            logger = getLogger('django')

            with ExitStack() as stack:
                if self._pid_file:
                    stack.enter_context(pid_file(dirname=config.PID_DIR, max_age=self._pid_file_max_age))

                if self._stoppable:
                    self._stoppable_instance = stoppable()
                    stack.enter_context(self._stoppable_instance)

                status = self.run(args, logger) or 0
        sys.exit(status) 
开发者ID:andersroos,项目名称:rankedftw,代码行数:20,代码来源:base.py

示例15: evaluate_semseg

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import ExitStack [as 别名]
def evaluate_semseg(model, data_loader, class_info, observers=()):
    model.eval()
    managers = [torch.no_grad()] + list(observers)
    with contextlib.ExitStack() as stack:
        for ctx_mgr in managers:
            stack.enter_context(ctx_mgr)
        conf_mat = np.zeros((model.num_classes, model.num_classes), dtype=np.uint64)
        for step, batch in tqdm(enumerate(data_loader), total=len(data_loader)):
            batch['original_labels'] = batch['original_labels'].numpy().astype(np.uint32)
            logits, additional = model.do_forward(batch, batch['original_labels'].shape[1:3])
            pred = torch.argmax(logits.data, dim=1).byte().cpu().numpy().astype(np.uint32)
            for o in observers:
                o(pred, batch, additional)
            cylib.collect_confusion_matrix(pred.flatten(), batch['original_labels'].flatten(), conf_mat)
        print('')
        pixel_acc, iou_acc, recall, precision, _, per_class_iou = compute_errors(conf_mat, class_info, verbose=True)
    model.train()
    return iou_acc, per_class_iou 
开发者ID:orsic,项目名称:swiftnet,代码行数:20,代码来源:evaluate.py


注:本文中的contextlib.ExitStack方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。