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