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


Python contextlib._GeneratorContextManager方法代码示例

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


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

示例1: __call__

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import _GeneratorContextManager [as 别名]
def __call__(self, func):
        if isgeneratorfunction(func):
            def inner(*args, **kwds):
                with self._recreate_cm():
                    yield from func(*args, **kwds)
        elif is_contextmanager(func):
            @contextmanager
            def inner(*args, **kwds):
                with self._recreate_cm():
                    with func(*args, **kwds) as ret:
                        yield ret
        else:
            def inner(*args, **kwds):
                with self._recreate_cm():
                    return func(*args, **kwds)
        return wraps(func)(inner)


# Some python version have a different signature for '_GeneratorContextManager.__init__', so we must adapt: 
开发者ID:weka-io,项目名称:easypy,代码行数:21,代码来源:contexts.py

示例2: test_open_and_close

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import _GeneratorContextManager [as 别名]
def test_open_and_close(self):
        # ProfileStore.open() returns a context manager that closes the
        # database on exit.
        config_file = self.makeDir().joinpath("config")
        config = ProfileStore.open(config_file)
        self.assertIsInstance(config, contextlib._GeneratorContextManager)
        with config as config:
            self.assertIsInstance(config, ProfileStore)
            self.assertEqual((1,), config.database.execute("SELECT 1").fetchone())
        self.assertRaises(sqlite3.ProgrammingError, config.database.execute, "SELECT 1") 
开发者ID:maas,项目名称:python-libmaas,代码行数:12,代码来源:test_profiles.py

示例3: test_open_and_close

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import _GeneratorContextManager [as 别名]
def test_open_and_close(self):
        # ProfileConfig.open() returns a context manager that closes the
        # database on exit.
        config_file = os.path.join(self.make_dir(), "config")
        config = api.ProfileConfig.open(config_file, create=True)
        self.assertIsInstance(config, contextlib._GeneratorContextManager)
        with config as config:
            self.assertIsInstance(config, api.ProfileConfig)
            with config.cursor() as cursor:
                self.assertEqual((1,), cursor.execute("SELECT 1").fetchone())
        self.assertRaises(sqlite3.ProgrammingError, config.cursor) 
开发者ID:maas,项目名称:maas,代码行数:13,代码来源:test_config.py

示例4: test_open_and_close

# 需要导入模块: import contextlib [as 别名]
# 或者: from contextlib import _GeneratorContextManager [as 别名]
def test_open_and_close(self):
        # ConfigurationDatabase.open() returns a context manager that closes
        # the database on exit.
        config_file = os.path.join(self.make_dir(), "config")
        config = ConfigurationDatabase.open_for_update(config_file)
        self.assertIsInstance(config, contextlib._GeneratorContextManager)
        with config as config:
            self.assertIsInstance(config, ConfigurationDatabase)
            with config.cursor() as cursor:
                self.assertEqual((1,), cursor.execute("SELECT 1").fetchone())
        self.assertRaises(sqlite3.ProgrammingError, config.cursor) 
开发者ID:maas,项目名称:maas,代码行数:13,代码来源:test_config.py


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