當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。