本文整理匯總了Python中__builtin__.open方法的典型用法代碼示例。如果您正苦於以下問題:Python __builtin__.open方法的具體用法?Python __builtin__.open怎麽用?Python __builtin__.open使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類__builtin__
的用法示例。
在下文中一共展示了__builtin__.open方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_write
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def test_write(self):
expected_dict = {
'project_id': 'test_project',
'client_id': 'test_client_id',
'client_secret': 'test_client_secret',
'bucket': 'test_project.appspot.com',
}
self.fs.CreateFile('/this/config/file.yaml', contents=_EMPTY_CONFIG)
test_config = common.ProjectConfig(
'asdf', 'test_project', 'test_client_id', 'test_client_secret', None,
'/this/config/file.yaml',
)
test_config.write()
with open('/this/config/file.yaml') as config_file:
config = yaml.safe_load(config_file)
self.assertEqual(config['asdf'], expected_dict)
示例2: setUp
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def setUp(self):
super(AuthTest, self).setUp()
self._test_project = 'test_project'
self._test_client_id = 'test_client_id'
self._test_client_secret = 'test_client_secret'
self._test_config = common.ProjectConfig(
'test_key', self._test_project, self._test_client_id,
self._test_client_secret, None, '/test/path.yaml')
# Save the real modules for clean up.
self.real_open = builtins.open
# Create a fake file system and stub out builtin modules.
self.fs = fake_filesystem.FakeFilesystem()
self.os = fake_filesystem.FakeOsModule(self.fs)
self.open = fake_filesystem.FakeFileOpen(self.fs)
self.stubs = mox3_stubout.StubOutForTesting()
self.stubs.SmartSet(builtins, 'open', self.open)
self.stubs.SmartSet(auth, 'os', self.os)
示例3: get_source
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def get_source(self, fullname):
"""Returns the source for the module specified by fullname.
This implements part of the extensions to the PEP 302 importer protocol.
Args:
fullname: The fullname of the module.
Returns:
The source for the module.
"""
full_path, _, _, loader = self._get_module_info(fullname)
if loader:
return loader.get_source(fullname)
if full_path is None:
return None
source_file = open(full_path)
try:
return source_file.read()
finally:
source_file.close()
示例4: GetVersionObject
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def GetVersionObject(isfile=os.path.isfile, open_fn=open):
"""Gets the version of the SDK by parsing the VERSION file.
Args:
isfile: used for testing.
open_fn: Used for testing.
Returns:
A Yaml object or None if the VERSION file does not exist.
"""
version_filename = os.path.join(os.path.dirname(google.appengine.__file__),
VERSION_FILE)
if not isfile(version_filename):
logging.error('Could not find version file at %s', version_filename)
return None
version_fh = open_fn(version_filename, 'r')
try:
version = yaml.safe_load(version_fh)
finally:
version_fh.close()
return version
示例5: __init__
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def __init__(self, stream, errors='strict'):
""" Creates a StreamWriter instance.
stream must be a file-like object open for writing
(binary) data.
The StreamWriter may use different error handling
schemes by providing the errors keyword argument. These
parameters are predefined:
'strict' - raise a ValueError (or a subclass)
'ignore' - ignore the character and continue with the next
'replace'- replace with a suitable replacement character
'xmlcharrefreplace' - Replace with the appropriate XML
character reference.
'backslashreplace' - Replace with backslashed escape
sequences (only for encoding).
The set of allowed parameter values can be extended via
register_error.
"""
self.stream = stream
self.errors = errors
示例6: open
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def open(fn, *args, **kwargs):
'''
Open a file in the current output directory
args same as for open()
'''
return _open(filename(fn), *args, **kwargs)
示例7: setUp
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def setUp(self):
super(CommonTest, self).setUp()
# Save the real modules for clean up.
self.real_open = builtins.open
# Create a fake file system and stub out builtin modules.
self.fs = fake_filesystem.FakeFilesystem()
self.os = fake_filesystem.FakeOsModule(self.fs)
self.open = fake_filesystem.FakeFileOpen(self.fs)
self.stubs = mox3_stubout.StubOutForTesting()
self.stubs.SmartSet(builtins, 'open', self.open)
self.stubs.SmartSet(common, 'os', self.os)
示例8: tearDown
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def tearDown(self):
super(CommonTest, self).tearDown()
self.stubs.UnsetAll()
builtins.open = self.real_open
示例9: tearDown
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def tearDown(self):
super(AuthTest, self).tearDown()
self.stubs.UnsetAll()
builtins.open = self.real_open
示例10: tearDown
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def tearDown(self):
super(ConfigurationTest, self).tearDown()
self.stubs.UnsetAll()
__builtin__.open = self.real_open
__builtin__.file = self.real_file
示例11: __init__
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def __init__(self, name, mode):
mode = {
"r": os.O_RDONLY,
"w": os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
}[mode]
if hasattr(os, "O_BINARY"):
mode |= os.O_BINARY
self.fd = os.open(name, mode, 0o666)
示例12: bz2open
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
"""Open bzip2 compressed tar archive name for reading or writing.
Appending is not allowed.
"""
if len(mode) > 1 or mode not in "rw":
raise ValueError("mode must be 'r' or 'w'.")
try:
import bz2
except ImportError:
raise CompressionError("bz2 module is not available")
if fileobj is not None:
fileobj = _BZ2Proxy(fileobj, mode)
else:
fileobj = bz2.BZ2File(name, mode, compresslevel=compresslevel)
try:
t = cls.taropen(name, mode, fileobj, **kwargs)
except (IOError, EOFError):
fileobj.close()
raise ReadError("not a bzip2 file")
t._extfileobj = False
return t
# All *open() methods are registered here.
示例13: _check
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def _check(self, mode=None):
"""Check if TarFile is still open, and if the operation's mode
corresponds to TarFile's mode.
"""
if self.closed:
raise IOError("%s is closed" % self.__class__.__name__)
if mode is not None and self.mode not in mode:
raise IOError("bad operation for mode %r" % self.mode)
示例14: is_tarfile
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def is_tarfile(name):
"""Return True if name points to a tar archive that we
are able to handle, else return False.
"""
try:
t = open(name)
t.close()
return True
except TarError:
return False
示例15: _find_module_or_loader
# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import open [as 別名]
def _find_module_or_loader(self, submodule_name, fullname, path):
"""Acts like imp.find_module with support for path hooks.
Args:
submodule_name: The name of the submodule within its parent package.
fullname: The full name of the module to load.
path: A list containing the paths to search for the module.
Returns:
A tuple (source_file, path_name, description, loader) where:
source_file: An open file or None.
path_name: A str containing the the path to the module.
description: A description tuple like the one imp.find_module returns.
loader: A PEP 302 compatible path hook. If this is not None, then the
other elements will be None.
Raises:
ImportError: The module could not be imported.
"""
for path_entry in path + [None]:
result = self._find_path_hook(submodule_name, fullname, path_entry)
if result is not None:
break
else:
raise ImportError('No module named %s' % fullname)
if isinstance(result, tuple):
return result + (None,)
else:
return None, None, None, result.find_module(fullname)