本文整理汇总了Python中six.moves.builtins方法的典型用法代码示例。如果您正苦于以下问题:Python moves.builtins方法的具体用法?Python moves.builtins怎么用?Python moves.builtins使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves
的用法示例。
在下文中一共展示了moves.builtins方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _open_cache
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def _open_cache(self, initial_contents=None, update_cache=True):
"""Creates an ApiCache object, mocking the contents of the cache on disk.
Args:
initial_contents: A dict containing the initial contents of the cache
update_cache: Specifies whether ApiCache should write out the
cache file when closing it
Returns:
ApiCache
"""
if not initial_contents:
initial_contents = {}
file_contents = simplejson.dumps(initial_contents)
mock_read = mock_open(read_data=file_contents)
with patch.object(builtins, 'open', mock_read, create=True):
api_cache = ApiCache(self._file_name, update_cache=update_cache)
return api_cache
示例2: _close_cache
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def _close_cache(self, api_cache, cache_written=True):
"""Closes an ApiCache and reads the final contents that were written to disk.
Args:
api_cache: An ApiCache instance
cache_written: Specifies whether it should test that the cache
was written out to the cache file or whether to
test that it was not written out
Returns:
A dict representing the contents of the cache that was written
out to the cache file or `None` in case cache was not expected
to be written out
"""
mock_write = mock_open()
with patch.object(builtins, 'open', mock_write, create=True) as patched_open:
api_cache.close()
if cache_written:
return assert_cache_written(mock_write, patched_open)
return assert_cache_not_written(mock_write)
示例3: _astroid_bootstrapping
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def _astroid_bootstrapping(astroid_builtin=None):
"""astroid boot strapping the builtins module"""
# this boot strapping is necessary since we need the Const nodes to
# inspect_build builtins, and then we can proxy Const
if astroid_builtin is None:
from six.moves import builtins
astroid_builtin = Astroid_BUILDER.inspect_build(builtins)
# pylint: disable=redefined-outer-name
for cls, node_cls in node_classes.CONST_CLS.items():
if cls is type(None):
proxy = build_class('NoneType')
proxy.parent = astroid_builtin
elif cls is type(NotImplemented):
proxy = build_class('NotImplementedType')
proxy.parent = astroid_builtin
else:
proxy = astroid_builtin.getattr(cls.__name__)[0]
if cls in (dict, list, set, tuple):
node_cls._proxied = proxy
else:
_CONST_PROXY[cls] = proxy
示例4: _do_test_add_write_failure
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def _do_test_add_write_failure(self, errno, exception):
filesystem.ChunkedFile.CHUNKSIZE = units.Ki
image_id = str(uuid.uuid4())
file_size = 5 * units.Ki # 5K
file_contents = b"*" * file_size
path = os.path.join(self.test_dir, image_id)
image_file = six.BytesIO(file_contents)
with mock.patch.object(builtins, 'open') as popen:
e = IOError()
e.errno = errno
popen.side_effect = e
self.assertRaises(exception,
self.store.add,
image_id, image_file, 0, self.hash_algo)
self.assertFalse(os.path.exists(path))
示例5: _do_test_add_write_failure
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def _do_test_add_write_failure(self, errno, exception):
filesystem.ChunkedFile.CHUNKSIZE = units.Ki
image_id = str(uuid.uuid4())
file_size = 5 * units.Ki # 5K
file_contents = b"*" * file_size
path = os.path.join(self.test_dir, image_id)
image_file = six.BytesIO(file_contents)
with mock.patch.object(builtins, 'open') as popen:
e = IOError()
e.errno = errno
popen.side_effect = e
self.assertRaises(exception,
self.store.add,
image_id, image_file, 0)
self.assertFalse(os.path.exists(path))
示例6: requires_ipython
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def requires_ipython(fn):
"""Decorator for methods that can only be run in IPython."""
@functools.wraps(fn)
def run_if_ipython(*args, **kwargs):
"""Invokes `fn` if called from IPython, otherwise just emits a warning."""
if getattr(builtins, '__IPYTHON__', None):
# __IPYTHON__ variable is set by IPython, see
# https://ipython.org/ipython-doc/rel-0.10.2/html/interactive/reference.html#embedding-ipython.
return fn(*args, **kwargs)
else:
absl.logging.warning(
'Method "%s" is a no-op when invoked outside of IPython.',
fn.__name__)
return run_if_ipython
示例7: register_formatters
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def register_formatters():
"""Register HTML notebook formatters for TFX classes.
This method registers HTML formatters for TFX classes for display in
IPython / Jupyter / Colab notebooks. No action will be performed if called
outside a notebook environment.
"""
if getattr(builtins, '__IPYTHON__', None):
# Skip registration if (1) IPython is not installed or (2) if IPython is
# installed but we are not running in the notebook context (in this case,
# get_ipython() returns None).
try:
ipython = __import__('IPython.core.getipython').get_ipython()
if not ipython:
return
except ImportError:
return
html_formatter = ipython.display_formatter.formatters['text/html']
for cls, formatter in FORMATTER_REGISTRY.items():
html_formatter.for_type(cls, formatter.render)
示例8: setUp
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def setUp(self):
super(OsWinBaseTestCase, self).setUp()
self._mock_wmi = mock.MagicMock()
baseutils.BaseUtilsVirt._old_wmi = self._mock_wmi
mock_os = mock.MagicMock(Version='6.3.0')
self._mock_wmi.WMI.return_value.Win32_OperatingSystem.return_value = (
[mock_os])
if os.name == 'nt':
# The wmi module is expected to exist and by the time this runs,
# the tested module will have imported it already.
wmi_patcher = mock.patch('wmi.WMI', new=self._mock_wmi.WMI)
else:
# The wmi module doesn't exist, we'll have to "create" it.
wmi_patcher = mock.patch.object(builtins, 'wmi', create=True,
new=self._mock_wmi)
wmi_patcher.start()
示例9: _populate_known_types
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def _populate_known_types(self):
b = [x for x in vars(builtins).values()
if type(x) is type(str)]
def traverse(obj, namespace):
for name in dir(obj):
# Hack for 3.2's warning about body_params
if name == 'body_params':
continue
vtype = type(getattr(obj, name, None))
if vtype in b:
self.known_config_types[namespace + '.' + name] = vtype
traverse(cherrypy.request, 'request')
traverse(cherrypy.response, 'response')
traverse(cherrypy.server, 'server')
traverse(cherrypy.engine, 'engine')
traverse(cherrypy.log, 'log')
示例10: build_Name
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def build_Name(self, o):
name = o.name
if name == 'None':
return None
if name == 'True':
return True
if name == 'False':
return False
# See if the Name is a package or module. If it is, import it.
try:
return modules(name)
except ImportError:
pass
# See if the Name is in builtins.
try:
return getattr(builtins, name)
except AttributeError:
pass
raise TypeError('unrepr could not resolve the name %s' % repr(name))
示例11: imported_member
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def imported_member(self, node, member, name):
"""verify this is not an imported class or handle it"""
# /!\ some classes like ExtensionClass doesn't have a __module__
# attribute ! Also, this may trigger an exception on badly built module
# (see http://www.logilab.org/ticket/57299 for instance)
try:
modname = getattr(member, '__module__', None)
except: # pylint: disable=bare-except
_LOG.exception('unexpected error while building '
'astroid from living object')
modname = None
if modname is None:
if (name in ('__new__', '__subclasshook__')
or (name in _BUILTINS and _JYTHON)):
# Python 2.5.1 (r251:54863, Sep 1 2010, 22:03:14)
# >>> print object.__new__.__module__
# None
modname = six.moves.builtins.__name__
else:
attach_dummy_node(node, name, member)
return True
real_name = {
'gtk': 'gtk_gtk',
'_io': 'io',
}.get(modname, modname)
if real_name != self._module.__name__:
# check if it sounds valid and then add an import node, else use a
# dummy node
try:
getattr(sys.modules[modname], name)
except (KeyError, AttributeError):
attach_dummy_node(node, name, member)
else:
attach_import_node(node, modname, name)
return True
return False
### astroid bootstrapping ######################################################
示例12: test_instance_id_returns_id_in_hex_form_without_0x
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def test_instance_id_returns_id_in_hex_form_without_0x():
from moler.helpers import instance_id
from six.moves import builtins
# 0xf0a1 == 61601 decimal
with mock.patch.object(builtins, "id", return_value=61601):
instance = "moler object"
assert "0x" not in instance_id(instance)
assert instance_id(instance) == "f0a1"
示例13: test_check_dir_exc
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def test_check_dir_exc(self, mock_check_create_dir):
class FakeWindowsError(Exception):
def __init__(self, winerror=None):
self.winerror = winerror
mock_check_create_dir.side_effect = FakeWindowsError(
pathutils.ERROR_INVALID_NAME)
with mock.patch.object(builtins, 'WindowsError',
FakeWindowsError, create=True):
self.assertRaises(exception.AdminRequired,
self._pathutils.check_dir,
mock.sentinel.dir_name,
create_dir=True)
示例14: test_upload_vopt_by_filepath
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def test_upload_vopt_by_filepath(self, mock_create_file, mock_log_warn):
"""Tests the uploads of the virtual disks with an upload retry."""
fake_file = self._fake_meta()
fake_file.adapter.helpers = [vb.vios_busy_retry_helper]
mock_create_file.return_value = fake_file
self.adpt.upload_file.side_effect = [exc.Error("error"),
object()]
m = mock.mock_open()
with mock.patch.object(builtins, 'open', m):
v_opt, f_wrap = ts.upload_vopt(
self.adpt, self.v_uuid, 'fake-path', 'test2', f_size=50)
# Test that vopt was 'uploaded'
self.adpt.upload_file.assert_called_with(mock.ANY, m(), helpers=[])
self.assertIsNone(f_wrap)
self.assertIsNotNone(v_opt)
self.assertIsInstance(v_opt, stor.VOptMedia)
self.assertEqual('test2', v_opt.media_name)
# Validate that there was a warning log call and multiple executions
# of the upload
mock_log_warn.assert_called_once()
self.assertEqual(2, self.adpt.upload_file.call_count)
# Ensure cleanup was called twice since the first uploads fails.
self.adpt.delete.assert_has_calls([mock.call(
'File', service='web',
root_id='6233b070-31cc-4b57-99bd-37f80e845de9')]*2)
示例15: test_check_psycopg2
# 需要导入模块: from six import moves [as 别名]
# 或者: from six.moves import builtins [as 别名]
def test_check_psycopg2(self):
with patch.object(builtins, '__import__', Mock(side_effect=ImportError)):
self.assertRaises(SystemExit, check_psycopg2)
with patch('psycopg2.__version__', '2.5.3.dev1 a b c'):
self.assertRaises(SystemExit, check_psycopg2)